2024-07-15 19:20:32 +10:00
|
|
|
import {
|
|
|
|
CallExpression,
|
2024-10-11 23:25:51 +02:00
|
|
|
Expr,
|
|
|
|
Identifier,
|
2024-07-15 19:20:32 +10:00
|
|
|
ObjectExpression,
|
|
|
|
PathToNode,
|
|
|
|
Program,
|
|
|
|
VariableDeclaration,
|
|
|
|
VariableDeclarator,
|
2024-09-27 15:44:44 -07:00
|
|
|
sketchFromKclValue,
|
2024-07-15 19:20:32 +10:00
|
|
|
} from '../wasm'
|
|
|
|
import {
|
|
|
|
createCallExpressionStdLib,
|
|
|
|
createPipeSubstitution,
|
|
|
|
createObjectExpression,
|
|
|
|
createArrayExpression,
|
|
|
|
createIdentifier,
|
|
|
|
createPipeExpression,
|
|
|
|
} from '../modifyAst'
|
|
|
|
import {
|
|
|
|
getNodeFromPath,
|
|
|
|
getNodePathFromSourceRange,
|
|
|
|
hasSketchPipeBeenExtruded,
|
|
|
|
traverse,
|
|
|
|
} from '../queryAst'
|
|
|
|
import {
|
|
|
|
addTagForSketchOnFace,
|
|
|
|
getTagFromCallExpression,
|
|
|
|
sketchLineHelperMap,
|
|
|
|
} from '../std/sketch'
|
2024-08-26 08:07:20 +02:00
|
|
|
import { err, trap } from 'lib/trap'
|
2024-10-11 23:25:51 +02:00
|
|
|
import { Selections } from 'lib/selections'
|
2024-08-26 08:07:20 +02:00
|
|
|
import { KclCommandValue } from 'lib/commandTypes'
|
|
|
|
import {
|
|
|
|
ArtifactGraph,
|
2024-09-17 13:22:53 -05:00
|
|
|
getSweepFromSuspectedPath,
|
2024-08-26 08:07:20 +02:00
|
|
|
} from 'lang/std/artifactGraph'
|
2024-11-16 16:49:44 -05:00
|
|
|
import {
|
|
|
|
kclManager,
|
|
|
|
engineCommandManager,
|
|
|
|
editorManager,
|
|
|
|
codeManager,
|
|
|
|
} from 'lib/singletons'
|
2024-10-30 16:52:17 -04:00
|
|
|
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
2024-08-26 08:07:20 +02:00
|
|
|
|
2024-10-03 11:14:02 +02:00
|
|
|
// Apply Fillet To Selection
|
2024-08-26 08:07:20 +02:00
|
|
|
|
|
|
|
export function applyFilletToSelection(
|
2024-10-30 16:52:17 -04:00
|
|
|
ast: Node<Program>,
|
2024-08-26 08:07:20 +02:00
|
|
|
selection: Selections,
|
|
|
|
radius: KclCommandValue
|
|
|
|
): void | Error {
|
2024-10-03 11:14:02 +02:00
|
|
|
// 1. clone and modify with fillet and tag
|
|
|
|
const result = modifyAstCloneWithFilletAndTag(ast, selection, radius)
|
2024-09-09 12:15:16 +02:00
|
|
|
if (err(result)) return result
|
|
|
|
const { modifiedAst, pathToFilletNode } = result
|
|
|
|
|
2024-10-03 11:14:02 +02:00
|
|
|
// 2. update ast
|
2024-09-09 18:17:45 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2024-09-09 12:15:16 +02:00
|
|
|
updateAstAndFocus(modifiedAst, pathToFilletNode)
|
|
|
|
}
|
|
|
|
|
2024-10-03 11:14:02 +02:00
|
|
|
export function modifyAstCloneWithFilletAndTag(
|
2024-10-30 16:52:17 -04:00
|
|
|
ast: Node<Program>,
|
2024-09-09 12:15:16 +02:00
|
|
|
selection: Selections,
|
|
|
|
radius: KclCommandValue
|
2024-10-30 16:52:17 -04:00
|
|
|
): { modifiedAst: Node<Program>; pathToFilletNode: Array<PathToNode> } | Error {
|
2024-10-03 11:14:02 +02:00
|
|
|
let clonedAst = structuredClone(ast)
|
|
|
|
const clonedAstForGetExtrude = structuredClone(ast)
|
|
|
|
|
|
|
|
const astResult = insertRadiusIntoAst(clonedAst, radius)
|
2024-08-26 08:07:20 +02:00
|
|
|
if (err(astResult)) return astResult
|
|
|
|
|
|
|
|
const artifactGraph = engineCommandManager.artifactGraph
|
|
|
|
|
2024-10-03 11:14:02 +02:00
|
|
|
// Step 1: modify ast with tags and group them by extrude nodes (bodies)
|
2024-10-11 23:25:51 +02:00
|
|
|
const extrudeToTagsMap: Map<
|
|
|
|
PathToNode,
|
|
|
|
Array<{ tag: string; selectionType: string }>
|
|
|
|
> = new Map()
|
2024-10-03 11:14:02 +02:00
|
|
|
const lookupMap: Map<string, PathToNode> = new Map() // work around for Map key comparison
|
2024-08-26 08:07:20 +02:00
|
|
|
|
2024-09-12 21:45:26 +02:00
|
|
|
for (const selectionRange of selection.codeBasedSelections) {
|
|
|
|
const singleSelection = {
|
|
|
|
codeBasedSelections: [selectionRange],
|
|
|
|
otherSelections: [],
|
|
|
|
}
|
2024-10-11 23:25:51 +02:00
|
|
|
const selectionType = singleSelection.codeBasedSelections[0].type
|
2024-09-12 21:45:26 +02:00
|
|
|
|
2024-10-03 11:14:02 +02:00
|
|
|
const result = getPathToExtrudeForSegmentSelection(
|
|
|
|
clonedAstForGetExtrude,
|
|
|
|
singleSelection,
|
|
|
|
artifactGraph
|
|
|
|
)
|
|
|
|
if (err(result)) return result
|
|
|
|
const { pathToSegmentNode, pathToExtrudeNode } = result
|
|
|
|
|
|
|
|
const tagResult = mutateAstWithTagForSketchSegment(
|
2024-09-12 21:45:26 +02:00
|
|
|
clonedAst,
|
2024-10-03 11:14:02 +02:00
|
|
|
pathToSegmentNode
|
|
|
|
)
|
|
|
|
if (err(tagResult)) return tagResult
|
|
|
|
const { tag } = tagResult
|
2024-10-11 23:25:51 +02:00
|
|
|
const tagInfo = { tag, selectionType }
|
2024-10-03 11:14:02 +02:00
|
|
|
|
|
|
|
// Group tags by their corresponding extrude node
|
|
|
|
const extrudeKey = JSON.stringify(pathToExtrudeNode)
|
|
|
|
|
|
|
|
if (lookupMap.has(extrudeKey)) {
|
|
|
|
const existingPath = lookupMap.get(extrudeKey)
|
|
|
|
if (!existingPath) return new Error('Path to extrude node not found.')
|
2024-10-11 23:25:51 +02:00
|
|
|
extrudeToTagsMap.get(existingPath)?.push(tagInfo)
|
2024-10-03 11:14:02 +02:00
|
|
|
} else {
|
|
|
|
lookupMap.set(extrudeKey, pathToExtrudeNode)
|
2024-10-11 23:25:51 +02:00
|
|
|
extrudeToTagsMap.set(pathToExtrudeNode, [tagInfo])
|
2024-10-03 11:14:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: Apply fillet(s) for each extrude node (body)
|
|
|
|
let pathToFilletNodes: Array<PathToNode> = []
|
2024-10-11 23:25:51 +02:00
|
|
|
for (const [pathToExtrudeNode, tagInfos] of extrudeToTagsMap.entries()) {
|
2024-10-03 11:14:02 +02:00
|
|
|
// Create a fillet expression with multiple tags
|
|
|
|
const radiusValue =
|
2024-09-12 21:45:26 +02:00
|
|
|
'variableName' in radius ? radius.variableIdentifierAst : radius.valueAst
|
2024-10-11 23:25:51 +02:00
|
|
|
|
|
|
|
const tagCalls = tagInfos.map(({ tag, selectionType }) => {
|
|
|
|
return getEdgeTagCall(tag, selectionType)
|
|
|
|
})
|
|
|
|
const firstTag = tagCalls[0] // can be Identifier or CallExpression (for opposite and adjacent edges)
|
|
|
|
|
2024-10-03 11:14:02 +02:00
|
|
|
const filletCall = createCallExpressionStdLib('fillet', [
|
|
|
|
createObjectExpression({
|
|
|
|
radius: radiusValue,
|
2024-10-11 23:25:51 +02:00
|
|
|
tags: createArrayExpression(tagCalls),
|
2024-10-03 11:14:02 +02:00
|
|
|
}),
|
|
|
|
createPipeSubstitution(),
|
|
|
|
])
|
|
|
|
|
|
|
|
// Locate the extrude call
|
|
|
|
const locatedExtrudeDeclarator = locateExtrudeDeclarator(
|
|
|
|
clonedAst,
|
|
|
|
pathToExtrudeNode
|
2024-09-12 21:45:26 +02:00
|
|
|
)
|
2024-10-03 11:14:02 +02:00
|
|
|
if (err(locatedExtrudeDeclarator)) return locatedExtrudeDeclarator
|
|
|
|
const { extrudeDeclarator } = locatedExtrudeDeclarator
|
|
|
|
|
|
|
|
// Modify the extrude expression to include this fillet expression
|
|
|
|
// CallExpression - no fillet
|
2024-11-19 21:30:26 +01:00
|
|
|
// PipeExpression - fillet exists or extrude in sketch pipe
|
2024-10-03 11:14:02 +02:00
|
|
|
|
|
|
|
let pathToFilletNode: PathToNode = []
|
|
|
|
|
|
|
|
if (extrudeDeclarator.init.type === 'CallExpression') {
|
|
|
|
// 1. case when no fillet exists
|
|
|
|
|
|
|
|
// modify ast with new fillet call by mutating the extrude node
|
|
|
|
extrudeDeclarator.init = createPipeExpression([
|
|
|
|
extrudeDeclarator.init,
|
|
|
|
filletCall,
|
|
|
|
])
|
|
|
|
|
|
|
|
// get path to the fillet node
|
|
|
|
pathToFilletNode = getPathToNodeOfFilletLiteral(
|
|
|
|
pathToExtrudeNode,
|
|
|
|
extrudeDeclarator,
|
2024-10-11 23:25:51 +02:00
|
|
|
firstTag
|
2024-10-03 11:14:02 +02:00
|
|
|
)
|
|
|
|
pathToFilletNodes.push(pathToFilletNode)
|
|
|
|
} else if (extrudeDeclarator.init.type === 'PipeExpression') {
|
2024-11-19 21:30:26 +01:00
|
|
|
// 2. case when fillet exists or extrude in sketch pipe
|
2024-10-03 11:14:02 +02:00
|
|
|
|
|
|
|
// mutate the extrude node with the new fillet call
|
|
|
|
extrudeDeclarator.init.body.push(filletCall)
|
|
|
|
|
|
|
|
// get path to the fillet node
|
|
|
|
pathToFilletNode = getPathToNodeOfFilletLiteral(
|
|
|
|
pathToExtrudeNode,
|
|
|
|
extrudeDeclarator,
|
2024-10-11 23:25:51 +02:00
|
|
|
firstTag
|
2024-10-03 11:14:02 +02:00
|
|
|
)
|
|
|
|
pathToFilletNodes.push(pathToFilletNode)
|
|
|
|
} else {
|
|
|
|
return new Error('Unsupported extrude type.')
|
|
|
|
}
|
2024-09-12 21:45:26 +02:00
|
|
|
}
|
2024-09-23 08:07:31 +02:00
|
|
|
return { modifiedAst: clonedAst, pathToFilletNode: pathToFilletNodes }
|
2024-08-26 08:07:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function insertRadiusIntoAst(
|
|
|
|
ast: Program,
|
|
|
|
radius: KclCommandValue
|
|
|
|
): { ast: Program } | Error {
|
|
|
|
try {
|
|
|
|
// Validate and update AST
|
|
|
|
if (
|
|
|
|
'variableName' in radius &&
|
|
|
|
radius.variableName &&
|
|
|
|
radius.insertIndex !== undefined
|
|
|
|
) {
|
|
|
|
const newAst = structuredClone(ast)
|
|
|
|
newAst.body.splice(radius.insertIndex, 0, radius.variableDeclarationAst)
|
|
|
|
return { ast: newAst }
|
|
|
|
}
|
|
|
|
return { ast }
|
|
|
|
} catch (error) {
|
|
|
|
return new Error(`Failed to handle AST: ${(error as Error).message}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPathToExtrudeForSegmentSelection(
|
|
|
|
ast: Program,
|
|
|
|
selection: Selections,
|
|
|
|
artifactGraph: ArtifactGraph
|
|
|
|
): { pathToSegmentNode: PathToNode; pathToExtrudeNode: PathToNode } | Error {
|
|
|
|
const pathToSegmentNode = getNodePathFromSourceRange(
|
|
|
|
ast,
|
|
|
|
selection.codeBasedSelections[0].range
|
|
|
|
)
|
|
|
|
|
|
|
|
const varDecNode = getNodeFromPath<VariableDeclaration>(
|
|
|
|
ast,
|
|
|
|
pathToSegmentNode,
|
|
|
|
'VariableDeclaration'
|
|
|
|
)
|
|
|
|
if (err(varDecNode)) return varDecNode
|
|
|
|
const sketchVar = varDecNode.node.declarations[0].id.name
|
|
|
|
|
2024-09-27 15:44:44 -07:00
|
|
|
const sketch = sketchFromKclValue(
|
2024-08-26 08:07:20 +02:00
|
|
|
kclManager.programMemory.get(sketchVar),
|
|
|
|
sketchVar
|
|
|
|
)
|
2024-09-27 15:44:44 -07:00
|
|
|
if (trap(sketch)) return sketch
|
2024-08-26 08:07:20 +02:00
|
|
|
|
2024-09-27 15:44:44 -07:00
|
|
|
const extrusion = getSweepFromSuspectedPath(sketch.id, artifactGraph)
|
2024-08-26 08:07:20 +02:00
|
|
|
if (err(extrusion)) return extrusion
|
|
|
|
|
|
|
|
const pathToExtrudeNode = getNodePathFromSourceRange(
|
|
|
|
ast,
|
|
|
|
extrusion.codeRef.range
|
|
|
|
)
|
|
|
|
if (err(pathToExtrudeNode)) return pathToExtrudeNode
|
|
|
|
|
|
|
|
return { pathToSegmentNode, pathToExtrudeNode }
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateAstAndFocus(
|
2024-10-30 16:52:17 -04:00
|
|
|
modifiedAst: Node<Program>,
|
2024-09-23 08:07:31 +02:00
|
|
|
pathToFilletNode: Array<PathToNode>
|
2024-08-26 08:07:20 +02:00
|
|
|
) {
|
|
|
|
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
|
|
|
|
focusPath: pathToFilletNode,
|
|
|
|
})
|
2024-11-16 16:49:44 -05:00
|
|
|
|
|
|
|
await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst)
|
|
|
|
|
2024-08-26 08:07:20 +02:00
|
|
|
if (updatedAst?.selections) {
|
|
|
|
editorManager.selectRange(updatedAst?.selections)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mutateAstWithTagForSketchSegment(
|
2024-10-30 16:52:17 -04:00
|
|
|
astClone: Node<Program>,
|
2024-08-26 08:07:20 +02:00
|
|
|
pathToSegmentNode: PathToNode
|
|
|
|
): { modifiedAst: Program; tag: string } | Error {
|
|
|
|
const segmentNode = getNodeFromPath<CallExpression>(
|
|
|
|
astClone,
|
2024-07-15 19:20:32 +10:00
|
|
|
pathToSegmentNode,
|
|
|
|
'CallExpression'
|
|
|
|
)
|
2024-08-26 08:07:20 +02:00
|
|
|
if (err(segmentNode)) return segmentNode
|
2024-07-15 19:20:32 +10:00
|
|
|
|
2024-08-26 08:07:20 +02:00
|
|
|
// Check whether selection is a valid segment
|
|
|
|
if (!(segmentNode.node.callee.name in sketchLineHelperMap)) {
|
2024-07-15 19:20:32 +10:00
|
|
|
return new Error('Selection is not a sketch segment')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add tag to the sketch segment or use existing tag
|
2024-08-26 08:07:20 +02:00
|
|
|
// a helper function that creates the updated node and applies the changes to the AST
|
2024-07-15 19:20:32 +10:00
|
|
|
const taggedSegment = addTagForSketchOnFace(
|
|
|
|
{
|
|
|
|
pathToNode: pathToSegmentNode,
|
2024-08-26 08:07:20 +02:00
|
|
|
node: astClone,
|
2024-07-15 19:20:32 +10:00
|
|
|
},
|
2024-09-26 18:25:05 +10:00
|
|
|
segmentNode.node.callee.name,
|
|
|
|
null
|
2024-07-15 19:20:32 +10:00
|
|
|
)
|
|
|
|
if (err(taggedSegment)) return taggedSegment
|
|
|
|
const { tag } = taggedSegment
|
|
|
|
|
2024-08-26 08:07:20 +02:00
|
|
|
return { modifiedAst: astClone, tag }
|
|
|
|
}
|
2024-07-15 19:20:32 +10:00
|
|
|
|
2024-10-11 23:25:51 +02:00
|
|
|
function getEdgeTagCall(
|
|
|
|
tag: string,
|
|
|
|
selectionType: string
|
2024-10-30 16:52:17 -04:00
|
|
|
): Node<Identifier | CallExpression> {
|
2024-10-11 23:25:51 +02:00
|
|
|
let tagCall: Expr = createIdentifier(tag)
|
|
|
|
|
|
|
|
// Modify the tag based on selectionType
|
|
|
|
if (selectionType === 'edge') {
|
|
|
|
tagCall = createCallExpressionStdLib('getOppositeEdge', [tagCall])
|
|
|
|
} else if (selectionType === 'adjacent-edge') {
|
|
|
|
tagCall = createCallExpressionStdLib('getNextAdjacentEdge', [tagCall])
|
|
|
|
}
|
|
|
|
return tagCall
|
|
|
|
}
|
|
|
|
|
2024-08-26 08:07:20 +02:00
|
|
|
function locateExtrudeDeclarator(
|
|
|
|
node: Program,
|
|
|
|
pathToExtrudeNode: PathToNode
|
|
|
|
): { extrudeDeclarator: VariableDeclarator } | Error {
|
2024-11-19 21:30:26 +01:00
|
|
|
const nodeOfExtrudeCall = getNodeFromPath<VariableDeclaration>(
|
2024-08-26 08:07:20 +02:00
|
|
|
node,
|
|
|
|
pathToExtrudeNode,
|
|
|
|
'VariableDeclaration'
|
|
|
|
)
|
2024-11-19 21:30:26 +01:00
|
|
|
if (err(nodeOfExtrudeCall)) return nodeOfExtrudeCall
|
2024-08-26 08:07:20 +02:00
|
|
|
|
2024-11-19 21:30:26 +01:00
|
|
|
const { node: extrudeVarDecl } = nodeOfExtrudeCall
|
2024-08-26 08:07:20 +02:00
|
|
|
const extrudeDeclarator = extrudeVarDecl.declarations[0]
|
|
|
|
if (!extrudeDeclarator) {
|
|
|
|
return new Error('Extrude Declarator not found.')
|
|
|
|
}
|
|
|
|
|
|
|
|
const extrudeInit = extrudeDeclarator?.init
|
|
|
|
if (!extrudeInit) {
|
|
|
|
return new Error('Extrude Init not found.')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
extrudeInit.type !== 'CallExpression' &&
|
|
|
|
extrudeInit.type !== 'PipeExpression'
|
|
|
|
) {
|
|
|
|
return new Error('Extrude must be a PipeExpression or CallExpression')
|
|
|
|
}
|
|
|
|
|
|
|
|
return { extrudeDeclarator }
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPathToNodeOfFilletLiteral(
|
|
|
|
pathToExtrudeNode: PathToNode,
|
|
|
|
extrudeDeclarator: VariableDeclarator,
|
2024-10-11 23:25:51 +02:00
|
|
|
tag: Identifier | CallExpression
|
2024-08-26 08:07:20 +02:00
|
|
|
): PathToNode {
|
|
|
|
let pathToFilletObj: PathToNode = []
|
|
|
|
let inFillet = false
|
|
|
|
|
|
|
|
traverse(extrudeDeclarator.init, {
|
|
|
|
enter(node, path) {
|
|
|
|
if (node.type === 'CallExpression' && node.callee.name === 'fillet') {
|
|
|
|
inFillet = true
|
|
|
|
}
|
|
|
|
if (inFillet && node.type === 'ObjectExpression') {
|
|
|
|
if (!hasTag(node, tag)) return false
|
|
|
|
pathToFilletObj = getPathToRadiusLiteral(node, path)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
leave(node) {
|
|
|
|
if (node.type === 'CallExpression' && node.callee.name === 'fillet') {
|
|
|
|
inFillet = false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
let indexOfPipeExpression = pathToExtrudeNode.findIndex(
|
|
|
|
(path) => path[1] === 'PipeExpression'
|
|
|
|
)
|
|
|
|
|
|
|
|
indexOfPipeExpression =
|
|
|
|
indexOfPipeExpression === -1
|
|
|
|
? pathToExtrudeNode.length
|
|
|
|
: indexOfPipeExpression
|
|
|
|
|
|
|
|
return [
|
|
|
|
...pathToExtrudeNode.slice(0, indexOfPipeExpression),
|
|
|
|
...pathToFilletObj,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2024-10-11 23:25:51 +02:00
|
|
|
function hasTag(
|
|
|
|
node: ObjectExpression,
|
|
|
|
tag: Identifier | CallExpression
|
|
|
|
): boolean {
|
2024-08-26 08:07:20 +02:00
|
|
|
return node.properties.some((prop) => {
|
|
|
|
if (prop.key.name === 'tags' && prop.value.type === 'ArrayExpression') {
|
2024-10-11 23:25:51 +02:00
|
|
|
// if selection is a base edge:
|
|
|
|
if (tag.type === 'Identifier') {
|
|
|
|
return prop.value.elements.some(
|
|
|
|
(element) =>
|
|
|
|
element.type === 'Identifier' && element.name === tag.name
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// if selection is an adjacent or opposite edge:
|
|
|
|
if (tag.type === 'CallExpression') {
|
|
|
|
return prop.value.elements.some(
|
|
|
|
(element) =>
|
|
|
|
element.type === 'CallExpression' &&
|
|
|
|
element.callee.name === tag.callee.name && // edge location
|
|
|
|
element.arguments[0].type === 'Identifier' &&
|
|
|
|
tag.arguments[0].type === 'Identifier' &&
|
|
|
|
element.arguments[0].name === tag.arguments[0].name // tag name
|
|
|
|
)
|
|
|
|
}
|
2024-08-26 08:07:20 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPathToRadiusLiteral(node: ObjectExpression, path: any): PathToNode {
|
|
|
|
let pathToFilletObj = path
|
|
|
|
node.properties.forEach((prop, index) => {
|
|
|
|
if (prop.key.name === 'radius') {
|
|
|
|
pathToFilletObj.push(
|
|
|
|
['properties', 'ObjectExpression'],
|
|
|
|
[index, 'index'],
|
|
|
|
['value', 'Property']
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return pathToFilletObj
|
|
|
|
}
|
|
|
|
|
2024-10-03 11:14:02 +02:00
|
|
|
// Button states
|
2024-08-26 08:07:20 +02:00
|
|
|
|
2024-07-15 19:20:32 +10:00
|
|
|
export const hasValidFilletSelection = ({
|
|
|
|
selectionRanges,
|
|
|
|
ast,
|
|
|
|
code,
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
2024-10-30 16:52:17 -04:00
|
|
|
ast: Node<Program>
|
2024-07-15 19:20:32 +10:00
|
|
|
code: string
|
|
|
|
}) => {
|
2024-10-11 23:25:51 +02:00
|
|
|
// check if there is anything filletable in the scene
|
2024-07-15 19:20:32 +10:00
|
|
|
let extrudeExists = false
|
|
|
|
traverse(ast, {
|
|
|
|
enter(node) {
|
|
|
|
if (node.type === 'CallExpression' && node.callee.name === 'extrude') {
|
|
|
|
extrudeExists = true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if (!extrudeExists) return false
|
|
|
|
|
2024-10-11 23:25:51 +02:00
|
|
|
// check if nothing is selected
|
|
|
|
if (selectionRanges.codeBasedSelections.length === 0) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if selection is last string in code
|
|
|
|
if (selectionRanges.codeBasedSelections[0].range[0] === code.length) {
|
|
|
|
return true
|
2024-07-15 19:20:32 +10:00
|
|
|
}
|
|
|
|
|
2024-10-11 23:25:51 +02:00
|
|
|
// selection exists:
|
|
|
|
for (const selection of selectionRanges.codeBasedSelections) {
|
|
|
|
// check if all selections are in sketchLineHelperMap
|
|
|
|
const path = getNodePathFromSourceRange(ast, selection.range)
|
2024-10-30 16:52:17 -04:00
|
|
|
const segmentNode = getNodeFromPath<Node<CallExpression>>(
|
2024-10-11 23:25:51 +02:00
|
|
|
ast,
|
|
|
|
path,
|
|
|
|
'CallExpression'
|
2024-07-15 19:20:32 +10:00
|
|
|
)
|
2024-10-11 23:25:51 +02:00
|
|
|
if (err(segmentNode)) return false
|
|
|
|
if (segmentNode.node.type !== 'CallExpression') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (!(segmentNode.node.callee.name in sketchLineHelperMap)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if selection is extruded
|
|
|
|
// TODO: option 1 : extrude is in the sketch pipe
|
|
|
|
|
|
|
|
// option 2: extrude is outside the sketch pipe
|
|
|
|
const extrudeExists = hasSketchPipeBeenExtruded(selection, ast)
|
|
|
|
if (err(extrudeExists)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (!extrudeExists) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if tag exists for the selection
|
|
|
|
let tagExists = false
|
|
|
|
let tag = ''
|
|
|
|
traverse(segmentNode.node, {
|
|
|
|
enter(node) {
|
|
|
|
if (node.type === 'TagDeclarator') {
|
|
|
|
tagExists = true
|
|
|
|
tag = node.value
|
2024-07-15 19:20:32 +10:00
|
|
|
}
|
2024-10-11 23:25:51 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// check if tag is used in fillet
|
|
|
|
if (tagExists) {
|
|
|
|
// create tag call
|
|
|
|
let tagCall: Expr = getEdgeTagCall(tag, selection.type)
|
|
|
|
|
|
|
|
// check if tag is used in fillet
|
|
|
|
let inFillet = false
|
|
|
|
let tagUsedInFillet = false
|
|
|
|
traverse(ast, {
|
|
|
|
enter(node) {
|
|
|
|
if (node.type === 'CallExpression' && node.callee.name === 'fillet') {
|
|
|
|
inFillet = true
|
|
|
|
}
|
|
|
|
if (inFillet && node.type === 'ObjectExpression') {
|
|
|
|
if (hasTag(node, tagCall)) {
|
|
|
|
tagUsedInFillet = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
leave(node) {
|
|
|
|
if (node.type === 'CallExpression' && node.callee.name === 'fillet') {
|
|
|
|
inFillet = false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if (tagUsedInFillet) {
|
|
|
|
return false
|
2024-07-15 19:20:32 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-11 23:25:51 +02:00
|
|
|
return true
|
2024-07-15 19:20:32 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
type EdgeTypes =
|
|
|
|
| 'baseEdge'
|
|
|
|
| 'getNextAdjacentEdge'
|
|
|
|
| 'getPreviousAdjacentEdge'
|
|
|
|
| 'getOppositeEdge'
|
|
|
|
|
|
|
|
export const isTagUsedInFillet = ({
|
|
|
|
ast,
|
|
|
|
callExp,
|
|
|
|
}: {
|
2024-10-30 16:52:17 -04:00
|
|
|
ast: Node<Program>
|
2024-07-15 19:20:32 +10:00
|
|
|
callExp: CallExpression
|
|
|
|
}): Array<EdgeTypes> => {
|
|
|
|
const tag = getTagFromCallExpression(callExp)
|
|
|
|
if (err(tag)) return []
|
|
|
|
|
|
|
|
let inFillet = false
|
|
|
|
let inObj = false
|
|
|
|
let inTagHelper: EdgeTypes | '' = ''
|
|
|
|
const edges: Array<EdgeTypes> = []
|
|
|
|
traverse(ast, {
|
|
|
|
enter: (node) => {
|
|
|
|
if (node.type === 'CallExpression' && node.callee.name === 'fillet') {
|
|
|
|
inFillet = true
|
|
|
|
}
|
|
|
|
if (inFillet && node.type === 'ObjectExpression') {
|
|
|
|
node.properties.forEach((prop) => {
|
|
|
|
if (
|
|
|
|
prop.key.name === 'tags' &&
|
|
|
|
prop.value.type === 'ArrayExpression'
|
|
|
|
) {
|
|
|
|
inObj = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
inObj &&
|
|
|
|
inFillet &&
|
|
|
|
node.type === 'CallExpression' &&
|
|
|
|
(node.callee.name === 'getOppositeEdge' ||
|
|
|
|
node.callee.name === 'getNextAdjacentEdge' ||
|
|
|
|
node.callee.name === 'getPreviousAdjacentEdge')
|
|
|
|
) {
|
|
|
|
inTagHelper = node.callee.name
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
inObj &&
|
|
|
|
inFillet &&
|
|
|
|
!inTagHelper &&
|
|
|
|
node.type === 'Identifier' &&
|
|
|
|
node.name === tag
|
|
|
|
) {
|
|
|
|
edges.push('baseEdge')
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
inObj &&
|
|
|
|
inFillet &&
|
|
|
|
inTagHelper &&
|
|
|
|
node.type === 'Identifier' &&
|
|
|
|
node.name === tag
|
|
|
|
) {
|
|
|
|
edges.push(inTagHelper)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
leave: (node) => {
|
|
|
|
if (node.type === 'CallExpression' && node.callee.name === 'fillet') {
|
|
|
|
inFillet = false
|
|
|
|
}
|
|
|
|
if (inFillet && node.type === 'ObjectExpression') {
|
|
|
|
node.properties.forEach((prop) => {
|
|
|
|
if (
|
|
|
|
prop.key.name === 'tags' &&
|
|
|
|
prop.value.type === 'ArrayExpression'
|
|
|
|
) {
|
|
|
|
inObj = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
inObj &&
|
|
|
|
inFillet &&
|
|
|
|
node.type === 'CallExpression' &&
|
|
|
|
(node.callee.name === 'getOppositeEdge' ||
|
|
|
|
node.callee.name === 'getNextAdjacentEdge' ||
|
|
|
|
node.callee.name === 'getPreviousAdjacentEdge')
|
|
|
|
) {
|
|
|
|
inTagHelper = ''
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return edges
|
|
|
|
}
|