Skip to content

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

Agents and scripts: the plastic features Boss, Lip, Snap Fit and Rest describe their exact parameters and reply (#12622) (#12623)

austin62jake6b5b49b2859e
diff --git a/crates/engine/src/cmd/plastic.rs b/crates/engine/src/cmd/plastic.rs
index 41571943e6..a875e6815d 100644
--- a/crates/engine/src/cmd/plastic.rs
+++ b/crates/engine/src/cmd/plastic.rs
@@ -12,8 +12,10 @@ use solidcraft_geom::prelude::*;
use super::CommandSpec;
use super::features::{add_feature, check_expr};
use crate::params::{bad, bool_, expr, req_expr, str_, string_list, vec3};
+use crate::schema::features::FeatureResult;
use crate::schema::plastic::{
- AssignRuleParams, AssignRuleResult, ManageRulesParams, ManageRulesResult, RuleExpressions, RuleInfo, RuleValuesOrError, RulesParams, RulesResult,
+ AssignRuleParams, AssignRuleResult, BossParams, LipParams, ManageRulesParams, ManageRulesResult, RestParams, RuleExpressions, RuleInfo,
+ RuleValuesOrError, RulesParams, RulesResult, SnapFitParams,
};
use crate::schema::{TypedContract, output_schema_for};
use crate::{EngineError, Result, Session};
@@ -21,23 +23,47 @@ use crate::{EngineError, Result, Session};
const MANAGE_RULES_CONTRACT: TypedContract<ManageRulesParams> = TypedContract::new();
const ASSIGN_RULE_CONTRACT: TypedContract<AssignRuleParams> = TypedContract::new();
const RULES_CONTRACT: TypedContract<RulesParams> = TypedContract::new();
+const BOSS_CONTRACT: TypedContract<BossParams> = TypedContract::new();
+const LIP_CONTRACT: TypedContract<LipParams> = TypedContract::new();
+const SNAP_FIT_CONTRACT: TypedContract<SnapFitParams> = TypedContract::new();
+const REST_CONTRACT: TypedContract<RestParams> = TypedContract::new();
pub static COMMANDS: &[CommandSpec] = &[
- CommandSpec::new("plastic.boss", "Boss", boss).at("PLASTIC", "CREATE").icon("boss").params(
- "position: [x,y,z] on a face; direction? (default: out of the face); diameter, height; hole_diameter?, hole_depth? (default: to the face); \
- draft? (angle), fillet? (root radius); ribs? (count), rib_thickness?, rib_length? (out from the wall), rib_offset? (below the top); body?",
- ),
- CommandSpec::new("plastic.lip", "Lip", lip).at("PLASTIC", "CREATE").icon("lip").params(
- "face: [x,y,z] on the rim face of a shelled body; width, height; type?: lip|groove; gap? (groove clearance); side?: inside|outside (which rim edge); body?",
- ),
- CommandSpec::new("plastic.snap_fit", "Snap Fit", snap_fit).at("PLASTIC", "CREATE").icon("snap_fit").params(
- "position: [x,y,z] on a face; direction? (default: out of the face); hook: [x,y,z] (the catch's direction); length, thickness, width, catch_depth, catch_length; \
- base_height? (extends the base below the face); new_body?: bool (make a separate body instead of joining); body?",
- ),
- CommandSpec::new("plastic.rest", "Rest", rest).at("PLASTIC", "CREATE").icon("rest").params(
- "position: [x,y,z] on a face (the rest's centre); direction? (default: out of the face); width (round: the diameter); length? (a rectangle, along `along`); along?: [x,y,z]; \
- height; draft? (angle; default: the body's plastic rule); thickness? (wall of a hollow rest); body?",
- ),
+ CommandSpec::new("plastic.boss", "Boss", crate::checked!("plastic.boss", boss))
+ .at("PLASTIC", "CREATE")
+ .icon("boss")
+ .contract(&BOSS_CONTRACT)
+ .output(output_schema_for::<FeatureResult>)
+ .params(
+ "position: [x,y,z] on a face; direction? (default: out of the face); diameter, height; hole_diameter?, hole_depth? (default: to the face); \
+ draft? (angle), fillet? (root radius); ribs? (count), rib_thickness?, rib_length? (out from the wall), rib_offset? (below the top); body?",
+ ),
+ CommandSpec::new("plastic.lip", "Lip", crate::checked!("plastic.lip", lip))
+ .at("PLASTIC", "CREATE")
+ .icon("lip")
+ .contract(&LIP_CONTRACT)
+ .output(output_schema_for::<FeatureResult>)
+ .params(
+ "face: [x,y,z] on the rim face of a shelled body; width, height; type?: lip|groove; gap? (groove clearance); side?: inside|outside (which rim edge); body?",
+ ),
+ CommandSpec::new("plastic.snap_fit", "Snap Fit", crate::checked!("plastic.snap_fit", snap_fit))
+ .at("PLASTIC", "CREATE")
+ .icon("snap_fit")
+ .contract(&SNAP_FIT_CONTRACT)
+ .output(output_schema_for::<FeatureResult>)
+ .params(
+ "position: [x,y,z] on a face; direction? (default: out of the face); hook: [x,y,z] (the catch's direction); length, thickness, width, catch_depth, catch_length; \
+ base_height? (extends the base below the face); new_body?: bool (make a separate body instead of joining); body?",
+ ),
+ CommandSpec::new("plastic.rest", "Rest", crate::checked!("plastic.rest", rest))
+ .at("PLASTIC", "CREATE")
+ .icon("rest")
+ .contract(&REST_CONTRACT)
+ .output(output_schema_for::<FeatureResult>)
+ .params(
+ "position: [x,y,z] on a face (the rest's centre); direction? (default: out of the face); width (round: the diameter); length? (a rectangle, along `along`); along?: [x,y,z]; \
+ height; draft? (angle; default: the body's plastic rule); thickness? (wall of a hollow rest); body?",
+ ),
CommandSpec::new("plastic.manage_rules", "Manage Plastic Rules", crate::checked!("plastic.manage_rules", manage_rules))
.at("PLASTIC", "SETUP")
.icon("plastic_rule")
diff --git a/crates/engine/src/schema/plastic.rs b/crates/engine/src/schema/plastic.rs
index 232c4473b2..3e1558daa6 100644
--- a/crates/engine/src/schema/plastic.rs
+++ b/crates/engine/src/schema/plastic.rs
@@ -1,24 +1,30 @@
-//! Contracts and reply types for the plastic rule commands in `cmd/plastic.rs` (#12313): Manage,
-//! Assign and List Plastic Rules. A plastic rule holds a plastic part's design values, which
-//! Boss, Lip, Snap Fit and Rest take as defaults.
+//! Contracts and reply types for the commands in `cmd/plastic.rs`: the plastic rule commands
+//! Manage, Assign and List Plastic Rules (#12313), and the plastic features Boss, Lip, Snap Fit
+//! and Rest (#12622). A plastic rule holds a plastic part's design values, which the features
+//! take as defaults.
//!
//! The handlers read their parameters from the raw JSON, so each key here reads as its handler
-//! reads it and fails with the handler's message. Whether a rule or a body exists, and whether
-//! an expression evaluates, stay the handlers' to check. Where a handler ignores a key of the
-//! wrong type (a `delete` that isn't true or false, a rule value that is neither a number nor
-//! text), the contract refuses it.
+//! reads it and fails with the handler's message. Whether a rule, a body or a face exists, and
+//! whether an expression evaluates and fits the part, stay the handlers' to check. Where a
+//! handler ignores a key of the wrong type (a `delete` that isn't true or false, a rule value or
+//! a feature's optional size that is neither a number nor text), the contract refuses it.
+use std::borrow::Cow;
use std::collections::BTreeMap;
use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
use serde::de::Error as _;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;
+use solidcore_record::Vec3;
use solidcraft_doc::plastic::{PlasticRule, PlasticValues};
+use solidcraft_geom::Vec3Geom as _;
use super::check::whole;
use super::config::NameList;
-use super::types::{AngleExpr, LengthExpr, not_blank_pattern};
+use super::types::{
+ AngleExpr, FeatureCommon, LengthExpr, LipSideParam, LipTypeParam, NumberExpr, Point3, TouchingParam, feature_common, not_blank_pattern, present,
+};
/// The longest rule name, in bytes once trimmed, as `plastic.manage_rules` reads it.
const MAX_RULE_NAME_BYTES: usize = 128;
@@ -275,6 +281,239 @@ pub struct RulesResult {
pub param_warnings: Vec<String>,
}
+// ---- Plastic features (#12622) ----
+//
+// Each feature joins the body it stands on (a groove cuts it) and replies as every feature
+// command does, with `FeatureResult`. The common keys (`name`, `body_name`, `body_names`,
+// `participants`) are `FeatureCommon`'s; `touching` is declared here because the features join.
+
+/// `v` as `params::vec3` reads it. With `direction`, it must also have a length, since the
+/// handlers make it a unit vector and refuse a zero one.
+fn vector(v: &Value, direction: bool) -> Option<Vec3> {
+ let p = crate::params::vec3(v)?;
+ if direction { p.normalized() } else { Some(p) }
+}
+
+/// A point or direction key of the plastic features: read as [`vector`] reads it, refused with
+/// the handler's whole message, published as a [`Point3`] with the key's own description.
+macro_rules! plastic_vector {
+ ($(#[$doc:meta])* $name:ident, direction: $direction:literal, $message:literal, $description:literal) => {
+ $(#[$doc])*
+ #[derive(Clone, Copy, Debug, PartialEq)]
+ pub struct $name(pub Vec3);
+
+ impl<'de> Deserialize<'de> for $name {
+ fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
+ vector(&Value::deserialize(d)?, $direction).map($name).ok_or_else(|| D::Error::custom(whole($message)))
+ }
+ }
+
+ impl JsonSchema for $name {
+ fn schema_name() -> Cow<'static, str> {
+ stringify!($name).into()
+ }
+ fn inline_schema() -> bool {
+ true
+ }
+ fn json_schema(generator: &mut SchemaGenerator) -> Schema {
+ let mut schema = generator.subschema_for::<Point3>();
+ schema.insert("description".into(), $description.into());
+ schema
+ }
+ }
+ };
+}
+
+plastic_vector!(
+ /// Boss, Snap Fit and Rest's `position`.
+ FacePosition,
+ direction: false,
+ "`position` must be a point [x, y, z] on a face",
+ "A point in mm on the face the feature stands on: [x, y, z] or {\"x\", \"y\", \"z\"}."
+);
+plastic_vector!(
+ /// Boss, Snap Fit and Rest's `direction`.
+ FaceDirection,
+ direction: true,
+ "`direction` must be a non-zero [x, y, z]",
+ "The direction the feature stands out of the face, not zero. Default: square out of the face at `position`, which must then be planar."
+);
+plastic_vector!(
+ /// Lip's `face`.
+ RimFace,
+ direction: false,
+ "`face` must be a point [x, y, z] on the rim face",
+ "A point in mm on the planar rim face of a shelled body, round its opening: [x, y, z] or {\"x\", \"y\", \"z\"}."
+);
+plastic_vector!(
+ /// Snap Fit's `hook`.
+ HookDirection,
+ direction: true,
+ "`hook` must be the catch's direction [x, y, z]",
+ "The direction the catch sticks out of the arm: across `direction`, not zero."
+);
+plastic_vector!(
+ /// Rest's `along`.
+ AlongDirection,
+ direction: true,
+ "`along` must be a direction across the face",
+ "The direction of a rectangular rest's `length`: across `direction`, not zero."
+);
+
+/// Add a boss: a round post standing on a face, for a screw, with an optional hole, draft, root
+/// fillet and ribs. It joins the body it stands on. One undo step.
+#[derive(Debug, Deserialize, JsonSchema)]
+#[schemars(deny_unknown_fields)]
+pub struct BossParams {
+ pub position: FacePosition,
+ #[serde(default, deserialize_with = "present", skip_serializing_if = "Option::is_none")]
+ #[schemars(with = "FaceDirection")]
+ pub direction: Option<FaceDirection>,
+ /// The post's diameter at the face, at least 0.01 mm.
+ pub diameter: LengthExpr,
+ /// How far the post stands out of the face, at least 0.01 mm.
+ pub height: LengthExpr,
+ /// The screw hole's diameter, down from the post's top. Default: no hole.
+ #[serde(default)]
+ pub hole_diameter: Option<LengthExpr>,
+ /// How deep the hole goes from the post's top. Default: down to the face (the post's height).
+ #[serde(default)]
+ pub hole_depth: Option<LengthExpr>,
+ /// The post's draft: it narrows towards its top by this angle. Default: the body's plastic
+ /// rule's draft, or none.
+ #[serde(default)]
+ pub draft: Option<AngleExpr>,
+ /// The radius of the round where the post meets the face. Default: none.
+ #[serde(default)]
+ pub fillet: Option<LengthExpr>,
+ /// How many ribs brace the post, evenly round it: 0 to 64. Default: none.
+ #[serde(default)]
+ pub ribs: Option<NumberExpr>,
+ /// Each rib's thickness. Default: 0.3 times the post's radius.
+ #[serde(default)]
+ pub rib_thickness: Option<LengthExpr>,
+ /// How far each rib reaches out from the post's wall. Default: the post's radius.
+ #[serde(default)]
+ pub rib_length: Option<LengthExpr>,
+ /// How far below the post's top the ribs stop. Default: 0, the ribs reach the top.
+ #[serde(default)]
+ pub rib_offset: Option<LengthExpr>,
+ /// The body the boss joins, by name. Default: the body at `position`.
+ #[serde(default)]
+ pub body: Option<String>,
+ /// What a join makes of pieces that share no face. Default: separate.
+ #[serde(default)]
+ pub touching: Option<TouchingParam>,
+ #[serde(flatten, deserialize_with = "feature_common")]
+ pub common: FeatureCommon,
+}
+
+/// Add a lip or a groove: a band along one edge of a shelled body's rim face, raised for a lip
+/// and cut for a groove, so two halves of a case fit together. A lip joins the body and a groove
+/// cuts it. One undo step.
+#[derive(Debug, Deserialize, JsonSchema)]
+#[schemars(deny_unknown_fields)]
+pub struct LipParams {
+ pub face: RimFace,
+ /// The band's width across the rim.
+ pub width: LengthExpr,
+ /// A lip's height above the rim, or a groove's depth into it.
+ pub height: LengthExpr,
+ #[serde(default, rename = "type")]
+ pub kind: Option<LipTypeParam>,
+ /// A groove's clearance: it is this much wider than `width`, at least 0. Default: the body's
+ /// plastic rule's clearance, or none. A lip doesn't use it.
+ #[serde(default)]
+ pub gap: Option<LengthExpr>,
+ #[serde(default)]
+ pub side: Option<LipSideParam>,
+ /// True: the band runs along the outside edge, as `side: outside` does. Default: false.
+ #[serde(default)]
+ pub outside: Option<bool>,
+ /// The shelled body, by name. Default: the body at `face`.
+ #[serde(default)]
+ pub body: Option<String>,
+ /// What a lip's join makes of pieces that share no face. Default: separate.
+ #[serde(default)]
+ pub touching: Option<TouchingParam>,
+ #[serde(flatten, deserialize_with = "feature_common")]
+ pub common: FeatureCommon,
+}
+
+/// Add a snap fit: a cantilever arm standing on a face with a catch at its tip, which clips into
+/// another part. It joins the body it stands on, or makes a body of its own. One undo step.
+#[derive(Debug, Deserialize, JsonSchema)]
+#[schemars(deny_unknown_fields)]
+pub struct SnapFitParams {
+ pub position: FacePosition,
+ #[serde(default, deserialize_with = "present", skip_serializing_if = "Option::is_none")]
+ #[schemars(with = "FaceDirection")]
+ pub direction: Option<FaceDirection>,
+ pub hook: HookDirection,
+ /// The arm's length out of the face, at least 0.01 mm.
+ pub length: LengthExpr,
+ /// The arm's thickness, along `hook`, at least 0.01 mm.
+ pub thickness: LengthExpr,
+ /// The arm's width, across `hook`, at least 0.01 mm.
+ pub width: LengthExpr,
+ /// How far the catch sticks out of the arm, at least 0.01 mm.
+ pub catch_depth: LengthExpr,
+ /// The catch's length along the arm, at the arm's tip: at least 0.01 mm and shorter than
+ /// `length`.
+ pub catch_length: LengthExpr,
+ /// How far the arm's base reaches below the face, into the wall, at least 0.01 mm. Default:
+ /// none, the arm starts at the face.
+ #[serde(default)]
+ pub base_height: Option<LengthExpr>,
+ /// True: the snap fit is a body of its own instead of joining the body. Default: false.
+ #[serde(default)]
+ pub new_body: Option<bool>,
+ /// The body it stands on, by name. Default: the body at `position`.
+ #[serde(default)]
+ pub body: Option<String>,
+ /// What a join makes of pieces that share no face. Default: separate.
+ #[serde(default)]
+ pub touching: Option<TouchingParam>,
+ #[serde(flatten, deserialize_with = "feature_common")]
+ pub common: FeatureCommon,
+}
+
+/// Add a rest: a pad standing on a face for another part to rest on, round or rectangular, solid
+/// or hollow. It joins the body it stands on. One undo step.
+#[derive(Debug, Deserialize, JsonSchema)]
+#[schemars(deny_unknown_fields)]
+pub struct RestParams {
+ pub position: FacePosition,
+ #[serde(default, deserialize_with = "present", skip_serializing_if = "Option::is_none")]
+ #[schemars(with = "FaceDirection")]
+ pub direction: Option<FaceDirection>,
+ /// The pad's width at the face: a round rest's diameter.
+ pub width: LengthExpr,
+ /// A rectangular rest's length, along `along`. Default: a round rest.
+ #[serde(default)]
+ pub length: Option<LengthExpr>,
+ #[serde(default, deserialize_with = "present", skip_serializing_if = "Option::is_none")]
+ #[schemars(with = "AlongDirection")]
+ pub along: Option<AlongDirection>,
+ /// How far the pad stands out of the face, at least 0.01 mm.
+ pub height: LengthExpr,
+ /// The pad's draft, under 57°: it narrows towards its top by this angle. Default: the body's
+ /// plastic rule's draft, or none.
+ #[serde(default)]
+ pub draft: Option<AngleExpr>,
+ /// A hollow rest's wall thickness. Default: a solid rest.
+ #[serde(default)]
+ pub thickness: Option<LengthExpr>,
+ /// The body it stands on, by name. Default: the body at `position`.
+ #[serde(default)]
+ pub body: Option<String>,
+ /// What a join makes of pieces that share no face. Default: separate.
+ #[serde(default)]
+ pub touching: Option<TouchingParam>,
+ #[serde(flatten, deserialize_with = "feature_common")]
+ pub common: FeatureCommon,
+}
+
#[cfg(test)]
#[path = "plastic_tests.rs"]
mod tests;
diff --git a/crates/engine/src/schema/plastic_tests.rs b/crates/engine/src/schema/plastic_tests.rs
index d4e14f49b2..eaa1179776 100644
--- a/crates/engine/src/schema/plastic_tests.rs
+++ b/crates/engine/src/schema/plastic_tests.rs
@@ -1,7 +1,11 @@
use serde_json::{Value, json};
-use super::{AssignRuleParams, AssignRuleResult, ManageRulesParams, ManageRulesResult, RulesParams, RulesResult};
+use super::{
+ AssignRuleParams, AssignRuleResult, BossParams, LipParams, ManageRulesParams, ManageRulesResult, RestParams, RulesParams, RulesResult,
+ SnapFitParams,
+};
use crate::Session;
+use crate::schema::features::FeatureResult;
use crate::schema::tests::{assert_keys, assert_strict_keeps_todays_errors, conforms};
use crate::schema::{ParamCheck, input_schema_for, output_schema_for};
@@ -173,3 +177,289 @@ fn plastic_rules_reply_with_their_schemas() {
let r = run(&mut s, "plastic.manage_rules", json!({"name": "Mine", "delete": true}), output_schema_for::<ManageRulesResult>());
assert_eq!(r["deleted"], "Mine", "{r}");
}
+
+// ---- Plastic features (#12622) ----
+
+/// A 40 × 40 × 4 mm plate, `Body1`, whose top face (z = 4) the features stand on.
+fn plate(level: ParamCheck) -> Session {
+ let mut s = Session { param_check: level, ..Session::default() };
+ s.execute("solid.box", &json!({"length": 40, "width": 40, "height": 4})).unwrap_or_else(|e| panic!("{e}"));
+ s
+}
+
+/// An 80 × 50 × 30 mm box shelled 2 mm thick with its top open: the rim face is at z = 30.
+fn shelled(level: ParamCheck) -> Session {
+ let mut s = Session { param_check: level, ..Session::default() };
+ for (id, p) in
+ [("solid.box", json!({"length": 80, "width": 50, "height": 30})), ("solid.shell", json!({"faces": [[40, 25, 30]], "thickness": 2}))]
+ {
+ s.execute(id, &p).unwrap_or_else(|e| panic!("{id}: {e}"));
+ }
+ s
+}
+
+/// `base` with `key` set to `value`.
+fn with(base: &Value, key: &str, value: Value) -> Value {
+ let mut p = base.clone();
+ p[key] = value;
+ p
+}
+
+fn boss() -> Value {
+ json!({"position": [20, 20, 4], "diameter": 6, "height": 5})
+}
+
+fn lip() -> Value {
+ json!({"face": [1, 25, 30], "width": 1, "height": 2})
+}
+
+fn snap_fit() -> Value {
+ json!({"position": [20, 20, 4], "hook": [1, 0, 0], "length": 15, "thickness": 1.5, "width": 6, "catch_depth": 1, "catch_length": 3})
+}
+
+fn rest() -> Value {
+ json!({"position": [20, 20, 4], "width": 6, "height": 2})
+}
+
+#[test]
+fn the_feature_schemas_and_the_key_check_agree() {
+ assert_keys(
+ input_schema_for::<BossParams>(),
+ &[
+ "body",
+ "body_name",
+ "body_names",
+ "diameter",
+ "direction",
+ "draft",
+ "fillet",
+ "height",
+ "hole_depth",
+ "hole_diameter",
+ "name",
+ "participants",
+ "position",
+ "rib_length",
+ "rib_offset",
+ "rib_thickness",
+ "ribs",
+ "touching",
+ ],
+ );
+ assert_keys(
+ input_schema_for::<LipParams>(),
+ &["body", "body_name", "body_names", "face", "gap", "height", "name", "outside", "participants", "side", "touching", "type", "width"],
+ );
+ assert_keys(
+ input_schema_for::<SnapFitParams>(),
+ &[
+ "base_height",
+ "body",
+ "body_name",
+ "body_names",
+ "catch_depth",
+ "catch_length",
+ "direction",
+ "hook",
+ "length",
+ "name",
+ "new_body",
+ "participants",
+ "position",
+ "thickness",
+ "touching",
+ "width",
+ ],
+ );
+ assert_keys(
+ input_schema_for::<RestParams>(),
+ &[
+ "along",
+ "body",
+ "body_name",
+ "body_names",
+ "direction",
+ "draft",
+ "height",
+ "length",
+ "name",
+ "participants",
+ "position",
+ "thickness",
+ "touching",
+ "width",
+ ],
+ );
+}
+
+/// #12622: calls to the plastic features that fail today get the same message in strict. They
+/// fail before the feature is built, so the shared helper's sketch, which has no body, does for
+/// them. A required key left out is the exception the helper allows: strict says it is required.
+#[test]
+fn strict_keeps_the_features_errors() {
+ let up = |p: Value| with(&p, "direction", json!([0, 0, 1]));
+ assert_strict_keeps_todays_errors(&[
+ ("plastic.boss", with(&boss(), "position", json!(null))),
+ ("plastic.boss", with(&boss(), "position", json!([20, 20]))),
+ ("plastic.boss", with(&boss(), "position", json!("top"))),
+ ("plastic.boss", with(&boss(), "direction", json!([0, 0, 0]))),
+ ("plastic.boss", with(&boss(), "direction", json!(null))),
+ ("plastic.boss", with(&boss(), "direction", json!("up"))),
+ // No direction, and no planar face at the point to stand out of.
+ ("plastic.boss", boss()),
+ ("plastic.boss", with(&up(boss()), "diameter", json!(null))),
+ ("plastic.boss", with(&up(boss()), "diameter", json!(true))),
+ ("plastic.boss", with(&up(boss()), "diameter", json!(" "))),
+ ("plastic.boss", with(&up(boss()), "diameter", json!(1e9))),
+ ("plastic.boss", with(&up(boss()), "diameter", json!("1".repeat(4097)))),
+ ("plastic.boss", with(&up(boss()), "height", json!("nope + 1"))),
+ ("plastic.boss", with(&up(boss()), "hole_diameter", json!("nope"))),
+ ("plastic.boss", with(&up(boss()), "ribs", json!("2 mm"))),
+ ("plastic.boss", with(&up(boss()), "draft", json!("2 mm"))),
+ ("plastic.lip", with(&lip(), "face", json!(null))),
+ ("plastic.lip", with(&lip(), "face", json!([1, 25]))),
+ ("plastic.lip", with(&lip(), "width", json!(null))),
+ ("plastic.lip", with(&lip(), "height", json!([]))),
+ ("plastic.lip", with(&lip(), "type", json!("tongue"))),
+ ("plastic.lip", with(&lip(), "type", json!("TONGUE"))),
+ ("plastic.lip", with(&lip(), "side", json!("left"))),
+ ("plastic.snap_fit", with(&up(snap_fit()), "hook", json!(null))),
+ ("plastic.snap_fit", with(&up(snap_fit()), "hook", json!([0, 0, 0]))),
+ ("plastic.snap_fit", with(&up(snap_fit()), "hook", json!("x"))),
+ // A hook along the arm.
+ ("plastic.snap_fit", with(&up(snap_fit()), "hook", json!([0, 0, 1]))),
+ ("plastic.snap_fit", with(&up(snap_fit()), "catch_length", json!(true))),
+ ("plastic.rest", with(&up(rest()), "along", json!(null))),
+ ("plastic.rest", with(&up(rest()), "along", json!([0, 0, 0]))),
+ // `along` along the rest's direction, not across the face.
+ ("plastic.rest", with(&up(rest()), "along", json!([0, 0, 2]))),
+ ("plastic.rest", with(&up(rest()), "width", json!(false))),
+ ]);
+ // `add_feature` refuses an unknown `touching` without the command's name, as it does for
+ // every feature; strict gives the same words after the command's name.
+ let p = with(&up(boss()), "touching", json!("glued"));
+ let error = |level: ParamCheck| Session { param_check: level, ..Session::default() }.execute("plastic.boss", &p).err().map(|e| e.to_string());
+ let off = error(ParamCheck::Off).unwrap_or_default();
+ assert_eq!(off, "`touching` must be separate or one_body");
+ assert_eq!(error(ParamCheck::Strict), Some(format!("invalid parameters for `plastic.boss`: {off}")));
+}
+
+/// #12622: optional keys of the wrong type that the features ignore today (the feature is then
+/// built without them) are refused by the contracts: in Warn the call still works, with a
+/// warning naming the key.
+#[test]
+fn the_feature_contracts_refuse_what_the_handlers_ignore() {
+ for (id, p, key) in [
+ ("plastic.boss", with(&boss(), "hole_diameter", json!(true)), "hole_diameter"),
+ ("plastic.boss", with(&boss(), "draft", json!("")), "draft"),
+ ("plastic.boss", with(&boss(), "ribs", json!([])), "ribs"),
+ ("plastic.boss", with(&boss(), "fillet", json!(1e9)), "fillet"),
+ ("plastic.boss", with(&boss(), "body", json!(5)), "body"),
+ ("plastic.lip", with(&lip(), "type", json!(5)), "type"),
+ ("plastic.lip", with(&lip(), "side", json!(true)), "side"),
+ ("plastic.lip", with(&lip(), "outside", json!("yes")), "outside"),
+ ("plastic.lip", with(&lip(), "gap", json!({})), "gap"),
+ ("plastic.snap_fit", with(&snap_fit(), "new_body", json!("no")), "new_body"),
+ ("plastic.snap_fit", with(&snap_fit(), "base_height", json!(false)), "base_height"),
+ ("plastic.rest", with(&rest(), "thickness", json!(true)), "thickness"),
+ ("plastic.rest", with(&rest(), "length", json!("")), "length"),
+ ] {
+ let fixture: fn(ParamCheck) -> Session = if id == "plastic.lip" { shelled } else { plate };
+ assert!(fixture(ParamCheck::Off).execute(id, &p).is_ok(), "{p} works today");
+ let warned = fixture(ParamCheck::Warn).execute(id, &p).unwrap_or_else(|e| panic!("{p}: {e}"));
+ assert!(warned["param_warnings"].to_string().contains(key), "{warned}");
+ let error = fixture(ParamCheck::Strict).execute(id, &p).err().map(|e| e.to_string()).unwrap_or_default();
+ assert!(error.contains(&format!("`{key}`")), "{p}: {error}");
+ }
+}
+
+/// #12622: the feature schemas take what the handlers take. Each call is checked against both
+/// Boss's schema and its handler on the plate, with the check off, so the two can't drift apart.
+/// Lip's, Snap Fit's and Rest's schemas are checked on their own keys.
+#[test]
+fn the_feature_schemas_take_what_the_handlers_take() {
+ let schema = input_schema_for::<BossParams>();
+ let schema = schema.as_value();
+ for p in [
+ boss(),
+ with(&boss(), "position", json!({"x": 20, "y": 20, "z": 4})),
+ with(&boss(), "position", json!([20, 20])),
+ with(&boss(), "direction", json!([0, 0, 1])),
+ with(&boss(), "diameter", json!("6 mm")),
+ with(&boss(), "diameter", json!("")),
+ with(&boss(), "diameter", json!(1e9)),
+ with(&boss(), "hole_diameter", json!(2)),
+ with(&boss(), "ribs", json!(4)),
+ with(&boss(), "ribs", json!("4")),
+ with(&boss(), "draft", json!("1 deg")),
+ with(&boss(), "touching", json!("one_body")),
+ with(&boss(), "touching", json!("glued")),
+ with(&boss(), "body", json!("Body1")),
+ with(&boss(), "name", json!("Post")),
+ ] {
+ let takes = plate(ParamCheck::Off).execute("plastic.boss", &p).is_ok();
+ assert_eq!(conforms(schema, schema, &p), takes, "{p}");
+ }
+ for (p, takes) in [
+ (lip(), true),
+ (with(&lip(), "type", json!("groove")), true),
+ (with(&lip(), "side", json!("outside")), true),
+ (with(&lip(), "outside", json!(true)), true),
+ (with(&lip(), "type", json!("tongue")), false),
+ (with(&lip(), "outside", json!("yes")), false),
+ ] {
+ let schema = input_schema_for::<LipParams>();
+ assert_eq!(conforms(schema.as_value(), schema.as_value(), &p), takes, "{p}");
+ }
+ for (p, takes) in [
+ (snap_fit(), true),
+ (with(&snap_fit(), "new_body", json!(true)), true),
+ (with(&snap_fit(), "hook", json!(null)), false),
+ (with(&snap_fit(), "base_height", json!(true)), false),
+ ] {
+ let schema = input_schema_for::<SnapFitParams>();
+ assert_eq!(conforms(schema.as_value(), schema.as_value(), &p), takes, "{p}");
+ }
+ for (p, takes) in [
+ (rest(), true),
+ (with(&with(&rest(), "length", json!(10)), "along", json!([1, 0, 0])), true),
+ (with(&rest(), "along", json!(null)), false),
+ (with(&rest(), "thickness", json!(true)), false),
+ ] {
+ let schema = input_schema_for::<RestParams>();
+ assert_eq!(conforms(schema.as_value(), schema.as_value(), &p), takes, "{p}");
+ }
+}
+
+/// #12622: each plastic feature replies as a feature command does, in Warn without warnings: a
+/// boss with a hole and ribs, a lip and a groove (its type in capitals, as the handler reads it),
+/// a snap fit as a body of its own, and a rectangular hollow rest. A misspelt key is ignored
+/// today: Warn says so, and strict refuses it.
+#[test]
+fn plastic_features_reply_with_their_schemas() {
+ let schema = output_schema_for::<FeatureResult>();
+ let schema = schema.as_value();
+ let check = |s: &mut Session, id: &str, p: Value| {
+ let r = s.execute(id, &p).unwrap_or_else(|e| panic!("{id} {p}: {e}"));
+ assert!(conforms(schema, schema, &r), "{r} doesn't match {schema}");
+ assert!(r.get("param_warnings").is_none(), "{r}");
+ r
+ };
+ let mut s = plate(ParamCheck::Warn);
+ let r =
+ check(&mut s, "plastic.boss", json!({"position": [10, 10, 4], "diameter": 6, "height": 5, "hole_diameter": 2, "ribs": 4, "name": "Post"}));
+ assert_eq!((&r["name"], &r["changed_bodies"]), (&json!("Post"), &json!(["Body1"])), "{r}");
+ let r = check(&mut s, "plastic.snap_fit", with(&with(&snap_fit(), "position", json!([30, 30, 4])), "new_body", json!(true)));
+ assert_eq!(r["created_bodies"].as_array().map(Vec::len), Some(1), "a body of its own: {r}");
+ check(&mut s, "plastic.rest", json!({"position": [30, 10, 4], "width": 6, "length": 8, "along": [1, 0, 0], "height": 2, "thickness": 1}));
+ let mut s = shelled(ParamCheck::Warn);
+ check(&mut s, "plastic.lip", lip());
+ let mut s = shelled(ParamCheck::Warn);
+ check(&mut s, "plastic.lip", with(&with(&lip(), "type", json!("GROOVE")), "side", json!("Outside")));
+ // A misspelt key.
+ let p = with(&boss(), "hole_dia", json!(2));
+ let r = plate(ParamCheck::Warn).execute("plastic.boss", &p).unwrap_or_else(|e| panic!("{p}: {e}"));
+ assert!(r["param_warnings"].to_string().contains("hole_dia"), "{r}");
+ let error = plate(ParamCheck::Strict).execute("plastic.boss", &p).err().map(|e| e.to_string()).unwrap_or_default();
+ assert!(error.contains("`hole_dia`"), "{error}");
+}
diff --git a/crates/engine/src/schema/types.rs b/crates/engine/src/schema/types.rs
index 6abb3bf331..9bff4188ca 100644
--- a/crates/engine/src/schema/types.rs
+++ b/crates/engine/src/schema/types.rs
@@ -531,6 +531,22 @@ keyword_param!(
"mitre or swept",
"How two rounds turn round a sharp, straight, convex side edge square to both on a plane (the rounds along a boss's base, at its upright edges): mitre (the default) runs them on to meet in a mitre; swept ends them at the side edge and fills the corner with their section revolved about it, a torus. swept takes type: constant, corner: rolling, no setbacks and fit: exact or reduce, and is refused with the reason where the rounds are unequal or the side edge is concave, curved or tangent. Matching ignores case."
);
+keyword_param!(
+ /// `plastic.lip`'s `type`.
+ LipTypeParam,
+ "type",
+ &["lip", "groove"],
+ "lip or groove",
+ "What the feature makes along the rim: lip (the default), a raised band, or groove, a band cut into the rim for another part's lip. Matching ignores case."
+);
+keyword_param!(
+ /// `plastic.lip`'s `side`.
+ LipSideParam,
+ "side",
+ &["inside", "outside"],
+ "inside or outside",
+ "Which edge of the rim face the band runs along: inside (the default) or outside. Matching ignores case."
+);
/// How a feature combines with existing bodies, as `features::operation` reads it.
#[derive(Clone, Debug, PartialEq)]
diff --git a/crates/engine/src/schema/unconverted.txt b/crates/engine/src/schema/unconverted.txt
index 7b0e7dc020..1a0ab3f87a 100644
--- a/crates/engine/src/schema/unconverted.txt
+++ b/crates/engine/src/schema/unconverted.txt
@@ -79,10 +79,6 @@ parameters.graph
parameters.import
parameters.list
parameters.users
-plastic.boss
-plastic.lip
-plastic.rest
-plastic.snap_fit
render.aging.describe
render.backends
render.cancel