diff --git a/src/bonsai/bonsai/tool/cost.py b/src/bonsai/bonsai/tool/cost.py index 919e649b82..64f8026533 100644 --- a/src/bonsai/bonsai/tool/cost.py +++ b/src/bonsai/bonsai/tool/cost.py @@ -568,10 +568,7 @@ class Cost(bonsai.core.tool.Cost): import time start = time.time() - csv2ifc = Csv2Ifc() - csv2ifc.csv = file_path - csv2ifc.file = tool.Ifc.get() - csv2ifc.is_schedule_of_rates = is_schedule_of_rates + csv2ifc = Csv2Ifc(file_path, tool.Ifc.get(), is_schedule_of_rates=is_schedule_of_rates) csv2ifc.execute() print("Import finished in {:.2f} seconds".format(time.time() - start)) diff --git a/src/ifc5d/README.md b/src/ifc5d/README.md index c0d7d7f298..8eeee358c8 100644 --- a/src/ifc5d/README.md +++ b/src/ifc5d/README.md @@ -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. - '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 -#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. diff --git a/src/ifc5d/ifc5d/csv2ifc.py b/src/ifc5d/ifc5d/csv2ifc.py index 6a04a630e5..27424484af 100644 --- a/src/ifc5d/ifc5d/csv2ifc.py +++ b/src/ifc5d/ifc5d/csv2ifc.py @@ -58,11 +58,8 @@ class Csv2Ifc: # Inputs. csv: str file: Union[ifcopenshell.file, None] = None - """If not provided, boilerplate file will be created.""" 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 - """Whether imported schedule is a schedule of rates.""" # Output. cost_items: list[CostItem] @@ -71,10 +68,33 @@ class Csv2Ifc: headers: CsvHeader units: dict[str, ifcopenshell.entity_instance] - def __init__(self): - self.units: dict[str, ifcopenshell.entity_instance] = {} + def __init__( + 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: + assert self.csv self.parse_csv() self.create_ifc() diff --git a/src/ifc5d/ifc5d/ifc5Dspreadsheet.py b/src/ifc5d/ifc5d/ifc5Dspreadsheet.py index 17d005106b..b44ebb34e3 100644 --- a/src/ifc5d/ifc5d/ifc5Dspreadsheet.py +++ b/src/ifc5d/ifc5d/ifc5Dspreadsheet.py @@ -235,9 +235,7 @@ class Ifc5Dwriter: # Inputs. file: ifcopenshell.file output: str - """Output filepath.""" cost_schedule: Union[ifcopenshell.entity_instance, None] - """Cost schedule to export. If not provided - export all.""" colors: dict[int, str] """Colors to use for hierarchy indices.""" @@ -266,8 +264,9 @@ class Ifc5Dwriter: cost_schedule: Optional[ifcopenshell.entity_instance] = None, ): """ - Args: - cost_schedule: exported cost schedule. If not provided, will export all available schedules. + :param file: IFC file to exprot cost schedules from. + :param output: Output directory for csv files. + :param cost_schedule: exported cost schedule. If not provided, will export all available schedules. """ self.output = output if isinstance(file, str): @@ -277,7 +276,7 @@ class Ifc5Dwriter: self.cost_schedule = cost_schedule self.colours = self.default_colors.copy() - def parse(self): + def parse(self) -> None: """Fill ``sheet_data`` from ``cost_schedules``.""" self.sheet_data = {} counter: Counter[str] = Counter() @@ -326,13 +325,13 @@ class Ifc5Dwriter: attribute = get_position_in_list(attribute, self.sheet_data[schedule_id]["headers"]) 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.parse() class Ifc5DCsvWriter(Ifc5Dwriter): - def write(self): + def write(self) -> None: import csv super().write() @@ -348,7 +347,7 @@ class Ifc5DCsvWriter(Ifc5Dwriter): class Ifc5DOdsWriter(Ifc5Dwriter): - def write(self): + def write(self) -> None: from odf.opendocument import OpenDocumentSpreadsheet from odf.style import Style, TableCellProperties from odf.number import NumberStyle, CurrencyStyle, CurrencySymbol, Number, Text @@ -465,7 +464,7 @@ class Ifc5DOdsWriter(Ifc5Dwriter): class Ifc5DXlsxWriter(Ifc5Dwriter): - def write(self): + def write(self) -> None: import xlsxwriter super().write()