Complete Coincident command behavior

This commit is contained in:
ramox81 committed 2026-09-15 14:00:26 +03:00
1 parent dfe8b77cca
commit ba2b371269
13 files changed
+1183 -207

No files matched your search

+246 -29
View File
@@ -1,20 +1,10 @@
//! CCONSTRAINT — manual Coincident constraint input.
//! unlike the rest of this module's plain select-and-click constraints,
//! Coincident addresses a *point* on an entity (an endpoint, a circle/arc
//! center), not the whole entity, so it needs two point picks instead of a
//! selection.
//!
//! A `CadCommand` has no document access (see `value.rs`'s module doc
//! comment for the same constraint on `DCONSTRAINT`/`ACONSTRAINT`), so this
//! command only accumulates the two raw points `on_point` receives and hands
//! them back as `CmdResult::AddCoincidentConstraint` — the host resolves
//! each point to a real `ParametricRef` (`parametric_constraints::nearest_parametric_point`)
//! and adds the constraint, the same split `ReassociateCenterMark` already
//! uses for a similar reason.
//! Interactive Coincident input: ordered point/curve picks, repeated
//! point-to-curve placement, and Coincident-only automatic inference.
use acadrust::{EntityType, Handle};
use glam::DVec3;
use crate::command::{CadCommand, CmdResult};
use crate::command::{CadCommand, CmdOption, CmdResult, CoincidentPick};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
pub mod coincident_tool {
@@ -31,45 +21,272 @@ pub mod coincident_tool {
}
}
#[derive(Default)]
#[derive(Clone)]
enum Step {
First,
FirstObject,
Second(CoincidentPick),
PointsOnCurve {
curve: CoincidentPick,
multiple: bool,
},
AutoConstrain {
handles: Vec<Handle>,
},
}
pub struct CoincidentConstraintCommand {
first: Option<DVec3>,
step: Step,
picked_entity: Option<EntityType>,
}
impl CoincidentConstraintCommand {
pub fn new() -> Self {
Self::default()
Self {
step: Step::First,
picked_entity: None,
}
}
fn point_pick(handle: Option<Handle>, point: DVec3) -> CoincidentPick {
CoincidentPick {
handle,
point,
whole_curve: false,
}
}
fn curve_pick(handle: Handle, point: DVec3) -> CoincidentPick {
CoincidentPick {
handle: Some(handle),
point,
whole_curve: true,
}
}
fn picked_constraint_point(entity: &EntityType, point: DVec3) -> bool {
crate::scene::parametric_constraints::is_parametric_point_near(
entity,
acadrust::types::Vector3::new(point.x, point.y, point.z),
)
}
fn valid_curve(entity: &EntityType) -> bool {
matches!(
entity,
EntityType::Line(_)
| EntityType::LwPolyline(_)
| EntityType::Polyline2D(_)
| EntityType::Circle(_)
| EntityType::Arc(_)
| EntityType::Ellipse(_)
| EntityType::Spline(_)
)
}
}
impl CadCommand for CoincidentConstraintCommand {
fn name(&self) -> &'static str {
"CCONSTRAINT"
"GCCOINCIDENT"
}
fn prompt(&self) -> String {
if self.first.is_none() {
"COINCIDENT Specify first point:".to_string()
} else {
"COINCIDENT Specify second point:".to_string()
match self.step.clone() {
Step::First => {
"COINCIDENT Select first point or [Object/Autoconstrain] <Object>:".to_string()
}
Step::FirstObject => "COINCIDENT Select object:".to_string(),
Step::Second(_) => "COINCIDENT Select second point or object:".to_string(),
Step::PointsOnCurve { multiple: false, .. } => {
"COINCIDENT Select point or [Multiple]:".to_string()
}
Step::PointsOnCurve { multiple: true, .. } => {
"COINCIDENT Select points to coincide with the first object (Enter = done):"
.to_string()
}
Step::AutoConstrain { handles } if handles.is_empty() => {
"COINCIDENT Select objects:".to_string()
}
Step::AutoConstrain { handles } => format!(
"COINCIDENT Select objects ({} selected, Enter = apply):",
handles.len()
),
}
}
fn on_point(&mut self, pt: DVec3) -> CmdResult {
match self.first {
None => {
self.first = Some(pt);
match &self.step {
Step::First => {
self.step = Step::Second(Self::point_pick(None, pt));
CmdResult::NeedPoint
}
Some(first) => CmdResult::AddCoincidentConstraint {
point_a: first,
point_b: pt,
Step::FirstObject => CmdResult::NeedPoint,
Step::Second(first) => CmdResult::AddCoincidentConstraint {
first: *first,
second: Self::point_pick(None, pt),
multiple: false,
label: "Coincident constraint",
},
Step::PointsOnCurve { curve, multiple } => CmdResult::AddCoincidentConstraint {
first: *curve,
second: Self::point_pick(None, pt),
multiple: *multiple,
label: "Coincident constraint",
},
Step::AutoConstrain { .. } => CmdResult::NeedPoint,
}
}
fn options(&self) -> Vec<CmdOption> {
match self.step.clone() {
Step::First => vec![
CmdOption::new("Object", "O"),
CmdOption::new("Autoconstrain", "A"),
],
Step::PointsOnCurve { multiple: false, .. } => vec![
CmdOption::new("Point", "P"),
CmdOption::new("Multiple", "M"),
],
Step::PointsOnCurve { multiple: true, .. } => vec![CmdOption::enter("Done")],
_ => Vec::new(),
}
}
fn wants_text_input(&self) -> bool {
true
}
fn point_step_accepts_keywords(&self) -> bool {
!matches!(&self.step, Step::AutoConstrain { .. })
}
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
let keyword = text
.trim()
.trim_start_matches('_')
.to_ascii_uppercase();
match (&mut self.step, keyword.as_str()) {
(Step::First, "O" | "OBJECT") => {
self.step = Step::FirstObject;
Some(CmdResult::NeedPoint)
}
(Step::First, "A" | "AUTOCONSTRAIN") => {
self.step = Step::AutoConstrain { handles: Vec::new() };
Some(CmdResult::NeedPoint)
}
(Step::PointsOnCurve { multiple, .. }, "M" | "MULTIPLE") => {
*multiple = true;
Some(CmdResult::NeedPoint)
}
(Step::PointsOnCurve { .. }, "P" | "POINT") => Some(CmdResult::NeedPoint),
_ => None,
}
}
fn needs_entity_pick(&self) -> bool {
!matches!(&self.step, Step::AutoConstrain { .. })
}
fn entity_pick_accepts_points(&self) -> bool {
true
}
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 {
if handle.is_null() {
return CmdResult::NeedPoint;
}
let is_point = self
.picked_entity
.as_ref()
.is_some_and(|entity| Self::picked_constraint_point(entity, point));
let is_curve = self
.picked_entity
.as_ref()
.is_some_and(Self::valid_curve);
self.picked_entity = None;
match self.step.clone() {
Step::FirstObject if is_curve => {
self.step = Step::PointsOnCurve {
curve: Self::curve_pick(handle, point),
multiple: false,
};
CmdResult::NeedPoint
}
Step::First if is_point => {
self.step = Step::Second(Self::point_pick(Some(handle), point));
CmdResult::NeedPoint
}
Step::First if is_curve => {
self.step = Step::PointsOnCurve {
curve: Self::curve_pick(handle, point),
multiple: false,
};
CmdResult::NeedPoint
}
Step::Second(first) if is_point => CmdResult::AddCoincidentConstraint {
first,
second: Self::point_pick(Some(handle), point),
multiple: false,
label: "Coincident constraint",
},
Step::Second(first) if is_curve => CmdResult::AddCoincidentConstraint {
first,
second: Self::curve_pick(handle, point),
multiple: false,
label: "Coincident constraint",
},
Step::PointsOnCurve { curve, multiple } if is_point => {
CmdResult::AddCoincidentConstraint {
first: curve,
second: Self::point_pick(Some(handle), point),
multiple,
label: "Coincident constraint",
}
}
_ => CmdResult::NeedPoint,
}
}
fn is_selection_gathering(&self) -> bool {
matches!(&self.step, Step::AutoConstrain { .. })
}
fn selection_forces_add(&self) -> bool {
true
}
fn on_selection_complete(&mut self, handles: Vec<Handle>) -> CmdResult {
if let Step::AutoConstrain { handles: selected } = &mut self.step {
*selected = handles;
}
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
if matches!(&self.step, Step::First) {
self.step = Step::FirstObject;
return CmdResult::NeedPoint;
}
if let Step::AutoConstrain { handles } = &self.step {
if !handles.is_empty() {
return CmdResult::AddAutoCoincidentConstraints {
handles: handles.clone(),
};
}
}
CmdResult::Cancel
}
@@ -80,5 +297,5 @@ impl CadCommand for CoincidentConstraintCommand {
// ── Autocomplete registry ─────────────────────────────────
inventory::submit!(crate::command::CommandRegistration {
names: &["CCONSTRAINT"]
names: &["CCONSTRAINT", "GCCOINCIDENT"]
});
+87
View File
@@ -0,0 +1,87 @@
use glam::DVec3;
use crate::command::{CadCommand, CmdOption, CmdResult};
/// Command-line front end for the standard geometric-constraint family.
/// Each choice is handed to the existing focused command so ribbon and typed
/// entry use one implementation.
pub struct GeomConstraintCommand;
impl GeomConstraintCommand {
pub fn new() -> Self {
Self
}
fn dispatch(keyword: &str) -> Option<&'static str> {
Some(match keyword {
"H" | "HORIZONTAL" => "HCONSTRAINT",
"V" | "VERTICAL" => "VCONSTRAINT",
"P" | "PERPENDICULAR" => "QCONSTRAINT",
"PA" | "PARALLEL" => "PCONSTRAINT",
"T" | "TANGENT" => "TCONSTRAINT",
"SM" | "SMOOTH" => "SMOOTHCONSTRAINT",
"C" | "COINCIDENT" => "GCCOINCIDENT",
"CON" | "CONCENTRIC" => "NCONSTRAINT",
"COL" | "COLLINEAR" | "COLINEAR" => "LCONSTRAINT",
"SY" | "SYMMETRIC" => "SYCONSTRAINT",
"E" | "EQUAL" => "ECONSTRAINT",
"F" | "FIX" => "FXCONSTRAINT",
_ => return None,
})
}
}
impl CadCommand for GeomConstraintCommand {
fn name(&self) -> &'static str {
"GEOMCONSTRAINT"
}
fn prompt(&self) -> String {
"GEOMCONSTRAINT Enter constraint type [Horizontal/Vertical/Perpendicular/Parallel/Tangent/Smooth/Coincident/Concentric/Collinear/Symmetric/Equal/Fix] <Coincident>:".to_string()
}
fn options(&self) -> Vec<CmdOption> {
vec![
CmdOption::new("Horizontal", "H"),
CmdOption::new("Vertical", "V"),
CmdOption::new("Perpendicular", "P"),
CmdOption::new("Parallel", "PA"),
CmdOption::new("Tangent", "T"),
CmdOption::new("Smooth", "SM"),
CmdOption::new("Coincident", "C"),
CmdOption::new("Concentric", "CON"),
CmdOption::new("Collinear", "COL"),
CmdOption::new("Symmetric", "SY"),
CmdOption::new("Equal", "E"),
CmdOption::new("Fix", "F"),
]
}
fn wants_text_input(&self) -> bool {
true
}
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
let keyword = text
.trim()
.trim_start_matches('_')
.to_ascii_uppercase();
Self::dispatch(&keyword).map(|command| CmdResult::Dispatch(command.to_string()))
}
fn on_point(&mut self, _point: DVec3) -> CmdResult {
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Dispatch("GCCOINCIDENT".to_string())
}
fn on_escape(&mut self) -> CmdResult {
CmdResult::Cancel
}
}
inventory::submit!(crate::command::CommandRegistration {
names: &["GEOMCONSTRAINT"]
});
+2
View File
@@ -4,11 +4,13 @@
mod coincident;
mod equal_distance;
mod geom_constraint;
mod point_on_entity;
mod tools;
mod value;
pub use coincident::{coincident_tool, CoincidentConstraintCommand};
pub use equal_distance::{equal_distance_tool, EqualDistanceConstraintCommand};
pub use geom_constraint::GeomConstraintCommand;
pub use point_on_entity::{
center_point_tool, midpoint_tool, point_on_curve_tool, PointOnEntityConstraintCommand,
};