diff --git a/src/wasm-lib/Cargo.lock b/src/wasm-lib/Cargo.lock index ea1960aed..3d05cd95b 100644 --- a/src/wasm-lib/Cargo.lock +++ b/src/wasm-lib/Cargo.lock @@ -1375,7 +1375,7 @@ dependencies = [ [[package]] name = "kcl-lib" -version = "0.1.60" +version = "0.1.61" dependencies = [ "anyhow", "approx", diff --git a/src/wasm-lib/kcl/Cargo.toml b/src/wasm-lib/kcl/Cargo.toml index a038b3432..8d34f3b51 100644 --- a/src/wasm-lib/kcl/Cargo.toml +++ b/src/wasm-lib/kcl/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "kcl-lib" description = "KittyCAD Language implementation and tools" -version = "0.1.60" +version = "0.1.61" edition = "2021" license = "MIT" repository = "https://github.com/KittyCAD/modeling-app" diff --git a/src/wasm-lib/kcl/src/executor.rs b/src/wasm-lib/kcl/src/executor.rs index de991fa1b..eff1d6538 100644 --- a/src/wasm-lib/kcl/src/executor.rs +++ b/src/wasm-lib/kcl/src/executor.rs @@ -153,6 +153,34 @@ pub enum MemoryItem { }, } +impl MemoryItem { + pub fn get_sketch_group_set(&self) -> Result { + match self { + MemoryItem::SketchGroup(s) => Ok(SketchGroupSet::SketchGroup(s.clone())), + MemoryItem::SketchGroups { value } => Ok(SketchGroupSet::SketchGroups(value.clone())), + MemoryItem::UserVal(value) => { + let sg: Vec> = serde_json::from_value(value.value.clone()) + .map_err(|e| anyhow::anyhow!("Failed to deserialize array of sketch groups from JSON: {}", e))?; + Ok(SketchGroupSet::SketchGroups(sg.clone())) + } + _ => anyhow::bail!("Not a sketch group or sketch groups: {:?}", self), + } + } + + pub fn get_extrude_group_set(&self) -> Result { + match self { + MemoryItem::ExtrudeGroup(e) => Ok(ExtrudeGroupSet::ExtrudeGroup(e.clone())), + MemoryItem::ExtrudeGroups { value } => Ok(ExtrudeGroupSet::ExtrudeGroups(value.clone())), + MemoryItem::UserVal(value) => { + let eg: Vec> = serde_json::from_value(value.value.clone()) + .map_err(|e| anyhow::anyhow!("Failed to deserialize array of extrude groups from JSON: {}", e))?; + Ok(ExtrudeGroupSet::ExtrudeGroups(eg.clone())) + } + _ => anyhow::bail!("Not a extrude group or extrude groups: {:?}", self), + } + } +} + /// A geometry. #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[ts(export)] diff --git a/src/wasm-lib/kcl/src/std/mod.rs b/src/wasm-lib/kcl/src/std/mod.rs index 04f0bc461..3b4c688a3 100644 --- a/src/wasm-lib/kcl/src/std/mod.rs +++ b/src/wasm-lib/kcl/src/std/mod.rs @@ -437,18 +437,14 @@ impl Args { }) })?; - let sketch_set = if let MemoryItem::SketchGroup(sg) = first_value { - SketchGroupSet::SketchGroup(sg.clone()) - } else if let MemoryItem::SketchGroups { value } = first_value { - SketchGroupSet::SketchGroups(value.clone()) - } else { - return Err(KclError::Type(KclErrorDetails { - message: format!( - "Expected a SketchGroup or Vector of SketchGroups as the first argument, found `{:?}`", - self.args - ), - source_ranges: vec![self.source_range], - })); + let sketch_set = match first_value.get_sketch_group_set() { + Ok(set) => set, + Err(err) => { + return Err(KclError::Type(KclErrorDetails { + message: format!("Expected an SketchGroupSet as the first argument: {}", err), + source_ranges: vec![self.source_range], + })) + } }; let second_value = self.args.get(1).ok_or_else(|| { @@ -672,18 +668,14 @@ impl Args { }) })?; - let sketch_set = if let MemoryItem::SketchGroup(sg) = second_value { - SketchGroupSet::SketchGroup(sg.clone()) - } else if let MemoryItem::SketchGroups { value } = second_value { - SketchGroupSet::SketchGroups(value.clone()) - } else { - return Err(KclError::Type(KclErrorDetails { - message: format!( - "Expected a SketchGroup or Vector of SketchGroups as the second argument, found `{:?}`", - self.args - ), - source_ranges: vec![self.source_range], - })); + let sketch_set = match second_value.get_sketch_group_set() { + Ok(set) => set, + Err(err) => { + return Err(KclError::Type(KclErrorDetails { + message: format!("Expected an SketchGroupSet as the second argument: {}", err), + source_ranges: vec![self.source_range], + })) + } }; Ok((data, sketch_set)) @@ -813,18 +805,14 @@ impl Args { }) })?; - let extrude_set = if let MemoryItem::ExtrudeGroup(eg) = second_value { - ExtrudeGroupSet::ExtrudeGroup(eg.clone()) - } else if let MemoryItem::ExtrudeGroups { value } = second_value { - ExtrudeGroupSet::ExtrudeGroups(value.clone()) - } else { - return Err(KclError::Type(KclErrorDetails { - message: format!( - "Expected a ExtrudeGroup or Vector of ExtrudeGroups as the second argument, found `{:?}`", - self.args - ), - source_ranges: vec![self.source_range], - })); + let extrude_set = match second_value.get_extrude_group_set() { + Ok(set) => set, + Err(err) => { + return Err(KclError::Type(KclErrorDetails { + message: format!("Expected an ExtrudeGroupSet as the second argument: {}", err), + source_ranges: vec![self.source_range], + })) + } }; Ok((data, extrude_set)) @@ -953,18 +941,14 @@ impl Args { }) })?; - let sketch_set = if let MemoryItem::SketchGroup(sg) = second_value { - SketchGroupSet::SketchGroup(sg.clone()) - } else if let MemoryItem::SketchGroups { value } = second_value { - SketchGroupSet::SketchGroups(value.clone()) - } else { - return Err(KclError::Type(KclErrorDetails { - message: format!( - "Expected a SketchGroup or Vector of SketchGroups as the second argument, found `{:?}`", - self.args - ), - source_ranges: vec![self.source_range], - })); + let sketch_set = match second_value.get_sketch_group_set() { + Ok(set) => set, + Err(err) => { + return Err(KclError::Type(KclErrorDetails { + message: format!("Expected an SketchGroupSet as the second argument: {}", err), + source_ranges: vec![self.source_range], + })) + } }; Ok((number, sketch_set)) diff --git a/src/wasm-lib/tests/executor/main.rs b/src/wasm-lib/tests/executor/main.rs index 96773cb98..8fe4c457f 100644 --- a/src/wasm-lib/tests/executor/main.rs +++ b/src/wasm-lib/tests/executor/main.rs @@ -2009,3 +2009,32 @@ const pattn2 = patternCircular3d({axis: [0,0, 1], center: [-20, -20, -20], repet let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap(); twenty_twenty::assert_image("tests/executor/outputs/circular_pattern3d_a_pattern.png", &result, 1.0); } + +#[tokio::test(flavor = "multi_thread")] +async fn serial_test_array_of_sketches() { + let code = r#"const plane001 = startSketchOn('XZ') + +const profile001 = plane001 + |> startProfileAt([40.82, 240.82], %) + |> line([235.72, -8.16], %) + |> line([13.27, -253.07], %) + |> line([-247.97, -19.39], %) + |> lineTo([profileStartX(%), profileStartY(%)], %) + |> close(%) + +const profile002 = plane001 + |> startProfileAt([47.17, -71.91], %) + |> line([247.96, -4.03], %) + |> line([-17.26, -116.79], %) + |> line([-235.87, 12.66], %) + |> lineTo([profileStartX(%), profileStartY(%)], %) + |> close(%) + +const sketch001 = [profile001, profile002] + +extrude(10, sketch001) +"#; + + let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap(); + twenty_twenty::assert_image("tests/executor/outputs/array_of_sketches.png", &result, 1.0); +} diff --git a/src/wasm-lib/tests/executor/outputs/array_of_sketches.png b/src/wasm-lib/tests/executor/outputs/array_of_sketches.png new file mode 100644 index 000000000..1e8257abf Binary files /dev/null and b/src/wasm-lib/tests/executor/outputs/array_of_sketches.png differ