2025-01-08 20:02:30 -05:00
|
|
|
import {
|
2025-02-15 00:57:04 +11:00
|
|
|
Expr,
|
2025-01-17 14:34:36 -05:00
|
|
|
Artifact,
|
|
|
|
ArtifactGraph,
|
|
|
|
ArtifactId,
|
2025-01-08 20:02:30 -05:00
|
|
|
PathToNode,
|
|
|
|
Program,
|
|
|
|
SourceRange,
|
2025-01-17 14:34:36 -05:00
|
|
|
PathArtifact,
|
|
|
|
PlaneArtifact,
|
|
|
|
WallArtifact,
|
|
|
|
SegmentArtifact,
|
|
|
|
Solid2dArtifact as Solid2D,
|
|
|
|
SweepArtifact,
|
|
|
|
SweepEdge,
|
|
|
|
CapArtifact,
|
|
|
|
EdgeCut,
|
2025-01-08 20:02:30 -05:00
|
|
|
} from 'lang/wasm'
|
2024-08-03 18:08:51 +10:00
|
|
|
import { Models } from '@kittycad/lib'
|
2025-01-27 14:24:28 +01:00
|
|
|
import { getNodePathFromSourceRange } from 'lang/queryAstNodePathUtils'
|
2024-08-03 18:08:51 +10:00
|
|
|
import { err } from 'lib/trap'
|
2025-02-15 00:57:04 +11:00
|
|
|
import { Cap, Plane, Wall } from 'wasm-lib/kcl/bindings/Artifact'
|
|
|
|
import { CapSubType } from 'wasm-lib/kcl/bindings/Artifact'
|
2024-08-03 18:08:51 +10:00
|
|
|
|
2025-01-17 14:34:36 -05:00
|
|
|
export type { Artifact, ArtifactId, SegmentArtifact } from 'lang/wasm'
|
2024-09-04 11:49:13 -04:00
|
|
|
|
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 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
|
|
|
}
|
|
|
|
|
2025-02-15 00:57:04 +11:00
|
|
|
export interface CapArtifactRich extends BaseArtifact {
|
|
|
|
type: 'cap'
|
|
|
|
subType: CapSubType
|
|
|
|
faceCodeRef: CodeRef
|
|
|
|
edgeCuts: Array<EdgeCut>
|
|
|
|
paths: Array<PathArtifact>
|
|
|
|
sweep?: SweepArtifact
|
|
|
|
}
|
|
|
|
export interface WallArtifactRich extends BaseArtifact {
|
|
|
|
type: 'wall'
|
|
|
|
id: ArtifactId
|
|
|
|
segment: PathArtifact
|
|
|
|
edgeCuts: Array<EdgeCut>
|
|
|
|
sweep: SweepArtifact
|
|
|
|
paths: Array<PathArtifact>
|
|
|
|
faceCodeRef: CodeRef
|
|
|
|
}
|
|
|
|
|
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 */
|
2025-02-15 00:57:04 +11:00
|
|
|
plane: PlaneArtifact | WallArtifact | CapArtifact
|
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 */
|
2025-01-17 14:34:36 -05:00
|
|
|
sweep: SweepArtifact | null
|
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-02-15 00:57:04 +11: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-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
|
|
|
}
|
|
|
|
|
|
|
|
export type EngineCommand = Models['WebSocketRequest_type']
|
|
|
|
|
|
|
|
type OkWebSocketResponseData = Models['OkWebSocketResponseData_type']
|
|
|
|
|
|
|
|
export interface ResponseMap {
|
|
|
|
[commandId: string]: OkWebSocketResponseData
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-15 00:57:04 +11:00
|
|
|
export function expandWall(
|
|
|
|
wall: WallArtifact,
|
|
|
|
artifactGraph: ArtifactGraph
|
|
|
|
): WallArtifactRich {
|
|
|
|
const { pathIds, sweepId: _s, edgeCutEdgeIds, ...keptProperties } = wall
|
|
|
|
const paths = pathIds?.length
|
|
|
|
? Array.from(
|
|
|
|
getArtifactsOfTypes(
|
|
|
|
{ keys: wall.pathIds, types: ['path'] },
|
|
|
|
artifactGraph
|
|
|
|
).values()
|
|
|
|
)
|
|
|
|
: []
|
|
|
|
const sweep = artifactGraph.get(wall.sweepId) as SweepArtifact
|
|
|
|
const edgeCuts = edgeCutEdgeIds?.length
|
|
|
|
? Array.from(
|
|
|
|
getArtifactsOfTypes(
|
|
|
|
{ keys: wall.edgeCutEdgeIds, types: ['edgeCut'] },
|
|
|
|
artifactGraph
|
|
|
|
).values()
|
|
|
|
)
|
|
|
|
: []
|
|
|
|
const segment = artifactGraph.get(wall.segId) as PathArtifact
|
|
|
|
return {
|
|
|
|
type: 'wall',
|
|
|
|
...keptProperties,
|
|
|
|
paths,
|
|
|
|
sweep,
|
|
|
|
segment,
|
|
|
|
edgeCuts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export function expandCap(
|
|
|
|
cap: CapArtifact,
|
|
|
|
artifactGraph: ArtifactGraph
|
|
|
|
): CapArtifactRich {
|
|
|
|
const { pathIds, sweepId: _s, edgeCutEdgeIds, ...keptProperties } = cap
|
|
|
|
const paths = pathIds?.length
|
|
|
|
? Array.from(
|
|
|
|
getArtifactsOfTypes(
|
|
|
|
{ keys: cap.pathIds, types: ['path'] },
|
|
|
|
artifactGraph
|
|
|
|
).values()
|
|
|
|
)
|
|
|
|
: []
|
|
|
|
const maybeSweep = getArtifactOfTypes(
|
|
|
|
{ key: cap.sweepId, types: ['sweep'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
const sweep = err(maybeSweep) ? undefined : maybeSweep
|
|
|
|
const edgeCuts = edgeCutEdgeIds?.length
|
|
|
|
? Array.from(
|
|
|
|
getArtifactsOfTypes(
|
|
|
|
{ keys: cap.edgeCutEdgeIds, types: ['edgeCut'] },
|
|
|
|
artifactGraph
|
|
|
|
).values()
|
|
|
|
)
|
|
|
|
: []
|
|
|
|
return {
|
|
|
|
type: 'cap',
|
|
|
|
...keptProperties,
|
|
|
|
paths,
|
|
|
|
sweep,
|
|
|
|
edgeCuts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 18:08:51 +10:00
|
|
|
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
|
|
|
|
)
|
2025-01-17 14:34:36 -05:00
|
|
|
: null
|
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
|
2025-02-15 00:57:04 +11:00
|
|
|
if (!surf) return new Error('Segment does not have a surface')
|
2024-08-03 18:08:51 +10:00
|
|
|
|
|
|
|
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(
|
2025-01-17 14:34:36 -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(
|
2025-01-17 14:34:36 -05:00
|
|
|
{ key: solid2d.pathId, types: ['path'] },
|
2024-08-03 18:08:51 +10:00
|
|
|
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)
|
2025-01-17 14:34:36 -05:00
|
|
|
if (artifact?.type === 'solid2d') {
|
2024-11-21 15:04:30 +11:00
|
|
|
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
|
|
|
|
2025-02-15 00:57:04 +11:00
|
|
|
function getPlaneFromPath(
|
|
|
|
path: PathArtifact,
|
|
|
|
graph: ArtifactGraph
|
|
|
|
): PlaneArtifact | WallArtifact | CapArtifact | Error {
|
|
|
|
const plane = getArtifactOfTypes(
|
|
|
|
{ key: path.planeId, types: ['plane', 'wall', 'cap'] },
|
|
|
|
graph
|
|
|
|
)
|
|
|
|
if (err(plane)) return plane
|
|
|
|
return plane
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPlaneFromSegment(
|
|
|
|
segment: SegmentArtifact,
|
|
|
|
graph: ArtifactGraph
|
|
|
|
): PlaneArtifact | WallArtifact | CapArtifact | Error {
|
|
|
|
const path = getArtifactOfTypes(
|
|
|
|
{ key: segment.pathId, types: ['path'] },
|
|
|
|
graph
|
|
|
|
)
|
|
|
|
if (err(path)) return path
|
|
|
|
return getPlaneFromPath(path, graph)
|
|
|
|
}
|
|
|
|
function getPlaneFromSolid2D(
|
|
|
|
solid2D: Solid2D,
|
|
|
|
graph: ArtifactGraph
|
|
|
|
): PlaneArtifact | WallArtifact | CapArtifact | Error {
|
|
|
|
const path = getArtifactOfTypes(
|
|
|
|
{ key: solid2D.pathId, types: ['path'] },
|
|
|
|
graph
|
|
|
|
)
|
|
|
|
if (err(path)) return path
|
|
|
|
return getPlaneFromPath(path, graph)
|
|
|
|
}
|
|
|
|
function getPlaneFromCap(
|
|
|
|
cap: CapArtifact,
|
|
|
|
graph: ArtifactGraph
|
|
|
|
): PlaneArtifact | WallArtifact | CapArtifact | Error {
|
|
|
|
const sweep = getArtifactOfTypes(
|
|
|
|
{ key: cap.sweepId, types: ['sweep'] },
|
|
|
|
graph
|
|
|
|
)
|
|
|
|
if (err(sweep)) return sweep
|
|
|
|
const path = getArtifactOfTypes({ key: sweep.pathId, types: ['path'] }, graph)
|
|
|
|
if (err(path)) return path
|
|
|
|
return getPlaneFromPath(path, graph)
|
|
|
|
}
|
|
|
|
function getPlaneFromWall(
|
|
|
|
wall: WallArtifact,
|
|
|
|
graph: ArtifactGraph
|
|
|
|
): PlaneArtifact | WallArtifact | CapArtifact | Error {
|
|
|
|
const sweep = getArtifactOfTypes(
|
|
|
|
{ key: wall.sweepId, types: ['sweep'] },
|
|
|
|
graph
|
|
|
|
)
|
|
|
|
if (err(sweep)) return sweep
|
|
|
|
const path = getArtifactOfTypes({ key: sweep.pathId, types: ['path'] }, graph)
|
|
|
|
if (err(path)) return path
|
|
|
|
return getPlaneFromPath(path, graph)
|
|
|
|
}
|
|
|
|
function getPlaneFromSweepEdge(edge: SweepEdge, graph: ArtifactGraph) {
|
|
|
|
const sweep = getArtifactOfTypes(
|
|
|
|
{ key: edge.sweepId, types: ['sweep'] },
|
|
|
|
graph
|
|
|
|
)
|
|
|
|
if (err(sweep)) return sweep
|
|
|
|
const path = getArtifactOfTypes({ key: sweep.pathId, types: ['path'] }, graph)
|
|
|
|
if (err(path)) return path
|
|
|
|
return getPlaneFromPath(path, graph)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPlaneFromArtifact(
|
|
|
|
artifact: Artifact | undefined,
|
|
|
|
graph: ArtifactGraph
|
|
|
|
): PlaneArtifact | WallArtifact | CapArtifact | Error {
|
|
|
|
if (!artifact) return new Error(`Artifact is undefined`)
|
|
|
|
if (artifact.type === 'plane') return artifact
|
|
|
|
if (artifact.type === 'path') return getPlaneFromPath(artifact, graph)
|
|
|
|
if (artifact.type === 'segment') return getPlaneFromSegment(artifact, graph)
|
|
|
|
if (artifact.type === 'solid2d') return getPlaneFromSolid2D(artifact, graph)
|
|
|
|
if (
|
|
|
|
// if the user selects a face with sketch on it (pathIds.length), they probably wanted to edit that sketch,
|
|
|
|
// not the sketch for the underlying sweep sketch
|
|
|
|
(artifact.type === 'wall' || artifact.type === 'cap') &&
|
|
|
|
artifact?.pathIds?.length
|
|
|
|
)
|
|
|
|
return artifact
|
|
|
|
if (artifact.type === 'cap') return getPlaneFromCap(artifact, graph)
|
|
|
|
if (artifact.type === 'wall') return getPlaneFromWall(artifact, graph)
|
|
|
|
if (artifact.type === 'sweepEdge')
|
|
|
|
return getPlaneFromSweepEdge(artifact, graph)
|
|
|
|
return new Error(`Artifact type ${artifact.type} does not have a plane`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const onlyConsecutivePaths = (
|
|
|
|
orderedNodePaths: PathToNode[],
|
|
|
|
originalPath: PathToNode,
|
|
|
|
ast: Program
|
|
|
|
): PathToNode[] => {
|
|
|
|
const isExprSafe = (index: number, ast: Program): boolean => {
|
|
|
|
// we allow expressions between profiles, but only basic math expressions 5 + 6 etc
|
|
|
|
// because 5 + doSomeMath() might be okay, but we can't know if it's an abstraction on a stdlib
|
|
|
|
// call that involves a engine call, and we can't have that in sketch-mode/mock-execution
|
|
|
|
const expr = ast.body?.[index]
|
|
|
|
if (!expr) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (expr.type === 'ImportStatement' || expr.type === 'ReturnStatement') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (expr.type === 'VariableDeclaration') {
|
|
|
|
const init = expr.declaration?.init
|
|
|
|
if (!init) return false
|
|
|
|
if (init.type === 'CallExpression') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (init.type === 'BinaryExpression' && isNodeSafe(init)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if (init.type === 'Literal' || init.type === 'MemberExpression') {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const originalIndex = Number(
|
|
|
|
orderedNodePaths.find(
|
|
|
|
(path) => path[1][0] === originalPath[1][0]
|
|
|
|
)?.[1]?.[0] || 0
|
|
|
|
)
|
|
|
|
|
|
|
|
const minIndex = Number(orderedNodePaths[0][1][0])
|
|
|
|
const maxIndex = Number(orderedNodePaths[orderedNodePaths.length - 1][1][0])
|
|
|
|
const pathIndexMap: any = {}
|
|
|
|
orderedNodePaths.forEach((path) => {
|
|
|
|
const bodyIndex = Number(path[1][0])
|
|
|
|
pathIndexMap[bodyIndex] = path
|
|
|
|
})
|
|
|
|
const safePaths: PathToNode[] = []
|
|
|
|
|
|
|
|
// traverse expressions in either direction from the profile selected
|
|
|
|
// when the user entered sketch mode
|
|
|
|
for (let i = originalIndex; i <= maxIndex; i++) {
|
|
|
|
if (pathIndexMap[i]) {
|
|
|
|
safePaths.push(pathIndexMap[i])
|
|
|
|
} else if (!isExprSafe(i, ast)) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let i = originalIndex - 1; i >= minIndex; i--) {
|
|
|
|
if (pathIndexMap[i]) {
|
|
|
|
safePaths.unshift(pathIndexMap[i])
|
|
|
|
} else if (!isExprSafe(i, ast)) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return safePaths
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPathsFromPlaneArtifact(
|
|
|
|
planeArtifact: PlaneArtifact,
|
|
|
|
artifactGraph: ArtifactGraph,
|
|
|
|
ast: Program
|
|
|
|
): PathToNode[] {
|
|
|
|
const nodePaths: PathToNode[] = []
|
|
|
|
for (const pathId of planeArtifact.pathIds) {
|
|
|
|
const path = artifactGraph.get(pathId)
|
|
|
|
if (!path) continue
|
|
|
|
if ('codeRef' in path && path.codeRef) {
|
|
|
|
// TODO should figure out why upstream the path is bad
|
|
|
|
const isNodePathBad = path.codeRef.pathToNode.length < 2
|
|
|
|
nodePaths.push(
|
|
|
|
isNodePathBad
|
|
|
|
? getNodePathFromSourceRange(ast, path.codeRef.range)
|
|
|
|
: path.codeRef.pathToNode
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return onlyConsecutivePaths(nodePaths, nodePaths[0], ast)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPathsFromArtifact({
|
|
|
|
sketchPathToNode,
|
|
|
|
artifact,
|
|
|
|
artifactGraph,
|
|
|
|
ast,
|
|
|
|
}: {
|
|
|
|
sketchPathToNode: PathToNode
|
|
|
|
artifact?: Artifact
|
|
|
|
artifactGraph: ArtifactGraph
|
|
|
|
ast: Program
|
|
|
|
}): PathToNode[] | Error {
|
|
|
|
const plane = getPlaneFromArtifact(artifact, artifactGraph)
|
|
|
|
if (err(plane)) return plane
|
|
|
|
const paths = getArtifactsOfTypes(
|
|
|
|
{ keys: plane.pathIds, types: ['path'] },
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
let nodePaths = [...paths.values()]
|
|
|
|
.map((path) => path.codeRef.pathToNode)
|
|
|
|
.sort((a, b) => Number(a[1][0]) - Number(b[1][0]))
|
|
|
|
return onlyConsecutivePaths(nodePaths, sketchPathToNode, ast)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isNodeSafe(node: Expr): boolean {
|
|
|
|
if (node.type === 'Literal' || node.type === 'MemberExpression') {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if (node.type === 'BinaryExpression') {
|
|
|
|
return isNodeSafe(node.left) && isNodeSafe(node.right)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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()) {
|
2025-02-15 00:57:04 +11:00
|
|
|
const codeRef = getFaceCodeRef(artifact)
|
|
|
|
if (codeRef) {
|
2024-12-17 15:12:18 -05:00
|
|
|
const match =
|
2025-02-15 00:57:04 +11:00
|
|
|
codeRef?.range[0] === range[0] && codeRef.range[1] === range[1]
|
2024-12-17 15:12:18 -05:00
|
|
|
if (match) return artifact
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
2025-02-15 00:57:04 +11:00
|
|
|
|
|
|
|
export function getFaceCodeRef(
|
|
|
|
artifact: Artifact | Plane | Wall | Cap
|
|
|
|
): CodeRef | null {
|
|
|
|
if ('faceCodeRef' in artifact) {
|
|
|
|
return artifact.faceCodeRef
|
|
|
|
}
|
|
|
|
if ('codeRef' in artifact) {
|
|
|
|
return artifact.codeRef
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|