KCL: change twist to a case of extrude (#7481)

@franknoirot @jtran and I decided that the `extrudeTwist()` function (which I added in https://github.com/KittyCAD/modeling-app/pull/7480) would be better as an optional case of the normal `extrude` function. Doing it this way means less work for the frontend team.
This commit is contained in:
Adam Chalmers
2025-06-16 11:35:30 -05:00
committed by GitHub
parent 4159cc0047
commit d6278cf075
10 changed files with 60 additions and 121 deletions

View File

@ -38,6 +38,10 @@ pub async fn extrude(exec_state: &mut ExecState, args: Args) -> Result<KclValue,
args.get_kw_arg_opt("bidirectionalLength", &RuntimeType::length(), exec_state)?;
let tag_start = args.get_kw_arg_opt("tagStart", &RuntimeType::tag_decl(), exec_state)?;
let tag_end = args.get_kw_arg_opt("tagEnd", &RuntimeType::tag_decl(), exec_state)?;
let twist_angle: Option<TyF64> = args.get_kw_arg_opt("twistAngle", &RuntimeType::degrees(), exec_state)?;
let twist_angle_step: Option<TyF64> = args.get_kw_arg_opt("twistAngleStep", &RuntimeType::degrees(), exec_state)?;
let twist_center: Option<[TyF64; 2]> = args.get_kw_arg_opt("twistCenter", &RuntimeType::point2d(), exec_state)?;
let tolerance: Option<TyF64> = args.get_kw_arg_opt("tolerance", &RuntimeType::length(), exec_state)?;
let result = inner_extrude(
sketches,
@ -46,6 +50,10 @@ pub async fn extrude(exec_state: &mut ExecState, args: Args) -> Result<KclValue,
bidirectional_length,
tag_start,
tag_end,
twist_angle,
twist_angle_step,
twist_center,
tolerance,
exec_state,
args,
)
@ -62,11 +70,16 @@ async fn inner_extrude(
bidirectional_length: Option<TyF64>,
tag_start: Option<TagNode>,
tag_end: Option<TagNode>,
twist_angle: Option<TyF64>,
twist_angle_step: Option<TyF64>,
twist_center: Option<[TyF64; 2]>,
tolerance: Option<TyF64>,
exec_state: &mut ExecState,
args: Args,
) -> Result<Vec<Solid>, KclError> {
// Extrude the element(s).
let mut solids = Vec::new();
let tolerance = LengthUnit(tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE));
if symmetric.unwrap_or(false) && bidirectional_length.is_some() {
return Err(KclError::new_semantic(KclErrorDetails::new(
@ -88,97 +101,29 @@ async fn inner_extrude(
for sketch in &sketches {
let id = exec_state.next_uuid();
let cmds = sketch.build_sketch_mode_cmds(
exec_state,
ModelingCmdReq {
cmd_id: id.into(),
cmd: ModelingCmd::from(mcmd::Extrude {
target: sketch.id.into(),
distance: LengthUnit(length.to_mm()),
faces: Default::default(),
opposite: opposite.clone(),
}),
},
);
exec_state
.batch_modeling_cmds(ModelingCmdMeta::from_args_id(&args, id), &cmds)
.await?;
solids.push(
do_post_extrude(
sketch,
id.into(),
length.clone(),
false,
&NamedCapTags {
start: tag_start.as_ref(),
end: tag_end.as_ref(),
},
exec_state,
&args,
None,
)
.await?,
);
}
Ok(solids)
}
/// Extrudes by a given amount, twisting the sketch as it goes.
pub async fn extrude_twist(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let sketches = args.get_unlabeled_kw_arg("sketches", &RuntimeType::sketches(), exec_state)?;
let length: TyF64 = args.get_kw_arg("length", &RuntimeType::length(), exec_state)?;
let tolerance: Option<TyF64> = args.get_kw_arg_opt("tolerance", &RuntimeType::length(), exec_state)?;
let angle: TyF64 = args.get_kw_arg("angle", &RuntimeType::degrees(), exec_state)?;
let angle_step: Option<TyF64> = args.get_kw_arg_opt("angleStep", &RuntimeType::degrees(), exec_state)?;
let center: Option<[TyF64; 2]> = args.get_kw_arg_opt("center", &RuntimeType::point2d(), exec_state)?;
let tag_start = args.get_kw_arg_opt("tagStart", &RuntimeType::tag_decl(), exec_state)?;
let tag_end = args.get_kw_arg_opt("tagEnd", &RuntimeType::tag_decl(), exec_state)?;
let result = inner_extrude_twist(
sketches, length, tag_start, tag_end, center, angle, angle_step, tolerance, exec_state, args,
)
.await?;
Ok(result.into())
}
#[allow(clippy::too_many_arguments)]
async fn inner_extrude_twist(
sketches: Vec<Sketch>,
length: TyF64,
tag_start: Option<TagNode>,
tag_end: Option<TagNode>,
center: Option<[TyF64; 2]>,
angle: TyF64,
angle_step: Option<TyF64>,
tolerance: Option<TyF64>,
exec_state: &mut ExecState,
args: Args,
) -> Result<Vec<Solid>, KclError> {
// Extrude the element(s).
let mut solids = Vec::new();
let tolerance = LengthUnit(tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE));
let center = center.map(point_to_mm).map(Point2d::from).unwrap_or_default();
let angle_step_size = Angle::from_degrees(angle_step.map(|a| a.to_degrees()).unwrap_or(15.0));
for sketch in &sketches {
let id = exec_state.next_uuid();
let cmds = sketch.build_sketch_mode_cmds(
exec_state,
ModelingCmdReq {
cmd_id: id.into(),
cmd: ModelingCmd::from(mcmd::TwistExtrude {
let cmd = match (&twist_angle, &twist_angle_step, &twist_center) {
(Some(angle), angle_step, center) => {
let center = center.clone().map(point_to_mm).map(Point2d::from).unwrap_or_default();
let total_rotation_angle = Angle::from_degrees(angle.to_degrees());
let angle_step_size = Angle::from_degrees(angle_step.clone().map(|a| a.to_degrees()).unwrap_or(15.0));
ModelingCmd::from(mcmd::TwistExtrude {
target: sketch.id.into(),
distance: LengthUnit(length.to_mm()),
faces: Default::default(),
center_2d: center,
total_rotation_angle: Angle::from_degrees(angle.to_degrees()),
total_rotation_angle,
angle_step_size,
tolerance,
}),
},
);
})
}
(None, _, _) => ModelingCmd::from(mcmd::Extrude {
target: sketch.id.into(),
distance: LengthUnit(length.to_mm()),
faces: Default::default(),
opposite: opposite.clone(),
}),
};
let cmds = sketch.build_sketch_mode_cmds(exec_state, ModelingCmdReq { cmd_id: id.into(), cmd });
exec_state
.batch_modeling_cmds(ModelingCmdMeta::from_args_id(&args, id), &cmds)
.await?;