mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-24 17:29:57 +00:00
Fix #2726.
This commit is contained in:
@@ -49,22 +49,40 @@ def get_subtypes(entity):
|
|||||||
|
|
||||||
|
|
||||||
def reassign_class(ifc_file, element, new_class):
|
def reassign_class(ifc_file, element, new_class):
|
||||||
|
schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name(ifc_file.schema)
|
||||||
try:
|
try:
|
||||||
new_element = ifcopenshell.create_entity(new_class)
|
declaration = schema.declaration_by_name(new_class)
|
||||||
new_element.GlobalId = element.GlobalId
|
except:
|
||||||
|
print(f"Class of {element} could not be changed to {new_class} as the class does not exist")
|
||||||
|
|
||||||
|
info = element.get_info()
|
||||||
|
|
||||||
|
new_attributes = {}
|
||||||
|
for attribute in declaration.all_attributes():
|
||||||
|
name = attribute.name()
|
||||||
|
old_attribute = info.get(name, None)
|
||||||
|
if old_attribute:
|
||||||
|
new_attributes[name] = old_attribute
|
||||||
|
|
||||||
|
inverse_pairs = ifc_file.get_inverse(element, allow_duplicate=True, with_attribute_indices=True)
|
||||||
|
ifc_file.remove(element)
|
||||||
|
|
||||||
|
try:
|
||||||
|
new_element = ifc_file.create_entity(new_class, id=info["id"], **new_attributes)
|
||||||
except:
|
except:
|
||||||
print(f"Class of {element} could not be changed to {new_class}")
|
print(f"Class of {element} could not be changed to {new_class}")
|
||||||
return element
|
old_class = info.pop("type")
|
||||||
new_attributes = [new_element.attribute_name(i) for i, attribute in enumerate(new_element)]
|
return ifc_file.create_entity(old_class, **info)
|
||||||
for i, attribute in enumerate(element):
|
|
||||||
try:
|
for inverse_pair in inverse_pairs:
|
||||||
new_element[new_attributes.index(element.attribute_name(i))] = attribute
|
inverse, index = inverse_pair
|
||||||
except:
|
if inverse[index] is None:
|
||||||
continue
|
inverse[index] = new_element
|
||||||
for inverse in ifc_file.get_inverse(element):
|
elif isinstance(inverse[index], tuple):
|
||||||
ifcopenshell.util.element.replace_attribute(inverse, element, new_element)
|
item = list(inverse[index])
|
||||||
ifc_file.remove(element)
|
item.append(new_element)
|
||||||
ifc_file.add(new_element, element.id())
|
inverse[index] = item
|
||||||
|
|
||||||
return new_element
|
return new_element
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class TestReassignClass(test.bootstrap.IFC4):
|
|||||||
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
|
||||||
new = ifcopenshell.api.run("root.reassign_class", self.file, product=element, ifc_class="IfcSlab")
|
new = ifcopenshell.api.run("root.reassign_class", self.file, product=element, ifc_class="IfcSlab")
|
||||||
assert len([e for e in self.file]) == 1
|
assert len([e for e in self.file]) == 1
|
||||||
assert new.id() == 2
|
assert new.id() == 1
|
||||||
assert new.is_a("IfcSlab")
|
assert new.is_a("IfcSlab")
|
||||||
|
|
||||||
def test_reassigning_a_predefined_type(self):
|
def test_reassigning_a_predefined_type(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user