mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-26 18:21:59 +00:00
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
This commit is contained in:
committed by
Dion Moult
parent
6ba56fdf35
commit
78f03c8544
@@ -38,6 +38,8 @@ import tempfile
|
|||||||
import zipfile
|
import zipfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import ifcopenshell.util.file
|
||||||
|
|
||||||
if hasattr(os, "uname"):
|
if hasattr(os, "uname"):
|
||||||
platform_system = os.uname()[0].lower()
|
platform_system = os.uname()[0].lower()
|
||||||
else:
|
else:
|
||||||
@@ -83,14 +85,6 @@ class SchemaError(Error):
|
|||||||
pass
|
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:
|
def open(path: "os.PathLike | str", format: str = None) -> file:
|
||||||
"""Loads an IFC dataset from a filepath
|
"""Loads an IFC dataset from a filepath
|
||||||
|
|
||||||
@@ -104,7 +98,7 @@ def open(path: "os.PathLike | str", format: str = None) -> file:
|
|||||||
"""
|
"""
|
||||||
path = Path(path)
|
path = Path(path)
|
||||||
if format is None:
|
if format is None:
|
||||||
format = guess_format(path)
|
format = ifcopenshell.util.file.guess_format(path)
|
||||||
if format == ".ifcXML":
|
if format == ".ifcXML":
|
||||||
f = ifcopenshell_wrapper.parse_ifcxml(str(path.absolute()))
|
f = ifcopenshell_wrapper.parse_ifcxml(str(path.absolute()))
|
||||||
if f:
|
if f:
|
||||||
|
|||||||
@@ -21,10 +21,14 @@ from __future__ import absolute_import
|
|||||||
from __future__ import division
|
from __future__ import division
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
import numbers
|
import numbers
|
||||||
import functools
|
import functools
|
||||||
import ifcopenshell.util.element
|
import zipfile
|
||||||
|
|
||||||
|
import ifcopenshell.util.element
|
||||||
|
import ifcopenshell.util.file
|
||||||
from . import ifcopenshell_wrapper
|
from . import ifcopenshell_wrapper
|
||||||
from .entity_instance import entity_instance
|
from .entity_instance import entity_instance
|
||||||
|
|
||||||
@@ -442,6 +446,45 @@ class file(object):
|
|||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return iter(self[id] for id in self.wrapped_data.entity_names())
|
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
|
@staticmethod
|
||||||
def from_string(s):
|
def from_string(s):
|
||||||
return file(ifcopenshell_wrapper.read(s))
|
return file(ifcopenshell_wrapper.read(s))
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
# IfcOpenShell - IFC toolkit and geometry engine
|
||||||
|
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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"
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
# IfcOpenShell - IFC toolkit and geometry engine
|
||||||
|
# Copyright (C) 2021 Thomas Krijnen <thomas@aecgeeks.com>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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)
|
||||||
Reference in New Issue
Block a user