2024-09-04 14:34:33 -07:00
|
|
|
//! Standard library plane helpers.
|
|
|
|
|
2024-11-18 16:25:25 -05:00
|
|
|
use kcmc::{each_cmd as mcmd, length_unit::LengthUnit, shared::Color, ModelingCmd};
|
|
|
|
use kittycad_modeling_cmds as kcmc;
|
2024-09-04 14:34:33 -07:00
|
|
|
|
2025-04-14 05:58:19 -04:00
|
|
|
use super::{args::TyF64, sketch::PlaneData};
|
2024-09-04 14:34:33 -07:00
|
|
|
use crate::{
|
|
|
|
errors::KclError,
|
2025-04-14 05:58:19 -04:00
|
|
|
execution::{types::RuntimeType, ExecState, KclValue, Plane, PlaneType},
|
2025-02-20 15:55:29 +13:00
|
|
|
std::Args,
|
2024-09-04 14:34:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Offset a plane by a distance along its normal.
|
2024-10-09 19:38:40 -04:00
|
|
|
pub async fn offset_plane(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
2025-02-20 15:55:29 +13:00
|
|
|
let std_plane = args.get_unlabeled_kw_arg("plane")?;
|
2025-04-14 05:58:19 -04:00
|
|
|
let offset: TyF64 = args.get_kw_arg_typed("offset", &RuntimeType::length(), exec_state)?;
|
2025-04-23 10:58:35 +12:00
|
|
|
let plane = inner_offset_plane(std_plane, offset, exec_state, &args).await?;
|
2025-01-22 09:42:09 +13:00
|
|
|
Ok(KclValue::Plane { value: Box::new(plane) })
|
2024-09-04 14:34:33 -07:00
|
|
|
}
|
|
|
|
|
2025-04-23 10:58:35 +12:00
|
|
|
async fn inner_offset_plane(
|
|
|
|
plane: PlaneData,
|
|
|
|
offset: TyF64,
|
|
|
|
exec_state: &mut ExecState,
|
|
|
|
args: &Args,
|
|
|
|
) -> Result<Plane, KclError> {
|
2025-02-20 15:55:29 +13:00
|
|
|
let mut plane = Plane::from_plane_data(plane, exec_state);
|
|
|
|
// Though offset planes might be derived from standard planes, they are not
|
2024-11-18 16:25:25 -05:00
|
|
|
// standard planes themselves.
|
|
|
|
plane.value = PlaneType::Custom;
|
2024-09-04 14:34:33 -07:00
|
|
|
|
2025-04-24 22:01:27 +12:00
|
|
|
let normal = plane.x_axis.cross(&plane.y_axis);
|
|
|
|
plane.origin += normal * offset.to_length_units(plane.origin.units);
|
2025-04-23 10:58:35 +12:00
|
|
|
make_offset_plane_in_engine(&plane, exec_state, args).await?;
|
2024-09-04 14:34:33 -07:00
|
|
|
|
2024-11-18 16:25:25 -05:00
|
|
|
Ok(plane)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Engine-side effectful creation of an actual plane object.
|
|
|
|
// offset planes are shown by default, and hidden by default if they
|
|
|
|
// are used as a sketch plane. That hiding command is sent within inner_start_profile_at
|
|
|
|
async fn make_offset_plane_in_engine(plane: &Plane, exec_state: &mut ExecState, args: &Args) -> Result<(), KclError> {
|
|
|
|
// Create new default planes.
|
|
|
|
let default_size = 100.0;
|
|
|
|
let color = Color {
|
|
|
|
r: 0.6,
|
|
|
|
g: 0.6,
|
|
|
|
b: 0.6,
|
|
|
|
a: 0.3,
|
|
|
|
};
|
|
|
|
|
|
|
|
args.batch_modeling_cmd(
|
|
|
|
plane.id,
|
|
|
|
ModelingCmd::from(mcmd::MakePlane {
|
|
|
|
clobber: false,
|
|
|
|
origin: plane.origin.into(),
|
|
|
|
size: LengthUnit(default_size),
|
|
|
|
x_axis: plane.x_axis.into(),
|
|
|
|
y_axis: plane.y_axis.into(),
|
|
|
|
hide: Some(false),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Set the color.
|
|
|
|
args.batch_modeling_cmd(
|
2024-12-17 09:38:32 +13:00
|
|
|
exec_state.next_uuid(),
|
2024-11-18 16:25:25 -05:00
|
|
|
ModelingCmd::from(mcmd::PlaneSetColor {
|
|
|
|
color,
|
|
|
|
plane_id: plane.id,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
2024-09-04 14:34:33 -07:00
|
|
|
}
|