forked from 1b-agent/opencadstudio-upstream
Match Smooth connected-endpoint workflow
This commit is contained in:
1 parent
7c6665153c
commit
dfa75f45b5
4 files changed
+221
-161
No files matched your search
@@ -5,21 +5,13 @@ 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,
|
||||
}
|
||||
}
|
||||
@@ -53,106 +45,8 @@ impl SmoothConstraintCommand {
|
||||
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(),
|
||||
)
|
||||
fn invalid_selection() -> CmdResult {
|
||||
CmdResult::ReportError(t!("No valid constraint point found.").into_owned())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,11 +56,7 @@ impl CadCommand for SmoothConstraintCommand {
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
t!("GCSMOOTH Select first spline curve:").into_owned()
|
||||
}
|
||||
|
||||
fn needs_entity_pick(&self) -> bool {
|
||||
@@ -192,26 +82,14 @@ impl CadCommand for SmoothConstraintCommand {
|
||||
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(source) = Self::source_reference(&entity, handle, point) else {
|
||||
return Self::invalid_selection();
|
||||
};
|
||||
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,
|
||||
// The host resolves the target from an existing Coincident
|
||||
// relation. GCSMOOTH itself accepts only the spline selection.
|
||||
refs: vec![source],
|
||||
driving_param: None,
|
||||
label: "Smooth constraint",
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user