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:
@ -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