diff --git a/src/blenderbim/blenderbim/bim/module/csv/__init__.py b/src/blenderbim/blenderbim/bim/module/csv/__init__.py index 238b4698cb..6a2641d36f 100644 --- a/src/blenderbim/blenderbim/bim/module/csv/__init__.py +++ b/src/blenderbim/blenderbim/bim/module/csv/__init__.py @@ -22,10 +22,13 @@ from . import ui, prop, operator classes = ( operator.AddCsvAttribute, operator.RemoveCsvAttribute, + operator.RemoveAllCsvAttributes, operator.ExportIfcCsv, operator.ImportIfcCsv, operator.EyedropIfcCsv, operator.SelectCsvIfcFile, + operator.ImportCsvAttributes, + operator.ExportCsvAttributes, prop.CsvProperties, ui.BIM_PT_ifccsv, ) diff --git a/src/blenderbim/blenderbim/bim/module/csv/operator.py b/src/blenderbim/blenderbim/bim/module/csv/operator.py index e5f248821c..4f6859249c 100644 --- a/src/blenderbim/blenderbim/bim/module/csv/operator.py +++ b/src/blenderbim/blenderbim/bim/module/csv/operator.py @@ -48,6 +48,61 @@ class RemoveCsvAttribute(bpy.types.Operator): context.scene.CsvProperties.csv_attributes.remove(self.index) return {"FINISHED"} +class RemoveAllCsvAttributes(bpy.types.Operator): + bl_idname = "bim.remove_all_csv_attributes" + bl_label = "Remove all CSV Attributes" + bl_options = {"REGISTER", "UNDO"} + + def execute(self, context): + context.scene.CsvProperties.csv_attributes.clear() + return {"FINISHED"} + +class ImportCsvAttributes(bpy.types.Operator): + bl_idname = "bim.import_csv_attributes" + bl_label = "Import CSV Attributes" + bl_options = {"REGISTER", "UNDO"} + filter_glob: bpy.props.StringProperty(default="*.json", options={"HIDDEN"}) + filepath: bpy.props.StringProperty(subtype="FILE_PATH") + + def execute(self, context): + csv_attributes = context.scene.CsvProperties.csv_attributes + csv_attributes.clear() + csv_json = json.load(open(self.filepath)) + i = 0 + for attribute in csv_json: + csv_attributes.add() + csv_attributes[i].name = attribute + i +=1 + + return {"FINISHED"} + + def invoke(self, context, event): + context.window_manager.fileselect_add(self) + return {"RUNNING_MODAL"} + +class ExportCsvAttributes(bpy.types.Operator): + bl_idname = "bim.export_csv_attributes" + bl_label = "Export CSV Attributes" + bl_options = {"REGISTER", "UNDO"} + filename_ext = ".json" + filter_glob: bpy.props.StringProperty(default="*.json", options={"HIDDEN"}) + filepath: bpy.props.StringProperty(subtype="FILE_PATH") + + def execute(self, context): + csv_attributes = [] + with open(self.filepath, "w") as outfile: + for attribute in context.scene.CsvProperties.csv_attributes: + csv_attributes.append(attribute.name) + + json.dump(csv_attributes, outfile) + + return {"FINISHED"} + + def invoke(self, context, event): + self.filepath = bpy.path.ensure_ext(bpy.data.filepath, ".json") + WindowManager = context.window_manager + WindowManager.fileselect_add(self) + return {"RUNNING_MODAL"} class ExportIfcCsv(bpy.types.Operator): bl_idname = "bim.export_ifccsv" diff --git a/src/blenderbim/blenderbim/bim/module/csv/ui.py b/src/blenderbim/blenderbim/bim/module/csv/ui.py index 819401c612..1ee7e46271 100644 --- a/src/blenderbim/blenderbim/bim/module/csv/ui.py +++ b/src/blenderbim/blenderbim/bim/module/csv/ui.py @@ -42,19 +42,33 @@ class BIM_PT_ifccsv(Panel): row = layout.row(align=True) row.prop(props, "csv_ifc_file") 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="") + layout.separator() row = layout.row() - row.operator("bim.add_csv_attribute") + split = row.split(factor=0.7) + c = split.column() + c.operator("bim.add_csv_attribute") + c = split.column() + c.operator("bim.import_csv_attributes", icon="IMPORT", text="Load Template") for index, attribute in enumerate(props.csv_attributes): row = layout.row(align=True) row.prop(attribute, "name", text="") row.operator("bim.remove_csv_attribute", icon="X", text="").index = index + if props.csv_attributes: + row = layout.row() + row.label() + row.operator("bim.remove_all_csv_attributes", icon="CANCEL", text="") + row = layout.row() + row.operator("bim.export_csv_attributes", icon="EXPORT", text="Create Template") + + layout.separator() + row = layout.row(align=True) row.prop(props, "csv_delimiter") @@ -62,6 +76,9 @@ class BIM_PT_ifccsv(Panel): 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") + row = layout.row() + split = row.split(factor=0.7) + c = split.column() + c.operator("bim.export_ifccsv", icon="EXPORT") + c = split.column() + c.operator("bim.import_ifccsv", icon="IMPORT")