Root/
| 1 | #!/usr/bin/perl |
| 2 | # |
| 3 | # mlztx - Multilayerize Text (in a KiCad board file) |
| 4 | # |
| 5 | # Written 2011 by Werner Almesberger |
| 6 | # Copyright 2011 Werner Almesberger |
| 7 | # |
| 8 | # This program is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License as published by |
| 10 | # the Free Software Foundation; either version 2 of the License, or |
| 11 | # (at your option) any later version. |
| 12 | # |
| 13 | |
| 14 | |
| 15 | sub usage |
| 16 | { |
| 17 | print STDERR "usage: $0 [-i] board_file src_layer layer ...\n\n"; |
| 18 | print STDERR " -i modify board file in place (default: write to ". |
| 19 | "stdout)\n"; |
| 20 | exit(1); |
| 21 | } |
| 22 | |
| 23 | |
| 24 | if ($ARGV[0] eq "-i") { |
| 25 | $in_place = 1; |
| 26 | $out = "_tmp"; |
| 27 | shift @ARGV; |
| 28 | } else { |
| 29 | $out = "-"; |
| 30 | } |
| 31 | |
| 32 | &usage if $ARGV[0] =~ /^-/; |
| 33 | &usage unless @ARGV > 2; |
| 34 | |
| 35 | $f = shift @ARGV; |
| 36 | $src = shift @ARGV; |
| 37 | @dst = @ARGV; |
| 38 | |
| 39 | open(FILE, $f) || die "$f: $!"; |
| 40 | while (<FILE>) { |
| 41 | last if /^\$EndBOARD/; |
| 42 | $s .= $_; |
| 43 | if (/^\$TEXTPCB/) { |
| 44 | $text = 1; |
| 45 | undef $te; |
| 46 | undef $po; |
| 47 | undef $de; |
| 48 | next; |
| 49 | } |
| 50 | next unless $text; |
| 51 | if (/^Te\s/) { |
| 52 | $te = $_; |
| 53 | next; |
| 54 | } |
| 55 | if (/^Po\s/) { |
| 56 | $po = $_; |
| 57 | next; |
| 58 | } |
| 59 | if (/^De\s/) { |
| 60 | $de = $_; |
| 61 | next; |
| 62 | } |
| 63 | die unless /^\$EndTEXTPCB/; |
| 64 | $text = 0; |
| 65 | die unless defined $te; |
| 66 | die unless defined $po; |
| 67 | die unless defined $de; |
| 68 | @po = split(/\s+/, $po); |
| 69 | $x = $po[1]; |
| 70 | $y = $po[2]; |
| 71 | @de = split(/\s+/, $de); |
| 72 | if ($de[1] == $src) { |
| 73 | die if defined $te{"$x $y"}; |
| 74 | $te{"$x $y"} = $te; |
| 75 | $po{"$x $y"} = $po; |
| 76 | $de{"$x $y"} = $de; |
| 77 | } else { |
| 78 | $have{"$x $y"} |= 1 << $de[1]; |
| 79 | } |
| 80 | } |
| 81 | close FILE; |
| 82 | |
| 83 | open(FILE, ">$out") || die "$out: $!"; |
| 84 | print FILE $s || die "$out: $!"; |
| 85 | for $key (keys %te) { |
| 86 | @de = split(/\s+/, $de{$key}); |
| 87 | for (@dst) { |
| 88 | next if $have{"$key"} & (1 << $_);; |
| 89 | @de[1] = $_; |
| 90 | $de = join(" ", @de); |
| 91 | print FILE "\$TEXTPCB\n$te{$key}$po{$key}$de\n\$EndTEXTPCB\n" || |
| 92 | die "$out: $!"; |
| 93 | } |
| 94 | } |
| 95 | print FILE "\$EndBOARD\n" || die "$out: $!"; |
| 96 | close FILE || die "$out: $!"; |
| 97 | |
| 98 | rename("$out", $f) || die "rename $out $f: $!" if $in_place; |
| 99 |
Branches:
master
