From d180c4732f25f8e647c4860269a892259ded9866 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Sun, 12 Apr 2020 09:51:10 +1000 Subject: [PATCH] Support for exporting IFCZIP files. See #834 --- .../blenderbim/bim/__init__.py | 4 ++-- .../blenderbim/bim/export_ifc.py | 18 +++++++++++++++++- .../blenderbim/bim/operator.py | 10 +++++++--- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/ifcblenderexport/blenderbim/bim/__init__.py b/src/ifcblenderexport/blenderbim/bim/__init__.py index 2558bd9bd3..8594675e64 100644 --- a/src/ifcblenderexport/blenderbim/bim/__init__.py +++ b/src/ifcblenderexport/blenderbim/bim/__init__.py @@ -139,11 +139,11 @@ if bpy is not None: def menu_func_export(self, context): self.layout.operator(operator.ExportIFC.bl_idname, - text="Industry Foundation Classes (.ifc)") + text="Industry Foundation Classes (.ifc/.ifczip)") def menu_func_import(self, context): self.layout.operator(operator.ImportIFC.bl_idname, - text="Industry Foundation Classes (.ifc)") + text="Industry Foundation Classes (.ifc/.ifczip)") def on_register(scene): prop.setDefaultProperties(scene) diff --git a/src/ifcblenderexport/blenderbim/bim/export_ifc.py b/src/ifcblenderexport/blenderbim/bim/export_ifc.py index d51260139a..4bbd1b0d6e 100644 --- a/src/ifcblenderexport/blenderbim/bim/export_ifc.py +++ b/src/ifcblenderexport/blenderbim/bim/export_ifc.py @@ -4,6 +4,8 @@ import json import time import datetime import os +import zipfile +import tempfile from pathlib import Path from mathutils import Vector, Matrix from .helper import SIUnitHelper @@ -1146,7 +1148,7 @@ class IfcExporter(): self.relate_to_objectives(self.ifc_parser.rel_associates_constraint_objective_type) self.relate_structural_members_to_connections() self.relate_objects_to_groups() - self.file.write(self.ifc_export_settings.output_file) + self.write_ifc_file() def set_common_definitions(self): # Owner history doesn't actually work like this, but for now, it does :) @@ -2422,6 +2424,20 @@ class IfcExporter(): def convert_si_to_unit(self, co): return co / self.ifc_parser.unit_scale + def write_ifc_file(self): + extension = self.ifc_export_settings.output_file.split('.')[-1] + if extension == 'ifczip': + with tempfile.TemporaryDirectory() as unzipped_path: + filename, ext = os.path.splitext(os.path.basename(self.ifc_export_settings.output_file)) + tmp_name = '{}.ifc'.format(filename) + tmp_file = os.path.join(unzipped_path, tmp_name) + self.file.write(tmp_file) + with zipfile.ZipFile(self.ifc_export_settings.output_file, + mode='w', compression=zipfile.ZIP_DEFLATED, compresslevel=9) as zf: + zf.write(tmp_file) + elif extension == 'ifc': + self.file.write(self.ifc_export_settings.output_file) + class IfcExportSettings: def __init__(self): diff --git a/src/ifcblenderexport/blenderbim/bim/operator.py b/src/ifcblenderexport/blenderbim/bim/operator.py index 6cff31974c..d313157088 100644 --- a/src/ifcblenderexport/blenderbim/bim/operator.py +++ b/src/ifcblenderexport/blenderbim/bim/operator.py @@ -34,7 +34,7 @@ colour_list = [ class ExportIFC(bpy.types.Operator): bl_idname = "export.ifc" - bl_label = "Export .ifc file" + bl_label = "Export IFC" filename_ext = ".ifc" filepath: bpy.props.StringProperty(subtype='FILE_PATH') @@ -51,7 +51,11 @@ class ExportIFC(bpy.types.Operator): logging.basicConfig( filename=context.scene.BIMProperties.data_dir + 'process.log', filemode='a', level=logging.DEBUG) - output_file = bpy.path.ensure_ext(self.filepath, '.ifc') + extension = self.filepath.split('.')[-1] + if extension == 'ifczip': + output_file = bpy.path.ensure_ext(self.filepath, '.ifczip') + else: + output_file = bpy.path.ensure_ext(self.filepath, '.ifc') ifc_export_settings = export_ifc.IfcExportSettings.factory(context, output_file, logger) ifc_parser = export_ifc.IfcParser(ifc_export_settings) qto_calculator = export_ifc.QtoCalculator() @@ -63,7 +67,7 @@ class ExportIFC(bpy.types.Operator): class ImportIFC(bpy.types.Operator, ImportHelper): bl_idname = "import.ifc" - bl_label = "Import .ifc file" + bl_label = "Import IFC" filename_ext = ".ifc" filter_glob: bpy.props.StringProperty(default="*.ifc;*.ifczip", options={'HIDDEN'})