refactor and black formatting

This commit is contained in:
falken10
2025-06-24 22:28:22 +02:00
committed by Massimo Fabbro
parent e6747b258c
commit ccd97c66ae
4 changed files with 50 additions and 44 deletions
+2 -1
View File
@@ -56,7 +56,7 @@ class CostSchedulesData:
"cost_values": cls.cost_values(), "cost_values": cls.cost_values(),
"quantity_types": cls.quantity_types(), "quantity_types": cls.quantity_types(),
"currency": cls.currency(), "currency": cls.currency(),
"csv_filepaths": cls.get_csv_filepaths(), # Add this line "csv_filepaths": cls.get_csv_filepaths(),
} }
cls.is_loaded = True cls.is_loaded = True
@@ -199,6 +199,7 @@ class CostSchedulesData:
# parametric_quantities.extend(quantities) # parametric_quantities.extend(quantities)
data["TotalCostQuantity"] = ifcopenshell.util.cost.get_total_quantity(cost_item) data["TotalCostQuantity"] = ifcopenshell.util.cost.get_total_quantity(cost_item)
data["UnitSymbol"] = "-" data["UnitSymbol"] = "-"
data["QuantityType"] = None
quantities: list[ifcopenshell.entity_instance] = cost_item.CostQuantities quantities: list[ifcopenshell.entity_instance] = cost_item.CostQuantities
if quantities: if quantities:
quantity = quantities[0] quantity = quantities[0]
+11 -39
View File
@@ -25,6 +25,9 @@ import bonsai.tool as tool
from bpy_extras.io_utils import ImportHelper, ExportHelper from bpy_extras.io_utils import ImportHelper, ExportHelper
import bonsai.tool as tool import bonsai.tool as tool
import bonsai.core.cost as core import bonsai.core.cost as core
from bonsai.bim.module.cost.data import CostSchedulesData
from pathlib import Path
from typing import get_args, TYPE_CHECKING, Literal from typing import get_args, TYPE_CHECKING, Literal
@@ -100,6 +103,7 @@ class EnableEditingCostItems(bpy.types.Operator, tool.Ifc.Operator):
def _execute(self, context): def _execute(self, context):
core.enable_editing_cost_items(tool.Cost, cost_schedule=tool.Ifc.get().by_id(self.cost_schedule)) core.enable_editing_cost_items(tool.Cost, cost_schedule=tool.Ifc.get().by_id(self.cost_schedule))
CostSchedulesData.is_loaded = False
class DisableEditingCostSchedule(bpy.types.Operator, tool.Ifc.Operator): class DisableEditingCostSchedule(bpy.types.Operator, tool.Ifc.Operator):
@@ -565,8 +569,6 @@ class ImportCostScheduleCsv(bpy.types.Operator, ImportHelper, tool.Ifc.Operator)
return True return True
def _execute(self, context): def _execute(self, context):
from pathlib import Path
from bonsai.bim.module.cost.data import CostSchedulesData
store_path = self.filepath store_path = self.filepath
if self.use_relative_path: if self.use_relative_path:
@@ -604,7 +606,7 @@ class RefreshCostScheduleCsv(bpy.types.Operator, tool.Ifc.Operator):
cls.poll_message_set("No active cost schedule") cls.poll_message_set("No active cost schedule")
return False return False
file_path = tool.Cost.get_cost_schedule_csv_filepath(props.active_cost_schedule_id) file_path = core.get_cost_schedule_csv_filepath(tool.Cost, props.active_cost_schedule_id)
if not file_path: if not file_path:
cls.poll_message_set("No CSV file associated with this cost schedule") cls.poll_message_set("No CSV file associated with this cost schedule")
return False return False
@@ -612,44 +614,14 @@ class RefreshCostScheduleCsv(bpy.types.Operator, tool.Ifc.Operator):
return True return True
def _execute(self, context): def _execute(self, context):
from pathlib import Path error_message = core.refresh_cost_schedule_csv(tool.Cost)
from bonsai.bim.module.cost.data import CostSchedulesData if error_message:
self.report({"ERROR"}, error_message)
props = tool.Cost.get_cost_props()
cost_schedule_id = props.active_cost_schedule_id
file_path = tool.Cost.get_cost_schedule_csv_filepath(cost_schedule_id)
if not file_path:
self.report({"ERROR"}, "No CSV file associated with this cost schedule")
return {"CANCELLED"} return {"CANCELLED"}
CostSchedulesData.is_loaded = False
resolved_path = Path(tool.Ifc.resolve_uri(file_path))
if not resolved_path.exists(): return {"FINISHED"}
self.report({"ERROR"}, f"File does not exist: '{file_path}' (resolved to '{resolved_path}')")
return {"CANCELLED"}
tool.Cost.delete_all_cost_items()
cost_schedule = tool.Ifc.get_entity_by_id(cost_schedule_id)
is_schedule_of_rates = tool.Cost.is_schedule_of_rates_csv(cost_schedule_id)
try:
from ifc5d.csv2ifc import Csv2Ifc
csv2ifc = Csv2Ifc()
csv2ifc.csv = str(resolved_path)
csv2ifc.file = tool.Ifc.get()
csv2ifc.cost_schedule = cost_schedule
csv2ifc.is_schedule_of_rates = is_schedule_of_rates
csv2ifc.refresh()
tool.Cost.load_cost_schedule_tree()
CostSchedulesData.is_loaded = False
return {"FINISHED"}
except Exception as e:
self.report({"ERROR"}, f"Error refreshing CSV: {str(e)}")
return {"CANCELLED"}
class AddCostColumn(bpy.types.Operator): class AddCostColumn(bpy.types.Operator):
bl_idname = "bim.add_cost_column" bl_idname = "bim.add_cost_column"
+1 -1
View File
@@ -222,7 +222,7 @@ class BIM_PT_cost_schedules(Panel):
quantities = CostSchedulesData.data["cost_quantities"] quantities = CostSchedulesData.data["cost_quantities"]
row = self.layout.row(align=True) row = self.layout.row(align=True)
# In IFC, all quantities of IfcCostTime should have 1 type. # In IFC, all quantities of IfcCostTime should have 1 type.
if quantities: if quantities and cost_item.get("QuantityType"):
quantity_class = cost_item["QuantityType"] quantity_class = cost_item["QuantityType"]
row.label(text=quantity_class) row.label(text=quantity_class)
else: else:
+36 -3
View File
@@ -18,12 +18,14 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING, Optional, Union, Literal from typing import TYPE_CHECKING, Optional, Union, Literal
import bonsai.tool as tool
import os
from bonsai.bim.module.cost.data import CostSchedulesData
if TYPE_CHECKING: if TYPE_CHECKING:
import bpy import bpy
import ifcopenshell import ifcopenshell
import ifcopenshell.util.cost import ifcopenshell.util.cost
import bonsai.tool as tool
def add_cost_schedule(ifc: type[tool.Ifc], name: str, predefined_type: str) -> None: def add_cost_schedule(ifc: type[tool.Ifc], name: str, predefined_type: str) -> None:
@@ -342,7 +344,13 @@ def select_cost_schedule_products(
def import_cost_schedule_csv( def import_cost_schedule_csv(
cost: type[tool.Cost], file_path: str, is_schedule_of_rates: bool cost: type[tool.Cost], file_path: str, is_schedule_of_rates: bool
) -> ifcopenshell.entity_instance: ) -> ifcopenshell.entity_instance:
cost_schedule = cost.import_cost_schedule_csv(file_path, is_schedule_of_rates)
resolved_path = tool.Ifc.resolve_uri(file_path)
if not os.path.exists(resolved_path):
print(f"Error: Could not find CSV file at {file_path} (resolved to {resolved_path})")
return None
cost_schedule = cost.import_cost_schedule_csv(resolved_path, is_schedule_of_rates)
return cost_schedule return cost_schedule
@@ -354,11 +362,28 @@ def remove_csv_filepath(cost: type[tool.Cost], cost_schedule) -> None:
cost.remove_csv_filepath(cost_schedule) cost.remove_csv_filepath(cost_schedule)
def refresh_cost_schedule_csv(ifc: type[tool.Ifc], cost: type[tool.Cost]) -> None: def refresh_cost_schedule_csv(cost: type[tool.Cost]) -> Optional[str]:
props = cost.get_cost_props()
cost_schedule_id = props.active_cost_schedule_id
file_path = cost.get_cost_schedule_csv_filepath(cost_schedule_id)
if not file_path:
return "No CSV file associated with this cost schedule"
resolved_path = tool.Ifc.resolve_uri(file_path)
if not os.path.exists(resolved_path):
return f"Could not find CSV file at {file_path} (resolved to {resolved_path})"
cost.delete_all_cost_items() cost.delete_all_cost_items()
cost.refresh_cost_schedule_csv() cost.refresh_cost_schedule_csv()
cost.load_cost_schedule_tree() cost.load_cost_schedule_tree()
CostSchedulesData.is_loaded = False
return None
def add_cost_column(cost: type[tool.Cost], name: str) -> None: def add_cost_column(cost: type[tool.Cost], name: str) -> None:
cost.add_cost_column(name) cost.add_cost_column(name)
@@ -460,3 +485,11 @@ def add_currency(ifc: type[tool.Ifc], cost: type[tool.Cost]) -> ifcopenshell.ent
def generate_cost_schedule_browser(cost: type[tool.Cost], cost_schedule: ifcopenshell.entity_instance) -> None: def generate_cost_schedule_browser(cost: type[tool.Cost], cost_schedule: ifcopenshell.entity_instance) -> None:
return cost.generate_cost_schedule_browser(cost_schedule) return cost.generate_cost_schedule_browser(cost_schedule)
def get_cost_schedule_csv_filepath(cost: type[tool.Cost], cost_schedule_id: int) -> Optional[str]:
return cost.get_cost_schedule_csv_filepath(cost_schedule_id)
def is_schedule_of_rates_csv(cost: type[tool.Cost], cost_schedule_id: int) -> bool:
return cost.is_schedule_of_rates_csv(cost_schedule_id)