@@ -4,38 +4,45 @@ import { readFile } from 'fs/promises'
|
|||||||
|
|
||||||
export const runCQ = async ({
|
export const runCQ = async ({
|
||||||
file,
|
file,
|
||||||
settings: { deflection = 0.3 } = {},
|
settings: { deflection = 0.3, parameters } = {},
|
||||||
} = {}) => {
|
} = {}) => {
|
||||||
const tempFile = await writeFiles(
|
const tempFile = await writeFiles(
|
||||||
[{ file, fileName: 'main.py' }],
|
[
|
||||||
|
{ file, fileName: 'main.py' },
|
||||||
|
{
|
||||||
|
file: JSON.stringify(parameters),
|
||||||
|
fileName: 'params.json',
|
||||||
|
}
|
||||||
|
],
|
||||||
'a' + nanoid() // 'a' ensure nothing funny happens if it start with a bad character like "-", maybe I should pick a safer id generator :shrug:
|
'a' + nanoid() // 'a' ensure nothing funny happens if it start with a bad character like "-", maybe I should pick a safer id generator :shrug:
|
||||||
)
|
)
|
||||||
const fullPath = `/tmp/${tempFile}/output.gz`
|
const fullPath = `/tmp/${tempFile}/output.gz`
|
||||||
const stlPath = `/tmp/${tempFile}/output.stl`
|
const stlPath = `/tmp/${tempFile}/output.stl`
|
||||||
const customizerPath = `/tmp/${tempFile}/customizer.param`
|
const customizerPath = `/tmp/${tempFile}/customizer.json`
|
||||||
const command = [
|
const mainCommand = [
|
||||||
`./cq-cli/cq-cli.py`,
|
`./cq-cli/cq-cli.py`,
|
||||||
`--codec stl`,
|
`--codec stl`,
|
||||||
`--infile /tmp/${tempFile}/main.py`,
|
`--infile /tmp/${tempFile}/main.py`,
|
||||||
`--outfile ${stlPath}`,
|
`--outfile ${stlPath}`,
|
||||||
`--outputopts "deflection:${deflection};angularDeflection:${deflection};"`,
|
`--outputopts "deflection:${deflection};angularDeflection:${deflection};"`,
|
||||||
`--params ${customizerPath}`,
|
`--params /tmp/${tempFile}/params.json`,
|
||||||
].join(' ')
|
].join(' ')
|
||||||
const command2 = [
|
const customizerCommand = [
|
||||||
`cq-cli/cq-cli`,
|
`./cq-cli/cq-cli.py`,
|
||||||
`--getparams true`,
|
`--getparams true`,
|
||||||
`--infile /tmp/${tempFile}/main.py`,
|
`--infile /tmp/${tempFile}/main.py`,
|
||||||
`--outfile ${customizerPath}`,
|
`--outfile ${customizerPath}`,
|
||||||
]
|
].join(' ')
|
||||||
console.log('command', command)
|
console.log('command', mainCommand)
|
||||||
let consoleMessage = ''
|
let consoleMessage = ''
|
||||||
try {
|
try {
|
||||||
consoleMessage = await runCommand(command, 30000)
|
;([consoleMessage] = await Promise.all([
|
||||||
const consoleMessage2 = await runCommand(command2, 30000)
|
runCommand(mainCommand, 30000),
|
||||||
|
runCommand(customizerCommand, 30000)
|
||||||
|
]))
|
||||||
const params = JSON.parse(
|
const params = JSON.parse(
|
||||||
await readFile(customizerPath, { encoding: 'ascii'})
|
await readFile(customizerPath, { encoding: 'ascii'})
|
||||||
)
|
)
|
||||||
console.log('params', params)
|
|
||||||
await writeFiles(
|
await writeFiles(
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,15 +7,17 @@ import {
|
|||||||
RenderArgs,
|
RenderArgs,
|
||||||
DefaultKernelExport,
|
DefaultKernelExport,
|
||||||
splitGziped,
|
splitGziped,
|
||||||
} from './common'
|
} from '../common'
|
||||||
|
import { CadQueryToCadhubParams } from './cadQueryParams'
|
||||||
|
|
||||||
export const render: DefaultKernelExport['render'] = async ({
|
export const render: DefaultKernelExport['render'] = async ({
|
||||||
code,
|
code,
|
||||||
settings: { quality = 'low' },
|
settings: { quality = 'low', parameters },
|
||||||
}: RenderArgs) => {
|
}: RenderArgs) => {
|
||||||
const body = JSON.stringify({
|
const body = JSON.stringify({
|
||||||
settings: {
|
settings: {
|
||||||
deflection: quality === 'low' ? 0.35 : 0.11,
|
deflection: quality === 'low' ? 0.35 : 0.11,
|
||||||
|
parameters,
|
||||||
},
|
},
|
||||||
file: code,
|
file: code,
|
||||||
})
|
})
|
||||||
@@ -49,16 +51,15 @@ export const render: DefaultKernelExport['render'] = async ({
|
|||||||
data: await stlToGeometry(window.URL.createObjectURL(blob)),
|
data: await stlToGeometry(window.URL.createObjectURL(blob)),
|
||||||
consoleMessage,
|
consoleMessage,
|
||||||
date: new Date(),
|
date: new Date(),
|
||||||
customizerParams: customizerParams,
|
customizerParams: CadQueryToCadhubParams([customizerParams]), // TODO, should already be an array and not need to be wrapped in one.
|
||||||
})
|
})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return createUnhealthyResponse(new Date())
|
return createUnhealthyResponse(new Date())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const openscad: DefaultKernelExport = {
|
const cadQuery: DefaultKernelExport = {
|
||||||
render,
|
render,
|
||||||
// more functions to come
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default openscad
|
export default cadQuery
|
||||||
49
app/web/src/helpers/cadPackages/cadQuery/cadQueryParams.ts
Normal file
49
app/web/src/helpers/cadPackages/cadQuery/cadQueryParams.ts
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import { CadhubParams } from 'src/components/Customizer/customizerConverter'
|
||||||
|
|
||||||
|
interface CadQueryParamsBase {
|
||||||
|
name: string
|
||||||
|
initial: number | string
|
||||||
|
type?: 'number'
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CadQueryNumberParam extends CadQueryParamsBase {
|
||||||
|
type: 'number'
|
||||||
|
initial: number
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CadQueryStringParam extends CadQueryParamsBase {
|
||||||
|
initial: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type CadQueryStringParams =
|
||||||
|
| CadQueryNumberParam
|
||||||
|
| CadQueryStringParam
|
||||||
|
|
||||||
|
|
||||||
|
export function CadQueryToCadhubParams(
|
||||||
|
input: CadQueryStringParams[]
|
||||||
|
): CadhubParams[] {
|
||||||
|
return input
|
||||||
|
.map((param): CadhubParams => {
|
||||||
|
const common: { caption: string; name: string } = {
|
||||||
|
caption: '',
|
||||||
|
name: param.name,
|
||||||
|
}
|
||||||
|
if(param.type === 'number') {
|
||||||
|
return {
|
||||||
|
type: 'number',
|
||||||
|
input: 'default-number',
|
||||||
|
...common,
|
||||||
|
initial: Number(param.initial),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
type: 'string',
|
||||||
|
input: 'default-string',
|
||||||
|
...common,
|
||||||
|
initial: String(param.initial),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter((a) => a)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -5,7 +5,7 @@ import openscad from './openScad/openScadController'
|
|||||||
import openScadGuide from 'src/helpers/cadPackages/openScad/userGuide.md'
|
import openScadGuide from 'src/helpers/cadPackages/openScad/userGuide.md'
|
||||||
import openScadInitialCode from 'src/helpers/cadPackages/openScad/initialCode.scad'
|
import openScadInitialCode from 'src/helpers/cadPackages/openScad/initialCode.scad'
|
||||||
|
|
||||||
import cadquery from './cadQueryController'
|
import cadquery from './cadQuery/cadQueryController'
|
||||||
import cadQueryGuide from 'src/helpers/cadPackages/cadQuery/userGuide.md'
|
import cadQueryGuide from 'src/helpers/cadPackages/cadQuery/userGuide.md'
|
||||||
import cadQueryInitialCode from 'src/helpers/cadPackages/cadQuery/initialCode.py'
|
import cadQueryInitialCode from 'src/helpers/cadPackages/cadQuery/initialCode.py'
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user