This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
MigrationToBim360.cs
267 lines (233 loc) · 10.6 KB
/
MigrationToBim360.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//
// (C) Copyright 2003-2020 by Autodesk, Inc. All rights reserved.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM 'AS IS' AND WITH ALL ITS FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using System.Windows;
using Autodesk.Revit.DB;
using Revit.SDK.Samples.CloudAPISample.CS.View;
using System.Threading.Tasks;
using Revit.SDK.Samples.CloudAPISample.CS.Forge;
namespace Revit.SDK.Samples.CloudAPISample.CS.Migration
{
/// <summary>
/// This is a sample shows how to migrate A360 Teams project to BIM360 Docs
/// with Revit Cloud API
/// </summary>
public class MigrationToBim360 : SampleContext
{
/// <summary>
/// File name to store models' guid info.
/// </summary>
public const string FModelsGuid = "modelsguid.json";
/// <summary>
/// File name to store linked models' info
/// </summary>
public const string FLinksInfo = "linkinfo.json";
/// <summary>
/// Default constructor
/// </summary>
public MigrationToBim360()
{
Model = new UIMigrationViewModel();
View = new ViewMigrationToBim360(this);
}
/// <summary>
/// Model for migration case
/// </summary>
public IMigrationModel Model { get; }
/// <inheritdoc />
public override void Terminate()
{
View = null;
}
private IEnumerable<RevitLinkType> GetLinkInstances(Document document)
{
var links = new FilteredElementCollector(document);
var classFilter = new ElementClassFilter(typeof(RevitLinkType));
links.WherePasses(classFilter);
foreach (var link in links)
if (link is RevitLinkType revitLinkType)
yield return revitLinkType;
}
private FolderLocation GetTargetFolderUrn(IList<MigrationRule> rules, string directory, string model)
{
foreach (var rule in rules)
{
var models = Directory.GetFiles(directory, rule.Pattern, SearchOption.TopDirectoryOnly);
if (models.Contains(model))
return rule.Target;
}
// If the given model can not match any pattern in rules, use root folder by default.
return Model.AvailableFolders.Last();
}
/// <summary>
/// User coroutine to update UI during processing.
/// </summary>
public IEnumerator Upload(string directory, Guid accountId, Guid projectId,
ObservableCollection<MigrationRule> modelRules)
{
var view = (ViewMigrationToBim360)View;
if (!Directory.Exists(directory))
{
view.UpdateUploadingProgress("Directory doesn't exist.", 0);
yield break;
}
view.UpdateUploadingProgress("Ready to start.", 2);
var models = Directory.GetFiles(directory, "*.rvt", SearchOption.AllDirectories);
var count = 0;
var ops = new OpenOptions
{
OpenForeignOption = OpenForeignOption.DoNotOpen,
DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets
};
// Open all documents and analyze if document has links.
// If so, save the relationship info into "mapLocalModelPathToLinksName"
var mapLocalModelPathToLinksName = new Dictionary<string, List<string>>();
foreach (var model in models)
{
var name = Path.GetFileName(model);
var progress = count++ * 100 / models.Length;
var modelpath = ModelPathUtils.ConvertUserVisiblePathToModelPath(model);
view.UpdateUploadingProgress($"Analyzing {name}", progress);
yield return null;
try
{
var doc = Application.Application.OpenDocumentFile(modelpath, ops);
var links = new List<string>();
foreach (var linkInstance in GetLinkInstances(doc)) links.Add(linkInstance.Name);
if (links.Count > 0) mapLocalModelPathToLinksName[model] = links;
doc.Close(false);
}
catch (Exception e)
{
var msg = $"Failed to analyze {name} - {e.Message}";
view.UpdateUploadingProgress(msg, progress);
MessageBox.Show(msg);
yield break;
}
yield return null;
}
// All link info should be dump to local file in case something wrong happens during uploading process
var jsonString = JsonConvert.SerializeObject(mapLocalModelPathToLinksName);
File.WriteAllText(FLinksInfo, jsonString);
view.UpdateUploadingProgress("Analyzing finished", 100);
// Begin Uploading
count = 0;
var mapModelsNameToGuid = new Dictionary<string, string>();
foreach (var model in models)
{
var name = Path.GetFileName(model);
var progress = count++ * 100 / models.Length;
var modelpath = ModelPathUtils.ConvertUserVisiblePathToModelPath(model);
view.UpdateUploadingProgress($"Uploading {name}", progress);
yield return null;
try
{
var doc = Application.Application.OpenDocumentFile(modelpath, ops);
//var folderUrn = GetTargetFolderUrn(modelRules, directory, model)?.Urn;
var folderUrn = modelRules.FirstOrDefault().Target.Urn;
doc.SaveAsCloudModel(accountId, projectId, folderUrn, $"Migrated_{Path.GetFileName(model)}");
var modelPath = doc.GetCloudModelPath();
mapModelsNameToGuid.Add(name, $"{modelPath.GetProjectGUID()},{modelPath.GetModelGUID()}");
doc.Close(false);
}
catch (Exception e)
{
var msg = $"Failed to upload {name} - {e.Message}";
view.UpdateUploadingProgress(msg, progress);
MessageBox.Show(msg);
yield break;
}
yield return null;
}
jsonString = JsonConvert.SerializeObject(mapModelsNameToGuid);
File.WriteAllText(FModelsGuid, jsonString);
view.UpdateUploadingProgress("Uploading finished", 100);
}
/// <summary>
/// Try to open each cloud model and reload if they have links,
/// making that point to cloud path.
/// </summary>
/// <param name="directory">Transit directory where cloud models exist</param>
/// <param name="projectId">not used</param>
/// <returns></returns>
public IEnumerator ReloadLinks(string directory, Guid projectId)
{
var view = (ViewMigrationToBim360)View;
if (!Directory.Exists(directory))
{
view.UpdateReloadingProgress("Directory doesn't exist.", 0);
yield break;
}
view.UpdateReloadingProgress("Ready to start.", 2);
var ops = new OpenOptions();
var transOp = new TransactWithCentralOptions();
var swcOptions = new SynchronizeWithCentralOptions();
var count = 0;
// Read mapping info
var jsonString = File.ReadAllText(FLinksInfo);
var mapLocalModelPathToLinksName = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(jsonString);
jsonString = File.ReadAllText(FModelsGuid);
var mapModelsNameToGuid = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);
// Try to open each cloud model and reload if they have links, making that point to cloud path.
foreach (var kvp in mapLocalModelPathToLinksName)
{
var localPath = kvp.Key;
var name = Path.GetFileName(localPath);
var guids = mapModelsNameToGuid[name].Split(',');
var hostModelPath =
ModelPathUtils.ConvertCloudGUIDsToCloudPath("US", Guid.Parse(guids[0]), Guid.Parse(guids[1]));
var progress = count++ * 100 / mapLocalModelPathToLinksName.Count();
view.UpdateReloadingProgress($"Reloading {name}", progress);
yield return null;
try
{
var doc = Application.Application.OpenDocumentFile(hostModelPath, ops,
new DefaultOpenFromCloudCallback());
foreach (var linkInstance in GetLinkInstances(doc))
if (mapModelsNameToGuid.TryGetValue(linkInstance.Name, out var sLinkedGuids))
{
guids = mapModelsNameToGuid[linkInstance.Name].Split(',');
var linkModelPath =
ModelPathUtils.ConvertCloudGUIDsToCloudPath("US", Guid.Parse(guids[0]),
Guid.Parse(guids[1]));
linkInstance.LoadFrom(linkModelPath, new WorksetConfiguration());
}
doc.SynchronizeWithCentral(transOp, swcOptions);
doc.Close(false);
}
catch (Exception e)
{
var msg = $"Failed to reload {name} - {e.Message}";
view.UpdateUploadingProgress(msg, progress);
MessageBox.Show(msg);
yield break;
}
}
view.UpdateReloadingProgress("Reloading finished", 100);
}
}
}