mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-11 18:16:40 +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:
@@ -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:
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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"
|
||||
Reference in New Issue
Block a user