Replace hard-coded unit scale with ifcopenshell.util.unit.calculate_unit_scale

This commit is contained in:
Bruno Perdigão
2025-01-22 16:19:53 -03:00
parent c6b64baccb
commit 09dbae7c56
3 changed files with 23 additions and 34 deletions
@@ -78,18 +78,11 @@ def get_wall_preview_data(context, relating_type):
elif offset_type == "INTERIOR":
offset = -thickness
unit_system = tool.Drawing.get_unit_system()
factor = 1
if unit_system == "IMPERIAL":
factor = 3.28084
if unit_system == "METRIC":
unit_length = context.scene.unit_settings.length_unit
if unit_length == "MILLIMETERS":
factor = 1000
# For the model properties, the offset value should just be converted
# However, for the wall preview logic that follows, offset and thickness must change direction
model_props.offset = offset * factor
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get())
model_props.offset = offset / unit_scale
thickness *= direction
offset *= direction
+8 -14
View File
@@ -19,6 +19,7 @@
import bpy
import bmesh
import math
import ifcopenshell
import bonsai.core.tool
import bonsai.tool as tool
from bonsai.bim.module.drawing.helper import format_distance
@@ -416,23 +417,20 @@ class Polyline(bonsai.core.tool.Polyline):
dimension = args[i]
if len(args) > i + 1:
expression = args[i + 1]
return expression(dimension) * factor
return expression(dimension) * unit_scale
else:
return dimension * factor
return dimension * unit_scale
try:
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get())
if bpy.context.scene.unit_settings.system == "IMPERIAL":
parser = Lark(grammar_imperial)
factor = 0.3048
else:
parser = Lark(grammar_metric)
factor = 1
if bpy.context.scene.unit_settings.length_unit == "MILLIMETERS":
factor = 0.001
if input_type == "A":
parser = Lark(grammar_metric)
factor = 1
unit_scale = 1
parse_tree = parser.parse(input_number)
@@ -445,18 +443,14 @@ class Polyline(bonsai.core.tool.Polyline):
@classmethod
def format_input_ui_units(cls, value: float, is_area: bool = False) -> str:
unit_system = tool.Drawing.get_unit_system()
if unit_system == "IMPERIAL":
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get())
if bpy.context.scene.unit_settings.system == "IMPERIAL":
precision = bpy.context.scene.DocProperties.imperial_precision
factor = 3.28084
else:
precision = None
factor = 1
if bpy.context.scene.unit_settings.length_unit == "MILLIMETERS":
factor = 1000
return format_distance(
value * factor,
value / unit_scale,
precision=precision,
hide_units=False,
isArea=is_area,
+13 -11
View File
@@ -17,6 +17,7 @@
# along with Bonsai. If not, see <http://www.gnu.org/licenses/>.
import bpy
import ifcopenshell
import bonsai.core.tool
import bonsai.tool as tool
from bonsai.bim.module.model.decorator import PolylineDecorator
@@ -51,8 +52,9 @@ class Snap(bonsai.core.tool.Snap):
distances = [3, 5, 15, 30]
unit_system = tool.Drawing.get_unit_system()
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get())
if unit_system == "IMPERIAL":
factor = 3.28084
factor = unit_scale
fractions = [24, 12, 6, 2]
ortho_threshold = [-10.0, -4.75, -2.2, -0.75]
distances = [3, 6, 10, 20]
@@ -60,29 +62,29 @@ class Snap(bonsai.core.tool.Snap):
increment = None
if rv3d.view_perspective == "PERSP":
if rv3d.view_distance < distances[0]:
increment = (1 / fractions[0]) / factor
increment = (1 / fractions[0]) * factor
elif distances[0] < rv3d.view_distance < distances[1]:
increment = (1 / fractions[1]) / factor
increment = (1 / fractions[1]) * factor
elif distances[1] < rv3d.view_distance < distances[2]:
increment = (1 / fractions[2]) / factor
increment = (1 / fractions[2]) * factor
elif distances[2] < rv3d.view_distance < distances[3]:
increment = (1 / fractions[3]) / factor
increment = (1 / fractions[3]) * factor
else:
increment = 1 / factor
increment = 1 * factor
if rv3d.view_perspective == "ORTHO" or (
rv3d.view_perspective == "CAMERA" and context.scene.camera.data.type == "ORTHO"
):
window_scale = rv3d.window_matrix.to_scale()
if window_scale[1] < ortho_threshold[0]:
increment = (1 / fractions[0]) / factor
increment = (1 / fractions[0]) * factor
elif ortho_threshold[0] < window_scale[1] < ortho_threshold[1]:
increment = (1 / fractions[1]) / factor
increment = (1 / fractions[1]) * factor
elif ortho_threshold[1] < window_scale[1] < ortho_threshold[2]:
increment = (1 / fractions[2]) / factor
increment = (1 / fractions[2]) * factor
elif ortho_threshold[2] < window_scale[1] < ortho_threshold[3]:
increment = (1 / fractions[3]) / factor
increment = (1 / fractions[3]) * factor
else:
increment = 1 / factor
increment = 1 * factor
return increment