Zoom out on extruded object (#2819)

This commit is contained in:
49fl
2024-07-04 01:19:24 -04:00
committed by GitHub
parent 08e9fe2e52
commit 1257ec0327
3 changed files with 52 additions and 3 deletions

View File

@ -15,6 +15,7 @@ import {
ProgramMemory, ProgramMemory,
recast, recast,
SketchGroup, SketchGroup,
SourceRange,
ExtrudeGroup, ExtrudeGroup,
} from 'lang/wasm' } from 'lang/wasm'
import { getNodeFromPath } from './queryAst' import { getNodeFromPath } from './queryAst'
@ -194,7 +195,11 @@ export class KclManager {
async executeAst( async executeAst(
ast: Program = this._ast, ast: Program = this._ast,
zoomToFit?: boolean, zoomToFit?: boolean,
executionId?: number executionId?: number,
zoomOnRangeAndType?: {
range: SourceRange
type: string
}
): Promise<void> { ): Promise<void> {
await this?.engineCommandManager?.waitForReady await this?.engineCommandManager?.waitForReady
const currentExecutionId = executionId || Date.now() const currentExecutionId = executionId || Date.now()
@ -218,12 +223,20 @@ export class KclManager {
defaultSelectionFilter(programMemory, this.engineCommandManager) defaultSelectionFilter(programMemory, this.engineCommandManager)
if (zoomToFit) { if (zoomToFit) {
let zoomObjectId: string | undefined = ''
if (zoomOnRangeAndType) {
zoomObjectId = this.engineCommandManager?.mapRangeToObjectId(
zoomOnRangeAndType.range,
zoomOnRangeAndType.type
)
}
await this.engineCommandManager.sendSceneCommand({ await this.engineCommandManager.sendSceneCommand({
type: 'modeling_cmd_req', type: 'modeling_cmd_req',
cmd_id: uuidv4(), cmd_id: uuidv4(),
cmd: { cmd: {
type: 'zoom_to_fit', type: 'zoom_to_fit',
object_ids: [], // leave empty to zoom to all objects object_ids: zoomObjectId ? [zoomObjectId] : [], // leave empty to zoom to all objects
padding: 0.1, // padding around the objects padding: 0.1, // padding around the objects
}, },
}) })
@ -357,6 +370,11 @@ export class KclManager {
execute: boolean, execute: boolean,
optionalParams?: { optionalParams?: {
focusPath?: PathToNode focusPath?: PathToNode
zoomToFit?: boolean
zoomOnRangeAndType?: {
range: SourceRange
type: string
}
} }
): Promise<{ ): Promise<{
newAst: Program newAst: Program
@ -400,7 +418,12 @@ export class KclManager {
codeManager.updateCodeEditor(newCode) codeManager.updateCodeEditor(newCode)
// Write the file to disk. // Write the file to disk.
await codeManager.writeToFile() await codeManager.writeToFile()
await this.executeAst(astWithUpdatedSource) await this.executeAst(
astWithUpdatedSource,
optionalParams?.zoomToFit,
undefined,
optionalParams?.zoomOnRangeAndType
)
} else { } else {
// When we don't re-execute, we still want to update the program // When we don't re-execute, we still want to update the program
// memory with the new ast. So we will hit the mock executor // memory with the new ast. So we will hit the mock executor

View File

@ -2084,4 +2084,25 @@ export class EngineCommandManager extends EventTarget {
setScaleGridVisibility(visible: boolean) { setScaleGridVisibility(visible: boolean) {
this.modifyGrid(!visible) this.modifyGrid(!visible)
} }
// Some "objects" have the same source range, such as sketch_mode_start and start_path.
// So when passing a range, we need to also specify the command type
mapRangeToObjectId(
range: SourceRange,
commandTypeToTarget: string
): string | undefined {
const values = Object.entries(this.artifactMap)
for (const [id, data] of values) {
if (data.type !== 'result') continue
// Our range selection seems to just select the cursor position, so either
// of these can be right...
if (
(data.range[0] === range[0] || data.range[1] === range[1]) &&
data.commandType === commandTypeToTarget
)
return id
}
return undefined
}
} }

View File

@ -1012,6 +1012,11 @@ export const modelingMachine = createMachine(
const updatedAst = await kclManager.updateAst(modifiedAst, true, { const updatedAst = await kclManager.updateAst(modifiedAst, true, {
focusPath: pathToExtrudeArg, focusPath: pathToExtrudeArg,
zoomToFit: true,
zoomOnRangeAndType: {
range: selection.codeBasedSelections[0].range,
type: 'start_path',
},
}) })
if (updatedAst?.selections) { if (updatedAst?.selections) {
editorManager.selectRange(updatedAst?.selections) editorManager.selectRange(updatedAst?.selections)