ifccsv - support importing enum properties

This commit is contained in:
Andrej730
2024-05-06 18:50:36 +05:00
parent 28d205b4a3
commit 8f34752141
2 changed files with 23 additions and 11 deletions
@@ -282,6 +282,7 @@ class ImportIfcCsv(bpy.types.Operator):
empty=props.empty_value, empty=props.empty_value,
bool_true=props.true_value, bool_true=props.true_value,
bool_false=props.false_value, bool_false=props.false_value,
concat=props.concat_value
) )
if not props.should_load_from_memory: if not props.should_load_from_memory:
ifc_file.write(props.csv_ifc_file) ifc_file.write(props.csv_ifc_file)
+22 -11
View File
@@ -391,15 +391,16 @@ class IfcCsv:
empty: str = "", empty: str = "",
bool_true: str = "YES", bool_true: str = "YES",
bool_false: str = "NO", bool_false: str = "NO",
concat: str = ", ",
) -> None: ) -> None:
ext = table.split(".")[-1].lower() ext = table.split(".")[-1].lower()
if ext == "csv": if ext == "csv":
self.import_csv(ifc_file, table, attributes, delimiter, null, empty, bool_true, bool_false) self.import_csv(ifc_file, table, attributes, delimiter, null, empty, bool_true, bool_false, concat)
elif ext == "ods": elif ext == "ods":
self.import_ods(ifc_file, table, attributes, null, empty, bool_true, bool_false) self.import_ods(ifc_file, table, attributes, null, empty, bool_true, bool_false, concat)
elif ext == "xlsx": elif ext == "xlsx":
self.import_xlsx(ifc_file, table, attributes, null, empty, bool_true, bool_false) self.import_xlsx(ifc_file, table, attributes, null, empty, bool_true, bool_false, concat)
def import_csv( def import_csv(
self, self,
@@ -411,6 +412,7 @@ class IfcCsv:
empty: str = "", empty: str = "",
bool_true: str = "YES", bool_true: str = "YES",
bool_false: str = "NO", bool_false: str = "NO",
concat: str = ", ",
) -> None: ) -> None:
with open(table, newline="", encoding="utf-8") as f: with open(table, newline="", encoding="utf-8") as f:
reader = csv.reader(f, delimiter=delimiter) reader = csv.reader(f, delimiter=delimiter)
@@ -423,17 +425,17 @@ class IfcCsv:
elif len(attributes) == len(headers) - 1: elif len(attributes) == len(headers) - 1:
attributes.insert(0, "") # The GlobalId column attributes.insert(0, "") # The GlobalId column
continue continue
self.process_row(ifc_file, row, headers, attributes, null, empty, bool_true, bool_false) self.process_row(ifc_file, row, headers, attributes, null, empty, bool_true, bool_false, concat)
def import_xlsx(self, ifc_file, table, attributes, null, empty, bool_true, bool_false): def import_xlsx(self, ifc_file, table, attributes, null, empty, bool_true, bool_false, concat):
df = pd.read_excel(table) df = pd.read_excel(table)
self.import_pd(ifc_file, df, attributes, null, empty, bool_true, bool_false) self.import_pd(ifc_file, df, attributes, null, empty, bool_true, bool_false, concat)
def import_ods(self, ifc_file, table, attributes, null, empty, bool_true, bool_false): def import_ods(self, ifc_file, table, attributes, null, empty, bool_true, bool_false, concat):
df = pd.read_excel(table, engine="odf") df = pd.read_excel(table, engine="odf")
self.import_pd(ifc_file, df, attributes, null, empty, bool_true, bool_false) self.import_pd(ifc_file, df, attributes, null, empty, bool_true, bool_false, concat)
def import_pd(self, ifc_file, df, attributes=None, null="-", empty="", bool_true="YES", bool_false="NO"): def import_pd(self, ifc_file, df, attributes=None, null="-", empty="", bool_true="YES", bool_false="NO", concat=", "):
headers = df.columns.tolist() headers = df.columns.tolist()
if not attributes: if not attributes:
@@ -442,7 +444,7 @@ class IfcCsv:
attributes.insert(0, "") # The GlobalId column attributes.insert(0, "") # The GlobalId column
for _, row in df.iterrows(): for _, row in df.iterrows():
self.process_row(ifc_file, row.tolist(), headers, attributes, null, empty, bool_true, bool_false) self.process_row(ifc_file, row.tolist(), headers, attributes, null, empty, bool_true, bool_false, concat)
def process_row( def process_row(
self, self,
@@ -454,6 +456,7 @@ class IfcCsv:
empty: str, empty: str,
bool_true: str, bool_true: str,
bool_false: str, bool_false: str,
concat: str
) -> None: ) -> None:
try: try:
element = ifc_file.by_guid(row[0]) element = ifc_file.by_guid(row[0])
@@ -472,7 +475,14 @@ class IfcCsv:
elif value == bool_false: elif value == bool_false:
value = False value = False
key = attributes[i] or headers[i] key = attributes[i] or headers[i]
ifcopenshell.util.selector.set_element_value(ifc_file, element, key, value) try:
ifcopenshell.util.selector.set_element_value(ifc_file, element, key, value)
except ValueError as e:
if "enum property" in e.args[0]:
value = value.split(concat)
ifcopenshell.util.selector.set_element_value(ifc_file, element, key, value)
else:
raise e
if __name__ == "__main__": if __name__ == "__main__":
@@ -534,5 +544,6 @@ if __name__ == "__main__":
delimiter=args.delimiter, delimiter=args.delimiter,
null=args.null, null=args.null,
empty=args.empty, empty=args.empty,
concat=args.concat
) )
ifc_file.write(args.ifc) ifc_file.write(args.ifc)