Skip to content

Commit

Permalink
implement subtract
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Oct 16, 2024
1 parent 0c2128d commit 01faddb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/subtract.fixture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Cube, Sphere, Subtract } from "../lib"
import { JsCadFixture } from "../lib/components/jscad-fixture"

export default () => (
<JsCadFixture>
<Subtract>
<Cube size={10} />
<Sphere radius={6} />
</Subtract>
</JsCadFixture>
)
19 changes: 19 additions & 0 deletions lib/create-host-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
RoundedCylinderProps,
Slice,
SphereProps,
SubtractProps,
TorusProps,
UnionProps,
} from "./jscad-fns"
Expand Down Expand Up @@ -257,6 +258,24 @@ 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) =>
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)
Expand Down
1 change: 1 addition & 0 deletions lib/intrinsic-jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/jscad-fns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions lib/jscad-fns/subtract.tsx
Original file line number Diff line number Diff line change
@@ -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 <subtract>{children}</subtract>
}

export const Subtract = withOffsetProp(withColorProp(SubtractBase))

0 comments on commit 01faddb

Please sign in to comment.