Option to bypass storing types when opening model

This commit is contained in:
Thomas Krijnen
2025-10-24 12:06:59 +02:00
parent a6d20c0cc4
commit 0494bd9677
8 changed files with 174 additions and 33 deletions
@@ -59,7 +59,7 @@ import sys
import zipfile
import tempfile
from pathlib import Path
from typing import Optional, Union, TYPE_CHECKING, Any, overload, Literal
from typing import Optional, Sequence, Union, TYPE_CHECKING, Any, overload, Literal
if TYPE_CHECKING:
import ifcopenshell.express.schema_class
@@ -138,7 +138,12 @@ def open(
path: Union[os.PathLike, str], format: Optional[str] = None, *, should_stream: bool = False, readonly: bool = False
) -> Union[_file, sqlite, _stream]: ...
def open(
path: Union[os.PathLike, str], format: Optional[str] = None, should_stream: bool = False, readonly: bool = False
path: Union[os.PathLike, str],
format: Optional[str] = None,
should_stream: bool = False,
readonly: bool = False,
mmap: bool = False,
bypass_types: Optional[Sequence[str]] = None,
) -> Union[_file, sqlite, _stream]:
"""Loads an IFC dataset from a filepath
@@ -186,7 +191,17 @@ def open(
if should_stream:
return stream(path)
if readonly: # Temporary conditional see #7131. Remove once newer builds don't segfault on Linux.
f = ifcopenshell_wrapper.open(str(path.absolute()), readonly)
f = ifcopenshell_wrapper.open(str(path.absolute()), readonly=readonly)
elif bypass_types:
f = ifcopenshell_wrapper.file(ifcopenshell_wrapper.uninitialized_tag())
for ty in bypass_types:
f.bypass_type(ty)
if mmap:
f.initialize(str(path.absolute()), mmap=mmap)
else:
f.initialize(str(path.absolute()))
elif mmap:
f = ifcopenshell_wrapper.open(str(path.absolute()), mmap=mmap)
else:
f = ifcopenshell_wrapper.open(str(path.absolute()))
return file(f)
+5 -1
View File
@@ -248,6 +248,7 @@ 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
INVALID_SYNTAX = ifcopenshell_wrapper.file_open_status.INVALID_SYNTAX
UNKNOWN = ifcopenshell_wrapper.file_open_status.UNKNOWN
import struct
@@ -586,8 +587,11 @@ class file:
"Unsupported schema: %s" % ",".join(self.header.file_schema.schema_identifiers),
),
INVALID_SYNTAX: lambda: (Error, "Syntax error during parse, check logs"),
# This is the case when passing uninitialized_tag
UNKNOWN: lambda: (None, None),
}[f.good().value()]()
raise exc(msg)
if exc is not None:
raise exc(msg)
else:
args = filter(None, [schema])
args = map(ifcopenshell_wrapper.schema_by_name, args)
@@ -23,6 +23,11 @@ import pytest
import ifcopenshell
import tempfile
try:
import psutil
except ImportError:
psutil = None
fn = os.path.join(os.path.dirname(__file__), "fixtures/ColumnPSetsOfSets.ifc")
@@ -32,18 +37,40 @@ def test_stream():
"value": ({"ref": 136}, {"ref": 138}),
}
def test_chunked_stream():
assert list(ifcopenshell.stream2(fn)) == list(ifcopenshell.stream2(fn, page_size=1024))
def test_mmaped_stream():
assert list(ifcopenshell.stream2(fn)) == list(ifcopenshell.stream2(fn, mmap=True))
def test_file():
f = ifcopenshell.open(fn)
assert f[139].RelatingPropertyDefinition.is_a("IfcPropertySetDefinitionSet")
assert {x.id() for x in f[139].RelatingPropertyDefinition[0]} == {136, 138}
def test_partial_open():
f = ifcopenshell.open(fn)
assert len(f.by_type("ifccartesianpoint"))
f = ifcopenshell.open(fn, bypass_types=("IfcRepresentationItem",))
assert len(f.by_type("ifccartesianpoint")) == 0
@pytest.mark.skipif(psutil is None, reason="psutil not installed")
def test_memusage_partial_open():
m0 = psutil.Process().memory_info().rss
f = ifcopenshell.open(fn)
m1 = psutil.Process().memory_info().rss
g = ifcopenshell.open(fn, bypass_types=("IfcRepresentationItem",))
m2 = psutil.Process().memory_info().rss
# arbitrary...
expected_ratio = 0.75
assert (m2 - m1) < (m1 - m0) * expected_ratio
def test_rocks():
with tempfile.TemporaryDirectory() as d:
rfn = os.path.join(d, os.path.basename(fn))