-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
convertpcb.pl
executable file
·3643 lines (3231 loc) · 116 KB
/
convertpcb.pl
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
#!/usr/bin/perl -w
use strict;
use Compress::Zlib;
use FindBin;
use lib "$FindBin::Bin";
use Math::Geometry::Planar;
use Data::Dumper;
use Cwd qw(abs_path cwd getcwd);
use File::Glob qw(:globally :nocase);
#use Math::Bezier;
# Things that are missing in KiCad:
# Octagonal pads, needed for Arduino designs (converting them to circles can cause overlaps!)
# More than 32 layers, seems not to be a big problem at the moment
# Multi-line Text Frames (Workaround: The text can be rendered by the converter)
# A GND symbol with multiple horizontal lines arranged as a triangle
# Individual colors for single objects like lines, ...
# Ellipse (Workaround: we could approximate them with Polylines)
# Elliptical Arc (Workaround: we could approximate them with Polylines)
# Element Classes (All Top Components, All Bottom Components, HDMI, Power, All Resistors...)
# Board regions for Rigid-Flex
# Support for STEP files
# Rounded Rectangles (Not just ovals that are circles when they are even sided
# Novena had too many netclasses for KiCad <BZR 5406, the NetClass editor in the Design Rules Editor vanished due to GUI layout when there are too many netclasses: https://bugs.launchpad.net/kicad/+bug/1418135
# Loading the 3D viewer is slow, especially when the zones are filled. It only utilizes a single core.
# The 3D view currently has a problem with relative pathes: https://bugs.launchpad.net/kicad/+bug/1417786 a workaround is available with the $absoluteWRLpath option
# Additional paramters in PCBnew for components that could be used for the BOM
# Odd-numbered amount of layers in PCB design (e.g. 5 layers)
# Things that are missing in Designer:
# The Zone-Fill-Polygons are not saved in the file. Workaround: Press "b" in PcbNew or: select the zone tool, right-click on an empty area, then "Fill all zones"
# Annotations in the fileformat
# Todos for this converter:
# Wrong recordtype errors for Regions6
# Correct positioning for Cones, Cylinders, ...
my $annotate=0;
my $absoluteWRLpath=0;
my $wrlprefix=$absoluteWRLpath ? Cwd::cwd() : ".";
our %shownwarnings=();
{
my $ofh = select STDOUT;
$|=1;
select $ofh;
}
my $current_status=<<EOF
Advanced Placer Options6 # Not needed
Arcs6 # Likely Done
Board6 # Here are various global infos about the whole board and about the layers
BoardRegions # Here we likely have only 1 region for Novena. Keepouts are defined here. KiCad does not have a Region concept yet.
Classes6 # NetClasses and ComponentClasses
ComponentBodies6 # Nearly Done
Components6 # Nearly Done
Connections6 # Empty
Coordinates6 # Empty
Design Rule Checker Options6 # To be done later
DifferentialPairs6 # To be done later
Dimensions6 # Annotations about the dimensions
EmbeddedBoards6 # Empty
EmbeddedFonts6 # 2 Fonts, we don´t need them
Embeddeds6 # Empty
ExtendedPrimitiveInformation # Empty
FileVersionInfo # Messages for when the file is opened in older Designer versions that do not support certain features in this fileformat
Fills6 # Needs to be verified, are they really rectangular?
FromTos6 # Empty
Models # Done
ModelsNoEmbed # Empty
Nets6 # Needed
Pads6 # Important
Pin Swap Options6 # Only 1 line, likely not needed
Polygons6 # Done
Regions6 #
Rules6 #
ShapeBasedComponentBodies6 # HALF-Done, do we need more?
ShapeBasedRegions6 # Not needed, I guess
SmartUnions # Empty
Texts # Warnings for older Designer versions, I think we don´t need to support those ;-)
Texts6 # Partly done, NEEDED
Textures # Empty
Tracks6 # Done
Vias6 # Done
WideStrings6 # Seems to be a copy of Texts6, just for Unicode?!?
EOF
;
my $pi=3.14159265359;
my $faktor=39.370078740158;
my $fak="0.39370078740158";
# Truth values
our %alttruth=("0"=>"False","1"=>"True");
our %altrule=("1"=>"Rule","2"=>"Manual");
our %fieldlabels=();
our %bytelabels=();
our %linebreaks=();
sub msubstr # My SubString is a substr function that annotates the fields while substringing them
{
$fieldlabels{$_[0]}{$_[1]}{$_[2]}=$_[3]||"?";
$bytelabels{$_}=$_[3]||"?" foreach($_[1] .. $_[1]+$_[2]-1);
if($_[1]>length($_[0]))
{
my ($package,$filename,$line,$subroutine) = caller(0);
print STDERR "Error: substring out of range: length:".length($_[0])." pos:$_[1] wantedsize:$_[2] field:$_[3] ($package,$filename:$line - $subroutine)\n";
return undef;
}
return substr($_[0],$_[1],$_[2]);
}
sub dumpAnnotatedHex($)
{
my $value=$_[0];
my $content="";
#print "Starting loop:\n";
my $prev="";
foreach(0 .. length($value)-1)
{
$content.="<br/>".(" " x $linebreaks{$_}) if($linebreaks{$_});
my $this=$bytelabels{$_}||"";
my $next=$bytelabels{$_+1}||"";
$content.="<div title='$this $_' style='background-color:".($this?"yellow":"white").";'>" if($prev ne $this);
$content.=sprintf("%02X",unpack("C",substr($value,$_,1)));
$content.= $this eq $next ? " ":"</div> ";
$prev=$this;
}
#print "Done.\n";
$content.="\n<br/><br/>\n";
return $content;
}
# Convert mil to millimeters
sub mil2mm($)
{
return undef unless(defined($_[0]));
my $data=$_[0];
$data=~s/mil$//;
$data/=$faktor;
return (sprintf("%.5f",$data) + 0);
}
# Convert binary mil to millimeter
sub bmil2mm($)
{
return (sprintf("%.5f",unpack("l",$_[0])/$faktor/10000) + 0);
}
# Convert binary mil to ascii mil
sub bmil2($)
{
return (unpack("l",$_[0])/10000)."mil";
}
# Necessary for comments
sub escapeCRLF($)
{
my $d=$_[0];
$d=~s/\n/\\n/gs;
return $d;
}
#Reads a file with one function
sub readfile($)
{
if(open(RFIN,"<$_[0]"))
{
my $old=$/;
undef $/;
binmode RFIN;
my $content=<RFIN>;
$/=$old;
close RFIN;
return($content);
}
return "";
}
#Writes a complete file
sub writefile($$)
{
#print "Writing $_[1] in file $_[0]";
if(open(MYOUT,">$_[0]"))
{
print MYOUT $_[1];
close MYOUT;
}
chmod 0666,$_[0];
}
# This function converts a binary string to its hex representation for debugging
sub bin2hex($)
{
my $orig=$_[0];
my $value="";
return "" if(!defined($orig) || $orig eq "");
foreach(0 .. length($orig)-1)
{
$value.=sprintf("%02X",unpack("C",substr($orig,$_,1)));
}
return $value;
}
# This function converts a binary string to its hex representation for debugging
sub bin2hexLF($)
{
my $orig=$_[0];
my $value="";
return "" if(!defined($orig) || $orig eq "");
foreach(0 .. length($orig)-1)
{
$value.="\n##" if(($_ % 100)==99);
$value.=sprintf("%02X",unpack("C",substr($orig,$_,1)));
}
return $value;
}
# This function returns, whether 2 values are near each other
sub near($$)
{
my $d=0.01;
return ($_[0]>$_[1]-$d && $_[0]<$_[1]+$d);
}
# Are 2 points near each other? near2d(x1,x2,y1,y2)
sub near2d($$$$)
{
my $d=0.0001;
return (sqrt(($_[0]-$_[1])*($_[0]-$_[1])+($_[2]-$_[3])*($_[2]-$_[3]))<$d);
}
# Reformat scientific notation for angles for ascii format
sub enull($)
{
my $d=$_[0];
$d=~s/E\+/E+0/; $d=~s/E\-/E-0/; $d=~s/^/ /;
return $d;
}
# Guess the binary encoding of a given field value
# Alternatively, we might want to output a list of all possible encodings for a given field value
sub EstimateEncoding($)
{
my $v=$_[0];
if($v=~m/-?\d+\.?\d*mil$/)
{
$v=~s/mil$//;
return pack("l",$v*10000);
}
if($v=~m/^ ?(\d+\.\d+E\+\d+)$/)
{
return pack("d",$1);
}
return "\x01" if($v eq "TRUE");
return "\x00" if($v eq "FALSE");
return "\x01" if($v eq "Rule");
return "\x02" if($v eq "Manual");
return pack("C",$v) if($v =~m/^\d{1,3}$/ && $v<256);
return pack("s",$v) if($v =~m/^-?\d{1,4}$/ && $v>=-65536 && $v <65536);
return $v;
}
our $version="";
sub getCRLF($)
{
my $d=$_[0];
return $d if($d=~m/[^\r]\n/);
return $d if(defined($ARGV[0]) && $ARGV[0] eq "CRLF");
$d=~s/\r\n/\n/sg unless($version eq "5.01");
return $d;
}
my %widths=();
open HOUT,">Pads.html";
print HOUT <<EOF
<html>
<head>
<style>
div {display: inline;}
</style>
</head>
<body>
<pre>
EOF
;
open DOUT,">Pads.txt";
our $cutcounter=0;
# The following code reads Gerber Files that were converted to .kicad_pcb for automated reverse engineering below:
my @gerbers=split"\n",readfile('novena_pvt1_e_gerbers\Gerbers.kicad_pcb');
my @g=();
@{$g[0]}=split"\n",readfile('novena_pvt1_e_gerbers\GTL.kicad_pcb');
@{$g[1]}=split"\n",readfile('novena_pvt1_e_gerbers\GP1.kicad_pcb');
@{$g[2]}=split"\n",readfile('novena_pvt1_e_gerbers\G1.kicad_pcb');
@{$g[3]}=split"\n",readfile('novena_pvt1_e_gerbers\G2.kicad_pcb');
@{$g[4]}=split"\n",readfile('novena_pvt1_e_gerbers\GP2.kicad_pcb');
@{$g[5]}=split"\n",readfile('novena_pvt1_e_gerbers\GP3.kicad_pcb');
@{$g[6]}=split"\n",readfile('novena_pvt1_e_gerbers\G3.kicad_pcb');
@{$g[7]}=split"\n",readfile('novena_pvt1_e_gerbers\G4.kicad_pcb');
@{$g[8]}=split"\n",readfile('novena_pvt1_e_gerbers\GP4.kicad_pcb');
@{$g[9]}=split"\n",readfile('novena_pvt1_e_gerbers\GBL.kicad_pcb');
# This is the main handling function that parses most of the binary files inside a .PcbDoc
# It iterates over all records in the given file, and callback´s a given function for every record given
sub HandleBinFile
{
my ($filename,$recordtype,$headerlen,$nskip,$piped)=@_;
# filename is the filename of the file to load
# recordtype is a string that is checked at the beginning of every record, after the header
# headerlen is the length of the header to read and have as callback parameter $_[2] at the beginning of each record
# nskip
# piped is a callback function that gets called with the parameters $piped->(\%d,$data,$header,$line);
my $model=1;
my $content=readfile($filename);
return unless defined($content);
$content=getCRLF($content);
return unless length($content)>4;
my $text="";
my @a=();
my %h=();
print "Writing to $filename.txt\n";
open HBOUT,">$filename.txt";
my $line=0;
my $pos=0;
while($pos<length($content)-4 && $pos>=0)
{
my $header=substr($content,$pos,$headerlen);
$pos+=$headerlen;
my $rtyp=substr($content,$pos,length($recordtype));
if($rtyp ne $recordtype && !($recordtype eq "\x01\x00" && $rtyp=~m/^(\x01|\x03|\x05|\x07|\x08)\x00$/)) # Dimensions have both 01:00 and 05:00 record types
{
print "ERROR: Wrong recordtype: ".bin2hex($rtyp).", expected ".bin2hex($recordtype)." at pos $pos.\n";
if(!$ARGV[0] eq "CRLF")
{
print "The wrong record type could be a new record type, or a decoding error due to wrong CRLF encoding.\n";
print "We are trying again now with a different CRLF encoding.\n";
system "$0 CRLF";
exit;
}
else
{
print "This record type is really unknown. Please contact the developer to add it.\n";
}
last;
}
$pos+=length($recordtype);
print HBOUT sprintf("Pos: %08X ",$pos);
my $len=sprintf("%.5f",unpack("l",substr($content,$pos,4)));
$len-- if($len==0x59917); # Workaround for Regions6 !!! TODO XXX Perhaps it is a CRLF issue?
$pos+=4;
print HBOUT sprintf("len: %08X ",$len);
my $data=substr($content,$pos,$len);
$pos+=$len;
if($data=~m/\n/s)
{
#print "Warning: $filename contains newline in record $line!\n";
}
$data=~s/\x00$//;
if(defined($piped))
{
my @a=split '\|',$data;
my %d=();
foreach my $c(@a)
{
#print "$c\n";
if($c=~m/^([^=]*)=(.*)$/)
{
my $name=$1;
my $value=$2;
$d{$name}=$value;
$h{"LAYER*".$1}{$value}=1 if($name=~m/^LAYER\d+(\w+)$/);
$h{$name}{$value}=1 if($model);
}
}
# CALLING THE CALLBACK FUNCTION TO HANDLE A RECORD:
$piped->(\%d,$data,$header,$line);
}
push @a,"|LINENO=$line|".$data;
$text.=$data."\n";
print HBOUT "|LINENO=$line|".($data=~m/\xff/?bin2hex($data):$data)."\n";
if($nskip)
{
my $nskiptimes=unpack("v",substr($content,$pos,4));
#print "nskip: $nskip nskiptimes: $nskiptimes\n";
$pos+=4+$nskiptimes*$nskip;
}
$line++;
}
if($model)
{
print HBOUT "\nModel:\n";
foreach(sort keys %h)
{
if(scalar(keys %{$h{$_}})>0)
{
print HBOUT "$_={".join(",",sort keys %{$h{$_}})."}\n";
}
}
}
close HBOUT;
}
# Generates the 3D WRL content for a Box
sub Box($$$$$$$$)
{
my ($translation,$rotation,$scale,$color,$shininess,$sx,$sy,$sz)=@_;
# Box("1 1 1","0 0 1 0","1 1 1","0.598039217 0.098039217 0.098039217","1","1","1","1");
return <<EOF
Transform
{
translation $translation
rotation $rotation
scale $scale
scaleOrientation 0 0 1 0
center 0 1 0
children
Shape
{
appearance
Appearance
{
material
Material
{
diffuseColor $color
shininess $shininess
}
}
geometry
IndexedFaceSet
{
coord
Coordinate
{
point [ 0 0 0,
0 0 $sz,
0 $sy $sz,
0 $sy 0,
$sx 0 0,
$sx 0 $sz,
$sx $sy $sz,
$sx $sy 0
]
}
coordIndex [ 3, 1, 2, -1, 0, 1, 3, -1,
5, 7, 6, -1, 5, 4, 7, -1,
4, 5, 0, -1, 0, 5, 1, -1,
6, 7, 3, -1, 6, 3, 2, -1,
3, 7, 0, -1, 0, 7, 4, -1,
6, 2, 1, -1, 6, 1, 5, -1 ]
ccw TRUE
solid FALSE
convex TRUE
}
}
}
EOF
;
}
sub Sphere($$$$$$$)
{
# Needs to be implemented
return "";
}
# Generates the 3D WRL content for an extruded Polygon
sub ExtrudedPolygon($$$$$$$)
{
my ($translation,$rotation,$scale,$color,$shininess,$height,$polygon)=@_;
my @points=@{$polygon};
my $count=0;
my $n=scalar(@points);
my @polyarray=();
my @point=();
my @line=();
foreach(0 .. $n-1)
{
push @point,$points[$_]." 0",$points[$_]." $height";
#push @line,$_*2,(($_+1)%$n)*2,(($_+2)%$n)*2,-1;
push @line,$_*2,$_*2+1,(($_+1)%$n)*2,-1; # Side
push @line,(($_+1)%$n)*2,$_*2+1,(($_+1)%$n)*2+1,-1; # Side
push @polyarray,[$1,$2] if($points[$_]=~m/^(-?\d+\.?\d*)\s+(-?\d+\.?\d*)$/ && ($points[$_] ne $points[($_+1)%$n]));
}
#print Dumper(\@polyarray);
if(scalar(@polyarray)<3)
{
return undef;
}
#print "Count: ".scalar(@polyarray)."\n";
my $poly = Math::Geometry::Planar->new;
$poly->points(\@polyarray);
$poly->cleanup();
#print "Convex: ".$poly->isconvex()."\n";
#print "IsSimple: ".$poly->issimple()."\n";
if(!$poly->isconvex()) # We should also allow concave, non-self-crossing polygons
#if(!$poly->issimple()) # This unfortunately does not work
{
return undef;
}
my @triangles=$poly->triangulate();
#print "Done\n";
my $pos=$n*2;
#print Dumper(\@triangles);
#push @line,-1,-1,-1,-1;
foreach(@triangles)
{
#Bottom:
push @point,"$_->{points}[0][0] $_->{points}[0][1] 0\n","$_->{points}[1][0] $_->{points}[1][1] 0","$_->{points}[2][0] $_->{points}[2][1] 0";
push @line,$pos,$pos+2,$pos+1,-1;
#Top:
push @point,"$_->{points}[0][0] $_->{points}[0][1] $height\n","$_->{points}[1][0] $_->{points}[1][1] $height","$_->{points}[2][0] $_->{points}[2][1] $height";
push @line,$pos+3,$pos+4,$pos+5,-1;
$pos+=6;
}
#exit;
my $points=join ",",@point;
my $lines=join ",",@line;
return <<EOF
Transform
{
translation $translation
rotation $rotation
scale $scale
scaleOrientation 0 0 1 0
center 0 0 0
children
Shape
{
appearance
Appearance
{
material
Material
{
diffuseColor $color
shininess $shininess
}
}
geometry
IndexedFaceSet
{
coord
Coordinate
{
point [ $points ]
}
coordIndex [ $lines ]
ccw TRUE
solid FALSE
convex TRUE
}
}
}
EOF
;
}
# Generates the 3D WRL content for a Cylinder
sub Cylinder($$$$$$$)
{
my ($translation,$rotation,$scale,$color,$shininess,$r,$h)=@_;
my $parts=32;
my @point=();
my @line=();
# Middle bottom point
push @point,"0 0 0";
# Middle top point
push @point,"0 0 $h";
foreach (0 .. $parts-1)
{
push @point,"".sprintf("%.7f",(sin($_*2*$pi/$parts)*$r))." ".sprintf("%.7f",(cos($_*2*$pi/$parts)*$r))." 0";
push @point,"".sprintf("%.7f",(sin($_*2*$pi/$parts)*$r))." ".sprintf("%.7f",(cos($_*2*$pi/$parts)*$r))." $h";
push @line,$_*2+2,(($_+1)%$parts)*2+2,0,-1; # Bottom
push @line,$_*2+3,(($_+1)%$parts)*2+3,1,-1; # Top
push @line,$_*2+2, $_*2+3, (($_+1)%$parts)*2+2,-1; # Side
push @line,$_*2+3,(($_+1)%$parts)*2+3, (($_+1)%$parts)*2+2,-1; # Side
}
my $points=join ",",@point;
my $lines=join ",",@line;
my $h2=$h/2;
return <<EOF
Transform
{
translation $translation
rotation $rotation
scale $scale
scaleOrientation 0 0 1 0
center 0 0 $h2
children
Shape
{
appearance
Appearance
{
material
Material
{
diffuseColor $color
shininess $shininess
}
}
geometry
IndexedFaceSet
{
coord
Coordinate
{
point [ $points ]
}
coordIndex [ $lines ]
ccw TRUE
solid FALSE
convex TRUE
}
}
}
EOF
;
}
# Generates the 3D WRL content for a Cone
sub Cone($$$$$$$)
{
my ($translation,$rotation,$scale,$color,$shininess,$r,$h)=@_;
my $parts=32;
my @point=();
my @line=();
# Middle point
push @point,"0 0 0";
# Top point
push @point,"0 0 $h";
foreach (0 .. $parts-1)
{
push @point,"".sprintf("%.7f",(sin($_*2*$pi/$parts)*$r))." ".sprintf("%.7f",(cos($_*2*$pi/$parts)*$r))." 0";
push @line,$_+2,0,(($_+1)%$parts)+2,-1;
push @line,$_+2,1,(($_+1)%$parts)+2,-1;
}
my $points=join ",",@point;
my $lines=join ",",@line;
return <<EOF
Transform
{
translation $translation
rotation $rotation
scale $scale
scaleOrientation 0 0 1 0
center 0 1 0
children
Shape
{
appearance
Appearance
{
material
Material
{
diffuseColor $color
shininess $shininess
}
}
geometry
IndexedFaceSet
{
coord
Coordinate
{
point [ $points ]
}
coordIndex [ $lines ]
ccw TRUE
solid FALSE
convex TRUE
}
}
}
EOF
;
}
# This marks a specific point for debugging and adds additionial lines around the point that look like a star
sub MarkPoint($$)
{
my $x=$_[0];
my $y=$_[1];
my $size=10;
print OUT "(gr_line (start ".($x-$size)." ".($y-$size).") (end ".($x+$size)." ".($y+$size).") (layer F.SilkS) (width 0.2032))\n";
print OUT "(gr_line (start ".($x+$size)." ".($y-$size).") (end ".($x-$size)." ".($y+$size).") (layer F.SilkS) (width 0.2032))\n";
print OUT "(gr_line (start ".($x)." ".($y-$size).") (end ".($x)." ".($y+$size).") (layer F.SilkS) (width 0.2032))\n";
print OUT "(gr_line (start ".($x-$size)." ".($y).") (end ".($x+$size)." ".($y).") (layer F.SilkS) (width 0.2032))\n";
}
my $USELOGGING=0;
my $layerdoku=<<EOF
KiCad Design with 10 layers:
(layers
(0 F.Cu signal)
(1 In1.Cu signal)
(2 In2.Cu power)
(3 In3.Cu power)
(4 In4.Cu signal)
(5 In5.Cu signal)
(6 In6.Cu signal)
(7 In7.Cu signal)
(8 In8.Cu signal)
(9 In9.Cu signal)
(10 In10.Cu signal)
(11 In11.Cu signal)
(12 In12.Cu signal)
(13 In13.Cu signal)
(31 B.Cu signal)
(32 B.Adhes user)
(33 F.Adhes user)
(34 B.Paste user)
(35 F.Paste user)
(36 B.SilkS user)
(37 F.SilkS user)
(38 B.Mask user)
(39 F.Mask user)
(40 Dwgs.User user)
(41 Cmts.User user)
(42 Eco1.User user)
(43 Eco2.User user)
(44 Edge.Cuts user)
(45 Margin user)
)
Novena:
Layer Name Dielectric Mechanic
name: 1 Top 2 FALSE
name: 2 Mid-Layer.1 0 FALSE
name: 3 L3 2 FALSE
name: 4 L4 1 FALSE
name: 5 Mid-Layer.4 0 FALSE
name: 6 Mid-Layer.5 0 FALSE
name: 7 Mid-Layer.6 0 FALSE
name: 8 Mid-Layer.7 0 FALSE
name: 9 Mid-Layer.8 0 FALSE
name: 10 Mid-Layer.9 0 FALSE
name: 11 L7 2 FALSE
name: 12 L8 1 FALSE
name: 13 Mid-Layer.12 0 FALSE
name: 14 Mid-Layer.13 0 FALSE
name: 15 Mid-Layer.14 0 FALSE
name: 16 Mid-Layer.15 0 FALSE
name: 17 Mid-Layer.16 0 FALSE
name: 18 Mid-Layer.17 0 FALSE
name: 19 Mid-Layer.18 0 FALSE
name: 20 Mid-Layer.19 0 FALSE
name: 21 Mid-Layer.20 0 FALSE
name: 22 Mid-Layer.21 0 FALSE
name: 23 Mid-Layer.22 0 FALSE
name: 24 Mid-Layer.23 0 FALSE
name: 25 Mid-Layer.24 0 FALSE
name: 26 Mid-Layer.25 0 FALSE
name: 27 Mid-Layer.26 0 FALSE
name: 28 Mid-Layer.27 0 FALSE
name: 29 Mid-Layer.28 0 FALSE
name: 30 Mid-Layer.29 0 FALSE
name: 31 Mid-Layer.30 0 FALSE
name: 32 Bot 0 FALSE
name: 33 Top.Overlay 0 FALSE
name: 34 Bottom.Overlay 0 FALSE
name: 35 Top.Paste 0 FALSE
name: 36 Bottom.Paste 0 FALSE
name: 37 Top.Solder 0 FALSE
name: 38 Bottom.Solder 0 FALSE
name: 39 L2-GND 1 FALSE
name: 40 L5-PWR 2 FALSE
name: 41 L6-PWR 1 FALSE
name: 42 L9-GND 2 FALSE
name: 43 Internal.Plane.5 0 FALSE
name: 44 Internal.Plane.6 0 FALSE
name: 45 Internal.Plane.7 0 FALSE
name: 46 Internal.Plane.8 0 FALSE
name: 47 Internal.Plane.9 0 FALSE
name: 48 Internal.Plane.10 0 FALSE
name: 49 Internal.Plane.11 0 FALSE
name: 50 Internal.Plane.12 0 FALSE
name: 51 Internal.Plane.13 0 FALSE
name: 52 Internal.Plane.14 0 FALSE
name: 53 Internal.Plane.15 0 FALSE
name: 54 Internal.Plane.16 0 FALSE
name: 55 Drill.Guide 0 FALSE
name: 56 Keep-Out.Layer 0 FALSE
name: 57 Mechanical.1 0 TRUE
name: 58 Mechanical.2 0 TRUE
name: 59 Mechanical.3 0 TRUE
name: 60 Mechanical.4 0 TRUE
name: 61 Mechanical.5 0 FALSE
name: 62 Mechanical.6 0 FALSE
name: 63 Mechanical.7 0 FALSE
name: 64 Mechanical.8 0 FALSE
name: 65 Mechanical.9 0 FALSE
name: 66 Mechanical.10 0 FALSE
name: 67 Mechanical.11 0 FALSE
name: 68 Mechanical.12 0 FALSE
name: 69 Mechanical.13 0 TRUE
name: 70 Mechanical.14 0 FALSE
name: 71 Mechanical.15 0 TRUE
name: 72 Mechanical.16 0 FALSE
name: 73 Drill.Drawing 0 FALSE
name: 74 Multi-Layer 0 FALSE
name: 75 Connections 0 FALSE
name: 76 Background 0 FALSE
name: 77 DRC.Error.Markers 0 FALSE
name: 78 Selections 0 FALSE
name: 79 Visible.Grid.1 0 FALSE
name: 80 Visible.Grid.2 0 FALSE
name: 81 Pad.Holes 0 FALSE
name: 82 Via.Holes 0 FALSE
EOF
;
our %altlayername=("1"=>"TOP","2"=>"MID1","3"=>"MID2","4"=>"MID3","5"=>"MID4","6"=>"MID5","7"=>"MID6","8"=>"MID7","9"=>"MID8","10"=>"MID9","11"=>"L7","12"=>"L8","32"=>"BOTTOM","33"=>"TOPOVERLAY",
"34"=>"BOTTOMOVERLAY","35"=>"TOPPASTE","36"=>"BOTTOMPASTE",
"37"=>"TOPSOLDER","38"=>"BOTTOMSOLDER","39"=>"PLANE1","40"=>"PLANE2","41"=>"PLANE3","42"=>"PLANE4","43"=>"PLANE5","44"=>"PLANE6",
"55"=>"DRILLGUIDE","56"=>"KEEPOUT","57"=>"MECHANICAL1","58"=>"MECHANICAL2",
"59"=>"MECHANICAL3","60"=>"MECHANICAL4","61"=>"MECHANICAL5","62"=>"MECHANICAL6","63"=>"MECHANICAL7","64"=>"MECHANICAL8","65"=>"MECHANICAL9",
"66"=>"MECHANICAL10","67"=>"MECHANICAL11","68"=>"MECHANICAL12",
"69"=>"MECHANICAL13","70"=>"MECHANICAL14","71"=>"MECHANICAL15","72"=>"MECHANICAL16","73"=>"DRILLDRAWING","74"=>"MULTILAYER");
# At first we read the curated Designer->KiCad standard component mappings:
my %modelhints=();
my %originalhints=();
foreach my $mod(glob('"pretty.pretty/*"'))
{
my $content=readfile($mod);
if($content=~m/\(model ([.\w\/\-]*)\s*(.*)/s)
{
my($model,$value)=($1,$2);
$value=~s/\)\s*$//s;
if(defined($modelhints{$model}))
{
print "Error: Redefining model for model $model hints in file $mod from file $originalhints{$model}.\nPlease delete one of the files\n";
exit;
}
$modelhints{$model}=$value;
$originalhints{$model}=$mod;
#print "\n*$model* =>\n----------\n$value\n------------\n";
}
}
our %result=();
our %cgoodbad=();
our %rawbinary=();
my $trackwidth=10;
# The following are the movement coordinates to align the Novena PCB to Gerber-Imports
my $xmove=95.3;
my $ymove=79.6;
# The following are the movement coordinates to put the board into the normal workspace
#$xmove=50;$ymove=250; # Enable to move Novena mainboard into the frame, or disable to move it to align to the Gerber-Imports
$xmove=0; $ymove=0; # Enable to align GPBB to the Gerber Imports
our %componentatx=();
our %pads=();
our %componentaty=();
our %netnames=("1"=>"Net1");
our $count=0;
sub HandleFill($$$$)
{
my $value=$_[1];
print OUT "#Fills#".escapeCRLF($_[3]).": ".bin2hexLF($value)."\n" if($annotate);
$rawbinary{"Fill"}{$_[3]}=$_[1];
my $component=unpack("s",substr($value,7,2));
assertdata("Fill",$_[3],"COMPONENT",unpack("s",substr($value,7,2))) if($component>=0);
# What can we do with Fills that are parts of Components?
my $layer=mapLayer(unpack("C",substr($value,0,1))) || "Cmts.User";
assertdata("Fill",$_[3],"LAYER",$altlayername{unpack("C",substr($value,0,1))});
my $net=unpack("s",substr($value,3,2))+2;
assertdata("Fill",$_[3],"NET",unpack("s",substr($value,3,2))) if($net>1);
my $netname=$netnames{$net};
my $x1=sprintf("%.5f",-$xmove+bmil2mm(substr($value,13,4)));
assertdata("Fill",$_[3],"X1",bmil2(substr($value,13,4)));
my $y1=sprintf("%.5f",+$ymove-bmil2mm(substr($value,17,4)));
assertdata("Fill",$_[3],"Y1",bmil2(substr($value,17,4)));
my $x2=sprintf("%.5f",-$xmove+bmil2mm(substr($value,21,4)));
assertdata("Fill",$_[3],"X2",bmil2(substr($value,21,4)));
my $y2=sprintf("%.5f",+$ymove-bmil2mm(substr($value,25,4)));
assertdata("Fill",$_[3],"Y2",bmil2(substr($value,25,4)));
my $dir=sprintf('%.5f',unpack("d",substr($value,29,8)));
assertdata("Fill",$_[3],"ROTATION",enull(sprintf("%.14E",unpack("d",substr($value,29,8)))));
my $dir2=sprintf('%.5f',unpack("d",substr($value,38,8))||0);
my $USERROUTED=length($value)>45?unpack("C",substr($value,45,1)):0; # AUTOGENERATED
assertdata("Fill",$_[3],"USERROUTED",uc($alttruth{$USERROUTED}));
#print "Koordinaten:\n";
#print "x:$x1 y:$y1 dir:$dir dir2:$dir2\n";
my $nettext=($net>1)?"(net $net) (net_name \"$netname\")":"";
print OUT <<EOF
(zone $nettext (layer $layer) (tstamp 53EB93DD) (hatch edge 0.508)
(connect_pads (clearance 0.508))
(min_thickness 0.254)
(fill (arc_segments 16) (thermal_gap 0.508) (thermal_bridge_width 0.508))
(polygon
(pts
(xy $x1 $y1) (xy $x2 $y1) (xy $x2 $y2) (xy $x1 $y2)
)
)
)
EOF
;
}
sub HandleTrack($$$$)
{
my $value=$_[1];
print OUT "#Tracks#".escapeCRLF($_[3]).": ".bin2hexLF($value)."\n" if($annotate);
$rawbinary{"Track"}{$_[3]}=$_[1];
my $net=unpack("s",substr($value,3,2))+2;
assertdata("Track",$_[3],"RECORD","Track");
assertdata("Track",$_[3],"INDEXFORSAVE",$_[3]);
assertdata("Track",$_[3],"NET",unpack("s",substr($value,3,2))) if($net>=2);
my $netname=$netnames{$net};
my $component=unpack("s",substr($value,7,2));
assertdata("Track",$_[3],"COMPONENT",unpack("s",substr($value,7,2))) if($component>=0);
my $x1=sprintf("%.5f",-$xmove+bmil2mm(substr($value,13,4)));
assertdata("Track",$_[3],"X1",bmil2(substr($value,13,4)));
my $y1=sprintf("%.5f",+$ymove-bmil2mm(substr($value,17,4)));
assertdata("Track",$_[3],"Y1",bmil2(substr($value,17,4)));
my $x2=sprintf("%.5f",-$xmove+bmil2mm(substr($value,21,4)));
assertdata("Track",$_[3],"X2",bmil2(substr($value,21,4)));
my $y2=sprintf("%.5f",+$ymove-bmil2mm(substr($value,25,4)));
assertdata("Track",$_[3],"Y2",bmil2(substr($value,25,4)));
my $width=bmil2mm(substr($value,29,4));
assertdata("Track",$_[3],"WIDTH",bmil2(substr($value,29,4)));
my $olayer=unpack("C",substr($value,0,1));
my $layer=mapLayer($olayer) || "Cmts.User";