forked from 1b-agent/opencadstudio-upstream
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:
1 parent
0c2a97e17e
commit
33314a5889
10 files changed
+160
-73
No files matched your search
@@ -13,7 +13,7 @@ use acadrust::entities::mtext::AttachmentPoint;
|
||||
use acadrust::entities::{Leader, LeaderCreationType, MText};
|
||||
use acadrust::types::Vector3;
|
||||
use acadrust::EntityType;
|
||||
use glam::Vec3;
|
||||
use glam::{Mat4, Vec3};
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::modules::{IconKind, ModuleEvent, ToolDef};
|
||||
@@ -32,11 +32,12 @@ pub fn tool() -> ToolDef {
|
||||
|
||||
pub struct LeaderCommand {
|
||||
verts: Vec<Vec3>,
|
||||
ucs: Mat4,
|
||||
}
|
||||
|
||||
impl LeaderCommand {
|
||||
pub fn new() -> Self {
|
||||
Self { verts: Vec::new() }
|
||||
Self { verts: Vec::new(), ucs: Mat4::IDENTITY }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +46,10 @@ impl CadCommand for LeaderCommand {
|
||||
"LEADER"
|
||||
}
|
||||
|
||||
fn set_ucs(&mut self, ucs: Mat4) {
|
||||
self.ucs = ucs;
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
if self.verts.is_empty() {
|
||||
"LEADER Specify arrowhead point:".into()
|
||||
@@ -67,9 +72,9 @@ impl CadCommand for LeaderCommand {
|
||||
}
|
||||
// Place the leader plus an empty MText annotation, link them, then open
|
||||
// the in-place MText editor so the user types the annotation text.
|
||||
let leader = build_leader(&self.verts);
|
||||
let (anchor, attach) = annotation_anchor(&self.verts, leader.text_height);
|
||||
let mtext = build_mtext("", anchor, leader.text_height, attach);
|
||||
let leader = build_leader(&self.verts, self.ucs);
|
||||
let (anchor, attach) = annotation_anchor(&self.verts, leader.text_height, self.ucs);
|
||||
let mtext = build_mtext("", anchor, leader.text_height, attach, self.ucs);
|
||||
CmdResult::CommitManyAndEditText {
|
||||
entities: vec![EntityType::Leader(leader), EntityType::MText(mtext)],
|
||||
edit_index: 1,
|
||||
@@ -96,22 +101,32 @@ fn v3(p: Vec3) -> Vector3 {
|
||||
Vector3::new(p.x as f64, p.y as f64, p.z as f64)
|
||||
}
|
||||
|
||||
fn build_leader(verts: &[Vec3]) -> Leader {
|
||||
fn build_leader(verts: &[Vec3], ucs: Mat4) -> Leader {
|
||||
let mut l = Leader::from_vertices(verts.iter().map(|p| v3(*p)).collect());
|
||||
l.creation_type = LeaderCreationType::WithText;
|
||||
l.hookline_enabled = true;
|
||||
// The hookline / text read along the UCS X axis (DXF "horizontal direction
|
||||
// for text"); the renderer uses it instead of world horizontal.
|
||||
let ux = ucs.transform_vector3(Vec3::X).normalize_or(Vec3::X);
|
||||
l.horizontal_direction = Vector3::new(ux.x as f64, ux.y as f64, 0.0);
|
||||
l
|
||||
}
|
||||
|
||||
/// Text anchor at the end of the landing line, and the attachment point that
|
||||
/// keeps the text reading away from the leader (text to the right of a
|
||||
/// left-pointing landing, to the left of a right-pointing one).
|
||||
fn annotation_anchor(verts: &[Vec3], text_height: f64) -> (Vec3, AttachmentPoint) {
|
||||
fn annotation_anchor(
|
||||
verts: &[Vec3],
|
||||
text_height: f64,
|
||||
ucs: Mat4,
|
||||
) -> (Vec3, AttachmentPoint) {
|
||||
let last = *verts.last().unwrap();
|
||||
let prev = verts[verts.len() - 2];
|
||||
let to_right = last.x >= prev.x;
|
||||
// Side + landing run along the UCS X axis (identity = world).
|
||||
let ux = ucs.transform_vector3(Vec3::X).normalize_or(Vec3::X);
|
||||
let to_right = (last - prev).dot(ux) >= 0.0;
|
||||
let sign = if to_right { 1.0_f32 } else { -1.0_f32 };
|
||||
let anchor = Vec3::new(last.x + sign * text_height as f32 * 1.5, last.y, last.z);
|
||||
let anchor = last + ux * (sign * text_height as f32 * 1.5);
|
||||
let attach = if to_right {
|
||||
AttachmentPoint::MiddleLeft
|
||||
} else {
|
||||
@@ -120,12 +135,21 @@ fn annotation_anchor(verts: &[Vec3], text_height: f64) -> (Vec3, AttachmentPoint
|
||||
(anchor, attach)
|
||||
}
|
||||
|
||||
fn build_mtext(text: &str, pos: Vec3, height: f64, attach: AttachmentPoint) -> MText {
|
||||
fn build_mtext(
|
||||
text: &str,
|
||||
pos: Vec3,
|
||||
height: f64,
|
||||
attach: AttachmentPoint,
|
||||
ucs: Mat4,
|
||||
) -> MText {
|
||||
let mut m = MText::new();
|
||||
m.value = text.to_string();
|
||||
m.insertion_point = v3(pos);
|
||||
m.height = height;
|
||||
m.attachment_point = attach;
|
||||
// Text reads along the UCS X axis.
|
||||
let ux = ucs.transform_vector3(Vec3::X);
|
||||
m.rotation = (ux.y as f64).atan2(ux.x as f64);
|
||||
m
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user