Lengths and angles should be set with |abs| values (#93)
* Lengths and angles should be set with |abs| values * clean up
This commit is contained in:
@ -1,7 +1,16 @@
|
||||
import { useEffect, useState, useRef } from 'react'
|
||||
import { abstractSyntaxTree, Value } from '../lang/abstractSyntaxTree'
|
||||
import {
|
||||
abstractSyntaxTree,
|
||||
BinaryPart,
|
||||
Value,
|
||||
} from '../lang/abstractSyntaxTree'
|
||||
import { executor } from '../lang/executor'
|
||||
import { findUniqueName } from '../lang/modifyAst'
|
||||
import {
|
||||
createIdentifier,
|
||||
createLiteral,
|
||||
createUnaryExpression,
|
||||
findUniqueName,
|
||||
} from '../lang/modifyAst'
|
||||
import { findAllPreviousVariables, PrevVariable } from '../lang/queryAst'
|
||||
import { lexer } from '../lang/tokeniser'
|
||||
import { useStore } from '../useStore'
|
||||
@ -238,3 +247,32 @@ export const CreateNewVariable = ({
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export function removeDoubleNegatives(
|
||||
valueNode: BinaryPart,
|
||||
sign: number,
|
||||
variableName?: string
|
||||
): BinaryPart {
|
||||
let finValue: BinaryPart = variableName
|
||||
? createIdentifier(variableName)
|
||||
: valueNode
|
||||
if (sign === -1) finValue = createUnaryExpression(finValue)
|
||||
if (
|
||||
finValue.type === 'UnaryExpression' &&
|
||||
finValue.operator === '-' &&
|
||||
finValue.argument.type === 'UnaryExpression' &&
|
||||
finValue.argument.operator === '-'
|
||||
) {
|
||||
finValue = finValue.argument.argument
|
||||
}
|
||||
if (
|
||||
finValue.type === 'UnaryExpression' &&
|
||||
finValue.operator === '-' &&
|
||||
finValue.argument.type === 'Literal' &&
|
||||
typeof finValue.argument.value === 'number' &&
|
||||
finValue.argument.value < 0
|
||||
) {
|
||||
finValue = createLiteral(-finValue.argument.value)
|
||||
}
|
||||
return finValue
|
||||
}
|
||||
|
@ -478,6 +478,7 @@ function LineRender({
|
||||
setBaseColor('orange')
|
||||
return
|
||||
}
|
||||
try {
|
||||
const level = getConstraintLevelFromSourceRange(sourceRange, ast)
|
||||
if (level === 'free') {
|
||||
setBaseColor('orange')
|
||||
@ -486,6 +487,9 @@ function LineRender({
|
||||
} else if (level === 'full') {
|
||||
setBaseColor('lightgreen')
|
||||
}
|
||||
} catch (e) {
|
||||
setBaseColor('orange')
|
||||
}
|
||||
}, [guiMode, ast, sourceRange])
|
||||
|
||||
return (
|
||||
|
@ -19,6 +19,7 @@ export const SetAngleLengthModal = ({
|
||||
isOpen: boolean
|
||||
onResolve: (a: {
|
||||
value: string
|
||||
sign: number
|
||||
valueNode: Value
|
||||
variableName?: string
|
||||
newVariableInsertIndex: number
|
||||
@ -27,7 +28,8 @@ export const SetAngleLengthModal = ({
|
||||
value: number
|
||||
valueName: string
|
||||
}) => {
|
||||
const [value, setValue] = useState(String(initialValue))
|
||||
const [sign, setSign] = useState(Math.sign(Number(initialValue)))
|
||||
const [value, setValue] = useState(String(initialValue * sign))
|
||||
const [shouldCreateVariable, setShouldCreateVariable] = useState(false)
|
||||
|
||||
const {
|
||||
@ -39,7 +41,10 @@ export const SetAngleLengthModal = ({
|
||||
setNewVariableName,
|
||||
inputRef,
|
||||
newVariableInsertIndex,
|
||||
} = useCalc({ value, initialVariableName: valueName })
|
||||
} = useCalc({
|
||||
value,
|
||||
initialVariableName: valueName,
|
||||
})
|
||||
|
||||
return (
|
||||
<Transition appear show={isOpen} as={Fragment}>
|
||||
@ -87,7 +92,13 @@ export const SetAngleLengthModal = ({
|
||||
>
|
||||
{valueName} Value
|
||||
</label>
|
||||
<div className="mt-1">
|
||||
<div className="mt-1 flex">
|
||||
<button
|
||||
className="border border-gray-300 px-2"
|
||||
onClick={() => setSign(-sign)}
|
||||
>
|
||||
{sign > 0 ? '+' : '-'}
|
||||
</button>
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="text"
|
||||
@ -121,6 +132,7 @@ export const SetAngleLengthModal = ({
|
||||
valueNode &&
|
||||
onResolve({
|
||||
value,
|
||||
sign,
|
||||
valueNode,
|
||||
newVariableInsertIndex,
|
||||
variableName: shouldCreateVariable
|
||||
|
@ -25,6 +25,7 @@ export const GetInfoModal = ({
|
||||
valueNode: Value
|
||||
variableName?: string
|
||||
newVariableInsertIndex: number
|
||||
sign: number
|
||||
}) => void
|
||||
onReject: (a: any) => void
|
||||
segName: string
|
||||
@ -32,8 +33,9 @@ export const GetInfoModal = ({
|
||||
value: number
|
||||
initialVariableName: string
|
||||
}) => {
|
||||
const [sign, setSign] = useState(Math.sign(Number(initialValue)))
|
||||
const [segName, setSegName] = useState(initialSegName)
|
||||
const [value, setValue] = useState(String(initialValue))
|
||||
const [value, setValue] = useState(String(Math.abs(initialValue)))
|
||||
const [shouldCreateVariable, setShouldCreateVariable] = useState(false)
|
||||
|
||||
const {
|
||||
@ -45,7 +47,7 @@ export const GetInfoModal = ({
|
||||
newVariableName,
|
||||
isNewVariableNameUnique,
|
||||
newVariableInsertIndex,
|
||||
} = useCalc({ value, initialVariableName })
|
||||
} = useCalc({ value: value, initialVariableName })
|
||||
|
||||
return (
|
||||
<Transition appear show={isOpen} as={Fragment}>
|
||||
@ -93,7 +95,13 @@ export const GetInfoModal = ({
|
||||
>
|
||||
Distance
|
||||
</label>
|
||||
<div className="mt-1">
|
||||
<div className="mt-1 flex">
|
||||
<button
|
||||
className="border border-gray-300 px-2 mr-1"
|
||||
onClick={() => setSign(-sign)}
|
||||
>
|
||||
{sign > 0 ? '+' : '-'}
|
||||
</button>
|
||||
<input
|
||||
type="text"
|
||||
name="val"
|
||||
@ -145,6 +153,7 @@ export const GetInfoModal = ({
|
||||
value,
|
||||
valueNode,
|
||||
newVariableInsertIndex,
|
||||
sign,
|
||||
variableName: shouldCreateVariable
|
||||
? newVariableName
|
||||
: undefined,
|
||||
|
@ -1,7 +1,11 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { create } from 'react-modal-promise'
|
||||
import { toolTips, useStore } from '../../useStore'
|
||||
import { Value, VariableDeclarator } from '../../lang/abstractSyntaxTree'
|
||||
import {
|
||||
BinaryPart,
|
||||
Value,
|
||||
VariableDeclarator,
|
||||
} from '../../lang/abstractSyntaxTree'
|
||||
import {
|
||||
getNodePathFromSourceRange,
|
||||
getNodeFromPath,
|
||||
@ -17,6 +21,7 @@ import {
|
||||
createIdentifier,
|
||||
createVariableDeclaration,
|
||||
} from '../../lang/modifyAst'
|
||||
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
||||
|
||||
const getModalInfo = create(GetInfoModal as any)
|
||||
|
||||
@ -95,12 +100,14 @@ export const Intersect = () => {
|
||||
valueNode,
|
||||
variableName,
|
||||
newVariableInsertIndex,
|
||||
sign,
|
||||
}: {
|
||||
segName: string
|
||||
value: number
|
||||
valueNode: Value
|
||||
variableName?: string
|
||||
newVariableInsertIndex: number
|
||||
sign: number
|
||||
} = await getModalInfo({
|
||||
segName: tagInfo?.tag,
|
||||
isSegNameEditable: !tagInfo?.isTagExisting,
|
||||
@ -111,6 +118,11 @@ export const Intersect = () => {
|
||||
updateAst(modifiedAst)
|
||||
} else {
|
||||
// transform again but forcing certain values
|
||||
const finalValue = removeDoubleNegatives(
|
||||
valueNode as BinaryPart,
|
||||
sign,
|
||||
variableName
|
||||
)
|
||||
const { modifiedAst: _modifiedAst } =
|
||||
transformSecondarySketchLinesTagFirst({
|
||||
ast,
|
||||
@ -118,9 +130,7 @@ export const Intersect = () => {
|
||||
transformInfos,
|
||||
programMemory,
|
||||
forceSegName: segName,
|
||||
forceValueUsedInTransform: variableName
|
||||
? createIdentifier(variableName)
|
||||
: valueNode,
|
||||
forceValueUsedInTransform: finalValue,
|
||||
})
|
||||
if (variableName) {
|
||||
const newBody = [..._modifiedAst.body]
|
||||
|
@ -5,7 +5,6 @@ import { Value } from '../../lang/abstractSyntaxTree'
|
||||
import {
|
||||
getNodePathFromSourceRange,
|
||||
getNodeFromPath,
|
||||
findAllPreviousVariables,
|
||||
} from '../../lang/queryAst'
|
||||
import {
|
||||
TransformInfo,
|
||||
@ -13,10 +12,8 @@ import {
|
||||
transformAstSketchLines,
|
||||
} from '../../lang/std/sketchcombos'
|
||||
import { SetAngleLengthModal } from '../SetAngleLengthModal'
|
||||
import {
|
||||
createIdentifier,
|
||||
createVariableDeclaration,
|
||||
} from '../../lang/modifyAst'
|
||||
import { createVariableDeclaration } from '../../lang/modifyAst'
|
||||
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
||||
|
||||
const getModalInfo = create(SetAngleLengthModal as any)
|
||||
|
||||
@ -71,11 +68,16 @@ export const SetAngleLength = ({
|
||||
referenceSegName: '',
|
||||
})
|
||||
try {
|
||||
const { valueNode, variableName, newVariableInsertIndex } =
|
||||
const { valueNode, variableName, newVariableInsertIndex, sign } =
|
||||
await getModalInfo({
|
||||
value: valueUsedInTransform,
|
||||
valueName: angleOrLength === 'setAngle' ? 'angle' : 'length',
|
||||
} as any)
|
||||
const finalValue = removeDoubleNegatives(
|
||||
valueNode,
|
||||
sign,
|
||||
variableName
|
||||
)
|
||||
|
||||
const { modifiedAst: _modifiedAst } = transformAstSketchLines({
|
||||
ast: JSON.parse(JSON.stringify(ast)),
|
||||
@ -83,9 +85,7 @@ export const SetAngleLength = ({
|
||||
transformInfos,
|
||||
programMemory,
|
||||
referenceSegName: '',
|
||||
forceValueUsedInTransform: variableName
|
||||
? createIdentifier(variableName)
|
||||
: valueNode,
|
||||
forceValueUsedInTransform: finalValue,
|
||||
})
|
||||
if (variableName) {
|
||||
const newBody = [..._modifiedAst.body]
|
||||
|
@ -1,7 +1,11 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { create } from 'react-modal-promise'
|
||||
import { toolTips, useStore } from '../../useStore'
|
||||
import { Value, VariableDeclarator } from '../../lang/abstractSyntaxTree'
|
||||
import {
|
||||
BinaryPart,
|
||||
Value,
|
||||
VariableDeclarator,
|
||||
} from '../../lang/abstractSyntaxTree'
|
||||
import {
|
||||
getNodePathFromSourceRange,
|
||||
getNodeFromPath,
|
||||
@ -17,6 +21,7 @@ import {
|
||||
createIdentifier,
|
||||
createVariableDeclaration,
|
||||
} from '../../lang/modifyAst'
|
||||
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
||||
|
||||
const getModalInfo = create(GetInfoModal as any)
|
||||
|
||||
@ -99,12 +104,14 @@ export const SetHorzDistance = ({
|
||||
valueNode,
|
||||
variableName,
|
||||
newVariableInsertIndex,
|
||||
sign,
|
||||
}: {
|
||||
segName: string
|
||||
value: number
|
||||
valueNode: Value
|
||||
variableName?: string
|
||||
newVariableInsertIndex: number
|
||||
sign: number
|
||||
} = await getModalInfo({
|
||||
segName: tagInfo?.tag,
|
||||
isSegNameEditable: !tagInfo?.isTagExisting,
|
||||
@ -115,6 +122,12 @@ export const SetHorzDistance = ({
|
||||
if (segName === tagInfo?.tag && value === valueUsedInTransform) {
|
||||
updateAst(modifiedAst)
|
||||
} else {
|
||||
const finalValue = removeDoubleNegatives(
|
||||
valueNode as BinaryPart,
|
||||
sign,
|
||||
variableName
|
||||
)
|
||||
console.log(finalValue)
|
||||
// transform again but forcing certain values
|
||||
const { modifiedAst: _modifiedAst } =
|
||||
transformSecondarySketchLinesTagFirst({
|
||||
@ -123,9 +136,7 @@ export const SetHorzDistance = ({
|
||||
transformInfos,
|
||||
programMemory,
|
||||
forceSegName: segName,
|
||||
forceValueUsedInTransform: variableName
|
||||
? createIdentifier(variableName)
|
||||
: valueNode,
|
||||
forceValueUsedInTransform: finalValue,
|
||||
})
|
||||
if (variableName) {
|
||||
const newBody = [..._modifiedAst.body]
|
||||
|
@ -523,6 +523,15 @@ export function createBinaryExpression([left, operator, right]: [
|
||||
}
|
||||
}
|
||||
|
||||
export function createBinaryExpressionWithUnary([left, right]: [
|
||||
BinaryExpression['left'],
|
||||
BinaryExpression['right']
|
||||
]): BinaryExpression {
|
||||
if (right.type === 'UnaryExpression' && right.operator === '-')
|
||||
return createBinaryExpression([left, '-', right.argument])
|
||||
return createBinaryExpression([left, '+', right])
|
||||
}
|
||||
|
||||
export function giveSketchFnCallTag(
|
||||
ast: Program,
|
||||
range: Range,
|
||||
|
@ -331,6 +331,14 @@ show(part001)`
|
||||
})
|
||||
})
|
||||
|
||||
describe('it recasts binary expression using brackets where needed', () => {
|
||||
it('when there are two minus in a row', () => {
|
||||
const code = `const part001 = 1 - (def - abc)`
|
||||
const recasted = recast(code2ast(code).ast)
|
||||
expect(recasted).toBe(code)
|
||||
})
|
||||
})
|
||||
|
||||
// helpers
|
||||
|
||||
function code2ast(code: string): { ast: Program; tokens: Token[] } {
|
||||
|
@ -83,7 +83,8 @@ function recastBinaryExpression(expression: BinaryExpression): string {
|
||||
|
||||
const shouldWrapRight =
|
||||
expression.right.type === 'BinaryExpression' &&
|
||||
precedence(expression.operator) > precedence(expression.right.operator)
|
||||
(precedence(expression.operator) > precedence(expression.right.operator) ||
|
||||
expression.right.operator === '-')
|
||||
const shouldWrapLeft =
|
||||
expression.left.type === 'BinaryExpression' &&
|
||||
precedence(expression.operator) > precedence(expression.left.operator)
|
||||
|
@ -138,7 +138,7 @@ const part001 = startSketchAt([0, 0])
|
||||
angleToMatchLengthX('seg01', myVar2, %),
|
||||
myVar2
|
||||
], %) // ln-angledLineToX-xAbsolute should use angleToMatchLengthX to get angle
|
||||
|> angledLine([315, segLen('seg01', %)], %) // ln-angledLineToY-free should become angledLine
|
||||
|> angledLine([-45, segLen('seg01', %)], %) // ln-angledLineToY-free should become angledLine
|
||||
|> angledLine([myAng2, segLen('seg01', %)], %) // ln-angledLineToY-angle should become angledLine
|
||||
|> angledLineToY([
|
||||
angleToMatchLengthY('seg01', myVar3, %),
|
||||
@ -152,7 +152,7 @@ const part001 = startSketchAt([0, 0])
|
||||
min(segLen('seg01', %), myVar),
|
||||
-legLen(segLen('seg01', %), myVar)
|
||||
], %) // ln-legLen but negative
|
||||
|> angledLine([248, segLen('seg01', %)], %) // ln-should become angledLine
|
||||
|> angledLine([-112, segLen('seg01', %)], %) // ln-should become angledLine
|
||||
|> angledLine([myVar, segLen('seg01', %)], %) // ln-use segLen for secound arg
|
||||
|> angledLine([45, segLen('seg01', %)], %) // ln-segLen again
|
||||
|> angledLine([54, segLen('seg01', %)], %) // ln-should be transformed to angledLine
|
||||
|
@ -14,6 +14,7 @@ import {
|
||||
} from '../queryAst'
|
||||
import {
|
||||
createBinaryExpression,
|
||||
createBinaryExpressionWithUnary,
|
||||
createCallExpression,
|
||||
createIdentifier,
|
||||
createLiteral,
|
||||
@ -212,9 +213,8 @@ const getLegAng = (arg: Value, legAngleVal: BinaryPart) => {
|
||||
const ang = (arg.type === 'Literal' && Number(arg.value)) || 0
|
||||
const normalisedAngle = ((ang % 360) + 360) % 360 // between 0 and 360
|
||||
const truncatedTo90 = Math.floor(normalisedAngle / 90) * 90
|
||||
const binExp = createBinaryExpression([
|
||||
const binExp = createBinaryExpressionWithUnary([
|
||||
createLiteral(truncatedTo90),
|
||||
'+',
|
||||
legAngleVal,
|
||||
])
|
||||
return truncatedTo90 === 0 ? legAngleVal : binExp
|
||||
@ -235,7 +235,7 @@ function getClosesAngleDirection(
|
||||
const angDiff = Math.abs(currentAng - refAngle)
|
||||
const normalisedAngle = ((angDiff % 360) + 360) % 360 // between 0 and 180
|
||||
return normalisedAngle > 90
|
||||
? createBinaryExpression([angleVal, '+', createLiteral(180)])
|
||||
? createBinaryExpressionWithUnary([angleVal, createLiteral(180)])
|
||||
: angleVal
|
||||
}
|
||||
|
||||
@ -250,9 +250,8 @@ const setHorzVertDistanceCreateNode =
|
||||
getArgLiteralVal(args?.[index]) - (referencedSegment?.to?.[index] || 0),
|
||||
2
|
||||
)
|
||||
const makeBinExp = createBinaryExpression([
|
||||
const makeBinExp = createBinaryExpressionWithUnary([
|
||||
createSegEnd(referenceSegName, !index),
|
||||
'+',
|
||||
(forceValueUsedInTransform as BinaryPart) ||
|
||||
createLiteral(valueUsedInTransform),
|
||||
])
|
||||
@ -275,15 +274,15 @@ const setHorzVertDistanceForAngleLineCreateNode =
|
||||
getArgLiteralVal(args?.[1]) - (referencedSegment?.to?.[index] || 0),
|
||||
2
|
||||
)
|
||||
const makeBinExp = createBinaryExpression([
|
||||
const binExp = createBinaryExpressionWithUnary([
|
||||
createSegEnd(referenceSegName, !index),
|
||||
'+',
|
||||
(forceValueUsedInTransform as BinaryPart) ||
|
||||
createLiteral(valueUsedInTransform),
|
||||
])
|
||||
console.log('here or no?', binExp)
|
||||
return createCallWrapper(
|
||||
xOrY === 'x' ? 'angledLineToX' : 'angledLineToY',
|
||||
[varValA, makeBinExp],
|
||||
[varValA, binExp],
|
||||
tag,
|
||||
valueUsedInTransform
|
||||
)
|
||||
@ -299,9 +298,8 @@ const setHorVertDistanceForXYLines =
|
||||
getArgLiteralVal(args?.[index]) - (referencedSegment?.to?.[index] || 0),
|
||||
2
|
||||
)
|
||||
const makeBinExp = createBinaryExpression([
|
||||
const makeBinExp = createBinaryExpressionWithUnary([
|
||||
createSegEnd(referenceSegName, xOrY === 'x'),
|
||||
'+',
|
||||
(forceValueUsedInTransform as BinaryPart) ||
|
||||
createLiteral(valueUsedInTransform),
|
||||
])
|
||||
@ -318,18 +316,16 @@ const setHorzVertDistanceConstraintLineCreateNode =
|
||||
(isX: boolean): TransformInfo['createNode'] =>
|
||||
({ referenceSegName, tag, varValA, varValB }) => {
|
||||
const varVal = (isX ? varValB : varValA) as BinaryPart
|
||||
const varValBinExp = createBinaryExpression([
|
||||
const varValBinExp = createBinaryExpressionWithUnary([
|
||||
createLastSeg(!isX),
|
||||
'+',
|
||||
varVal,
|
||||
])
|
||||
|
||||
return (args, referencedSegment) => {
|
||||
const makeBinExp = (index: 0 | 1) => {
|
||||
const arg = getArgLiteralVal(args?.[index])
|
||||
return createBinaryExpression([
|
||||
return createBinaryExpressionWithUnary([
|
||||
createSegEnd(referenceSegName, isX),
|
||||
'+',
|
||||
createLiteral(
|
||||
roundOff(arg - (referencedSegment?.to?.[index] || 0), 2)
|
||||
),
|
||||
@ -1149,6 +1145,7 @@ export function getTransformInfos(
|
||||
getNodeFromPath<Value>(ast, pathToNode, 'CallExpression').node
|
||||
)
|
||||
|
||||
try {
|
||||
const theTransforms = nodes.map((node) => {
|
||||
if (node?.type === 'CallExpression')
|
||||
return getTransformInfo(node, constraintType)
|
||||
@ -1156,6 +1153,10 @@ export function getTransformInfos(
|
||||
return false
|
||||
}) as TransformInfo[]
|
||||
return theTransforms
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
export function getRemoveConstraintsTransforms(
|
||||
|
@ -20,5 +20,6 @@ export function getLength(a: [number, number], b: [number, number]): number {
|
||||
export function getAngle(a: [number, number], b: [number, number]): number {
|
||||
const x = b[0] - a[0]
|
||||
const y = b[1] - a[1]
|
||||
return ((Math.atan2(y, x) * 180) / Math.PI + 360) % 360
|
||||
const result = ((Math.atan2(y, x) * 180) / Math.PI + 360) % 360
|
||||
return result > 180 ? result - 360 : result
|
||||
}
|
||||
|
Reference in New Issue
Block a user