-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsample.py
More file actions
649 lines (517 loc) · 18.5 KB
/
Copy pathsample.py
File metadata and controls
649 lines (517 loc) · 18.5 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
from pathlib import Path
from random import randint
import time
import os
import json
from typing import Callable
import requests
from QonicApi import QonicApi
import printMethods
from QonicApiLib import ProductFilter
def wait_for_operation(api: QonicApi, operation_id: str):
while True:
operation = api.get_operation(operation_id)
status = operation["status"]
print(f"Operation {operation_id} status: {status}")
if status in ("Ready", "Failed"):
return operation
time.sleep(2)
def run_product_modification(api: QonicApi, project_id: str, model_id: str, changes: dict) -> bool:
print("Starting modification session")
api.start_session(project_id, model_id)
try:
errors = api.modify_products(project_id, model_id, changes)
if errors:
print(errors)
return False
finally:
print("Closing modification session")
api.end_session(project_id, model_id)
print("Modification is done")
print()
return True
def handle_model_queries(api: QonicApi, project_id: str):
models = api.list_models(project_id)
print("your models:")
for model in models:
print(f"{model['id']} - {model['name']}")
print()
model_id = input("Enter a model id: ")
print()
available_fields = api.get_available_product_fields(project_id, model_id)
print("fields: " + ', '.join(available_fields[:10]) + " ...")
print("Querying the Guid, Class, Name and FireRating fields, filtered on Class Beam...")
print()
fields = ["Guid", "Class", "Name", "FireRating"]
filters: list[ProductFilter] = [{"property": "Class", "value": "Beam", "operator": "Contains"}]
properties = api.query_products(project_id, model_id, fields=fields, filters=filters)
print()
for row in properties:
print(f"{row}")
print()
if not properties:
print("No beams to add a fire rating to")
return
current_fire_rating = properties[0]["FireRating"]
if current_fire_rating["PropertySet"] is not None or current_fire_rating["Value"] is not None:
print("Beam already has a fire rating a change modification needed")
return
fire_rating = f"F{randint(1, 200)}"
print(f"Adding FireRating to first row {fire_rating}")
add_changes = {
"add": {
"FireRating": {
properties[0]["Guid"]: {
"PropertySet": "Pset_BeamCommon",
"Value": fire_rating,
}
}
}
}
if not run_product_modification(api, project_id, model_id, add_changes):
return
print("Querying data again")
properties = api.query_products(project_id, model_id, fields=fields, filters=filters)
print("Showing only the first row:")
print(properties[0])
print()
print("Starting modification session to reset value")
update_changes = {
"update": {
"FireRating": {
properties[0]["Guid"]: None
}
}
}
if not run_product_modification(api, project_id, model_id, update_changes):
return
print("Querying data again")
properties = api.query_products(project_id, model_id, fields=fields, filters=filters)
print("Showing only the first row:")
print(properties[0])
print()
print("Starting modification to delete property")
delete_changes = {
"delete": {
"FireRating": {
properties[0]["Guid"]: None
}
}
}
if not run_product_modification(api, project_id, model_id, delete_changes):
return
print("Querying data again")
properties = api.query_products(project_id, model_id, fields=fields, filters=filters)
if properties:
print("Showing only the first row:")
print(properties[0])
def handle_codifications(api: QonicApi, project_id: str):
print("Showing first codification library")
libraries = api.list_codification_libraries(project_id)
if not libraries:
print("No codification libraries found")
return
printMethods.printCodificationLibrary(libraries[0])
print("Adding new TestCodificationLibrary")
data = {
"name": "CodificationTestLibrary",
"description": "codification library for testing",
}
new_library = api.create_codification_library(project_id, data)
library_id = new_library["guid"]
new_code_to_add = {
"name": "NewRootCode",
"identification": "0",
"description": "NewCodeForTesting",
"parentId": None,
}
print("Adding new root code the library")
new_code = api.create_classification_code(
project_id,
library_id,
new_code_to_add,
)
print("updating the name of the new code from NewRootcode to updatedName")
updated_code = {
"name": "updatedName"
}
api.update_classification_code(
project_id,
library_id,
new_code["guid"],
updated_code,
)
print("Show new library")
newly_added_library = api.get_codification_library(project_id, library_id)
printMethods.printCodificationLibrary(newly_added_library)
print("Delete newly added code")
api.delete_classification_code(
project_id,
library_id,
new_code["guid"],
)
print("Delete new library")
api.delete_codification_library(
project_id,
library_id,
)
def handle_materials(api: QonicApi, project_id: str):
print("Showing first material library")
materials = api.get_material_overview(project_id)
if not materials.get("materialProperties"):
print("No material libraries found")
return
printMethods.printMaterials(materials["materialProperties"][0])
new_library = api.create_material_library(
project_id,
{"Name": "TestMaterial"},
)
library_id = new_library["guid"]
new_material = {
"name": "Concrete",
"description": "test",
"color": "#785B3DFF",
}
print("Adding new material to the library")
new_material = api.create_material(
project_id,
library_id,
new_material,
)
updated_material = {
"name": "Concrete",
"description": "concrete test material",
"color": "#49C73EFF",
}
new_material_id = new_material["guid"]
api.update_material(
project_id,
library_id,
new_material_id,
updated_material,
)
materials = api.get_material_overview(project_id)
printMethods.printMaterials(
[lib for lib in materials["materialProperties"] if lib["guid"] == library_id][0]
)
print("Delete material again")
api.delete_material(
project_id,
library_id,
new_material_id,
)
def handle_locations(api: QonicApi, project_id: str):
print("Get all locations")
locations = api.get_locations(project_id)
for location in locations:
printMethods.printLocations(location)
print("-----------------------------------------")
print("Add new site location")
new_site = {
"name": "NewLocation",
"type": "Site",
"parentGuid": None,
}
site = api.create_location(
project_id,
new_site,
)
guid_site = [prop for prop in site["properties"] if prop["name"] == "Guid"][0]["value"]
print("Add new building")
new_building = {
"name": "NewBuilding",
"type": "Building",
"parentGuid": guid_site,
}
api.create_location(
project_id,
new_building,
)
print("Update site name to newSite")
updated_site = {
"name": "newSite"
}
api.update_location(
project_id,
guid_site,
updated_site,
)
print("Show added locations")
locations = api.get_locations(project_id)
for location in locations:
if [prop for prop in site["properties"] if prop["name"] == "Guid"][0]["value"] == guid_site:
printMethods.printLocations(location)
print("Delete added locations")
api.delete_location(
project_id,
guid_site,
)
def handle_custom_properties(api: QonicApi, project_id: str):
print("Showing all custom properties")
custom_properties = api.get_custom_properties(project_id)
printMethods.printCustomProperties(custom_properties)
print("Create new propertyset: TestSet")
property_set_to_add = {
"Name": "TestSet",
"EntityTypes": [{"Value": "IfcWall"}, {"Value": "IfcBeam"}, {"Value": "IfcSlab"}],
}
added_set = api.create_property_set(
project_id,
property_set_to_add,
)
print("Add property to set: TestProperty")
property_to_add = {
"Name": "TestProperty",
"DataType": "String",
}
api.add_property_definition(
project_id,
added_set["id"],
property_to_add,
)
print("Choose model to add this property to")
models = api.list_models(project_id)
print("your models:")
for model in models:
print(f"{model['id']} - {model['name']}")
print()
model_id = input("Enter a model id: ")
print()
print("Querying the Guid, Class, Name fields, filtered on Class Wall...")
print()
fields = ["Guid", "Class", "Name"]
filters: list[ProductFilter] = [{"property": "Class", "value": "Wall", "operator": "Contains"}]
properties = api.query_products(project_id, model_id, fields=fields, filters=filters)
print()
for row in properties:
print(f"{row}")
print("Adding test property to first wall")
print(" Starting modification session")
if not properties:
print("No walls found")
return
api.start_session(project_id, model_id)
try:
changes = {
"add": {
"TestProperty": {
properties[0]["Guid"]: {
"PropertySet": "TestSet",
"Value": "testValue",
}
}
}
}
errors = api.modify_products(project_id, model_id, changes)
if errors:
print(str(errors))
return
finally:
print("Closing modification session")
api.end_session(project_id, model_id)
def handle_delete_product(api: QonicApi, project_id: str):
models = api.list_models(project_id)
print("your models:")
for model in models:
print(f"{model['id']} - {model['name']}")
print()
model_id = input("Enter a model id: ")
print()
available_fields = api.get_available_product_fields(project_id, model_id)
print("fields: " + ', '.join(available_fields[:10]) + " ...")
print()
print("Querying the Guid, Class, Name and FireRating fields, filtered on Class Beam...")
print()
fields = ["Guid", "Class", "Name"]
filters: list[ProductFilter] = [{"property": "Class", "value": "Wall", "operator": "Contains"}]
properties = api.query_products(project_id, model_id, fields=fields, filters=filters)
initial_amount_of_walls = len(properties)
print(f"Found {initial_amount_of_walls} walls")
print("Deleting the first wall")
if not properties:
print("No walls found")
return
guid = properties[0]["Guid"]
api.start_session(project_id, model_id)
try:
api.delete_product(project_id, model_id, guid)
finally:
print("Closing modification session")
api.end_session(project_id, model_id)
print("Modification is done")
print()
print("Querying data again")
properties_after = api.query_products(project_id, model_id, fields=fields, filters=filters)
amount_after_delete = len(properties_after)
print(f"Found {amount_after_delete} walls")
def handle_create_model(api: QonicApi, project_id: str):
print("Requesting upload URL")
upload_url = api.get_upload_url()
print("Upload URL received")
local_path = input("Enter the path to the local model file to upload: ").strip()
while not local_path or not os.path.isfile(local_path):
if not local_path:
print("No file path provided")
if not os.path.isfile(local_path):
print("File does not exist")
local_path = input("Enter the path to the local model file to upload: ").strip()
upload_file_name = os.path.basename(local_path)
print(f"Uploading {upload_file_name} to storage")
with open(local_path, "rb") as f:
resp = requests.put(upload_url, data=f)
resp.raise_for_status()
print("Upload finished")
model_name = upload_file_name if "." not in upload_file_name else upload_file_name.split(".")[0]
result = api.create_model(
project_id,
model_name=model_name,
upload_url=upload_url,
upload_file_name=upload_file_name,
tags=["Architecture"],
)
print(f"Created model with id {result['modelId']}")
print(f"Import operation id {result['id']} with status {result['status']}")
operation = wait_for_operation(api, result["id"])
print(f"Final import status: {operation['status']}")
def _resolve_output_path(user_input: str, model_id: str) -> Path:
p = Path(user_input).expanduser()
as_str = str(user_input)
if as_str.endswith(("/", "\\")) or (p.exists() and p.is_dir()):
p = p / f"{model_id}.ifc"
if p.suffix == "":
p = p / f"{model_id}.ifc"
return p
def handle_export_model(api: "QonicApi", project_id: str) -> None:
models = api.list_models(project_id)
if not models:
print("No models found")
return
print("your models:")
for model in models:
print(f"{model['id']} - {model['name']}")
print()
model_id = input("Enter a model id: ").strip()
if not model_id:
print("No model id provided")
return
output_raw = input(
"Enter a directory or path to save the IFC file (e.g. ./exports/ or ./exports/export.ifc): "
).strip()
if not output_raw:
print("No output path provided")
return
output_path = _resolve_output_path(output_raw, model_id)
try:
output_path.parent.mkdir(parents=True, exist_ok=True)
except Exception as e:
print(f"Could not create directory {output_path.parent}: {e}")
return
if output_path.exists():
print(f"Output file {output_path} already exists, please remove it first")
return
print("Starting IFC export")
operation = api.start_export_ifc(project_id, model_id)
operation_id = operation["id"]
print(f"Export operation id {operation_id}")
final_operation = wait_for_operation(api, operation_id)
if final_operation.get("status") != "Ready":
print("Export operation did not complete successfully")
return
result_url = api.get_export_ifc_result_url(project_id, model_id, operation_id)
print(f"Downloading IFC file to {output_path}")
resp = requests.get(result_url, stream=True)
resp.raise_for_status()
with open(output_path, "wb") as f:
for chunk in resp.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
print(f"IFC file saved to {output_path}")
def handle_calculate_quantities(api: QonicApi, project_id: str):
models = api.list_models(project_id)
if not models:
print("No models found")
return
print("your models:")
for model in models:
print(f"{model['id']} - {model['name']}")
print()
model_id = input("Enter a model id: ")
print()
print("Starting quantity calculation of 'Length' and 'GrossArea' for all products with class 'Wall'")
filters: list[ProductFilter] = [{"property": "Class", "value": "Wall", "operator": "Contains"}]
operation = api.calculate_quantities(
project_id,
model_id,
calculators=["Length", "GrossArea"],
filters=filters
)
operation_id = operation["id"]
print(f"Quantities operation id {operation_id}")
final_operation = wait_for_operation(api, operation_id)
if final_operation["status"] != "Ready":
print("Quantities operation did not complete successfully")
return
result_url = api.get_quantities_result_url(project_id, model_id, operation_id)
print("Downloading quantities result")
resp = requests.get(result_url)
resp.raise_for_status()
try:
data = resp.json()
except ValueError:
print("Result is not valid JSON, raw response below:")
print(resp.text)
return
print("Quantities result:")
print(json.dumps(data, indent=2))
def _choose_project(projects: list[dict]) -> str | None:
if not projects:
print("No projects found.")
return None
print("your projects:")
for p in projects:
print(f"{p['id']} - {p['name']}")
print()
valid_ids = {str(p["id"]) for p in projects}
while True:
pid = input("Enter a project id (or blank to exit): ").strip()
if not pid:
return None
if pid in valid_ids:
return pid
print("Invalid project id. Please pick one from the list.\n")
def _choose_action(actions: dict[str, tuple[str, Callable[[], None]]]) -> str:
print("Choose what to do next:")
for key, (label, _) in actions.items():
print(f"{key}: {label}")
return input(f"Enter choice ({', '.join(actions.keys())}): ").strip()
def main() -> None:
api = QonicApi()
api.authorize()
project_id = _choose_project(api.list_projects())
if not project_id:
return
actions: dict[str, tuple[str, Callable[[], None]]] = {
"1": ("Model Queries", lambda: handle_model_queries(api, project_id)),
"2": ("Codifications", lambda: handle_codifications(api, project_id)),
"3": ("Materials", lambda: handle_materials(api, project_id)),
"4": ("Locations", lambda: handle_locations(api, project_id)),
"5": ("CustomProperties", lambda: handle_custom_properties(api, project_id)),
"6": ("Delete Product", lambda: handle_delete_product(api, project_id)),
"7": ("Create model", lambda: handle_create_model(api, project_id)),
"8": ("Export model", lambda: handle_export_model(api, project_id)),
"9": ("Calculate quantities", lambda: handle_calculate_quantities(api, project_id)),
"10": ("Exit", lambda: exit()),
}
try:
while True:
print()
choice = _choose_action(actions)
if choice not in actions:
print("Please choose a valid option.\n")
continue
_, action = actions[choice]
action()
except (KeyboardInterrupt, EOFError):
print("\nExiting...")
if __name__ == "__main__":
main()