Fix edit insert order

This commit is contained in:
Pierre Jacquier
2025-03-28 13:18:27 -04:00
parent 8fbdac439b
commit f7501e3cef
2 changed files with 28 additions and 17 deletions

View File

@ -81,13 +81,14 @@ export function getAxisExpressionAndIndex(
export function revolveSketch(
ast: Node<Program>,
pathToSketchNode: PathToNode,
variableName: string | undefined,
angle: Expr = createLiteral(4),
angle: Expr,
axisOrEdge: 'Axis' | 'Edge',
axis: string | undefined,
edge: Selections | undefined,
artifactGraph: ArtifactGraph,
artifact?: Artifact
artifact?: Artifact,
variableName?: string,
insertIndex?: number
):
| {
modifiedAst: Node<Program>
@ -134,10 +135,16 @@ export function revolveSketch(
const name =
variableName ??
findUniqueName(clonedAst, KCL_DEFAULT_CONSTANT_PREFIXES.REVOLVE)
const VariableDeclaration = createVariableDeclaration(name, revolveCall)
const declaration = createVariableDeclaration(name, revolveCall)
let sketchIndexInBody: number | undefined
// If it's an edit flow (no change on selection yet)
if (insertIndex) {
sketchIndexInBody = insertIndex
} else {
const lastSketchNodePath =
orderedSketchNodePaths[orderedSketchNodePaths.length - 1]
let sketchIndexInBody = Number(lastSketchNodePath[1][0])
sketchIndexInBody = Number(lastSketchNodePath[1][0])
if (typeof sketchIndexInBody !== 'number') {
return new Error('expected sketchIndexInBody to be a number')
}
@ -146,8 +153,9 @@ export function revolveSketch(
if (axisIndexIfAxis) {
sketchIndexInBody = Math.max(sketchIndexInBody, axisIndexIfAxis)
}
}
clonedAst.body.splice(sketchIndexInBody + 1, 0, VariableDeclaration)
clonedAst.body.splice(sketchIndexInBody + 1, 0, declaration)
const pathToRevolveArg: PathToNode = [
['body', ''],

View File

@ -1791,6 +1791,7 @@ export const modelingMachine = setup({
const { nodeToEdit, selection, angle, axis, edge, axisOrEdge } = input
let ast = kclManager.ast
let variableName: string | undefined = undefined
let insertIndex: number | undefined = undefined
// If this is an edit flow, first we're going to remove the old extrusion
if (nodeToEdit && typeof nodeToEdit[1][0] === 'number') {
@ -1810,6 +1811,7 @@ export const modelingMachine = setup({
const newBody = [...ast.body]
newBody.splice(nodeToEdit[1][0], 1)
ast.body = newBody
insertIndex = nodeToEdit[1][0]
}
if (
@ -1831,13 +1833,14 @@ export const modelingMachine = setup({
const revolveSketchRes = revolveSketch(
ast,
pathToNode,
variableName,
'variableName' in angle ? angle.variableIdentifierAst : angle.valueAst,
axisOrEdge,
axis,
edge,
engineCommandManager.artifactGraph,
selection.graphSelections[0]?.artifact
selection.graphSelections[0]?.artifact,
variableName,
insertIndex
)
if (trap(revolveSketchRes)) return
const { modifiedAst, pathToRevolveArg } = revolveSketchRes