Add customizer support for OpenSCAD

This also includes sending metadata and part of the concatenated gzip,
not in the s3 metadata as that has a 2kb limit.

Resolves #320
This commit is contained in:
Kurt Hutten
2021-08-25 18:57:28 +10:00
parent 5d79efbf15
commit 87f132a684
18 changed files with 388 additions and 227 deletions

View File

@@ -0,0 +1,140 @@
import {
lambdaBaseURL,
stlToGeometry,
createHealthyResponse,
createUnhealthyResponse,
timeoutErrorMessage,
RenderArgs,
splitGziped,
} from '../common'
import { openScadToCadhubParams } from './openScadParams'
export const render = async ({ code, settings }: RenderArgs) => {
const pixelRatio = window.devicePixelRatio || 1
const size = {
x: Math.round(settings.viewerSize?.width * pixelRatio),
y: Math.round(settings.viewerSize?.height * pixelRatio),
}
const round1dec = (number) => Math.round((number + Number.EPSILON) * 10) / 10
const body = JSON.stringify({
settings: {
size,
parameters: settings.parameters,
camera: {
// rounding to give our caching a chance to sometimes work
...settings.camera,
dist: round1dec(settings.camera.dist),
position: {
x: round1dec(settings.camera.position.x),
y: round1dec(settings.camera.position.y),
z: round1dec(settings.camera.position.z),
},
rotation: {
x: round1dec(settings.camera.rotation.x),
y: round1dec(settings.camera.rotation.y),
z: round1dec(settings.camera.rotation.z),
},
},
},
file: code,
})
if (!settings.camera.position) {
return
}
try {
const response = await fetch(lambdaBaseURL + '/openscad/preview', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body,
})
if (response.status === 400) {
const { error } = await response.json()
const cleanedErrorMessage = cleanError(error)
return createUnhealthyResponse(new Date(), cleanedErrorMessage)
}
if (response.status === 502) {
return createUnhealthyResponse(new Date(), timeoutErrorMessage)
}
const data = await response.json()
const type = data.type !== 'stl' ? 'png' : 'geometry'
const newData = await fetch(data.url).then(async (a) => {
const blob = await a.blob()
const text = await new Response(blob).text()
const { consoleMessage, customizerParams } = splitGziped(text)
return {
data:
data.type !== 'stl'
? blob
: await stlToGeometry(window.URL.createObjectURL(blob)),
consoleMessage,
customizerParams,
}
})
return createHealthyResponse({
type,
data: newData.data,
consoleMessage: newData.consoleMessage,
date: new Date(),
customizerParams: openScadToCadhubParams(newData.customizerParams || []),
})
} catch (e) {
return createUnhealthyResponse(new Date())
}
}
export const stl = async ({ code, settings }: RenderArgs) => {
const body = JSON.stringify({
settings: {},
file: code,
})
try {
const response = await fetch(lambdaBaseURL + '/openscad/stl', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body,
})
if (response.status === 400) {
const { error } = await response.json()
const cleanedErrorMessage = cleanError(error)
return createUnhealthyResponse(new Date(), cleanedErrorMessage)
}
if (response.status === 502) {
return createUnhealthyResponse(new Date(), timeoutErrorMessage)
}
const data = await response.json()
const newData = await fetch(data.url).then(async (a) => {
const blob = await a.blob()
const text = await new Response(blob).text()
const { consoleMessage, customizerParams } = splitGziped(text)
return {
data: await stlToGeometry(window.URL.createObjectURL(blob)),
consoleMessage,
customizerParams,
}
})
return createHealthyResponse({
type: 'geometry',
data: newData.data,
consoleMessage: newData.consoleMessage,
date: new Date(),
customizerParams: openScadToCadhubParams(newData.customizerParams || []),
})
} catch (e) {
return createUnhealthyResponse(new Date())
}
}
const openscad = {
render,
stl,
}
export default openscad
function cleanError(error) {
return error.replace(/["|']\/tmp\/.+\/main.scad["|']/g, "'main.scad'")
}

View File

@@ -0,0 +1,102 @@
import { CadhubParams } from 'src/components/Customizer/customizerConverter'
interface OpenScadParamsBase {
caption: string
name: string
group: string
initial: number | string | boolean | number[]
type: 'string' | 'number' | 'boolean'
}
interface OpenScadNumberParam extends OpenScadParamsBase {
type: 'number'
initial: number | number[]
max?: number
min?: number
step?: number
options?: { name: string; value: number }[]
}
interface OpenScadStringParam extends OpenScadParamsBase {
type: 'string'
initial: string
maxLength?: number
options?: { name: string; value: string }[]
}
interface OpenScadBooleanParam extends OpenScadParamsBase {
type: 'boolean'
initial: boolean
}
export type OpenScadParams =
| OpenScadNumberParam
| OpenScadStringParam
| OpenScadBooleanParam
export function openScadToCadhubParams(
input: OpenScadParams[]
): CadhubParams[] {
return input
.map((param): CadhubParams => {
const common: { caption: string; name: string } = {
caption: param.caption,
name: param.name,
}
switch (param.type) {
case 'boolean':
return {
type: 'boolean',
input: 'default-boolean',
...common,
initial: param.initial,
}
case 'string':
if (!Array.isArray(param?.options)) {
return {
type: 'string',
input: 'default-string',
...common,
initial: param.initial,
maxLength: param.maxLength,
}
} else {
return {
type: 'string',
input: 'choice-string',
...common,
initial: param.initial,
options: param.options,
}
}
case 'number':
if (
!Array.isArray(param?.options) &&
!Array.isArray(param?.initial)
) {
return {
type: 'number',
input: 'default-number',
...common,
initial: param.initial,
min: param.min,
max: param.max,
step: param.step,
}
} else if (
Array.isArray(param?.options) &&
!Array.isArray(param?.initial)
) {
return {
type: 'number',
input: 'choice-number',
...common,
initial: param.initial,
options: param.options,
}
} // TODO else vector
break
default:
return
}
})
.filter((a) => a)
}