-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtogedasym.c
More file actions
1832 lines (1696 loc) · 63.4 KB
/
Copy pathtogedasym.c
File metadata and controls
1832 lines (1696 loc) · 63.4 KB
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
/* togedasym
* converter from ascii exported Pro*el library parser to gEDA symbols
* Copyright (C) 2008 Jacek Plucinski
*
* 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
*
*
* this code has been write with gvim (http://www.vim.org),
* big thanks for the big tool, my right hand
*/
#define FileVersion "v.2008.04.09 alpha"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <getopt.h>
#include "PfWSchLibStruct.h"
#include "PfWSchLibParser.h"
#ifdef _DEBUG
#include "dmalloc.h"
#endif
#define TOFILE // if TOFILE not define - don't create files (print all to stdout for tests)
#ifdef TOFILE // open component file and write to this or print to stdout
#define OUTPUT OutFile
#else
#define OUTPUT stdout
#endif
//CONSTANTS
#define YES 1
#define NO 0
#define PI 3.1415927
#define CompFileNameLength 30 // max number of characters for component file name
#define FixedPinLength 300
// pin attributes mask used by parser
#define DotMask 0x0400
#define ClockMask 0x0200
#define TypeMask 0x01C0
#define HiddenMask 0x0020
#define NameVisibleMask 0x0010
#define NumbVisibleMask 0x0008
#define RotationMask 0x0007
// constants END
// DEFAULT CONVERT PARAMETERS
int FirstComp = -1; // first comp number to convert (default first in lib)
int LastComp = -1; // last comp number to convert (default last in lib)
unsigned char XYmultip = 20; // Pro*el -> gEDA X Y multiplier (scale)
char SetLineWidth = NO; // turn line width conversion ON or OFF (global)
unsigned char LineWidth0 = 5;
unsigned char LineWidth1 = 10;
unsigned char LineWidth2 = 20;
unsigned char LineWidth3 = 30;
// pins conversion parameters
char SetScalePins = NO; // enable pins length to be scaled as graphics
unsigned int DotSymDia = 100; // dot symbol diameter
unsigned char DotSymLine = 10; // dot symbol line width
unsigned int ClkSymWidth = 75; // clock symbol width
unsigned int ClkSymHigh = 100; // clock symbol high
unsigned char ClkSymLine = 10; // clock symbol line width
int PinNumHoffset = 100; // pin number horizontal offset
int PinNumVoffset = 50; // pin number vertical offset
unsigned char PinNumTxtSize = 8; // pin number text size
int PinLabHoffset = -50;
int PinLabVoffset = 0;
unsigned char PinLabTxtSize = 8; // pin label text size
int PinTypHoffset = -50;
int PinTypVoffset = -100;
unsigned char PinTypTxtSize = 7; // pin type text size
int PinSeqHoffset = 100;
int PinSeqVoffset = -50;
unsigned char PinSeqTxtSize = 7; // pin sequence number text size
unsigned char ElArcAproxLev = 4; // quarter of number polygon edges aprox. ellipse for arc
unsigned char EllipAproxLev = 6; // quarter of number polygon edges aprox. ellipse
unsigned char ElliToCirTole = 1; // tolerance (0-100)% ellipse to circle approximation
unsigned char BezierLines = 7; // number of lines aproximated Bezier curve
// parts attributes texts parameters
unsigned int AttrHoffset = 400; // horizontal X coordinate for parts attributes draw
unsigned int AttrVoffset = 200; // vertical offset between parts attributes texts
unsigned char AttrTxtSize = 10; // parts attributes text size
char WrValue = NO; // write extra "value=" attribute with comp. name
char WrFootpr2 = NO; // activate write component footprint 2 attribute
char WrFootpr3 = NO; // --''-- -''- --''-- --''-- 3 --''--
char WrFootpr4 = NO; // --''-- -''- --''-- --''-- 4 --''--
char WrField1 = NO; // activate write component field 1 attribute
char WrField2 = NO; // --''-- -''- --''-- -''- 2 --''--
char WrField3 = NO; // --''-- -''- --''-- -''- 3 --''--
char WrField4 = NO; // --''-- -''- --''-- -''- 4 --''--
char WrField5 = NO; // --''-- -''- --''-- -''- 5 --''--
char WrField6 = NO; // --''-- -''- --''-- -''- 6 --''--
char WrField7 = NO; // --''-- -''- --''-- -''- 7 --''--
char WrField8 = NO; // --''-- -''- --''-- -''- 8 --''--
char WrPrField1 = NO; // activate write component part field 1 attribute
char WrPrField2 = NO; // --''-- -''- --''-- part -''- 2 --''--
char WrPrField3 = NO; // --''-- -''- --''-- part -''- 3 --''--
char WrPrField4 = NO; // --''-- -''- --''-- part -''- 4 --''--
char WrPrField5 = NO; // --''-- -''- --''-- part -''- 5 --''--
char WrPrField6 = NO; // --''-- -''- --''-- part -''- 6 --''--
char WrPrField7 = NO; // --''-- -''- --''-- part -''- 7 --''--
char WrPrField8 = NO; // --''-- -''- --''-- part -''- 8 --''--
char WrPrField9 = NO; // --''-- -''- --''-- part -''- 9 --''--
char WrPrField10 = NO; // --''-- -''- --''-- part -''- 10 --''--
char WrPrField11 = NO; // --''-- -''- --''-- part -''- 11 --''--
char WrPrField12 = NO; // --''-- -''- --''-- part -''- 12 --''--
char WrPrField13 = NO; // --''-- -''- --''-- part -''- 13 --''--
char WrPrField14 = NO; // --''-- -''- --''-- part -''- 14 --''--
char WrPrField15 = NO; // --''-- -''- --''-- part -''- 15 --''--
char WrPrField16 = NO; // --''-- -''- --''-- part -''- 16 --''--
//default convert parameters END
char *ExtraAttr[20]; // saved pointers to extra attr. parameters (reset to 0 at start)
static char *ShortParameters = // short parameters definitions
"hl:f:e:ps:wr:";
static struct option LongParameter[] = // long parameters definitions
{
{"help" ,0,0,'h'},
{"list" ,0,0,'l'},
{"first-component" ,1,0,'f'},
{"last-component" ,1,0,'e'},
{"scale" ,1,0,'s'},
{"line-width" ,0,0,'w'},
{"line-smallest" ,1,0,'A'},
{"line-small" ,1,0,'B'},
{"line-medium" ,1,0,'C'},
{"line-large" ,1,0,'D'},
{"pin-dot-diameter" ,1,0,'E'},
{"pin-dot-line" ,1,0,'F'},
{"pin-clk-width" ,1,0,'G'},
{"pin-clk-high" ,1,0,'H'},
{"pin-clk-line" ,1,0,'I'},
{"pin-number-h-offset" ,1,0,'J'},
{"pin-number-v-offset" ,1,0,'K'},
{"pin-number-text-size" ,1,0,'L'},
{"pin-label-h-offset" ,1,0,'M'},
{"pin-label-v-offset" ,1,0,'N'},
{"pin-label-text-size" ,1,0,'O'},
{"pin-type-h-offset" ,1,0,'P'},
{"pin-type-v-offset" ,1,0,'Q'},
{"pin-type-text-size" ,1,0,'R'},
{"pin-seq-h-offset" ,1,0,'S'},
{"pin-seq-v-offset" ,1,0,'T'},
{"pin-seq-text-size" ,1,0,'U'},
{"pin-length-scaled" ,0,0,'p'},
{"ellipticalarc-level" ,1,0,'W'},
{"ellipse-level" ,1,0,'X'},
{"ellipse-tolerance" ,1,0,'Y'},
{"bezier-lines" ,1,0,'t'},
{"attribute-h-offset" ,1,0,'Z'},
{"attribute-v-offset" ,1,0,'a'},
{"attribute-text-size" ,1,0,'b'},
{"write-value" ,0,0,'c'},
{"write-footprint" ,1,0,'d'}, // select write footprint 2..4
{"write-field" ,1,0,'j'}, // select write filed 1..8
{"write-part-field" ,1,0,'k'}, // select write part field 1..16
{"extra-attribute" ,1,0,'r'}, // extra attribute to write
{0,0,0,0}
};
PFWSCHLIBFILE * pPfWLib=NULL; // Pro*el library file
FILE *OutFile; // gEDA file with component declaration
int MaxY; // max Y coordinate value (for places to draw symbol attributes)
const char // text
GedaVersion[] = // text; version write to symbol file
{
"v 20031231 1"
},
TxtHelp[] = // text; long help
{
"SYNOPSIS\n"
"\n"
" togedasym --help|-h\n"
"\t print this help to standard out\n"
"\n"
" togedasym --list|-l <FILE>\n"
"\t list components in Pro*el schema library file to standard out\n"
"\n"
" togedasym [OPTION] ... <FILE>\n"
"\t convert Pro*el schema symbols library file to gEDA format\n"
"\n"
"DESCRIPTION\n"
"\n"
"\t Convert Pro*el ascii exported schema symbol library to gEDA symbol.\n"
"\t Primitives not serviced by converter are reported to stderr.\n"
"\t Options aren't obligatory and are used only to change default values.\n"
"\n"
"OPTION\n"
"\n"
"--first-component=NUMBER, -f NUMBER\n"
"\t First component number to convert. Use, if not all components\n"
"\t should be converted. Before use, list library by -l options\n"
"\t and select required range of components\n"
"\t Default: 1 (first components in library)\n"
"\n"
"--last-component=NUMBER, -e NUMBER\n"
"\t Last component number to convert. Use if not all components\n"
"\t should be converted. Before use, list library by -l options\n"
"\t and select required range of components\n"
"\t Default: last components in library\n"
"\n"
"--scale=NUMBER, -s NUMBER\n"
"\t Pro*el to gEDA X Y coordinations multiplier\n"
"\t Default: 20\n"
"\n"
"--line-width, -w\n"
"\t Globally turns on line width declaration in outputs parts.\n"
"\t Lines will have width declared by other parameters.\n"
"\t Default line width conversion is disabled -\n"
"\t - elements have 0 line width.\n"
"\n"
"--line-smallest=NUMBER\n"
"\t Sets line width for primitives with 'smallest' line width attribute.\n"
"\t Should be used with --line-width option.\n"
"\t Default: 5\n"
"\n"
"--line-small=NUMBER\n"
"\t Sets line width for primitives with 'small' line width attribute.\n"
"\t Should be used with --line-width option.\n"
"\t Default: 10\n"
"\n"
"--line-medium=NUMBER\n"
"\t Sets line width for primitives with 'medium' line width attribute.\n"
"\t Should be used with --line-width option.\n"
"\t Default: 20\n"
"\n"
"--line-large=NUMBER\n"
"\t Sets line width for primitives with 'large' line width attribute.\n"
"\t Should be use with --line-width option.\n"
"\t Default: 30\n"
"\n"
"--pin-dot-diameter=NUMBER\n"
"\t Sets pin dot symbol diameter.\n"
"\t Changing this parameter changes pin length.\n"
"\t Default: 100\n"
"\n"
"--pin-dot-line=NUMBER\n"
"\t Sets pin dot symbol line width.\n"
"\t Should be used with --line-width option.\n"
"\t Default: 10\n"
"\n"
"--pin-clk-width=NUMBER\n"
"\t Sets pin clock symbol width. Default: 75\n"
"\n"
"--pin-clk-high=NUMBER\n"
"\t Sets pin clock symbol height. Default: 100\n"
"\n"
"--pin-clk-line=NUMBER\n"
"\t Sets pin clock symbol line width.\n"
"\t Should be used with --line-width option.\n"
"\t Default: 10\n"
"\n"
"--pin-number-h-offset=NUMBER\n"
"\t Distance between text with pin number and pin start point\n"
"\t in direction parallel to pin. Default: 100\n"
"\n"
"--pin-number-v-offset=NUMBER\n"
"\t Distance between text with pin number and pin start point\n"
"\t in direction perpendicular to pin. Default: 100\n"
"\n"
"--pin-number-text-size=NUMBER\n"
"\t Size of text with pin number. Default: 8\n"
"\n"
"--pin-label-h-offset=NUMBER\n"
"\t Distance between text with pin label and pin start point\n"
"\t in direction parallel to pin. Default: -50\n"
"\n"
"--pin-label-v-offset=NUMBER\n"
"\t Distance between text with pin label and pin start point\n"
"\t in direction perpendicular to pin. Default: 0\n"
"\n"
"--pin-label-text-size=NUMBER\n"
"\t Size of text with pin label. Default: 8\n"
"\n"
"--pin-type-h-offset=NUMBER\n"
"\t Distance between text with pin type and pin start point\n"
"\t in direction parallel to pin. Default: -50\n"
"\n"
"--pin-type-v-offset=NUMBER\n"
"\t Distance between text with pin type and pin start point\n"
"\t in direction perpendicular to pin. Default: -100\n"
"\n"
"--pin-type-text-size=NUMBER\n"
"\t Size of text with pin type. Default: 7\n"
"\n"
"--pin-seq-h-offset=NUMBER\n"
"\t Distance between text with pin sequence number and pin start point\n"
"\t in direction parallel to pin. Default: 100\n"
"\n"
"--pin-seq-v-offset=NUMBER\n"
"\t Distance between text with pin sequence number and pin start point\n"
"\t in direction perpendicular to pin. Default: -50\n"
"\n"
"--pin-seq-text-size=NUMBER\n"
"\t Size of text with pin sequence number. Default: 7\n"
"\n"
"--pin-length-scaled, -p\n"
"\t Enable pin length to be scaled as graphics elements.\n"
"\t This option is used when component has long pin number\n"
"\t ('Emitter' for example). Default pin length has fixed value 300.\n"
"\n"
"--ellipticalarc-level=NUMBER\n"
"\t Elliptical arcs are approximated by polygon.\n"
"\t Parameter specifies quarter of number of polygon edges which\n"
"\t approximate ellipse. Higher values make elliptical arc smoother\n"
"\t but part 'heavier'. Default: 4\n"
"\n"
"--ellipse-level=NUMBER\n"
"\t Ellipses are approximated by polygon.\n"
"\t Parameter specifies quarter of number of polygon edges which\n"
"\t approximate ellipse. Higher values make ellipse smoother but part\n"
"\t 'heavier'. Default: 6\n"
"\n"
"--ellipse-tolerance=NUMBER\n"
"\t Ellipses and eliptical arcs with equal diameters are always drawn\n"
"\t as circles. Ellipses and eliptical arcs with percentage difference\n"
"\t of diameters smaller than NUMBER will be drawn as circles too.\n"
"\t If you mistakenly draw circle as ellipse, or arc as eliptical arc,\n"
"\t this parameter will help you to correct this.\n"
"\t To disable this function, set NUMBER to 0.\n"
"\t Default: 1 (ellipses with 1\% diameters different will be drawn\n"
"\t as circle)\n"
"\n"
"--bezier-lines=NUMBER\n"
"\t Bezier curves are approximated by lines.\n"
"\t Parameter specifies number of lines which aproximate bezier curve.\n"
"\t Higher values make curve smoother but part more 'heavier'.\n"
"\t Default: 7\n"
"\n"
"--attribute-h-offset=NUMBER\n"
"\t Sets horizontal (x) coordinate for text that contains part\n"
"\t attribues. After symbol translation in gschem, text's placement\n"
"\t should be corrected. Default: 400\n"
"\n"
"--attribute-v-offset=NUMBER\n"
"\t Vertical (y) distance between texts that contain parts attributes\n"
"\t and vertical (y) distance between first text that contain part\n"
"\t attribute and part. Default: 200\n"
"\n"
"--attribute-text-size=NUMBER\n"
"\t Part attributes texts size.\n"
"\t Default: 10\n"
"\n"
"--write-value\n"
"\t Attribute \"device\" should be invisible in gEDA.\n"
"\t This option enables to write additional visible text attribute\n"
"\t \"value=device\" which always shows component device type\n"
"\t and can by edited in gschem.\n"
"\n"
"--write-footprint=[2][:3][:4]\n"
"\t Enables to write footprints 2, 3 or 4 texts attributes\n"
"\t in parts files. Footprint texts have hidden attribute.\n"
"\t By default only footprint 1 is written (obligatory in gEDA).\n"
"\t If footprint attribute has not value, text 'no value' appears\n"
"\n"
"--write-field=[1][:2] ... [:8]\n"
"\t Enables to write field 1..8 texts attributes in parts files.\n"
"\t By default no field text is written.\n"
"\t If field attribute has no value, text 'no value' appears\n"
"\n"
"--write-part-field=[1][:2] ... [:16]\n"
"\t Enables to write part field 1..16 texts attributes in parts files.\n"
"\t By default no part field text is written.\n"
"\t If part field attribute has no value, text 'no value' appears.\n"
"\n"
"--extra-attribute=[STRING], -r [STRING]\n"
"\t Add additional hidden attribute to component,\n"
"\t for example: -r author=jonny bravo\n"
"\n"
},
TxtNotDeclared[] = // info text; component attributes have to declare
{
"not declared, declare please"
},
TxtErrOptionsFirstLastComp[] = // text; test for selected components numbers fail
{
"options error: \n"
" invalid first or last component number\n"
" first component number should be lower then last\n"
" and last component number should be lower then number\n"
" component in library\n"
},
TxtErrParamVal[] = // text; incorrect fields select
{
"incorrect parameter value between ':'"
};
int NotifyCallback(int nErrCode, char* message)
{
fprintf(stderr, "%s\n", message);
}
void Exit(char ExitCode)
{
PfWSchLibFreeAllEx(&pPfWLib);
switch (ExitCode)
{
case 0 :
exit(0);
case 1 :
exit(1);
case 'h' :
fprintf(stdout, "\nPro*el to gEDA schema symbol converter %s\n\n%s",
FileVersion, TxtHelp);
exit(0);
default:
exit(1);
}
}
void ListLibComponents() // list all library components
{
int i;
fprintf(stdout, "\nLibrary name: %s\nNumber Components: %d\n\n",
pPfWLib->LibraryName, pPfWLib->pLibV1->pComps->RealNumComp);
for(i = 0; i < pPfWLib->pLibV1->pComps->RealNumComp; i++)
{
fprintf(stdout, "%4d parts:%2d name: %s\n", i+1,
pPfWLib->pLibV1->pComps->pComp[i]->pParts->RealNumParts,
pPfWLib->pLibV1->pComps->pComp[i]->ComponentName1);
}
}
int ParseFile(int argc, char* argv[]) // parse library file
{
fprintf(stderr, "Parsing...\n");
PfWSchLibSetCallbackFunction(NotifyCallback);
if (PfWSchLibLoadFileEx(argv[argc-1],&pPfWLib))
{
fprintf(stderr, "Parse error, exit\n");
return 1;
}
else
{
fprintf(stderr, "Parse OK\n");
return 0;
}
}
int TestParam(int LongIndex, int Min, int Max) // test single parameter
{
int ParValue = atoi(optarg);
if (ParValue < Min)
{
fprintf(stderr,
"PARAM ERROR: invalid value: --%s=%d\n"
" parameter should be greater then %d\n",
LongParameter[LongIndex].name, ParValue, Min);
return 1;
}
if (ParValue > Max)
{
fprintf(stderr,
"PARAM ERROR: invalid value: --%s=%d\n"
" parameter should be lower then %d\n",
LongParameter[LongIndex].name, ParValue, Max);
return 1;
}
fprintf(stderr, "%s=%d\n", LongParameter[LongIndex].name, ParValue);
return 0;
}
int GetOptParValue()
{
/* function read positive value from optarg
* character by character
* return:
* value from string have optarg start point or
* -1 if no more value in optarg are to read
*
* warning:
* function destroy optarg pointer
*/
char Str[] = {0,0,0}; // one of parameter value as string
if (*optarg == ':') ++optarg; // skip ":"
if ((*optarg == '=') || (*optarg == 0)) return -1; // it is end optarg
Str[0] = *optarg; // read first value character
++optarg; // set pointer to second character
if ((*optarg == '=') || (*optarg == 0) || (*optarg == ':'))
return atoi(Str);
else // it's second value character
Str[1] = *optarg;
++optarg; // get next char (should be ":" or "=" or "0")
if ((*optarg == '=') || (*optarg == 0) || (*optarg == ':'))
return atoi(Str);
// strange string in options
fprintf(stderr,"%s\n", TxtErrParamVal);
return -2;
}
int GetOptions(int argc, char* argv[]) // read option & set parameters
{
int Option; // short option
int Arg = 0; // option argument value
int LongIndex;
char **pExtraAttr = ExtraAttr; // pointer to extra attributes string
if (argc<2) return -1; // no parameters
while ((Option = getopt_long(argc, argv,
ShortParameters, LongParameter, &LongIndex)) != -1)
{
/* search option LongIndex
* for short options getopt_long don't return LongIndex
* required to print information about option
*/
if (Option == '?') // next used 'while' fuse
{ fprintf(stderr, "invalid parameter\n"); return 1; }
LongIndex = 0; while (LongParameter[LongIndex].val != Option) ++LongIndex;
switch (Option)
{
case 'h' :
return 'h'; // info to exit program
case 'l' :
fprintf(stderr, "list components\n");
return 'l';
case 'f' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
FirstComp = atoi(optarg); break;
case 'e' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
LastComp = atoi(optarg); break;
case 's' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
XYmultip = atoi(optarg); break;
case 'w' :
SetLineWidth = YES;
fprintf(stderr, "set line width ON\n"); break;
case 'A' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
LineWidth0 = atoi(optarg); break;
case 'B' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
LineWidth1 = atoi(optarg); break;
case 'C' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
LineWidth2 = atoi(optarg); break;
case 'D' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
LineWidth3 = atoi(optarg); break;
case 'E' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
DotSymDia = atoi(optarg); break;
case 'F' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
DotSymLine = atoi(optarg); break;
case 'G' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
ClkSymWidth = atoi(optarg); break;
case 'H' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
ClkSymHigh = atoi(optarg); break;
case 'I' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
ClkSymLine = atoi(optarg); break;
case 'J' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
PinNumHoffset = atoi(optarg); break;
case 'K' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
PinNumVoffset = atoi(optarg); break;
case 'L' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
PinNumTxtSize = atoi(optarg); break;
case 'M' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
PinLabHoffset = atoi(optarg); break;
case 'N' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
PinLabVoffset = atoi(optarg); break;
case 'O' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
PinLabTxtSize = atoi(optarg); break;
case 'P' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
PinTypHoffset = atoi(optarg); break;
case 'Q' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
PinTypVoffset = atoi(optarg); break;
case 'R' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
PinTypTxtSize = atoi(optarg); break;
case 'S' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
PinSeqHoffset = atoi(optarg); break;
case 'T' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
PinSeqVoffset = atoi(optarg); break;
case 'U' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
PinSeqTxtSize = atoi(optarg); break;
case 'p' :
SetScalePins = YES;
fprintf(stderr, "scale length of pins ON\n"); break;
case 'W' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
ElArcAproxLev = atoi(optarg); break;
case 'X' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
EllipAproxLev = atoi(optarg); break;
case 'Y' :
if (TestParam(LongIndex, 1, 100)) return 1;
ElliToCirTole = atoi(optarg); break;
case 't' :
if (TestParam(LongIndex, 1, 0xff)) return 1;
BezierLines = atoi(optarg); break;
case 'Z' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
AttrHoffset = atoi(optarg); break;
case 'a' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
AttrVoffset = atoi(optarg); break;
case 'b' :
if (TestParam(LongIndex, 1, 0xffff)) return 1;
AttrTxtSize = atoi(optarg); break;
case 'c' :
fprintf(stderr, "write value attribute ON\n");
WrValue = YES; break;
case 'd' :
while ((Arg = GetOptParValue()) != -1 )
{
switch (Arg)
{
case 2 : WrFootpr2 = YES; break;
case 3 : WrFootpr3 = YES; break;
case 4 : WrFootpr4 = YES; break;
default : fprintf(stderr,
"PARAM ERROR: incorrect footprint number %d\n"
" footprint should be 2 3 or 4 and separate by \":\"\n"
" (footprint 1 always have to write to symbol -\n"
" - it's obligatory in gEDA)\n", Arg);
return 1; // exit
}
fprintf(stderr, "footprint %d = ON\n", Arg);
}
break;
case 'j' :
while ((Arg = GetOptParValue()) != -1 )
{
switch (Arg)
{
case 1 : WrField1 = YES; break;
case 2 : WrField2 = YES; break;
case 3 : WrField3 = YES; break;
case 4 : WrField4 = YES; break;
case 5 : WrField5 = YES; break;
case 6 : WrField6 = YES; break;
case 7 : WrField7 = YES; break;
case 8 : WrField8 = YES; break;
default : fprintf(stderr,
"PARAM ERROR: incorrect field number %d\n"
" field numbers should be 1..8 and separate by \":\"\n", Arg);
return 1; // exit
}
fprintf(stderr, "field %d = ON\n", Arg);
}
break;
case 'k' :
while ((Arg = GetOptParValue()) != -1 )
{
switch (Arg)
{
case 1 : WrPrField1 = YES; break;
case 2 : WrPrField2 = YES; break;
case 3 : WrPrField3 = YES; break;
case 4 : WrPrField4 = YES; break;
case 5 : WrPrField5 = YES; break;
case 6 : WrPrField6 = YES; break;
case 7 : WrPrField7 = YES; break;
case 8 : WrPrField8 = YES; break;
case 9 : WrPrField9 = YES; break;
case 10 : WrPrField10 = YES; break;
case 11 : WrPrField11 = YES; break;
case 12 : WrPrField12 = YES; break;
case 13 : WrPrField13 = YES; break;
case 14 : WrPrField14 = YES; break;
case 15 : WrPrField15 = YES; break;
case 16 : WrPrField16 = YES; break;
default : fprintf(stderr,
"PARAM ERROR: incorrect part field number %d\n"
" part field numbers should be 1..16 and separate by \":\"\n", Arg);
return 1; // exit
}
fprintf(stderr, "part field %d = ON\n", Arg);
}
break;
case 'r' :
*pExtraAttr = optarg;
fprintf(stderr, "extra attributes: %s\n", *pExtraAttr);
++pExtraAttr; break;
}
}
return 0;
}
int ChooseLineWidth(PRIMITIVE * pPrShort) // shoes primitives line width
{
int LineWidth;
switch (pPrShort->LineWidthOrRotation)
{
case 0 : LineWidth=LineWidth0; break;
case 1 : LineWidth=LineWidth1; break;
case 2 : LineWidth=LineWidth2; break;
case 3 : LineWidth=LineWidth3; break;
}
return LineWidth;
}
float CalcCirAngle(float Angle, float RadiusX, float RadiusY) // scale ellipse arc to circle arc
{
/* scale ellipse arc (in degrees) to circle arc
* and change value returned by arc tangent function
* from -/+ PI/2 to value between 0 and 2PI
*
* ellipse equations:
* http://en.wikipedia.org/wiki/Ellipse
* below use equations:
* tg(CirAngle) = (ScaleY/ScaleX) * tg(ElliAngle)
*/
float ScaleYX = RadiusX/RadiusY;
if (Angle == 0)
return (0);
else if (Angle == 90)
return (PI/2);
else if (Angle == 180)
return (PI);
else if (Angle == 270)
return (1.5*PI);
else if ((Angle > 0) && (Angle < 90))
return (atanf(ScaleYX * tanf(Angle*PI/180)));
else if ((Angle > 90) && (Angle < 270))
return (atanf(ScaleYX * tanf(Angle*PI/180)) + PI);
else if ((Angle > 270) && (Angle < 360))
return (atanf(ScaleYX * tanf(Angle*PI/180)) + 2*PI);
}
// DRAW COMPONENTS PRIMITIVES & ATTRIBUTES PROCEDURES
// ---- ---------- ---------- - ---------- ----------
int DrawRectangle(PRIMITIVE * pPrShort) // write Rectangle declaration to file
{
/* gEDA box object fields:
* field: value:
* type B
* x
* y
* width
* height
* color 3
* width
* capstyle 0
* dashtype 0
* dashlength -1
* dashspace -1
* filltype 0
* fillwidth -1
* angle1 -1
* pitch1 -1
* angle2 -1
* pitch2 -1
*/
int X = XYmultip*((pPrShort->X1orCenterX < pPrShort->X2orPinLength) ?
pPrShort->X1orCenterX : pPrShort->X2orPinLength);
int Y = XYmultip*((pPrShort->Y1orCenterY < pPrShort->Y2orPinAttributes) ?
pPrShort->Y1orCenterY : pPrShort->Y2orPinAttributes);
int Width = XYmultip*abs(pPrShort->X1orCenterX - pPrShort->X2orPinLength);
int Height = XYmultip*abs(pPrShort->Y1orCenterY - pPrShort->Y2orPinAttributes);
int LineWidth = (SetLineWidth) ? ChooseLineWidth(pPrShort) : 0;
// write box
fprintf(OUTPUT, "B %d %d %d %d 3 %d 0 0 -1 -1 0 -1 -1 -1 -1\n",
X, Y, Width, Height, LineWidth);
// Y coordinate correction for write text attributes
if (MaxY < (Y + Height)) MaxY = Y + Height;
return 0;
}
int DrawEllipse(PRIMITIVE * pPrShort) // write Ellipse declaration to file
{
/* ellipse is drawing as lines approximation in first quarter
* and mirrored to 3 last quarters;
* elementary angle for approximation is equal (PI/2)/EllipAproxLev
*/
int LineWidth = (SetLineWidth) ? ChooseLineWidth(pPrShort) : 0;
int CenterX = XYmultip*(pPrShort->X1orCenterX);
int CenterY = XYmultip*(pPrShort->Y1orCenterY);
int RadiusX = XYmultip*(pPrShort->RadiusXorNrPoints);
int RadiusY = XYmultip*(pPrShort->RadiusYorLineType);
char AngleCounter;
char SignX, SignY; // +1 or -1
float Angle = PI/4/EllipAproxLev; // start at 1/2 elementary angle
float X1, Y1, X2, Y2; // line points coordinates
// if ellipse is circle - draw circle & exit
if (200*(RadiusX-RadiusY)/(RadiusX+RadiusY) <= ElliToCirTole)
{
fprintf(OUTPUT, "V %d %d %d 3 %d 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",
CenterX, CenterY, RadiusX, LineWidth);
return 0;
}
// draw first line & mirror it
// (first line can be mirrored only by X or only by Y axis)
X1 = RadiusX * cosf(Angle);
Y1 = RadiusY * sinf(Angle);
for (SignX = -1; SignX < 2; SignX += 2)
fprintf(OUTPUT, "L %d %d %d %d 3 %d 0 0 -1 -1\n",
(int)(SignX*X1 + CenterX), (int)( SignX*Y1 + CenterY),
(int)(SignX*X1 + CenterX), (int)(-SignX*Y1 + CenterY),
LineWidth);
X1 = RadiusX * cosf(PI/2-Angle);
Y1 = RadiusY * sinf(PI/2-Angle);
for (SignX = -1; SignX < 2; SignX += 2)
fprintf(OUTPUT, "L %d %d %d %d 3 %d 0 0 -1 -1\n",
(int)( SignX*X1 + CenterX), (int)(SignX*Y1 + CenterY),
(int)(-SignX*X1 + CenterX), (int)(SignX*Y1 + CenterY),
LineWidth);
// draw lines up to PI/2 & mirror it
for (AngleCounter=1; AngleCounter < EllipAproxLev; ++AngleCounter)
{
// calculate line coordinates
X1 = RadiusX * cosf(Angle);
Y1 = RadiusY * sinf(Angle);
Angle += PI/2/EllipAproxLev;
X2 = RadiusX * cosf(Angle);
Y2 = RadiusY * sinf(Angle);
for (SignX = -1; SignX < 2; SignX += 2)
{
for (SignY = -1; SignY < 2; SignY += 2)
{
// draw approximated ellipse lines
fprintf(OUTPUT, "L %d %d %d %d 3 %d 0 0 -1 -1\n",
(int)(SignX*X1 + CenterX), (int)(SignY*Y1 + CenterY),
(int)(SignX*X2 + CenterX), (int)(SignY*Y2 + CenterY),
LineWidth);
}
}
}
// Y coordinate correction for write text attributes
if (MaxY < (CenterY+RadiusY)) MaxY = CenterY+RadiusY;
return 0;
}
int DrawArc(PRIMITIVE * pPrShort) // write Arc declaration to file
{
/*
* gEDA arc object fields:
* field: value:
* type A
* x
* y
* radius
* startangle
* sweepangle
* color 3
* width
* capstyle 0
* dashtype 0
* dashlength -1
* dashspace -1
*/
int X = XYmultip*(pPrShort->X1orCenterX);
int Y = XYmultip*(pPrShort->Y1orCenterY);
int Radius = XYmultip*(pPrShort->RadiusXorNrPoints);
int StartAngle = pPrShort->StartAngle;
int EndAngle = pPrShort->EndAngle;
int SweepAngle = (StartAngle < EndAngle) ?
EndAngle - StartAngle : 360 + EndAngle - StartAngle;
int LineWidth = (SetLineWidth) ? ChooseLineWidth(pPrShort) : 0;
fprintf(OUTPUT, "A %d %d %d %d %d 3 %d 0 0 -1 -1\n",
X, Y, Radius, StartAngle, SweepAngle, LineWidth);
// Y coordinate correction for write text attributes
if (MaxY < (Y+Radius)) MaxY = Y+Radius;
return 0;
}
int DrawEllipticalArc(PRIMITIVE * pPrShort) // write EllipticalArc aprox. by lines
{
/* elliptical arc is approximated by lines;
* first, elliptical arcs are rescaled to circle,
* next, arcs are approximated by lines
* and rescaled down to elliptical arc
*/
int LineWidth = (SetLineWidth) ? ChooseLineWidth(pPrShort) : 0;
int CenterX = XYmultip*(pPrShort->X1orCenterX);
int CenterY = XYmultip*(pPrShort->Y1orCenterY);
float RadiusX = XYmultip*(pPrShort->RadiusXorNrPoints);
float RadiusY = XYmultip*(pPrShort->RadiusYorLineType);
/* if elliptical arc is arc - draw arc & exit
* elliptical and arc have same parameters
* because of this DrawArc function is used
*/
if (200*(RadiusX-RadiusY)/(RadiusX+RadiusY) <= ElliToCirTole)
return DrawArc(pPrShort);
float StartAngle = (pPrShort->StartAngle > 0) ? // in degrees
pPrShort->StartAngle :
pPrShort->StartAngle + 360;
while (StartAngle >= 360) StartAngle-=360;
float EndAngle = (pPrShort->EndAngle > 0) ? // in degrees
pPrShort->EndAngle :
pPrShort->EndAngle + 360;
while (EndAngle >= 360) EndAngle-=360;
// scale elliptical arcs to circle arcs (result in radians)
float CirStartAngle = CalcCirAngle(StartAngle, RadiusX, RadiusY);
float CirEndAngle = CalcCirAngle( EndAngle, RadiusX, RadiusY);
if (CirStartAngle > CirEndAngle) CirEndAngle += 2*PI;
// draw lines
float Angle = CirStartAngle;
float NextAngle = Angle + PI/(2*ElArcAproxLev);
while (NextAngle < CirEndAngle)
{
fprintf(OUTPUT, "L %d %d %d %d 3 %d 0 0 -1 -1\n",
(int)(CenterX + (RadiusX * cosf(Angle))), // x1
(int)(CenterY + (RadiusY * sinf(Angle))), // y1
(int)(CenterX + (RadiusX * cosf(NextAngle))), // x2
(int)(CenterY + (RadiusY * sinf(NextAngle))), // y2
LineWidth);
Angle = NextAngle; NextAngle += PI/(2*ElArcAproxLev);
}
// draw last (or only one) line to end point
fprintf(OUTPUT, "L %d %d %d %d 3 %d 0 0 -1 -1\n",
(int)(CenterX + (RadiusX * cosf(Angle))), // x1
(int)(CenterY + (RadiusY * sinf(Angle))), // y1
(int)(CenterX + (RadiusX * cosf(CirEndAngle))), // x2
(int)(CenterY + (RadiusY * sinf(CirEndAngle))), // y2
LineWidth);
// Y coordinate correction for write text attributes
if (MaxY < (CenterY+RadiusY)) MaxY = CenterY+RadiusY;
return 0;
}
int DrawRoundRect(PRIMITIVE * pPrShort) // write round rectangle declaration to file
{
// round rectangle is draw as lines & arc
/* for round radius, smaller is taken
* field: type x1 y1 x2 y2 color width capstyle dashstyle dashlength dashspace
* value: L 3 0 0 -1 -1
*/
char i;
int Radius = XYmultip*((pPrShort->RadiusXorNrPoints < pPrShort->RadiusYorLineType) ?
pPrShort->RadiusXorNrPoints : pPrShort->RadiusYorLineType);
int X1, Y1, X2, Y2; // x,y for lines & arcs
int X1R, Y1R, X2R, Y2R; // x,y for arcs
int LineWidth = (SetLineWidth) ? ChooseLineWidth(pPrShort) : 0;
// will be: X1,Y1 - lower left corner , X2,Y2 - upper right corner
if (pPrShort->X1orCenterX < pPrShort->X2orPinLength)
{
X1 = XYmultip*(pPrShort->X1orCenterX);
X2 = XYmultip*(pPrShort->X2orPinLength);
}
else
{
X1 = XYmultip*(pPrShort->X2orPinLength);