2023-10-16 21:20:05 +11:00
|
|
|
import { toolTips } from '../../useStore'
|
|
|
|
import { Selections } from 'lib/selections'
|
2023-10-11 13:36:54 +11:00
|
|
|
import { BinaryPart, Program, Value, VariableDeclarator } from '../../lang/wasm'
|
2023-04-05 21:06:20 +10:00
|
|
|
import {
|
|
|
|
getNodePathFromSourceRange,
|
|
|
|
getNodeFromPath,
|
|
|
|
} from '../../lang/queryAst'
|
|
|
|
import { isSketchVariablesLinked } from '../../lang/std/sketchConstraints'
|
|
|
|
import {
|
|
|
|
transformSecondarySketchLinesTagFirst,
|
|
|
|
getTransformInfos,
|
2023-10-11 13:36:54 +11:00
|
|
|
PathToNodeMap,
|
2024-06-24 11:45:40 -04:00
|
|
|
TransformInfo,
|
2023-04-05 21:06:20 +10:00
|
|
|
} from '../../lang/std/sketchcombos'
|
2023-10-16 08:54:38 +11:00
|
|
|
import { GetInfoModal, createInfoModal } from '../SetHorVertDistanceModal'
|
2023-04-05 21:06:20 +10:00
|
|
|
import { createVariableDeclaration } from '../../lang/modifyAst'
|
|
|
|
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'
|
2023-04-05 21:06:20 +10:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
const getModalInfo = createInfoModal(GetInfoModal)
|
2023-10-11 13:36:54 +11:00
|
|
|
|
|
|
|
export function angleBetweenInfo({
|
|
|
|
selectionRanges,
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
2024-06-24 11:45:40 -04:00
|
|
|
}):
|
|
|
|
| {
|
|
|
|
transforms: TransformInfo[]
|
|
|
|
enabled: boolean
|
|
|
|
}
|
|
|
|
| Error {
|
2023-10-11 13:36:54 +11:00
|
|
|
const paths = selectionRanges.codeBasedSelections.map(({ range }) =>
|
|
|
|
getNodePathFromSourceRange(kclManager.ast, range)
|
|
|
|
)
|
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
const _nodes = paths.map((pathToNode) => {
|
|
|
|
const tmp = getNodeFromPath<Value>(kclManager.ast, pathToNode)
|
|
|
|
if (err(tmp)) return tmp
|
|
|
|
return tmp.node
|
|
|
|
})
|
|
|
|
const _err1 = _nodes.find(err)
|
|
|
|
if (err(_err1)) return _err1
|
|
|
|
const nodes = _nodes as Value[]
|
|
|
|
|
|
|
|
const _varDecs = paths.map((pathToNode) => {
|
|
|
|
const tmp = getNodeFromPath<VariableDeclarator>(
|
|
|
|
kclManager.ast,
|
|
|
|
pathToNode,
|
|
|
|
'VariableDeclarator'
|
|
|
|
)
|
|
|
|
if (err(tmp)) return tmp
|
|
|
|
return tmp.node
|
|
|
|
})
|
|
|
|
const _err2 = _varDecs.find(err)
|
|
|
|
if (err(_err2)) return _err2
|
|
|
|
const varDecs = _varDecs as VariableDeclarator[]
|
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
const primaryLine = varDecs[0]
|
|
|
|
const secondaryVarDecs = varDecs.slice(1)
|
|
|
|
const isOthersLinkedToPrimary = secondaryVarDecs.every((secondary) =>
|
|
|
|
isSketchVariablesLinked(secondary, primaryLine, kclManager.ast)
|
|
|
|
)
|
|
|
|
const isAllTooltips = nodes.every(
|
|
|
|
(node) =>
|
|
|
|
node?.type === 'CallExpression' &&
|
|
|
|
toolTips.includes(node.callee.name as any)
|
|
|
|
)
|
|
|
|
|
|
|
|
const theTransforms = getTransformInfos(
|
|
|
|
{
|
|
|
|
...selectionRanges,
|
|
|
|
codeBasedSelections: selectionRanges.codeBasedSelections.slice(1),
|
|
|
|
},
|
|
|
|
kclManager.ast,
|
|
|
|
'setAngleBetween'
|
|
|
|
)
|
|
|
|
|
|
|
|
const _enableEqual =
|
2023-12-01 20:18:51 +11:00
|
|
|
selectionRanges.otherSelections.length === 0 &&
|
2023-10-11 13:36:54 +11:00
|
|
|
secondaryVarDecs.length === 1 &&
|
|
|
|
isAllTooltips &&
|
|
|
|
isOthersLinkedToPrimary &&
|
|
|
|
theTransforms.every(Boolean)
|
|
|
|
return { enabled: _enableEqual, transforms: theTransforms }
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function applyConstraintAngleBetween({
|
|
|
|
selectionRanges,
|
|
|
|
}: // constraint,
|
|
|
|
{
|
|
|
|
selectionRanges: Selections
|
|
|
|
// constraint: 'setHorzDistance' | 'setVertDistance'
|
|
|
|
}): Promise<{
|
|
|
|
modifiedAst: Program
|
|
|
|
pathToNodeMap: PathToNodeMap
|
|
|
|
}> {
|
2024-06-24 11:45:40 -04:00
|
|
|
const info = angleBetweenInfo({ selectionRanges })
|
|
|
|
if (err(info)) return Promise.reject(info)
|
|
|
|
const transformInfos = info.transforms
|
|
|
|
|
|
|
|
const transformed1 = transformSecondarySketchLinesTagFirst({
|
|
|
|
ast: JSON.parse(JSON.stringify(kclManager.ast)),
|
|
|
|
selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
})
|
|
|
|
if (err(transformed1)) return Promise.reject(transformed1)
|
2023-10-11 13:36:54 +11:00
|
|
|
const { modifiedAst, tagInfo, valueUsedInTransform, pathToNodeMap } =
|
2024-06-24 11:45:40 -04:00
|
|
|
transformed1
|
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
const {
|
|
|
|
segName,
|
|
|
|
value,
|
|
|
|
valueNode,
|
|
|
|
variableName,
|
|
|
|
newVariableInsertIndex,
|
|
|
|
sign,
|
|
|
|
} = await getModalInfo({
|
|
|
|
segName: tagInfo?.tag,
|
|
|
|
isSegNameEditable: !tagInfo?.isTagExisting,
|
|
|
|
value: valueUsedInTransform,
|
|
|
|
initialVariableName: 'angle',
|
|
|
|
} as any)
|
2024-05-31 11:36:08 +10:00
|
|
|
if (
|
|
|
|
segName === tagInfo?.tag &&
|
|
|
|
Number(value) === valueUsedInTransform &&
|
|
|
|
!variableName
|
|
|
|
) {
|
2023-10-11 13:36:54 +11:00
|
|
|
return {
|
|
|
|
modifiedAst,
|
|
|
|
pathToNodeMap,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const finalValue = removeDoubleNegatives(
|
|
|
|
valueNode as BinaryPart,
|
|
|
|
sign,
|
|
|
|
variableName
|
|
|
|
)
|
|
|
|
// transform again but forcing certain values
|
2024-06-24 11:45:40 -04:00
|
|
|
const transformed2 = transformSecondarySketchLinesTagFirst({
|
|
|
|
ast: kclManager.ast,
|
|
|
|
selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
forceSegName: segName,
|
|
|
|
forceValueUsedInTransform: finalValue,
|
|
|
|
})
|
|
|
|
if (err(transformed2)) return Promise.reject(transformed2)
|
2023-10-11 13:36:54 +11:00
|
|
|
const { modifiedAst: _modifiedAst, pathToNodeMap: _pathToNodeMap } =
|
2024-06-24 11:45:40 -04:00
|
|
|
transformed2
|
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
if (variableName) {
|
|
|
|
const newBody = [..._modifiedAst.body]
|
|
|
|
newBody.splice(
|
|
|
|
newVariableInsertIndex,
|
|
|
|
0,
|
|
|
|
createVariableDeclaration(variableName, valueNode)
|
|
|
|
)
|
|
|
|
_modifiedAst.body = newBody
|
2024-05-31 11:36:08 +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-11 13:36:54 +11:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
modifiedAst: _modifiedAst,
|
|
|
|
pathToNodeMap: _pathToNodeMap,
|
|
|
|
}
|
|
|
|
}
|