Kwargs: profileStart/x/y (#6439)

This commit is contained in:
Adam Chalmers
2025-04-23 10:21:58 -05:00
committed by GitHub
parent 90f6c1bb04
commit a46186573c
9 changed files with 71 additions and 73 deletions

View File

@ -9,7 +9,7 @@ Extract the provided 2-dimensional sketch's profile's origin value.
```js ```js
profileStart(sketch: Sketch): [number] profileStart(profile: Sketch): [number]
``` ```
@ -17,7 +17,7 @@ profileStart(sketch: Sketch): [number]
| Name | Type | Description | Required | | Name | Type | Description | Required |
|----------|------|-------------|----------| |----------|------|-------------|----------|
| `sketch` | [`Sketch`](/docs/kcl/types/Sketch) | | Yes | | `profile` | [`Sketch`](/docs/kcl/types/Sketch) | Profile whose start is being used | Yes |
### Returns ### Returns

View File

@ -9,7 +9,7 @@ Extract the provided 2-dimensional sketch's profile's origin's 'x' value.
```js ```js
profileStartX(sketch: Sketch): number profileStartX(profile: Sketch): number
``` ```
@ -17,7 +17,7 @@ profileStartX(sketch: Sketch): number
| Name | Type | Description | Required | | Name | Type | Description | Required |
|----------|------|-------------|----------| |----------|------|-------------|----------|
| `sketch` | [`Sketch`](/docs/kcl/types/Sketch) | | Yes | | `profile` | [`Sketch`](/docs/kcl/types/Sketch) | Profile whose start is being used | Yes |
### Returns ### Returns

View File

@ -9,7 +9,7 @@ Extract the provided 2-dimensional sketch's profile's origin's 'y' value.
```js ```js
profileStartY(sketch: Sketch): number profileStartY(profile: Sketch): number
``` ```
@ -17,7 +17,7 @@ profileStartY(sketch: Sketch): number
| Name | Type | Description | Required | | Name | Type | Description | Required |
|----------|------|-------------|----------| |----------|------|-------------|----------|
| `sketch` | [`Sketch`](/docs/kcl/types/Sketch) | | Yes | | `profile` | [`Sketch`](/docs/kcl/types/Sketch) | Profile whose start is being used | Yes |
### Returns ### Returns

View File

@ -237227,10 +237227,10 @@
"summary": "Extract the provided 2-dimensional sketch's profile's origin value.", "summary": "Extract the provided 2-dimensional sketch's profile's origin value.",
"description": "", "description": "",
"tags": [], "tags": [],
"keywordArguments": false, "keywordArguments": true,
"args": [ "args": [
{ {
"name": "sketch", "name": "profile",
"type": "Sketch", "type": "Sketch",
"schema": { "schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema", "$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
@ -238899,7 +238899,8 @@
}, },
"required": true, "required": true,
"includeInSnippet": true, "includeInSnippet": true,
"labelRequired": true "description": "Profile whose start is being used",
"labelRequired": false
} }
], ],
"returnValue": { "returnValue": {
@ -238931,10 +238932,10 @@
"summary": "Extract the provided 2-dimensional sketch's profile's origin's 'x' value.", "summary": "Extract the provided 2-dimensional sketch's profile's origin's 'x' value.",
"description": "", "description": "",
"tags": [], "tags": [],
"keywordArguments": false, "keywordArguments": true,
"args": [ "args": [
{ {
"name": "sketch", "name": "profile",
"type": "Sketch", "type": "Sketch",
"schema": { "schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema", "$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
@ -240603,7 +240604,8 @@
}, },
"required": true, "required": true,
"includeInSnippet": true, "includeInSnippet": true,
"labelRequired": true "description": "Profile whose start is being used",
"labelRequired": false
} }
], ],
"returnValue": { "returnValue": {
@ -240630,10 +240632,10 @@
"summary": "Extract the provided 2-dimensional sketch's profile's origin's 'y' value.", "summary": "Extract the provided 2-dimensional sketch's profile's origin's 'y' value.",
"description": "", "description": "",
"tags": [], "tags": [],
"keywordArguments": false, "keywordArguments": true,
"args": [ "args": [
{ {
"name": "sketch", "name": "profile",
"type": "Sketch", "type": "Sketch",
"schema": { "schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema", "$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
@ -242302,7 +242304,8 @@
}, },
"required": true, "required": true,
"includeInSnippet": true, "includeInSnippet": true,
"labelRequired": true "description": "Profile whose start is being used",
"labelRequired": false
} }
], ],
"returnValue": { "returnValue": {

View File

@ -1820,18 +1820,6 @@ const bracket = startSketchOn(XY)
parse_execute(ast).await.unwrap(); parse_execute(ast).await.unwrap();
} }
#[tokio::test(flavor = "multi_thread")]
async fn test_bad_arg_count_std() {
let ast = "startSketchOn(XY)
|> startProfileAt([0, 0], %)
|> profileStartX()";
assert!(parse_execute(ast)
.await
.unwrap_err()
.message()
.contains("Expected a sketch argument"));
}
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_unary_operator_not_succeeds() { async fn test_unary_operator_not_succeeds() {
let ast = r#" let ast = r#"

View File

@ -675,28 +675,6 @@ impl Args {
Ok((sketches, sketch)) Ok((sketches, sketch))
} }
pub(crate) fn get_sketch(&self, exec_state: &mut ExecState) -> Result<Sketch, KclError> {
let Some(arg0) = self.args.first() else {
return Err(KclError::Semantic(KclErrorDetails {
message: "Expected a sketch argument".to_owned(),
source_ranges: vec![self.source_range],
}));
};
let sarg = arg0
.value
.coerce(&RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)
.map_err(|_| {
KclError::Type(KclErrorDetails {
message: format!("Expected a sketch, found {}", arg0.value.human_friendly_type()),
source_ranges: vec![self.source_range],
})
})?;
match sarg {
KclValue::Sketch { value } => Ok(*value),
_ => unreachable!(),
}
}
pub(crate) fn get_data<'a, T>(&'a self) -> Result<T, KclError> pub(crate) fn get_data<'a, T>(&'a self) -> Result<T, KclError>
where where
T: FromArgs<'a>, T: FromArgs<'a>,

View File

@ -1454,7 +1454,7 @@ pub(crate) async fn inner_start_profile_at(
/// Returns the X component of the sketch profile start point. /// Returns the X component of the sketch profile start point.
pub async fn profile_start_x(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> { pub async fn profile_start_x(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let sketch: Sketch = args.get_sketch(exec_state)?; let sketch: Sketch = args.get_unlabeled_kw_arg_typed("sketch", &RuntimeType::sketch(), exec_state)?;
let ty = sketch.units.into(); let ty = sketch.units.into();
let x = inner_profile_start_x(sketch)?; let x = inner_profile_start_x(sketch)?;
Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty))) Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty)))
@ -1471,15 +1471,20 @@ pub async fn profile_start_x(exec_state: &mut ExecState, args: Args) -> Result<K
/// |> angledLine(angle = 30, endAbsoluteX = profileStartX(%)) /// |> angledLine(angle = 30, endAbsoluteX = profileStartX(%))
/// ``` /// ```
#[stdlib { #[stdlib {
name = "profileStartX" name = "profileStartX",
keywords = true,
unlabeled_first = true,
args = {
profile = {docs = "Profile whose start is being used"},
}
}] }]
pub(crate) fn inner_profile_start_x(sketch: Sketch) -> Result<f64, KclError> { pub(crate) fn inner_profile_start_x(profile: Sketch) -> Result<f64, KclError> {
Ok(sketch.start.to[0]) Ok(profile.start.to[0])
} }
/// Returns the Y component of the sketch profile start point. /// Returns the Y component of the sketch profile start point.
pub async fn profile_start_y(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> { pub async fn profile_start_y(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let sketch: Sketch = args.get_sketch(exec_state)?; let sketch: Sketch = args.get_unlabeled_kw_arg_typed("sketch", &RuntimeType::sketch(), exec_state)?;
let ty = sketch.units.into(); let ty = sketch.units.into();
let x = inner_profile_start_y(sketch)?; let x = inner_profile_start_y(sketch)?;
Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty))) Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty)))
@ -1495,15 +1500,20 @@ pub async fn profile_start_y(exec_state: &mut ExecState, args: Args) -> Result<K
/// |> angledLine(angle = 30, endAbsoluteY = profileStartY(%)) /// |> angledLine(angle = 30, endAbsoluteY = profileStartY(%))
/// ``` /// ```
#[stdlib { #[stdlib {
name = "profileStartY" name = "profileStartY",
keywords = true,
unlabeled_first = true,
args = {
profile = {docs = "Profile whose start is being used"},
}
}] }]
pub(crate) fn inner_profile_start_y(sketch: Sketch) -> Result<f64, KclError> { pub(crate) fn inner_profile_start_y(profile: Sketch) -> Result<f64, KclError> {
Ok(sketch.start.to[1]) Ok(profile.start.to[1])
} }
/// Returns the sketch profile start point. /// Returns the sketch profile start point.
pub async fn profile_start(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> { pub async fn profile_start(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let sketch: Sketch = args.get_sketch(exec_state)?; let sketch: Sketch = args.get_unlabeled_kw_arg_typed("sketch", &RuntimeType::sketch(), exec_state)?;
let ty = sketch.units.into(); let ty = sketch.units.into();
let point = inner_profile_start(sketch)?; let point = inner_profile_start(sketch)?;
Ok(KclValue::from_point2d(point, ty, args.into())) Ok(KclValue::from_point2d(point, ty, args.into()))
@ -1522,10 +1532,15 @@ pub async fn profile_start(exec_state: &mut ExecState, args: Args) -> Result<Kcl
/// |> extrude(length = 20) /// |> extrude(length = 20)
/// ``` /// ```
#[stdlib { #[stdlib {
name = "profileStart" name = "profileStart",
keywords = true,
unlabeled_first = true,
args = {
profile = {docs = "Profile whose start is being used"},
}
}] }]
pub(crate) fn inner_profile_start(sketch: Sketch) -> Result<[f64; 2], KclError> { pub(crate) fn inner_profile_start(profile: Sketch) -> Result<[f64; 2], KclError> {
Ok(sketch.start.to) Ok(profile.start.to)
} }
/// Close the current sketch. /// Close the current sketch.

View File

@ -1013,13 +1013,18 @@ export class SceneEntities {
// Snapping logic for the profile start handle // Snapping logic for the profile start handle
if (intersectsProfileStart) { if (intersectsProfileStart) {
const originCoords = createArrayExpression([ const originCoords = createArrayExpression([
createCallExpressionStdLib('profileStartX', [ createCallExpressionStdLibKw(
'profileStartX',
createPipeSubstitution(), createPipeSubstitution(),
]), []
createCallExpressionStdLib('profileStartY', [ ),
createCallExpressionStdLibKw(
'profileStartY',
createPipeSubstitution(), createPipeSubstitution(),
]), []
),
]) ])
modifiedAst = addCallExpressionsToPipe({ modifiedAst = addCallExpressionsToPipe({
node: this.kclManager.ast, node: this.kclManager.ast,
variables: this.kclManager.variables, variables: this.kclManager.variables,
@ -2213,13 +2218,18 @@ export class SceneEntities {
modded = moddedResult.modifiedAst modded = moddedResult.modifiedAst
if (intersectsProfileStart) { if (intersectsProfileStart) {
const originCoords = createArrayExpression([ const originCoords = createArrayExpression([
createCallExpressionStdLib('profileStartX', [ createCallExpressionStdLibKw(
'profileStartX',
createPipeSubstitution(), createPipeSubstitution(),
]), []
createCallExpressionStdLib('profileStartY', [ ),
createCallExpressionStdLibKw(
'profileStartY',
createPipeSubstitution(), createPipeSubstitution(),
]), []
),
]) ])
const arcToCallExp = getNodeFromPath<CallExpression>( const arcToCallExp = getNodeFromPath<CallExpression>(
modded, modded,
mod.pathToNode, mod.pathToNode,

View File

@ -81,12 +81,16 @@ export const getRectangleCallExpressions = (
createLabeledArg( createLabeledArg(
ARG_END_ABSOLUTE, ARG_END_ABSOLUTE,
createArrayExpression([ createArrayExpression([
createCallExpressionStdLib('profileStartX', [ createCallExpressionStdLibKw(
'profileStartX',
createPipeSubstitution(), createPipeSubstitution(),
]), []
createCallExpressionStdLib('profileStartY', [ ),
createCallExpressionStdLibKw(
'profileStartY',
createPipeSubstitution(), createPipeSubstitution(),
]), []
),
]) ])
), ),
]), // close the rectangle ]), // close the rectangle