2024-07-02 17:16:27 +10:00
|
|
|
import { toolTips } from 'lang/langHelpers'
|
2024-09-13 21:14:14 +10:00
|
|
|
import { Program, Expr } from '../../lang/wasm'
|
2024-11-21 15:04:30 +11:00
|
|
|
import { Selections } from 'lib/selections'
|
|
|
|
import { getNodeFromPath } from '../../lang/queryAst'
|
2023-04-05 15:08:46 +10:00
|
|
|
import {
|
|
|
|
getTransformInfos,
|
|
|
|
transformAstSketchLines,
|
2023-10-16 08:54:38 +11:00
|
|
|
PathToNodeMap,
|
2024-09-13 21:14:14 +10:00
|
|
|
isExprBinaryPart,
|
2023-04-05 15:08:46 +10:00
|
|
|
} from '../../lang/std/sketchcombos'
|
2024-09-13 21:14:14 +10:00
|
|
|
import { TransformInfo } from 'lang/std/stdTypes'
|
2023-10-16 08:54:38 +11:00
|
|
|
import {
|
|
|
|
SetAngleLengthModal,
|
|
|
|
createSetAngleLengthModal,
|
|
|
|
} from '../SetAngleLengthModal'
|
2023-04-06 12:45:56 +10:00
|
|
|
import {
|
|
|
|
createIdentifier,
|
|
|
|
createVariableDeclaration,
|
|
|
|
} from '../../lang/modifyAst'
|
2023-04-05 15:08:46 +10:00
|
|
|
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
2024-03-22 16:55:30 +11:00
|
|
|
import { kclManager } from 'lib/singletons'
|
2024-06-24 11:45:40 -04:00
|
|
|
import { err } from 'lib/trap'
|
2024-10-30 16:52:17 -04:00
|
|
|
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
2023-04-05 15:08:46 +10:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
const getModalInfo = createSetAngleLengthModal(SetAngleLengthModal)
|
2023-09-16 01:23:11 -04:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
type Constraint = 'xAbs' | 'yAbs' | 'snapToYAxis' | 'snapToXAxis'
|
2023-09-16 01:23:11 -04:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
export function absDistanceInfo({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
|
|
|
constraint: Constraint
|
2024-06-24 11:45:40 -04:00
|
|
|
}):
|
|
|
|
| {
|
|
|
|
transforms: TransformInfo[]
|
|
|
|
enabled: boolean
|
|
|
|
}
|
|
|
|
| Error {
|
2023-10-16 08:54:38 +11:00
|
|
|
const disType =
|
|
|
|
constraint === 'xAbs' || constraint === 'yAbs'
|
|
|
|
? constraint
|
|
|
|
: constraint === 'snapToYAxis'
|
2023-04-06 12:45:56 +10:00
|
|
|
? 'xAbs'
|
|
|
|
: 'yAbs'
|
2024-11-21 15:04:30 +11:00
|
|
|
const _nodes = selectionRanges.graphSelections.map(({ codeRef }) => {
|
2024-08-12 15:38:42 -05:00
|
|
|
const tmp = getNodeFromPath<Expr>(
|
2024-06-24 11:45:40 -04:00
|
|
|
kclManager.ast,
|
2024-11-21 15:04:30 +11:00
|
|
|
codeRef.pathToNode,
|
2024-06-24 11:45:40 -04:00
|
|
|
'CallExpression'
|
|
|
|
)
|
|
|
|
if (err(tmp)) return tmp
|
|
|
|
return tmp.node
|
|
|
|
})
|
|
|
|
const _err1 = _nodes.find(err)
|
|
|
|
if (err(_err1)) return _err1
|
2024-08-12 15:38:42 -05:00
|
|
|
const nodes = _nodes as Expr[]
|
2024-06-24 11:45:40 -04:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
const isAllTooltips = nodes.every(
|
|
|
|
(node) =>
|
|
|
|
node?.type === 'CallExpression' &&
|
|
|
|
toolTips.includes(node.callee.name as any)
|
|
|
|
)
|
2023-04-05 15:08:46 +10:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
const transforms = getTransformInfos(selectionRanges, kclManager.ast, disType)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(transforms)) return transforms
|
2023-10-16 08:54:38 +11:00
|
|
|
|
|
|
|
const enableY =
|
|
|
|
disType === 'yAbs' &&
|
|
|
|
selectionRanges.otherSelections.length === 1 &&
|
|
|
|
selectionRanges.otherSelections[0] === 'x-axis' // select the x axis to set the distance from it i.e. y
|
|
|
|
const enableX =
|
|
|
|
disType === 'xAbs' &&
|
|
|
|
selectionRanges.otherSelections.length === 1 &&
|
|
|
|
selectionRanges.otherSelections[0] === 'y-axis' // select the y axis to set the distance from it i.e. x
|
2023-04-05 15:08:46 +10:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
const enabled =
|
|
|
|
isAllTooltips &&
|
|
|
|
transforms.every(Boolean) &&
|
2024-11-21 15:04:30 +11:00
|
|
|
selectionRanges.graphSelections.length === 1 &&
|
2023-10-16 08:54:38 +11:00
|
|
|
(enableX || enableY)
|
|
|
|
|
|
|
|
return { enabled, transforms }
|
|
|
|
}
|
2023-04-05 15:08:46 +10:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
export async function applyConstraintAbsDistance({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
|
|
|
constraint: 'xAbs' | 'yAbs'
|
|
|
|
}): Promise<{
|
|
|
|
modifiedAst: Program
|
|
|
|
pathToNodeMap: PathToNodeMap
|
|
|
|
}> {
|
2024-06-24 11:45:40 -04:00
|
|
|
const info = absDistanceInfo({
|
2023-10-16 08:54:38 +11:00
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
2024-06-24 11:45:40 -04:00
|
|
|
})
|
|
|
|
if (err(info)) return Promise.reject(info)
|
|
|
|
const transformInfos = info.transforms
|
|
|
|
|
|
|
|
const transform1 = transformAstSketchLines({
|
2024-07-25 20:11:46 -04:00
|
|
|
ast: structuredClone(kclManager.ast),
|
2024-11-21 15:04:30 +11:00
|
|
|
selectionRanges,
|
2023-10-16 08:54:38 +11:00
|
|
|
transformInfos,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
referenceSegName: '',
|
|
|
|
})
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(transform1)) return Promise.reject(transform1)
|
|
|
|
const { valueUsedInTransform } = transform1
|
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
let forceVal = valueUsedInTransform || 0
|
|
|
|
const { valueNode, variableName, newVariableInsertIndex, sign } =
|
|
|
|
await getModalInfo({
|
|
|
|
value: forceVal,
|
|
|
|
valueName: constraint === 'yAbs' ? 'yDis' : 'xDis',
|
|
|
|
})
|
2024-09-13 21:14:14 +10:00
|
|
|
if (!isExprBinaryPart(valueNode))
|
|
|
|
return Promise.reject('Invalid valueNode, is not a BinaryPart')
|
|
|
|
let finalValue = removeDoubleNegatives(valueNode, sign, variableName)
|
2023-04-05 15:08:46 +10:00
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
const transform2 = transformAstSketchLines({
|
2024-07-25 20:11:46 -04:00
|
|
|
ast: structuredClone(kclManager.ast),
|
2024-11-21 15:04:30 +11:00
|
|
|
selectionRanges,
|
2023-10-16 08:54:38 +11:00
|
|
|
transformInfos,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
referenceSegName: '',
|
|
|
|
forceValueUsedInTransform: finalValue,
|
|
|
|
})
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(transform2)) return Promise.reject(transform2)
|
|
|
|
const { modifiedAst: _modifiedAst, pathToNodeMap } = transform2
|
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
if (variableName) {
|
|
|
|
const newBody = [..._modifiedAst.body]
|
|
|
|
newBody.splice(
|
|
|
|
newVariableInsertIndex,
|
|
|
|
0,
|
|
|
|
createVariableDeclaration(variableName, valueNode)
|
|
|
|
)
|
|
|
|
_modifiedAst.body = newBody
|
2024-05-31 14:00:32 +10:00
|
|
|
Object.values(pathToNodeMap).forEach((pathToNode) => {
|
|
|
|
const index = pathToNode.findIndex((a) => a[0] === 'body') + 1
|
|
|
|
pathToNode[index][0] = Number(pathToNode[index][0]) + 1
|
|
|
|
})
|
2023-10-16 08:54:38 +11:00
|
|
|
}
|
2024-12-16 10:34:11 -05:00
|
|
|
return { modifiedAst: _modifiedAst, pathToNodeMap }
|
2023-10-16 08:54:38 +11:00
|
|
|
}
|
2023-04-06 12:45:56 +10:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
export function applyConstraintAxisAlign({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
|
|
|
constraint: 'snapToYAxis' | 'snapToXAxis'
|
2024-06-24 11:45:40 -04:00
|
|
|
}):
|
|
|
|
| {
|
2024-10-30 16:52:17 -04:00
|
|
|
modifiedAst: Node<Program>
|
2024-06-24 11:45:40 -04:00
|
|
|
pathToNodeMap: PathToNodeMap
|
|
|
|
}
|
|
|
|
| Error {
|
|
|
|
const info = absDistanceInfo({
|
2023-10-16 08:54:38 +11:00
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
2024-06-24 11:45:40 -04:00
|
|
|
})
|
|
|
|
if (err(info)) return info
|
|
|
|
const transformInfos = info.transforms
|
2023-04-05 15:08:46 +10:00
|
|
|
|
2024-02-11 18:26:09 -08:00
|
|
|
let finalValue = createIdentifier('ZERO')
|
2023-10-10 06:43:25 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
return transformAstSketchLines({
|
2024-07-25 20:11:46 -04:00
|
|
|
ast: structuredClone(kclManager.ast),
|
2024-11-21 15:04:30 +11:00
|
|
|
selectionRanges,
|
2023-10-16 08:54:38 +11:00
|
|
|
transformInfos,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
referenceSegName: '',
|
|
|
|
forceValueUsedInTransform: finalValue,
|
|
|
|
})
|
2023-04-05 15:08:46 +10:00
|
|
|
}
|