kicad-to-circuit-json 0.0.146 → 0.0.147

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.
Files changed (2) hide show
  1. package/dist/index.js +136 -25
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -456,9 +456,95 @@ function getDistance(a, b) {
456
456
  return Math.hypot(b.x - a.x, b.y - a.y);
457
457
  }
458
458
 
459
+ // lib/stages/pcb/CollectFootprintsStage/assemble-courtyard-contours.ts
460
+ import { Polygon } from "@flatten-js/core";
461
+ var ENDPOINT_TOLERANCE = 1e-6;
462
+ function assembleCourtyardContours(routes) {
463
+ const nodes = [];
464
+ const edges = [];
465
+ const outlines = [];
466
+ let invalidCount = 0;
467
+ const getNode = (point) => {
468
+ const existing = nodes.findIndex(
469
+ (node) => Math.hypot(node.point.x - point.x, node.point.y - point.y) <= ENDPOINT_TOLERANCE
470
+ );
471
+ if (existing !== -1) return existing;
472
+ nodes.push({ point, edges: [] });
473
+ return nodes.length - 1;
474
+ };
475
+ for (const route of routes) {
476
+ if (route.length < 2 || route.some((p) => !Number.isFinite(p.x) || !Number.isFinite(p.y))) {
477
+ invalidCount++;
478
+ continue;
479
+ }
480
+ const start = getNode(route[0]);
481
+ const end = getNode(route[route.length - 1]);
482
+ const index = edges.length;
483
+ edges.push({ route, start, end });
484
+ nodes[start].edges.push(index);
485
+ nodes[end].edges.push(index);
486
+ }
487
+ const visited = /* @__PURE__ */ new Set();
488
+ for (let first = 0; first < edges.length; first++) {
489
+ if (visited.has(first)) continue;
490
+ const connectedEdges = /* @__PURE__ */ new Set();
491
+ const connectedNodes = /* @__PURE__ */ new Set();
492
+ const pending = [edges[first].start];
493
+ while (pending.length) {
494
+ const node = pending.pop();
495
+ if (connectedNodes.has(node)) continue;
496
+ connectedNodes.add(node);
497
+ for (const index of nodes[node].edges) {
498
+ connectedEdges.add(index);
499
+ const edge = edges[index];
500
+ pending.push(edge.start, edge.end);
501
+ }
502
+ }
503
+ for (const index of connectedEdges) visited.add(index);
504
+ if ([...connectedNodes].some((node) => nodes[node].edges.length !== 2)) {
505
+ invalidCount++;
506
+ continue;
507
+ }
508
+ const remaining = new Set(connectedEdges);
509
+ const outline = [];
510
+ let currentNode = edges[first].start;
511
+ while (remaining.size) {
512
+ const index = nodes[currentNode].edges.find((i) => remaining.has(i));
513
+ const edge = edges[index];
514
+ remaining.delete(index);
515
+ const forward = currentNode === edge.start;
516
+ const points = forward ? edge.route : [...edge.route].reverse();
517
+ if (outline.length === 0) outline.push(nodes[currentNode].point);
518
+ outline.push(...points.slice(1, -1));
519
+ currentNode = forward ? edge.end : edge.start;
520
+ outline.push(nodes[currentNode].point);
521
+ }
522
+ outline.pop();
523
+ if (outline.length < 3) {
524
+ invalidCount++;
525
+ continue;
526
+ }
527
+ const polygon = new Polygon(outline.map((p) => [p.x, p.y]));
528
+ if (polygon.area() <= 1e-12 || !polygon.isValid()) {
529
+ invalidCount++;
530
+ continue;
531
+ }
532
+ outlines.push(outline);
533
+ }
534
+ return { outlines, invalidCount };
535
+ }
536
+
459
537
  // lib/stages/pcb/CollectFootprintsStage/process-graphics.ts
460
538
  function insertFootprintRoute(options) {
461
- const { ctx, componentId, layer, renderLayer, route, strokeWidth } = options;
539
+ const {
540
+ ctx,
541
+ componentId,
542
+ layer,
543
+ renderLayer,
544
+ route,
545
+ strokeWidth,
546
+ courtyardRoutes
547
+ } = options;
462
548
  if (renderLayer.endsWith("_silkscreen")) {
463
549
  ctx.db.pcb_silkscreen_path.insert({
464
550
  pcb_component_id: componentId,
@@ -477,11 +563,7 @@ function insertFootprintRoute(options) {
477
563
  });
478
564
  return;
479
565
  }
480
- ctx.db.pcb_courtyard_outline.insert({
481
- pcb_component_id: componentId,
482
- layer,
483
- outline: route
484
- });
566
+ courtyardRoutes.push({ layer, route });
485
567
  }
486
568
  function getGraphicStrokeWidth(graphic, fallback = 0.12) {
487
569
  return graphic.stroke?.width ?? graphic.stroke?._sxWidth?.value ?? graphic._sxStroke?._sxWidth?.value ?? graphic.width ?? graphic._sxWidth?.value ?? fallback;
@@ -497,6 +579,7 @@ function rotatePoint(params) {
497
579
  function processFootprintGraphics(params) {
498
580
  const { ctx, footprint, componentId, footprintPlacement } = params;
499
581
  if (!ctx.k2cMatPcb) return;
582
+ const courtyardRoutes = [];
500
583
  const lines = footprint.fpLines || [];
501
584
  const lineArray = Array.isArray(lines) ? lines : lines ? [lines] : [];
502
585
  for (const line of lineArray) {
@@ -504,7 +587,8 @@ function processFootprintGraphics(params) {
504
587
  ctx,
505
588
  line,
506
589
  componentId,
507
- footprintPlacement
590
+ footprintPlacement,
591
+ courtyardRoutes
508
592
  });
509
593
  }
510
594
  const rects = footprint.fpRects || [];
@@ -514,7 +598,8 @@ function processFootprintGraphics(params) {
514
598
  ctx,
515
599
  rect,
516
600
  componentId,
517
- footprintPlacement
601
+ footprintPlacement,
602
+ courtyardRoutes
518
603
  });
519
604
  }
520
605
  const circles = footprint.fpCircles || [];
@@ -530,7 +615,8 @@ function processFootprintGraphics(params) {
530
615
  ctx,
531
616
  circle,
532
617
  componentId,
533
- footprintPlacement
618
+ footprintPlacement,
619
+ courtyardRoutes
534
620
  });
535
621
  }
536
622
  const arcs = footprint.fpArcs || [];
@@ -540,7 +626,8 @@ function processFootprintGraphics(params) {
540
626
  ctx,
541
627
  arc,
542
628
  componentId,
543
- footprintPlacement
629
+ footprintPlacement,
630
+ courtyardRoutes
544
631
  });
545
632
  }
546
633
  const polys = footprint.fpPolys || [];
@@ -551,12 +638,31 @@ function processFootprintGraphics(params) {
551
638
  poly,
552
639
  footprintReference: params.footprintReference,
553
640
  componentId,
554
- footprintPlacement
641
+ footprintPlacement,
642
+ courtyardRoutes
555
643
  });
556
644
  }
645
+ for (const layer of ["top", "bottom"]) {
646
+ const { outlines, invalidCount } = assembleCourtyardContours(
647
+ courtyardRoutes.filter((path) => path.layer === layer).map((path) => path.route)
648
+ );
649
+ for (const outline of outlines) {
650
+ ctx.db.pcb_courtyard_outline.insert({
651
+ pcb_component_id: componentId,
652
+ layer,
653
+ outline
654
+ });
655
+ }
656
+ if (invalidCount > 0) {
657
+ ;
658
+ (ctx.warnings ??= []).push(
659
+ `Footprint ${params.footprintReference}: omitted ${invalidCount} invalid ${layer} courtyard contour(s); paths must form closed, simple, non-zero-area rings.`
660
+ );
661
+ }
662
+ }
557
663
  }
558
664
  function createFootprintLine(params) {
559
- const { ctx, line, componentId, footprintPlacement } = params;
665
+ const { ctx, line, componentId, footprintPlacement, courtyardRoutes } = params;
560
666
  if (!ctx.k2cMatPcb) return;
561
667
  const renderLayer = mapKicadLayerToPcbRenderLayer(line.layer);
562
668
  if (!isPcbAnnotationRenderLayer(renderLayer)) return;
@@ -588,11 +694,12 @@ function createFootprintLine(params) {
588
694
  layer,
589
695
  renderLayer,
590
696
  route: [startPos, endPos],
591
- strokeWidth
697
+ strokeWidth,
698
+ courtyardRoutes
592
699
  });
593
700
  }
594
701
  function createFootprintRect(params) {
595
- const { ctx, rect, componentId, footprintPlacement } = params;
702
+ const { ctx, rect, componentId, footprintPlacement, courtyardRoutes } = params;
596
703
  if (!ctx.k2cMatPcb) return;
597
704
  const renderLayer = mapKicadLayerToPcbRenderLayer(rect.layer);
598
705
  if (!isPcbAnnotationRenderLayer(renderLayer)) return;
@@ -671,11 +778,12 @@ function createFootprintRect(params) {
671
778
  layer,
672
779
  renderLayer,
673
780
  route,
674
- strokeWidth
781
+ strokeWidth,
782
+ courtyardRoutes
675
783
  });
676
784
  }
677
785
  function createFootprintCircle(params) {
678
- const { ctx, circle, componentId, footprintPlacement } = params;
786
+ const { ctx, circle, componentId, footprintPlacement, courtyardRoutes } = params;
679
787
  if (!ctx.k2cMatPcb) return;
680
788
  const renderLayer = mapKicadLayerToPcbRenderLayer(circle.layer);
681
789
  if (!isPcbAnnotationRenderLayer(renderLayer)) return;
@@ -727,7 +835,8 @@ function createFootprintCircle(params) {
727
835
  layer,
728
836
  renderLayer,
729
837
  route: circleRoute,
730
- strokeWidth
838
+ strokeWidth,
839
+ courtyardRoutes
731
840
  });
732
841
  }
733
842
  function calculateArcCenter2(params) {
@@ -748,7 +857,7 @@ function calculateArcCenter2(params) {
748
857
  return { center: { x: cx, y: cy }, radius };
749
858
  }
750
859
  function createFootprintArc(params) {
751
- const { ctx, arc, componentId, footprintPlacement } = params;
860
+ const { ctx, arc, componentId, footprintPlacement, courtyardRoutes } = params;
752
861
  if (!ctx.k2cMatPcb) return;
753
862
  const renderLayer = mapKicadLayerToPcbRenderLayer(arc.layer);
754
863
  if (!isPcbAnnotationRenderLayer(renderLayer)) return;
@@ -795,7 +904,8 @@ function createFootprintArc(params) {
795
904
  layer,
796
905
  renderLayer,
797
906
  route: [startPos, endPos],
798
- strokeWidth
907
+ strokeWidth,
908
+ courtyardRoutes
799
909
  });
800
910
  return;
801
911
  }
@@ -843,11 +953,12 @@ function createFootprintArc(params) {
843
953
  layer,
844
954
  renderLayer,
845
955
  route: arcRoute,
846
- strokeWidth
956
+ strokeWidth,
957
+ courtyardRoutes
847
958
  });
848
959
  }
849
960
  function createFootprintPoly(params) {
850
- const { ctx, poly, componentId, footprintPlacement } = params;
961
+ const { ctx, poly, componentId, footprintPlacement, courtyardRoutes } = params;
851
962
  if (!ctx.k2cMatPcb) return;
852
963
  const renderLayer = mapKicadLayerToPcbRenderLayer(poly.layer);
853
964
  const [copperLayer] = getLayerRefsFromLayers(poly.layer, ctx.kicadPcb);
@@ -911,10 +1022,9 @@ function createFootprintPoly(params) {
911
1022
  }
912
1023
  const layer = mapTextLayer(poly.layer);
913
1024
  if (renderLayer.endsWith("_courtyard")) {
914
- ctx.db.pcb_courtyard_outline.insert({
915
- pcb_component_id: componentId,
1025
+ courtyardRoutes.push({
916
1026
  layer,
917
- outline: transformedPts
1027
+ route: [...transformedPts, transformedPts[0]]
918
1028
  });
919
1029
  return;
920
1030
  }
@@ -924,7 +1034,8 @@ function createFootprintPoly(params) {
924
1034
  layer,
925
1035
  renderLayer,
926
1036
  route: transformedPts,
927
- strokeWidth
1037
+ strokeWidth,
1038
+ courtyardRoutes
928
1039
  });
929
1040
  }
930
1041
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "kicad-to-circuit-json",
3
3
  "main": "dist/index.js",
4
4
  "type": "module",
5
- "version": "0.0.146",
5
+ "version": "0.0.147",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/tscircuit/kicad-to-circuit-json"