From 8979fa46c2aad25f315cc291bb5ce6e4168235f4 Mon Sep 17 00:00:00 2001 From: ramox81 <184937705+ramox81@users.noreply.github.com> Date: Fri, 11 Sep 2026 11:28:21 +0300 Subject: [PATCH] Place curve markers along spatial splines and polylines --- src/entities/curve.rs | 45 ++++++++++++++++++++++++++++++ src/modules/draw/inquiry/divide.rs | 40 ++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/src/entities/curve.rs b/src/entities/curve.rs index f477a83b8..0eede62e8 100644 --- a/src/entities/curve.rs +++ b/src/entities/curve.rs @@ -107,6 +107,51 @@ pub fn entity_curve(entity: &EntityType) -> Option { } } +/// Exact spatial source geometry for commands that traverse nonplanar curves. +/// Curve construction and arc-length calculations remain in the kernel. +pub fn entity_spatial_measurement(entity: &EntityType) -> Option { + use cadkernel::space::{ArcLengthCurve3, NurbsCurve3, Parameterization}; + let curve = match entity { + EntityType::Polyline3D(polyline) => { + if polyline.flags.spline_fit { + let degree = match polyline.smooth_type as i16 { 5 => 2, 6 => 3, _ => return None }; + let controls: Vec<_> = polyline.vertices.iter().filter(|vertex| vertex.flags & 16 != 0) + .map(|vertex| [vertex.position.x, vertex.position.y, vertex.position.z]).collect(); + let curve = NurbsCurve3::from_control_polygon(degree, &controls, polyline.is_closed())?; + return ArcLengthCurve3::from_nurbs(curve); + } + let points: Vec<_> = polyline.vertices.iter().map(|vertex| { + [vertex.position.x, vertex.position.y, vertex.position.z] + }).collect(); + return ArcLengthCurve3::from_polyline(&points, polyline.is_closed()); + } + EntityType::Spline(spline) => { + if crate::entities::spline::uses_fit_method(spline) { + let points: Vec<_> = spline.fit_points.iter().copied().map(xyz).collect(); + let parameterization = match spline.knot_parameterization { + 1 => Parameterization::Centripetal, 2 => Parameterization::Uniform, + _ => Parameterization::Chord, + }; + if spline.flags.periodic { + NurbsCurve3::interpolate_periodic(&points, parameterization)? + } else { + if spline.flags.closed { return None; } + NurbsCurve3::interpolate_fit(&points, Some(xyz(spline.begin_tangent)), + Some(xyz(spline.end_tangent)), parameterization)? + } + } else { + let controls: Vec<_> = spline.control_points.iter().copied().map(xyz).collect(); + let weights = if spline.weights.is_empty() { vec![1.0; controls.len()] } + else { spline.weights.clone() }; + NurbsCurve3::new_strict(spline.degree as usize, controls, spline.knots.clone(), weights)? + .with_periodicity(spline.flags.periodic) + } + } + _ => return None, + }; + ArcLengthCurve3::from_nurbs(curve) +} + /// The plane an OCS-stored entity's coordinates are read in. /// /// `elevation` is the entity's third stored coordinate — the distance along diff --git a/src/modules/draw/inquiry/divide.rs b/src/modules/draw/inquiry/divide.rs index 3ffb0d3e8..b5c0ba84e 100644 --- a/src/modules/draw/inquiry/divide.rs +++ b/src/modules/draw/inquiry/divide.rs @@ -5,7 +5,7 @@ use acadrust::{Entity, EntityType, Handle}; use cadkernel::space::PlanarCurve; use glam::DVec3; use crate::command::{CadCommand, CmdOption, CmdResult, CurveMarker, WorkingPlane}; -use crate::entities::curve::entity_curve; +use crate::entities::curve::{entity_curve, entity_spatial_measurement}; use crate::t; const MEASURE: bool = false; @@ -215,9 +215,10 @@ pub fn measure_entity(entity: &EntityType, segment_length: f64) -> Vec Vec) -> EntityType { +fn make_marker(curve: &MeasuredCurve, distance: f64, marker: Option<&CurveMarker>) -> EntityType { let pos = curve.point_at_distance(distance); let Some(marker) = marker else { let mut point = PointEnt::new(); @@ -251,9 +252,36 @@ fn make_point(pos: [f64; 3]) -> EntityType { EntityType::Point(p) } -fn measurable(entity: &EntityType) -> Option<(PlanarCurve, f64)> { - let curve = entity_curve(entity)?; - let total = curve.curve.length(); +/// The entity's curve and its length, or `None` for anything that cannot be +/// walked along — a hatch, a block, an unbounded ray. +enum MeasuredCurve { + Planar(PlanarCurve), + Spatial(cadkernel::space::ArcLengthCurve3), +} +impl MeasuredCurve { + fn is_closed(&self) -> bool { + match self { Self::Planar(curve) => curve.is_closed(), Self::Spatial(curve) => curve.is_closed() } + } + fn point_at_distance(&self, distance: f64) -> [f64; 3] { + match self { Self::Planar(curve) => curve.point_at_distance(distance), Self::Spatial(curve) => curve.point_at_distance(distance) } + } + fn parameter_at_distance(&self, distance: f64) -> f64 { + match self { Self::Planar(curve) => curve.parameter_at_distance(distance), Self::Spatial(curve) => curve.parameter_at_distance(distance) } + } + fn tangent_at(&self, parameter: f64) -> [f64; 3] { + match self { Self::Planar(curve) => curve.tangent_at(parameter), Self::Spatial(curve) => curve.tangent_at(parameter) } + } +} + +fn measurable(entity: &EntityType) -> Option<(MeasuredCurve, f64)> { + let (curve, total) = if let Some(curve) = entity_curve(entity) { + let total = curve.length(); + (MeasuredCurve::Planar(curve), total) + } else { + let curve = entity_spatial_measurement(entity)?; + let total = curve.length(); + (MeasuredCurve::Spatial(curve), total) + }; (total.is_finite() && total > 1e-10).then_some((curve, total)) }