Allow multiple profiles in the same sketch (#5196)

* Revert "Revert multi-profile (#4812)"

This reverts commit efe8089b08.

* fix poor 1000ms wait UX

* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu-latest-8-cores)

* trigger CI

* Add Rust side artifacts for startSketchOn face or plane (#4834)

* Add Rust side artifacts for startSketchOn face or plane

* move ast digging

---------

Co-authored-by: Kurt Hutten Irev-Dev <k.hutten@protonmail.ch>

* lint

* lint

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-macos-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-16-cores)

* trigger CI

* chore: disabled file watcher which prevents faster file write (#4835)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* partial fixes

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* Trigger CI

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* Trigger CI

* Fix up all the tests

* Fix partial execution

* wip

* WIP

* wip

* rust changes to make three point confrom to same as others since we're not ready with name params yet

* most of the fix for 3 point circle

* get overlays working for circle three point

* fmt

* fix types

* cargo fmt

* add face codef ref for walls and caps

* fix sketch on face after updates to rust side artifact graph

* some things needed for multi-profile tests

* bad attempts at fixing rust

* more

* more

* fix rust

* more rust fixes

* overlay fix

* remove duplicate test

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* lint and typing

* maybe fix a unit test

* small thing

* fix circ dep

* fix unit test

* fix some tests

* fix sweep point-and-click test

* fix more tests and add a fix me

* fix more tests

* fix electron specific test

* tsc

* more test tweaks

* update docs

* commint snaps?

* is clippy happy now?

* clippy again

* test works now without me changing anything big-fixed-itself

* small bug

* make three point have cross hair to make it consistent with othe rtools

* fix up state diagram

* fmt

* add draft point for first click of three point circ

* 1 test for three point circle

* 2 test for three point circle

* clean up

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* remove bad doc comment

* remove test skip

* remove onboarding test changes

* Update src/lang/modifyAst.ts

Co-authored-by: Jonathan Tran <jonnytran@gmail.com>

* Update output from simulation tests

* Fix to use correct source ranges

This also reduces cloning.

* Change back to skipping face cap none and both

* Update output after changing back to skipping none and both

* Fix clippy warning

* fix profile start snap bug

* add path ids to cap

* fix going into edit sketch

* make other startSketchOn's work

* fix snapshot test

* explain function name

* Update src/lib/rectangleTool.ts

Co-authored-by: Frank Noirot <frank@zoo.dev>

* rename error

* remove file tree from diff

* Update src/clientSideScene/segments.ts

Co-authored-by: Frank Noirot <frank@zoo.dev>

* nit

* Prevent double write to KCL code on revolve

* Update output after adding cap-to-path graph edge

* Fix edit/select sketch-on-cap via feature tree

* clean up for face codeRef

* fix changing tools part way through circle/rect tools

* fix delete of circle profile

* fix close profiles

* fix closing profile bug (tangentArcTo being ignored)

* remove stale comment

* Delete paths associated with sketch when the sketch plane is deleted

* Add support for deleting sketches on caps (not walls)

* get delet working for walls

* make delet of extrusions work for multi profile

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* Delete the sketch statement too on the cap and wall cases

* Don't write to file in `split-sketch-pipe-if-needed` unless necessary

* Don't wait for file write to complete within `updateEditorWithAstAndWriteToFile`
It is already debounced internally. If we await it, we will have to wait for a debounced timeout

* docs

* fix circ dep

* tsc

* fix selection enter sketch weirdness

* test fixes

* comment out and fixme for delete related tests

* add skip wins

* try and get last test to pass

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
Co-authored-by: Kevin Nadro <nadr0@users.noreply.github.com>
Co-authored-by: Pierre Jacquier <pierre@zoo.dev>
Co-authored-by: 49lf <ircsurfer33@gmail.com>
Co-authored-by: Frank Noirot <frank@zoo.dev>
Co-authored-by: Frank Noirot <frankjohnson1993@gmail.com>
This commit is contained in:
Kurt Hutten
2025-02-15 00:57:04 +11:00
committed by GitHub
parent 8c5662e458
commit 834f7133d8
74 changed files with 23720 additions and 2950 deletions

View File

@ -1,4 +1,5 @@
import {
Expr,
Artifact,
ArtifactGraph,
ArtifactId,
@ -18,7 +19,8 @@ import {
import { Models } from '@kittycad/lib'
import { getNodePathFromSourceRange } from 'lang/queryAstNodePathUtils'
import { err } from 'lib/trap'
import { codeManager } from 'lib/singletons'
import { Cap, Plane, Wall } from 'wasm-lib/kcl/bindings/Artifact'
import { CapSubType } from 'wasm-lib/kcl/bindings/Artifact'
export type { Artifact, ArtifactId, SegmentArtifact } from 'lang/wasm'
@ -37,10 +39,28 @@ export interface PlaneArtifactRich extends BaseArtifact {
codeRef: CodeRef
}
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
}
export interface PathArtifactRich extends BaseArtifact {
type: 'path'
/** A path must always lie on a plane */
plane: PlaneArtifact | WallArtifact
plane: PlaneArtifact | WallArtifact | CapArtifact
/** A path must always contain 0 or more segments */
segments: Array<SegmentArtifact>
/** A path may not result in a sweep artifact */
@ -51,7 +71,7 @@ export interface PathArtifactRich extends BaseArtifact {
interface SegmentArtifactRich extends BaseArtifact {
type: 'segment'
path: PathArtifact
surf?: WallArtifact
surf: WallArtifact
edges: Array<SweepEdge>
edgeCut?: EdgeCut
codeRef: CodeRef
@ -151,6 +171,73 @@ export function expandPlane(
}
}
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,
}
}
export function expandPath(
path: PathArtifact,
artifactGraph: ArtifactGraph
@ -239,6 +326,7 @@ export function expandSegment(
if (err(path)) return path
if (err(surf)) return surf
if (err(edgeCut)) return edgeCut
if (!surf) return new Error('Segment does not have a surface')
return {
type: 'segment',
@ -410,6 +498,220 @@ export function codeRefFromRange(range: SourceRange, ast: Program): CodeRef {
}
}
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
}
/**
* Get an artifact from a code source range
*/
@ -418,12 +720,24 @@ export function getArtifactFromRange(
artifactGraph: ArtifactGraph
): Artifact | null {
for (const artifact of artifactGraph.values()) {
if ('codeRef' in artifact) {
const codeRef = getFaceCodeRef(artifact)
if (codeRef) {
const match =
artifact.codeRef?.range[0] === range[0] &&
artifact.codeRef.range[1] === range[1]
codeRef?.range[0] === range[0] && codeRef.range[1] === range[1]
if (match) return artifact
}
}
return null
}
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
}