2025-01-08 20:02:30 -05:00
|
|
|
import {
|
|
|
|
ArtifactCommand,
|
|
|
|
ExecState,
|
|
|
|
PathToNode,
|
|
|
|
Program,
|
|
|
|
SourceRange,
|
|
|
|
sourceRangeFromRust,
|
|
|
|
} from 'lang/wasm'
|
2024-08-03 18:08:51 +10:00
|
|
|
import { Models } from '@kittycad/lib'
|
2024-12-16 10:34:11 -05:00
|
|
|
import { getNodePathFromSourceRange } from 'lang/queryAst'
|
2024-08-03 18:08:51 +10:00
|
|
|
import { err } from 'lib/trap'
|
2025-01-08 20:02:30 -05:00
|
|
|
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
2024-08-03 18:08:51 +10:00
|
|
|
|
2024-09-04 11:49:13 -04:00
|
|
|
export type ArtifactId = string
|
|
|
|
|
2024-11-21 15:04:30 +11:00
|
|
|
interface BaseArtifact {
|
|
|
|
id: ArtifactId
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface CodeRef {
|
2024-08-03 18:08:51 +10:00
|
|
|
range: SourceRange
|
|
|
|
pathToNode: PathToNode
|
|
|
|
}
|
|
|
|
|
2024-11-21 15:04:30 +11:00
|
|
|
export interface PlaneArtifact extends BaseArtifact {
|
2024-08-03 18:08:51 +10:00
|
|
|
type: 'plane'
|
2024-09-04 11:49:13 -04:00
|
|
|
pathIds: Array<ArtifactId>
|
2024-11-21 15:04:30 +11:00
|
|
|
codeRef: CodeRef
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
2024-11-21 15:04:30 +11:00
|
|
|
export interface PlaneArtifactRich extends BaseArtifact {
|
2024-08-03 18:08:51 +10:00
|
|
|
type: 'plane'
|
|
|
|
paths: Array<PathArtifact>
|
2024-11-21 15:04:30 +11:00
|
|
|
codeRef: CodeRef
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
|
2024-11-21 15:04:30 +11:00
|
|
|
export interface PathArtifact extends BaseArtifact {
|
2024-08-03 18:08:51 +10:00
|
|
|
type: 'path'
|
2024-09-04 11:49:13 -04:00
|
|
|
planeId: ArtifactId
|
|
|
|
segIds: Array<ArtifactId>
|
2025-01-13 15:02:55 -05:00
|
|
|
sweepId?: ArtifactId
|
2024-09-04 11:49:13 -04:00
|
|
|
solid2dId?: ArtifactId
|
2024-11-21 15:04:30 +11:00
|
|
|
codeRef: CodeRef
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
|
2024-12-16 10:34:11 -05:00
|
|
|
interface solid2D extends BaseArtifact {
|
2024-08-03 18:08:51 +10:00
|
|
|
type: 'solid2D'
|
2024-09-04 11:49:13 -04:00
|
|
|
pathId: ArtifactId
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
2024-11-21 15:04:30 +11:00
|
|
|
export interface PathArtifactRich extends BaseArtifact {
|
2024-08-03 18:08:51 +10:00
|
|
|
type: 'path'
|
2024-11-05 21:32:05 -08:00
|
|
|
/** A path must always lie on a plane */
|
2024-12-16 10:34:11 -05:00
|
|
|
plane: PlaneArtifact | WallArtifact
|
2024-11-05 21:32:05 -08:00
|
|
|
/** A path must always contain 0 or more segments */
|
2024-08-03 18:08:51 +10:00
|
|
|
segments: Array<SegmentArtifact>
|
2024-11-05 21:32:05 -08:00
|
|
|
/** A path may not result in a sweep artifact */
|
|
|
|
sweep?: SweepArtifact
|
2024-11-21 15:04:30 +11:00
|
|
|
codeRef: CodeRef
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
|
2024-11-21 15:04:30 +11:00
|
|
|
export interface SegmentArtifact extends BaseArtifact {
|
2024-08-03 18:08:51 +10:00
|
|
|
type: 'segment'
|
2024-09-04 11:49:13 -04:00
|
|
|
pathId: ArtifactId
|
2025-01-13 15:02:55 -05:00
|
|
|
surfaceId?: ArtifactId
|
2024-09-04 11:49:13 -04:00
|
|
|
edgeIds: Array<ArtifactId>
|
|
|
|
edgeCutId?: ArtifactId
|
2024-11-21 15:04:30 +11:00
|
|
|
codeRef: CodeRef
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
2024-11-21 15:04:30 +11:00
|
|
|
interface SegmentArtifactRich extends BaseArtifact {
|
2024-08-03 18:08:51 +10:00
|
|
|
type: 'segment'
|
|
|
|
path: PathArtifact
|
2025-01-13 15:02:55 -05:00
|
|
|
surf?: WallArtifact
|
2024-12-16 10:34:11 -05:00
|
|
|
edges: Array<SweepEdge>
|
2024-08-03 18:08:51 +10:00
|
|
|
edgeCut?: EdgeCut
|
2024-11-21 15:04:30 +11:00
|
|
|
codeRef: CodeRef
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
|
2024-09-17 13:22:53 -05:00
|
|
|
/** A Sweep is a more generic term for extrude, revolve, loft and sweep*/
|
2024-11-21 15:04:30 +11:00
|
|
|
interface SweepArtifact extends BaseArtifact {
|
2024-09-17 13:22:53 -05:00
|
|
|
type: 'sweep'
|
2025-01-11 08:20:49 -05:00
|
|
|
subType: 'extrusion' | 'revolve' | 'loft' | 'sweep'
|
2024-09-17 13:22:53 -05:00
|
|
|
pathId: string
|
|
|
|
surfaceIds: Array<string>
|
|
|
|
edgeIds: Array<string>
|
2024-11-21 15:04:30 +11:00
|
|
|
codeRef: CodeRef
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
2024-11-21 15:04:30 +11:00
|
|
|
interface SweepArtifactRich extends BaseArtifact {
|
2024-09-17 13:22:53 -05:00
|
|
|
type: 'sweep'
|
2025-01-11 08:20:49 -05:00
|
|
|
subType: 'extrusion' | 'revolve' | 'loft' | 'sweep'
|
2024-08-03 18:08:51 +10:00
|
|
|
path: PathArtifact
|
|
|
|
surfaces: Array<WallArtifact | CapArtifact>
|
2024-12-16 10:34:11 -05:00
|
|
|
edges: Array<SweepEdge>
|
2024-11-21 15:04:30 +11:00
|
|
|
codeRef: CodeRef
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
|
2024-11-21 15:04:30 +11:00
|
|
|
interface WallArtifact extends BaseArtifact {
|
2024-08-03 18:08:51 +10:00
|
|
|
type: 'wall'
|
2024-09-04 11:49:13 -04:00
|
|
|
segId: ArtifactId
|
|
|
|
edgeCutEdgeIds: Array<ArtifactId>
|
2024-09-17 13:22:53 -05:00
|
|
|
sweepId: ArtifactId
|
2024-09-04 11:49:13 -04:00
|
|
|
pathIds: Array<ArtifactId>
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
2024-11-21 15:04:30 +11:00
|
|
|
interface CapArtifact extends BaseArtifact {
|
2024-08-03 18:08:51 +10:00
|
|
|
type: 'cap'
|
|
|
|
subType: 'start' | 'end'
|
2024-09-04 11:49:13 -04:00
|
|
|
edgeCutEdgeIds: Array<ArtifactId>
|
2024-09-17 13:22:53 -05:00
|
|
|
sweepId: ArtifactId
|
2024-09-04 11:49:13 -04:00
|
|
|
pathIds: Array<ArtifactId>
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
|
2024-12-16 10:34:11 -05:00
|
|
|
interface SweepEdge extends BaseArtifact {
|
2024-09-17 13:22:53 -05:00
|
|
|
type: 'sweepEdge'
|
2024-09-04 11:49:13 -04:00
|
|
|
segId: ArtifactId
|
2024-09-17 13:22:53 -05:00
|
|
|
sweepId: ArtifactId
|
2024-08-30 19:46:48 +10:00
|
|
|
subType: 'opposite' | 'adjacent'
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/** A edgeCut is a more generic term for both fillet or chamfer */
|
2024-11-21 15:04:30 +11:00
|
|
|
interface EdgeCut extends BaseArtifact {
|
2024-08-03 18:08:51 +10:00
|
|
|
type: 'edgeCut'
|
|
|
|
subType: 'fillet' | 'chamfer'
|
2024-09-04 11:49:13 -04:00
|
|
|
consumedEdgeId: ArtifactId
|
|
|
|
edgeIds: Array<ArtifactId>
|
2025-01-13 15:02:55 -05:00
|
|
|
surfaceId?: ArtifactId
|
2024-11-21 15:04:30 +11:00
|
|
|
codeRef: CodeRef
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
|
2024-11-21 15:04:30 +11:00
|
|
|
interface EdgeCutEdge extends BaseArtifact {
|
2024-08-03 18:08:51 +10:00
|
|
|
type: 'edgeCutEdge'
|
2024-09-04 11:49:13 -04:00
|
|
|
edgeCutId: ArtifactId
|
|
|
|
surfaceId: ArtifactId
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
export type Artifact =
|
|
|
|
| PlaneArtifact
|
|
|
|
| PathArtifact
|
|
|
|
| SegmentArtifact
|
2024-09-17 13:22:53 -05:00
|
|
|
| SweepArtifact
|
2024-08-03 18:08:51 +10:00
|
|
|
| WallArtifact
|
|
|
|
| CapArtifact
|
2024-12-16 10:34:11 -05:00
|
|
|
| SweepEdge
|
2024-08-03 18:08:51 +10:00
|
|
|
| EdgeCut
|
|
|
|
| EdgeCutEdge
|
2024-12-16 10:34:11 -05:00
|
|
|
| solid2D
|
2024-08-03 18:08:51 +10:00
|
|
|
|
2024-09-04 11:49:13 -04:00
|
|
|
export type ArtifactGraph = Map<ArtifactId, Artifact>
|
2024-08-03 18:08:51 +10:00
|
|
|
|
|
|
|
export type EngineCommand = Models['WebSocketRequest_type']
|
|
|
|
|
|
|
|
type OkWebSocketResponseData = Models['OkWebSocketResponseData_type']
|
|
|
|
|
|
|
|
export interface ResponseMap {
|
|
|
|
[commandId: string]: OkWebSocketResponseData
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Creates a graph of artifacts from a list of ordered commands and their responses
|
|
|
|
* muting the Map should happen entirely this function, other functions called within
|
|
|
|
* should return data on how to update the map, and not do so directly.
|
|
|
|
*/
|
|
|
|
export function createArtifactGraph({
|
2025-01-08 20:02:30 -05:00
|
|
|
artifactCommands,
|
2024-08-03 18:08:51 +10:00
|
|
|
responseMap,
|
|
|
|
ast,
|
2025-01-08 20:02:30 -05:00
|
|
|
execStateArtifacts,
|
2024-08-03 18:08:51 +10:00
|
|
|
}: {
|
2025-01-08 20:02:30 -05:00
|
|
|
artifactCommands: Array<ArtifactCommand>
|
2024-08-03 18:08:51 +10:00
|
|
|
responseMap: ResponseMap
|
2025-01-08 20:02:30 -05:00
|
|
|
ast: Node<Program>
|
|
|
|
execStateArtifacts: ExecState['artifacts']
|
2024-08-03 18:08:51 +10:00
|
|
|
}) {
|
2024-09-04 11:49:13 -04:00
|
|
|
const myMap = new Map<ArtifactId, Artifact>()
|
2024-08-03 18:08:51 +10:00
|
|
|
|
|
|
|
/** see docstring for {@link getArtifactsToUpdate} as to why this is needed */
|
|
|
|
let currentPlaneId = ''
|
|
|
|
|
2025-01-08 20:02:30 -05:00
|
|
|
for (const artifactCommand of artifactCommands) {
|
|
|
|
if (artifactCommand.command.type === 'enable_sketch_mode') {
|
|
|
|
currentPlaneId = artifactCommand.command.entity_id
|
|
|
|
}
|
|
|
|
if (artifactCommand.command.type === 'sketch_mode_disable') {
|
|
|
|
currentPlaneId = ''
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
const artifactsToUpdate = getArtifactsToUpdate({
|
2025-01-08 20:02:30 -05:00
|
|
|
artifactCommand,
|
2024-08-03 18:08:51 +10:00
|
|
|
responseMap,
|
2024-09-04 11:49:13 -04:00
|
|
|
getArtifact: (id: ArtifactId) => myMap.get(id),
|
2024-08-03 18:08:51 +10:00
|
|
|
currentPlaneId,
|
|
|
|
ast,
|
2025-01-08 20:02:30 -05:00
|
|
|
execStateArtifacts,
|
2024-08-03 18:08:51 +10:00
|
|
|
})
|
|
|
|
artifactsToUpdate.forEach(({ id, artifact }) => {
|
|
|
|
const mergedArtifact = mergeArtifacts(myMap.get(id), artifact)
|
|
|
|
myMap.set(id, mergedArtifact)
|
|
|
|
})
|
2025-01-08 20:02:30 -05:00
|
|
|
}
|
2024-08-03 18:08:51 +10:00
|
|
|
return myMap
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Merges two artifacts, since our artifacts only contain strings and arrays of string for values we coerce that
|
|
|
|
* but maybe types can be improved here.
|
|
|
|
*/
|
|
|
|
function mergeArtifacts(
|
|
|
|
oldArtifact: Artifact | undefined,
|
|
|
|
newArtifact: Artifact
|
|
|
|
): Artifact {
|
|
|
|
// only has string and array of strings
|
|
|
|
interface GenericArtifact {
|
|
|
|
[key: string]: string | Array<string>
|
|
|
|
}
|
|
|
|
if (!oldArtifact) return newArtifact
|
|
|
|
// merging artifacts of different types should never happen, but if it does, just return the new artifact
|
|
|
|
if (oldArtifact.type !== newArtifact.type) return newArtifact
|
|
|
|
const _oldArtifact = oldArtifact as any as GenericArtifact
|
|
|
|
const mergedArtifact = { ...oldArtifact, ...newArtifact } as GenericArtifact
|
|
|
|
Object.entries(newArtifact as any as GenericArtifact).forEach(
|
|
|
|
([propName, value]) => {
|
|
|
|
const otherValue = _oldArtifact[propName]
|
|
|
|
if (Array.isArray(value) && Array.isArray(otherValue)) {
|
|
|
|
mergedArtifact[propName] = [...new Set([...otherValue, ...value])]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return mergedArtifact as any as Artifact
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes a single command and it's response in order to populate the artifact map
|
|
|
|
* It does not mutate the map directly, but returns an array of artifacts to update
|
|
|
|
*
|
|
|
|
* @param currentPlaneId is only needed for `start_path` commands because this command does not have a pathId
|
|
|
|
* instead it relies on the id used with the `enable_sketch_mode` command, so this much be kept track of
|
|
|
|
* outside of this function. It would be good to update the `start_path` command to include the planeId so we
|
|
|
|
* can remove this.
|
|
|
|
*/
|
|
|
|
export function getArtifactsToUpdate({
|
2025-01-08 20:02:30 -05:00
|
|
|
artifactCommand,
|
2024-08-03 18:08:51 +10:00
|
|
|
getArtifact,
|
|
|
|
responseMap,
|
|
|
|
currentPlaneId,
|
|
|
|
ast,
|
2025-01-08 20:02:30 -05:00
|
|
|
execStateArtifacts,
|
2024-08-03 18:08:51 +10:00
|
|
|
}: {
|
2025-01-08 20:02:30 -05:00
|
|
|
artifactCommand: ArtifactCommand
|
2024-08-03 18:08:51 +10:00
|
|
|
responseMap: ResponseMap
|
|
|
|
/** Passing in a getter because we don't wan this function to update the map directly */
|
2024-09-04 11:49:13 -04:00
|
|
|
getArtifact: (id: ArtifactId) => Artifact | undefined
|
|
|
|
currentPlaneId: ArtifactId
|
2025-01-08 20:02:30 -05:00
|
|
|
ast: Node<Program>
|
|
|
|
execStateArtifacts: ExecState['artifacts']
|
2024-08-03 18:08:51 +10:00
|
|
|
}): Array<{
|
2024-09-04 11:49:13 -04:00
|
|
|
id: ArtifactId
|
2024-08-03 18:08:51 +10:00
|
|
|
artifact: Artifact
|
|
|
|
}> {
|
2025-01-08 20:02:30 -05:00
|
|
|
const range = sourceRangeFromRust(artifactCommand.range)
|
2024-08-03 18:08:51 +10:00
|
|
|
const pathToNode = getNodePathFromSourceRange(ast, range)
|
|
|
|
|
2025-01-08 20:02:30 -05:00
|
|
|
const id = artifactCommand.cmdId
|
2024-08-03 18:08:51 +10:00
|
|
|
const response = responseMap[id]
|
2025-01-08 20:02:30 -05:00
|
|
|
const cmd = artifactCommand.command
|
2024-08-03 18:08:51 +10:00
|
|
|
const returnArr: ReturnType<typeof getArtifactsToUpdate> = []
|
2024-08-05 12:04:53 +10:00
|
|
|
if (!response) return returnArr
|
2024-11-18 16:25:25 -05:00
|
|
|
if (cmd.type === 'make_plane' && range[1] !== 0) {
|
|
|
|
// If we're calling `make_plane` and the code range doesn't end at `0`
|
|
|
|
// it's not a default plane, but a custom one from the offsetPlane standard library function
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
id,
|
|
|
|
artifact: {
|
|
|
|
type: 'plane',
|
2024-11-21 15:04:30 +11:00
|
|
|
id,
|
2024-11-18 16:25:25 -05:00
|
|
|
pathIds: [],
|
|
|
|
codeRef: { range, pathToNode },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
} else if (cmd.type === 'enable_sketch_mode') {
|
2024-08-03 18:08:51 +10:00
|
|
|
const plane = getArtifact(currentPlaneId)
|
|
|
|
const pathIds = plane?.type === 'plane' ? plane?.pathIds : []
|
|
|
|
const codeRef =
|
|
|
|
plane?.type === 'plane' ? plane?.codeRef : { range, pathToNode }
|
|
|
|
const existingPlane = getArtifact(currentPlaneId)
|
|
|
|
if (existingPlane?.type === 'wall') {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
id: currentPlaneId,
|
|
|
|
artifact: {
|
|
|
|
type: 'wall',
|
2024-11-21 15:04:30 +11:00
|
|
|
id: currentPlaneId,
|
2024-08-03 18:08:51 +10:00
|
|
|
segId: existingPlane.segId,
|
|
|
|
edgeCutEdgeIds: existingPlane.edgeCutEdgeIds,
|
2024-09-17 13:22:53 -05:00
|
|
|
sweepId: existingPlane.sweepId,
|
2024-08-03 18:08:51 +10:00
|
|
|
pathIds: existingPlane.pathIds,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
} else {
|
|
|
|
return [
|
2024-11-21 15:04:30 +11:00
|
|
|
{
|
|
|
|
id: currentPlaneId,
|
|
|
|
artifact: { type: 'plane', id: currentPlaneId, pathIds, codeRef },
|
|
|
|
},
|
2024-08-03 18:08:51 +10:00
|
|
|
]
|
|
|
|
}
|
|
|
|
} else if (cmd.type === 'start_path') {
|
|
|
|
returnArr.push({
|
|
|
|
id,
|
|
|
|
artifact: {
|
|
|
|
type: 'path',
|
2024-11-21 15:04:30 +11:00
|
|
|
id,
|
2024-08-03 18:08:51 +10:00
|
|
|
segIds: [],
|
|
|
|
planeId: currentPlaneId,
|
2025-01-13 15:02:55 -05:00
|
|
|
sweepId: undefined,
|
2024-08-03 18:08:51 +10:00
|
|
|
codeRef: { range, pathToNode },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const plane = getArtifact(currentPlaneId)
|
|
|
|
const codeRef =
|
|
|
|
plane?.type === 'plane' ? plane?.codeRef : { range, pathToNode }
|
|
|
|
if (plane?.type === 'plane') {
|
|
|
|
returnArr.push({
|
|
|
|
id: currentPlaneId,
|
2024-11-21 15:04:30 +11:00
|
|
|
artifact: { type: 'plane', id: currentPlaneId, pathIds: [id], codeRef },
|
2024-08-03 18:08:51 +10:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if (plane?.type === 'wall') {
|
|
|
|
returnArr.push({
|
|
|
|
id: currentPlaneId,
|
|
|
|
artifact: {
|
|
|
|
type: 'wall',
|
2024-11-21 15:04:30 +11:00
|
|
|
id: currentPlaneId,
|
2024-08-03 18:08:51 +10:00
|
|
|
segId: plane.segId,
|
|
|
|
edgeCutEdgeIds: plane.edgeCutEdgeIds,
|
2024-09-17 13:22:53 -05:00
|
|
|
sweepId: plane.sweepId,
|
2024-08-03 18:08:51 +10:00
|
|
|
pathIds: [id],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return returnArr
|
|
|
|
} else if (cmd.type === 'extend_path' || cmd.type === 'close_path') {
|
|
|
|
const pathId = cmd.type === 'extend_path' ? cmd.path : cmd.path_id
|
|
|
|
returnArr.push({
|
|
|
|
id,
|
|
|
|
artifact: {
|
|
|
|
type: 'segment',
|
2024-11-21 15:04:30 +11:00
|
|
|
id,
|
2024-08-03 18:08:51 +10:00
|
|
|
pathId,
|
2025-01-13 15:02:55 -05:00
|
|
|
surfaceId: undefined,
|
2024-08-03 18:08:51 +10:00
|
|
|
edgeIds: [],
|
|
|
|
codeRef: { range, pathToNode },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const path = getArtifact(pathId)
|
|
|
|
if (path?.type === 'path')
|
|
|
|
returnArr.push({
|
|
|
|
id: pathId,
|
|
|
|
artifact: { ...path, segIds: [id] },
|
|
|
|
})
|
|
|
|
if (
|
2024-08-05 12:04:53 +10:00
|
|
|
response?.type === 'modeling' &&
|
2024-08-03 18:08:51 +10:00
|
|
|
response.data.modeling_response.type === 'close_path'
|
|
|
|
) {
|
|
|
|
returnArr.push({
|
|
|
|
id: response.data.modeling_response.data.face_id,
|
2024-11-21 15:04:30 +11:00
|
|
|
artifact: {
|
|
|
|
type: 'solid2D',
|
|
|
|
id: response.data.modeling_response.data.face_id,
|
|
|
|
pathId,
|
|
|
|
},
|
2024-08-03 18:08:51 +10:00
|
|
|
})
|
|
|
|
const path = getArtifact(pathId)
|
|
|
|
if (path?.type === 'path')
|
|
|
|
returnArr.push({
|
|
|
|
id: pathId,
|
|
|
|
artifact: {
|
|
|
|
...path,
|
|
|
|
solid2dId: response.data.modeling_response.data.face_id,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return returnArr
|
2025-01-11 08:20:49 -05:00
|
|
|
} else if (
|
|
|
|
cmd.type === 'extrude' ||
|
|
|
|
cmd.type === 'revolve' ||
|
|
|
|
cmd.type === 'sweep'
|
|
|
|
) {
|
2024-09-17 13:22:53 -05:00
|
|
|
const subType = cmd.type === 'extrude' ? 'extrusion' : cmd.type
|
2024-08-03 18:08:51 +10:00
|
|
|
returnArr.push({
|
|
|
|
id,
|
|
|
|
artifact: {
|
2024-09-17 13:22:53 -05:00
|
|
|
type: 'sweep',
|
|
|
|
subType: subType,
|
2024-11-21 15:04:30 +11:00
|
|
|
id,
|
2024-08-03 18:08:51 +10:00
|
|
|
pathId: cmd.target,
|
|
|
|
surfaceIds: [],
|
|
|
|
edgeIds: [],
|
|
|
|
codeRef: { range, pathToNode },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const path = getArtifact(cmd.target)
|
|
|
|
if (path?.type === 'path')
|
|
|
|
returnArr.push({
|
|
|
|
id: cmd.target,
|
2024-09-17 13:22:53 -05:00
|
|
|
artifact: { ...path, sweepId: id },
|
2024-08-03 18:08:51 +10:00
|
|
|
})
|
|
|
|
return returnArr
|
2025-01-09 15:36:50 -05:00
|
|
|
} else if (
|
|
|
|
cmd.type === 'loft' &&
|
|
|
|
response.type === 'modeling' &&
|
|
|
|
response.data.modeling_response.type === 'loft'
|
|
|
|
) {
|
|
|
|
returnArr.push({
|
|
|
|
id,
|
|
|
|
artifact: {
|
|
|
|
type: 'sweep',
|
|
|
|
subType: 'loft',
|
|
|
|
id,
|
|
|
|
// TODO: make sure to revisit this choice, don't think it matters for now
|
|
|
|
pathId: cmd.section_ids[0],
|
|
|
|
surfaceIds: [],
|
|
|
|
edgeIds: [],
|
|
|
|
codeRef: { range, pathToNode },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
for (const sectionId of cmd.section_ids) {
|
|
|
|
const path = getArtifact(sectionId)
|
|
|
|
if (path?.type === 'path')
|
|
|
|
returnArr.push({
|
|
|
|
id: sectionId,
|
|
|
|
artifact: { ...path, sweepId: id },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return returnArr
|
2024-08-03 18:08:51 +10:00
|
|
|
} else if (
|
|
|
|
cmd.type === 'solid3d_get_extrusion_face_info' &&
|
|
|
|
response?.type === 'modeling' &&
|
|
|
|
response.data.modeling_response.type === 'solid3d_get_extrusion_face_info'
|
|
|
|
) {
|
|
|
|
let lastPath: PathArtifact
|
|
|
|
response.data.modeling_response.data.faces.forEach(
|
|
|
|
({ curve_id, cap, face_id }) => {
|
|
|
|
if (cap === 'none' && curve_id && face_id) {
|
|
|
|
const seg = getArtifact(curve_id)
|
|
|
|
if (seg?.type !== 'segment') return
|
|
|
|
const path = getArtifact(seg.pathId)
|
|
|
|
if (path?.type === 'path' && seg?.type === 'segment') {
|
|
|
|
lastPath = path
|
|
|
|
returnArr.push({
|
|
|
|
id: face_id,
|
|
|
|
artifact: {
|
|
|
|
type: 'wall',
|
2024-11-21 15:04:30 +11:00
|
|
|
id: face_id,
|
2024-08-03 18:08:51 +10:00
|
|
|
segId: curve_id,
|
|
|
|
edgeCutEdgeIds: [],
|
2025-01-13 15:02:55 -05:00
|
|
|
// TODO: Add explicit check for sweepId. Should never use ''
|
|
|
|
sweepId: path.sweepId ?? '',
|
2024-08-03 18:08:51 +10:00
|
|
|
pathIds: [],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
returnArr.push({
|
|
|
|
id: curve_id,
|
|
|
|
artifact: { ...seg, surfaceId: face_id },
|
|
|
|
})
|
2025-01-13 15:02:55 -05:00
|
|
|
if (path.sweepId) {
|
|
|
|
const sweep = getArtifact(path.sweepId)
|
|
|
|
if (sweep?.type === 'sweep') {
|
|
|
|
returnArr.push({
|
|
|
|
id: path.sweepId,
|
|
|
|
artifact: {
|
|
|
|
...sweep,
|
|
|
|
surfaceIds: [face_id],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response.data.modeling_response.data.faces.forEach(({ cap, face_id }) => {
|
|
|
|
if ((cap === 'top' || cap === 'bottom') && face_id) {
|
|
|
|
const path = lastPath
|
|
|
|
if (path?.type === 'path') {
|
|
|
|
returnArr.push({
|
|
|
|
id: face_id,
|
|
|
|
artifact: {
|
|
|
|
type: 'cap',
|
2024-11-21 15:04:30 +11:00
|
|
|
id: face_id,
|
2024-08-03 18:08:51 +10:00
|
|
|
subType: cap === 'bottom' ? 'start' : 'end',
|
|
|
|
edgeCutEdgeIds: [],
|
2025-01-13 15:02:55 -05:00
|
|
|
// TODO: Add explicit check for sweepId. Should never use ''
|
|
|
|
sweepId: path.sweepId ?? '',
|
2024-08-03 18:08:51 +10:00
|
|
|
pathIds: [],
|
|
|
|
},
|
|
|
|
})
|
2025-01-13 15:02:55 -05:00
|
|
|
if (path.sweepId) {
|
|
|
|
const sweep = getArtifact(path.sweepId)
|
|
|
|
if (sweep?.type !== 'sweep') return
|
|
|
|
returnArr.push({
|
|
|
|
id: path.sweepId,
|
|
|
|
artifact: {
|
|
|
|
...sweep,
|
|
|
|
surfaceIds: [face_id],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return returnArr
|
2024-08-30 19:46:48 +10:00
|
|
|
} else if (
|
|
|
|
// is opposite edge
|
|
|
|
(cmd.type === 'solid3d_get_opposite_edge' &&
|
|
|
|
response.type === 'modeling' &&
|
|
|
|
response.data.modeling_response.type === 'solid3d_get_opposite_edge' &&
|
|
|
|
response.data.modeling_response.data.edge) ||
|
|
|
|
// or is adjacent edge
|
2024-09-26 18:25:05 +10:00
|
|
|
(cmd.type === 'solid3d_get_next_adjacent_edge' &&
|
2024-08-30 19:46:48 +10:00
|
|
|
response.type === 'modeling' &&
|
|
|
|
response.data.modeling_response.type ===
|
2024-09-26 18:25:05 +10:00
|
|
|
'solid3d_get_next_adjacent_edge' &&
|
2024-08-30 19:46:48 +10:00
|
|
|
response.data.modeling_response.data.edge)
|
|
|
|
) {
|
|
|
|
const wall = getArtifact(cmd.face_id)
|
|
|
|
if (wall?.type !== 'wall') return returnArr
|
2024-09-17 13:22:53 -05:00
|
|
|
const sweep = getArtifact(wall.sweepId)
|
|
|
|
if (sweep?.type !== 'sweep') return returnArr
|
|
|
|
const path = getArtifact(sweep.pathId)
|
2024-08-30 19:46:48 +10:00
|
|
|
if (path?.type !== 'path') return returnArr
|
|
|
|
const segment = getArtifact(cmd.edge_id)
|
|
|
|
if (segment?.type !== 'segment') return returnArr
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
id: response.data.modeling_response.data.edge,
|
|
|
|
artifact: {
|
2024-09-17 13:22:53 -05:00
|
|
|
type: 'sweepEdge',
|
2024-11-21 15:04:30 +11:00
|
|
|
id: response.data.modeling_response.data.edge,
|
2024-08-30 19:46:48 +10:00
|
|
|
subType:
|
2024-09-26 18:25:05 +10:00
|
|
|
cmd.type === 'solid3d_get_next_adjacent_edge'
|
2024-08-30 19:46:48 +10:00
|
|
|
? 'adjacent'
|
|
|
|
: 'opposite',
|
|
|
|
segId: cmd.edge_id,
|
2025-01-13 15:02:55 -05:00
|
|
|
// TODO: Add explicit check for sweepId. Should never use ''
|
|
|
|
sweepId: path.sweepId ?? '',
|
2024-08-30 19:46:48 +10:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: cmd.edge_id,
|
|
|
|
artifact: {
|
|
|
|
...segment,
|
|
|
|
edgeIds: [response.data.modeling_response.data.edge],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2025-01-13 15:02:55 -05:00
|
|
|
id: sweep.id,
|
2024-08-30 19:46:48 +10:00
|
|
|
artifact: {
|
2024-09-17 13:22:53 -05:00
|
|
|
...sweep,
|
2024-08-30 19:46:48 +10:00
|
|
|
edgeIds: [response.data.modeling_response.data.edge],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
2024-08-03 18:08:51 +10:00
|
|
|
} else if (cmd.type === 'solid3d_fillet_edge') {
|
|
|
|
returnArr.push({
|
|
|
|
id,
|
|
|
|
artifact: {
|
|
|
|
type: 'edgeCut',
|
2024-11-21 15:04:30 +11:00
|
|
|
id,
|
2024-08-03 18:08:51 +10:00
|
|
|
subType: cmd.cut_type,
|
|
|
|
consumedEdgeId: cmd.edge_id,
|
|
|
|
edgeIds: [],
|
2025-01-13 15:02:55 -05:00
|
|
|
surfaceId: undefined,
|
2024-08-03 18:08:51 +10:00
|
|
|
codeRef: { range, pathToNode },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const consumedEdge = getArtifact(cmd.edge_id)
|
|
|
|
if (consumedEdge?.type === 'segment') {
|
|
|
|
returnArr.push({
|
|
|
|
id: cmd.edge_id,
|
|
|
|
artifact: { ...consumedEdge, edgeCutId: id },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return returnArr
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
/** filter map items of a specific type */
|
|
|
|
export function filterArtifacts<T extends Artifact['type'][]>(
|
|
|
|
{
|
|
|
|
types,
|
|
|
|
predicate,
|
|
|
|
}: {
|
|
|
|
types: T
|
|
|
|
predicate?: (value: Extract<Artifact, { type: T[number] }>) => boolean
|
|
|
|
},
|
|
|
|
map: ArtifactGraph
|
|
|
|
) {
|
|
|
|
return new Map(
|
|
|
|
Array.from(map).filter(
|
|
|
|
([_, value]) =>
|
|
|
|
types.includes(value.type) &&
|
|
|
|
(!predicate ||
|
|
|
|
predicate(value as Extract<Artifact, { type: T[number] }>))
|
|
|
|
)
|
2024-09-04 11:49:13 -04:00
|
|
|
) as Map<ArtifactId, Extract<Artifact, { type: T[number] }>>
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getArtifactsOfTypes<T extends Artifact['type'][]>(
|
|
|
|
{
|
|
|
|
keys,
|
|
|
|
types,
|
|
|
|
predicate,
|
|
|
|
}: {
|
|
|
|
keys: string[]
|
|
|
|
types: T
|
|
|
|
predicate?: (value: Extract<Artifact, { type: T[number] }>) => boolean
|
|
|
|
},
|
|
|
|
map: ArtifactGraph
|
2024-09-04 11:49:13 -04:00
|
|
|
): Map<ArtifactId, Extract<Artifact, { type: T[number] }>> {
|
2024-08-03 18:08:51 +10:00
|
|
|
return new Map(
|
|
|
|
[...map].filter(
|
|
|
|
([key, value]) =>
|
|
|
|
keys.includes(key) &&
|
|
|
|
types.includes(value.type) &&
|
|
|
|
(!predicate ||
|
|
|
|
predicate(value as Extract<Artifact, { type: T[number] }>))
|
|
|
|
)
|
2024-09-04 11:49:13 -04:00
|
|
|
) as Map<ArtifactId, Extract<Artifact, { type: T[number] }>>
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getArtifactOfTypes<T extends Artifact['type'][]>(
|
|
|
|
{
|
|
|
|
key,
|
|
|
|
types,
|
|
|
|
}: {
|
2024-09-04 11:49:13 -04:00
|
|
|
key: ArtifactId
|
2024-08-03 18:08:51 +10:00
|
|
|
types: T
|
|
|
|
},
|
|
|
|
map: ArtifactGraph
|
|
|
|
): Extract<Artifact, { type: T[number] }> | Error {
|
|
|
|
const artifact = map.get(key)
|
|
|
|
if (!artifact) return new Error(`No artifact found with key ${key}`)
|
|
|
|
if (!types.includes(artifact?.type))
|
|
|
|
return new Error(`Expected ${types} but got ${artifact?.type}`)
|
|
|
|
return artifact as Extract<Artifact, { type: T[number] }>
|
|
|
|
}
|
|
|
|
|
|
|
|
export function expandPlane(
|
|
|
|
plane: PlaneArtifact,
|
|
|
|
artifactGraph: ArtifactGraph
|
|
|
|
): PlaneArtifactRich {
|
|
|
|
const paths = getArtifactsOfTypes(
|
|
|
|
{ keys: plane.pathIds, types: ['path'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
type: 'plane',
|
2024-11-21 15:04:30 +11:00
|
|
|
id: plane.id,
|
2024-08-03 18:08:51 +10:00
|
|
|
paths: Array.from(paths.values()),
|
|
|
|
codeRef: plane.codeRef,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function expandPath(
|
|
|
|
path: PathArtifact,
|
|
|
|
artifactGraph: ArtifactGraph
|
|
|
|
): PathArtifactRich | Error {
|
|
|
|
const segs = getArtifactsOfTypes(
|
|
|
|
{ keys: path.segIds, types: ['segment'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
2024-11-05 21:32:05 -08:00
|
|
|
const sweep = path.sweepId
|
|
|
|
? getArtifactOfTypes(
|
|
|
|
{
|
|
|
|
key: path.sweepId,
|
|
|
|
types: ['sweep'],
|
|
|
|
},
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
: undefined
|
2024-08-03 18:08:51 +10:00
|
|
|
const plane = getArtifactOfTypes(
|
|
|
|
{ key: path.planeId, types: ['plane', 'wall'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
2024-09-17 13:22:53 -05:00
|
|
|
if (err(sweep)) return sweep
|
2024-08-03 18:08:51 +10:00
|
|
|
if (err(plane)) return plane
|
|
|
|
return {
|
|
|
|
type: 'path',
|
2024-11-21 15:04:30 +11:00
|
|
|
id: path.id,
|
2024-08-03 18:08:51 +10:00
|
|
|
segments: Array.from(segs.values()),
|
2024-09-17 13:22:53 -05:00
|
|
|
sweep,
|
2024-08-03 18:08:51 +10:00
|
|
|
plane,
|
|
|
|
codeRef: path.codeRef,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-17 13:22:53 -05:00
|
|
|
export function expandSweep(
|
|
|
|
sweep: SweepArtifact,
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph: ArtifactGraph
|
2024-09-17 13:22:53 -05:00
|
|
|
): SweepArtifactRich | Error {
|
2024-08-03 18:08:51 +10:00
|
|
|
const surfs = getArtifactsOfTypes(
|
2024-09-17 13:22:53 -05:00
|
|
|
{ keys: sweep.surfaceIds, types: ['wall', 'cap'] },
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
const edges = getArtifactsOfTypes(
|
2024-09-17 13:22:53 -05:00
|
|
|
{ keys: sweep.edgeIds, types: ['sweepEdge'] },
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
const path = getArtifactOfTypes(
|
2024-09-17 13:22:53 -05:00
|
|
|
{ key: sweep.pathId, types: ['path'] },
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
if (err(path)) return path
|
|
|
|
return {
|
2024-09-17 13:22:53 -05:00
|
|
|
type: 'sweep',
|
2024-11-12 15:49:49 -05:00
|
|
|
subType: sweep.subType,
|
2024-11-21 15:04:30 +11:00
|
|
|
id: sweep.id,
|
2024-08-03 18:08:51 +10:00
|
|
|
surfaces: Array.from(surfs.values()),
|
|
|
|
edges: Array.from(edges.values()),
|
|
|
|
path,
|
2024-09-17 13:22:53 -05:00
|
|
|
codeRef: sweep.codeRef,
|
2024-08-03 18:08:51 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function expandSegment(
|
|
|
|
segment: SegmentArtifact,
|
|
|
|
artifactGraph: ArtifactGraph
|
|
|
|
): SegmentArtifactRich | Error {
|
|
|
|
const path = getArtifactOfTypes(
|
|
|
|
{ key: segment.pathId, types: ['path'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
2025-01-13 15:02:55 -05:00
|
|
|
const surf = segment.surfaceId
|
|
|
|
? getArtifactOfTypes(
|
|
|
|
{ key: segment.surfaceId, types: ['wall'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
: undefined
|
2024-08-03 18:08:51 +10:00
|
|
|
const edges = getArtifactsOfTypes(
|
2024-09-17 13:22:53 -05:00
|
|
|
{ keys: segment.edgeIds, types: ['sweepEdge'] },
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
const edgeCut = segment.edgeCutId
|
|
|
|
? getArtifactOfTypes(
|
|
|
|
{ key: segment.edgeCutId, types: ['edgeCut'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
: undefined
|
|
|
|
if (err(path)) return path
|
|
|
|
if (err(surf)) return surf
|
|
|
|
if (err(edgeCut)) return edgeCut
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'segment',
|
2024-11-21 15:04:30 +11:00
|
|
|
id: segment.id,
|
2024-08-03 18:08:51 +10:00
|
|
|
path,
|
|
|
|
surf,
|
|
|
|
edges: Array.from(edges.values()),
|
|
|
|
edgeCut: edgeCut,
|
|
|
|
codeRef: segment.codeRef,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCapCodeRef(
|
|
|
|
cap: CapArtifact,
|
|
|
|
artifactGraph: ArtifactGraph
|
2024-11-21 15:04:30 +11:00
|
|
|
): CodeRef | Error {
|
2024-09-17 13:22:53 -05:00
|
|
|
const sweep = getArtifactOfTypes(
|
|
|
|
{ key: cap.sweepId, types: ['sweep'] },
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph
|
|
|
|
)
|
2024-09-17 13:22:53 -05:00
|
|
|
if (err(sweep)) return sweep
|
2024-08-03 18:08:51 +10:00
|
|
|
const path = getArtifactOfTypes(
|
2024-09-17 13:22:53 -05:00
|
|
|
{ key: sweep.pathId, types: ['path'] },
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
if (err(path)) return path
|
|
|
|
return path.codeRef
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSolid2dCodeRef(
|
2024-12-16 10:34:11 -05:00
|
|
|
solid2D: solid2D,
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph: ArtifactGraph
|
2024-11-21 15:04:30 +11:00
|
|
|
): CodeRef | Error {
|
2024-08-03 18:08:51 +10:00
|
|
|
const path = getArtifactOfTypes(
|
|
|
|
{ key: solid2D.pathId, types: ['path'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
if (err(path)) return path
|
|
|
|
return path.codeRef
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getWallCodeRef(
|
|
|
|
wall: WallArtifact,
|
|
|
|
artifactGraph: ArtifactGraph
|
2024-11-21 15:04:30 +11:00
|
|
|
): CodeRef | Error {
|
2024-08-03 18:08:51 +10:00
|
|
|
const seg = getArtifactOfTypes(
|
|
|
|
{ key: wall.segId, types: ['segment'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
if (err(seg)) return seg
|
|
|
|
return seg.codeRef
|
|
|
|
}
|
|
|
|
|
2024-09-17 13:22:53 -05:00
|
|
|
export function getSweepEdgeCodeRef(
|
2024-12-16 10:34:11 -05:00
|
|
|
edge: SweepEdge,
|
2024-08-30 19:46:48 +10:00
|
|
|
artifactGraph: ArtifactGraph
|
2024-11-21 15:04:30 +11:00
|
|
|
): CodeRef | Error {
|
2024-08-30 19:46:48 +10:00
|
|
|
const seg = getArtifactOfTypes(
|
|
|
|
{ key: edge.segId, types: ['segment'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
if (err(seg)) return seg
|
|
|
|
return seg.codeRef
|
|
|
|
}
|
2024-11-21 15:04:30 +11:00
|
|
|
export function getEdgeCutConsumedCodeRef(
|
2024-09-26 18:25:05 +10:00
|
|
|
edge: EdgeCut,
|
|
|
|
artifactGraph: ArtifactGraph
|
2024-11-21 15:04:30 +11:00
|
|
|
): CodeRef | Error {
|
2024-09-26 18:25:05 +10:00
|
|
|
const seg = getArtifactOfTypes(
|
|
|
|
{ key: edge.consumedEdgeId, types: ['segment', 'sweepEdge'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
if (err(seg)) return seg
|
|
|
|
if (seg.type === 'segment') return seg.codeRef
|
|
|
|
return getSweepEdgeCodeRef(seg, artifactGraph)
|
|
|
|
}
|
2024-08-30 19:46:48 +10:00
|
|
|
|
2024-09-17 13:22:53 -05:00
|
|
|
export function getSweepFromSuspectedSweepSurface(
|
2024-09-04 11:49:13 -04:00
|
|
|
id: ArtifactId,
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph: ArtifactGraph
|
2024-09-17 13:22:53 -05:00
|
|
|
): SweepArtifact | Error {
|
2024-08-03 18:08:51 +10:00
|
|
|
const artifact = getArtifactOfTypes(
|
2024-09-26 18:25:05 +10:00
|
|
|
{ key: id, types: ['wall', 'cap', 'edgeCut'] },
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
if (err(artifact)) return artifact
|
2024-09-26 18:25:05 +10:00
|
|
|
if (artifact.type === 'wall' || artifact.type === 'cap') {
|
|
|
|
return getArtifactOfTypes(
|
|
|
|
{ key: artifact.sweepId, types: ['sweep'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const segOrEdge = getArtifactOfTypes(
|
|
|
|
{ key: artifact.consumedEdgeId, types: ['segment', 'sweepEdge'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
if (err(segOrEdge)) return segOrEdge
|
|
|
|
if (segOrEdge.type === 'segment') {
|
|
|
|
const path = getArtifactOfTypes(
|
|
|
|
{ key: segOrEdge.pathId, types: ['path'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
if (err(path)) return path
|
2025-01-13 15:02:55 -05:00
|
|
|
if (!path.sweepId) return new Error('Path does not have a sweepId')
|
2024-09-26 18:25:05 +10:00
|
|
|
return getArtifactOfTypes(
|
|
|
|
{ key: path.sweepId, types: ['sweep'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
}
|
2024-08-03 18:08:51 +10:00
|
|
|
return getArtifactOfTypes(
|
2024-09-26 18:25:05 +10:00
|
|
|
{ key: segOrEdge.sweepId, types: ['sweep'] },
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-09-17 13:22:53 -05:00
|
|
|
export function getSweepFromSuspectedPath(
|
2024-09-04 11:49:13 -04:00
|
|
|
id: ArtifactId,
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph: ArtifactGraph
|
2024-09-17 13:22:53 -05:00
|
|
|
): SweepArtifact | Error {
|
2024-08-03 18:08:51 +10:00
|
|
|
const path = getArtifactOfTypes({ key: id, types: ['path'] }, artifactGraph)
|
|
|
|
if (err(path)) return path
|
2025-01-13 15:02:55 -05:00
|
|
|
if (!path.sweepId) return new Error('Path does not have a sweepId')
|
2024-08-03 18:08:51 +10:00
|
|
|
return getArtifactOfTypes(
|
2024-09-17 13:22:53 -05:00
|
|
|
{ key: path.sweepId, types: ['sweep'] },
|
2024-08-03 18:08:51 +10:00
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
}
|
2024-11-21 15:04:30 +11:00
|
|
|
|
|
|
|
export function getCodeRefsByArtifactId(
|
|
|
|
id: string,
|
|
|
|
artifactGraph: ArtifactGraph
|
|
|
|
): Array<CodeRef> | null {
|
|
|
|
const artifact = artifactGraph.get(id)
|
|
|
|
if (artifact?.type === 'solid2D') {
|
|
|
|
const codeRef = getSolid2dCodeRef(artifact, artifactGraph)
|
|
|
|
if (err(codeRef)) return null
|
|
|
|
return [codeRef]
|
|
|
|
} else if (artifact?.type === 'cap') {
|
|
|
|
const codeRef = getCapCodeRef(artifact, artifactGraph)
|
|
|
|
if (err(codeRef)) return null
|
|
|
|
return [codeRef]
|
|
|
|
} else if (artifact?.type === 'wall') {
|
|
|
|
const extrusion = getSweepFromSuspectedSweepSurface(id, artifactGraph)
|
|
|
|
const codeRef = getWallCodeRef(artifact, artifactGraph)
|
|
|
|
if (err(codeRef)) return null
|
|
|
|
return err(extrusion) ? [codeRef] : [codeRef, extrusion.codeRef]
|
|
|
|
} else if (artifact?.type === 'sweepEdge') {
|
|
|
|
const codeRef = getSweepEdgeCodeRef(artifact, artifactGraph)
|
|
|
|
if (err(codeRef)) return null
|
|
|
|
return [codeRef]
|
|
|
|
} else if (artifact?.type === 'segment') {
|
|
|
|
return [artifact.codeRef]
|
|
|
|
} else if (artifact?.type === 'edgeCut') {
|
|
|
|
const codeRef = artifact.codeRef
|
|
|
|
const consumedCodeRef = getEdgeCutConsumedCodeRef(artifact, artifactGraph)
|
|
|
|
if (err(consumedCodeRef)) return [codeRef]
|
|
|
|
return [codeRef, consumedCodeRef]
|
|
|
|
} else if (artifact && 'codeRef' in artifact) {
|
|
|
|
return [artifact.codeRef]
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function codeRefFromRange(range: SourceRange, ast: Program): CodeRef {
|
|
|
|
return {
|
|
|
|
range,
|
|
|
|
pathToNode: getNodePathFromSourceRange(ast, range),
|
|
|
|
}
|
|
|
|
}
|
2024-12-17 15:12:18 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an artifact from a code source range
|
|
|
|
*/
|
|
|
|
export function getArtifactFromRange(
|
|
|
|
range: SourceRange,
|
|
|
|
artifactGraph: ArtifactGraph
|
|
|
|
): Artifact | null {
|
|
|
|
for (const artifact of artifactGraph.values()) {
|
|
|
|
if ('codeRef' in artifact) {
|
|
|
|
const match =
|
2024-12-20 16:19:59 -05:00
|
|
|
artifact.codeRef?.range[0] === range[0] &&
|
2024-12-17 15:12:18 -05:00
|
|
|
artifact.codeRef.range[1] === range[1]
|
|
|
|
if (match) return artifact
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|