Fix #2424. Bug where indirect assets (e.g. importing a type using a material) with relationships had their relationships duplicated

This commit is contained in:
Dion Moult
2023-02-24 13:01:14 +11:00
parent 5b5dd73684
commit ae09adf854
2 changed files with 95 additions and 23 deletions
@@ -106,40 +106,38 @@ class Usecase:
self.target_class = "IfcProfileDef"
return self.append_profile_def()
def get_existing_element(self):
def get_existing_element(self, element):
if element.id() in self.added_elements:
return self.added_elements[element.id()]
try:
return self.file.by_guid(self.settings["element"].GlobalId)
if element.is_a("IfcRoot"):
return self.file.by_guid(element.GlobalId)
elif element.is_a("IfcMaterial"):
return [e for e in self.file.by_type("IfcMaterial") if e.Name == element.Name][0]
elif element.is_a("IfcProfileDef"):
return [e for e in self.file.by_type("IfcProfileDef") if e.ProfileName == element.ProfileName][0]
except:
return False
def append_material(self):
if [e for e in self.file.by_type("IfcMaterial") if e.Name == self.settings["element"].Name]:
return
self.whitelisted_inverse_attributes = {"IfcMaterial": ["HasProperties", "HasRepresentation"]}
self.whitelisted_inverse_attributes = {
"IfcMaterial": ["HasExternalReferences", "HasProperties", "HasRepresentation"]
}
self.existing_contexts = self.file.by_type("IfcGeometricRepresentationContext")
element = self.add_element(self.settings["element"])
if not element.HasRepresentation:
return element
self.reuse_existing_contexts()
if element.HasRepresentation:
self.reuse_existing_contexts()
return element
def append_cost_schedule(self):
element = self.get_existing_element()
if element:
return element
self.whitelisted_inverse_attributes = {"IfcCostSchedule": ["Controls"], "IfcCostItem": ["IsNestedBy"]}
return self.add_element(self.settings["element"])
def append_profile_def(self):
if [e for e in self.file.by_type("IfcProfileDef") if e.ProfileName == self.settings["element"].ProfileName]:
return
self.whitelisted_inverse_attributes = {"IfcProfileDef": ["HasProperties"]}
return self.add_element(self.settings["element"])
def append_type_product(self):
element = self.get_existing_element()
if element:
return element
self.whitelisted_inverse_attributes = {
"IfcObjectDefinition": ["HasAssociations"],
"IfcMaterialDefinition": ["HasExternalReferences", "HasProperties", "HasRepresentation"],
@@ -151,14 +149,11 @@ class Usecase:
return element
def append_product(self):
element = self.get_existing_element()
if element:
return element
self.whitelisted_inverse_attributes = {
"IfcObjectDefinition": ["HasAssociations"],
"IfcObject": ["IsDefinedBy.IfcRelDefinesByProperties"],
"IfcElement": ["HasOpenings"],
"IfcMaterialDefinition": ["HasExternalReferences", "HasProperties"],
"IfcMaterialDefinition": ["HasExternalReferences", "HasProperties", "HasRepresentation"],
"IfcRepresentationItem": ["StyledByItem"],
}
self.existing_contexts = self.file.by_type("IfcGeometricRepresentationContext")
@@ -185,15 +180,41 @@ class Usecase:
def add_element(self, element):
if element.id() == 0:
return
if element.id() in self.added_elements:
return self.added_elements[element.id()]
existing_element = self.get_existing_element(element)
if existing_element:
return existing_element
# if element.id() in self.added_elements:
# return self.added_elements[element.id()]
new = self.file.add(element)
self.added_elements[element.id()] = new
for subelement in self.settings["library"].traverse(element):
self.check_inverses(element)
for subelement in self.settings["library"].traverse(element)[1:]:
existing_element = self.get_existing_element(subelement)
if existing_element:
self.added_elements[subelement.id()] = existing_element
if not self.has_whitelisted_inverses(existing_element):
self.check_inverses(subelement)
break
self.added_elements[subelement.id()] = self.file.add(subelement)
self.check_inverses(subelement)
return new
def has_whitelisted_inverses(self, element):
for source_class, attributes in self.whitelisted_inverse_attributes.items():
if not element.is_a(source_class):
continue
for attribute in attributes:
attribute_class = None
if "." in attribute:
attribute, attribute_class = attribute.split(".")
value = getattr(element, attribute, [])
if attribute_class:
for subvalue in value:
if subvalue.is_a(attribute_class):
return True
elif value:
return True
def check_inverses(self, element):
for source_class, attributes in self.whitelisted_inverse_attributes.items():
if not element.is_a(source_class):