From 4689cc858aee27bffdddd71abe1e1b0be0ffad25 Mon Sep 17 00:00:00 2001 From: bosonprojets Date: Fri, 9 Sep 2022 02:23:53 +0100 Subject: [PATCH] BBIM Feature to import resources data from CSV --- .../bim/module/resource/__init__.py | 1 + .../bim/module/resource/operator.py | 15 ++++++++ .../blenderbim/bim/module/resource/ui.py | 2 ++ src/blenderbim/blenderbim/core/tool.py | 1 + .../ifcresources => ifc4d/ifc4d}/csv2ifc.py | 34 +++++++++++-------- .../ifc4d}/resources.csv | 0 6 files changed, 38 insertions(+), 15 deletions(-) rename src/{ifcresources/ifcresources => ifc4d/ifc4d}/csv2ifc.py (85%) rename src/{ifcresources/ifcresources => ifc4d/ifc4d}/resources.csv (100%) diff --git a/src/blenderbim/blenderbim/bim/module/resource/__init__.py b/src/blenderbim/blenderbim/bim/module/resource/__init__.py index b2bdd02794..03cf738203 100644 --- a/src/blenderbim/blenderbim/bim/module/resource/__init__.py +++ b/src/blenderbim/blenderbim/bim/module/resource/__init__.py @@ -48,6 +48,7 @@ classes = ( operator.DisableEditingResourceQuantity, operator.DisableEditingResourceCostValue, operator.CalculateResourceWork, + operator.ImportResources, prop.Resource, prop.BIMResourceProperties, prop.BIMResourceTreeProperties, diff --git a/src/blenderbim/blenderbim/bim/module/resource/operator.py b/src/blenderbim/blenderbim/bim/module/resource/operator.py index b85825804c..573b9c49c3 100644 --- a/src/blenderbim/blenderbim/bim/module/resource/operator.py +++ b/src/blenderbim/blenderbim/bim/module/resource/operator.py @@ -330,3 +330,18 @@ class EditResourceQuantity(bpy.types.Operator, tool.Ifc.Operator): tool.Resource, tool.Ifc, physical_quantity=tool.Ifc.get().by_id(self.physical_quantity) ) + +class ImportResources(bpy.types.Operator, tool.Ifc.Operator, ImportHelper): + bl_idname = "import_resources.bim" + bl_label = "Import P6" + bl_options = {"REGISTER", "UNDO"} + filename_ext = ".csv" + filter_glob: bpy.props.StringProperty(default="*.csv", options={"HIDDEN"}) + + @classmethod + def poll(cls, context): + ifc_file = tool.Ifc.get() + return ifc_file is not None + + def _execute(self, context): + core.import_resources(tool.Resource, file_path=self.filepath) diff --git a/src/blenderbim/blenderbim/bim/module/resource/ui.py b/src/blenderbim/blenderbim/bim/module/resource/ui.py index 778512a120..1a816a37c6 100644 --- a/src/blenderbim/blenderbim/bim/module/resource/ui.py +++ b/src/blenderbim/blenderbim/bim/module/resource/ui.py @@ -54,10 +54,12 @@ class BIM_PT_resources(Panel): row.operator("bim.disable_resource_editing_ui", text="", icon="CANCEL") else: row.operator("bim.load_resources", text="Load Resources", icon="GREASEPENCIL") + row.operator("import_resources.bim", text="Import Resources", icon="IMPORT") if not self.props.is_editing: return self.draw_resource_operators() + self.layout.template_list( "BIM_UL_resources", "", diff --git a/src/blenderbim/blenderbim/core/tool.py b/src/blenderbim/blenderbim/core/tool.py index 25f14cea25..8cfaab8d94 100644 --- a/src/blenderbim/blenderbim/core/tool.py +++ b/src/blenderbim/blenderbim/core/tool.py @@ -427,6 +427,7 @@ class Resource: def get_resource_quantity_attributes(cls): pass def expand_resource(cls, resource): pass def contract_resource(cls, resource): pass + def import_resources(cls, file_path): pass @interface class Root: diff --git a/src/ifcresources/ifcresources/csv2ifc.py b/src/ifc4d/ifc4d/csv2ifc.py similarity index 85% rename from src/ifcresources/ifcresources/csv2ifc.py rename to src/ifc4d/ifc4d/csv2ifc.py index 28022e2956..108453b3e7 100644 --- a/src/ifcresources/ifcresources/csv2ifc.py +++ b/src/ifc4d/ifc4d/csv2ifc.py @@ -26,9 +26,7 @@ class Csv2Ifc: def __init__(self): self.csv = None self.file = None - self.resource_items = [] - self.cost_schedule = None - self.is_schedule_of_rates = False + self.resources = [] self.units = {} def execute(self): @@ -52,7 +50,7 @@ class Csv2Ifc: cost_data = self.get_row_resource_data(row) hierarchy_key = int(row[0]) if hierarchy_key == 1: - self.resource_items.append(cost_data) + self.resources.append(cost_data) else: self.parents[hierarchy_key - 1]["children"].append(cost_data) self.parents[hierarchy_key] = cost_data @@ -73,12 +71,12 @@ class Csv2Ifc: } return { - "Identification": str(identification) if identification else None, - "Name": str(name) if name else None, + "Identification": str(identification).strip() if identification else None, + "Name": str(name).strip() if name else None, "Type": type, "BaseCostValue": float(base_cost_value) if base_cost_value else None, "BaseCostQuantity": float(base_cost_quantity) if base_cost_quantity else None, - "Unit": str(base_cost_unit) if base_cost_unit else None, + "Unit": str(base_cost_unit).strip() if base_cost_unit else None, "Productivity": productivity if productivity["BaseQuantityProducedName"] else None, "children": [], } @@ -86,10 +84,10 @@ class Csv2Ifc: def create_ifc(self): if not self.file: self.create_boilerplate_ifc() - self.create_resources(self.resource_items) + self.create_resources(self.resources) - def create_resources(self, resource_items, parent=None): - for resource in resource_items: + def create_resources(self, resources, parent=None): + for resource in resources: self.create_resource(resource, parent) def create_resource(self, resource, parent): @@ -115,29 +113,35 @@ class Csv2Ifc: cost_value.AppliedValue = self.file.createIfcMonetaryMeasure(resource["BaseCostValue"]) if resource["Unit"]: measure_class = ifcopenshell.util.unit.get_symbol_measure_class(resource["Unit"]) + print(measure_class) value_component = self.file.create_entity(measure_class, resource["BaseCostQuantity"]) + print(value_component) unit_component = None if measure_class == "IfcNumericMeasure": unit_component = self.create_unit(resource["Unit"]) else: unit_type = ifcopenshell.util.unit.get_measure_unit_type(measure_class) + print(unit_type) unit_assignment = ifcopenshell.util.unit.get_unit_assignment(self.file) if unit_assignment: units = [u for u in unit_assignment.Units if getattr(u, "UnitType", None) == unit_type] if units: unit_component = units[0] if not unit_component: - unit_component = self.create_unit(resource["Unit"]) + unit_component = self.create_unit(resource["Unit"], unit_type) + print(unit_component) cost_value.UnitBasis = self.file.createIfcMeasureWithUnit(value_component, unit_component) self.create_resources(resource["children"], resource["ifc"]) - def create_unit(self, symbol): + def create_unit(self, symbol, unit_type): unit = self.units.get(symbol, None) if unit: return unit - unit = self.file.createIfcContextDependentUnit( - self.file.createIfcDimensionalExponents(0, 0, 0, 0, 0, 0, 0), "USERDEFINED", symbol - ) + else: + unit = ifcopenshell.api.run("unit.add_si_unit", self.file, unit_type=unit_type, name=ifcopenshell.util.unit.si_type_names.get(unit_type, None)) + # unit = self.file.createIfcContextDependentUnit( + # self.file.createIfcDimensionalExponents(0, 0, 0, 0, 0, 0, 0), "USERDEFINED", symbol + # ) self.units[symbol] = unit return unit diff --git a/src/ifcresources/ifcresources/resources.csv b/src/ifc4d/ifc4d/resources.csv similarity index 100% rename from src/ifcresources/ifcresources/resources.csv rename to src/ifc4d/ifc4d/resources.csv