Skip to content
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
13 changes: 9 additions & 4 deletions lib/converters/circuit-to-3d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ import type {
import {
fitMeshToCadBounds,
getMeshOrigin,
getModelOrientationRotation,
getMeshWithBoardNormalTransform,
} from "../utils/cad-mesh-placement"
import { getDefaultModelTransform } from "../utils/get-default-model-transform"
import {
getBoundingBoxSize,
rotateMesh,
scaleMesh,
translateMesh,
} from "../utils/mesh-scale"
Expand Down Expand Up @@ -491,9 +490,15 @@ export async function convertCircuitJsonTo3D(
}

if (box.mesh) {
box.mesh = rotateMesh(box.mesh, getModelOrientationRotation(cad))
box.mesh = getMeshWithBoardNormalTransform(
box.mesh,
cad.model_board_normal_direction,
)

const meshOrigin = getMeshOrigin(cad, box.mesh.boundingBox)
const meshOrigin = getMeshOrigin(cad, box.mesh.boundingBox, {
loaderTransform: defaultTransform,
modelBoardNormalDirection: cad.model_board_normal_direction,
})
if (meshOrigin) {
box.mesh = translateMesh(box.mesh, {
x: -meshOrigin.x,
Expand Down
54 changes: 49 additions & 5 deletions lib/utils/cad-mesh-placement.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
import type { CadComponent } from "circuit-json"
import type { OBJMesh, Point3, STLMesh } from "../types"
import type {
CoordinateTransformConfig,
OBJMesh,
Point3,
STLMesh,
} from "../types"
import { applyCoordinateTransform } from "./coordinate-transform"
import {
getBoundingBoxCenter,
getBoundingBoxSize,
rotatePoint,
rotateMesh,
scaleMesh,
scaleMeshByAxis,
} from "./mesh-scale"

export function getModelOrientationRotation(cad: CadComponent): Point3 {
const modelBoardNormalDirection = cad.model_board_normal_direction
if (!modelBoardNormalDirection) {
function getOrientationRotationForBoardNormal(
modelBoardNormalDirection?: CadComponent["model_board_normal_direction"],
): Point3 {
if (!modelBoardNormalDirection || modelBoardNormalDirection === "z+") {
return { x: 0, y: 0, z: 0 }
}

switch (modelBoardNormalDirection) {
case "x+":
return { x: 0, y: 0, z: 90 }
case "x-":
return { x: 0, y: 0, z: -90 }
case "y+":
return { x: 0, y: 0, z: 0 }
case "y-":
return { x: 0, y: 0, z: 180 }
case "z-":
Expand All @@ -25,12 +38,43 @@ export function getModelOrientationRotation(cad: CadComponent): Point3 {
}
}

export function getMeshWithBoardNormalTransform<T extends STLMesh | OBJMesh>(
mesh: T,
modelBoardNormalDirection?: CadComponent["model_board_normal_direction"],
): T {
return rotateMesh(
mesh,
getOrientationRotationForBoardNormal(modelBoardNormalDirection),
)
}

export function getMeshOrigin(
cad: CadComponent,
meshBounds: { min: Point3; max: Point3 },
options?: {
loaderTransform?: CoordinateTransformConfig
modelBoardNormalDirection?: CadComponent["model_board_normal_direction"]
},
): Point3 | null {
if (cad.model_origin_position) {
return cad.model_origin_position
let origin: Point3 = {
x: cad.model_origin_position.x,
y: cad.model_origin_position.y,
z: cad.model_origin_position.z,
}

if (options?.loaderTransform) {
origin = applyCoordinateTransform(origin, options.loaderTransform)
}

if (options?.modelBoardNormalDirection) {
origin = rotatePoint(
origin,
getOrientationRotationForBoardNormal(options.modelBoardNormalDirection),
)
}

return origin
}

switch (cad.model_origin_alignment) {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/mesh-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function scalePointByAxis(point: Point3, scale: Point3): Point3 {
}
}

function rotatePoint(point: Point3, rotationDeg: Point3): Point3 {
export function rotatePoint(point: Point3, rotationDeg: Point3): Point3 {
let { x, y, z } = point

if (rotationDeg.x !== 0) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions tests/snapshot/arduino-model-placement.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { test, expect } from "bun:test"
import { renderGLTFToPNGBufferFromGLBBuffer } from "poppygl"
import { convertCircuitJsonToGltf } from "../../lib/index"
import { getBestCameraPosition } from "../../lib/utils/camera-position"
import type { CircuitJson } from "circuit-json"

test("cad model placment", async () => {
const circuitJson = [
{
type: "pcb_board",
pcb_board_id: "pcb_board_0",
center: { x: 0, y: 0 },
width: 50,
height: 30,
thickness: 1.4,
},
{
type: "source_component",
source_component_id: "source_component_0",
name: "U1",
display_value: "C9900017879",
},
{
type: "pcb_component",
pcb_component_id: "pcb_component_0",
source_component_id: "source_component_0",
center: { x: 0, y: 0 },
width: 6,
height: 5,
layer: "top",
},
{
type: "cad_component",
cad_component_id: "cad_component_0",
position: {
x: 0,
y: 1.0658141036401503e-14,
z: 0.7,
},
rotation: {
x: 0,
y: 0,
z: 270,
},
pcb_component_id: "pcb_component_0",
source_component_id: "source_component_0",
model_stl_url: null,
model_obj_url:
"https://modelcdn.tscircuit.com/easyeda_models/download?uuid=4e90b6d8552a4e058d9ebe9d82e11f3a&pn=C9900017879&cachebust_origin=",
model_mtl_url: null,
model_gltf_url: null,
model_glb_url: null,
model_step_url: null,
model_wrl_url: null,
model_jscad: null,
model_unit_to_mm_scale_factor: null,
model_board_normal_direction: null,
model_origin_alignment: "center_of_component_on_board_surface",
anchor_alignment: "center_of_component_on_board_surface",
model_origin_position: {
x: 0,
y: 0,
z: -2.5,
},
footprinter_string: null,
show_as_translucent_model: null,
},
] as CircuitJson

const glbResult = await convertCircuitJsonToGltf(circuitJson, {
format: "glb",
boardTextureResolution: 1024,
includeModels: true,
showBoundingBoxes: false,
})

expect(glbResult).toBeInstanceOf(ArrayBuffer)
expect((glbResult as ArrayBuffer).byteLength).toBeGreaterThan(0)

const cameraOptions = getBestCameraPosition(circuitJson)

expect(
renderGLTFToPNGBufferFromGLBBuffer(glbResult as ArrayBuffer, cameraOptions),
).toMatchPngSnapshot(import.meta.path)
})
Loading