mlztx/cptx |
| 1 | #!/usr/bin/perl |
| 2 | # |
| 3 | # cptx - Copy the content of text fields (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 |
| 18 | "usage: $0 [-i] board_file src_layer src_x src_y layer x y ...\n\n"; |
| 19 | print STDERR |
| 20 | " -i modify board file in place (default: write to stdout)\n"; |
| 21 | exit(1); |
| 22 | } |
| 23 | |
| 24 | |
| 25 | if ($ARGV[0] eq "-i") { |
| 26 | $in_place = 1; |
| 27 | $out = "_tmp"; |
| 28 | shift @ARGV; |
| 29 | } else { |
| 30 | $out = "-"; |
| 31 | } |
| 32 | |
| 33 | &usage if $ARGV[0] =~ /^-/; |
| 34 | &usage unless @ARGV >= 7; |
| 35 | |
| 36 | $f = shift @ARGV; |
| 37 | $src_layer = shift @ARGV; |
| 38 | $src_x = shift @ARGV; |
| 39 | $src_y = shift @ARGV; |
| 40 | |
| 41 | @dst = @ARGV; |
| 42 | &usage if @dst % 3; |
| 43 | |
| 44 | open(FILE, $f) || die "$f: $!"; |
| 45 | LINE: while (<FILE>) { |
| 46 | last if /^\$EndBOARD/; |
| 47 | if (/^\$TEXTPCB/) { |
| 48 | $text = 1; |
| 49 | $t = $_; |
| 50 | undef $te; |
| 51 | undef $po; |
| 52 | undef $de; |
| 53 | next; |
| 54 | } |
| 55 | if (!$text) { |
| 56 | $s .= $_; |
| 57 | next; |
| 58 | } |
| 59 | $t .= $_; |
| 60 | if (/^Te\s/) { |
| 61 | $te = $_; |
| 62 | next; |
| 63 | } |
| 64 | if (/^Po\s/) { |
| 65 | $po = $_; |
| 66 | next; |
| 67 | } |
| 68 | if (/^De\s/) { |
| 69 | $de = $_; |
| 70 | next; |
| 71 | } |
| 72 | die unless /^\$EndTEXTPCB/; |
| 73 | $text = 0; |
| 74 | die unless defined $te; |
| 75 | die unless defined $po; |
| 76 | die unless defined $de; |
| 77 | @po = split(/\s+/, $po); |
| 78 | $x = $po[1]; |
| 79 | $y = $po[2]; |
| 80 | @de = split(/\s+/, $de); |
| 81 | $layer = $de[1]; |
| 82 | if ($layer == $src_layer && $x == $src_x && $y == $src_y) { |
| 83 | die unless $te =~ /"[^"]*"/; |
| 84 | $str = $&; |
| 85 | } |
| 86 | for ($i = 0; $i != @dst; $i += 3) { |
| 87 | if ($layer == $dst[$i] && |
| 88 | $x == $dst[$i+1] && $y == $dst[$i+2]) { |
| 89 | push(@d, $t); |
| 90 | next LINE; |
| 91 | } |
| 92 | } |
| 93 | $s .= $t; |
| 94 | } |
| 95 | close FILE; |
| 96 | |
| 97 | die unless defined $str; |
| 98 | |
| 99 | open(FILE, ">$out") || die "$out: $!"; |
| 100 | print FILE $s || die "$out: $!"; |
| 101 | |
| 102 | for (@d) { |
| 103 | ($tmp = $_) =~ s/"[^"]*"/$str/; |
| 104 | print FILE $tmp || die "$out: $!"; |
| 105 | } |
| 106 | print FILE "\$EndBOARD\n" || die "$out: $!"; |
| 107 | close FILE || die "$out: $!"; |
| 108 | |
| 109 | rename("$out", $f) || die "rename $out $f: $!" if $in_place; |