create sketch can rotate to match base planes

This commit is contained in:
Kurt Hutten IrevDev
2022-12-04 15:50:52 +11:00
parent 6f40d9b6b7
commit 5fa2472e0d
4 changed files with 102 additions and 79 deletions

View File

@ -51,14 +51,15 @@ export const BasePlanes = () => {
end: 0, end: 0,
body: [], body: [],
} }
const { modifiedAst, id } = addSketchTo(_ast)
const axis = axisIndex === 0 ? 'xy' : axisIndex === 1 ? 'xz' : 'yz' const axis = axisIndex === 0 ? 'xy' : axisIndex === 1 ? 'xz' : 'yz'
const { modifiedAst, id, pathToNode } = addSketchTo(_ast, axis)
setGuiMode({ setGuiMode({
mode: 'sketch', mode: 'sketch',
sketchMode: 'points', sketchMode: 'points',
axis, axis,
id, id,
pathToNode,
}) })
updateAst(modifiedAst) updateAst(modifiedAst)

View File

@ -56,7 +56,7 @@ export const SketchPlane = () => {
} else if (guiMode.axis === 'yz') { } else if (guiMode.axis === 'yz') {
addLinePoint = [point.z, point.y] addLinePoint = [point.z, point.y]
} }
const { modifiedAst, id } = addLine(_ast, guiMode.id, addLinePoint) const { modifiedAst } = addLine(_ast, guiMode.pathToNode, addLinePoint)
updateAst(modifiedAst) updateAst(modifiedAst)
}} }}
> >

View File

@ -330,7 +330,10 @@ function makeVariableDeclarators(
const declarationToken = previousMeaningfulToken(tokens, index) const declarationToken = previousMeaningfulToken(tokens, index)
const contentsStartToken = nextMeaningfulToken(tokens, assignmentToken.index) const contentsStartToken = nextMeaningfulToken(tokens, assignmentToken.index)
const nextAfterInit = nextMeaningfulToken(tokens, contentsStartToken.index) const nextAfterInit = nextMeaningfulToken(tokens, contentsStartToken.index)
const pipeStartIndex = assignmentToken?.token?.type === 'operator' ? contentsStartToken.index : assignmentToken.index const pipeStartIndex =
assignmentToken?.token?.type === 'operator'
? contentsStartToken.index
: assignmentToken.index
const nextPipeOperator = hasPipeOperator(tokens, pipeStartIndex) const nextPipeOperator = hasPipeOperator(tokens, pipeStartIndex)
let init: Value let init: Value
let lastIndex = contentsStartToken.index let lastIndex = contentsStartToken.index
@ -844,13 +847,16 @@ export function findNextCallExpression(
if (nextToken.index >= tokens.length) { if (nextToken.index >= tokens.length) {
return { token: null, index: tokens.length - 1 } return { token: null, index: tokens.length - 1 }
} }
if (nextToken.token.type === 'word' && veryNextToken?.type === 'brace' && veryNextToken?.value === '(') { if (
nextToken.token.type === 'word' &&
veryNextToken?.type === 'brace' &&
veryNextToken?.value === '('
) {
return nextToken return nextToken
} }
return findNextCallExpression(tokens, nextToken.index) return findNextCallExpression(tokens, nextToken.index)
} }
export function findNextClosingCurlyBrace( export function findNextClosingCurlyBrace(
tokens: Token[], tokens: Token[],
index: number index: number
@ -884,8 +890,14 @@ export function hasPipeOperator(
if (limitIndex === -1) { if (limitIndex === -1) {
const callExpressionEnd = isCallExpression(tokens, index) const callExpressionEnd = isCallExpression(tokens, index)
if (callExpressionEnd !== -1) { if (callExpressionEnd !== -1) {
const tokenAfterCallExpression = nextMeaningfulToken(tokens, callExpressionEnd) const tokenAfterCallExpression = nextMeaningfulToken(
if (tokenAfterCallExpression?.token?.type === 'operator' && tokenAfterCallExpression.token.value === '|>') { tokens,
callExpressionEnd
)
if (
tokenAfterCallExpression?.token?.type === 'operator' &&
tokenAfterCallExpression.token.value === '|>'
) {
return tokenAfterCallExpression return tokenAfterCallExpression
} }
return false return false
@ -893,8 +905,14 @@ export function hasPipeOperator(
const currentToken = tokens[index] const currentToken = tokens[index]
if (currentToken?.type === 'brace' && currentToken?.value === '{') { if (currentToken?.type === 'brace' && currentToken?.value === '{') {
const closingBraceIndex = findClosingBrace(tokens, index) const closingBraceIndex = findClosingBrace(tokens, index)
const tokenAfterClosingBrace = nextMeaningfulToken(tokens, closingBraceIndex) const tokenAfterClosingBrace = nextMeaningfulToken(
if (tokenAfterClosingBrace?.token?.type === 'operator' && tokenAfterClosingBrace.token.value === '|>') { tokens,
closingBraceIndex
)
if (
tokenAfterClosingBrace?.token?.type === 'operator' &&
tokenAfterClosingBrace.token.value === '|>'
) {
return tokenAfterClosingBrace return tokenAfterClosingBrace
} }
return false return false
@ -969,8 +987,9 @@ export function findClosingBrace(
export function addSketchTo( export function addSketchTo(
node: Program, node: Program,
axis: 'xy' | 'xz' | 'yz',
name = '' name = ''
): { modifiedAst: Program; id: string } { ): { modifiedAst: Program; id: string; pathToNode: (string | number)[] } {
const _node = { ...node } const _node = { ...node }
const dumbyStartend = { start: 0, end: 0 } const dumbyStartend = { start: 0, end: 0 }
const _name = name || findUniqueName(node, 'mySketch') const _name = name || findUniqueName(node, 'mySketch')
@ -984,6 +1003,36 @@ export function addSketchTo(
...dumbyStartend, ...dumbyStartend,
body: sketchBody, body: sketchBody,
} }
const rotate: CallExpression = {
type: 'CallExpression',
...dumbyStartend,
callee: {
type: 'Identifier',
...dumbyStartend,
name: axis === 'xz' ? 'rx' : 'ry',
},
arguments: [
{
type: 'Literal',
...dumbyStartend,
value: axis === 'yz' ? -90 : 90,
raw: axis === 'yz' ? '-90' : '90',
},
{
type: 'PipeSubstitution',
...dumbyStartend,
},
],
optional: false,
}
const pipChain: PipeExpression = {
type: 'PipeExpression',
...dumbyStartend,
body: [sketch, rotate],
}
const sketchVariableDeclaration: VariableDeclaration = { const sketchVariableDeclaration: VariableDeclaration = {
type: 'VariableDeclaration', type: 'VariableDeclaration',
...dumbyStartend, ...dumbyStartend,
@ -997,22 +1046,35 @@ export function addSketchTo(
...dumbyStartend, ...dumbyStartend,
name: _name, name: _name,
}, },
init: sketch, init: axis === 'xy' ? sketch : pipChain,
}, },
], ],
} }
const showCallIndex = getShowIndex(_node) const showCallIndex = getShowIndex(_node)
let sketchIndex = showCallIndex
if (showCallIndex === -1) { if (showCallIndex === -1) {
_node.body = [...node.body, sketchVariableDeclaration] _node.body = [...node.body, sketchVariableDeclaration]
sketchIndex = _node.body.length - 1
} else { } else {
const newBody = [...node.body] const newBody = [...node.body]
newBody.splice(showCallIndex, 0, sketchVariableDeclaration) newBody.splice(showCallIndex, 0, sketchVariableDeclaration)
_node.body = newBody _node.body = newBody
} }
let pathToNode: (string | number)[] = [
'body',
sketchIndex,
'declarations',
'0',
'init',
]
if (axis !== 'xy') {
pathToNode = [...pathToNode, 'body', '0']
}
return { return {
modifiedAst: addToShow(_node, _name), modifiedAst: addToShow(_node, _name),
id: _name, id: _name,
pathToNode,
} }
} }
@ -1107,15 +1169,15 @@ function getShowIndex(node: Program): number {
export function addLine( export function addLine(
node: Program, node: Program,
id: string, pathToNode: (string | number)[],
to: [number, number] to: [number, number]
): { modifiedAst: Program; id: string } { ): { modifiedAst: Program; pathToNode: (string | number)[] } {
const _node = { ...node } const _node = { ...node }
const dumbyStartend = { start: 0, end: 0 } const dumbyStartend = { start: 0, end: 0 }
const { index, sketchDeclaration, sketchExpression } = getSketchStatement( const sketchExpression = getNodeFromPath(
_node, _node,
id pathToNode
) ) as SketchExpression
const line: ExpressionStatement = { const line: ExpressionStatement = {
type: 'ExpressionStatement', type: 'ExpressionStatement',
...dumbyStartend, ...dumbyStartend,
@ -1145,78 +1207,28 @@ export function addLine(
}, },
} }
const newBody = [...sketchExpression.body.body, line] const newBody = [...sketchExpression.body.body, line]
const newSketchExpression: SketchExpression = { sketchExpression.body.body = newBody
...sketchExpression,
body: {
...sketchExpression.body,
body: newBody,
},
}
const newSketchDeclaration: VariableDeclaration = {
...sketchDeclaration,
declarations: [
{
...sketchDeclaration.declarations[0],
init: newSketchExpression,
},
],
}
_node.body[index] = newSketchDeclaration
return { return {
modifiedAst: _node, modifiedAst: _node,
id, pathToNode,
} }
} }
function getSketchStatement(
node: Program,
id: string
): {
sketchDeclaration: VariableDeclaration
sketchExpression: SketchExpression
index: number
} {
const sketchStatementIndex = node.body.findIndex(
(statement) =>
statement.type === 'VariableDeclaration' &&
statement.kind === 'sketch' &&
statement.declarations[0].id.type === 'Identifier' &&
statement.declarations[0].id.name === id
)
const sketchStatement = node.body.find(
(statement) =>
statement.type === 'VariableDeclaration' &&
statement.kind === 'sketch' &&
statement.declarations[0].id.type === 'Identifier' &&
statement.declarations[0].id.name === id
)
if (
!sketchStatement ||
sketchStatement.type !== 'VariableDeclaration' ||
sketchStatement.declarations[0].init.type !== 'SketchExpression'
)
throw new Error('No sketch found')
return { function isCallExpression(tokens: Token[], index: number): number {
sketchDeclaration: sketchStatement,
sketchExpression: sketchStatement.declarations[0].init,
index: sketchStatementIndex,
}
}
function isCallExpression(
tokens: Token[],
index: number
): number {
const currentToken = tokens[index] const currentToken = tokens[index]
const veryNextToken = tokens[index + 1] // i.e. no whitespace const veryNextToken = tokens[index + 1] // i.e. no whitespace
if(currentToken.type === 'word' && veryNextToken.type === 'brace' &&veryNextToken.value === '(') { if (
currentToken.type === 'word' &&
veryNextToken.type === 'brace' &&
veryNextToken.value === '('
) {
return findClosingBrace(tokens, index + 1) return findClosingBrace(tokens, index + 1)
} }
return -1 return -1
} }
function debuggerr(tokens: Token[], indexes: number[], msg=''): string { function debuggerr(tokens: Token[], indexes: number[], msg = ''): string {
// return '' // return ''
const sortedIndexes = [...indexes].sort((a, b) => a - b) const sortedIndexes = [...indexes].sort((a, b) => a - b)
const min = Math.min(...indexes) const min = Math.min(...indexes)
@ -1243,10 +1255,19 @@ function debuggerr(tokens: Token[], indexes: number[], msg=''): string {
topString += top topString += top
bottomString += bottom bottomString += bottom
}) })
const debugResult = [`${msg} - debuggerr: ${sortedIndexes}`, topString, bottomString].join( const debugResult = [
'\n' `${msg} - debuggerr: ${sortedIndexes}`,
) topString,
bottomString,
].join('\n')
console.log(debugResult) console.log(debugResult)
return debugResult return debugResult
} }
function getNodeFromPath(node: Program, path: (string | number)[]) {
let currentNode = node as any
for (const pathItem of path) {
currentNode = currentNode[pathItem]
}
return currentNode
}

View File

@ -14,7 +14,8 @@ type GuiModes =
mode: 'sketch' mode: 'sketch'
sketchMode: 'points' sketchMode: 'points'
axis: 'xy' | 'xz' | 'yz' axis: 'xy' | 'xz' | 'yz'
id: string id?: string
pathToNode: (string | number)[]
} }
| { | {
mode: 'sketch' mode: 'sketch'