Add rectangle tool to sketch mode (#2005)

* Initial draft rectangle appear on screen

* rectangle tool extra

* Fix draft lines in all quadrants

* Wait for first click to set up draft rectangle

* Working rectangle commit

* Update toolbar icon and disabling logic

* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)

* fmt

* Working tool, one remaining bug around naively updating sketch nodes

* Break out rectangle AST utilities

* Remove unused imports

* Disable Rectangle tool if sketch is not empty

* Use existing tools for generating tag names

* Add snapshot test for tool

* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)

* Rerun CI

* Add comments, remove unrelated changes

* fix rectangle bug from bad ast

* Make rectangle tool equippable when the line tool is equipped

* Change snapshot test to check the draft rectangle instead of commited one

* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)

* Rerun CI

* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)

* Rerun CI

---------

Co-authored-by: Kurt Hutten Irev-Dev <k.hutten@protonmail.ch>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Frank Noirot
2024-04-19 11:56:21 -04:00
committed by GitHub
parent 9dfe0c3d80
commit 6450622146
15 changed files with 503 additions and 50 deletions

105
src/lib/rectangleTool.ts Normal file
View File

@ -0,0 +1,105 @@
import {
createArrayExpression,
createBinaryExpression,
createCallExpressionStdLib,
createLiteral,
createPipeSubstitution,
createUnaryExpression,
} from 'lang/modifyAst'
import { roundOff } from './utils'
import { ArrayExpression, CallExpression, PipeExpression } from 'lang/wasm'
/**
* Returns AST expressions for this KCL code:
* const yo = startSketchOn('XY')
* |> startProfileAt([0, 0], %)
* |> angledLine([0, 0], %, 'a')
* |> angledLine([segAng('a', %) - 90, 0], %, 'b')
* |> angledLine([segAng('a', %), -segLen('a', %)], %, 'c')
* |> close(%)
*/
export const getRectangleCallExpressions = (
rectangleOrigin: [number, number],
tags: [string, string, string]
) => [
createCallExpressionStdLib('startProfileAt', [
createArrayExpression([
createLiteral(roundOff(rectangleOrigin[0])),
createLiteral(roundOff(rectangleOrigin[1])),
]),
createPipeSubstitution(),
]),
createCallExpressionStdLib('angledLine', [
createArrayExpression([
createLiteral(0), // 0 deg
createLiteral(0), // This will be the width of the rectangle
]),
createPipeSubstitution(),
createLiteral(tags[0]),
]),
createCallExpressionStdLib('angledLine', [
createArrayExpression([
createBinaryExpression([
createCallExpressionStdLib('segAng', [
createLiteral(tags[0]),
createPipeSubstitution(),
]),
'+',
createLiteral(90),
]), // 90 offset from the previous line
createLiteral(0), // This will be the height of the rectangle
]),
createPipeSubstitution(),
createLiteral(tags[1]),
]),
createCallExpressionStdLib('angledLine', [
createArrayExpression([
createCallExpressionStdLib('segAng', [
createLiteral(tags[0]),
createPipeSubstitution(),
]), // same angle as the first line
createUnaryExpression(
createCallExpressionStdLib('segLen', [
createLiteral(tags[0]),
createPipeSubstitution(),
]),
'-'
), // negative height
]),
createPipeSubstitution(),
createLiteral(tags[2]),
]),
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([
createCallExpressionStdLib('segAng', [
createLiteral(tag),
createPipeSubstitution(),
]),
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
])
}