Skip to content
Draft
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
29 changes: 26 additions & 3 deletions lib/converters/circuit-to-3d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
CircuitTo3DOptions,
Camera3D,
Light3D,
ExternalGLTFInstance,
} from "../types"
import { loadSTL } from "../loaders/stl"
import { loadOBJ } from "../loaders/obj"
Expand Down Expand Up @@ -47,6 +48,7 @@ export async function convertCircuitJsonTo3D(

const db: any = cju(circuitJson)
const boxes: Box3D[] = []
const externalGLTFs: ExternalGLTFInstance[] = []

// Get PCB board
const pcbBoard = db.pcb_board.list()[0]
Expand Down Expand Up @@ -95,7 +97,12 @@ export async function convertCircuitJsonTo3D(

for (const cad of cadComponents) {
const { model_stl_url, model_obj_url } = cad
if (!model_stl_url && !model_obj_url) continue
const model_gltf_url = (cad as any).model_gltf_url as string | undefined
const model_glb_url = (cad as any).model_glb_url as string | undefined

if (!model_stl_url && !model_obj_url && !model_gltf_url && !model_glb_url) {
continue
}

pcbComponentIdsWith3D.add(cad.pcb_component_id)

Expand All @@ -118,6 +125,21 @@ export async function convertCircuitJsonTo3D(
z: pcbComponent?.center.y ?? 0,
}

const rotation = cad.rotation
? convertRotationFromCadRotation(cad.rotation)
: undefined

if (model_gltf_url || model_glb_url) {
externalGLTFs.push({
url: model_gltf_url ?? model_glb_url!,
format: model_gltf_url ? "gltf" : "glb",
name: cad.pcb_component_id,
translation: center,
rotation,
})
continue
}

const box: Box3D = {
center,
size,
Expand All @@ -127,8 +149,8 @@ export async function convertCircuitJsonTo3D(
}

// Add rotation if specified
if (cad.rotation) {
box.rotation = convertRotationFromCadRotation(cad.rotation)
if (rotation) {
box.rotation = rotation
}

// Try to load the mesh with default coordinate transform if none specified
Expand Down Expand Up @@ -218,5 +240,6 @@ export async function convertCircuitJsonTo3D(
boxes,
camera,
lights,
externalGLTFs: externalGLTFs.length > 0 ? externalGLTFs : undefined,
}
}
10 changes: 10 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { CircuitJson } from "circuit-json"
import type { ConversionOptions } from "./types"
import { convertCircuitJsonTo3D } from "./converters/circuit-to-3d"
import { convertSceneToGLTF } from "./converters/scene-to-gltf"
import { combineBaseGLTFWithExternalModels } from "./utils/combine-gltf"

export async function convertCircuitJsonToGltf(
circuitJson: CircuitJson,
Expand Down Expand Up @@ -32,6 +33,14 @@ export async function convertCircuitJsonToGltf(

const result = await convertSceneToGLTF(scene3D, gltfOptions)

if (scene3D.externalGLTFs?.length) {
return combineBaseGLTFWithExternalModels(
result,
format,
scene3D.externalGLTFs,
)
}

return result
}

Expand All @@ -54,6 +63,7 @@ export type {
CircuitTo3DOptions,
BoardRenderOptions,
CoordinateTransformConfig,
ExternalGLTFInstance,
} from "./types"

// Re-export loaders
Expand Down
10 changes: 10 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,20 @@ export interface Box3D {
labelColor?: Color
}

export interface ExternalGLTFInstance {
url: string
format: "gltf" | "glb"
name?: string
translation: Point3
rotation?: Point3
scale?: Point3
}

export interface Scene3D {
boxes: Box3D[]
camera?: Camera3D
lights?: Light3D[]
externalGLTFs?: ExternalGLTFInstance[]
}

export interface Camera3D {
Expand Down
Loading
Loading