set angle between two selected lines (#104)
This commit is contained in:
@ -10,6 +10,7 @@ import { SetHorzVertDistance } from './components/Toolbar/SetHorzVertDistance'
|
||||
import { SetAngleLength } from './components/Toolbar/SetAngleLength'
|
||||
import { ConvertToVariable } from './components/Toolbar/ConvertVariable'
|
||||
import { SetAbsDistance } from './components/Toolbar/SetAbsDistance'
|
||||
import { SetAngleBetween } from './components/Toolbar/setAngleBetween'
|
||||
|
||||
export const Toolbar = () => {
|
||||
const {
|
||||
@ -178,6 +179,7 @@ export const Toolbar = () => {
|
||||
<SetAngleLength angleOrLength="setLength" />
|
||||
<Intersect />
|
||||
<RemoveConstrainingValues />
|
||||
<SetAngleBetween />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
@ -17,10 +17,7 @@ import {
|
||||
getTransformInfos,
|
||||
} from '../../lang/std/sketchcombos'
|
||||
import { GetInfoModal } from '../SetHorVertDistanceModal'
|
||||
import {
|
||||
createIdentifier,
|
||||
createVariableDeclaration,
|
||||
} from '../../lang/modifyAst'
|
||||
import { createVariableDeclaration } from '../../lang/modifyAst'
|
||||
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
||||
|
||||
const getModalInfo = create(GetInfoModal as any)
|
||||
|
153
src/components/Toolbar/setAngleBetween.tsx
Normal file
153
src/components/Toolbar/setAngleBetween.tsx
Normal file
@ -0,0 +1,153 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { create } from 'react-modal-promise'
|
||||
import { toolTips, useStore } from '../../useStore'
|
||||
import {
|
||||
BinaryPart,
|
||||
Value,
|
||||
VariableDeclarator,
|
||||
} from '../../lang/abstractSyntaxTree'
|
||||
import {
|
||||
getNodePathFromSourceRange,
|
||||
getNodeFromPath,
|
||||
} from '../../lang/queryAst'
|
||||
import { isSketchVariablesLinked } from '../../lang/std/sketchConstraints'
|
||||
import {
|
||||
TransformInfo,
|
||||
transformSecondarySketchLinesTagFirst,
|
||||
getTransformInfos,
|
||||
} from '../../lang/std/sketchcombos'
|
||||
import { GetInfoModal } from '../SetHorVertDistanceModal'
|
||||
import { createVariableDeclaration } from '../../lang/modifyAst'
|
||||
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
||||
|
||||
const getModalInfo = create(GetInfoModal as any)
|
||||
|
||||
export const SetAngleBetween = () => {
|
||||
const { guiMode, selectionRanges, ast, programMemory, updateAst } = useStore(
|
||||
(s) => ({
|
||||
guiMode: s.guiMode,
|
||||
ast: s.ast,
|
||||
updateAst: s.updateAst,
|
||||
selectionRanges: s.selectionRanges,
|
||||
programMemory: s.programMemory,
|
||||
})
|
||||
)
|
||||
const [enable, setEnable] = useState(false)
|
||||
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
|
||||
useEffect(() => {
|
||||
if (!ast) return
|
||||
const paths = selectionRanges.codeBasedSelections.map(({ range }) =>
|
||||
getNodePathFromSourceRange(ast, range)
|
||||
)
|
||||
const nodes = paths.map(
|
||||
(pathToNode) => getNodeFromPath<Value>(ast, pathToNode).node
|
||||
)
|
||||
const varDecs = paths.map(
|
||||
(pathToNode) =>
|
||||
getNodeFromPath<VariableDeclarator>(
|
||||
ast,
|
||||
pathToNode,
|
||||
'VariableDeclarator'
|
||||
)?.node
|
||||
)
|
||||
const primaryLine = varDecs[0]
|
||||
const secondaryVarDecs = varDecs.slice(1)
|
||||
const isOthersLinkedToPrimary = secondaryVarDecs.every((secondary) =>
|
||||
isSketchVariablesLinked(secondary, primaryLine, 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),
|
||||
},
|
||||
ast,
|
||||
'setAngleBetween'
|
||||
)
|
||||
setTransformInfos(theTransforms)
|
||||
|
||||
const _enableEqual =
|
||||
secondaryVarDecs.length === 1 &&
|
||||
isAllTooltips &&
|
||||
isOthersLinkedToPrimary &&
|
||||
theTransforms.every(Boolean)
|
||||
setEnable(_enableEqual)
|
||||
}, [guiMode, selectionRanges])
|
||||
if (guiMode.mode !== 'sketch') return null
|
||||
|
||||
return (
|
||||
<button
|
||||
onClick={async () => {
|
||||
if (transformInfos && ast) {
|
||||
const { modifiedAst, tagInfo, valueUsedInTransform } =
|
||||
transformSecondarySketchLinesTagFirst({
|
||||
ast: JSON.parse(JSON.stringify(ast)),
|
||||
selectionRanges,
|
||||
transformInfos,
|
||||
programMemory,
|
||||
})
|
||||
const {
|
||||
segName,
|
||||
value,
|
||||
valueNode,
|
||||
variableName,
|
||||
newVariableInsertIndex,
|
||||
sign,
|
||||
}: {
|
||||
segName: string
|
||||
value: number
|
||||
valueNode: Value
|
||||
variableName?: string
|
||||
newVariableInsertIndex: number
|
||||
sign: number
|
||||
} = await getModalInfo({
|
||||
segName: tagInfo?.tag,
|
||||
isSegNameEditable: !tagInfo?.isTagExisting,
|
||||
value: valueUsedInTransform,
|
||||
initialVariableName: 'angle',
|
||||
} as any)
|
||||
if (segName === tagInfo?.tag && value === valueUsedInTransform) {
|
||||
updateAst(modifiedAst)
|
||||
} else {
|
||||
const finalValue = removeDoubleNegatives(
|
||||
valueNode as BinaryPart,
|
||||
sign,
|
||||
variableName
|
||||
)
|
||||
// transform again but forcing certain values
|
||||
const { modifiedAst: _modifiedAst } =
|
||||
transformSecondarySketchLinesTagFirst({
|
||||
ast,
|
||||
selectionRanges,
|
||||
transformInfos,
|
||||
programMemory,
|
||||
forceSegName: segName,
|
||||
forceValueUsedInTransform: finalValue,
|
||||
})
|
||||
if (variableName) {
|
||||
const newBody = [..._modifiedAst.body]
|
||||
newBody.splice(
|
||||
newVariableInsertIndex,
|
||||
0,
|
||||
createVariableDeclaration(variableName, valueNode)
|
||||
)
|
||||
_modifiedAst.body = newBody
|
||||
}
|
||||
updateAst(_modifiedAst)
|
||||
}
|
||||
}
|
||||
}}
|
||||
className={`border m-1 px-1 rounded text-xs ${
|
||||
enable ? 'bg-gray-50 text-gray-800' : 'bg-gray-200 text-gray-400'
|
||||
}`}
|
||||
disabled={!enable}
|
||||
>
|
||||
angleBetween
|
||||
</button>
|
||||
)
|
||||
}
|
@ -953,7 +953,15 @@ export const angledLineToX: SketchLineHelper = {
|
||||
previousSketch
|
||||
)
|
||||
},
|
||||
add: ({ node, pathToNode, to, from, createCallback, replaceExisting }) => {
|
||||
add: ({
|
||||
node,
|
||||
pathToNode,
|
||||
to,
|
||||
from,
|
||||
createCallback,
|
||||
replaceExisting,
|
||||
referencedSegment,
|
||||
}) => {
|
||||
const _node = { ...node }
|
||||
const { node: pipe } = getNodeFromPath<PipeExpression>(
|
||||
_node,
|
||||
@ -963,7 +971,10 @@ export const angledLineToX: SketchLineHelper = {
|
||||
const angle = createLiteral(roundOff(getAngle(from, to), 0))
|
||||
const xArg = createLiteral(roundOff(to[0], 2))
|
||||
if (replaceExisting && createCallback) {
|
||||
const { callExp, valueUsedInTransform } = createCallback([angle, xArg])
|
||||
const { callExp, valueUsedInTransform } = createCallback(
|
||||
[angle, xArg],
|
||||
referencedSegment
|
||||
)
|
||||
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
||||
pipe.body[callIndex] = callExp
|
||||
return {
|
||||
@ -1038,7 +1049,15 @@ export const angledLineToY: SketchLineHelper = {
|
||||
previousSketch
|
||||
)
|
||||
},
|
||||
add: ({ node, pathToNode, to, from, createCallback, replaceExisting }) => {
|
||||
add: ({
|
||||
node,
|
||||
pathToNode,
|
||||
to,
|
||||
from,
|
||||
createCallback,
|
||||
replaceExisting,
|
||||
referencedSegment,
|
||||
}) => {
|
||||
const _node = { ...node }
|
||||
const { node: pipe } = getNodeFromPath<PipeExpression>(
|
||||
_node,
|
||||
@ -1049,7 +1068,10 @@ export const angledLineToY: SketchLineHelper = {
|
||||
const yArg = createLiteral(roundOff(to[1], 2))
|
||||
|
||||
if (replaceExisting && createCallback) {
|
||||
const { callExp, valueUsedInTransform } = createCallback([angle, yArg])
|
||||
const { callExp, valueUsedInTransform } = createCallback(
|
||||
[angle, yArg],
|
||||
referencedSegment
|
||||
)
|
||||
const { index: callIndex } = splitPathAtPipeExpression(pathToNode)
|
||||
pipe.body[callIndex] = callExp
|
||||
return {
|
||||
|
@ -13,6 +13,7 @@ import {
|
||||
getNodePathFromSourceRange,
|
||||
} from '../queryAst'
|
||||
import {
|
||||
createBinaryExpression,
|
||||
createBinaryExpressionWithUnary,
|
||||
createCallExpression,
|
||||
createIdentifier,
|
||||
@ -25,7 +26,7 @@ import {
|
||||
import { createFirstArg, getFirstArg, replaceSketchLine } from './sketch'
|
||||
import { ProgramMemory } from '../executor'
|
||||
import { getSketchSegmentFromSourceRange } from './sketchConstraints'
|
||||
import { getAngle, roundOff } from '../../lib/utils'
|
||||
import { getAngle, roundOff, normaliseAngle } from '../../lib/utils'
|
||||
|
||||
type LineInputsType =
|
||||
| 'xAbsolute'
|
||||
@ -48,6 +49,7 @@ export type ConstraintType =
|
||||
| 'removeConstrainingValues'
|
||||
| 'xAbs'
|
||||
| 'yAbs'
|
||||
| 'setAngleBetween'
|
||||
|
||||
function createCallWrapper(
|
||||
a: TooTip,
|
||||
@ -294,13 +296,12 @@ const setAbsDistanceCreateNode =
|
||||
isXOrYLine = false,
|
||||
index = xOrY === 'x' ? 0 : 1
|
||||
): TransformInfo['createNode'] =>
|
||||
({ tag, forceValueUsedInTransform, ...rest }) => {
|
||||
return (args, referencedSegment, ...rest2) => {
|
||||
({ tag, forceValueUsedInTransform }) => {
|
||||
return (args, referencedSegment) => {
|
||||
const valueUsedInTransform = roundOff(
|
||||
getArgLiteralVal(args?.[index]) - (referencedSegment?.to?.[index] || 0),
|
||||
2
|
||||
)
|
||||
console.log(rest, rest2)
|
||||
const val =
|
||||
(forceValueUsedInTransform as BinaryPart) ||
|
||||
createLiteral(valueUsedInTransform)
|
||||
@ -434,6 +435,49 @@ const setAngledIntersectForAngledLines: TransformInfo['createNode'] =
|
||||
})
|
||||
}
|
||||
|
||||
const setAngleBetweenCreateNode =
|
||||
(tranformToType: 'none' | 'xAbs' | 'yAbs'): TransformInfo['createNode'] =>
|
||||
({ referenceSegName, tag, forceValueUsedInTransform, varValA, varValB }) => {
|
||||
return (args, referencedSegment) => {
|
||||
const refAngle = referencedSegment
|
||||
? getAngle(referencedSegment?.from, referencedSegment?.to)
|
||||
: 0
|
||||
let valueUsedInTransform = roundOff(
|
||||
normaliseAngle(
|
||||
(args[0].type === 'Literal' ? Number(args[0].value) : 0) - refAngle
|
||||
)
|
||||
)
|
||||
let firstHalfValue = createSegAngle(referenceSegName) as BinaryPart
|
||||
if (Math.abs(valueUsedInTransform) > 90) {
|
||||
firstHalfValue = createBinaryExpression([
|
||||
firstHalfValue,
|
||||
'+',
|
||||
createIdentifier('_180'),
|
||||
])
|
||||
valueUsedInTransform = normaliseAngle(valueUsedInTransform - 180)
|
||||
}
|
||||
const binExp = createBinaryExpressionWithUnary([
|
||||
firstHalfValue,
|
||||
(forceValueUsedInTransform as BinaryPart) ||
|
||||
createLiteral(valueUsedInTransform),
|
||||
])
|
||||
return createCallWrapper(
|
||||
tranformToType === 'none'
|
||||
? 'angledLine'
|
||||
: tranformToType === 'xAbs'
|
||||
? 'angledLineToX'
|
||||
: 'angledLineToY',
|
||||
tranformToType === 'none'
|
||||
? [binExp, args[1]]
|
||||
: tranformToType === 'xAbs'
|
||||
? [binExp, varValA]
|
||||
: [binExp, varValB],
|
||||
tag,
|
||||
valueUsedInTransform
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const transformMap: TransformMap = {
|
||||
line: {
|
||||
xRelative: {
|
||||
@ -543,6 +587,10 @@ const transformMap: TransformMap = {
|
||||
tooltip: 'angledLineThatIntersects',
|
||||
createNode: setAngledIntersectLineForLines,
|
||||
},
|
||||
setAngleBetween: {
|
||||
tooltip: 'angledLine',
|
||||
createNode: setAngleBetweenCreateNode('none'),
|
||||
},
|
||||
},
|
||||
},
|
||||
lineTo: {
|
||||
@ -594,6 +642,10 @@ const transformMap: TransformMap = {
|
||||
() =>
|
||||
createCallWrapper('xLineTo', varValA, tag),
|
||||
},
|
||||
setAngleBetween: {
|
||||
tooltip: 'angledLineToX',
|
||||
createNode: setAngleBetweenCreateNode('xAbs'),
|
||||
},
|
||||
},
|
||||
yAbsolute: {
|
||||
equalLength: {
|
||||
@ -636,6 +688,10 @@ const transformMap: TransformMap = {
|
||||
)
|
||||
},
|
||||
},
|
||||
setAngleBetween: {
|
||||
tooltip: 'angledLineToY',
|
||||
createNode: setAngleBetweenCreateNode('yAbs'),
|
||||
},
|
||||
},
|
||||
},
|
||||
angledLine: {
|
||||
|
Reference in New Issue
Block a user