diff --git a/src/wasm-lib/kcl/src/executor.rs b/src/wasm-lib/kcl/src/executor.rs index 2c855417a0..208f7a637c 100644 --- a/src/wasm-lib/kcl/src/executor.rs +++ b/src/wasm-lib/kcl/src/executor.rs @@ -1127,7 +1127,7 @@ pub struct TagEngineInfo { /// The sketch the tag is on. pub sketch: uuid::Uuid, /// The path the tag is on. - pub path: Option, + pub path: Option, /// The surface information for the tag. pub surface: Option, } @@ -1206,7 +1206,7 @@ impl Sketch { tag_identifier.info = Some(TagEngineInfo { id: base.geo_meta.id, sketch: self.id, - path: Some(base.clone()), + path: Some(current_path.clone()), surface: None, }); @@ -1658,6 +1658,32 @@ pub enum Path { }, } +/// What kind of path is this? +#[derive(Display)] +enum PathType { + ToPoint, + Base, + TangentialArc, + TangentialArcTo, + Circle, + Horizontal, + AngledLineTo, +} + +impl From for PathType { + fn from(value: Path) -> Self { + match value { + Path::ToPoint { .. } => Self::ToPoint, + Path::TangentialArcTo { .. } => Self::TangentialArcTo, + Path::TangentialArc { .. } => Self::TangentialArc, + Path::Circle { .. } => Self::Circle, + Path::Horizontal { .. } => Self::Horizontal, + Path::AngledLineTo { .. } => Self::AngledLineTo, + Path::Base { .. } => Self::Base, + } + } +} + impl Path { pub fn get_id(&self) -> uuid::Uuid { match self { @@ -1695,6 +1721,21 @@ impl Path { } } + /// Where does this path segment start? + pub fn get_from(&self) -> &[f64; 2] { + &self.get_base().from + } + /// Where does this path segment end? + pub fn get_to(&self) -> &[f64; 2] { + &self.get_base().to + } + + /// Length of this path segment, in cartesian plane. + pub fn length(&self) -> f64 { + // TODO 4297: check what type of line this path is, don't assume linear. + ((self.get_from()[1] - self.get_to()[1]).powi(2) + (self.get_from()[0] - self.get_to()[0]).powi(2)).sqrt() + } + pub fn get_base_mut(&mut self) -> Option<&mut BasePath> { match self { Path::ToPoint { base } => Some(base), diff --git a/src/wasm-lib/kcl/src/std/segment.rs b/src/wasm-lib/kcl/src/std/segment.rs index 9dfa429eb1..2b5413ba9e 100644 --- a/src/wasm-lib/kcl/src/std/segment.rs +++ b/src/wasm-lib/kcl/src/std/segment.rs @@ -42,7 +42,7 @@ fn inner_segment_end_x(tag: &TagIdentifier, exec_state: &mut ExecState, args: Ar }) })?; - Ok(path.to[0]) + Ok(path.get_base().to[0]) } /// Returns the segment end of y. @@ -79,7 +79,7 @@ fn inner_segment_end_y(tag: &TagIdentifier, exec_state: &mut ExecState, args: Ar }) })?; - Ok(path.to[1]) + Ok(path.get_to()[1]) } /// Returns the last segment of x. @@ -202,7 +202,8 @@ fn inner_segment_length(tag: &TagIdentifier, exec_state: &mut ExecState, args: A }) })?; - let result = ((path.from[1] - path.to[1]).powi(2) + (path.from[0] - path.to[0]).powi(2)).sqrt(); + // TODO 4297: check what type of line this path is, don't assume linear. + let result = path.length(); Ok(result) } @@ -242,7 +243,7 @@ fn inner_segment_angle(tag: &TagIdentifier, exec_state: &mut ExecState, args: Ar }) })?; - let result = between(path.from.into(), path.to.into()); + let result = between(path.get_from().into(), path.get_to().into()); Ok(result.to_degrees()) } @@ -286,7 +287,8 @@ fn inner_angle_to_match_length_x( }) })?; - let length = ((path.from[1] - path.to[1]).powi(2) + (path.from[0] - path.to[0]).powi(2)).sqrt(); + let length = + ((path.get_from()[1] - path.get_to()[1]).powi(2) + (path.get_from()[0] - path.get_to()[0]).powi(2)).sqrt(); let last_line = sketch .paths @@ -350,7 +352,7 @@ fn inner_angle_to_match_length_y( }) })?; - let length = ((path.from[1] - path.to[1]).powi(2) + (path.from[0] - path.to[0]).powi(2)).sqrt(); + let length = path.length(); let last_line = sketch .paths diff --git a/src/wasm-lib/kcl/src/std/sketch.rs b/src/wasm-lib/kcl/src/std/sketch.rs index 91085fa315..07d24adeb5 100644 --- a/src/wasm-lib/kcl/src/std/sketch.rs +++ b/src/wasm-lib/kcl/src/std/sketch.rs @@ -813,7 +813,7 @@ async fn inner_angled_line_that_intersects( let from = sketch.current_pen_position()?; let to = intersection_with_parallel_line( - &[path.from.into(), path.to.into()], + &[path.get_from().into(), path.get_to().into()], data.offset.unwrap_or_default(), data.angle, from, @@ -1244,7 +1244,9 @@ pub(crate) async fn inner_start_profile_at( tag_identifier.info = Some(TagEngineInfo { id: current_path.geo_meta.id, sketch: path_id, - path: Some(current_path.clone()), + path: Some(Path::Base { + base: current_path.clone(), + }), surface: None, }); HashMap::from([(tag.name.to_string(), tag_identifier)])