Bring the Equal constraint to reference parity

Equal was applied through a generic gather-then-apply flow and, with the
default size-retaining solve mode, never changed anything: the solver
held both sizes and the relation could not be met. Now:

- GCEQUAL joins ECONSTRAINT and GEOMCONSTRAINT Equal; both objects are
  picked inside the command (`Select first object or [Multiple]:`,
  `Select second object:`; Multiple gathers `Select objects to make equal
  to the first:`), a selection made beforehand is not used, a miss says
  `No object found.` and re-asks, an unsupported object or the same object
  twice is reported and re-asked, a repeat is refused.
- The follower is resized in place before the relation is added: a line or
  straight polyline segment keeps its start and direction and its end
  moves to the first object's length; a circle or arc keeps its center and
  takes the radius.
- During edits the side that is not being edited follows the same rule:
  its size is not retained, its start (or center) and other vertices are
  held and its direction kept, so only its length or radius changes; a
  chain of Equal relations follows along.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
ramox81andClaude Fable 5.1 committed 2026-09-18 22:56:31 +03:00
1 parent 1f0b888833
commit 8520ee45e8
10 files changed
+598 -5

No files matched your search

+83
View File
@@ -265,6 +265,89 @@ pub(crate) fn set_polyline_vertex(
}
}
/// The size an Equal relation copies.
#[derive(Clone, Copy)]
pub(crate) enum EqualSize {
/// A line's or a straight polyline segment's length.
Length(f64),
/// A circle's or an arc's radius.
Radius(f64),
}
pub(crate) fn equal_size(
document: &acadrust::CadDocument,
reference: ParametricRef,
) -> Option<EqualSize> {
let entity = document.get_entity(reference.entity)?;
match entity {
acadrust::EntityType::Circle(circle) if reference.marker.is_none() => {
Some(EqualSize::Radius(circle.radius))
}
acadrust::EntityType::Arc(arc) if reference.marker.is_none() => {
Some(EqualSize::Radius(arc.radius))
}
acadrust::EntityType::Line(_)
| acadrust::EntityType::LwPolyline(_)
| acadrust::EntityType::Polyline2D(_) => {
let index = reference.segment_index().map_or(0, |index| index as i32);
let start = resolve_point(entity, index)?;
let end = resolve_point(entity, index + 1)?;
let length = (end - start).length();
(length > 1.0e-12).then_some(EqualSize::Length(length))
}
_ => None,
}
}
/// `follower` resized to `first`'s size the way the reference does it: a
/// line or segment keeps its start and direction and only its end moves,
/// a circle or arc keeps its center and takes the radius. `None` when the
/// two do not share a size kind.
pub(crate) fn equal_size_follower(
document: &acadrust::CadDocument,
first: ParametricRef,
follower: ParametricRef,
) -> Option<acadrust::EntityType> {
let size = equal_size(document, first)?;
let original = document.get_entity(follower.entity)?;
let mut entity = original.clone();
match size {
EqualSize::Radius(radius) if follower.marker.is_none() => match &mut entity {
acadrust::EntityType::Circle(circle) => circle.radius = radius,
acadrust::EntityType::Arc(arc) => arc.radius = radius,
_ => return None,
},
EqualSize::Length(length) => {
let index = follower.segment_index().map_or(0, |index| index as i32);
let start = resolve_point(original, index)?;
let end = resolve_point(original, index + 1)?;
let current = (end - start).length();
if current <= 1.0e-12 {
return None;
}
let scale = length / current;
let x = start.x + (end.x - start.x) * scale;
let y = start.y + (end.y - start.y) * scale;
if matches!(
original,
acadrust::EntityType::LwPolyline(_) | acadrust::EntityType::Polyline2D(_)
) {
if !set_polyline_vertex(&mut entity, index as usize + 1, x, y) {
return None;
}
} else if let (acadrust::EntityType::Line(line), None) = (&mut entity, follower.marker)
{
line.end.x = x;
line.end.y = y;
} else {
return None;
}
}
_ => return None,
}
Some(entity)
}
/// Grabbed points are exact kernel inputs; the solver anchors the remaining
/// endpoint coordinates according to the line's directional constraints.
pub(crate) fn grip_solve_anchor_refs(
+82
View File
@@ -2450,6 +2450,87 @@ fn solve_scope(
}
}
}
// Equal: the side that is not being edited follows the other's length
// or radius the way the reference does it — its start (a circle its
// center) and direction stay and only its end moves — so its size is
// not retained below, its other points are held and its direction is
// kept while the edit lasts. A chain of Equal relations follows along.
let edited = |handle: Handle| {
driven_refs.iter().any(|reference| reference.entity == handle)
|| initial_fixed_refs.iter().any(|reference| reference.entity == handle)
};
let mut equal_followers: Vec<ParametricRef> = Vec::new();
let mut moving: Vec<Handle> = cache.keys().copied().filter(|handle| edited(*handle)).collect();
loop {
let before = equal_followers.len();
for constraint in &constraints {
if !constraint.enabled || constraint.kind != ConstraintKind::Equal {
continue;
}
let [a, b] = constraint.refs.as_slice() else { continue };
let follower = match (moving.contains(&a.entity), moving.contains(&b.entity)) {
(true, false) => *b,
(false, true) => *a,
_ => continue,
};
if !equal_followers.contains(&follower) {
equal_followers.push(follower);
moving.push(follower.entity);
}
}
if equal_followers.len() == before {
break;
}
}
for follower in &equal_followers {
let Some(geometry) = resolve_ref(document, &mut sys, &mut cache, *follower) else {
continue;
};
let mut hold = |anchor: ParametricRef| {
if !anchors.contains(&anchor) {
anchors.push(anchor);
}
};
let line = match &geometry {
EntityGeom::Line(line) => Some(*line),
EntityGeom::Polyline { .. } => follower
.segment_index()
.and_then(|index| geometry.line_segment(index)),
_ => None,
};
match &geometry {
EntityGeom::Circle(_) | EntityGeom::Arc(_) => {
hold(ParametricRef::center(follower.entity));
}
EntityGeom::Line(_) => hold(ParametricRef::point(follower.entity, 0)),
EntityGeom::Polyline { points, .. } => {
let moving_vertex = follower
.segment_index()
.map(|index| (index + 1) % points.len().max(1));
for index in (0..points.len()).filter(|index| Some(*index) != moving_vertex) {
hold(ParametricRef::point(follower.entity, index as i32));
}
}
_ => {}
}
if let Some(line) = line {
let (dx, dy) = {
let store = sys.store();
(
store.get(line.p2.x) - store.get(line.p1.x),
store.get(line.p2.y) - store.get(line.p1.y),
)
};
if dx.hypot(dy) > f64::EPSILON {
let datum = GLine {
p1: GPoint::new(sys.add_param(0.0, true), sys.add_param(0.0, true)),
p2: GPoint::new(sys.add_param(dx, true), sys.add_param(dy, true)),
};
let parallel = ParallelConstraint::new(sys.store(), line, datum);
sys.add_constraint(Rc::new(parallel));
}
}
}
if !retain_lengths {
let mut axis_lines: Vec<_> = line_axes.into_iter().filter_map(|(reference, vertical)| {
let geometry = cache.get(&reference.entity)?;
@@ -2583,6 +2664,7 @@ fn solve_scope(
|| symmetric_constraints.iter().any(|constraint| {
constraint.refs.get(2).is_some_and(|axis| axis.entity == *handle)
})
|| equal_followers.iter().any(|follower| follower.entity == *handle)
{
continue;
}