Skip to content

Commit

Permalink
Merge branch 'main' into achalmers/kw-fn-sketches
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchalmers committed Feb 4, 2025
2 parents 6f53220 + 25ad603 commit 130e225
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 27 deletions.
6 changes: 5 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ export function App() {

// Generate thumbnail.png when loading the app
useEffect(() => {
if (!capturedCanvas && lastCommandType === 'execution-done') {
if (
isDesktop() &&
!capturedCanvas &&
lastCommandType === 'execution-done'
) {
setTimeout(() => {
const projectDirectoryWithoutEndingSlash = loaderData?.project?.path
if (!projectDirectoryWithoutEndingSlash) {
Expand Down
24 changes: 21 additions & 3 deletions src/lang/modifyAst/addEdgeTreatment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import { getNodePathFromSourceRange } from 'lang/queryAstNodePathUtils'
import { createLiteral } from 'lang/modifyAst'
import { err } from 'lib/trap'
import { Selection, Selections } from 'lib/selections'
import { engineCommandManager, kclManager } from 'lib/singletons'
import {
codeManager,
editorManager,
engineCommandManager,
kclManager,
} from 'lib/singletons'
import { VITE_KC_DEV_TOKEN } from 'env'
import { isOverlap } from 'lib/utils'
import { codeRefFromRange } from 'lang/std/artifactGraph'
Expand Down Expand Up @@ -56,6 +61,13 @@ afterAll(() => {
engineCommandManager.tearDown()
})

const dependencies = {
kclManager,
engineCommandManager,
editorManager,
codeManager,
}

const runGetPathToExtrudeForSegmentSelectionTest = async (
code: string,
selectedSegmentSnippet: string,
Expand Down Expand Up @@ -134,7 +146,8 @@ const runGetPathToExtrudeForSegmentSelectionTest = async (
const pathResult = getPathToExtrudeForSegmentSelection(
ast,
selection,
artifactGraph
artifactGraph,
dependencies
)
if (err(pathResult)) return pathResult
const { pathToExtrudeNode } = pathResult
Expand Down Expand Up @@ -292,7 +305,12 @@ const runModifyAstCloneWithEdgeTreatmentAndTag = async (
}

// apply edge treatment to selection
const result = modifyAstWithEdgeTreatmentAndTag(ast, selection, parameters)
const result = modifyAstWithEdgeTreatmentAndTag(
ast,
selection,
parameters,
dependencies
)
if (err(result)) {
return result
}
Expand Down
74 changes: 54 additions & 20 deletions src/lang/modifyAst/addEdgeTreatment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ import { err, trap } from 'lib/trap'
import { Selection, Selections } from 'lib/selections'
import { KclCommandValue } from 'lib/commandTypes'
import { Artifact, getSweepFromSuspectedPath } from 'lang/std/artifactGraph'
import {
kclManager,
engineCommandManager,
editorManager,
codeManager,
} from 'lib/singletons'
import { Node } from 'wasm-lib/kcl/bindings/Node'
import { findKwArg } from 'lang/util'
import { KclManager } from 'lang/KclSingleton'
import { EngineCommandManager } from 'lang/std/engineConnection'
import EditorManager from 'editor/manager'
import CodeManager from 'lang/codeManager'

// Edge Treatment Types
export enum EdgeTreatmentType {
Expand All @@ -66,21 +64,38 @@ export type EdgeTreatmentParameters = ChamferParameters | FilletParameters
export async function applyEdgeTreatmentToSelection(
ast: Node<Program>,
selection: Selections,
parameters: EdgeTreatmentParameters
parameters: EdgeTreatmentParameters,
dependencies: {
kclManager: KclManager
engineCommandManager: EngineCommandManager
editorManager: EditorManager
codeManager: CodeManager
}
): Promise<void | Error> {
// 1. clone and modify with edge treatment and tag
const result = modifyAstWithEdgeTreatmentAndTag(ast, selection, parameters)
const result = modifyAstWithEdgeTreatmentAndTag(
ast,
selection,
parameters,
dependencies
)
if (err(result)) return result
const { modifiedAst, pathToEdgeTreatmentNode } = result

// 2. update ast
await updateAstAndFocus(modifiedAst, pathToEdgeTreatmentNode)
await updateAstAndFocus(modifiedAst, pathToEdgeTreatmentNode, dependencies)
}

export function modifyAstWithEdgeTreatmentAndTag(
ast: Node<Program>,
selections: Selections,
parameters: EdgeTreatmentParameters
parameters: EdgeTreatmentParameters,
dependencies: {
kclManager: KclManager
engineCommandManager: EngineCommandManager
editorManager: EditorManager
codeManager: CodeManager
}
):
| { modifiedAst: Node<Program>; pathToEdgeTreatmentNode: Array<PathToNode> }
| Error {
Expand All @@ -90,7 +105,7 @@ export function modifyAstWithEdgeTreatmentAndTag(
const astResult = insertParametersIntoAst(clonedAst, parameters)
if (err(astResult)) return astResult

const artifactGraph = engineCommandManager.artifactGraph
const artifactGraph = dependencies.engineCommandManager.artifactGraph

// Step 1: modify ast with tags and group them by extrude nodes (bodies)
const extrudeToTagsMap: Map<
Expand All @@ -103,7 +118,8 @@ export function modifyAstWithEdgeTreatmentAndTag(
const result = getPathToExtrudeForSegmentSelection(
clonedAstForGetExtrude,
selection,
artifactGraph
artifactGraph,
dependencies
)
if (err(result)) return result
const { pathToSegmentNode, pathToExtrudeNode } = result
Expand Down Expand Up @@ -259,7 +275,13 @@ function insertParametersIntoAst(
export function getPathToExtrudeForSegmentSelection(
ast: Program,
selection: Selection,
artifactGraph: ArtifactGraph
artifactGraph: ArtifactGraph,
dependencies: {
kclManager: KclManager
engineCommandManager: EngineCommandManager
editorManager: EditorManager
codeManager: CodeManager
}
): { pathToSegmentNode: PathToNode; pathToExtrudeNode: PathToNode } | Error {
const pathToSegmentNode = getNodePathFromSourceRange(
ast,
Expand All @@ -275,7 +297,7 @@ export function getPathToExtrudeForSegmentSelection(
const sketchVar = varDecNode.node.declaration.id.name

const sketch = sketchFromKclValue(
kclManager.programMemory.get(sketchVar),
dependencies.kclManager.programMemory.get(sketchVar),
sketchVar
)
if (trap(sketch)) return sketch
Expand All @@ -294,16 +316,28 @@ export function getPathToExtrudeForSegmentSelection(

async function updateAstAndFocus(
modifiedAst: Node<Program>,
pathToEdgeTreatmentNode: Array<PathToNode>
pathToEdgeTreatmentNode: Array<PathToNode>,
dependencies: {
kclManager: KclManager
engineCommandManager: EngineCommandManager
editorManager: EditorManager
codeManager: CodeManager
}
): Promise<void> {
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
focusPath: pathToEdgeTreatmentNode,
})
const updatedAst = await dependencies.kclManager.updateAst(
modifiedAst,
true,
{
focusPath: pathToEdgeTreatmentNode,
}
)

await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst)
await dependencies.codeManager.updateEditorWithAstAndWriteToFile(
updatedAst.newAst
)

if (updatedAst?.selections) {
editorManager.selectRange(updatedAst?.selections)
dependencies.editorManager.selectRange(updatedAst?.selections)
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/lang/modifyAst/addShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,28 @@ import {
createVariableDeclaration,
} from 'lang/modifyAst'
import { KCL_DEFAULT_CONSTANT_PREFIXES } from 'lib/constants'
import { KclManager } from 'lang/KclSingleton'
import { EngineCommandManager } from 'lang/std/engineConnection'
import EditorManager from 'editor/manager'
import CodeManager from 'lang/codeManager'

export function addShell({
node,
selection,
artifactGraph,
thickness,
dependencies,
}: {
node: Node<Program>
selection: Selections
artifactGraph: ArtifactGraph
thickness: Expr
dependencies: {
kclManager: KclManager
engineCommandManager: EngineCommandManager
editorManager: EditorManager
codeManager: CodeManager
}
}): Error | { modifiedAst: Node<Program>; pathToNode: PathToNode } {
const modifiedAst = structuredClone(node)

Expand All @@ -42,7 +53,8 @@ export function addShell({
const extrudeLookupResult = getPathToExtrudeForSegmentSelection(
clonedAstForGetExtrude,
graphSelection,
artifactGraph
artifactGraph,
dependencies
)
if (err(extrudeLookupResult)) {
return new Error("Couldn't find extrude")
Expand Down
25 changes: 23 additions & 2 deletions src/machines/modelingMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,12 @@ export const modelingMachine = setup({
// Extract inputs
const ast = kclManager.ast
const { selection, thickness } = input
const dependencies = {
kclManager,
engineCommandManager,
editorManager,
codeManager,
}

// Insert the thickness variable if it exists
if (
Expand All @@ -1705,6 +1711,7 @@ export const modelingMachine = setup({
'variableName' in thickness
? thickness.variableIdentifierAst
: thickness.valueAst,
dependencies,
})
if (err(shellResult)) {
return err(shellResult)
Expand Down Expand Up @@ -1744,12 +1751,19 @@ export const modelingMachine = setup({
type: EdgeTreatmentType.Fillet,
radius,
}
const dependencies = {
kclManager,
engineCommandManager,
editorManager,
codeManager,
}

// Apply fillet to selection
const filletResult = await applyEdgeTreatmentToSelection(
ast,
selection,
parameters
parameters,
dependencies
)
if (err(filletResult)) return filletResult
}
Expand All @@ -1771,12 +1785,19 @@ export const modelingMachine = setup({
type: EdgeTreatmentType.Chamfer,
length,
}
const dependencies = {
kclManager,
engineCommandManager,
editorManager,
codeManager,
}

// Apply chamfer to selection
const chamferResult = await applyEdgeTreatmentToSelection(
ast,
selection,
parameters
parameters,
dependencies
)
if (err(chamferResult)) return chamferResult
}
Expand Down

0 comments on commit 130e225

Please sign in to comment.