fix(dimension): complete aligned workflow

Preserve association modes, source geometry, and sub-unit formatting.
This commit is contained in:
Hakan Seven committed 2026-08-25 13:18:09 +03:00
commit 97ec3ed67e
27 files changed
+564 -191

No files matched your search

+115 -36
View File
@@ -1,7 +1,7 @@
// DIMALIGNED command — aligned dimension (measures true distance between two points).
use acadrust::entities::{Dimension, DimensionAligned};
use acadrust::types::Vector3;
use acadrust::types::{Handle, Vector3};
use acadrust::EntityType;
use glam::DVec3;
@@ -38,6 +38,10 @@ pub struct AlignedDimensionCommand {
text_angle: Option<f64>,
/// True while the next typed value is captured as the text angle.
awaiting_angle: bool,
selecting_object: bool,
picked_entity: Option<EntityType>,
source_handle: Option<Handle>,
mtext_override: bool,
}
impl AlignedDimensionCommand {
@@ -49,6 +53,10 @@ impl AlignedDimensionCommand {
awaiting_text: false,
text_angle: None,
awaiting_angle: false,
selecting_object: false,
picked_entity: None,
source_handle: None,
mtext_override: false,
}
}
}
@@ -64,18 +72,29 @@ impl CadCommand for AlignedDimensionCommand {
fn prompt(&self) -> String {
if self.awaiting_text {
return t!("DIMALIGNED Enter dimension text (blank = measured value):").into_owned();
return if self.mtext_override {
t!("DIMALIGNED Enter formatted dimension text (blank = measured value):")
.into_owned()
} else {
t!("DIMALIGNED Enter dimension text (blank = measured value):").into_owned()
};
}
if self.awaiting_angle {
return t!("DIMALIGNED Specify text angle (degrees):").into_owned();
}
if self.selecting_object {
return t!("DIMALIGNED Select object to dimension:").into_owned();
}
match self.step {
Step::First => t!("DIMALIGNED Specify first extension line origin:").into_owned(),
Step::First => t!(
"DIMALIGNED Specify first extension line origin or press Enter to select object:"
)
.into_owned(),
Step::Second(_) => {
t!("DIMALIGNED Specify second extension line origin [Text/Angle]:").into_owned()
t!("DIMALIGNED Specify second extension line origin:").into_owned()
}
Step::DimLine { .. } => {
t!("DIMALIGNED Specify dimension line location [Text/Angle]:").into_owned()
t!("DIMALIGNED Specify dimension line location [Mtext/Text/Angle]:").into_owned()
}
}
}
@@ -87,6 +106,9 @@ impl CadCommand for AlignedDimensionCommand {
CmdResult::NeedPoint
}
Step::Second(p1) => {
if pt.distance_squared(p1) <= 1e-24 {
return CmdResult::NeedPoint;
}
self.step = Step::DimLine { p1, p2: pt };
CmdResult::NeedPoint
}
@@ -95,29 +117,25 @@ impl CadCommand for AlignedDimensionCommand {
let p2 = self.plane.to_local(p2);
let pt = self.plane.to_local(pt);
let mut dim = DimensionAligned::new(v3(p1), v3(p2));
// The dimension line runs through the cursor: store it as the
// definition point and let the renderer project it onto the
// line perpendicular (same as the preview and DIMLINEAR).
//
// The old path called `set_offset` with the straight-line
// p2→cursor *distance*, which `set_offset` then re-applies along
// the line perpendicular — placing the line at the wrong
// perpendicular distance and always on the +perp side, so it
// never matched the preview the user was dragging. (#150)
// Store the cursor position so commit matches the preview.
dim.definition_point = v3(pt);
dim.base.definition_point = v3(pt);
let (d1, d2) = dim_line_endpoints(p1, p2, pt);
dim.base.text_middle_point = v3((d1 + d2) * 0.5);
dim.base.insertion_point = dim.base.text_middle_point;
dim.base.actual_measurement = dim.measurement();
dim.base.user_text = self.text_override.clone();
crate::entities::dimension::set_dimension_text_override(
&mut dim.base,
self.text_override.clone(),
);
// An explicit text angle overrides the default rotation.
if let Some(a) = self.text_angle {
dim.base.text_rotation = a;
}
CmdResult::CommitAndExit(self.plane.place_entity(EntityType::Dimension(
Dimension::Aligned(dim),
)))
CmdResult::CommitDimension {
entity: self.plane.place_entity(EntityType::Dimension(Dimension::Aligned(dim))),
source: self.source_handle,
}
}
}
}
@@ -132,6 +150,10 @@ impl CadCommand for AlignedDimensionCommand {
self.awaiting_angle = false;
return CmdResult::NeedPoint;
}
if matches!(self.step, Step::First) {
self.selecting_object = true;
return CmdResult::NeedPoint;
}
CmdResult::Cancel
}
@@ -173,8 +195,17 @@ impl CadCommand for AlignedDimensionCommand {
self.awaiting_angle = false;
return Some(CmdResult::NeedPoint);
}
if !matches!(self.step, Step::DimLine { .. }) {
return None;
}
match text.trim().to_uppercase().as_str() {
"T" | "TEXT" | "M" | "MTEXT" => {
"T" | "TEXT" => {
self.mtext_override = false;
self.awaiting_text = true;
Some(CmdResult::NeedPoint)
}
"M" | "MTEXT" => {
self.mtext_override = true;
self.awaiting_text = true;
Some(CmdResult::NeedPoint)
}
@@ -186,6 +217,38 @@ impl CadCommand for AlignedDimensionCommand {
}
}
fn needs_entity_pick(&self) -> bool {
self.selecting_object
}
fn entity_pick_highlights_hover(&self) -> bool {
true
}
fn inject_before_entity_pick(&self) -> bool {
true
}
fn inject_picked_entity(&mut self, entity: EntityType) {
self.picked_entity = Some(entity);
}
fn on_entity_pick(&mut self, handle: Handle, point: DVec3) -> CmdResult {
let Some(entity) = self.picked_entity.take() else {
return CmdResult::NeedPoint;
};
let Some((p1, p2)) = super::linear_dim::dimension_source_points(&entity, point) else {
return CmdResult::NeedPoint;
};
if p1.distance_squared(p2) <= 1e-24 {
return CmdResult::NeedPoint;
}
self.source_handle = Some(handle);
self.selecting_object = false;
self.step = Step::DimLine { p1, p2 };
CmdResult::NeedPoint
}
fn on_mouse_move(&mut self, pt: DVec3) -> Option<WireModel> {
let (p1, p2) = match self.step {
Step::First => return None,
@@ -272,13 +335,15 @@ fn dim_line_endpoints(p1: DVec3, p2: DVec3, dim_pt: DVec3) -> (DVec3, DVec3) {
}
fn preview_aligned(p1: DVec3, p2: DVec3, dim_pt: DVec3) -> WireModel {
// Show ext lines + dim line.
let (d1, d2) = dim_line_endpoints(p1, p2, dim_pt);
// Preview WireModel points are screen/GPU-side: downcast to f32.
let p1 = p1.as_vec3();
let p2 = p2.as_vec3();
let d1 = d1.as_vec3();
let d2 = d2.as_vec3();
let axis = (d2 - d1).normalize_or_zero();
let perp = DVec3::new(-axis.y, axis.x, 0.0);
let nan = [f32::NAN, 0.0, 0.0];
let arrow = 0.22;
let text = (d1 + d2) * 0.5 + perp * 0.15;
let half_width = ((p2 - p1).length().log10().max(0.0) + 1.0) * 0.18;
let half_height = 0.16;
let to_point = |point: DVec3| point.as_vec3().to_array();
WireModel {
point_marker: None,
taper_widths: Vec::new(),
@@ -292,19 +357,33 @@ fn preview_aligned(p1: DVec3, p2: DVec3, dim_pt: DVec3) -> WireModel {
render_instance: None,
pick_tris: Vec::new(),
pick_tris_low: Vec::new(),
dash_from_start: false,
dash_align_end: None,
text_verts: Vec::new(),
dash_from_start: false,
dash_align_end: None,
text_verts: Vec::new(),
name: "dimaligned_preview".into(),
points: vec![
[p1.x, p1.y, p1.z],
[d1.x, d1.y, d1.z],
[f32::NAN, 0.0, 0.0],
[p2.x, p2.y, p2.z],
[d2.x, d2.y, d2.z],
[f32::NAN, 0.0, 0.0],
[d1.x, d1.y, d1.z],
[d2.x, d2.y, d2.z],
to_point(p1),
to_point(d1),
nan,
to_point(p2),
to_point(d2),
nan,
to_point(d1),
to_point(d2),
nan,
to_point(d1 + axis * arrow + perp * arrow * 0.45),
to_point(d1),
to_point(d1 + axis * arrow - perp * arrow * 0.45),
nan,
to_point(d2 - axis * arrow + perp * arrow * 0.45),
to_point(d2),
to_point(d2 - axis * arrow - perp * arrow * 0.45),
nan,
to_point(text - axis * half_width - perp * half_height),
to_point(text + axis * half_width - perp * half_height),
to_point(text + axis * half_width + perp * half_height),
to_point(text - axis * half_width + perp * half_height),
to_point(text - axis * half_width - perp * half_height),
],
points_low: Vec::new(),
color: WireModel::CYAN,
+28 -18
View File
@@ -1,7 +1,9 @@
use acadrust::entities::{Dimension, DimensionLinear};
use acadrust::types::Vector3;
use acadrust::EntityType;
use cadkernel::geom2d::{closest_point, Curve, Line as KernelLine};
use cadkernel::geom2d::{
closest_point, Circle as KernelCircle, Curve, Line as KernelLine,
};
use crate::command::{CadCommand, CmdResult, WorkingPlane};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
@@ -9,18 +11,7 @@ use crate::scene::model::wire_model::WireModel;
use glam::DVec3;
use crate::t;
/// Which axis a linear dimension measures along.
///
/// Not the segment's own direction. A linear dimension reports the run or the
/// rise, and which of the two is wanted is said by where the dimension line is
/// put: pulled clear of the points above or below, the horizontal distance is
/// what is left to show; pulled out to the side, the vertical one. Taking the
/// answer from the two origins alone made every shallow tilt horizontal
/// forever, whatever the cursor did. (#669)
///
/// "Clear of the points" is measured against their extent rather than their
/// midpoint: a dimension line dragged straight up from near one end of a long
/// pair is still above them, though it is a long way from the middle.
/// Select the measured axis from where the dimension line clears the points.
fn measure_axis(first: DVec3, second: DVec3, def: DVec3) -> DVec3 {
let outside = |value: f64, a: f64, b: f64| {
let (low, high) = if a <= b { (a, b) } else { (b, a) };
@@ -193,10 +184,9 @@ impl CadCommand for LinearDimensionCommand {
let entity = self.plane.place_entity(EntityType::Dimension(
Dimension::Linear(dim),
));
if let Some(source) = self.source_handle {
CmdResult::CommitAssociativeDimension { entity, source }
} else {
CmdResult::CommitAndExit(entity)
CmdResult::CommitDimension {
entity,
source: self.source_handle,
}
}
}
@@ -366,11 +356,31 @@ fn dimension_line_offset(second: DVec3, point: DVec3, axis: DVec3) -> f64 {
(point - second).dot(perpendicular)
}
fn dimension_source_points(entity: &EntityType, click: DVec3) -> Option<(DVec3, DVec3)> {
pub(crate) fn dimension_source_points(
entity: &EntityType,
click: DVec3,
) -> Option<(DVec3, DVec3)> {
let point = |p: Vector3| DVec3::new(p.x, p.y, p.z);
match entity {
EntityType::Line(line) => Some((point(line.start), point(line.end))),
EntityType::Arc(arc) => Some((point(arc.start_point_wcs()), point(arc.end_point_wcs()))),
EntityType::Circle(circle) => {
let click = crate::scene::view::transform::wcs_point_to_ocs(
(click.x, click.y, click.z),
(circle.normal.x, circle.normal.y, circle.normal.z),
);
let curve = Curve::Circle(KernelCircle {
centre: [circle.center.x, circle.center.y],
radius: circle.radius,
});
let first = closest_point(&curve, [click.0, click.1]).point;
let parameter = curve.parameter_at(first);
let second = curve.point_at((parameter + 0.5).rem_euclid(1.0));
Some((
ocs_point(first, circle.center.z, circle.normal),
ocs_point(second, circle.center.z, circle.normal),
))
}
EntityType::LwPolyline(polyline) => nearest_planar_source(
polyline
.vertices