Skip to content

Commit

Permalink
Add CadQuery customizer (#547)
Browse files Browse the repository at this point in the history
* Rough changes to make the CadQuery integration work with the customizer

* Tweak runCQ

* Switched to Anaconda

* Cleaned up code

* Update CadHub after anaconda

Related to #547

* Add final tweaks to CQ customizer

* Separated out customizer.json from params.json

* Changes after discussing CadHub integration

* linting runCQ

Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
  • Loading branch information
jmwright and Irev-Dev committed Oct 14, 2021
1 parent 3df903f commit 96ee9c4
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 20 deletions.
27 changes: 19 additions & 8 deletions app/api/src/docker/cadquery/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM public.ecr.aws/lts/ubuntu:20.04_stable

ENV PATH="/root/miniconda3/bin:${PATH}"
ARG PATH="/root/miniconda3/bin:${PATH}"
ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update -qq
RUN apt-get update --fix-missing -qq
RUN apt-get -y -qq install software-properties-common dirmngr apt-transport-https lsb-release ca-certificates xvfb
RUN apt-get update -qq
RUN apt-get install -y wget
Expand All @@ -21,7 +22,9 @@ RUN apt-get update && \
cmake \
unzip \
automake autoconf libtool \
libcurl4-openssl-dev
libcurl4-openssl-dev \
curl \
git

# Add the lambda emulator for local dev, (see entrypoint.sh for where it's used),
# I have the file locally (gitignored) to speed up build times (as it downloads everytime),
Expand All @@ -35,15 +38,23 @@ COPY package*.json /var/task/
RUN npm install
RUN npm install aws-lambda-ric@1.0.0

# Install Miniconda
RUN wget \
https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
&& bash Miniconda3-latest-Linux-x86_64.sh -b \
&& rm -f Miniconda3-latest-Linux-x86_64.sh
RUN conda --version

# Install CadQuery
RUN conda install -c cadquery -c conda-forge cadquery=master ocp=7.5.2 python=3.8
RUN conda info

# Get a copy of cq-cli from GitHub
RUN git clone https://github.com/CadQuery/cq-cli.git

# Get the distribution copy of cq-cli
RUN apt-get install -y libglew2.1
RUN wget https://github.com/CadQuery/cq-cli/releases/download/v2.2-beta.2/cq-cli-Linux-x86_64.zip
# Comment the entry above out and uncomment the one below to revert to the stable release
# RUN wget https://github.com/CadQuery/cq-cli/releases/download/v2.1.0/cq-cli-Linux-x86_64.zip
RUN unzip cq-cli-Linux-x86_64.zip

RUN chmod +x cq-cli/cq-cli
RUN echo "cadhub-concat-split" > /var/task/cadhub-concat-split

# using built javascript from dist
Expand Down
22 changes: 18 additions & 4 deletions app/api/src/docker/cadquery/runCQ.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
import { writeFiles, runCommand } from '../common/utils'
import { nanoid } from 'nanoid'
import { readFile } from 'fs/promises'

export const runCQ = async ({
file,
settings: { deflection = 0.3 } = {},
settings: { deflection = 0.3, parameters } = {},
} = {}) => {
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:
)
const fullPath = `/tmp/${tempFile}/output.gz`
const stlPath = `/tmp/${tempFile}/output.stl`
const customizerPath = `/tmp/${tempFile}/customizer.json`
const command = [
`cq-cli/cq-cli`,
`./cq-cli/cq-cli.py`,
`--codec stl`,
`--infile /tmp/${tempFile}/main.py`,
`--outfile ${stlPath}`,
`--outputopts "deflection:${deflection};angularDeflection:${deflection};"`,
`--params /tmp/${tempFile}/params.json`,
`--getparams ${customizerPath}`,
].join(' ')
console.log('command', command)
let consoleMessage = ''
try {
consoleMessage = await runCommand(command, 30000)
const params = JSON.parse(
await readFile(customizerPath, { encoding: 'ascii' })
)
await writeFiles(
[
{
file: JSON.stringify({
customizerParams: params,
consoleMessage,
type: 'stl',
}),
Expand All @@ -41,6 +55,6 @@ export const runCQ = async ({
)
return { consoleMessage, fullPath }
} catch (error) {
return { error: consoleMessage, fullPath }
return { error: consoleMessage || error, fullPath }
}
}
2 changes: 1 addition & 1 deletion app/api/src/docker/openscad/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM public.ecr.aws/lts/ubuntu:20.04_stable
ARG DEBIAN_FRONTEND=noninteractive

## install things needed to run openscad (xvfb is an important one)
RUN apt-get update -qq
RUN apt-get update --fix-missing -qq
# double check this below, I'm not sure we need inkscape etc
RUN apt-get -y -qq install software-properties-common dirmngr apt-transport-https lsb-release ca-certificates xvfb imagemagick unzip inkscape
RUN apt-get install -y curl wget
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import {
RenderArgs,
DefaultKernelExport,
splitGziped,
} from './common'
} from '../common'
import { CadQueryToCadhubParams } from './cadQueryParams'

export const render: DefaultKernelExport['render'] = async ({
code,
settings: { quality = 'low' },
settings: { quality = 'low', parameters },
}: RenderArgs) => {
const body = JSON.stringify({
settings: {
deflection: quality === 'low' ? 0.35 : 0.11,
parameters,
},
file: code,
})
Expand Down Expand Up @@ -43,21 +45,21 @@ export const render: DefaultKernelExport['render'] = async ({
}
const blob = await response.blob()
const text = await new Response(blob).text()
const { consoleMessage } = splitGziped(text)
const { consoleMessage, customizerParams, type } = splitGziped(text)
return createHealthyResponse({
type: 'geometry',
data: await stlToGeometry(window.URL.createObjectURL(blob)),
consoleMessage,
date: new Date(),
customizerParams: CadQueryToCadhubParams(customizerParams),
})
} catch (e) {
return createUnhealthyResponse(new Date())
}
}

const openscad: DefaultKernelExport = {
const cadQuery: DefaultKernelExport = {
render,
// more functions to come
}

export default openscad
export default cadQuery
63 changes: 63 additions & 0 deletions app/web/src/helpers/cadPackages/cadQuery/cadQueryParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { CadhubParams } from 'src/components/Customizer/customizerConverter'

interface CadQueryParamsBase {
name: string
initial: number | string | boolean
type?: 'number' | 'string' | 'boolean'
}

interface CadQueryNumberParam extends CadQueryParamsBase {
type: 'number'
initial: number
}

interface CadQueryStringParam extends CadQueryParamsBase {
type: 'string'
initial: string
}

interface CadQueryBooleanParam extends CadQueryParamsBase {
type: 'boolean'
initial: boolean
}

export type CadQueryStringParams =
| CadQueryNumberParam
| CadQueryStringParam
| CadQueryBooleanParam

export function CadQueryToCadhubParams(
input: CadQueryStringParams[]
): CadhubParams[] {
return input
.map((param): CadhubParams => {
const common: { caption: string; name: string } = {
caption: '',
name: param.name,
}
switch (param.type) {
case 'number':
return {
type: 'number',
input: 'default-number',
...common,
initial: param.initial,
}
case 'string':
return {
type: 'string',
input: 'default-string',
...common,
initial: param.initial,
}
case 'boolean':
return {
type: 'boolean',
input: 'default-boolean',
...common,
initial: param.initial,
}
}
})
.filter((a) => a)
}
2 changes: 1 addition & 1 deletion app/web/src/helpers/cadPackages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import openscad from './openScad/openScadController'
import openScadGuide from 'src/helpers/cadPackages/openScad/userGuide.md'
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 cadQueryInitialCode from 'src/helpers/cadPackages/cadQuery/initialCode.py'

Expand Down

0 comments on commit 96ee9c4

Please sign in to comment.