forked from 1b-agent/opencadstudio-upstream
Linear dimensional constraints: point flow, dynamic dimension, driving parameter
DCLINEAR, DCHORIZONTAL, DCVERTICAL and DCALIGNED follow the reference flow: `Specify first constraint point or [Object] <Object>:`, `Specify second constraint point:`, `Specify dimension line location:`, then `Dimension text = …` and `Enter value or name and value <d1=100>:`. Linear reads horizontal or vertical from the dimension line location the way a linear dimension does; Object takes a line's, an arc's or a polyline segment's ends. A miss reports `No valid constraint point found.` / `No object found.` and asks again; another object type reports `Invalid selection for Linear. Select a line, polyline segment or arc.`. Each constraint creates a drawing parameter (`d1`, `d2`, …, or the typed `name=expression`), drives the constraint through it, and draws a dynamic dimension in constraint gray whose text follows CONSTRAINTNAMEFORMAT (`d1=100.0000`); the dimension is associated with the constrained objects and its text is refreshed after every solve. A typed value other than the measured one moves the second point while the first stays, and so does a later parameter edit. Deleting the constraint removes its dimension and, when nothing else reads it, its parameter. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
fd2062bdec
commit
72fd3fd35b
9 files changed
+898
-36
No files matched your search
+246
-2
@@ -1103,6 +1103,50 @@ impl OpenCADStudio {
|
||||
TableCellEditStart::Started
|
||||
}
|
||||
|
||||
/// Reports a refused command-line value and shows the prompt again.
|
||||
fn reprompt_active_command(&mut self, i: usize, message: &str) {
|
||||
self.command_line.push_error(message);
|
||||
if let Some(prompt) = self.tabs[i].active_cmd.as_ref().map(|command| command.prompt()) {
|
||||
self.command_line.push_info(&prompt);
|
||||
}
|
||||
}
|
||||
|
||||
/// Drops what removed dimensional constraints leave behind: their dynamic
|
||||
/// dimensions, and their parameters when no other constraint reads them.
|
||||
pub(crate) fn purge_dimensional_extras(
|
||||
&mut self,
|
||||
i: usize,
|
||||
dimensions: Vec<Handle>,
|
||||
parameters: Vec<String>,
|
||||
) {
|
||||
if !dimensions.is_empty() {
|
||||
self.tabs[i].scene.erase_entities(&dimensions);
|
||||
}
|
||||
let unused: Vec<String> = parameters
|
||||
.into_iter()
|
||||
.filter(|name| {
|
||||
!self.tabs[i]
|
||||
.scene
|
||||
.parametric_constraints
|
||||
.iter()
|
||||
.flat_map(|set| set.constraints.iter())
|
||||
.any(|constraint| {
|
||||
matches!(
|
||||
&constraint.driving_param,
|
||||
Some(crate::scene::named_parameters::DrivingValue::Named(used))
|
||||
if used == name
|
||||
)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
if !unused.is_empty() {
|
||||
self.tabs[i].scene.record_undo_named_parameters_before();
|
||||
for name in &unused {
|
||||
self.tabs[i].scene.named_parameters_mut().remove(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_parametric_constraint(
|
||||
&mut self,
|
||||
id: crate::scene::parametric_constraints::ConstraintId,
|
||||
@@ -1117,8 +1161,13 @@ impl OpenCADStudio {
|
||||
return false;
|
||||
};
|
||||
let touched: Vec<Handle> = constraint.refs.iter().map(|r| r.entity).collect();
|
||||
let dimension = set.dimensions.get(&id).copied();
|
||||
let parameter = match &constraint.driving_param {
|
||||
Some(crate::scene::named_parameters::DrivingValue::Named(name)) => Some(name.clone()),
|
||||
_ => None,
|
||||
};
|
||||
let constraints_before = set.clone();
|
||||
let pending = self.begin_undo(i, label, touched.len(), true);
|
||||
let pending = self.begin_undo(i, label, touched.len(), dimension.is_none());
|
||||
self.tabs[i]
|
||||
.scene
|
||||
.record_undo_parametric_constraints_before(scope, constraints_before);
|
||||
@@ -1126,6 +1175,11 @@ impl OpenCADStudio {
|
||||
.scene
|
||||
.parametric_constraint_set_mut(scope)
|
||||
.remove(id);
|
||||
self.purge_dimensional_extras(
|
||||
i,
|
||||
dimension.into_iter().collect(),
|
||||
parameter.into_iter().collect(),
|
||||
);
|
||||
if self.tabs[i].scene.selected_constraint == Some(id) {
|
||||
self.tabs[i].scene.selected_constraint = None;
|
||||
}
|
||||
@@ -1219,6 +1273,23 @@ impl OpenCADStudio {
|
||||
.flat_map(|c| c.refs.iter().map(|r| r.entity))
|
||||
.collect();
|
||||
|
||||
// A changed value moves a dimensional constraint's second point; its
|
||||
// first point stays, as in the reference.
|
||||
let anchors: Vec<crate::scene::parametric_constraints::ParametricRef> = self.tabs[i]
|
||||
.scene
|
||||
.parametric_constraints
|
||||
.iter()
|
||||
.flat_map(|set| set.constraints.iter())
|
||||
.filter(|c| {
|
||||
c.enabled
|
||||
&& matches!(
|
||||
c.driving_param,
|
||||
Some(crate::scene::named_parameters::DrivingValue::Named(_))
|
||||
)
|
||||
})
|
||||
.filter_map(|c| c.refs.first().copied())
|
||||
.filter(|reference| reference.marker.is_some())
|
||||
.collect();
|
||||
let pending = self.begin_undo(i, "Apply named parameters", touched.len(), true);
|
||||
self.tabs[i].scene.record_undo_named_parameters_before();
|
||||
self.tabs[i].scene.named_parameters = table;
|
||||
@@ -1228,10 +1299,15 @@ impl OpenCADStudio {
|
||||
.into_iter()
|
||||
.map(|h| (h, crate::scene::ChangeKind::Modified))
|
||||
.collect();
|
||||
self.tabs[i].scene.bump_entities(&changes);
|
||||
self.tabs[i].scene.bump_entities_with_parametric_policy(
|
||||
&changes,
|
||||
&anchors,
|
||||
self.constraint_solve_mode,
|
||||
);
|
||||
} else {
|
||||
self.tabs[i].scene.sync_native_parametric_graph();
|
||||
}
|
||||
self.tabs[i].scene.refresh_dynamic_dimension_texts();
|
||||
if let Some(pd) = pending {
|
||||
self.commit_undo_delta(i, pd);
|
||||
}
|
||||
@@ -3450,6 +3526,174 @@ impl OpenCADStudio {
|
||||
self.commit_undo_delta(i, pd);
|
||||
}
|
||||
}
|
||||
CmdResult::CheckConstraintPoint(pick) => {
|
||||
use crate::scene::parametric_constraints::{
|
||||
nearest_parametric_point, nearest_parametric_point_on_entity, resolve_point,
|
||||
};
|
||||
let scope = self.tabs[i].current_parametric_scope();
|
||||
let resolved = {
|
||||
let document = &self.tabs[i].scene.document;
|
||||
let world =
|
||||
acadrust::types::Vector3::new(pick.point.x, pick.point.y, pick.point.z);
|
||||
let reference = match pick.handle {
|
||||
Some(handle) => {
|
||||
nearest_parametric_point_on_entity(document, scope, handle, world)
|
||||
}
|
||||
None => nearest_parametric_point(document, scope, world, None),
|
||||
};
|
||||
reference.map(|reference| {
|
||||
let point = document
|
||||
.get_entity(reference.entity)
|
||||
.zip(reference.marker)
|
||||
.and_then(|(entity, marker)| resolve_point(entity, marker))
|
||||
.map_or(pick.point, |p| glam::DVec3::new(p.x, p.y, p.z));
|
||||
(reference, point)
|
||||
})
|
||||
};
|
||||
let Some((reference, point)) = resolved else {
|
||||
return self.apply_cmd_result(CmdResult::ReportError(
|
||||
crate::modules::parametric::DimConstraintCommand::NO_POINT.to_string(),
|
||||
));
|
||||
};
|
||||
let result = self.tabs[i]
|
||||
.active_cmd
|
||||
.as_mut()
|
||||
.map(|command| command.accept_constraint_point(reference, point));
|
||||
return match result {
|
||||
Some(result) => self.apply_cmd_result(result),
|
||||
None => Task::none(),
|
||||
};
|
||||
}
|
||||
CmdResult::AddDimensionalConstraint {
|
||||
kind,
|
||||
first,
|
||||
second,
|
||||
first_point,
|
||||
second_point,
|
||||
location,
|
||||
axis,
|
||||
name,
|
||||
expression,
|
||||
label,
|
||||
} => {
|
||||
use crate::modules::annotate::{aligned_dim, linear_dim};
|
||||
use crate::scene::named_parameters::{is_valid_name, DrivingValue};
|
||||
use crate::scene::parametric_constraints::{dynamic_dimension_text, ConstraintKind};
|
||||
|
||||
let scope = self.tabs[i].current_parametric_scope();
|
||||
let refs = vec![first, second];
|
||||
// A refused value keeps the prompt open, as the reference's
|
||||
// editor does.
|
||||
if let Err(message) = self.tabs[i].scene.validate_parametric_constraint(
|
||||
kind,
|
||||
&refs,
|
||||
Some(&DrivingValue::Literal(1.0)),
|
||||
) {
|
||||
self.reprompt_active_command(i, message);
|
||||
return Task::none();
|
||||
}
|
||||
let name = name.trim().to_string();
|
||||
if !is_valid_name(&name) {
|
||||
self.reprompt_active_command(i, "Invalid parameter name.");
|
||||
return Task::none();
|
||||
}
|
||||
let mut table = self.tabs[i].scene.named_parameters().clone();
|
||||
if let Err(error) = table.set(&name, &expression) {
|
||||
self.reprompt_active_command(i, &error.to_string());
|
||||
return Task::none();
|
||||
}
|
||||
let value = match table.resolve(&name) {
|
||||
Ok(value) if value.is_finite() => value,
|
||||
_ => {
|
||||
self.reprompt_active_command(i, "Invalid expression.");
|
||||
return Task::none();
|
||||
}
|
||||
};
|
||||
let text = dynamic_dimension_text(
|
||||
&name,
|
||||
value,
|
||||
self.tabs[i].scene.constraint_name_format,
|
||||
);
|
||||
let mut entity = if kind == ConstraintKind::Distance {
|
||||
aligned_dim::aligned_dimension_entity(
|
||||
first_point,
|
||||
second_point,
|
||||
location,
|
||||
Some(text),
|
||||
)
|
||||
} else {
|
||||
linear_dim::linear_dimension_entity(
|
||||
first_point,
|
||||
second_point,
|
||||
location,
|
||||
axis,
|
||||
Some(text),
|
||||
)
|
||||
};
|
||||
crate::scene::creation_style::apply_current_creation_styles(
|
||||
&self.tabs[i].scene.document,
|
||||
&mut entity,
|
||||
);
|
||||
let layer = self.tabs[i].active_layer.clone();
|
||||
if layer != "0" {
|
||||
entity.as_entity_mut().set_layer(layer);
|
||||
}
|
||||
// A dynamic dimension draws in the constraint gray, not the
|
||||
// current color.
|
||||
entity.common_mut().color = acadrust::types::Color::Rgb {
|
||||
r: 103,
|
||||
g: 109,
|
||||
b: 118,
|
||||
};
|
||||
let constraints_before = self.tabs[i]
|
||||
.scene
|
||||
.parametric_constraint_set(scope)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| {
|
||||
crate::scene::parametric_constraints::ParametricConstraintSet::new(scope)
|
||||
});
|
||||
let mut touched = vec![first.entity];
|
||||
if second.entity != first.entity {
|
||||
touched.push(second.entity);
|
||||
}
|
||||
let pending = self.begin_undo(i, label, touched.len() + 1, false);
|
||||
self.tabs[i]
|
||||
.scene
|
||||
.record_undo_parametric_constraints_before(scope, constraints_before);
|
||||
self.tabs[i].scene.record_undo_named_parameters_before();
|
||||
self.tabs[i].scene.named_parameters = table;
|
||||
let dimension = self.tabs[i].scene.add_entity(entity);
|
||||
let set = self.tabs[i].scene.parametric_constraint_set_mut(scope);
|
||||
let id = set.add(kind, refs, Some(DrivingValue::Named(name)));
|
||||
set.dimensions.insert(id, dimension);
|
||||
self.tabs[i].scene.note_parametric_constraint_applied(
|
||||
scope,
|
||||
id,
|
||||
self.constraint_bar_display,
|
||||
);
|
||||
self.tabs[i].scene.attach_dimension_association(
|
||||
dimension,
|
||||
vec![Some(first.entity), Some(second.entity)],
|
||||
);
|
||||
let changes: Vec<(Handle, crate::scene::ChangeKind)> = touched
|
||||
.into_iter()
|
||||
.map(|handle| (handle, crate::scene::ChangeKind::Modified))
|
||||
.collect();
|
||||
// The first point stays; a value other than the measured one
|
||||
// moves the second, as in the reference.
|
||||
self.tabs[i].scene.bump_entities_with_parametric_policy(
|
||||
&changes,
|
||||
&[first],
|
||||
self.constraint_solve_mode,
|
||||
);
|
||||
self.tabs[i].dirty = true;
|
||||
self.tabs[i].active_cmd = None;
|
||||
self.tabs[i].snap_result = None;
|
||||
self.refresh_properties();
|
||||
if let Some(pd) = pending {
|
||||
self.commit_undo_delta(i, pd);
|
||||
}
|
||||
}
|
||||
CmdResult::AddFixedConstraint(pick) => {
|
||||
use crate::modules::parametric::FixConstraintCommand;
|
||||
use crate::scene::parametric_constraints::{
|
||||
|
||||
Reference in new issue
Block a user