From bce4a9af59efc6c6ecbefddf2e64e0276618328a Mon Sep 17 00:00:00 2001 From: Severin Ibarluzea Date: Wed, 16 Oct 2024 14:23:02 -0700 Subject: [PATCH] implement subtract (#84) * implement subtract * formatbot: Automatically format code * minor typefix --------- Co-authored-by: tscircuitbot --- examples/subtract.fixture.tsx | 11 +++++++++++ lib/create-host-config.ts | 21 +++++++++++++++++++++ lib/intrinsic-jsx.d.ts | 1 + lib/jscad-fns/index.ts | 1 + lib/jscad-fns/subtract.tsx | 15 +++++++++++++++ 5 files changed, 49 insertions(+) create mode 100644 examples/subtract.fixture.tsx create mode 100644 lib/jscad-fns/subtract.tsx diff --git a/examples/subtract.fixture.tsx b/examples/subtract.fixture.tsx new file mode 100644 index 0000000..3d27252 --- /dev/null +++ b/examples/subtract.fixture.tsx @@ -0,0 +1,11 @@ +import { Cube, Sphere, Subtract } from "../lib" +import { JsCadFixture } from "../lib/components/jscad-fixture" + +export default () => ( + + + + + + +) diff --git a/lib/create-host-config.ts b/lib/create-host-config.ts index e19b246..ebafae4 100644 --- a/lib/create-host-config.ts +++ b/lib/create-host-config.ts @@ -24,6 +24,7 @@ import type { RoundedCylinderProps, Slice, SphereProps, + SubtractProps, TorusProps, UnionProps, } from "./jscad-fns" @@ -257,6 +258,26 @@ export function createHostConfig(jscad: JSCADModule) { return geometries.reduce((acc, curr) => jscad.booleans.union(acc, curr)) } + case "subtract": { + const { children } = props as SubtractProps + if (!Array.isArray(children) || children.length < 2) { + throw new Error("Subtract must have at least two children") + } + + const geometries = children.map((child: any) => + createInstance( + child.type, + child.props, + rootContainerInstance, + hostContext, + internalInstanceHandle, + ), + ) + return geometries.reduce((acc, curr) => + jscad.booleans.subtract(acc, curr), + ) + } + case "translate": { const { args, children } = props as JSX.IntrinsicElements["translate"] const childGeometry = renderChildren(children) diff --git a/lib/intrinsic-jsx.d.ts b/lib/intrinsic-jsx.d.ts index 580d154..88a0d57 100644 --- a/lib/intrinsic-jsx.d.ts +++ b/lib/intrinsic-jsx.d.ts @@ -33,6 +33,7 @@ declare global { torus: FN.TorusProps & CommonProps custom: FN.CustomProps & CommonProps union: FN.UnionProps & CommonProps + subtract: FN.SubtractProps & CommonProps circle: FN.CircleProps & CommonProps rectangle: FN.RectangleProps & CommonProps hull: FN.HullProps & CommonProps diff --git a/lib/jscad-fns/index.ts b/lib/jscad-fns/index.ts index 7d9e461..5f36e90 100644 --- a/lib/jscad-fns/index.ts +++ b/lib/jscad-fns/index.ts @@ -20,6 +20,7 @@ export * from "./rotate" export * from "./rounded-cuboid" export * from "./rounded-cylinder" export * from "./sphere" +export * from "./subtract" export * from "./torus" export * from "./translate" export * from "./union" diff --git a/lib/jscad-fns/subtract.tsx b/lib/jscad-fns/subtract.tsx new file mode 100644 index 0000000..2e7d3b4 --- /dev/null +++ b/lib/jscad-fns/subtract.tsx @@ -0,0 +1,15 @@ +import { withColorProp } from "lib/wrappers/with-color-prop" +import { withOffsetProp } from "lib/wrappers/with-offset-prop" + +export type SubtractProps = { + children: React.ReactNode[] +} + +const SubtractBase = ({ children }: SubtractProps) => { + if (!Array.isArray(children) || children.length < 2) { + throw new Error("Subtract must have at least two children") + } + return {children} +} + +export const Subtract = withOffsetProp(withColorProp(SubtractBase))