ifc2x3 tests

This commit is contained in:
Andrej730
2024-05-10 11:33:33 +05:00
parent c50d1ea2fe
commit 1e630ede08
41 changed files with 315 additions and 94 deletions
@@ -121,27 +121,36 @@ def add_pset(file: ifcopenshell.file, product: ifcopenshell.entity_instance, nam
has_property_sets.append(pset)
settings["product"].HasPropertySets = has_property_sets
return pset
elif settings["product"].is_a("IfcMaterialDefinition"):
for definition in settings["product"].HasProperties or []:
if definition.Name == settings["name"]:
# in IFC2X3 IfcMaterialDefinition not yet existed
elif settings["product"].is_a("IfcMaterialDefinition") or settings["product"].is_a("IfcMaterial"):
if file.schema == "IFC2X3":
ifc_class = "IfcExtendedMaterialProperties"
definitions = (d for d in file.by_type("IfcMaterialProperties") if d.Material == settings["product"])
else:
ifc_class = "IfcMaterialProperties"
definitions = settings["product"].HasProperties
for definition in definitions:
# In IFC2X3 not all IfcMaterialProperties has Name
if getattr(definition, "Name") == settings["name"]:
return definition
return file.create_entity(
"IfcMaterialProperties",
ifc_class,
**{
"Name": settings["name"],
"Material": settings["product"],
}
)
elif settings["product"].is_a("IfcProfileDef"):
for definition in settings["product"].HasProperties or []:
if definition.Name == settings["name"]:
return definition
# in IFC2X3 IfcProfileProperties doesn't have Name and we cannot identify them
if file.schema != "IFC2X3":
for definition in settings["product"].HasProperties or []:
if definition.Name == settings["name"]:
return definition
return file.create_entity(
"IfcProfileProperties",
**{
"Name": settings["name"],
"ProfileDefinition": settings["product"],
}
)
kwargs = {}
kwargs["ProfileDefinition"] = settings["product"]
if file.schema != "IFC2X3":
kwargs["Name"] = settings["name"]
return file.create_entity("IfcProfileProperties", **kwargs)
@@ -97,7 +97,7 @@ def add_resource(
related_objects=[resource],
relating_object=settings["parent_resource"],
)
else:
elif file.schema != "IFC2X3":
context = file.by_type("IfcContext")[0]
ifcopenshell.api.run(
"project.assign_declaration",
@@ -175,6 +175,6 @@ def add_task(
related_objects=[task],
relating_object=settings["parent_task"],
)
if settings["parent_task"].Identification:
if file.schema != "IFC2X3" and settings["parent_task"].Identification:
task.Identification = settings["parent_task"].Identification + "." + str(len(rel.RelatedObjects))
return task
@@ -90,11 +90,17 @@ def add_work_schedule(
predefined_type=settings["predefined_type"],
name=settings["name"],
)
work_schedule.CreationDate = ifcopenshell.util.date.datetime2ifc(datetime.now(), "IfcDateTime")
if file.schema == "IFC2X3":
work_schedule.CreationDate = createIfcDateAndTime(file, datetime.now())
else:
work_schedule.CreationDate = ifcopenshell.util.date.datetime2ifc(datetime.now(), "IfcDateTime")
user = ifcopenshell.api.owner.settings.get_user(file)
if user:
work_schedule.Creators = [user.ThePerson]
work_schedule.StartTime = ifcopenshell.util.date.datetime2ifc(settings["start_time"], "IfcDateTime")
if file.schema == "IFC2X3":
work_schedule.StartTime = createIfcDateAndTime(file, settings["start_time"])
else:
work_schedule.StartTime = ifcopenshell.util.date.datetime2ifc(settings["start_time"], "IfcDateTime")
if settings["object_type"]:
work_schedule.ObjectType = settings["object_type"]
if settings["work_plan"]:
@@ -106,7 +112,7 @@ def add_work_schedule(
"relating_object": settings["work_plan"],
}
)
else:
elif file.schema != "IFC2X3":
# TODO: this is an ambiguity by buildingSMART
# See https://forums.buildingsmart.org/t/is-the-ifcworkschedule-project-declaration-mutually-exclusive-to-aggregation-within-a-relating-ifcworkplan/3510
context = file.by_type("IfcContext")[0]
@@ -117,3 +123,12 @@ def add_work_schedule(
relating_context=context,
)
return work_schedule
def createIfcDateAndTime(file: ifcopenshell.file, dt: datetime):
ifc_dt = file.create_entity("IfcDateAndTime")
ifc_dt.DateComponent = file.create_entity(
"IfcCalendarDate", **ifcopenshell.util.date.datetime2ifc(dt, "IfcCalendarDate")
)
ifc_dt.TimeComponent = file.create_entity("IfcLocalTime", **ifcopenshell.util.date.datetime2ifc(dt, "IfcLocalTime"))
return ifc_dt
@@ -177,11 +177,31 @@ class Usecase:
representations.append(self.create_styled_representation())
definition_representation.Representations = representations
def has_proposed_style(self, styled_item):
return any(s == self.settings["style"] for s in styled_item.Styles)
def has_proposed_style(self, styled_item: ifcopenshell.entity_instance) -> bool:
style = self.settings["style"]
styles = styled_item.Styles
if style in styles:
return True
if self.file.schema != "IFC4X3":
# IfcPresentationStyleAssignment is removed in IFC4X3
for s in styles:
if s.is_a("IfcPresentationStyleAssignment"):
if style in s.Styles:
return True
return False
def has_same_style_type(self, styled_item):
return any(s.is_a() == self.settings["style"].is_a() for s in styled_item.Styles)
def has_same_style_type(self, styled_item: ifcopenshell.entity_instance) -> bool:
style = self.settings["style"]
style_class = style.is_a()
for s in styled_item.Styles:
s_class = s.is_a()
if s_class == style_class:
return True
elif s_class == "IfcPresentationStyleAssignment":
for ss in s.Styles:
if ss.is_a() == style_class:
return True
return False
def create_new_definition_representation(self):
representation = self.create_styled_representation()
@@ -214,6 +234,14 @@ class Usecase:
return self.file.create_entity(
"IfcStyledItem", **{"Styles": [self.style], "Name": self.settings["style"].Name}
)
reuse_item.Styles = (self.style,)
# IfcPresentationStyleAssignment we created end up not being used
# TODO: do not create IfcPresentationStyleAssignment in the first place
# as it might get removed
if reuse_item.is_a("IfcPresentationStyleAssignment") and self.style.is_a("IfcPresentationStyleAssignment"):
self.file.remove(self.style)
self.style = reuse_item
reuse_item.Styles = (self.settings["style"],)
reuse_item.Name = self.settings["style"].Name
return reuse_item
@@ -45,22 +45,25 @@ def remove_surface_style(file: ifcopenshell.file, style: ifcopenshell.entity_ins
# Remove the shading item
ifcopenshell.api.run("style.remove_surface_style", model, style=shading)
"""
settings = {"style": style}
to_delete = set()
if settings["style"].is_a("IfcSurfaceStyleWithTextures"):
for texture in settings["style"].Textures or []:
if texture.IsMappedBy:
for coordinate in texture.IsMappedBy:
to_delete.add(coordinate)
else:
to_delete.add(texture)
if style.is_a("IfcSurfaceStyleWithTextures"):
textures = style.Textures
if file.schema == "IFC2X3":
to_delete.update(textures)
else:
for texture in textures:
if coords := texture.IsMappedBy:
for coordinate in coords:
to_delete.add(coordinate)
else:
to_delete.add(texture)
for attribute in settings["style"]:
for attribute in style:
if isinstance(attribute, ifcopenshell.entity_instance) and attribute.id():
to_delete.add(attribute)
file.remove(settings["style"])
file.remove(style)
for element in to_delete:
ifcopenshell.util.element.remove_deep2(file, element)
@@ -65,7 +65,14 @@ def unassign_material_style(
for item in representation.Items:
if not item.is_a("IfcStyledItem"):
continue
styles = [s for s in item.Styles if s != settings["style"]]
styles = []
for s in item.Styles:
if s == settings["style"]:
continue
if s.is_a("IfcPresentationStyleAssignment"):
if s.Styles == (settings["style"],):
continue
styles.append(s)
if not styles:
file.remove(item)
elif len(styles) != len(item.Styles):
@@ -158,6 +158,7 @@ def get_psets(
if qtos_only and not definition.is_a("IfcElementQuantity"):
continue
psets[definition.Name] = get_property_definition(definition, verbose=verbose)
# NOTE: doesn't account for IFC2X3 missing HasProperties
elif element.is_a("IfcMaterialDefinition") or element.is_a("IfcProfileDef"):
for definition in getattr(element, "HasProperties", None) or []:
if qtos_only: