-
Notifications
You must be signed in to change notification settings - Fork 101
Add bodyType to Revolve P&C #9572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
a262e20
550d426
a5111f5
5c212c8
0edf999
2d28e4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ import { | |
| addSweep, | ||
| getAxisExpressionAndIndex, | ||
| retrieveAxisOrEdgeSelectionsFromOpArg, | ||
| retrieveBodyTypeFromOpArg, | ||
| } from '@src/lang/modifyAst/sweeps' | ||
| import type { Node } from '@rust/kcl-lib/bindings/Node' | ||
| import type { ConnectionManager } from '@src/network/connectionManager' | ||
|
|
@@ -832,6 +833,41 @@ profile001 = circle(sketch001, center = [3, 0], radius = 1)` | |
| ) | ||
| }) | ||
|
|
||
| it('should add basic revolve call with surface bodyType', async () => { | ||
| const { ast, sketches } = await getAstAndSketchSelections( | ||
| circleCode, | ||
| instanceInThisFile, | ||
| kclManagerInThisFile | ||
| ) | ||
| expect(sketches.graphSelections).toHaveLength(1) | ||
| const angle = await getKclCommandValue( | ||
| '10', | ||
| instanceInThisFile, | ||
| rustContextInThisFile | ||
| ) | ||
| const axis = 'X' | ||
| const result = addRevolve({ | ||
| ast, | ||
| sketches, | ||
| angle, | ||
| axis, | ||
| bodyType: 'SURFACE', | ||
| wasmInstance: instanceInThisFile, | ||
| }) | ||
| if (err(result)) throw result | ||
| await runNewAstAndCheckForSweep(result.modifiedAst, rustContextInThisFile) | ||
| const newCode = recast(result.modifiedAst, instanceInThisFile) | ||
| expect(newCode).toContain(circleCode) | ||
| expect(newCode).toContain( | ||
| `revolve001 = revolve( | ||
| profile001, | ||
| angle = 10, | ||
| axis = X, | ||
| bodyType = SURFACE, | ||
| )` | ||
| ) | ||
| }) | ||
|
|
||
| it('should add basic revolve call with symmetric true', async () => { | ||
| const { ast, sketches } = await getAstAndSketchSelections( | ||
| circleCode, | ||
|
|
@@ -1157,4 +1193,40 @@ helix001 = helix( | |
| expect(result.axis).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('Testing retrieveBodyTypeFromOpArg', () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New util function used for Extrude and Revolve here in the edit flow, see |
||
| async function findBodyTypeArg(code: string) { | ||
| const ast = assertParse(code, instanceInThisFile) | ||
| const { operations } = await enginelessExecutor( | ||
| ast, | ||
| rustContextInThisFile | ||
| ) | ||
| const op = operations.find( | ||
| (o) => o.type === 'StdLibCall' && o.name === 'extrude' | ||
| ) | ||
| if (!op || op.type !== 'StdLibCall' || !op.labeledArgs.bodyType) { | ||
| throw new Error('Extrude operation not found') | ||
| } | ||
|
|
||
| return op.labeledArgs.bodyType | ||
| } | ||
|
|
||
| it('should return SOLID bodyType from op argument', async () => { | ||
| const code = `${circleProfileCode} | ||
| extrude001 = extrude(profile001, length = 1, bodyType = SOLID)` | ||
| const opArg = await findBodyTypeArg(code) | ||
| const result = retrieveBodyTypeFromOpArg(opArg, code) | ||
| if (err(result)) throw result | ||
| expect(result).toEqual('SOLID') | ||
| }) | ||
|
|
||
| it('should return SURFACE bodyType from op argument', async () => { | ||
| const code = `${circleProfileCode} | ||
| extrude001 = extrude(profile001, length = 1, bodyType = SURFACE)` | ||
| const opArg = await findBodyTypeArg(code) | ||
| const result = retrieveBodyTypeFromOpArg(opArg, code) | ||
| if (err(result)) throw result | ||
| expect(result).toEqual('SURFACE') | ||
| }) | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,8 @@ import { | |
| KCL_DEFAULT_CONSTANT_PREFIXES, | ||
| type KclPreludeExtrudeMethod, | ||
| type KclPreludeBodyType, | ||
| KCL_PRELUDE_BODY_TYPE_SOLID, | ||
| KCL_PRELUDE_BODY_TYPE_SURFACE, | ||
| } from '@src/lib/constants' | ||
| import { err } from '@src/lib/trap' | ||
| import type { Selections } from '@src/machines/modelingSharedTypes' | ||
|
|
@@ -453,6 +455,7 @@ export function addRevolve({ | |
| bidirectionalAngle, | ||
| tagStart, | ||
| tagEnd, | ||
| bodyType, | ||
| nodeToEdit, | ||
| }: { | ||
| ast: Node<Program> | ||
|
|
@@ -465,6 +468,7 @@ export function addRevolve({ | |
| bidirectionalAngle?: KclCommandValue | ||
| tagStart?: string | ||
| tagEnd?: string | ||
| bodyType?: KclPreludeBodyType | ||
| nodeToEdit?: PathToNode | ||
| }): | ||
| | { | ||
|
|
@@ -517,6 +521,9 @@ export function addRevolve({ | |
| const tagEndExpr = tagEnd | ||
| ? [createLabeledArg('tagEnd', createTagDeclarator(tagEnd))] | ||
| : [] | ||
| const bodyTypeExpr = bodyType | ||
| ? [createLabeledArg('bodyType', createLocalName(bodyType))] | ||
| : [] | ||
|
|
||
| const sketchesExpr = createVariableExpressionsArray(vars.exprs) | ||
| const call = createCallExpressionStdLibKw('revolve', sketchesExpr, [ | ||
|
|
@@ -526,6 +533,7 @@ export function addRevolve({ | |
| ...bidirectionalAngleExpr, | ||
| ...tagStartExpr, | ||
| ...tagEndExpr, | ||
| ...bodyTypeExpr, | ||
| ]) | ||
|
|
||
| // Insert variables for labeled arguments if provided | ||
|
|
@@ -732,3 +740,21 @@ export function retrieveTagDeclaratorFromOpArg( | |
| toUtf16(opArg.sourceRange[1], code) | ||
| ) | ||
| } | ||
|
|
||
| export function retrieveBodyTypeFromOpArg( | ||
| opArg: OpArg, | ||
| code: string | ||
| ): KclPreludeBodyType | Error { | ||
| /** Version of `toUtf16` bound to our code, for mapping source range values. */ | ||
| const boundToUtf16 = (n: number) => toUtf16(n, code) | ||
| const result = code.slice(...opArg.sourceRange.map(boundToUtf16)) | ||
| if (result === KCL_PRELUDE_BODY_TYPE_SOLID) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't need to normalize the lover/upper case here, right?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question, are both defined? |
||
| return KCL_PRELUDE_BODY_TYPE_SOLID | ||
| } | ||
|
|
||
| if (result === KCL_PRELUDE_BODY_TYPE_SURFACE) { | ||
| return KCL_PRELUDE_BODY_TYPE_SURFACE | ||
| } | ||
|
|
||
| return new Error("Couldn't retrieve bodyType argument") | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ import { | |
| } from '@src/lang/modifyAst/faces' | ||
| import { | ||
| retrieveAxisOrEdgeSelectionsFromOpArg, | ||
| retrieveBodyTypeFromOpArg, | ||
| retrieveTagDeclaratorFromOpArg, | ||
| SWEEP_CONSTANTS, | ||
| SWEEP_MODULE, | ||
|
|
@@ -48,8 +49,6 @@ import { err } from '@src/lib/trap' | |
| import type { CommandBarMachineEvent } from '@src/machines/commandBarMachine' | ||
| import { retrieveEdgeSelectionsFromOpArgs } from '@src/lang/modifyAst/edges' | ||
| import { | ||
| KCL_PRELUDE_BODY_TYPE_SOLID, | ||
| KCL_PRELUDE_BODY_TYPE_SURFACE, | ||
| type KclPreludeBodyType, | ||
| KCL_PRELUDE_EXTRUDE_METHOD_MERGE, | ||
| KCL_PRELUDE_EXTRUDE_METHOD_NEW, | ||
|
|
@@ -407,16 +406,9 @@ const prepareToEditExtrude: PrepareToEditCallback = async ({ | |
| // bodyType argument from a string | ||
| let bodyType: KclPreludeBodyType | undefined | ||
| if ('bodyType' in operation.labeledArgs && operation.labeledArgs.bodyType) { | ||
| const result = code.slice( | ||
| ...operation.labeledArgs.bodyType.sourceRange.map(boundToUtf16) | ||
| ) | ||
| if (result === KCL_PRELUDE_BODY_TYPE_SOLID) { | ||
| bodyType = KCL_PRELUDE_BODY_TYPE_SOLID | ||
| } else if (result === KCL_PRELUDE_BODY_TYPE_SURFACE) { | ||
| bodyType = KCL_PRELUDE_BODY_TYPE_SURFACE | ||
| } else { | ||
| return { reason: "Couldn't retrieve bodyType argument" } | ||
| } | ||
| const res = retrieveBodyTypeFromOpArg(operation.labeledArgs.bodyType, code) | ||
| if (err(res)) return { reason: res.message } | ||
| bodyType = res | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so much cleaner, love it |
||
| } | ||
|
|
||
| // 3. Assemble the default argument values for the command, | ||
|
|
@@ -1349,6 +1341,14 @@ const prepareToEditRevolve: PrepareToEditCallback = async ({ | |
| tagEnd = retrieveTagDeclaratorFromOpArg(operation.labeledArgs.tagEnd, code) | ||
| } | ||
|
|
||
| // bodyType argument from a string | ||
| let bodyType: KclPreludeBodyType | undefined | ||
| if ('bodyType' in operation.labeledArgs && operation.labeledArgs.bodyType) { | ||
| const res = retrieveBodyTypeFromOpArg(operation.labeledArgs.bodyType, code) | ||
| if (err(res)) return { reason: res.message } | ||
| bodyType = res | ||
| } | ||
|
|
||
| // 3. Assemble the default argument values for the command, | ||
| // with `nodeToEdit` set, which will let the actor know | ||
| // to edit the node that corresponds to the StdLibCall. | ||
|
|
@@ -1362,6 +1362,7 @@ const prepareToEditRevolve: PrepareToEditCallback = async ({ | |
| bidirectionalAngle, | ||
| tagStart, | ||
| tagEnd, | ||
| bodyType, | ||
| nodeToEdit: pathToNodeFromRustNodePath(operation.nodePath), | ||
| } | ||
| return { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra codemod integration test case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good stuff