From 80066ba9e73561101568430f7081923c6b6e07de Mon Sep 17 00:00:00 2001 From: Thomas Krijnen Date: Sat, 25 Oct 2025 21:54:42 +0200 Subject: [PATCH] Different hack for allowing f.header.file_description.file.header.file_description.file --- src/ifcopenshell-python/ifcopenshell/file.py | 27 +++++++++++++++----- src/ifcwrap/IfcParseWrapper.i | 10 ++------ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/file.py b/src/ifcopenshell-python/ifcopenshell/file.py index e2784f9b49..00d84cc8f9 100644 --- a/src/ifcopenshell-python/ifcopenshell/file.py +++ b/src/ifcopenshell-python/ifcopenshell/file.py @@ -497,6 +497,24 @@ class rocksdb_file_storage: return "".join("".join(map(str, t)) if t[1] else "" for t in zip(prefixes, version_tuple[0:2])) +class file_header: + def __init__(self, file, header_data): + self.file = file + self.header_data = header_data + + @property + def file_description(self) -> entity_instance: + return entity_instance.wrap_value(self.header_data.file_description_py(), file=self.file) + + @property + def file_name(self) -> entity_instance: + return entity_instance.wrap_value(self.header_data.file_name_py(), file=self.file) + + @property + def file_schema(self) -> entity_instance: + return entity_instance.wrap_value(self.header_data.file_schema_py(), file=self.file) + + class file: """Base class for containing IFC files. @@ -514,7 +532,7 @@ class file: """ wrapped_data: ifcopenshell_wrapper.file - header: ifcopenshell_wrapper.IfcSpfHeader + header: file_header units: dict[str, entity_instance] = {} history_size: int = 64 history: list[Transaction] @@ -1029,12 +1047,7 @@ class file: @property def header(self): - try: # Temporary workaround until new builds are ready. See #7131. - h = self.wrapped_data.header() - except: - return self.wrapped_data.header - object.__setattr__(h, "file_ref", lambda inst: entity_instance.wrap_value(inst, file=self)) - return h + return file_header(self, self.wrapped_data.header()) @property def storage(self) -> Optional[rocksdb_file_storage]: diff --git a/src/ifcwrap/IfcParseWrapper.i b/src/ifcwrap/IfcParseWrapper.i index 13e34b5575..858705ebf0 100644 --- a/src/ifcwrap/IfcParseWrapper.i +++ b/src/ifcwrap/IfcParseWrapper.i @@ -651,6 +651,8 @@ private: %extend IfcParse::IfcSpfHeader { // Cast to base class pointers for SWIG, because // it has no idea about the schema definitions. + // The code to access these methods as attributes + // is in file.py IfcUtil::IfcBaseClass* file_description_py() { return $self->file_description(); } @@ -660,14 +662,6 @@ private: IfcUtil::IfcBaseClass* file_schema_py() { return $self->file_schema(); } - - %pythoncode %{ - # Hide the getters with read-only property implementations - # self.file_ref is set in ifcopenshell.file.header() - file_description = property(lambda self: self.file_ref(self.file_description_py())) - file_name = property(lambda self: self.file_ref(self.file_name_py())) - file_schema = property(lambda self: self.file_ref(self.file_schema_py())) - %} }; %extend IfcParse::FileDescription {