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
profileStart(sketch: Sketch): [number]
profileStart(profile: Sketch): [number]
```
@ -17,7 +17,7 @@ profileStart(sketch: Sketch): [number]
| 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

View File

@ -9,7 +9,7 @@ Extract the provided 2-dimensional sketch's profile's origin's 'x' value.
```js
profileStartX(sketch: Sketch): number
profileStartX(profile: Sketch): number
```
@ -17,7 +17,7 @@ profileStartX(sketch: Sketch): number
| 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

View File

@ -9,7 +9,7 @@ Extract the provided 2-dimensional sketch's profile's origin's 'y' value.
```js
profileStartY(sketch: Sketch): number
profileStartY(profile: Sketch): number
```
@ -17,7 +17,7 @@ profileStartY(sketch: Sketch): number
| 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

View File

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

View File

@ -1820,18 +1820,6 @@ const bracket = startSketchOn(XY)
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")]
async fn test_unary_operator_not_succeeds() {
let ast = r#"

View File

@ -675,28 +675,6 @@ impl Args {
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>
where
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.
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 x = inner_profile_start_x(sketch)?;
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(%))
/// ```
#[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> {
Ok(sketch.start.to[0])
pub(crate) fn inner_profile_start_x(profile: Sketch) -> Result<f64, KclError> {
Ok(profile.start.to[0])
}
/// 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> {
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 x = inner_profile_start_y(sketch)?;
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(%))
/// ```
#[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> {
Ok(sketch.start.to[1])
pub(crate) fn inner_profile_start_y(profile: Sketch) -> Result<f64, KclError> {
Ok(profile.start.to[1])
}
/// Returns the sketch profile start point.
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 point = inner_profile_start(sketch)?;
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)
/// ```
#[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> {
Ok(sketch.start.to)
pub(crate) fn inner_profile_start(profile: Sketch) -> Result<[f64; 2], KclError> {
Ok(profile.start.to)
}
/// Close the current sketch.

View File

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

View File

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