use anyhow::Result;
use derive_docs::stdlib;
use kittycad::types::{Angle, ModelingCmd, Point3D};
use kittycad_execution_plan_macros::ExecutionPlanValue;
use parse_display::{Display, FromStr};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{
errors::{KclError, KclErrorDetails},
executor::{
BasePath, ExtrudeGroup, ExtrudeSurface, Face, GeoMeta, MemoryItem, Path, Plane, PlaneType, Point2d, Point3d,
Position, Rotation, SketchGroup, SketchGroupSet, SketchSurface, SourceRange,
},
std::{
utils::{
arc_angles, arc_center_and_end, get_tangent_point_from_previous_arc, get_tangential_arc_to_info,
get_x_component, get_y_component, intersection_with_parallel_line, TangentialArcInfoInput,
},
Args,
},
};
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase", untagged)]
pub enum LineToData {
PointWithTag {
to: [f64; 2],
tag: String,
},
Point([f64; 2]),
}
pub async fn line_to(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (LineToData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_line_to(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "lineTo",
}]
async fn inner_line_to(
data: LineToData,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?;
let to = match data {
LineToData::PointWithTag { to, .. } => to,
LineToData::Point(to) => to,
};
let id = uuid::Uuid::new_v4();
args.send_modeling_cmd(
id,
ModelingCmd::ExtendPath {
path: sketch_group.id,
segment: kittycad::types::PathSegment::Line {
end: Point3D {
x: to[0],
y: to[1],
z: 0.0,
},
relative: false,
},
},
)
.await?;
let current_path = Path::ToPoint {
base: BasePath {
from: from.into(),
to,
name: if let LineToData::PointWithTag { tag, .. } = data {
tag.to_string()
} else {
"".to_string()
},
geo_meta: GeoMeta {
id,
metadata: args.source_range.into(),
},
},
};
let mut new_sketch_group = sketch_group.clone();
new_sketch_group.value.push(current_path);
Ok(new_sketch_group)
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase", untagged)]
pub enum AxisLineToData {
PointWithTag {
to: f64,
tag: String,
},
Point(f64),
}
pub async fn x_line_to(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (AxisLineToData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_x_line_to(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "xLineTo",
}]
async fn inner_x_line_to(
data: AxisLineToData,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?;
let line_to_data = match data {
AxisLineToData::PointWithTag { to, tag } => LineToData::PointWithTag { to: [to, from.y], tag },
AxisLineToData::Point(data) => LineToData::Point([data, from.y]),
};
let new_sketch_group = inner_line_to(line_to_data, sketch_group, args).await?;
Ok(new_sketch_group)
}
pub async fn y_line_to(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (AxisLineToData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_y_line_to(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "yLineTo",
}]
async fn inner_y_line_to(
data: AxisLineToData,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?;
let line_to_data = match data {
AxisLineToData::PointWithTag { to, tag } => LineToData::PointWithTag { to: [from.x, to], tag },
AxisLineToData::Point(data) => LineToData::Point([from.x, data]),
};
let new_sketch_group = inner_line_to(line_to_data, sketch_group, args).await?;
Ok(new_sketch_group)
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase", untagged)]
pub enum LineData {
PointWithTag {
to: [f64; 2],
tag: String,
},
Point([f64; 2]),
}
pub async fn line(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (LineData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_line(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "line",
}]
async fn inner_line(data: LineData, sketch_group: Box<SketchGroup>, args: Args) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?;
let inner_args = match &data {
LineData::PointWithTag { to, .. } => *to,
LineData::Point(to) => *to,
};
let delta = inner_args;
let to = [from.x + inner_args[0], from.y + inner_args[1]];
let id = uuid::Uuid::new_v4();
args.send_modeling_cmd(
id,
ModelingCmd::ExtendPath {
path: sketch_group.id,
segment: kittycad::types::PathSegment::Line {
end: Point3D {
x: delta[0],
y: delta[1],
z: 0.0,
},
relative: true,
},
},
)
.await?;
let current_path = Path::ToPoint {
base: BasePath {
from: from.into(),
to,
name: if let LineData::PointWithTag { tag, .. } = data {
tag.to_string()
} else {
"".to_string()
},
geo_meta: GeoMeta {
id,
metadata: args.source_range.into(),
},
},
};
let mut new_sketch_group = sketch_group.clone();
new_sketch_group.value.push(current_path);
Ok(new_sketch_group)
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase", untagged)]
pub enum AxisLineData {
LengthWithTag {
length: f64,
tag: String,
},
Length(f64),
}
pub async fn x_line(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (AxisLineData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_x_line(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "xLine",
}]
async fn inner_x_line(
data: AxisLineData,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let line_data = match data {
AxisLineData::LengthWithTag { length, tag } => LineData::PointWithTag { to: [length, 0.0], tag },
AxisLineData::Length(length) => LineData::Point([length, 0.0]),
};
let new_sketch_group = inner_line(line_data, sketch_group, args).await?;
Ok(new_sketch_group)
}
pub async fn y_line(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (AxisLineData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_y_line(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "yLine",
}]
async fn inner_y_line(
data: AxisLineData,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let line_data = match data {
AxisLineData::LengthWithTag { length, tag } => LineData::PointWithTag { to: [0.0, length], tag },
AxisLineData::Length(length) => LineData::Point([0.0, length]),
};
let new_sketch_group = inner_line(line_data, sketch_group, args).await?;
Ok(new_sketch_group)
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase", untagged)]
pub enum AngledLineData {
AngleWithTag {
angle: f64,
length: f64,
tag: String,
},
AngleAndLength([f64; 2]),
}
impl AngledLineData {
pub fn into_inner_line(self, to: [f64; 2]) -> LineData {
if let AngledLineData::AngleWithTag { tag, .. } = self {
LineData::PointWithTag { to, tag }
} else {
LineData::Point(to)
}
}
}
pub async fn angled_line(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (AngledLineData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_angled_line(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "angledLine",
}]
async fn inner_angled_line(
data: AngledLineData,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?;
let (angle, length) = match &data {
AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length),
AngledLineData::AngleAndLength(angle_and_length) => (angle_and_length[0], angle_and_length[1]),
};
let delta: [f64; 2] = [
length * f64::cos(angle.to_radians()),
length * f64::sin(angle.to_radians()),
];
let relative = true;
let to: [f64; 2] = [from.x + delta[0], from.y + delta[1]];
let id = uuid::Uuid::new_v4();
let current_path = Path::ToPoint {
base: BasePath {
from: from.into(),
to,
name: if let AngledLineData::AngleWithTag { tag, .. } = data {
tag.to_string()
} else {
"".to_string()
},
geo_meta: GeoMeta {
id,
metadata: args.source_range.into(),
},
},
};
args.send_modeling_cmd(
id,
ModelingCmd::ExtendPath {
path: sketch_group.id,
segment: kittycad::types::PathSegment::Line {
end: Point3D {
x: delta[0],
y: delta[1],
z: 0.0,
},
relative,
},
},
)
.await?;
let mut new_sketch_group = sketch_group.clone();
new_sketch_group.value.push(current_path);
Ok(new_sketch_group)
}
pub async fn angled_line_of_x_length(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (AngledLineData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_angled_line_of_x_length(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "angledLineOfXLength",
}]
async fn inner_angled_line_of_x_length(
data: AngledLineData,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let (angle, length) = match &data {
AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length),
AngledLineData::AngleAndLength(angle_and_length) => (angle_and_length[0], angle_and_length[1]),
};
let to = get_y_component(Angle::from_degrees(angle), length);
let new_sketch_group = inner_line(data.into_inner_line(to.into()), sketch_group, args).await?;
Ok(new_sketch_group)
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase", untagged)]
pub enum AngledLineToData {
AngleWithTag {
angle: f64,
to: f64,
tag: String,
},
AngleAndPoint([f64; 2]),
}
impl AngledLineToData {
pub fn into_inner_line(self, x_to: f64, y_to: f64) -> LineToData {
if let AngledLineToData::AngleWithTag { tag, .. } = self {
LineToData::PointWithTag { to: [x_to, y_to], tag }
} else {
LineToData::Point([x_to, y_to])
}
}
}
pub async fn angled_line_to_x(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (AngledLineToData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_angled_line_to_x(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "angledLineToX",
}]
async fn inner_angled_line_to_x(
data: AngledLineToData,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?;
let (angle, x_to) = match &data {
AngledLineToData::AngleWithTag { angle, to, .. } => (*angle, *to),
AngledLineToData::AngleAndPoint(angle_and_to) => (angle_and_to[0], angle_and_to[1]),
};
let x_component = x_to - from.x;
let y_component = x_component * f64::tan(angle.to_radians());
let y_to = from.y + y_component;
let new_sketch_group = inner_line_to(data.into_inner_line(x_to, y_to), sketch_group, args).await?;
Ok(new_sketch_group)
}
pub async fn angled_line_of_y_length(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (AngledLineData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_angled_line_of_y_length(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "angledLineOfYLength",
}]
async fn inner_angled_line_of_y_length(
data: AngledLineData,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let (angle, length) = match &data {
AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length),
AngledLineData::AngleAndLength(angle_and_length) => (angle_and_length[0], angle_and_length[1]),
};
let to = get_x_component(Angle::from_degrees(angle), length);
let new_sketch_group = inner_line(data.into_inner_line(to.into()), sketch_group, args).await?;
Ok(new_sketch_group)
}
pub async fn angled_line_to_y(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (AngledLineToData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_angled_line_to_y(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "angledLineToY",
}]
async fn inner_angled_line_to_y(
data: AngledLineToData,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?;
let (angle, y_to) = match &data {
AngledLineToData::AngleWithTag { angle, to, .. } => (*angle, *to),
AngledLineToData::AngleAndPoint(angle_and_to) => (angle_and_to[0], angle_and_to[1]),
};
let y_component = y_to - from.y;
let x_component = y_component / f64::tan(angle.to_radians());
let x_to = from.x + x_component;
let new_sketch_group = inner_line_to(data.into_inner_line(x_to, y_to), sketch_group, args).await?;
Ok(new_sketch_group)
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct AngledLineThatIntersectsData {
pub angle: f64,
pub intersect_tag: String,
pub offset: Option<f64>,
pub tag: Option<String>,
}
pub async fn angled_line_that_intersects(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (AngledLineThatIntersectsData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_angled_line_that_intersects(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "angledLineThatIntersects",
}]
async fn inner_angled_line_that_intersects(
data: AngledLineThatIntersectsData,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let intersect_path = sketch_group
.get_path_by_name(&data.intersect_tag)
.ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: format!(
"Expected a line that exists in the given SketchGroup, found `{}`",
data.intersect_tag
),
source_ranges: vec![args.source_range],
})
})?
.get_base();
let from = sketch_group.get_coords_from_paths()?;
let to = intersection_with_parallel_line(
&[intersect_path.from.into(), intersect_path.to.into()],
data.offset.unwrap_or_default(),
data.angle,
from,
);
let line_to_data = if let Some(tag) = data.tag {
LineToData::PointWithTag { to: to.into(), tag }
} else {
LineToData::Point(to.into())
};
let new_sketch_group = inner_line_to(line_to_data, sketch_group, args).await?;
Ok(new_sketch_group)
}
pub async fn start_sketch_at(args: Args) -> Result<MemoryItem, KclError> {
let data: LineData = args.get_data()?;
let sketch_group = inner_start_sketch_at(data, args).await?;
Ok(MemoryItem::SketchGroup(sketch_group))
}
#[stdlib {
name = "startSketchAt",
}]
async fn inner_start_sketch_at(data: LineData, args: Args) -> Result<Box<SketchGroup>, KclError> {
let xy_plane = PlaneData::XY;
let sketch_surface = inner_start_sketch_on(SketchData::Plane(xy_plane), None, args.clone()).await?;
let sketch_group = inner_start_profile_at(data, sketch_surface, args).await?;
Ok(sketch_group)
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase", untagged)]
pub enum SketchData {
Plane(PlaneData),
ExtrudeGroup(Box<ExtrudeGroup>),
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, ExecutionPlanValue)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub enum PlaneData {
#[serde(rename = "XY", alias = "xy")]
XY,
#[serde(rename = "-XY", alias = "-xy")]
NegXY,
#[serde(rename = "XZ", alias = "xz")]
XZ,
#[serde(rename = "-XZ", alias = "-xz")]
NegXZ,
#[serde(rename = "YZ", alias = "yz")]
YZ,
#[serde(rename = "-YZ", alias = "-yz")]
NegYZ,
Plane {
origin: Box<Point3d>,
x_axis: Box<Point3d>,
y_axis: Box<Point3d>,
z_axis: Box<Point3d>,
},
}
impl From<PlaneData> for Plane {
fn from(value: PlaneData) -> Self {
let id = uuid::Uuid::new_v4();
match value {
PlaneData::XY => Plane {
id,
origin: Point3d::new(0.0, 0.0, 0.0),
x_axis: Point3d::new(1.0, 0.0, 0.0),
y_axis: Point3d::new(0.0, 1.0, 0.0),
z_axis: Point3d::new(0.0, 0.0, 1.0),
value: PlaneType::XY,
meta: vec![],
},
PlaneData::NegXY => Plane {
id,
origin: Point3d::new(0.0, 0.0, 0.0),
x_axis: Point3d::new(1.0, 0.0, 0.0),
y_axis: Point3d::new(0.0, 1.0, 0.0),
z_axis: Point3d::new(0.0, 0.0, -1.0),
value: PlaneType::XY,
meta: vec![],
},
PlaneData::XZ => Plane {
id,
origin: Point3d::new(0.0, 0.0, 0.0),
x_axis: Point3d::new(1.0, 0.0, 0.0),
y_axis: Point3d::new(0.0, 0.0, 1.0),
z_axis: Point3d::new(0.0, 1.0, 0.0),
value: PlaneType::XZ,
meta: vec![],
},
PlaneData::NegXZ => Plane {
id,
origin: Point3d::new(0.0, 0.0, 0.0),
x_axis: Point3d::new(1.0, 0.0, 0.0),
y_axis: Point3d::new(0.0, 0.0, 1.0),
z_axis: Point3d::new(0.0, -1.0, 0.0),
value: PlaneType::XZ,
meta: vec![],
},
PlaneData::YZ => Plane {
id,
origin: Point3d::new(0.0, 0.0, 0.0),
x_axis: Point3d::new(0.0, 1.0, 0.0),
y_axis: Point3d::new(0.0, 0.0, 1.0),
z_axis: Point3d::new(1.0, 0.0, 0.0),
value: PlaneType::YZ,
meta: vec![],
},
PlaneData::NegYZ => Plane {
id,
origin: Point3d::new(0.0, 0.0, 0.0),
x_axis: Point3d::new(0.0, 1.0, 0.0),
y_axis: Point3d::new(0.0, 0.0, 1.0),
z_axis: Point3d::new(-1.0, 0.0, 0.0),
value: PlaneType::YZ,
meta: vec![],
},
PlaneData::Plane {
origin,
x_axis,
y_axis,
z_axis,
} => Plane {
id,
origin: *origin,
x_axis: *x_axis,
y_axis: *y_axis,
z_axis: *z_axis,
value: PlaneType::Custom,
meta: vec![],
},
}
}
}
pub async fn start_sketch_on(args: Args) -> Result<MemoryItem, KclError> {
let (data, tag): (SketchData, Option<SketchOnFaceTag>) = args.get_data_and_optional_tag()?;
match inner_start_sketch_on(data, tag, args).await? {
SketchSurface::Plane(plane) => Ok(MemoryItem::Plane(plane)),
SketchSurface::Face(face) => Ok(MemoryItem::Face(face)),
}
}
#[stdlib {
name = "startSketchOn",
}]
async fn inner_start_sketch_on(
data: SketchData,
tag: Option<SketchOnFaceTag>,
args: Args,
) -> Result<SketchSurface, KclError> {
match data {
SketchData::Plane(plane_data) => {
let plane = start_sketch_on_plane(plane_data, args).await?;
Ok(SketchSurface::Plane(plane))
}
SketchData::ExtrudeGroup(extrude_group) => {
let Some(tag) = tag else {
return Err(KclError::Type(KclErrorDetails {
message: "Expected a tag for the face to sketch on".to_string(),
source_ranges: vec![args.source_range],
}));
};
let face = start_sketch_on_face(extrude_group, tag, args).await?;
Ok(SketchSurface::Face(face))
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display)]
#[ts(export)]
#[serde(rename_all = "snake_case", untagged)]
#[display("{0}")]
pub enum SketchOnFaceTag {
StartOrEnd(StartOrEnd),
String(String),
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
#[display(style = "snake_case")]
pub enum StartOrEnd {
#[serde(rename = "start", alias = "START")]
Start,
#[serde(rename = "end", alias = "END")]
End,
}
async fn start_sketch_on_face(
extrude_group: Box<ExtrudeGroup>,
tag: SketchOnFaceTag,
args: Args,
) -> Result<Box<Face>, KclError> {
let extrude_plane_id = match tag {
SketchOnFaceTag::String(ref s) => extrude_group
.value
.iter()
.find_map(|extrude_surface| match extrude_surface {
ExtrudeSurface::ExtrudePlane(extrude_plane) if extrude_plane.name == *s => {
Some(Ok(extrude_plane.face_id))
}
ExtrudeSurface::ExtrudeArc(extrude_arc) if extrude_arc.name == *s => {
Some(Err(KclError::Type(KclErrorDetails {
message: format!("Cannot sketch on a non-planar surface: `{}`", tag),
source_ranges: vec![args.source_range],
})))
}
ExtrudeSurface::ExtrudePlane(_) | ExtrudeSurface::ExtrudeArc(_) => None,
})
.ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: format!("Expected a face with the tag `{}`", tag),
source_ranges: vec![args.source_range],
})
})??,
SketchOnFaceTag::StartOrEnd(StartOrEnd::Start) => extrude_group.start_cap_id.ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: "Expected a start face to sketch on".to_string(),
source_ranges: vec![args.source_range],
})
})?,
SketchOnFaceTag::StartOrEnd(StartOrEnd::End) => extrude_group.end_cap_id.ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: "Expected an end face to sketch on".to_string(),
source_ranges: vec![args.source_range],
})
})?,
};
let id = uuid::Uuid::new_v4();
args.send_modeling_cmd(
id,
ModelingCmd::EnableSketchMode {
animated: false,
ortho: false,
entity_id: extrude_plane_id,
adjust_camera: false,
},
)
.await?;
Ok(Box::new(Face {
id,
value: tag.to_string(),
sketch_group_id: extrude_group.id,
x_axis: extrude_group.x_axis,
y_axis: extrude_group.y_axis,
z_axis: extrude_group.z_axis,
meta: vec![args.source_range.into()],
}))
}
async fn start_sketch_on_plane(data: PlaneData, args: Args) -> Result<Box<Plane>, KclError> {
let mut plane: Plane = data.clone().into();
let id = uuid::Uuid::new_v4();
let default_origin = Point3D { x: 0.0, y: 0.0, z: 0.0 };
let (x_axis, y_axis) = match data {
PlaneData::XY => (Point3D { x: 1.0, y: 0.0, z: 0.0 }, Point3D { x: 0.0, y: 1.0, z: 0.0 }),
PlaneData::NegXY => (
Point3D {
x: -1.0,
y: 0.0,
z: 0.0,
},
Point3D { x: 0.0, y: 1.0, z: 0.0 },
),
PlaneData::XZ => (
Point3D {
x: -1.0,
y: 0.0,
z: 0.0,
},
Point3D { x: 0.0, y: 0.0, z: 1.0 },
), PlaneData::NegXZ => (
Point3D {
x: 1.0, y: 0.0,
z: 0.0,
},
Point3D { x: 0.0, y: 0.0, z: 1.0 },
),
PlaneData::YZ => (Point3D { x: 0.0, y: 1.0, z: 0.0 }, Point3D { x: 0.0, y: 0.0, z: 1.0 }),
PlaneData::NegYZ => (
Point3D {
x: 0.0,
y: -1.0,
z: 0.0,
},
Point3D { x: 0.0, y: 0.0, z: 1.0 },
),
_ => (Point3D { x: 1.0, y: 0.0, z: 0.0 }, Point3D { x: 0.0, y: 1.0, z: 0.0 }),
};
plane.id = match data {
PlaneData::Plane {
origin,
x_axis,
y_axis,
z_axis: _,
} => {
args.send_modeling_cmd(
id,
ModelingCmd::MakePlane {
clobber: false,
origin: (*origin).into(),
size: 60.0,
x_axis: (*x_axis).into(),
y_axis: (*y_axis).into(),
hide: Some(true),
},
)
.await?;
id
}
_ => {
args.send_modeling_cmd(
id,
ModelingCmd::MakePlane {
clobber: false,
origin: default_origin,
size: 60.0,
x_axis,
y_axis,
hide: Some(true),
},
)
.await?;
id
}
};
args.send_modeling_cmd(
uuid::Uuid::new_v4(),
ModelingCmd::SketchModeEnable {
animated: false,
ortho: false,
plane_id: plane.id,
disable_camera_with_plane: Some(plane.z_axis.clone().into()),
},
)
.await?;
Ok(Box::new(plane))
}
pub async fn start_profile_at(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_surface): (LineData, SketchSurface) = args.get_data_and_sketch_surface()?;
let sketch_group = inner_start_profile_at(data, sketch_surface, args).await?;
Ok(MemoryItem::SketchGroup(sketch_group))
}
#[stdlib {
name = "startProfileAt",
}]
async fn inner_start_profile_at(
data: LineData,
sketch_surface: SketchSurface,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let to = match &data {
LineData::PointWithTag { to, .. } => *to,
LineData::Point(to) => *to,
};
let id = uuid::Uuid::new_v4();
let path_id = uuid::Uuid::new_v4();
args.send_modeling_cmd(path_id, ModelingCmd::StartPath {}).await?;
args.send_modeling_cmd(
id,
ModelingCmd::MovePathPen {
path: path_id,
to: Point3D {
x: to[0],
y: to[1],
z: 0.0,
},
},
)
.await?;
let current_path = BasePath {
from: to,
to,
name: if let LineData::PointWithTag { tag, .. } = data {
tag.to_string()
} else {
"".to_string()
},
geo_meta: GeoMeta {
id,
metadata: args.source_range.into(),
},
};
let sketch_group = SketchGroup {
id: path_id,
on: sketch_surface.clone(),
position: Position([0.0, 0.0, 0.0]),
rotation: Rotation([0.0, 0.0, 0.0, 1.0]),
x_axis: sketch_surface.x_axis(),
y_axis: sketch_surface.y_axis(),
z_axis: sketch_surface.z_axis(),
entity_id: Some(sketch_surface.id()),
value: vec![],
start: current_path,
meta: vec![args.source_range.into()],
};
Ok(Box::new(sketch_group))
}
pub async fn close(args: Args) -> Result<MemoryItem, KclError> {
let (sketch_group, tag): (Box<SketchGroup>, Option<String>) = args.get_sketch_group_and_optional_tag()?;
let new_sketch_group = inner_close(sketch_group, tag, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "close",
}]
async fn inner_close(
sketch_group: Box<SketchGroup>,
tag: Option<String>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?;
let to: Point2d = sketch_group.start.from.into();
let id = uuid::Uuid::new_v4();
args.send_modeling_cmd(
id,
ModelingCmd::ClosePath {
path_id: sketch_group.id,
},
)
.await?;
if let SketchSurface::Plane(_) = sketch_group.on {
args.send_modeling_cmd(uuid::Uuid::new_v4(), kittycad::types::ModelingCmd::SketchModeDisable {})
.await?;
}
let mut new_sketch_group = sketch_group.clone();
new_sketch_group.value.push(Path::ToPoint {
base: BasePath {
from: from.into(),
to: to.into(),
name: tag.unwrap_or_default(),
geo_meta: GeoMeta {
id,
metadata: args.source_range.into(),
},
},
});
Ok(new_sketch_group)
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase", untagged)]
pub enum ArcData {
AnglesAndRadius {
angle_start: f64,
angle_end: f64,
radius: f64,
#[serde(default)]
tag: Option<String>,
},
CenterToRadius {
center: [f64; 2],
to: [f64; 2],
radius: f64,
#[serde(default)]
tag: Option<String>,
},
}
pub async fn arc(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (ArcData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_arc(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "arc",
}]
async fn inner_arc(data: ArcData, sketch_group: Box<SketchGroup>, args: Args) -> Result<Box<SketchGroup>, KclError> {
let from: Point2d = sketch_group.get_coords_from_paths()?;
let (center, angle_start, angle_end, radius, end) = match &data {
ArcData::AnglesAndRadius {
angle_start,
angle_end,
radius,
..
} => {
let a_start = Angle::from_degrees(*angle_start);
let a_end = Angle::from_degrees(*angle_end);
let (center, end) = arc_center_and_end(from, a_start, a_end, *radius);
(center, a_start, a_end, *radius, end)
}
ArcData::CenterToRadius { center, to, radius, .. } => {
let (angle_start, angle_end) = arc_angles(from, center.into(), to.into(), *radius, args.source_range)?;
(center.into(), angle_start, angle_end, *radius, to.into())
}
};
let id = uuid::Uuid::new_v4();
args.send_modeling_cmd(
id,
ModelingCmd::ExtendPath {
path: sketch_group.id,
segment: kittycad::types::PathSegment::Arc {
start: angle_start,
end: angle_end,
center: center.into(),
radius,
relative: false,
},
},
)
.await?;
let current_path = Path::ToPoint {
base: BasePath {
from: from.into(),
to: end.into(),
name: match data {
ArcData::AnglesAndRadius { tag, .. } => tag.unwrap_or_default().to_string(),
ArcData::CenterToRadius { tag, .. } => tag.unwrap_or_default().to_string(),
},
geo_meta: GeoMeta {
id,
metadata: args.source_range.into(),
},
},
};
let mut new_sketch_group = sketch_group.clone();
new_sketch_group.value.push(current_path);
Ok(new_sketch_group)
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, JsonSchema, ts_rs::TS)]
#[ts(export)]
#[serde(rename_all = "camelCase", untagged)]
pub enum TangentialArcData {
RadiusAndOffset {
radius: f64,
offset: f64,
},
PointWithTag {
to: [f64; 2],
tag: String,
},
Point([f64; 2]),
}
pub async fn tangential_arc(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (TangentialArcData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_tangential_arc(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "tangentialArc",
}]
async fn inner_tangential_arc(
data: TangentialArcData,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let from: Point2d = sketch_group.get_coords_from_paths()?;
let id = uuid::Uuid::new_v4();
let to = match &data {
TangentialArcData::RadiusAndOffset { radius, offset } => {
let end_angle = Angle::from_degrees(*offset);
let start_angle = Angle::from_degrees(0.0);
let (_, to) = arc_center_and_end(from, start_angle, end_angle, *radius);
args.send_modeling_cmd(
id,
ModelingCmd::ExtendPath {
path: sketch_group.id,
segment: kittycad::types::PathSegment::TangentialArc {
radius: *radius,
offset: Angle {
unit: kittycad::types::UnitAngle::Degrees,
value: *offset,
},
},
},
)
.await?;
to.into()
}
TangentialArcData::PointWithTag { to, .. } => {
args.send_modeling_cmd(id, tan_arc_to(&sketch_group, to)).await?;
*to
}
TangentialArcData::Point(to) => {
args.send_modeling_cmd(id, tan_arc_to(&sketch_group, to)).await?;
*to
}
};
let to = [from.x + to[0], from.y + to[1]];
let current_path = Path::TangentialArc {
base: BasePath {
from: from.into(),
to,
name: match data {
TangentialArcData::PointWithTag { tag, .. } => tag.to_string(),
TangentialArcData::Point(_) | TangentialArcData::RadiusAndOffset { .. } => "".to_string(),
},
geo_meta: GeoMeta {
id,
metadata: args.source_range.into(),
},
},
};
let mut new_sketch_group = sketch_group.clone();
new_sketch_group.value.push(current_path);
Ok(new_sketch_group)
}
fn tan_arc_to(sketch_group: &SketchGroup, to: &[f64; 2]) -> ModelingCmd {
ModelingCmd::ExtendPath {
path: sketch_group.id,
segment: kittycad::types::PathSegment::TangentialArcTo {
angle_snap_increment: None,
to: Point3D {
x: to[0],
y: to[1],
z: 0.0,
},
},
}
}
fn too_few_args(source_range: SourceRange) -> KclError {
KclError::Syntax(KclErrorDetails {
source_ranges: vec![source_range],
message: "too few arguments".to_owned(),
})
}
fn get_arg<I: Iterator>(it: &mut I, src: SourceRange) -> Result<I::Item, KclError> {
it.next().ok_or_else(|| too_few_args(src))
}
pub async fn tangential_arc_to(args: Args) -> Result<MemoryItem, KclError> {
let src = args.source_range;
let mut it = args.args.iter();
let to: [f64; 2] = get_arg(&mut it, src)?.get_json()?;
let sketch_group: Box<SketchGroup> = get_arg(&mut it, src)?.get_json()?;
let tag = if let Ok(memory_item) = get_arg(&mut it, src) {
memory_item.get_json_opt()?
} else {
None
};
let new_sketch_group = inner_tangential_arc_to(to, sketch_group, tag, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "tangentialArcTo",
}]
async fn inner_tangential_arc_to(
to: [f64; 2],
sketch_group: Box<SketchGroup>,
tag: Option<String>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let from: Point2d = sketch_group.get_coords_from_paths()?;
let tangent_info = sketch_group.get_tangential_info_from_paths();
let tan_previous_point = if tangent_info.is_center {
get_tangent_point_from_previous_arc(tangent_info.center_or_tangent_point, tangent_info.ccw, from.into())
} else {
tangent_info.center_or_tangent_point
};
let [to_x, to_y] = to;
let result = get_tangential_arc_to_info(TangentialArcInfoInput {
arc_start_point: [from.x, from.y],
arc_end_point: to,
tan_previous_point,
obtuse: true,
});
let delta = [to_x - from.x, to_y - from.y];
let id = uuid::Uuid::new_v4();
args.send_modeling_cmd(id, tan_arc_to(&sketch_group, &delta)).await?;
let current_path = Path::TangentialArcTo {
base: BasePath {
from: from.into(),
to,
name: tag.unwrap_or_default(),
geo_meta: GeoMeta {
id,
metadata: args.source_range.into(),
},
},
center: result.center,
ccw: result.ccw > 0,
};
let mut new_sketch_group = sketch_group.clone();
new_sketch_group.value.push(current_path);
Ok(new_sketch_group)
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct BezierData {
to: [f64; 2],
control1: [f64; 2],
control2: [f64; 2],
tag: Option<String>,
}
pub async fn bezier_curve(args: Args) -> Result<MemoryItem, KclError> {
let (data, sketch_group): (BezierData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
let new_sketch_group = inner_bezier_curve(data, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "bezierCurve",
}]
async fn inner_bezier_curve(
data: BezierData,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?;
let relative = true;
let delta = data.to;
let to = [from.x + data.to[0], from.y + data.to[1]];
let id = uuid::Uuid::new_v4();
args.send_modeling_cmd(
id,
ModelingCmd::ExtendPath {
path: sketch_group.id,
segment: kittycad::types::PathSegment::Bezier {
control1: Point3D {
x: data.control1[0],
y: data.control1[1],
z: 0.0,
},
control2: Point3D {
x: data.control2[0],
y: data.control2[1],
z: 0.0,
},
end: Point3D {
x: delta[0],
y: delta[1],
z: 0.0,
},
relative,
},
},
)
.await?;
let current_path = Path::ToPoint {
base: BasePath {
from: from.into(),
to,
name: data.tag.unwrap_or_default().to_string(),
geo_meta: GeoMeta {
id,
metadata: args.source_range.into(),
},
},
};
let mut new_sketch_group = sketch_group.clone();
new_sketch_group.value.push(current_path);
Ok(new_sketch_group)
}
pub async fn hole(args: Args) -> Result<MemoryItem, KclError> {
let (hole_sketch_group, sketch_group): (SketchGroupSet, Box<SketchGroup>) = args.get_sketch_groups()?;
let new_sketch_group = inner_hole(hole_sketch_group, sketch_group, args).await?;
Ok(MemoryItem::SketchGroup(new_sketch_group))
}
#[stdlib {
name = "hole",
}]
async fn inner_hole(
hole_sketch_group: SketchGroupSet,
sketch_group: Box<SketchGroup>,
args: Args,
) -> Result<Box<SketchGroup>, KclError> {
match hole_sketch_group {
SketchGroupSet::SketchGroup(hole_sketch_group) => {
args.send_modeling_cmd(
uuid::Uuid::new_v4(),
ModelingCmd::Solid2DAddHole {
object_id: sketch_group.id,
hole_id: hole_sketch_group.id,
},
)
.await?;
args.send_modeling_cmd(
uuid::Uuid::new_v4(),
ModelingCmd::ObjectVisible {
object_id: hole_sketch_group.id,
hidden: true,
},
)
.await?;
}
SketchGroupSet::SketchGroups(hole_sketch_groups) => {
for hole_sketch_group in hole_sketch_groups {
args.send_modeling_cmd(
uuid::Uuid::new_v4(),
ModelingCmd::Solid2DAddHole {
object_id: sketch_group.id,
hole_id: hole_sketch_group.id,
},
)
.await?;
args.send_modeling_cmd(
uuid::Uuid::new_v4(),
ModelingCmd::ObjectVisible {
object_id: hole_sketch_group.id,
hidden: true,
},
)
.await?;
}
}
}
Ok(sketch_group)
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use crate::std::sketch::{LineData, PlaneData};
#[test]
fn test_deserialize_line_data() {
let data = LineData::Point([0.0, 1.0]);
let mut str_json = serde_json::to_string(&data).unwrap();
assert_eq!(str_json, "[0.0,1.0]");
str_json = "[0, 1]".to_string();
let data: LineData = serde_json::from_str(&str_json).unwrap();
assert_eq!(data, LineData::Point([0.0, 1.0]));
str_json = "{ \"to\": [0.0, 1.0], \"tag\": \"thing\" }".to_string();
let data: LineData = serde_json::from_str(&str_json).unwrap();
assert_eq!(
data,
LineData::PointWithTag {
to: [0.0, 1.0],
tag: "thing".to_string()
}
);
}
#[test]
fn test_deserialize_plane_data() {
let data = PlaneData::XY;
let mut str_json = serde_json::to_string(&data).unwrap();
assert_eq!(str_json, "\"XY\"");
str_json = "\"YZ\"".to_string();
let data: PlaneData = serde_json::from_str(&str_json).unwrap();
assert_eq!(data, PlaneData::YZ);
str_json = "\"-YZ\"".to_string();
let data: PlaneData = serde_json::from_str(&str_json).unwrap();
assert_eq!(data, PlaneData::NegYZ);
str_json = "\"-xz\"".to_string();
let data: PlaneData = serde_json::from_str(&str_json).unwrap();
assert_eq!(data, PlaneData::NegXZ);
}
#[test]
fn test_deserialize_sketch_on_face_tag() {
let data = "start";
let mut str_json = serde_json::to_string(&data).unwrap();
assert_eq!(str_json, "\"start\"");
str_json = "\"end\"".to_string();
let data: crate::std::sketch::SketchOnFaceTag = serde_json::from_str(&str_json).unwrap();
assert_eq!(
data,
crate::std::sketch::SketchOnFaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::End)
);
str_json = "\"thing\"".to_string();
let data: crate::std::sketch::SketchOnFaceTag = serde_json::from_str(&str_json).unwrap();
assert_eq!(data, crate::std::sketch::SketchOnFaceTag::String("thing".to_string()));
str_json = "\"END\"".to_string();
let data: crate::std::sketch::SketchOnFaceTag = serde_json::from_str(&str_json).unwrap();
assert_eq!(
data,
crate::std::sketch::SketchOnFaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::End)
);
str_json = "\"start\"".to_string();
let data: crate::std::sketch::SketchOnFaceTag = serde_json::from_str(&str_json).unwrap();
assert_eq!(
data,
crate::std::sketch::SketchOnFaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::Start)
);
str_json = "\"START\"".to_string();
let data: crate::std::sketch::SketchOnFaceTag = serde_json::from_str(&str_json).unwrap();
assert_eq!(
data,
crate::std::sketch::SketchOnFaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::Start)
);
}
}