diff --git a/src/CadViewerJscad.tsx b/src/CadViewerJscad.tsx index 4ff4faa8..a2cc0b38 100644 --- a/src/CadViewerJscad.tsx +++ b/src/CadViewerJscad.tsx @@ -20,6 +20,7 @@ import { VisibleSTLModel } from "./three-components/VisibleSTLModel" import { ThreeErrorBoundary } from "./three-components/ThreeErrorBoundary" import { addFauxBoardIfNeeded } from "./utils/preprocess-circuit-json" import { tuple } from "./utils/tuple" +import { useLayerVisibility } from "./contexts/LayerVisibilityContext" interface Props { /** @@ -121,9 +122,13 @@ export const CadViewerJscad = forwardRef< // Use the state `boardGeom` which starts simplified and gets updated const { stls: boardStls, loading } = useStlsFromGeom(boardGeom) + const { visibility } = useLayerVisibility() const cad_components = su(internalCircuitJson).cad_component.list() + // If this is a faux board and faux board visibility is disabled, don't render the board + const shouldRenderBoard = !(isFauxBoard && !visibility.fauxBoard) + return ( - {boardStls.map(({ stlData, color, layerType }, index) => ( - - ))} + {shouldRenderBoard && + boardStls.map(({ stlData, color, layerType }, index) => ( + + ))} {cad_components.map((cad_component) => ( { const { rootObject } = useThree() const { visibility } = useLayerVisibility() @@ -38,6 +40,23 @@ const BoardMeshes = ({ useEffect(() => { if (!rootObject) return + // If this is a faux board and faux board visibility is disabled, don't render anything + // But we still need to clean up any previously added meshes + if (isFauxBoard && !visibility.fauxBoard) { + // Clean up any previously added meshes + geometryMeshes.forEach((mesh) => { + if (mesh.parent === rootObject) { + rootObject.remove(mesh) + } + }) + textureMeshes.forEach((mesh) => { + if (mesh.parent === rootObject) { + rootObject.remove(mesh) + } + }) + return + } + // Filter and add meshes based on visibility settings geometryMeshes.forEach((mesh) => { let shouldShow = true @@ -118,7 +137,7 @@ const BoardMeshes = ({ } }) } - }, [rootObject, geometryMeshes, textureMeshes, visibility]) + }, [rootObject, geometryMeshes, textureMeshes, visibility, isFauxBoard]) return null } @@ -238,6 +257,7 @@ try { error: builderError, isLoading: builderIsLoading, boardData, + isFauxBoard, } = useManifoldBoardBuilder(manifoldJSModule, circuitJson) const geometryMeshes = useMemo(() => createGeometryMeshes(geoms), [geoms]) @@ -325,6 +345,7 @@ try { {cadComponents.map((cad_component: CadComponent) => ( { + e.preventDefault()} + onPointerDown={(e) => { + e.preventDefault() + setLayerVisibility("fauxBoard", !visibility.fauxBoard) + }} + onMouseEnter={() => setHoveredItem("fauxBoard")} + onMouseLeave={() => setHoveredItem(null)} + onTouchStart={() => setHoveredItem("fauxBoard")} + > + + {visibility.fauxBoard && } + + + Faux Board + + +