From a30010cb3e89e1da3eaf8ddade32c5b28388341d Mon Sep 17 00:00:00 2001 From: ramox81 <184937705+ramox81@users.noreply.github.com> Date: Mon, 24 Aug 2026 12:10:29 +0300 Subject: [PATCH] Keep linear dimensions associated with sources --- src/app/command_driver.rs | 53 ++++++++- src/command.rs | 5 + src/scene/dimension_assoc.rs | 204 +++++++++++++++++++++++++++++++++++ src/scene/mod.rs | 6 ++ 4 files changed, 266 insertions(+), 2 deletions(-) create mode 100644 src/scene/dimension_assoc.rs diff --git a/src/app/command_driver.rs b/src/app/command_driver.rs index 97a03e26f..77a1e1756 100644 --- a/src/app/command_driver.rs +++ b/src/app/command_driver.rs @@ -801,7 +801,21 @@ impl OpenCADStudio { // the structure snapshot fallback. let delta_safe = self.delta_add_safe(i, &entity); let pending = self.begin_undo(i, label, 1, delta_safe); - self.commit_entity(entity); + let is_linear_dimension = matches!( + entity, + acadrust::EntityType::Dimension(acadrust::entities::Dimension::Linear(_)) + ); + let committed = self.commit_entity_handle(entity); + if is_linear_dimension { + if let Some(handle) = committed { + let sources = self.tabs[i] + .scene + .infer_linear_dimension_sources(handle); + self.tabs[i] + .scene + .attach_linear_dimension_association(handle, sources); + } + } self.tabs[i].dirty = true; let prompt = self.tabs[i].active_cmd.as_ref().map(|c| c.prompt()); if let Some(p) = prompt { @@ -1191,7 +1205,21 @@ impl OpenCADStudio { let label = self.history_label_from_active_cmd(i, "ENTITY"); let delta_safe = self.delta_add_safe(i, &entity); let pending = self.begin_undo(i, label, 1, delta_safe); - self.commit_entity(entity); + let is_linear_dimension = matches!( + entity, + acadrust::EntityType::Dimension(acadrust::entities::Dimension::Linear(_)) + ); + let committed = self.commit_entity_handle(entity); + if is_linear_dimension { + if let Some(handle) = committed { + let sources = self.tabs[i] + .scene + .infer_linear_dimension_sources(handle); + self.tabs[i] + .scene + .attach_linear_dimension_association(handle, sources); + } + } self.tabs[i].dirty = true; self.tabs[i].scene.clear_preview_wire(); self.tabs[i].active_cmd = None; @@ -1201,6 +1229,27 @@ impl OpenCADStudio { self.commit_undo_delta(i, pd); } } + CmdResult::CommitAssociativeDimension { entity, source } => { + let label = self.history_label_from_active_cmd(i, "DIMLINEAR"); + let pending = self.begin_undo(i, label, 1, false); + if let Some(handle) = self.commit_entity_handle(entity) { + self.tabs[i] + .scene + .attach_linear_dimension_association(handle, [Some(source), Some(source)]); + self.tabs[i].scene.bump_entities(&[ + (handle, crate::scene::ChangeKind::Modified), + (source, crate::scene::ChangeKind::Modified), + ]); + } + self.tabs[i].dirty = true; + self.tabs[i].scene.clear_preview_wire(); + self.tabs[i].active_cmd = None; + self.tabs[i].snap_result = None; + self.restore_pre_cmd_tangent(); + if let Some(pending) = pending { + self.commit_undo_delta(i, pending); + } + } CmdResult::CommitSolid { entity, solid, diff --git a/src/command.rs b/src/command.rs index 0ed4a77f8..89b8d0382 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1212,6 +1212,11 @@ pub enum CmdResult { CommitEntitiesAndExit(Vec), /// Commit an acadrust entity to the document and end the command. CommitAndExit(EntityType), + /// Commit an object-selected linear dimension and retain its source link. + CommitAssociativeDimension { + entity: EntityType, + source: Handle, + }, /// Commit a Model-tab 3D solid: the acadrust entity (for selection / /// persistence) plus its B-rep (cached for boolean ops + shaded /// rendering). Ends the command. diff --git a/src/scene/dimension_assoc.rs b/src/scene/dimension_assoc.rs new file mode 100644 index 000000000..ea91679fb --- /dev/null +++ b/src/scene/dimension_assoc.rs @@ -0,0 +1,204 @@ +use acadrust::entities::Dimension; +use acadrust::objects::{ + AssocDimensionAssociation, AssocDimensionReference, AssociativeData, + AssociativeObject, ObjectType, +}; +use acadrust::types::{Handle, Vector3}; +use acadrust::EntityType; + +use super::{ChangeKind, Scene}; + +fn point_distance_squared(first: Vector3, second: Vector3) -> f64 { + let dx = first.x - second.x; + let dy = first.y - second.y; + let dz = first.z - second.z; + dx * dx + dy * dy + dz * dz +} + +fn source_points(entity: &EntityType) -> Vec { + match entity { + EntityType::Line(line) => vec![line.start, line.end], + EntityType::Arc(arc) => vec![arc.start_point(), arc.end_point()], + EntityType::LwPolyline(polyline) => polyline + .vertices + .iter() + .map(|vertex| { + Vector3::new(vertex.location.x, vertex.location.y, polyline.elevation) + }) + .collect(), + EntityType::Polyline2D(polyline) => polyline + .vertices + .iter() + .map(|vertex| vertex.location) + .collect(), + _ => Vec::new(), + } +} + +fn source_marker(entity: &EntityType, point: Vector3) -> Option { + source_points(entity) + .into_iter() + .enumerate() + .min_by(|(_, first), (_, second)| { + point_distance_squared(*first, point) + .total_cmp(&point_distance_squared(*second, point)) + }) + .map(|(index, _)| index as i32) +} + +fn resolve_reference(scene: &Scene, reference: &AssocDimensionReference) -> Option { + let source = *reference.xrefs.first()?; + let entity = scene.document.get_entity(source)?; + source_points(entity) + .get(reference.main_gs_marker.max(0) as usize) + .copied() +} + +impl Scene { + pub(crate) fn attach_linear_dimension_association( + &mut self, + dimension: Handle, + sources: [Option; 2], + ) { + let Some(EntityType::Dimension(Dimension::Linear(linear))) = + self.document.get_entity(dimension) + else { + return; + }; + let first_point = linear.first_point; + let second_point = linear.second_point; + let source_data = [first_point, second_point].map(|point| point); + let resolved: [Option<(Handle, i32)>; 2] = std::array::from_fn(|index| { + let source = sources[index]?; + let entity = self.document.get_entity(source)?; + source_marker(entity, source_data[index]).map(|marker| (source, marker)) + }); + if resolved.iter().all(Option::is_none) { + return; + } + + let reference = |source: Handle, marker: i32, point: Vector3| AssocDimensionReference { + class_name: "AcDbOsnapPointRef".to_string(), + osnap_type: 1, + xrefs: vec![source], + main_subent_type: 1, + main_gs_marker: marker, + osnap_point: point, + ..AssocDimensionReference::default() + }; + let mut references: [Vec; 4] = + std::array::from_fn(|_| Vec::new()); + let mut associativity = 0; + for (index, resolved) in resolved.into_iter().enumerate() { + if let Some((source, marker)) = resolved { + associativity |= 1 << index; + references[index].push(reference(source, marker, source_data[index])); + } + } + + let association_handle = self.document.allocate_handle(); + let mut object = AssociativeObject::new("DIMASSOC", "AcDbDimAssoc"); + object.handle = association_handle; + object.reactors.push(dimension); + object.data = AssociativeData::DimensionAssociation(AssocDimensionAssociation { + associativity, + dimension, + references, + ..AssocDimensionAssociation::default() + }); + self.document + .objects + .insert(association_handle, ObjectType::Associative(object)); + + let mut reactor_targets = vec![dimension]; + reactor_targets.extend(sources.into_iter().flatten()); + reactor_targets.sort_by_key(|handle| handle.value()); + reactor_targets.dedup(); + for handle in reactor_targets { + if let Some(entity) = self.document.get_entity_mut(handle) { + if !entity.common().reactors.contains(&association_handle) { + entity.common_mut().reactors.push(association_handle); + } + } + } + } + + pub(crate) fn infer_linear_dimension_sources( + &self, + dimension: Handle, + ) -> [Option; 2] { + let Some(EntityType::Dimension(Dimension::Linear(linear))) = + self.document.get_entity(dimension) + else { + return [None, None]; + }; + [linear.first_point, linear.second_point].map(|point| { + self.document + .entities() + .filter(|entity| entity.common().handle != dimension) + .filter_map(|entity| { + source_points(entity) + .into_iter() + .map(|candidate| point_distance_squared(candidate, point)) + .min_by(f64::total_cmp) + .map(|distance| (distance, entity.common().handle)) + }) + .filter(|(distance, _)| *distance <= 1e-16) + .min_by(|first, second| first.0.total_cmp(&second.0)) + .map(|(_, handle)| handle) + }) + } + + pub(crate) fn refresh_associative_dimensions( + &mut self, + changes: &[(Handle, ChangeKind)], + ) -> Vec<(Handle, ChangeKind)> { + let changed: rustc_hash::FxHashSet<_> = + changes.iter().map(|(handle, _)| *handle).collect(); + if changed.is_empty() { + return Vec::new(); + } + let associations: Vec<_> = self + .document + .objects + .values() + .filter_map(|object| { + let ObjectType::Associative(object) = object else { + return None; + }; + let AssociativeData::DimensionAssociation(association) = &object.data else { + return None; + }; + association + .references + .iter() + .flatten() + .any(|reference| reference.xrefs.iter().any(|handle| changed.contains(handle))) + .then_some(association.clone()) + }) + .collect(); + + let mut refreshed = Vec::new(); + for association in associations { + let first = association.references[0] + .first() + .and_then(|reference| resolve_reference(self, reference)); + let second = association.references[1] + .first() + .and_then(|reference| resolve_reference(self, reference)); + let (Some(first), Some(second)) = (first, second) else { + continue; + }; + if let Some(EntityType::Dimension(Dimension::Linear(linear))) = + self.document.get_entity_mut(association.dimension) + { + linear.first_point = first; + linear.second_point = second; + linear.base.actual_measurement = linear.measurement(); + linear.base.definition_point = linear.definition_point; + refreshed.push((association.dimension, ChangeKind::Modified)); + } + } + refreshed + } +} diff --git a/src/scene/mod.rs b/src/scene/mod.rs index b22dde385..cf14edc61 100644 --- a/src/scene/mod.rs +++ b/src/scene/mod.rs @@ -22,6 +22,7 @@ pub mod view; mod boundary; mod camera_ops; pub(crate) mod centerline; +pub(crate) mod dimension_assoc; pub(crate) mod centermark; mod entity; mod group_layer; @@ -2400,6 +2401,11 @@ impl Scene { changes.push(change); } } + for change in self.refresh_associative_dimensions(&changes) { + if !changes.iter().any(|(handle, _)| *handle == change.0) { + changes.push(change); + } + } for change in self.refresh_associative_hatches(&changes) { if !changes.iter().any(|(handle, _)| *handle == change.0) { changes.push(change);