circuit-json-to-kicad 0.0.6 → 0.0.8

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 +87 -21
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -74,6 +74,37 @@ import {
74
74
  Xy
75
75
  } from "kicadts";
76
76
  import { symbols } from "schematic-symbols";
77
+
78
+ // lib/schematic/schematic-utils.ts
79
+ function getLibraryId(sourceComp, schematicComp) {
80
+ if (sourceComp.type !== "source_component") {
81
+ if (schematicComp.symbol_name) {
82
+ return `Custom:${schematicComp.symbol_name}`;
83
+ }
84
+ return "Device:Component";
85
+ }
86
+ if (sourceComp.ftype === "simple_resistor") {
87
+ return `Device:R_${sourceComp.source_component_id}`;
88
+ }
89
+ if (sourceComp.ftype === "simple_capacitor") {
90
+ return `Device:C_${sourceComp.source_component_id}`;
91
+ }
92
+ if (sourceComp.ftype === "simple_inductor") {
93
+ return `Device:L_${sourceComp.source_component_id}`;
94
+ }
95
+ if (sourceComp.ftype === "simple_diode") {
96
+ return `Device:D_${sourceComp.source_component_id}`;
97
+ }
98
+ if (sourceComp.ftype === "simple_chip") {
99
+ return `Device:U_${sourceComp.source_component_id}`;
100
+ }
101
+ if (schematicComp.symbol_name) {
102
+ return `Custom:${schematicComp.symbol_name}`;
103
+ }
104
+ return `Device:Component_${sourceComp.source_component_id}`;
105
+ }
106
+
107
+ // lib/schematic/stages/AddLibrarySymbolsStage.ts
77
108
  var AddLibrarySymbolsStage = class extends ConverterStage {
78
109
  _step() {
79
110
  const { kicadSch, db } = this.ctx;
@@ -122,7 +153,8 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
122
153
  const libSymbol = this.createLibrarySymbolFromSchematicSymbol(
123
154
  symbolName,
124
155
  symbolData,
125
- sourceComp
156
+ sourceComp,
157
+ exampleComp
126
158
  );
127
159
  librarySymbols.push(libSymbol);
128
160
  }
@@ -171,8 +203,8 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
171
203
  /**
172
204
  * Convert schematic-symbols data to KiCad library symbol
173
205
  */
174
- createLibrarySymbolFromSchematicSymbol(symbolName, symbolData, sourceComp) {
175
- const libId = this.getLibraryId(symbolName, sourceComp);
206
+ createLibrarySymbolFromSchematicSymbol(symbolName, symbolData, sourceComp, schematicComp) {
207
+ const libId = getLibraryId(sourceComp, schematicComp);
176
208
  const symbol = new SchematicSymbol({
177
209
  libraryId: libId,
178
210
  excludeFromSim: false,
@@ -194,21 +226,6 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
194
226
  symbol._sxEmbeddedFonts = new EmbeddedFonts(false);
195
227
  return symbol;
196
228
  }
197
- /**
198
- * Get KiCad library ID for a symbol
199
- */
200
- getLibraryId(symbolName, sourceComp) {
201
- if (sourceComp?.ftype === "simple_resistor") {
202
- return "Device:R";
203
- }
204
- if (sourceComp?.ftype === "simple_capacitor") {
205
- return "Device:C";
206
- }
207
- if (sourceComp?.ftype === "simple_chip") {
208
- return "Device:U";
209
- }
210
- return `Custom:${symbolName}`;
211
- }
212
229
  /**
213
230
  * Add properties to the library symbol
214
231
  */
@@ -485,7 +502,7 @@ var AddSchematicSymbolsStage = class extends ConverterStage {
485
502
  uuid,
486
503
  fieldsAutoplaced: true
487
504
  });
488
- const libId = this.getLibraryId(sourceComponent);
505
+ const libId = getLibraryId(sourceComponent, schematicComponent);
489
506
  const symLibId = new SymbolLibId(libId);
490
507
  symbol._sxLibId = symLibId;
491
508
  const { reference, value, description } = this.getComponentMetadata(sourceComponent);
@@ -809,6 +826,52 @@ var AddSheetInstancesStage = class extends ConverterStage {
809
826
  }
810
827
  };
811
828
 
829
+ // lib/schematic/getSchematicBoundsAndCenter.ts
830
+ function getSchematicBoundsAndCenter(db) {
831
+ const schematicComponents = db.schematic_component.list();
832
+ const schematicTraces = db.schematic_trace.list();
833
+ let minX = Infinity;
834
+ let minY = Infinity;
835
+ let maxX = -Infinity;
836
+ let maxY = -Infinity;
837
+ for (const component of schematicComponents) {
838
+ const width = component.size?.width ?? 0;
839
+ const height = component.size?.height ?? 0;
840
+ minX = Math.min(minX, component.center.x - width / 2);
841
+ minY = Math.min(minY, component.center.y - height / 2);
842
+ maxX = Math.max(maxX, component.center.x + width / 2);
843
+ maxY = Math.max(maxY, component.center.y + height / 2);
844
+ }
845
+ for (const trace of schematicTraces) {
846
+ for (const edge of trace.edges) {
847
+ minX = Math.min(minX, edge.from.x, edge.to.x);
848
+ minY = Math.min(minY, edge.from.y, edge.to.y);
849
+ maxX = Math.max(maxX, edge.from.x, edge.to.x);
850
+ maxY = Math.max(maxY, edge.from.y, edge.to.y);
851
+ }
852
+ }
853
+ if (minX === Infinity) {
854
+ minX = 0;
855
+ minY = 0;
856
+ maxX = 0;
857
+ maxY = 0;
858
+ }
859
+ const centerX = (minX + maxX) / 2;
860
+ const centerY = (minY + maxY) / 2;
861
+ return {
862
+ center: {
863
+ x: centerX,
864
+ y: centerY
865
+ },
866
+ bounds: {
867
+ minX,
868
+ minY,
869
+ maxX,
870
+ maxY
871
+ }
872
+ };
873
+ }
874
+
812
875
  // lib/schematic/CircuitJsonToKicadSchConverter.ts
813
876
  var CircuitJsonToKicadSchConverter = class {
814
877
  ctx;
@@ -822,8 +885,10 @@ var CircuitJsonToKicadSchConverter = class {
822
885
  const CIRCUIT_JSON_SCALE_FACTOR = 15;
823
886
  const KICAD_CENTER_X = 95.25;
824
887
  const KICAD_CENTER_Y = 73.66;
888
+ const db = cju(circuitJson);
889
+ const { center } = getSchematicBoundsAndCenter(db);
825
890
  this.ctx = {
826
- db: cju(circuitJson),
891
+ db,
827
892
  circuitJson,
828
893
  kicadSch: new KicadSch({
829
894
  generator: "circuit-json-to-kicad",
@@ -831,7 +896,8 @@ var CircuitJsonToKicadSchConverter = class {
831
896
  }),
832
897
  c2kMatSch: compose(
833
898
  translate(KICAD_CENTER_X, KICAD_CENTER_Y),
834
- scale(CIRCUIT_JSON_SCALE_FACTOR, -CIRCUIT_JSON_SCALE_FACTOR)
899
+ scale(CIRCUIT_JSON_SCALE_FACTOR, -CIRCUIT_JSON_SCALE_FACTOR),
900
+ translate(-center.x, -center.y)
835
901
  )
836
902
  };
837
903
  this.pipeline = [
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "circuit-json-to-kicad",
3
3
  "main": "dist/index.js",
4
- "version": "0.0.6",
4
+ "version": "0.0.8",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"