From 7522f8fb7a9f7ec74aa7b58d45a295b3ccc8caef Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Fri, 22 Jan 2021 16:15:39 +1100 Subject: [PATCH] WIP fix issues in IFCCSV refactor. See #1222. --- .../blenderbim/bim/module/csv/__init__.py | 1 + .../blenderbim/bim/module/csv/operator.py | 47 ++++++++++++++----- .../blenderbim/bim/module/csv/prop.py | 8 +++- .../blenderbim/bim/module/csv/ui.py | 17 ++++--- 4 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/ifcblenderexport/blenderbim/bim/module/csv/__init__.py b/src/ifcblenderexport/blenderbim/bim/module/csv/__init__.py index f8e236dc26..3e0e3722b9 100644 --- a/src/ifcblenderexport/blenderbim/bim/module/csv/__init__.py +++ b/src/ifcblenderexport/blenderbim/bim/module/csv/__init__.py @@ -7,6 +7,7 @@ classes = ( operator.ExportIfcCsv, operator.ImportIfcCsv, operator.EyedropIfcCsv, + operator.SelectCsvIfcFile, prop.CsvProperties, ui.BIM_PT_ifccsv, ) diff --git a/src/ifcblenderexport/blenderbim/bim/module/csv/operator.py b/src/ifcblenderexport/blenderbim/bim/module/csv/operator.py index c3650146aa..1a54a1855e 100644 --- a/src/ifcblenderexport/blenderbim/bim/module/csv/operator.py +++ b/src/ifcblenderexport/blenderbim/bim/module/csv/operator.py @@ -9,24 +9,25 @@ import tempfile from blenderbim.bim.ifc import IfcStore - class AddCsvAttribute(bpy.types.Operator): bl_idname = "bim.add_csv_attribute" bl_label = "Add CSV Attribute" def execute(self, context): - attribute = bpy.context.scene.CsvProperties.csv_attributes.add() + attribute = context.scene.CsvProperties.csv_attributes.add() return {"FINISHED"} + class RemoveCsvAttribute(bpy.types.Operator): bl_idname = "bim.remove_csv_attribute" bl_label = "Remove CSV Attribute" index: bpy.props.IntProperty() def execute(self, context): - bpy.context.scene.CsvProperties.csv_attributes.remove(self.index) + context.scene.CsvProperties.csv_attributes.remove(self.index) return {"FINISHED"} + class ExportIfcCsv(bpy.types.Operator): bl_idname = "bim.export_ifccsv" bl_label = "Export IFC to CSV" @@ -42,21 +43,26 @@ class ExportIfcCsv(bpy.types.Operator): def execute(self, context): import ifccsv + props = context.scene.CsvProperties self.filepath = bpy.path.ensure_ext(self.filepath, ".csv") - ifc_file = ifcopenshell.open(bpy.context.scene.CsvProperties.csv_ifc_file) + if props.should_load_from_memory: + ifc_file = IfcStore.get_file() + else: + ifc_file = ifcopenshell.open(props.csv_ifc_file) selector = ifcopenshell.util.selector.Selector() - results = selector.parse(ifc_file, bpy.context.scene.CsvProperties.ifc_selector) + results = selector.parse(ifc_file, props.ifc_selector) ifc_csv = ifccsv.IfcCsv() ifc_csv.output = self.filepath - ifc_csv.attributes = [a.name for a in bpy.context.scene.CsvProperties.csv_attributes] + ifc_csv.attributes = [a.name for a in props.csv_attributes] ifc_csv.selector = selector - if bpy.context.scene.CsvProperties.csv_delimiter == "CUSTOM": - ifc_csv.delimiter = bpy.context.scene.CsvProperties.csv_custom_delimiter + if props.csv_delimiter == "CUSTOM": + ifc_csv.delimiter = props.csv_custom_delimiter else: - ifc_csv.delimiter = bpy.context.scene.CsvProperties.csv_delimiter + ifc_csv.delimiter = props.csv_delimiter ifc_csv.export(ifc_file, results) return {"FINISHED"} + class ImportIfcCsv(bpy.types.Operator): bl_idname = "bim.import_ifccsv" bl_label = "Import CSV to IFC" @@ -74,18 +80,35 @@ class ImportIfcCsv(bpy.types.Operator): ifc_csv = ifccsv.IfcCsv() ifc_csv.output = self.filepath - ifc_csv.Import(bpy.context.scene.CsvProperties.csv_ifc_file) + ifc_csv.Import(context.scene.CsvProperties.csv_ifc_file) return {"FINISHED"} + class EyedropIfcCsv(bpy.types.Operator): bl_idname = "bim.eyedrop_ifccsv" bl_label = "Query Selected Items" def execute(self, context): global_ids = [] - for obj in bpy.context.selected_objects: + for obj in context.selected_objects: if hasattr(obj, "BIMObjectProperties") and obj.BIMObjectProperties.attributes.get("GlobalId"): global_ids.append("#" + obj.BIMObjectProperties.attributes.get("GlobalId").string_value) - bpy.context.scene.CsvProperties.ifc_selector = "|".join(global_ids) + context.scene.CsvProperties.ifc_selector = "|".join(global_ids) return {"FINISHED"} + +class SelectCsvIfcFile(bpy.types.Operator): + bl_idname = "bim.select_csv_ifc_file" + bl_label = "Select CSV IFC File" + filename_ext = ".ifc" + filter_glob: bpy.props.StringProperty(default="*.ifc;*.ifczip;*.ifcxml", options={"HIDDEN"}) + filepath: bpy.props.StringProperty(subtype="FILE_PATH") + + + def execute(self, context): + context.scene.CsvProperties.csv_ifc_file = self.filepath + return {"FINISHED"} + + def invoke(self, context, event): + context.window_manager.fileselect_add(self) + return {"RUNNING_MODAL"} diff --git a/src/ifcblenderexport/blenderbim/bim/module/csv/prop.py b/src/ifcblenderexport/blenderbim/bim/module/csv/prop.py index 24a9915f7a..a21893cc94 100644 --- a/src/ifcblenderexport/blenderbim/bim/module/csv/prop.py +++ b/src/ifcblenderexport/blenderbim/bim/module/csv/prop.py @@ -18,10 +18,14 @@ class CsvProperties(PropertyGroup): ifc_selector: StringProperty(default="", name="IFC Selector") csv_attributes: CollectionProperty(name="CSV Attributes", type=StrProperty) csv_delimiter: EnumProperty( - items=[(";", ";", ""), (",", ",", ""), (".", ".", ""), ("CUSTOM", "Custom", ""),], + items=[ + (";", ";", ""), + (",", ",", ""), + (".", ".", ""), + ("CUSTOM", "Custom", ""), + ], name="IFC CSV Delimiter", default=",", ) csv_custom_delimiter: StringProperty(default="", name="Custom Delimiter") should_load_from_memory: BoolProperty(default=False, name="Load from Memory") - diff --git a/src/ifcblenderexport/blenderbim/bim/module/csv/ui.py b/src/ifcblenderexport/blenderbim/bim/module/csv/ui.py index f478682eb2..025dfc7396 100644 --- a/src/ifcblenderexport/blenderbim/bim/module/csv/ui.py +++ b/src/ifcblenderexport/blenderbim/bim/module/csv/ui.py @@ -16,20 +16,20 @@ class BIM_PT_ifccsv(Panel): scene = context.scene props = scene.CsvProperties - if IfcStore.get_file(): + if IfcStore.get_file(): row = layout.row() row.prop(props, "should_load_from_memory") - + if not IfcStore.get_file() or not props.should_load_from_memory: row = layout.row(align=True) row.prop(props, "csv_ifc_file") - row.operator("bim.import_ifccsv", icon="FILE_FOLDER", text="") - row = layout.row(align=True) - row.prop(props, "ifc_selector") - row.operator("bim.eyedrop_ifccsv", icon="EYEDROPPER", text="") + row.operator("bim.select_csv_ifc_file", icon="FILE_FOLDER", text="") + + row = layout.row(align=True) + row.prop(props, "ifc_selector") + row.operator("bim.eyedrop_ifccsv", icon="EYEDROPPER", text="") row = layout.row() - row.label(text="Add IFC attributes to filter", icon="FILE_BLANK") row.operator("bim.add_csv_attribute") for index, attribute in enumerate(props.csv_attributes): @@ -40,11 +40,10 @@ class BIM_PT_ifccsv(Panel): row = layout.row(align=True) row.prop(props, "csv_delimiter") - if(props.csv_delimiter == 'CUSTOM'): + if props.csv_delimiter == "CUSTOM": row = layout.row(align=True) row.prop(props, "csv_custom_delimiter") row = layout.row(align=True) row.operator("bim.export_ifccsv", icon="EXPORT") row.operator("bim.import_ifccsv", icon="IMPORT") -