From 34fc2136c68f928fb19e744fc7761b15110724e7 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Sun, 9 Aug 2020 18:10:30 +1000 Subject: [PATCH] Fix #839. Add upstream support for IFCJSON, including compact, v4, and v5a --- src/ifcblenderexport/Makefile | 7 ++ .../blenderbim/bim/export_ifc.py | 17 ++- .../blenderbim/bim/ifc2json.py | 106 ------------------ src/ifcblenderexport/blenderbim/bim/prop.py | 2 + src/ifcblenderexport/blenderbim/bim/ui.py | 4 + 5 files changed, 26 insertions(+), 110 deletions(-) delete mode 100644 src/ifcblenderexport/blenderbim/bim/ifc2json.py diff --git a/src/ifcblenderexport/Makefile b/src/ifcblenderexport/Makefile index 249cd94565..e82b077b3c 100644 --- a/src/ifcblenderexport/Makefile +++ b/src/ifcblenderexport/Makefile @@ -267,6 +267,13 @@ endif # Provides IFCCSV functionality cd dist/blenderbim/libs/site/packages/ && wget https://raw.githubusercontent.com/IfcOpenShell/IfcOpenShell/v0.6.0/src/ifccsv/ifccsv.py + # Provides IFCJSON functionality + mkdir dist/working + cd dist/working && wget https://github.com/IFCJSON-Team/IFC2JSON_python/archive/master.zip + cd dist/working && unzip master.zip + cp -r dist/working/IFC2JSON_python-master/ifcjson dist/blenderbim/libs/site/packages/ + rm -rf dist/working + # Provides IFCPatch functionality cd dist/blenderbim/libs/site/packages/ && wget https://raw.githubusercontent.com/IfcOpenShell/IfcOpenShell/v0.6.0/src/ifcpatch/ifcpatch.py diff --git a/src/ifcblenderexport/blenderbim/bim/export_ifc.py b/src/ifcblenderexport/blenderbim/bim/export_ifc.py index 635b4f6529..18cc7f5370 100644 --- a/src/ifcblenderexport/blenderbim/bim/export_ifc.py +++ b/src/ifcblenderexport/blenderbim/bim/export_ifc.py @@ -12,7 +12,6 @@ from .helper import SIUnitHelper from . import schema import ifcopenshell import addon_utils -from .ifc2json import IFC2JSON class ArrayModifier: count: int @@ -2868,9 +2867,17 @@ class IfcExporter(): elif extension == 'ifc': self.file.write(self.ifc_export_settings.output_file) elif extension == 'ifcjson': - ifc2json = IFC2JSON(self.file).convert() - with open(self.ifc_export_settings.output_file, 'w') as f: - f.write(json.dumps(ifc2json, indent = 4)) + import ifcjson + if self.ifc_export_settings.json_version == '4': + jsonData = ifcjson.IFC2JSON4(self.file, self.ifc_export_settings.json_compact).spf2Json() + with open(self.ifc_export_settings.output_file, 'w') as outfile: + json.dump(jsonData, outfile, + indent=None if self.ifc_export_settings.json_compact else 4) + elif self.ifc_export_settings.json_version == '5a': + jsonData = ifcjson.IFC2JSON5a(self.file, self.ifc_export_settings.json_compact).spf2Json() + with open(self.ifc_export_settings.output_file, 'w') as outfile: + json.dump(jsonData, outfile, + indent=None if self.ifc_export_settings.json_compact else 4) class IfcExportSettings: @@ -2900,6 +2907,8 @@ class IfcExportSettings: settings.data_dir = scene_bim.data_dir settings.schema_dir = scene_bim.schema_dir settings.has_representations = scene_bim.export_has_representations + settings.json_version = scene_bim.export_json_version + settings.json_compact = scene_bim.export_json_compact settings.schema = scene_bim.export_schema settings.should_use_presentation_style_assignment = scene_bim.export_should_use_presentation_style_assignment settings.should_guess_quantities = scene_bim.export_should_guess_quantities diff --git a/src/ifcblenderexport/blenderbim/bim/ifc2json.py b/src/ifcblenderexport/blenderbim/bim/ifc2json.py deleted file mode 100644 index 5d64261db2..0000000000 --- a/src/ifcblenderexport/blenderbim/bim/ifc2json.py +++ /dev/null @@ -1,106 +0,0 @@ -import ifcopenshell - -class IFC2JSON: - def __init__(self, file): - self.file = file - self.id_objects = {} - self.jsonObjects = [] - - def convert(self): - # schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name(self.file.schema) - entityIter = iter(self.file) - for entity in entityIter: - self.entityToDict(entity) - for key in self.id_objects: - self.jsonObjects.append(self.id_objects[key]) - - return { - 'data': self.jsonObjects - } - - def entityToDict(self, entity): - ref = { - 'Type': entity.is_a() - } - attr_dict = entity.__dict__ - - # check for globalid - if 'GlobalId' in attr_dict: - ref['ref'] = attr_dict['GlobalId'] - if not attr_dict['GlobalId'] in self.id_objects: - d = { - 'Type': entity.is_a() - } - - for i in range(0,len(entity)): - attr = entity.attribute_name(i) - if attr in attr_dict: - if not attr == 'OwnerHistory': - jsonValue = self.getEntityValue(attr_dict[attr]) - if jsonValue: - d[attr] = jsonValue - if attr_dict[attr] == None: - continue - elif isinstance(attr_dict[attr], ifcopenshell.entity_instance): - d[attr] = self.entityToDict(attr_dict[attr]) - elif isinstance(attr_dict[attr], tuple): - subEnts = [] - for subEntity in attr_dict[attr]: - if isinstance(subEntity, ifcopenshell.entity_instance): - subEntJson = self.entityToDict(subEntity) - if subEntJson: - subEnts.append(subEntJson) - else: - subEnts.append(subEntity) - if len(subEnts) > 0: - d[attr] = subEnts - else: - d[attr] = attr_dict[attr] - self.id_objects[attr_dict['GlobalId']] = d - return ref - else: - d = { - 'Type': entity.is_a() - } - - for i in range(0,len(entity)): - attr = entity.attribute_name(i) - if attr in attr_dict: - if not attr == 'OwnerHistory': - jsonValue = self.getEntityValue(attr_dict[attr]) - if jsonValue: - d[attr] = jsonValue - if attr_dict[attr] == None: - continue - elif isinstance(attr_dict[attr], ifcopenshell.entity_instance): - d[attr] = self.entityToDict(attr_dict[attr]) - elif isinstance(attr_dict[attr], tuple): - subEnts = [] - for subEntity in attr_dict[attr]: - if isinstance(subEntity, ifcopenshell.entity_instance): - # subEnts.append(None) - subEntJson = self.entityToDict(subEntity) - if subEntJson: - subEnts.append(subEntJson) - else: - subEnts.append(subEntity) - if len(subEnts) > 0: - d[attr] = subEnts - else: - d[attr] = attr_dict[attr] - return d - - def getEntityValue(self, value): - if value == None: - jsonValue = None - elif isinstance(value, ifcopenshell.entity_instance): - jsonValue = self.entityToDict(value) - elif isinstance(value, tuple): - jsonValue = None - subEnts = [] - for subEntity in value: - subEnts.append(self.getEntityValue(subEntity)) - jsonValue = subEnts - else: - jsonValue = value - return jsonValue diff --git a/src/ifcblenderexport/blenderbim/bim/prop.py b/src/ifcblenderexport/blenderbim/bim/prop.py index 3406f2ddd2..f209e39cfa 100644 --- a/src/ifcblenderexport/blenderbim/bim/prop.py +++ b/src/ifcblenderexport/blenderbim/bim/prop.py @@ -1003,6 +1003,8 @@ class BIMProperties(PropertyGroup): name="Predefined Type", default=None) ifc_userdefined_type: StringProperty(name="Userdefined Type") export_schema: EnumProperty(items=[('IFC4', 'IFC4', ''), ('IFC2X3', 'IFC2X3', '')], name='IFC Schema') + export_json_version: EnumProperty(items=[('4', '4', ''), ('5a', '5a', '')], name='IFC JSON Version') + export_json_compact: BoolProperty(name="Export Compact IFCJSON", default=False) export_has_representations: BoolProperty(name="Export Representations", default=True) export_should_guess_quantities: BoolProperty(name="Export with Guessed Quantities", default=False) export_should_use_presentation_style_assignment: BoolProperty(name="Export with Presentation Style Assignment", default=False) diff --git a/src/ifcblenderexport/blenderbim/bim/ui.py b/src/ifcblenderexport/blenderbim/bim/ui.py index 486190d2f8..2d1604a892 100644 --- a/src/ifcblenderexport/blenderbim/bim/ui.py +++ b/src/ifcblenderexport/blenderbim/bim/ui.py @@ -1558,6 +1558,8 @@ class BIM_PT_mvd(Panel): row = layout.row() row.prop(bim_properties, 'export_schema') + row = layout.row() + row.prop(bim_properties, 'export_json_version') row = layout.row() row.prop(bim_properties, 'ifc_import_filter') @@ -1589,6 +1591,8 @@ class BIM_PT_mvd(Panel): row.prop(bim_properties, 'import_should_use_cpu_multiprocessing') row = layout.row() row.prop(bim_properties, 'import_should_import_with_profiling') + row = layout.row() + row.prop(bim_properties, 'export_json_compact') layout.label(text='Simplifications:')