forked from 1b-agent/opencadstudio-upstream
Complete Coincident command behavior
This commit is contained in:
1 parent
dfe8b77cca
commit
ba2b371269
13 files changed
+1183
-207
No files matched your search
Vendored
+1
-10
@@ -32,7 +32,7 @@ pub fn general_section(entity: &EntityType) -> PropSection {
|
||||
|
||||
let hyperlink = crate::scene::pe_url_of(entity).unwrap_or_default().to_owned();
|
||||
|
||||
let mut section = PropSection {
|
||||
let section = PropSection {
|
||||
title: t!("General").into_owned(),
|
||||
props: vec![
|
||||
Property {
|
||||
@@ -88,15 +88,6 @@ pub fn general_section(entity: &EntityType) -> PropSection {
|
||||
],
|
||||
};
|
||||
|
||||
// Thickness (DXF 39) is a General-group property, but only the entity
|
||||
// types that carry an extrusion thickness expose it (line, circle, arc,
|
||||
// polyline, text, 2D solid, …). Show it right after Hyperlink for those.
|
||||
if let Some(t) = crate::scene::view::dispatch::entity_thickness(entity) {
|
||||
section
|
||||
.props
|
||||
.push(crate::entities::common::edit_prop(t!("Thickness").as_ref(), "thickness", t));
|
||||
}
|
||||
|
||||
section
|
||||
}
|
||||
|
||||
|
||||
+9
-1
@@ -2836,6 +2836,14 @@ impl Scene {
|
||||
}
|
||||
|
||||
pub fn bump_entities(&mut self, changes: &[(Handle, ChangeKind)]) {
|
||||
self.bump_entities_with_parametric_driven(changes, &[]);
|
||||
}
|
||||
|
||||
pub fn bump_entities_with_parametric_driven(
|
||||
&mut self,
|
||||
changes: &[(Handle, ChangeKind)],
|
||||
driven_refs: &[parametric_constraints::ParametricRef],
|
||||
) {
|
||||
if changes.iter().any(|(handle, kind)| {
|
||||
matches!(kind, ChangeKind::Removed)
|
||||
|| self
|
||||
@@ -2869,7 +2877,7 @@ impl Scene {
|
||||
}
|
||||
}
|
||||
if !self.parametric_constraints.is_empty() {
|
||||
for change in self.refresh_parametric_constraints(&changes) {
|
||||
for change in self.refresh_parametric_constraints_with_driven(&changes, driven_refs) {
|
||||
if !changes.iter().any(|(handle, _)| *handle == change.0) {
|
||||
changes.push(change);
|
||||
}
|
||||
|
||||
@@ -361,6 +361,7 @@ pub(crate) fn resolve_point(entity: &acadrust::EntityType, marker: i32) -> Optio
|
||||
return match entity {
|
||||
acadrust::EntityType::Circle(circle) => Some(circle.center_wcs()),
|
||||
acadrust::EntityType::Arc(arc) => Some(arc.center_wcs()),
|
||||
acadrust::EntityType::Ellipse(ellipse) => Some(ellipse.center),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
@@ -392,6 +393,23 @@ pub(crate) fn resolve_point(entity: &acadrust::EntityType, marker: i32) -> Optio
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
let bulge = match entity {
|
||||
acadrust::EntityType::LwPolyline(polyline) => {
|
||||
polyline.vertices.get(segment)?.bulge
|
||||
}
|
||||
acadrust::EntityType::Polyline2D(polyline) => {
|
||||
polyline.vertices.get(segment)?.bulge
|
||||
}
|
||||
_ => 0.0,
|
||||
};
|
||||
if let Some(arc) = cadkernel::geom2d::BulgeArc::from_bulge(
|
||||
[a.x, a.y],
|
||||
[b.x, b.y],
|
||||
bulge,
|
||||
) {
|
||||
let midpoint = arc.sample(0.5);
|
||||
return Some(Vector3::new(midpoint[0], midpoint[1], (a.z + b.z) * 0.5));
|
||||
}
|
||||
return Some((a + b) * 0.5);
|
||||
}
|
||||
if marker < 0 {
|
||||
@@ -406,6 +424,97 @@ pub(crate) fn resolve_point(entity: &acadrust::EntityType, marker: i32) -> Optio
|
||||
/// already coincident for [`nearest_parametric_point`]'s purposes.
|
||||
const COINCIDENT_EPSILON_SQ: f64 = 1.0e-12;
|
||||
|
||||
/// Addressable constraint points for one entity, in the same marker space
|
||||
/// used by persistent constraint references.
|
||||
pub(crate) fn parametric_point_candidates(
|
||||
entity: &acadrust::EntityType,
|
||||
) -> Vec<(i32, Vector3)> {
|
||||
let mut points: Vec<_> = super::dimension_assoc::source_points(entity)
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(marker, point)| (marker as i32, point))
|
||||
.collect();
|
||||
match entity {
|
||||
acadrust::EntityType::Circle(circle) => points.push((-3, circle.center_wcs())),
|
||||
acadrust::EntityType::Arc(arc) => points.push((-3, arc.center_wcs())),
|
||||
acadrust::EntityType::Ellipse(ellipse) => points.push((-3, ellipse.center)),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if matches!(
|
||||
entity,
|
||||
acadrust::EntityType::Line(_)
|
||||
| acadrust::EntityType::Arc(_)
|
||||
| acadrust::EntityType::Spline(_)
|
||||
| acadrust::EntityType::Ellipse(_)
|
||||
) {
|
||||
if let Some(curve) = crate::entities::curve::entity_curve(entity) {
|
||||
if !curve.is_closed() {
|
||||
let point = curve.point_at(0.5);
|
||||
points.push((-2, Vector3::new(point[0], point[1], point[2])));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let polyline_midpoints = |vertices: &[Vector3], bulges: &[f64], closed: bool| {
|
||||
let mut result = Vec::new();
|
||||
for (index, bulge) in bulges.iter().copied().enumerate() {
|
||||
let Some(a) = vertices.get(index).copied() else {
|
||||
continue;
|
||||
};
|
||||
let b = vertices
|
||||
.get(index + 1)
|
||||
.copied()
|
||||
.or_else(|| closed.then(|| vertices.first().copied()).flatten());
|
||||
if let Some(b) = b {
|
||||
let midpoint = cadkernel::geom2d::BulgeArc::from_bulge(
|
||||
[a.x, a.y],
|
||||
[b.x, b.y],
|
||||
bulge,
|
||||
)
|
||||
.map(|arc| {
|
||||
let point = arc.sample(0.5);
|
||||
Vector3::new(point[0], point[1], (a.z + b.z) * 0.5)
|
||||
})
|
||||
.unwrap_or((a + b) * 0.5);
|
||||
result.push((
|
||||
POLYLINE_SEGMENT_MIDPOINT_MARKER_BASE - index as i32,
|
||||
midpoint,
|
||||
));
|
||||
}
|
||||
}
|
||||
result
|
||||
};
|
||||
match entity {
|
||||
acadrust::EntityType::LwPolyline(polyline) => {
|
||||
let vertices = super::dimension_assoc::source_points(entity);
|
||||
let bulges: Vec<_> = polyline
|
||||
.vertices
|
||||
.iter()
|
||||
.map(|vertex| vertex.bulge)
|
||||
.collect();
|
||||
points.extend(polyline_midpoints(&vertices, &bulges, polyline.is_closed));
|
||||
}
|
||||
acadrust::EntityType::Polyline2D(polyline) => {
|
||||
let vertices = super::dimension_assoc::source_points(entity);
|
||||
let bulges: Vec<_> = polyline
|
||||
.vertices
|
||||
.iter()
|
||||
.map(|vertex| vertex.bulge)
|
||||
.collect();
|
||||
points.extend(polyline_midpoints(&vertices, &bulges, polyline.is_closed()));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
points
|
||||
}
|
||||
|
||||
pub(crate) fn is_parametric_point_near(entity: &acadrust::EntityType, point: Vector3) -> bool {
|
||||
parametric_point_candidates(entity)
|
||||
.into_iter()
|
||||
.any(|(_, candidate)| (candidate - point).length_squared() <= COINCIDENT_EPSILON_SQ)
|
||||
}
|
||||
|
||||
/// Finds the addressable entity point nearest a snapped world position.
|
||||
/// Returns `None` when no point in the scope is within the coincidence
|
||||
/// tolerance.
|
||||
@@ -431,21 +540,96 @@ pub(crate) fn nearest_parametric_point(
|
||||
if common.owner_handle != owner || Some(common.handle) == exclude {
|
||||
continue;
|
||||
}
|
||||
for (marker, point) in super::dimension_assoc::source_points(candidate)
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
consider(common.handle, marker as i32, point);
|
||||
}
|
||||
match candidate {
|
||||
acadrust::EntityType::Circle(c) => consider(common.handle, -3, c.center_wcs()),
|
||||
acadrust::EntityType::Arc(a) => consider(common.handle, -3, a.center_wcs()),
|
||||
_ => {}
|
||||
for (marker, point) in parametric_point_candidates(candidate) {
|
||||
consider(common.handle, marker, point);
|
||||
}
|
||||
}
|
||||
best.map(|(_, r)| r)
|
||||
}
|
||||
|
||||
/// Resolve a point pick within one explicitly selected entity. This keeps two
|
||||
/// different endpoints at the same world coordinate distinguishable.
|
||||
pub(crate) fn nearest_parametric_point_on_entity(
|
||||
document: &acadrust::CadDocument,
|
||||
scope: ParametricScope,
|
||||
handle: Handle,
|
||||
world_point: Vector3,
|
||||
) -> Option<ParametricRef> {
|
||||
let entity = document.get_entity(handle)?;
|
||||
if entity.common().owner_handle != scope.owner_handle(document) {
|
||||
return None;
|
||||
}
|
||||
parametric_point_candidates(entity)
|
||||
.into_iter()
|
||||
.filter_map(|(marker, point)| {
|
||||
let distance = (point - world_point).length_squared();
|
||||
(distance <= COINCIDENT_EPSILON_SQ)
|
||||
.then_some((distance, ParametricRef::point(handle, marker)))
|
||||
})
|
||||
.min_by(|(a, _), (b, _)| a.total_cmp(b))
|
||||
.map(|(_, reference)| reference)
|
||||
}
|
||||
|
||||
fn point_segment_distance_squared(point: Vector3, start: Vector3, end: Vector3) -> f64 {
|
||||
let edge = end - start;
|
||||
let denominator = edge.length_squared();
|
||||
if denominator <= f64::EPSILON {
|
||||
return (point - start).length_squared();
|
||||
}
|
||||
let unit = ((point - start).dot(&edge) / denominator).clamp(0.0, 1.0);
|
||||
(point - (start + edge * unit)).length_squared()
|
||||
}
|
||||
|
||||
/// Resolve the whole curve or the picked straight polyline segment used by a
|
||||
/// point-to-curve Coincident relation.
|
||||
pub(crate) fn parametric_curve_ref_for_pick(
|
||||
document: &acadrust::CadDocument,
|
||||
scope: ParametricScope,
|
||||
handle: Handle,
|
||||
world_point: Vector3,
|
||||
) -> Option<ParametricRef> {
|
||||
let entity = document.get_entity(handle)?;
|
||||
if entity.common().owner_handle != scope.owner_handle(document) {
|
||||
return None;
|
||||
}
|
||||
match entity {
|
||||
acadrust::EntityType::Line(_)
|
||||
| acadrust::EntityType::Circle(_)
|
||||
| acadrust::EntityType::Arc(_)
|
||||
| acadrust::EntityType::Ellipse(_)
|
||||
| acadrust::EntityType::Spline(_) => Some(ParametricRef::whole(handle)),
|
||||
acadrust::EntityType::LwPolyline(polyline) => {
|
||||
let vertices = super::dimension_assoc::source_points(entity);
|
||||
(0..polyline.vertices.len())
|
||||
.filter_map(|index| {
|
||||
let start = vertices.get(index).copied()?;
|
||||
let end = vertices
|
||||
.get(index + 1)
|
||||
.copied()
|
||||
.or_else(|| polyline.is_closed.then(|| vertices.first().copied()).flatten())?;
|
||||
Some((point_segment_distance_squared(world_point, start, end), index))
|
||||
})
|
||||
.min_by(|(a, _), (b, _)| a.total_cmp(b))
|
||||
.map(|(_, index)| ParametricRef::segment(handle, index))
|
||||
}
|
||||
acadrust::EntityType::Polyline2D(polyline) => {
|
||||
let vertices = super::dimension_assoc::source_points(entity);
|
||||
(0..polyline.vertices.len())
|
||||
.filter_map(|index| {
|
||||
let start = vertices.get(index).copied()?;
|
||||
let end = vertices
|
||||
.get(index + 1)
|
||||
.copied()
|
||||
.or_else(|| polyline.is_closed().then(|| vertices.first().copied()).flatten())?;
|
||||
Some((point_segment_distance_squared(world_point, start, end), index))
|
||||
})
|
||||
.min_by(|(a, _), (b, _)| a.total_cmp(b))
|
||||
.map(|(_, index)| ParametricRef::segment(handle, index))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
impl ConstraintKind {
|
||||
/// The short symbol a constraint glyph shows — matches the existing
|
||||
/// ribbon icons (`crate::modules::parametric::{tools,value}`) for
|
||||
@@ -568,6 +752,49 @@ pub(crate) fn glyph_placement(
|
||||
}
|
||||
|
||||
impl super::Scene {
|
||||
/// Expand a set of entities to the full enabled constraint component in
|
||||
/// one scope. Whole-object translations use this to keep every connected
|
||||
/// entity's own shape while moving the assembly.
|
||||
pub(crate) fn parametric_connected_handles(
|
||||
&self,
|
||||
scope: ParametricScope,
|
||||
seeds: &[Handle],
|
||||
) -> Vec<Handle> {
|
||||
let mut ordered = seeds.to_vec();
|
||||
let mut found: std::collections::HashSet<_> = seeds.iter().copied().collect();
|
||||
let Some(set) = self.parametric_constraint_set(scope) else {
|
||||
return ordered;
|
||||
};
|
||||
loop {
|
||||
let mut added = false;
|
||||
for constraint in set.constraints.iter().filter(|constraint| {
|
||||
constraint.enabled
|
||||
&& matches!(
|
||||
constraint.kind,
|
||||
ConstraintKind::Coincident | ConstraintKind::PointOnCurve
|
||||
)
|
||||
}) {
|
||||
if !constraint
|
||||
.refs
|
||||
.iter()
|
||||
.any(|reference| found.contains(&reference.entity))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
for reference in &constraint.refs {
|
||||
if found.insert(reference.entity) {
|
||||
ordered.push(reference.entity);
|
||||
added = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !added {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ordered
|
||||
}
|
||||
|
||||
pub fn is_parametric_constraint_visible(
|
||||
&self,
|
||||
scope: ParametricScope,
|
||||
@@ -724,6 +951,68 @@ impl super::Scene {
|
||||
mapped
|
||||
}
|
||||
|
||||
/// Infer only endpoint/vertex coincidences from the selected objects.
|
||||
/// Unlike the broader automatic constraint pass, this includes spline,
|
||||
/// ellipse, and polyline vertices while leaving centers and midpoints to
|
||||
/// their dedicated constraint kinds.
|
||||
pub fn inferred_coincident_constraints(
|
||||
&self,
|
||||
scope: ParametricScope,
|
||||
handles: &[Handle],
|
||||
) -> Vec<Vec<ParametricRef>> {
|
||||
let owner = scope.owner_handle(&self.document);
|
||||
let mut sources = Vec::new();
|
||||
let mut seen_handles = std::collections::HashSet::new();
|
||||
for handle in handles.iter().copied() {
|
||||
if !seen_handles.insert(handle) {
|
||||
continue;
|
||||
}
|
||||
let Some(entity) = self.document.get_entity(handle) else {
|
||||
continue;
|
||||
};
|
||||
if entity.common().owner_handle != owner {
|
||||
continue;
|
||||
}
|
||||
for (marker, point) in super::dimension_assoc::source_points(entity)
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
sources.push((ParametricRef::point(handle, marker as i32), point));
|
||||
}
|
||||
}
|
||||
|
||||
let existing = self.parametric_constraint_set(scope);
|
||||
let mut inferred = Vec::new();
|
||||
for first in 0..sources.len() {
|
||||
for second in first + 1..sources.len() {
|
||||
if sources[first].0.entity == sources[second].0.entity
|
||||
|| (sources[first].1 - sources[second].1).length_squared()
|
||||
> COINCIDENT_EPSILON_SQ
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let refs = vec![sources[first].0, sources[second].0];
|
||||
let already_exists = existing.is_some_and(|set| {
|
||||
set.constraints.iter().any(|constraint| {
|
||||
constraint.kind == ConstraintKind::Coincident
|
||||
&& constraint.refs.len() == 2
|
||||
&& (constraint.refs == refs
|
||||
|| (constraint.refs[0] == refs[1]
|
||||
&& constraint.refs[1] == refs[0]))
|
||||
})
|
||||
});
|
||||
if !already_exists
|
||||
&& self
|
||||
.validate_parametric_constraint(ConstraintKind::Coincident, &refs, None)
|
||||
.is_ok()
|
||||
{
|
||||
inferred.push(refs);
|
||||
}
|
||||
}
|
||||
}
|
||||
inferred
|
||||
}
|
||||
|
||||
pub fn smooth_constraint_refs(&self, handles: &[Handle]) -> Option<Vec<ParametricRef>> {
|
||||
let [first, second] = handles else {
|
||||
return None;
|
||||
|
||||
+277
-43
@@ -45,6 +45,7 @@ enum EntityGeom {
|
||||
Polyline {
|
||||
points: Rc<Vec<GPoint>>,
|
||||
straight: Rc<Vec<bool>>,
|
||||
arcs: Rc<Vec<Option<PolylineArc>>>,
|
||||
closed: bool,
|
||||
},
|
||||
Circle(GCircle),
|
||||
@@ -61,6 +62,12 @@ enum EntityGeom {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct PolylineArc {
|
||||
arc: GArc,
|
||||
sweep: f64,
|
||||
}
|
||||
|
||||
impl EntityGeom {
|
||||
/// The `cadkernel_constraints::geo::Point` for a marker on this entity — `None` for
|
||||
/// a marker this entity type/value doesn't support (see
|
||||
@@ -91,6 +98,7 @@ impl EntityGeom {
|
||||
points,
|
||||
straight,
|
||||
closed,
|
||||
..
|
||||
} = self
|
||||
else {
|
||||
return None;
|
||||
@@ -108,6 +116,13 @@ impl EntityGeom {
|
||||
};
|
||||
Some(GLine { p1, p2 })
|
||||
}
|
||||
|
||||
fn arc_segment(&self, index: usize) -> Option<PolylineArc> {
|
||||
let EntityGeom::Polyline { arcs, .. } = self else {
|
||||
return None;
|
||||
};
|
||||
arcs.get(index).copied().flatten()
|
||||
}
|
||||
}
|
||||
|
||||
/// `Tangent`/`Normal` only ever care about "a whole circle-shaped curve" or
|
||||
@@ -131,8 +146,14 @@ fn as_circle_or_line(g: EntityGeom, reference: ParametricRef) -> CircleOrLine {
|
||||
EntityGeom::Spline { .. } => CircleOrLine::Other,
|
||||
EntityGeom::Polyline { .. } => reference
|
||||
.segment_index()
|
||||
.and_then(|index| g.line_segment(index))
|
||||
.map(CircleOrLine::Line)
|
||||
.and_then(|index| {
|
||||
g.line_segment(index)
|
||||
.map(CircleOrLine::Line)
|
||||
.or_else(|| {
|
||||
g.arc_segment(index)
|
||||
.map(|segment| CircleOrLine::Circle(segment.arc.circle))
|
||||
})
|
||||
})
|
||||
.unwrap_or(CircleOrLine::Other),
|
||||
}
|
||||
}
|
||||
@@ -182,42 +203,114 @@ fn register_entity(
|
||||
sys.add_param(table.insertion_point.x, false),
|
||||
sys.add_param(table.insertion_point.y, false),
|
||||
))),
|
||||
EntityType::LwPolyline(polyline) => Some(EntityGeom::Polyline {
|
||||
points: Rc::new(
|
||||
super::dimension_assoc::source_points(entity)
|
||||
.into_iter()
|
||||
.map(|point| {
|
||||
GPoint::new(sys.add_param(point.x, false), sys.add_param(point.y, false))
|
||||
EntityType::LwPolyline(polyline) => {
|
||||
let source = super::dimension_assoc::source_points(entity);
|
||||
let points: Vec<_> = source
|
||||
.iter()
|
||||
.map(|point| {
|
||||
GPoint::new(sys.add_param(point.x, false), sys.add_param(point.y, false))
|
||||
})
|
||||
.collect();
|
||||
let arcs = (0..polyline.vertices.len())
|
||||
.map(|index| {
|
||||
let next = if index + 1 < source.len() {
|
||||
index + 1
|
||||
} else if polyline.is_closed {
|
||||
0
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
let bulge = polyline.vertices[index].bulge;
|
||||
let shape = cadkernel::geom2d::BulgeArc::from_bulge(
|
||||
[source[index].x, source[index].y],
|
||||
[source[next].x, source[next].y],
|
||||
bulge,
|
||||
)?;
|
||||
Some(PolylineArc {
|
||||
arc: GArc {
|
||||
circle: GCircle {
|
||||
center: GPoint::new(
|
||||
sys.add_param(shape.center[0], false),
|
||||
sys.add_param(shape.center[1], false),
|
||||
),
|
||||
rad: sys.add_param(shape.radius, false),
|
||||
},
|
||||
start: points[index],
|
||||
end: points[next],
|
||||
start_angle: sys.add_param(shape.start_angle, false),
|
||||
end_angle: sys.add_param(shape.start_angle + shape.sweep, false),
|
||||
},
|
||||
sweep: shape.sweep,
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
straight: Rc::new(
|
||||
polyline
|
||||
.vertices
|
||||
.iter()
|
||||
.map(|vertex| vertex.bulge.abs() <= 1e-9)
|
||||
.collect(),
|
||||
),
|
||||
closed: polyline.is_closed,
|
||||
}),
|
||||
EntityType::Polyline2D(polyline) => Some(EntityGeom::Polyline {
|
||||
points: Rc::new(
|
||||
super::dimension_assoc::source_points(entity)
|
||||
.into_iter()
|
||||
.map(|point| {
|
||||
GPoint::new(sys.add_param(point.x, false), sys.add_param(point.y, false))
|
||||
})
|
||||
.collect();
|
||||
Some(EntityGeom::Polyline {
|
||||
points: Rc::new(points),
|
||||
straight: Rc::new(
|
||||
polyline
|
||||
.vertices
|
||||
.iter()
|
||||
.map(|vertex| vertex.bulge.abs() <= 1e-9)
|
||||
.collect(),
|
||||
),
|
||||
arcs: Rc::new(arcs),
|
||||
closed: polyline.is_closed,
|
||||
})
|
||||
}
|
||||
EntityType::Polyline2D(polyline) => {
|
||||
let source = super::dimension_assoc::source_points(entity);
|
||||
let points: Vec<_> = source
|
||||
.iter()
|
||||
.map(|point| {
|
||||
GPoint::new(sys.add_param(point.x, false), sys.add_param(point.y, false))
|
||||
})
|
||||
.collect();
|
||||
let arcs = (0..polyline.vertices.len())
|
||||
.map(|index| {
|
||||
let next = if index + 1 < source.len() {
|
||||
index + 1
|
||||
} else if polyline.is_closed() {
|
||||
0
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
let bulge = polyline.vertices[index].bulge;
|
||||
let shape = cadkernel::geom2d::BulgeArc::from_bulge(
|
||||
[source[index].x, source[index].y],
|
||||
[source[next].x, source[next].y],
|
||||
bulge,
|
||||
)?;
|
||||
Some(PolylineArc {
|
||||
arc: GArc {
|
||||
circle: GCircle {
|
||||
center: GPoint::new(
|
||||
sys.add_param(shape.center[0], false),
|
||||
sys.add_param(shape.center[1], false),
|
||||
),
|
||||
rad: sys.add_param(shape.radius, false),
|
||||
},
|
||||
start: points[index],
|
||||
end: points[next],
|
||||
start_angle: sys.add_param(shape.start_angle, false),
|
||||
end_angle: sys.add_param(shape.start_angle + shape.sweep, false),
|
||||
},
|
||||
sweep: shape.sweep,
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
straight: Rc::new(
|
||||
polyline
|
||||
.vertices
|
||||
.iter()
|
||||
.map(|vertex| vertex.bulge.abs() <= 1e-9)
|
||||
.collect(),
|
||||
),
|
||||
closed: polyline.is_closed(),
|
||||
}),
|
||||
})
|
||||
.collect();
|
||||
Some(EntityGeom::Polyline {
|
||||
points: Rc::new(points),
|
||||
straight: Rc::new(
|
||||
polyline
|
||||
.vertices
|
||||
.iter()
|
||||
.map(|vertex| vertex.bulge.abs() <= 1e-9)
|
||||
.collect(),
|
||||
),
|
||||
arcs: Rc::new(arcs),
|
||||
closed: polyline.is_closed(),
|
||||
})
|
||||
}
|
||||
EntityType::Line(l) => {
|
||||
let p1 = GPoint::new(
|
||||
sys.add_param(l.start.x, false),
|
||||
@@ -439,6 +532,42 @@ fn resolve_constraint_point(
|
||||
)));
|
||||
return Some(point);
|
||||
}
|
||||
if let Some(segment) = reference
|
||||
.segment_midpoint_index()
|
||||
.and_then(|index| geometry.arc_segment(index))
|
||||
{
|
||||
let (start, end) = {
|
||||
let store = sys.store();
|
||||
(
|
||||
store.get(segment.arc.start_angle),
|
||||
store.get(segment.arc.end_angle),
|
||||
)
|
||||
};
|
||||
let parameter_value = (start + end) * 0.5;
|
||||
let parameter = sys.add_param(parameter_value, false);
|
||||
sys.add_constraint(Rc::new(CenterOfGravity::new(
|
||||
parameter,
|
||||
vec![segment.arc.start_angle, segment.arc.end_angle],
|
||||
vec![0.5, 0.5],
|
||||
)));
|
||||
let value = cadkernel_constraints::geo::Curve::value(
|
||||
&segment.arc,
|
||||
sys.store(),
|
||||
parameter_value,
|
||||
0.0,
|
||||
None,
|
||||
);
|
||||
let point = GPoint::new(sys.add_param(value.x, false), sys.add_param(value.y, false));
|
||||
let curve = Rc::new(segment.arc);
|
||||
sys.add_constraint(Rc::new(CurveValue::new(
|
||||
point,
|
||||
point.x,
|
||||
curve.clone(),
|
||||
parameter,
|
||||
)));
|
||||
sys.add_constraint(Rc::new(CurveValue::new(point, point.y, curve, parameter)));
|
||||
return Some(point);
|
||||
}
|
||||
if marker != -2 {
|
||||
return None;
|
||||
}
|
||||
@@ -935,8 +1064,22 @@ fn build_constraint(
|
||||
}
|
||||
EntityGeom::Polyline { .. } => b
|
||||
.segment_index()
|
||||
.and_then(|index| geom.line_segment(index))
|
||||
.map(|line| vec![Rc::new(PointOnLine::new(p, line)) as Rc<dyn Constraint>])
|
||||
.and_then(|index| {
|
||||
geom.line_segment(index)
|
||||
.map(|line| {
|
||||
vec![Rc::new(PointOnLine::new(p, line)) as Rc<dyn Constraint>]
|
||||
})
|
||||
.or_else(|| {
|
||||
geom.arc_segment(index).map(|segment| {
|
||||
let zero = sys.add_param(0.0, true);
|
||||
vec![Rc::new(P2CDistance::new(
|
||||
segment.arc.circle,
|
||||
p,
|
||||
zero,
|
||||
)) as Rc<dyn Constraint>]
|
||||
})
|
||||
})
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
EntityGeom::Point(_) => Vec::new(),
|
||||
}
|
||||
@@ -1564,6 +1707,7 @@ fn solve_scope(
|
||||
document: &acadrust::CadDocument,
|
||||
drawing_params: &ParameterTable,
|
||||
set: &ParametricConstraintSet,
|
||||
driven_refs: &[ParametricRef],
|
||||
) -> Option<SolveResult> {
|
||||
let params = if set.local_parameters.is_empty() {
|
||||
drawing_params
|
||||
@@ -1604,9 +1748,8 @@ fn solve_scope(
|
||||
// its endpoint. Not tracked in `owner`: these are solver-internal
|
||||
// bookkeeping, never redundant with anything a user-facing constraint
|
||||
// could name, so there's no `ConstraintId` for them to report against.
|
||||
for geom in cache.values() {
|
||||
let EntityGeom::Arc(arc) = geom else { continue };
|
||||
let curve = Rc::new(*arc);
|
||||
let add_arc_rules = |sys: &mut System, arc: GArc, sweep: Option<f64>| {
|
||||
let curve = Rc::new(arc);
|
||||
sys.add_constraint(Rc::new(CurveValue::new(
|
||||
arc.start,
|
||||
arc.start.x,
|
||||
@@ -1631,6 +1774,25 @@ fn solve_scope(
|
||||
curve,
|
||||
arc.end_angle,
|
||||
)));
|
||||
if let Some(sweep) = sweep {
|
||||
let target = sys.add_param(sweep, true);
|
||||
sys.add_constraint(Rc::new(Difference::new(
|
||||
arc.start_angle,
|
||||
arc.end_angle,
|
||||
target,
|
||||
)));
|
||||
}
|
||||
};
|
||||
for geom in cache.values() {
|
||||
match geom {
|
||||
EntityGeom::Arc(arc) => add_arc_rules(&mut sys, *arc, None),
|
||||
EntityGeom::Polyline { arcs, .. } => {
|
||||
for segment in arcs.iter().flatten() {
|
||||
add_arc_rules(&mut sys, segment.arc, Some(segment.sweep));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Ellipse rules (same motivation as arc rules, different mechanism):
|
||||
@@ -1667,6 +1829,66 @@ fn solve_scope(
|
||||
sys.add_constraint(Rc::new(Difference::new(el.center.y, el.focus1.y, dy)));
|
||||
}
|
||||
|
||||
// The first ordered Coincident selection is the anchor for this solve.
|
||||
// Pin either its addressed point or every defining parameter of its whole
|
||||
// curve temporarily; the relation remains persistent, while these pins do
|
||||
// not enter the drawing's constraint graph.
|
||||
for reference in driven_refs {
|
||||
if !set
|
||||
.constraints
|
||||
.iter()
|
||||
.any(|constraint| constraint.refs.iter().any(|item| item.entity == reference.entity))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let params_to_pin = if reference.marker.is_some() && reference.segment_index().is_none() {
|
||||
resolve_constraint_point(document, &mut sys, &mut cache, *reference)
|
||||
.map(|point| vec![point.x, point.y])
|
||||
.unwrap_or_default()
|
||||
} else {
|
||||
match resolve_ref(document, &mut sys, &mut cache, *reference) {
|
||||
Some(EntityGeom::Point(point)) => vec![point.x, point.y],
|
||||
Some(EntityGeom::Line(line))
|
||||
| Some(EntityGeom::Ray(line))
|
||||
| Some(EntityGeom::XLine(line)) => {
|
||||
vec![line.p1.x, line.p1.y, line.p2.x, line.p2.y]
|
||||
}
|
||||
Some(EntityGeom::Polyline { points, .. }) => points
|
||||
.iter()
|
||||
.flat_map(|point| [point.x, point.y])
|
||||
.collect(),
|
||||
Some(EntityGeom::Circle(circle)) => {
|
||||
vec![circle.center.x, circle.center.y, circle.rad]
|
||||
}
|
||||
Some(EntityGeom::Arc(arc)) => vec![
|
||||
arc.circle.center.x,
|
||||
arc.circle.center.y,
|
||||
arc.circle.rad,
|
||||
arc.start_angle,
|
||||
arc.end_angle,
|
||||
],
|
||||
Some(EntityGeom::Ellipse(ellipse)) => vec![
|
||||
ellipse.center.x,
|
||||
ellipse.center.y,
|
||||
ellipse.focus1.x,
|
||||
ellipse.focus1.y,
|
||||
ellipse.radmin,
|
||||
],
|
||||
Some(EntityGeom::Spline { curve, .. }) => curve
|
||||
.poles
|
||||
.iter()
|
||||
.flat_map(|point| [point.x, point.y])
|
||||
.collect(),
|
||||
None => Vec::new(),
|
||||
}
|
||||
};
|
||||
for parameter in params_to_pin {
|
||||
let value = sys.store().get(parameter);
|
||||
let target = sys.add_param(value, true);
|
||||
sys.add_constraint(Rc::new(Equal::new(parameter, target, 1.0)));
|
||||
}
|
||||
}
|
||||
|
||||
let partitions = sys.partition();
|
||||
for sub in &partitions {
|
||||
solve_dl(sub, sys.store_mut());
|
||||
@@ -1683,7 +1905,9 @@ fn solve_scope(
|
||||
.map(|g| match g {
|
||||
EntityGeom::Point(_) => 2,
|
||||
EntityGeom::Line(_) | EntityGeom::Ray(_) | EntityGeom::XLine(_) => 4,
|
||||
EntityGeom::Polyline { points, .. } => points.len() * 2,
|
||||
EntityGeom::Polyline { points, arcs, .. } => {
|
||||
points.len() * 2 + arcs.iter().flatten().count() * 5
|
||||
}
|
||||
EntityGeom::Circle(_) => 3,
|
||||
// Raw param count (center×2, rad, start×2, end×2, both
|
||||
// angles) — same "raw, not netted against its own
|
||||
@@ -2151,9 +2375,18 @@ impl Scene {
|
||||
/// the resulting `(Handle, ChangeKind::Modified)` entries the same way
|
||||
/// `refresh_associative_dimensions`/`_hatches` do, for `bump_entities`
|
||||
/// to fold into its own `changes` vec.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn refresh_parametric_constraints(
|
||||
&mut self,
|
||||
changes: &[(Handle, ChangeKind)],
|
||||
) -> Vec<(Handle, ChangeKind)> {
|
||||
self.refresh_parametric_constraints_with_driven(changes, &[])
|
||||
}
|
||||
|
||||
pub(crate) fn refresh_parametric_constraints_with_driven(
|
||||
&mut self,
|
||||
changes: &[(Handle, ChangeKind)],
|
||||
driven_refs: &[ParametricRef],
|
||||
) -> Vec<(Handle, ChangeKind)> {
|
||||
// An erased entity takes its constraints with it instead of leaving
|
||||
// dangling references. Done first,
|
||||
@@ -2200,6 +2433,7 @@ impl Scene {
|
||||
&self.document,
|
||||
&self.named_parameters,
|
||||
&self.parametric_constraints[i],
|
||||
driven_refs,
|
||||
) else {
|
||||
continue;
|
||||
};
|
||||
@@ -2237,7 +2471,7 @@ impl Scene {
|
||||
continue;
|
||||
}
|
||||
let Some((solved, _dof, _conflicts)) =
|
||||
solve_scope(&self.document, &self.named_parameters, set)
|
||||
solve_scope(&self.document, &self.named_parameters, set, &[])
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -80,6 +80,25 @@ pub fn properties_sectioned(
|
||||
} else {
|
||||
sections.extend(groups);
|
||||
}
|
||||
if let Some(thickness) = entity_thickness(entity) {
|
||||
let already_present = sections.iter().flat_map(|section| §ion.props).any(|property| {
|
||||
property.field == "thickness"
|
||||
|| property.field == "sl_thickness"
|
||||
|| property.label == crate::t!("Thickness").as_ref()
|
||||
});
|
||||
if !already_present {
|
||||
if let Some(geometry) = sections
|
||||
.iter_mut()
|
||||
.find(|section| section.title == crate::t!("Geometry").as_ref())
|
||||
{
|
||||
geometry.props.push(crate::entities::common::edit_prop(
|
||||
crate::t!("Thickness").as_ref(),
|
||||
"thickness",
|
||||
thickness,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
sections
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user