Skip to content

Commit 2120653

Browse files
committed
Minor fix.
1 parent 43869c3 commit 2120653

File tree

5 files changed

+68
-30
lines changed

5 files changed

+68
-30
lines changed

src/blenderbim/blenderbim/bim/module/type/data.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import bpy
20+
import ifcopenshell.util.type
21+
import ifcopenshell.util.element
2022
import blenderbim.tool as tool
2123

2224

@@ -30,24 +32,55 @@ class TypeData:
3032

3133
@classmethod
3234
def load(cls):
35+
cls.is_loaded = True
3336
cls.data = {
3437
"relating_type_classes": cls.relating_type_classes(),
3538
"relating_types": cls.relating_types(),
39+
"is_product": cls.is_product(),
40+
"total_instances": cls.total_instances(),
41+
"type_name": cls.type_name(),
3642
}
37-
cls.is_loaded = True
3843

3944
@classmethod
4045
def relating_type_classes(cls):
4146
results = []
42-
element = tool.Ifc.get_entity(bpy.context.active_object)
47+
obj = bpy.context.active_object
48+
if not obj:
49+
return
50+
element = tool.Ifc.get_entity(obj)
51+
if not element:
52+
return []
4353
types = ifcopenshell.util.type.get_applicable_types(element.is_a(), schema=tool.Ifc.get_schema())
44-
applicable_types_enum.extend((t, t, "") for t in types)
54+
results.extend((t, t, "") for t in types)
4555
return results
4656

4757
@classmethod
4858
def relating_types(cls):
59+
relating_type_classes = cls.relating_type_classes()
60+
if not relating_type_classes:
61+
return []
4962
results = []
50-
elements = tool.Ifc.get().by_type(bpy.context.active_object.BIMTypeProperties.relating_type_class)
63+
relating_type_class = bpy.context.active_object.BIMTypeProperties.relating_type_class
64+
if not relating_type_class and relating_type_classes:
65+
relating_type_class = relating_type_classes[0][0]
66+
elements = tool.Ifc.get().by_type(relating_type_class)
5167
elements = [(str(e.id()), e.Name, "") for e in elements]
52-
relating_types_enum.extend(sorted(elements, key=lambda s: s[1]))
68+
results.extend(sorted(elements, key=lambda s: s[1]))
5369
return results
70+
71+
@classmethod
72+
def is_product(cls):
73+
element = tool.Ifc.get_entity(bpy.context.active_object)
74+
return element.is_a("IfcProduct")
75+
76+
@classmethod
77+
def total_instances(cls):
78+
element = tool.Ifc.get_entity(bpy.context.active_object)
79+
return str(len(ifcopenshell.util.element.get_types(element)))
80+
81+
@classmethod
82+
def type_name(cls):
83+
element = tool.Ifc.get_entity(bpy.context.active_object)
84+
type = ifcopenshell.util.element.get_type(element)
85+
if type:
86+
return f"{type.is_a()}/{type.Name or 'Unnamed'}"

src/blenderbim/blenderbim/bim/module/type/prop.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ def get_relating_type(self, context):
4646
return TypeData.data["relating_types"]
4747

4848

49-
def update_is_editing_type(self, context):
49+
def update_relating_type_class(self, context):
5050
TypeData.is_loaded = False
5151

5252

5353
class BIMTypeProperties(PropertyGroup):
54-
is_editing_type: BoolProperty(name="Is Editing Type", update=update_is_editing_type)
55-
relating_type_class: EnumProperty(items=get_relating_type_class, name="Relating Type Class")
54+
is_editing_type: BoolProperty(name="Is Editing Type")
55+
relating_type_class: EnumProperty(items=get_relating_type_class, name="Relating Type Class", update=update_relating_type_class)
5656
relating_type: EnumProperty(items=get_relating_type, name="Relating Type")

src/blenderbim/blenderbim/bim/module/type/ui.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
# You should have received a copy of the GNU General Public License
1717
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import blenderbim.tool as tool
1920
import blenderbim.bim.module.type.prop as type_prop
2021
from bpy.types import Panel
2122
from blenderbim.bim.ifc import IfcStore
22-
from ifcopenshell.api.type.data import Data
23+
from blenderbim.bim.module.type.data import TypeData
2324

2425

2526
class BIM_PT_type(Panel):
@@ -33,23 +34,20 @@ class BIM_PT_type(Panel):
3334
def poll(cls, context):
3435
if not context.active_object:
3536
return False
36-
props = context.active_object.BIMObjectProperties
37-
if not props.ifc_definition_id:
38-
return False
39-
if not IfcStore.get_element(props.ifc_definition_id):
40-
return False
41-
if props.ifc_definition_id not in Data.products and props.ifc_definition_id not in Data.types:
42-
Data.load(IfcStore.get_file(), props.ifc_definition_id)
43-
if props.ifc_definition_id not in Data.products and props.ifc_definition_id not in Data.types:
44-
return False
45-
if not Data.products.get(props.ifc_definition_id, None) and not Data.types.get(props.ifc_definition_id, None):
37+
element = tool.Ifc.get_entity(context.active_object)
38+
if not element:
4639
return False
40+
if not element.is_a("IfcProduct") and not element.is_a("IfcTypeProduct"):
41+
return True
4742
return True
4843

4944
def draw(self, context):
45+
if not TypeData.is_loaded:
46+
TypeData.load()
47+
5048
oprops = context.active_object.BIMObjectProperties
5149

52-
if oprops.ifc_definition_id in Data.products:
50+
if TypeData.data["is_product"]:
5351
self.draw_product_ui(context)
5452
else:
5553
self.draw_type_ui(context)
@@ -58,7 +56,7 @@ def draw_type_ui(self, context):
5856
props = context.active_object.BIMTypeProperties
5957
oprops = context.active_object.BIMObjectProperties
6058
row = self.layout.row(align=True)
61-
row.label(text=f"{len(Data.types[oprops.ifc_definition_id])} Typed Objects")
59+
row.label(text=f"{TypeData.data['total_instances']} Typed Objects")
6260
row.operator("bim.select_type_objects", icon="RESTRICT_SELECT_OFF", text="")
6361

6462
def draw_product_ui(self, context):
@@ -69,26 +67,23 @@ def draw_product_ui(self, context):
6967
row = self.layout.row(align=True)
7068

7169
row.prop(props, "relating_type_class", text="")
72-
if type_prop.get_object_relating_type(None, context):
70+
if type_prop.get_relating_type(None, context):
7371
row.prop(props, "relating_type", text="")
7472
row.operator("bim.assign_type", icon="CHECKMARK", text="")
7573
else:
7674
row.label(text="No Types Found")
7775
row.operator("bim.disable_editing_type", icon="CANCEL", text="")
7876
else:
7977
row = self.layout.row(align=True)
80-
name = "{}/{}".format(
81-
Data.products[oprops.ifc_definition_id]["type"], Data.products[oprops.ifc_definition_id]["Name"]
82-
)
83-
if name == "None/None":
84-
row.label(text="This object has no type")
85-
row.operator("bim.enable_editing_type", icon="GREASEPENCIL", text="")
86-
else:
87-
row.label(text=name)
78+
if TypeData.data["type_name"]:
79+
row.label(text=TypeData.data["type_name"])
8880
row.operator("bim.select_type", icon="TRACKER", text="")
8981
row.operator("bim.select_similar_type", icon="RESTRICT_SELECT_OFF", text="")
9082
row.operator("bim.enable_editing_type", icon="GREASEPENCIL", text="")
9183
row.operator("bim.unassign_type", icon="X", text="")
84+
else:
85+
row.label(text="This object has no type")
86+
row.operator("bim.enable_editing_type", icon="GREASEPENCIL", text="")
9287

9388

9489
def add_object_button(self, context):

src/ifcopenshell-python/ifcopenshell/util/element.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ def get_type(element):
8181
def get_types(type):
8282
for rel in getattr(type, "Types", []):
8383
return rel.RelatedObjects
84+
for rel in getattr(type, "ObjectTypeOf", []):
85+
return rel.RelatedObjects
8486
return []
8587

8688

src/ifcopenshell-python/test/util/test_element.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ def test_getting_the_type_of_a_product(self):
144144
assert subject.get_types(element_type) == (element,)
145145

146146

147+
class TestGetTypesIFC2X3(test.bootstrap.IFC2X3):
148+
def test_getting_the_type_of_a_product(self):
149+
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
150+
element_type = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
151+
ifcopenshell.api.run("type.assign_type", self.file, related_object=element, relating_type=element_type)
152+
assert subject.get_types(element_type) == (element,)
153+
154+
147155
class TestGetMaterial(test.bootstrap.IFC4):
148156
def test_getting_the_material_of_a_product(self):
149157
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")

0 commit comments

Comments
 (0)