easyeda 0.0.385 → 0.0.386

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/main.cjs CHANGED
@@ -1078,7 +1078,7 @@ var import_promises = __toESM(require("fs/promises"), 1);
1078
1078
  var package_default = {
1079
1079
  name: "easyeda",
1080
1080
  type: "module",
1081
- version: "0.0.384",
1081
+ version: "0.0.385",
1082
1082
  files: [
1083
1083
  "dist"
1084
1084
  ],
@@ -16560,24 +16560,163 @@ function generateArcFromSweep(startX, startY, endX, endY, radius, largeArcFlag,
16560
16560
  // lib/utils/easyeda-unit-to-mm.ts
16561
16561
  var mil10ToMm = (value) => value * 10 * 0.0254;
16562
16562
 
16563
+ // lib/utils/parse-easyeda-path-commands.ts
16564
+ var argumentCounts = {
16565
+ M: 2,
16566
+ L: 2,
16567
+ H: 1,
16568
+ V: 1,
16569
+ C: 6,
16570
+ S: 4,
16571
+ Q: 4,
16572
+ T: 2,
16573
+ A: 7,
16574
+ Z: 0
16575
+ };
16576
+ var parseEasyEdaPathCommands = (pathData) => {
16577
+ const commands = [];
16578
+ let currentCommand;
16579
+ let lastTokenEnd = 0;
16580
+ for (const token of pathData.matchAll(
16581
+ /([a-df-z])|([-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][-+]?\d+)?)/gi
16582
+ )) {
16583
+ if (!/^[\s,]*$/.test(pathData.slice(lastTokenEnd, token.index))) return null;
16584
+ lastTokenEnd = token.index + token[0].length;
16585
+ if (token[1]) {
16586
+ if (!Object.hasOwn(argumentCounts, token[1].toUpperCase())) return null;
16587
+ currentCommand = { command: token[1], arguments: [] };
16588
+ commands.push(currentCommand);
16589
+ } else {
16590
+ const commandArgument = Number(token[2]);
16591
+ if (!currentCommand || !Number.isFinite(commandArgument)) return null;
16592
+ currentCommand.arguments.push(commandArgument);
16593
+ }
16594
+ }
16595
+ if (!/^[\s,]*$/.test(pathData.slice(lastTokenEnd))) return null;
16596
+ if (commands[0]?.command.toUpperCase() !== "M") return null;
16597
+ for (const pathCommand of commands) {
16598
+ const argumentCount = argumentCounts[pathCommand.command.toUpperCase()];
16599
+ const count = pathCommand.arguments.length;
16600
+ if (argumentCount === 0 ? count !== 0 : count === 0 || count % argumentCount !== 0) {
16601
+ return null;
16602
+ }
16603
+ }
16604
+ return commands;
16605
+ };
16606
+
16607
+ // lib/utils/get-easyeda-npth-pill-hole.ts
16608
+ var PATH_GEOMETRY_TOLERANCE = 1e-9;
16609
+ var isClose = (a, b) => Math.abs(a - b) < PATH_GEOMETRY_TOLERANCE;
16610
+ var getEasyEdaNpthPillHole = (pathData) => {
16611
+ const commands = parseEasyEdaPathCommands(pathData);
16612
+ if (!commands || commands.map((command) => command.command).join("") !== "MLALAZ") {
16613
+ return null;
16614
+ }
16615
+ const coordinates = commands.slice(0, -1).map((command) => command.arguments);
16616
+ if (coordinates.map((command) => command.length).join(",") !== "2,2,7,2,7") {
16617
+ return null;
16618
+ }
16619
+ const [start, firstLineEnd, firstArc, secondLineEnd, secondArc] = coordinates;
16620
+ const firstArcEnd = firstArc.slice(5);
16621
+ const secondArcEnd = secondArc.slice(5);
16622
+ const radius = firstArc[0];
16623
+ if (radius <= 0 || !isClose(firstArc[1], radius) || !isClose(secondArc[0], radius) || !isClose(secondArc[1], radius) || ![0, 1].includes(firstArc[3]) || ![0, 1].includes(secondArc[3]) || ![0, 1].includes(firstArc[4]) || firstArc[4] !== secondArc[4] || !isClose(start[0], secondArcEnd[0]) || !isClose(start[1], secondArcEnd[1])) {
16624
+ return null;
16625
+ }
16626
+ const horizontal = isClose(start[1], firstLineEnd[1]) && isClose(firstLineEnd[0], firstArcEnd[0]) && isClose(firstArcEnd[1], secondLineEnd[1]) && isClose(secondLineEnd[0], start[0]) && isClose(Math.abs(firstArcEnd[1] - firstLineEnd[1]), radius * 2);
16627
+ const vertical = isClose(start[0], firstLineEnd[0]) && isClose(firstLineEnd[1], firstArcEnd[1]) && isClose(firstArcEnd[0], secondLineEnd[0]) && isClose(secondLineEnd[1], start[1]) && isClose(Math.abs(firstArcEnd[0] - firstLineEnd[0]), radius * 2);
16628
+ if (!horizontal && !vertical) return null;
16629
+ const straightLength = horizontal ? Math.abs(start[0] - firstLineEnd[0]) : Math.abs(start[1] - firstLineEnd[1]);
16630
+ if (straightLength < PATH_GEOMETRY_TOLERANCE) return null;
16631
+ const crossProduct = (firstLineEnd[0] - firstArcEnd[0]) * (firstLineEnd[1] - start[1]) - (firstLineEnd[1] - firstArcEnd[1]) * (firstLineEnd[0] - start[0]);
16632
+ if (firstArc[4] !== Number(crossProduct > 0)) return null;
16633
+ return {
16634
+ hole_shape: "pill",
16635
+ x: mil2mm((start[0] + firstArcEnd[0]) / 2) * 10,
16636
+ y: mil2mm((start[1] + firstArcEnd[1]) / 2) * 10,
16637
+ hole_width: mil2mm(horizontal ? straightLength + radius * 2 : radius * 2) * 10,
16638
+ hole_height: mil2mm(vertical ? straightLength + radius * 2 : radius * 2) * 10
16639
+ };
16640
+ };
16641
+
16642
+ // lib/utils/convert-easyeda-npth-cutouts-to-holes.ts
16643
+ var convertEasyEdaNpthCutoutsToHoles = (circuitJson, npthRegions) => {
16644
+ const npthPillHoles = npthRegions.map((region, index) => ({
16645
+ pcb_cutout_id: `pcb_cutout_npth_${index + 1}`,
16646
+ hole: getEasyEdaNpthPillHole(region.pathData)
16647
+ }));
16648
+ return circuitJson.map((element) => {
16649
+ if (element.type !== "pcb_cutout" || element.shape !== "polygon") {
16650
+ return element;
16651
+ }
16652
+ const npthPillHole = npthPillHoles.find(
16653
+ (candidate) => candidate.pcb_cutout_id === element.pcb_cutout_id
16654
+ );
16655
+ if (!npthPillHole?.hole) return element;
16656
+ const xs = element.points.map((point4) => point4.x);
16657
+ const ys = element.points.map((point4) => point4.y);
16658
+ return pcb_hole.parse({
16659
+ type: "pcb_hole",
16660
+ pcb_hole_id: element.pcb_cutout_id.replace("pcb_cutout_", "pcb_hole_"),
16661
+ pcb_component_id: element.pcb_component_id,
16662
+ ...npthPillHole.hole,
16663
+ x: (Math.min(...xs) + Math.max(...xs)) / 2,
16664
+ y: (Math.min(...ys) + Math.max(...ys)) / 2
16665
+ });
16666
+ });
16667
+ };
16668
+
16563
16669
  // lib/utils/expand-easyeda-path-data.ts
16564
16670
  var expandEasyEdaPathData = (pathData) => {
16671
+ const commands = parseEasyEdaPathCommands(pathData);
16672
+ if (!commands) throw new Error("Invalid EasyEDA SOLIDREGION path");
16673
+ for (const { command } of commands) {
16674
+ if (!/^[MLHVAZ]$/i.test(command)) {
16675
+ throw new Error(
16676
+ `Unsupported EasyEDA SOLIDREGION path command: ${command}`
16677
+ );
16678
+ }
16679
+ }
16680
+ if (commands.slice(1).some(({ command }) => command.toUpperCase() === "M")) {
16681
+ throw new Error("Multiple EasyEDA SOLIDREGION subpaths are not supported");
16682
+ }
16565
16683
  const rawRoute = [];
16566
16684
  let currentPoint;
16567
- for (const commandMatch of pathData.matchAll(/([MLAZ])([^MLAZ]*)/gi)) {
16568
- const command = commandMatch[1]?.toUpperCase();
16569
- const values = commandMatch[2]?.match(/[-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][-+]?\d+)?/g)?.map(Number) ?? [];
16570
- if ((command === "M" || command === "L") && values.length >= 2) {
16571
- for (let offset = 0; offset + 1 < values.length; offset += 2) {
16572
- currentPoint = { x: values[offset], y: values[offset + 1] };
16685
+ let subpathStart;
16686
+ let afterClose = false;
16687
+ for (const pathCommand of commands) {
16688
+ const command = pathCommand.command.toUpperCase();
16689
+ const relative = pathCommand.command !== command;
16690
+ const commandArguments = pathCommand.arguments;
16691
+ if (afterClose && command !== "Z" && currentPoint) {
16692
+ const lastPoint = rawRoute.at(-1);
16693
+ if (lastPoint?.x !== currentPoint.x || lastPoint?.y !== currentPoint.y) {
16573
16694
  rawRoute.push(currentPoint);
16574
16695
  }
16575
- } else if (command === "A" && currentPoint && values.length >= 7) {
16576
- for (let offset = 0; offset + 6 < values.length; offset += 7) {
16577
- const [radiusX, , , largeArcFlag, sweepFlag, endX, endY] = values.slice(
16578
- offset,
16579
- offset + 7
16580
- );
16696
+ }
16697
+ afterClose = command === "Z";
16698
+ if ((command === "M" || command === "L") && commandArguments.length >= 2) {
16699
+ for (let offset = 0; offset + 1 < commandArguments.length; offset += 2) {
16700
+ currentPoint = {
16701
+ x: commandArguments[offset] + (relative ? currentPoint?.x ?? 0 : 0),
16702
+ y: commandArguments[offset + 1] + (relative ? currentPoint?.y ?? 0 : 0)
16703
+ };
16704
+ rawRoute.push(currentPoint);
16705
+ if (command === "M" && offset === 0) subpathStart = currentPoint;
16706
+ }
16707
+ } else if ((command === "H" || command === "V") && currentPoint) {
16708
+ for (const coordinate of commandArguments) {
16709
+ currentPoint = {
16710
+ x: command === "H" ? coordinate + (relative ? currentPoint.x : 0) : currentPoint.x,
16711
+ y: command === "V" ? coordinate + (relative ? currentPoint.y : 0) : currentPoint.y
16712
+ };
16713
+ rawRoute.push(currentPoint);
16714
+ }
16715
+ } else if (command === "A" && currentPoint && commandArguments.length >= 7) {
16716
+ for (let offset = 0; offset + 6 < commandArguments.length; offset += 7) {
16717
+ const [radiusX, , , largeArcFlag, sweepFlag, endpointX, endpointY] = commandArguments.slice(offset, offset + 7);
16718
+ const endX = endpointX + (relative ? currentPoint.x : 0);
16719
+ const endY = endpointY + (relative ? currentPoint.y : 0);
16581
16720
  const generatedArcRoute = generateArcFromSweep(
16582
16721
  currentPoint.x,
16583
16722
  currentPoint.y,
@@ -16599,6 +16738,8 @@ var expandEasyEdaPathData = (pathData) => {
16599
16738
  rawRoute.push(...arcRoute.slice(1));
16600
16739
  currentPoint = { x: endX, y: endY };
16601
16740
  }
16741
+ } else if (command === "Z" && subpathStart) {
16742
+ currentPoint = subpathStart;
16602
16743
  }
16603
16744
  }
16604
16745
  return rawRoute;
@@ -17645,9 +17786,10 @@ var convertEasyEdaJsonToCircuitJsonWithPinAttributes = (easyEdaJson, {
17645
17786
  ).forEach((sr, index) => {
17646
17787
  circuitElements.push(handleCutout(sr, index));
17647
17788
  });
17648
- easyEdaJson.packageDetail.dataStr.shape.filter(
17789
+ const npthRegions = easyEdaJson.packageDetail.dataStr.shape.filter(
17649
17790
  (shape) => shape.type === "SOLIDREGION" && isPcbSolidRegionNpth(shape)
17650
- ).forEach((sr, index) => {
17791
+ );
17792
+ npthRegions.forEach((sr, index) => {
17651
17793
  const geometry = getEasyEdaNpthSlotGeometry(sr);
17652
17794
  if (!geometry) return;
17653
17795
  circuitElements.push(
@@ -17942,7 +18084,10 @@ var convertEasyEdaJsonToCircuitJsonWithPinAttributes = (easyEdaJson, {
17942
18084
  }
17943
18085
  pcb_component2.center = { x: 0, y: 0 };
17944
18086
  }
17945
- return { circuitJson: circuitElements, resolvedPinAttributes };
18087
+ return {
18088
+ circuitJson: convertEasyEdaNpthCutoutsToHoles(circuitElements, npthRegions),
18089
+ resolvedPinAttributes
18090
+ };
17946
18091
  };
17947
18092
  var convertEasyEdaJsonToCircuitJson = (easyEdaJson, options = {}) => convertEasyEdaJsonToCircuitJsonWithPinAttributes(easyEdaJson, options).circuitJson;
17948
18093