Skip to content

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

Create Sketch: the flat faces of a cone take an attached sketch (#7476) (#7772)

semmelrinde51d8842e8a1e
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index c17156db80..89a7dedce1 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -5764,13 +5764,13 @@ fn feature_tools_with_constructor(
FeatureKind::Torus { center, major, minor, .. } => {
Ok(unnamed(vec![kernel::torus(point(vals, center)?, val(vals, major, Kind::Length)?, val(vals, minor, Kind::Length)?)?]))
}
- FeatureKind::Cone { base, axis, radius, top_radius, height, .. } => Ok(unnamed(vec![more::cone(
+ FeatureKind::Cone { base, axis, radius, top_radius, height, .. } => Ok(vec![more::cone(
point(vals, base)?,
axis.0,
val(vals, radius, Kind::Length)?,
val(vals, top_radius, Kind::Length)?,
val(vals, height, Kind::Length)?,
- )?])),
+ )?]),
FeatureKind::Hole { .. } => Ok(hole_feature_tools(vals, st, f)?.into_iter().map(|(tool, _)| tool).collect()),
_ => Err(DocError::Invalid(format!("{} cannot be patterned or mirrored yet", f.name))),
}
@@ -7988,8 +7988,8 @@ fn eval_feature(
FeatureKind::Cone { base, axis, radius, top_radius, height, operation } => {
let (radius, top_radius, height) =
(val(vals, radius, Kind::Length)?, val(vals, top_radius, Kind::Length)?, val(vals, height, Kind::Length)?);
- let b = more::cone(point(vals, base)?, axis.0, radius, top_radius, height)?;
- apply_op(doc, st, f, vec![b], *operation, &[])
+ let tool = more::cone(point(vals, base)?, axis.0, radius, top_radius, height)?;
+ apply_traced(doc, st, f, vec![tool], *operation, &[])
}
FeatureKind::Combine { target, tools, operation, keep_tools } => {
if tools.iter().any(|t| t == target) {
diff --git a/crates/doc/src/eval_more.rs b/crates/doc/src/eval_more.rs
index 06593f6ba1..c4177578d2 100644
--- a/crates/doc/src/eval_more.rs
+++ b/crates/doc/src/eval_more.rs
@@ -1441,7 +1441,8 @@ const MAX_CONE_SIZE: f64 = 1e7;
/// of `top_radius` `height` along `axis` (a negative height goes the other way). A top radius of
/// 0 (or below a micrometre) makes a full cone ending in an apex. Built, as the torus is, by
/// revolving its half section, a trapezoid (a triangle for a full cone) with an edge on the axis.
-pub(super) fn cone(base: Vec3, axis: Vec3, radius: f64, top_radius: f64, height: f64) -> Result<Body> {
+/// Its faces are named `bottom`, `side` and `top` ([`crate::lineage::cone_roles`]).
+pub(super) fn cone(base: Vec3, axis: Vec3, radius: f64, top_radius: f64, height: f64) -> Result<Traced> {
let in_range = |v: f64| v.is_finite() && v.abs() <= MAX_CONE_SIZE;
if !(in_range(base.x) && in_range(base.y) && in_range(base.z)) {
return Err(DocError::Invalid("the cone's base is out of range".into()));
@@ -1475,14 +1476,16 @@ pub(super) fn cone(base: Vec3, axis: Vec3, radius: f64, top_radius: f64, height:
(Some(b), true) => b,
_ => return Err(DocError::Invalid("the cone could not be built".into())),
};
+ // Read off the revolve's own history; moving the body keeps its faces' order.
+ let roles = crate::lineage::cone_roles(&body);
if (dir - Vec3::Z).len() < 1e-12 && base.len() == 0.0 {
- return Ok(body);
+ return Ok(Traced::made(body, roles));
}
// A rotation taking +Z onto the axis (x and y square to it, right-handed), then the base.
let x = Plane::from_normal(base, dir).ok_or_else(|| DocError::Invalid("the cone's axis has no direction".into()))?.x;
let y = dir.cross(x);
let m = [[x.x, x.y, x.z, 0.0], [y.x, y.y, y.z, 0.0], [dir.x, dir.y, dir.z, 0.0], [base.x, base.y, base.z, 1.0]];
- Ok(kernel::transform_matrix(&body, m)?)
+ Ok(Traced::made(kernel::transform_matrix(&body, m)?, roles))
}
/// A primitive tool's exact geometry, for [`sheet_primitive_cut`].
diff --git a/crates/doc/src/lineage.rs b/crates/doc/src/lineage.rs
index 386e1c3332..c2523006a7 100644
--- a/crates/doc/src/lineage.rs
+++ b/crates/doc/src/lineage.rs
@@ -429,6 +429,24 @@ pub(crate) fn cylinder_tool(body: Body) -> Traced {
Traced::made(body, roles)
}
+/// The roles of a cone primitive's faces, from the body revolved from its half section (base
+/// side first, then the sloped side, then the top side of a cut-off cone; the side on the axis
+/// makes no face): `bottom`, `side` and `top`, as a cylinder's are (#7476). `None` when the body
+/// has no history of its own.
+pub(crate) fn cone_roles(body: &Body) -> Option<Vec<Vec<String>>> {
+ body.history().filter(|h| h.operands == 0).map(|h| {
+ h.faces
+ .iter()
+ .map(|f| match f.origins.as_slice() {
+ [FaceOrigin::Profile { segment: 0 }] => vec!["bottom".to_string()],
+ [FaceOrigin::Profile { segment: 1 }] => vec!["side".to_string()],
+ [FaceOrigin::Profile { segment: 2 }] => vec!["top".to_string()],
+ _ => Vec::new(),
+ })
+ .collect()
+ })
+}
+
/// A tool a feature builds without a sketch (a boss's post, a rib, a lip's band), its faces named
/// by the part it makes: `<part>:top` (its end cap), `<part>:base` (its start cap) and
/// `<part>:side:<k>` (swept from segment `k` of its profile), as its construction records them.
diff --git a/crates/engine/src/cmd/lineage_face_tests.rs b/crates/engine/src/cmd/lineage_face_tests.rs
index 0c3346ba11..5b8c3e6fb3 100644
--- a/crates/engine/src/cmd/lineage_face_tests.rs
+++ b/crates/engine/src/cmd/lineage_face_tests.rs
@@ -116,6 +116,42 @@ fn a_chamfer_face_takes_a_sketch_that_follows_the_chamfer() {
healthy(&reopened(&s), "OnBevel");
}
+/// #7476: a cone's base and the flat top of a cut-off cone take a sketch, as a cylinder's caps
+/// do, on the origin and placed along a slanted axis. The sketch on the top follows the height.
+#[test]
+fn the_flat_faces_of_a_cone_take_a_sketch_that_follows_the_cone() {
+ use solidcore_record::Vec3;
+ let slant = Vec3::new(1., 1., 0.).normalized().unwrap();
+ for (base, axis, along) in [(Vec3::ZERO, json!("Z"), Vec3::Z), (Vec3::new(5., -3., 2.), json!([1, 1, 0]), slant)] {
+ let mut s = Session::default();
+ run(&mut s, "parameters.add", json!({"name": "tall", "expression": "20 mm"}));
+ let at = [base.x, base.y, base.z];
+ run(&mut s, "solid.cone", json!({"radius": 10, "top_radius": 4, "height": "tall", "base": at, "axis": axis, "name": "Cut"}));
+ run(&mut s, "solid.cone", json!({"radius": 3, "height": 5, "base": [40, 0, 0], "name": "Full"}));
+ no_errors(&s);
+ let top = |tall: f64| base + along * tall;
+ sketch_on(&mut s, [top(20.).x, top(20.).y, top(20.).z], "OnTop");
+ sketch_on(&mut s, at, "OnBase");
+ sketch_on(&mut s, [40., 0., 0.], "OnFullBase");
+ let height = |p: &solidcore_record::Plane| (p.origin - base).dot(along);
+ for (name, role) in [("OnTop", ":top"), ("OnBase", ":bottom"), ("OnFullBase", ":bottom")] {
+ let FaceAttachmentStatus::Healthy { plane, target, .. } = status(&s, name) else { panic!("{name}: {:?}", status(&s, name)) };
+ assert_eq!(target.certificate, Some(FaceCertificate::Lineage), "{name}");
+ assert!(target.name.ends_with(role), "{name}: {}", target.name);
+ let axis = if name == "OnFullBase" { Vec3::Z } else { along };
+ assert!(plane.normal().cross(axis).len() < 1e-9, "{name}: {plane:?}");
+ }
+ assert!((height(&healthy(&s, "OnTop").0) - 20.).abs() < 1e-9);
+ assert!(height(&healthy(&s, "OnBase").0).abs() < 1e-9);
+ run(&mut s, "parameters.change", json!({"name": "tall", "expression": "30 mm"}));
+ no_errors(&s);
+ assert!((height(&healthy(&s, "OnTop").0) - 30.).abs() < 1e-9);
+ assert!(height(&healthy(&s, "OnBase").0).abs() < 1e-9);
+ let t = reopened(&s);
+ assert!((height(&healthy(&t, "OnTop").0) - 30.).abs() < 1e-9);
+ }
+}
+
/// A plate with a boss patterned 3 times along x, `pitch` apart.
fn patterned_bosses(sketch_on_seed_first: bool) -> Session {
let mut s = Session::default();