Skip to content

Commit de46595

Browse files
committed
See IfcOpenShell#1917. Refactor and fix warning message when you haven't got any contexts in a project yet.
1 parent 62236c4 commit de46595

10 files changed

Lines changed: 87 additions & 35 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ def draw(self, context):
5959
row.operator("bim.select_high_polygon_meshes").threshold = context.scene.BIMDebugProperties.number_of_polygons
6060
row.prop(props, "number_of_polygons", text="")
6161
row = layout.split(factor=0.7, align=True)
62-
row.operator("bim.select_highest_polygon_meshes").percentile = context.scene.BIMDebugProperties.percentile_of_polygons
62+
row.operator(
63+
"bim.select_highest_polygon_meshes"
64+
).percentile = context.scene.BIMDebugProperties.percentile_of_polygons
6365
row.prop(props, "percentile_of_polygons", text="")
6466

6567
layout.label(text="Inspector:")

src/blenderbim/blenderbim/bim/module/geometry/operator.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030
import blenderbim.core.root
3131
import blenderbim.tool as tool
3232
import blenderbim.bim.handler
33-
from blenderbim.bim.ifc import IfcStore
34-
from blenderbim.bim import import_ifc
33+
from mathutils import Vector
3534
from ifcopenshell.api.geometry.data import Data
3635
from ifcopenshell.api.context.data import Data as ContextData
3736
from ifcopenshell.api.void.data import Data as VoidData
38-
from mathutils import Vector
37+
from blenderbim.bim import import_ifc
38+
from blenderbim.bim.ifc import IfcStore
39+
from blenderbim.bim.module.root.prop import get_contexts
3940

4041

4142
class Operator:
@@ -67,7 +68,9 @@ class AddRepresentation(bpy.types.Operator, Operator):
6768
profile_set_usage: bpy.props.IntProperty()
6869

6970
def _execute(self, context):
70-
ifc_context = self.context_id or int(context.scene.BIMProperties.contexts or "0") or None
71+
ifc_context = self.context_id
72+
if not ifc_context and get_contexts(self, context):
73+
ifc_context = int(context.scene.BIMRootProperties.contexts or "0") or None
7174
if ifc_context:
7275
ifc_context = tool.Ifc.get().by_id(ifc_context)
7376
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object

src/blenderbim/blenderbim/bim/module/project/operator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ def _execute(self, context):
9191
tool.Ifc, context_type="Plan", context_identifier="Annotation", target_view="PLAN_VIEW", parent=plan
9292
)
9393

94-
ContextData.load(tool.Ifc.get())
95-
context.scene.BIMProperties.contexts = str(body_context.id())
94+
blenderbim.bim.handler.refresh_ui_data()
95+
context.scene.BIMRootProperties.contexts = str(body_context.id())
9696

9797
bpy.ops.bim.assign_class(obj=site.name, ifc_class="IfcSite")
9898
bpy.ops.bim.assign_class(obj=building.name, ifc_class="IfcBuilding")
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# BlenderBIM Add-on - OpenBIM Blender Add-on
2+
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
3+
#
4+
# This file is part of BlenderBIM Add-on.
5+
#
6+
# BlenderBIM Add-on is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# BlenderBIM Add-on is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
18+
19+
import bpy
20+
import blenderbim.tool as tool
21+
22+
23+
def refresh():
24+
IfcClassData.is_loaded = False
25+
26+
27+
class IfcClassData:
28+
data = {}
29+
is_loaded = False
30+
31+
@classmethod
32+
def load(cls):
33+
cls.is_loaded = True
34+
cls.data = {
35+
"contexts": cls.contexts(),
36+
}
37+
38+
@classmethod
39+
def contexts(cls):
40+
results = []
41+
for element in tool.Ifc.get().by_type("IfcGeometricRepresentationContext", include_subtypes=False):
42+
results.append((str(element.id()), element.ContextType or "Unnamed", ""))
43+
for element in tool.Ifc.get().by_type("IfcGeometricRepresentationSubContext", include_subtypes=False):
44+
results.append(
45+
(
46+
str(element.id()),
47+
"{}/{}/{}".format(
48+
element.ContextType or "Unnamed",
49+
element.ContextIdentifier or "Unnamed",
50+
element.TargetView or "Unnamed",
51+
),
52+
"",
53+
)
54+
)
55+
return results

src/blenderbim/blenderbim/bim/module/root/operator.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import blenderbim.tool as tool
3232
from ifcopenshell.api.void.data import Data as VoidData
3333
from blenderbim.bim.ifc import IfcStore
34+
from blenderbim.bim.module.root.prop import get_contexts
3435

3536

3637
class Operator:
@@ -156,7 +157,9 @@ def assign_class(self, context, obj):
156157
IfcStore.link_element(product, obj)
157158

158159
if self.should_add_representation:
159-
ifc_context = self.context_id or int(context.scene.BIMProperties.contexts or "0") or None
160+
ifc_context = self.context_id
161+
if not ifc_context and get_contexts(self, context):
162+
ifc_context = int(context.scene.BIMRootProperties.contexts or "0") or None
160163
if ifc_context:
161164
ifc_context = tool.Ifc.get().by_id(ifc_context)
162165
blenderbim.core.geometry.add_representation(

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import bpy
2020
import ifcopenshell
2121
import ifcopenshell.util.schema
22+
from blenderbim.bim.module.root.data import IfcClassData
2223
from blenderbim.bim.ifc import IfcStore
2324
from bpy.types import PropertyGroup
2425
from bpy.props import (
@@ -109,7 +110,14 @@ def getIfcClasses(self, context):
109110
return classes_enum
110111

111112

113+
def get_contexts(self, context):
114+
if not IfcClassData.is_loaded:
115+
IfcClassData.load()
116+
return IfcClassData.data["contexts"]
117+
118+
112119
class BIMRootProperties(PropertyGroup):
120+
contexts: EnumProperty(items=get_contexts, name="Contexts")
113121
ifc_product: EnumProperty(items=getIfcProducts, name="Products", update=refreshClasses)
114122
ifc_class: EnumProperty(items=getIfcClasses, name="Class", update=refreshPredefinedTypes)
115123
ifc_predefined_type: EnumProperty(items=getIfcPredefinedTypes, name="Predefined Type", default=None)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from bpy.types import Panel
2222
from ifcopenshell.api.root.data import Data
2323
from blenderbim.bim.ifc import IfcStore
24+
from blenderbim.bim.module.root.data import IfcClassData
2425

2526

2627
class BIM_PT_class(Panel):
@@ -37,6 +38,8 @@ def poll(cls, context):
3738
return IfcStore.get_file()
3839

3940
def draw(self, context):
41+
if not IfcClassData.is_loaded:
42+
IfcClassData.load()
4043
props = context.active_object.BIMObjectProperties
4144
if props.ifc_definition_id:
4245
if props.ifc_definition_id not in Data.products:
@@ -100,4 +103,4 @@ def draw_class_dropdowns(self, context, ifc_predefined_types, should_draw_produc
100103
row = self.layout.row()
101104
row.prop(props, "ifc_userdefined_type")
102105
row = self.layout.row()
103-
row.prop(context.scene.BIMProperties, "contexts")
106+
row.prop(context.scene.BIMRootProperties, "contexts")

src/blenderbim/blenderbim/bim/prop.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -114,27 +114,6 @@ def getMaterialPsetNames(self, context):
114114
return materialpsetnames_enum
115115

116116

117-
def getContexts(self, context):
118-
from ifcopenshell.api.context.data import Data
119-
120-
if not Data.is_loaded:
121-
Data.load(IfcStore.get_file())
122-
results = []
123-
for ifc_id, context in Data.contexts.items():
124-
results.append((str(ifc_id), context["ContextType"], ""))
125-
for ifc_id2, subcontext in context["HasSubContexts"].items():
126-
results.append(
127-
(
128-
str(ifc_id2),
129-
"{}/{}/{}".format(
130-
subcontext["ContextType"], subcontext["ContextIdentifier"], subcontext["TargetView"]
131-
),
132-
"",
133-
)
134-
)
135-
return results
136-
137-
138117
class StrProperty(PropertyGroup):
139118
pass
140119

@@ -235,7 +214,6 @@ class BIMProperties(PropertyGroup):
235214
ifc_file: StringProperty(name="IFC File", update=update_ifc_file)
236215
export_schema: EnumProperty(items=[("IFC4", "IFC4", ""), ("IFC2X3", "IFC2X3", "")], name="IFC Schema")
237216
last_transaction: StringProperty(name="Last Transaction")
238-
contexts: EnumProperty(items=getContexts, name="Contexts")
239217
should_section_selected_objects: BoolProperty(name="Section Selected Objects", default=False)
240218
section_plane_colour: FloatVectorProperty(
241219
name="Temporary Section Cutaway Colour", subtype="COLOR", default=(1, 0, 0), min=0.0, max=1.0

src/blenderbim/test/bim/feature/geometry.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Scenario: Add representation
2020
And the object "IfcWall/Cube" is selected
2121
Then the object "IfcWall/Cube" data is a "Tessellation" representation of "Model/Body/MODEL_VIEW"
2222
When the variable "context" is "{ifc}.by_type('IfcGeometricRepresentationSubContext')[-1].id()"
23-
And I set "scene.BIMProperties.contexts" to "{context}"
23+
And I set "scene.BIMRootProperties.contexts" to "{context}"
2424
And I press "bim.add_representation"
2525
Then the object "IfcWall/Cube" data is a "Annotation2D" representation of "Plan/Annotation/PLAN_VIEW"
2626

@@ -37,7 +37,7 @@ Scenario: Add representation - add a new representation to a typed instance
3737
And the object "IfcWall/Instance.001" data is a "Tessellation" representation of "Model/Body/MODEL_VIEW"
3838
When the object "IfcWall/Instance" is selected
3939
And the variable "context" is "{ifc}.by_type('IfcGeometricRepresentationSubContext')[-1].id()"
40-
And I set "scene.BIMProperties.contexts" to "{context}"
40+
And I set "scene.BIMRootProperties.contexts" to "{context}"
4141
And I press "bim.add_representation"
4242
Then the object "IfcWall/Instance" data is a "Annotation2D" representation of "Plan/Annotation/PLAN_VIEW"
4343
And the object "IfcWall/Instance.001" data is a "Annotation2D" representation of "Plan/Annotation/PLAN_VIEW"
@@ -80,7 +80,7 @@ Scenario: Switch representation - current edited representation is updated prior
8080
And I set "scene.BIMRootProperties.ifc_class" to "IfcWall"
8181
And I press "bim.assign_class"
8282
And the variable "context" is "{ifc}.by_type('IfcGeometricRepresentationSubContext')[-1].id()"
83-
And I set "scene.BIMProperties.contexts" to "{context}"
83+
And I set "scene.BIMRootProperties.contexts" to "{context}"
8484
And I press "bim.add_representation"
8585
When the object "IfcWall/Cube" is scaled to "2"
8686
And the variable "representation" is "{ifc}.by_type('IfcShapeRepresentation')[0].id()"

src/blenderbim/test/bim/feature/model.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Scenario: Add type instance - add a mesh where existing instances have changed c
4141
And the object "IfcWall/Instance" data is a "Tessellation" representation of "Model/Body/MODEL_VIEW"
4242
And the object "IfcWall/Instance" is selected
4343
And the variable "context" is "{ifc}.by_type('IfcGeometricRepresentationSubContext')[-1].id()"
44-
And I set "scene.BIMProperties.contexts" to "{context}"
44+
And I set "scene.BIMRootProperties.contexts" to "{context}"
4545
And I press "bim.add_representation"
4646
And the object "IfcWall/Instance" data is a "Annotation2D" representation of "Plan/Annotation/PLAN_VIEW"
4747
When I press "bim.add_type_instance"

0 commit comments

Comments
 (0)