forked from lachlanA/eagle-to-kicad-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheagle-lbr2kicad-1.0.ulp
2876 lines (2482 loc) · 101 KB
/
eagle-lbr2kicad-1.0.ulp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#usage "<b>KiCAD lib and module exporting tool version 2.3)</b>\n"
"<p>"
"This ULP can exports Eagle CAD Package and Symbol contents into KiCAD lib and module formats."
"<p>"
"In Load any library and execute the ULP."
"<p>"
"<author>Author: Dmitri N. Sytov sdn (at) openhardware.ru</author>"
"<author>Author: juergen.messerer (at) freesurf.ch</author>"
"<author>Author: \"lachlanusa (at) gmail.com\"</author>"
#require 6.0000
//#require 5.0000
#include "eagle_to_kicad_include.inc"
/*
* CHANGELOG=(dd.mm.yyyy)===============================================
* 07.05.2016 Fix device with no package which has more than 1 pin
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 03.05.2016 Fix F1 feild to match Def feild, IE remove any illgial charters.
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 03.05.2016 Fix layers in Text string's so will place correctly on the layers.
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 29.04.2016 Fix loging file name, and add user option to select loging directory.
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 08.02.2016 Change Default size of F0 and F1, Refance/value to 45
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 08.02.2016 Fix over bar, on pin labels, this changes Eagle ! to KiCad ~
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 15.10.2015: Alays display report file, even if no errors
* Add spacing to make window wider, 1000px, for better report display
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 12.10.2015: Check for pagage before printing reporting on missing prefix.
*
* 09.10.2015: Add missing prefix report Dilog
*
* 27.08.2015: Fix Power systembal to correct for KiCad Power fills, change power supllie to opersit of Eagle.
* Change pin to hide when power. supply system.
*
* 19.07.2015: Fix / slash in Ealge's lib and part name's and device name
* Add faster string replace/subsute sub "string charstr_replace(string search, string replace, string subject)"
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 10.02.2015: Fix add lib prefix to footprint name
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 03.02.2015: Fix Alais name, and inc version.
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 25.09.2014: Add version reqired, to alows us to make 2 scripts. One for 6.0+ and one for 5.0
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 06.09.2014: Fix device alaias, to put in zero size string in * place holder when
* '' is found in part Tecknowage/alias name.
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 06.09.2014: Move some code to main(), bit easy to understand!
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 05.09.2014: Inc version number to 1.1
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 05.09.2014: Try a number of fix's for part name, and ALISA list, to work better with eagle2kicad_sch.ulp
* Note there are many problems here. See as badly name eagle lib's will make for lot's of devices name collisions!
* Which will make KiCad stop loading the lib/mod!
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 26.07.2014: Fix >NAME and >VALUE in in module, so eagle text is not lost, or confused with module prefix/value
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 26.07.2014: Fix text width in module, so module text use's eagles text width.
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 26.07.2014: Fix Squ Pad size 0 problem in module
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 14.07.2014: Add option allows combining of device's into one KiCad lib part if the pin name/number match exactly.
* Note: Pin number/name IS the deciding factor! NOT package type or package pin count.
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 12.07.2014: Add option to allows adding $PACKAGE list to KiCad where parts Shari'a common pin refances.
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 21.06.2014: Fix bug where, >NAME and >VALUE were getting past thou from eage to kicad libs
* because there were not in upper case.
* thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 20.06.2014: Add option to Remove Text after @, this works bit like the hide after @ in Eagle SCH
* thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 20.06.2014: Fix bug in str_replace(string search, string replace, string subject
* Bug was, it did not return the Original string, if the search string was not
* found, but returned the replacement string !!
* thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 12.06.2014: Fix \r \n in value import, which eagle allows but KiCAD dos not like.
* also remove any other charters which are not printable
* thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 14.02.2014: - Fix "Multiple contacts on pin, use contacts() instead" error in
* Eagle 6.0 (and later) when multiple pads are connected to same pin. (caspencer)
* - Fix issue with incorrect smd/pad angles
* thanks to: Jan Phillip Willmann (jantek.willmann at gmail.com)
*
* 28.12.2012: Fixed BUG: wrong scaling of elements
* Now compatible with Eagle 6.3 and KiCad Build 2012-01-19 by Wawszczak
*
* 02.01.2012: Fixed BUGS: (that it is compatible with the new KiCad versions)
* KiCad "undefined draw command"
* KiCad "unexpected end of file / module"
*
* 01.12.2006: Little changes on the script with DEV.package. Always check if
* DEV.package exist otherwise an error will occur.
*
* 22.08.2006: - Changes made by Sergiusz Urbaniak (mieszkoman (at) gmx.de)
* - Selection of export of Pin Names and Pin Numbers
* - Prefixing each module with the Eagle library name (easier to navigate in the netlist)
* - Support of multiple units
*
* 17.07.2006: The positions of the name and the prefix are fixed for multi part devices
*
* 17.07.2006: Now the script creates also devices without packages.
* When a device has more then one part then it use symbol name
* for the alias "F0, F1, .."
* When it has more then one package then it use the device name
*
* 19.06.2006: Bugfix, devices with more then one packages will now generate
* a device for each package
*
* 14.06.2006: Add the translation feature for multipart devices
*
* 12.06.2006: Fixed the arc problem. There is still a text position problem
*
* 08.06.2006: Now it converts also the symbol from eagle
*
* 14.11.2005: Fixed BUGS:
* - Invalid layers in $PAD section
* - Text fields has right coords but bad align
* - Polygonal areas draws twice
* Thanks to:
* Biglacko <biglacko (at) startolj.hu>
*/
/* ==========================================================================
* License: This file is released under the license of the GNU Public license
* Version 2.
* ==========================================================================
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
real VERSION = 2.6;
string Version = "2.6";
int add_libnamePrefixToPartName = 0;
int rect_conv = 1; // many QFP-like packages use rectangles as a pin contour (too much segments in the ki-format)
int poly_conv = 1;
int holes_conv = 1;
string g_lib_prefix ;
int writePins = 0;
int drawPinNumber = 1;
int drawPinName = 1;
int outPutAlias = 1;
int outPutFootPrint = 1; // Controls the Out put foot print in F2
int outPutFootPrints = 1; // Controls the Out put footprints in $FPLIST
string footPrintList = "";
string footPrintPreFix = ""; // Prefix for footPrint note maybe blank
string undefinedPartPreFix = "M";
string aliasList = ""; // list of part alias's
int wildCardPrefix = 1; // 1 = pre add * wild card to $FPLIST footprint list names
int wildPostfix = 1; // 1 = post add * wild card to $FPLIST footprint list names
int eaglePartToKiCadPartConversion = 0; // control's if we cobine footprints or not, 0 = combine, 1 = dont combine
int devicesetsCount = 0;
int devicesCount = 0;
int deleteTextArfterATsysbolInPinName = 1; //Eagle dos not show any text in pin name's arfter the @ sysbold.
int deleteNameTextSCH = 1; //Delete >Name from schematic
int deleteValueTextSCH = 1; //Delete >Value from schematic
string symbolicPrefix; /*list symbolic prefix(s)(commer seperated) not to show pin's or pad's or Part, or symbolic prefix for, Note list format is D, C, X, V, # */
int dontShowPin = 1; //Dont show pin's from symbloicPrefix list
int dontShowPad = 1; //Dont show Pad's from symbloicPrefix list
int dontShowPartName = 1; //Dont show part name from symbloicPrefix list
int dontShowPartValue = 1; //Dont show part name from symbloicPrefix list
string pinNamePnumberList[]; // Holds aray of pint number and name + padcount
string packageNameList[]; // Holds aray of package names
string packageNameListMatch[]; // Holds aray of package names
string NameSCH = ">Name";
string ValueSCH = ">Value";
int RECT_WIDTH = 26;
real linMaxWidth = 10.0;
real linMinWidth = 0.01;
int limitTextLineWidth = 0;
int minMaxUnitsSelect = 0;
int minMaxUnitsSelectOld = 0;
real eagleToKiCadUnitCoversionFactor1 = 808;
real eagleToKiCadUnitCoversionFactor2 = 404;
string partlist[];
int partlistSize = 0;
string conflictPartsLibsList[];
int conflictPartsLibsListsize = 0;
int MM = 0, INCH = 1, TOINCH = 2;
string logfileName;
string logfile;
string outputPath;
string infoDeviceConversion = "<pre>Allows combining of device's into one KiCad lib part if the pin name/number match exactly.<br>"+
"Note pin number/name not package type or the number of pins on the package is the deciding factor.<br>"+
"If not enabled conversion will make a new KiCad library part/module for every one of eagle<br>devices/footprint this of course "+
"make's for a very large KiCad library so this give's<br>the option of combining the eagle part's where"+
" possible to make a Kicad library,<br>there is some risk to this, as the matching process could have<br>"+
"<b><br>So it's import you check the results when using this option<b>!!!</b></pre>";
string infoTechnologies = "Make KiCad part Alias's from the <i>Eagle</i> Technologies definition in Eagle device\'s";
string infoLibNamePartNamePrefix = "Prefix lib-name to the Part-name IE: LibFred_PartName";
string infoDeleArfter = "<pre>Eagle SCH/PCB will not show any charters after \'@\' in pin names<br>"+
"Checking this will delete any charters after and including the <b>'@'</b><br>so it wont show in KiCad</pre>";
string infoPreFixSeach = "Add a wild card prefix '*' to each footprint in the footprint list, this allows easy footprint searching in CvPCB.";
string infoPostFixSeach = "Add a wild card post-fix '*' to each footprint in the footprint list, this allows easy footprint searching in CvPCB.";
string infoNoPrefix = "If a eagle lib part has no prefix IE: \"U\", \"R\", \"C\" etc, then we use this letter(s) for SCH/Part Prefix";
string infoFootPrintList = "KiCad use's the footprint list to help select from the list of PCB module/footprint(s) in KiCad's module lib.<br>This option controls the output, and output format"+
" given to KiCad<br>Note: This is not the same as KiCad footprint";
string infoFootPrint = "<pre>You have 2 options, for the footprint name in KiCad.<br>Just use eagle device name for the footprint name in KiCad,<br>"+
"OR prefix the eagle lbr name to the eagle device name to crate the the KiCad footprint name.<br>"+
"IE: \"eagleLbrName-eagleDeviceName\".<br>"+
"Note: is not the same as the footprint list. In KiCad<br>"+
"Which is used to filter the module footprints displayed in KiCad's CvPcb program.</pre>";
string infoFootPrintPrefix = "You can override the default eagle lib name, which is the prefix to the KiCad footprint name.<br>Note: that this program always adds '_' between prefix name and foot print name.";
string infoTextMaxMinValue = "<pre>You can override text font line width size in eagle, and use the Max and Min line width,<br>which KiCad use's to draw the text<br>"+
"IE: Any text line width which is bigger than Max line will be replaced with Max value.<br>"+
" Any text line width which is less than Min line will be replaced with Min width value.</pre>";
string infoProgramHowTo = "This program converts a eagle lib to KiCad lib and mod lib.<br>You have a number of options to how this conversion is done.<br>"+
"<b><br><i>WARNING: You should check the results of the conversion!<br>As there is no way this program will make a perfect conversion<i></b>"+
"<pre><b><font color=red>BUGS and and warnings:<br> 1: Text can appear on any layer in Eagle lib's, where as KiCad only allows Text on top layer<br>"+
" This will result in all text from eagle appearing on the top silk screen layer,<br> and mirrored text will no longer be mirrored !</pre></font>"+
"<i>This version of eagle-to-kicad is hosted on <a href=\"http://github.com/lachlanA/eagle-to-kicad/\">https://github.com/lachlanA/eagle-to-kicad</a><br>"+
"Please check this site for latest version, bug reports, and to make suggestions/improvement<br><i>";
// please edit this LUT if you found mistakes,
// and send your corrections to sdn (at) openhardware.ru.
/*
LAYER_CMP_N 15
CMP_N 15
NB_COPPER_LAYERS (CMP_N+1)
FIRST_NO_COPPER_LAYER 16
ADHESIVE_N_CU 16
ADHESIVE_N_CMP 17
SOLDERPASTE_N_CU 18
SOLDERPASTE_N_CMP 19
SILKSCREEN_N_CU 20
SILKSCREEN_N_CMP 21
SOLDERMASK_N_CU 22
SOLDERMASK_N_CMP 23
DRAW_N 24
COMMENT_N 25
ECO1_N 26
ECO2_N 27
EDGE_N 28
*/
// Lookup table for shape layers
int layer_lut[] =
{
0,
15, //LAYER_TOP
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //inner layers
16, //LAYER_BOTTOM
16, //LAYER_PADS
16, //LAYER_VIAS
21, //LAYER_UNROUTED
21, //LAYER_DIMENSION
21, //LAYER_TPLACE
21, //LAYER_BPLACE
21, //LAYER_TORIGINS
20, //LAYER_BORIGINS
21, //LAYER_TNAMES
20, //LAYER_BNAMES
21, //LAYER_TVALUES
20, //LAYER_BVALUES
21, //LAYER_TSTOP
21, //LAYER_BSTOP
21, //LAYER_TCREAM
21, //LAYER_BCREAM
21, //LAYER_TFINISH
21, //LAYER_BFINISH
21, //LAYER_TGLUE
21, //LAYER_BGLUE
21, //LAYER_TTEST
21, //LAYER_BTEST
21, //LAYER_TKEEPOUT
21, //LAYER_BKEEPOUT
21, //LAYER_TRESTRICT
21, //LAYER_BRESTRICT
21, //LAYER_VRESTRICT
21, //LAYER_DRILLS
21, //LAYER_HOLES
21, //LAYER_MILLING
21, //LAYER_MEASURES
24, //LAYER_DOCUMENT
21, //LAYER_REFERENCE
24, //LAYER_TDOCU
24, //LAYER_BDOCU
21, //LAYER_NETS
21, //LAYER_BUSSES
21, //LAYER_PINS
21, //LAYER_SYMBOLS
21, //LAYER_NAMES
21 //LAYER_VALUES
};
// Lookup table for pad layers
//CUIVRE_LAYER 0x00000001
//CMP_LAYER 0x00008000
//ADHESIVE_LAYER_CU 0x00010000
//ADHESIVE_LAYER_CMP 0x00020000
//SOLDERPASTE_LAYER_CU 0x00040000
//SOLDERPASTE_LAYER_CMP 0x00080000
//SILKSCREEN_LAYER_CU 0x00100000
//SILKSCREEN_LAYER_CMP 0x00200000
//SOLDERMASK_LAYER_CU 0x00400000
//SOLDERMASK_LAYER_CMP 0x00800000
//DRAW_LAYER 0x01000000
//COMMENT_LAYER 0x02000000
//ECO1_LAYER 0x04000000
//ECO2_LAYER 0x08000000
//EDGE_LAYER 0x10000000
//intS_LAYER 0xE0000000
//ALL_LAYERS 0x1FFFFFFF
//ALL_NO_CU_LAYERS 0x1FFF0000
//ALL_CU_LAYERS 0x0000FFFF
//INTERNAL_LAYERS 0x00007FFE
//EXTERNAL_LAYERS 0x00008001
int pad_lut[] =
{
0,
0x00008000 | 0x00800000 | 0x00080000, //LAYER_TOP
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //inner layers
0x00000001 | 0x00400000, //LAYER_BOTTOM
0x0000FFFF | 0x00C00000 | 0x000C0000, //LAYER_PADS
0x00008001 | 0x00800000 | 0x00400000, //LAYER_VIAS
0, //LAYER_UNROUTED
0, //LAYER_DIMENSION
0, //LAYER_TPLACE
0, //LAYER_BPLACE
0, //LAYER_TORIGINS
0, //LAYER_BORIGINS
0, //LAYER_TNAMES
0, //LAYER_BNAMES
0, //LAYER_TVALUES
0, //LAYER_BVALUES
0, //LAYER_TSTOP
0, //LAYER_BSTOP
0, //LAYER_TCREAM
0, //LAYER_BCREAM
0, //LAYER_TFINISH
0, //LAYER_BFINISH
0, //LAYER_TGLUE
0, //LAYER_BGLUE
0, //LAYER_TTEST
0, //LAYER_BTEST
0, //LAYER_TKEEPOUT
0, //LAYER_BKEEPOUT
0, //LAYER_TRESTRICT
0, //LAYER_BRESTRICT
0, //LAYER_VRESTRICT
0x01000000, //LAYER_DRILLS
0x01000000, //LAYER_HOLES
0x01000000, //LAYER_MILLING
0, //LAYER_MEASURES
0, //LAYER_DOCUMENT
0, //LAYER_REFERENCE
0, //LAYER_TDOCU
0, //LAYER_BDOCU
0, //LAYER_NETS
0, //LAYER_BUSSES
0x00008001 | 0x00A00000 | 0x00080000, //LAYER_PINS
0, //LAYER_SYMBOLS
0, //LAYER_NAMES
0 //LAYER_VALUES
};
//------------------------------------------------------
// Global variables
//------------------------------------------------------
string g_mod_name ;
string g_lib_name ;
real g_Fact = 254 * 32; // global uMeter to uInch conversion factor
real LibTexPosionOffset = 0.770;
real textSizeCorrectionfactor = 0.8;
string missingPrefixLog[];
int g_missingPrefix = 0; // global Indicator if an eagle devices hasn't got an prefix
string errorLog[];
int warnings_count = 0;
int errors_count = 0;
/**
* @brief replace new line charter with string "\n" as kicad can handel that, but not new line chareter
* @param textString string with new line charter '\n'
* @return string with "\n" in '\n' place
*/
string stripLfCrToEscCRLF( string textString )
{
string newString;
int idx = 0;
int idx2 = 0;
for ( idx = 0; textString[idx];)
{
if( ( isspace( textString[idx] )) || ( isprint( textString[idx] )))
{
if(( textString[idx] == '\n') || ( textString[idx] == '\r' ) )
{
newString = newString + "\\n";
idx++;
idx2 = idx2 + 2;
}
else
{
newString[idx2] = textString[idx];
idx++;
idx2++;
}
}
else
{ // Ok bad chart so replace with ~
newString[idx2] = '~';
idx++;
idx2++;
}
}
return newString;
}
/**
* @brief
* @param unitConvertTo convert to unit (MM,INCH,TOINCH)
* @param unitConvertFrom convert from unit (MM,INCH,TOINCH)
* @param inputValue unit to convert
* @return converted unid
*/
real unitConver(int unitConvertTo, int unitConvertFrom, real inputValue)
{
// 0 = MM, 1 = Inch, 2 = Thousands of inch
real converversionTable0[] = {1.0, 25.4, 0.0254};
real converversionTable1[] = {0.03937007874, 1.0, 0.001};
real converversionTable2[] = {39.37007874, 1000.0, 1.0};
switch(unitConvertTo)
{
case 0: // MM
return (inputValue * converversionTable0[ unitConvertFrom ]);
break;
case 1: // INCH
return (inputValue * converversionTable1[ unitConvertFrom ]);
break;
case 2: // TOINCH
return (inputValue * converversionTable2[ unitConvertFrom ]);
break;
}
}
/**
* @brief look to layer
* @param layer layer ID
* @return Return layer ID
*/
int layer_lookup(int layer)
{
if (layer > 42) return 24;
return layer_lut[layer + 1];
}
/**
* @brief convert internal eagle units in 1E4*inchs
* @param units internal eagle units
* @return 1E4*inchs(internal eagle units)
*/
int egl2ki(int units)
{
real inch = u2inch(units);
return int(inch * 10000);
}
/**
* @brief check for smd pads in package
* @param PAC package
* @return if has smd pads return true else false
*/
int issmd(UL_PACKAGE PAC)
{
int res = 0;
PAC.contacts(C)
{
res |= C.smd ? 1 : 0;
}
return res;
}
/**
* @brief replaces all occurrences of a charter in a string with a string
* @param search charter to search
* @param replace charter to replace
* @param subject original string
* @return return modified string
*/
string charstr_replace(string search, string replace, string subject)
{
int lastpos = 0;
int pos = 0;
string before;
string after;
for( pos = 0; pos < strlen(search); pos++ )
{
lastpos = 0;
while( (( lastpos = strchr( subject, search[ pos ], lastpos)) != -1 ))
{
before = strsub(subject, 0, lastpos); // get the before string
after = strsub(subject, lastpos + 1, strlen(subject) - lastpos );
subject = before + replace + after;
lastpos = strlen(before) + strlen( replace ); //
}
}
return subject;
}
/**
* @brief replace all occurrences of a string
* @param search string to sarch
* @param replace string to replace
* @param subject original string
* @return return modified string
*/
string str_replace(string search, string replace, string subject)
{
int lastpos = 0;
int pos;
string before;
string after;
// Check if there is any thing to replace
if( strstr(subject, search, lastpos) == -1)
return subject; // No so just return input string
while (strstr(subject, search, lastpos) >= 0)
{
pos = strstr(subject, search, lastpos);
before = strsub(subject, 0, pos);
after = strsub(subject, pos + strlen(search), strlen(subject) - ( pos + strlen(search)) );
subject = before + replace + after;
lastpos = pos + strlen(replace);
}
return subject;
}
/**
* @brief replace escape quotes in string
* @param input original string
* @return return modified string
*/
string escape_quotes(string input)
{
return str_replace( "\"", "\\\"", input);
}
//****
// Truncats string, from delimiter charter in string
// Return trancated string
/**
* @brief Splits a string and return the first one.
* @param name original string
* @param delimiter delimited charter
* @param flag if 0, return original string, else the splited string
* @return splited string
*/
string stripArfterAT(string name, char delimiter, int flag)
{
string a[];
int lastpos = 0;
if(flag == 0)
return name;
// Split, if more the 1 delimiter take ony the first part, if not found return with no change
if( strsplit(a, name, delimiter) >= 1 )
return a[0];
else
return name;
}
/**
* @brief transform int to string
* @param value int value
* @return string with int value
*/
string intToString( int value )
{
string tmpS;
sprintf( tmpS, "%d", value );
return tmpS;
}
/**
* @brief search for conflicking part
* @param search search part in partList
* @param offset offset in partList
* @return return "-CONFLICKING-PART-" if there is a conflict, And add to conflictPartsLibsList[] list
* else return "" if all good
*/
string partlistSearchAdd( string search, int offset )
{
int n;
if( partlistSize == 0 )
{
partlist[ partlistSize++ ] = search;
return "";
}
for( n = 0; n < partlistSize; n++ )
{
// printf("\nsearch=%s partlist[%d]=%s", search, n, partlist[n]);
// printf("search=%s partlist[%d]=%s\n", search, n, partlist[n]);
if( (strlen( search ) == 0) ) // Null search ?
return "";
if( search == partlist[ n ] )
break;
else
{
if( (n + 1) >= partlistSize )
{
partlist[ partlistSize++ ] = search;
return "";
}
}
}
conflictPartsLibsList[ conflictPartsLibsListsize++ ] = search;
return "-CONFLICKING-PART-" + intToString( conflictPartsLibsListsize ) ;
}
/**
* @brief write index of modules
* @param LIB Library
*/
void write_kikad_mod_idx(UL_LIBRARY LIB)
{
// write INDEX start tag
printf("$INDEX\n");
// write index of modules
// LIB.packages(PAC)
// {
// if ( outPutFootPrint ) // Prefix libprefix in foot print name
// printf("%s-%s\n", escape_quotes(g_lib_prefix), escape_quotes(strupr(PAC.name)));
// else
// printf("%s\n", escape_quotes(strupr(PAC.name)));
// }
if( outPutFootPrint == 0 )
LIB.packages(PAC) // Don't add lib prefix
{
printf("%s\n", charstr_replace(",/", "_", escape_quotes(strupr(PAC.name))));
// printf("%s\n", str_replace( ",", "_", escape_quotes(strupr(PAC.name))));
}
else
LIB.packages(PAC) // Add lib prefix
{
printf("%s-%s\n", escape_quotes(g_lib_prefix), charstr_replace(",/", "_", escape_quotes(strupr(PAC.name))));
// printf("%s-%s\n", escape_quotes(g_lib_prefix), str_replace( ",", "_", escape_quotes(strupr(PAC.name))));
}
// write INDEX end tag
printf("$EndINDEX\n");
}
/**
* @brief pad conversion
* @param PAC eagle package http://web.mit.edu/xavid/arch/i386_rhel4/help/181.htm
*/
void write_kikad_mod_pad(UL_PACKAGE PAC)
{
char shp = 'R';
string signal, type = "STD";
int angle = 0, dx, dy, drill, layset;
PAC.contacts(CN)
{
// write PAD start tag
printf("$PAD\n");
signal = CN.signal;
if (CN.smd)
{
type = "SMD";
if (CN.smd.roundness > 40)
{
if (CN.smd.dx == CN.smd.dy)
shp = 'C';
else
shp = 'O';
}
angle = CN.smd.angle;
dx = CN.smd.dx;
dy = CN.smd.dy;
layset = CN.smd.layer;
}
if (CN.pad)
{
type = "STD";
if (CN.pad.shape[LAYER_TOP] == PAD_SHAPE_ROUND )
{
shp = 'C';
dy = CN.pad.diameter[LAYER_TOP];
}
if (CN.pad.shape[LAYER_TOP] == PAD_SHAPE_OCTAGON)
{
shp = 'C';
dy = CN.pad.diameter[LAYER_TOP] * 2;
}
if (CN.pad.shape[LAYER_TOP] == PAD_SHAPE_LONG )
{
shp = 'O';
angle = CN.pad.angle + 90;
dy = CN.pad.diameter[LAYER_TOP] * 2;
}
if (CN.pad.shape[LAYER_TOP] == PAD_SHAPE_SQUARE )
{
shp = 'R';
dy = CN.pad.diameter[LAYER_TOP];
}
dx = CN.pad.diameter[LAYER_TOP];
drill = CN.pad.drill;
layset = LAYER_PADS ;
}
// pad shape
printf("Sh \"%s\" %c %d %d %d %d %d\n", CN.name, shp, egl2ki(dx), egl2ki(dy), 0, 0, angle * 10);
printf("Dr %d %d %d \n", egl2ki(drill), 0, 0);
printf("At %s N %08X\n", type, pad_lut[layset]);
printf("Ne 0 \"%s\"\n", ""); // should always be a null not signal name !
printf("Po %d %d \n", egl2ki(CN.x), -egl2ki(CN.y));
// write PAD end tag
printf("$EndPAD\n");
}
}
//------------------------------------------------------
// shape conversion
//------------------------------------------------------
/**
* shape conversion
* @param PAC eagle package http://web.mit.edu/xavid/arch/i386_rhel4/help/181.htm
*/
void write_kicad_mod_shapes(UL_PACKAGE PAC)
{
//writing shapes
//also we must convert inverted y-coordinates (take it with minus)
//
//segments (always from eagle's rectangles,wires and optional polygons)
// converting rectangles -------------------------------------------------------------
int layer;
if (rect_conv)
PAC.rectangles(R)
{
layer = layer_lookup(R.layer);
printf("DS %d %d %d %d %d %d\n",
egl2ki(R.x1), -egl2ki(R.y1), egl2ki(R.x2), -egl2ki(R.y1), RECT_WIDTH, layer);
printf("DS %d %d %d %d %d %d\n",
egl2ki(R.x2), -egl2ki(R.y1), egl2ki(R.x2), -egl2ki(R.y2), RECT_WIDTH, layer);
printf("DS %d %d %d %d %d %d\n",
egl2ki(R.x1), -egl2ki(R.y2), egl2ki(R.x2), -egl2ki(R.y2), RECT_WIDTH, layer);
printf("DS %d %d %d %d %d %d\n",
egl2ki(R.x1), -egl2ki(R.y1), egl2ki(R.x1), -egl2ki(R.y2), RECT_WIDTH, layer);
}
// converting wires---------------------------------------------------------------------
PAC.wires(W)
{
layer = layer_lookup(W.layer);
if (!W.arc)
printf("DS %d %d %d %d %d %d\n",
egl2ki(W.x1), -egl2ki(W.y1), egl2ki(W.x2), -egl2ki(W.y2), egl2ki(W.width), layer);
}
// polygons converting ------------------------------------------------------------------
if (poly_conv)
PAC.polygons(P)
{
P.contours(CONT)
{
layer = layer_lookup(CONT.layer);
printf("DS %d %d %d %d %d %d\n",
egl2ki(CONT.x1), -egl2ki(CONT.y1), egl2ki(CONT.x2), -egl2ki(CONT.y2), egl2ki(CONT.width), layer);
}
}
//circles (always from eagle's circles and optional from holes)
//converting circles --------------------------------------------------------------------
PAC.circles(C)
{
layer = layer_lookup(C.layer);
printf("DC %d %d %d %d %d %d\n", egl2ki(C.x) , -egl2ki(C.y) ,
egl2ki(C.x) + ((egl2ki(C.x) > 0) ? 1 : (-1)) * egl2ki(C.radius / 2),
-egl2ki(C.y) - ((egl2ki(C.y) > 0) ? 1 : (-1)) * egl2ki(C.radius / 2), egl2ki(C.width / 2), layer);
}
if (holes_conv)
PAC.holes(H)
{
layer = 21;
printf("DC %d %d %d %d %d %d\n",
egl2ki(H.x), -egl2ki(H.y),
egl2ki(H.x) + ((egl2ki(H.x) > 0) ? 1 : (-1)) * egl2ki(H.drill / 4),
-egl2ki(H.y) - ((egl2ki(H.y) > 0) ? 1 : (-1)) * egl2ki(H.drill / 4),
50, layer);
printf("DS %d %d %d %d %d %d\n",
egl2ki(H.x - (H.drill / 2)), -egl2ki(H.y),
egl2ki(H.x + (H.drill / 2)), -egl2ki(H.y), 50, layer);
printf("DS %d %d %d %d %d %d\n",
egl2ki(H.x), -egl2ki(H.y - (H.drill / 2)),
egl2ki(H.x), -egl2ki(H.y + (H.drill / 2)), 50, layer);
}
//converting (always from wires)arcs ----------------------------------------------------
PAC.wires(W)
{
if (W.arc)
{
layer = layer_lookup(W.layer);
printf("DA %d %d %d %d %d %d %d \n",
egl2ki(W.arc.xc ), -egl2ki(W.arc.yc),
egl2ki(W.arc.x2), -egl2ki(W.arc.y2),
int((360 + abs(W.curve)) * 10) ,
egl2ki(W.arc.width), layer);
}
}
}
/**
* field conversion
* @param PAC eagle package http://web.mit.edu/xavid/arch/i386_rhel4/help/181.htm
*/
void write_kicad_mod_text(UL_PACKAGE PAC)
{
int textnum = 0;
int offset = 0;
int xoffs, yoffs, size;
int nameCt = 0;
int valueCt = 0;
int textCt = 0;
string text;
string NAME = "";
string VALUE = "";
int textWidth = 0;
PAC.texts(T)
{
textWidth = 0;
// find the max with of font/text
T.wires(W)
{
textWidth = W.width / eagleToKiCadUnitCoversionFactor1;
break;
}
// Limit line width ?
if( limitTextLineWidth )
{
if( textWidth < ( unitConver( MM, minMaxUnitsSelect, linMinWidth )*eagleToKiCadUnitCoversionFactor2))
textWidth = unitConver( MM, minMaxUnitsSelect, linMinWidth ) * eagleToKiCadUnitCoversionFactor2;
if( textWidth > ( unitConver( MM, minMaxUnitsSelect, linMaxWidth )*eagleToKiCadUnitCoversionFactor2 ))
textWidth = unitConver( MM, minMaxUnitsSelect, linMaxWidth ) * eagleToKiCadUnitCoversionFactor2;
}
size = egl2ki(T.size );
switch (int(T.angle))
{
case 0:
xoffs = (T.size * strlen(T.value)) / 2;
yoffs = T.size / 2;
break;
case 90:
xoffs = -T.size / 2;
yoffs = (T.size * strlen(T.value)) / 2;
break;
case 180:
xoffs = -(T.size * strlen(T.value)) / 2;
yoffs = -T.size / 2;
break;
case 270:
xoffs = (T.size / 2);
yoffs = -(T.size * strlen(T.value)) / 2;
break;
}
// check for part prefix/>NAME
if (strupr(T.value) == ">NAME" )
{
if ( nameCt == 0 )
{
NAME = strupr(T.value);
sprintf(NAME, "T%d %d %d %d %d %d %d N V %d N \"%s\"\n",
0, egl2ki(T.x + xoffs), -egl2ki(T.y + yoffs),
size, size,
int((abs(T.angle)) * 10), textWidth, layer_lut[T.layer], strupr(T.value) );
nameCt++;
textCt++;
continue;
}
else