|
| 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