2023-02-21 10:50:45 +11:00
|
|
|
import {
|
|
|
|
ProgramMemory,
|
|
|
|
Path,
|
|
|
|
SketchGroup,
|
|
|
|
SourceRange,
|
|
|
|
PathToNode,
|
2023-09-12 18:10:27 -07:00
|
|
|
MemoryItem,
|
2023-02-21 10:50:45 +11:00
|
|
|
} from '../executor'
|
2023-02-12 10:56:45 +11:00
|
|
|
import {
|
|
|
|
Program,
|
|
|
|
PipeExpression,
|
|
|
|
CallExpression,
|
|
|
|
VariableDeclarator,
|
|
|
|
Value,
|
|
|
|
Literal,
|
2023-03-19 18:46:39 +11:00
|
|
|
VariableDeclaration,
|
2023-07-13 16:57:22 +10:00
|
|
|
} from '../abstractSyntaxTreeTypes'
|
2023-03-03 20:35:48 +11:00
|
|
|
import {
|
|
|
|
getNodeFromPath,
|
|
|
|
getNodeFromPathCurry,
|
|
|
|
getNodePathFromSourceRange,
|
|
|
|
} from '../queryAst'
|
2023-09-13 07:23:14 -07:00
|
|
|
import { isLiteralArrayOrStatic } from './sketchcombos'
|
2023-02-21 10:50:45 +11:00
|
|
|
import { GuiModes, toolTips, TooTip } from '../../useStore'
|
2023-09-13 08:36:47 +10:00
|
|
|
import { createPipeExpression, splitPathAtPipeExpression } from '../modifyAst'
|
2023-06-22 16:43:33 +10:00
|
|
|
import { generateUuidFromHashSeed } from '../../lib/uuid'
|
2023-02-12 10:56:45 +11:00
|
|
|
|
2023-08-24 17:28:59 -07:00
|
|
|
import { SketchLineHelper, ModifyAstBase, TransformCallback } from './stdTypes'
|
2023-02-12 10:56:45 +11:00
|
|
|
|
|
|
|
import {
|
|
|
|
createLiteral,
|
|
|
|
createCallExpression,
|
|
|
|
createArrayExpression,
|
|
|
|
createPipeSubstitution,
|
|
|
|
createObjectExpression,
|
|
|
|
mutateArrExp,
|
|
|
|
mutateObjExpProp,
|
|
|
|
findUniqueName,
|
|
|
|
} from '../modifyAst'
|
|
|
|
import { roundOff, getLength, getAngle } from '../../lib/utils'
|
2023-03-20 07:09:19 +11:00
|
|
|
import { getSketchSegmentFromSourceRange } from './sketchConstraints'
|
2023-08-24 17:28:59 -07:00
|
|
|
import { perpendicularDistance } from 'sketch-helpers'
|
2023-02-12 10:56:45 +11:00
|
|
|
|
|
|
|
export type Coords2d = [number, number]
|
|
|
|
|
|
|
|
export function getCoordsFromPaths(skGroup: SketchGroup, index = 0): Coords2d {
|
|
|
|
const currentPath = skGroup?.value?.[index]
|
|
|
|
if (!currentPath && skGroup?.start) {
|
2023-03-17 08:27:40 +11:00
|
|
|
return skGroup.start.to
|
2023-02-12 10:56:45 +11:00
|
|
|
} else if (!currentPath) {
|
|
|
|
return [0, 0]
|
|
|
|
}
|
|
|
|
if (currentPath.type === 'toPoint') {
|
|
|
|
return [currentPath.to[0], currentPath.to[1]]
|
|
|
|
}
|
|
|
|
return [0, 0]
|
|
|
|
}
|
|
|
|
|
2023-03-02 21:19:11 +11:00
|
|
|
export function createFirstArg(
|
2023-02-21 10:50:45 +11:00
|
|
|
sketchFn: TooTip,
|
2023-04-03 16:05:25 +10:00
|
|
|
val: Value | [Value, Value] | [Value, Value, Value],
|
2023-02-21 10:50:45 +11:00
|
|
|
tag?: Value
|
|
|
|
): Value {
|
|
|
|
if (!tag) {
|
|
|
|
if (Array.isArray(val)) {
|
|
|
|
return createArrayExpression(val)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
if (Array.isArray(val)) {
|
|
|
|
if (['line', 'lineTo'].includes(sketchFn))
|
|
|
|
return createObjectExpression({ to: createArrayExpression(val), tag })
|
|
|
|
if (
|
|
|
|
['angledLine', 'angledLineOfXLength', 'angledLineOfYLength'].includes(
|
|
|
|
sketchFn
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return createObjectExpression({ angle: val[0], length: val[1], tag })
|
|
|
|
if (['angledLineToX', 'angledLineToY'].includes(sketchFn))
|
|
|
|
return createObjectExpression({ angle: val[0], to: val[1], tag })
|
2023-04-03 16:05:25 +10:00
|
|
|
if (['angledLineThatIntersects'].includes(sketchFn) && val[2])
|
|
|
|
return createObjectExpression({
|
|
|
|
angle: val[0],
|
|
|
|
offset: val[1],
|
|
|
|
intersectTag: val[2],
|
|
|
|
tag,
|
|
|
|
})
|
2023-02-21 10:50:45 +11:00
|
|
|
} else {
|
|
|
|
if (['xLine', 'yLine'].includes(sketchFn))
|
|
|
|
return createObjectExpression({ length: val, tag })
|
|
|
|
if (['xLineTo', 'yLineTo'].includes(sketchFn))
|
|
|
|
return createObjectExpression({ to: val, tag })
|
2023-03-17 08:27:40 +11:00
|
|
|
if (['startSketchAt'].includes(sketchFn))
|
|
|
|
return createObjectExpression({ to: val, tag })
|
2023-02-21 10:50:45 +11:00
|
|
|
}
|
|
|
|
throw new Error('all sketch line types should have been covered')
|
|
|
|
}
|
|
|
|
|
2023-06-22 16:43:33 +10:00
|
|
|
type LineData = {
|
|
|
|
from: [number, number, number]
|
|
|
|
to: [number, number, number]
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeId(seed: string | any) {
|
|
|
|
if (typeof seed === 'string') {
|
|
|
|
return generateUuidFromHashSeed(seed)
|
|
|
|
}
|
|
|
|
return generateUuidFromHashSeed(JSON.stringify(seed))
|
|
|
|
}
|
|
|
|
|
2023-02-12 10:56:45 +11:00
|
|
|
export const lineTo: SketchLineHelper = {
|
2023-03-05 07:34:56 +11:00
|
|
|
add: ({
|
|
|
|
node,
|
|
|
|
pathToNode,
|
|
|
|
to,
|
|
|
|
createCallback,
|
|
|
|
replaceExisting,
|
|
|
|
referencedSegment,
|
|
|
|
}) => {
|
2023-02-12 10:56:45 +11:00
|
|
|
const _node = { ...node }
|
|
|
|
const { node: pipe } = getNodeFromPath<PipeExpression>(
|
|
|
|
_node,
|
|
|
|
pathToNode,
|
|
|
|
'PipeExpression'
|
|
|
|
)
|
2023-03-05 07:34:56 +11:00
|
|
|
|
|
|
|
const newVals: [Value, Value] = [
|
|
|
|
createLiteral(roundOff(to[0], 2)),
|
|
|
|
createLiteral(roundOff(to[1], 2)),
|
|
|
|
]
|
|
|
|
|
2023-03-07 15:45:59 +11:00
|
|
|
const newLine = createCallExpression('lineTo', [
|
|
|
|
createArrayExpression(newVals),
|
|
|
|
createPipeSubstitution(),
|
|
|
|
])
|
2023-04-01 16:47:00 +11:00
|
|
|
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
2023-03-07 15:45:59 +11:00
|
|
|
if (replaceExisting && createCallback) {
|
2023-03-10 08:48:50 +11:00
|
|
|
const { callExp, valueUsedInTransform } = createCallback(
|
|
|
|
newVals,
|
|
|
|
referencedSegment
|
|
|
|
)
|
|
|
|
pipe.body[callIndex] = callExp
|
2023-03-07 15:45:59 +11:00
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
2023-03-10 08:48:50 +11:00
|
|
|
valueUsedInTransform: valueUsedInTransform,
|
2023-03-07 15:45:59 +11:00
|
|
|
}
|
2023-03-05 07:34:56 +11:00
|
|
|
} else {
|
|
|
|
pipe.body = [...pipe.body, newLine]
|
|
|
|
}
|
2023-02-12 10:56:45 +11:00
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateArgs: ({ node, pathToNode, to }) => {
|
|
|
|
const _node = { ...node }
|
|
|
|
const { node: callExpression } = getNodeFromPath<CallExpression>(
|
|
|
|
_node,
|
|
|
|
pathToNode
|
|
|
|
)
|
|
|
|
|
|
|
|
const toArrExp = createArrayExpression([
|
|
|
|
createLiteral(to[0]),
|
|
|
|
createLiteral(to[1]),
|
|
|
|
])
|
|
|
|
|
|
|
|
mutateArrExp(callExpression.arguments?.[0], toArrExp) ||
|
|
|
|
mutateObjExpProp(callExpression.arguments?.[0], toArrExp, 'to')
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTag: addTagWithTo('default'),
|
|
|
|
}
|
|
|
|
|
|
|
|
export const line: SketchLineHelper = {
|
|
|
|
add: ({
|
|
|
|
node,
|
|
|
|
previousProgramMemory,
|
|
|
|
pathToNode,
|
|
|
|
to,
|
2023-03-02 21:19:11 +11:00
|
|
|
from,
|
|
|
|
replaceExisting,
|
2023-03-17 21:15:46 +11:00
|
|
|
referencedSegment,
|
2023-03-02 21:19:11 +11:00
|
|
|
createCallback,
|
2023-02-12 10:56:45 +11:00
|
|
|
}) => {
|
|
|
|
const _node = { ...node }
|
2023-09-13 08:36:47 +10:00
|
|
|
const { node: pipe } = getNodeFromPath<PipeExpression | CallExpression>(
|
2023-02-12 10:56:45 +11:00
|
|
|
_node,
|
|
|
|
pathToNode,
|
|
|
|
'PipeExpression'
|
|
|
|
)
|
|
|
|
const { node: varDec } = getNodeFromPath<VariableDeclarator>(
|
|
|
|
_node,
|
|
|
|
pathToNode,
|
|
|
|
'VariableDeclarator'
|
|
|
|
)
|
|
|
|
const variableName = varDec.id.name
|
|
|
|
const sketch = previousProgramMemory?.root?.[variableName]
|
2023-09-12 18:10:27 -07:00
|
|
|
if (sketch.type !== 'SketchGroup') throw new Error('not a SketchGroup')
|
2023-03-02 21:19:11 +11:00
|
|
|
|
|
|
|
const newXVal = createLiteral(roundOff(to[0] - from[0], 2))
|
|
|
|
const newYVal = createLiteral(roundOff(to[1] - from[1], 2))
|
|
|
|
|
2023-09-13 08:36:47 +10:00
|
|
|
if (replaceExisting && createCallback && pipe.type !== 'CallExpression') {
|
2023-04-01 16:47:00 +11:00
|
|
|
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
2023-03-17 21:15:46 +11:00
|
|
|
const { callExp, valueUsedInTransform } = createCallback(
|
|
|
|
[newXVal, newYVal],
|
|
|
|
referencedSegment
|
|
|
|
)
|
|
|
|
pipe.body[callIndex] = callExp
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
valueUsedInTransform,
|
|
|
|
}
|
2023-03-02 21:19:11 +11:00
|
|
|
}
|
|
|
|
|
2023-03-17 21:15:46 +11:00
|
|
|
const callExp = createCallExpression('line', [
|
|
|
|
createArrayExpression([newXVal, newYVal]),
|
|
|
|
createPipeSubstitution(),
|
|
|
|
])
|
2023-09-13 08:36:47 +10:00
|
|
|
if (pipe.type === 'PipeExpression') {
|
|
|
|
pipe.body = [...pipe.body, callExp]
|
|
|
|
} else {
|
|
|
|
varDec.init = createPipeExpression([varDec.init, callExp])
|
|
|
|
}
|
2023-02-12 10:56:45 +11:00
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateArgs: ({ node, pathToNode, to, from }) => {
|
|
|
|
const _node = { ...node }
|
2023-03-19 18:46:39 +11:00
|
|
|
const { node: callExpression } = getNodeFromPath<CallExpression>(
|
2023-02-12 10:56:45 +11:00
|
|
|
_node,
|
|
|
|
pathToNode
|
|
|
|
)
|
|
|
|
|
|
|
|
const toArrExp = createArrayExpression([
|
|
|
|
createLiteral(roundOff(to[0] - from[0], 2)),
|
|
|
|
createLiteral(roundOff(to[1] - from[1], 2)),
|
|
|
|
])
|
|
|
|
|
2023-09-13 08:36:47 +10:00
|
|
|
if (callExpression.arguments?.[0].type === 'ObjectExpression') {
|
2023-03-17 15:53:20 +11:00
|
|
|
const toProp = callExpression.arguments?.[0].properties?.find(
|
|
|
|
({ key }) => key.name === 'to'
|
|
|
|
)
|
2023-03-19 18:46:39 +11:00
|
|
|
mutateObjExpProp(callExpression.arguments?.[0], toArrExp, 'to')
|
2023-03-17 15:53:20 +11:00
|
|
|
} else {
|
2023-03-19 18:46:39 +11:00
|
|
|
mutateArrExp(callExpression.arguments?.[0], toArrExp)
|
2023-03-17 15:53:20 +11:00
|
|
|
}
|
2023-02-12 10:56:45 +11:00
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTag: addTagWithTo('default'),
|
|
|
|
}
|
|
|
|
|
|
|
|
export const xLineTo: SketchLineHelper = {
|
2023-02-21 10:50:45 +11:00
|
|
|
add: ({ node, pathToNode, to, replaceExisting, createCallback }) => {
|
2023-02-12 10:56:45 +11:00
|
|
|
const _node = { ...node }
|
2023-02-21 10:50:45 +11:00
|
|
|
const getNode = getNodeFromPathCurry(_node, pathToNode)
|
|
|
|
const { node: pipe } = getNode<PipeExpression>('PipeExpression')
|
|
|
|
|
|
|
|
const newVal = createLiteral(roundOff(to[0], 2))
|
|
|
|
|
2023-03-17 21:15:46 +11:00
|
|
|
if (replaceExisting && createCallback) {
|
2023-04-01 16:47:00 +11:00
|
|
|
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
2023-03-17 21:15:46 +11:00
|
|
|
const { callExp, valueUsedInTransform } = createCallback([newVal, newVal])
|
|
|
|
pipe.body[callIndex] = callExp
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
valueUsedInTransform,
|
|
|
|
}
|
2023-02-21 10:50:45 +11:00
|
|
|
}
|
2023-03-17 21:15:46 +11:00
|
|
|
const callExp = createCallExpression('xLineTo', [
|
|
|
|
newVal,
|
|
|
|
createPipeSubstitution(),
|
|
|
|
])
|
|
|
|
pipe.body = [...pipe.body, callExp]
|
2023-02-12 10:56:45 +11:00
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
2023-03-19 18:46:39 +11:00
|
|
|
updateArgs: ({ node, pathToNode, to }) => {
|
2023-02-12 10:56:45 +11:00
|
|
|
const _node = { ...node }
|
|
|
|
const { node: callExpression } = getNodeFromPath<CallExpression>(
|
|
|
|
_node,
|
|
|
|
pathToNode
|
|
|
|
)
|
|
|
|
const newX = createLiteral(roundOff(to[0], 2))
|
2023-09-13 07:23:14 -07:00
|
|
|
if (isLiteralArrayOrStatic(callExpression.arguments?.[0])) {
|
2023-02-12 10:56:45 +11:00
|
|
|
callExpression.arguments[0] = newX
|
|
|
|
} else {
|
|
|
|
mutateObjExpProp(callExpression.arguments?.[0], newX, 'to')
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTag: addTagWithTo('default'),
|
|
|
|
}
|
|
|
|
|
|
|
|
export const yLineTo: SketchLineHelper = {
|
2023-02-21 10:50:45 +11:00
|
|
|
add: ({ node, pathToNode, to, replaceExisting, createCallback }) => {
|
2023-02-12 10:56:45 +11:00
|
|
|
const _node = { ...node }
|
2023-02-21 10:50:45 +11:00
|
|
|
const getNode = getNodeFromPathCurry(_node, pathToNode)
|
|
|
|
const { node: pipe } = getNode<PipeExpression>('PipeExpression')
|
|
|
|
|
|
|
|
const newVal = createLiteral(roundOff(to[1], 2))
|
2023-03-17 21:15:46 +11:00
|
|
|
|
|
|
|
if (replaceExisting && createCallback) {
|
2023-04-01 16:47:00 +11:00
|
|
|
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
2023-03-17 21:15:46 +11:00
|
|
|
const { callExp, valueUsedInTransform } = createCallback([newVal, newVal])
|
|
|
|
pipe.body[callIndex] = callExp
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
valueUsedInTransform,
|
|
|
|
}
|
2023-02-21 10:50:45 +11:00
|
|
|
}
|
2023-03-17 21:15:46 +11:00
|
|
|
const callExp = createCallExpression('yLineTo', [
|
|
|
|
newVal,
|
|
|
|
createPipeSubstitution(),
|
|
|
|
])
|
|
|
|
pipe.body = [...pipe.body, callExp]
|
2023-02-12 10:56:45 +11:00
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateArgs: ({ node, pathToNode, to, from }) => {
|
|
|
|
const _node = { ...node }
|
|
|
|
const { node: callExpression } = getNodeFromPath<CallExpression>(
|
|
|
|
_node,
|
|
|
|
pathToNode
|
|
|
|
)
|
|
|
|
const newY = createLiteral(roundOff(to[1], 2))
|
2023-09-13 07:23:14 -07:00
|
|
|
if (isLiteralArrayOrStatic(callExpression.arguments?.[0])) {
|
2023-02-12 10:56:45 +11:00
|
|
|
callExpression.arguments[0] = newY
|
|
|
|
} else {
|
|
|
|
mutateObjExpProp(callExpression.arguments?.[0], newY, 'to')
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTag: addTagWithTo('default'),
|
|
|
|
}
|
|
|
|
|
|
|
|
export const xLine: SketchLineHelper = {
|
2023-02-21 10:50:45 +11:00
|
|
|
add: ({ node, pathToNode, to, from, replaceExisting, createCallback }) => {
|
2023-02-12 10:56:45 +11:00
|
|
|
const _node = { ...node }
|
2023-02-21 10:50:45 +11:00
|
|
|
const getNode = getNodeFromPathCurry(_node, pathToNode)
|
|
|
|
const { node: pipe } = getNode<PipeExpression>('PipeExpression')
|
|
|
|
|
|
|
|
const newVal = createLiteral(roundOff(to[0] - from[0], 2))
|
|
|
|
const firstArg = newVal
|
2023-03-10 14:55:16 +11:00
|
|
|
|
|
|
|
if (replaceExisting && createCallback) {
|
2023-04-01 16:47:00 +11:00
|
|
|
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
2023-03-10 14:55:16 +11:00
|
|
|
const { callExp, valueUsedInTransform } = createCallback([
|
|
|
|
firstArg,
|
|
|
|
firstArg,
|
|
|
|
])
|
|
|
|
pipe.body[callIndex] = callExp
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
valueUsedInTransform,
|
|
|
|
}
|
2023-02-12 10:56:45 +11:00
|
|
|
}
|
2023-03-10 14:55:16 +11:00
|
|
|
|
|
|
|
const newLine = createCallExpression('xLine', [
|
|
|
|
firstArg,
|
|
|
|
createPipeSubstitution(),
|
|
|
|
])
|
|
|
|
pipe.body = [...pipe.body, newLine]
|
2023-02-21 10:50:45 +11:00
|
|
|
return { modifiedAst: _node, pathToNode }
|
2023-02-12 10:56:45 +11:00
|
|
|
},
|
|
|
|
updateArgs: ({ node, pathToNode, to, from }) => {
|
|
|
|
const _node = { ...node }
|
|
|
|
const { node: callExpression } = getNodeFromPath<CallExpression>(
|
|
|
|
_node,
|
|
|
|
pathToNode
|
|
|
|
)
|
|
|
|
const newX = createLiteral(roundOff(to[0] - from[0], 2))
|
2023-09-13 07:23:14 -07:00
|
|
|
if (isLiteralArrayOrStatic(callExpression.arguments?.[0])) {
|
2023-02-12 10:56:45 +11:00
|
|
|
callExpression.arguments[0] = newX
|
|
|
|
} else {
|
|
|
|
mutateObjExpProp(callExpression.arguments?.[0], newX, 'length')
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTag: addTagWithTo('length'),
|
|
|
|
}
|
|
|
|
|
|
|
|
export const yLine: SketchLineHelper = {
|
2023-02-21 10:50:45 +11:00
|
|
|
add: ({ node, pathToNode, to, from, replaceExisting, createCallback }) => {
|
2023-02-12 10:56:45 +11:00
|
|
|
const _node = { ...node }
|
2023-02-21 10:50:45 +11:00
|
|
|
const getNode = getNodeFromPathCurry(_node, pathToNode)
|
|
|
|
const { node: pipe } = getNode<PipeExpression>('PipeExpression')
|
|
|
|
const newVal = createLiteral(roundOff(to[1] - from[1], 2))
|
2023-03-10 14:55:16 +11:00
|
|
|
if (replaceExisting && createCallback) {
|
2023-04-01 16:47:00 +11:00
|
|
|
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
2023-03-10 14:55:16 +11:00
|
|
|
const { callExp, valueUsedInTransform } = createCallback([newVal, newVal])
|
|
|
|
pipe.body[callIndex] = callExp
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
valueUsedInTransform,
|
|
|
|
}
|
2023-02-12 10:56:45 +11:00
|
|
|
}
|
2023-03-10 14:55:16 +11:00
|
|
|
|
|
|
|
const newLine = createCallExpression('yLine', [
|
|
|
|
newVal,
|
|
|
|
createPipeSubstitution(),
|
|
|
|
])
|
|
|
|
pipe.body = [...pipe.body, newLine]
|
2023-02-21 10:50:45 +11:00
|
|
|
return { modifiedAst: _node, pathToNode }
|
2023-02-12 10:56:45 +11:00
|
|
|
},
|
|
|
|
updateArgs: ({ node, pathToNode, to, from }) => {
|
|
|
|
const _node = { ...node }
|
|
|
|
const { node: callExpression } = getNodeFromPath<CallExpression>(
|
|
|
|
_node,
|
|
|
|
pathToNode
|
|
|
|
)
|
|
|
|
const newY = createLiteral(roundOff(to[1] - from[1], 2))
|
2023-09-13 07:23:14 -07:00
|
|
|
if (isLiteralArrayOrStatic(callExpression.arguments?.[0])) {
|
2023-02-12 10:56:45 +11:00
|
|
|
callExpression.arguments[0] = newY
|
|
|
|
} else {
|
|
|
|
mutateObjExpProp(callExpression.arguments?.[0], newY, 'length')
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTag: addTagWithTo('length'),
|
|
|
|
}
|
|
|
|
|
|
|
|
export const angledLine: SketchLineHelper = {
|
2023-03-17 21:15:46 +11:00
|
|
|
add: ({
|
|
|
|
node,
|
|
|
|
pathToNode,
|
|
|
|
to,
|
|
|
|
from,
|
|
|
|
createCallback,
|
|
|
|
replaceExisting,
|
|
|
|
referencedSegment,
|
|
|
|
}) => {
|
2023-02-12 10:56:45 +11:00
|
|
|
const _node = { ...node }
|
2023-03-02 21:19:11 +11:00
|
|
|
const getNode = getNodeFromPathCurry(_node, pathToNode)
|
|
|
|
const { node: pipe } = getNode<PipeExpression>('PipeExpression')
|
|
|
|
|
|
|
|
const newAngleVal = createLiteral(roundOff(getAngle(from, to), 0))
|
|
|
|
const newLengthVal = createLiteral(roundOff(getLength(from, to), 2))
|
2023-03-10 08:48:50 +11:00
|
|
|
const newLine = createCallExpression('angledLine', [
|
|
|
|
createArrayExpression([newAngleVal, newLengthVal]),
|
|
|
|
createPipeSubstitution(),
|
|
|
|
])
|
2023-03-02 21:19:11 +11:00
|
|
|
|
2023-03-10 08:48:50 +11:00
|
|
|
if (replaceExisting && createCallback) {
|
2023-04-01 16:47:00 +11:00
|
|
|
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
2023-03-17 21:15:46 +11:00
|
|
|
const { callExp, valueUsedInTransform } = createCallback(
|
|
|
|
[newAngleVal, newLengthVal],
|
|
|
|
referencedSegment
|
|
|
|
)
|
2023-03-10 08:48:50 +11:00
|
|
|
pipe.body[callIndex] = callExp
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
valueUsedInTransform,
|
|
|
|
}
|
2023-03-02 21:19:11 +11:00
|
|
|
} else {
|
|
|
|
pipe.body = [...pipe.body, newLine]
|
|
|
|
}
|
2023-02-12 10:56:45 +11:00
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateArgs: ({ node, pathToNode, to, from }) => {
|
|
|
|
const _node = { ...node }
|
2023-02-21 10:50:45 +11:00
|
|
|
const { node: callExpression } = getNodeFromPath<CallExpression>(
|
2023-02-12 10:56:45 +11:00
|
|
|
_node,
|
|
|
|
pathToNode
|
|
|
|
)
|
|
|
|
const angle = roundOff(getAngle(from, to), 0)
|
|
|
|
const lineLength = roundOff(getLength(from, to), 2)
|
|
|
|
|
|
|
|
const angleLit = createLiteral(angle)
|
|
|
|
const lengthLit = createLiteral(lineLength)
|
|
|
|
|
|
|
|
const firstArg = callExpression.arguments?.[0]
|
|
|
|
if (!mutateArrExp(firstArg, createArrayExpression([angleLit, lengthLit]))) {
|
|
|
|
mutateObjExpProp(firstArg, angleLit, 'angle')
|
|
|
|
mutateObjExpProp(firstArg, lengthLit, 'length')
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTag: addTagWithTo('angleLength'),
|
|
|
|
}
|
|
|
|
|
|
|
|
export const angledLineOfXLength: SketchLineHelper = {
|
|
|
|
add: ({
|
|
|
|
node,
|
|
|
|
previousProgramMemory,
|
|
|
|
pathToNode,
|
|
|
|
to,
|
2023-03-02 21:19:11 +11:00
|
|
|
from,
|
|
|
|
createCallback,
|
|
|
|
replaceExisting,
|
2023-02-12 10:56:45 +11:00
|
|
|
}) => {
|
|
|
|
const _node = { ...node }
|
|
|
|
const { node: pipe } = getNodeFromPath<PipeExpression>(
|
|
|
|
_node,
|
|
|
|
pathToNode,
|
|
|
|
'PipeExpression'
|
|
|
|
)
|
|
|
|
const { node: varDec } = getNodeFromPath<VariableDeclarator>(
|
|
|
|
_node,
|
|
|
|
pathToNode,
|
|
|
|
'VariableDeclarator'
|
|
|
|
)
|
|
|
|
const variableName = varDec.id.name
|
|
|
|
const sketch = previousProgramMemory?.root?.[variableName]
|
2023-09-12 18:10:27 -07:00
|
|
|
if (sketch.type !== 'SketchGroup') throw new Error('not a SketchGroup')
|
2023-03-02 21:19:11 +11:00
|
|
|
const angle = createLiteral(roundOff(getAngle(from, to), 0))
|
|
|
|
const xLength = createLiteral(roundOff(Math.abs(from[0] - to[0]), 2) || 0.1)
|
|
|
|
const newLine = createCallback
|
2023-03-07 15:45:59 +11:00
|
|
|
? createCallback([angle, xLength]).callExp
|
2023-03-02 21:19:11 +11:00
|
|
|
: createCallExpression('angledLineOfXLength', [
|
|
|
|
createArrayExpression([angle, xLength]),
|
|
|
|
createPipeSubstitution(),
|
|
|
|
])
|
2023-04-01 16:47:00 +11:00
|
|
|
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
2023-03-02 21:19:11 +11:00
|
|
|
if (replaceExisting) {
|
|
|
|
pipe.body[callIndex] = newLine
|
|
|
|
} else {
|
|
|
|
pipe.body = [...pipe.body, newLine]
|
|
|
|
}
|
2023-02-12 10:56:45 +11:00
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateArgs: ({ node, pathToNode, to, from }) => {
|
|
|
|
const _node = { ...node }
|
2023-04-01 16:47:00 +11:00
|
|
|
const { node: callExpression } = getNodeFromPath<CallExpression>(
|
2023-02-12 10:56:45 +11:00
|
|
|
_node,
|
|
|
|
pathToNode
|
|
|
|
)
|
|
|
|
const angle = roundOff(getAngle(from, to), 0)
|
|
|
|
const xLength = roundOff(Math.abs(to[0] - from[0]), 2)
|
|
|
|
|
|
|
|
const firstArg = callExpression.arguments?.[0]
|
|
|
|
const adjustedXLength = isAngleLiteral(firstArg)
|
|
|
|
? Math.abs(xLength)
|
|
|
|
: xLength // todo make work for variable angle > 180
|
|
|
|
|
|
|
|
const angleLit = createLiteral(angle)
|
|
|
|
const lengthLit = createLiteral(adjustedXLength)
|
|
|
|
|
|
|
|
if (!mutateArrExp(firstArg, createArrayExpression([angleLit, lengthLit]))) {
|
|
|
|
mutateObjExpProp(firstArg, angleLit, 'angle')
|
|
|
|
mutateObjExpProp(firstArg, lengthLit, 'length')
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTag: addTagWithTo('angleLength'),
|
|
|
|
}
|
|
|
|
|
|
|
|
export const angledLineOfYLength: SketchLineHelper = {
|
|
|
|
add: ({
|
|
|
|
node,
|
|
|
|
previousProgramMemory,
|
|
|
|
pathToNode,
|
|
|
|
to,
|
2023-03-02 21:19:11 +11:00
|
|
|
from,
|
|
|
|
createCallback,
|
|
|
|
replaceExisting,
|
2023-02-12 10:56:45 +11:00
|
|
|
}) => {
|
|
|
|
const _node = { ...node }
|
|
|
|
const { node: pipe } = getNodeFromPath<PipeExpression>(
|
|
|
|
_node,
|
|
|
|
pathToNode,
|
|
|
|
'PipeExpression'
|
|
|
|
)
|
|
|
|
const { node: varDec } = getNodeFromPath<VariableDeclarator>(
|
|
|
|
_node,
|
|
|
|
pathToNode,
|
|
|
|
'VariableDeclarator'
|
|
|
|
)
|
|
|
|
const variableName = varDec.id.name
|
|
|
|
const sketch = previousProgramMemory?.root?.[variableName]
|
2023-09-12 18:10:27 -07:00
|
|
|
if (sketch.type !== 'SketchGroup') throw new Error('not a SketchGroup')
|
2023-03-02 21:19:11 +11:00
|
|
|
|
|
|
|
const angle = createLiteral(roundOff(getAngle(from, to), 0))
|
|
|
|
const yLength = createLiteral(roundOff(Math.abs(from[1] - to[1]), 2) || 0.1)
|
|
|
|
const newLine = createCallback
|
2023-03-07 15:45:59 +11:00
|
|
|
? createCallback([angle, yLength]).callExp
|
2023-03-02 21:19:11 +11:00
|
|
|
: createCallExpression('angledLineOfYLength', [
|
|
|
|
createArrayExpression([angle, yLength]),
|
|
|
|
createPipeSubstitution(),
|
|
|
|
])
|
2023-04-01 16:47:00 +11:00
|
|
|
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
2023-03-02 21:19:11 +11:00
|
|
|
if (replaceExisting) {
|
|
|
|
pipe.body[callIndex] = newLine
|
|
|
|
} else {
|
|
|
|
pipe.body = [...pipe.body, newLine]
|
|
|
|
}
|
2023-02-12 10:56:45 +11:00
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateArgs: ({ node, pathToNode, to, from }) => {
|
|
|
|
const _node = { ...node }
|
2023-04-01 16:47:00 +11:00
|
|
|
const { node: callExpression } = getNodeFromPath<CallExpression>(
|
2023-02-12 10:56:45 +11:00
|
|
|
_node,
|
|
|
|
pathToNode
|
|
|
|
)
|
|
|
|
const angle = roundOff(getAngle(from, to), 0)
|
|
|
|
const yLength = roundOff(to[1] - from[1], 2)
|
|
|
|
|
|
|
|
const firstArg = callExpression.arguments?.[0]
|
|
|
|
const adjustedYLength = isAngleLiteral(firstArg)
|
|
|
|
? Math.abs(yLength)
|
|
|
|
: yLength // todo make work for variable angle > 180
|
|
|
|
|
|
|
|
const angleLit = createLiteral(angle)
|
|
|
|
const lengthLit = createLiteral(adjustedYLength)
|
|
|
|
|
|
|
|
if (!mutateArrExp(firstArg, createArrayExpression([angleLit, lengthLit]))) {
|
|
|
|
mutateObjExpProp(firstArg, angleLit, 'angle')
|
|
|
|
mutateObjExpProp(firstArg, lengthLit, 'length')
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTag: addTagWithTo('angleLength'),
|
|
|
|
}
|
|
|
|
|
|
|
|
export const angledLineToX: SketchLineHelper = {
|
2023-04-05 21:06:20 +10:00
|
|
|
add: ({
|
|
|
|
node,
|
|
|
|
pathToNode,
|
|
|
|
to,
|
|
|
|
from,
|
|
|
|
createCallback,
|
|
|
|
replaceExisting,
|
|
|
|
referencedSegment,
|
|
|
|
}) => {
|
2023-02-12 10:56:45 +11:00
|
|
|
const _node = { ...node }
|
|
|
|
const { node: pipe } = getNodeFromPath<PipeExpression>(
|
|
|
|
_node,
|
|
|
|
pathToNode,
|
|
|
|
'PipeExpression'
|
|
|
|
)
|
2023-03-02 21:19:11 +11:00
|
|
|
const angle = createLiteral(roundOff(getAngle(from, to), 0))
|
|
|
|
const xArg = createLiteral(roundOff(to[0], 2))
|
2023-03-17 08:27:40 +11:00
|
|
|
if (replaceExisting && createCallback) {
|
2023-04-05 21:06:20 +10:00
|
|
|
const { callExp, valueUsedInTransform } = createCallback(
|
|
|
|
[angle, xArg],
|
|
|
|
referencedSegment
|
|
|
|
)
|
2023-04-01 16:47:00 +11:00
|
|
|
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
2023-03-17 08:27:40 +11:00
|
|
|
pipe.body[callIndex] = callExp
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
valueUsedInTransform,
|
|
|
|
}
|
2023-03-02 21:19:11 +11:00
|
|
|
}
|
2023-03-17 08:27:40 +11:00
|
|
|
|
|
|
|
const callExp = createCallExpression('angledLineToX', [
|
|
|
|
createArrayExpression([angle, xArg]),
|
|
|
|
createPipeSubstitution(),
|
|
|
|
])
|
|
|
|
pipe.body = [...pipe.body, callExp]
|
2023-02-12 10:56:45 +11:00
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateArgs: ({ node, pathToNode, to, from }) => {
|
|
|
|
const _node = { ...node }
|
2023-04-01 16:47:00 +11:00
|
|
|
const { node: callExpression } = getNodeFromPath<CallExpression>(
|
2023-02-12 10:56:45 +11:00
|
|
|
_node,
|
|
|
|
pathToNode
|
|
|
|
)
|
|
|
|
const angle = roundOff(getAngle(from, to), 0)
|
|
|
|
const xLength = roundOff(to[0], 2)
|
|
|
|
|
|
|
|
const firstArg = callExpression.arguments?.[0]
|
|
|
|
const adjustedXLength = xLength
|
|
|
|
|
|
|
|
const angleLit = createLiteral(angle)
|
|
|
|
const lengthLit = createLiteral(adjustedXLength)
|
|
|
|
|
|
|
|
if (!mutateArrExp(firstArg, createArrayExpression([angleLit, lengthLit]))) {
|
|
|
|
mutateObjExpProp(firstArg, angleLit, 'angle')
|
|
|
|
mutateObjExpProp(firstArg, lengthLit, 'to')
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTag: addTagWithTo('angleTo'),
|
|
|
|
}
|
|
|
|
|
|
|
|
export const angledLineToY: SketchLineHelper = {
|
2023-04-05 21:06:20 +10:00
|
|
|
add: ({
|
|
|
|
node,
|
|
|
|
pathToNode,
|
|
|
|
to,
|
|
|
|
from,
|
|
|
|
createCallback,
|
|
|
|
replaceExisting,
|
|
|
|
referencedSegment,
|
|
|
|
}) => {
|
2023-02-12 10:56:45 +11:00
|
|
|
const _node = { ...node }
|
|
|
|
const { node: pipe } = getNodeFromPath<PipeExpression>(
|
|
|
|
_node,
|
|
|
|
pathToNode,
|
|
|
|
'PipeExpression'
|
|
|
|
)
|
2023-03-02 21:19:11 +11:00
|
|
|
const angle = createLiteral(roundOff(getAngle(from, to), 0))
|
|
|
|
const yArg = createLiteral(roundOff(to[1], 2))
|
2023-03-10 14:55:16 +11:00
|
|
|
|
|
|
|
if (replaceExisting && createCallback) {
|
2023-04-05 21:06:20 +10:00
|
|
|
const { callExp, valueUsedInTransform } = createCallback(
|
|
|
|
[angle, yArg],
|
|
|
|
referencedSegment
|
|
|
|
)
|
2023-04-01 16:47:00 +11:00
|
|
|
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
2023-03-10 14:55:16 +11:00
|
|
|
pipe.body[callIndex] = callExp
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
valueUsedInTransform,
|
|
|
|
}
|
2023-03-02 21:19:11 +11:00
|
|
|
}
|
2023-03-10 14:55:16 +11:00
|
|
|
|
|
|
|
const newLine = createCallExpression('angledLineToY', [
|
|
|
|
createArrayExpression([angle, yArg]),
|
|
|
|
createPipeSubstitution(),
|
|
|
|
])
|
|
|
|
pipe.body = [...pipe.body, newLine]
|
2023-02-12 10:56:45 +11:00
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateArgs: ({ node, pathToNode, to, from }) => {
|
|
|
|
const _node = { ...node }
|
2023-04-01 16:47:00 +11:00
|
|
|
const { node: callExpression } = getNodeFromPath<CallExpression>(
|
2023-02-12 10:56:45 +11:00
|
|
|
_node,
|
|
|
|
pathToNode
|
|
|
|
)
|
|
|
|
const angle = roundOff(getAngle(from, to), 0)
|
|
|
|
const xLength = roundOff(to[1], 2)
|
|
|
|
|
|
|
|
const firstArg = callExpression.arguments?.[0]
|
|
|
|
const adjustedXLength = xLength
|
|
|
|
|
|
|
|
const angleLit = createLiteral(angle)
|
|
|
|
const lengthLit = createLiteral(adjustedXLength)
|
|
|
|
|
|
|
|
if (!mutateArrExp(firstArg, createArrayExpression([angleLit, lengthLit]))) {
|
|
|
|
mutateObjExpProp(firstArg, angleLit, 'angle')
|
|
|
|
mutateObjExpProp(firstArg, lengthLit, 'to')
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTag: addTagWithTo('angleTo'),
|
|
|
|
}
|
|
|
|
|
2023-03-19 18:46:39 +11:00
|
|
|
export const angledLineThatIntersects: SketchLineHelper = {
|
|
|
|
add: ({
|
|
|
|
node,
|
|
|
|
pathToNode,
|
|
|
|
to,
|
|
|
|
from,
|
|
|
|
createCallback,
|
|
|
|
replaceExisting,
|
|
|
|
referencedSegment,
|
|
|
|
}) => {
|
|
|
|
const _node = { ...node }
|
|
|
|
const { node: pipe } = getNodeFromPath<PipeExpression>(
|
|
|
|
_node,
|
|
|
|
pathToNode,
|
|
|
|
'PipeExpression'
|
|
|
|
)
|
|
|
|
const angle = createLiteral(roundOff(getAngle(from, to), 0))
|
|
|
|
if (!referencedSegment)
|
|
|
|
throw new Error('referencedSegment must be provided')
|
|
|
|
const offset = createLiteral(
|
|
|
|
roundOff(
|
|
|
|
perpendicularDistance(
|
|
|
|
referencedSegment?.from,
|
|
|
|
referencedSegment?.to,
|
|
|
|
to
|
|
|
|
),
|
|
|
|
2
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if (replaceExisting && createCallback) {
|
|
|
|
const { callExp, valueUsedInTransform } = createCallback([angle, offset])
|
2023-04-01 16:47:00 +11:00
|
|
|
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
2023-03-19 18:46:39 +11:00
|
|
|
pipe.body[callIndex] = callExp
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
valueUsedInTransform,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error('not implemented')
|
|
|
|
},
|
|
|
|
updateArgs: ({ node, pathToNode, to, from, previousProgramMemory }) => {
|
|
|
|
const _node = { ...node }
|
2023-04-01 16:47:00 +11:00
|
|
|
const { node: callExpression } = getNodeFromPath<CallExpression>(
|
2023-03-19 18:46:39 +11:00
|
|
|
_node,
|
|
|
|
pathToNode
|
|
|
|
)
|
|
|
|
const angle = roundOff(getAngle(from, to), 0)
|
|
|
|
|
|
|
|
const firstArg = callExpression.arguments?.[0]
|
|
|
|
const intersectTag =
|
|
|
|
firstArg.type === 'ObjectExpression'
|
|
|
|
? firstArg.properties.find((p) => p.key.name === 'intersectTag')
|
|
|
|
?.value || createLiteral('')
|
|
|
|
: createLiteral('')
|
|
|
|
const intersectTagName =
|
|
|
|
intersectTag.type === 'Literal' ? intersectTag.value : ''
|
|
|
|
const { node: varDec } = getNodeFromPath<VariableDeclaration>(
|
|
|
|
_node,
|
|
|
|
pathToNode,
|
|
|
|
'VariableDeclaration'
|
|
|
|
)
|
|
|
|
|
|
|
|
const varName = varDec.declarations[0].id.name
|
|
|
|
const sketchGroup = previousProgramMemory.root[varName] as SketchGroup
|
|
|
|
const intersectPath = sketchGroup.value.find(
|
2023-09-12 18:10:27 -07:00
|
|
|
({ name }: Path) => name === intersectTagName
|
2023-03-19 18:46:39 +11:00
|
|
|
)
|
|
|
|
let offset = 0
|
|
|
|
if (intersectPath) {
|
|
|
|
offset = roundOff(
|
|
|
|
perpendicularDistance(intersectPath?.from, intersectPath?.to, to),
|
|
|
|
2
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const angleLit = createLiteral(angle)
|
|
|
|
|
|
|
|
mutateObjExpProp(firstArg, angleLit, 'angle')
|
|
|
|
mutateObjExpProp(firstArg, createLiteral(offset), 'offset')
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addTag: addTagWithTo('angleTo'), // TODO might be wrong
|
|
|
|
}
|
|
|
|
|
2023-02-12 10:56:45 +11:00
|
|
|
export const sketchLineHelperMap: { [key: string]: SketchLineHelper } = {
|
|
|
|
line,
|
|
|
|
lineTo,
|
|
|
|
xLine,
|
|
|
|
yLine,
|
|
|
|
xLineTo,
|
|
|
|
yLineTo,
|
|
|
|
angledLine,
|
|
|
|
angledLineOfXLength,
|
|
|
|
angledLineOfYLength,
|
|
|
|
angledLineToX,
|
|
|
|
angledLineToY,
|
2023-03-19 18:46:39 +11:00
|
|
|
angledLineThatIntersects,
|
2023-02-12 10:56:45 +11:00
|
|
|
} as const
|
|
|
|
|
|
|
|
export function changeSketchArguments(
|
|
|
|
node: Program,
|
|
|
|
programMemory: ProgramMemory,
|
|
|
|
sourceRange: SourceRange,
|
|
|
|
args: [number, number],
|
|
|
|
guiMode: GuiModes,
|
|
|
|
from: [number, number]
|
|
|
|
): { modifiedAst: Program } {
|
|
|
|
const _node = { ...node }
|
|
|
|
const thePath = getNodePathFromSourceRange(_node, sourceRange)
|
2023-04-01 16:47:00 +11:00
|
|
|
const { node: callExpression, shallowPath } = getNodeFromPath<CallExpression>(
|
2023-02-12 10:56:45 +11:00
|
|
|
_node,
|
|
|
|
thePath
|
|
|
|
)
|
|
|
|
if (guiMode.mode !== 'sketch') throw new Error('not in sketch mode')
|
|
|
|
|
|
|
|
if (callExpression?.callee?.name in sketchLineHelperMap) {
|
|
|
|
const { updateArgs } = sketchLineHelperMap[callExpression.callee.name]
|
2023-03-19 18:46:39 +11:00
|
|
|
if (!updateArgs) throw new Error('not a sketch line helper')
|
2023-02-12 10:56:45 +11:00
|
|
|
return updateArgs({
|
|
|
|
node: _node,
|
|
|
|
previousProgramMemory: programMemory,
|
2023-04-01 16:47:00 +11:00
|
|
|
pathToNode: shallowPath,
|
2023-02-12 10:56:45 +11:00
|
|
|
to: args,
|
|
|
|
from,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('not a sketch line helper')
|
|
|
|
}
|
|
|
|
|
2023-02-21 10:50:45 +11:00
|
|
|
interface CreateLineFnCallArgs {
|
|
|
|
node: Program
|
|
|
|
programMemory: ProgramMemory
|
|
|
|
to: [number, number]
|
|
|
|
from: [number, number]
|
|
|
|
fnName: TooTip
|
|
|
|
pathToNode: PathToNode
|
|
|
|
}
|
2023-02-12 10:56:45 +11:00
|
|
|
|
2023-02-21 10:50:45 +11:00
|
|
|
export function addNewSketchLn({
|
2023-03-17 15:53:20 +11:00
|
|
|
node: _node,
|
2023-02-21 10:50:45 +11:00
|
|
|
programMemory: previousProgramMemory,
|
|
|
|
to,
|
|
|
|
fnName,
|
|
|
|
pathToNode,
|
|
|
|
}: Omit<CreateLineFnCallArgs, 'from'>): { modifiedAst: Program } {
|
2023-03-17 15:53:20 +11:00
|
|
|
const node = JSON.parse(JSON.stringify(_node))
|
2023-03-21 19:02:18 +11:00
|
|
|
const { add, updateArgs } = sketchLineHelperMap?.[fnName] || {}
|
|
|
|
if (!add || !updateArgs) throw new Error('not a sketch line helper')
|
2023-02-21 10:50:45 +11:00
|
|
|
const { node: varDec } = getNodeFromPath<VariableDeclarator>(
|
|
|
|
node,
|
|
|
|
pathToNode,
|
|
|
|
'VariableDeclarator'
|
|
|
|
)
|
2023-09-13 08:36:47 +10:00
|
|
|
const { node: pipeExp, shallowPath: pipePath } = getNodeFromPath<
|
|
|
|
PipeExpression | CallExpression
|
|
|
|
>(node, pathToNode, 'PipeExpression')
|
2023-02-21 10:50:45 +11:00
|
|
|
const variableName = varDec.id.name
|
|
|
|
const sketch = previousProgramMemory?.root?.[variableName]
|
2023-09-12 18:10:27 -07:00
|
|
|
if (sketch.type !== 'SketchGroup') throw new Error('not a SketchGroup')
|
2023-03-17 15:53:20 +11:00
|
|
|
|
2023-09-13 08:36:47 +10:00
|
|
|
const last = sketch.value[sketch.value.length - 1] || sketch.start
|
2023-02-21 10:50:45 +11:00
|
|
|
const from = last.to
|
|
|
|
|
|
|
|
return add({
|
|
|
|
node,
|
|
|
|
previousProgramMemory,
|
|
|
|
pathToNode,
|
|
|
|
to,
|
|
|
|
from,
|
|
|
|
replaceExisting: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function replaceSketchLine({
|
|
|
|
node,
|
|
|
|
programMemory,
|
|
|
|
sourceRange,
|
|
|
|
fnName,
|
|
|
|
to,
|
|
|
|
from,
|
|
|
|
createCallback,
|
2023-03-05 07:34:56 +11:00
|
|
|
referencedSegment,
|
2023-02-21 10:50:45 +11:00
|
|
|
}: {
|
|
|
|
node: Program
|
|
|
|
programMemory: ProgramMemory
|
|
|
|
sourceRange: SourceRange
|
|
|
|
fnName: TooTip
|
|
|
|
to: [number, number]
|
|
|
|
from: [number, number]
|
|
|
|
createCallback: TransformCallback
|
2023-03-05 07:34:56 +11:00
|
|
|
referencedSegment?: Path
|
2023-04-14 07:49:36 +10:00
|
|
|
}): {
|
|
|
|
modifiedAst: Program
|
|
|
|
valueUsedInTransform?: number
|
|
|
|
pathToNode: PathToNode
|
|
|
|
} {
|
2023-03-19 18:46:39 +11:00
|
|
|
if (![...toolTips, 'intersect'].includes(fnName))
|
|
|
|
throw new Error('not a tooltip')
|
2023-02-21 10:50:45 +11:00
|
|
|
const _node = { ...node }
|
|
|
|
const thePath = getNodePathFromSourceRange(_node, sourceRange)
|
|
|
|
|
|
|
|
const { add } = sketchLineHelperMap[fnName]
|
2023-04-14 07:49:36 +10:00
|
|
|
const { modifiedAst, valueUsedInTransform, pathToNode } = add({
|
2023-02-21 10:50:45 +11:00
|
|
|
node: _node,
|
|
|
|
previousProgramMemory: programMemory,
|
|
|
|
pathToNode: thePath,
|
2023-03-05 07:34:56 +11:00
|
|
|
referencedSegment,
|
2023-02-21 10:50:45 +11:00
|
|
|
to,
|
|
|
|
from,
|
|
|
|
replaceExisting: true,
|
|
|
|
createCallback,
|
|
|
|
})
|
2023-04-14 07:49:36 +10:00
|
|
|
return { modifiedAst, valueUsedInTransform, pathToNode }
|
2023-02-12 10:56:45 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
export function addTagForSketchOnFace(
|
|
|
|
a: ModifyAstBase,
|
|
|
|
expressionName: string
|
|
|
|
) {
|
|
|
|
if (expressionName in sketchLineHelperMap) {
|
|
|
|
const { addTag } = sketchLineHelperMap[expressionName]
|
|
|
|
return addTag(a)
|
|
|
|
}
|
|
|
|
throw new Error('not a sketch line helper')
|
|
|
|
}
|
|
|
|
|
|
|
|
function isAngleLiteral(lineArugement: Value): boolean {
|
|
|
|
return lineArugement?.type === 'ArrayExpression'
|
2023-09-13 07:23:14 -07:00
|
|
|
? isLiteralArrayOrStatic(lineArugement.elements[0])
|
2023-02-12 10:56:45 +11:00
|
|
|
: lineArugement?.type === 'ObjectExpression'
|
2023-09-13 07:23:14 -07:00
|
|
|
? isLiteralArrayOrStatic(
|
|
|
|
lineArugement.properties.find(({ key }) => key.name === 'angle')?.value
|
|
|
|
)
|
2023-02-12 10:56:45 +11:00
|
|
|
: false
|
|
|
|
}
|
|
|
|
|
|
|
|
type addTagFn = (a: ModifyAstBase) => { modifiedAst: Program; tag: string }
|
|
|
|
|
|
|
|
function addTagWithTo(
|
|
|
|
argType: 'angleLength' | 'angleTo' | 'default' | 'length'
|
|
|
|
): addTagFn {
|
|
|
|
return ({ node, pathToNode }) => {
|
|
|
|
let tagName = findUniqueName(node, 'seg', 2)
|
|
|
|
const _node = { ...node }
|
|
|
|
const { node: callExpression } = getNodeFromPath<CallExpression>(
|
|
|
|
_node,
|
|
|
|
pathToNode
|
|
|
|
)
|
|
|
|
const firstArg = callExpression.arguments?.[0]
|
|
|
|
if (firstArg.type === 'ObjectExpression') {
|
|
|
|
const existingTagName = firstArg.properties?.find(
|
|
|
|
(prop) => prop.key.name === 'tag'
|
|
|
|
)
|
|
|
|
if (!existingTagName) {
|
|
|
|
mutateObjExpProp(
|
|
|
|
callExpression.arguments?.[0],
|
|
|
|
createLiteral(tagName),
|
|
|
|
'tag'
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
tagName = `${(existingTagName.value as Literal).value}`
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
tag: tagName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (firstArg.type === 'ArrayExpression') {
|
|
|
|
const objExp =
|
|
|
|
argType === 'default'
|
|
|
|
? createObjectExpression({
|
|
|
|
to: firstArg,
|
|
|
|
tag: createLiteral(tagName),
|
|
|
|
})
|
|
|
|
: argType === 'angleLength'
|
|
|
|
? createObjectExpression({
|
|
|
|
angle: firstArg.elements[0],
|
|
|
|
length: firstArg.elements[1],
|
|
|
|
tag: createLiteral(tagName),
|
|
|
|
})
|
|
|
|
: createObjectExpression({
|
|
|
|
angle: firstArg.elements[0],
|
|
|
|
to: firstArg.elements[1],
|
|
|
|
tag: createLiteral(tagName),
|
|
|
|
})
|
|
|
|
callExpression.arguments[0] = objExp
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
tag: tagName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (firstArg.type === 'Literal') {
|
|
|
|
const objExp =
|
|
|
|
argType === 'length'
|
|
|
|
? createObjectExpression({
|
|
|
|
length: firstArg,
|
|
|
|
tag: createLiteral(tagName),
|
|
|
|
})
|
|
|
|
: createObjectExpression({
|
|
|
|
to: firstArg,
|
|
|
|
tag: createLiteral(tagName),
|
|
|
|
})
|
|
|
|
callExpression.arguments[0] = objExp
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
tag: tagName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error('lineTo must be called with an object or array')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getYComponent(
|
|
|
|
angleDegree: number,
|
|
|
|
xComponent: number
|
|
|
|
): [number, number] {
|
|
|
|
const normalisedAngle = ((angleDegree % 360) + 360) % 360 // between 0 and 360
|
|
|
|
let yComponent = xComponent * Math.tan((normalisedAngle * Math.PI) / 180)
|
|
|
|
const sign = normalisedAngle > 90 && normalisedAngle <= 270 ? -1 : 1
|
|
|
|
return [sign * xComponent, sign * yComponent]
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getXComponent(
|
|
|
|
angleDegree: number,
|
|
|
|
yComponent: number
|
|
|
|
): [number, number] {
|
|
|
|
const normalisedAngle = ((angleDegree % 360) + 360) % 360 // between 0 and 360
|
|
|
|
let xComponent = yComponent / Math.tan((normalisedAngle * Math.PI) / 180)
|
|
|
|
const sign = normalisedAngle > 180 && normalisedAngle <= 360 ? -1 : 1
|
|
|
|
return [sign * xComponent, sign * yComponent]
|
|
|
|
}
|
2023-02-21 10:50:45 +11:00
|
|
|
|
|
|
|
function getFirstArgValuesForXYFns(callExpression: CallExpression): {
|
|
|
|
val: [Value, Value]
|
|
|
|
tag?: Value
|
|
|
|
} {
|
|
|
|
// used for lineTo, line
|
|
|
|
const firstArg = callExpression.arguments[0]
|
|
|
|
if (firstArg.type === 'ArrayExpression') {
|
|
|
|
return { val: [firstArg.elements[0], firstArg.elements[1]] }
|
|
|
|
}
|
|
|
|
if (firstArg.type === 'ObjectExpression') {
|
|
|
|
const to = firstArg.properties.find((p) => p.key.name === 'to')?.value
|
|
|
|
const tag = firstArg.properties.find((p) => p.key.name === 'tag')?.value
|
|
|
|
if (to?.type === 'ArrayExpression') {
|
|
|
|
const [x, y] = to.elements
|
|
|
|
return { val: [x, y], tag }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error('expected ArrayExpression or ObjectExpression')
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFirstArgValuesForAngleFns(callExpression: CallExpression): {
|
|
|
|
val: [Value, Value]
|
|
|
|
tag?: Value
|
|
|
|
} {
|
|
|
|
// used for angledLine, angledLineOfXLength, angledLineToX, angledLineOfYLength, angledLineToY
|
|
|
|
const firstArg = callExpression.arguments[0]
|
|
|
|
if (firstArg.type === 'ArrayExpression') {
|
|
|
|
return { val: [firstArg.elements[0], firstArg.elements[1]] }
|
|
|
|
}
|
|
|
|
if (firstArg.type === 'ObjectExpression') {
|
|
|
|
const tag = firstArg.properties.find((p) => p.key.name === 'tag')?.value
|
|
|
|
const angle = firstArg.properties.find((p) => p.key.name === 'angle')?.value
|
|
|
|
const secondArgName = ['angledLineToX', 'angledLineToY'].includes(
|
|
|
|
callExpression?.callee?.name as TooTip
|
|
|
|
)
|
|
|
|
? 'to'
|
|
|
|
: 'length'
|
|
|
|
const length = firstArg.properties.find(
|
|
|
|
(p) => p.key.name === secondArgName
|
|
|
|
)?.value
|
|
|
|
if (angle && length) {
|
|
|
|
return { val: [angle, length], tag }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error('expected ArrayExpression or ObjectExpression')
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFirstArgValuesForXYLineFns(callExpression: CallExpression): {
|
|
|
|
val: Value
|
|
|
|
tag?: Value
|
|
|
|
} {
|
|
|
|
// used for xLine, yLine, xLineTo, yLineTo
|
|
|
|
const firstArg = callExpression.arguments[0]
|
|
|
|
if (firstArg.type !== 'ObjectExpression') {
|
|
|
|
return { val: firstArg }
|
|
|
|
}
|
|
|
|
const tag = firstArg.properties.find((p) => p.key.name === 'tag')?.value
|
2023-03-17 08:27:40 +11:00
|
|
|
const secondArgName = ['xLineTo', 'yLineTo', 'startSketchAt'].includes(
|
2023-02-21 10:50:45 +11:00
|
|
|
// const secondArgName = ['xLineTo', 'yLineTo', 'angledLineToX', 'angledLineToY'].includes(
|
|
|
|
callExpression?.callee?.name
|
|
|
|
)
|
|
|
|
? 'to'
|
|
|
|
: 'length'
|
|
|
|
const length = firstArg.properties.find(
|
|
|
|
(p) => p.key.name === secondArgName
|
|
|
|
)?.value
|
|
|
|
if (length) {
|
|
|
|
return { val: length, tag }
|
|
|
|
}
|
2023-03-22 03:02:37 +11:00
|
|
|
console.warn('expected ArrayExpression or ObjectExpression')
|
|
|
|
return {
|
|
|
|
val: createLiteral(1),
|
|
|
|
}
|
2023-02-21 10:50:45 +11:00
|
|
|
}
|
|
|
|
|
2023-03-19 18:46:39 +11:00
|
|
|
const getAngledLineThatIntersects = (
|
|
|
|
callExp: CallExpression
|
|
|
|
): {
|
2023-04-03 16:05:25 +10:00
|
|
|
val: [Value, Value, Value]
|
2023-03-19 18:46:39 +11:00
|
|
|
tag?: Value
|
|
|
|
} => {
|
|
|
|
const firstArg = callExp.arguments[0]
|
|
|
|
if (firstArg.type === 'ObjectExpression') {
|
|
|
|
const tag = firstArg.properties.find((p) => p.key.name === 'tag')?.value
|
|
|
|
const angle = firstArg.properties.find((p) => p.key.name === 'angle')?.value
|
|
|
|
const offset = firstArg.properties.find(
|
|
|
|
(p) => p.key.name === 'offset'
|
|
|
|
)?.value
|
2023-04-03 16:05:25 +10:00
|
|
|
const intersectTag = firstArg.properties.find(
|
|
|
|
(p) => p.key.name === 'intersectTag'
|
|
|
|
)?.value
|
|
|
|
if (angle && offset && intersectTag) {
|
|
|
|
return { val: [angle, offset, intersectTag], tag }
|
2023-03-19 18:46:39 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error('expected ArrayExpression or ObjectExpression')
|
|
|
|
}
|
|
|
|
|
2023-03-02 21:19:11 +11:00
|
|
|
export function getFirstArg(callExp: CallExpression): {
|
2023-04-03 16:05:25 +10:00
|
|
|
val: Value | [Value, Value] | [Value, Value, Value]
|
2023-03-02 21:19:11 +11:00
|
|
|
tag?: Value
|
|
|
|
} {
|
|
|
|
const name = callExp?.callee?.name
|
|
|
|
if (['lineTo', 'line'].includes(name)) {
|
|
|
|
return getFirstArgValuesForXYFns(callExp)
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
[
|
|
|
|
'angledLine',
|
|
|
|
'angledLineOfXLength',
|
|
|
|
'angledLineToX',
|
|
|
|
'angledLineOfYLength',
|
|
|
|
'angledLineToY',
|
|
|
|
].includes(name)
|
|
|
|
) {
|
|
|
|
return getFirstArgValuesForAngleFns(callExp)
|
|
|
|
}
|
|
|
|
if (['xLine', 'yLine', 'xLineTo', 'yLineTo'].includes(name)) {
|
|
|
|
return getFirstArgValuesForXYLineFns(callExp)
|
|
|
|
}
|
2023-03-17 08:27:40 +11:00
|
|
|
if (['startSketchAt'].includes(name)) {
|
|
|
|
return getFirstArgValuesForXYLineFns(callExp)
|
|
|
|
}
|
2023-03-19 18:46:39 +11:00
|
|
|
if (['angledLineThatIntersects'].includes(name)) {
|
|
|
|
return getAngledLineThatIntersects(callExp)
|
|
|
|
}
|
2023-03-02 21:19:11 +11:00
|
|
|
throw new Error('unexpected call expression')
|
2023-02-21 10:50:45 +11:00
|
|
|
}
|