@tscircuit/circuit-json-util 0.0.133 → 0.0.135

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -836,7 +836,8 @@ import {
836
836
  applyToPoint as applyToPoint2,
837
837
  compose,
838
838
  decomposeTSR,
839
- rotateDEG
839
+ rotateDEG,
840
+ scale
840
841
  } from "transformation-matrix";
841
842
 
842
843
  // lib/direction-to-vec.ts
@@ -1022,9 +1023,99 @@ var transformSchematicElement = (elm, matrix) => {
1022
1023
  var transformSchematicElements = (elms, matrix) => {
1023
1024
  return elms.map((elm) => transformSchematicElement(elm, matrix));
1024
1025
  };
1026
+ var isReflection = (matrix) => matrix.a * matrix.d - matrix.b * matrix.c < 0;
1025
1027
  var getTransformedRectRotation = (rotation, matrix) => {
1026
- const isReflected = matrix.a * matrix.d - matrix.b * matrix.c < 0;
1027
- return decomposeTSR(compose(matrix, rotateDEG(rotation)), false, isReflected).rotation.angle * 180 / Math.PI;
1028
+ return decomposeTSR(
1029
+ compose(matrix, rotateDEG(rotation)),
1030
+ false,
1031
+ isReflection(matrix)
1032
+ ).rotation.angle * 180 / Math.PI;
1033
+ };
1034
+ var normalizeHoleRotation = (degrees) => {
1035
+ const normalized = (degrees % 360 + 360) % 360;
1036
+ const rounded = Math.round(normalized);
1037
+ const snapped = Math.abs(normalized - rounded) < 1e-6 ? rounded : normalized;
1038
+ return snapped === 360 ? 0 : snapped;
1039
+ };
1040
+ var getTransformedHoleRotation = (rotation, matrix) => {
1041
+ const transformed = normalizeHoleRotation(
1042
+ getTransformedRectRotation(rotation ?? 0, matrix)
1043
+ );
1044
+ return isReflection(matrix) ? transformed % 180 : transformed;
1045
+ };
1046
+ var transformPolygonPadOrientation = (hole, matrix) => {
1047
+ const transformed = normalizeHoleRotation(
1048
+ getTransformedRectRotation(hole.ccw_rotation ?? 0, matrix)
1049
+ );
1050
+ if (!isReflection(matrix)) {
1051
+ hole.ccw_rotation = transformed;
1052
+ return;
1053
+ }
1054
+ const localMirror = transformed >= 180 ? scale(1, -1) : scale(-1, 1);
1055
+ hole.pad_outline = hole.pad_outline.map(
1056
+ (point) => applyToPoint2(localMirror, point)
1057
+ );
1058
+ hole.ccw_rotation = transformed % 180;
1059
+ };
1060
+ var transformHoleOffset = (hole, matrix) => {
1061
+ const { hole_offset_x: x = 0, hole_offset_y: y = 0 } = hole;
1062
+ if (x === 0 && y === 0) return;
1063
+ const offset = applyToPoint2({ ...matrix, e: 0, f: 0 }, { x, y });
1064
+ hole.hole_offset_x = offset.x;
1065
+ hole.hole_offset_y = offset.y;
1066
+ };
1067
+ var transformHoleOrientation = (elm, matrix) => {
1068
+ const appliedRotation = Math.atan2(matrix.b, matrix.a) * 180 / Math.PI;
1069
+ if (!isReflection(matrix) && normalizeHoleRotation(appliedRotation) === 0) {
1070
+ return;
1071
+ }
1072
+ if (elm.type === "pcb_plated_hole") {
1073
+ if ("hole_offset_x" in elm) transformHoleOffset(elm, matrix);
1074
+ switch (elm.shape) {
1075
+ case "pill":
1076
+ case "oval":
1077
+ elm.ccw_rotation = getTransformedHoleRotation(elm.ccw_rotation, matrix);
1078
+ return;
1079
+ case "hole_with_polygon_pad":
1080
+ transformPolygonPadOrientation(elm, matrix);
1081
+ return;
1082
+ case "circular_hole_with_rect_pad":
1083
+ elm.rect_ccw_rotation = getTransformedHoleRotation(
1084
+ elm.rect_ccw_rotation,
1085
+ matrix
1086
+ );
1087
+ return;
1088
+ case "rotated_pill_hole_with_rect_pad":
1089
+ elm.hole_ccw_rotation = getTransformedHoleRotation(
1090
+ elm.hole_ccw_rotation,
1091
+ matrix
1092
+ );
1093
+ elm.rect_ccw_rotation = getTransformedHoleRotation(
1094
+ elm.rect_ccw_rotation,
1095
+ matrix
1096
+ );
1097
+ return;
1098
+ case "pill_hole_with_rect_pad": {
1099
+ const rotation = getTransformedHoleRotation(0, matrix);
1100
+ if (rotation === 0) return;
1101
+ Object.assign(elm, {
1102
+ shape: "rotated_pill_hole_with_rect_pad",
1103
+ hole_shape: "rotated_pill",
1104
+ hole_ccw_rotation: rotation,
1105
+ rect_ccw_rotation: rotation
1106
+ });
1107
+ return;
1108
+ }
1109
+ }
1110
+ return;
1111
+ }
1112
+ if (elm.hole_shape === "rotated_pill") {
1113
+ elm.ccw_rotation = getTransformedHoleRotation(elm.ccw_rotation, matrix);
1114
+ } else if (elm.hole_shape === "pill") {
1115
+ const rotation = getTransformedHoleRotation(0, matrix);
1116
+ if (rotation === 0) return;
1117
+ Object.assign(elm, { hole_shape: "rotated_pill", ccw_rotation: rotation });
1118
+ }
1028
1119
  };
1029
1120
  var transformPcbSolderPasteRectOrPill = (paste, matrix) => {
1030
1121
  const center = applyToPoint2(matrix, { x: paste.x, y: paste.y });
@@ -1117,11 +1208,11 @@ var transformPCBElement = (elm, matrix) => {
1117
1208
  });
1118
1209
  elm.x = x;
1119
1210
  elm.y = y;
1120
- if (elm.type === "pcb_smtpad" && (elm.shape === "rotated_rect" || elm.shape === "rotated_pill") || elm.type === "pcb_plated_hole" && (elm.shape === "pill" || elm.shape === "oval")) {
1121
- elm.ccw_rotation = getTransformedRectRotation(
1122
- elm.ccw_rotation ?? 0,
1123
- matrix
1124
- );
1211
+ if (elm.type === "pcb_smtpad" && (elm.shape === "rotated_rect" || elm.shape === "rotated_pill")) {
1212
+ elm.ccw_rotation = getTransformedRectRotation(elm.ccw_rotation, matrix);
1213
+ }
1214
+ if (elm.type === "pcb_plated_hole" || elm.type === "pcb_hole") {
1215
+ transformHoleOrientation(elm, matrix);
1125
1216
  }
1126
1217
  } else if (elm.type === "pcb_keepout" && elm.shape === "outline") {
1127
1218
  elm.outline = elm.outline.map((point) => applyToPoint2(matrix, point));
@@ -3290,6 +3381,206 @@ var getTraceSegmentPolygon = (start, end, startWidth, endWidth = startWidth) =>
3290
3381
  );
3291
3382
  };
3292
3383
  var copperPolygonsTouch = (a, b, tolerance = 1e-7) => a.vertices.some((p) => b.contains(p)) || b.vertices.some((p) => a.contains(p)) || a.distanceTo(b)[0] <= tolerance;
3384
+
3385
+ // lib/convert-schematic-length-unit.ts
3386
+ import {
3387
+ LEGACY_SCHEMATIC_LENGTH_UNIT,
3388
+ MM_PER_SCHEMATIC_UNIT
3389
+ } from "circuit-json";
3390
+ var millimetersPerUnit = {
3391
+ mm: 1,
3392
+ in: 25.4,
3393
+ [LEGACY_SCHEMATIC_LENGTH_UNIT]: MM_PER_SCHEMATIC_UNIT
3394
+ };
3395
+ function getSchematicLengthUnitScale(from, to) {
3396
+ return millimetersPerUnit[from] / millimetersPerUnit[to];
3397
+ }
3398
+ function convertSchematicLengthUnit(element, unit) {
3399
+ const from = "schematic_length_unit" in element ? element.schematic_length_unit : void 0;
3400
+ if (unit === from) return element;
3401
+ const factor = getSchematicLengthUnitScale(
3402
+ from ?? LEGACY_SCHEMATIC_LENGTH_UNIT,
3403
+ unit
3404
+ );
3405
+ const point = (p) => ({
3406
+ ...p,
3407
+ x: p.x * factor,
3408
+ y: p.y * factor
3409
+ });
3410
+ const lengths = (obj, ...keys) => {
3411
+ const result = { ...obj };
3412
+ for (const key of keys) {
3413
+ const value = obj[key];
3414
+ if (typeof value === "number")
3415
+ Object.assign(result, { [key]: value * factor });
3416
+ }
3417
+ return result;
3418
+ };
3419
+ const tag = { schematic_length_unit: unit };
3420
+ switch (element.type) {
3421
+ case "schematic_sheet":
3422
+ return {
3423
+ ...element,
3424
+ ...tag,
3425
+ ...element.center && { center: point(element.center) }
3426
+ };
3427
+ case "schematic_net_label":
3428
+ return {
3429
+ ...element,
3430
+ ...tag,
3431
+ center: point(element.center),
3432
+ ...element.anchor_position && {
3433
+ anchor_position: point(element.anchor_position)
3434
+ }
3435
+ };
3436
+ case "schematic_port":
3437
+ return {
3438
+ ...lengths(
3439
+ element,
3440
+ "display_pin_label_font_size",
3441
+ "distance_from_component_edge"
3442
+ ),
3443
+ ...tag,
3444
+ center: point(element.center),
3445
+ ...element.boundary_position && {
3446
+ boundary_position: point(element.boundary_position)
3447
+ }
3448
+ };
3449
+ case "schematic_component":
3450
+ return {
3451
+ ...lengths(element, "pin_spacing", "box_width"),
3452
+ ...tag,
3453
+ center: point(element.center),
3454
+ size: lengths(element.size, "width", "height"),
3455
+ ...element.pin_styles && {
3456
+ pin_styles: Object.fromEntries(
3457
+ Object.entries(element.pin_styles).map(([pin, style]) => [
3458
+ pin,
3459
+ lengths(
3460
+ style,
3461
+ "left_margin",
3462
+ "right_margin",
3463
+ "top_margin",
3464
+ "bottom_margin"
3465
+ )
3466
+ ])
3467
+ )
3468
+ }
3469
+ };
3470
+ case "schematic_text":
3471
+ return {
3472
+ ...lengths(element, "font_size"),
3473
+ ...tag,
3474
+ position: point(element.position)
3475
+ };
3476
+ case "schematic_voltage_probe":
3477
+ return { ...element, ...tag, position: point(element.position) };
3478
+ case "schematic_debug_object":
3479
+ if (element.shape === "line")
3480
+ return {
3481
+ ...element,
3482
+ ...tag,
3483
+ start: point(element.start),
3484
+ end: point(element.end)
3485
+ };
3486
+ if (element.shape === "rect")
3487
+ return {
3488
+ ...element,
3489
+ ...tag,
3490
+ center: point(element.center),
3491
+ size: lengths(element.size, "height", "width")
3492
+ };
3493
+ return { ...element, ...tag, center: point(element.center) };
3494
+ case "schematic_trace":
3495
+ return {
3496
+ ...element,
3497
+ ...tag,
3498
+ edges: element.edges.map((edge) => ({
3499
+ ...edge,
3500
+ from: point(edge.from),
3501
+ to: point(edge.to)
3502
+ })),
3503
+ junctions: element.junctions.map(point)
3504
+ };
3505
+ case "schematic_table":
3506
+ return {
3507
+ ...lengths(element, "cell_padding", "border_width"),
3508
+ ...tag,
3509
+ anchor_position: point(element.anchor_position),
3510
+ column_widths: element.column_widths.map((width) => width * factor),
3511
+ row_heights: element.row_heights.map((height) => height * factor)
3512
+ };
3513
+ case "schematic_table_cell":
3514
+ return {
3515
+ ...lengths(element, "width", "height", "font_size"),
3516
+ ...tag,
3517
+ center: point(element.center)
3518
+ };
3519
+ case "schematic_rect":
3520
+ return {
3521
+ ...lengths(element, "width", "height", "stroke_width"),
3522
+ ...tag,
3523
+ center: point(element.center)
3524
+ };
3525
+ case "schematic_group":
3526
+ return {
3527
+ ...lengths(element, "width", "height"),
3528
+ ...tag,
3529
+ center: point(element.center)
3530
+ };
3531
+ case "schematic_arc":
3532
+ case "schematic_circle":
3533
+ return {
3534
+ ...lengths(element, "radius", "stroke_width"),
3535
+ ...tag,
3536
+ center: point(element.center)
3537
+ };
3538
+ case "schematic_box":
3539
+ return { ...lengths(element, "x", "y", "width", "height"), ...tag };
3540
+ case "schematic_graphic":
3541
+ return { ...lengths(element, "width", "height"), ...tag };
3542
+ case "schematic_line":
3543
+ return {
3544
+ ...lengths(
3545
+ element,
3546
+ "x1",
3547
+ "x2",
3548
+ "y1",
3549
+ "y2",
3550
+ "stroke_width",
3551
+ "dash_length",
3552
+ "dash_gap"
3553
+ ),
3554
+ ...tag
3555
+ };
3556
+ case "schematic_path":
3557
+ return {
3558
+ ...lengths(element, "stroke_width", "dash_length", "dash_gap"),
3559
+ ...tag,
3560
+ points: element.points.map(point)
3561
+ };
3562
+ case "source_failed_to_create_component_error":
3563
+ return {
3564
+ ...element,
3565
+ ...tag,
3566
+ ...element.schematic_center && {
3567
+ schematic_center: lengths(element.schematic_center, "x", "y")
3568
+ }
3569
+ };
3570
+ default:
3571
+ return element;
3572
+ }
3573
+ }
3574
+ function convertSchematicLengthUnits(circuit, unit) {
3575
+ if (unit === LEGACY_SCHEMATIC_LENGTH_UNIT) {
3576
+ const needsConversion = (element) => "schematic_length_unit" in element && element.schematic_length_unit !== void 0 && element.schematic_length_unit !== unit;
3577
+ if (!circuit.some(needsConversion)) return circuit;
3578
+ return circuit.map(
3579
+ (element) => needsConversion(element) ? convertSchematicLengthUnit(element, unit) : element
3580
+ );
3581
+ }
3582
+ return circuit.map((element) => convertSchematicLengthUnit(element, unit));
3583
+ }
3293
3584
  export {
3294
3585
  analyzePcbPin1Location,
3295
3586
  applySelector,
@@ -3301,6 +3592,8 @@ export {
3301
3592
  cju_indexed_default as cjuIndexed,
3302
3593
  computeClearanceBetweenElements,
3303
3594
  computeGapBetweenCopper,
3595
+ convertSchematicLengthUnit,
3596
+ convertSchematicLengthUnits,
3304
3597
  copperPolygonsTouch,
3305
3598
  createBoardOwnerMap,
3306
3599
  directionToVec,
@@ -3326,6 +3619,7 @@ export {
3326
3619
  getReadableNameForPcbSmtpad,
3327
3620
  getReadableNameForPcbTrace,
3328
3621
  getSchematicElementBounds,
3622
+ getSchematicLengthUnitScale,
3329
3623
  getSmtPadPolygon,
3330
3624
  getStringFromCircuitJsonTree,
3331
3625
  getTraceSegmentPolygon,