Skip to content

Commit

Permalink
feat: add handling of boolean vector parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
seasick committed Jan 22, 2024
1 parent 927e04b commit 73cc256
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 11 deletions.
12 changes: 11 additions & 1 deletion src/lib/openSCAD/parseParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,18 @@ function convertType(rawValue): {
type: ParameterType;
} {
if (/^\d+(\.\d+)?$/.test(rawValue)) {
// Raw value matches something like `123.123` or `123`.
return { value: parseFloat(rawValue), type: 'number' };
} else if (rawValue === 'true' || rawValue === 'false') {
// Raw value matches `true` or `false`.
return { value: rawValue === 'true', type: 'boolean' };
} else if (rawValue.startsWith('[') && rawValue.endsWith(']')) {
// Array type
// Raw values is an array
const arrayValue = rawValue
.slice(1, -1)
.split(',')
.map((item) => item.trim());

if (arrayValue.every((item) => /^\d+(\.\d+)?$/.test(item))) {
return {
value: arrayValue.map((item) => parseFloat(item)),
Expand All @@ -186,6 +189,13 @@ function convertType(rawValue): {
value: arrayValue.map((item) => item.slice(1, -1)),
type: 'string[]',
};
} else if (
arrayValue.every((item) => item === 'true' || item === 'false')
) {
return {
value: arrayValue.map((item) => item === 'true'),
type: 'boolean[]',
};
} else {
return { value: arrayValue, type: 'string[]' };
}
Expand Down
24 changes: 14 additions & 10 deletions src/openSCADWorker.mts
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,20 @@ async function preview(
params: OpenSCADWorkerInputMessage['params'],
fileType = 'stl'
): Promise<OpenSCADWorkerOutputMessage> {
const parameters = params.map(({ name, type, value }) => {
if (type === 'string' && typeof value === 'string') {
value = escapeShell(value);
} else if (type === 'number[]' && Array.isArray(value)) {
value = `[${value.join(',')}]`;
} else if (type === 'string[]' && Array.isArray(value)) {
value = `[${value.map((item) => escapeShell(item)).join(',')}]`;
}
return `-D${name}=${value}`;
});
const parameters = params
.map(({ name, type, value }) => {
if (type === 'string' && typeof value === 'string') {
value = escapeShell(value);
} else if (type === 'number[]' && Array.isArray(value)) {
value = `[${value.join(',')}]`;
} else if (type === 'string[]' && Array.isArray(value)) {
value = `[${value.map((item) => escapeShell(item)).join(',')}]`;
} else if (type === 'boolean[]' && Array.isArray(value)) {
value = `[${value.join(',')}]`;
}
return `-D${name}=${value}`;
})
.filter((x) => !!x);

parameters.push('--export-format=binstl');
parameters.push(`--enable=manifold`);
Expand Down
71 changes: 71 additions & 0 deletions tests/__snapshots__/openSCADparseParameters.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,77 @@ exports[`testing parameter parsing of openscad scripts testing example1.scad: ex
]
`;

exports[`testing parameter parsing of openscad scripts testing example2.scad: example2.scad 1`] = `
[
{
"description": "Variable which will be treated as \`number\`",
"group": undefined,
"name": "x",
"options": undefined,
"range": undefined,
"type": "number",
"value": 1,
},
{
"description": "Variable which will be treated as \`string\`",
"group": undefined,
"name": "y",
"options": undefined,
"range": undefined,
"type": "string",
"value": "a",
},
{
"description": "Variable which will be treated as \`boolean\`",
"group": undefined,
"name": "z",
"options": undefined,
"range": undefined,
"type": "boolean",
"value": true,
},
{
"description": "Variable which will be treated as \`number[]\`",
"group": undefined,
"name": "xxx",
"options": undefined,
"range": undefined,
"type": "number[]",
"value": [
1,
2,
3,
],
},
{
"description": "Variable which will be treated as \`string[]\`",
"group": undefined,
"name": "yyy",
"options": undefined,
"range": undefined,
"type": "string[]",
"value": [
"a",
"b",
"c",
],
},
{
"description": "Variable which will be treated as \`boolean[]\`",
"group": undefined,
"name": "zzz",
"options": undefined,
"range": undefined,
"type": "boolean[]",
"value": [
true,
false,
true,
],
},
]
`;

exports[`testing parameter parsing of openscad scripts testing printables-513382.scad: printables-513382.scad 1`] = `
[
{
Expand Down
28 changes: 28 additions & 0 deletions tests/fixtures/openSCAD/example2.scad
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Variable which will be treated as `number`
x = 1;

// Variable which will be treated as `string`
y = "a";

// Variable which will be treated as `boolean`
z = true;

// Variable which will be treated as `number[]`
xxx = [1, 2, 3];

// Variable which will be treated as `string[]`
yyy = ["a", "b", "c"];

// Variable which will be treated as `boolean[]`
zzz = [true, false, true];


cube();

echo(x);
echo(y);
echo(z);

echo(xxx);
echo(yyy);
echo(zzz);

0 comments on commit 73cc256

Please sign in to comment.