* claim: #5023 * Extrude and loft: refuse repeated profile indexes (#5023) `profiles: [0, 0, ...]` with 10 000 copies built 10 000 identical bodies in about 35 s, and a loft section listing one profile 10 000 times took 25 s before it was refused. A list of profile indexes now refuses a repeated index while it is parsed ("profile 0 is listed twice"), as offset association ids already were. Before the profiles are resolved, a selection naming more profiles than its sketch has, an index the sketch doesn't have, or two points in the same profile is refused too. A design file that repeats an index still opens, using the profile once.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 =
|
||||
|
||||
@@ -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"}]}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user