Fix possibility of adding new invalid surface styles

1) Add default values for new IfcSurfaceStyleLighting so they won't appear invalid.
2) Temporarily disable starting surface style with a texture style since it requires additional texture UI to be exposed or some default texture to be assigned to keep it valid.
This commit is contained in:
Andrej730
2025-02-28 18:06:33 +05:00
parent c81fe97cc2
commit db52e70121
2 changed files with 34 additions and 17 deletions
+30 -14
View File
@@ -643,32 +643,48 @@ class AddPresentationStyle(bpy.types.Operator, tool.Ifc.Operator):
def _execute(self, context):
props = tool.Style.get_style_props()
ifc_file = tool.Ifc.get()
if props.style_type == "IfcSurfaceStyle":
style = ifcopenshell.api.run("style.add_style", tool.Ifc.get(), name=props.style_name)
def get_colour_dict(name: Union[str, None], r: float, g: float, b: float) -> dict[str, Any]:
return {
"Name": name,
"Red": r,
"Green": g,
"Blue": b,
}
# setup surface style element
surface_style = None
if props.surface_style_class in ("IfcSurfaceStyleShading", "IfcSurfaceStyleRendering"):
attributes = {
"SurfaceColour": {
"Name": None,
"Red": props.surface_colour[0],
"Green": props.surface_colour[1],
"Blue": props.surface_colour[2],
}
"SurfaceColour": get_colour_dict(None, *props.surface_colour),
}
if props.surface_style_class == "IfcSurfaceStyleRendering":
attributes["ReflectanceMethod"] = "NOTDEFINED"
elif props.surface_style_class == "IfcSurfaceStyleLighting":
# Requires all those colors to be valid style.
attributes = {
"DiffuseTransmissionColour": get_colour_dict(None, 0.0, 0.0, 0.0),
"DiffuseReflectionColour": get_colour_dict(None, 0.0, 0.0, 0.0),
"TransmissionColour": get_colour_dict(None, 0.0, 0.0, 0.0),
"ReflectanceColour": get_colour_dict(None, 0.0, 0.0, 0.0),
}
elif props.surface_style_class == "IfcSurfaceStyleWithTextures":
# TODO: Requires textures to be valid.
self.report(
{"ERROR"},
"Adding IfcSurfaceStyleWithTextures directly is not supported yet."
"You can create Rendering style and then add Texture style to it.",
)
return {"CANCELLED"}
else:
# NOTE: for all other styles we produce just empty styles.
# In the future we might need to expose to adding presentation style UI
# LightingStyle colors and TextureStyle textures UI
# as they are required for those surface styles to keep IFC valid
# The rest of styles are valid even without any attributes assigned.
attributes = {}
surface_style = ifcopenshell.api.run(
"style.add_surface_style",
tool.Ifc.get(),
style = ifcopenshell.api.style.add_style(ifc_file, name=props.style_name)
surface_style = ifcopenshell.api.style.add_surface_style(
ifc_file,
style=style,
ifc_class=props.surface_style_class,
attributes=attributes,
@@ -22,11 +22,12 @@ from typing import Any, Optional, Literal
SURFACE_STYLE_TYPES = Literal[
"IfcExternallyDefinedSurfaceStyle",
"IfcSurfaceStyleShading",
"IfcSurfaceStyleRendering",
"IfcSurfaceStyleWithTextures",
"IfcSurfaceStyleLighting",
"IfcSurfaceStyleRefraction",
"IfcSurfaceStyleShading",
"IfcSurfaceStyleWithTextures",
"IfcExternallyDefinedSurfaceStyle",
]