feat(ucs): orient text-bearing annotations to the active UCS

New annotations now place their text and reading geometry along the
active UCS X axis (identity = world), so they read square to the user's
coordinate system instead of world-horizontal:

- TEXT / MTEXT: rotation defaults to the UCS X angle
  (DocumentTab::ucs_rotation_angle, model space only).
- Linear dimensions: measure along the UCS axis the span is nearest, and
  bake the UCS X angle as the text rotation so the value reads square
  even under a style that forces horizontal text. DIMCONTINUE / DIMBASELINE
  derive their axis from the base dim's rotation (any angle), not a
  world H/V test.
- LEADER: the MText annotation rotates to the UCS, and the hookline runs
  along the leader's horizontal_direction (set to UCS X), used by the
  renderer instead of world horizontal.
- MLEADER: context.text_direction = UCS X drives both the text rotation
  and, in the renderer, the text side and dogleg/landing direction.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Hakan SevenandClaude Opus 4.8 committed 2026-06-22 18:18:35 +03:00
1 parent 0c2a97e17e
commit 33314a5889
10 files changed
+160 -73

No files matched your search

+40 -30
View File
@@ -5,7 +5,21 @@ use acadrust::EntityType;
use crate::command::{CadCommand, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::model::wire_model::WireModel;
use glam::Vec3;
use glam::{Mat4, Vec3};
/// World-space measurement axis for a linear dimension between `first` and
/// `second`, chosen as the UCS X or Y axis (whichever the span is closer to).
/// `ucs` is the UCS→wire affine; identity gives the world X/Y behaviour.
fn measure_axis(first: Vec3, second: Vec3, ucs: Mat4) -> Vec3 {
let ux = ucs.transform_vector3(Vec3::X).normalize_or(Vec3::X);
let uy = ucs.transform_vector3(Vec3::Y).normalize_or(Vec3::Y);
let du = ucs.inverse().transform_vector3(second - first);
if du.x.abs() >= du.y.abs() {
ux
} else {
uy
}
}
pub const ICON: IconKind = IconKind::Svg(include_bytes!("../../../assets/icons/dim_linear.svg"));
@@ -26,12 +40,14 @@ enum Step {
pub struct LinearDimensionCommand {
step: Step,
ucs: Mat4,
}
impl LinearDimensionCommand {
pub fn new() -> Self {
Self {
step: Step::FirstPoint,
ucs: Mat4::IDENTITY,
}
}
}
@@ -41,6 +57,10 @@ impl CadCommand for LinearDimensionCommand {
"DIMLINEAR"
}
fn set_ucs(&mut self, ucs: Mat4) {
self.ucs = ucs;
}
fn prompt(&self) -> String {
match self.step {
Step::FirstPoint => "DIMLINEAR Specify first extension line origin:".into(),
@@ -61,14 +81,23 @@ impl CadCommand for LinearDimensionCommand {
}
Step::DimensionLine { first, second } => {
let mut dim = DimensionLinear::new(v3(first), v3(second));
dim.rotation = if (second.y - first.y).abs() > (second.x - first.x).abs() {
std::f64::consts::FRAC_PI_2
} else {
0.0
};
// Measure along the UCS axis the span is closest to; the DXF
// rotation is that axis's angle in world space.
let axis = measure_axis(first, second, self.ucs);
dim.rotation = (axis.y as f64).atan2(axis.x as f64);
// Bake the UCS X-axis angle as the text rotation so the
// measurement text reads horizontally in the UCS (i.e. square on
// screen) regardless of the style's force-horizontal flags,
// which would otherwise pin it to world-horizontal and tilt it
// under a rotated UCS. Left at 0 (natural) when there's no UCS.
let ux = self.ucs.transform_vector3(Vec3::X);
let ucs_ang = (ux.y as f64).atan2(ux.x as f64);
if ucs_ang.abs() > 1e-9 {
dim.base.text_rotation = ucs_ang;
}
dim.definition_point = v3(pt);
dim.base.definition_point = v3(pt);
dim.base.text_middle_point = v3(linear_text_pos(first, second, pt));
dim.base.text_middle_point = v3(linear_text_pos(first, second, pt, axis));
dim.base.insertion_point = dim.base.text_middle_point;
dim.base.actual_measurement = dim.measurement();
CmdResult::CommitAndExit(EntityType::Dimension(Dimension::Linear(dim)))
@@ -89,7 +118,8 @@ impl CadCommand for LinearDimensionCommand {
Step::FirstPoint => None,
Step::SecondPoint(first) => Some(preview_wire(vec![first, pt])),
Step::DimensionLine { first, second } => {
Some(preview_wire(linear_dimension_preview(first, second, pt)))
let axis = measure_axis(first, second, self.ucs);
Some(preview_wire(linear_dimension_preview(first, second, pt, axis)))
}
}
}
@@ -119,17 +149,7 @@ fn preview_wire(points: Vec<Vec3>) -> WireModel {
}
}
fn linear_dimension_preview(first: Vec3, second: Vec3, def: Vec3) -> Vec<Vec3> {
let axis = {
let d = second - first;
if d.length_squared() <= 1e-12 {
Vec3::X
} else if d.x.abs() >= d.y.abs() {
Vec3::X
} else {
Vec3::Y
}
};
fn linear_dimension_preview(first: Vec3, second: Vec3, def: Vec3, axis: Vec3) -> Vec<Vec3> {
let perp = Vec3::new(-axis.y, axis.x, 0.0);
let offset = (def - first).dot(perp);
let d1 = first + perp * offset;
@@ -146,17 +166,7 @@ fn linear_dimension_preview(first: Vec3, second: Vec3, def: Vec3) -> Vec<Vec3> {
]
}
fn linear_text_pos(first: Vec3, second: Vec3, def: Vec3) -> Vec3 {
let axis = {
let d = second - first;
if d.length_squared() <= 1e-12 {
Vec3::X
} else if d.x.abs() >= d.y.abs() {
Vec3::X
} else {
Vec3::Y
}
};
fn linear_text_pos(first: Vec3, second: Vec3, def: Vec3, axis: Vec3) -> Vec3 {
let perp = Vec3::new(-axis.y, axis.x, 0.0);
let offset = (def - first).dot(perp);
let d1 = first + perp * offset;