diff --git a/src/AnyCadComponent.tsx b/src/AnyCadComponent.tsx index 04e5a975..72c490c0 100644 --- a/src/AnyCadComponent.tsx +++ b/src/AnyCadComponent.tsx @@ -9,6 +9,7 @@ import { GltfModel } from "./three-components/GltfModel" import { JscadModel } from "./three-components/JscadModel" import { FootprinterModel } from "./three-components/FootprinterModel" import { tuple } from "./utils/tuple" +import { getModelUrl } from "./utils/get-model-url" import { Html } from "./react-three/Html" export const AnyCadComponent = ({ @@ -46,10 +47,7 @@ export const AnyCadComponent = ({ })?.name }, [circuitJson, cad_component.source_component_id]) - const url = - cad_component.model_obj_url ?? - cad_component.model_wrl_url ?? - cad_component.model_stl_url + const url = getModelUrl(cad_component) const gltfUrl = cad_component.model_gltf_url const rotationOffset = cad_component.rotation ? tuple( diff --git a/src/utils/get-model-url.ts b/src/utils/get-model-url.ts new file mode 100644 index 00000000..53f29a97 --- /dev/null +++ b/src/utils/get-model-url.ts @@ -0,0 +1,20 @@ +export function getModelUrl(component: { + model_obj_url?: string + model_wrl_url?: string + model_stl_url?: string + footprinter_string?: string +}): string | undefined { + return ( + component.model_obj_url ?? + component.model_wrl_url ?? + component.model_stl_url ?? + getKicadUrl(component.footprinter_string) + ) +} + +function getKicadUrl(footprinter?: string): string | undefined { + if (!footprinter) return undefined + if (!footprinter.startsWith("kicad:")) return undefined + const path = footprinter.replace(/^kicad:/, "").replace(/:/g, "/") + return `https://kicad-mod-cache.tscircuit.com/${path}.wrl` +} diff --git a/src/utils/render-component.tsx b/src/utils/render-component.tsx index fa8f1283..6508869c 100644 --- a/src/utils/render-component.tsx +++ b/src/utils/render-component.tsx @@ -8,17 +8,14 @@ import { executeJscadOperations } from "jscad-planner" import * as THREE from "three" import { load3DModel } from "./load-model" import type { CadComponent } from "circuit-json" +import { getModelUrl } from "./get-model-url" export async function renderComponent( component: CadComponent, scene: THREE.Scene, ) { - // Handle STL/OBJ models first - const url = - component.model_obj_url ?? - component.model_wrl_url ?? - component.model_stl_url ?? - component.model_gltf_url + // Handle STL/OBJ/WRL/GLTF models first + const url = getModelUrl(component) ?? component.model_gltf_url if (url) { const model = await load3DModel(url) if (model) {