From 77821caae41fe1e88a1cddb9194c49d62df8c6a0 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Wed, 2 Feb 2022 15:04:19 +1100 Subject: [PATCH] Adding surface styles now respect mutually exclusive style types --- .../api/style/add_surface_style.py | 23 ++-- .../test/api/style/test_add_surface_style.py | 113 ++++++++++++++++++ 2 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 src/ifcopenshell-python/test/api/style/test_add_surface_style.py diff --git a/src/ifcopenshell-python/ifcopenshell/api/style/add_surface_style.py b/src/ifcopenshell-python/ifcopenshell/api/style/add_surface_style.py index eb9cd83760..9128064eee 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/style/add_surface_style.py +++ b/src/ifcopenshell-python/ifcopenshell/api/style/add_surface_style.py @@ -16,6 +16,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with IfcOpenShell. If not, see . +import ifcopenshell +import ifcopenshell.api + class Usecase: def __init__(self, file, **settings): @@ -25,22 +28,20 @@ class Usecase: self.settings[key] = value def execute(self): - for key, value in self.settings["attributes"].items(): - if key == "SurfaceColour" and value: - self.settings["attributes"][key] = self.create_colour_rgb(value) - if key == "DiffuseColour" and isinstance(value, dict): - self.settings["attributes"][key] = self.create_colour_rgb(value) - - style_item = self.file.create_entity(self.settings["ifc_class"], **self.settings["attributes"]) + style_item = self.file.create_entity(self.settings["ifc_class"]) + ifcopenshell.api.run( + "style.edit_surface_style", self.file, style=style_item, attributes=self.settings["attributes"] + ) styles = list(self.settings["style"].Styles or []) - duplicate_items = [s for s in styles if s.is_a(self.settings["ifc_class"])] + select_class = self.settings["ifc_class"] + if select_class == "IfcSurfaceStyleRendering": + select_class = "IfcSurfaceStyleShading" + duplicate_items = [s for s in styles if s.is_a(select_class)] for duplicate_item in duplicate_items: self.file.remove(duplicate_item) + styles = list(self.settings["style"].Styles or []) styles.append(style_item) self.settings["style"].Styles = styles return style_item - - def create_colour_rgb(self, value): - return self.file.createIfcColourRgb(value["Name"], value["Red"], value["Green"], value["Blue"]) diff --git a/src/ifcopenshell-python/test/api/style/test_add_surface_style.py b/src/ifcopenshell-python/test/api/style/test_add_surface_style.py new file mode 100644 index 0000000000..b13fd8e7b7 --- /dev/null +++ b/src/ifcopenshell-python/test/api/style/test_add_surface_style.py @@ -0,0 +1,113 @@ +# IfcOpenShell - IFC toolkit and geometry engine +# Copyright (C) 2022 Dion Moult +# +# This file is part of IfcOpenShell. +# +# IfcOpenShell is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# IfcOpenShell is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with IfcOpenShell. If not, see . + +import pytest +import test.bootstrap +import ifcopenshell.api + + +class TestAddSurfaceStyle(test.bootstrap.IFC4): + def test_adding_a_surface_style(self): + style = self.file.createIfcSurfaceStyle() + result = ifcopenshell.api.run( + "style.add_surface_style", + self.file, + style=style, + ifc_class="IfcSurfaceStyleShading", + attributes={"SurfaceColour": {"Name": "", "Red": 1, "Green": 1, "Blue": 1}, "Transparency": 0.5}, + ) + assert result.is_a("IfcSurfaceStyleShading") + assert result.SurfaceColour.Red == 1 + assert result.SurfaceColour.Green == 1 + assert result.SurfaceColour.Blue == 1 + assert result.Transparency == 0.5 + assert style.Styles[0] == result + + def test_adding_a_rendering_style(self): + style = self.file.createIfcSurfaceStyle() + result = ifcopenshell.api.run( + "style.add_surface_style", + self.file, + style=style, + ifc_class="IfcSurfaceStyleRendering", + attributes={"SurfaceColour": {"Name": "", "Red": 1, "Green": 1, "Blue": 1}, "Transparency": 0.5}, + ) + assert result.is_a("IfcSurfaceStyleRendering") + assert result.SurfaceColour.Red == 1 + assert result.SurfaceColour.Green == 1 + assert result.SurfaceColour.Blue == 1 + assert result.Transparency == 0.5 + assert style.Styles[0] == result + + def test_not_adding_a_style_twice(self): + style = self.file.createIfcSurfaceStyle() + ifcopenshell.api.run( + "style.add_surface_style", + self.file, + style=style, + ifc_class="IfcSurfaceStyleRendering", + attributes={"SurfaceColour": {"Name": "", "Red": 1, "Green": 1, "Blue": 1}, "Transparency": 0.5}, + ) + result = ifcopenshell.api.run( + "style.add_surface_style", + self.file, + style=style, + ifc_class="IfcSurfaceStyleRendering", + attributes={"SurfaceColour": {"Name": "", "Red": 1, "Green": 1, "Blue": 1}, "Transparency": 0.5}, + ) + assert style.Styles[0] == result + assert len(style.Styles) == 1 + + def test_adding_multiple_styles_of_different_types(self): + style = self.file.createIfcSurfaceStyle() + result1 = ifcopenshell.api.run( + "style.add_surface_style", + self.file, + style=style, + ifc_class="IfcSurfaceStyleShading", + attributes={"SurfaceColour": {"Name": "", "Red": 1, "Green": 1, "Blue": 1}, "Transparency": 0.5}, + ) + result2 = ifcopenshell.api.run( + "style.add_surface_style", + self.file, + style=style, + ifc_class="IfcSurfaceStyleWithTextures", + attributes={}, + ) + assert result1 in style.Styles + assert result2 in style.Styles + assert len(style.Styles) == 2 + + def test_ensure_shading_and_rendering_are_mutually_exclusive_when_adding(self): + style = self.file.createIfcSurfaceStyle() + ifcopenshell.api.run( + "style.add_surface_style", + self.file, + style=style, + ifc_class="IfcSurfaceStyleShading", + attributes={"SurfaceColour": {"Name": "", "Red": 1, "Green": 1, "Blue": 1}, "Transparency": 0.5}, + ) + result = ifcopenshell.api.run( + "style.add_surface_style", + self.file, + style=style, + ifc_class="IfcSurfaceStyleRendering", + attributes={"SurfaceColour": {"Name": "", "Red": 1, "Green": 1, "Blue": 1}, "Transparency": 0.5}, + ) + assert style.Styles[0] == result + assert len(style.Styles) == 1