First pass at handling more than 2 sketches

This commit is contained in:
Pierre Jacquier
2024-11-29 10:09:10 -05:00
parent 0c2cd24bda
commit e984b20664
4 changed files with 34 additions and 48 deletions

View File

@ -586,7 +586,10 @@ export const ModelingMachineProvider = ({
return doesSceneHaveSweepableSketch(kclManager.ast) return doesSceneHaveSweepableSketch(kclManager.ast)
} }
// TODO: check if we need a check like this for loft // TODO: check if we need a check like this for loft
console.log('isSketchPipe(selectionRanges)', isSketchPipe(selectionRanges)) console.log(
'isSketchPipe(selectionRanges)',
isSketchPipe(selectionRanges)
)
// if (!isSketchPipe(selectionRanges)) return false // if (!isSketchPipe(selectionRanges)) return false
const canLoft = canLoftSelection(selectionRanges) const canLoft = canLoftSelection(selectionRanges)

View File

@ -348,53 +348,46 @@ export function extrudeSketch(
export function loftSketches( export function loftSketches(
node: Node<Program>, node: Node<Program>,
pathToNode0: PathToNode, nodePaths: PathToNode[]
pathToNode1: PathToNode,
): ):
| { | {
modifiedAst: Node<Program> modifiedAst: Node<Program>
pathToNode: PathToNode
pathToLoftArg: PathToNode pathToLoftArg: PathToNode
} }
| Error { | Error {
const _node = structuredClone(node) const _node = structuredClone(node)
// TODO: make this whole thing list based const variableDeclarators = []
const _node0 = getNodeFromPath<VariableDeclarator>( const pathsToDeclarations = []
for (const path of nodePaths) {
const node = getNodeFromPath<VariableDeclarator>(
_node, _node,
pathToNode0, path,
'VariableDeclarator' 'VariableDeclarator'
) )
const _node1 = getNodeFromPath<VariableDeclarator>( if (err(node)) {
_node, return node
pathToNode1, }
'VariableDeclarator'
)
if (err(_node0)) return _node0
if (err(_node1)) return _node1
const { node: variableDeclarator0, shallowPath: pathToDecleration0 } = _node0
const { node: variableDeclarator1, shallowPath: pathToDecleration1 } = _node1
const identifiers = createArrayExpression([ const { node: variableDeclarator, shallowPath: pathToDecleration } = node
createIdentifier(variableDeclarator0.id.name), variableDeclarators.push(variableDeclarator)
createIdentifier(variableDeclarator1.id.name), pathsToDeclarations.push(pathToDecleration)
]) }
const loftCall = createCallExpressionStdLib('loft', [
identifiers, const identifiers = createArrayExpression(
// TODO: add others variableDeclarators.map((d) => createIdentifier(d.id.name))
]) )
const loftCall = createCallExpressionStdLib('loft', [identifiers])
// We're not creating a pipe expression, // We're not creating a pipe expression,
// but rather a separate constant for the extrusion // but rather a separate constant for the extrusion
const name = findUniqueName(node, KCL_DEFAULT_CONSTANT_PREFIXES.LOFT) const name = findUniqueName(node, KCL_DEFAULT_CONSTANT_PREFIXES.LOFT)
const VariableDeclaration = createVariableDeclaration(name, loftCall) const VariableDeclaration = createVariableDeclaration(name, loftCall)
// TODO: check these blocks below for 0..n // TODO: check which item lastPath should be here
const sketchIndexInPathToNode = const lastPath = pathsToDeclarations[pathsToDeclarations.length - 1]
pathToDecleration1.findIndex((a) => a[0] === 'body') + 1 const sketchIndexInPathToNode = lastPath.findIndex((a) => a[0] === 'body') + 1
const sketchIndexInBody = pathToDecleration1[ const sketchIndexInBody = lastPath[sketchIndexInPathToNode][0] as number
sketchIndexInPathToNode
][0] as number
_node.body.splice(sketchIndexInBody + 1, 0, VariableDeclaration) _node.body.splice(sketchIndexInBody + 1, 0, VariableDeclaration)
const pathToLoftArg: PathToNode = [ const pathToLoftArg: PathToNode = [
@ -408,7 +401,6 @@ export function loftSketches(
] ]
return { return {
modifiedAst: _node, modifiedAst: _node,
pathToNode: [...pathToNode0.slice(0, -1), [-1, 'index']],
pathToLoftArg, pathToLoftArg,
} }
} }

View File

@ -570,7 +570,7 @@ export function canLoftSelection(selection: Selections) {
// !!isSketchPipe(selection) && // !!isSketchPipe(selection) &&
commonNodes.length > 1 && commonNodes.length > 1 &&
// commonNodes.every((n) => !hasSketchPipeBeenExtruded(n.selection, n.ast)) && // commonNodes.every((n) => !hasSketchPipeBeenExtruded(n.selection, n.ast)) &&
(commonNodes.every((n) => nodeHasClose(n) || nodeHasCircle(n))) && commonNodes.every((n) => nodeHasClose(n) || nodeHasCircle(n)) &&
commonNodes.every((n) => !nodeHasExtrude(n)) commonNodes.every((n) => !nodeHasExtrude(n))
) )
} }

View File

@ -715,19 +715,10 @@ export const modelingMachine = setup({
const { selection } = event.data const { selection } = event.data
let ast = kclManager.ast let ast = kclManager.ast
// TODO: make it all list based // TODO: make it all list based
const pathToNode0 = getNodePathFromSourceRange( const nodePaths = selection.graphSelections.map((s) =>
ast, getNodePathFromSourceRange(ast, s?.codeRef.range)
selection.graphSelections[0]?.codeRef.range
)
const pathToNode1 = getNodePathFromSourceRange(
ast,
selection.graphSelections[1]?.codeRef.range
)
const loftSketchesRes = loftSketches(
ast,
pathToNode0,
pathToNode1,
) )
const loftSketchesRes = loftSketches(ast, nodePaths)
if (trap(loftSketchesRes)) return if (trap(loftSketchesRes)) return
const { modifiedAst, pathToLoftArg } = loftSketchesRes const { modifiedAst, pathToLoftArg } = loftSketchesRes