5 # Copyright (C) 2003 Dan McMahill
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 # This script is used to renumber devices in a gschem (part of gEDA)
23 # schematic. This program works on single as well as multi-sheet
27 # renum.pl file1.sch [file2.sch [file3.sch ...]]
30 # for parsing input options
36 # don't allow -he to be interpreted as --help
37 $Getopt::Long
::autoabbrev
=0;
39 &GetOptions
(("help" => \
&usage
,
41 "pgskip:i" => \
$pgskip,
42 "verbose" => \
$verbose,
43 "version" => \
&version
46 usage
() if $Getopt::Long
::error
;
48 # set the default increment for sheets
49 unless( $pgskip ) { $pgskip = 100; }
55 # make sure the input schematic exists and we can read it
58 $fname[$i]=shift(@ARGV);
59 die "Schematic file $fname[$i] does not exist or can not be read"
66 for($i=0; $i < $filecnt; $i++) {
67 print "Processing input file #",$i+1,": $fname[$i]\n";
68 open(NETLIST
,"$fname[$i]") or die "Can't open $fname[$i]: $!\n";
71 $outfname="$fname[$i].renum";
72 open(OUTNET
,">$outfname") or die "Can't open $outfname: $!\n";
74 while($line = <NETLIST
>) {
75 unless( $line =~ /^refdes/) {
82 # for lines like "refdes=CR37" pick out the "CR" part.
83 # There's probably a more concise way, but the perl book
84 # is at work right now.
87 $pre =~ s/[0-9\?]*$//;
88 print "Refdes line \"$line\" has pre=\"$pre\"\n" if($verbose);
90 # if we're skipping numbers, then start at 100 for page 1
91 # and we'll jump to 200 for page 2, etc.
92 if( ! $devcnt{$pre} ) { $devcnt{$pre} = $pgskip ?
($i+1)*$pgskip : 0; }
94 print "Renumbering $line to $pre$devcnt{$pre}\n" if($verbose);
95 print OUTNET
"refdes=$pre$devcnt{$pre}\n";
100 # round each element up to the next 100 to help identify what
101 # schematic page a component is on by the refdes.
103 foreach $dev (keys %devcnt) {
104 my $new_cnt = ($i + 2)*$pgskip;
105 if( $new_cnt < $devcnt{$dev} ) {
106 print STDERR
"ERROR: You have more than $pgskip elements with\n";
107 print STDERR
"prefix $dev on this sheet. You will need to either\n";
108 print STDERR
"reduce this number or specify a larger step with the\n";
109 print STDERR
"--pgskip argument.\n";
112 $devcnt{$dev} = $new_cnt;
113 print "Incremented \"$dev\" counter to $devcnt{$dev}\n" if($verbose);
117 # make this an option to not overwrite the original
119 print "Leaving page #",$i+1," output in $outfname\n";
122 system("mv $outfname $fname[$i]");
129 #######################################################################
133 #######################################################################
135 #---------------------------------
138 # prints program usage
139 #---------------------------------
146 print "\t$pname [--nocopy] [--pgskip [number] ] file1 [file2 [file3 ... ] ]\n";
147 print "\t$pname --help\n";
148 print "\t$pname --version\n";
150 print "$pname reads a gschem schematic file or files and renumbers all reference\n";
151 print "designators. The reference designators are numbered starting with 1 and the\n";
152 print "old schematic file is replaced by the modified schematic file.\n";
154 print "$pname accepts the following options:\n";
156 print " --help Displays this help message.\n";
158 print " --nocopy If given, this flag leaves the modified files in new files\n";
159 print " whose names are generated by appending a \".renum\" to the\n";
160 print " original file names. The default is to overwrite the original.\n";
162 print " --pgskip When this flag is used, components on the first schematic sheet\n";
163 print " are numbered starting with 101. On the second sheet, they start\n";
164 print " with 201, etc Specifying a value gives the step between pages.\n";
165 print " For example --pgskip 10 will start with 11, 21, 31, etc.\n";
167 print " --verbose Enables verbose output.\n";
169 print " --version Shows the version of this program.\n";
171 print "Example:\n\n";
172 print "\t$pname mysch.sch\n";
173 print "\t$pname --pgskip pg1.sch pg2.sch pg3.sch\n";
175 print "$pname was written by Dan McMahill <dmcmahill\@netbsd.org>\n";
180 #---------------------------------
183 # prints program version
184 #---------------------------------
187 open(PROG
,"$0") or die "Could not open \"$0\" to find version\n\n";
191 while($line = <PROG
>) {
192 if( $line =~ /^#\s*\$Id.*\$/) {
193 @words = split ' ',,$line;
194 $version = $words[3];
196 print "$pname ($0): Version $version, $date\n";
200 print "Could not determine version of \"$0\"\n\n";
204 # ----------------------------------------------
209 # Revision 1.2 2005-12-21 00:09:56 danmc
210 # - Fix a bug where when using the --pgskip option, components which were
211 # present on page n, but not on pages n+1 through n+j, and present again
212 # on page n+j+1 got numbered in a strange way. For example, J101 on page
213 # 1, no connectors on page 2, J201 on page 3 instead of J301. Noted by
216 # - While here allow the user to change the default increment from 100 to whatever
219 # - Also fix a bug where exactly 101 components with the same refdes prefix
220 # would cause a duplicate refdes on the next page.
222 # Revision 1.1 2003/02/21 03:21:12 ahvezda
223 # Added scripts/refdes_renum written by Dan McMahill