2024-04-19 11:56:21 -04:00
|
|
|
import {
|
|
|
|
createArrayExpression,
|
|
|
|
createBinaryExpression,
|
|
|
|
createCallExpressionStdLib,
|
2024-06-24 22:39:04 -07:00
|
|
|
createIdentifier,
|
2024-04-19 11:56:21 -04:00
|
|
|
createLiteral,
|
|
|
|
createPipeSubstitution,
|
2024-06-24 22:39:04 -07:00
|
|
|
createTagDeclarator,
|
2024-04-19 11:56:21 -04:00
|
|
|
createUnaryExpression,
|
|
|
|
} from 'lang/modifyAst'
|
|
|
|
import { ArrayExpression, CallExpression, PipeExpression } from 'lang/wasm'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns AST expressions for this KCL code:
|
|
|
|
* const yo = startSketchOn('XY')
|
|
|
|
* |> startProfileAt([0, 0], %)
|
2024-07-27 22:56:46 -07:00
|
|
|
* |> angledLine([0, 0], %, $a)
|
|
|
|
* |> angledLine([segAng(a) - 90, 0], %, $b)
|
|
|
|
* |> angledLine([segAng(a), -segLen(a)], %, $c)
|
2024-04-19 11:56:21 -04:00
|
|
|
* |> close(%)
|
|
|
|
*/
|
|
|
|
export const getRectangleCallExpressions = (
|
|
|
|
rectangleOrigin: [number, number],
|
|
|
|
tags: [string, string, string]
|
|
|
|
) => [
|
|
|
|
createCallExpressionStdLib('angledLine', [
|
|
|
|
createArrayExpression([
|
|
|
|
createLiteral(0), // 0 deg
|
|
|
|
createLiteral(0), // This will be the width of the rectangle
|
|
|
|
]),
|
|
|
|
createPipeSubstitution(),
|
2024-06-24 22:39:04 -07:00
|
|
|
createTagDeclarator(tags[0]),
|
2024-04-19 11:56:21 -04:00
|
|
|
]),
|
|
|
|
createCallExpressionStdLib('angledLine', [
|
|
|
|
createArrayExpression([
|
|
|
|
createBinaryExpression([
|
2024-07-27 22:56:46 -07:00
|
|
|
createCallExpressionStdLib('segAng', [createIdentifier(tags[0])]),
|
2024-04-19 11:56:21 -04:00
|
|
|
'+',
|
|
|
|
createLiteral(90),
|
|
|
|
]), // 90 offset from the previous line
|
|
|
|
createLiteral(0), // This will be the height of the rectangle
|
|
|
|
]),
|
|
|
|
createPipeSubstitution(),
|
2024-06-24 22:39:04 -07:00
|
|
|
createTagDeclarator(tags[1]),
|
2024-04-19 11:56:21 -04:00
|
|
|
]),
|
|
|
|
createCallExpressionStdLib('angledLine', [
|
|
|
|
createArrayExpression([
|
2024-07-27 22:56:46 -07:00
|
|
|
createCallExpressionStdLib('segAng', [createIdentifier(tags[0])]), // same angle as the first line
|
2024-04-19 11:56:21 -04:00
|
|
|
createUnaryExpression(
|
2024-07-27 22:56:46 -07:00
|
|
|
createCallExpressionStdLib('segLen', [createIdentifier(tags[0])]),
|
2024-04-19 11:56:21 -04:00
|
|
|
'-'
|
|
|
|
), // negative height
|
|
|
|
]),
|
|
|
|
createPipeSubstitution(),
|
2024-06-24 22:39:04 -07:00
|
|
|
createTagDeclarator(tags[2]),
|
2024-04-19 11:56:21 -04:00
|
|
|
]),
|
2024-05-31 14:02:46 -04:00
|
|
|
createCallExpressionStdLib('lineTo', [
|
|
|
|
createArrayExpression([
|
|
|
|
createCallExpressionStdLib('profileStartX', [createPipeSubstitution()]),
|
|
|
|
createCallExpressionStdLib('profileStartY', [createPipeSubstitution()]),
|
|
|
|
]),
|
|
|
|
createPipeSubstitution(),
|
|
|
|
]), // close the rectangle
|
2024-04-19 11:56:21 -04:00
|
|
|
createCallExpressionStdLib('close', [createPipeSubstitution()]),
|
|
|
|
]
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mutates the pipeExpression to update the rectangle sketch
|
|
|
|
* @param pipeExpression
|
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param tag
|
|
|
|
*/
|
|
|
|
export function updateRectangleSketch(
|
|
|
|
pipeExpression: PipeExpression,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
tag: string
|
|
|
|
) {
|
|
|
|
;((pipeExpression.body[2] as CallExpression)
|
|
|
|
.arguments[0] as ArrayExpression) = createArrayExpression([
|
|
|
|
createLiteral(x >= 0 ? 0 : 180),
|
|
|
|
createLiteral(Math.abs(x)),
|
|
|
|
])
|
|
|
|
;((pipeExpression.body[3] as CallExpression)
|
|
|
|
.arguments[0] as ArrayExpression) = createArrayExpression([
|
|
|
|
createBinaryExpression([
|
2024-07-27 22:56:46 -07:00
|
|
|
createCallExpressionStdLib('segAng', [createIdentifier(tag)]),
|
2024-04-19 11:56:21 -04:00
|
|
|
Math.sign(y) === Math.sign(x) ? '+' : '-',
|
|
|
|
createLiteral(90),
|
|
|
|
]), // 90 offset from the previous line
|
|
|
|
createLiteral(Math.abs(y)), // This will be the height of the rectangle
|
|
|
|
])
|
|
|
|
}
|