circuit-json-to-kicad 0.0.10 → 0.0.12

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 +119 -68
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -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;
@@ -131,12 +132,11 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
131
132
  continue;
132
133
  }
133
134
  }
134
- const libSymbol = this.createLibrarySymbolFromSchematicSymbol(
135
- symbolName,
135
+ const libSymbol = this.createLibrarySymbolFromSchematicSymbol({
136
136
  symbolData,
137
137
  sourceComp,
138
138
  schematicComponent
139
- );
139
+ });
140
140
  librarySymbols.push(libSymbol);
141
141
  }
142
142
  libSymbols.symbols = librarySymbols;
@@ -184,8 +184,12 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
184
184
  /**
185
185
  * Convert schematic-symbols data to KiCad library symbol
186
186
  */
187
- createLibrarySymbolFromSchematicSymbol(symbolName, symbolData, sourceComp, schematicComp) {
188
- const libId = getLibraryId(sourceComp, schematicComp);
187
+ createLibrarySymbolFromSchematicSymbol({
188
+ symbolData,
189
+ sourceComp,
190
+ schematicComponent
191
+ }) {
192
+ const libId = getLibraryId(sourceComp, schematicComponent);
189
193
  const symbol = new SchematicSymbol({
190
194
  libraryId: libId,
191
195
  excludeFromSim: false,
@@ -200,9 +204,18 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
200
204
  symbol._sxPinNames = pinNames;
201
205
  this.addSymbolProperties(symbol, libId, sourceComp);
202
206
  const isChip = sourceComp?.ftype === "simple_chip";
203
- const drawingSymbol = this.createDrawingSubsymbol(libId, symbolData, isChip);
207
+ const drawingSymbol = this.createDrawingSubsymbol({
208
+ libId,
209
+ symbolData,
210
+ isChip
211
+ });
204
212
  symbol.subSymbols.push(drawingSymbol);
205
- const pinSymbol = this.createPinSubsymbol(libId, symbolData, isChip);
213
+ const pinSymbol = this.createPinSubsymbol({
214
+ libId,
215
+ symbolData,
216
+ isChip,
217
+ schematicComponent
218
+ });
206
219
  symbol.subSymbols.push(pinSymbol);
207
220
  symbol._sxEmbeddedFonts = new EmbeddedFonts(false);
208
221
  return symbol;
@@ -291,7 +304,11 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
291
304
  * Create the drawing subsymbol (primitives, no pins)
292
305
  * Converts schematic-symbols primitives to KiCad drawing elements
293
306
  */
294
- createDrawingSubsymbol(libId, symbolData, isChip = false) {
307
+ createDrawingSubsymbol({
308
+ libId,
309
+ symbolData,
310
+ isChip
311
+ }) {
295
312
  const drawingSymbol = new SchematicSymbol({
296
313
  libraryId: `${libId.split(":")[1]}_0_1`
297
314
  });
@@ -299,11 +316,12 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
299
316
  for (const primitive of symbolData.primitives || []) {
300
317
  if (primitive.type === "path" && primitive.points) {
301
318
  const fillType = isChip ? "background" : "none";
302
- const polyline = this.createPolylineFromPoints(
303
- primitive.points,
304
- symbolScale,
319
+ const polyline = this.createPolylineFromPoints({
320
+ points: primitive.points,
321
+ scale: symbolScale,
322
+ center: symbolData.center,
305
323
  fillType
306
- );
324
+ });
307
325
  drawingSymbol.polylines.push(polyline);
308
326
  }
309
327
  }
@@ -312,9 +330,20 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
312
330
  /**
313
331
  * Create a KiCad polyline from points
314
332
  */
315
- createPolylineFromPoints(points, scale3, fillType = "none") {
333
+ createPolylineFromPoints({
334
+ points,
335
+ scale: scale3,
336
+ center,
337
+ fillType
338
+ }) {
316
339
  const polyline = new SymbolPolyline();
317
- 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
+ });
318
347
  const pts = new Pts(xyPoints);
319
348
  polyline.points = pts;
320
349
  const stroke = new Stroke();
@@ -329,7 +358,12 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
329
358
  /**
330
359
  * Create the pin subsymbol
331
360
  */
332
- createPinSubsymbol(libId, symbolData, isChip = false) {
361
+ createPinSubsymbol({
362
+ libId,
363
+ symbolData,
364
+ isChip,
365
+ schematicComponent
366
+ }) {
333
367
  const pinSymbol = new SchematicSymbol({
334
368
  libraryId: `${libId.split(":")[1]}_1_1`
335
369
  });
@@ -342,7 +376,9 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
342
376
  port,
343
377
  symbolData.center,
344
378
  symbolData.size,
345
- isChip
379
+ isChip,
380
+ i,
381
+ schematicComponent
346
382
  );
347
383
  pin.at = [x, y, angle];
348
384
  pin.length = isChip ? 6 : 1.27;
@@ -367,17 +403,52 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
367
403
  * Calculate KiCad pin position and rotation from schematic-symbols port
368
404
  * Scale pins to match the c2kMatSch transformation scale
369
405
  */
370
- calculatePinPosition(port, center, size, isChip) {
406
+ calculatePinPosition(port, center, size, isChip, portIndex, schematicComponent) {
371
407
  const symbolScale = this.ctx.c2kMatSch?.a || 15;
372
- const dx = port.x - center.x;
373
- const dy = port.y - center.y;
374
- let x = port.x * symbolScale;
375
- 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 isHorizontalPin;
436
+ if (isChip && size) {
437
+ const halfWidth = size.width / 2;
438
+ const halfHeight = size.height / 2;
439
+ const normalizedDx = Math.abs(dx) / halfWidth;
440
+ const normalizedDy = Math.abs(dy) / halfHeight;
441
+ isHorizontalPin = normalizedDx > normalizedDy;
442
+ } else {
443
+ isHorizontalPin = Math.abs(dx) > Math.abs(dy);
444
+ }
445
+ let x = scaled.x;
446
+ let y = scaled.y;
376
447
  const chipPinLength = 6;
377
448
  if (isChip && size) {
378
449
  const halfWidth = size.width / 2 * symbolScale;
379
450
  const halfHeight = size.height / 2 * symbolScale;
380
- if (Math.abs(dx) > Math.abs(dy)) {
451
+ if (isHorizontalPin) {
381
452
  x = dx > 0 ? halfWidth : -halfWidth;
382
453
  y = dy * symbolScale;
383
454
  } else {
@@ -386,7 +457,7 @@ var AddLibrarySymbolsStage = class extends ConverterStage {
386
457
  }
387
458
  }
388
459
  let angle = 0;
389
- if (Math.abs(dx) > Math.abs(dy)) {
460
+ if (isHorizontalPin) {
390
461
  if (dx > 0) {
391
462
  if (isChip) {
392
463
  angle = 180;
@@ -453,7 +524,7 @@ import {
453
524
  TextEffectsFont as TextEffectsFont2,
454
525
  TextEffectsJustify
455
526
  } from "kicadts";
456
- import { applyToPoint } from "transformation-matrix";
527
+ import { applyToPoint as applyToPoint2 } from "transformation-matrix";
457
528
  import { symbols as symbols2 } from "schematic-symbols";
458
529
  var AddSchematicSymbolsStage = class extends ConverterStage {
459
530
  _step() {
@@ -468,7 +539,7 @@ var AddSchematicSymbolsStage = class extends ConverterStage {
468
539
  const sourceComponent = schematicComponent.source_component_id ? db.source_component.get(schematicComponent.source_component_id) : null;
469
540
  if (!sourceComponent) continue;
470
541
  if (!this.ctx.c2kMatSch) continue;
471
- const { x, y } = applyToPoint(this.ctx.c2kMatSch, {
542
+ const { x, y } = applyToPoint2(this.ctx.c2kMatSch, {
472
543
  x: schematicComponent.center.x,
473
544
  y: schematicComponent.center.y
474
545
  });
@@ -567,7 +638,7 @@ var AddSchematicSymbolsStage = class extends ConverterStage {
567
638
  ) || [];
568
639
  const refText = schematicTexts.find((t) => t.text && t.text.length > 0);
569
640
  if (refText && this.ctx.c2kMatSch) {
570
- const refTextPos2 = applyToPoint(this.ctx.c2kMatSch, {
641
+ const refTextPos2 = applyToPoint2(this.ctx.c2kMatSch, {
571
642
  x: refText.position.x,
572
643
  y: refText.position.y
573
644
  });
@@ -593,37 +664,17 @@ var AddSchematicSymbolsStage = class extends ConverterStage {
593
664
  }
594
665
  }
595
666
  }
596
- const refTextPos = refTextPrimitive && this.ctx.c2kMatSch ? applyToPoint(this.ctx.c2kMatSch, {
597
- x: schematicComponent.center.x + refTextPrimitive.x,
598
- y: schematicComponent.center.y + refTextPrimitive.y
667
+ const symbolCenter = symbol.center || { x: 0, y: 0 };
668
+ const refTextPos = refTextPrimitive && this.ctx.c2kMatSch ? applyToPoint2(this.ctx.c2kMatSch, {
669
+ x: schematicComponent.center.x + (refTextPrimitive.x - symbolCenter.x),
670
+ y: schematicComponent.center.y + (refTextPrimitive.y - symbolCenter.y)
599
671
  }) : { x: symbolKicadPos.x, y: symbolKicadPos.y - 6 };
600
- const valTextPos = valTextPrimitive && this.ctx.c2kMatSch ? applyToPoint(this.ctx.c2kMatSch, {
601
- x: schematicComponent.center.x + valTextPrimitive.x,
602
- y: schematicComponent.center.y + valTextPrimitive.y
672
+ const valTextPos = valTextPrimitive && this.ctx.c2kMatSch ? applyToPoint2(this.ctx.c2kMatSch, {
673
+ x: schematicComponent.center.x + (valTextPrimitive.x - symbolCenter.x),
674
+ y: schematicComponent.center.y + (valTextPrimitive.y - symbolCenter.y)
603
675
  }) : { x: symbolKicadPos.x, y: symbolKicadPos.y + 6 };
604
676
  return { refTextPos, valTextPos };
605
677
  }
606
- /**
607
- * Get KiCad library ID for a component
608
- */
609
- getLibraryId(sourceComp) {
610
- if (sourceComp.ftype === "simple_resistor") {
611
- return "Device:R";
612
- }
613
- if (sourceComp.ftype === "simple_capacitor") {
614
- return "Device:C";
615
- }
616
- if (sourceComp.ftype === "simple_inductor") {
617
- return "Device:L";
618
- }
619
- if (sourceComp.ftype === "simple_diode") {
620
- return "Device:D";
621
- }
622
- if (sourceComp.ftype === "simple_chip") {
623
- return "Device:U";
624
- }
625
- return "Device:Component";
626
- }
627
678
  /**
628
679
  * Get component metadata (reference, value, description)
629
680
  */
@@ -694,7 +745,7 @@ var AddSchematicSymbolsStage = class extends ConverterStage {
694
745
 
695
746
  // lib/schematic/stages/AddSchematicTracesStage.ts
696
747
  import { Wire, Pts as Pts2, Xy as Xy2, Stroke as Stroke2, Junction } from "kicadts";
697
- import { applyToPoint as applyToPoint2 } from "transformation-matrix";
748
+ import { applyToPoint as applyToPoint3 } from "transformation-matrix";
698
749
  var AddSchematicTracesStage = class extends ConverterStage {
699
750
  _step() {
700
751
  const { kicadSch, db } = this.ctx;
@@ -732,11 +783,11 @@ var AddSchematicTracesStage = class extends ConverterStage {
732
783
  "Schematic transformation matrix not initialized in context"
733
784
  );
734
785
  }
735
- const from = applyToPoint2(this.ctx.c2kMatSch, {
786
+ const from = applyToPoint3(this.ctx.c2kMatSch, {
736
787
  x: edge.from.x,
737
788
  y: edge.from.y
738
789
  });
739
- const to = applyToPoint2(this.ctx.c2kMatSch, {
790
+ const to = applyToPoint3(this.ctx.c2kMatSch, {
740
791
  x: edge.to.x,
741
792
  y: edge.to.y
742
793
  });
@@ -762,7 +813,7 @@ var AddSchematicTracesStage = class extends ConverterStage {
762
813
  "Schematic transformation matrix not initialized in context"
763
814
  );
764
815
  }
765
- const { x, y } = applyToPoint2(this.ctx.c2kMatSch, {
816
+ const { x, y } = applyToPoint3(this.ctx.c2kMatSch, {
766
817
  x: junction.x,
767
818
  y: junction.y
768
819
  });
@@ -1018,7 +1069,7 @@ var AddNetsStage = class extends ConverterStage {
1018
1069
 
1019
1070
  // lib/pcb/stages/AddFootprintsStage.ts
1020
1071
  import { Footprint, FpText, FootprintPad } from "kicadts";
1021
- import { applyToPoint as applyToPoint3 } from "transformation-matrix";
1072
+ import { applyToPoint as applyToPoint4 } from "transformation-matrix";
1022
1073
  var AddFootprintsStage = class extends ConverterStage {
1023
1074
  componentsProcessed = 0;
1024
1075
  pcbComponents = [];
@@ -1042,7 +1093,7 @@ var AddFootprintsStage = class extends ConverterStage {
1042
1093
  const sourceComponent = component.source_component_id ? this.ctx.db.source_component.get(component.source_component_id) : null;
1043
1094
  const footprintName = sourceComponent?.ftype || "Unknown";
1044
1095
  const componentName = sourceComponent?.name || `Component_${this.componentsProcessed}`;
1045
- const transformedPos = applyToPoint3(c2kMatPcb, {
1096
+ const transformedPos = applyToPoint4(c2kMatPcb, {
1046
1097
  x: component.center.x,
1047
1098
  y: component.center.y
1048
1099
  });
@@ -1123,7 +1174,7 @@ var AddFootprintsStage = class extends ConverterStage {
1123
1174
 
1124
1175
  // lib/pcb/stages/AddTracesStage.ts
1125
1176
  import { Segment, SegmentNet } from "kicadts";
1126
- import { applyToPoint as applyToPoint4 } from "transformation-matrix";
1177
+ import { applyToPoint as applyToPoint5 } from "transformation-matrix";
1127
1178
  var AddTracesStage = class extends ConverterStage {
1128
1179
  tracesProcessed = 0;
1129
1180
  pcbTraces = [];
@@ -1151,11 +1202,11 @@ var AddTracesStage = class extends ConverterStage {
1151
1202
  for (let i = 0; i < trace.route.length - 1; i++) {
1152
1203
  const startPoint = trace.route[i];
1153
1204
  const endPoint = trace.route[i + 1];
1154
- const transformedStart = applyToPoint4(c2kMatPcb, {
1205
+ const transformedStart = applyToPoint5(c2kMatPcb, {
1155
1206
  x: startPoint.x,
1156
1207
  y: startPoint.y
1157
1208
  });
1158
- const transformedEnd = applyToPoint4(c2kMatPcb, {
1209
+ const transformedEnd = applyToPoint5(c2kMatPcb, {
1159
1210
  x: endPoint.x,
1160
1211
  y: endPoint.y
1161
1212
  });
@@ -1189,7 +1240,7 @@ var AddTracesStage = class extends ConverterStage {
1189
1240
 
1190
1241
  // lib/pcb/stages/AddViasStage.ts
1191
1242
  import { Via, ViaNet } from "kicadts";
1192
- import { applyToPoint as applyToPoint5 } from "transformation-matrix";
1243
+ import { applyToPoint as applyToPoint6 } from "transformation-matrix";
1193
1244
  var AddViasStage = class extends ConverterStage {
1194
1245
  viasProcessed = 0;
1195
1246
  pcbVias = [];
@@ -1210,7 +1261,7 @@ var AddViasStage = class extends ConverterStage {
1210
1261
  return;
1211
1262
  }
1212
1263
  const via = this.pcbVias[this.viasProcessed];
1213
- const transformedPos = applyToPoint5(c2kMatPcb, {
1264
+ const transformedPos = applyToPoint6(c2kMatPcb, {
1214
1265
  x: via.x,
1215
1266
  y: via.y
1216
1267
  });
@@ -1237,7 +1288,7 @@ var AddViasStage = class extends ConverterStage {
1237
1288
 
1238
1289
  // lib/pcb/stages/AddGraphicsStage.ts
1239
1290
  import { GrLine } from "kicadts";
1240
- import { applyToPoint as applyToPoint6 } from "transformation-matrix";
1291
+ import { applyToPoint as applyToPoint7 } from "transformation-matrix";
1241
1292
  var AddGraphicsStage = class extends ConverterStage {
1242
1293
  _step() {
1243
1294
  const { kicadPcb, c2kMatPcb } = this.ctx;
@@ -1254,11 +1305,11 @@ var AddGraphicsStage = class extends ConverterStage {
1254
1305
  const startPoint = path.route[i];
1255
1306
  const endPoint = path.route[i + 1];
1256
1307
  if (!startPoint || !endPoint) continue;
1257
- const transformedStart = applyToPoint6(c2kMatPcb, {
1308
+ const transformedStart = applyToPoint7(c2kMatPcb, {
1258
1309
  x: startPoint.x,
1259
1310
  y: startPoint.y
1260
1311
  });
1261
- const transformedEnd = applyToPoint6(c2kMatPcb, {
1312
+ const transformedEnd = applyToPoint7(c2kMatPcb, {
1262
1313
  x: endPoint.x,
1263
1314
  y: endPoint.y
1264
1315
  });
@@ -1299,7 +1350,7 @@ var AddGraphicsStage = class extends ConverterStage {
1299
1350
  ];
1300
1351
  }
1301
1352
  const transformedCorners = corners.map(
1302
- (corner) => applyToPoint6(c2kMatPcb, corner)
1353
+ (corner) => applyToPoint7(c2kMatPcb, corner)
1303
1354
  );
1304
1355
  for (let i = 0; i < transformedCorners.length; i++) {
1305
1356
  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.10",
4
+ "version": "0.0.12",
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": {