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-04-05 15:08:46 +10:00
|
|
|
import {
|
|
|
|
getNodePathFromSourceRange,
|
|
|
|
getNodeFromPath,
|
|
|
|
} from '../../lang/queryAst'
|
|
|
|
import {
|
|
|
|
getTransformInfos,
|
|
|
|
transformAstSketchLines,
|
2023-10-16 08:54:38 +11:00
|
|
|
PathToNodeMap,
|
2023-04-05 15:08:46 +10:00
|
|
|
} from '../../lang/std/sketchcombos'
|
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-02-11 12:59:00 +11:00
|
|
|
import { kclManager } from 'lang/KclSingleton'
|
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
|
|
|
|
}) {
|
|
|
|
const disType =
|
|
|
|
constraint === 'xAbs' || constraint === 'yAbs'
|
|
|
|
? constraint
|
|
|
|
: constraint === 'snapToYAxis'
|
2023-04-06 12:45:56 +10:00
|
|
|
? 'xAbs'
|
|
|
|
: 'yAbs'
|
2023-10-16 08:54:38 +11:00
|
|
|
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)
|
|
|
|
)
|
2023-04-05 15:08:46 +10:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
const transforms = getTransformInfos(selectionRanges, kclManager.ast, disType)
|
|
|
|
|
|
|
|
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) &&
|
|
|
|
selectionRanges.codeBasedSelections.length === 1 &&
|
|
|
|
(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
|
|
|
|
}> {
|
|
|
|
const transformInfos = absDistanceInfo({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
}).transforms
|
|
|
|
const { valueUsedInTransform } = transformAstSketchLines({
|
|
|
|
ast: JSON.parse(JSON.stringify(kclManager.ast)),
|
|
|
|
selectionRanges: selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
referenceSegName: '',
|
|
|
|
})
|
|
|
|
let forceVal = valueUsedInTransform || 0
|
|
|
|
const { valueNode, variableName, newVariableInsertIndex, sign } =
|
|
|
|
await getModalInfo({
|
|
|
|
value: forceVal,
|
|
|
|
valueName: constraint === 'yAbs' ? 'yDis' : 'xDis',
|
|
|
|
})
|
|
|
|
let finalValue = removeDoubleNegatives(
|
|
|
|
valueNode as BinaryPart,
|
|
|
|
sign,
|
|
|
|
variableName
|
|
|
|
)
|
2023-04-05 15:08:46 +10:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
const { modifiedAst: _modifiedAst, pathToNodeMap } = transformAstSketchLines({
|
|
|
|
ast: JSON.parse(JSON.stringify(kclManager.ast)),
|
|
|
|
selectionRanges: selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
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 }
|
|
|
|
}
|
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'
|
|
|
|
}): {
|
|
|
|
modifiedAst: Program
|
|
|
|
pathToNodeMap: PathToNodeMap
|
|
|
|
} {
|
|
|
|
const transformInfos = absDistanceInfo({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
}).transforms
|
2023-04-05 15:08:46 +10:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
let finalValue = createIdentifier('_0')
|
2023-10-10 06:43:25 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
return transformAstSketchLines({
|
|
|
|
ast: JSON.parse(JSON.stringify(kclManager.ast)),
|
|
|
|
selectionRanges: selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
referenceSegName: '',
|
|
|
|
forceValueUsedInTransform: finalValue,
|
|
|
|
})
|
2023-04-05 15:08:46 +10:00
|
|
|
}
|