Creating new sketch calling sketch fns with dumby default values (#67)

This commit is contained in:
Kurt Hutten
2023-03-17 15:53:20 +11:00
committed by GitHub
parent c11e7e7fd3
commit f7c78db92b
3 changed files with 109 additions and 17 deletions

View File

@ -111,9 +111,9 @@ describe('Testing addSketchTo', () => {
'yz' 'yz'
) )
const str = recast(result.modifiedAst) const str = recast(result.modifiedAst)
expect(str).toBe(`const part001 = startSketchAt([0, 0]) expect(str).toBe(`const part001 = startSketchAt('default')
|> ry(90, %) |> ry(90, %)
|> lineTo([1, 1], %) |> line('default', %)
show(part001)`) show(part001)`)
}) })
}) })

View File

@ -32,14 +32,14 @@ export function addSketchTo(
const _name = name || findUniqueName(node, 'part') const _name = name || findUniqueName(node, 'part')
const startSketchAt = createCallExpression('startSketchAt', [ const startSketchAt = createCallExpression('startSketchAt', [
createArrayExpression([createLiteral(0), createLiteral(0)]), createLiteral('default'),
]) ])
const rotate = createCallExpression(axis === 'xz' ? 'rx' : 'ry', [ const rotate = createCallExpression(axis === 'xz' ? 'rx' : 'ry', [
createLiteral(90), createLiteral(90),
createPipeSubstitution(), createPipeSubstitution(),
]) ])
const initialLineTo = createCallExpression('lineTo', [ const initialLineTo = createCallExpression('line', [
createArrayExpression([createLiteral(1), createLiteral(1)]), createLiteral('default'),
createPipeSubstitution(), createPipeSubstitution(),
]) ])

View File

@ -40,6 +40,7 @@ import {
findUniqueName, findUniqueName,
} from '../modifyAst' } from '../modifyAst'
import { roundOff, getLength, getAngle } from '../../lib/utils' import { roundOff, getLength, getAngle } from '../../lib/utils'
import { getSketchSegmentIndexFromSourceRange } from './sketchConstraints'
export type Coords2d = [number, number] export type Coords2d = [number, number]
@ -204,11 +205,12 @@ export const lineTo: SketchLineHelper = {
export const line: SketchLineHelper = { export const line: SketchLineHelper = {
fn: ( fn: (
{ sourceRange, programMemory }, { sourceRange },
data: data:
| [number, number] | [number, number]
| 'default'
| { | {
to: [number, number] to: [number, number] | 'default'
// name?: string // name?: string
tag?: string tag?: string
}, },
@ -217,7 +219,13 @@ export const line: SketchLineHelper = {
if (!previousSketch) throw new Error('lineTo must be called after lineTo') if (!previousSketch) throw new Error('lineTo must be called after lineTo')
const sketchGroup = { ...previousSketch } const sketchGroup = { ...previousSketch }
const from = getCoordsFromPaths(sketchGroup, sketchGroup.value.length - 1) const from = getCoordsFromPaths(sketchGroup, sketchGroup.value.length - 1)
const args = 'to' in data ? data.to : data let args: [number, number] = [0.2, 1]
if (data !== 'default' && 'to' in data && data.to !== 'default') {
args = data.to
} else if (data !== 'default' && !('to' in data)) {
args = data
}
const to: [number, number] = [from[0] + args[0], from[1] + args[1]] const to: [number, number] = [from[0] + args[0], from[1] + args[1]]
const geo = lineGeo({ const geo = lineGeo({
from: [...from, 0], from: [...from, 0],
@ -242,7 +250,7 @@ export const line: SketchLineHelper = {
], ],
}, },
} }
if ('tag' in data) { if (data !== 'default' && 'tag' in data) {
currentPath.name = data.tag currentPath.name = data.tag
} }
return { return {
@ -307,9 +315,26 @@ export const line: SketchLineHelper = {
createLiteral(roundOff(to[1] - from[1], 2)), createLiteral(roundOff(to[1] - from[1], 2)),
]) ])
mutateArrExp(callExpression.arguments?.[0], toArrExp) || if (
mutateObjExpProp(callExpression.arguments?.[0], toArrExp, 'to') callExpression.arguments?.[0].type === 'Literal' &&
callExpression.arguments?.[0].value === 'default'
) {
callExpression.arguments[0] = toArrExp
} else if (callExpression.arguments?.[0].type === 'ObjectExpression') {
const toProp = callExpression.arguments?.[0].properties?.find(
({ key }) => key.name === 'to'
)
if (
toProp &&
toProp.value.type === 'Literal' &&
toProp.value.value === 'default'
) {
toProp.value = toArrExp
}
} else {
mutateArrExp(callExpression.arguments?.[0], toArrExp) ||
mutateObjExpProp(callExpression.arguments?.[0], toArrExp, 'to')
}
return { return {
modifiedAst: _node, modifiedAst: _node,
pathToNode, pathToNode,
@ -1085,21 +1110,71 @@ interface CreateLineFnCallArgs {
} }
export function addNewSketchLn({ export function addNewSketchLn({
node, node: _node,
programMemory: previousProgramMemory, programMemory: previousProgramMemory,
to, to,
fnName, fnName,
pathToNode, pathToNode,
}: Omit<CreateLineFnCallArgs, 'from'>): { modifiedAst: Program } { }: Omit<CreateLineFnCallArgs, 'from'>): { modifiedAst: Program } {
const { add } = sketchLineHelperMap[fnName] const node = JSON.parse(JSON.stringify(_node))
const { add, updateArgs } = sketchLineHelperMap[fnName]
const { node: varDec } = getNodeFromPath<VariableDeclarator>( const { node: varDec } = getNodeFromPath<VariableDeclarator>(
node, node,
pathToNode, pathToNode,
'VariableDeclarator' 'VariableDeclarator'
) )
const { node: pipeExp, path: pipePath } = getNodeFromPath<PipeExpression>(
node,
pathToNode,
'PipeExpression'
)
const maybeStartSketchAt = pipeExp.body.find(
(exp) =>
exp.type === 'CallExpression' &&
exp.callee.name === 'startSketchAt' &&
exp.arguments[0].type === 'Literal' &&
exp.arguments[0].value === 'default'
)
const maybeDefaultLine = pipeExp.body.findIndex(
(exp) =>
exp.type === 'CallExpression' &&
exp.callee.name === 'line' &&
exp.arguments[0].type === 'Literal' &&
exp.arguments[0].value === 'default'
)
const defaultLinePath = [...pipePath, 'body', maybeDefaultLine]
const variableName = varDec.id.name const variableName = varDec.id.name
const sketch = previousProgramMemory?.root?.[variableName] const sketch = previousProgramMemory?.root?.[variableName]
if (sketch.type !== 'sketchGroup') throw new Error('not a sketchGroup') if (sketch.type !== 'sketchGroup') throw new Error('not a sketchGroup')
if (maybeStartSketchAt) {
const startSketchAt = maybeStartSketchAt as any
startSketchAt.arguments[0] = createArrayExpression([
createLiteral(to[0]),
createLiteral(to[1]),
])
return {
modifiedAst: node,
}
}
if (maybeDefaultLine !== -1) {
const defaultLine = getNodeFromPath<CallExpression>(
node,
defaultLinePath
).node
const { from } = getSketchSegmentIndexFromSourceRange(sketch, [
defaultLine.start,
defaultLine.end,
])
return updateArgs({
node,
previousProgramMemory,
pathToNode: defaultLinePath,
to,
from,
})
}
const last = sketch.value[sketch.value.length - 1] const last = sketch.value[sketch.value.length - 1]
const from = last.to const from = last.to
@ -1287,13 +1362,20 @@ export const startSketchAt: InternalFn = (
{ sourceRange, programMemory }, { sourceRange, programMemory },
data: data:
| [number, number] | [number, number]
| 'default'
| { | {
to: [number, number] to: [number, number] | 'default'
// name?: string // name?: string
tag?: string tag?: string
} }
): SketchGroup => { ): SketchGroup => {
const to = 'to' in data ? data.to : data let to: [number, number] = [0, 0]
if (data !== 'default' && 'to' in data && data.to !== 'default') {
to = data.to
} else if (data !== 'default' && !('to' in data)) {
to = data
}
const geo = sketchBaseGeo({ to: [...to, 0] }) const geo = sketchBaseGeo({ to: [...to, 0] })
const currentPath: Path = { const currentPath: Path = {
type: 'base', type: 'base',
@ -1310,7 +1392,7 @@ export const startSketchAt: InternalFn = (
], ],
}, },
} }
if ('tag' in data) { if (data !== 'default' && 'tag' in data) {
currentPath.name = data.tag currentPath.name = data.tag
} }
return { return {
@ -1354,6 +1436,14 @@ function getFirstArgValuesForXYFns(callExpression: CallExpression): {
} { } {
// used for lineTo, line // used for lineTo, line
const firstArg = callExpression.arguments[0] const firstArg = callExpression.arguments[0]
if (firstArg.type === 'Literal' && firstArg.value === 'default') {
return {
val:
callExpression.callee.name === 'startSketchAt'
? [createLiteral(0), createLiteral(0)]
: [createLiteral(1), createLiteral(1)],
}
}
if (firstArg.type === 'ArrayExpression') { if (firstArg.type === 'ArrayExpression') {
return { val: [firstArg.elements[0], firstArg.elements[1]] } return { val: [firstArg.elements[0], firstArg.elements[1]] }
} }
@ -1363,6 +1453,8 @@ function getFirstArgValuesForXYFns(callExpression: CallExpression): {
if (to?.type === 'ArrayExpression') { if (to?.type === 'ArrayExpression') {
const [x, y] = to.elements const [x, y] = to.elements
return { val: [x, y], tag } return { val: [x, y], tag }
} else if (to?.type === 'Literal' && to.value === 'default') {
return { val: [createLiteral(0), createLiteral(0)], tag }
} }
} }
throw new Error('expected ArrayExpression or ObjectExpression') throw new Error('expected ArrayExpression or ObjectExpression')