Initial work to support curv

This commit is contained in:
lf94
2021-11-19 23:36:57 -05:00
parent e9859d85b8
commit 146da84b62
16 changed files with 591 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
export type CadPackageType = 'openscad' | 'cadquery' | 'jscad' | 'INIT'
export type CadPackageType = 'openscad' | 'cadquery' | 'jscad' | 'curv' | 'INIT'
interface CadPackageConfig {
label: string
@@ -23,6 +23,11 @@ export const cadPackageConfigs: { [key in CadPackageType]: CadPackageConfig } =
buttonClasses: 'bg-ch-purple-500',
dotClasses: 'bg-yellow-300',
},
curv: {
label: 'Curv',
buttonClasses: 'bg-ch-purple-500',
dotClasses: 'bg-yellow-300',
},
INIT: {
label: '',
buttonClasses: '',

View File

@@ -18,6 +18,7 @@ const IdeEditor = ({ Loading }) => {
cadquery: 'python',
openscad: 'cpp',
jscad: 'javascript',
curv: 'javascript',
INIT: '',
}
const monaco = useMonaco()

View File

@@ -35,7 +35,7 @@ export interface Project {
code: string
mainImage: string
createdAt: string
cadPackage: 'openscad' | 'cadquery'
cadPackage: 'openscad' | 'cadquery' | 'curv'
user: {
id: string
userName: string

View File

@@ -95,6 +95,13 @@ const menuOptions: {
dotClasses: 'bg-yellow-300',
ideType: 'jscad',
},
{
name: 'Curv',
sub: 'beta',
bgClasses: 'bg-ch-blue-700',
dotClasses: 'bg-blue-800',
ideType: 'curv',
},
]
const NavPlusButton: React.FC = () => {

View File

@@ -0,0 +1,143 @@
import {
lambdaBaseURL,
stlToGeometry,
createHealthyResponse,
createUnhealthyResponse,
timeoutErrorMessage,
RenderArgs,
splitGziped,
} from '../common'
import { CurvToCadhubParams } from './openScadParams'
import type { XYZ, Camera } from 'src/helpers/hooks/useIdeState'
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,
viewAll: settings.viewAll,
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 + '/curv/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 blob = await response.blob()
const text = await new Response(blob).text()
const { consoleMessage, customizerParams, type, cameraInfo } =
splitGziped(text)
const vecArray2Obj = (arr: number[]): XYZ => ({
x: arr[0],
y: arr[1],
z: arr[2],
})
const camera: Camera = cameraInfo
? {
dist: cameraInfo?.distance,
position: vecArray2Obj(cameraInfo?.translation),
rotation: vecArray2Obj(cameraInfo?.rotation),
isScadUpdate: true,
}
: undefined
return createHealthyResponse({
type: type !== 'stl' ? 'png' : 'geometry',
data:
type !== 'stl'
? blob
: await stlToGeometry(window.URL.createObjectURL(blob)),
consoleMessage,
camera,
date: new Date(),
customizerParams: curvToCadhubParams(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 + '/curv/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 blob = await response.blob()
const text = await new Response(blob).text()
const { consoleMessage, customizerParams, type } = splitGziped(text)
return createHealthyResponse({
type: type !== 'stl' ? 'png' : 'geometry',
data:
type !== 'stl'
? blob
: await stlToGeometry(window.URL.createObjectURL(blob)),
consoleMessage,
date: new Date(),
customizerParams: openScadToCadhubParams(customizerParams || []),
})
} catch (e) {
return createUnhealthyResponse(new Date())
}
}
const curv = {
render,
stl,
}
export default curv
function cleanError(error) {
return error.replace(/["|']\/tmp\/.+\/main.curv["|']/g, "'main.curv'")
}

View File

@@ -0,0 +1,90 @@
import { CadhubParams } from 'src/components/Customizer/customizerConverter'
interface CurvParamsBase {
caption: string
name: string
group: string
initial: number | string | number[]
type: 'string' | 'number'
}
interface CurvNumberParam extends CurvParamsBase {
type: 'number'
initial: number | number[]
max?: number
min?: number
step?: number
options?: { name: string; value: number }[]
}
interface CurvStringParam extends CurvParamsBase {
type: 'string'
initial: string
maxLength?: number
options?: { name: string; value: string }[]
}
export type CurvParams =
| CurvNumberParam
| CurvStringParam
export function openScadToCadhubParams(
input: CurvParams[]
): CadhubParams[] {
return input
.map((param): CadhubParams => {
const common: { caption: string; name: string } = {
caption: param.caption,
name: param.name,
}
switch (param.type) {
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)
}

View File

@@ -0,0 +1,8 @@
// sphere box
// ^ first comment is used for download title (i.e "involute-donut.stl")
(smooth 1).union [
box,
sphere
]

View File

@@ -0,0 +1,9 @@
---
title: Curv
Written with: [Domain-Specific Language](https://martinfowler.com/dsl.html)
Kernal type: Signed distance functions
Maintained by: [Doug Moen and contributors](https://github.com/curv/curv/graphs/contributors)
Documentation: [curv3d.org](https://curv3d.org)
---
Curv is a programming language for creating art using mathematics. Its a 2D and 3D geometric modelling tool that supports full colour, animation and 3D printing.

View File

@@ -13,16 +13,22 @@ import jscad from './jsCad/jsCadController'
import jsCadGuide from 'src/helpers/cadPackages/jsCad/userGuide.md'
import jsCadInitialCode from 'src/helpers/cadPackages/jsCad/initialCode.jscad.js'
import curv from './curv/curvController'
import curvGuide from 'src/helpers/cadPackages/curv/userGuide.md'
import curvInitialCode from 'src/helpers/cadPackages/curv/initialCode.py'
export const cadPackages: { [key in CadPackageType]: DefaultKernelExport } = {
openscad,
cadquery,
jscad,
curv,
}
export const initGuideMap: { [key in CadPackageType]: string } = {
openscad: openScadGuide,
cadquery: cadQueryGuide,
jscad: jsCadGuide,
curv: curvGuide,
INIT: '',
}
@@ -30,5 +36,6 @@ export const initCodeMap: { [key in CadPackageType]: string } = {
openscad: openScadInitialCode,
cadquery: cadQueryInitialCode,
jscad: jsCadInitialCode,
curv: curvInitialCode,
INIT: '',
}