Add 8 geometric constraint kinds and an opt-in DWG-native persistence toggle

New ConstraintKind variants — Concentric, CenterPoint, Colinear, Midpoint,
Fixed, PointOnCurve, EqualDistance, Symmetric — each built entirely from
existing ocs_gcs (FreeCAD planegcs) primitives, no new solver math needed.
build_constraint now returns Vec<Rc<dyn Constraint>> instead of
Option<Rc<...>>, generalizing the old Coincident-only "needs a second
system-level constraint" special case to every kind that needs one.
Wired into the ribbon (7 new buttons), a new point+pre-selected-entity
command for CenterPoint/Midpoint/PointOnCurve, a new 4-point command for
EqualDistance, and the existing DWG-native class-name mapping (all 8 reuse
the same generic "plain Geometrical node" path already built for
Coincident/Horizontal/etc.). Deliberately not modeled: Normal and
EqualCurvature, which for this app's Line/Circle-only entity model would
just be Perpendicular and Equal wearing different DWG class names.

Also makes DWG-native constraint persistence opt-in (Options > General,
off by default) rather than unconditional: a scope that doesn't yet have
the native graph is only given one when the setting is on, but a scope
that already has one — from an earlier save, or a file that came from
real AutoCAD — keeps getting it resynced regardless of the setting, so
toggling it off never freezes a stale graph in place.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
Felix RiestraandClaude Sonnet 5 committed 2026-09-09 19:38:31 +02:00
1 parent cf33a1d019
commit 31b5ef4ec3
18 files changed
+1076 -106

No files matched your search

@@ -0,0 +1,67 @@
//! EDCONSTRAINT — AutoCAD's fourth `Equal` sub-kind
//! (`ACEQUALDISTANCECONSTRAINT`): the distance between one point pair
//! equals the distance between another, rather than two whole entities
//! being equal length/radius. Needs four point picks (unlike the two
//! whole-entity selections `ECONSTRAINT` uses), so — like
//! `CoincidentConstraintCommand` — it accumulates raw points and hands them
//! back for the host to resolve (`CadCommand` has no document access).
use glam::DVec3;
use crate::command::{CadCommand, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
pub mod equal_distance_tool {
use super::*;
pub fn tool() -> ToolDef {
ToolDef {
id: "EDCONSTRAINT",
label: "Equal Distance",
icon: IconKind::Glyph("≐"),
event: ModuleEvent::Command("EDCONSTRAINT".to_string()),
}
}
}
#[derive(Default)]
pub struct EqualDistanceConstraintCommand {
points: Vec<DVec3>,
}
impl EqualDistanceConstraintCommand {
pub fn new() -> Self {
Self::default()
}
}
impl CadCommand for EqualDistanceConstraintCommand {
fn name(&self) -> &'static str {
"EDCONSTRAINT"
}
fn prompt(&self) -> String {
match self.points.len() {
0 => "EQUAL DISTANCE Specify first point of first pair:".to_string(),
1 => "EQUAL DISTANCE Specify second point of first pair:".to_string(),
2 => "EQUAL DISTANCE Specify first point of second pair:".to_string(),
_ => "EQUAL DISTANCE Specify second point of second pair:".to_string(),
}
}
fn on_point(&mut self, pt: DVec3) -> CmdResult {
self.points.push(pt);
if self.points.len() < 4 {
return CmdResult::NeedPoint;
}
let [a, b, c, d] = self.points[..] else { unreachable!("just checked len == 4") };
CmdResult::AddEqualDistanceConstraint { points: [a, b, c, d], label: "Equal distance constraint" }
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_escape(&mut self) -> CmdResult {
CmdResult::Cancel
}
}
+26 -11
View File
@@ -1,21 +1,36 @@
//! Ribbon tools and typed-value commands for the persistent "Constraints"
//! group (design doc `docs/parametric_system_design.md` §6.1).
//!
//! All eight — Horizontal, Vertical, Parallel, Perpendicular, Equal,
//! Tangent, Distance, Angle — add a [`crate::scene::sketch_constraints::SketchConstraint`]
//! to the current scope and let `Scene::bump_entities` solve it, the same
//! Every one adds a [`crate::scene::sketch_constraints::SketchConstraint`]
//! to the current scope and lets `Scene::bump_entities` solve it, the same
//! path any later edit to the constrained entities re-solves through
//! (`src/scene/sketch_solve.rs`). Horizontal/Vertical/Parallel/Perpendicular/
//! Equal/Tangent dispatch straight to `CmdResult::AddSketchConstraint` from
//! `src/app/commands/draw.rs` (no typed value needed, so no `CadCommand` of
//! their own); Distance and Angle need one (`value.rs`), since the target
//! value comes from the command line after the tool is clicked; Coincident
//! needs one too (`coincident.rs`), since it addresses a *point* on an
//! entity rather than the whole thing, so it can't reuse plain selection.
//! (`src/scene/sketch_solve.rs`). By how they gather input:
//! - **Plain selection, no picking** — Horizontal, Vertical, Fixed (select
//! one entity); Parallel, Perpendicular, Equal, Tangent, Colinear,
//! Concentric (select two); Symmetric (select three: the mirrored pair,
//! then the mirror line) — dispatch straight to
//! `CmdResult::AddSketchConstraint` from `src/app/commands/draw.rs`.
//! - **A typed value** — Distance, Angle (`value.rs`): the target value
//! comes from the command line after the tool is clicked.
//! - **A picked point, entity pre-selected** — CenterPoint, Midpoint,
//! PointOnCurve (`point_on_entity.rs`): the entity side has no ambiguity
//! (select it first, like Distance/Angle's target), but the point side
//! does (which of a line's two endpoints?), so it's picked interactively
//! and resolved by the host.
//! - **Picked points only, nothing pre-selected** — Coincident
//! (`coincident.rs`, two points), EqualDistance (`equal_distance.rs`,
//! four points): every side is a point, so plain selection can't express
//! it at all.
mod coincident;
mod equal_distance;
mod point_on_entity;
mod tools;
mod value;
pub use coincident::{coincident_tool, CoincidentConstraintCommand};
pub use tools::{equal, horizontal, parallel, perpendicular, tangent, vertical};
pub use equal_distance::{equal_distance_tool, EqualDistanceConstraintCommand};
pub use point_on_entity::{center_point_tool, midpoint_tool, point_on_curve_tool, PointOnEntityConstraintCommand};
pub use tools::{
concentric, equal, fixed, horizontal, colinear, parallel, perpendicular, symmetric, tangent, vertical,
};
pub use value::{angle_tool, distance_tool, AngleConstraintCommand, DistanceConstraintCommand};
@@ -0,0 +1,91 @@
//! CENTERPOINT / MIDPOINT / ONCURVE — constraints between one picked point
//! and a whole entity selected *before* the tool runs (same "select first,
//! then invoke" convention `DistanceConstraintCommand`
//! (`src/modules/draw/constrain/value.rs`) already uses for its own target
//! entity). The entity side has no ambiguity (a whole circle, or a whole
//! line whose midpoint/curve is being addressed, not one specific
//! endpoint) so it doesn't need interactive picking; the point side does
//! (which of a line's two endpoints, or some other addressable point), so
//! this command accumulates just that one point pick and hands it back for
//! the host to resolve via `nearest_sketch_point` — the same split
//! `CoincidentConstraintCommand` uses and for the same reason (`CadCommand`
//! has no document access).
use glam::DVec3;
use crate::command::{CadCommand, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::sketch_constraints::ConstraintKind;
use acadrust::types::Handle;
pub mod center_point_tool {
use super::*;
pub fn tool() -> ToolDef {
ToolDef {
id: "CPCONSTRAINT",
label: "Center Point",
icon: IconKind::Glyph("⊕"),
event: ModuleEvent::Command("CPCONSTRAINT".to_string()),
}
}
}
pub mod midpoint_tool {
use super::*;
pub fn tool() -> ToolDef {
ToolDef {
id: "MPCONSTRAINT",
label: "Midpoint",
icon: IconKind::Glyph("M"),
event: ModuleEvent::Command("MPCONSTRAINT".to_string()),
}
}
}
pub mod point_on_curve_tool {
use super::*;
pub fn tool() -> ToolDef {
ToolDef {
id: "OCCONSTRAINT",
label: "Point on Curve",
icon: IconKind::Glyph("∈"),
event: ModuleEvent::Command("OCCONSTRAINT".to_string()),
}
}
}
/// Shared by all three tools above — only `name`/`kind`/`label` differ.
pub struct PointOnEntityConstraintCommand {
name: &'static str,
kind: ConstraintKind,
target: Handle,
label: &'static str,
}
impl PointOnEntityConstraintCommand {
pub fn new(name: &'static str, kind: ConstraintKind, target: Handle, label: &'static str) -> Self {
Self { name, kind, target, label }
}
}
impl CadCommand for PointOnEntityConstraintCommand {
fn name(&self) -> &'static str {
self.name
}
fn prompt(&self) -> String {
"Specify point:".to_string()
}
fn on_point(&mut self, pt: DVec3) -> CmdResult {
CmdResult::AddPointOnEntityConstraint { point: pt, target: self.target, kind: self.kind, label: self.label }
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_escape(&mut self) -> CmdResult {
CmdResult::Cancel
}
}
+48
View File
@@ -73,3 +73,51 @@ pub mod tangent {
}
}
}
pub mod concentric {
use super::*;
pub fn tool() -> ToolDef {
ToolDef {
id: "NCONSTRAINT",
label: "Concentric",
icon: IconKind::Glyph("◎"),
event: ModuleEvent::Command("NCONSTRAINT".to_string()),
}
}
}
pub mod colinear {
use super::*;
pub fn tool() -> ToolDef {
ToolDef {
id: "LCONSTRAINT",
label: "Colinear",
icon: IconKind::Glyph("L"),
event: ModuleEvent::Command("LCONSTRAINT".to_string()),
}
}
}
pub mod fixed {
use super::*;
pub fn tool() -> ToolDef {
ToolDef {
id: "FXCONSTRAINT",
label: "Fixed",
icon: IconKind::Glyph("F"),
event: ModuleEvent::Command("FXCONSTRAINT".to_string()),
}
}
}
pub mod symmetric {
use super::*;
pub fn tool() -> ToolDef {
ToolDef {
id: "SYCONSTRAINT",
label: "Symmetric",
icon: IconKind::Glyph("S"),
event: ModuleEvent::Command("SYCONSTRAINT".to_string()),
}
}
}
+13 -1
View File
@@ -34,7 +34,11 @@ impl CadModule for DrawModule {
};
use crate::modules::insert::{create_block, insert_block};
use clipboard::{copy_clip, cut, paste};
use constrain::{angle_tool, coincident_tool, distance_tool, equal, horizontal, parallel, perpendicular, tangent, vertical};
use constrain::{
angle_tool, center_point_tool, coincident_tool, colinear, concentric, distance_tool, equal,
equal_distance_tool, fixed, horizontal, midpoint_tool, parallel, perpendicular, point_on_curve_tool,
symmetric, tangent, vertical,
};
use draw::{arc, circle, ellipse, hatch, line, polyline, shapes};
use groups::{group, ungroup};
use layers::{
@@ -134,6 +138,14 @@ impl CadModule for DrawModule {
coincident_tool::tool().into(),
distance_tool::tool().into(),
angle_tool::tool().into(),
concentric::tool().into(),
colinear::tool().into(),
fixed::tool().into(),
symmetric::tool().into(),
center_point_tool::tool().into(),
midpoint_tool::tool().into(),
point_on_curve_tool::tool().into(),
equal_distance_tool::tool().into(),
],
},
RibbonGroup {