move ifc file handling logic to ifcopenshell.file

to reuse the same logic for both ifcopenshell.open and ifcopenshell.file.from_string()
This commit is contained in:
Andrej730
2024-06-12 17:30:22 +05:00
parent 5d6215d889
commit d2e7e3daaf
2 changed files with 17 additions and 16 deletions
@@ -103,10 +103,6 @@ try:
except:
pass
READ_ERROR = ifcopenshell_wrapper.file_open_status.READ_ERROR
NO_HEADER = ifcopenshell_wrapper.file_open_status.NO_HEADER
UNSUPPORTED_SCHEMA = ifcopenshell_wrapper.file_open_status.UNSUPPORTED_SCHEMA
class Error(Exception):
"""Error used when a generic problem occurs"""
@@ -162,18 +158,7 @@ def open(path: Union[os.PathLike, str], format: Optional[str] = None, should_str
if should_stream:
return stream(path)
f = ifcopenshell_wrapper.open(str(path.absolute()))
if f.good():
return file(f)
else:
exc, msg = {
READ_ERROR: (IOError, "Unable to open file for reading"),
NO_HEADER: (Error, "Unable to parse IFC SPF header"),
UNSUPPORTED_SCHEMA: (
SchemaError,
"Unsupported schema: %s" % ",".join(f.header.file_schema.schema_identifiers),
),
}[f.good().value()]
raise exc(msg)
return file(f)
def create_entity(type, schema="IFC4", *args, **kwargs):
@@ -175,6 +175,10 @@ class Transaction:
file_dict = {}
READ_ERROR = ifcopenshell_wrapper.file_open_status.READ_ERROR
NO_HEADER = ifcopenshell_wrapper.file_open_status.NO_HEADER
UNSUPPORTED_SCHEMA = ifcopenshell_wrapper.file_open_status.UNSUPPORTED_SCHEMA
class file:
"""Base class for containing IFC files.
@@ -246,6 +250,18 @@ class file:
else:
schema = {"IFC4X3": "IFC4X3_ADD2"}.get(schema, schema)
if f is not None:
if not f.good():
from . import Error, SchemaError
exc, msg = {
READ_ERROR: (IOError, "Unable to open file for reading"),
NO_HEADER: (Error, "Unable to parse IFC SPF header"),
UNSUPPORTED_SCHEMA: (
SchemaError,
"Unsupported schema: %s" % ",".join(f.header.file_schema.schema_identifiers),
),
}[f.good().value()]
raise exc(msg)
self.wrapped_data = f
else:
args = filter(None, [schema])