Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement subtract #84

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
)
21 changes: 21 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,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)
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))
Loading