forked from 1b-agent/opencadstudio-upstream
Aligned constraint: axis lines, zero collapse, Properties form/reference/description, screen-sized text
Point & line and 2Lines take a text baseline or an ellipse axis as the line; a zero value folds the points onto the first one; 2Lines turns the second line about its start. The Properties Constraint section edits Constraint Form (Dynamic on the *ADSK_CONSTRAINTS layer, Annotational on the current layer), Reference (driven constraint in parentheses whose parameter follows the measurement) and Description (stored with the parameter and the native variable). Dynamic dimensions keep a 12 px text and 8 px arrows at any zoom, drop trailing zeros and show formulas as `fx: len=d1*2`; the hidden constraints layer stays out of the layer lists. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
6969c19746
commit
5fab97a7d0
12 files changed
+476
-45
No files matched your search
@@ -1217,6 +1217,7 @@ impl Allocator<'_> {
|
||||
name: &str,
|
||||
formula: &str,
|
||||
resolved: f64,
|
||||
description: &str,
|
||||
) -> Handle {
|
||||
if let Some(&handle) = self.variables.get(name) {
|
||||
if let Some(ObjectType::Associative(object)) = self.document.objects.get_mut(&handle) {
|
||||
@@ -1244,7 +1245,7 @@ impl Allocator<'_> {
|
||||
name: name.to_string(),
|
||||
expression: formula.to_string(),
|
||||
evaluator: "AcDbCalc:1.0".to_string(),
|
||||
description: String::new(),
|
||||
description: description.to_string(),
|
||||
value: Self::numeric_eval(resolved),
|
||||
has_cached_value: false,
|
||||
cached_value: String::new(),
|
||||
@@ -1883,6 +1884,7 @@ fn driving_value(
|
||||
variable.expression.clone()
|
||||
};
|
||||
if parameters.set(&variable.name, &source).is_ok() {
|
||||
parameters.set_description(&variable.name, &variable.description);
|
||||
Some(DrivingValue::Named(variable.name.clone()))
|
||||
} else {
|
||||
literal
|
||||
@@ -2610,6 +2612,7 @@ fn materialize_scope(
|
||||
variable.action.action_index = index as i32 + 1;
|
||||
if let Some(parameter) = parameters.get(&variable.name) {
|
||||
variable.expression = parameter.source.clone();
|
||||
variable.description = parameters.description(¶meter.name).to_string();
|
||||
if let Ok(resolved) = parameters.resolve(¶meter.name) {
|
||||
variable.value = Allocator::numeric_eval(resolved);
|
||||
}
|
||||
@@ -2621,7 +2624,13 @@ fn materialize_scope(
|
||||
if materialize_all_parameters {
|
||||
for parameter in parameters.iter() {
|
||||
let resolved = parameters.resolve(¶meter.name).unwrap_or(0.0);
|
||||
allocator.variable(network_handle, ¶meter.name, ¶meter.source, resolved);
|
||||
allocator.variable(
|
||||
network_handle,
|
||||
¶meter.name,
|
||||
¶meter.source,
|
||||
resolved,
|
||||
parameters.description(¶meter.name),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2666,7 +2675,13 @@ fn materialize_scope(
|
||||
DrivingValue::Literal(_) => (format!("d{node_id}"), resolved.to_string()),
|
||||
};
|
||||
node_names.insert(node_id, name.clone());
|
||||
let variable_handle = allocator.variable(network_handle, &name, &formula, resolved);
|
||||
let variable_handle = allocator.variable(
|
||||
network_handle,
|
||||
&name,
|
||||
&formula,
|
||||
resolved,
|
||||
parameters.description(&name),
|
||||
);
|
||||
let dependency_id = group_dependencies.len() as i32 + 1;
|
||||
let dep_handle =
|
||||
allocator.value_dependency(group_handle?, variable_handle, resolved, dependency_id);
|
||||
|
||||
+1
-1
@@ -1404,7 +1404,7 @@ impl Scene {
|
||||
|| (!include_preview_hidden && self.preview_hidden.contains(&common.handle))
|
||||
|| self.hidden_dynamic_dimensions.contains(&common.handle)
|
||||
// A dynamic dimension never plots.
|
||||
|| (plot_only && self.is_dynamic_dimension(common.handle))
|
||||
|| (plot_only && self.is_plot_hidden_dynamic_dimension(common.handle))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -474,6 +474,9 @@ pub struct Parameter {
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
pub struct ParameterTable {
|
||||
parameters: Vec<Parameter>,
|
||||
/// The free-text description a parameter carries (the reference's
|
||||
/// Description column), by name.
|
||||
descriptions: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl ParameterTable {
|
||||
@@ -481,6 +484,23 @@ impl ParameterTable {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn description(&self, name: &str) -> &str {
|
||||
self.descriptions.get(name).map_or("", String::as_str)
|
||||
}
|
||||
|
||||
/// Stores `description` for an existing parameter; an empty text clears it.
|
||||
pub fn set_description(&mut self, name: &str, description: &str) {
|
||||
if !self.contains(name) {
|
||||
return;
|
||||
}
|
||||
if description.trim().is_empty() {
|
||||
self.descriptions.remove(name);
|
||||
} else {
|
||||
self.descriptions
|
||||
.insert(name.to_string(), description.trim().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.parameters.is_empty()
|
||||
}
|
||||
@@ -549,6 +569,7 @@ impl ParameterTable {
|
||||
/// need to invent (a future UI stage may still want to warn before
|
||||
/// deleting — that's a UI-layer decision, not a data-layer one).
|
||||
pub fn remove(&mut self, name: &str) -> bool {
|
||||
self.descriptions.remove(name);
|
||||
match self.index_of(name) {
|
||||
Some(i) => {
|
||||
self.parameters.remove(i);
|
||||
|
||||
@@ -364,16 +364,59 @@ pub(crate) fn next_dimensional_parameter_name(
|
||||
/// The constraint-bar label that draws a dynamic dimension's lock mark.
|
||||
pub const DYNAMIC_DIMENSION_GLYPH: &str = "\u{1F512}";
|
||||
|
||||
/// The layer the reference keeps dynamic dimensions on; an annotational
|
||||
/// dimensional constraint sits on an ordinary layer instead.
|
||||
pub(crate) const DYNAMIC_DIMENSION_LAYER: &str = "*ADSK_CONSTRAINTS";
|
||||
|
||||
/// The text a dynamic dimension shows: CONSTRAINTNAMEFORMAT 0 = name,
|
||||
/// 1 = value, 2 = name=value.
|
||||
pub(crate) fn dynamic_dimension_text(name: &str, value: f64, format: u8) -> String {
|
||||
match format {
|
||||
/// 1 = value, 2 = name=value, the value with its trailing zeros dropped
|
||||
/// (`d9=80`, `d1=111.8034`). A parameter defined by a formula shows the
|
||||
/// formula behind `fx:` (`fx: len=d1*2`); a reference (driven) constraint's
|
||||
/// text sits in parentheses.
|
||||
pub(crate) fn dynamic_dimension_text(
|
||||
name: &str,
|
||||
value: f64,
|
||||
format: u8,
|
||||
reference: bool,
|
||||
expression: Option<&str>,
|
||||
) -> String {
|
||||
let formula = expression
|
||||
.map(str::trim)
|
||||
.filter(|source| !source.is_empty() && source.parse::<f64>().is_err());
|
||||
let shown = match formula {
|
||||
Some(source) => source.to_string(),
|
||||
None => {
|
||||
let text = format!("{value:.4}");
|
||||
let text = text.trim_end_matches('0').trim_end_matches('.');
|
||||
if text.is_empty() || text == "-" {
|
||||
"0".to_string()
|
||||
} else {
|
||||
text.to_string()
|
||||
}
|
||||
}
|
||||
};
|
||||
let text = match format {
|
||||
0 => name.to_string(),
|
||||
1 => format!("{value:.4}"),
|
||||
_ => format!("{name}={value:.4}"),
|
||||
1 => shown,
|
||||
_ => format!("{name}={shown}"),
|
||||
};
|
||||
let text = if formula.is_some() {
|
||||
format!("fx: {text}")
|
||||
} else {
|
||||
text
|
||||
};
|
||||
if reference {
|
||||
format!("({text})")
|
||||
} else {
|
||||
text
|
||||
}
|
||||
}
|
||||
|
||||
/// Screen height of a dynamic dimension's text and its arrows, in pixels —
|
||||
/// the reference keeps them readable at any zoom.
|
||||
const DYNAMIC_TEXT_PX: f32 = 12.0;
|
||||
const DYNAMIC_ARROW_PX: f32 = 8.0;
|
||||
|
||||
/// The expression a measured value seeds a parameter with: twelve
|
||||
/// significant digits with the trailing zeros dropped (`100`,
|
||||
/// `111.803398875`), as the reference stores it.
|
||||
@@ -1593,8 +1636,11 @@ impl super::Scene {
|
||||
pub(crate) fn refresh_dynamic_dimension_texts(&mut self) {
|
||||
let format = self.constraint_name_format;
|
||||
let mut updates: Vec<(Handle, String, Option<(Vector3, Vector3)>)> = Vec::new();
|
||||
// A reference (driven) constraint's parameter follows the geometry.
|
||||
let mut followed: Vec<(String, f64)> = Vec::new();
|
||||
for set in &self.parametric_constraints {
|
||||
let table = if set.local_parameters.is_empty() {
|
||||
let model_table = set.local_parameters.is_empty();
|
||||
let table = if model_table {
|
||||
&self.named_parameters
|
||||
} else {
|
||||
&set.local_parameters
|
||||
@@ -1603,13 +1649,6 @@ impl super::Scene {
|
||||
let Some(constraint) = set.get(*id) else {
|
||||
continue;
|
||||
};
|
||||
let (name, value) = match &constraint.driving_param {
|
||||
Some(DrivingValue::Named(name)) => {
|
||||
(name.clone(), table.resolve(name).unwrap_or(f64::NAN))
|
||||
}
|
||||
Some(DrivingValue::Literal(value)) => (String::new(), *value),
|
||||
None => continue,
|
||||
};
|
||||
let world = |reference: &ParametricRef| {
|
||||
let entity = self.document.get_entity(reference.entity)?;
|
||||
resolve_point(entity, reference.marker?)
|
||||
@@ -1618,13 +1657,56 @@ impl super::Scene {
|
||||
[first, second, ..] => world(first).zip(world(second)),
|
||||
_ => None,
|
||||
};
|
||||
let reference = !constraint.enabled;
|
||||
let measured = points.map(|(first, second)| {
|
||||
let delta = second - first;
|
||||
let axis = match self.document.get_entity(*dimension) {
|
||||
Some(acadrust::EntityType::Dimension(
|
||||
acadrust::entities::Dimension::Linear(linear),
|
||||
)) => Some((linear.rotation.cos(), linear.rotation.sin())),
|
||||
_ => None,
|
||||
};
|
||||
match (constraint.kind, axis) {
|
||||
(ConstraintKind::DistanceX, _) => delta.x.abs(),
|
||||
(ConstraintKind::DistanceY, _) => delta.y.abs(),
|
||||
(ConstraintKind::DistanceDirected, Some((ax, ay))) => {
|
||||
(delta.x * ax + delta.y * ay).abs()
|
||||
}
|
||||
_ => delta.length(),
|
||||
}
|
||||
});
|
||||
let (name, value, source) = match &constraint.driving_param {
|
||||
Some(DrivingValue::Named(name)) => {
|
||||
let value = match (reference, measured) {
|
||||
(true, Some(measured)) => {
|
||||
if model_table {
|
||||
followed.push((name.clone(), measured));
|
||||
}
|
||||
measured
|
||||
}
|
||||
_ => table.resolve(name).unwrap_or(f64::NAN),
|
||||
};
|
||||
let source = (!reference)
|
||||
.then(|| table.get(name).map(|parameter| parameter.source.clone()))
|
||||
.flatten();
|
||||
(name.clone(), value, source)
|
||||
}
|
||||
Some(DrivingValue::Literal(value)) => (String::new(), *value, None),
|
||||
None => continue,
|
||||
};
|
||||
updates.push((
|
||||
*dimension,
|
||||
dynamic_dimension_text(&name, value, format),
|
||||
dynamic_dimension_text(&name, value, format, reference, source.as_deref()),
|
||||
points,
|
||||
));
|
||||
}
|
||||
}
|
||||
for (name, measured) in followed {
|
||||
let source = measured_expression(measured);
|
||||
if self.named_parameters.get(&name).map(|p| p.source.as_str()) != Some(source.as_str()) {
|
||||
let _ = self.named_parameters.set(&name, &source);
|
||||
}
|
||||
}
|
||||
for (handle, text, points) in updates {
|
||||
let Some(acadrust::EntityType::Dimension(mut dimension)) =
|
||||
self.document.get_entity(handle).cloned()
|
||||
@@ -1717,6 +1799,63 @@ impl super::Scene {
|
||||
ids.len()
|
||||
}
|
||||
|
||||
/// True for a dynamic-form dimensional constraint dimension — the ones
|
||||
/// on the reference's constraints layer, which never plot; an
|
||||
/// annotational one on an ordinary layer plots like any dimension.
|
||||
pub(crate) fn is_plot_hidden_dynamic_dimension(&self, handle: Handle) -> bool {
|
||||
self.is_dynamic_dimension(handle)
|
||||
&& self
|
||||
.document
|
||||
.get_entity(handle)
|
||||
.is_some_and(|entity| entity.common().layer == DYNAMIC_DIMENSION_LAYER)
|
||||
}
|
||||
|
||||
/// Keeps dynamic dimensions readable at any zoom: their text and arrows
|
||||
/// measure a fixed screen size, as the reference draws them. Runs every
|
||||
/// tick and touches an entity only when the zoom moved it off size.
|
||||
pub(crate) fn refresh_dynamic_dimension_sizes(&mut self) {
|
||||
use crate::entities::dim_override as dov;
|
||||
let Some(wpp) = self.world_per_pixel() else {
|
||||
return;
|
||||
};
|
||||
let text_height = (DYNAMIC_TEXT_PX * wpp) as f64;
|
||||
let arrow = (DYNAMIC_ARROW_PX * wpp) as f64;
|
||||
let handles: Vec<Handle> = self
|
||||
.parametric_constraints
|
||||
.iter()
|
||||
.flat_map(|set| set.dimensions.values().copied())
|
||||
.collect();
|
||||
for handle in handles {
|
||||
let Some(entity) = self.document.get_entity(handle) else {
|
||||
continue;
|
||||
};
|
||||
// An annotational dimension keeps its style's sizes.
|
||||
if entity.common().layer != DYNAMIC_DIMENSION_LAYER {
|
||||
continue;
|
||||
}
|
||||
let current = dov::real(&entity.common().extended_data, dov::DIMTXT);
|
||||
if current.is_some_and(|height| (height - text_height).abs() <= text_height * 0.02) {
|
||||
continue;
|
||||
}
|
||||
let mut entity = entity.clone();
|
||||
dov::set_on_entity(&mut entity, dov::DIMSCALE, Some(acadrust::xdata::XDataValue::Real(1.0)));
|
||||
dov::set_on_entity(&mut entity, dov::DIMTXT, Some(acadrust::xdata::XDataValue::Real(text_height)));
|
||||
dov::set_on_entity(&mut entity, dov::DIMASZ, Some(acadrust::xdata::XDataValue::Real(arrow)));
|
||||
self.update_entity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
/// Makes sure the reference's dynamic dimension layer exists.
|
||||
pub(crate) fn ensure_dynamic_dimension_layer(&mut self) {
|
||||
if self.document.layers.contains(DYNAMIC_DIMENSION_LAYER) {
|
||||
return;
|
||||
}
|
||||
let mut layer = acadrust::tables::Layer::new(DYNAMIC_DIMENSION_LAYER);
|
||||
layer.color = acadrust::types::Color::from_index(7);
|
||||
layer.handle = self.document.allocate_handle();
|
||||
let _ = self.document.layers.add(layer);
|
||||
}
|
||||
|
||||
/// True for a dimension that shows a dimensional constraint.
|
||||
pub(crate) fn is_dynamic_dimension(&self, handle: Handle) -> bool {
|
||||
self.parametric_constraints
|
||||
|
||||
@@ -1625,10 +1625,13 @@ fn build_constraint(
|
||||
distance_direction_type::PARALLEL_TO_LINE
|
||||
| distance_direction_type::PERPENDICULAR_TO_LINE
|
||||
) {
|
||||
if let Some(line) = direction_ref
|
||||
.first()
|
||||
.and_then(|reference| whole_line(sys, cache, *reference))
|
||||
{
|
||||
if let Some(line) = direction_ref.first().and_then(|reference| {
|
||||
// A text baseline or an ellipse axis directs the
|
||||
// distance the same way a line does.
|
||||
whole_line(sys, cache, *reference).or_else(|| {
|
||||
directional_line(sys, cache, *reference).map(|(line, _)| line)
|
||||
})
|
||||
}) {
|
||||
let current = {
|
||||
let store = sys.store();
|
||||
let delta = [
|
||||
@@ -3808,6 +3811,24 @@ impl Scene {
|
||||
self.parametric_constraints[i].dof = None;
|
||||
self.parametric_constraints[i].conflicts.clear();
|
||||
}
|
||||
// A dimensional constraint set to zero means the collapse.
|
||||
let zero_collapse: HashSet<Handle> = {
|
||||
let set = &self.parametric_constraints[i];
|
||||
let params = if set.local_parameters.is_empty() {
|
||||
&self.named_parameters
|
||||
} else {
|
||||
&set.local_parameters
|
||||
};
|
||||
set.constraints
|
||||
.iter()
|
||||
.filter(|c| {
|
||||
c.enabled
|
||||
&& c.kind == ConstraintKind::Distance
|
||||
&& resolved_target(params, c).is_some_and(|v| v.abs() <= f64::EPSILON)
|
||||
})
|
||||
.flat_map(|c| c.refs.iter().map(|r| r.entity))
|
||||
.collect()
|
||||
};
|
||||
for (handle, new_entity) in solved {
|
||||
// An edit the constraints can only satisfy by collapsing the
|
||||
// entity (a rotated line whose start is fixed and direction
|
||||
@@ -3816,7 +3837,8 @@ impl Scene {
|
||||
let new_entity = match originals.get(&handle) {
|
||||
Some(original)
|
||||
if collapsed_by_solve(&new_entity)
|
||||
&& !collapsed_by_solve(original.as_ref()) =>
|
||||
&& !collapsed_by_solve(original.as_ref())
|
||||
&& !zero_collapse.contains(&handle) =>
|
||||
{
|
||||
original.as_ref().clone()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user