From 1779fe88ef410973e1fa67ab52b928a1a3044a44 Mon Sep 17 00:00:00 2001 From: ihabelaghoury <46285271+ihabelaghoury@users.noreply.github.com> Date: Tue, 12 Jan 2021 02:26:59 +0200 Subject: [PATCH] Add new feature to Load from IFCStore in Cobie (#1253) Co-authored-by: Ihab El Aghoury --- .../blenderbim/bim/module/cobie/__init__.py | 9 +++-- .../blenderbim/bim/module/cobie/operator.py | 36 ++++++++++++++----- .../blenderbim/bim/module/cobie/prop.py | 21 +++++++++++ .../blenderbim/bim/module/cobie/ui.py | 14 +++++--- src/ifcblenderexport/blenderbim/bim/prop.py | 4 --- src/ifccobie/cobie.py | 9 +++-- 6 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 src/ifcblenderexport/blenderbim/bim/module/cobie/prop.py diff --git a/src/ifcblenderexport/blenderbim/bim/module/cobie/__init__.py b/src/ifcblenderexport/blenderbim/bim/module/cobie/__init__.py index 2f4f1736ba..98091992a3 100644 --- a/src/ifcblenderexport/blenderbim/bim/module/cobie/__init__.py +++ b/src/ifcblenderexport/blenderbim/bim/module/cobie/__init__.py @@ -1,17 +1,20 @@ import bpy -from . import ui, operator +from . import ui, prop, operator classes = ( operator.SelectCobieIfcFile, operator.SelectCobieJsonFile, operator.ExecuteIfcCobie, + prop.COBieProperties, ui.BIM_PT_cobie, ) + def register(): - pass + bpy.types.Scene.COBieProperties = bpy.props.PointerProperty(type=prop.COBieProperties) def unregister(): - pass + del bpy.types.Scene.COBieProperties + diff --git a/src/ifcblenderexport/blenderbim/bim/module/cobie/operator.py b/src/ifcblenderexport/blenderbim/bim/module/cobie/operator.py index 32a95b59e7..9e12c090be 100644 --- a/src/ifcblenderexport/blenderbim/bim/module/cobie/operator.py +++ b/src/ifcblenderexport/blenderbim/bim/module/cobie/operator.py @@ -1,13 +1,20 @@ import bpy +import os +import logging +import ifcopenshell +import json +import webbrowser +import tempfile from blenderbim.bim.ifc import IfcStore + class SelectCobieIfcFile(bpy.types.Operator): bl_idname = "bim.select_cobie_ifc_file" bl_label = "Select COBie IFC File" filepath: bpy.props.StringProperty(subtype="FILE_PATH") def execute(self, context): - bpy.context.scene.BIMProperties.cobie_ifc_file = self.filepath + bpy.context.scene.COBieProperties.cobie_ifc_file = self.filepath return {"FINISHED"} def invoke(self, context, event): @@ -21,7 +28,7 @@ class SelectCobieJsonFile(bpy.types.Operator): filepath: bpy.props.StringProperty(subtype="FILE_PATH") def execute(self, context): - bpy.context.scene.BIMProperties.cobie_json_file = self.filepath + bpy.context.scene.COBieProperties.cobie_json_file = self.filepath return {"FINISHED"} def invoke(self, context, event): @@ -36,8 +43,13 @@ class ExecuteIfcCobie(bpy.types.Operator): def execute(self, context): from cobie import IfcCobieParser - - output_dir = os.path.dirname(bpy.context.scene.BIMProperties.cobie_ifc_file) + props = bpy.context.scene.COBieProperties + + output_dir = os.path.dirname(props.cobie_ifc_file) + + if props.should_load_from_memory: + output_dir = tempfile.gettempdir() + output = os.path.join(output_dir, "output") logger = logging.getLogger("IFCtoCOBie") fh = logging.FileHandler(os.path.join(output_dir, "cobie.log")) @@ -46,16 +58,22 @@ class ExecuteIfcCobie(bpy.types.Operator): logger = logging.getLogger("IFCtoCOBie") logger.addHandler(fh) selector = ifcopenshell.util.selector.Selector() - if bpy.context.scene.BIMProperties.cobie_json_file: - with open(bpy.context.scene.BIMProperties.cobie_json_file, "r") as f: + if props.cobie_json_file: + with open(props.cobie_json_file, "r") as f: custom_data = json.load(f) else: custom_data = {} parser = IfcCobieParser(logger, selector) + + ifc_file = IfcStore.get_file() + + if not (ifc_file and props.should_load_from_memory): + ifc_file = props.cobie_ifc_file + parser.parse( - bpy.context.scene.BIMProperties.cobie_ifc_file, - bpy.context.scene.BIMProperties.cobie_types, - bpy.context.scene.BIMProperties.cobie_components, + ifc_file, + props.cobie_types, + props.cobie_components, custom_data, ) if self.file_format == "xlsx": diff --git a/src/ifcblenderexport/blenderbim/bim/module/cobie/prop.py b/src/ifcblenderexport/blenderbim/bim/module/cobie/prop.py new file mode 100644 index 0000000000..095622298f --- /dev/null +++ b/src/ifcblenderexport/blenderbim/bim/module/cobie/prop.py @@ -0,0 +1,21 @@ +import bpy +from bpy.types import PropertyGroup +from bpy.props import ( + PointerProperty, + StringProperty, + EnumProperty, + BoolProperty, + IntProperty, + FloatProperty, + FloatVectorProperty, + CollectionProperty, +) + + +class COBieProperties(PropertyGroup): + cobie_ifc_file: StringProperty(default="", name="COBie IFC File") + cobie_types: StringProperty(default=".COBieType", name="COBie Types") + cobie_components: StringProperty(default=".COBie", name="COBie Components") + cobie_json_file: StringProperty(default="", name="COBie JSON File") + should_load_from_memory: BoolProperty(default=False, name="Load from Memory") + diff --git a/src/ifcblenderexport/blenderbim/bim/module/cobie/ui.py b/src/ifcblenderexport/blenderbim/bim/module/cobie/ui.py index 18c71f404c..cbce5b033e 100644 --- a/src/ifcblenderexport/blenderbim/bim/module/cobie/ui.py +++ b/src/ifcblenderexport/blenderbim/bim/module/cobie/ui.py @@ -15,11 +15,17 @@ class BIM_PT_cobie(Panel): layout.use_property_split = True scene = context.scene - props = scene.BIMProperties + props = scene.COBieProperties - row = layout.row(align=True) - row.prop(props, "cobie_ifc_file") - row.operator("bim.select_cobie_ifc_file", icon="FILE_FOLDER", text="") + + 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, "cobie_ifc_file") + row.operator("bim.select_cobie_ifc_file", icon="FILE_FOLDER", text="") row = layout.row() row.prop(props, "cobie_types") diff --git a/src/ifcblenderexport/blenderbim/bim/prop.py b/src/ifcblenderexport/blenderbim/bim/prop.py index 8f8806a1f2..5b7540b865 100644 --- a/src/ifcblenderexport/blenderbim/bim/prop.py +++ b/src/ifcblenderexport/blenderbim/bim/prop.py @@ -1315,10 +1315,6 @@ class BIMProperties(PropertyGroup): features_dir: StringProperty(default="", name="Features Directory", update=refreshFeaturesFiles) features_file: EnumProperty(items=getFeaturesFiles, name="Features File", update=refreshScenarios) scenario: EnumProperty(items=getScenarios, name="Scenario") - cobie_ifc_file: StringProperty(default="", name="COBie IFC File") - cobie_types: StringProperty(default=".COBieType", name="COBie Types") - cobie_components: StringProperty(default=".COBie", name="COBie Components") - cobie_json_file: StringProperty(default="", name="COBie JSON File") diff_json_file: StringProperty(default="", name="Diff JSON File") diff_old_file: StringProperty(default="", name="Diff Old IFC File") diff_new_file: StringProperty(default="", name="Diff New IFC File") diff --git a/src/ifccobie/cobie.py b/src/ifccobie/cobie.py index d744c2e5b5..41ed46612d 100755 --- a/src/ifccobie/cobie.py +++ b/src/ifccobie/cobie.py @@ -54,12 +54,17 @@ class IfcCobieParser: } self.default_date = (datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=-2177452801)).isoformat() - def parse(self, filename, type_query=".COBieType", component_query=".COBie", custom_data={}): + def parse(self, file, type_query=".COBieType", component_query=".COBie", custom_data={}): self.custom_data = custom_data for sheet in self.sheets: if sheet not in self.custom_data: self.custom_data[sheet] = {} - self.file = ifcopenshell.open(filename) + + if isinstance(file, str): + self.file = ifcopenshell.open(file) + else: + self.file = file + self.type_assets = self.selector.parse(self.file, type_query) self.component_assets = self.selector.parse(self.file, component_query) self.get_contacts()