Csv2Ifc - move arguments to init for a simpler setup

This commit is contained in:
Andrej730
2025-04-17 16:58:59 +05:00
parent 209caec49a
commit 37a0b3f6a9
4 changed files with 52 additions and 19 deletions
+1 -4
View File
@@ -568,10 +568,7 @@ class Cost(bonsai.core.tool.Cost):
import time import time
start = time.time() start = time.time()
csv2ifc = Csv2Ifc() csv2ifc = Csv2Ifc(file_path, tool.Ifc.get(), is_schedule_of_rates=is_schedule_of_rates)
csv2ifc.csv = file_path
csv2ifc.file = tool.Ifc.get()
csv2ifc.is_schedule_of_rates = is_schedule_of_rates
csv2ifc.execute() csv2ifc.execute()
print("Import finished in {:.2f} seconds".format(time.time() - start)) print("Import finished in {:.2f} seconds".format(time.time() - start))
+18 -1
View File
@@ -36,9 +36,26 @@ E.g. '1', '1.1', '1.1.1', etc.
E.g. root items of the same level have index '1', their children have '2', etc. E.g. root items of the same level have index '1', their children have '2', etc.
- 'Index' was preferred for import hierarchy source over 'Hierarchy' as it's easier to edit from the table view. - 'Index' was preferred for import hierarchy source over 'Hierarchy' as it's easier to edit from the table view.
Simple example:
```python
import ifc5d.csv2ifc
csv2ifc = ifc5d.csv2ifc.Csv2Ifc(csv_filepath, ifc_file)
csv2ifc.execute()
```
## Usage IFC to CSV, ODS, XSLS ## Usage IFC to CSV, ODS, XSLS
#TODO Simple example:
```python
import ifc5d.ifc5Dspreadsheet
# csv.
writer = ifc5d.ifc5Dspreadsheet.Ifc5DCsvWriter(ifc_file, temp_csv_dir)
writer.write()
```
### CLI app for converting IFC files to CSV, ODS or XLSX format. ### CLI app for converting IFC files to CSV, ODS or XLSX format.
+25 -5
View File
@@ -58,11 +58,8 @@ class Csv2Ifc:
# Inputs. # Inputs.
csv: str csv: str
file: Union[ifcopenshell.file, None] = None file: Union[ifcopenshell.file, None] = None
"""If not provided, boilerplate file will be created."""
cost_schedule: Union[ifcopenshell.entity_instance, None] = None cost_schedule: Union[ifcopenshell.entity_instance, None] = None
"""Cost schedule to load cost items to. If not provided, new one will be created."""
is_schedule_of_rates: bool = False is_schedule_of_rates: bool = False
"""Whether imported schedule is a schedule of rates."""
# Output. # Output.
cost_items: list[CostItem] cost_items: list[CostItem]
@@ -71,10 +68,33 @@ class Csv2Ifc:
headers: CsvHeader headers: CsvHeader
units: dict[str, ifcopenshell.entity_instance] units: dict[str, ifcopenshell.entity_instance]
def __init__(self): def __init__(
self.units: dict[str, ifcopenshell.entity_instance] = {} self,
csv: str = None,
ifc_file: Union[ifcopenshell.file, None] = None,
cost_schedule: Union[ifcopenshell.entity_instance, None] = None,
*,
is_schedule_of_rates: bool = False,
):
"""
:param csv: CSV filepath to import.
:param ifc_file: IFC file to import to. If not provided, boilerplate file will be created.
:param cost_schedule: Cost schedule to load cost items to. If not provided, new one will be created.
:param is_schedule_of_rates: Whether imported schedule is a schedule of rates.
"""
# TODO: Arguments added only at 25.04.14
# `csv` argument is actually not optional, it's only optional for backwards compatibility.
# And will be deprecated later.
if csv is None:
print("WARNING. `csv` argument is not optional and should be provided to the constructor.")
self.csv = csv
self.file = ifc_file
self.cost_schedule = cost_schedule
self.is_schedule_of_rates = is_schedule_of_rates
self.units = {}
def execute(self) -> None: def execute(self) -> None:
assert self.csv
self.parse_csv() self.parse_csv()
self.create_ifc() self.create_ifc()
+8 -9
View File
@@ -235,9 +235,7 @@ class Ifc5Dwriter:
# Inputs. # Inputs.
file: ifcopenshell.file file: ifcopenshell.file
output: str output: str
"""Output filepath."""
cost_schedule: Union[ifcopenshell.entity_instance, None] cost_schedule: Union[ifcopenshell.entity_instance, None]
"""Cost schedule to export. If not provided - export all."""
colors: dict[int, str] colors: dict[int, str]
"""Colors to use for hierarchy indices.""" """Colors to use for hierarchy indices."""
@@ -266,8 +264,9 @@ class Ifc5Dwriter:
cost_schedule: Optional[ifcopenshell.entity_instance] = None, cost_schedule: Optional[ifcopenshell.entity_instance] = None,
): ):
""" """
Args: :param file: IFC file to exprot cost schedules from.
cost_schedule: exported cost schedule. If not provided, will export all available schedules. :param output: Output directory for csv files.
:param cost_schedule: exported cost schedule. If not provided, will export all available schedules.
""" """
self.output = output self.output = output
if isinstance(file, str): if isinstance(file, str):
@@ -277,7 +276,7 @@ class Ifc5Dwriter:
self.cost_schedule = cost_schedule self.cost_schedule = cost_schedule
self.colours = self.default_colors.copy() self.colours = self.default_colors.copy()
def parse(self): def parse(self) -> None:
"""Fill ``sheet_data`` from ``cost_schedules``.""" """Fill ``sheet_data`` from ``cost_schedules``."""
self.sheet_data = {} self.sheet_data = {}
counter: Counter[str] = Counter() counter: Counter[str] = Counter()
@@ -326,13 +325,13 @@ class Ifc5Dwriter:
attribute = get_position_in_list(attribute, self.sheet_data[schedule_id]["headers"]) attribute = get_position_in_list(attribute, self.sheet_data[schedule_id]["headers"])
return "{}{}".format(self.column_indexes[attribute], self.row_count) return "{}{}".format(self.column_indexes[attribute], self.row_count)
def write(self): def write(self) -> None:
self.cost_schedules = IfcDataGetter.get_schedules(self.file, self.cost_schedule) self.cost_schedules = IfcDataGetter.get_schedules(self.file, self.cost_schedule)
self.parse() self.parse()
class Ifc5DCsvWriter(Ifc5Dwriter): class Ifc5DCsvWriter(Ifc5Dwriter):
def write(self): def write(self) -> None:
import csv import csv
super().write() super().write()
@@ -348,7 +347,7 @@ class Ifc5DCsvWriter(Ifc5Dwriter):
class Ifc5DOdsWriter(Ifc5Dwriter): class Ifc5DOdsWriter(Ifc5Dwriter):
def write(self): def write(self) -> None:
from odf.opendocument import OpenDocumentSpreadsheet from odf.opendocument import OpenDocumentSpreadsheet
from odf.style import Style, TableCellProperties from odf.style import Style, TableCellProperties
from odf.number import NumberStyle, CurrencyStyle, CurrencySymbol, Number, Text from odf.number import NumberStyle, CurrencyStyle, CurrencySymbol, Number, Text
@@ -465,7 +464,7 @@ class Ifc5DOdsWriter(Ifc5Dwriter):
class Ifc5DXlsxWriter(Ifc5Dwriter): class Ifc5DXlsxWriter(Ifc5Dwriter):
def write(self): def write(self) -> None:
import xlsxwriter import xlsxwriter
super().write() super().write()