@tscircuit/circuit-json-util 0.0.134 → 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));