-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
480 lines (418 loc) · 21.8 KB
/
Program.cs
File metadata and controls
480 lines (418 loc) · 21.8 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
namespace SampleApp
{
using Autodesk.Data;
using Autodesk.Data.DataModels;
using Autodesk.Data.Enums;
using System.Configuration;
using System.Data;
using Client = Autodesk.Data.AECDM.Interface.IClient;
/// <summary>
/// AECDM Sample Application - Demonstrates using Autodesk Data SDK for working with AECDM data.
///
/// This application demonstrates the following workflow:
/// 1. Initialize the Autodesk Data SDK
/// 2. Query AECDM Elements (filtered, complete groups)
/// 3. Export processed data to IFC format (Download and processing of geometry data is handled by the SDK)
/// 4. Get Element geometries as granular mesh data
///
/// Prerequisites:
/// - Autodesk developer credentials (Client ID, Client Secret)
/// - Configured App.config file
/// - Network access to Autodesk APIs
/// </summary>
internal class Program
{
/// <summary>
/// Main entry point of the application. Demonstrates sample workflows for AECDM data processing using Autodesk Data SDK.
/// </summary>
static async Task Main(string[] args)
{
try
{
// Initialize the Autodesk Data SDK and required services
var client = SetupAutodeskDataSDK();
// Alternatively, use PKCE Authentication setup if preferred
// var client = SetupAutodeskDataSDKWithPKCEAuth();
var region = PromptForRegion();
var elementGroupInfo = await SelectElementGroupViaNavigationAsync(client, region);
if (elementGroupInfo == null)
{
Console.WriteLine("No element group selected. Exiting.");
return;
}
Console.WriteLine($"\nUsing Element Group: {elementGroupInfo.Name} (ID: {elementGroupInfo.Id})");
Console.Write("Do you want to include Extended Properties? (Yes/No): ");
var includeExtendedProperties = Console.ReadLine();
// IFC Conversion examples
// Filtered Elements from an Element Group
await ConvertFilteredAECDMElementsToIFCAsync(client, elementGroupInfo, includeExtendedProperties == "Yes", region);
// Complete Element Group
await ConvertCompleteAECDMElementGroupToIFCAsync(client, elementGroupInfo, includeExtendedProperties == "Yes", region);
// Mesh Geometry examples
// Filtered Elements from an Element Group
await GetMeshGeometriesForFilteredAECDMElementsAsync(client, elementGroupInfo, region);
// Complete Element Group
await GetMeshGeometriesForCompleteAECDMElementGroupAsync(client, elementGroupInfo, region);
// Advanced example with options
await GetMeshGeometriesExampleWithOptions(client, elementGroupInfo, region);
Console.WriteLine("\nSample workflows completed successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"\nApplication failed with error: {ex.Message}");
Console.WriteLine($"Stack trace: {ex.StackTrace}");
Environment.Exit(1);
}
}
/// <summary>
/// Uses the Navigation API to let the user browse Hubs → Projects → ElementGroups
/// and interactively select an ElementGroup to use in the workflow.
/// </summary>
private static async Task<ElementGroupInfo> SelectElementGroupViaNavigationAsync(Client aecdmClient, Region region)
{
Console.WriteLine("\n========================================");
Console.WriteLine("AECDM Navigation: Browse Element Groups");
Console.WriteLine("========================================\n");
// Step 1: Get all Hubs
Console.WriteLine("Fetching Hubs...");
var hubs = await aecdmClient.GetHubsAsync(region);
if (hubs.Count == 0)
{
Console.WriteLine("No hubs found. Make sure AECDM is enabled on your account.");
return null;
}
Console.WriteLine($"Found {hubs.Count} hub(s):");
for (int i = 0; i < hubs.Count; i++)
{
Console.WriteLine($" {i + 1}. {hubs[i].Name} (ID: {hubs[i].Id})");
}
var selectedHub = hubs[0];
if (hubs.Count > 1)
{
Console.Write($"Select a hub (1-{hubs.Count}) [default: 1]: ");
var hubInput = Console.ReadLine()?.Trim();
if (int.TryParse(hubInput, out int hubIndex) && hubIndex >= 1 && hubIndex <= hubs.Count)
{
selectedHub = hubs[hubIndex - 1];
}
}
Console.WriteLine($"→ Using hub: {selectedHub.Name}\n");
// Step 2: Get Projects in the selected Hub
Console.WriteLine("Fetching Projects...");
var projects = await aecdmClient.GetProjectsAsync(selectedHub, region);
if (projects.Count == 0)
{
Console.WriteLine("No projects found in this hub.");
return null;
}
Console.WriteLine($"Found {projects.Count} project(s):");
for (int i = 0; i < projects.Count; i++)
{
Console.WriteLine($" {i + 1}. {projects[i].Name} (ID: {projects[i].Id})");
}
var selectedProject = projects[0];
if (projects.Count > 1)
{
Console.Write($"Select a project (1-{projects.Count}) [default: 1]: ");
var projInput = Console.ReadLine()?.Trim();
if (int.TryParse(projInput, out int projIndex) && projIndex >= 1 && projIndex <= projects.Count)
{
selectedProject = projects[projIndex - 1];
}
}
Console.WriteLine($"→ Using project: {selectedProject.Name}\n");
// Step 3: Get ElementGroups in the selected Project
Console.WriteLine("Fetching Element Groups (Revit models)...");
var elementGroups = await aecdmClient.GetElementGroupsAsync(selectedProject, region);
if (elementGroups.Count == 0)
{
Console.WriteLine("No element groups found. Make sure Revit 2024+ models were uploaded after AECDM activation.");
return null;
}
Console.WriteLine($"Found {elementGroups.Count} element group(s):");
for (int i = 0; i < elementGroups.Count; i++)
{
Console.WriteLine($" {i + 1}. {elementGroups[i].Name} (ID: {elementGroups[i].Id})");
}
var selectedElementGroup = elementGroups[0];
if (elementGroups.Count > 1)
{
Console.Write($"Select an element group (1-{elementGroups.Count}) [default: 1]: ");
var egInput = Console.ReadLine()?.Trim();
if (int.TryParse(egInput, out int egIndex) && egIndex >= 1 && egIndex <= elementGroups.Count)
{
selectedElementGroup = elementGroups[egIndex - 1];
}
}
Console.WriteLine($"→ Selected: {selectedElementGroup.Name} (ID: {selectedElementGroup.Id})");
return selectedElementGroup;
}
#region IFC Conversion Examples
/// <summary>
/// IFC Conversion Example: Shows how to provide a region when creating an ElementGroup.
/// The region can be US (default), EMEA, or AUS.
/// </summary>
private static async Task ConvertFilteredAECDMElementsToIFCAsync(Client client, ElementGroupInfo elementGroupInfo, bool includeExtendedProperties, Region region)
{
var elementGroup = new ElementGroup(client, region); // Used to group and process AECDM elements
// Build the filter equivalent to:
// (property.name.category == Doors and 'property.name.Element Context'==Instance) or ... for Walls, Windows, Roofs
// This filter selects elements where:
// (category == "Doors" AND Element Context == "Instance")
// OR (category == "Walls" AND Element Context == "Instance")
// OR (category == "Windows" AND Element Context == "Instance")
// OR (category == "Roofs" AND Element Context == "Instance")
var filter = ElementPropertyFilter.AnyOf(
ElementPropertyFilter.AllOf(
ElementPropertyFilter.Property("category", "==", "Doors"),
ElementPropertyFilter.Property("Element Context", "==", "Instance")
),
ElementPropertyFilter.AllOf(
ElementPropertyFilter.Property("category", "==", "Walls"),
ElementPropertyFilter.Property("Element Context", "==", "Instance")
),
ElementPropertyFilter.AllOf(
ElementPropertyFilter.Property("category", "==", "Windows"),
ElementPropertyFilter.Property("Element Context", "==", "Instance")
),
ElementPropertyFilter.AllOf(
ElementPropertyFilter.Property("category", "==", "Roofs"),
ElementPropertyFilter.Property("Element Context", "==", "Instance")
)
);
Console.WriteLine("Fetching filtered AECDM elements from a sample element group...");
// Populate ElementGroup with filtered elements
await elementGroup.GetElementsAsync(elementGroupInfo, filter, includeExtendedProperties);
// Export ElementGroup to IFC format
Console.WriteLine("Converting elements to IFC format...");
var ifcFilePath = await elementGroup.ConvertToIfc("IFCFileName");
Console.WriteLine($"IFC file created at: {ifcFilePath}");
}
private static async Task ConvertCompleteAECDMElementGroupToIFCAsync(Client client, ElementGroupInfo elementGroupInfo, bool includeExtendedProperties, Region region)
{
var elementGroup = new ElementGroup(client, region); // Used to group and process AECDM elements
Console.WriteLine("Fetching all AECDM elements from a sample element group...");
// Populate ElementGroup with all elements
await elementGroup.GetElementsAsync(elementGroupInfo, includeExtendedProperties: includeExtendedProperties);
// Export ElementGroup to IFC format
Console.WriteLine("Converting elements to IFC format...");
var ifcFilePath = await elementGroup.ConvertToIfc("IFCFileName");
Console.WriteLine($"IFC file created at: {ifcFilePath}");
}
#endregion
#region Mesh Geometry Examples
private static async Task GetMeshGeometriesForFilteredAECDMElementsAsync(Client client, ElementGroupInfo elementGroupInfo, Region region)
{
var elementGroup = new ElementGroup(client, region); // Used to group and process AECDM elements
// Build the filter equivalent to:
// (property.name.category == Doors and 'property.name.Element Context'==Instance) or ... for Walls, Windows, Roofs
// This filter selects elements where:
// (category == "Doors" AND Element Context == "Instance")
// OR (category == "Walls" AND Element Context == "Instance")
// OR (category == "Windows" AND Element Context == "Instance")
// OR (category == "Roofs" AND Element Context == "Instance")
var filter = ElementPropertyFilter.AnyOf(
ElementPropertyFilter.AllOf(
ElementPropertyFilter.Property("category", "==", "Doors"),
ElementPropertyFilter.Property("Element Context", "==", "Instance")
),
ElementPropertyFilter.AllOf(
ElementPropertyFilter.Property("category", "==", "Walls"),
ElementPropertyFilter.Property("Element Context", "==", "Instance")
),
ElementPropertyFilter.AllOf(
ElementPropertyFilter.Property("category", "==", "Windows"),
ElementPropertyFilter.Property("Element Context", "==", "Instance")
),
ElementPropertyFilter.AllOf(
ElementPropertyFilter.Property("category", "==", "Roofs"),
ElementPropertyFilter.Property("Element Context", "==", "Instance")
)
);
Console.WriteLine("Fetching filtered AECDM elements from a sample element group...");
// Populate ElementGroup with filtered elements
await elementGroup.GetElementsAsync(elementGroupInfo, filter);
// Retrieve and process mesh geometry information for each element in the group
var elementGeometryMap = await elementGroup.GetElementGeometriesAsMeshAsync().ConfigureAwait(false);
foreach (var kv in elementGeometryMap)
{
var element = kv.Key;
var meshList = kv.Value;
if (meshList.Count() > 0)
{
Console.WriteLine($"Element ID: {element.Id} has {meshList.Count()} mesh geometries.");
foreach (var meshObj in meshList)
{
if (meshObj is MeshGeometry meshGeometry)
{
Console.WriteLine($" Mesh Vertices Count: {meshGeometry.Mesh?.Vertices.Count}");
}
}
}
}
}
private static async Task GetMeshGeometriesForCompleteAECDMElementGroupAsync(Client client, ElementGroupInfo elementGroupInfo, Region region)
{
var elementGroup = new ElementGroup(client, region); // Used to group and process AECDM elements
Console.WriteLine("Fetching all AECDM elements from a sample element group...");
// Populate ElementGroup with all elements
await elementGroup.GetElementsAsync(elementGroupInfo);
// Retrieve and process mesh geometry information for each element in the group
var elementGeometryMap = await elementGroup.GetElementGeometriesAsMeshAsync().ConfigureAwait(false);
foreach (var kv in elementGeometryMap)
{
var element = kv.Key;
var meshList = kv.Value;
if (meshList.Count() > 0)
{
Console.WriteLine($"Element ID: {element.Id} has {meshList.Count()} mesh geometries.");
foreach (var meshObj in meshList)
{
if (meshObj is MeshGeometry meshGeometry)
{
Console.WriteLine($" Mesh Vertices Count: {meshGeometry.Mesh?.Vertices.Count}");
}
}
}
}
}
private static async Task GetMeshGeometriesExampleWithOptions(Client client, ElementGroupInfo elementGroupInfo, Region region)
{
var elementGroup = new ElementGroup(client, region); // Used to group and process AECDM elements
Console.WriteLine("Fetching all AECDM elements from a sample element group...");
// Populate ElementGroup with all elements and capture the returned elements
var elements = await elementGroup.GetElementsAsync(elementGroupInfo);
// Retrieve and process mesh geometry information for specific elements in the group
// Specify Mesh Conversion options for Brep to Mesh conversion
var wallElements = elements.Where(e => e.Category == "Walls");
var elementGeometryMap = await elementGroup.GetElementGeometriesAsMeshAsync(wallElements, new Autodesk.Data.Geometry.BRepToMeshOptions()
{
SurfaceTolerance = 1.0,
NormalTolerance = 15,
MaxEdgeLength = 2.0,
GridAspectRatio = 0.1,
}).ConfigureAwait(false);
foreach (var kv in elementGeometryMap)
{
var element = kv.Key;
var meshList = kv.Value;
if (meshList.Count() > 0)
{
Console.WriteLine($"Element ID: {element.Id} has {meshList.Count()} mesh geometries.");
foreach (var meshObj in meshList)
{
if (meshObj is MeshGeometry meshGeometry)
{
Console.WriteLine($" Mesh Vertices Count: {meshGeometry.Mesh?.Vertices.Count}");
}
}
}
}
}
#endregion
/// <summary>
/// Prompts the user to select a region (US, EMEA, or AUS) from the console.
/// </summary>
private static Region PromptForRegion()
{
Console.WriteLine("\nSelect a region:");
Console.WriteLine(" 1. US (default)");
Console.WriteLine(" 2. EMEA");
Console.WriteLine(" 3. AUS");
Console.Write("Enter choice (1-3) [default: 1]: ");
var input = Console.ReadLine()?.Trim();
return input switch
{
"2" => Region.EMEA,
"3" => Region.AUS,
_ => Region.US
};
}
/// <summary>
/// Configures and initializes the Autodesk Data SDK using values from App.config.
/// </summary>
/// <returns>Returns a configured SDK Client instance.</returns>
private static Client SetupAutodeskDataSDK()
{
// Read configuration from App.config
var authClientID = GetRequiredConfigValue("AuthClientID");
var authClientSecret = GetRequiredConfigValue("AuthClientSecret");
var authCallBack = GetRequiredConfigValue("AuthCallBack");
var applicationName = ConfigurationManager.AppSettings["ApplicationName"] ?? "AECDMSampleApp";
var logLevel = ConfigurationManager.AppSettings["LogLevel"] ?? "Info";
// Determine application data path
var appBasePath = ConfigurationManager.AppSettings["ApplicationDataPath"];
if (string.IsNullOrEmpty(appBasePath))
{
appBasePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
}
Console.WriteLine($"Application: {applicationName}");
Console.WriteLine($"Data Path: {appBasePath}");
Console.WriteLine($"Log Level: {logLevel}");
// Configure SDK options
var sdkOptions = new SDKOptionsDefaultSetup
{
ConnectorName = applicationName,
ClientId = authClientID,
ClientSecret = authClientSecret,
CallBack = authCallBack,
HostApplicationName = applicationName,
HostApplicationVersion = "1.0.0",
ConnectorVersion = "1.0.0",
};
var clientFactory = new DataSdkClientFactory();
Client aecdmClient = clientFactory.CreateAecdmClient(sdkOptions);
return aecdmClient;
}
/// <summary>
/// Demonstrates initializing the Autodesk Data SDK using credentials for PKCE Authentiication.
/// </summary>
/// <returns>Returns a configured SDK Client instance.</returns>
private static Client SetupAutodeskDataSDKWithPKCEAuth()
{
// Read configuration from App.config
var authClientID = GetRequiredConfigValue("AuthClientID"); //PKCE Client ID
var authCallBack = GetRequiredConfigValue("AuthCallBack"); //PKCE Callback URL.No Client Secret needed for PKCE
var applicationName = ConfigurationManager.AppSettings["ApplicationName"] ?? "AECDMSampleApp";
var logLevel = ConfigurationManager.AppSettings["LogLevel"] ?? "Info";
//// Determine application data path
var appBasePath = ConfigurationManager.AppSettings["ApplicationDataPath"];
if (string.IsNullOrEmpty(appBasePath))
{
appBasePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
}
Console.WriteLine($"Application: {applicationName}");
Console.WriteLine($"Data Path: {appBasePath}");
Console.WriteLine($"Log Level: {logLevel}");
// Configure SDK options
var sdkOptions = new SDKOptionsDefaultSetup
{
ConnectorName = applicationName,
ClientId = authClientID,
CallBack = authCallBack,
HostApplicationName = applicationName,
HostApplicationVersion = "1.0.0",
ConnectorVersion = "1.0.0",
};
var clientFactory = new DataSdkClientFactory();
Client aecdmClient = clientFactory.CreateAecdmClient(sdkOptions);
return aecdmClient;
}
/// <summary>
/// Retrieves a required configuration value from App.config. Throws an exception if the value is missing or empty.
/// </summary>
/// <param name="key">The configuration key to retrieve.</param>
/// <returns>The configuration value associated with the specified key.</returns>
private static string GetRequiredConfigValue(string key)
{
var value = ConfigurationManager.AppSettings[key];
if (string.IsNullOrEmpty(value))
{
throw new InvalidOperationException(
$"Required configuration value '{key}' is missing or empty in App.config. " +
"Please check the README for setup instructions.");
}
return value;
}
}
}