Skip to content
This repository was archived by the owner on Nov 24, 2024. It is now read-only.

Commit 78314b1

Browse files
committed
Forgot to commit append_asset usecase, also minor fixes
1 parent ef88b30 commit 78314b1

2 files changed

Lines changed: 61 additions & 6 deletions

File tree

src/blenderbim/blenderbim/bim/export_ifc.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ def sync_object_placements_and_deletions(self):
8080
for guid, obj in IfcStore.guid_map.items():
8181
try:
8282
self.sync_object_placement(obj)
83-
except:
84-
pass
85-
self.sync_object_container(guid, obj)
83+
self.sync_object_container(guid, obj)
84+
except ReferenceError:
85+
pass # The object is likely deleted
8686
if self.should_delete(obj):
8787
to_delete.append(guid)
8888

@@ -105,9 +105,10 @@ def sync_edited_objects(self):
105105

106106
def sync_object_placement(self, obj):
107107
blender_matrix = np.matrix(obj.matrix_world)
108-
ifc_matrix = ifcopenshell.util.placement.get_local_placement(
109-
self.file.by_id(obj.BIMObjectProperties.ifc_definition_id).ObjectPlacement
110-
)
108+
element = self.file.by_id(obj.BIMObjectProperties.ifc_definition_id)
109+
if not hasattr(element, "ObjectPlacement"):
110+
return
111+
ifc_matrix = ifcopenshell.util.placement.get_local_placement(element.ObjectPlacement)
111112
ifc_matrix[0][3] *= self.unit_scale
112113
ifc_matrix[1][3] *= self.unit_scale
113114
ifc_matrix[2][3] *= self.unit_scale
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import ifcopenshell
2+
import ifcopenshell.api
3+
4+
5+
class Usecase:
6+
def __init__(self, file, **settings):
7+
self.file = file
8+
self.settings = {"element": None}
9+
for key, value in settings.items():
10+
self.settings[key] = value
11+
12+
def execute(self):
13+
element = self.file.add(self.settings["element"])
14+
self.existing_contexts = self.file.by_type("IfcGeometricRepresentationContext")
15+
added_contexts = [e for e in self.file.traverse(element) if e.is_a("IfcGeometricRepresentationContext")]
16+
for added_context in added_contexts:
17+
equivalent_existing_context = self.get_equivalent_existing_context(added_context)
18+
if not equivalent_existing_context:
19+
equivalent_existing_context = self.create_equivalent_context(added_context)
20+
for inverse in self.file.get_inverse(added_context):
21+
ifcopenshell.util.element.replace_attribute(inverse, added_context, equivalent_existing_context)
22+
for added_context in added_contexts:
23+
if added_context.is_a() == "IfcGeometricRepresentationContext":
24+
ifcopenshell.util.element.remove_deep(self.file, added_context)
25+
return element
26+
27+
def get_equivalent_existing_context(self, added_context):
28+
for context in self.existing_contexts:
29+
if context.is_a() != added_context.is_a():
30+
continue
31+
if context.is_a("IfcGeometricRepresentationSubContext"):
32+
if (
33+
context.ContextType == added_context.ContextType
34+
and context.ContextIdentifier == added_context.ContextIdentifier
35+
and context.TargetView == added_context.TargetView
36+
):
37+
return context
38+
elif (
39+
context.ContextType == added_context.ContextType
40+
and context.ContextIdentifier == added_context.ContextIdentifier
41+
):
42+
return context
43+
44+
def create_equivalent_context(self, added_context):
45+
if added_context.is_a("IfcGeometricRepresentationSubContext"):
46+
return ifcopenshell.api.run(
47+
"context.add_context",
48+
context=added_context.ContextType,
49+
subcontext=added_context.ContextIdentifier,
50+
target_view=added_context.TargetView,
51+
)
52+
return ifcopenshell.api.run(
53+
"context.add_context", context=added_context.ContextType, subcontext=added_context.ContextIdentifier
54+
)

0 commit comments

Comments
 (0)