use anyhow::Result;
use derive_docs::stdlib;
use kcmc::each_cmd as mcmd;
use kcmc::length_unit::LengthUnit;
use kcmc::shared::Angle;
use kcmc::shared::Point2d as KPoint2d;
use kcmc::ModelingCmd;
use kittycad_modeling_cmds as kcmc;
use kittycad_modeling_cmds::shared::PathSegment;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{
ast::types::TagDeclarator,
errors::KclError,
executor::{BasePath, ExecState, GeoMeta, KclValue, Path, SketchGroup, SketchSurface},
std::Args,
};
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(untagged)]
pub enum SketchSurfaceOrGroup {
SketchSurface(SketchSurface),
SketchGroup(Box<SketchGroup>),
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct CircleData {
pub center: [f64; 2],
pub radius: f64,
}
pub async fn circle(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let (data, sketch_surface_or_group, tag): (CircleData, SketchSurfaceOrGroup, Option<TagDeclarator>) =
args.get_circle_args()?;
let sketch_group = inner_circle(data, sketch_surface_or_group, tag, exec_state, args).await?;
Ok(KclValue::new_user_val(sketch_group.meta.clone(), sketch_group))
}
#[stdlib {
name = "circle",
}]
async fn inner_circle(
data: CircleData,
sketch_surface_or_group: SketchSurfaceOrGroup,
tag: Option<TagDeclarator>,
exec_state: &mut ExecState,
args: Args,
) -> Result<SketchGroup, KclError> {
let sketch_surface = match sketch_surface_or_group {
SketchSurfaceOrGroup::SketchSurface(surface) => surface,
SketchSurfaceOrGroup::SketchGroup(group) => group.on,
};
let sketch_group = crate::std::sketch::inner_start_profile_at(
[data.center[0] + data.radius, data.center[1]],
sketch_surface,
None,
exec_state,
args.clone(),
)
.await?;
let angle_start = Angle::zero();
let angle_end = Angle::turn();
let id = uuid::Uuid::new_v4();
args.batch_modeling_cmd(
id,
ModelingCmd::from(mcmd::ExtendPath {
path: sketch_group.id.into(),
segment: PathSegment::Arc {
start: angle_start,
end: angle_end,
center: KPoint2d::from(data.center).map(LengthUnit),
radius: data.radius.into(),
relative: false,
},
}),
)
.await?;
let current_path = Path::Circle {
base: BasePath {
from: data.center,
to: data.center,
tag: tag.clone(),
geo_meta: GeoMeta {
id,
metadata: args.source_range.into(),
},
},
radius: data.radius,
center: data.center,
ccw: angle_start.to_degrees() < angle_end.to_degrees(),
};
let mut new_sketch_group = sketch_group.clone();
if let Some(tag) = &tag {
new_sketch_group.add_tag(tag, ¤t_path);
}
new_sketch_group.value.push(current_path);
args.batch_modeling_cmd(
id,
ModelingCmd::from(mcmd::ClosePath {
path_id: new_sketch_group.id,
}),
)
.await?;
Ok(new_sketch_group)
}