Dimensional constraints: DCFORM, DIMCONSTRAINT options, -PARAMETERS, value prompt, save without display overrides

DCFORM sets the annotational or dynamic form and continues into the
DIMCONSTRAINT option prompt, whose wording and last-used default match the
reference. -PARAMETERS manages parameters from the command line; a
double-clicked constraint dimension opens its value prompt. Dynamic
dimensions rescale on every camera change and their display overrides stay
out of the saved file. A driving dimension between an entity's own ends no
longer has its length retained, so a new value moves the second point along
the dimension axis; parameter edits hold the first constraint point.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
ramox81andClaude Fable 5.1 committed 2026-09-21 15:08:44 +03:00
1 parent aa51765a62
commit 3e28604752
12 files changed
+810 -34

No files matched your search

+267 -7
View File
@@ -3,6 +3,28 @@ use crate::command::{CmdResult, SelectionEntity, StepInput};
use acadrust::Handle;
use iced::Task;
/// `old` as a whole identifier in `source` becomes `new`.
fn replace_identifier(source: &str, old: &str, new: &str) -> String {
let mut out = String::with_capacity(source.len());
let mut token = String::new();
let flush = |token: &mut String, out: &mut String| {
if !token.is_empty() {
out.push_str(if token == old { new } else { token.as_str() });
token.clear();
}
};
for c in source.chars() {
if c.is_alphanumeric() || c == '_' {
token.push(c);
} else {
flush(&mut token, &mut out);
out.push(c);
}
}
flush(&mut token, &mut out);
out
}
impl OpenCADStudio {
pub(super) fn reject_locked_edit(&mut self, i: usize, handle: Handle) -> bool {
let Some(layer) = self.tabs[i].scene.locked_layer_name(handle) else {
@@ -1147,6 +1169,213 @@ impl OpenCADStudio {
}
}
/// `Dynamic` or `Annotational`, the DCFORM setting.
pub(crate) fn constraint_form_name(&self) -> &'static str {
if self.constraint_form_annotational {
"Annotational"
} else {
"Dynamic"
}
}
/// The value prompt a double-clicked constraint dimension opens.
pub(crate) fn dynamic_dimension_value_command(
&self,
i: usize,
handle: Handle,
) -> Option<crate::modules::parametric::DimensionValueCommand> {
use crate::scene::named_parameters::DrivingValue;
let scene = &self.tabs[i].scene;
for set in &scene.parametric_constraints {
let Some((id, _)) = set
.dimensions
.iter()
.find(|(_, dimension)| **dimension == handle)
else {
continue;
};
let constraint = set.get(*id)?;
let Some(DrivingValue::Named(name)) = &constraint.driving_param else {
return None;
};
let table = if set.local_parameters.is_empty() {
scene.named_parameters()
} else {
&set.local_parameters
};
let current = table
.get(name)
.map(|parameter| parameter.source.trim().to_string())
.unwrap_or_default();
return Some(crate::modules::parametric::DimensionValueCommand::new(
name.clone(),
current,
));
}
None
}
/// Applies `expression` or `newname=expression` to parameter `name`:
/// the dimension value prompt and -PARAMETERS Edit.
pub(crate) fn apply_parameter_input(&mut self, i: usize, name: &str, input: &str) {
let input = input.trim();
let (target, expression) = match input.split_once('=') {
Some((new_name, expression)) => {
(new_name.trim().to_string(), expression.trim().to_string())
}
None => (name.to_string(), input.to_string()),
};
if expression.is_empty() {
return;
}
if target != name {
if let Err(error) = self.rename_parameter(i, name, &target) {
self.command_line.push_error(&error);
return;
}
}
let mut table = self.tabs[i].scene.named_parameters().clone();
if let Err(error) = table.set(&target, &expression) {
self.command_line.push_error(&error.to_string());
return;
}
if !table
.resolve(&target)
.is_ok_and(|value| value.is_finite())
{
self.command_line.push_error("Invalid expression.");
return;
}
let pending = self.begin_undo(i, "Edit parameter", 0, true);
self.tabs[i].scene.record_undo_named_parameters_before();
self.tabs[i].scene.named_parameters = table;
self.resolve_named_parameter_edit(i, &target);
self.tabs[i].scene.refresh_dynamic_dimension_texts();
self.tabs[i].dirty = true;
if let Some(pd) = pending {
self.commit_undo_delta(i, pd);
}
}
/// -PARAMETERS New.
pub(crate) fn create_parameter(&mut self, i: usize, name: &str, expression: &str) {
if self.tabs[i].scene.named_parameters().contains(name) {
self.command_line
.push_error(&format!("Parameter {name} already exists."));
return;
}
let mut table = self.tabs[i].scene.named_parameters().clone();
if let Err(error) = table.set(name, expression) {
self.command_line.push_error(&error.to_string());
return;
}
let pending = self.begin_undo(i, "New parameter", 0, true);
self.tabs[i].scene.record_undo_named_parameters_before();
self.tabs[i].scene.named_parameters = table;
self.tabs[i].scene.sync_native_parametric_graph();
self.tabs[i].dirty = true;
if let Some(pd) = pending {
self.commit_undo_delta(i, pd);
}
}
/// Renames a parameter everywhere: the table, the expressions that use
/// it and the constraints it drives.
pub(crate) fn rename_parameter(&mut self, i: usize, old: &str, new: &str) -> Result<(), String> {
use crate::scene::named_parameters::{is_reserved_name, is_valid_name, DrivingValue};
let table = self.tabs[i].scene.named_parameters().clone();
let Some(source) = table.get(old).map(|parameter| parameter.source.clone()) else {
return Err(format!("Parameter {old} not found."));
};
if !is_valid_name(new) || is_reserved_name(new) || table.contains(new) {
return Err(format!("Invalid parameter name {new}."));
}
let mut renamed = table.clone();
renamed.set(new, &source).map_err(|error| error.to_string())?;
for parameter in table.iter() {
if parameter.name == old {
continue;
}
let rewritten = replace_identifier(&parameter.source, old, new);
if rewritten != parameter.source {
renamed
.set(&parameter.name, &rewritten)
.map_err(|error| error.to_string())?;
}
}
renamed.remove(old);
let pending = self.begin_undo(i, "Rename parameter", 0, true);
self.tabs[i].scene.record_undo_named_parameters_before();
self.tabs[i].scene.named_parameters = renamed;
let scopes: Vec<_> = self.tabs[i]
.scene
.parametric_constraints
.iter()
.map(|set| set.scope)
.collect();
for scope in scopes {
let before = self.tabs[i]
.scene
.parametric_constraint_set(scope)
.filter(|set| {
set.constraints.iter().any(|constraint| {
matches!(&constraint.driving_param, Some(DrivingValue::Named(name)) if name == old)
})
})
.cloned();
let Some(before) = before else {
continue;
};
self.tabs[i]
.scene
.record_undo_parametric_constraints_before(scope, before);
let set = self.tabs[i].scene.parametric_constraint_set_mut(scope);
for constraint in &mut set.constraints {
if matches!(&constraint.driving_param, Some(DrivingValue::Named(name)) if name == old) {
constraint.driving_param = Some(DrivingValue::Named(new.to_string()));
}
}
}
self.tabs[i].scene.refresh_dynamic_dimension_texts();
self.tabs[i].scene.sync_native_parametric_graph();
self.tabs[i].dirty = true;
if let Some(pd) = pending {
self.commit_undo_delta(i, pd);
}
Ok(())
}
/// -PARAMETERS Delete: a parameter no constraint drives with.
pub(crate) fn delete_parameter(&mut self, i: usize, name: &str) {
use crate::scene::named_parameters::DrivingValue;
if !self.tabs[i].scene.named_parameters().contains(name) {
self.command_line
.push_error(&format!("Parameter {name} not found."));
return;
}
let in_use = self.tabs[i]
.scene
.parametric_constraints
.iter()
.flat_map(|set| set.constraints.iter())
.any(|constraint| {
matches!(&constraint.driving_param, Some(DrivingValue::Named(used)) if used == name)
});
if in_use {
self.command_line
.push_error(&format!("Parameter {name} is used by a constraint."));
return;
}
let pending = self.begin_undo(i, "Delete parameter", 0, true);
self.tabs[i].scene.record_undo_named_parameters_before();
self.tabs[i].scene.named_parameters_mut().remove(name);
self.tabs[i].scene.sync_native_parametric_graph();
self.tabs[i].dirty = true;
if let Some(pd) = pending {
self.commit_undo_delta(i, pd);
}
}
fn remove_parametric_constraint(
&mut self,
id: crate::scene::parametric_constraints::ConstraintId,
@@ -1344,7 +1573,25 @@ impl OpenCADStudio {
.into_iter()
.map(|h| (h, crate::scene::ChangeKind::Modified))
.collect();
self.tabs[i].scene.bump_entities(&changes);
// 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(n)) if n == name)
})
.filter_map(|c| c.refs.first().copied())
.filter(|reference| reference.marker.is_some())
.collect();
self.tabs[i].scene.bump_entities_with_parametric_policy(
&changes,
&anchors,
self.constraint_solve_mode,
);
}
/// Commits one field of one Parameters-section row (Properties panel) —
@@ -3609,9 +3856,17 @@ impl OpenCADStudio {
return Task::none();
}
};
// DCFORM Annotational: an ordinary plotted dimension that shows
// the value at dimension precision.
let annotational = self.constraint_form_annotational;
let source = if annotational {
format!("{value:.4}")
} else {
expression.clone()
};
let text = dynamic_dimension_text(
&name,
&expression,
&source,
value,
self.tabs[i].scene.constraint_name_format,
);
@@ -3641,11 +3896,13 @@ impl OpenCADStudio {
}
// 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,
};
if !annotational {
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)
@@ -3664,6 +3921,9 @@ impl OpenCADStudio {
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);
if annotational {
self.tabs[i].scene.mark_dimension_annotational(dimension);
}
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);
+134 -4
View File
@@ -1310,6 +1310,7 @@ impl OpenCADStudio {
}
"DCCONVERT" => {
self.dim_constraint_last = "Convert";
let handles = self.tabs[i].scene.selected_handles_in_order();
if handles.is_empty() {
use crate::modules::draw::select::SelectObjectsCommand;
@@ -1508,14 +1509,129 @@ impl OpenCADStudio {
"DIMCONSTRAINT" => {
use crate::modules::parametric::DimConstraintMenuCommand;
// Only the dynamic form exists here.
self.command_line
.push_output("Current settings: Constraint form = Dynamic");
let new_cmd = DimConstraintMenuCommand;
self.command_line.push_output(&format!(
"Current settings: Constraint form = {}",
self.constraint_form_name()
));
let new_cmd = DimConstraintMenuCommand::new(self.dim_constraint_last);
self.command_line.push_info(&new_cmd.prompt());
self.tabs[i].active_cmd = Some(Box::new(new_cmd));
}
"DCFORM" => {
use crate::modules::parametric::ConstraintFormCommand;
let new_cmd = ConstraintFormCommand::new(self.constraint_form_annotational);
self.command_line.push_info(&new_cmd.prompt());
self.tabs[i].active_cmd = Some(Box::new(new_cmd));
}
// DCFORM's answer, then on into DIMCONSTRAINT's options as the
// reference does.
cmd if cmd.starts_with("DCFORM_SET ") => {
use crate::modules::parametric::DimConstraintMenuCommand;
self.constraint_form_annotational = cmd
.trim_start_matches("DCFORM_SET ")
.trim()
.eq_ignore_ascii_case("Annotational");
self.command_line.push_output(&format!(
"Current settings: Constraint form = {}",
self.constraint_form_name()
));
let new_cmd = DimConstraintMenuCommand::new(self.dim_constraint_last);
self.command_line.push_info(&new_cmd.prompt());
self.tabs[i].active_cmd = Some(Box::new(new_cmd));
}
// A dynamic dimension's value prompt (double-click).
cmd if cmd.starts_with("DCVALUE ") => {
let rest = cmd.trim_start_matches("DCVALUE ").trim();
if let Some((name, input)) = rest.split_once(' ') {
self.apply_parameter_input(i, name.trim(), input);
}
}
"-PARAMETERS" => {
use crate::modules::parametric::ParametersCliCommand;
let new_cmd = ParametersCliCommand::new();
self.command_line.push_info(&new_cmd.prompt());
self.tabs[i].active_cmd = Some(Box::new(new_cmd));
}
cmd if cmd.starts_with("-PARAMETERS ") => {
let rest = cmd.trim_start_matches("-PARAMETERS ").trim();
let (op, args) = rest.split_once(' ').unwrap_or((rest, ""));
match op {
"LIST" => {
let rule = "-".repeat(74);
let rows: Vec<String> = {
let table = self.tabs[i].scene.named_parameters();
table
.iter()
.map(|parameter| {
let value = table
.resolve(&parameter.name)
.map(|value| format!("{value:.4}"))
.unwrap_or_else(|_| "**".to_string());
format!(
"Parameter: {:<12} Expression: {:<21} Value: {value}",
parameter.name, parameter.source
)
})
.collect()
};
self.command_line.push_output(&rule);
for row in rows {
self.command_line.push_output(&row);
}
self.command_line.push_output(&rule);
}
"NEW" => {
if let Some((name, expression)) = args.split_once(' ') {
self.create_parameter(i, name.trim(), expression.trim());
}
}
"EDIT" => {
use crate::modules::parametric::ParametersCliCommand;
let name = args.trim().to_string();
let old = {
let table = self.tabs[i].scene.named_parameters();
table.get(&name).map(|parameter| {
let value = table
.resolve(&name)
.map(|value| format!("{value:.4}"))
.unwrap_or_else(|_| "**".to_string());
format!("Old Expression = {}, Value = {value}", parameter.source)
})
};
match old {
Some(line) => {
self.command_line.push_output(&line);
let new_cmd = ParametersCliCommand::edit_expression(name);
self.command_line.push_info(&new_cmd.prompt());
self.tabs[i].active_cmd = Some(Box::new(new_cmd));
}
None => self
.command_line
.push_error(&format!("Parameter {name} not found.")),
}
}
"SET" => {
if let Some((name, expression)) = args.split_once(' ') {
self.apply_parameter_input(i, name.trim(), expression.trim());
}
}
"RENAME" => {
if let Some((old, new)) = args.split_once(' ') {
if let Err(error) = self.rename_parameter(i, old.trim(), new.trim()) {
self.command_line.push_error(&error);
}
}
}
"DELETE" => self.delete_parameter(i, args.trim()),
_ => {}
}
}
"EDCONSTRAINT" => {
use crate::modules::parametric::EqualDistanceConstraintCommand;
let new_cmd = EqualDistanceConstraintCommand::new();
@@ -1765,6 +1881,12 @@ impl OpenCADStudio {
"DCALIGNED" => DimConstraintAxis::Aligned,
_ => DimConstraintAxis::Linear,
};
self.dim_constraint_last = match cmd {
"DCHORIZONTAL" => "Horizontal",
"DCVERTICAL" => "Vertical",
"DCALIGNED" => "Aligned",
_ => "Linear",
};
let name = next_dimensional_parameter_name(self.tabs[i].scene.named_parameters());
// Both constraint points are picked inside the command.
self.tabs[i].scene.deselect_all();
@@ -1774,6 +1896,11 @@ impl OpenCADStudio {
}
"DCONSTRAINT" | "DCRADIUS" | "DCDIAMETER" => {
if cmd == "DCRADIUS" {
self.dim_constraint_last = "Radius";
} else if cmd == "DCDIAMETER" {
self.dim_constraint_last = "Diameter";
}
let handles = self.tabs[i].scene.selected_handles_in_order();
if handles.is_empty() {
use crate::modules::draw::select::SelectObjectsCommand;
@@ -1813,6 +1940,9 @@ impl OpenCADStudio {
}
"ACONSTRAINT" | "DCANGULAR" => {
if cmd == "DCANGULAR" {
self.dim_constraint_last = "ANgular";
}
let handles = self.tabs[i].scene.selected_handles_in_order();
if handles.is_empty() {
use crate::modules::draw::select::SelectObjectsCommand;
+6
View File
@@ -612,6 +612,10 @@ pub(super) struct OpenCADStudio {
pub constraint_solve_mode: bool,
pub constraint_infer: bool,
pub constraint_bar_display: i16,
/// DCFORM: new dimensional constraints use the annotational form.
pub constraint_form_annotational: bool,
/// The dimensional constraint DIMCONSTRAINT offers by default: the last one used.
pub dim_constraint_last: &'static str,
pub constraint_bar_mode: i16,
/// Minutes between autosaves to a `.sv$` recovery file (SAVETIME command);
/// 0 disables autosave.
@@ -3949,6 +3953,8 @@ impl OpenCADStudio {
constraint_solve_mode: true,
constraint_infer: false,
constraint_bar_display: 3,
constraint_form_annotational: false,
dim_constraint_last: "Aligned",
constraint_bar_mode: 4095,
savetime_min: 10,
default_bg_color: None,
+3 -3
View File
@@ -1464,7 +1464,7 @@ impl OpenCADStudio {
self.prepare_native_save(i);
let version = self.tabs[i].scene.document.version;
let snapshot = self.tabs[i].scene.document.clone();
let snapshot = self.tabs[i].scene.document_for_save();
crate::io::save_owned_as_version_atomic(
snapshot,
&path,
@@ -1893,7 +1893,7 @@ impl OpenCADStudio {
path: std::path::PathBuf,
) -> Task<Message> {
let i = self.active_tab;
let document = self.tabs[i].scene.document.clone();
let document = self.tabs[i].scene.document_for_save();
let handles: Vec<_> = self.tabs[i].scene.selected.iter().copied().collect();
let worker_name = block_name.clone();
let worker_path = path.clone();
@@ -2298,7 +2298,7 @@ impl OpenCADStudio {
crate::ui::wrap_bar::dropdown_bounds(crate::app::view::VIEWPORT_CAPTURE_BOUNDS_ID)
});
let clone_started = iced::time::Instant::now();
let mut snapshot = self.tabs[i].scene.document.clone();
let mut snapshot = self.tabs[i].scene.document_for_save();
// Save-As across folders: rebase relative reference paths onto the
// new base dir inside the snapshot only (live strings are untouched).
if purpose == crate::app::SavePurpose::SaveAs {
+9
View File
@@ -5060,6 +5060,15 @@ properties={:.1}ms picked={}",
{
return Task::none();
}
// A constraint dimension edits its parameter from the
// command line, standing in for the reference's in-place
// value editor.
if let Some(prompt) = self.dynamic_dimension_value_command(i, handle) {
use crate::command::CadCommand;
self.command_line.push_info(&prompt.prompt());
self.tabs[i].active_cmd = Some(Box::new(prompt));
return Task::none();
}
// Any text-bearing entity opens its in-place editor
// (plain box or rich MText editor, per type). A
// Leader resolves to the entity it annotates.