diff --git a/src/bonsai/bonsai/bim/module/drawing/decoration.py b/src/bonsai/bonsai/bim/module/drawing/decoration.py index 180c7225f6..ec09e29145 100644 --- a/src/bonsai/bonsai/bim/module/drawing/decoration.py +++ b/src/bonsai/bonsai/bim/module/drawing/decoration.py @@ -740,6 +740,10 @@ class DimensionDecorator(BaseDecorator): text_dir = p1 - p0 if text_dir.length < 1: continue + # Normalize so text always reads left-to-right (or bottom-to-top for + # vertical dims) regardless of which end was drawn first. + if text_dir.x < 0 or (abs(text_dir.x) < 1e-6 and text_dir.y < 0): + text_dir = -text_dir perpendicular = Vector((-text_dir.y, text_dir.x)).normalized() text_offset = perpendicular * text_offset_value @@ -748,7 +752,7 @@ class DimensionDecorator(BaseDecorator): "multiline": True, "text_dir": text_dir, } - base_pos = p1 if is_ordinate else p0 + text_dir * 0.5 + base_pos = p1 if is_ordinate else (p0 + p1) / 2 if not show_description_only: segment_length = (v1 - v0).length diff --git a/src/bonsai/bonsai/bim/module/drawing/svgwriter.py b/src/bonsai/bonsai/bim/module/drawing/svgwriter.py index 3ded635261..42b58c1276 100644 --- a/src/bonsai/bonsai/bim/module/drawing/svgwriter.py +++ b/src/bonsai/bonsai/bim/module/drawing/svgwriter.py @@ -1588,8 +1588,10 @@ class SvgWriter: end = (offset + v1.xy * Vector((1, -1))) * self.svg_scale mid = ((end - start) / 2) + start vector = end - start + sheet_dimension = vector.length + if sheet_dimension < 1e-6: + return perpendicular = Vector((vector.y, -vector.x)).normalized() - sheet_dimension = (end - start).length # if annotation can't fit offset text to the right of marker if distance_override is not None: @@ -1597,6 +1599,13 @@ class SvgWriter: else: text_position = mid if sheet_dimension > 5 else (end + (3 * vector.normalized())) angle = math.degrees(vector.angle_signed(Vector((1, 0)))) + # Keep text readable regardless of draw direction: if the dimension runs + # right-to-left the raw angle is near ±180° which renders text upside-down. + # Flip both angle and perpendicular so text always reads left-to-right and + # stays on the same side of the dimension line. + if abs(angle) > 90: + angle += 180 + perpendicular = -perpendicular line = self.svg.line(start=start, end=end, class_=" ".join(classes)) self.svg.add(line)