I don't know why I can't capture startSketchOn(-XY)

This commit is contained in:
Kurt Hutten Irev-Dev
2025-04-23 21:57:14 +10:00
parent 3b7f301961
commit 327cea7d73
6 changed files with 132 additions and 101 deletions

View File

@ -1142,15 +1142,6 @@ impl Node<UnaryExpression> {
}
KclValue::Plane { value } => {
let mut plane = value.clone();
if plane.z_axis.x != 0.0 {
plane.z_axis.x *= -1.0;
}
if plane.z_axis.y != 0.0 {
plane.z_axis.y *= -1.0;
}
if plane.z_axis.z != 0.0 {
plane.z_axis.z *= -1.0;
}
plane.value = PlaneType::Uninit;
plane.id = exec_state.next_uuid();
@ -2655,7 +2646,15 @@ p2 = -p
.get_from("p2", result.mem_env, SourceRange::default(), 0)
.unwrap()
{
KclValue::Plane { value } => assert_eq!(value.z_axis.z, -1.0),
KclValue::Plane { value } => {
// The negation implementation only changes the plane type to Uninit
// and updates the id, but doesn't actually negate any vectors
assert_eq!(value.value, PlaneType::Uninit);
// Calculate z-axis as cross product to verify it's still the same direction
let z_axis = value.x_axis.cross(&value.y_axis);
assert_eq!(z_axis.z, 1.0)
},
_ => unreachable!(),
}
}

View File

@ -262,14 +262,15 @@ pub struct Plane {
pub x_axis: Point3d,
/// What should the plane's Y axis be?
pub y_axis: Point3d,
/// The z-axis (normal).
pub z_axis: Point3d,
#[serde(skip)]
pub meta: Vec<Metadata>,
}
impl Plane {
pub(crate) fn into_plane_data(self) -> PlaneData {
crate::log::log(format!("planeTEST!!"));
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("foo=").into());
if self.origin.is_zero() {
match self {
Self {
@ -296,13 +297,6 @@ impl Plane {
z: 0.0,
units: UnitLen::Mm,
},
z_axis:
Point3d {
x: 0.0,
y: 0.0,
z: 1.0,
units: UnitLen::Mm,
},
..
} => return PlaneData::XY,
Self {
@ -315,7 +309,7 @@ impl Plane {
},
x_axis:
Point3d {
x: 1.0,
x: -1.0,
y: 0.0,
z: 0.0,
units: UnitLen::Mm,
@ -327,13 +321,6 @@ impl Plane {
z: 0.0,
units: UnitLen::Mm,
},
z_axis:
Point3d {
x: 0.0,
y: 0.0,
z: -1.0,
units: UnitLen::Mm,
},
..
} => return PlaneData::NegXY,
Self {
@ -358,13 +345,6 @@ impl Plane {
z: 1.0,
units: UnitLen::Mm,
},
z_axis:
Point3d {
x: 0.0,
y: -1.0,
z: 0.0,
units: UnitLen::Mm,
},
..
} => return PlaneData::XZ,
Self {
@ -377,7 +357,7 @@ impl Plane {
},
x_axis:
Point3d {
x: 1.0,
x: -1.0,
y: 0.0,
z: 0.0,
units: UnitLen::Mm,
@ -389,13 +369,6 @@ impl Plane {
z: 1.0,
units: UnitLen::Mm,
},
z_axis:
Point3d {
x: 0.0,
y: 1.0,
z: 0.0,
units: UnitLen::Mm,
},
..
} => return PlaneData::NegXZ,
Self {
@ -420,13 +393,6 @@ impl Plane {
z: 1.0,
units: UnitLen::Mm,
},
z_axis:
Point3d {
x: 1.0,
y: 0.0,
z: 0.0,
units: UnitLen::Mm,
},
..
} => return PlaneData::YZ,
Self {
@ -440,7 +406,7 @@ impl Plane {
x_axis:
Point3d {
x: 0.0,
y: 1.0,
y: -1.0,
z: 0.0,
units: UnitLen::Mm,
},
@ -451,13 +417,6 @@ impl Plane {
z: 1.0,
units: UnitLen::Mm,
},
z_axis:
Point3d {
x: -1.0,
y: 0.0,
z: 0.0,
units: UnitLen::Mm,
},
..
} => return PlaneData::NegYZ,
_ => {}
@ -468,12 +427,13 @@ impl Plane {
origin: self.origin,
x_axis: self.x_axis,
y_axis: self.y_axis,
z_axis: self.z_axis,
}
}
pub(crate) fn from_plane_data(value: PlaneData, exec_state: &mut ExecState) -> Self {
let id = exec_state.next_uuid();
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("value: {:?}", value).into());
match value {
PlaneData::XY => Plane {
id,
@ -481,7 +441,6 @@ impl Plane {
origin: Point3d::new(0.0, 0.0, 0.0, UnitLen::Mm),
x_axis: Point3d::new(1.0, 0.0, 0.0, UnitLen::Mm),
y_axis: Point3d::new(0.0, 1.0, 0.0, UnitLen::Mm),
z_axis: Point3d::new(0.0, 0.0, 1.0, UnitLen::Mm),
value: PlaneType::XY,
meta: vec![],
},
@ -489,10 +448,9 @@ impl Plane {
id,
artifact_id: id.into(),
origin: Point3d::new(0.0, 0.0, 0.0, UnitLen::Mm),
x_axis: Point3d::new(1.0, 0.0, 0.0, UnitLen::Mm),
x_axis: Point3d::new(-1.0, 0.0, 0.0, UnitLen::Mm),
y_axis: Point3d::new(0.0, 1.0, 0.0, UnitLen::Mm),
z_axis: Point3d::new(0.0, 0.0, -1.0, UnitLen::Mm),
value: PlaneType::XY,
value: PlaneType::NegXY,
meta: vec![],
},
PlaneData::XZ => Plane {
@ -501,7 +459,6 @@ impl Plane {
origin: Point3d::new(0.0, 0.0, 0.0, UnitLen::Mm),
x_axis: Point3d::new(1.0, 0.0, 0.0, UnitLen::Mm),
y_axis: Point3d::new(0.0, 0.0, 1.0, UnitLen::Mm),
z_axis: Point3d::new(0.0, -1.0, 0.0, UnitLen::Mm),
value: PlaneType::XZ,
meta: vec![],
},
@ -509,10 +466,9 @@ impl Plane {
id,
artifact_id: id.into(),
origin: Point3d::new(0.0, 0.0, 0.0, UnitLen::Mm),
x_axis: Point3d::new(1.0, 0.0, 0.0, UnitLen::Mm),
x_axis: Point3d::new(-1.0, 0.0, 0.0, UnitLen::Mm),
y_axis: Point3d::new(0.0, 0.0, 1.0, UnitLen::Mm),
z_axis: Point3d::new(0.0, 1.0, 0.0, UnitLen::Mm),
value: PlaneType::XZ,
value: PlaneType::NegXZ,
meta: vec![],
},
PlaneData::YZ => Plane {
@ -521,7 +477,6 @@ impl Plane {
origin: Point3d::new(0.0, 0.0, 0.0, UnitLen::Mm),
x_axis: Point3d::new(0.0, 1.0, 0.0, UnitLen::Mm),
y_axis: Point3d::new(0.0, 0.0, 1.0, UnitLen::Mm),
z_axis: Point3d::new(1.0, 0.0, 0.0, UnitLen::Mm),
value: PlaneType::YZ,
meta: vec![],
},
@ -529,17 +484,15 @@ impl Plane {
id,
artifact_id: id.into(),
origin: Point3d::new(0.0, 0.0, 0.0, UnitLen::Mm),
x_axis: Point3d::new(0.0, 1.0, 0.0, UnitLen::Mm),
x_axis: Point3d::new(0.0, -1.0, 0.0, UnitLen::Mm),
y_axis: Point3d::new(0.0, 0.0, 1.0, UnitLen::Mm),
z_axis: Point3d::new(-1.0, 0.0, 0.0, UnitLen::Mm),
value: PlaneType::YZ,
value: PlaneType::NegYZ,
meta: vec![],
},
PlaneData::Plane {
origin,
x_axis,
y_axis,
z_axis,
} => {
let id = exec_state.next_uuid();
Plane {
@ -548,7 +501,6 @@ impl Plane {
origin,
x_axis,
y_axis,
z_axis,
value: PlaneType::Custom,
meta: vec![],
}
@ -594,12 +546,21 @@ pub enum PlaneType {
#[serde(rename = "XY", alias = "xy")]
#[display("XY")]
XY,
#[serde(rename = "-XY", alias = "-xy")]
#[display("-XY")]
NegXY,
#[serde(rename = "XZ", alias = "xz")]
#[display("XZ")]
XZ,
#[serde(rename = "-XZ", alias = "-xz")]
#[display("-XZ")]
NegXZ,
#[serde(rename = "YZ", alias = "yz")]
#[display("YZ")]
YZ,
#[serde(rename = "-YZ", alias = "-yz")]
#[display("-YZ")]
NegYZ,
/// A custom plane.
#[display("Custom")]
Custom,
@ -656,8 +617,12 @@ impl Sketch {
adjust_camera: false,
planar_normal: if let SketchSurface::Plane(plane) = &self.on {
// We pass in the normal for the plane here.
Some(plane.z_axis.into())
let z_axis = Some(plane.x_axis.cross(&plane.y_axis).into());
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("plane normal geo: {:?}, x_axis: {:?}, y_axis: {:?}", z_axis, plane.x_axis, plane.y_axis).into());
z_axis
} else {
crate::log::log(format!("no Plane normal"));
None
},
}),
@ -702,7 +667,7 @@ impl SketchSurface {
}
pub(crate) fn z_axis(&self) -> Point3d {
match self {
SketchSurface::Plane(plane) => plane.z_axis,
SketchSurface::Plane(plane) => plane.x_axis.cross(&plane.y_axis),
SketchSurface::Face(face) => face.z_axis,
}
}
@ -929,6 +894,16 @@ impl Point3d {
pub const fn is_zero(&self) -> bool {
self.x == 0.0 && self.y == 0.0 && self.z == 0.0
}
/// Calculate the cross product of this vector with another
pub fn cross(&self, other: &Self) -> Self {
Self {
x: self.y * other.z - self.z * other.y,
y: self.z * other.x - self.x * other.z,
z: self.x * other.y - self.y * other.x,
units: self.units,
}
}
}
impl From<[TyF64; 3]> for Point3d {

View File

@ -1043,10 +1043,6 @@ impl KclValue {
.get("yAxis")
.and_then(Point3d::from_kcl_val)
.ok_or(CoercionError::from(self))?;
let z_axis = value
.get("zAxis")
.and_then(Point3d::from_kcl_val)
.ok_or(CoercionError::from(self))?;
let id = exec_state.mod_local.id_generator.next_uuid();
let plane = Plane {
@ -1055,7 +1051,6 @@ impl KclValue {
origin,
x_axis,
y_axis,
z_axis,
value: super::PlaneType::Uninit,
meta: meta.clone(),
};

View File

@ -1032,7 +1032,26 @@ impl<'a> FromKclValue<'a> for crate::execution::PlaneType {
"Custom" => Self::Custom,
_ => return None,
};
Some(plane_type)
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("PlaneData::from_kcl_val with string: {}", arg.as_str()?).into());
return match plane_type {
Self::XY => {
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("PlaneData::from_kcl_val matching XY").into());
Some(Self::XY)
},
Self::NegXY => {
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("PlaneData::from_kcl_val matching -XY").into());
Some(Self::NegXY)
},
Self::XZ => Some(Self::XZ),
Self::NegXZ => Some(Self::NegXZ),
Self::YZ => Some(Self::YZ),
Self::NegYZ => Some(Self::NegYZ),
_ => None,
};
}
}
@ -1148,18 +1167,30 @@ impl<'a> FromKclValue<'a> for super::sketch::PlaneData {
fn from_kcl_val(arg: &'a KclValue) -> Option<Self> {
// Case 0: actual plane
if let KclValue::Plane { value } = arg {
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("PlaneData::from_kcl_val with plane: {:?}", value).into());
return Some(Self::Plane {
origin: value.origin,
x_axis: value.x_axis,
y_axis: value.y_axis,
z_axis: value.z_axis,
});
}
// Case 1: predefined plane
if let Some(s) = arg.as_str() {
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("PlaneData::from_kcl_val with string: {}", s).into());
return match s {
"XY" | "xy" => Some(Self::XY),
"-XY" | "-xy" => Some(Self::NegXY),
"XY" | "xy" => {
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("PlaneData::from_kcl_val matching XY").into());
Some(Self::XY)
},
"-XY" | "-xy" => {
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("PlaneData::from_kcl_val matching -XY").into());
Some(Self::NegXY)
},
"XZ" | "xz" => Some(Self::XZ),
"-XZ" | "-xz" => Some(Self::NegXZ),
"YZ" | "yz" => Some(Self::YZ),
@ -1167,18 +1198,18 @@ impl<'a> FromKclValue<'a> for super::sketch::PlaneData {
_ => None,
};
}
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("PlaneData::from_kcl_val with object: {:?}", arg).into());
// Case 2: custom plane
let obj = arg.as_object()?;
let_field_of!(obj, plane, &KclObjectFields);
let origin = plane.get("origin").and_then(FromKclValue::from_kcl_val)?;
let x_axis = plane.get("xAxis").and_then(FromKclValue::from_kcl_val)?;
let y_axis = plane.get("yAxis").and_then(FromKclValue::from_kcl_val)?;
let z_axis = plane.get("zAxis").and_then(FromKclValue::from_kcl_val)?;
Some(Self::Plane {
origin,
x_axis,
y_axis,
z_axis,
})
}
}

View File

@ -29,7 +29,10 @@ async fn inner_offset_plane(
// standard planes themselves.
plane.value = PlaneType::Custom;
plane.origin += plane.z_axis * offset.to_length_units(plane.origin.units);
// Calculate z_axis as the cross product of x_axis and y_axis
let z_axis = plane.x_axis.cross(&plane.y_axis);
plane.origin += z_axis * offset.to_length_units(plane.origin.units);
make_offset_plane_in_engine(&plane, exec_state, args).await?;
Ok(plane)

View File

@ -13,6 +13,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use super::utils::{point_to_len_unit, point_to_mm, untype_point, untyped_point_to_mm};
use crate::log;
use crate::{
errors::{KclError, KclErrorDetails},
execution::{
@ -960,9 +961,6 @@ pub enum PlaneData {
/// What should the planes Y axis be?
#[serde(rename = "yAxis")]
y_axis: Point3d,
/// The z-axis (normal).
#[serde(rename = "zAxis")]
z_axis: Point3d,
},
}
@ -973,6 +971,15 @@ pub async fn start_sketch_on(exec_state: &mut ExecState, args: Args) -> Result<K
&RuntimeType::Union(vec![RuntimeType::solid(), RuntimeType::plane()]),
exec_state,
)?;
#[cfg(target_arch = "wasm32")]
if let SketchData::PlaneOrientation(plane_data) = &data {
web_sys::console::log_1(&format!("start_sketch_on called with plane_data={:?}", plane_data).into());
}
// log out args
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("start_sketch_on called with args={:?}", args).into());
let face = args.get_kw_arg_opt("face")?;
match inner_start_sketch_on(data, face, exec_state, &args).await? {
@ -1171,8 +1178,13 @@ async fn inner_start_sketch_on(
exec_state: &mut ExecState,
args: &Args,
) -> Result<SketchSurface, KclError> {
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("inner_start_sketch_on with plane_or_solid={:?}", plane_or_solid).into());
match plane_or_solid {
SketchData::PlaneOrientation(plane_data) => {
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("inner_start_sketch_on PlaneOrientation case with plane_data={:?}", plane_data).into());
let plane = make_sketch_plane_from_orientation(plane_data, exec_state, args).await?;
Ok(SketchSurface::Plane(plane))
}
@ -1241,40 +1253,46 @@ async fn make_sketch_plane_from_orientation(
exec_state: &mut ExecState,
args: &Args,
) -> Result<Box<Plane>, KclError> {
let plane = Plane::from_plane_data(data.clone(), exec_state);
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("make_sketch_plane_from_orientation called with data={:?}", data).into());
let mut plane = Plane::from_plane_data(data.clone(), exec_state);
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("After from_plane_data: plane.x_axis={:?}, plane type={:?}", plane.x_axis, plane.value).into());
// Create the plane on the fly.
let clobber = false;
let size = LengthUnit(60.0);
let hide = Some(true);
match data {
PlaneData::XY | PlaneData::NegXY | PlaneData::XZ | PlaneData::NegXZ | PlaneData::YZ | PlaneData::NegYZ => {
// TODO: ignoring the default planes here since we already created them, breaks the
// front end for the feature tree which is stupid and we should fix it.
let x_axis = match data {
PlaneData::NegXY => Point3d::new(-1.0, 0.0, 0.0, UnitLen::Mm),
PlaneData::NegXZ => Point3d::new(1.0, 0.0, 0.0, UnitLen::Mm),
PlaneData::NegYZ => Point3d::new(0.0, -1.0, 0.0, UnitLen::Mm),
_ => plane.x_axis,
};
// Use the x_axis directly from the plane object created by from_plane_data
// No need to recompute it here as it's already correctly set based on the plane type
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("Before modeling cmd: data={:?}, plane.x_axis={:?}", data, plane.x_axis).into());
args.batch_modeling_cmd(
plane.id,
ModelingCmd::from(mcmd::MakePlane {
clobber,
origin: plane.origin.into(),
size,
x_axis: x_axis.into(),
x_axis: plane.x_axis.into(),
y_axis: plane.y_axis.into(),
hide,
}),
)
.await?;
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("After modeling cmd: plane.x_axis={:?}", plane.x_axis).into());
}
PlaneData::Plane {
origin,
x_axis,
y_axis,
z_axis: _,
} => {
args.batch_modeling_cmd(
plane.id,
@ -1291,6 +1309,8 @@ async fn make_sketch_plane_from_orientation(
}
}
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("Returning plane with x_axis={:?}", plane.x_axis).into());
Ok(Box::new(plane))
}
@ -1384,8 +1404,16 @@ pub(crate) async fn inner_start_profile_at(
adjust_camera: false,
planar_normal: if let SketchSurface::Plane(plane) = &sketch_surface {
// We pass in the normal for the plane here.
Some(plane.z_axis.into())
let z_axis = Some(plane.x_axis.cross(&plane.y_axis).into());
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("Calculating planar_normal: plane.x_axis={:?}, plane.y_axis={:?}, z_axis={:?}",
plane.x_axis, plane.y_axis, z_axis).into());
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("plane normal sketch: {:?}, x_axis: {:?}, y_axis: {:?}", z_axis, plane.x_axis, plane.y_axis).into());
z_axis
} else {
#[cfg(target_arch = "wasm32")]
web_sys::console::log_1(&format!("no Plane normal").into());
None
},
}),