forked from 1b-agent/opencadstudio-upstream
fix: align concentric constraint behavior
This commit is contained in:
1 parent
a27545c379
commit
a6a70420c4
15 files changed
+668
-46
No files matched your search
@@ -0,0 +1,158 @@
|
||||
use acadrust::{EntityType, Handle};
|
||||
use glam::DVec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::parametric_constraints::ParametricRef;
|
||||
use crate::t;
|
||||
|
||||
pub struct ConcentricConstraintCommand {
|
||||
first: Option<ParametricRef>,
|
||||
picked_entity: Option<EntityType>,
|
||||
}
|
||||
|
||||
impl ConcentricConstraintCommand {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
first: None,
|
||||
picked_entity: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_first(first: ParametricRef) -> Self {
|
||||
Self {
|
||||
first: Some(first),
|
||||
picked_entity: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn invalid_selection() -> CmdResult {
|
||||
CmdResult::ReportError(
|
||||
t!("Invalid selection for Concentric. Select a circle, arc, ellipse or polyline arc segment.")
|
||||
.into_owned(),
|
||||
)
|
||||
}
|
||||
|
||||
fn polyline_segment_is_arc(entity: &EntityType, index: usize) -> bool {
|
||||
match entity {
|
||||
EntityType::LwPolyline(polyline) => polyline
|
||||
.vertices
|
||||
.get(index)
|
||||
.is_some_and(|vertex| vertex.bulge.abs() > 1.0e-12),
|
||||
EntityType::Polyline2D(polyline) => polyline
|
||||
.vertices
|
||||
.get(index)
|
||||
.is_some_and(|vertex| vertex.bulge.abs() > 1.0e-12),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn picked_reference(entity: &EntityType, handle: Handle, point: DVec3) -> Option<ParametricRef> {
|
||||
match entity {
|
||||
EntityType::Circle(_) | EntityType::Arc(_) | EntityType::Ellipse(_) => {
|
||||
Some(ParametricRef::center(handle))
|
||||
}
|
||||
EntityType::LwPolyline(_) | EntityType::Polyline2D(_) => {
|
||||
let planar = crate::entities::curve::entity_curve(entity)?;
|
||||
let local = planar.plane.project(point.to_array())?;
|
||||
let segments = planar.curve.segments();
|
||||
let (index, segment) = cadkernel::geom2d::nearest_of(segments.iter(), local)?;
|
||||
matches!(segment, cadkernel::geom2d::Curve::Arc(_))
|
||||
.then(|| ParametricRef::segment_center(handle, index))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn preselected_reference(entity: &EntityType, handle: Handle) -> Option<ParametricRef> {
|
||||
match entity {
|
||||
EntityType::Circle(_) | EntityType::Arc(_) | EntityType::Ellipse(_) => {
|
||||
Some(ParametricRef::center(handle))
|
||||
}
|
||||
EntityType::LwPolyline(polyline) => {
|
||||
let segment_count = if polyline.is_closed {
|
||||
polyline.vertices.len()
|
||||
} else {
|
||||
polyline.vertices.len().saturating_sub(1)
|
||||
};
|
||||
(segment_count == 1 && Self::polyline_segment_is_arc(entity, 0))
|
||||
.then(|| ParametricRef::segment_center(handle, 0))
|
||||
}
|
||||
EntityType::Polyline2D(polyline) => {
|
||||
let segment_count = if polyline.is_closed() {
|
||||
polyline.vertices.len()
|
||||
} else {
|
||||
polyline.vertices.len().saturating_sub(1)
|
||||
};
|
||||
(segment_count == 1 && Self::polyline_segment_is_arc(entity, 0))
|
||||
.then(|| ParametricRef::segment_center(handle, 0))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for ConcentricConstraintCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"GCCONCENTRIC"
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
if self.first.is_some() {
|
||||
t!("GCCONCENTRIC Select second object:").into_owned()
|
||||
} else {
|
||||
t!("GCCONCENTRIC Select first object:").into_owned()
|
||||
}
|
||||
}
|
||||
|
||||
fn needs_entity_pick(&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 Some(entity) = self.picked_entity.take() else {
|
||||
return CmdResult::NeedPoint;
|
||||
};
|
||||
let Some(reference) = Self::picked_reference(&entity, handle, point) else {
|
||||
return Self::invalid_selection();
|
||||
};
|
||||
let Some(first) = self.first else {
|
||||
self.first = Some(reference);
|
||||
return CmdResult::NeedPoint;
|
||||
};
|
||||
if first == reference {
|
||||
return Self::invalid_selection();
|
||||
}
|
||||
CmdResult::AddConcentricConstraint {
|
||||
first,
|
||||
second: reference,
|
||||
label: "Concentric constraint",
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, _point: DVec3) -> CmdResult {
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
|
||||
fn on_escape(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ impl GeomConstraintCommand {
|
||||
"T" | "TANGENT" => "TCONSTRAINT",
|
||||
"SM" | "SMOOTH" => "SMOOTHCONSTRAINT",
|
||||
"C" | "COINCIDENT" => "GCCOINCIDENT",
|
||||
"CON" | "CONCENTRIC" => "NCONSTRAINT",
|
||||
"CON" | "CONCENTRIC" => "GCCONCENTRIC",
|
||||
"COL" | "COLLINEAR" | "COLINEAR" => "LCONSTRAINT",
|
||||
"SY" | "SYMMETRIC" => "SYCONSTRAINT",
|
||||
"E" | "EQUAL" => "ECONSTRAINT",
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
//! resulting constraints through the geometry kernel.
|
||||
|
||||
mod coincident;
|
||||
mod concentric;
|
||||
mod equal_distance;
|
||||
mod geom_constraint;
|
||||
#[path = "perpendicular.rs"]
|
||||
@@ -13,6 +14,7 @@ mod tangent_command;
|
||||
mod tools;
|
||||
mod value;
|
||||
pub use coincident::{coincident_tool, CoincidentConstraintCommand};
|
||||
pub use concentric::ConcentricConstraintCommand;
|
||||
pub use equal_distance::{equal_distance_tool, EqualDistanceConstraintCommand};
|
||||
pub use geom_constraint::GeomConstraintCommand;
|
||||
pub use perpendicular_command::{PerpendicularConstraintCommand, PerpendicularPick};
|
||||
@@ -168,7 +170,7 @@ mod tests {
|
||||
groups[0].tools.iter().map(item_id).collect::<Vec<_>>(),
|
||||
[
|
||||
"AUTOCONSTRAIN", "CCONSTRAINT", "PCONSTRAINT", "TCONSTRAINT",
|
||||
"LCONSTRAINT", "QCONSTRAINT", "SMOOTHCONSTRAINT", "NCONSTRAINT",
|
||||
"LCONSTRAINT", "QCONSTRAINT", "SMOOTHCONSTRAINT", "GCCONCENTRIC",
|
||||
"HCONSTRAINT", "SYCONSTRAINT", "FXCONSTRAINT", "VCONSTRAINT",
|
||||
"ECONSTRAINT", "GCVISIBILITY", "GCSHOWALL", "GCHIDEALL",
|
||||
]
|
||||
|
||||
@@ -88,12 +88,12 @@ pub mod concentric {
|
||||
use super::*;
|
||||
pub fn tool() -> ToolDef {
|
||||
ToolDef {
|
||||
id: "NCONSTRAINT",
|
||||
id: "GCCONCENTRIC",
|
||||
label: "Concentric",
|
||||
icon: IconKind::Svg(include_bytes!(
|
||||
"../../../assets/icons/constrain/concentric.svg"
|
||||
)),
|
||||
event: ModuleEvent::Command("NCONSTRAINT".to_string()),
|
||||
event: ModuleEvent::Command("GCCONCENTRIC".to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -168,6 +168,7 @@ inventory::submit!(crate::command::CommandRegistration {
|
||||
"ECONSTRAINT",
|
||||
"TCONSTRAINT",
|
||||
"NCONSTRAINT",
|
||||
"GCCONCENTRIC",
|
||||
"NRCONSTRAINT",
|
||||
"LCONSTRAINT",
|
||||
"FXCONSTRAINT",
|
||||
|
||||
Reference in new issue
Block a user