From 25a7a9528454a264664949ad8e637014b09ec1c7 Mon Sep 17 00:00:00 2001 From: CyrilWaechter Date: Sat, 26 Nov 2022 05:19:01 +0100 Subject: [PATCH] Allow to write .ifcZIP | .ifcXML #2350 Transparently write any .ifc | .ifcXML | .ifcZIP formatted file with any extension : - by guessing format from its extension (eg. .zip | .ifczip | .ifcZIP | .ifcXML | .ifcxml | .IFC | .ifc) zipped file can file can be of any supported format - by specifying a format - by specifying if it should be zipped --- .../ifcopenshell/__init__.py | 12 +--- src/ifcopenshell-python/ifcopenshell/file.py | 45 ++++++++++++- .../ifcopenshell/util/file.py | 27 ++++++++ src/ifcopenshell-python/test/test_write.py | 63 +++++++++++++++++++ 4 files changed, 137 insertions(+), 10 deletions(-) create mode 100644 src/ifcopenshell-python/ifcopenshell/util/file.py create mode 100644 src/ifcopenshell-python/test/test_write.py diff --git a/src/ifcopenshell-python/ifcopenshell/__init__.py b/src/ifcopenshell-python/ifcopenshell/__init__.py index 93571e3c74..9a31e1b213 100644 --- a/src/ifcopenshell-python/ifcopenshell/__init__.py +++ b/src/ifcopenshell-python/ifcopenshell/__init__.py @@ -38,6 +38,8 @@ import tempfile import zipfile from pathlib import Path +import ifcopenshell.util.file + if hasattr(os, "uname"): platform_system = os.uname()[0].lower() else: @@ -83,14 +85,6 @@ class SchemaError(Error): pass -def guess_format(path: Path) -> "str | None": - """Try to guess format using file extension""" - if path.suffix.lower() in (".ifczip", ".zip"): - return ".ifcZIP" - if path.suffix.lower() in (".ifcxml", ".xml"): - return ".ifcXML" - - def open(path: "os.PathLike | str", format: str = None) -> file: """Loads an IFC dataset from a filepath @@ -104,7 +98,7 @@ def open(path: "os.PathLike | str", format: str = None) -> file: """ path = Path(path) if format is None: - format = guess_format(path) + format = ifcopenshell.util.file.guess_format(path) if format == ".ifcXML": f = ifcopenshell_wrapper.parse_ifcxml(str(path.absolute())) if f: diff --git a/src/ifcopenshell-python/ifcopenshell/file.py b/src/ifcopenshell-python/ifcopenshell/file.py index bc11ff1e26..6e7d32e308 100644 --- a/src/ifcopenshell-python/ifcopenshell/file.py +++ b/src/ifcopenshell-python/ifcopenshell/file.py @@ -21,10 +21,14 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function +import os +from pathlib import Path import numbers import functools -import ifcopenshell.util.element +import zipfile +import ifcopenshell.util.element +import ifcopenshell.util.file from . import ifcopenshell_wrapper from .entity_instance import entity_instance @@ -442,6 +446,45 @@ class file(object): def __iter__(self): return iter(self[id] for id in self.wrapped_data.entity_names()) + def write(self, path: "os.PathLike | str", format=None, zipped=False) -> None: + """Write ifc model to file. + + :param format: Force use of a specific format. Guessed from file name if None. + Supported formats : .ifc, .ifcXML, .ifcZIP (equivalent to format=".ifc" with zipped=True) + For zipped .ifcXML use format=".ifcXML" with zipped=True + :param zipped: zip the file after it is written + + Examples: + >>> model.write("path/to/model.ifc") + >>> model.write("path/to/model.ifcXML") + >>> model.write("path/to/model.ifcZIP") + >>> model.write("path/to/model.ifcZIP", format=".ifcXML", zipped=True) + >>> model.write("path/to/model.anyextension", format=".ifcXML") + """ + path = Path(path) + if format == None: + format = ifcopenshell.util.file.guess_format(path) + if format == ".ifcXML": + serializer = ifcopenshell_wrapper.XmlSerializer(self, str(path)) + serializer.finalize() + if zipped: + unzipped_path = path.with_suffix(format) + path.rename(unzipped_path) + with zipfile.ZipFile(path, "w") as zip_file: + zip_file.write(unzipped_path, unzipped_path.name, compress_type=zipfile.ZIP_DEFLATED) + unzipped_path.unlink() + return + if format == ".ifcZIP": + return self.write(path, ".ifc", zipped=True) + self.wrapped_data.write(str(path)) + if zipped: + unzipped_path = path.with_suffix(format) + path.rename(unzipped_path) + with zipfile.ZipFile(path, "w") as zip_file: + zip_file.write(unzipped_path, unzipped_path.name, compress_type=zipfile.ZIP_DEFLATED) + unzipped_path.unlink() + return + @staticmethod def from_string(s): return file(ifcopenshell_wrapper.read(s)) diff --git a/src/ifcopenshell-python/ifcopenshell/util/file.py b/src/ifcopenshell-python/ifcopenshell/util/file.py new file mode 100644 index 0000000000..eb682d19b4 --- /dev/null +++ b/src/ifcopenshell-python/ifcopenshell/util/file.py @@ -0,0 +1,27 @@ +# IfcOpenShell - IFC toolkit and geometry engine +# Copyright (C) 2021 Dion Moult +# +# This file is part of IfcOpenShell. +# +# IfcOpenShell is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# IfcOpenShell is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with IfcOpenShell. If not, see . + +from pathlib import Path + + +def guess_format(path: Path) -> "str | None": + """Try to guess format using file extension""" + if path.suffix.lower() in (".ifczip", ".zip"): + return ".ifcZIP" + if path.suffix.lower() in (".ifcxml", ".xml"): + return ".ifcXML" diff --git a/src/ifcopenshell-python/test/test_write.py b/src/ifcopenshell-python/test/test_write.py new file mode 100644 index 0000000000..0255305140 --- /dev/null +++ b/src/ifcopenshell-python/test/test_write.py @@ -0,0 +1,63 @@ +# IfcOpenShell - IFC toolkit and geometry engine +# Copyright (C) 2021 Thomas Krijnen +# +# This file is part of IfcOpenShell. +# +# IfcOpenShell is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# IfcOpenShell is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with IfcOpenShell. If not, see . + +import tempfile +from pathlib import Path +import pytest +import ifcopenshell + + +TEST_FILE_DIR = Path("../../test/input/") + + +class TestWrite: + def setup(self): + self.model = ifcopenshell.open(TEST_FILE_DIR / "WallInstance_IFC4Add2.ifc") + + def assert_model_is_written(self, filename, format=None, zipped=False): + with tempfile.TemporaryDirectory() as temp_dir: + file_path = Path(temp_dir) / filename + self.model.write(file_path, format, zipped) + assert file_path.exists() + + def test_write_ifcspf(self): + self.assert_model_is_written("model.ifc") + + def test_write_ifcxml(self): + self.assert_model_is_written("model.ifcXML") + + def test_write_ifc_zip_ifcxml_format(self): + self.assert_model_is_written("model.ifcZIP", format=".ifcXML", zipped=True) + + def test_write_ifc_zip_ifcspf_format(self): + self.assert_model_is_written("model.ifcZIP") + + def test_write_zip(self): + self.assert_model_is_written("model.zip", format=".ifcZIP") + + def test_write_anyextension_ifcspf_format(self): + self.assert_model_is_written("model.anyextension", format=".ifc") + + def test_write_anyextension_ifczip_ifcspf_format(self): + self.assert_model_is_written("model.anyextension", format=".ifc", zipped=True) + + def test_write_anyextension_ifcxml_format(self): + self.assert_model_is_written("model.anyextension", format=".ifcXML") + + def test_write_anyextension_ifczip_ifcxml_format(self): + self.assert_model_is_written("model.anyextension", format=".ifcXML", zipped=True)