From 9d6e7c2481ba0a826a9d2871a6a44c363eb102ea Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Sat, 18 Jul 2020 17:29:32 +1000 Subject: [PATCH] Allow for wildcard expansion in IFCCSV export --- .../blenderbim/bim/operator.py | 2 +- src/ifccsv/ifccsv.py | 30 +++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/ifcblenderexport/blenderbim/bim/operator.py b/src/ifcblenderexport/blenderbim/bim/operator.py index 1d4a9faeed..64eb348bf9 100644 --- a/src/ifcblenderexport/blenderbim/bim/operator.py +++ b/src/ifcblenderexport/blenderbim/bim/operator.py @@ -2815,7 +2815,7 @@ class ExportIfcCsv(bpy.types.Operator): ifc_csv = ifccsv.IfcCsv() ifc_csv.output = self.filepath ifc_csv.attributes = [a.name for a in bpy.context.scene.BIMProperties.csv_attributes] - ifc_csv.export(ifc_selector_parser.results) + ifc_csv.export(ifc_selector_parser.file, ifc_selector_parser.results) return {'FINISHED'} diff --git a/src/ifccsv/ifccsv.py b/src/ifccsv/ifccsv.py index c76af680e1..e099621be2 100644 --- a/src/ifccsv/ifccsv.py +++ b/src/ifccsv/ifccsv.py @@ -176,7 +176,7 @@ class IfcAttributeExtractor(): return getattr(element, key) elif '.' not in key: return None - pset_name, prop = key.split('.') + pset_name, prop = key.split('.', 1) pset = IfcAttributeExtractor.get_element_pset(element, pset_name) if pset: return IfcAttributeExtractor.get_pset_property(pset, prop) @@ -192,11 +192,11 @@ class IfcAttributeExtractor(): if '.' not in key: return if key[0:3] == 'Qto': - qto, prop = key.split('.') + qto, prop = key.split('.', 1) qto = IfcAttributeExtractor.get_element_qto(element, qto_name) if qto: return IfcAttributeExtractor.set_qto_property(qto, prop, value) - pset_name, prop = key.split('.') + pset_name, prop = key.split('.', 1) pset = IfcAttributeExtractor.get_element_pset(element, pset_name) if pset: return IfcAttributeExtractor.set_pset_property(pset, prop, value) @@ -257,13 +257,20 @@ class IfcCsv(): self.attributes = [] self.output = '' - def export(self, elements): + def export(self, ifc_file, elements): + self.ifc_file = ifc_file for element in elements: result = [] if hasattr(element, 'GlobalId'): result.append(element.GlobalId) else: result.append(None) + + for index, attribute in enumerate(self.attributes): + if '*' in attribute: + self.attributes.extend(self.get_wildcard_attributes(attribute)) + del(self.attributes[index]) + for attribute in self.attributes: result.append(IfcAttributeExtractor.get_element_key(element, attribute)) self.results.append(result) @@ -276,6 +283,19 @@ class IfcCsv(): for row in self.results: writer.writerow(row) + def get_wildcard_attributes(self, attribute): + results = set() + pset_qto_name = attribute.split('.', 1)[0] + for element in self.ifc_file.by_type('IfcPropertySet') + self.ifc_file.by_type('IfcElementQuantity'): + print(element) + if element.Name != pset_qto_name: + continue + if element.is_a('IfcPropertySet'): + results.update([p.Name for p in element.HasProperties]) + else: + results.update([p.Name for p in element.Quantities]) + return ['{}.{}'.format(pset_qto_name, n) for n in results] + def Import(self, ifc): ifc_file = ifcopenshell.open(ifc) with open(self.output, newline='', encoding='utf-8') as f: @@ -338,7 +358,7 @@ if __name__ == '__main__': ifc_csv = IfcCsv() ifc_csv.output = args.csv ifc_csv.attributes = args.arguments if args.arguments else [] - ifc_csv.export(ifc_selector_parser.results) + ifc_csv.export(ifc_selector_parser.file, ifc_selector_parser.results) elif getattr(args, 'import'): ifc_csv = IfcCsv() ifc_csv.output = args.csv