bonsai, ifcopenshell - append IfcSurfaceStyles from other projects

And other IfcPresentationStyles, though they are not currently supported by bonsai.

Example - https://imgur.com/a/AHlvowp
This commit is contained in:
Andrej730
2024-08-15 13:57:00 +05:00
parent bd8a2736d4
commit 7597b1b29c
4 changed files with 65 additions and 7 deletions
@@ -25,7 +25,18 @@ import ifcopenshell.api.owner.settings
import ifcopenshell.util.element
import ifcopenshell.util.geolocation
import ifcopenshell.util.placement
from typing import Optional, Any, Union
from typing import Optional, Any, Union, Literal, get_args
APPENDABLE_ASSET = Literal[
"IfcTypeProduct",
"IfcProduct",
"IfcMaterial",
"IfcCostSchedule",
"IfcProfileDef",
"IfcPresentationStyle",
]
APPENDABLE_ASSET_TYPES = get_args(APPENDABLE_ASSET)
def append_asset(
@@ -154,6 +165,9 @@ class Usecase:
elif self.settings["element"].is_a("IfcProfileDef"):
self.target_class = "IfcProfileDef"
return self.append_profile_def()
elif self.settings["element"].is_a("IfcPresentationStyle"):
self.target_class = "IfcPresentationStyle"
return self.append_presentation_style()
def get_existing_element(self, element):
if element.id() in self.added_elements:
@@ -186,6 +200,10 @@ class Usecase:
self.whitelisted_inverse_attributes = {"IfcProfileDef": ["HasProperties"]}
return self.add_element(self.settings["element"])
def append_presentation_style(self):
self.whitelisted_inverse_attributes = {}
return self.add_element(self.settings["element"])
def append_type_product(self):
self.whitelisted_inverse_attributes = {
"IfcObjectDefinition": ["HasAssociations"],
@@ -408,6 +408,31 @@ class TestAppendAssetIFC2X3(test.bootstrap.IFC2X3):
)
assert np.array_equal(ifcopenshell.util.placement.get_local_placement(new.ObjectPlacement), resulting_matrix)
def test_append_a_surface_style(self):
ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
library = ifcopenshell.api.project.create_file(version=self.file.schema)
ifcopenshell.api.root.create_entity(library, ifc_class="IfcProject")
style_name = "New Style"
style = ifcopenshell.api.style.add_style(library, style_name)
shading_attrs = {"SurfaceColour": {"Name": "", "Red": 1, "Green": 1, "Blue": 1}}
ifcopenshell.api.style.add_surface_style(
library, style, ifc_class="IfcSurfaceStyleShading", attributes=shading_attrs
)
new_style = ifcopenshell.api.project.append_asset(self.file, library=library, element=style)
assert self.file.by_type("IfcSurfaceStyle") == [new_style]
assert new_style.Name == style_name
style_elements = new_style.Styles
assert len(style_elements) == 1
# Check shading style.
shading_style = style_elements[0]
assert shading_style.is_a("IfcSurfaceStyleShading")
shading_colour_info = shading_style.SurfaceColour.get_info()
del shading_colour_info["id"], shading_colour_info["type"]
assert shading_colour_info == shading_attrs["SurfaceColour"]
class TestAppendAssetIFC4(test.bootstrap.IFC4, TestAppendAssetIFC2X3):
# NOTE: breaks in IFC2X3 since IfcProfileDef doesn't have "HasProperties" inverse in ifc2x3