feat(dimension): complete linear workflow

This commit is contained in:
Hakan Seven committed 2026-08-24 23:42:49 +03:00
commit 959197bb3c
22 files changed
+2576 -258

No files matched your search

+247 -14
View File
@@ -1,6 +1,7 @@
use acadrust::entities::{Dimension, DimensionLinear};
use acadrust::types::Vector3;
use acadrust::EntityType;
use cadkernel::geom2d::{closest_point, Curve, Line as KernelLine};
use crate::command::{CadCommand, CmdResult, WorkingPlane};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
@@ -57,6 +58,25 @@ enum Step {
DimensionLine { first: DVec3, second: DVec3 },
}
#[derive(Clone, Copy)]
enum AxisMode {
Automatic,
Horizontal,
Vertical,
Rotated(f64),
}
impl AxisMode {
fn axis(self, first: DVec3, second: DVec3, point: DVec3) -> DVec3 {
match self {
Self::Automatic => measure_axis(first, second, point),
Self::Horizontal => DVec3::X,
Self::Vertical => DVec3::Y,
Self::Rotated(angle) => DVec3::new(angle.cos(), angle.sin(), 0.0),
}
}
}
pub struct LinearDimensionCommand {
step: Step,
plane: WorkingPlane,
@@ -68,6 +88,13 @@ pub struct LinearDimensionCommand {
text_angle: Option<f64>,
/// True while the next typed value is captured as the text angle.
awaiting_angle: bool,
/// True while a rotation value for the Rotated option is being entered.
awaiting_rotation: bool,
axis_mode: AxisMode,
selecting_object: bool,
picked_entity: Option<EntityType>,
source_handle: Option<acadrust::Handle>,
mtext_override: bool,
}
impl LinearDimensionCommand {
@@ -79,6 +106,12 @@ impl LinearDimensionCommand {
awaiting_text: false,
text_angle: None,
awaiting_angle: false,
awaiting_rotation: false,
axis_mode: AxisMode::Automatic,
selecting_object: false,
picked_entity: None,
source_handle: None,
mtext_override: false,
}
}
}
@@ -94,18 +127,32 @@ impl CadCommand for LinearDimensionCommand {
fn prompt(&self) -> String {
if self.awaiting_text {
return t!("DIMLINEAR Enter dimension text (blank = measured value):").into_owned();
return if self.mtext_override {
t!("DIMLINEAR Enter formatted dimension text (blank = measured value):")
.into_owned()
} else {
t!("DIMLINEAR Enter dimension text (blank = measured value):").into_owned()
};
}
if self.awaiting_angle {
return t!("DIMLINEAR Specify text angle (degrees):").into_owned();
}
if self.awaiting_rotation {
return t!("DIMLINEAR Specify dimension line angle (degrees):").into_owned();
}
if self.selecting_object {
return t!("DIMLINEAR Select object to dimension:").into_owned();
}
match self.step {
Step::FirstPoint => t!("DIMLINEAR Specify first extension line origin:").into_owned(),
Step::FirstPoint => {
t!("DIMLINEAR Specify first extension line origin or press Enter to select object:")
.into_owned()
}
Step::SecondPoint(_) => {
t!("DIMLINEAR Specify second extension line origin [Text/Angle]:").into_owned()
t!("DIMLINEAR Specify second extension line origin:").into_owned()
}
Step::DimensionLine { .. } => {
t!("DIMLINEAR Specify dimension line location [Text/Angle]:").into_owned()
t!("DIMLINEAR Specify dimension line location [Mtext/Text/Angle/Horizontal/Vertical/Rotated]:").into_owned()
}
}
}
@@ -117,6 +164,9 @@ impl CadCommand for LinearDimensionCommand {
CmdResult::NeedPoint
}
Step::SecondPoint(first) => {
if pt.distance_squared(first) <= 1e-24 {
return CmdResult::NeedPoint;
}
self.step = Step::DimensionLine { first, second: pt };
CmdResult::NeedPoint
}
@@ -125,21 +175,29 @@ impl CadCommand for LinearDimensionCommand {
let second = self.plane.to_local(second);
let pt = self.plane.to_local(pt);
let mut dim = DimensionLinear::new(v3(first), v3(second));
let axis = measure_axis(first, second, pt);
let axis = self.axis_mode.axis(first, second, pt);
dim.rotation = axis.y.atan2(axis.x);
dim.definition_point = v3(pt);
dim.base.definition_point = v3(pt);
dim.set_offset(dimension_line_offset(second, pt, axis));
dim.base.definition_point = dim.definition_point;
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();
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 UCS-derived rotation.
if let Some(a) = self.text_angle {
dim.base.text_rotation = a;
}
CmdResult::CommitAndExit(self.plane.place_entity(EntityType::Dimension(
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)
}
}
}
}
@@ -154,6 +212,14 @@ impl CadCommand for LinearDimensionCommand {
self.awaiting_angle = false;
return CmdResult::NeedPoint;
}
if self.awaiting_rotation {
self.awaiting_rotation = false;
return CmdResult::NeedPoint;
}
if matches!(self.step, Step::FirstPoint) {
self.selecting_object = true;
return CmdResult::NeedPoint;
}
CmdResult::Cancel
}
@@ -168,7 +234,7 @@ impl CadCommand for LinearDimensionCommand {
fn point_step_accepts_keywords(&self) -> bool {
// While typing the override text or angle, route input as a value, not
// a point pick / keyword.
!self.awaiting_text && !self.awaiting_angle
!self.awaiting_text && !self.awaiting_angle && !self.awaiting_rotation
}
fn wants_text_with_spaces(&self) -> bool {
@@ -199,8 +265,24 @@ impl CadCommand for LinearDimensionCommand {
self.awaiting_angle = false;
return Some(CmdResult::NeedPoint);
}
if self.awaiting_rotation {
if let Some(angle) = crate::entities::common::parse_typed_angle(text.trim()) {
self.axis_mode = AxisMode::Rotated(angle);
}
self.awaiting_rotation = false;
return Some(CmdResult::NeedPoint);
}
if !matches!(self.step, Step::DimensionLine { .. }) {
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)
}
@@ -208,10 +290,54 @@ impl CadCommand for LinearDimensionCommand {
self.awaiting_angle = true;
Some(CmdResult::NeedPoint)
}
"H" | "HORIZONTAL" => {
self.axis_mode = AxisMode::Horizontal;
Some(CmdResult::NeedPoint)
}
"V" | "VERTICAL" => {
self.axis_mode = AxisMode::Vertical;
Some(CmdResult::NeedPoint)
}
"R" | "ROTATED" => {
self.awaiting_rotation = true;
Some(CmdResult::NeedPoint)
}
_ => None,
}
}
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: acadrust::Handle, point: DVec3) -> CmdResult {
let Some(entity) = self.picked_entity.take() else {
return CmdResult::NeedPoint;
};
let Some((first, second)) = dimension_source_points(&entity, point) else {
return CmdResult::NeedPoint;
};
if first.distance_squared(second) <= 1e-24 {
return CmdResult::NeedPoint;
}
self.source_handle = Some(handle);
self.selecting_object = false;
self.step = Step::DimensionLine { first, second };
CmdResult::NeedPoint
}
fn on_mouse_move(&mut self, pt: DVec3) -> Option<WireModel> {
match self.step {
Step::FirstPoint => None,
@@ -220,7 +346,7 @@ impl CadCommand for LinearDimensionCommand {
let first = self.plane.to_local(first);
let second = self.plane.to_local(second);
let pt = self.plane.to_local(pt);
let axis = measure_axis(first, second, pt);
let axis = self.axis_mode.axis(first, second, pt);
let points = linear_dimension_preview(first, second, pt, axis)
.into_iter()
.map(|point| self.plane.to_world(point))
@@ -235,6 +361,97 @@ fn v3(pt: DVec3) -> Vector3 {
Vector3::new(pt.x, pt.y, pt.z)
}
fn dimension_line_offset(second: DVec3, point: DVec3, axis: DVec3) -> f64 {
let perpendicular = DVec3::new(-axis.y, axis.x, 0.0);
(point - second).dot(perpendicular)
}
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::LwPolyline(polyline) => nearest_planar_source(
polyline
.vertices
.iter()
.map(|vertex| [vertex.location.x, vertex.location.y]),
polyline.is_closed,
polyline.elevation,
polyline.normal,
click,
),
EntityType::Polyline2D(polyline) => {
let vertices = crate::entities::polyline::drawn_vertices2d(polyline)
.unwrap_or_else(|| polyline.vertices.clone());
nearest_planar_source(
vertices
.iter()
.map(|vertex| [vertex.location.x, vertex.location.y]),
polyline.is_closed(),
polyline.elevation,
polyline.normal,
click,
)
}
_ => None,
}
}
fn nearest_planar_source(
points: impl IntoIterator<Item = [f64; 2]>,
closed: bool,
elevation: f64,
normal: Vector3,
click: DVec3,
) -> Option<(DVec3, DVec3)> {
let click = crate::scene::view::transform::wcs_point_to_ocs(
(click.x, click.y, click.z),
(normal.x, normal.y, normal.z),
);
let (first, second) = nearest_segment(points, closed, [click.0, click.1])?;
Some((
ocs_point(first, elevation, normal),
ocs_point(second, elevation, normal),
))
}
fn nearest_segment(
points: impl IntoIterator<Item = [f64; 2]>,
closed: bool,
click: [f64; 2],
) -> Option<([f64; 2], [f64; 2])> {
let points: Vec<_> = points.into_iter().collect();
if points.len() < 2 {
return None;
}
let count = if closed { points.len() } else { points.len() - 1 };
(0..count)
.map(|index| {
let first = points[index];
let second = points[(index + 1) % points.len()];
let distance = closest_point(
&Curve::Line(KernelLine {
start: first,
end: second,
}),
click,
)
.distance;
(distance, first, second)
})
.min_by(|a, b| a.0.total_cmp(&b.0))
.map(|(_, first, second)| (first, second))
}
fn ocs_point(point: [f64; 2], elevation: f64, normal: Vector3) -> DVec3 {
let point = crate::scene::view::transform::ocs_point_to_wcs(
(point[0], point[1], elevation),
(normal.x, normal.y, normal.z),
);
DVec3::new(point.0, point.1, point.2)
}
fn preview_wire(points: Vec<DVec3>) -> WireModel {
WireModel {
point_marker: None,
@@ -290,7 +507,23 @@ fn dim_line_endpoints(first: DVec3, second: DVec3, def: DVec3, axis: DVec3) -> (
fn linear_dimension_preview(first: DVec3, second: DVec3, def: DVec3, axis: DVec3) -> Vec<DVec3> {
let (d1, d2) = dim_line_endpoints(first, second, def, axis);
let nan = DVec3::new(f64::NAN, f64::NAN, f64::NAN);
vec![first, d1, nan, second, d2, nan, d1, d2]
let arrow = 0.22;
let perp = DVec3::new(-axis.y, axis.x, 0.0);
let text = linear_text_pos(first, second, def, axis);
let half_width = ((second - first).length().log10().max(0.0) + 1.0) * 0.18;
let half_height = 0.16;
vec![
first, d1, nan, second, d2, nan, d1, d2, nan,
d1, d1 + axis * arrow + perp * arrow * 0.45, nan,
d1, d1 + axis * arrow - perp * arrow * 0.45, nan,
d2, d2 - axis * arrow + perp * arrow * 0.45, nan,
d2, d2 - axis * arrow - perp * arrow * 0.45, nan,
text - axis * half_width - perp * half_height,
text + axis * half_width - perp * half_height,
text + axis * half_width + perp * half_height,
text - axis * half_width + perp * half_height,
text - axis * half_width - perp * half_height,
]
}
fn linear_text_pos(first: DVec3, second: DVec3, def: DVec3, axis: DVec3) -> DVec3 {