KCL: Convert x/y lines to use keyword arguments (#5615)
Previously, `xLine`, `xLineTo`, `yLine` and `yLineTo` used positional arguments. Now: - `xLineTo` and `yLineTo` have been removed - `xLine` and `yLine` both use keyword arguments: - `length`, optional (i.e. a relative distance along the X or Y axis) - `endAbsolute` optional (i.e. an absolute point along the X or Y axis) - `tag` optional - Exactly one of `length` or `endAbsolute` must be given. Not both, not neither. For example: ``` // Old way |> xLine(6.04, %) |> yLineTo(20, %, $base) // New way |> xLine(length = 6.04) |> yLine(endAbsolute = 20, tag = $base) ``` This also improves some of the general-purpose keyword arguments code in modeling app's TS codebase.
This commit is contained in:
@ -108,7 +108,7 @@ pub async fn sqrt(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, K
|
||||
/// angle = 50,
|
||||
/// length = sqrt(2500),
|
||||
/// }, %)
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// example = extrude(exampleSketch, length = 5)
|
||||
@ -173,7 +173,7 @@ pub async fn round(_exec_state: &mut ExecState, args: Args) -> Result<KclValue,
|
||||
/// |> startProfileAt([0, 0], %)
|
||||
/// |> line(endAbsolute = [12, 10])
|
||||
/// |> line(end = [round(7.02986), 0])
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// extrude001 = extrude(sketch001, length = 5)
|
||||
@ -201,7 +201,7 @@ pub async fn floor(_exec_state: &mut ExecState, args: Args) -> Result<KclValue,
|
||||
/// |> startProfileAt([0, 0], %)
|
||||
/// |> line(endAbsolute = [12, 10])
|
||||
/// |> line(end = [floor(7.02986), 0])
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// extrude001 = extrude(sketch001, length = 5)
|
||||
@ -229,7 +229,7 @@ pub async fn ceil(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, K
|
||||
/// |> startProfileAt([0, 0], %)
|
||||
/// |> line(endAbsolute = [12, 10])
|
||||
/// |> line(end = [ceil(7.02986), 0])
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// extrude001 = extrude(sketch001, length = 5)
|
||||
@ -347,7 +347,7 @@ pub async fn pow(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kc
|
||||
/// angle = 50,
|
||||
/// length = pow(5, 2),
|
||||
/// }, %)
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// example = extrude(exampleSketch, length = 5)
|
||||
@ -408,7 +408,7 @@ pub async fn asin(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, K
|
||||
/// angle = toDegrees(asin(0.5)),
|
||||
/// length = 20,
|
||||
/// }, %)
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// extrude001 = extrude(sketch001, length = 5)
|
||||
@ -438,7 +438,7 @@ pub async fn atan(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, K
|
||||
/// angle = toDegrees(atan(1.25)),
|
||||
/// length = 20,
|
||||
/// }, %)
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// extrude001 = extrude(sketch001, length = 5)
|
||||
@ -468,7 +468,7 @@ pub async fn atan2(_exec_state: &mut ExecState, args: Args) -> Result<KclValue,
|
||||
/// angle = toDegrees(atan2(1.25, 2)),
|
||||
/// length = 20,
|
||||
/// }, %)
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// extrude001 = extrude(sketch001, length = 5)
|
||||
@ -632,7 +632,7 @@ pub async fn e(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclE
|
||||
/// angle = 30,
|
||||
/// length = 2 * e() ^ 2,
|
||||
/// }, %)
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// example = extrude(exampleSketch, length = 10)
|
||||
@ -664,7 +664,7 @@ pub async fn tau(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kc
|
||||
/// angle = 50,
|
||||
/// length = 10 * tau(),
|
||||
/// }, %)
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// example = extrude(exampleSketch, length = 5)
|
||||
@ -695,7 +695,7 @@ pub async fn to_radians(_exec_state: &mut ExecState, args: Args) -> Result<KclVa
|
||||
/// angle = 50,
|
||||
/// length = 70 * cos(toRadians(45)),
|
||||
/// }, %)
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// example = extrude(exampleSketch, length = 5)
|
||||
@ -725,7 +725,7 @@ pub async fn to_degrees(_exec_state: &mut ExecState, args: Args) -> Result<KclVa
|
||||
/// angle = 50,
|
||||
/// length = 70 * cos(toDegrees(pi()/4)),
|
||||
/// }, %)
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// example = extrude(exampleSketch, length = 5)
|
||||
|
@ -73,9 +73,7 @@ lazy_static! {
|
||||
Box::new(crate::std::shapes::CircleThreePoint),
|
||||
Box::new(crate::std::shapes::Polygon),
|
||||
Box::new(crate::std::sketch::Line),
|
||||
Box::new(crate::std::sketch::XLineTo),
|
||||
Box::new(crate::std::sketch::XLine),
|
||||
Box::new(crate::std::sketch::YLineTo),
|
||||
Box::new(crate::std::sketch::YLine),
|
||||
Box::new(crate::std::sketch::AngledLineToX),
|
||||
Box::new(crate::std::sketch::AngledLineToY),
|
||||
|
@ -657,7 +657,7 @@ pub async fn angle_to_match_length_y(exec_state: &mut ExecState, args: Args) ->
|
||||
/// angle = angleToMatchLengthY(seg01, 15, %),
|
||||
/// length = 5,
|
||||
/// }, %)
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// extrusion = extrude(sketch001, length = 5)
|
||||
|
@ -260,113 +260,14 @@ async fn straight_line(
|
||||
Ok(new_sketch)
|
||||
}
|
||||
|
||||
/// Draw a line to a point on the x-axis.
|
||||
pub async fn x_line_to(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let (to, sketch, tag): (f64, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
|
||||
|
||||
let new_sketch = inner_x_line_to(to, sketch, tag, exec_state, args).await?;
|
||||
Ok(KclValue::Sketch {
|
||||
value: Box::new(new_sketch),
|
||||
})
|
||||
}
|
||||
|
||||
/// Draw a line parallel to the X axis, that ends at the given X.
|
||||
/// E.g. if the previous line ended at (1, 1),
|
||||
/// then xLineTo(4) draws a line from (1, 1) to (4, 1)
|
||||
///
|
||||
/// ```no_run
|
||||
/// exampleSketch = startSketchOn(XZ)
|
||||
/// |> startProfileAt([0, 0], %)
|
||||
/// |> xLineTo(15, %)
|
||||
/// |> angledLine({
|
||||
/// angle = 80,
|
||||
/// length = 15,
|
||||
/// }, %)
|
||||
/// |> line(end = [8, -10])
|
||||
/// |> xLineTo(40, %)
|
||||
/// |> angledLine({
|
||||
/// angle = 135,
|
||||
/// length = 30,
|
||||
/// }, %)
|
||||
/// |> xLineTo(10, %)
|
||||
/// |> close()
|
||||
///
|
||||
/// example = extrude(exampleSketch, length = 10)
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "xLineTo",
|
||||
}]
|
||||
async fn inner_x_line_to(
|
||||
to: f64,
|
||||
sketch: Sketch,
|
||||
tag: Option<TagNode>,
|
||||
exec_state: &mut ExecState,
|
||||
args: Args,
|
||||
) -> Result<Sketch, KclError> {
|
||||
let from = sketch.current_pen_position()?;
|
||||
|
||||
let new_sketch = straight_line(
|
||||
StraightLineParams::absolute([to, from.y], sketch, tag),
|
||||
exec_state,
|
||||
args,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(new_sketch)
|
||||
}
|
||||
|
||||
/// Draw a line to a point on the y-axis.
|
||||
pub async fn y_line_to(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let (to, sketch, tag): (f64, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
|
||||
|
||||
let new_sketch = inner_y_line_to(to, sketch, tag, exec_state, args).await?;
|
||||
Ok(KclValue::Sketch {
|
||||
value: Box::new(new_sketch),
|
||||
})
|
||||
}
|
||||
|
||||
/// Draw a line parallel to the Y axis, that ends at the given Y.
|
||||
/// E.g. if the previous line ended at (1, 1),
|
||||
/// then yLineTo(4) draws a line from (1, 1) to (1, 4)
|
||||
///
|
||||
/// ```no_run
|
||||
/// exampleSketch = startSketchOn(XZ)
|
||||
/// |> startProfileAt([0, 0], %)
|
||||
/// |> angledLine({
|
||||
/// angle = 50,
|
||||
/// length = 45,
|
||||
/// }, %)
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> close()
|
||||
///
|
||||
/// example = extrude(exampleSketch, length = 5)
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "yLineTo",
|
||||
}]
|
||||
async fn inner_y_line_to(
|
||||
to: f64,
|
||||
sketch: Sketch,
|
||||
tag: Option<TagNode>,
|
||||
exec_state: &mut ExecState,
|
||||
args: Args,
|
||||
) -> Result<Sketch, KclError> {
|
||||
let from = sketch.current_pen_position()?;
|
||||
|
||||
let new_sketch = straight_line(
|
||||
StraightLineParams::absolute([from.x, to], sketch, tag),
|
||||
exec_state,
|
||||
args,
|
||||
)
|
||||
.await?;
|
||||
Ok(new_sketch)
|
||||
}
|
||||
|
||||
/// Draw a line on the x-axis.
|
||||
pub async fn x_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let (length, sketch, tag): (f64, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
|
||||
let sketch = args.get_unlabeled_kw_arg("sketch")?;
|
||||
let length = args.get_kw_arg_opt("length")?;
|
||||
let end_absolute = args.get_kw_arg_opt("endAbsolute")?;
|
||||
let tag = args.get_kw_arg_opt(NEW_TAG_KW)?;
|
||||
|
||||
let new_sketch = inner_x_line(length, sketch, tag, exec_state, args).await?;
|
||||
let new_sketch = inner_x_line(sketch, length, end_absolute, tag, exec_state, args).await?;
|
||||
Ok(KclValue::Sketch {
|
||||
value: Box::new(new_sketch),
|
||||
})
|
||||
@ -378,34 +279,49 @@ pub async fn x_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue,
|
||||
/// ```no_run
|
||||
/// exampleSketch = startSketchOn(XZ)
|
||||
/// |> startProfileAt([0, 0], %)
|
||||
/// |> xLine(15, %)
|
||||
/// |> xLine(length = 15)
|
||||
/// |> angledLine({
|
||||
/// angle = 80,
|
||||
/// length = 15,
|
||||
/// }, %)
|
||||
/// |> line(end = [8, -10])
|
||||
/// |> xLine(10, %)
|
||||
/// |> xLine(length = 10)
|
||||
/// |> angledLine({
|
||||
/// angle = 120,
|
||||
/// length = 30,
|
||||
/// }, %)
|
||||
/// |> xLine(-15, %)
|
||||
/// |> xLine(length = -15)
|
||||
/// |> close()
|
||||
///
|
||||
/// example = extrude(exampleSketch, length = 10)
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "xLine",
|
||||
keywords = true,
|
||||
unlabeled_first = true,
|
||||
args = {
|
||||
sketch = { docs = "Which sketch should this path be added to?"},
|
||||
length = { docs = "How far away along the X axis should this line go? Incompatible with `endAbsolute`.", include_in_snippet = true},
|
||||
end_absolute = { docs = "Which absolute X value should this line go to? Incompatible with `length`."},
|
||||
tag = { docs = "Create a new tag which refers to this line"},
|
||||
}
|
||||
}]
|
||||
async fn inner_x_line(
|
||||
length: f64,
|
||||
sketch: Sketch,
|
||||
length: Option<f64>,
|
||||
end_absolute: Option<f64>,
|
||||
tag: Option<TagNode>,
|
||||
exec_state: &mut ExecState,
|
||||
args: Args,
|
||||
) -> Result<Sketch, KclError> {
|
||||
let from = sketch.current_pen_position()?;
|
||||
straight_line(
|
||||
StraightLineParams::relative([length, 0.0], sketch, tag),
|
||||
StraightLineParams {
|
||||
sketch,
|
||||
end_absolute: end_absolute.map(|x| [x, from.y]),
|
||||
end: length.map(|x| [x, 0.0]),
|
||||
tag,
|
||||
},
|
||||
exec_state,
|
||||
args,
|
||||
)
|
||||
@ -414,9 +330,12 @@ async fn inner_x_line(
|
||||
|
||||
/// Draw a line on the y-axis.
|
||||
pub async fn y_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let (length, sketch, tag): (f64, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
|
||||
let sketch = args.get_unlabeled_kw_arg("sketch")?;
|
||||
let length = args.get_kw_arg_opt("length")?;
|
||||
let end_absolute = args.get_kw_arg_opt("endAbsolute")?;
|
||||
let tag = args.get_kw_arg_opt(NEW_TAG_KW)?;
|
||||
|
||||
let new_sketch = inner_y_line(length, sketch, tag, exec_state, args).await?;
|
||||
let new_sketch = inner_y_line(sketch, length, end_absolute, tag, exec_state, args).await?;
|
||||
Ok(KclValue::Sketch {
|
||||
value: Box::new(new_sketch),
|
||||
})
|
||||
@ -428,29 +347,44 @@ pub async fn y_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue,
|
||||
/// ```no_run
|
||||
/// exampleSketch = startSketchOn(XZ)
|
||||
/// |> startProfileAt([0, 0], %)
|
||||
/// |> yLine(15, %)
|
||||
/// |> yLine(length = 15)
|
||||
/// |> angledLine({
|
||||
/// angle = 30,
|
||||
/// length = 15,
|
||||
/// }, %)
|
||||
/// |> line(end = [8, -10])
|
||||
/// |> yLine(-5, %)
|
||||
/// |> yLine(length = -5)
|
||||
/// |> close()
|
||||
///
|
||||
/// example = extrude(exampleSketch, length = 10)
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "yLine",
|
||||
keywords = true,
|
||||
unlabeled_first = true,
|
||||
args = {
|
||||
sketch = { docs = "Which sketch should this path be added to?"},
|
||||
length = { docs = "How far away along the Y axis should this line go? Incompatible with `endAbsolute`.", include_in_snippet = true},
|
||||
end_absolute = { docs = "Which absolute Y value should this line go to? Incompatible with `length`."},
|
||||
tag = { docs = "Create a new tag which refers to this line"},
|
||||
}
|
||||
}]
|
||||
async fn inner_y_line(
|
||||
length: f64,
|
||||
sketch: Sketch,
|
||||
length: Option<f64>,
|
||||
end_absolute: Option<f64>,
|
||||
tag: Option<TagNode>,
|
||||
exec_state: &mut ExecState,
|
||||
args: Args,
|
||||
) -> Result<Sketch, KclError> {
|
||||
let from = sketch.current_pen_position()?;
|
||||
straight_line(
|
||||
StraightLineParams::relative([0.0, length], sketch, tag),
|
||||
StraightLineParams {
|
||||
sketch,
|
||||
end_absolute: end_absolute.map(|y| [from.x, y]),
|
||||
end: length.map(|y| [0.0, y]),
|
||||
tag,
|
||||
},
|
||||
exec_state,
|
||||
args,
|
||||
)
|
||||
@ -489,13 +423,13 @@ pub async fn angled_line(exec_state: &mut ExecState, args: Args) -> Result<KclVa
|
||||
/// ```no_run
|
||||
/// exampleSketch = startSketchOn(XZ)
|
||||
/// |> startProfileAt([0, 0], %)
|
||||
/// |> yLineTo(15, %)
|
||||
/// |> yLine(endAbsolute = 15)
|
||||
/// |> angledLine({
|
||||
/// angle = 30,
|
||||
/// length = 15,
|
||||
/// }, %)
|
||||
/// |> line(end = [8, -10])
|
||||
/// |> yLineTo(0, %)
|
||||
/// |> yLine(endAbsolute = 0)
|
||||
/// |> close()
|
||||
///
|
||||
/// example = extrude(exampleSketch, length = 10)
|
||||
@ -1069,9 +1003,9 @@ pub async fn start_sketch_on(exec_state: &mut ExecState, args: Args) -> Result<K
|
||||
/// })
|
||||
/// |> startProfileAt([0, 0], %)
|
||||
/// |> line(end = [100.0, 0])
|
||||
/// |> yLine(-100.0, %)
|
||||
/// |> xLine(-100.0, %)
|
||||
/// |> yLine(100.0, %)
|
||||
/// |> yLine(length = -100.0)
|
||||
/// |> xLine(length = -100.0)
|
||||
/// |> yLine(length = 100.0)
|
||||
/// |> close()
|
||||
/// |> extrude(length = 3.14)
|
||||
/// ```
|
||||
|
Reference in New Issue
Block a user