Skip to content

Commit 71b78fc

Browse files
Fix through-hole component positioning in STEP model merger (#33)
* Fix through-hole component positioning in STEP model merger * fix
1 parent 78bdb39 commit 71b78fc

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

lib/step-model-merger.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,25 @@ function adjustTransformForPlacement(
189189
const targetY = transform.translation.y
190190
const targetZ = transform.translation.z
191191

192-
transform.translation.x = targetX - center.x
193-
transform.translation.y = targetY - center.y
194-
195192
// Check if this is a through-hole component (model spans both positive and negative Z)
196193
// Through-hole components have their z=0 as the natural reference point (e.g., flange position)
197-
const isThroughHoleComponent = minZ < 0 && maxZ > 0
194+
// Use a small tolerance to avoid floating point precision issues (e.g., -2.78e-17 being treated as negative)
195+
// but not so large that it excludes real geometry (e.g., MachineContactMedium has maxZ = 0.083mm)
196+
const THROUGH_HOLE_Z_TOLERANCE = 0.001 // 1 micrometer - filters floating point noise but keeps real geometry
197+
const isThroughHoleComponent =
198+
minZ < -THROUGH_HOLE_Z_TOLERANCE && maxZ > THROUGH_HOLE_Z_TOLERANCE
199+
200+
// Center X/Y by bounding box for all components
201+
transform.translation.x = targetX - center.x
202+
transform.translation.y = targetY - center.y
198203

199204
if (isThroughHoleComponent) {
200-
// For through-hole components, use the position.z directly as the offset from board top
201-
// The model's z=0 is the reference point, so we just add board thickness to get to top surface
205+
// Place model's z=0 at board top surface.
206+
// position.z = 0 means the component sits directly on the board top.
202207
transform.translation.z = targetZ + boardThickness
203-
} else if (boardThickness > 0) {
208+
}
209+
210+
if (!isThroughHoleComponent && boardThickness > 0) {
204211
const halfThickness = boardThickness / 2
205212
const offsetZ = targetZ - halfThickness
206213
if (normalizedLayer === "bottom") {
@@ -209,9 +216,11 @@ function adjustTransformForPlacement(
209216
} else {
210217
transform.translation.z = boardThickness - minZ + offsetZ
211218
}
212-
} else {
219+
} else if (!isThroughHoleComponent) {
220+
// Only apply center-based Z for non-through-hole components without board thickness
213221
transform.translation.z = targetZ - center.z
214222
}
223+
// For through-hole components, Z is already set above
215224
}
216225

217226
function normalizeDegrees(value: number): number {

0 commit comments

Comments
 (0)