use anyhow::Result;
use derive_docs::stdlib;
use schemars::JsonSchema;
use crate::{
errors::{KclError, KclErrorDetails},
executor::{KclValue, SketchGroup, TagIdentifier},
std::{utils::between, Args},
};
pub async fn segment_end_x(args: Args) -> Result<KclValue, KclError> {
let tag: TagIdentifier = args.get_data()?;
let result = inner_segment_end_x(&tag, args.clone())?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "segEndX",
}]
fn inner_segment_end_x(tag: &TagIdentifier, args: Args) -> Result<f64, KclError> {
let line = args.get_tag_engine_info(tag)?;
let path = line.path.clone().ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: format!("Expected a line segment with a path, found `{:?}`", line),
source_ranges: vec![args.source_range],
})
})?;
Ok(path.to[0])
}
pub async fn segment_end_y(args: Args) -> Result<KclValue, KclError> {
let tag: TagIdentifier = args.get_data()?;
let result = inner_segment_end_y(&tag, args.clone())?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "segEndY",
}]
fn inner_segment_end_y(tag: &TagIdentifier, args: Args) -> Result<f64, KclError> {
let line = args.get_tag_engine_info(tag)?;
let path = line.path.clone().ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: format!("Expected a line segment with a path, found `{:?}`", line),
source_ranges: vec![args.source_range],
})
})?;
Ok(path.to[1])
}
pub async fn last_segment_x(args: Args) -> Result<KclValue, KclError> {
let sketch_group = args.get_sketch_group()?;
let result = inner_last_segment_x(sketch_group, args.clone())?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "lastSegX",
}]
fn inner_last_segment_x(sketch_group: SketchGroup, args: Args) -> Result<f64, KclError> {
let last_line = sketch_group
.value
.last()
.ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: format!(
"Expected a SketchGroup with at least one segment, found `{:?}`",
sketch_group
),
source_ranges: vec![args.source_range],
})
})?
.get_base();
Ok(last_line.to[0])
}
pub async fn last_segment_y(args: Args) -> Result<KclValue, KclError> {
let sketch_group = args.get_sketch_group()?;
let result = inner_last_segment_y(sketch_group, args.clone())?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "lastSegY",
}]
fn inner_last_segment_y(sketch_group: SketchGroup, args: Args) -> Result<f64, KclError> {
let last_line = sketch_group
.value
.last()
.ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: format!(
"Expected a SketchGroup with at least one segment, found `{:?}`",
sketch_group
),
source_ranges: vec![args.source_range],
})
})?
.get_base();
Ok(last_line.to[1])
}
pub async fn segment_length(args: Args) -> Result<KclValue, KclError> {
let tag: TagIdentifier = args.get_data()?;
let result = inner_segment_length(&tag, args.clone())?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "segLen",
}]
fn inner_segment_length(tag: &TagIdentifier, args: Args) -> Result<f64, KclError> {
let line = args.get_tag_engine_info(tag)?;
let path = line.path.clone().ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: format!("Expected a line segment with a path, found `{:?}`", line),
source_ranges: vec![args.source_range],
})
})?;
let result = ((path.from[1] - path.to[1]).powi(2) + (path.from[0] - path.to[0]).powi(2)).sqrt();
Ok(result)
}
pub async fn segment_angle(args: Args) -> Result<KclValue, KclError> {
let tag: TagIdentifier = args.get_data()?;
let result = inner_segment_angle(&tag, args.clone())?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "segAng",
}]
fn inner_segment_angle(tag: &TagIdentifier, args: Args) -> Result<f64, KclError> {
let line = args.get_tag_engine_info(tag)?;
let path = line.path.clone().ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: format!("Expected a line segment with a path, found `{:?}`", line),
source_ranges: vec![args.source_range],
})
})?;
let result = between(path.from.into(), path.to.into());
Ok(result.degrees())
}
pub async fn angle_to_match_length_x(args: Args) -> Result<KclValue, KclError> {
let (tag, to, sketch_group) = args.get_tag_to_number_sketch_group()?;
let result = inner_angle_to_match_length_x(&tag, to, sketch_group, args.clone())?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "angleToMatchLengthX",
}]
fn inner_angle_to_match_length_x(
tag: &TagIdentifier,
to: f64,
sketch_group: SketchGroup,
args: Args,
) -> Result<f64, KclError> {
let line = args.get_tag_engine_info(tag)?;
let path = line.path.clone().ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: format!("Expected a line segment with a path, found `{:?}`", line),
source_ranges: vec![args.source_range],
})
})?;
let length = ((path.from[1] - path.to[1]).powi(2) + (path.from[0] - path.to[0]).powi(2)).sqrt();
let last_line = sketch_group
.value
.last()
.ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: format!(
"Expected a SketchGroup with at least one segment, found `{:?}`",
sketch_group
),
source_ranges: vec![args.source_range],
})
})?
.get_base();
let diff = (to - last_line.to[0]).abs();
let angle_r = (diff / length).acos();
if diff > length {
Ok(0.0)
} else {
Ok(angle_r.to_degrees())
}
}
pub async fn angle_to_match_length_y(args: Args) -> Result<KclValue, KclError> {
let (tag, to, sketch_group) = args.get_tag_to_number_sketch_group()?;
let result = inner_angle_to_match_length_y(&tag, to, sketch_group, args.clone())?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "angleToMatchLengthY",
}]
fn inner_angle_to_match_length_y(
tag: &TagIdentifier,
to: f64,
sketch_group: SketchGroup,
args: Args,
) -> Result<f64, KclError> {
let line = args.get_tag_engine_info(tag)?;
let path = line.path.clone().ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: format!("Expected a line segment with a path, found `{:?}`", line),
source_ranges: vec![args.source_range],
})
})?;
let length = ((path.from[1] - path.to[1]).powi(2) + (path.from[0] - path.to[0]).powi(2)).sqrt();
let last_line = sketch_group
.value
.last()
.ok_or_else(|| {
KclError::Type(KclErrorDetails {
message: format!(
"Expected a SketchGroup with at least one segment, found `{:?}`",
sketch_group
),
source_ranges: vec![args.source_range],
})
})?
.get_base();
let diff = (to - last_line.to[1]).abs();
let angle_r = (diff / length).asin();
if diff > length {
Ok(0.0)
} else {
Ok(angle_r.to_degrees())
}
}