circuit-json-to-kicad 0.0.9 → 0.0.11

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 +118 -96
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -75,7 +75,7 @@ import {
75
75
  } from "kicadts";
76
76
  import { symbols } from "schematic-symbols";
77
77
 
78
- // lib/schematic/schematic-utils.ts
78
+ // lib/schematic/getLibraryId.ts
79
79
  function getLibraryId(sourceComp, schematicComp) {
80
80
  if (sourceComp.type !== "source_component") {
81
81
  if (schematicComp.symbol_name) {
@@ -105,6 +105,7 @@ function getLibraryId(sourceComp, schematicComp) {
105
105
  }
106
106
 
107
107
  // lib/schematic/stages/AddLibrarySymbolsStage.ts
108
+ import { applyToPoint, scale as createScaleMatrix } from "transformation-matrix";
108
109
  var AddLibrarySymbolsStage = class extends ConverterStage {
109
110
  _step() {
110
111
  const { kicadSch, db } = this.ctx;
@@ -114,48 +115,28 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
114
115
  return;
115
116
  }
116
117
  const libSymbols = new LibSymbols();
117
- const symbolsToCreate = /* @__PURE__ */ new Set();
118
- for (const comp of schematicComponents) {
119
- if (comp.symbol_name) {
120
- symbolsToCreate.add(comp.symbol_name);
121
- } else {
122
- const sourceComp = comp.source_component_id ? db.source_component.get(comp.source_component_id) : null;
123
- if (sourceComp?.ftype === "simple_chip") {
124
- symbolsToCreate.add(`generic_chip_${comp.source_component_id}`);
125
- }
126
- }
127
- }
128
118
  const librarySymbols = [];
129
- for (const symbolName of symbolsToCreate) {
130
- let symbolData = symbols[symbolName];
131
- let exampleComp;
132
- let sourceComp;
119
+ for (const schematicComponent of schematicComponents) {
120
+ const sourceComp = schematicComponent.source_component_id ? db.source_component.get(schematicComponent.source_component_id) : null;
121
+ if (!sourceComp) continue;
122
+ const symbolName = schematicComponent.symbol_name || (sourceComp.ftype === "simple_chip" ? `generic_chip_${schematicComponent.source_component_id}` : null);
123
+ if (!symbolName) {
124
+ continue;
125
+ }
126
+ let symbolData;
133
127
  if (symbolName.startsWith("generic_chip_")) {
134
- const sourceCompId = symbolName.replace("generic_chip_", "");
135
- sourceComp = db.source_component.get(sourceCompId);
136
- exampleComp = schematicComponents.find(
137
- (c) => c.source_component_id === sourceCompId
138
- );
139
- if (exampleComp) {
140
- symbolData = this.createGenericChipSymbolData(exampleComp, db);
141
- }
128
+ symbolData = this.createGenericChipSymbolData(schematicComponent, db);
142
129
  } else {
143
130
  symbolData = symbols[symbolName];
144
131
  if (!symbolData) {
145
- console.warn(`Symbol ${symbolName} not found in schematic-symbols`);
146
132
  continue;
147
133
  }
148
- exampleComp = schematicComponents.find(
149
- (c) => c.symbol_name === symbolName
150
- );
151
- sourceComp = exampleComp && exampleComp.source_component_id ? db.source_component.get(exampleComp.source_component_id) : null;
152
134
  }
153
- const libSymbol = this.createLibrarySymbolFromSchematicSymbol(
154
- symbolName,
135
+ const libSymbol = this.createLibrarySymbolFromSchematicSymbol({
155
136
  symbolData,
156
137
  sourceComp,
157
- exampleComp
158
- );
138
+ schematicComponent
139
+ });
159
140
  librarySymbols.push(libSymbol);
160
141
  }
161
142
  libSymbols.symbols = librarySymbols;
@@ -203,8 +184,12 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
203
184
  /**
204
185
  * Convert schematic-symbols data to KiCad library symbol
205
186
  */
206
- createLibrarySymbolFromSchematicSymbol(symbolName, symbolData, sourceComp, schematicComp) {
207
- const libId = getLibraryId(sourceComp, schematicComp);
187
+ createLibrarySymbolFromSchematicSymbol({
188
+ symbolData,
189
+ sourceComp,
190
+ schematicComponent
191
+ }) {
192
+ const libId = getLibraryId(sourceComp, schematicComponent);
208
193
  const symbol = new SchematicSymbol({
209
194
  libraryId: libId,
210
195
  excludeFromSim: false,
@@ -219,9 +204,18 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
219
204
  symbol._sxPinNames = pinNames;
220
205
  this.addSymbolProperties(symbol, libId, sourceComp);
221
206
  const isChip = sourceComp?.ftype === "simple_chip";
222
- const drawingSymbol = this.createDrawingSubsymbol(libId, symbolData, isChip);
207
+ const drawingSymbol = this.createDrawingSubsymbol({
208
+ libId,
209
+ symbolData,
210
+ isChip
211
+ });
223
212
  symbol.subSymbols.push(drawingSymbol);
224
- const pinSymbol = this.createPinSubsymbol(libId, symbolData, isChip);
213
+ const pinSymbol = this.createPinSubsymbol({
214
+ libId,
215
+ symbolData,
216
+ isChip,
217
+ schematicComponent
218
+ });
225
219
  symbol.subSymbols.push(pinSymbol);
226
220
  symbol._sxEmbeddedFonts = new EmbeddedFonts(false);
227
221
  return symbol;
@@ -310,7 +304,11 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
310
304
  * Create the drawing subsymbol (primitives, no pins)
311
305
  * Converts schematic-symbols primitives to KiCad drawing elements
312
306
  */
313
- createDrawingSubsymbol(libId, symbolData, isChip = false) {
307
+ createDrawingSubsymbol({
308
+ libId,
309
+ symbolData,
310
+ isChip
311
+ }) {
314
312
  const drawingSymbol = new SchematicSymbol({
315
313
  libraryId: `${libId.split(":")[1]}_0_1`
316
314
  });
@@ -318,11 +316,12 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
318
316
  for (const primitive of symbolData.primitives || []) {
319
317
  if (primitive.type === "path" && primitive.points) {
320
318
  const fillType = isChip ? "background" : "none";
321
- const polyline = this.createPolylineFromPoints(
322
- primitive.points,
323
- symbolScale,
319
+ const polyline = this.createPolylineFromPoints({
320
+ points: primitive.points,
321
+ scale: symbolScale,
322
+ center: symbolData.center,
324
323
  fillType
325
- );
324
+ });
326
325
  drawingSymbol.polylines.push(polyline);
327
326
  }
328
327
  }
@@ -331,9 +330,20 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
331
330
  /**
332
331
  * Create a KiCad polyline from points
333
332
  */
334
- createPolylineFromPoints(points, scale3, fillType = "none") {
333
+ createPolylineFromPoints({
334
+ points,
335
+ scale: scale3,
336
+ center,
337
+ fillType
338
+ }) {
335
339
  const polyline = new SymbolPolyline();
336
- const xyPoints = points.map((p) => new Xy(p.x * scale3, p.y * scale3));
340
+ const cx = center?.x ?? 0;
341
+ const cy = center?.y ?? 0;
342
+ const scaleMatrix = createScaleMatrix(scale3, scale3);
343
+ const xyPoints = points.map((p) => {
344
+ const translated = applyToPoint(scaleMatrix, { x: p.x - cx, y: p.y - cy });
345
+ return new Xy(translated.x, translated.y);
346
+ });
337
347
  const pts = new Pts(xyPoints);
338
348
  polyline.points = pts;
339
349
  const stroke = new Stroke();
@@ -348,7 +358,12 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
348
358
  /**
349
359
  * Create the pin subsymbol
350
360
  */
351
- createPinSubsymbol(libId, symbolData, isChip = false) {
361
+ createPinSubsymbol({
362
+ libId,
363
+ symbolData,
364
+ isChip,
365
+ schematicComponent
366
+ }) {
352
367
  const pinSymbol = new SchematicSymbol({
353
368
  libraryId: `${libId.split(":")[1]}_1_1`
354
369
  });
@@ -361,7 +376,9 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
361
376
  port,
362
377
  symbolData.center,
363
378
  symbolData.size,
364
- isChip
379
+ isChip,
380
+ i,
381
+ schematicComponent
365
382
  );
366
383
  pin.at = [x, y, angle];
367
384
  pin.length = isChip ? 6 : 1.27;
@@ -386,12 +403,37 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
386
403
  * Calculate KiCad pin position and rotation from schematic-symbols port
387
404
  * Scale pins to match the c2kMatSch transformation scale
388
405
  */
389
- calculatePinPosition(port, center, size, isChip) {
406
+ calculatePinPosition(port, center, size, isChip, portIndex, schematicComponent) {
390
407
  const symbolScale = this.ctx.c2kMatSch?.a || 15;
391
- const dx = port.x - center.x;
392
- const dy = port.y - center.y;
393
- let x = port.x * symbolScale;
394
- let y = port.y * symbolScale;
408
+ let portX = port.x ?? 0;
409
+ let portY = port.y ?? 0;
410
+ let usingCircuitJsonPort = false;
411
+ if (portIndex !== void 0 && schematicComponent) {
412
+ const schematicPorts = this.ctx.db.schematic_port.list().filter(
413
+ (p) => p.schematic_component_id === schematicComponent.schematic_component_id
414
+ ).sort((a, b) => (a.pin_number || 0) - (b.pin_number || 0));
415
+ if (schematicPorts[portIndex]) {
416
+ const schPort = schematicPorts[portIndex];
417
+ portX = schPort.center.x - schematicComponent.center.x;
418
+ portY = schPort.center.y - schematicComponent.center.y;
419
+ usingCircuitJsonPort = true;
420
+ }
421
+ }
422
+ let dx;
423
+ let dy;
424
+ if (usingCircuitJsonPort) {
425
+ dx = portX;
426
+ dy = portY;
427
+ } else {
428
+ const cx = center?.x ?? 0;
429
+ const cy = center?.y ?? 0;
430
+ dx = portX - cx;
431
+ dy = portY - cy;
432
+ }
433
+ const scaleMatrix = createScaleMatrix(symbolScale, symbolScale);
434
+ const scaled = applyToPoint(scaleMatrix, { x: dx, y: dy });
435
+ let x = scaled.x;
436
+ let y = scaled.y;
395
437
  const chipPinLength = 6;
396
438
  if (isChip && size) {
397
439
  const halfWidth = size.width / 2 * symbolScale;
@@ -472,7 +514,7 @@ import {
472
514
  TextEffectsFont as TextEffectsFont2,
473
515
  TextEffectsJustify
474
516
  } from "kicadts";
475
- import { applyToPoint } from "transformation-matrix";
517
+ import { applyToPoint as applyToPoint2 } from "transformation-matrix";
476
518
  import { symbols as symbols2 } from "schematic-symbols";
477
519
  var AddSchematicSymbolsStage = class extends ConverterStage {
478
520
  _step() {
@@ -487,7 +529,7 @@ var AddSchematicSymbolsStage = class extends ConverterStage {
487
529
  const sourceComponent = schematicComponent.source_component_id ? db.source_component.get(schematicComponent.source_component_id) : null;
488
530
  if (!sourceComponent) continue;
489
531
  if (!this.ctx.c2kMatSch) continue;
490
- const { x, y } = applyToPoint(this.ctx.c2kMatSch, {
532
+ const { x, y } = applyToPoint2(this.ctx.c2kMatSch, {
491
533
  x: schematicComponent.center.x,
492
534
  y: schematicComponent.center.y
493
535
  });
@@ -586,7 +628,7 @@ var AddSchematicSymbolsStage = class extends ConverterStage {
586
628
  ) || [];
587
629
  const refText = schematicTexts.find((t) => t.text && t.text.length > 0);
588
630
  if (refText && this.ctx.c2kMatSch) {
589
- const refTextPos2 = applyToPoint(this.ctx.c2kMatSch, {
631
+ const refTextPos2 = applyToPoint2(this.ctx.c2kMatSch, {
590
632
  x: refText.position.x,
591
633
  y: refText.position.y
592
634
  });
@@ -612,37 +654,17 @@ var AddSchematicSymbolsStage = class extends ConverterStage {
612
654
  }
613
655
  }
614
656
  }
615
- const refTextPos = refTextPrimitive && this.ctx.c2kMatSch ? applyToPoint(this.ctx.c2kMatSch, {
616
- x: schematicComponent.center.x + refTextPrimitive.x,
617
- y: schematicComponent.center.y + refTextPrimitive.y
657
+ const symbolCenter = symbol.center || { x: 0, y: 0 };
658
+ const refTextPos = refTextPrimitive && this.ctx.c2kMatSch ? applyToPoint2(this.ctx.c2kMatSch, {
659
+ x: schematicComponent.center.x + (refTextPrimitive.x - symbolCenter.x),
660
+ y: schematicComponent.center.y + (refTextPrimitive.y - symbolCenter.y)
618
661
  }) : { x: symbolKicadPos.x, y: symbolKicadPos.y - 6 };
619
- const valTextPos = valTextPrimitive && this.ctx.c2kMatSch ? applyToPoint(this.ctx.c2kMatSch, {
620
- x: schematicComponent.center.x + valTextPrimitive.x,
621
- y: schematicComponent.center.y + valTextPrimitive.y
662
+ const valTextPos = valTextPrimitive && this.ctx.c2kMatSch ? applyToPoint2(this.ctx.c2kMatSch, {
663
+ x: schematicComponent.center.x + (valTextPrimitive.x - symbolCenter.x),
664
+ y: schematicComponent.center.y + (valTextPrimitive.y - symbolCenter.y)
622
665
  }) : { x: symbolKicadPos.x, y: symbolKicadPos.y + 6 };
623
666
  return { refTextPos, valTextPos };
624
667
  }
625
- /**
626
- * Get KiCad library ID for a component
627
- */
628
- getLibraryId(sourceComp) {
629
- if (sourceComp.ftype === "simple_resistor") {
630
- return "Device:R";
631
- }
632
- if (sourceComp.ftype === "simple_capacitor") {
633
- return "Device:C";
634
- }
635
- if (sourceComp.ftype === "simple_inductor") {
636
- return "Device:L";
637
- }
638
- if (sourceComp.ftype === "simple_diode") {
639
- return "Device:D";
640
- }
641
- if (sourceComp.ftype === "simple_chip") {
642
- return "Device:U";
643
- }
644
- return "Device:Component";
645
- }
646
668
  /**
647
669
  * Get component metadata (reference, value, description)
648
670
  */
@@ -713,7 +735,7 @@ var AddSchematicSymbolsStage = class extends ConverterStage {
713
735
 
714
736
  // lib/schematic/stages/AddSchematicTracesStage.ts
715
737
  import { Wire, Pts as Pts2, Xy as Xy2, Stroke as Stroke2, Junction } from "kicadts";
716
- import { applyToPoint as applyToPoint2 } from "transformation-matrix";
738
+ import { applyToPoint as applyToPoint3 } from "transformation-matrix";
717
739
  var AddSchematicTracesStage = class extends ConverterStage {
718
740
  _step() {
719
741
  const { kicadSch, db } = this.ctx;
@@ -751,11 +773,11 @@ var AddSchematicTracesStage = class extends ConverterStage {
751
773
  "Schematic transformation matrix not initialized in context"
752
774
  );
753
775
  }
754
- const from = applyToPoint2(this.ctx.c2kMatSch, {
776
+ const from = applyToPoint3(this.ctx.c2kMatSch, {
755
777
  x: edge.from.x,
756
778
  y: edge.from.y
757
779
  });
758
- const to = applyToPoint2(this.ctx.c2kMatSch, {
780
+ const to = applyToPoint3(this.ctx.c2kMatSch, {
759
781
  x: edge.to.x,
760
782
  y: edge.to.y
761
783
  });
@@ -781,7 +803,7 @@ var AddSchematicTracesStage = class extends ConverterStage {
781
803
  "Schematic transformation matrix not initialized in context"
782
804
  );
783
805
  }
784
- const { x, y } = applyToPoint2(this.ctx.c2kMatSch, {
806
+ const { x, y } = applyToPoint3(this.ctx.c2kMatSch, {
785
807
  x: junction.x,
786
808
  y: junction.y
787
809
  });
@@ -1037,7 +1059,7 @@ var AddNetsStage = class extends ConverterStage {
1037
1059
 
1038
1060
  // lib/pcb/stages/AddFootprintsStage.ts
1039
1061
  import { Footprint, FpText, FootprintPad } from "kicadts";
1040
- import { applyToPoint as applyToPoint3 } from "transformation-matrix";
1062
+ import { applyToPoint as applyToPoint4 } from "transformation-matrix";
1041
1063
  var AddFootprintsStage = class extends ConverterStage {
1042
1064
  componentsProcessed = 0;
1043
1065
  pcbComponents = [];
@@ -1061,7 +1083,7 @@ var AddFootprintsStage = class extends ConverterStage {
1061
1083
  const sourceComponent = component.source_component_id ? this.ctx.db.source_component.get(component.source_component_id) : null;
1062
1084
  const footprintName = sourceComponent?.ftype || "Unknown";
1063
1085
  const componentName = sourceComponent?.name || `Component_${this.componentsProcessed}`;
1064
- const transformedPos = applyToPoint3(c2kMatPcb, {
1086
+ const transformedPos = applyToPoint4(c2kMatPcb, {
1065
1087
  x: component.center.x,
1066
1088
  y: component.center.y
1067
1089
  });
@@ -1142,7 +1164,7 @@ var AddFootprintsStage = class extends ConverterStage {
1142
1164
 
1143
1165
  // lib/pcb/stages/AddTracesStage.ts
1144
1166
  import { Segment, SegmentNet } from "kicadts";
1145
- import { applyToPoint as applyToPoint4 } from "transformation-matrix";
1167
+ import { applyToPoint as applyToPoint5 } from "transformation-matrix";
1146
1168
  var AddTracesStage = class extends ConverterStage {
1147
1169
  tracesProcessed = 0;
1148
1170
  pcbTraces = [];
@@ -1170,11 +1192,11 @@ var AddTracesStage = class extends ConverterStage {
1170
1192
  for (let i = 0; i < trace.route.length - 1; i++) {
1171
1193
  const startPoint = trace.route[i];
1172
1194
  const endPoint = trace.route[i + 1];
1173
- const transformedStart = applyToPoint4(c2kMatPcb, {
1195
+ const transformedStart = applyToPoint5(c2kMatPcb, {
1174
1196
  x: startPoint.x,
1175
1197
  y: startPoint.y
1176
1198
  });
1177
- const transformedEnd = applyToPoint4(c2kMatPcb, {
1199
+ const transformedEnd = applyToPoint5(c2kMatPcb, {
1178
1200
  x: endPoint.x,
1179
1201
  y: endPoint.y
1180
1202
  });
@@ -1208,7 +1230,7 @@ var AddTracesStage = class extends ConverterStage {
1208
1230
 
1209
1231
  // lib/pcb/stages/AddViasStage.ts
1210
1232
  import { Via, ViaNet } from "kicadts";
1211
- import { applyToPoint as applyToPoint5 } from "transformation-matrix";
1233
+ import { applyToPoint as applyToPoint6 } from "transformation-matrix";
1212
1234
  var AddViasStage = class extends ConverterStage {
1213
1235
  viasProcessed = 0;
1214
1236
  pcbVias = [];
@@ -1229,7 +1251,7 @@ var AddViasStage = class extends ConverterStage {
1229
1251
  return;
1230
1252
  }
1231
1253
  const via = this.pcbVias[this.viasProcessed];
1232
- const transformedPos = applyToPoint5(c2kMatPcb, {
1254
+ const transformedPos = applyToPoint6(c2kMatPcb, {
1233
1255
  x: via.x,
1234
1256
  y: via.y
1235
1257
  });
@@ -1256,7 +1278,7 @@ var AddViasStage = class extends ConverterStage {
1256
1278
 
1257
1279
  // lib/pcb/stages/AddGraphicsStage.ts
1258
1280
  import { GrLine } from "kicadts";
1259
- import { applyToPoint as applyToPoint6 } from "transformation-matrix";
1281
+ import { applyToPoint as applyToPoint7 } from "transformation-matrix";
1260
1282
  var AddGraphicsStage = class extends ConverterStage {
1261
1283
  _step() {
1262
1284
  const { kicadPcb, c2kMatPcb } = this.ctx;
@@ -1273,11 +1295,11 @@ var AddGraphicsStage = class extends ConverterStage {
1273
1295
  const startPoint = path.route[i];
1274
1296
  const endPoint = path.route[i + 1];
1275
1297
  if (!startPoint || !endPoint) continue;
1276
- const transformedStart = applyToPoint6(c2kMatPcb, {
1298
+ const transformedStart = applyToPoint7(c2kMatPcb, {
1277
1299
  x: startPoint.x,
1278
1300
  y: startPoint.y
1279
1301
  });
1280
- const transformedEnd = applyToPoint6(c2kMatPcb, {
1302
+ const transformedEnd = applyToPoint7(c2kMatPcb, {
1281
1303
  x: endPoint.x,
1282
1304
  y: endPoint.y
1283
1305
  });
@@ -1318,7 +1340,7 @@ var AddGraphicsStage = class extends ConverterStage {
1318
1340
  ];
1319
1341
  }
1320
1342
  const transformedCorners = corners.map(
1321
- (corner) => applyToPoint6(c2kMatPcb, corner)
1343
+ (corner) => applyToPoint7(c2kMatPcb, corner)
1322
1344
  );
1323
1345
  for (let i = 0; i < transformedCorners.length; i++) {
1324
1346
  const start = transformedCorners[i];
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.9",
4
+ "version": "0.0.11",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"
@@ -24,7 +24,7 @@
24
24
  "schematic-symbols": "^0.0.202",
25
25
  "sharp": "^0.34.4",
26
26
  "transformation-matrix": "^3.1.0",
27
- "tscircuit": "^0.0.698",
27
+ "tscircuit": "^0.0.713",
28
28
  "tsup": "^8.5.0"
29
29
  },
30
30
  "peerDependencies": {