2011-03-15 06:10:31 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# gmerge - Merge multiple KiCAD Gerber files into one
|
|
|
|
#
|
2017-05-07 20:29:16 +03:00
|
|
|
# Written 2011, 2016 by Werner Almesberger
|
|
|
|
# Copyright 2011, 2016 Werner Almesberger
|
2011-03-15 06:10:31 +02:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
$state = "HDR";
|
|
|
|
|
|
|
|
LINE: while (<>) {
|
|
|
|
if ($state eq "HDR") {
|
|
|
|
if (/^G04 APERTURE LIST/) {
|
|
|
|
$state = "APT";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
$hdr .= $_;
|
|
|
|
$phdr .= $_ unless /^G04/;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ($state eq "APT") {
|
|
|
|
if (/^G04 APERTURE END LIST/) {
|
|
|
|
$state = "CMD";
|
|
|
|
next;
|
|
|
|
}
|
2017-05-07 20:29:16 +03:00
|
|
|
die "unrecognized aperture"
|
|
|
|
unless /^%ADD(\d+)([CRO],(\d|\.)+(X(\d|\.)+)?)\*%/;
|
2011-03-15 06:10:31 +02:00
|
|
|
for (keys %apt) {
|
|
|
|
if ($apt{$_} == $2) {
|
|
|
|
$map{$1} = $_;
|
|
|
|
next LINE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for ($t = $1; defined $apt{$t}; $t++) {}
|
|
|
|
$apt{$t} = $2;
|
|
|
|
$map{$1} = $t;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
die "internal error" unless $state eq "CMD";
|
|
|
|
if (/^M02\*/) {
|
|
|
|
$state = "HDR";
|
|
|
|
if (defined $ghdr) {
|
|
|
|
if ($gphdr ne $phdr) {
|
|
|
|
print STDERR "--- first header ---\n", $gphdr;
|
|
|
|
print STDERR "--- current header ---\n", $phdr;
|
|
|
|
die "incompatible headers";
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$ghdr = $hdr;
|
|
|
|
$gphdr = $phdr;
|
|
|
|
}
|
|
|
|
undef $hdr;
|
|
|
|
undef $phdr;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if (/^G54D(\d+)\*/) {
|
|
|
|
die "undeclared aperture" unless defined $map{$1};
|
|
|
|
$c .= "G54D".$map{$1}."*\n";
|
|
|
|
next;
|
|
|
|
}
|
2011-03-22 15:26:53 +02:00
|
|
|
die "unexpected command \"$_\"" unless /^X-?\d+Y-?\d+D0*[12]\*/;
|
2011-03-15 06:10:31 +02:00
|
|
|
$c .= $_;
|
|
|
|
}
|
|
|
|
|
|
|
|
print $ghdr || die $!;
|
|
|
|
print "G04 APERTURE LIST*\n" || die $!;
|
|
|
|
for (sort keys %apt) {
|
2017-05-07 20:29:16 +03:00
|
|
|
print "\%ADD".$_.$apt{$_}."*%\n" || die $!;
|
2011-03-15 06:10:31 +02:00
|
|
|
}
|
|
|
|
print "G04 APERTURE END LIST*\n" || die $!;
|
|
|
|
print $c || die $!;
|
|
|
|
print "M02*\n" || die $!;
|