2023-10-16 21:20:05 +11:00
|
|
|
import { toolTips } from '../../useStore'
|
|
|
|
import { Selections } from 'lib/selections'
|
2023-10-16 08:54:38 +11:00
|
|
|
import { BinaryPart, Program, Value } from '../../lang/wasm'
|
2023-03-10 08:48:50 +11:00
|
|
|
import {
|
|
|
|
getNodePathFromSourceRange,
|
|
|
|
getNodeFromPath,
|
|
|
|
} from '../../lang/queryAst'
|
|
|
|
import {
|
2023-10-11 13:36:54 +11:00
|
|
|
PathToNodeMap,
|
2023-03-10 08:48:50 +11:00
|
|
|
getTransformInfos,
|
|
|
|
transformAstSketchLines,
|
|
|
|
} from '../../lang/std/sketchcombos'
|
2023-10-16 08:54:38 +11:00
|
|
|
import {
|
|
|
|
SetAngleLengthModal,
|
|
|
|
createSetAngleLengthModal,
|
|
|
|
} from '../SetAngleLengthModal'
|
2023-04-03 20:40:58 +10:00
|
|
|
import {
|
|
|
|
createBinaryExpressionWithUnary,
|
|
|
|
createIdentifier,
|
|
|
|
createVariableDeclaration,
|
|
|
|
} from '../../lang/modifyAst'
|
2023-04-02 17:20:11 +10:00
|
|
|
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
2023-04-03 20:40:58 +10:00
|
|
|
import { normaliseAngle } from '../../lib/utils'
|
2023-10-11 13:36:54 +11:00
|
|
|
import { kclManager } from 'lang/KclSinglton'
|
2023-03-10 08:48:50 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
const getModalInfo = createSetAngleLengthModal(SetAngleLengthModal)
|
2023-10-11 13:36:54 +11:00
|
|
|
|
|
|
|
export function setAngleLengthInfo({
|
|
|
|
selectionRanges,
|
|
|
|
angleOrLength = 'setLength',
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
|
|
|
angleOrLength?: 'setLength' | 'setAngle'
|
|
|
|
}) {
|
|
|
|
const paths = selectionRanges.codeBasedSelections.map(({ range }) =>
|
|
|
|
getNodePathFromSourceRange(kclManager.ast, range)
|
|
|
|
)
|
|
|
|
const nodes = paths.map(
|
|
|
|
(pathToNode) =>
|
|
|
|
getNodeFromPath<Value>(kclManager.ast, pathToNode, 'CallExpression').node
|
|
|
|
)
|
|
|
|
const isAllTooltips = nodes.every(
|
|
|
|
(node) =>
|
|
|
|
node?.type === 'CallExpression' &&
|
|
|
|
toolTips.includes(node.callee.name as any)
|
|
|
|
)
|
|
|
|
|
|
|
|
const transforms = getTransformInfos(
|
|
|
|
selectionRanges,
|
|
|
|
kclManager.ast,
|
|
|
|
angleOrLength
|
|
|
|
)
|
|
|
|
const enabled = isAllTooltips && transforms.every(Boolean)
|
|
|
|
return { enabled, transforms }
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function applyConstraintAngleLength({
|
|
|
|
selectionRanges,
|
|
|
|
angleOrLength = 'setLength',
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
|
|
|
angleOrLength?: 'setLength' | 'setAngle'
|
|
|
|
}): Promise<{
|
|
|
|
modifiedAst: Program
|
|
|
|
pathToNodeMap: PathToNodeMap
|
|
|
|
}> {
|
|
|
|
const { transforms } = setAngleLengthInfo({ selectionRanges, angleOrLength })
|
|
|
|
const { valueUsedInTransform } = transformAstSketchLines({
|
|
|
|
ast: JSON.parse(JSON.stringify(kclManager.ast)),
|
|
|
|
selectionRanges,
|
|
|
|
transformInfos: transforms,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
referenceSegName: '',
|
|
|
|
})
|
|
|
|
try {
|
|
|
|
const isReferencingYAxis =
|
|
|
|
selectionRanges.otherSelections.length === 1 &&
|
|
|
|
selectionRanges.otherSelections[0] === 'y-axis'
|
|
|
|
const isReferencingYAxisAngle =
|
|
|
|
isReferencingYAxis && angleOrLength === 'setAngle'
|
|
|
|
|
|
|
|
const isReferencingXAxis =
|
|
|
|
selectionRanges.otherSelections.length === 1 &&
|
|
|
|
selectionRanges.otherSelections[0] === 'x-axis'
|
|
|
|
const isReferencingXAxisAngle =
|
|
|
|
isReferencingXAxis && angleOrLength === 'setAngle'
|
|
|
|
|
|
|
|
let forceVal = valueUsedInTransform || 0
|
|
|
|
let calcIdentifier = createIdentifier('_0')
|
|
|
|
if (isReferencingYAxisAngle) {
|
|
|
|
calcIdentifier = createIdentifier(forceVal < 0 ? '_270' : '_90')
|
|
|
|
forceVal = normaliseAngle(forceVal + (forceVal < 0 ? 90 : -90))
|
|
|
|
} else if (isReferencingXAxisAngle) {
|
|
|
|
calcIdentifier = createIdentifier(Math.abs(forceVal) > 90 ? '_180' : '_0')
|
|
|
|
forceVal =
|
|
|
|
Math.abs(forceVal) > 90 ? normaliseAngle(forceVal - 180) : forceVal
|
|
|
|
}
|
|
|
|
const { valueNode, variableName, newVariableInsertIndex, sign } =
|
|
|
|
await getModalInfo({
|
|
|
|
value: forceVal,
|
|
|
|
valueName: angleOrLength === 'setAngle' ? 'angle' : 'length',
|
|
|
|
shouldCreateVariable: true,
|
2023-10-16 08:54:38 +11:00
|
|
|
})
|
|
|
|
|
|
|
|
let finalValue = removeDoubleNegatives(
|
|
|
|
valueNode as BinaryPart,
|
|
|
|
sign,
|
|
|
|
variableName
|
|
|
|
)
|
2023-10-11 13:36:54 +11:00
|
|
|
if (
|
|
|
|
isReferencingYAxisAngle ||
|
|
|
|
(isReferencingXAxisAngle && calcIdentifier.name !== '_0')
|
|
|
|
) {
|
|
|
|
finalValue = createBinaryExpressionWithUnary([calcIdentifier, finalValue])
|
|
|
|
}
|
|
|
|
|
|
|
|
const { modifiedAst: _modifiedAst, pathToNodeMap } =
|
|
|
|
transformAstSketchLines({
|
|
|
|
ast: JSON.parse(JSON.stringify(kclManager.ast)),
|
|
|
|
selectionRanges,
|
|
|
|
transformInfos: transforms,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
referenceSegName: '',
|
|
|
|
forceValueUsedInTransform: finalValue,
|
|
|
|
})
|
|
|
|
if (variableName) {
|
|
|
|
const newBody = [..._modifiedAst.body]
|
|
|
|
newBody.splice(
|
|
|
|
newVariableInsertIndex,
|
|
|
|
0,
|
|
|
|
createVariableDeclaration(variableName, valueNode)
|
|
|
|
)
|
|
|
|
_modifiedAst.body = newBody
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
modifiedAst: _modifiedAst,
|
|
|
|
pathToNodeMap,
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log('erorr', e)
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
}
|