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 { engineCommandManager } from 'lib/singletons'
import { import {
ArtifactGraph, ArtifactGraph,
DefaultPlaneArtifactRich,
expandDefaultPlane,
expandPlane, expandPlane,
PlaneArtifactRich, PlaneArtifactRich,
} from 'lang/std/artifactGraph' } from 'lang/std/artifactGraph'
@ -30,9 +32,11 @@ export function DebugFeatureTree() {
function computeTree(artifactGraph: ArtifactGraph): GenericObj[] { function computeTree(artifactGraph: ArtifactGraph): GenericObj[] {
let items: GenericObj[] = [] let items: GenericObj[] = []
const planes: PlaneArtifactRich[] = [] const planes: (PlaneArtifactRich | DefaultPlaneArtifactRich)[] = []
for (const artifact of artifactGraph.values()) { 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)) planes.push(expandPlane(artifact, artifactGraph))
} }
} }

View File

@ -2,6 +2,9 @@ import { PathToNode, Program, SourceRange } from 'lang/wasm'
import { Models } from '@kittycad/lib' import { Models } from '@kittycad/lib'
import { getNodePathFromSourceRange } from 'lang/queryAst' import { getNodePathFromSourceRange } from 'lang/queryAst'
import { err } from 'lib/trap' 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 export type ArtifactId = string
@ -10,6 +13,19 @@ interface CommonCommandProperties {
pathToNode: PathToNode 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 { export interface PlaneArtifact {
type: 'plane' type: 'plane'
pathIds: Array<ArtifactId> pathIds: Array<ArtifactId>
@ -119,6 +135,7 @@ interface EdgeCutEdge {
} }
export type Artifact = export type Artifact =
| DefaultPlaneArtifact
| PlaneArtifact | PlaneArtifact
| PathArtifact | PathArtifact
| SegmentArtifact | SegmentArtifact
@ -149,10 +166,12 @@ export interface OrderedCommand {
* should return data on how to update the map, and not do so directly. * should return data on how to update the map, and not do so directly.
*/ */
export function createArtifactGraph({ export function createArtifactGraph({
defaultPlanes,
orderedCommands, orderedCommands,
responseMap, responseMap,
ast, ast,
}: { }: {
defaultPlanes?: DefaultPlanes | null
orderedCommands: Array<OrderedCommand> orderedCommands: Array<OrderedCommand>
responseMap: ResponseMap responseMap: ResponseMap
ast: Program ast: Program
@ -162,6 +181,18 @@ export function createArtifactGraph({
/** see docstring for {@link getArtifactsToUpdate} as to why this is needed */ /** see docstring for {@link getArtifactsToUpdate} as to why this is needed */
let currentPlaneId = '' 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) => { orderedCommands.forEach((orderedCommand) => {
if (orderedCommand.command?.type === 'modeling_cmd_req') { if (orderedCommand.command?.type === 'modeling_cmd_req') {
if (orderedCommand.command.cmd.type === 'enable_sketch_mode') { if (orderedCommand.command.cmd.type === 'enable_sketch_mode') {
@ -264,7 +295,10 @@ export function getArtifactsToUpdate({
] ]
} else if (cmd.type === 'enable_sketch_mode') { } else if (cmd.type === 'enable_sketch_mode') {
const plane = getArtifact(currentPlaneId) const plane = getArtifact(currentPlaneId)
const pathIds = plane?.type === 'plane' ? plane?.pathIds : [] const pathIds =
plane?.type === 'plane' || plane?.type === 'defaultPlane'
? plane?.pathIds
: []
const codeRef = const codeRef =
plane?.type === 'plane' ? plane?.codeRef : { range, pathToNode } plane?.type === 'plane' ? plane?.codeRef : { range, pathToNode }
const existingPlane = getArtifact(currentPlaneId) 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 { } else {
return [ return [
{ id: currentPlaneId, artifact: { type: 'plane', pathIds, codeRef } }, { 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 return returnArr
} else if (cmd.type === 'extend_path' || cmd.type === 'close_path') { } else if (cmd.type === 'extend_path' || cmd.type === 'close_path') {
const pathId = cmd.type === 'extend_path' ? cmd.path : cmd.path_id 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] }> 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( export function expandPlane(
plane: PlaneArtifact, plane: PlaneArtifact,
artifactGraph: ArtifactGraph artifactGraph: ArtifactGraph

View File

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