Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Pattern and mirror: a rib as the source (#4593) (#4667)

Martin Pålsson96ce31153cb3
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index f08180adf9..3837037ceb 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -5068,6 +5068,7 @@ fn feature_op(f: &Feature) -> (Operation, Vec<String>) {
FeatureKind::Pipe { operation, targets, .. } => (*operation, crate::plain(targets)),
FeatureKind::Loft { operation, targets, .. } | FeatureKind::Sweep { operation, targets, .. } => (*operation, crate::plain(targets)),
FeatureKind::Coil { operation, targets, .. } => (*operation, crate::plain(targets)),
+ FeatureKind::Rib { .. } => (Operation::Join, Vec::new()),
_ => (Operation::NewBody, Vec::new()),
}
}
@@ -5427,6 +5428,16 @@ fn replay(
);
}
}
+ } else if let FeatureKind::Rib { .. } = &src.kind {
+ // A rib is built again at each copy of its sketch, against the bodies there (#4593):
+ // its walls run until they meet the body, which may not lie the same way there.
+ for (k, m) in mats.iter().enumerate() {
+ for body in more::rib_tools(vals, st, src, Some(m))? {
+ // Each copy's faces are named as the rib's, with the instance.
+ let t = Traced::unnamed(body);
+ copies.push(Traced { lineage: t.lineage.map(|l| l.copied(src.id, k + 1)), body: t.body });
+ }
+ }
} else {
// Each copy's faces are named as the source feature's, with the instance.
let tools = feature_tools_with_constructor(doc, vals, src, st, &mut None)?;
diff --git a/crates/doc/src/eval_more.rs b/crates/doc/src/eval_more.rs
index 5f450368aa..55ea98d299 100644
--- a/crates/doc/src/eval_more.rs
+++ b/crates/doc/src/eval_more.rs
@@ -69,27 +69,8 @@ pub(super) fn eval(doc: &Document, vals: &BTreeMap<String, Value>, f: &Feature,
}
apply_op(st, f, tools, if *deboss { Operation::Cut } else { Operation::Join }, &crate::plain(targets))
}
- FeatureKind::Rib { sketch, curves, thickness, depth, flip, web, normal } => {
- let ss = st.sketch(sketch.0).ok_or_else(|| DocError::Unknown(format!("sketch {sketch}")))?.clone();
- let t = val(vals, thickness, Kind::Length)?;
- if !(t > 0.0) {
- return Err(DocError::Invalid("the rib thickness must be positive".into()));
- }
- let depth = match depth {
- Some(e) => Some(val(vals, e, Kind::Length)?)
- .filter(|d| *d > 0.0)
- .ok_or_else(|| DocError::Invalid("the rib depth must be positive".into()))
- .map(Some)?,
- None => None,
- };
- let groups: Vec<Vec<String>> = if *web { curves.iter().map(|c| vec![c.clone()]).collect() } else { vec![curves.clone()] };
- if groups.is_empty() || curves.is_empty() || curves.len() > 1000 {
- return Err(DocError::Invalid("a rib needs 1…1000 sketch curves".into()));
- }
- let mut tools = Vec::new();
- for g in &groups {
- tools.push(if *normal { rib_tool_normal(st, &ss, g, t, depth, *flip)? } else { rib_tool(st, &ss, g, t, depth, *flip)? });
- }
+ FeatureKind::Rib { .. } => {
+ let tools = rib_tools(vals, st, f, None)?;
apply_op(st, f, tools, Operation::Join, &[])
}
FeatureKind::ReplaceFace { faces, target, body } => {
@@ -176,6 +157,41 @@ pub(super) fn eval(doc: &Document, vals: &BTreeMap<String, Value>, f: &Feature,
}
}
+/// The walls of rib `f` (one per curve for a web), each run until it meets the bodies. `at`
+/// moves the rib's sketch first: a pattern's or mirror's copy of the rib is built again where
+/// the copy is, against the bodies there (#4593), as a hole is drilled again at each copy.
+pub(super) fn rib_tools(vals: &BTreeMap<String, Value>, st: &ModelState, f: &Feature, at: Option<&crate::Mat>) -> Result<Vec<Body>> {
+ let FeatureKind::Rib { sketch, curves, thickness, depth, flip, web, normal } = &f.kind else {
+ return Err(DocError::Invalid(format!("{} is not a rib", f.name)));
+ };
+ let mut ss = st.sketch(sketch.0).ok_or_else(|| DocError::Unknown(format!("sketch {sketch}")))?.clone();
+ if let Some(m) = at {
+ let p = ss.plane;
+ ss.plane = Plane::new(apply_point(m, p.origin), crate::apply_vector(m, p.x), crate::apply_vector(m, p.y))
+ .ok_or_else(|| DocError::Invalid("the rib's sketch plane at the copy".into()))?;
+ }
+ let t = val(vals, thickness, Kind::Length)?;
+ if !(t > 0.0) {
+ return Err(DocError::Invalid("the rib thickness must be positive".into()));
+ }
+ let depth = match depth {
+ Some(e) => Some(val(vals, e, Kind::Length)?)
+ .filter(|d| *d > 0.0)
+ .ok_or_else(|| DocError::Invalid("the rib depth must be positive".into()))
+ .map(Some)?,
+ None => None,
+ };
+ let groups: Vec<Vec<String>> = if *web { curves.iter().map(|c| vec![c.clone()]).collect() } else { vec![curves.clone()] };
+ if groups.is_empty() || curves.is_empty() || curves.len() > 1000 {
+ return Err(DocError::Invalid("a rib needs 1…1000 sketch curves".into()));
+ }
+ let mut tools = Vec::new();
+ for g in &groups {
+ tools.push(if *normal { rib_tool_normal(st, &ss, g, t, depth, *flip)? } else { rib_tool(st, &ss, g, t, depth, *flip)? });
+ }
+ Ok(tools)
+}
+
/// Unit outward normal of the planar face through `p`: of the planar faces whose plane runs
/// through it, the one nearest it. A face elsewhere in such a plane (the wall of a notch, the end
/// of a rib, in line with a rim) is no pick of `p`, though its plane runs through it as exactly.
diff --git a/crates/engine/src/cmd/patterns_tests.rs b/crates/engine/src/cmd/patterns_tests.rs
index e9a3a0e4e7..e12284342a 100644
--- a/crates/engine/src/cmd/patterns_tests.rs
+++ b/crates/engine/src/cmd/patterns_tests.rs
@@ -604,3 +604,47 @@ fn copies_of_a_join_that_merged_its_targets_join_the_merged_body() {
assert!(rel(volume(&mut s), v0 + n * (v1 - v0)) < 1e-3, "{id}: {} vs {}", volume(&mut s), v0 + n * (v1 - v0));
}
}
+
+/// #4593: a rib is the source of a pattern or mirror. Each copy is built again where it is, its
+/// walls run until they meet the body there. An 80 × 80 × 5 plate with a 16 × 16 × 30 boss in the
+/// middle; the rib's triangle (8, 5)-(8, 25)-(30, 5) in the XZ plane, 4 thick, is 880 mm³.
+#[test]
+fn patterns_and_mirrors_of_a_rib() {
+ use solidcraft_geom::Vec3;
+ let rib = 0.5 * 22.0 * 20.0 * 4.0;
+ let ribbed = || {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length": 80, "width": 80, "height": 5, "corner": [-40, -40, 0]}));
+ run(&mut s, "solid.box", json!({"length": 16, "width": 16, "height": 30, "corner": [-8, -8, 0], "operation": "join"}));
+ run(&mut s, "sketch.create", json!({"plane": {"origin": [0, 0, 0], "x_dir": [1, 0, 0], "y_dir": [0, 0, 1]}, "name": "RibSketch"}));
+ run(&mut s, "sketch.line", json!({"points": [[8, 25], [30, 5]], "ids": ["l1"]}));
+ run(&mut s, "sketch.finish", json!({}));
+ let before = volume(&mut s);
+ run(&mut s, "solid.rib", json!({"sketch": "RibSketch", "curves": ["l1"], "thickness": 4, "name": "Rib"}));
+ assert!(rel(volume(&mut s), before + rib) < 1e-6, "the rib itself");
+ (s, before)
+ };
+ // The issue's step: a circular pattern of the rib (here four, a quarter turn apart).
+ let (mut s, before) = ribbed();
+ run(&mut s, "solid.pattern.circular", json!({"features": ["Rib"], "axis": {"origin": [0, 0, -100], "dir": [0, 0, 1]}, "count": 4, "angle": 360}));
+ let ribs = [Vec3::new(12.0, 0.0, 8.0), Vec3::new(0.0, 12.0, 8.0), Vec3::new(-12.0, 0.0, 8.0), Vec3::new(0.0, -12.0, 8.0)];
+ let check = |s: &Session, inside: &[Vec3]| {
+ let st = s.world_state();
+ assert!(s.model.results.iter().all(|r| r.error.is_none()), "{:?}", s.model.results.iter().map(|r| &r.error).collect::<Vec<_>>());
+ assert_eq!(st.bodies.len(), 1);
+ let body = &st.bodies[0].body;
+ for p in inside {
+ assert!(body.contains(*p), "a rib at {p:?}");
+ // Beside it, across its thickness: no material (clear of the boss too).
+ let side = if p.y == 0.0 { Vec3::new(0.0, 3.0, 0.0) } else { Vec3::new(3.0, 0.0, 0.0) };
+ assert!(!body.contains(*p + side), "nothing beside the rib at {p:?}");
+ }
+ };
+ check(&s, &ribs);
+ assert!(rel(volume(&mut s), before + 4.0 * rib) < 1e-6, "four ribs: {} vs {}", volume(&mut s), before + 4.0 * rib);
+ // A mirror of the rib across YZ.
+ let (mut s, before) = ribbed();
+ run(&mut s, "solid.mirror", json!({"features": ["Rib"], "plane": {"origin": [0, 0, 0], "normal": [1, 0, 0]}}));
+ check(&s, &[ribs[0], ribs[2]]);
+ assert!(rel(volume(&mut s), before + 2.0 * rib) < 1e-6, "two ribs: {} vs {}", volume(&mut s), before + 2.0 * rib);
+}