forked from 1b-agent/opencadstudio-upstream
fix(mleader): improve behavior and persistence
Keep annotation contexts, grip placement, and style dictionary entries consistent across editing and file saves. Refs #779
This commit is contained in:
commit
20c7a2df58
29 files changed
+858
-195
No files matched your search
@@ -1,9 +1,4 @@
|
||||
// MLEADER command
|
||||
//
|
||||
// Flow:
|
||||
// 1. CollectPoints — click arrowhead, then bend points; Enter (≥2) to finish
|
||||
// 2. AskText — wants_text_input; blank Enter = no text
|
||||
// → commit single MultiLeader entity
|
||||
// MLEADER: pick the arrow and elbow, then edit the text.
|
||||
|
||||
use acadrust::entities::MultiLeader;
|
||||
use acadrust::types::Vector3;
|
||||
@@ -74,17 +69,34 @@ impl CadCommand for MLeaderCommand {
|
||||
if self.verts.is_empty() {
|
||||
t!("MLEADER Specify arrowhead point:").into_owned()
|
||||
} else {
|
||||
t!(
|
||||
"MLEADER Specify next point [%{count} pts — Enter to place text]:",
|
||||
count = self.verts.len()
|
||||
)
|
||||
.into_owned()
|
||||
t!("MLEADER Specify landing point:").into_owned()
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: DVec3) -> CmdResult {
|
||||
self.verts.push(pt);
|
||||
CmdResult::NeedPoint
|
||||
|
||||
if self.verts.len() < 2 {
|
||||
return CmdResult::NeedPoint;
|
||||
}
|
||||
|
||||
let local: Vec<DVec3> = self
|
||||
.verts
|
||||
.iter()
|
||||
.map(|point| self.plane.to_local(*point))
|
||||
.collect();
|
||||
|
||||
let ml = build_mleader(
|
||||
"",
|
||||
&local,
|
||||
Mat4::IDENTITY,
|
||||
self.style.as_ref(),
|
||||
self.display_scale,
|
||||
);
|
||||
|
||||
CmdResult::CommitAndEditText(
|
||||
self.plane.place_entity(EntityType::MultiLeader(ml)),
|
||||
)
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
@@ -162,14 +174,14 @@ fn build_mleader(
|
||||
style: Option<&acadrust::objects::MultiLeaderStyle>,
|
||||
display_scale: f64,
|
||||
) -> MultiLeader {
|
||||
// Last vertex = content/text location; remaining = leader line points
|
||||
let (leader_pts, content_pt) = verts.split_at(verts.len() - 1);
|
||||
let content_pt = content_pt[0];
|
||||
// First click is the arrow; second click is the elbow.
|
||||
let arrow_pt = verts[0];
|
||||
let elbow_pt = verts[1];
|
||||
|
||||
let leader_v3: Vec<Vector3> = leader_pts.iter().map(|p| v3(*p)).collect();
|
||||
let content_v3 = v3(content_pt);
|
||||
let arrow_v3 = v3(arrow_pt);
|
||||
let elbow_v3 = v3(elbow_pt);
|
||||
|
||||
let mut ml = MultiLeader::with_text(text, content_v3, leader_v3);
|
||||
let mut ml = MultiLeader::with_text(text, elbow_v3, vec![arrow_v3]);
|
||||
if let Some(style) = style {
|
||||
crate::scene::annotative::apply_mleader_style(&mut ml, style);
|
||||
} else {
|
||||
@@ -186,33 +198,41 @@ fn build_mleader(
|
||||
ml.context.text_height = ml.text_height * display_scale;
|
||||
ml.context.arrowhead_size = ml.arrowhead_size * display_scale;
|
||||
ml.context.landing_gap = landing_gap;
|
||||
// "Horizontal" landing + text run along the active UCS X axis (identity =
|
||||
// world), so the annotation reads square to the user's coordinate system.
|
||||
// Align the landing and text with the active UCS X axis.
|
||||
let ux = ucs.transform_vector3(Vec3::X).normalize_or(Vec3::X);
|
||||
// Which side of the leader the text sits on, measured along the UCS X axis.
|
||||
let last_leader = leader_pts.last().copied().unwrap_or(content_pt);
|
||||
let to_right = (content_pt - last_leader).dot(ux.as_dvec3()) >= 0.0;
|
||||
let to_right = (elbow_pt - arrow_pt).dot(ux.as_dvec3()) >= 0.0;
|
||||
let sign = if to_right { 1.0 } else { -1.0 };
|
||||
let landing = ux * (sign as f32);
|
||||
ml.context.text_attachment_point =
|
||||
if to_right {
|
||||
acadrust::entities::multileader::TextAttachmentPointType::Left
|
||||
} else {
|
||||
acadrust::entities::multileader::TextAttachmentPointType::Right
|
||||
};
|
||||
|
||||
// Text + landing read along the UCS X axis. text_direction is what the
|
||||
// renderer consults first, so set both.
|
||||
ml.context.text_rotation = (ux.y as f64).atan2(ux.x as f64);
|
||||
ml.context.text_direction = Vector3::new(ux.x as f64, ux.y as f64, 0.0);
|
||||
|
||||
if let Some(root) = ml.context.leader_roots.first_mut() {
|
||||
// Leader ends at the clicked point; the landing runs from there toward
|
||||
// the text along the UCS X axis.
|
||||
root.direction = Vector3::new(landing.x as f64, landing.y as f64, 0.0);
|
||||
root.connection_point = content_v3;
|
||||
root.direction =
|
||||
Vector3::new(landing.x as f64, landing.y as f64, 0.0);
|
||||
|
||||
root.connection_point = elbow_v3;
|
||||
root.landing_distance = landing_distance;
|
||||
}
|
||||
|
||||
// Seed the text one landing-length past the leader end, on the side the
|
||||
// user dragged toward, offset along the UCS X axis.
|
||||
// Place text beyond the landing and gap.
|
||||
let off = landing * (landing_distance + landing_gap) as f32;
|
||||
ml.context.text_location =
|
||||
Vector3::new(content_v3.x + off.x as f64, content_v3.y + off.y as f64, content_v3.z);
|
||||
|
||||
let text_location = Vector3::new(
|
||||
elbow_v3.x + off.x as f64,
|
||||
elbow_v3.y + off.y as f64,
|
||||
elbow_v3.z,
|
||||
);
|
||||
|
||||
ml.context.text_location = text_location;
|
||||
ml.context.content_base_point = text_location;
|
||||
|
||||
ml
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user