SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Revolve, sweep, loft and hole from a nested STEP part's face join and drill it (#3223) (#3987)
raykholo22190e703c1c
diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rsindex 7688f1690f..00a9ff49fa 100644--- a/crates/engine/src/cmd/features.rs+++ b/crates/engine/src/cmd/features.rs@@ -564,11 +564,12 @@ fn add_feature_in(s: &mut Session, p: &Value, kind: FeatureKind, world: bool) ->// A cut or intersect made where the active component has no body of its own (a sketch in// the assembly's root, cutting through the parts, #1234) would have nothing to work on: it// takes part in every body instead, which it changes only where it meets them. An extrude's- // own operation counts too: an automatic one that came out as a cut (#3223).+ // own operation counts too: an automatic one that came out as a cut (#3223); and so does a+ // hole, which is always a cut (one at the points of a sketch on a nested part's face).let participants = if participants.is_empty()&& !primitive&& (matches!(str_(p, "operation").and_then(Operation::parse), Some(Operation::Cut | Operation::Intersect))- || matches!(&kind, FeatureKind::Extrude { operation: Operation::Cut | Operation::Intersect, .. }))+ || matches!(&kind, FeatureKind::Extrude { operation: Operation::Cut | Operation::Intersect, .. } | FeatureKind::Hole { .. }))&& s.cut_takes_every_body(&s.model.state()){vec!["*".to_string()]@@ -771,6 +772,16 @@ fn sketch_face_body(s: &Session, sketch: u64) -> Option<String> {active_scope(s, &st).into_iter().find(|b| b.name == anchor.target.body).map(|b| b.name.clone())}+/// The bodies a Join named none for: the body whose face a profile sketch is on (the first+/// sketch that is on one), as for an extrude. Explicit `targets` or `participants` keep theirs.+fn face_join_targets(s: &Session, p: &Value, operation: Operation, sketches: &[u64]) -> Vec<BodyName> {+ let mut targets = string_list(p, "targets");+ if operation == Operation::Join && targets.is_empty() && string_list(p, "participants").is_empty() {+ targets.extend(sketches.iter().find_map(|sk| sketch_face_body(s, *sk)));+ }+ targets.into_iter().map(BodyName).collect()+}+/// The bodies a feature of the active component works on when it names none: the component's/// own. A component with no body of its own (the root of an opened STEP assembly, whose parts/// are components inside it) works on the bodies of the components inside it whose geometry is@@ -935,13 +946,14 @@ fn revolve(s: &mut Session, p: &Value) -> Result<Value> {None => solidcraft_doc::RevolveConvention::SketchNormal,Some(value) => serde_json::from_value(value.clone()).map_err(|_| bad(cmd, "`convention` must be sketch_normal or legacy_axis_start"))?,};+ let operation = operation(p, cmd)?;let kind = FeatureKind::Revolve {sketch: SketchId(sketch),profiles,axis,angle,- operation: operation(p, cmd)?,- targets: string_list(p, "targets").into_iter().map(BodyName).collect(),+ operation,+ targets: face_join_targets(s, p, operation, &[sketch]),angle2,symmetric,convention,@@ -2189,6 +2201,8 @@ fn sweep(s: &mut Session, p: &Value) -> Result<Value> {}let (orientation, twist, scale) = sweep_options(s, p, cmd)?;let (rails, rail_fit) = sweep_rails(s, p, cmd, path_sketch)?;+ let operation = operation(p, cmd)?;+ let targets = face_join_targets(s, p, operation, &[sketch]);add_feature(s,p,@@ -2202,8 +2216,8 @@ fn sweep(s: &mut Session, p: &Value) -> Result<Value> {scale,rails,rail_fit,- operation: operation(p, cmd)?,- targets: string_list(p, "targets").into_iter().map(BodyName).collect(),+ operation,+ targets,},)}@@ -2324,17 +2338,10 @@ fn loft(s: &mut Session, p: &Value) -> Result<Value> {sections.push(solidcraft_doc::LoftSection { sketch: SketchId(sketch), profiles: profiles(sec, cmd)?, point: None });}let (rails, rail_fit) = guide_rails(s, p, cmd, "loft", None)?;- add_feature(- s,- p,- FeatureKind::Loft {- sections,- operation: operation(p, cmd)?,- targets: string_list(p, "targets").into_iter().map(BodyName).collect(),- rails,- rail_fit,- },- )+ let operation = operation(p, cmd)?;+ let sketches: Vec<u64> = sections.iter().map(|sec| sec.sketch.0).collect();+ let targets = face_join_targets(s, p, operation, &sketches);+ add_feature(s, p, FeatureKind::Loft { sections, operation, targets, rails, rail_fit })}fn thread(s: &mut Session, p: &Value) -> Result<Value> {diff --git a/crates/engine/src/cmd/occurrence_tests.rs b/crates/engine/src/cmd/occurrence_tests.rsindex b236a8fbff..228db99277 100644--- a/crates/engine/src/cmd/occurrence_tests.rs+++ b/crates/engine/src/cmd/occurrence_tests.rs@@ -860,3 +860,97 @@ fn component_delete_removes_joints_to_its_occurrences_and_undo_restores_them() {run(&mut s, "edit.undo", json!({}));assert_eq!(s.doc, before);}++/// The stood-up nested STEP part of `a_stood_up_nested_step_part_is_cut_and_joined_from_its_face`,+/// opened with its imported root active, and a sketch on the part's top face. Returns the+/// session, the sketch, the middle of the top face in the root's frame, the face's outward+/// normal and that middle in the sketch's coordinates.+fn nested_part_face_sketch() -> (Session, u64, solidcraft_geom::Vec3, solidcraft_geom::Vec3, solidcraft_geom::Vec2) {+ let dir = std::env::temp_dir().join(format!("solidcraft-nested-scope-{}-{:?}", std::process::id(), std::thread::current().id()));+ std::fs::create_dir_all(&dir).unwrap();+ let path = dir.join("nested.step");+ let mut src = Session::default();+ let (_, carrier) = created(&mut src, "Carrier");+ let (part, nested) = created(&mut src, "Nested Part");+ run(&mut src, "solid.box", json!({"length": 40, "width": 24, "height": 10, "body_name": "NestedBlock"}));+ run(&mut src, "occurrence.move", json!({"occurrence": carrier, "translate": [12, 7, 5], "axis": [0, 0, 1], "angle": "30 deg"}));+ run(&mut src, "occurrence.move", json!({"occurrence": nested, "translate": [3, 11, 2], "axis": [0, 1, 0], "angle": "20 deg"}));+ let top = solidcraft_doc::apply_point(&src.doc.component_transform(part), solidcraft_geom::Vec3::new(20.0, 12.0, 10.0));+ run(&mut src, "file.export", json!({"path": path, "format": "step"}));+ let mut s = Session::default();+ run(&mut s, "doc.open", json!({"path": path, "up": "y"}));+ let _ = std::fs::remove_dir_all(&dir);+ let at = solidcraft_doc::apply_point(&s.doc.component_transform(s.active_component), top);+ let sketch = run(&mut s, "sketch.create", json!({"plane": {"face": [at.x, at.y, at.z]}, "project_edges": false}))["sketch"].as_u64().unwrap();+ let Some(solidcraft_doc::FeatureKind::Sketch { plane: solidcraft_doc::PlaneRef::Face { plane, .. }, .. }) =+ s.doc.feature(sketch).map(|f| &f.kind)+ else {+ panic!("a sketch on the face")+ };+ let (normal, centre) = (plane.normal(), plane.to_local(top));+ (s, sketch, top, normal, centre)+}++/// After a feature from the nested part's face: still one body, in one piece, `change` mm³+/// bigger than the 40 × 24 × 10 block (within a thousandth: round profiles are faceted).+fn assert_block_changed_by(s: &mut Session, change: f64) {+ let st = s.model.state();+ assert_eq!(st.bodies.iter().map(|b| b.name.as_str()).collect::<Vec<_>>(), ["NestedBlock"]);+ assert_eq!(st.body("NestedBlock").map(|b| b.body.shell_count()), Some(1));+ let volume = run(s, "inspect.measure", json!({"bodies": ["NestedBlock"]}))["total"]["volume_mm3"].as_f64().unwrap();+ assert!((volume - 9600.0 - change).abs() < 1e-3 * change.abs(), "{volume}");+}++/// #3223 for Revolve: a ring revolved about a line on the nested part's face, with the imported+/// root active, joins the part (the half above the face) instead of starting a body of its own.+#[test]+fn a_revolve_from_a_nested_step_parts_face_joins_it() {+ let (mut s, _, _, _, c) = nested_part_face_sketch();+ let axis = run(&mut s, "sketch.line", json!({"points": [[c.x, c.y - 5.0], [c.x, c.y + 5.0]]}))["curves"][0].as_str().unwrap().to_string();+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [c.x + 1.0, c.y - 2.0], "p1": [c.x + 3.0, c.y + 2.0]}));+ run(&mut s, "sketch.finish", json!({}));+ run(&mut s, "solid.revolve", json!({"axis": axis, "angle": "360 deg", "operation": "join"}));+ assert_block_changed_by(&mut s, std::f64::consts::PI * (9.0 - 1.0) * 4.0 / 2.0);+}++/// #3223 for Loft: a loft from a circle on the nested part's face to one 5 mm above joins it.+#[test]+fn a_loft_from_a_nested_step_parts_face_joins_it() {+ let (mut s, sketch, top, _, c) = nested_part_face_sketch();+ run(&mut s, "sketch.circle.center", json!({"center": [c.x, c.y], "radius": 3}));+ run(&mut s, "sketch.finish", json!({}));+ let at = solidcraft_doc::apply_point(&s.doc.component_transform(s.active_component), top);+ let high =+ run(&mut s, "sketch.create", json!({"plane": {"face": [at.x, at.y, at.z]}, "offset": 5, "project_edges": false}))["sketch"].as_u64().unwrap();+ run(&mut s, "sketch.circle.center", json!({"center": [c.x, c.y], "radius": 3}));+ run(&mut s, "sketch.finish", json!({}));+ run(&mut s, "solid.loft", json!({"sections": [{"sketch": sketch}, {"sketch": high}], "operation": "join"}));+ assert_block_changed_by(&mut s, std::f64::consts::PI * 9.0 * 5.0);+}++/// #3223 for Sweep: a circle on the nested part's face swept 5 mm out along its normal joins it.+#[test]+fn a_sweep_from_a_nested_step_parts_face_joins_it() {+ let (mut s, sketch, top, n, c) = nested_part_face_sketch();+ run(&mut s, "sketch.circle.center", json!({"center": [c.x, c.y], "radius": 3}));+ run(&mut s, "sketch.finish", json!({}));+ let root = s.doc.component_transform(s.active_component);+ let (a, b) = (solidcraft_doc::apply_point(&root, top), solidcraft_doc::apply_point(&root, top + n * 5.0));+ let path = run(&mut s, "sketch.create", json!({"plane": "XY"}))["sketch"].as_u64().unwrap();+ run(&mut s, "sketch.options", json!({"sketch_3d": true}));+ let wire = run(&mut s, "sketch.line3d", json!({"points": [[a.x, a.y, a.z], [b.x, b.y, b.z]]}))["curves"][0].as_str().unwrap().to_string();+ run(&mut s, "sketch.finish", json!({}));+ run(&mut s, "solid.sweep", json!({"sketch": sketch, "path_sketch": path, "path": [wire], "operation": "join"}));+ assert_block_changed_by(&mut s, std::f64::consts::PI * 9.0 * 5.0);+}++/// #3223 for Hole: a hole at a point sketched on the nested part's face, with the imported root+/// active, drills the part instead of finding no body to cut.+#[test]+fn a_hole_at_a_point_on_a_nested_step_parts_face_drills_it() {+ let (mut s, sketch, _, _, c) = nested_part_face_sketch();+ run(&mut s, "sketch.point", json!({"point": [c.x, c.y]}));+ run(&mut s, "sketch.finish", json!({}));+ run(&mut s, "solid.hole", json!({"sketch": sketch, "diameter": 4, "depth": 5}));+ assert_block_changed_by(&mut s, -std::f64::consts::PI * 4.0 * 5.0);+}