Fix #5460. Assigning a class now uses the new add element mode internally.

This commit is contained in:
Dion Moult
2024-09-25 23:48:44 +10:00
parent 47581f2316
commit 42f32e3418
3 changed files with 39 additions and 24 deletions
+5 -13
View File
@@ -135,21 +135,13 @@ class IfcClassData:
("EMPTY", "No Geometry", "Start with an empty object"), ("EMPTY", "No Geometry", "Start with an empty object"),
None, None,
] ]
if (
hasattr(bpy.context, "selected_objects")
and len(bpy.context.selected_objects) > 0
and (obj := bpy.context.active_object)
and obj.type == "MESH"
):
templates.append(
(
"OBJ",
"Tessellation From Active Selection",
"Use the actively selected object as a template to create a new tessellation",
)
)
templates.extend( templates.extend(
[ [
(
"OBJ",
"Tessellation From Object",
"Use an object as a template to create a new tessellation",
),
( (
"MESH", "MESH",
"Custom Tessellation", "Custom Tessellation",
+24 -11
View File
@@ -198,17 +198,35 @@ class AssignClass(bpy.types.Operator, tool.Ifc.Operator):
if obj.mode != "OBJECT": if obj.mode != "OBJECT":
self.report({"ERROR"}, "Object must be in OBJECT mode to assign class") self.report({"ERROR"}, "Object must be in OBJECT mode to assign class")
continue continue
core.assign_class( element = core.assign_class(
tool.Ifc, tool.Ifc,
tool.Collector, tool.Collector,
tool.Root, tool.Root,
obj=obj, obj=obj,
ifc_class=ifc_class, ifc_class=ifc_class,
predefined_type=predefined_type, predefined_type=predefined_type,
should_add_representation=self.should_add_representation, should_add_representation=False,
context=ifc_context, context=ifc_context,
ifc_representation_class=self.ifc_representation_class, ifc_representation_class=self.ifc_representation_class,
) )
if self.should_add_representation and obj.data and len(obj.data.vertices):
builder = ifcopenshell.util.shape_builder.ShapeBuilder(tool.Ifc.get())
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get())
verts = [v.co / unit_scale for v in obj.data.vertices]
faces = [p.vertices[:] for p in obj.data.polygons]
item = builder.mesh(verts, faces)
representation = builder.get_representation(ifc_context, [item])
ifcopenshell.api.geometry.assign_representation(tool.Ifc.get(), element, representation)
bonsai.core.geometry.switch_representation(
tool.Ifc,
tool.Geometry,
obj=obj,
representation=representation,
should_reload=True,
is_global=True,
should_sync_changes_first=False,
)
context.view_layer.objects.active = active_object context.view_layer.objects.active = active_object
@@ -357,21 +375,13 @@ class AddElement(bpy.types.Operator, tool.Ifc.Operator):
if representation_template == "EMTPY" or not ifc_context: if representation_template == "EMTPY" or not ifc_context:
pass pass
elif ( elif representation_template == "OBJ" and (template_obj := props.representation_obj):
representation_template == "OBJ"
and (template_obj := context.active_object)
and template_obj.type == "MESH"
and len(template_obj.data.vertices)
):
obj.matrix_world = template_obj.matrix_world obj.matrix_world = template_obj.matrix_world
builder = ifcopenshell.util.shape_builder.ShapeBuilder(tool.Ifc.get()) builder = ifcopenshell.util.shape_builder.ShapeBuilder(tool.Ifc.get())
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get()) unit_scale = ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get())
bm = bmesh.new()
bmesh.ops.create_cube(bm, size=0.5)
verts = [v.co / unit_scale for v in template_obj.data.vertices] verts = [v.co / unit_scale for v in template_obj.data.vertices]
faces = [p.vertices[:] for p in template_obj.data.polygons] faces = [p.vertices[:] for p in template_obj.data.polygons]
item = builder.mesh(verts, faces) item = builder.mesh(verts, faces)
bm.free()
representation = builder.get_representation(ifc_context, [item]) representation = builder.get_representation(ifc_context, [item])
ifcopenshell.api.geometry.assign_representation(tool.Ifc.get(), element, representation) ifcopenshell.api.geometry.assign_representation(tool.Ifc.get(), element, representation)
bonsai.core.geometry.switch_representation( bonsai.core.geometry.switch_representation(
@@ -531,6 +541,9 @@ class AddElement(bpy.types.Operator, tool.Ifc.Operator):
row = self.layout.row() row = self.layout.row()
row.prop(props, "ifc_userdefined_type") row.prop(props, "ifc_userdefined_type")
prop_with_search(self.layout, props, "representation_template", text="Representation") prop_with_search(self.layout, props, "representation_template", text="Representation")
if props.representation_template == "OBJ":
row = self.layout.row()
row.prop(props, "representation_obj", text="Object")
if props.representation_template != "EMPTY": if props.representation_template != "EMPTY":
prop_with_search(self.layout, props, "contexts") prop_with_search(self.layout, props, "contexts")
+10
View File
@@ -115,6 +115,10 @@ def is_object_class_applicable(self, obj):
return element.is_a("IfcTypeObject") == active_element.is_a("IfcTypeObject") return element.is_a("IfcTypeObject") == active_element.is_a("IfcTypeObject")
def poll_representation_obj(self, obj):
return obj.type == "MESH" and obj.data.polygons
class BIMRootProperties(PropertyGroup): class BIMRootProperties(PropertyGroup):
contexts: EnumProperty(items=get_contexts, name="Contexts", options=set()) contexts: EnumProperty(items=get_contexts, name="Contexts", options=set())
ifc_product: EnumProperty(items=get_ifc_products, name="Products", update=refresh_classes) ifc_product: EnumProperty(items=get_ifc_products, name="Products", update=refresh_classes)
@@ -124,6 +128,12 @@ class BIMRootProperties(PropertyGroup):
representation_template: bpy.props.EnumProperty( representation_template: bpy.props.EnumProperty(
items=get_representation_template, name="Representation Template", default=0 items=get_representation_template, name="Representation Template", default=0
) )
representation_obj: bpy.props.PointerProperty(
type=bpy.types.Object,
name="Representation Object",
poll=poll_representation_obj,
description="The representation will be a tessellation of the selected object",
)
relating_class_object: PointerProperty( relating_class_object: PointerProperty(
type=bpy.types.Object, type=bpy.types.Object,
name="Copy Class", name="Copy Class",