Skip to content

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

Extrude and loft: refuse duplicate profile indexes (#5023) (#5028)

raykholo1a5fcd05d6e0
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index e1d8a57ec1..d8a982a672 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -2156,8 +2156,10 @@ pub fn select_profiles(ss: &SolvedSketch, sel: &ProfileSel) -> Result<Vec<Region
solidcraft_sketch::without_text_counters(&ss.sketch, chosen).into_iter().map(|p| p.region).collect()
}
ProfileSel::Indices { indices } => {
+ // A design file may list a profile more than once; it is still one profile.
+ let mut seen = std::collections::BTreeSet::new();
let mut v = Vec::new();
- for i in indices {
+ for i in indices.iter().filter(|i| seen.insert(**i)) {
v.push(ps.get(*i).ok_or_else(|| DocError::Unknown(format!("profile {i} of {}", ss.name)))?.region.clone());
}
v
diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs
index 4b0bfd3935..216b126683 100644
--- a/crates/engine/src/cmd/features.rs
+++ b/crates/engine/src/cmd/features.rs
@@ -307,9 +307,30 @@ pub(super) fn stable_profiles(s: &Session, kind: &mut FeatureKind) -> Result<()>
for (sketch, sel) in kind.profile_sels_mut() {
let Some(ss) = st.sketch(sketch) else { continue };
let indices = match sel {
- ProfileSel::Indices { .. } => solidcraft_doc::profile_selection_indices(ss, sel)?,
+ ProfileSel::Indices { indices } => {
+ let have = ss.profiles.len();
+ if indices.len() > have {
+ return Err(EngineError::Other(format!("{} profiles are listed, but {} has {have}", indices.len(), ss.name)));
+ }
+ if let Some(i) = indices.iter().find(|i| **i >= have) {
+ return Err(EngineError::Other(format!("there is no profile {i} in {} (it has {have})", ss.name)));
+ }
+ solidcraft_doc::profile_selection_indices(ss, sel)?
+ }
ProfileSel::Points { points } => {
+ if points.len() > ss.profiles.len() {
+ return Err(EngineError::Other(format!(
+ "{} profile points are given, but {} has {} profiles",
+ points.len(),
+ ss.name,
+ ss.profiles.len()
+ )));
+ }
let Some(indices) = points.iter().map(|point| profile_at_point(&ss.profiles, *point)).collect::<Option<Vec<_>>>() else { continue };
+ let mut seen = std::collections::BTreeSet::new();
+ if let Some(i) = indices.iter().find(|i| !seen.insert(**i)) {
+ return Err(EngineError::Other(format!("two profile points pick the same profile ({i}) in {}", ss.name)));
+ }
indices
}
ProfileSel::Curves { loops } => {
@@ -410,7 +431,17 @@ pub(crate) fn profiles(p: &Value, cmd: &str) -> Result<ProfileSel> {
Value::Array(a) if a.len() > 10_000 => Err(bad(cmd, "too many profiles")),
Value::Array(a) => {
if a.iter().all(Value::is_u64) {
- return Ok(ProfileSel::Indices { indices: a.iter().filter_map(Value::as_u64).map(|x| x as usize).collect() });
+ // Each profile once: a repeat would build the same body again.
+ let mut seen = std::collections::BTreeSet::new();
+ let mut indices = Vec::with_capacity(a.len());
+ for i in a.iter().filter_map(Value::as_u64) {
+ let i = usize::try_from(i).map_err(|_| bad(cmd, format!("there is no profile {i}")))?;
+ if !seen.insert(i) {
+ return Err(bad(cmd, format!("profile {i} is listed twice")));
+ }
+ indices.push(i);
+ }
+ return Ok(ProfileSel::Indices { indices });
}
if a.iter().all(|x| x.get("point").is_some()) {
let pts =
diff --git a/crates/engine/src/cmd/features_more_tests.rs b/crates/engine/src/cmd/features_more_tests.rs
index 0c13423082..56245159d3 100644
--- a/crates/engine/src/cmd/features_more_tests.rs
+++ b/crates/engine/src/cmd/features_more_tests.rs
@@ -1719,3 +1719,33 @@ fn loft_through_coplanar_sections_is_refused() {
assert!(e.contains("same plane"), "{e}");
assert!(s.model.state().bodies.is_empty());
}
+
+#[test]
+fn duplicate_and_excess_profile_picks_are_refused_quickly() {
+ let mut s = Session::default();
+ run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Bottom"}));
+ run(&mut s, "sketch.circle.center", json!({"center": [2.5, 2.5], "radius": 2.5}));
+ run(&mut s, "sketch.finish", json!({}));
+ run(&mut s, "sketch.create", json!({"plane": "XY", "offset": 30, "name": "Top"}));
+ run(&mut s, "sketch.circle.center", json!({"center": [2.5, 2.5], "radius": 2}));
+ run(&mut s, "sketch.finish", json!({}));
+ let started = std::time::Instant::now();
+ let many = vec![0; 10_000];
+ let e = s.execute("solid.extrude", &json!({"sketch": "Bottom", "distance": 5, "profiles": many})).unwrap_err().to_string();
+ assert!(e.contains("profile 0 is listed twice"), "{e}");
+ let loft = json!({"sections": [{"sketch": "Bottom", "profiles": many}, {"sketch": "Top"}]});
+ let e = s.execute("solid.loft", &loft).unwrap_err().to_string();
+ assert!(e.contains("profile 0 is listed twice"), "{e}");
+ // More profiles than the sketch has, or one it doesn't have.
+ let e = s.execute("solid.extrude", &json!({"sketch": "Bottom", "distance": 5, "profiles": [0, 1]})).unwrap_err().to_string();
+ assert!(e.contains("2 profiles are listed, but Bottom has 1"), "{e}");
+ let e = s.execute("solid.extrude", &json!({"sketch": "Bottom", "distance": 5, "profiles": [3]})).unwrap_err().to_string();
+ assert!(e.contains("there is no profile 3 in Bottom"), "{e}");
+ let points = json!([{"point": [1, 1]}, {"point": [2, 2]}]);
+ let e = s.execute("solid.extrude", &json!({"sketch": "Bottom", "distance": 5, "profiles": points})).unwrap_err().to_string();
+ assert!(e.contains("2 profile points are given, but Bottom has 1"), "{e}");
+ assert!(started.elapsed().as_secs_f64() < 5.0, "refusing took {:?}", started.elapsed());
+ assert!(s.world_state().bodies.is_empty(), "nothing was built");
+ // One pick of the profile still builds.
+ run(&mut s, "solid.loft", json!({"sections": [{"sketch": "Bottom", "profiles": [0]}, {"sketch": "Top"}]}));
+}