565230c705eb2007f354b8f2eb71e65bb69b97fa
[hw/teledongle] / panel2pcb
1 #!/usr/bin/perl
2 # -*- perl -*-
3
4 # Copyright 2006 DJ Delorie <dj@delorie.com>
5 # Released under the terms of the GNU General Public License, version 2
6
7 if (! @ARGV) {
8     print "Usage: pcb2panel board1.pcb board2.pcb board3.pcb > boards.pcb";
9     print "Then edit boards.pcb, putting each outline where you want it\n";
10     print "and sizing the board.  Then:\n";
11     print "panel2pcb boards.pcb\n";
12     print "and edit/print boards.panel.pcb\n";
13     exit 0;
14 }
15
16 $mydir = $0;
17 if ($mydir =~ m@/@) {
18     $mydir =~ s@[^/]*$@@;
19 } else {
20     $mydir = ".";
21 }
22     require "$mydir/panel.pl";
23
24 $panel = shift;
25
26 open(P, $panel);
27 while (<P>) {
28     if (/PCB\[.* (\d+) (\d+)\]/) {
29         $panel_width = $1;
30         $panel_height = $2;
31     }
32     if (/Element\[\"[^\"]*\" \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" (\d+) (\d+)/) {
33         $pcb = $1;
34         $base = $2;
35         $value = $3;
36         $mx = $4;
37         $my = $5;
38         %pinx = ();
39         %piny = ();
40     }
41     if (/Pin\[([\d-]+) ([\d-]+) \d+ \d+ \d+ \d+ \"(\d)\"/) {
42         $pinx{$3} = $1;
43         $piny{$3} = $2;
44     }
45     if ($pcb && /\)/) {
46         if ($pinx{'1'} < $pinx{'2'}) {
47             $rot = 0;
48         } elsif ($pinx{'1'} > $pinx{'2'}) {
49             $rot = 2;
50         } elsif ($piny{'1'} < $piny{'2'}) {
51             $rot = 3;
52         } elsif ($piny{'1'} > $piny{'2'}) {
53             $rot = 1;
54         }
55         push (@paste, "$pcb\0$rot\0$mx\0$my");
56         $pcb = undef;
57     }
58     if (/Via/) {
59         push (@panelvias, $_);
60     }
61     if (/^Layer\([^)]*\)$/) {
62         $junk = <P>; # The opening '('
63         while ($junk = <P>) {
64             last if $junk =~ /^\)/;
65             push (@panelcopper, $junk);
66         }
67     }
68 }
69
70 $tmp = "/tmp/panel$$.pcb";
71
72 $start = $paste[0];
73 $start =~ s/\0.*//;
74
75 $panel =~ s/\.pcb$//;
76 &baseboard($start, $panel_width, $panel_height, $panel);
77
78 $lastboard = undef;
79 for $paste (sort @paste) {
80     ($pcb, $rot, $mx, $my) = split(/\0/, $paste);
81     if ($lastboard ne $pcb) {
82         &loadboard ($pcb);
83         $lastboard = $pcb;
84         $lastrot = 0;
85     }
86     while ($lastrot != $rot) {
87         print PS "PasteBuffer(Rotate,1)\n";
88         $lastrot = ($lastrot+1) % 4;
89     }
90     print PS "PasteBuffer(ToLayout,$mx,$my)\n";
91 }
92
93 &done();