Fix #3511. You can now use a query to load in occurrences from another project (library).

This commit is contained in:
Dion Moult
2023-07-29 22:43:50 +10:00
parent c076cbc5db
commit 7739779ed6
3 changed files with 59 additions and 14 deletions
@@ -22,6 +22,7 @@ from . import ui, prop, operator
classes = (
operator.AppendEntireLibrary,
operator.AppendLibraryElement,
operator.AppendLibraryElementByQuery,
operator.AssignLibraryDeclaration,
operator.ChangeLibraryElement,
operator.CreateProject,
@@ -342,7 +342,7 @@ class SaveLibraryFile(bpy.types.Operator):
class AppendEntireLibrary(bpy.types.Operator):
bl_idname = "bim.append_entire_library"
bl_label = "Append Entiry Library"
bl_label = "Append Entire Library"
@classmethod
def poll(cls, context):
@@ -363,6 +363,33 @@ class AppendEntireLibrary(bpy.types.Operator):
return {"FINISHED"}
class AppendLibraryElementByQuery(bpy.types.Operator):
bl_idname = "bim.append_library_element_by_query"
bl_label = "Append Library Element By Query"
query: bpy.props.StringProperty(name="Query")
@classmethod
def poll(cls, context):
return IfcStore.get_file()
def execute(self, context):
return IfcStore.execute_ifc_operator(self, context)
def _execute(self, context):
self.file = IfcStore.get_file()
self.library = IfcStore.library_file
for element in ifcopenshell.util.selector.filter_elements(self.library, self.query):
bpy.ops.bim.append_library_element(definition=element.id())
return {"FINISHED"}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
self.layout.prop(self, "query")
class AppendLibraryElement(bpy.types.Operator):
bl_idname = "bim.append_library_element"
bl_label = "Append Library Element"
@@ -393,6 +420,8 @@ class AppendLibraryElement(bpy.types.Operator):
return {"FINISHED"}
if element.is_a("IfcTypeProduct"):
self.import_type_from_ifc(element, context)
elif element.is_a("IfcProduct"):
self.import_product_from_ifc(element, context)
elif element.is_a("IfcMaterial"):
self.import_material_from_ifc(element, context)
try:
@@ -412,6 +441,19 @@ class AppendLibraryElement(bpy.types.Operator):
blender_material = ifc_importer.create_material(element)
self.import_material_styles(blender_material, element, ifc_importer)
def import_product_from_ifc(self, element, context):
self.file = IfcStore.get_file()
logger = logging.getLogger("ImportIFC")
ifc_import_settings = import_ifc.IfcImportSettings.factory(context, IfcStore.path, logger)
ifc_importer = import_ifc.IfcImporter(ifc_import_settings)
ifc_importer.file = self.file
ifc_importer.process_context_filter()
ifc_importer.material_creator.load_existing_materials()
self.import_materials(element, ifc_importer)
self.import_styles(element, ifc_importer)
ifc_importer.create_generic_elements({element})
ifc_importer.place_objects_in_collections()
def import_type_from_ifc(self, element, context):
self.file = IfcStore.get_file()
logger = logging.getLogger("ImportIFC")
@@ -431,24 +473,25 @@ class AppendLibraryElement(bpy.types.Operator):
ifc_importer.file = self.file
ifc_importer.type_collection = type_collection
ifc_importer.material_creator.load_existing_materials()
self.import_type_materials(element, ifc_importer)
self.import_type_styles(element, ifc_importer)
self.import_materials(element, ifc_importer)
self.import_styles(element, ifc_importer)
ifc_importer.create_element_type(element)
ifc_importer.place_objects_in_collections()
def import_type_materials(self, element, ifc_importer):
for rel in element.HasAssociations:
if not rel.is_a("IfcRelAssociatesMaterial"):
def import_materials(self, element, ifc_importer):
for material in ifcopenshell.util.element.get_materials(element):
if IfcStore.get_element(material.id()):
continue
for material in [e for e in self.file.traverse(rel) if e.is_a("IfcMaterial")]:
if IfcStore.get_element(material.id()):
continue
blender_material = ifc_importer.create_material(material)
self.import_material_styles(blender_material, material, ifc_importer)
blender_material = ifc_importer.create_material(material)
self.import_material_styles(blender_material, material, ifc_importer)
def import_type_styles(self, element, ifc_importer):
for representation_map in element.RepresentationMaps or []:
for element in self.file.traverse(representation_map):
def import_styles(self, element, ifc_importer):
if element.is_a("IfcTypeProduct"):
representations = element.RepresentationMaps or []
elif element.is_a("IfcProduct"):
representations = [element.Representation] if element.Representation else []
for representation in representations or []:
for element in self.file.traverse(representation):
if not element.is_a("IfcRepresentationItem") or not element.StyledByItem:
continue
for element2 in self.file.traverse(element.StyledByItem[0]):
@@ -252,6 +252,7 @@ class BIM_PT_project_library(Panel):
row.label(text=IfcStore.library_path or "No Library Loaded", icon="ASSET_MANAGER")
if IfcStore.library_file:
row.label(text=IfcStore.library_file.schema)
row.operator("bim.append_library_element_by_query", text="", icon="APPEND_BLEND")
row.operator("bim.save_library_file", text="", icon="EXPORT")
row.operator("bim.select_library_file", icon="FILE_FOLDER", text="")
if IfcStore.library_file: