Remove CostSchedulesMapping and added csv_filepaths to data.py

This commit is contained in:
falken10
2025-06-24 19:08:09 +02:00
committed by Massimo Fabbro
parent 6aa6803490
commit e6747b258c
5 changed files with 58 additions and 47 deletions
@@ -84,7 +84,6 @@ classes = (
prop.CostItem, prop.CostItem,
prop.CostItemQuantity, prop.CostItemQuantity,
prop.CostItemType, prop.CostItemType,
prop.CostSchedulesMapping,
prop.ScheduleColumn, prop.ScheduleColumn,
prop.BIMCostProperties, prop.BIMCostProperties,
ui.BIM_PT_cost_schedules, ui.BIM_PT_cost_schedules,
+28
View File
@@ -56,9 +56,37 @@ 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
} }
cls.is_loaded = True cls.is_loaded = True
@classmethod
def get_csv_filepaths(cls) -> dict[int, str]:
"""Get CSV filepaths for cost schedules from document references."""
filepaths = {}
ifc_file = tool.Ifc.get()
cost_docs_document = next(
(
document
for document in ifc_file.by_type("IfcDocumentInformation")
if document.Name == "BBIM_Cost_Documents"
),
None,
)
if cost_docs_document:
references = tool.Document.get_document_references(cost_docs_document)
for reference in references:
if reference.Description and "Cost Schedule ID:" in reference.Description:
schedule_id_str = reference.Description.split("Cost Schedule ID:")[1].strip()
try:
schedule_id = int(schedule_id_str)
filepaths[schedule_id] = reference.Location
except ValueError:
pass
return filepaths
@classmethod @classmethod
def currency(cls) -> Union[Currency, None]: def currency(cls) -> Union[Currency, None]:
unit = tool.Unit.get_project_currency_unit() unit = tool.Unit.get_project_currency_unit()
+16 -2
View File
@@ -566,6 +566,7 @@ class ImportCostScheduleCsv(bpy.types.Operator, ImportHelper, tool.Ifc.Operator)
def _execute(self, context): def _execute(self, context):
from pathlib import Path 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:
@@ -579,6 +580,8 @@ class ImportCostScheduleCsv(bpy.types.Operator, ImportHelper, tool.Ifc.Operator)
cost_schedule = core.import_cost_schedule_csv(tool.Cost, str(resolved_path), self.is_schedule_of_rates) cost_schedule = core.import_cost_schedule_csv(tool.Cost, str(resolved_path), self.is_schedule_of_rates)
if cost_schedule: if cost_schedule:
core.add_csv_filepath(tool.Cost, store_path, self.is_schedule_of_rates, cost_schedule) core.add_csv_filepath(tool.Cost, store_path, self.is_schedule_of_rates, cost_schedule)
CostSchedulesData.is_loaded = False
return {"FINISHED"} return {"FINISHED"}
return {"CANCELLED"} return {"CANCELLED"}
@@ -588,7 +591,6 @@ class ImportCostScheduleCsv(bpy.types.Operator, ImportHelper, tool.Ifc.Operator)
row = self.layout.row() row = self.layout.row()
row.prop(self, "use_relative_path") row.prop(self, "use_relative_path")
class RefreshCostScheduleCsv(bpy.types.Operator, tool.Ifc.Operator): class RefreshCostScheduleCsv(bpy.types.Operator, tool.Ifc.Operator):
bl_idname = "bim.refresh_cost_schedule_csv" bl_idname = "bim.refresh_cost_schedule_csv"
bl_label = "Refresh Cost Schedule CSV" bl_label = "Refresh Cost Schedule CSV"
@@ -601,15 +603,26 @@ class RefreshCostScheduleCsv(bpy.types.Operator, tool.Ifc.Operator):
if not props.active_cost_schedule_id: if not props.active_cost_schedule_id:
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)
if not file_path:
cls.poll_message_set("No CSV file associated with this cost schedule")
return False
return True return True
def _execute(self, context): def _execute(self, context):
from pathlib import Path from pathlib import Path
from bonsai.bim.module.cost.data import CostSchedulesData
props = tool.Cost.get_cost_props() props = tool.Cost.get_cost_props()
cost_schedule_id = props.active_cost_schedule_id cost_schedule_id = props.active_cost_schedule_id
file_path = tool.Cost.get_cost_schedule_csv_filepath(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"}
resolved_path = Path(tool.Ifc.resolve_uri(file_path)) resolved_path = Path(tool.Ifc.resolve_uri(file_path))
if not resolved_path.exists(): if not resolved_path.exists():
self.report({"ERROR"}, f"File does not exist: '{file_path}' (resolved to '{resolved_path}')") self.report({"ERROR"}, f"File does not exist: '{file_path}' (resolved to '{resolved_path}')")
@@ -631,12 +644,13 @@ class RefreshCostScheduleCsv(bpy.types.Operator, tool.Ifc.Operator):
csv2ifc.refresh() csv2ifc.refresh()
tool.Cost.load_cost_schedule_tree() tool.Cost.load_cost_schedule_tree()
CostSchedulesData.is_loaded = False
return {"FINISHED"} return {"FINISHED"}
except Exception as e: except Exception as e:
self.report({"ERROR"}, f"Error refreshing CSV: {str(e)}") self.report({"ERROR"}, f"Error refreshing CSV: {str(e)}")
return {"CANCELLED"} return {"CANCELLED"}
class AddCostColumn(bpy.types.Operator): class AddCostColumn(bpy.types.Operator):
bl_idname = "bim.add_cost_column" bl_idname = "bim.add_cost_column"
bl_label = "Add Cost Column" bl_label = "Add Cost Column"
-12
View File
@@ -174,16 +174,6 @@ class CostItemQuantity(PropertyGroup):
unit_symbol: str unit_symbol: str
total_cost_quantity: float total_cost_quantity: float
class CostSchedulesMapping(PropertyGroup):
cost_schedule_id: IntProperty(name="cost_schedule_id")
csv_filepath: StringProperty(name="filepath")
if TYPE_CHECKING:
cost_schedule_id: int
csv_filepath: str
class CostItemType(PropertyGroup): class CostItemType(PropertyGroup):
name: StringProperty(name="Name") name: StringProperty(name="Name")
ifc_definition_id: IntProperty(name="IFC Definition ID") ifc_definition_id: IntProperty(name="IFC Definition ID")
@@ -289,7 +279,6 @@ class BIMCostProperties(PropertyGroup):
custom_currency: StringProperty( custom_currency: StringProperty(
name="Custom Currency", default="USD", description="Custom Currency in ISO 4217 format" name="Custom Currency", default="USD", description="Custom Currency in ISO 4217 format"
) )
cost_schedule_files: CollectionProperty(name="Cost Schedule Files", type=CostSchedulesMapping)
if TYPE_CHECKING: if TYPE_CHECKING:
cost_schedule_predefined_types: str cost_schedule_predefined_types: str
@@ -344,7 +333,6 @@ class BIMCostProperties(PropertyGroup):
show_cost_item_operators: bool show_cost_item_operators: bool
currency: str currency: str
custom_currency: str custom_currency: str
cost_schedule_files: bpy.types.bpy_prop_collection_idprop[CostSchedulesMapping]
@property @property
def active_cost_item(self) -> Union[CostItem, None]: def active_cost_item(self) -> Union[CostItem, None]:
+2 -20
View File
@@ -77,26 +77,8 @@ class BIM_PT_cost_schedules(Panel):
col.label(text="Linked CSV:") col.label(text="Linked CSV:")
row_1 = col.row(align=True) row_1 = col.row(align=True)
ifc_file = tool.Ifc.get() csv_filepaths = CostSchedulesData.data["csv_filepaths"]
cost_docs_document = next( file_path = csv_filepaths.get(self.props.active_cost_schedule_id)
(
document
for document in ifc_file.by_type("IfcDocumentInformation")
if document.Name == "BBIM_Cost_Documents"
),
None,
)
file_path = None
if cost_docs_document:
references = tool.Document.get_document_references(cost_docs_document)
for reference in references:
if (
reference.Description
and f"Cost Schedule ID: {self.props.active_cost_schedule_id}" in reference.Description
):
file_path = reference.Location
break
if file_path: if file_path:
row_1.label(text=file_path) row_1.label(text=file_path)