Refactor Fillet AST Mod to Async Actor (#4803)

This commit is contained in:
max
2025-01-08 16:05:24 +01:00
committed by GitHub
parent 5b1dcfecd6
commit 28b5f7080c
2 changed files with 46 additions and 32 deletions

View File

@ -61,19 +61,18 @@ export interface FilletParameters {
export type EdgeTreatmentParameters = ChamferParameters | FilletParameters
// Apply Edge Treatment (Fillet or Chamfer) To Selection
export function applyEdgeTreatmentToSelection(
export async function applyEdgeTreatmentToSelection(
ast: Node<Program>,
selection: Selections,
parameters: EdgeTreatmentParameters
): void | Error {
): Promise<void | Error> {
// 1. clone and modify with edge treatment and tag
const result = modifyAstWithEdgeTreatmentAndTag(ast, selection, parameters)
if (err(result)) return result
const { modifiedAst, pathToEdgeTreatmentNode } = result
// 2. update ast
// eslint-disable-next-line @typescript-eslint/no-floating-promises
updateAstAndFocus(modifiedAst, pathToEdgeTreatmentNode)
await updateAstAndFocus(modifiedAst, pathToEdgeTreatmentNode)
}
export function modifyAstWithEdgeTreatmentAndTag(
@ -291,7 +290,7 @@ export function getPathToExtrudeForSegmentSelection(
async function updateAstAndFocus(
modifiedAst: Node<Program>,
pathToEdgeTreatmentNode: Array<PathToNode>
) {
): Promise<void> {
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
focusPath: pathToEdgeTreatmentNode,
})

View File

@ -763,30 +763,6 @@ export const modelingMachine = setup({
await codeManager.updateEditorWithAstAndWriteToFile(modifiedAst)
})().catch(reportRejection)
},
'AST fillet': ({ event }) => {
if (event.type !== 'Fillet') return
if (!event.data) return
// Extract inputs
const ast = kclManager.ast
const { selection, radius } = event.data
const parameters: FilletParameters = {
type: EdgeTreatmentType.Fillet,
radius,
}
// Apply fillet to selection
const applyEdgeTreatmentToSelectionResult = applyEdgeTreatmentToSelection(
ast,
selection,
parameters
)
if (err(applyEdgeTreatmentToSelectionResult))
return applyEdgeTreatmentToSelectionResult
// eslint-disable-next-line @typescript-eslint/no-floating-promises
codeManager.updateEditorWithAstAndWriteToFile(kclManager.ast)
},
'set selection filter to curves only': () => {
;(async () => {
await engineCommandManager.sendSceneCommand({
@ -1670,6 +1646,33 @@ export const modelingMachine = setup({
}
}
),
filletAstMod: fromPromise(
async ({
input,
}: {
input: ModelingCommandSchema['Fillet'] | undefined
}) => {
if (!input) {
return new Error('No input provided')
}
// Extract inputs
const ast = kclManager.ast
const { selection, radius } = input
const parameters: FilletParameters = {
type: EdgeTreatmentType.Fillet,
radius,
}
// Apply fillet to selection
const filletResult = await applyEdgeTreatmentToSelection(
ast,
selection,
parameters
)
if (err(filletResult)) return filletResult
}
),
'submit-prompt-edit': fromPromise(
async ({ input }: { input: ModelingCommandSchema['Prompt-to-edit'] }) => {
console.log('doing thing', input)
@ -1745,9 +1748,8 @@ export const modelingMachine = setup({
},
Fillet: {
target: 'idle',
actions: ['AST fillet'],
reenter: false,
target: 'Applying fillet',
reenter: true,
},
Export: {
@ -2553,6 +2555,19 @@ export const modelingMachine = setup({
},
},
'Applying fillet': {
invoke: {
src: 'filletAstMod',
id: 'filletAstMod',
input: ({ event }) => {
if (event.type !== 'Fillet') return undefined
return event.data
},
onDone: ['idle'],
onError: ['idle'],
},
},
'Applying Prompt-to-edit': {
invoke: {
src: 'submit-prompt-edit',