Compare commits

...

1 Commits

Author SHA1 Message Date
259c4df7bc Add default planes into artifactGraph 2024-11-19 18:54:11 -05:00
4 changed files with 110 additions and 3 deletions

View File

@ -2,6 +2,8 @@ import { useMemo } from 'react'
import { engineCommandManager } from 'lib/singletons'
import {
ArtifactGraph,
DefaultPlaneArtifactRich,
expandDefaultPlane,
expandPlane,
PlaneArtifactRich,
} from 'lang/std/artifactGraph'
@ -30,9 +32,11 @@ export function DebugFeatureTree() {
function computeTree(artifactGraph: ArtifactGraph): GenericObj[] {
let items: GenericObj[] = []
const planes: PlaneArtifactRich[] = []
const planes: (PlaneArtifactRich | DefaultPlaneArtifactRich)[] = []
for (const artifact of artifactGraph.values()) {
if (artifact.type === 'plane') {
if (artifact.type === 'defaultPlane') {
planes.push(expandDefaultPlane(artifact, artifactGraph))
} else if (artifact.type === 'plane') {
planes.push(expandPlane(artifact, artifactGraph))
}
}

View File

@ -2,6 +2,9 @@ import { PathToNode, Program, SourceRange } from 'lang/wasm'
import { Models } from '@kittycad/lib'
import { getNodePathFromSourceRange } from 'lang/queryAst'
import { err } from 'lib/trap'
import { DefaultPlaneStr } from 'clientSideScene/sceneEntities'
import { DefaultPlanes } from 'wasm-lib/kcl/bindings/DefaultPlanes'
import { defaultPlaneKeyToKcl, isDefaultPlaneKey } from 'lib/planes'
export type ArtifactId = string
@ -10,6 +13,19 @@ interface CommonCommandProperties {
pathToNode: PathToNode
}
export interface DefaultPlaneArtifact {
type: 'defaultPlane'
pathIds: Array<ArtifactId>
/** The default plane's KCL keyword, e.g. `'XY'` or `'-YZ'` */
planeName: DefaultPlaneStr
}
export interface DefaultPlaneArtifactRich {
type: 'defaultPlane'
paths: Array<PathArtifact>
/** The default plane's KCL keyword, e.g. `'XY'` or `'-YZ'` */
planeName: DefaultPlaneStr
}
export interface PlaneArtifact {
type: 'plane'
pathIds: Array<ArtifactId>
@ -119,6 +135,7 @@ interface EdgeCutEdge {
}
export type Artifact =
| DefaultPlaneArtifact
| PlaneArtifact
| PathArtifact
| SegmentArtifact
@ -149,10 +166,12 @@ export interface OrderedCommand {
* should return data on how to update the map, and not do so directly.
*/
export function createArtifactGraph({
defaultPlanes,
orderedCommands,
responseMap,
ast,
}: {
defaultPlanes?: DefaultPlanes | null
orderedCommands: Array<OrderedCommand>
responseMap: ResponseMap
ast: Program
@ -162,6 +181,18 @@ export function createArtifactGraph({
/** see docstring for {@link getArtifactsToUpdate} as to why this is needed */
let currentPlaneId = ''
// First, populate the map with the default planes
if (defaultPlanes && defaultPlanes !== null) {
Object.entries(defaultPlanes).forEach(([name, id]) => {
if (!isDefaultPlaneKey(name, defaultPlanes)) return
myMap.set(id, {
type: 'defaultPlane',
pathIds: [],
planeName: defaultPlaneKeyToKcl(name),
})
})
}
orderedCommands.forEach((orderedCommand) => {
if (orderedCommand.command?.type === 'modeling_cmd_req') {
if (orderedCommand.command.cmd.type === 'enable_sketch_mode') {
@ -264,7 +295,10 @@ export function getArtifactsToUpdate({
]
} else if (cmd.type === 'enable_sketch_mode') {
const plane = getArtifact(currentPlaneId)
const pathIds = plane?.type === 'plane' ? plane?.pathIds : []
const pathIds =
plane?.type === 'plane' || plane?.type === 'defaultPlane'
? plane?.pathIds
: []
const codeRef =
plane?.type === 'plane' ? plane?.codeRef : { range, pathToNode }
const existingPlane = getArtifact(currentPlaneId)
@ -281,6 +315,17 @@ export function getArtifactsToUpdate({
},
},
]
} else if (existingPlane?.type === 'defaultPlane') {
return [
{
id: currentPlaneId,
artifact: {
type: 'defaultPlane',
pathIds,
planeName: existingPlane.planeName,
},
},
]
} else {
return [
{ id: currentPlaneId, artifact: { type: 'plane', pathIds, codeRef } },
@ -318,6 +363,16 @@ export function getArtifactsToUpdate({
},
})
}
if (plane?.type === 'defaultPlane') {
returnArr.push({
id: currentPlaneId,
artifact: {
type: 'defaultPlane',
pathIds: [id],
planeName: plane.planeName,
},
})
}
return returnArr
} else if (cmd.type === 'extend_path' || cmd.type === 'close_path') {
const pathId = cmd.type === 'extend_path' ? cmd.path : cmd.path_id
@ -580,6 +635,21 @@ export function getArtifactOfTypes<T extends Artifact['type'][]>(
return artifact as Extract<Artifact, { type: T[number] }>
}
export function expandDefaultPlane(
plane: DefaultPlaneArtifact,
artifactGraph: ArtifactGraph
): DefaultPlaneArtifactRich {
const paths = getArtifactsOfTypes(
{ keys: plane.pathIds, types: ['path'] },
artifactGraph
)
return {
type: 'defaultPlane',
paths: Array.from(paths.values()),
planeName: plane.planeName,
}
}
export function expandPlane(
plane: PlaneArtifact,
artifactGraph: ArtifactGraph

View File

@ -2128,6 +2128,7 @@ export class EngineCommandManager extends EventTarget {
async waitForAllCommands() {
await Promise.all(Object.values(this.pendingCommands).map((a) => a.promise))
this.artifactGraph = createArtifactGraph({
defaultPlanes: this.defaultPlanes,
orderedCommands: this.orderedCommands,
responseMap: this.responseMap,
ast: this.getAst(),

32
src/lib/planes.ts Normal file
View File

@ -0,0 +1,32 @@
import { DefaultPlaneStr } from 'clientSideScene/sceneEntities'
import { DefaultPlanes } from 'wasm-lib/kcl/bindings/DefaultPlanes'
/**
* Converts a key of DefaultPlanes to the way it is written in KCL.
*/
export function defaultPlaneKeyToKcl(
key: keyof DefaultPlanes
): DefaultPlaneStr {
switch (key) {
case 'negXy':
return '-XY'
case 'negXz':
return '-XZ'
case 'negYz':
return '-YZ'
case 'xy':
return 'XY'
case 'xz':
return 'XZ'
case 'yz':
return 'YZ'
}
}
/** A simple type guard to verify a string is a keyof DefaultPlanes */
export function isDefaultPlaneKey(
key: string,
planesObj: DefaultPlanes
): key is keyof DefaultPlanes {
return key in planesObj
}