Allow passing a vec of sketches or extrudes thru a user value (#2743)

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

---------

Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2024-06-21 16:44:31 -07:00
committed by GitHub
parent 4a275c2ff7
commit bf8fb0d127
6 changed files with 91 additions and 50 deletions

View File

@ -1375,7 +1375,7 @@ dependencies = [
[[package]] [[package]]
name = "kcl-lib" name = "kcl-lib"
version = "0.1.60" version = "0.1.61"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"approx", "approx",

View File

@ -1,7 +1,7 @@
[package] [package]
name = "kcl-lib" name = "kcl-lib"
description = "KittyCAD Language implementation and tools" description = "KittyCAD Language implementation and tools"
version = "0.1.60" version = "0.1.61"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
repository = "https://github.com/KittyCAD/modeling-app" repository = "https://github.com/KittyCAD/modeling-app"

View File

@ -153,6 +153,34 @@ pub enum MemoryItem {
}, },
} }
impl MemoryItem {
pub fn get_sketch_group_set(&self) -> Result<SketchGroupSet> {
match self {
MemoryItem::SketchGroup(s) => Ok(SketchGroupSet::SketchGroup(s.clone())),
MemoryItem::SketchGroups { value } => Ok(SketchGroupSet::SketchGroups(value.clone())),
MemoryItem::UserVal(value) => {
let sg: Vec<Box<SketchGroup>> = 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<ExtrudeGroupSet> {
match self {
MemoryItem::ExtrudeGroup(e) => Ok(ExtrudeGroupSet::ExtrudeGroup(e.clone())),
MemoryItem::ExtrudeGroups { value } => Ok(ExtrudeGroupSet::ExtrudeGroups(value.clone())),
MemoryItem::UserVal(value) => {
let eg: Vec<Box<ExtrudeGroup>> = 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. /// A geometry.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)] #[ts(export)]

View File

@ -437,18 +437,14 @@ impl Args {
}) })
})?; })?;
let sketch_set = if let MemoryItem::SketchGroup(sg) = first_value { let sketch_set = match first_value.get_sketch_group_set() {
SketchGroupSet::SketchGroup(sg.clone()) Ok(set) => set,
} else if let MemoryItem::SketchGroups { value } = first_value { Err(err) => {
SketchGroupSet::SketchGroups(value.clone()) return Err(KclError::Type(KclErrorDetails {
} else { message: format!("Expected an SketchGroupSet as the first argument: {}", err),
return Err(KclError::Type(KclErrorDetails { source_ranges: vec![self.source_range],
message: format!( }))
"Expected a SketchGroup or Vector of SketchGroups as the first argument, found `{:?}`", }
self.args
),
source_ranges: vec![self.source_range],
}));
}; };
let second_value = self.args.get(1).ok_or_else(|| { 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 { let sketch_set = match second_value.get_sketch_group_set() {
SketchGroupSet::SketchGroup(sg.clone()) Ok(set) => set,
} else if let MemoryItem::SketchGroups { value } = second_value { Err(err) => {
SketchGroupSet::SketchGroups(value.clone()) return Err(KclError::Type(KclErrorDetails {
} else { message: format!("Expected an SketchGroupSet as the second argument: {}", err),
return Err(KclError::Type(KclErrorDetails { source_ranges: vec![self.source_range],
message: format!( }))
"Expected a SketchGroup or Vector of SketchGroups as the second argument, found `{:?}`", }
self.args
),
source_ranges: vec![self.source_range],
}));
}; };
Ok((data, sketch_set)) Ok((data, sketch_set))
@ -813,18 +805,14 @@ impl Args {
}) })
})?; })?;
let extrude_set = if let MemoryItem::ExtrudeGroup(eg) = second_value { let extrude_set = match second_value.get_extrude_group_set() {
ExtrudeGroupSet::ExtrudeGroup(eg.clone()) Ok(set) => set,
} else if let MemoryItem::ExtrudeGroups { value } = second_value { Err(err) => {
ExtrudeGroupSet::ExtrudeGroups(value.clone()) return Err(KclError::Type(KclErrorDetails {
} else { message: format!("Expected an ExtrudeGroupSet as the second argument: {}", err),
return Err(KclError::Type(KclErrorDetails { source_ranges: vec![self.source_range],
message: format!( }))
"Expected a ExtrudeGroup or Vector of ExtrudeGroups as the second argument, found `{:?}`", }
self.args
),
source_ranges: vec![self.source_range],
}));
}; };
Ok((data, extrude_set)) Ok((data, extrude_set))
@ -953,18 +941,14 @@ impl Args {
}) })
})?; })?;
let sketch_set = if let MemoryItem::SketchGroup(sg) = second_value { let sketch_set = match second_value.get_sketch_group_set() {
SketchGroupSet::SketchGroup(sg.clone()) Ok(set) => set,
} else if let MemoryItem::SketchGroups { value } = second_value { Err(err) => {
SketchGroupSet::SketchGroups(value.clone()) return Err(KclError::Type(KclErrorDetails {
} else { message: format!("Expected an SketchGroupSet as the second argument: {}", err),
return Err(KclError::Type(KclErrorDetails { source_ranges: vec![self.source_range],
message: format!( }))
"Expected a SketchGroup or Vector of SketchGroups as the second argument, found `{:?}`", }
self.args
),
source_ranges: vec![self.source_range],
}));
}; };
Ok((number, sketch_set)) Ok((number, sketch_set))

View File

@ -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(); 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); 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);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 KiB