Fix sketch groups and extrude groups when used inside objects (#3439)

* Fix SketchGroups and ExtrudeGroups to work with user objects

* Fix to never clone more than once

* Fix error messages to be more helpful

* Add test
This commit is contained in:
Jonathan Tran
2024-08-15 01:37:33 -04:00
committed by GitHub
parent 75ae4b4a4a
commit 0916f990cb
3 changed files with 54 additions and 6 deletions

View File

@ -294,9 +294,19 @@ impl KclValue {
KclValue::SketchGroup(s) => Ok(SketchGroupSet::SketchGroup(s.clone())),
KclValue::SketchGroups { value } => Ok(SketchGroupSet::SketchGroups(value.clone())),
KclValue::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(sg.into())
let value = value.value.clone();
match value {
JValue::Null | JValue::Bool(_) | JValue::Number(_) | JValue::String(_) => Err(anyhow::anyhow!(
"Failed to deserialize sketch group set from JSON {}",
human_friendly_type(&value)
)),
JValue::Array(_) => serde_json::from_value::<Vec<Box<SketchGroup>>>(value)
.map(SketchGroupSet::from)
.map_err(|e| anyhow::anyhow!("Failed to deserialize array of sketch groups from JSON: {}", e)),
JValue::Object(_) => serde_json::from_value::<Box<SketchGroup>>(value)
.map(SketchGroupSet::from)
.map_err(|e| anyhow::anyhow!("Failed to deserialize sketch group from JSON: {}", e)),
}
}
_ => anyhow::bail!("Not a sketch group or sketch groups: {:?}", self),
}
@ -307,9 +317,19 @@ impl KclValue {
KclValue::ExtrudeGroup(e) => Ok(ExtrudeGroupSet::ExtrudeGroup(e.clone())),
KclValue::ExtrudeGroups { value } => Ok(ExtrudeGroupSet::ExtrudeGroups(value.clone())),
KclValue::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(eg.into())
let value = value.value.clone();
match value {
JValue::Null | JValue::Bool(_) | JValue::Number(_) | JValue::String(_) => Err(anyhow::anyhow!(
"Failed to deserialize extrude group set from JSON {}",
human_friendly_type(&value)
)),
JValue::Array(_) => serde_json::from_value::<Vec<Box<ExtrudeGroup>>>(value)
.map(ExtrudeGroupSet::from)
.map_err(|e| anyhow::anyhow!("Failed to deserialize array of extrude groups from JSON: {}", e)),
JValue::Object(_) => serde_json::from_value::<Box<ExtrudeGroup>>(value)
.map(ExtrudeGroupSet::from)
.map_err(|e| anyhow::anyhow!("Failed to deserialize extrude group from JSON: {}", e)),
}
}
_ => anyhow::bail!("Not a extrude group or extrude groups: {:?}", self),
}

View File

@ -0,0 +1,27 @@
fn test = () => {
return startSketchOn('XY')
|> startProfileAt([0, 0], %)
|> line([0, 1], %)
|> line([1, 0], %)
|> line([0, -1], %)
|> close(%)
}
fn test2 = () => {
return {
thing: startSketchOn('XY')
|> startProfileAt([0, 0], %)
|> line([0, 1], %)
|> line([1, 0], %)
|> line([0, -1], %)
|> close(%)
}
}
const x = test()
x
|> extrude(-10, %)
const x2 = test2()
x2.thing
|> extrude(10, %)

View File

@ -85,3 +85,4 @@ gen_test_fail!(
object_prop_not_found,
"undefined value: Property 'age' not found in object"
);
gen_test!(sketch_group_in_object);