2022-12-25 21:14:43 +11:00
|
|
|
import { abstractSyntaxTree } from './abstractSyntaxTree'
|
|
|
|
import { lexer } from './tokeniser'
|
2023-06-22 16:43:33 +10:00
|
|
|
import { SketchGroup, ExtrudeGroup } from './executor'
|
2023-02-21 09:42:41 +11:00
|
|
|
import { initPromise } from './rust'
|
2023-06-22 16:43:33 +10:00
|
|
|
import { executor } from '../lib/testHelpers'
|
2023-02-21 09:42:41 +11:00
|
|
|
|
|
|
|
beforeAll(() => initPromise)
|
2022-12-25 21:14:43 +11:00
|
|
|
|
2023-01-08 16:37:31 +11:00
|
|
|
describe('testing artifacts', () => {
|
2023-06-22 16:43:33 +10:00
|
|
|
test('sketch artifacts', async () => {
|
2022-12-25 21:14:43 +11:00
|
|
|
const code = `
|
2023-02-12 10:56:45 +11:00
|
|
|
const mySketch001 = startSketchAt([0, 0])
|
|
|
|
|> lineTo([-1.59, -1.54], %)
|
|
|
|
|> lineTo([0.46, -5.82], %)
|
2022-12-25 21:14:43 +11:00
|
|
|
|> rx(45, %)
|
|
|
|
show(mySketch001)`
|
2023-06-22 16:43:33 +10:00
|
|
|
const programMemory = await executor(abstractSyntaxTree(lexer(code)))
|
2023-01-08 16:37:31 +11:00
|
|
|
const geos = programMemory?.return?.map(
|
|
|
|
(a) => programMemory?.root?.[a.name]
|
|
|
|
)
|
|
|
|
const artifactsWithoutGeos = removeGeo(geos as any)
|
|
|
|
expect(artifactsWithoutGeos).toEqual([
|
2022-12-25 21:14:43 +11:00
|
|
|
{
|
2023-01-08 16:37:31 +11:00
|
|
|
type: 'sketchGroup',
|
2023-03-17 08:27:40 +11:00
|
|
|
start: {
|
|
|
|
type: 'base',
|
|
|
|
to: [0, 0],
|
|
|
|
from: [0, 0],
|
|
|
|
__geoMeta: {
|
2023-06-22 16:43:33 +10:00
|
|
|
id: '66366561-6465-4734-a463-366330356563',
|
2023-03-17 08:27:40 +11:00
|
|
|
sourceRange: [21, 42],
|
|
|
|
pathToNode: [],
|
|
|
|
geos: ['sketchBase'],
|
|
|
|
},
|
|
|
|
},
|
2023-01-08 16:37:31 +11:00
|
|
|
value: [
|
|
|
|
{
|
|
|
|
type: 'toPoint',
|
|
|
|
to: [-1.59, -1.54],
|
|
|
|
from: [0, 0],
|
|
|
|
__geoMeta: {
|
2023-02-12 10:56:45 +11:00
|
|
|
sourceRange: [48, 73],
|
2023-06-22 16:43:33 +10:00
|
|
|
id: '30366338-6462-4330-a364-303935626163',
|
2023-01-08 16:37:31 +11:00
|
|
|
pathToNode: [],
|
|
|
|
geos: ['line', 'lineEnd'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'toPoint',
|
|
|
|
to: [0.46, -5.82],
|
|
|
|
from: [-1.59, -1.54],
|
|
|
|
__geoMeta: {
|
2023-02-12 10:56:45 +11:00
|
|
|
sourceRange: [79, 103],
|
2023-06-22 16:43:33 +10:00
|
|
|
id: '32653334-6331-4231-b162-663334363535',
|
2023-01-08 16:37:31 +11:00
|
|
|
pathToNode: [],
|
|
|
|
geos: ['line', 'lineEnd'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
position: [0, 0, 0],
|
|
|
|
rotation: [0.3826834323650898, 0, 0, 0.9238795325112867],
|
|
|
|
__meta: [
|
2023-02-12 10:56:45 +11:00
|
|
|
{ sourceRange: [21, 42], pathToNode: [] },
|
|
|
|
{ sourceRange: [109, 118], pathToNode: [] },
|
2023-01-08 16:37:31 +11:00
|
|
|
],
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
test('extrude artifacts', async () => {
|
2023-01-08 16:37:31 +11:00
|
|
|
const code = `
|
2023-02-12 10:56:45 +11:00
|
|
|
const mySketch001 = startSketchAt([0, 0])
|
|
|
|
|> lineTo([-1.59, -1.54], %)
|
|
|
|
|> lineTo([0.46, -5.82], %)
|
2023-01-08 16:37:31 +11:00
|
|
|
|> rx(45, %)
|
|
|
|
|> extrude(2, %)
|
|
|
|
show(mySketch001)`
|
2023-06-22 16:43:33 +10:00
|
|
|
const programMemory = await executor(abstractSyntaxTree(lexer(code)))
|
2023-01-08 16:37:31 +11:00
|
|
|
const geos = programMemory?.return?.map(
|
|
|
|
(a) => programMemory?.root?.[a.name]
|
|
|
|
)
|
|
|
|
const artifactsWithoutGeos = removeGeo(geos as any)
|
|
|
|
expect(artifactsWithoutGeos).toEqual([
|
|
|
|
{
|
|
|
|
type: 'extrudeGroup',
|
|
|
|
value: [
|
2023-01-10 15:40:34 +11:00
|
|
|
{
|
|
|
|
type: 'extrudePlane',
|
|
|
|
position: [-0.795, -0.5444722215136415, -0.5444722215136416],
|
|
|
|
rotation: [
|
|
|
|
0.35471170441873584, 0.3467252481708758, -0.14361830020955396,
|
|
|
|
0.8563498075401887,
|
|
|
|
],
|
|
|
|
__geoMeta: {
|
2023-06-22 16:43:33 +10:00
|
|
|
id: expect.anything(),
|
|
|
|
geo: undefined,
|
|
|
|
refId: '30366338-6462-4330-a364-303935626163',
|
2023-02-12 10:56:45 +11:00
|
|
|
sourceRange: [48, 73],
|
2023-01-10 15:40:34 +11:00
|
|
|
pathToNode: [],
|
|
|
|
},
|
|
|
|
},
|
2023-01-08 16:37:31 +11:00
|
|
|
{
|
|
|
|
type: 'extrudePlane',
|
2023-01-09 08:52:48 +11:00
|
|
|
position: [
|
|
|
|
-0.5650000000000001, -2.602152954766495, -2.602152954766495,
|
|
|
|
],
|
|
|
|
rotation: [
|
|
|
|
0.20394238048109659, 0.7817509623502217, -0.3238118510036805,
|
|
|
|
0.4923604609001174,
|
|
|
|
],
|
2023-01-08 16:37:31 +11:00
|
|
|
__geoMeta: {
|
2023-06-22 16:43:33 +10:00
|
|
|
id: expect.anything(),
|
|
|
|
geo: undefined,
|
|
|
|
refId: '32653334-6331-4231-b162-663334363535',
|
2023-02-12 10:56:45 +11:00
|
|
|
sourceRange: [79, 103],
|
2023-01-08 16:37:31 +11:00
|
|
|
pathToNode: [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
height: 2,
|
|
|
|
position: [0, 0, 0],
|
|
|
|
rotation: [0.3826834323650898, 0, 0, 0.9238795325112867],
|
|
|
|
__meta: [
|
2023-02-12 10:56:45 +11:00
|
|
|
{ sourceRange: [124, 137], pathToNode: [] },
|
|
|
|
{ sourceRange: [21, 42], pathToNode: [] },
|
2022-12-25 21:14:43 +11:00
|
|
|
],
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
test('sketch extrude and sketch on one of the faces', async () => {
|
2023-01-09 08:52:48 +11:00
|
|
|
const code = `
|
2023-02-12 10:56:45 +11:00
|
|
|
const sk1 = startSketchAt([0, 0])
|
|
|
|
|> lineTo([-2.5, 0], %)
|
|
|
|
|> lineTo({ to: [0, 10], tag: "p" }, %)
|
|
|
|
|> lineTo([2.5, 0], %)
|
2023-01-09 08:52:48 +11:00
|
|
|
|> rx(45, %)
|
|
|
|
|> translate([1,0,1], %)
|
|
|
|
|> ry(5, %)
|
|
|
|
const theExtrude = extrude(2, sk1)
|
|
|
|
const theTransf = getExtrudeWallTransform('p', theExtrude)
|
2023-02-12 10:56:45 +11:00
|
|
|
const sk2 = startSketchAt([0, 0])
|
|
|
|
|> lineTo([-2.5, 0], %)
|
|
|
|
|> lineTo({ to: [0, 3], tag: "p" }, %)
|
|
|
|
|> lineTo([2.5, 0], %)
|
2023-01-09 08:52:48 +11:00
|
|
|
|> transform(theTransf, %)
|
|
|
|
|> extrude(2, %)
|
|
|
|
|
|
|
|
|
|
|
|
show(theExtrude, sk2)`
|
2023-06-22 16:43:33 +10:00
|
|
|
const programMemory = await executor(abstractSyntaxTree(lexer(code)))
|
2023-01-09 08:52:48 +11:00
|
|
|
const geos = programMemory?.return?.map(
|
|
|
|
(a) => programMemory?.root?.[a.name]
|
|
|
|
)
|
|
|
|
const artifactsWithoutGeos = removeGeo(geos as any)
|
|
|
|
expect(artifactsWithoutGeos).toEqual([
|
|
|
|
{
|
|
|
|
type: 'extrudeGroup',
|
|
|
|
value: [
|
2023-01-10 15:40:34 +11:00
|
|
|
{
|
|
|
|
type: 'extrudePlane',
|
|
|
|
position: [-0.1618929317752782, 0, 1.01798363377866],
|
|
|
|
rotation: [
|
|
|
|
0.3823192025331841, -0.04029905920751535, -0.016692416874629204,
|
|
|
|
0.9230002039112793,
|
|
|
|
],
|
|
|
|
__geoMeta: {
|
2023-06-22 16:43:33 +10:00
|
|
|
id: expect.anything(), // todo figure out why isn't deterministic
|
|
|
|
geo: undefined,
|
|
|
|
refId: '36613364-6238-4330-b766-613131633135',
|
2023-02-12 10:56:45 +11:00
|
|
|
sourceRange: [40, 60],
|
2023-01-10 15:40:34 +11:00
|
|
|
pathToNode: [],
|
|
|
|
},
|
|
|
|
},
|
2023-01-09 08:52:48 +11:00
|
|
|
{
|
|
|
|
type: 'extrudePlane',
|
|
|
|
position: [
|
|
|
|
0.14624915180581843, 3.5355339059327373, 4.540063765792454,
|
|
|
|
],
|
|
|
|
rotation: [
|
|
|
|
-0.24844095888221532, 0.7523143130765927, -0.2910733573455524,
|
|
|
|
-0.5362616571538269,
|
|
|
|
],
|
|
|
|
__geoMeta: {
|
2023-06-22 16:43:33 +10:00
|
|
|
id: expect.anything(),
|
|
|
|
geo: undefined,
|
|
|
|
refId: '32313832-3531-4933-b839-316634316237',
|
2023-02-12 10:56:45 +11:00
|
|
|
sourceRange: [66, 102],
|
2023-01-09 08:52:48 +11:00
|
|
|
pathToNode: [],
|
|
|
|
},
|
|
|
|
name: 'p',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'extrudePlane',
|
|
|
|
position: [
|
|
|
|
2.636735897035183, 3.5355339059327386, 4.322174408923308,
|
|
|
|
],
|
|
|
|
rotation: [
|
|
|
|
0.22212685137378593, 0.7027132469491032, -0.3116187916437232,
|
|
|
|
0.5997895323824204,
|
|
|
|
],
|
|
|
|
__geoMeta: {
|
2023-06-22 16:43:33 +10:00
|
|
|
id: expect.anything(),
|
|
|
|
geo: undefined,
|
|
|
|
refId: '31356564-3364-4562-a438-653732633238',
|
2023-02-12 10:56:45 +11:00
|
|
|
sourceRange: [108, 127],
|
2023-01-09 08:52:48 +11:00
|
|
|
pathToNode: [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
height: 2,
|
|
|
|
position: [1.083350440839404, 0, 0.9090389553440874],
|
|
|
|
rotation: [
|
|
|
|
0.38231920253318413, 0.04029905920751535, -0.01669241687462921,
|
|
|
|
0.9230002039112792,
|
|
|
|
],
|
|
|
|
__meta: [
|
2023-02-21 10:28:34 +11:00
|
|
|
{ sourceRange: [203, 218], pathToNode: [] },
|
2023-02-12 10:56:45 +11:00
|
|
|
{ sourceRange: [13, 34], pathToNode: [] },
|
2023-01-09 08:52:48 +11:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'extrudeGroup',
|
|
|
|
value: [
|
2023-01-10 15:40:34 +11:00
|
|
|
{
|
|
|
|
type: 'extrudePlane',
|
|
|
|
position: [
|
|
|
|
0.5230004643466108, 4.393026831645281, 5.367870706359959,
|
|
|
|
],
|
|
|
|
rotation: [
|
|
|
|
-0.5548685410139091, 0.7377864971619333, 0.3261466075583827,
|
|
|
|
-0.20351996751370383,
|
|
|
|
],
|
|
|
|
__geoMeta: {
|
2023-06-22 16:43:33 +10:00
|
|
|
id: expect.anything(),
|
|
|
|
geo: undefined,
|
|
|
|
refId: '31623462-6433-4233-b361-303837663464',
|
2023-02-12 10:56:45 +11:00
|
|
|
sourceRange: [317, 337],
|
2023-01-10 15:40:34 +11:00
|
|
|
pathToNode: [],
|
|
|
|
},
|
|
|
|
},
|
2023-01-09 08:52:48 +11:00
|
|
|
{
|
|
|
|
type: 'extrudePlane',
|
|
|
|
position: [
|
|
|
|
0.43055783927228125, 5.453687003425103, 4.311246666755821,
|
|
|
|
],
|
|
|
|
rotation: [
|
|
|
|
0.5307054034531232, -0.4972416536396126, 0.3641462373475848,
|
|
|
|
-0.5818075544860157,
|
|
|
|
],
|
|
|
|
__geoMeta: {
|
2023-06-22 16:43:33 +10:00
|
|
|
id: expect.anything(),
|
|
|
|
geo: undefined,
|
|
|
|
refId: '66363230-3430-4961-b831-646363376538',
|
2023-02-12 10:56:45 +11:00
|
|
|
sourceRange: [343, 378],
|
2023-01-09 08:52:48 +11:00
|
|
|
pathToNode: [],
|
|
|
|
},
|
|
|
|
name: 'p',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'extrudePlane',
|
|
|
|
position: [
|
|
|
|
-0.3229447858093035, 3.7387011520000146, 2.6556327856208117,
|
|
|
|
],
|
|
|
|
rotation: [
|
|
|
|
0.06000443169260189, 0.12863059446321826, 0.6408199244764428,
|
|
|
|
-0.7544557394170275,
|
|
|
|
],
|
|
|
|
__geoMeta: {
|
2023-06-22 16:43:33 +10:00
|
|
|
id: expect.anything(),
|
|
|
|
geo: undefined,
|
|
|
|
refId: '62366564-3261-4061-b533-623433336531',
|
2023-02-12 10:56:45 +11:00
|
|
|
sourceRange: [384, 403],
|
2023-01-09 08:52:48 +11:00
|
|
|
pathToNode: [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
height: 2,
|
|
|
|
position: [0.14624915180581843, 3.5355339059327373, 4.540063765792454],
|
|
|
|
rotation: [
|
|
|
|
0.24844095888221532, -0.7523143130765927, 0.2910733573455524,
|
|
|
|
-0.5362616571538269,
|
|
|
|
],
|
|
|
|
__meta: [
|
2023-02-12 10:56:45 +11:00
|
|
|
{ sourceRange: [438, 451], pathToNode: [] },
|
|
|
|
{ sourceRange: [290, 311], pathToNode: [] },
|
2023-01-09 08:52:48 +11:00
|
|
|
],
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
2022-12-25 21:14:43 +11:00
|
|
|
})
|
|
|
|
|
2023-01-08 16:37:31 +11:00
|
|
|
function removeGeo(arts: (SketchGroup | ExtrudeGroup)[]): any {
|
2022-12-25 21:14:43 +11:00
|
|
|
return arts.map((art) => {
|
2023-06-22 16:43:33 +10:00
|
|
|
if (!art) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
if (art?.type === 'extrudeGroup') {
|
2022-12-25 21:14:43 +11:00
|
|
|
return {
|
|
|
|
...art,
|
2023-01-08 16:37:31 +11:00
|
|
|
value: art.value.map((v) => ({
|
|
|
|
...v,
|
|
|
|
__geoMeta: {
|
|
|
|
...v.__geoMeta,
|
2023-06-22 16:43:33 +10:00
|
|
|
geo: (v?.__geoMeta as any)?.geo?.type,
|
2023-01-08 16:37:31 +11:00
|
|
|
},
|
|
|
|
})),
|
2022-12-25 21:14:43 +11:00
|
|
|
}
|
|
|
|
}
|
2023-01-08 16:37:31 +11:00
|
|
|
return {
|
|
|
|
...art,
|
2023-03-17 08:27:40 +11:00
|
|
|
start: art.start
|
|
|
|
? {
|
|
|
|
...art.start,
|
|
|
|
__geoMeta: {
|
|
|
|
...art.start.__geoMeta,
|
|
|
|
geos: art.start.__geoMeta.geos.map((g) => g.type),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: {},
|
2023-01-08 16:37:31 +11:00
|
|
|
value: art.value.map((v) => ({
|
|
|
|
...v,
|
|
|
|
__geoMeta: {
|
|
|
|
...v.__geoMeta,
|
|
|
|
geos: v.__geoMeta.geos.map((g) => g.type),
|
|
|
|
},
|
|
|
|
})),
|
2022-12-25 21:14:43 +11:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|