Skip to content

Commit 5413fe2

Browse files
authored
Quick pass at burning down KCL-P&C drift (#12314)
1 parent 232934d commit 5413fe2

11 files changed

Lines changed: 378 additions & 53 deletions

src/lang/modifyAst/boolean.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import {
77
} from '@src/lang/create'
88
import {
99
createVariableExpressionsArray,
10+
insertVariableAndOffsetPathToNode,
1011
setCallInAst,
1112
} from '@src/lang/modifyAst'
12-
import { getVariableExprsFromSelection } from '@src/lang/queryAst'
13+
import {
14+
getVariableExprsFromSelection,
15+
valueOrVariable,
16+
} from '@src/lang/queryAst'
1317
import type { ArtifactGraph, PathToNode, Program } from '@src/lang/wasm'
1418
import { modelingStdLibCommandName } from '@src/lib/commandBarConfigs/modelingCommandStdLib'
19+
import type { KclCommandValue } from '@src/lib/commandTypes'
1520
import { KCL_DEFAULT_CONSTANT_PREFIXES } from '@src/lib/constants'
1621
import { err } from '@src/lib/trap'
1722
import type { ModuleType } from '@src/lib/wasm_lib_wrapper'
@@ -21,12 +26,14 @@ export function addUnion({
2126
ast,
2227
artifactGraph,
2328
solids,
29+
tolerance,
2430
nodeToEdit,
2531
wasmInstance,
2632
}: {
2733
ast: Node<Program>
2834
artifactGraph: ArtifactGraph
2935
solids: Selections
36+
tolerance?: KclCommandValue
3037
nodeToEdit?: PathToNode
3138
wasmInstance: ModuleType
3239
}): Error | { modifiedAst: Node<Program>; pathToNode: PathToNode } {
@@ -54,8 +61,11 @@ export function addUnion({
5461
const call = createCallExpressionStdLibKw(
5562
modelingStdLibCommandName('Boolean Union'),
5663
objectsExpr,
57-
[]
64+
tolerance ? [createLabeledArg('tolerance', valueOrVariable(tolerance))] : []
5865
)
66+
if (tolerance && 'variableName' in tolerance && tolerance.variableName) {
67+
insertVariableAndOffsetPathToNode(tolerance, modifiedAst, mNodeToEdit)
68+
}
5969

6070
// 3. If edit, we assign the new function call declaration to the existing node,
6171
// otherwise just push to the end
@@ -81,12 +91,14 @@ export function addIntersect({
8191
ast,
8292
artifactGraph,
8393
solids,
94+
tolerance,
8495
nodeToEdit,
8596
wasmInstance,
8697
}: {
8798
ast: Node<Program>
8899
artifactGraph: ArtifactGraph
89100
solids: Selections
101+
tolerance?: KclCommandValue
90102
nodeToEdit?: PathToNode
91103
wasmInstance: ModuleType
92104
}): Error | { modifiedAst: Node<Program>; pathToNode: PathToNode } {
@@ -114,8 +126,11 @@ export function addIntersect({
114126
const call = createCallExpressionStdLibKw(
115127
modelingStdLibCommandName('Boolean Intersect'),
116128
objectsExpr,
117-
[]
129+
tolerance ? [createLabeledArg('tolerance', valueOrVariable(tolerance))] : []
118130
)
131+
if (tolerance && 'variableName' in tolerance && tolerance.variableName) {
132+
insertVariableAndOffsetPathToNode(tolerance, modifiedAst, mNodeToEdit)
133+
}
119134

120135
// 3. If edit, we assign the new function call declaration to the existing node,
121136
// otherwise just push to the end
@@ -142,13 +157,15 @@ export function addSubtract({
142157
artifactGraph,
143158
solids,
144159
tools,
160+
tolerance,
145161
nodeToEdit,
146162
wasmInstance,
147163
}: {
148164
ast: Node<Program>
149165
artifactGraph: ArtifactGraph
150166
solids: Selections
151167
tools: Selections
168+
tolerance?: KclCommandValue
152169
nodeToEdit?: PathToNode
153170
wasmInstance: ModuleType
154171
}): Error | { modifiedAst: Node<Program>; pathToNode: PathToNode } {
@@ -196,8 +213,16 @@ export function addSubtract({
196213
const call = createCallExpressionStdLibKw(
197214
modelingStdLibCommandName('Boolean Subtract'),
198215
objectsExpr,
199-
[createLabeledArg('tools', toolsExpr)]
216+
[
217+
createLabeledArg('tools', toolsExpr),
218+
...(tolerance
219+
? [createLabeledArg('tolerance', valueOrVariable(tolerance))]
220+
: []),
221+
]
200222
)
223+
if (tolerance && 'variableName' in tolerance && tolerance.variableName) {
224+
insertVariableAndOffsetPathToNode(tolerance, modifiedAst, mNodeToEdit)
225+
}
201226
if (vars.pathIfPipe && toolVars.pathIfPipe) {
202227
return new Error(
203228
'Cannot use both solids and tools in a subtraction operation with a pipe'

src/lang/modifyAst/edges.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export function addFillet({
6363
artifactGraph,
6464
selection,
6565
radius,
66+
tolerance,
6667
tag,
6768
version,
6869
nodeToEdit,
@@ -72,6 +73,7 @@ export function addFillet({
7273
artifactGraph: ArtifactGraph
7374
selection: Selections
7475
radius: KclCommandValue
76+
tolerance?: KclCommandValue
7577
tag?: string
7678
version?: KclCommandValue
7779
nodeToEdit?: PathToNode
@@ -126,13 +128,19 @@ export function addFillet({
126128
if (version && 'variableName' in version && version.variableName) {
127129
insertVariableAndOffsetPathToNode(version, modifiedAst, mNodeToEdit)
128130
}
131+
if (tolerance && 'variableName' in tolerance && tolerance.variableName) {
132+
insertVariableAndOffsetPathToNode(tolerance, modifiedAst, mNodeToEdit)
133+
}
129134

130135
// 3. Create fillet calls for each body
131136
const pathToNodes: PathToNode[] = []
132137
for (const data of bodies.values()) {
133138
const tagArgs = tag
134139
? [createLabeledArg('tag', createTagDeclarator(tag))]
135140
: []
141+
const toleranceArgs = tolerance
142+
? [createLabeledArg('tolerance', valueOrVariable(tolerance))]
143+
: []
136144
const versionArgs = version
137145
? [createLabeledArg('version', valueOrVariable(version))]
138146
: []
@@ -142,6 +150,7 @@ export function addFillet({
142150
[
143151
createLabeledArg('tags', data.tagsExpr),
144152
createLabeledArg('radius', valueOrVariable(radius)),
153+
...toleranceArgs,
145154
...tagArgs,
146155
...versionArgs,
147156
]

src/lang/modifyAst/surfaces.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import type { Node } from '@rust/kcl-lib/bindings/Node'
2-
import { createCallExpressionStdLibKw } from '@src/lang/create'
2+
import {
3+
createCallExpressionStdLibKw,
4+
createLabeledArg,
5+
} from '@src/lang/create'
36
import {
47
createVariableExpressionsArray,
8+
insertVariableAndOffsetPathToNode,
59
setCallInAst,
610
} from '@src/lang/modifyAst'
7-
import { getVariableExprsFromSelection } from '@src/lang/queryAst'
11+
import {
12+
getVariableExprsFromSelection,
13+
valueOrVariable,
14+
} from '@src/lang/queryAst'
815
import type { ArtifactGraph, PathToNode, Program } from '@src/lang/wasm'
916
import { modelingStdLibCommandName } from '@src/lib/commandBarConfigs/modelingCommandStdLib'
17+
import type { KclCommandValue } from '@src/lib/commandTypes'
1018
import { KCL_DEFAULT_CONSTANT_PREFIXES } from '@src/lib/constants'
1119
import { err } from '@src/lib/trap'
1220
import type { ModuleType } from '@src/lib/wasm_lib_wrapper'
@@ -97,12 +105,14 @@ export function addJoinSurfaces({
97105
ast,
98106
artifactGraph,
99107
selection,
108+
tolerance,
100109
wasmInstance,
101110
nodeToEdit,
102111
}: {
103112
ast: Node<Program>
104113
artifactGraph: ArtifactGraph
105114
selection: Selections
115+
tolerance?: KclCommandValue
106116
wasmInstance: ModuleType
107117
nodeToEdit?: PathToNode
108118
}): Error | { modifiedAst: Node<Program>; pathToNode: PathToNode } {
@@ -134,8 +144,11 @@ export function addJoinSurfaces({
134144
const call = createCallExpressionStdLibKw(
135145
modelingStdLibCommandName('Join Surfaces'),
136146
objectsExpr,
137-
[]
147+
tolerance ? [createLabeledArg('tolerance', valueOrVariable(tolerance))] : []
138148
)
149+
if (tolerance && 'variableName' in tolerance && tolerance.variableName) {
150+
insertVariableAndOffsetPathToNode(tolerance, modifiedAst, mNodeToEdit)
151+
}
139152

140153
// 3. If edit, we assign the new function call declaration to the existing node,
141154
// otherwise just push to the end

src/lang/modifyAst/sweeps.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ export function addSweep({
324324
path,
325325
wasmInstance,
326326
sectional,
327+
tolerance,
327328
relativeTo,
328329
translateProfileToPath,
329330
orientProfilePerpendicular,
@@ -339,6 +340,7 @@ export function addSweep({
339340
path: Selections
340341
wasmInstance: ModuleType
341342
sectional?: boolean
343+
tolerance?: KclCommandValue
342344
relativeTo?: SweepRelativeTo
343345
translateProfileToPath?: boolean
344346
orientProfilePerpendicular?: boolean
@@ -438,6 +440,9 @@ export function addSweep({
438440
sectional !== undefined
439441
? [createLabeledArg('sectional', createLiteral(sectional, wasmInstance))]
440442
: []
443+
const toleranceExpr = tolerance
444+
? [createLabeledArg('tolerance', valueOrVariable(tolerance))]
445+
: []
441446
// `relativeTo` is legacy for new sweep calls; only preserve or update it when
442447
// editing existing code that already depends on that argument.
443448
const relativeToExpr =
@@ -501,6 +506,7 @@ export function addSweep({
501506
[
502507
createLabeledArg('path', pathExpr),
503508
...sectionalExpr,
509+
...toleranceExpr,
504510
...relativeToExpr,
505511
...tagStartExpr,
506512
...tagEndExpr,
@@ -514,6 +520,9 @@ export function addSweep({
514520
if (version && 'variableName' in version && version.variableName) {
515521
insertVariableAndOffsetPathToNode(version, modifiedAst, mNodeToEdit)
516522
}
523+
if (tolerance && 'variableName' in tolerance && tolerance.variableName) {
524+
insertVariableAndOffsetPathToNode(tolerance, modifiedAst, mNodeToEdit)
525+
}
517526

518527
// 3. If edit, we assign the new function call declaration to the existing node,
519528
// otherwise just push to the end
@@ -543,6 +552,7 @@ export function addLoft({
543552
vDegree,
544553
bezApproximateRational,
545554
baseCurveIndex,
555+
tolerance,
546556
tagStart,
547557
tagEnd,
548558
bodyType,
@@ -555,6 +565,7 @@ export function addLoft({
555565
vDegree?: KclCommandValue
556566
bezApproximateRational?: boolean
557567
baseCurveIndex?: KclCommandValue
568+
tolerance?: KclCommandValue
558569
tagStart?: string
559570
tagEnd?: string
560571
bodyType?: KclPreludeBodyType
@@ -619,6 +630,9 @@ export function addLoft({
619630
const baseCurveIndexExpr = baseCurveIndex
620631
? [createLabeledArg('baseCurveIndex', valueOrVariable(baseCurveIndex))]
621632
: []
633+
const toleranceExpr = tolerance
634+
? [createLabeledArg('tolerance', valueOrVariable(tolerance))]
635+
: []
622636
const tagStartExpr = tagStart
623637
? [createLabeledArg('tagStart', createTagDeclarator(tagStart))]
624638
: []
@@ -637,6 +651,7 @@ export function addLoft({
637651
...vDegreeExpr,
638652
...bezApproximateRationalExpr,
639653
...baseCurveIndexExpr,
654+
...toleranceExpr,
640655
...tagStartExpr,
641656
...tagEndExpr,
642657
...bodyTypeExpr,
@@ -654,6 +669,9 @@ export function addLoft({
654669
) {
655670
insertVariableAndOffsetPathToNode(baseCurveIndex, modifiedAst, mNodeToEdit)
656671
}
672+
if (tolerance && 'variableName' in tolerance && tolerance.variableName) {
673+
insertVariableAndOffsetPathToNode(tolerance, modifiedAst, mNodeToEdit)
674+
}
657675

658676
// 3. If edit, we assign the new function call declaration to the existing node,
659677
// otherwise just push to the end
@@ -683,6 +701,7 @@ export function addRevolve({
683701
wasmInstance,
684702
axis,
685703
edge,
704+
tolerance,
686705
symmetric,
687706
bidirectionalAngle,
688707
tagStart,
@@ -697,6 +716,7 @@ export function addRevolve({
697716
wasmInstance: ModuleType
698717
axis?: string
699718
edge?: Selections
719+
tolerance?: KclCommandValue
700720
symmetric?: boolean
701721
bidirectionalAngle?: KclCommandValue
702722
tagStart?: string
@@ -764,6 +784,9 @@ export function addRevolve({
764784
symmetric !== undefined
765785
? [createLabeledArg('symmetric', createLiteral(symmetric, wasmInstance))]
766786
: []
787+
const toleranceExpr = tolerance
788+
? [createLabeledArg('tolerance', valueOrVariable(tolerance))]
789+
: []
767790
const bidirectionalAngleExpr = bidirectionalAngle
768791
? [
769792
createLabeledArg(
@@ -789,6 +812,7 @@ export function addRevolve({
789812
[
790813
createLabeledArg('angle', valueOrVariable(angle)),
791814
createLabeledArg('axis', getAxisResult.generatedAxis),
815+
...toleranceExpr,
792816
...symmetricExpr,
793817
...bidirectionalAngleExpr,
794818
...tagStartExpr,
@@ -801,6 +825,9 @@ export function addRevolve({
801825
if ('variableName' in angle && angle.variableName) {
802826
insertVariableAndOffsetPathToNode(angle, modifiedAst, mNodeToEdit)
803827
}
828+
if (tolerance && 'variableName' in tolerance && tolerance.variableName) {
829+
insertVariableAndOffsetPathToNode(tolerance, modifiedAst, mNodeToEdit)
830+
}
804831

805832
if (
806833
bidirectionalAngle &&

src/lang/modifyAst/transforms.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,31 @@ scale(
781781
return recast(result.modifiedAst, instance)
782782
}
783783

784+
async function runAddAxisRotateTest(
785+
code: string,
786+
instance: ModuleType,
787+
kclManager: KclManager,
788+
rustContext: RustContext
789+
) {
790+
const {
791+
artifactGraph,
792+
ast,
793+
sketches: objects,
794+
} = await getAstAndSketchSelections(code, instance, kclManager)
795+
const result = addRotate({
796+
ast,
797+
artifactGraph,
798+
objects,
799+
axis: 'Z',
800+
angle: await getKclCommandValue('90deg', instance, rustContext),
801+
global: true,
802+
wasmInstance: instanceInThisFile,
803+
})
804+
if (err(result)) throw result
805+
await runNewAstAndCheckForSweep(result.modifiedAst, rustContext)
806+
return recast(result.modifiedAst, instance)
807+
}
808+
784809
it('should add a standalone call on sweep selection', async () => {
785810
const code = `sketch001 = startSketchOn(XY)
786811
profile001 = circle(sketch001, center = [0, 0], radius = 1)
@@ -801,6 +826,25 @@ extrude001 = extrude(profile001, length = 1)`
801826
expect(newCode).toContain(code + '\n' + expectedNewLine)
802827
})
803828

829+
it('should add a named axis as a bare identifier', async () => {
830+
const code = `sketch001 = startSketchOn(XY)
831+
profile001 = circle(sketch001, center = [0, 0], radius = 1)
832+
extrude001 = extrude(profile001, length = 1)`
833+
const expectedNewLine = `rotate(
834+
extrude001,
835+
axis = Z,
836+
angle = 90deg,
837+
global = true,
838+
)`
839+
const newCode = await runAddAxisRotateTest(
840+
code,
841+
instanceInThisFile,
842+
kclManagerInThisFile,
843+
rustContextInThisFile
844+
)
845+
expect(newCode).toContain(code + '\n' + expectedNewLine)
846+
})
847+
804848
it('should push a call in pipe if selection was in variable-less pipe', async () => {
805849
const code = `startSketchOn(XY)
806850
|> circle(center = [0, 0], radius = 1)

0 commit comments

Comments
 (0)