perpendicular distance & remove constraint - constraint fixes (#2579)

* perpendicular distance constraint

* remove constraints fix
This commit is contained in:
Kurt Hutten
2024-06-03 22:40:59 +10:00
committed by GitHub
parent cf03021366
commit b02529cae0
5 changed files with 215 additions and 28 deletions

View File

@ -2440,6 +2440,141 @@ test('Extrude from command bar selects extrude line after', async ({
})
test.describe('Testing constraints', () => {
test(`Test remove constraints`, async ({ page }) => {
await page.addInitScript(async () => {
localStorage.setItem(
'persistCode',
`const yo = 79
const part001 = startSketchOn('XZ')
|> startProfileAt([-7.54, -26.74], %)
|> line([74.36, 130.4], %, 'seg01')
|> line([78.92, -120.11], %)
|> angledLine([segAng('seg01', %), yo], %)
|> line([41.19, 28.97 + 5], %)
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], %, 'seg01')").click()
await page.getByRole('button', { name: 'Edit Sketch' }).click()
const line3 = await 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: 'remove constraints', exact: true })
.click()
const activeLinesContent = await page.locator('.cm-activeLine').all()
await expect(activeLinesContent).toHaveLength(1)
await expect(activeLinesContent[0]).toHaveText('|> line([39.13, 68.63], %)')
// 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 perpendicular distance constraint', () => {
const cases = [
{
testName: 'Add variable',
offset: '-offset001',
},
{
testName: 'No variable',
offset: '-128.05',
},
] as const
for (const { testName, offset } of cases) {
test(`${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], %, 'seg01')
|> line([78.92, -120.11], %)
|> angledLine([segAng('seg01', %), 78.33], %)
|> 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], %, 'seg01')").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: 'perpendicular distance', 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()
const activeLinesContent = await page.locator('.cm-activeLine').all()
await expect(activeLinesContent[0]).toHaveText(
`|> line([74.36, 130.4], %, 'seg01')`
)
await expect(activeLinesContent[1]).toHaveText(`}, %)`)
await expect(page.locator('.cm-content')).toContainText(`angle: -57,`)
await expect(page.locator('.cm-content')).toContainText(
`offset: ${offset},`
)
// 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 distance between constraint', () => {
const cases = [
{
@ -2866,7 +3001,7 @@ const part002 = startSketchOn('XZ')
] as const
for (const { codeAfter, constraintName } of cases) {
test(`${constraintName}`, async ({ page }) => {
await page.addInitScript(async () => {
await page.addInitScript(async (customCode) => {
localStorage.setItem(
'persistCode',
`const yo = 5
@ -2892,15 +3027,21 @@ const part002 = startSketchOn('XZ')
await page.getByText('line([74.36, 130.4], %)').click()
await page.getByRole('button', { name: 'Edit Sketch' }).click()
const line1 = await u.getBoundingBox(`[data-overlay-index="${0}"]`)
const line3 = await u.getBoundingBox(`[data-overlay-index="${2}"]`)
const line4 = await u.getBoundingBox(`[data-overlay-index="${3}"]`)
const line1 = await u.getSegmentBodyCoords(
`[data-overlay-index="${0}"]`
)
const line3 = await u.getSegmentBodyCoords(
`[data-overlay-index="${2}"]`
)
const line4 = await u.getSegmentBodyCoords(
`[data-overlay-index="${3}"]`
)
// select two segments by holding down shift
await page.mouse.click(line1.x - 20, line1.y + 20)
await page.mouse.click(line1.x, line1.y)
await page.keyboard.down('Shift')
await page.mouse.click(line3.x - 3, line3.y + 20)
await page.mouse.click(line4.x - 15, line4.y + 15)
await page.mouse.click(line3.x, line3.y)
await page.mouse.click(line4.x, line4.y)
await page.keyboard.up('Shift')
const constraintMenuButton = page.getByRole('button', {
name: 'Constrain',

View File

@ -638,13 +638,26 @@ export const ModelingMachineProvider = ({
},
'Get perpendicular distance info': async ({
selectionRanges,
sketchDetails,
}): Promise<SetSelections> => {
const { modifiedAst, pathToNodeMap } = await applyConstraintIntersect(
{
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 {
selectionType: 'completeSelection',
selection: pathMapToSelections(
@ -652,6 +665,7 @@ export const ModelingMachineProvider = ({
selectionRanges,
pathToNodeMap
),
updatedPathToNode,
}
},
'Get ABS X info': async ({

View File

@ -140,7 +140,11 @@ export async function applyConstraintIntersect({
value: valueUsedInTransform,
initialVariableName: 'offset',
})
if (segName === tagInfo?.tag && Number(value) === valueUsedInTransform) {
if (
!variableName &&
segName === tagInfo?.tag &&
Number(value) === valueUsedInTransform
) {
return {
modifiedAst,
pathToNodeMap,
@ -169,6 +173,10 @@ export async function applyConstraintIntersect({
createVariableDeclaration(variableName, valueNode)
)
_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 {
modifiedAst: _modifiedAst,

View File

@ -130,7 +130,6 @@ export async function applyConstraintHorzVertDistance({
forceValueUsedInTransform: finalValue,
})
if (variableName) {
console.log('variableName', variableName)
const newBody = [..._modifiedAst.body]
newBody.splice(
newVariableInsertIndex,

View File

@ -360,10 +360,8 @@ export const modelingMachine = createMachine(
},
'Constrain remove constraints': {
target: 'SketchIdle',
internal: true,
cond: 'Can constrain remove constraints',
actions: ['Constrain remove constraints'],
target: 'Await constrain remove constraints',
},
'Re-execute': {
@ -586,6 +584,16 @@ export const modelingMachine = createMachine(
},
},
'Await constrain remove constraints': {
invoke: {
src: 'do-constrain-remove-constraint',
id: 'do-constrain-remove-constraint',
onDone: {
target: 'SketchIdle',
actions: 'Set selection',
},
},
},
'Await constrain horizontally': {
invoke: {
src: 'do-constrain-horizontally',
@ -848,21 +856,6 @@ export const modelingMachine = createMachine(
'set new sketch metadata': assign((_, { data }) => ({
sketchDetails: data,
})),
// TODO implement source ranges for all of these constraints
// need to make the async like the modal constraints
'Constrain remove constraints': ({ selectionRanges, sketchDetails }) => {
const { modifiedAst } = applyRemoveConstrainingValues({
selectionRanges,
})
if (!sketchDetails) return
sceneEntitiesManager.updateAstAndRejigSketch(
sketchDetails?.sketchPathToNode || [],
modifiedAst,
sketchDetails.zAxis,
sketchDetails.yAxis,
sketchDetails.origin
)
},
'AST extrude': async (_, event) => {
if (!event.data) return
const { selection, distance } = event.data
@ -1109,6 +1102,38 @@ export const modelingMachine = createMachine(
},
// end actions
services: {
'do-constrain-remove-constraint': async ({
selectionRanges,
sketchDetails,
}) => {
const { modifiedAst, pathToNodeMap } = applyRemoveConstrainingValues({
selectionRanges,
})
if (!sketchDetails) return
sceneEntitiesManager.updateAstAndRejigSketch(
sketchDetails?.sketchPathToNode || [],
modifiedAst,
sketchDetails.zAxis,
sketchDetails.yAxis,
sketchDetails.origin
)
if (!sketchDetails) return
await sceneEntitiesManager.updateAstAndRejigSketch(
sketchDetails.sketchPathToNode,
modifiedAst,
sketchDetails.zAxis,
sketchDetails.yAxis,
sketchDetails.origin
)
return {
selectionType: 'completeSelection',
selection: updateSelections(
pathToNodeMap,
selectionRanges,
parse(recast(modifiedAst))
),
}
},
'do-constrain-horizontally': async ({
selectionRanges,
sketchDetails,