Fix #5964. Only get the unit assignment from the project to prevent ambiguity.

I really don't know why I didn't do this before.
This commit is contained in:
Dion Moult
2025-01-20 23:50:03 +11:00
parent c5fe80db7b
commit 7b62b035c9
3 changed files with 8 additions and 12 deletions
+2 -2
View File
@@ -893,9 +893,9 @@ class IfcImporter:
tool.Loader.set_unit_scale(self.unit_scale)
def set_units(self):
if not (units := self.file.by_type("IfcUnitAssignment")):
if not (assignment := self.file.by_type("IfcProject")[0].UnitsInContext):
return # Geometry is optional in IFC
for unit in units[0].Units:
for unit in assignment.Units:
if unit.is_a("IfcNamedUnit") and unit.UnitType == "LENGTHUNIT":
if unit.is_a("IfcSIUnit"):
bpy.context.scene.unit_settings.system = "METRIC"
+4 -5
View File
@@ -116,9 +116,9 @@ class Unit(bonsai.core.tool.Unit):
for unit_class in ["IfcDerivedUnit", "IfcMonetaryUnit", "IfcNamedUnit"]:
units += tool.Ifc.get().by_type(unit_class)
assigned_units = tool.Ifc.get().by_type("IfcUnitAssignment")
if assigned_units:
assigned_units = assigned_units[0].Units
assigned_units = []
if assignment := tool.Ifc.get().by_type("IfcProject")[0].UnitsInContext:
assigned_units = assignment.Units
for unit in units:
name = ""
@@ -162,8 +162,7 @@ class Unit(bonsai.core.tool.Unit):
@classmethod
def get_project_currency_unit(cls) -> Union[ifcopenshell.entity_instance, None]:
unit_assignments = tool.Ifc.get().by_type("IfcUnitAssignment")
for assignment in unit_assignments:
if assignment := tool.Ifc.get().by_type("IfcProject")[0].UnitsInContext:
for unit in assignment.Units:
if unit.is_a("IfcMonetaryUnit"):
return unit
@@ -404,9 +404,7 @@ def get_named_dimensions(name):
def get_unit_assignment(ifc_file: ifcopenshell.file) -> Union[ifcopenshell.entity_instance, None]:
unit_assignments = ifc_file.by_type("IfcUnitAssignment")
if unit_assignments:
return unit_assignments[0]
return ifc_file.by_type("IfcProject")[0].UnitsInContext
def get_project_unit(ifc_file: ifcopenshell.file, unit_type: str) -> Union[ifcopenshell.entity_instance, None]:
@@ -641,9 +639,8 @@ def calculate_unit_scale(ifc_file: ifcopenshell.file, unit_type: str = "LENGTHUN
:param unit_type: The type of SI unit, defaults to "LENGTHUNIT"
:returns: The scale factor
"""
if not ifc_file.by_type("IfcUnitAssignment"):
if not (units := ifc_file.by_type("IfcProject")[0].UnitsInContext):
return 1
units = ifc_file.by_type("IfcUnitAssignment")[0]
unit_scale = 1
for unit in units.Units:
if not hasattr(unit, "UnitType") or unit.UnitType != unit_type: