M0: truck-based kernel with local fillets and chamfers, parametric document timeline
The kernel moved from monstertruck to truck: truck's booleans handle blind holes, pockets and bosses that monstertruck's did not (see the ADR). Fillets and chamfers on straight convex edges between planar faces are our own exact local B-rep operation. The document holds parameters with unit-aware expressions and a timeline that re-evaluates incrementally. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dafce1f4e2
commit
a97af44979
18 files changed
+2740
-625
No files matched your search
@@ -0,0 +1,583 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solvecraft_geom::{Plane, Vec2, Vec3};
|
||||
use solvecraft_sketch::Sketch;
|
||||
|
||||
use crate::expr::{self, Kind};
|
||||
use crate::{DocError, Result};
|
||||
|
||||
pub const MAX_FEATURES: usize = 10_000;
|
||||
pub const MAX_PARAMS: usize = 10_000;
|
||||
|
||||
/// A named parameter. User parameters are created by the user; model parameters (`d1`, `d2`…)
|
||||
/// are created by dimensions and features.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Param {
|
||||
pub name: String,
|
||||
pub expr: String,
|
||||
/// `mm`, `deg` or empty (unit-less).
|
||||
pub unit: String,
|
||||
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||
pub comment: String,
|
||||
/// Created by a dimension or feature (not by the user).
|
||||
#[serde(default)]
|
||||
pub model: bool,
|
||||
}
|
||||
|
||||
impl Param {
|
||||
pub fn kind(&self) -> Kind {
|
||||
Kind::from_unit(&self.unit)
|
||||
}
|
||||
}
|
||||
|
||||
/// Where a sketch lies.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum PlaneRef {
|
||||
/// `XY`, `XZ` or `YZ`.
|
||||
Origin { name: String },
|
||||
/// An explicit plane (e.g. picked from a planar face).
|
||||
Custom { plane: Plane },
|
||||
/// Another plane offset along its normal by an expression.
|
||||
Offset { base: Box<PlaneRef>, distance: String },
|
||||
}
|
||||
|
||||
/// Which closed profiles of a sketch a feature uses.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum ProfileSel {
|
||||
/// Every profile.
|
||||
All,
|
||||
/// Profiles by index in the solved sketch's profile list.
|
||||
Indices { indices: Vec<usize> },
|
||||
/// Profiles whose outer loop is made of exactly these curves (order-insensitive); stable
|
||||
/// across parameter changes.
|
||||
Curves { loops: Vec<Vec<String>> },
|
||||
/// The profile containing each sketch point.
|
||||
Points { points: Vec<Vec2> },
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Operation {
|
||||
#[default]
|
||||
NewBody,
|
||||
Join,
|
||||
Cut,
|
||||
Intersect,
|
||||
}
|
||||
|
||||
impl Operation {
|
||||
pub fn parse(s: &str) -> Option<Operation> {
|
||||
Some(match s.to_ascii_lowercase().replace([' ', '_', '-'], "").as_str() {
|
||||
"new" | "newbody" | "newcomponent" => Operation::NewBody,
|
||||
"join" | "union" | "add" => Operation::Join,
|
||||
"cut" | "subtract" | "difference" => Operation::Cut,
|
||||
"intersect" | "intersection" => Operation::Intersect,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Direction {
|
||||
/// Along the sketch normal.
|
||||
#[default]
|
||||
Positive,
|
||||
Negative,
|
||||
/// Both sides; `distance` per side.
|
||||
Symmetric,
|
||||
}
|
||||
|
||||
/// Extent of an extrude.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Extent {
|
||||
pub distance: String,
|
||||
#[serde(default)]
|
||||
pub direction: Direction,
|
||||
/// Optional second side distance (two-sided extrude), along the negative normal.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub distance2: Option<String>,
|
||||
/// Start offset from the sketch plane (expression).
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub start_offset: Option<String>,
|
||||
}
|
||||
|
||||
/// Revolve axis.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum AxisRef {
|
||||
/// A line of the profile's sketch.
|
||||
SketchLine { curve: String },
|
||||
/// Sketch x or y axis through the sketch origin (`x` / `y`).
|
||||
SketchAxis { axis: String },
|
||||
/// World X / Y / Z axis through the origin.
|
||||
World { axis: String },
|
||||
}
|
||||
|
||||
/// Feature definitions.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum FeatureKind {
|
||||
Sketch {
|
||||
plane: PlaneRef,
|
||||
sketch: Sketch,
|
||||
},
|
||||
Extrude {
|
||||
/// Feature id of the sketch.
|
||||
sketch: u64,
|
||||
profiles: ProfileSel,
|
||||
extent: Extent,
|
||||
#[serde(default)]
|
||||
operation: Operation,
|
||||
/// Target body names for join/cut/intersect (empty = all bodies).
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
targets: Vec<String>,
|
||||
},
|
||||
Revolve {
|
||||
sketch: u64,
|
||||
profiles: ProfileSel,
|
||||
axis: AxisRef,
|
||||
angle: String,
|
||||
#[serde(default)]
|
||||
operation: Operation,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
targets: Vec<String>,
|
||||
},
|
||||
Fillet {
|
||||
/// Reference points on the edges to round (each re-finds the nearest edge).
|
||||
edges: Vec<Vec3>,
|
||||
radius: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
body: Option<String>,
|
||||
},
|
||||
Chamfer {
|
||||
edges: Vec<Vec3>,
|
||||
distance: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
body: Option<String>,
|
||||
},
|
||||
Box {
|
||||
corner: Vec3,
|
||||
length: String,
|
||||
width: String,
|
||||
height: String,
|
||||
#[serde(default)]
|
||||
operation: Operation,
|
||||
},
|
||||
Cylinder {
|
||||
base: Vec3,
|
||||
#[serde(default = "z_axis")]
|
||||
axis: Vec3,
|
||||
radius: String,
|
||||
height: String,
|
||||
#[serde(default)]
|
||||
operation: Operation,
|
||||
},
|
||||
Sphere {
|
||||
center: Vec3,
|
||||
radius: String,
|
||||
#[serde(default)]
|
||||
operation: Operation,
|
||||
},
|
||||
Torus {
|
||||
center: Vec3,
|
||||
major: String,
|
||||
minor: String,
|
||||
#[serde(default)]
|
||||
operation: Operation,
|
||||
},
|
||||
Combine {
|
||||
target: String,
|
||||
tools: Vec<String>,
|
||||
operation: Operation,
|
||||
#[serde(default)]
|
||||
keep_tools: bool,
|
||||
},
|
||||
Move {
|
||||
bodies: Vec<String>,
|
||||
translate: [String; 3],
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
rotate_axis: Option<Vec3>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
angle: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
fn z_axis() -> Vec3 {
|
||||
Vec3::Z
|
||||
}
|
||||
|
||||
impl FeatureKind {
|
||||
/// Fusion-like type name (timeline tooltip, `inspect`).
|
||||
pub fn type_name(&self) -> &'static str {
|
||||
match self {
|
||||
FeatureKind::Sketch { .. } => "Sketch",
|
||||
FeatureKind::Extrude { .. } => "ExtrudeFeature",
|
||||
FeatureKind::Revolve { .. } => "RevolveFeature",
|
||||
FeatureKind::Fillet { .. } => "FilletFeature",
|
||||
FeatureKind::Chamfer { .. } => "ChamferFeature",
|
||||
FeatureKind::Box { .. } => "BoxFeature",
|
||||
FeatureKind::Cylinder { .. } => "CylinderFeature",
|
||||
FeatureKind::Sphere { .. } => "SphereFeature",
|
||||
FeatureKind::Torus { .. } => "TorusFeature",
|
||||
FeatureKind::Combine { .. } => "CombineFeature",
|
||||
FeatureKind::Move { .. } => "MoveFeature",
|
||||
}
|
||||
}
|
||||
/// Default name prefix (`Extrude` → `Extrude1`).
|
||||
pub fn base_name(&self) -> &'static str {
|
||||
match self {
|
||||
FeatureKind::Sketch { .. } => "Sketch",
|
||||
FeatureKind::Extrude { .. } => "Extrude",
|
||||
FeatureKind::Revolve { .. } => "Revolve",
|
||||
FeatureKind::Fillet { .. } => "Fillet",
|
||||
FeatureKind::Chamfer { .. } => "Chamfer",
|
||||
FeatureKind::Box { .. } => "Box",
|
||||
FeatureKind::Cylinder { .. } => "Cylinder",
|
||||
FeatureKind::Sphere { .. } => "Sphere",
|
||||
FeatureKind::Torus { .. } => "Torus",
|
||||
FeatureKind::Combine { .. } => "Combine",
|
||||
FeatureKind::Move { .. } => "Move",
|
||||
}
|
||||
}
|
||||
/// Every expression the feature uses.
|
||||
pub fn expressions(&self) -> Vec<&str> {
|
||||
let mut v: Vec<&str> = Vec::new();
|
||||
match self {
|
||||
FeatureKind::Sketch { plane, .. } => {
|
||||
let mut p = plane;
|
||||
while let PlaneRef::Offset { base, distance } = p {
|
||||
v.push(distance);
|
||||
p = base;
|
||||
}
|
||||
}
|
||||
FeatureKind::Extrude { extent, .. } => {
|
||||
v.push(&extent.distance);
|
||||
if let Some(d) = &extent.distance2 {
|
||||
v.push(d);
|
||||
}
|
||||
if let Some(d) = &extent.start_offset {
|
||||
v.push(d);
|
||||
}
|
||||
}
|
||||
FeatureKind::Revolve { angle, .. } => v.push(angle),
|
||||
FeatureKind::Fillet { radius, .. } => v.push(radius),
|
||||
FeatureKind::Chamfer { distance, .. } => v.push(distance),
|
||||
FeatureKind::Box { length, width, height, .. } => v.extend([length.as_str(), width, height]),
|
||||
FeatureKind::Cylinder { radius, height, .. } => v.extend([radius.as_str(), height]),
|
||||
FeatureKind::Sphere { radius, .. } => v.push(radius),
|
||||
FeatureKind::Torus { major, minor, .. } => v.extend([major.as_str(), minor]),
|
||||
FeatureKind::Combine { .. } => {}
|
||||
FeatureKind::Move { translate, angle, .. } => {
|
||||
v.extend(translate.iter().map(String::as_str));
|
||||
if let Some(a) = angle {
|
||||
v.push(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Feature {
|
||||
pub id: u64,
|
||||
pub name: String,
|
||||
#[serde(default)]
|
||||
pub suppressed: bool,
|
||||
/// Names for the bodies this feature creates (in order); missing ones get `BodyN`.
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub body_names: Vec<String>,
|
||||
#[serde(flatten)]
|
||||
pub kind: FeatureKind,
|
||||
}
|
||||
|
||||
/// The document.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Document {
|
||||
#[serde(default = "default_format")]
|
||||
pub format: String,
|
||||
pub name: String,
|
||||
#[serde(default = "default_units")]
|
||||
pub units: String,
|
||||
pub params: Vec<Param>,
|
||||
pub features: Vec<Feature>,
|
||||
/// Timeline marker: features at or after this index are rolled back (not evaluated).
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub marker: Option<usize>,
|
||||
#[serde(default)]
|
||||
pub next_id: u64,
|
||||
}
|
||||
|
||||
fn default_format() -> String {
|
||||
"solvecraft/1".into()
|
||||
}
|
||||
fn default_units() -> String {
|
||||
"mm".into()
|
||||
}
|
||||
|
||||
impl Default for Document {
|
||||
fn default() -> Self {
|
||||
Document::new("Untitled")
|
||||
}
|
||||
}
|
||||
|
||||
impl Document {
|
||||
pub fn new(name: &str) -> Self {
|
||||
Document {
|
||||
format: default_format(),
|
||||
name: name.into(),
|
||||
units: default_units(),
|
||||
params: Vec::new(),
|
||||
features: Vec::new(),
|
||||
marker: None,
|
||||
next_id: 1,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_json(s: &str) -> Result<Document> {
|
||||
let d: Document = serde_json::from_str(s).map_err(|e| DocError::Invalid(format!("document: {e}")))?;
|
||||
if d.features.len() > MAX_FEATURES || d.params.len() > MAX_PARAMS {
|
||||
return Err(DocError::Invalid("document too large".into()));
|
||||
}
|
||||
Ok(d)
|
||||
}
|
||||
pub fn to_json(&self) -> String {
|
||||
serde_json::to_string_pretty(self).unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn feature(&self, id: u64) -> Option<&Feature> {
|
||||
self.features.iter().find(|f| f.id == id)
|
||||
}
|
||||
pub fn feature_mut(&mut self, id: u64) -> Option<&mut Feature> {
|
||||
self.features.iter_mut().find(|f| f.id == id)
|
||||
}
|
||||
pub fn feature_index(&self, id: u64) -> Option<usize> {
|
||||
self.features.iter().position(|f| f.id == id)
|
||||
}
|
||||
/// Find a feature by id (number) or name.
|
||||
pub fn find_feature(&self, r: &str) -> Option<&Feature> {
|
||||
if let Ok(id) = r.parse::<u64>()
|
||||
&& let Some(f) = self.feature(id)
|
||||
{
|
||||
return Some(f);
|
||||
}
|
||||
self.features.iter().find(|f| f.name.eq_ignore_ascii_case(r))
|
||||
}
|
||||
|
||||
fn unique_name(&self, base: &str) -> String {
|
||||
let mut n = 1;
|
||||
loop {
|
||||
let name = format!("{base}{n}");
|
||||
if !self.features.iter().any(|f| f.name == name) {
|
||||
return name;
|
||||
}
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Insert a feature at the marker (or the end) and return its id.
|
||||
pub fn add_feature(&mut self, kind: FeatureKind, name: Option<&str>) -> Result<u64> {
|
||||
if self.features.len() >= MAX_FEATURES {
|
||||
return Err(DocError::Invalid("too many features".into()));
|
||||
}
|
||||
let id = self.next_id.max(1);
|
||||
self.next_id = id + 1;
|
||||
let name = match name {
|
||||
Some(n) if !n.trim().is_empty() => n.trim().to_string(),
|
||||
_ => self.unique_name(kind.base_name()),
|
||||
};
|
||||
let f = Feature { id, name, suppressed: false, body_names: Vec::new(), kind };
|
||||
match self.marker {
|
||||
Some(m) if m < self.features.len() => {
|
||||
self.features.insert(m, f);
|
||||
self.marker = Some(m + 1);
|
||||
}
|
||||
_ => self.features.push(f),
|
||||
}
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
/// Delete a feature (and, for a sketch, the features that use it).
|
||||
pub fn delete_feature(&mut self, id: u64) -> Result<Vec<u64>> {
|
||||
let idx = self.feature_index(id).ok_or_else(|| DocError::Unknown(format!("feature {id}")))?;
|
||||
let mut gone = vec![id];
|
||||
for f in &self.features {
|
||||
match &f.kind {
|
||||
FeatureKind::Extrude { sketch, .. } | FeatureKind::Revolve { sketch, .. } if *sketch == id => gone.push(f.id),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
self.features.retain(|f| !gone.contains(&f.id));
|
||||
if let Some(m) = self.marker
|
||||
&& m > idx
|
||||
{
|
||||
self.marker = Some(m.saturating_sub(gone.len()).max(idx));
|
||||
}
|
||||
// Drop model parameters nobody references any more.
|
||||
self.prune_model_params();
|
||||
Ok(gone)
|
||||
}
|
||||
|
||||
/// Remove model parameters not referenced by any dimension, feature or other parameter.
|
||||
pub fn prune_model_params(&mut self) {
|
||||
let used = self.referenced_names();
|
||||
self.params.retain(|p| !p.model || used.contains(&p.name));
|
||||
}
|
||||
|
||||
fn referenced_names(&self) -> Vec<String> {
|
||||
let mut used: Vec<String> = Vec::new();
|
||||
for f in &self.features {
|
||||
for e in f.kind.expressions() {
|
||||
used.extend(expr::references(e));
|
||||
}
|
||||
if let FeatureKind::Sketch { sketch, .. } = &f.kind {
|
||||
used.extend(sketch.constraints.iter().filter_map(|c| c.param.clone()));
|
||||
}
|
||||
}
|
||||
for p in &self.params {
|
||||
used.extend(expr::references(&p.expr));
|
||||
}
|
||||
used
|
||||
}
|
||||
|
||||
pub fn param(&self, name: &str) -> Option<&Param> {
|
||||
self.params.iter().find(|p| p.name == name)
|
||||
}
|
||||
|
||||
fn valid_name(name: &str) -> bool {
|
||||
let mut cs = name.chars();
|
||||
matches!(cs.next(), Some(c) if c.is_alphabetic() || c == '_')
|
||||
&& cs.all(|c| c.is_alphanumeric() || c == '_')
|
||||
&& !matches!(name, "mm" | "cm" | "m" | "um" | "in" | "ft" | "deg" | "rad" | "pi" | "PI")
|
||||
&& name.len() <= 64
|
||||
}
|
||||
|
||||
/// Add or change a parameter. Fails if the result would not evaluate.
|
||||
pub fn set_param(&mut self, name: &str, expr_s: &str, unit: Option<&str>, comment: Option<&str>) -> Result<()> {
|
||||
if !Self::valid_name(name) {
|
||||
return Err(DocError::Invalid(format!("`{name}` is not a valid parameter name")));
|
||||
}
|
||||
if expr_s.len() > 4096 {
|
||||
return Err(DocError::Expr("expression too long".into()));
|
||||
}
|
||||
let mut next = self.clone();
|
||||
match next.params.iter_mut().find(|p| p.name == name) {
|
||||
Some(p) => {
|
||||
p.expr = expr_s.to_string();
|
||||
if let Some(u) = unit {
|
||||
p.unit = u.to_string();
|
||||
}
|
||||
if let Some(c) = comment {
|
||||
p.comment = c.to_string();
|
||||
}
|
||||
}
|
||||
None => {
|
||||
if next.params.len() >= MAX_PARAMS {
|
||||
return Err(DocError::Invalid("too many parameters".into()));
|
||||
}
|
||||
next.params.push(Param {
|
||||
name: name.into(),
|
||||
expr: expr_s.into(),
|
||||
unit: unit.unwrap_or("mm").into(),
|
||||
comment: comment.unwrap_or_default().into(),
|
||||
model: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
let (_, errors) = next.param_values();
|
||||
if let Some(e) = errors.get(name) {
|
||||
return Err(DocError::Expr(e.clone()));
|
||||
}
|
||||
*self = next;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Create the next free model parameter `dN` with the given expression and unit.
|
||||
pub fn new_model_param(&mut self, expr_s: &str, unit: &str) -> String {
|
||||
let mut n = 1;
|
||||
let name = loop {
|
||||
let c = format!("d{n}");
|
||||
if self.param(&c).is_none() {
|
||||
break c;
|
||||
}
|
||||
n += 1;
|
||||
};
|
||||
self.params.push(Param { name: name.clone(), expr: expr_s.into(), unit: unit.into(), comment: String::new(), model: true });
|
||||
name
|
||||
}
|
||||
|
||||
/// Remove a user parameter (fails while something references it).
|
||||
pub fn remove_param(&mut self, name: &str) -> Result<()> {
|
||||
if self.param(name).is_none() {
|
||||
return Err(DocError::Unknown(format!("parameter `{name}`")));
|
||||
}
|
||||
let mut probe = self.clone();
|
||||
probe.params.retain(|p| p.name != name);
|
||||
if probe.referenced_names().iter().any(|n| n == name) {
|
||||
return Err(DocError::Invalid(format!("`{name}` is still used")));
|
||||
}
|
||||
*self = probe;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Evaluate all parameters: values (mm / rad / unit-less) and errors by name.
|
||||
pub fn param_values(&self) -> (std::collections::BTreeMap<String, expr::Value>, std::collections::BTreeMap<String, String>) {
|
||||
let list: Vec<(String, String, Kind)> = self.params.iter().map(|p| (p.name.clone(), p.expr.clone(), p.kind())).collect();
|
||||
expr::eval_params(&list)
|
||||
}
|
||||
|
||||
/// Evaluate an expression against the document's parameters.
|
||||
pub fn eval(&self, e: &str, kind: Kind) -> Result<f64> {
|
||||
let (vals, _) = self.param_values();
|
||||
Self::eval_in(&vals, e, kind)
|
||||
}
|
||||
|
||||
pub fn eval_in(vals: &std::collections::BTreeMap<String, expr::Value>, e: &str, kind: Kind) -> Result<f64> {
|
||||
expr::eval_with(e, &|n| vals.get(n).copied().ok_or_else(|| DocError::Expr(format!("unknown parameter `{n}`"))))?.to_kind(kind)
|
||||
}
|
||||
|
||||
/// Resolve a plane reference.
|
||||
pub fn resolve_plane(&self, vals: &std::collections::BTreeMap<String, expr::Value>, p: &PlaneRef, depth: usize) -> Result<Plane> {
|
||||
if depth > 32 {
|
||||
return Err(DocError::Invalid("plane offsets nested too deeply".into()));
|
||||
}
|
||||
match p {
|
||||
PlaneRef::Origin { name } => Plane::named(name).ok_or_else(|| DocError::Unknown(format!("plane `{name}`"))),
|
||||
PlaneRef::Custom { plane } => Plane::new(plane.origin, plane.x, plane.y).ok_or_else(|| DocError::Invalid("degenerate plane".into())),
|
||||
PlaneRef::Offset { base, distance } => {
|
||||
let b = self.resolve_plane(vals, base, depth + 1)?;
|
||||
Ok(b.offset(Self::eval_in(vals, distance, Kind::Length)?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Mutable access to a sketch feature's sketch.
|
||||
pub fn sketch_mut(&mut self, id: u64) -> Result<&mut Sketch> {
|
||||
match self.feature_mut(id).map(|f| &mut f.kind) {
|
||||
Some(FeatureKind::Sketch { sketch, .. }) => Ok(sketch),
|
||||
Some(_) => Err(DocError::Invalid(format!("feature {id} is not a sketch"))),
|
||||
None => Err(DocError::Unknown(format!("sketch {id}"))),
|
||||
}
|
||||
}
|
||||
pub fn sketch(&self, id: u64) -> Result<&Sketch> {
|
||||
match self.feature(id).map(|f| &f.kind) {
|
||||
Some(FeatureKind::Sketch { sketch, .. }) => Ok(sketch),
|
||||
Some(_) => Err(DocError::Invalid(format!("feature {id} is not a sketch"))),
|
||||
None => Err(DocError::Unknown(format!("sketch {id}"))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply parameter values to every dimension in a sketch (in place).
|
||||
pub fn apply_dimension_values(&self, vals: &std::collections::BTreeMap<String, expr::Value>, sk: &mut Sketch) -> Result<()> {
|
||||
for c in &mut sk.constraints {
|
||||
if let Some(p) = &c.param {
|
||||
let kind = if c.kind.is_angle() { Kind::Angle } else { Kind::Length };
|
||||
let v = vals.get(p).copied().ok_or_else(|| DocError::Unknown(format!("parameter `{p}`")))?.to_kind(kind)?;
|
||||
c.kind.set_value(v);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,545 @@
|
||||
//! Timeline evaluation.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
use serde::Serialize;
|
||||
use solvecraft_geom::{Mesh, Plane, Region2, Vec2, Vec3};
|
||||
use solvecraft_kernel::{self as kernel, Body, BoolOp};
|
||||
use solvecraft_sketch::{CurveKind, Profile, SolveReport, find_profiles, solve};
|
||||
|
||||
use crate::expr::{self, Kind, Value};
|
||||
use crate::{AxisRef, DocError, Document, Feature, FeatureKind, Operation, ProfileSel, Result};
|
||||
|
||||
/// A body in the evaluated model.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModelBody {
|
||||
pub name: String,
|
||||
pub body: Body,
|
||||
/// Feature that created it.
|
||||
pub feature: u64,
|
||||
mesh: OnceLock<Arc<Mesh>>,
|
||||
}
|
||||
|
||||
impl ModelBody {
|
||||
pub fn new(name: String, body: Body, feature: u64) -> Self {
|
||||
ModelBody { name, body, feature, mesh: OnceLock::new() }
|
||||
}
|
||||
/// Display mesh (chord tolerance 1/1000 of the body size), computed once.
|
||||
pub fn mesh(&self) -> Arc<Mesh> {
|
||||
self.mesh
|
||||
.get_or_init(|| {
|
||||
let tol = (self.body.size() * 1e-3).max(1e-3);
|
||||
Arc::new(self.body.tessellate(tol).unwrap_or_default())
|
||||
})
|
||||
.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// A solved sketch in the evaluated model.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SolvedSketch {
|
||||
pub feature: u64,
|
||||
pub name: String,
|
||||
pub plane: Plane,
|
||||
pub sketch: solvecraft_sketch::Sketch,
|
||||
pub report: SolveReport,
|
||||
pub profiles: Vec<Profile>,
|
||||
}
|
||||
|
||||
/// Model after some prefix of the timeline.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct ModelState {
|
||||
pub bodies: Vec<ModelBody>,
|
||||
pub sketches: Vec<SolvedSketch>,
|
||||
pub body_counter: usize,
|
||||
}
|
||||
|
||||
impl ModelState {
|
||||
pub fn body(&self, name: &str) -> Option<&ModelBody> {
|
||||
self.bodies.iter().find(|b| b.name == name)
|
||||
}
|
||||
pub fn sketch(&self, feature: u64) -> Option<&SolvedSketch> {
|
||||
self.sketches.iter().find(|s| s.feature == feature)
|
||||
}
|
||||
}
|
||||
|
||||
/// Evaluation result of one feature.
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct FeatureResult {
|
||||
pub id: u64,
|
||||
pub name: String,
|
||||
pub error: Option<String>,
|
||||
pub warning: Option<String>,
|
||||
pub ms: f64,
|
||||
pub skipped: bool,
|
||||
#[serde(skip)]
|
||||
fingerprint: u64,
|
||||
#[serde(skip)]
|
||||
pub state: Arc<ModelState>,
|
||||
}
|
||||
|
||||
/// Incremental evaluator: holds the state after every feature.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Model {
|
||||
pub results: Vec<FeatureResult>,
|
||||
pub param_errors: BTreeMap<String, String>,
|
||||
empty: Arc<ModelState>,
|
||||
/// Features recomputed by the last `evaluate`.
|
||||
pub last_recomputed: usize,
|
||||
}
|
||||
|
||||
fn fingerprint(prev: u64, f: &Feature, vals: &BTreeMap<String, Value>, rolled_back: bool) -> u64 {
|
||||
let mut h = std::collections::hash_map::DefaultHasher::new();
|
||||
prev.hash(&mut h);
|
||||
rolled_back.hash(&mut h);
|
||||
serde_json::to_string(f).unwrap_or_default().hash(&mut h);
|
||||
let mut names: Vec<String> = f.kind.expressions().iter().flat_map(|e| expr::references(e)).collect();
|
||||
if let FeatureKind::Sketch { sketch, .. } = &f.kind {
|
||||
names.extend(sketch.constraints.iter().filter_map(|c| c.param.clone()));
|
||||
}
|
||||
names.sort();
|
||||
names.dedup();
|
||||
for n in names {
|
||||
n.hash(&mut h);
|
||||
match vals.get(&n) {
|
||||
Some(v) => {
|
||||
v.v.to_bits().hash(&mut h);
|
||||
v.len.hash(&mut h);
|
||||
v.ang.hash(&mut h);
|
||||
}
|
||||
None => 0u8.hash(&mut h),
|
||||
}
|
||||
}
|
||||
h.finish()
|
||||
}
|
||||
|
||||
fn now() -> std::time::Instant {
|
||||
std::time::Instant::now()
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub fn new() -> Self {
|
||||
Model::default()
|
||||
}
|
||||
|
||||
/// The final model state.
|
||||
pub fn state(&self) -> Arc<ModelState> {
|
||||
self.results.iter().rev().find(|r| !r.skipped).map(|r| r.state.clone()).unwrap_or_else(|| self.empty.clone())
|
||||
}
|
||||
|
||||
/// State just before the feature with this id (for editing a sketch in context).
|
||||
pub fn state_before(&self, id: u64) -> Arc<ModelState> {
|
||||
let Some(i) = self.results.iter().position(|r| r.id == id) else { return self.state() };
|
||||
self.results.get(..i).and_then(|s| s.iter().rev().find(|r| !r.skipped)).map(|r| r.state.clone()).unwrap_or_else(|| self.empty.clone())
|
||||
}
|
||||
|
||||
pub fn result(&self, id: u64) -> Option<&FeatureResult> {
|
||||
self.results.iter().find(|r| r.id == id)
|
||||
}
|
||||
|
||||
/// Re-evaluate the document, reusing results up to the first changed feature.
|
||||
pub fn evaluate(&mut self, doc: &Document) {
|
||||
let (vals, perr) = doc.param_values();
|
||||
self.param_errors = perr;
|
||||
let mut prev_fp = 0u64;
|
||||
let mut state = self.empty.clone();
|
||||
let mut out: Vec<FeatureResult> = Vec::with_capacity(doc.features.len());
|
||||
let mut reuse = true;
|
||||
let mut recomputed = 0;
|
||||
let marker = doc.marker.unwrap_or(usize::MAX);
|
||||
for (i, f) in doc.features.iter().enumerate() {
|
||||
let rolled_back = i >= marker;
|
||||
let fp = fingerprint(prev_fp, f, &vals, rolled_back);
|
||||
prev_fp = fp;
|
||||
if reuse
|
||||
&& let Some(old) = self.results.get(i)
|
||||
&& old.id == f.id
|
||||
&& old.fingerprint == fp
|
||||
{
|
||||
if !old.skipped {
|
||||
state = old.state.clone();
|
||||
}
|
||||
out.push(old.clone());
|
||||
continue;
|
||||
}
|
||||
reuse = false;
|
||||
if f.suppressed || rolled_back {
|
||||
out.push(FeatureResult {
|
||||
id: f.id,
|
||||
name: f.name.clone(),
|
||||
error: None,
|
||||
warning: None,
|
||||
ms: 0.0,
|
||||
skipped: true,
|
||||
fingerprint: fp,
|
||||
state: state.clone(),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
recomputed += 1;
|
||||
let t0 = now();
|
||||
let mut next = (*state).clone();
|
||||
let mut warning = None;
|
||||
let r = kernel::guard(&f.name, || {
|
||||
eval_feature(doc, &vals, f, &mut next, &mut warning).map_err(|e| kernel::KernelError::Failed(e.to_string()))
|
||||
});
|
||||
let ms = t0.elapsed().as_secs_f64() * 1000.0;
|
||||
let error = match r {
|
||||
Ok(()) => {
|
||||
state = Arc::new(next);
|
||||
None
|
||||
}
|
||||
Err(e) => Some(e.to_string().trim_start_matches("the operation failed: ").to_string()),
|
||||
};
|
||||
out.push(FeatureResult { id: f.id, name: f.name.clone(), error, warning, ms, skipped: false, fingerprint: fp, state: state.clone() });
|
||||
}
|
||||
self.results = out;
|
||||
self.last_recomputed = recomputed;
|
||||
}
|
||||
}
|
||||
|
||||
fn val(vals: &BTreeMap<String, Value>, e: &str, k: Kind) -> Result<f64> {
|
||||
Document::eval_in(vals, e, k)
|
||||
}
|
||||
|
||||
fn select_profiles(ss: &SolvedSketch, sel: &ProfileSel) -> Result<Vec<Region2>> {
|
||||
let ps = &ss.profiles;
|
||||
if ps.is_empty() {
|
||||
return Err(DocError::Invalid(format!("{} has no closed profiles", ss.name)));
|
||||
}
|
||||
let out: Vec<Region2> = match sel {
|
||||
ProfileSel::All => ps.iter().map(|p| p.region.clone()).collect(),
|
||||
ProfileSel::Indices { indices } => {
|
||||
let mut v = Vec::new();
|
||||
for i in indices {
|
||||
v.push(ps.get(*i).ok_or_else(|| DocError::Unknown(format!("profile {i} of {}", ss.name)))?.region.clone());
|
||||
}
|
||||
v
|
||||
}
|
||||
ProfileSel::Curves { loops } => {
|
||||
let mut v = Vec::new();
|
||||
for l in loops {
|
||||
let mut want: Vec<&String> = l.iter().collect();
|
||||
want.sort();
|
||||
let found = ps.iter().find(|p| {
|
||||
let mut have: Vec<&String> = p.outer_curves.iter().collect();
|
||||
have.sort();
|
||||
have == want
|
||||
});
|
||||
v.push(found.ok_or_else(|| DocError::Unknown(format!("profile bounded by {l:?} in {}", ss.name)))?.region.clone());
|
||||
}
|
||||
v
|
||||
}
|
||||
ProfileSel::Points { points } => {
|
||||
let mut v = Vec::new();
|
||||
for p in points {
|
||||
// Innermost profile containing the point.
|
||||
let found = ps.iter().filter(|q| q.region.contains(*p)).min_by(|a, b| a.area.total_cmp(&b.area));
|
||||
v.push(found.ok_or_else(|| DocError::Unknown(format!("profile at {:?} in {}", [p.x, p.y], ss.name)))?.region.clone());
|
||||
}
|
||||
v
|
||||
}
|
||||
};
|
||||
if out.is_empty() {
|
||||
return Err(DocError::Invalid("no profile selected".into()));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn new_name(state: &mut ModelState, f: &Feature, k: usize) -> String {
|
||||
if let Some(n) = f.body_names.get(k)
|
||||
&& !n.is_empty()
|
||||
&& state.body(n).is_none()
|
||||
{
|
||||
return n.clone();
|
||||
}
|
||||
loop {
|
||||
state.body_counter += 1;
|
||||
let n = format!("Body{}", state.body_counter);
|
||||
if state.body(&n).is_none() {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Add tool bodies to the model according to the operation.
|
||||
fn apply_op(state: &mut ModelState, f: &Feature, tools: Vec<Body>, op: Operation, targets: &[String]) -> Result<()> {
|
||||
let target_idx: Vec<usize> = if targets.is_empty() {
|
||||
(0..state.bodies.len()).collect()
|
||||
} else {
|
||||
let mut v = Vec::new();
|
||||
for t in targets {
|
||||
v.push(state.bodies.iter().position(|b| &b.name == t).ok_or_else(|| DocError::Unknown(format!("body `{t}`")))?);
|
||||
}
|
||||
v
|
||||
};
|
||||
if op == Operation::NewBody || target_idx.is_empty() {
|
||||
if op == Operation::Cut || op == Operation::Intersect {
|
||||
return Err(DocError::Invalid("there is no body to cut or intersect".into()));
|
||||
}
|
||||
for (k, b) in tools.into_iter().enumerate() {
|
||||
let name = new_name(state, f, k);
|
||||
state.bodies.push(ModelBody::new(name, b, f.id));
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
match op {
|
||||
Operation::Join => {
|
||||
let Some(&first) = target_idx.first() else { return Ok(()) };
|
||||
let Some(mut acc) = state.bodies.get(first).map(|b| b.body.clone()) else { return Ok(()) };
|
||||
for t in &tools {
|
||||
acc = kernel::boolean(&acc, t, BoolOp::Union)?.ok_or_else(|| DocError::Invalid("join produced nothing".into()))?;
|
||||
}
|
||||
if let Some(mb) = state.bodies.get_mut(first) {
|
||||
*mb = ModelBody::new(mb.name.clone(), acc, mb.feature);
|
||||
}
|
||||
}
|
||||
Operation::Cut | Operation::Intersect => {
|
||||
let bop = if op == Operation::Cut { BoolOp::Cut } else { BoolOp::Intersect };
|
||||
let mut remove = Vec::new();
|
||||
for &ti in &target_idx {
|
||||
let Some(mut acc) = state.bodies.get(ti).map(|b| Some(b.body.clone())) else { continue };
|
||||
for t in &tools {
|
||||
acc = match acc {
|
||||
Some(a) => kernel::boolean(&a, t, bop)?,
|
||||
None => None,
|
||||
};
|
||||
}
|
||||
match acc {
|
||||
Some(b) => {
|
||||
if let Some(mb) = state.bodies.get_mut(ti) {
|
||||
*mb = ModelBody::new(mb.name.clone(), b, mb.feature);
|
||||
}
|
||||
}
|
||||
None => remove.push(ti),
|
||||
}
|
||||
}
|
||||
let mut i = 0;
|
||||
state.bodies.retain(|_| {
|
||||
let keep = !remove.contains(&i);
|
||||
i += 1;
|
||||
keep
|
||||
});
|
||||
}
|
||||
Operation::NewBody => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sample points inside a region (sketch coordinates).
|
||||
fn region_samples(r: &Region2) -> Vec<Vec2> {
|
||||
let mut out = vec![r.interior_point()];
|
||||
let pts = r.outer.polyline(1e-2);
|
||||
let (mut lo, mut hi) = (Vec2::new(f64::INFINITY, f64::INFINITY), Vec2::new(f64::NEG_INFINITY, f64::NEG_INFINITY));
|
||||
for p in &pts {
|
||||
lo = Vec2::new(lo.x.min(p.x), lo.y.min(p.y));
|
||||
hi = Vec2::new(hi.x.max(p.x), hi.y.max(p.y));
|
||||
}
|
||||
for i in 0..5 {
|
||||
for j in 0..5 {
|
||||
let p = Vec2::new(lo.x + (hi.x - lo.x) * (0.1 + 0.2 * i as f64), lo.y + (hi.y - lo.y) * (0.1 + 0.2 * j as f64));
|
||||
if r.contains(p) {
|
||||
out.push(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Booleans struggle with coincident faces (a cut starting on the face it cuts, a boss
|
||||
/// starting on the face it joins). Extending the tool past such an end changes nothing when the
|
||||
/// space beyond is empty (cut) or already material (join), so do exactly that.
|
||||
fn extend_for_coplanar(st: &ModelState, plane: &Plane, r: &Region2, lo: f64, hi: f64, op: Operation, targets: &[String]) -> (f64, f64) {
|
||||
if !matches!(op, Operation::Cut | Operation::Join) || st.bodies.is_empty() {
|
||||
return (lo, hi);
|
||||
}
|
||||
let bodies: Vec<&ModelBody> = st.bodies.iter().filter(|b| targets.is_empty() || targets.contains(&b.name)).collect();
|
||||
if bodies.is_empty() {
|
||||
return (lo, hi);
|
||||
}
|
||||
let size = bodies.iter().map(|b| b.body.size()).fold(0.0, f64::max);
|
||||
let delta = (size * 0.01).max(1e-3).min((hi - lo).abs().max(1e-3));
|
||||
let meshes: Vec<Arc<Mesh>> = bodies.iter().map(|b| b.mesh()).collect();
|
||||
let samples = region_samples(r);
|
||||
let n = plane.normal();
|
||||
let inside_any = |p: Vec3| meshes.iter().any(|m| m.contains(p));
|
||||
let ok_beyond = |h: f64, dir: f64| {
|
||||
samples.iter().all(|s| {
|
||||
let base = plane.to_world(*s);
|
||||
[0.5, 1.0].iter().all(|k| {
|
||||
let p = base + n * (h + dir * delta * k);
|
||||
let inside = inside_any(p);
|
||||
if op == Operation::Cut { !inside } else { inside }
|
||||
})
|
||||
})
|
||||
};
|
||||
let lo2 = if ok_beyond(lo, -1.0) { lo - delta } else { lo };
|
||||
let hi2 = if ok_beyond(hi, 1.0) { hi + delta } else { hi };
|
||||
(lo2, hi2)
|
||||
}
|
||||
|
||||
/// The body a fillet/chamfer applies to: by name, or the one closest to the first edge point.
|
||||
fn blend_target(state: &ModelState, body: &Option<String>, edges: &[Vec3]) -> Result<usize> {
|
||||
if let Some(n) = body {
|
||||
return state.bodies.iter().position(|b| &b.name == n).ok_or_else(|| DocError::Unknown(format!("body `{n}`")));
|
||||
}
|
||||
let p = edges.first().ok_or_else(|| DocError::Invalid("no edges selected".into()))?;
|
||||
let mut best: Option<(usize, f64)> = None;
|
||||
for (i, b) in state.bodies.iter().enumerate() {
|
||||
let tol = (b.body.size() * 2e-3).max(1e-3);
|
||||
if let Ok(Some((_, d))) = b.body.nearest_edge(*p, tol)
|
||||
&& best.is_none_or(|(_, bd)| d < bd)
|
||||
{
|
||||
best = Some((i, d));
|
||||
}
|
||||
}
|
||||
best.map(|(i, _)| i).ok_or_else(|| DocError::Invalid("there is no body to modify".into()))
|
||||
}
|
||||
|
||||
fn revolve_axis(ss: &SolvedSketch, axis: &AxisRef) -> Result<(Vec2, Vec2)> {
|
||||
match axis {
|
||||
AxisRef::SketchLine { curve } => {
|
||||
let ci = ss.sketch.curve_index(curve).ok_or_else(|| DocError::Unknown(format!("sketch line `{curve}`")))?;
|
||||
match ss.sketch.curves.get(ci).map(|c| &c.kind) {
|
||||
Some(CurveKind::Line { a, b }) => {
|
||||
let (pa, pb) = (ss.sketch.point(*a).unwrap_or_default(), ss.sketch.point(*b).unwrap_or_default());
|
||||
Ok((pa, pb - pa))
|
||||
}
|
||||
_ => Err(DocError::Invalid(format!("`{curve}` is not a line"))),
|
||||
}
|
||||
}
|
||||
AxisRef::SketchAxis { axis } => match axis.to_ascii_lowercase().as_str() {
|
||||
"x" => Ok((Vec2::ZERO, Vec2::X)),
|
||||
"y" => Ok((Vec2::ZERO, Vec2::Y)),
|
||||
other => Err(DocError::Unknown(format!("sketch axis `{other}`"))),
|
||||
},
|
||||
AxisRef::World { axis } => {
|
||||
let d = match axis.to_ascii_uppercase().as_str() {
|
||||
"X" => Vec3::X,
|
||||
"Y" => Vec3::Y,
|
||||
"Z" => Vec3::Z,
|
||||
other => return Err(DocError::Unknown(format!("axis `{other}`"))),
|
||||
};
|
||||
let pl = &ss.plane;
|
||||
if d.dot(pl.normal()).abs() > 1e-9 || pl.height(Vec3::ZERO).abs() > 1e-9 {
|
||||
return Err(DocError::Invalid(format!("the {axis} axis does not lie in the sketch plane")));
|
||||
}
|
||||
Ok((pl.to_local(Vec3::ZERO), Vec2::new(d.dot(pl.x), d.dot(pl.y))))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_feature(doc: &Document, vals: &BTreeMap<String, Value>, f: &Feature, st: &mut ModelState, warning: &mut Option<String>) -> Result<()> {
|
||||
match &f.kind {
|
||||
FeatureKind::Sketch { plane, sketch } => {
|
||||
let plane = doc.resolve_plane(vals, plane, 0)?;
|
||||
let mut sk = sketch.clone();
|
||||
doc.apply_dimension_values(vals, &mut sk)?;
|
||||
let report = solve(&mut sk);
|
||||
if !report.ok() {
|
||||
*warning = Some(format!("the sketch constraints conflict ({})", report.failing.join(", ")));
|
||||
}
|
||||
let profiles = find_profiles(&sk);
|
||||
st.sketches.retain(|s| s.feature != f.id);
|
||||
st.sketches.push(SolvedSketch { feature: f.id, name: f.name.clone(), plane, sketch: sk, report, profiles });
|
||||
Ok(())
|
||||
}
|
||||
FeatureKind::Extrude { sketch, profiles, extent, operation, targets } => {
|
||||
let ss = st.sketch(*sketch).ok_or_else(|| DocError::Unknown(format!("sketch {sketch} (it must come earlier in the timeline)")))?.clone();
|
||||
let regions = select_profiles(&ss, profiles)?;
|
||||
let d = val(vals, &extent.distance, Kind::Length)?;
|
||||
let off = match &extent.start_offset {
|
||||
Some(e) => val(vals, e, Kind::Length)?,
|
||||
None => 0.0,
|
||||
};
|
||||
let (a, b) = match (&extent.distance2, extent.direction) {
|
||||
(Some(d2), _) => (-val(vals, d2, Kind::Length)?, d),
|
||||
(None, crate::Direction::Positive) => (0.0, d),
|
||||
(None, crate::Direction::Negative) => (-d, 0.0),
|
||||
(None, crate::Direction::Symmetric) => (-d, d),
|
||||
};
|
||||
// A negative distance flips the side.
|
||||
let (lo, hi) = if a <= b { (a, b) } else { (b, a) };
|
||||
let mut tools = Vec::new();
|
||||
for r in ®ions {
|
||||
let (l2, h2) = extend_for_coplanar(st, &ss.plane, r, lo + off, hi + off, *operation, targets);
|
||||
tools.extend(kernel::extrude(&ss.plane, std::slice::from_ref(r), l2, h2)?);
|
||||
}
|
||||
apply_op(st, f, tools, *operation, targets)
|
||||
}
|
||||
FeatureKind::Revolve { sketch, profiles, axis, angle, operation, targets } => {
|
||||
let ss = st.sketch(*sketch).ok_or_else(|| DocError::Unknown(format!("sketch {sketch} (it must come earlier in the timeline)")))?.clone();
|
||||
let regions = select_profiles(&ss, profiles)?;
|
||||
let (o, d) = revolve_axis(&ss, axis)?;
|
||||
let ang = val(vals, angle, Kind::Angle)?;
|
||||
let tools = kernel::revolve(&ss.plane, ®ions, o, d, ang)?;
|
||||
apply_op(st, f, tools, *operation, targets)
|
||||
}
|
||||
FeatureKind::Fillet { edges, radius, body } | FeatureKind::Chamfer { edges, distance: radius, body } => {
|
||||
let r = val(vals, radius, Kind::Length)?;
|
||||
let ti = blend_target(st, body, edges)?;
|
||||
let Some(mb) = st.bodies.get(ti) else { return Err(DocError::Invalid("body".into())) };
|
||||
let nb =
|
||||
if matches!(f.kind, FeatureKind::Fillet { .. }) { kernel::fillet(&mb.body, edges, r)? } else { kernel::chamfer(&mb.body, edges, r)? };
|
||||
let (name, feat) = (mb.name.clone(), mb.feature);
|
||||
if let Some(slot) = st.bodies.get_mut(ti) {
|
||||
*slot = ModelBody::new(name, nb, feat);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
FeatureKind::Box { corner, length, width, height, operation } => {
|
||||
let s = Vec3::new(val(vals, length, Kind::Length)?, val(vals, width, Kind::Length)?, val(vals, height, Kind::Length)?);
|
||||
let b = kernel::box_solid(*corner, *corner + s)?;
|
||||
apply_op(st, f, vec![b], *operation, &[])
|
||||
}
|
||||
FeatureKind::Cylinder { base, axis, radius, height, operation } => {
|
||||
let b = kernel::cylinder(*base, *axis, val(vals, radius, Kind::Length)?, val(vals, height, Kind::Length)?)?;
|
||||
apply_op(st, f, vec![b], *operation, &[])
|
||||
}
|
||||
FeatureKind::Sphere { center, radius, operation } => {
|
||||
let b = kernel::sphere(*center, val(vals, radius, Kind::Length)?)?;
|
||||
apply_op(st, f, vec![b], *operation, &[])
|
||||
}
|
||||
FeatureKind::Torus { center, major, minor, operation } => {
|
||||
let b = kernel::torus(*center, val(vals, major, Kind::Length)?, val(vals, minor, Kind::Length)?)?;
|
||||
apply_op(st, f, vec![b], *operation, &[])
|
||||
}
|
||||
FeatureKind::Combine { target, tools, operation, keep_tools } => {
|
||||
if tools.iter().any(|t| t == target) {
|
||||
return Err(DocError::Invalid("a body cannot be combined with itself".into()));
|
||||
}
|
||||
let mut tb = Vec::new();
|
||||
for t in tools {
|
||||
tb.push(st.body(t).ok_or_else(|| DocError::Unknown(format!("body `{t}`")))?.body.clone());
|
||||
}
|
||||
if st.body(target).is_none() {
|
||||
return Err(DocError::Unknown(format!("body `{target}`")));
|
||||
}
|
||||
let op = if *operation == Operation::NewBody { Operation::Join } else { *operation };
|
||||
apply_op(st, f, tb, op, std::slice::from_ref(target))?;
|
||||
if !keep_tools {
|
||||
st.bodies.retain(|b| !tools.contains(&b.name));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
FeatureKind::Move { bodies, translate, rotate_axis, angle } => {
|
||||
let t =
|
||||
Vec3::new(val(vals, &translate[0], Kind::Length)?, val(vals, &translate[1], Kind::Length)?, val(vals, &translate[2], Kind::Length)?);
|
||||
let ang = match angle {
|
||||
Some(a) => val(vals, a, Kind::Angle)?,
|
||||
None => 0.0,
|
||||
};
|
||||
let axis = rotate_axis.unwrap_or(Vec3::Z);
|
||||
for n in bodies {
|
||||
let i = st.bodies.iter().position(|b| &b.name == n).ok_or_else(|| DocError::Unknown(format!("body `{n}`")))?;
|
||||
if let Some(mb) = st.bodies.get_mut(i) {
|
||||
let nb = kernel::transform(&mb.body, t, Vec3::ZERO, axis, ang)?;
|
||||
*mb = ModelBody::new(mb.name.clone(), nb, mb.feature);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,506 @@
|
||||
//! Parameter expressions: `2 * width + 5 mm`, `angle / 2`, `sqrt(a^2 + b^2)`.
|
||||
//!
|
||||
//! Values carry a simple dimension (powers of length and angle). Lengths are millimetres and
|
||||
//! angles radians internally. A unit-less number added to a dimensioned value takes that value's
|
||||
//! unit (`width + 5` adds 5 mm); a unit-less final result takes the expected unit's default
|
||||
//! (mm for lengths, degrees for angles). Trigonometric functions take an angle; a unit-less
|
||||
//! argument is read as degrees.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use crate::DocError;
|
||||
|
||||
const MAX_LEN: usize = 4096;
|
||||
const MAX_DEPTH: usize = 64;
|
||||
|
||||
/// What an expression must evaluate to.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Kind {
|
||||
Length,
|
||||
Angle,
|
||||
Unitless,
|
||||
}
|
||||
|
||||
impl Kind {
|
||||
pub fn from_unit(u: &str) -> Kind {
|
||||
match u.trim() {
|
||||
"deg" | "rad" | "°" => Kind::Angle,
|
||||
"" | "none" | "unitless" => Kind::Unitless,
|
||||
_ => Kind::Length,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A dimensioned value.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Value {
|
||||
pub v: f64,
|
||||
pub len: i8,
|
||||
pub ang: i8,
|
||||
}
|
||||
|
||||
impl Value {
|
||||
pub fn num(v: f64) -> Value {
|
||||
Value { v, len: 0, ang: 0 }
|
||||
}
|
||||
pub fn length(mm: f64) -> Value {
|
||||
Value { v: mm, len: 1, ang: 0 }
|
||||
}
|
||||
pub fn angle(rad: f64) -> Value {
|
||||
Value { v: rad, len: 0, ang: 1 }
|
||||
}
|
||||
fn unitless(&self) -> bool {
|
||||
self.len == 0 && self.ang == 0
|
||||
}
|
||||
/// Convert to the expected kind (mm or radians).
|
||||
pub fn to_kind(self, k: Kind) -> Result<f64, DocError> {
|
||||
let out = match k {
|
||||
Kind::Length if self.len == 1 && self.ang == 0 => self.v,
|
||||
Kind::Length if self.unitless() => self.v,
|
||||
Kind::Angle if self.ang == 1 && self.len == 0 => self.v,
|
||||
Kind::Angle if self.unitless() => self.v.to_radians(),
|
||||
Kind::Unitless if self.unitless() => self.v,
|
||||
_ => return Err(DocError::Expr(format!("wrong units for a {k:?} value"))),
|
||||
};
|
||||
if out.is_finite() { Ok(out) } else { Err(DocError::Expr("result is not a finite number".into())) }
|
||||
}
|
||||
}
|
||||
|
||||
fn unit(name: &str) -> Option<Value> {
|
||||
Some(match name {
|
||||
"mm" => Value::length(1.0),
|
||||
"cm" => Value::length(10.0),
|
||||
"m" => Value::length(1000.0),
|
||||
"um" => Value::length(0.001),
|
||||
"in" => Value::length(25.4),
|
||||
"ft" => Value::length(304.8),
|
||||
"deg" => Value::angle(std::f64::consts::PI / 180.0),
|
||||
"rad" => Value::angle(1.0),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
enum Tok {
|
||||
Num(f64),
|
||||
Id(String),
|
||||
Op(char),
|
||||
}
|
||||
|
||||
fn lex(s: &str) -> Result<Vec<Tok>, DocError> {
|
||||
if s.len() > MAX_LEN {
|
||||
return Err(DocError::Expr("expression too long".into()));
|
||||
}
|
||||
let cs: Vec<char> = s.chars().collect();
|
||||
let mut i = 0;
|
||||
let mut out = Vec::new();
|
||||
while let Some(&c) = cs.get(i) {
|
||||
if c.is_whitespace() {
|
||||
i += 1;
|
||||
} else if c.is_ascii_digit() || c == '.' {
|
||||
let st = i;
|
||||
while cs.get(i).is_some_and(|c| c.is_ascii_digit() || *c == '.') {
|
||||
i += 1;
|
||||
}
|
||||
// Exponent: 1e-3
|
||||
if cs.get(i).is_some_and(|c| *c == 'e' || *c == 'E') && cs.get(i + 1).is_some_and(|c| c.is_ascii_digit() || *c == '-' || *c == '+') {
|
||||
i += 2;
|
||||
while cs.get(i).is_some_and(char::is_ascii_digit) {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
let t: String = cs.get(st..i).map(|s| s.iter().collect()).unwrap_or_default();
|
||||
out.push(Tok::Num(t.parse().map_err(|_| DocError::Expr(format!("bad number `{t}`")))?));
|
||||
} else if c.is_alphabetic() || c == '_' {
|
||||
let st = i;
|
||||
while cs.get(i).is_some_and(|c| c.is_alphanumeric() || *c == '_') {
|
||||
i += 1;
|
||||
}
|
||||
out.push(Tok::Id(cs.get(st..i).map(|s| s.iter().collect()).unwrap_or_default()));
|
||||
} else if c == '°' {
|
||||
out.push(Tok::Id("deg".into()));
|
||||
i += 1;
|
||||
} else if "+-*/^(),".contains(c) {
|
||||
out.push(Tok::Op(c));
|
||||
i += 1;
|
||||
} else {
|
||||
return Err(DocError::Expr(format!("unexpected `{c}`")));
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Identifiers an expression refers to (excluding units and functions).
|
||||
pub fn references(s: &str) -> Vec<String> {
|
||||
let Ok(toks) = lex(s) else { return Vec::new() };
|
||||
let mut out: Vec<String> = Vec::new();
|
||||
for (i, t) in toks.iter().enumerate() {
|
||||
if let Tok::Id(id) = t {
|
||||
let is_call = matches!(toks.get(i + 1), Some(Tok::Op('(')));
|
||||
if !is_call && unit(id).is_none() && id != "pi" && id != "PI" && !out.contains(id) {
|
||||
out.push(id.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
struct Parser<'a> {
|
||||
toks: Vec<Tok>,
|
||||
pos: usize,
|
||||
lookup: &'a dyn Fn(&str) -> Result<Value, DocError>,
|
||||
depth: usize,
|
||||
}
|
||||
|
||||
impl Parser<'_> {
|
||||
fn peek(&self) -> Option<&Tok> {
|
||||
self.toks.get(self.pos)
|
||||
}
|
||||
fn next(&mut self) -> Option<Tok> {
|
||||
let t = self.toks.get(self.pos).cloned();
|
||||
self.pos += 1;
|
||||
t
|
||||
}
|
||||
fn enter(&mut self) -> Result<(), DocError> {
|
||||
self.depth += 1;
|
||||
if self.depth > MAX_DEPTH { Err(DocError::Expr("expression nested too deeply".into())) } else { Ok(()) }
|
||||
}
|
||||
fn expr(&mut self) -> Result<Value, DocError> {
|
||||
self.enter()?;
|
||||
let mut a = self.term()?;
|
||||
while let Some(Tok::Op(c @ ('+' | '-'))) = self.peek().cloned() {
|
||||
self.pos += 1;
|
||||
let b = self.term()?;
|
||||
a = add(a, b, c == '-')?;
|
||||
}
|
||||
self.depth -= 1;
|
||||
Ok(a)
|
||||
}
|
||||
fn term(&mut self) -> Result<Value, DocError> {
|
||||
let mut a = self.unary()?;
|
||||
loop {
|
||||
match self.peek().cloned() {
|
||||
Some(Tok::Op('*')) => {
|
||||
self.pos += 1;
|
||||
let b = self.unary()?;
|
||||
a = mul(a, b, false);
|
||||
}
|
||||
Some(Tok::Op('/')) => {
|
||||
self.pos += 1;
|
||||
let b = self.unary()?;
|
||||
a = mul(a, b, true);
|
||||
}
|
||||
// Juxtaposition: `10 mm`, `2 width`.
|
||||
Some(Tok::Id(_) | Tok::Num(_)) | Some(Tok::Op('(')) => {
|
||||
let b = self.unary()?;
|
||||
a = mul(a, b, false);
|
||||
}
|
||||
_ => return Ok(a),
|
||||
}
|
||||
}
|
||||
}
|
||||
fn unary(&mut self) -> Result<Value, DocError> {
|
||||
match self.peek() {
|
||||
Some(Tok::Op('-')) => {
|
||||
self.pos += 1;
|
||||
self.enter()?;
|
||||
let v = self.unary()?;
|
||||
self.depth -= 1;
|
||||
Ok(Value { v: -v.v, ..v })
|
||||
}
|
||||
Some(Tok::Op('+')) => {
|
||||
self.pos += 1;
|
||||
self.unary()
|
||||
}
|
||||
_ => self.power(),
|
||||
}
|
||||
}
|
||||
fn power(&mut self) -> Result<Value, DocError> {
|
||||
let base = self.atom()?;
|
||||
if let Some(Tok::Op('^')) = self.peek() {
|
||||
self.pos += 1;
|
||||
self.enter()?;
|
||||
let e = self.unary()?;
|
||||
self.depth -= 1;
|
||||
if !e.unitless() {
|
||||
return Err(DocError::Expr("exponent must be unit-less".into()));
|
||||
}
|
||||
let n = e.v;
|
||||
let k = if (n - n.round()).abs() < 1e-12 && n.abs() < 16.0 { n.round() as i8 } else { 0 };
|
||||
if !base.unitless() && k == 0 {
|
||||
return Err(DocError::Expr("dimensioned values need a small integer exponent".into()));
|
||||
}
|
||||
return Ok(Value { v: base.v.powf(n), len: base.len.saturating_mul(k), ang: base.ang.saturating_mul(k) });
|
||||
}
|
||||
Ok(base)
|
||||
}
|
||||
fn atom(&mut self) -> Result<Value, DocError> {
|
||||
match self.next() {
|
||||
Some(Tok::Num(v)) => Ok(Value::num(v)),
|
||||
Some(Tok::Op('(')) => {
|
||||
let v = self.expr()?;
|
||||
match self.next() {
|
||||
Some(Tok::Op(')')) => Ok(v),
|
||||
_ => Err(DocError::Expr("missing `)`".into())),
|
||||
}
|
||||
}
|
||||
Some(Tok::Id(id)) => {
|
||||
if let Some(Tok::Op('(')) = self.peek() {
|
||||
self.pos += 1;
|
||||
let mut args = Vec::new();
|
||||
if !matches!(self.peek(), Some(Tok::Op(')'))) {
|
||||
loop {
|
||||
args.push(self.expr()?);
|
||||
match self.next() {
|
||||
Some(Tok::Op(',')) => continue,
|
||||
Some(Tok::Op(')')) => break,
|
||||
_ => return Err(DocError::Expr("bad function arguments".into())),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.pos += 1;
|
||||
}
|
||||
return call(&id, &args);
|
||||
}
|
||||
if id == "pi" || id == "PI" {
|
||||
return Ok(Value::num(std::f64::consts::PI));
|
||||
}
|
||||
if let Some(u) = unit(&id) {
|
||||
return Ok(u);
|
||||
}
|
||||
(self.lookup)(&id)
|
||||
}
|
||||
Some(Tok::Op(c)) => Err(DocError::Expr(format!("unexpected `{c}`"))),
|
||||
None => Err(DocError::Expr("unexpected end of expression".into())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add(a: Value, b: Value, sub: bool) -> Result<Value, DocError> {
|
||||
let (len, ang) = if a.unitless() {
|
||||
(b.len, b.ang)
|
||||
} else if b.unitless() || (a.len == b.len && a.ang == b.ang) {
|
||||
(a.len, a.ang)
|
||||
} else {
|
||||
return Err(DocError::Expr("cannot add values with different units".into()));
|
||||
};
|
||||
// A unit-less operand next to an angle is in degrees.
|
||||
let conv = |x: Value| if x.unitless() && ang == 1 && len == 0 { x.v.to_radians() } else { x.v };
|
||||
let (x, y) = (conv(a), conv(b));
|
||||
Ok(Value { v: if sub { x - y } else { x + y }, len, ang })
|
||||
}
|
||||
|
||||
fn mul(a: Value, b: Value, div: bool) -> Value {
|
||||
if div {
|
||||
Value { v: a.v / b.v, len: a.len.saturating_sub(b.len), ang: a.ang.saturating_sub(b.ang) }
|
||||
} else {
|
||||
Value { v: a.v * b.v, len: a.len.saturating_add(b.len), ang: a.ang.saturating_add(b.ang) }
|
||||
}
|
||||
}
|
||||
|
||||
fn call(f: &str, args: &[Value]) -> Result<Value, DocError> {
|
||||
let one = || match args {
|
||||
[a] => Ok(*a),
|
||||
_ => Err(DocError::Expr(format!("`{f}` takes one argument"))),
|
||||
};
|
||||
let rad = |a: Value| {
|
||||
if a.ang == 1 && a.len == 0 {
|
||||
Ok(a.v)
|
||||
} else if a.unitless() {
|
||||
Ok(a.v.to_radians())
|
||||
} else {
|
||||
Err(DocError::Expr(format!("`{f}` needs an angle")))
|
||||
}
|
||||
};
|
||||
let num = |a: Value| if a.unitless() { Ok(a.v) } else { Err(DocError::Expr(format!("`{f}` needs a unit-less value"))) };
|
||||
Ok(match f {
|
||||
"sin" => Value::num(rad(one()?)?.sin()),
|
||||
"cos" => Value::num(rad(one()?)?.cos()),
|
||||
"tan" => Value::num(rad(one()?)?.tan()),
|
||||
"asin" => Value::angle(num(one()?)?.asin()),
|
||||
"acos" => Value::angle(num(one()?)?.acos()),
|
||||
"atan" => Value::angle(num(one()?)?.atan()),
|
||||
"sqrt" => {
|
||||
let a = one()?;
|
||||
if a.len % 2 != 0 || a.ang % 2 != 0 {
|
||||
return Err(DocError::Expr("sqrt of an odd power of a unit".into()));
|
||||
}
|
||||
Value { v: a.v.sqrt(), len: a.len / 2, ang: a.ang / 2 }
|
||||
}
|
||||
"abs" => {
|
||||
let a = one()?;
|
||||
Value { v: a.v.abs(), ..a }
|
||||
}
|
||||
"floor" | "ceil" | "round" => {
|
||||
let a = one()?;
|
||||
let v = match f {
|
||||
"floor" => a.v.floor(),
|
||||
"ceil" => a.v.ceil(),
|
||||
_ => a.v.round(),
|
||||
};
|
||||
Value { v, ..a }
|
||||
}
|
||||
"min" | "max" => {
|
||||
let Some(first) = args.first().copied() else { return Err(DocError::Expr(format!("`{f}` needs arguments"))) };
|
||||
let mut acc = first;
|
||||
for a in args.iter().skip(1) {
|
||||
let d = add(*a, Value { v: 0.0, ..acc }, false)?;
|
||||
if (f == "min" && d.v < acc.v) || (f == "max" && d.v > acc.v) {
|
||||
acc = d;
|
||||
}
|
||||
}
|
||||
acc
|
||||
}
|
||||
_ => return Err(DocError::Expr(format!("unknown function `{f}`"))),
|
||||
})
|
||||
}
|
||||
|
||||
/// Evaluate an expression; `lookup` resolves parameter names.
|
||||
pub fn eval_with(s: &str, lookup: &dyn Fn(&str) -> Result<Value, DocError>) -> Result<Value, DocError> {
|
||||
let toks = lex(s)?;
|
||||
if toks.is_empty() {
|
||||
return Err(DocError::Expr("empty expression".into()));
|
||||
}
|
||||
let mut p = Parser { toks, pos: 0, lookup, depth: 0 };
|
||||
let v = p.expr()?;
|
||||
if p.pos < p.toks.len() {
|
||||
return Err(DocError::Expr(format!("unexpected input in `{s}`")));
|
||||
}
|
||||
if !v.v.is_finite() {
|
||||
return Err(DocError::Expr(format!("`{s}` is not a finite number")));
|
||||
}
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
/// Evaluate all parameters (name → (expression, kind)) in dependency order.
|
||||
pub fn eval_params(params: &[(String, String, Kind)]) -> (BTreeMap<String, Value>, BTreeMap<String, String>) {
|
||||
let mut done: BTreeMap<String, Value> = BTreeMap::new();
|
||||
let mut errors: BTreeMap<String, String> = BTreeMap::new();
|
||||
fn visit(
|
||||
name: &str,
|
||||
params: &[(String, String, Kind)],
|
||||
done: &mut BTreeMap<String, Value>,
|
||||
errors: &mut BTreeMap<String, String>,
|
||||
stack: &mut Vec<String>,
|
||||
) -> Result<Value, DocError> {
|
||||
if let Some(v) = done.get(name) {
|
||||
return Ok(*v);
|
||||
}
|
||||
if let Some(e) = errors.get(name) {
|
||||
return Err(DocError::Expr(e.clone()));
|
||||
}
|
||||
let Some((_, expr, kind)) = params.iter().find(|(n, _, _)| n == name) else {
|
||||
return Err(DocError::Expr(format!("unknown parameter `{name}`")));
|
||||
};
|
||||
if stack.iter().any(|s| s == name) || stack.len() > 256 {
|
||||
return Err(DocError::Expr(format!("circular reference through `{name}`")));
|
||||
}
|
||||
stack.push(name.to_string());
|
||||
let r = {
|
||||
let deps = references(expr);
|
||||
let mut vals: BTreeMap<String, Value> = BTreeMap::new();
|
||||
let mut err = None;
|
||||
for d in deps {
|
||||
match visit(&d, params, done, errors, stack) {
|
||||
Ok(v) => {
|
||||
vals.insert(d, v);
|
||||
}
|
||||
Err(e) => {
|
||||
err = Some(e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
match err {
|
||||
Some(e) => Err(e),
|
||||
None => eval_with(expr, &|n| vals.get(n).copied().ok_or_else(|| DocError::Expr(format!("unknown parameter `{n}`")))).and_then(|v| {
|
||||
let x = v.to_kind(*kind)?;
|
||||
Ok(match kind {
|
||||
Kind::Length => Value::length(x),
|
||||
Kind::Angle => Value::angle(x),
|
||||
Kind::Unitless => Value::num(x),
|
||||
})
|
||||
}),
|
||||
}
|
||||
};
|
||||
stack.pop();
|
||||
match &r {
|
||||
Ok(v) => {
|
||||
done.insert(name.to_string(), *v);
|
||||
}
|
||||
Err(e) => {
|
||||
errors.insert(name.to_string(), e.to_string());
|
||||
}
|
||||
}
|
||||
r
|
||||
}
|
||||
for (n, _, _) in params {
|
||||
let mut stack = Vec::new();
|
||||
let _ = visit(n, params, &mut done, &mut errors, &mut stack);
|
||||
}
|
||||
(done, errors)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn ev(s: &str, k: Kind) -> Result<f64, DocError> {
|
||||
let look = |n: &str| match n {
|
||||
"width" => Ok(Value::length(40.0)),
|
||||
"ang" => Ok(Value::angle(std::f64::consts::FRAC_PI_2)),
|
||||
"count" => Ok(Value::num(3.0)),
|
||||
_ => Err(DocError::Expr(format!("unknown {n}"))),
|
||||
};
|
||||
eval_with(s, &look)?.to_kind(k)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arithmetic_and_units() {
|
||||
assert_eq!(ev("10", Kind::Length).unwrap(), 10.0);
|
||||
assert!((ev("90 deg", Kind::Angle).unwrap() - std::f64::consts::FRAC_PI_2).abs() < 1e-12);
|
||||
assert_eq!(ev("10 mm", Kind::Length).unwrap(), 10.0);
|
||||
assert_eq!(ev("1 in", Kind::Length).unwrap(), 25.4);
|
||||
assert_eq!(ev("2 cm + 5", Kind::Length).unwrap(), 25.0);
|
||||
assert_eq!(ev("width / 2 + 1", Kind::Length).unwrap(), 21.0);
|
||||
assert_eq!(ev("width * count", Kind::Length).unwrap(), 120.0);
|
||||
assert_eq!(ev("-(2+3)*2^2", Kind::Unitless).unwrap(), -20.0);
|
||||
assert!((ev("45", Kind::Angle).unwrap() - std::f64::consts::FRAC_PI_4).abs() < 1e-12);
|
||||
assert!((ev("ang / 2", Kind::Angle).unwrap() - std::f64::consts::FRAC_PI_4).abs() < 1e-12);
|
||||
assert!((ev("ang + 90", Kind::Angle).unwrap() - std::f64::consts::PI).abs() < 1e-12);
|
||||
assert!((ev("sqrt((3 mm) ^ 2 + (4 mm) ^ 2)", Kind::Length).unwrap() - 5.0).abs() < 1e-12);
|
||||
assert!((ev("sin(30)", Kind::Unitless).unwrap() - 0.5).abs() < 1e-12);
|
||||
assert!((ev("2*pi", Kind::Unitless).unwrap() - std::f64::consts::TAU).abs() < 1e-12);
|
||||
assert_eq!(ev("max(1 mm, 3, 2)", Kind::Length).unwrap(), 3.0);
|
||||
assert_eq!(ev("1e-3 m", Kind::Length).unwrap(), 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors() {
|
||||
assert!(ev("width + ang", Kind::Length).is_err());
|
||||
assert!(ev("width * width", Kind::Length).is_err());
|
||||
assert!(ev("nope", Kind::Length).is_err());
|
||||
assert!(ev("(1", Kind::Length).is_err());
|
||||
assert!(ev("1 / 0", Kind::Length).is_err());
|
||||
assert!(ev("", Kind::Length).is_err());
|
||||
assert!(ev("1 $ 2", Kind::Length).is_err());
|
||||
let deep = "(".repeat(500) + "1" + &")".repeat(500);
|
||||
assert!(ev(&deep, Kind::Length).is_err());
|
||||
assert!(ev(&"-".repeat(500), Kind::Length).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn params_in_order_with_cycles() {
|
||||
let p = vec![
|
||||
("b".to_string(), "a * 2".to_string(), Kind::Length),
|
||||
("a".to_string(), "10 mm".to_string(), Kind::Length),
|
||||
("x".to_string(), "y".to_string(), Kind::Length),
|
||||
("y".to_string(), "x + 1".to_string(), Kind::Length),
|
||||
];
|
||||
let (v, e) = eval_params(&p);
|
||||
assert_eq!(v.get("b").unwrap().v, 20.0);
|
||||
assert!(e.contains_key("x") && e.contains_key("y"));
|
||||
assert_eq!(references("2*width + sin(ang) + 3 mm"), vec!["width".to_string(), "ang".to_string()]);
|
||||
}
|
||||
}
|
||||
@@ -1 +1,34 @@
|
||||
//! The SolveCraft document: user and model parameters, and a parametric timeline of features
|
||||
//! (sketches, extrudes, revolves, fillets, primitives, combines…). Evaluating the timeline with
|
||||
//! the kernel produces the model (bodies and solved sketches). Evaluation is incremental: a
|
||||
//! feature is recomputed only when it, a parameter it uses, or an earlier feature changed.
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
mod document;
|
||||
mod eval;
|
||||
pub mod expr;
|
||||
|
||||
pub use document::*;
|
||||
pub use eval::{FeatureResult, Model, ModelBody, ModelState, SolvedSketch};
|
||||
pub use solvecraft_kernel as kernel;
|
||||
pub use solvecraft_sketch as sketch;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
|
||||
pub enum DocError {
|
||||
#[error("expression: {0}")]
|
||||
Expr(String),
|
||||
#[error("{0}")]
|
||||
Invalid(String),
|
||||
#[error("unknown {0}")]
|
||||
Unknown(String),
|
||||
#[error(transparent)]
|
||||
Sketch(#[from] solvecraft_sketch::SketchError),
|
||||
#[error(transparent)]
|
||||
Kernel(#[from] solvecraft_kernel::KernelError),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, DocError>;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,183 @@
|
||||
use solvecraft_geom::{Vec2, Vec3};
|
||||
use solvecraft_kernel::measure;
|
||||
use solvecraft_sketch::{ConstraintKind, Sketch};
|
||||
|
||||
use crate::*;
|
||||
|
||||
fn rel(a: f64, b: f64) -> f64 {
|
||||
(a - b).abs() / b.abs().max(1e-12)
|
||||
}
|
||||
|
||||
/// 40 x 30 rectangle with its width and height driven by `width` / d2.
|
||||
fn plate_doc() -> (Document, u64) {
|
||||
let mut doc = Document::new("plate");
|
||||
doc.set_param("width", "40 mm", Some("mm"), None).unwrap();
|
||||
let mut sk = Sketch::new();
|
||||
let pts = [Vec2::new(0.0, 0.0), Vec2::new(38.0, 0.5), Vec2::new(39.0, 31.0), Vec2::new(0.5, 29.0)];
|
||||
let ids: Vec<usize> = pts.iter().map(|p| sk.add_point(*p, None).unwrap()).collect();
|
||||
let l: Vec<usize> = (0..4).map(|i| sk.add_line_pts(ids[i], ids[(i + 1) % 4], None).unwrap()).collect();
|
||||
use ConstraintKind::*;
|
||||
sk.add_constraint(Coincident { p: ids[0], q: 0 }, None).unwrap();
|
||||
sk.add_constraint(Horizontal { l: l[0] }, None).unwrap();
|
||||
sk.add_constraint(Horizontal { l: l[2] }, None).unwrap();
|
||||
sk.add_constraint(Vertical { l: l[1] }, None).unwrap();
|
||||
sk.add_constraint(Vertical { l: l[3] }, None).unwrap();
|
||||
let d1 = doc.new_model_param("width", "mm");
|
||||
sk.add_constraint(Length { l: l[0], value: 40.0 }, Some(d1)).unwrap();
|
||||
let d2 = doc.new_model_param("30 mm", "mm");
|
||||
sk.add_constraint(Length { l: l[1], value: 30.0 }, Some(d2)).unwrap();
|
||||
let s = doc.add_feature(FeatureKind::Sketch { plane: PlaneRef::Origin { name: "XY".into() }, sketch: sk }, None).unwrap();
|
||||
doc.add_feature(
|
||||
FeatureKind::Extrude {
|
||||
sketch: s,
|
||||
profiles: ProfileSel::All,
|
||||
extent: Extent { distance: "20".into(), direction: Direction::Positive, distance2: None, start_offset: None },
|
||||
operation: Operation::NewBody,
|
||||
targets: vec![],
|
||||
},
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
(doc, s)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parametric_plate_reevaluates() {
|
||||
let (mut doc, _) = plate_doc();
|
||||
let mut m = Model::new();
|
||||
m.evaluate(&doc);
|
||||
assert!(m.results.iter().all(|r| r.error.is_none()), "{:?}", m.results);
|
||||
let st = m.state();
|
||||
assert_eq!(st.bodies.len(), 1);
|
||||
assert_eq!(st.bodies[0].name, "Body1");
|
||||
assert!(rel(measure(&st.bodies[0].body).unwrap().volume, 24000.0) < 1e-9);
|
||||
assert!(st.sketches[0].report.fully_constrained());
|
||||
|
||||
// No change: nothing recomputed.
|
||||
m.evaluate(&doc);
|
||||
assert_eq!(m.last_recomputed, 0);
|
||||
|
||||
doc.set_param("width", "50 mm", None, None).unwrap();
|
||||
m.evaluate(&doc);
|
||||
assert_eq!(m.last_recomputed, 2);
|
||||
assert!(rel(measure(&m.state().bodies[0].body).unwrap().volume, 30000.0) < 1e-9);
|
||||
|
||||
// Changing only the extrude distance re-evaluates only the extrude.
|
||||
if let Some(FeatureKind::Extrude { extent, .. }) = doc.features.get_mut(1).map(|f| &mut f.kind) {
|
||||
extent.distance = "width / 5".into();
|
||||
}
|
||||
m.evaluate(&doc);
|
||||
assert_eq!(m.last_recomputed, 1);
|
||||
assert!(rel(measure(&m.state().bodies[0].body).unwrap().volume, 50.0 * 30.0 * 10.0) < 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn box_fillet_cut_timeline() {
|
||||
let (mut doc, _) = plate_doc();
|
||||
doc.add_feature(FeatureKind::Fillet { edges: vec![Vec3::new(0.0, 0.0, 10.0)], radius: "3".into(), body: None }, None).unwrap();
|
||||
let mut sk = Sketch::new();
|
||||
sk.add_circle(Vec2::new(20.0, 15.0), 5.0, None, None).unwrap();
|
||||
let s2 = doc.add_feature(FeatureKind::Sketch { plane: PlaneRef::Origin { name: "XY".into() }, sketch: sk }, None).unwrap();
|
||||
doc.add_feature(
|
||||
FeatureKind::Extrude {
|
||||
sketch: s2,
|
||||
profiles: ProfileSel::Points { points: vec![Vec2::new(20.0, 15.0)] },
|
||||
extent: Extent { distance: "20".into(), direction: Direction::Positive, distance2: None, start_offset: None },
|
||||
operation: Operation::Cut,
|
||||
targets: vec![],
|
||||
},
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
let mut m = Model::new();
|
||||
m.evaluate(&doc);
|
||||
assert!(m.results.iter().all(|r| r.error.is_none()), "{:?}", m.results.iter().map(|r| &r.error).collect::<Vec<_>>());
|
||||
let st = m.state();
|
||||
let v = measure(&st.bodies[0].body).unwrap().volume;
|
||||
let expect = 24000.0 - (9.0 - std::f64::consts::PI * 9.0 / 4.0) * 20.0 - std::f64::consts::PI * 25.0 * 20.0;
|
||||
assert!(rel(v, expect) < 1e-3, "{v} vs {expect}");
|
||||
// Roll back to before the fillet.
|
||||
doc.marker = Some(2);
|
||||
m.evaluate(&doc);
|
||||
assert!(rel(measure(&m.state().bodies[0].body).unwrap().volume, 24000.0) < 1e-9);
|
||||
assert!(m.results[3].skipped);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_are_reported_not_fatal() {
|
||||
let (mut doc, s) = plate_doc();
|
||||
doc.add_feature(
|
||||
FeatureKind::Revolve {
|
||||
sketch: s,
|
||||
profiles: ProfileSel::All,
|
||||
axis: AxisRef::SketchAxis { axis: "y".into() },
|
||||
angle: "90 deg".into(),
|
||||
operation: Operation::NewBody,
|
||||
targets: vec![],
|
||||
},
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
doc.add_feature(FeatureKind::Fillet { edges: vec![Vec3::new(500.0, 0.0, 0.0)], radius: "1".into(), body: None }, None).unwrap();
|
||||
doc.add_feature(
|
||||
FeatureKind::Box { corner: Vec3::ZERO, length: "nope".into(), width: "1".into(), height: "1".into(), operation: Operation::NewBody },
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
let mut m = Model::new();
|
||||
m.evaluate(&doc);
|
||||
assert!(m.results[2].error.is_none(), "{:?}", m.results[2]);
|
||||
assert!(m.results[3].error.is_some());
|
||||
assert!(m.results[4].error.as_deref().unwrap_or("").contains("nope"));
|
||||
assert_eq!(m.state().bodies.len(), 2);
|
||||
let quarter = measure(&m.state().bodies[1].body).unwrap().volume;
|
||||
// Revolving the 40x30 rectangle (x in [0,40]) 90° about sketch y: π/4 · 40² · 30.
|
||||
assert!(rel(quarter, std::f64::consts::FRAC_PI_4 * 1600.0 * 30.0) < 1e-3, "{quarter}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn params_and_serialization() {
|
||||
let (mut doc, _) = plate_doc();
|
||||
assert!(doc.set_param("bad name", "1", None, None).is_err());
|
||||
assert!(doc.set_param("mm", "1", None, None).is_err());
|
||||
assert!(doc.set_param("loop", "loop + 1", None, None).is_err());
|
||||
assert!(doc.remove_param("width").is_err(), "used by d1");
|
||||
doc.set_param("spare", "3", None, None).unwrap();
|
||||
doc.remove_param("spare").unwrap();
|
||||
let json = doc.to_json();
|
||||
let back = Document::from_json(&json).unwrap();
|
||||
assert_eq!(back, doc);
|
||||
assert!(Document::from_json("{").is_err());
|
||||
let gone = doc.delete_feature(doc.features[0].id).unwrap();
|
||||
assert_eq!(gone.len(), 2, "the extrude goes with its sketch");
|
||||
assert!(doc.param("d1").is_none(), "unused model parameters are pruned");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn primitives_and_combine() {
|
||||
let mut doc = Document::new("p");
|
||||
doc.add_feature(
|
||||
FeatureKind::Box { corner: Vec3::ZERO, length: "10".into(), width: "10".into(), height: "10".into(), operation: Operation::NewBody },
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
doc.add_feature(
|
||||
FeatureKind::Cylinder {
|
||||
base: Vec3::new(5.0, 5.0, 0.0),
|
||||
axis: Vec3::Z,
|
||||
radius: "2".into(),
|
||||
height: "20".into(),
|
||||
operation: Operation::NewBody,
|
||||
},
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
doc.add_feature(FeatureKind::Combine { target: "Body1".into(), tools: vec!["Body2".into()], operation: Operation::Cut, keep_tools: false }, None)
|
||||
.unwrap();
|
||||
let mut m = Model::new();
|
||||
m.evaluate(&doc);
|
||||
assert!(m.results.iter().all(|r| r.error.is_none()), "{:?}", m.results);
|
||||
let st = m.state();
|
||||
assert_eq!(st.bodies.len(), 1);
|
||||
assert!(rel(measure(&st.bodies[0].body).unwrap().volume, 1000.0 - std::f64::consts::PI * 40.0) < 1e-3);
|
||||
}
|
||||
Reference in new issue
Block a user