forked from 1b-agent/opencadstudio-upstream
feat(dimension): complete radius workflow
This commit is contained in:
commit
8140c39e0d
27 files changed
+760
-133
No files matched your search
@@ -1,12 +1,14 @@
|
||||
use acadrust::entities::{Dimension, DimensionRadius};
|
||||
use acadrust::types::Vector3;
|
||||
use acadrust::types::{Handle, Vector3};
|
||||
use acadrust::EntityType;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult, WorkingPlane};
|
||||
use crate::command::{
|
||||
CadCommand, CmdOption, CmdResult, DimensionAssociationInput, WorkingPlane,
|
||||
};
|
||||
use crate::modules::{IconKind, ModuleEvent, ToolDef};
|
||||
use crate::scene::model::wire_model::WireModel;
|
||||
use glam::{DVec3, Vec3};
|
||||
use crate::t;
|
||||
use glam::{DVec3, Vec3};
|
||||
|
||||
pub const ICON: IconKind = IconKind::Svg(include_bytes!("../../../assets/icons/dim_radius.svg"));
|
||||
|
||||
@@ -20,40 +22,43 @@ pub fn tool() -> ToolDef {
|
||||
}
|
||||
|
||||
enum Step {
|
||||
CenterPoint,
|
||||
RadiusPoint(DVec3),
|
||||
TextPoint { center: DVec3, point: DVec3 },
|
||||
SelectObject,
|
||||
DimLine(crate::scene::dimension_assoc::RadialSourceGeometry),
|
||||
}
|
||||
|
||||
pub struct RadiusDimensionCommand {
|
||||
step: Step,
|
||||
plane: WorkingPlane,
|
||||
/// Optional text that replaces the measured value (None = measurement).
|
||||
text_override: Option<String>,
|
||||
/// True while the next typed line is captured as the text override.
|
||||
awaiting_text: bool,
|
||||
/// Explicit text rotation in radians (None = follow the UCS/style).
|
||||
text_angle: Option<f64>,
|
||||
/// True while the next typed value is captured as the text angle.
|
||||
awaiting_angle: bool,
|
||||
picked_entity: Option<EntityType>,
|
||||
source_handle: Option<Handle>,
|
||||
}
|
||||
|
||||
impl RadiusDimensionCommand {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
step: Step::CenterPoint,
|
||||
plane: WorkingPlane::default(),
|
||||
step: Step::SelectObject,
|
||||
text_override: None,
|
||||
awaiting_text: false,
|
||||
text_angle: None,
|
||||
awaiting_angle: false,
|
||||
picked_entity: None,
|
||||
source_handle: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn editor_anchor(&self) -> DVec3 {
|
||||
match self.step {
|
||||
Step::SelectObject => DVec3::ZERO,
|
||||
Step::DimLine(source) => dvec(source.point_at_angle(source.start_angle)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for RadiusDimensionCommand {
|
||||
fn set_working_plane(&mut self, plane: WorkingPlane) {
|
||||
self.plane = plane;
|
||||
fn set_working_plane(&mut self, _plane: WorkingPlane) {
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
@@ -68,48 +73,50 @@ impl CadCommand for RadiusDimensionCommand {
|
||||
return t!("DIMRADIUS Specify text angle (degrees):").into_owned();
|
||||
}
|
||||
match self.step {
|
||||
Step::CenterPoint => t!("DIMRADIUS Specify center point:").into_owned(),
|
||||
Step::RadiusPoint(_) => t!("DIMRADIUS Specify radius point:").into_owned(),
|
||||
Step::TextPoint { .. } => {
|
||||
t!("DIMRADIUS Specify dimension line location [Text/Angle]:").into_owned()
|
||||
Step::SelectObject => t!("DIMRADIUS Select arc, circle, or polyline arc:").into_owned(),
|
||||
Step::DimLine(_) => {
|
||||
t!("DIMRADIUS Specify dimension line location [Mtext/Text/Angle]:").into_owned()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: DVec3) -> CmdResult {
|
||||
match self.step {
|
||||
Step::CenterPoint => {
|
||||
self.step = Step::RadiusPoint(pt);
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
Step::RadiusPoint(center) => {
|
||||
self.step = Step::TextPoint { center, point: pt };
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
Step::TextPoint { center, point } => {
|
||||
let center = self.plane.to_local(center);
|
||||
let point = self.plane.to_local(point);
|
||||
let pt = self.plane.to_local(pt);
|
||||
Step::SelectObject => CmdResult::NeedPoint,
|
||||
Step::DimLine(source) => {
|
||||
let source_plane = WorkingPlane::new(
|
||||
DVec3::from_array(source.plane.origin),
|
||||
DVec3::from_array(source.plane.x_axis),
|
||||
DVec3::from_array(source.plane.y_axis),
|
||||
);
|
||||
let center_world = dvec(source.center_world());
|
||||
let point_world = dvec(source.chord_at(pt.to_array()));
|
||||
let center = source_plane.to_local(center_world);
|
||||
let point = source_plane.to_local(point_world);
|
||||
let pt = source_plane.to_local(pt);
|
||||
let mut dim = DimensionRadius::new(v3(center), v3(point));
|
||||
dim.base.definition_point = v3(point);
|
||||
dim.base.text_middle_point = v3(pt);
|
||||
dim.base.insertion_point = v3(pt);
|
||||
dim.base.text_user_positioned = true;
|
||||
dim.leader_length = point.distance(pt);
|
||||
dim.base.actual_measurement = dim.measurement();
|
||||
dim.base.user_text = self.text_override.clone();
|
||||
// An explicit text angle overrides the default rotation.
|
||||
crate::entities::dimension::set_dimension_text_override(
|
||||
&mut dim.base,
|
||||
self.text_override.clone(),
|
||||
);
|
||||
if let Some(a) = self.text_angle {
|
||||
dim.base.text_rotation = a;
|
||||
}
|
||||
CmdResult::CommitAndExit(self.plane.place_entity(EntityType::Dimension(
|
||||
Dimension::Radius(dim),
|
||||
)))
|
||||
CmdResult::CommitDimension {
|
||||
entity: source_plane.place_entity(EntityType::Dimension(Dimension::Radius(dim))),
|
||||
association: DimensionAssociationInput::Infer(self.source_handle),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
// A bare Enter while entering override text accepts the measured value.
|
||||
if self.awaiting_text {
|
||||
self.awaiting_text = false;
|
||||
return CmdResult::NeedPoint;
|
||||
@@ -130,19 +137,28 @@ impl CadCommand for RadiusDimensionCommand {
|
||||
}
|
||||
|
||||
fn point_step_accepts_keywords(&self) -> bool {
|
||||
// While entering the override text or angle it is a value, not a point step.
|
||||
!self.awaiting_text && !self.awaiting_angle
|
||||
}
|
||||
|
||||
fn wants_text_with_spaces(&self) -> bool {
|
||||
// The override text may contain spaces.
|
||||
self.awaiting_text
|
||||
}
|
||||
|
||||
fn options(&self) -> Vec<CmdOption> {
|
||||
if matches!(self.step, Step::DimLine(_)) && !self.awaiting_text && !self.awaiting_angle {
|
||||
vec![
|
||||
CmdOption::new("MText", "MTEXT"),
|
||||
CmdOption::new("Text", "TEXT"),
|
||||
CmdOption::new("Angle", "ANGLE"),
|
||||
]
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
|
||||
if self.awaiting_text {
|
||||
let t = text.trim();
|
||||
// Blank (or the "<>" placeholder) keeps the measured value.
|
||||
self.text_override = if t.is_empty() || t == "<>" {
|
||||
None
|
||||
} else {
|
||||
@@ -153,7 +169,6 @@ impl CadCommand for RadiusDimensionCommand {
|
||||
}
|
||||
if self.awaiting_angle {
|
||||
let t = text.trim();
|
||||
// Blank clears any explicit angle (follow the UCS/style again).
|
||||
self.text_angle = if t.is_empty() {
|
||||
None
|
||||
} else {
|
||||
@@ -162,11 +177,21 @@ impl CadCommand for RadiusDimensionCommand {
|
||||
self.awaiting_angle = false;
|
||||
return Some(CmdResult::NeedPoint);
|
||||
}
|
||||
match text.trim().to_uppercase().as_str() {
|
||||
"T" | "TEXT" | "M" | "MTEXT" => {
|
||||
if !matches!(self.step, Step::DimLine(_)) {
|
||||
return None;
|
||||
}
|
||||
match text.trim().to_ascii_uppercase().as_str() {
|
||||
"T" | "TEXT" => {
|
||||
self.awaiting_text = true;
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
"M" | "MTEXT" => {
|
||||
Some(CmdResult::SuspendForMTextInput {
|
||||
pos: self.editor_anchor(),
|
||||
initial: self.text_override.clone().unwrap_or_default(),
|
||||
height: 2.5,
|
||||
})
|
||||
}
|
||||
"A" | "ANGLE" => {
|
||||
self.awaiting_angle = true;
|
||||
Some(CmdResult::NeedPoint)
|
||||
@@ -175,18 +200,67 @@ impl CadCommand for RadiusDimensionCommand {
|
||||
}
|
||||
}
|
||||
|
||||
fn on_editor_text(&mut self, value: String) {
|
||||
let value = value.trim();
|
||||
self.text_override = if value.is_empty() || value == "<>" {
|
||||
None
|
||||
} else {
|
||||
Some(value.to_string())
|
||||
};
|
||||
}
|
||||
|
||||
fn on_editor_closed(&mut self, _committed: bool) -> CmdResult {
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
|
||||
fn needs_entity_pick(&self) -> bool {
|
||||
matches!(self.step, Step::SelectObject)
|
||||
}
|
||||
|
||||
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(source) = crate::scene::dimension_assoc::radial_source_at(
|
||||
&entity,
|
||||
Vector3::new(point.x, point.y, point.z),
|
||||
) else {
|
||||
return CmdResult::NeedPoint;
|
||||
};
|
||||
if !source.radius.is_finite() || source.radius <= 1e-12 {
|
||||
return CmdResult::NeedPoint;
|
||||
}
|
||||
self.source_handle = Some(handle);
|
||||
self.step = Step::DimLine(source);
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
|
||||
fn on_mouse_move(&mut self, pt: DVec3) -> Option<WireModel> {
|
||||
let pt = pt.as_vec3();
|
||||
match self.step {
|
||||
Step::CenterPoint => None,
|
||||
Step::RadiusPoint(center) => Some(preview_wire(vec![center.as_vec3(), pt])),
|
||||
Step::TextPoint { center, point } => Some(preview_wire(vec![
|
||||
center.as_vec3(),
|
||||
point.as_vec3(),
|
||||
Vec3::new(f32::NAN, f32::NAN, f32::NAN),
|
||||
point.as_vec3(),
|
||||
pt,
|
||||
])),
|
||||
Step::SelectObject => None,
|
||||
Step::DimLine(source) => {
|
||||
let center = dvec(source.center_world()).as_vec3();
|
||||
let point = dvec(source.chord_at(pt.to_array())).as_vec3();
|
||||
Some(preview_wire(vec![
|
||||
center,
|
||||
point,
|
||||
Vec3::new(f32::NAN, f32::NAN, f32::NAN),
|
||||
point,
|
||||
pt.as_vec3(),
|
||||
]))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -195,6 +269,10 @@ fn v3(pt: DVec3) -> Vector3 {
|
||||
Vector3::new(pt.x, pt.y, pt.z)
|
||||
}
|
||||
|
||||
fn dvec(point: Vector3) -> DVec3 {
|
||||
DVec3::new(point.x, point.y, point.z)
|
||||
}
|
||||
|
||||
fn preview_wire(points: Vec<Vec3>) -> WireModel {
|
||||
WireModel {
|
||||
point_marker: None,
|
||||
@@ -209,9 +287,9 @@ fn preview_wire(points: Vec<Vec3>) -> 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: "dimradius_preview".to_string(),
|
||||
points: points.into_iter().map(|p| [p.x, p.y, p.z]).collect(),
|
||||
points_low: Vec::new(),
|
||||
@@ -231,6 +309,4 @@ fn preview_wire(points: Vec<Vec3>) -> WireModel {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ── Autocomplete registry ─────────────────────────────────
|
||||
inventory::submit!(crate::command::CommandRegistration { names: &["DIMRADIUS"] }); // RadiusDimensionCommand
|
||||
inventory::submit!(crate::command::CommandRegistration { names: &["DIMRADIUS"] });
|
||||
Reference in new issue
Block a user