Deduplicate code by reusing tool.document

This commit is contained in:
Andrej730
2026-07-14 19:28:26 +05:00
parent 3a8619726b
commit 0968d06780
8 changed files with 27 additions and 54 deletions
+3 -6
View File
@@ -156,16 +156,13 @@ class BrickschemaReferencesData:
for rel in getattr(tool.Ifc.get_entity(bpy.context.active_object), "HasAssociations", []): for rel in getattr(tool.Ifc.get_entity(bpy.context.active_object), "HasAssociations", []):
if rel.is_a("IfcRelAssociatesLibrary"): if rel.is_a("IfcRelAssociatesLibrary"):
reference = rel.RelatingLibrary reference = rel.RelatingLibrary
if tool.Ifc.get_schema() == "IFC2X3" and "#" not in reference.ItemReference: identification = tool.Document.get_external_reference_id(reference)
continue if not identification or "#" not in identification:
if tool.Ifc.get_schema() != "IFC2X3" and "#" not in reference.Identification:
continue continue
results.append( results.append(
{ {
"id": reference.id(), "id": reference.id(),
"identification": ( "identification": identification,
reference.ItemReference if tool.Ifc.get_schema() == "IFC2X3" else reference.Identification
),
"name": reference.Name or "Unnamed", "name": reference.Name or "Unnamed",
} }
) )
+11 -22
View File
@@ -127,36 +127,25 @@ class ObjectDocumentData:
identification = None identification = None
if is_information: if is_information:
if tool.Ifc.get_schema() == "IFC2X3": identification = tool.Document.get_document_information_id(relating_document)
identification = relating_document.DocumentId
else:
identification = relating_document.Identification
location = getattr(relating_document, "Location", None) location = getattr(relating_document, "Location", None)
description = getattr(relating_document, "Description", "No description") description = getattr(relating_document, "Description", "No description")
else: else:
description = relating_document.Description description = relating_document.Description
if tool.Ifc.get_schema() == "IFC2X3": referenced_document = tool.Document.get_reference_document(relating_document)
reference_to_document = relating_document.ReferenceToDocument
if not name and reference_to_document:
name = reference_to_document[0].Name
identification = relating_document.ItemReference if not name and referenced_document:
if not identification and reference_to_document: name = referenced_document.Name
identification = reference_to_document[0].DocumentId
location = relating_document.Location
else:
referenced_document = relating_document.ReferencedDocument
if not name and referenced_document:
name = referenced_document.Name
identification = relating_document.Identification identification = tool.Document.get_external_reference_id(relating_document)
if not identification and referenced_document: if not identification and referenced_document:
identification = referenced_document.Identification identification = tool.Document.get_document_information_id(referenced_document)
location = relating_document.Location location = relating_document.Location
if location is None and referenced_document: # IFC2X3 IfcDocumentInformation has no Location to fall back to.
location = referenced_document.Location if location is None and referenced_document and tool.Ifc.get_schema() != "IFC2X3":
location = referenced_document.Location
location = cls.convert_to_file_uri(location) if location else None location = cls.convert_to_file_uri(location) if location else None
@@ -3586,7 +3586,7 @@ class EditSheet(bpy.types.Operator, tool.Ifc.Operator):
if sheet.is_a("IfcDocumentInformation"): if sheet.is_a("IfcDocumentInformation"):
self.document_type = "SHEET" self.document_type = "SHEET"
self.name = sheet.Name self.name = sheet.Name
self.identification = sheet.DocumentId if tool.Ifc.get_schema() == "IFC2X3" else sheet.Identification self.identification = tool.Document.get_document_information_id(sheet)
elif sheet.is_a("IfcDocumentReference") and tool.Drawing.get_reference_description(sheet) == "TITLEBLOCK": elif sheet.is_a("IfcDocumentReference") and tool.Drawing.get_reference_description(sheet) == "TITLEBLOCK":
self.document_type = "TITLEBLOCK" self.document_type = "TITLEBLOCK"
else: else:
@@ -903,12 +903,8 @@ class SvgWriter:
continue continue
sheet = tool.Drawing.get_reference_document(sheet_reference) sheet = tool.Drawing.get_reference_document(sheet_reference)
if sheet: if sheet:
if tool.Ifc.get_schema() == "IFC2X3": reference_id = tool.Document.get_external_reference_id(sheet_reference) or "-"
reference_id = sheet_reference.ItemReference or "-" sheet_id = tool.Document.get_document_information_id(sheet) or "-"
sheet_id = sheet.DocumentId or "-"
else:
reference_id = sheet_reference.Identification or "-"
sheet_id = sheet.Identification or "-"
return (reference_id, sheet_id) return (reference_id, sheet_id)
break break
return ("-", "-") return ("-", "-")
+1 -3
View File
@@ -103,9 +103,7 @@ class LibraryReferencesData:
results.append( results.append(
{ {
"id": library.id(), "id": library.id(),
"identification": ( "identification": tool.Document.get_external_reference_id(library),
library.ItemReference if tool.Ifc.get_schema() == "IFC2X3" else library.Identification
),
"name": library.Name or "Unnamed", "name": library.Name or "Unnamed",
} }
) )
+3 -4
View File
@@ -192,10 +192,9 @@ class Brick(bonsai.core.tool.Brick):
def get_brick(cls, element: ifcopenshell.entity_instance) -> Union[str, None]: def get_brick(cls, element: ifcopenshell.entity_instance) -> Union[str, None]:
for rel in element.HasAssociations: for rel in element.HasAssociations:
if rel.is_a("IfcRelAssociatesLibrary"): if rel.is_a("IfcRelAssociatesLibrary"):
if tool.Ifc.get_schema() == "IFC2X3" and "#" in rel.RelatingLibrary.ItemReference: identification = tool.Document.get_external_reference_id(rel.RelatingLibrary)
return rel.RelatingLibrary.ItemReference if identification and "#" in identification:
if tool.Ifc.get_schema() != "IFC2X3" and "#" in rel.RelatingLibrary.Identification: return identification
return rel.RelatingLibrary.Identification
@classmethod @classmethod
def get_brick_class(cls, element: ifcopenshell.entity_instance) -> Union[str, None]: def get_brick_class(cls, element: ifcopenshell.entity_instance) -> Union[str, None]:
+2 -1
View File
@@ -261,7 +261,8 @@ class Document(bonsai.core.tool.Document):
def get_reference_document(cls, reference: ifcopenshell.entity_instance) -> ifcopenshell.entity_instance | None: def get_reference_document(cls, reference: ifcopenshell.entity_instance) -> ifcopenshell.entity_instance | None:
# TODO: migrate to util.document and replace all instances # TODO: migrate to util.document and replace all instances
if reference.file.schema == "IFC2X3": if reference.file.schema == "IFC2X3":
return (reference.ReferenceToDocument or (None))[0] reference_to_document = reference.ReferenceToDocument
return reference_to_document[0] if reference_to_document else None
return reference.ReferencedDocument return reference.ReferencedDocument
@classmethod @classmethod
+4 -11
View File
@@ -1167,10 +1167,7 @@ class Drawing(bonsai.core.tool.Drawing):
new = documents_collection.add() new = documents_collection.add()
new.ifc_definition_id = schedule.id() new.ifc_definition_id = schedule.id()
new.name = schedule.Name or "Unnamed" new.name = schedule.Name or "Unnamed"
if tool.Ifc.get_schema() == "IFC2X3": new.identification = tool.Document.get_document_information_id(schedule) or ""
new.identification = schedule.DocumentId
else:
new.identification = schedule.Identification
@classmethod @classmethod
def get_sheet_identification(cls, sheet: ifcopenshell.entity_instance) -> str: def get_sheet_identification(cls, sheet: ifcopenshell.entity_instance) -> str:
@@ -1211,10 +1208,7 @@ class Drawing(bonsai.core.tool.Drawing):
new.ifc_definition_id = reference.id() new.ifc_definition_id = reference.id()
new.is_sheet = False new.is_sheet = False
if tool.Ifc.get_schema() == "IFC2X3": new.identification = tool.Document.get_external_reference_id(reference) or ""
new.identification = reference.ItemReference or ""
else:
new.identification = reference.Identification or ""
new.name = os.path.basename(reference.Location) new.name = os.path.basename(reference.Location)
new.reference_type = reference_description new.reference_type = reference_description
@@ -2450,9 +2444,8 @@ class Drawing(bonsai.core.tool.Drawing):
def get_reference_document( def get_reference_document(
cls, reference: ifcopenshell.entity_instance cls, reference: ifcopenshell.entity_instance
) -> Union[ifcopenshell.entity_instance, None]: ) -> Union[ifcopenshell.entity_instance, None]:
if tool.Ifc.get_schema() == "IFC2X3": # TODO: migrate to document.get_reference_document.
return reference.ReferenceToDocument[0] return tool.Document.get_reference_document(reference)
return reference.ReferencedDocument
@classmethod @classmethod
def select_assigned_product(cls, context: bpy.types.Context) -> None: def select_assigned_product(cls, context: bpy.types.Context) -> None: