Switching construction type now has an option to map representations

This commit is contained in:
Dion Moult
2021-02-14 16:49:59 +11:00
parent 4e897a0c21
commit 6966da3bdf
9 changed files with 75 additions and 37 deletions
@@ -645,9 +645,8 @@ class IfcImporter:
except: except:
self.ifc_import_settings.logger.error("Failed to generate shape for %s", element) self.ifc_import_settings.logger.error("Failed to generate shape for %s", element)
obj = bpy.data.objects.new(self.get_name(element), mesh) obj = bpy.data.objects.new(self.get_name(element), mesh)
obj.BIMObjectProperties.ifc_definition_id = element.id() IfcStore.link_element(element, obj)
self.material_creator.create(element, obj, mesh) self.material_creator.create(element, obj, mesh)
self.type_collection.objects.link(obj)
self.type_products[element.GlobalId] = obj self.type_products[element.GlobalId] = obj
def get_type_product_body_representation_map(self, element): def get_type_product_body_representation_map(self, element):
@@ -1265,6 +1264,8 @@ class IfcImporter:
def place_object_in_spatial_tree(self, element, obj): def place_object_in_spatial_tree(self, element, obj):
if element.is_a("IfcProject"): if element.is_a("IfcProject"):
return return
elif element.is_a("IfcTypeObject"):
self.type_collection.objects.link(obj)
elif element.GlobalId in self.spatial_structure_elements: elif element.GlobalId in self.spatial_structure_elements:
if not obj.data: if not obj.data:
return return
@@ -4,6 +4,7 @@ from . import ui, prop, operator
classes = ( classes = (
operator.EditObjectPlacement, operator.EditObjectPlacement,
operator.AddRepresentation, operator.AddRepresentation,
operator.MapRepresentations,
operator.MapRepresentation, operator.MapRepresentation,
operator.SwitchRepresentation, operator.SwitchRepresentation,
operator.RemoveRepresentation, operator.RemoveRepresentation,
@@ -19,7 +19,10 @@ class Data:
product = file.by_id(product_id) product = file.by_id(product_id)
representations = [] representations = []
if product.is_a("IfcProduct"): if product.is_a("IfcProduct"):
representations = product.Representation.Representations if product.Representation:
representations = product.Representation.Representations
else:
representations = []
elif product.is_a("IfcTypeProduct"): elif product.is_a("IfcTypeProduct"):
representations = [rm.MappedRepresentation for rm in product.RepresentationMaps] representations = [rm.MappedRepresentation for rm in product.RepresentationMaps]
for representation in representations: for representation in representations:
@@ -203,15 +203,16 @@ class SwitchRepresentation(bpy.types.Operator):
class RemoveRepresentation(bpy.types.Operator): class RemoveRepresentation(bpy.types.Operator):
bl_idname = "bim.remove_representation" bl_idname = "bim.remove_representation"
bl_label = "Remove Representation" bl_label = "Remove Representation"
obj: bpy.props.StringProperty() # TODO obj: bpy.props.StringProperty()
representation_id: bpy.props.IntProperty() representation_id: bpy.props.IntProperty()
def execute(self, context): def execute(self, context):
self.file = IfcStore.get_file() self.file = IfcStore.get_file()
representation = self.file.by_id(self.representation_id) representation = self.file.by_id(self.representation_id)
obj = bpy.context.active_object obj = bpy.data.objects.get(self.obj) if self.obj else bpy.context.active_object
product = self.file.by_id(obj.BIMObjectProperties.ifc_definition_id) product = self.file.by_id(obj.BIMObjectProperties.ifc_definition_id)
if representation.RepresentationType == "MappedRepresentation": is_mapped_representation = representation.RepresentationType == "MappedRepresentation"
if is_mapped_representation:
mesh_name = "{}/{}".format( mesh_name = "{}/{}".format(
representation.ContextOfItems.id(), representation.Items[0].MappingSource.MappedRepresentation.id() representation.ContextOfItems.id(), representation.Items[0].MappingSource.MappedRepresentation.id()
) )
@@ -225,33 +226,64 @@ class RemoveRepresentation(bpy.types.Operator):
if not void_mesh: if not void_mesh:
void_mesh = bpy.data.meshes.new("Void") void_mesh = bpy.data.meshes.new("Void")
obj.data = void_mesh obj.data = void_mesh
if representation.RepresentationType != "MappedRepresentation": if not is_mapped_representation:
bpy.data.meshes.remove(mesh) bpy.data.meshes.remove(mesh)
product = self.file.by_id(obj.BIMObjectProperties.ifc_definition_id) product = self.file.by_id(obj.BIMObjectProperties.ifc_definition_id)
unassign_representation.Usecase(self.file, {"product": product, "representation": representation}).execute() unassign_representation.Usecase(self.file, {"product": product, "representation": representation}).execute()
remove_representation.Usecase(self.file, {"representation": representation}).execute() if not is_mapped_representation:
remove_representation.Usecase(self.file, {"representation": representation}).execute()
Data.load(product.id()) Data.load(product.id())
return {"FINISHED"} return {"FINISHED"}
class MapRepresentations(bpy.types.Operator):
bl_idname = "bim.map_representations"
bl_label = "Map Representations"
product_id: bpy.props.IntProperty()
type_product_id: bpy.props.IntProperty()
def execute(self, context):
related_object = IfcStore.id_map[self.product_id]
if self.product_id not in Data.products:
Data.load(self.product_id)
for representation_id in Data.products[self.product_id]:
bpy.ops.bim.remove_representation(obj=related_object.name, representation_id=representation_id)
if self.type_product_id not in Data.products:
Data.load(self.type_product_id)
for representation_id in Data.products[self.type_product_id]:
bpy.ops.bim.map_representation(
obj=related_object.name,
representation_id=representation_id,
obj_data=IfcStore.id_map[self.type_product_id].data.name,
)
return {"FINISHED"}
class MapRepresentation(bpy.types.Operator): class MapRepresentation(bpy.types.Operator):
bl_idname = "bim.map_representation" bl_idname = "bim.map_representation"
bl_label = "Map Representation" bl_label = "Map Representation"
obj: bpy.props.StringProperty() obj: bpy.props.StringProperty()
representation_id: bpy.props.IntProperty()
obj_data: bpy.props.StringProperty() obj_data: bpy.props.StringProperty()
def execute(self, context): def execute(self, context):
objs = [bpy.data.objects.get(self.obj)] if self.obj else bpy.context.selected_objects objs = [bpy.data.objects.get(self.obj)] if self.obj else bpy.context.selected_objects
obj_data = bpy.data.meshes.get(self.obj_data) if self.obj_data else bpy.context.active_object.data obj_data = bpy.data.meshes.get(self.obj_data) if self.obj_data else None
objs = [o for o in objs if o.data != obj_data]
self.file = IfcStore.get_file() self.file = IfcStore.get_file()
for obj in objs: for obj in objs:
bpy.ops.bim.edit_object_placement(obj=obj.name) bpy.ops.bim.edit_object_placement(obj=obj.name)
product = self.file.by_id(obj.BIMObjectProperties.ifc_definition_id) product = self.file.by_id(obj.BIMObjectProperties.ifc_definition_id)
target_representation = self.file.by_id(obj_data.BIMMeshProperties.ifc_definition_id) if obj_data:
obj.data = obj_data obj.data = obj_data
result = map_representation.Usecase(self.file, {"representation": target_representation}).execute() result = map_representation.Usecase(
self.file, {"representation": self.file.by_id(self.representation_id)}
).execute()
assign_representation.Usecase(self.file, {"product": product, "representation": result}).execute() assign_representation.Usecase(self.file, {"product": product, "representation": result}).execute()
Data.load(obj.BIMObjectProperties.ifc_definition_id) Data.load(obj.BIMObjectProperties.ifc_definition_id)
return {"FINISHED"} return {"FINISHED"}
@@ -32,10 +32,10 @@ class Usecase:
self.settings["product"].RepresentationMaps = self.settings["product"].RepresentationMaps or None self.settings["product"].RepresentationMaps = self.settings["product"].RepresentationMaps or None
def remove_representation_map_only(self, representation_map): def remove_representation_map_only(self, representation_map):
dummy_representation = self.file.createIfcShapeRepresentation() dummy_representation = self.file.createIfcShapeRepresentation()
representation_map.MappedRepresentation = dummy_representation representation_map.MappedRepresentation = dummy_representation
ifcopenshell.util.element.remove_deep(self.file, representation_map) ifcopenshell.util.element.remove_deep(self.file, representation_map)
self.file.remove(representation_map) self.file.remove(representation_map)
def unassign_products_using_mapped_representation(self, representation_map): def unassign_products_using_mapped_representation(self, representation_map):
mapped_representations = [] mapped_representations = []
@@ -46,10 +46,7 @@ class Usecase:
continue continue
for definition in inverse.OfProductRepresentation or []: for definition in inverse.OfProductRepresentation or []:
for product in definition.ShapeOfProduct or []: for product in definition.ShapeOfProduct or []:
mapped_representations.append({ mapped_representations.append({"product": product, "representation": inverse})
"product": product,
"representation": inverse
})
just_representations.append(inverse) just_representations.append(inverse)
for item in mapped_representations: for item in mapped_representations:
self.unassign_product_representation(item["product"], item["representation"]) self.unassign_product_representation(item["product"], item["representation"])
@@ -115,7 +115,6 @@ class VIEW3D_MT_PIE_bim(bpy.types.Menu):
pie = self.layout.menu_pie() pie = self.layout.menu_pie()
pie.operator("bim.edit_object_placement") pie.operator("bim.edit_object_placement")
pie.operator("bim.update_mesh_representation") pie.operator("bim.update_mesh_representation")
pie.operator("bim.map_representation")
pie.operator("bim.pie_add_opening") pie.operator("bim.pie_add_opening")
pie.operator("bim.pie_update_container") pie.operator("bim.pie_update_container")
pie.operator("bim.open_pie_class", text="Assign IFC Class") pie.operator("bim.open_pie_class", text="Assign IFC Class")
@@ -2,6 +2,7 @@ import bpy
import numpy as np import numpy as np
import ifcopenshell import ifcopenshell
import ifcopenshell.util.schema import ifcopenshell.util.schema
import ifcopenshell.util.element
import blenderbim.bim.module.root.create_product as create_product import blenderbim.bim.module.root.create_product as create_product
import blenderbim.bim.module.root.remove_product as remove_product import blenderbim.bim.module.root.remove_product as remove_product
import blenderbim.bim.module.root.reassign_class as reassign_class import blenderbim.bim.module.root.reassign_class as reassign_class
@@ -219,16 +220,14 @@ class CopyClass(bpy.types.Operator):
for obj in objects: for obj in objects:
if not obj.BIMObjectProperties.ifc_definition_id: if not obj.BIMObjectProperties.ifc_definition_id:
continue continue
result = copy_class.Usecase(self.file, { result = copy_class.Usecase(
"product": self.file.by_id(obj.BIMObjectProperties.ifc_definition_id) self.file, {"product": self.file.by_id(obj.BIMObjectProperties.ifc_definition_id)}
}).execute() ).execute()
IfcStore.link_element(result, obj) IfcStore.link_element(result, obj)
if obj.data.users == 1: if obj.data.users == 1:
bpy.ops.bim.add_representation(obj=obj.name) bpy.ops.bim.add_representation(obj=obj.name)
else: else:
obj_data = obj.data.name bpy.ops.bim.map_representations(
temporary_mesh = bpy.data.meshes.new("Temporary Mesh") product_id=result.id(), type_product_id=ifcopenshell.util.element.get_type(result).id()
obj.data = temporary_mesh )
bpy.ops.bim.map_representation(obj=obj.name, obj_data=obj_data)
bpy.data.meshes.remove(temporary_mesh)
return {"FINISHED"} return {"FINISHED"}
@@ -12,6 +12,7 @@ class AssignType(bpy.types.Operator):
bl_label = "Assign Type" bl_label = "Assign Type"
relating_type: bpy.props.IntProperty() relating_type: bpy.props.IntProperty()
related_object: bpy.props.StringProperty() related_object: bpy.props.StringProperty()
should_map_representations: bpy.props.BoolProperty()
def execute(self, context): def execute(self, context):
self.file = IfcStore.get_file() self.file = IfcStore.get_file()
@@ -27,6 +28,10 @@ class AssignType(bpy.types.Operator):
}, },
).execute() ).execute()
Data.load(oprops.ifc_definition_id) Data.load(oprops.ifc_definition_id)
if self.should_map_representations:
bpy.ops.bim.map_representations(product_id=oprops.ifc_definition_id, type_product_id=relating_type)
bpy.ops.bim.disable_editing_type(obj=related_object.name) bpy.ops.bim.disable_editing_type(obj=related_object.name)
return {"FINISHED"} return {"FINISHED"}
@@ -82,13 +87,13 @@ class SelectSimilarType(bpy.types.Operator):
product = self.file.by_id(oprops.ifc_definition_id) product = self.file.by_id(oprops.ifc_definition_id)
declaration = IfcStore.get_schema().declaration_by_name(product.is_a()) declaration = IfcStore.get_schema().declaration_by_name(product.is_a())
if ifcopenshell.util.schema.is_a(declaration, "IfcElementType"): if ifcopenshell.util.schema.is_a(declaration, "IfcElementType"):
related_objects = get_related_objects.Usecase(self.file, { related_objects = get_related_objects.Usecase(
"relating_type": self.file.by_id(oprops.ifc_definition_id) self.file, {"relating_type": self.file.by_id(oprops.ifc_definition_id)}
}).execute() ).execute()
else: else:
related_objects = get_related_objects.Usecase(self.file, { related_objects = get_related_objects.Usecase(
"related_object": self.file.by_id(oprops.ifc_definition_id) self.file, {"related_object": self.file.by_id(oprops.ifc_definition_id)}
}).execute() ).execute()
for obj in bpy.context.visible_objects: for obj in bpy.context.visible_objects:
if obj.BIMObjectProperties.ifc_definition_id in related_objects: if obj.BIMObjectProperties.ifc_definition_id in related_objects:
obj.select_set(True) obj.select_set(True)
@@ -35,7 +35,8 @@ class BIM_PT_type(Panel):
row.prop(props, "relating_type_class", text="") row.prop(props, "relating_type_class", text="")
if props.relating_type: if props.relating_type:
row.prop(props, "relating_type", text="") row.prop(props, "relating_type", text="")
row.operator("bim.assign_type", icon="CHECKMARK", text="") op = row.operator("bim.assign_type", icon="CHECKMARK", text="")
op.should_map_representations = props.should_map_representations
else: else:
row.prop(props, "blank_relating_type", text="") row.prop(props, "blank_relating_type", text="")
row.operator("bim.disable_editing_type", icon="X", text="") row.operator("bim.disable_editing_type", icon="X", text="")