From 9a02fc02dbb0ebd320bb0ac76fc68e531cc77c20 Mon Sep 17 00:00:00 2001 From: Sigma Dimensions <79010126+myoualid@users.noreply.github.com> Date: Wed, 31 May 2023 16:21:19 +0100 Subject: [PATCH] Exporting cost schedules opens the folder directory --- .../blenderbim/bim/module/cost/operator.py | 4 ++- src/blenderbim/blenderbim/core/cost.py | 3 +- src/blenderbim/blenderbim/tool/cost.py | 21 +++++++++--- src/ifc5d/ifc5d/ifc5Dspreadsheet.py | 32 +++++++++++-------- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/cost/operator.py b/src/blenderbim/blenderbim/bim/module/cost/operator.py index c76178e320..a3152e87e1 100644 --- a/src/blenderbim/blenderbim/bim/module/cost/operator.py +++ b/src/blenderbim/blenderbim/bim/module/cost/operator.py @@ -634,7 +634,9 @@ class ExportCostSchedules(bpy.types.Operator): def execute(self, context): cost_schedule = tool.Ifc.get().by_id(self.cost_schedule) if self.cost_schedule else None - core.export_cost_schedules(tool.Cost, format=self.format, cost_schedule=cost_schedule) + r = core.export_cost_schedules(tool.Cost, format=self.format, cost_schedule=cost_schedule) + if isinstance(r, str): + self.report({"ERROR"}, r) return {"FINISHED"} def invoke(self, context, event): diff --git a/src/blenderbim/blenderbim/core/cost.py b/src/blenderbim/blenderbim/core/cost.py index e96541e52b..f22ad4dce4 100644 --- a/src/blenderbim/blenderbim/core/cost.py +++ b/src/blenderbim/blenderbim/core/cost.py @@ -237,7 +237,8 @@ def calculate_cost_item_resource_value(ifc, cost_item): def export_cost_schedules(cost, format, cost_schedule=None): - cost.export_cost_schedules(format, cost_schedule) + cost.play_sound() + return cost.export_cost_schedules(format, cost_schedule) def clear_cost_item_assignments(ifc, cost, cost_item, related_object_type): diff --git a/src/blenderbim/blenderbim/tool/cost.py b/src/blenderbim/blenderbim/tool/cost.py index 5079729cec..b32d9d84a7 100644 --- a/src/blenderbim/blenderbim/tool/cost.py +++ b/src/blenderbim/blenderbim/tool/cost.py @@ -499,11 +499,14 @@ class Cost(blenderbim.core.tool.Cost): @classmethod def export_cost_schedules(cls, format=None, cost_schedule=None): - path = os.path.join(bpy.context.scene.BIMProperties.data_dir, "build", "cost_schedules") + import subprocess + import os + import sys + path = os.path.join(bpy.context.scene.BIMProperties.data_dir, "cost_schedules") + if not os.path.exists(path): + os.makedirs(path) if format == "CSV": from ifc5d.ifc5Dspreadsheet import Ifc5DCsvWriter - if not os.path.exists(path): - os.makedirs(path) writer = Ifc5DCsvWriter(file=tool.Ifc.get(), output=path, cost_schedule=cost_schedule) writer.write() elif format == "ODS": @@ -516,6 +519,16 @@ class Cost(blenderbim.core.tool.Cost): writer = Ifc5DXlsxWriter(file=tool.Ifc.get(), output=path, cost_schedule=cost_schedule ) writer.write() + try: + if path: + if sys.platform == "win32": + os.startfile(path) + elif sys.platform == "darwin": + subprocess.call(["open", path]) + elif sys.platform == "linux": + subprocess.call(["xdg-open", path]) + except: + return 'Could not open file location' @classmethod def get_units(cls): @@ -659,4 +672,4 @@ class Cost(blenderbim.core.tool.Cost): @classmethod def has_schedules(cls): - return bool(tool.Ifc.get().by_type("IfcCostSchedule")) \ No newline at end of file + return bool(tool.Ifc.get().by_type("IfcCostSchedule")) diff --git a/src/ifc5d/ifc5d/ifc5Dspreadsheet.py b/src/ifc5d/ifc5d/ifc5Dspreadsheet.py index 3fa9e9b1c5..8e2b739cbb 100644 --- a/src/ifc5d/ifc5d/ifc5Dspreadsheet.py +++ b/src/ifc5d/ifc5d/ifc5Dspreadsheet.py @@ -30,10 +30,10 @@ import ifcopenshell.util.date class IfcDataGetter: @staticmethod - def get_schedules(file): - if not file: - return None - return file.by_type("IfcCostSchedule") + def get_schedules(file, filter_by_schedule=None): + return [ + schedule for schedule in file.by_type("IfcCostSchedule") if not filter_by_schedule or schedule == filter_by_schedule + ] @staticmethod def canonicalise_time(time): @@ -221,10 +221,6 @@ class Ifc5Dwriter: self.used_names = [] for i in range(26): self.column_indexes.append("ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i % 26]) - if self.cost_schedule: - for cost_schedule in self.cost_schedules: - if cost_schedule.id() != self.cost_schedule.id(): - self.cost_schedules.remove(cost_schedule) for cost_schedule in self.cost_schedules: sheet_id = cost_schedule.id() self.sheet_data[sheet_id] = {} @@ -269,7 +265,7 @@ class Ifc5Dwriter: return "{}{}".format(self.column_indexes[attribute], self.row_count) def write(self): - self.cost_schedules = IfcDataGetter.get_schedules(self.file) + self.cost_schedules = IfcDataGetter.get_schedules(self.file, self.cost_schedule) self.sheet_data = {} self.parse() @@ -311,10 +307,14 @@ class Ifc5DOdsWriter(Ifc5Dwriter): ns1.addElement(Number(decimalplaces="2", minintegerdigits="1", grouping="true")) self.doc.styles.addElement(ns1) + file_name = "" for cost_schedule in self.cost_schedules: + if file_name: + file_name += "_" + cost_schedule.Name or "" + else: + file_name += cost_schedule.Name or "" self.write_table(cost_schedule) - - self.doc.save(self.output, True) + self.doc.save(os.path.join(self.output, file_name), True) def write_table(self, cost_schedule): from odf.table import Table, TableRow, TableCell @@ -408,8 +408,14 @@ class Ifc5DXlsxWriter(Ifc5Dwriter): import xlsxwriter super().write() - - self.workbook = xlsxwriter.Workbook(self.output + ".xlsx") + file_name = "" + for cost_schedule in self.cost_schedules: + if file_name: + file_name += "_" + cost_schedule.Name or "" + else: + file_name += cost_schedule.Name or "" + self.file_path = os.path.join(self.output, "{}.xlsx".format(file_name)) + self.workbook = xlsxwriter.Workbook(self.file_path) self.used_names = [] for cost_schedule in self.cost_schedules: self.write_table(cost_schedule)