add button for extruding sketches

This commit is contained in:
Kurt Hutten IrevDev
2023-01-06 12:45:34 +11:00
parent 82f4616032
commit 0515acf459
2 changed files with 135 additions and 4 deletions

View File

@ -1,10 +1,17 @@
import { useStore } from './useStore'
import { extrudeSketch } from './lang/modifyAst'
import { getNodePathFromSourceRange } from './lang/abstractSyntaxTree'
export const Toolbar = () => {
const { setGuiMode, guiMode } = useStore(({ guiMode, setGuiMode }) => ({
guiMode,
setGuiMode,
}))
const { setGuiMode, guiMode, selectionRange, ast, updateAst } = useStore(
({ guiMode, setGuiMode, selectionRange, ast, updateAst }) => ({
guiMode,
setGuiMode,
selectionRange,
ast,
updateAst,
})
)
return (
<div>
{guiMode.mode === 'default' && (
@ -36,6 +43,32 @@ export const Toolbar = () => {
EditSketch
</button>
)}
{guiMode.mode === 'canEditSketch' && (
<>
<button
onClick={() => {
if (!ast) return
const pathToNote = getNodePathFromSourceRange(ast, selectionRange)
const { modifiedAst } = extrudeSketch(ast, pathToNote)
updateAst(modifiedAst)
}}
className="border m-1 px-1 rounded"
>
ExtrudeSketch
</button>
<button
onClick={() => {
if (!ast) return
const pathToNote = getNodePathFromSourceRange(ast, selectionRange)
const { modifiedAst } = extrudeSketch(ast, pathToNote, false)
updateAst(modifiedAst)
}}
className="border m-1 px-1 rounded"
>
ExtrudeSketch (w/o pipe)
</button>
</>
)}
{guiMode.mode !== 'default' && (
<button

View File

@ -8,6 +8,7 @@ import {
ExpressionStatement,
Value,
getNodeFromPath,
VariableDeclarator,
} from './abstractSyntaxTree'
export function addSketchTo(
@ -277,3 +278,100 @@ export function changeArguments(
pathToNode,
}
}
export function extrudeSketch(
node: Program,
pathToNode: (string | number)[],
shouldPipe = true
): { modifiedAst: Program; pathToNode: (string | number)[] } {
const _node = { ...node }
const dumbyStartend = { start: 0, end: 0 }
const sketchExpression = getNodeFromPath(
_node,
pathToNode,
'SketchExpression'
) as SketchExpression
// determine if sketchExpression is in a pipeExpression or not
const pipeExpression = getNodeFromPath(_node, pathToNode, 'PipeExpression')
const isInPipeExpression = pipeExpression.type === 'PipeExpression'
const variableDeclorator = getNodeFromPath(
_node,
pathToNode,
'VariableDeclarator'
) as VariableDeclarator
const extrudeCall: CallExpression = {
type: 'CallExpression',
...dumbyStartend,
callee: {
type: 'Identifier',
...dumbyStartend,
name: 'extrude',
},
optional: false,
arguments: [
{
type: 'Literal',
...dumbyStartend,
value: 4,
raw: '4',
},
shouldPipe
? {
type: 'PipeSubstitution',
...dumbyStartend,
}
: {
type: 'Identifier',
...dumbyStartend,
name: variableDeclorator.id.name,
},
],
}
if (shouldPipe) {
const pipeChain: PipeExpression = isInPipeExpression
? {
type: 'PipeExpression',
...dumbyStartend,
body: [...pipeExpression.body, extrudeCall],
}
: {
type: 'PipeExpression',
...dumbyStartend,
body: [sketchExpression, extrudeCall],
}
variableDeclorator.init = pipeChain
return {
modifiedAst: _node,
pathToNode,
}
}
const name = findUniqueName(node, 'part')
const VariableDeclaration: VariableDeclaration = {
type: 'VariableDeclaration',
...dumbyStartend,
declarations: [
{
type: 'VariableDeclarator',
...dumbyStartend,
id: {
type: 'Identifier',
...dumbyStartend,
name,
},
init: extrudeCall,
},
],
kind: 'const',
}
const showCallIndex = getShowIndex(_node)
_node.body.splice(showCallIndex, 0, VariableDeclaration)
return {
modifiedAst: addToShow(_node, name),
pathToNode: [...pathToNode.slice(0, -1), showCallIndex],
}
}