forked from 1b-agent/opencadstudio-upstream
Merge pull request #1331 from ramox81/codex/smooth-constraint-parity
# Conflicts: # src/app/commands/styleprops.rs
This commit is contained in:
commit
76451f7298
18 files changed
+762
-127
No files matched your search
@@ -19,7 +19,7 @@ impl GeomConstraintCommand {
|
||||
"P" | "PERPENDICULAR" => "GCPERPENDICULAR",
|
||||
"PA" | "PARALLEL" => "PCONSTRAINT",
|
||||
"T" | "TANGENT" => "TCONSTRAINT",
|
||||
"SM" | "SMOOTH" => "SMOOTHCONSTRAINT",
|
||||
"SM" | "SMOOTH" => "GCSMOOTH",
|
||||
"C" | "COINCIDENT" => "GCCOINCIDENT",
|
||||
"CON" | "CONCENTRIC" => "NCONSTRAINT",
|
||||
"COL" | "COLLINEAR" | "COLINEAR" => "LCONSTRAINT",
|
||||
|
||||
@@ -8,6 +8,7 @@ mod geom_constraint;
|
||||
#[path = "perpendicular.rs"]
|
||||
mod perpendicular_command;
|
||||
mod point_on_entity;
|
||||
mod smooth;
|
||||
#[path = "tangent.rs"]
|
||||
mod tangent_command;
|
||||
mod tools;
|
||||
@@ -19,6 +20,7 @@ pub use perpendicular_command::{PerpendicularConstraintCommand, PerpendicularPic
|
||||
pub use point_on_entity::{
|
||||
center_point_tool, midpoint_tool, point_on_curve_tool, PointOnEntityConstraintCommand,
|
||||
};
|
||||
pub use smooth::SmoothConstraintCommand;
|
||||
pub use tangent_command::TangentConstraintCommand;
|
||||
pub use tools::{
|
||||
colinear, concentric, equal, fixed, horizontal, normal, parallel, perpendicular, symmetric,
|
||||
@@ -62,7 +64,7 @@ impl CadModule for ParametricModule {
|
||||
RibbonItem::LargeTool(colinear::tool()),
|
||||
RibbonItem::LargeTool(perpendicular::tool()),
|
||||
RibbonItem::LargeTool(command(
|
||||
"SMOOTHCONSTRAINT", "Smooth",
|
||||
"GCSMOOTH", "Smooth",
|
||||
include_bytes!("../../../assets/icons/constrain/smooth.svg"),
|
||||
)),
|
||||
RibbonItem::LargeTool(concentric::tool()),
|
||||
@@ -136,7 +138,7 @@ impl CadModule for ParametricModule {
|
||||
|
||||
inventory::submit!(crate::command::CommandRegistration {
|
||||
names: &[
|
||||
"AUTOCONSTRAIN", "CONSTRAINTSETTINGS", "SMOOTHCONSTRAINT", "GCSHOW", "GCHIDE", "GCRESET",
|
||||
"AUTOCONSTRAIN", "CONSTRAINTSETTINGS", "GCSMOOTH", "SMOOTHCONSTRAINT", "GCSHOW", "GCHIDE", "GCRESET",
|
||||
"GCSHOWALL", "GCHIDEALL", "DCSHOW", "DCHIDE", "DCSHOWALL", "DCHIDEALL",
|
||||
"DCCONVERT", "DELCONSTRAINT",
|
||||
]
|
||||
@@ -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", "GCSMOOTH", "NCONSTRAINT",
|
||||
"HCONSTRAINT", "SYCONSTRAINT", "FXCONSTRAINT", "VCONSTRAINT",
|
||||
"ECONSTRAINT", "GCVISIBILITY", "GCSHOWALL", "GCHIDEALL",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,231 @@
|
||||
use acadrust::{EntityType, Handle};
|
||||
use glam::DVec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::parametric_constraints::{ConstraintKind, ParametricRef};
|
||||
use crate::t;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct SmoothTarget {
|
||||
endpoint: ParametricRef,
|
||||
curve: ParametricRef,
|
||||
}
|
||||
|
||||
pub struct SmoothConstraintCommand {
|
||||
source: Option<ParametricRef>,
|
||||
picked_entity: Option<EntityType>,
|
||||
}
|
||||
|
||||
impl SmoothConstraintCommand {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
source: None,
|
||||
picked_entity: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn point(point: acadrust::types::Vector3) -> DVec3 {
|
||||
DVec3::new(point.x, point.y, point.z)
|
||||
}
|
||||
|
||||
fn nearest_marker(points: &[acadrust::types::Vector3], point: DVec3) -> Option<i32> {
|
||||
points
|
||||
.iter()
|
||||
.enumerate()
|
||||
.min_by(|(_, left), (_, right)| {
|
||||
(Self::point(**left) - point)
|
||||
.length_squared()
|
||||
.total_cmp(&(Self::point(**right) - point).length_squared())
|
||||
})
|
||||
.map(|(index, _)| index as i32)
|
||||
}
|
||||
|
||||
fn source_reference(entity: &EntityType, handle: Handle, point: DVec3) -> Option<ParametricRef> {
|
||||
let EntityType::Spline(spline) = entity else {
|
||||
return None;
|
||||
};
|
||||
if spline.flags.closed || spline.flags.periodic {
|
||||
return None;
|
||||
}
|
||||
let points = crate::scene::dimension_assoc::source_points(entity);
|
||||
let endpoints = [*points.first()?, *points.last()?];
|
||||
let marker = Self::nearest_marker(&endpoints, point)?;
|
||||
Some(ParametricRef::point(handle, marker))
|
||||
}
|
||||
|
||||
fn target_reference(entity: &EntityType, handle: Handle, point: DVec3) -> Option<SmoothTarget> {
|
||||
match entity {
|
||||
EntityType::Line(_) | EntityType::Arc(_) => {
|
||||
let points = crate::scene::dimension_assoc::source_points(entity);
|
||||
let endpoints = [*points.first()?, *points.last()?];
|
||||
let marker = Self::nearest_marker(&endpoints, point)?;
|
||||
Some(SmoothTarget {
|
||||
endpoint: ParametricRef::point(handle, marker),
|
||||
curve: ParametricRef::whole(handle),
|
||||
})
|
||||
}
|
||||
EntityType::Spline(spline) if !spline.flags.closed && !spline.flags.periodic => {
|
||||
let points = crate::scene::dimension_assoc::source_points(entity);
|
||||
let endpoints = [*points.first()?, *points.last()?];
|
||||
let marker = Self::nearest_marker(&endpoints, point)?;
|
||||
Some(SmoothTarget {
|
||||
endpoint: ParametricRef::point(handle, marker),
|
||||
curve: ParametricRef::whole(handle),
|
||||
})
|
||||
}
|
||||
EntityType::LwPolyline(_) | EntityType::Polyline2D(_) => {
|
||||
let (source, _, _) = crate::scene::centerline::picked_source(entity, handle, point)?;
|
||||
let segment = usize::try_from(source.segment_index).ok()?;
|
||||
let points = crate::scene::dimension_assoc::source_points(entity);
|
||||
let closed = match entity {
|
||||
EntityType::LwPolyline(polyline) => polyline.is_closed,
|
||||
EntityType::Polyline2D(polyline) => polyline.is_closed(),
|
||||
_ => false,
|
||||
};
|
||||
let end = if segment + 1 < points.len() {
|
||||
segment + 1
|
||||
} else if closed {
|
||||
0
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
let candidates = [points.get(segment).copied()?, points.get(end).copied()?];
|
||||
let side = Self::nearest_marker(&candidates, point)? as usize;
|
||||
let vertex = if side == 0 { segment } else { end };
|
||||
Some(SmoothTarget {
|
||||
endpoint: ParametricRef::point(handle, vertex as i32),
|
||||
curve: ParametricRef::segment(handle, segment),
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn preselected_refs(
|
||||
first: (&EntityType, Handle),
|
||||
second: (&EntityType, Handle),
|
||||
) -> Option<Vec<ParametricRef>> {
|
||||
let source_points = crate::scene::dimension_assoc::source_points(first.0);
|
||||
let source_ends = [*source_points.first()?, *source_points.last()?];
|
||||
let source_point = source_ends
|
||||
.iter()
|
||||
.copied()
|
||||
.min_by(|left, right| {
|
||||
let target_points = crate::scene::dimension_assoc::source_points(second.0);
|
||||
let distance = |source: acadrust::types::Vector3| {
|
||||
target_points
|
||||
.iter()
|
||||
.map(|target| (source - *target).length_squared())
|
||||
.fold(f64::INFINITY, f64::min)
|
||||
};
|
||||
distance(*left).total_cmp(&distance(*right))
|
||||
})?;
|
||||
let source = Self::source_reference(first.0, first.1, Self::point(source_point))?;
|
||||
|
||||
let target_points = crate::scene::dimension_assoc::source_points(second.0);
|
||||
let target_point = target_points
|
||||
.iter()
|
||||
.copied()
|
||||
.min_by(|left, right| {
|
||||
(Self::point(*left) - Self::point(source_point))
|
||||
.length_squared()
|
||||
.total_cmp(
|
||||
&(Self::point(*right) - Self::point(source_point)).length_squared(),
|
||||
)
|
||||
})?;
|
||||
let target = Self::target_reference(second.0, second.1, Self::point(target_point))?;
|
||||
let mut refs = vec![source, target.endpoint];
|
||||
if target.curve.marker.is_some() {
|
||||
refs.push(target.curve);
|
||||
}
|
||||
Some(refs)
|
||||
}
|
||||
|
||||
fn invalid_source() -> CmdResult {
|
||||
CmdResult::ReportError(
|
||||
t!("Invalid first selection for Smooth. Select an endpoint of an open spline.")
|
||||
.into_owned(),
|
||||
)
|
||||
}
|
||||
|
||||
fn invalid_target() -> CmdResult {
|
||||
CmdResult::ReportError(
|
||||
t!("Invalid second selection for Smooth. Select an endpoint of a line, arc, polyline segment or open spline.")
|
||||
.into_owned(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for SmoothConstraintCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"GCSMOOTH"
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
if self.source.is_some() {
|
||||
t!("GCSMOOTH Select second curve endpoint:").into_owned()
|
||||
} else {
|
||||
t!("GCSMOOTH Select first spline curve endpoint:").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(source) = self.source else {
|
||||
let Some(source) = Self::source_reference(&entity, handle, point) else {
|
||||
return Self::invalid_source();
|
||||
};
|
||||
self.source = Some(source);
|
||||
return CmdResult::NeedPoint;
|
||||
};
|
||||
let Some(target) = Self::target_reference(&entity, handle, point) else {
|
||||
return Self::invalid_target();
|
||||
};
|
||||
if source.entity == target.endpoint.entity {
|
||||
return Self::invalid_target();
|
||||
}
|
||||
let mut refs = vec![source, target.endpoint];
|
||||
if target.curve.marker.is_some() {
|
||||
refs.push(target.curve);
|
||||
}
|
||||
CmdResult::AddParametricConstraint {
|
||||
kind: ConstraintKind::Smooth,
|
||||
refs,
|
||||
driving_param: None,
|
||||
label: "Smooth 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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user