fix horz vert distance contraint (#2572)

fix hor vert distance contraint
This commit is contained in:
Kurt Hutten
2024-06-03 15:37:23 +10:00
committed by GitHub
parent 59b1319e50
commit f52d2d55f1
4 changed files with 151 additions and 6 deletions

View File

@ -1,7 +1,7 @@
import { test, expect, Page } from '@playwright/test' import { test, expect, Page } from '@playwright/test'
import { makeTemplate, getUtils, doExport } from './test-utils' import { makeTemplate, getUtils, doExport } from './test-utils'
import waitOn from 'wait-on' import waitOn from 'wait-on'
import { roundOff, uuidv4 } from 'lib/utils' import { XOR, roundOff, uuidv4 } from 'lib/utils'
import { SaveSettingsPayload } from 'lib/settings/settingsTypes' import { SaveSettingsPayload } from 'lib/settings/settingsTypes'
import { secrets } from './secrets' import { secrets } from './secrets'
import { import {
@ -2440,6 +2440,110 @@ test('Extrude from command bar selects extrude line after', async ({
}) })
test.describe('Testing constraints', () => { test.describe('Testing constraints', () => {
test.describe('Test distance between constraint', () => {
const cases = [
{
testName: 'Add variable',
constraint: 'horizontal distance',
value: "segEndX('seg01', %) + xDis001, 61.34",
},
{
testName: 'No variable',
constraint: 'horizontal distance',
value: "segEndX('seg01', %) + 88.08, 61.34",
},
{
testName: 'Add variable',
constraint: 'vertical distance',
value: "154.9, segEndY('seg01', %) - yDis001",
},
{
testName: 'No variable',
constraint: 'vertical distance',
value: "154.9, segEndY('seg01', %) - 42.32",
},
] as const
for (const { testName, value, constraint } of cases) {
test(`${constraint} - ${testName}`, async ({ page }) => {
await page.addInitScript(async () => {
localStorage.setItem(
'persistCode',
`const yo = 5
const part001 = startSketchOn('XZ')
|> startProfileAt([-7.54, -26.74], %)
|> line([74.36, 130.4], %)
|> line([78.92, -120.11], %)
|> line([9.16, 77.79], %)
|> line([41.19, 28.97], %)
const part002 = startSketchOn('XZ')
|> startProfileAt([299.05, 231.45], %)
|> xLine(-425.34, %, 'seg-what')
|> yLine(-264.06, %)
|> xLine(segLen('seg-what', %), %)
|> lineTo([profileStartX(%), profileStartY(%)], %)`
)
})
const u = await getUtils(page)
await page.setViewportSize({ width: 1200, height: 500 })
await page.goto('/')
await u.waitForAuthSkipAppStart()
await page.getByText('line([74.36, 130.4], %)').click()
await page.getByRole('button', { name: 'Edit Sketch' }).click()
const [line1, line3] = await Promise.all([
u.getSegmentBodyCoords(`[data-overlay-index="${0}"]`),
u.getSegmentBodyCoords(`[data-overlay-index="${2}"]`),
])
await page.mouse.click(line1.x, line1.y)
await page.keyboard.down('Shift')
await page.mouse.click(line3.x, line3.y)
await page.waitForTimeout(100) // this wait is needed for webkit - not sure why
await page.keyboard.up('Shift')
await page
.getByRole('button', {
name: 'Constrain',
})
.click()
await page
.getByRole('button', { name: constraint, exact: true })
.click()
const createNewVariableCheckbox = page.getByTestId(
'create-new-variable-checkbox'
)
const isChecked = await createNewVariableCheckbox.isChecked()
const addVariable = testName === 'Add variable'
XOR(isChecked, addVariable) && // XOR because no need to click the checkbox if the state is already correct
(await createNewVariableCheckbox.click())
await page
.getByRole('button', { name: 'Add constraining value' })
.click()
// checking activeLines assures the cursors are where they should be
const codeAfter = [
`|> line([74.36, 130.4], %, 'seg01')`,
`|> lineTo([${value}], %)`,
]
const activeLinesContent = await page.locator('.cm-activeLine').all()
await Promise.all(
activeLinesContent.map(async (line, i) => {
await expect(page.locator('.cm-content')).toContainText(
codeAfter[i]
)
// if the code is an active line then the cursor should be on that line
await expect(line).toHaveText(codeAfter[i])
})
)
// checking the count of the overlays is a good proxy check that the client sketch scene is in a good state
await expect(page.getByTestId('segment-overlay')).toHaveCount(4)
})
}
})
test.describe('Test ABS distance constraint', () => { test.describe('Test ABS distance constraint', () => {
const cases = [ const cases = [
{ {
@ -2521,7 +2625,7 @@ const part002 = startSketchOn('XZ')
'create-new-variable-checkbox' 'create-new-variable-checkbox'
) )
const isChecked = await createNewVariableCheckbox.isChecked() const isChecked = await createNewVariableCheckbox.isChecked()
;((isChecked && !addVariable) || (!isChecked && addVariable)) && XOR(isChecked, addVariable) && // XOR because no need to click the checkbox if the state is already correct
(await createNewVariableCheckbox.click()) (await createNewVariableCheckbox.click())
await page await page
@ -2627,7 +2731,7 @@ const part002 = startSketchOn('XZ')
'create-new-variable-checkbox' 'create-new-variable-checkbox'
) )
const isChecked = await createNewVariableCheckbox.isChecked() const isChecked = await createNewVariableCheckbox.isChecked()
;((isChecked && !addVariable) || (!isChecked && addVariable)) && XOR(isChecked, addVariable) && // XOR because no need to click the checkbox if the state is already correct
(await createNewVariableCheckbox.click()) (await createNewVariableCheckbox.click())
await page await page

View File

@ -508,13 +508,26 @@ export const ModelingMachineProvider = ({
}, },
'Get horizontal info': async ({ 'Get horizontal info': async ({
selectionRanges, selectionRanges,
sketchDetails,
}): Promise<SetSelections> => { }): Promise<SetSelections> => {
const { modifiedAst, pathToNodeMap } = const { modifiedAst, pathToNodeMap } =
await applyConstraintHorzVertDistance({ await applyConstraintHorzVertDistance({
constraint: 'setHorzDistance', constraint: 'setHorzDistance',
selectionRanges, selectionRanges,
}) })
await kclManager.updateAst(modifiedAst, true) const _modifiedAst = parse(recast(modifiedAst))
if (!sketchDetails) throw new Error('No sketch details')
const updatedPathToNode = updatePathToNodeFromMap(
sketchDetails.sketchPathToNode,
pathToNodeMap
)
await sceneEntitiesManager.updateAstAndRejigSketch(
updatedPathToNode,
_modifiedAst,
sketchDetails.zAxis,
sketchDetails.yAxis,
sketchDetails.origin
)
return { return {
selectionType: 'completeSelection', selectionType: 'completeSelection',
selection: pathMapToSelections( selection: pathMapToSelections(
@ -522,17 +535,31 @@ export const ModelingMachineProvider = ({
selectionRanges, selectionRanges,
pathToNodeMap pathToNodeMap
), ),
updatedPathToNode,
} }
}, },
'Get vertical info': async ({ 'Get vertical info': async ({
selectionRanges, selectionRanges,
sketchDetails,
}): Promise<SetSelections> => { }): Promise<SetSelections> => {
const { modifiedAst, pathToNodeMap } = const { modifiedAst, pathToNodeMap } =
await applyConstraintHorzVertDistance({ await applyConstraintHorzVertDistance({
constraint: 'setVertDistance', constraint: 'setVertDistance',
selectionRanges, selectionRanges,
}) })
await kclManager.updateAst(modifiedAst, true) const _modifiedAst = parse(recast(modifiedAst))
if (!sketchDetails) throw new Error('No sketch details')
const updatedPathToNode = updatePathToNodeFromMap(
sketchDetails.sketchPathToNode,
pathToNodeMap
)
await sceneEntitiesManager.updateAstAndRejigSketch(
updatedPathToNode,
_modifiedAst,
sketchDetails.zAxis,
sketchDetails.yAxis,
sketchDetails.origin
)
return { return {
selectionType: 'completeSelection', selectionType: 'completeSelection',
selection: pathMapToSelections( selection: pathMapToSelections(
@ -540,6 +567,7 @@ export const ModelingMachineProvider = ({
selectionRanges, selectionRanges,
pathToNodeMap pathToNodeMap
), ),
updatedPathToNode,
} }
}, },
'Get angle info': async ({ 'Get angle info': async ({

View File

@ -106,7 +106,11 @@ export async function applyConstraintHorzVertDistance({
value: valueUsedInTransform, value: valueUsedInTransform,
initialVariableName: constraint === 'setHorzDistance' ? 'xDis' : 'yDis', initialVariableName: constraint === 'setHorzDistance' ? 'xDis' : 'yDis',
} as any) } as any)
if (segName === tagInfo?.tag && Number(value) === valueUsedInTransform) { if (
!variableName &&
segName === tagInfo?.tag &&
Number(value) === valueUsedInTransform
) {
return { return {
modifiedAst, modifiedAst,
pathToNodeMap, pathToNodeMap,
@ -126,6 +130,7 @@ export async function applyConstraintHorzVertDistance({
forceValueUsedInTransform: finalValue, forceValueUsedInTransform: finalValue,
}) })
if (variableName) { if (variableName) {
console.log('variableName', variableName)
const newBody = [..._modifiedAst.body] const newBody = [..._modifiedAst.body]
newBody.splice( newBody.splice(
newVariableInsertIndex, newVariableInsertIndex,
@ -133,6 +138,10 @@ export async function applyConstraintHorzVertDistance({
createVariableDeclaration(variableName, valueNode) createVariableDeclaration(variableName, valueNode)
) )
_modifiedAst.body = newBody _modifiedAst.body = newBody
Object.values(pathToNodeMap).forEach((pathToNode) => {
const index = pathToNode.findIndex((a) => a[0] === 'body') + 1
pathToNode[index][0] = Number(pathToNode[index][0]) + 1
})
} }
return { return {
modifiedAst: _modifiedAst, modifiedAst: _modifiedAst,

View File

@ -127,3 +127,7 @@ export function isReducedMotion(): boolean {
window.matchMedia('(prefers-reduced-motion)').matches window.matchMedia('(prefers-reduced-motion)').matches
) )
} }
export function XOR(bool1: boolean, bool2: boolean): boolean {
return (bool1 || bool2) && !(bool1 && bool2)
}