2012-03-19 04:05:40 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# fped2d2z.pl - Convert fped 2D stacks into 2.5D paths with Z information
|
|
|
|
#
|
|
|
|
# Written 2012 by Werner Almesberger
|
|
|
|
# Copyright 2012 Werner Almesberger
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
sub usage
|
|
|
|
{
|
2012-08-22 19:51:16 +03:00
|
|
|
print STDERR "usage: $0 [-r|-t Zmax] [Zin=Zout ...] prefix ".
|
|
|
|
"[file ...]\n\n";
|
2012-03-19 20:59:24 +02:00
|
|
|
print STDERR " Zin=Zout replace Zin with Zout\n";
|
2012-08-22 19:51:16 +03:00
|
|
|
print STDERR " -t Zmax set the top of the piece\n";
|
2012-03-19 20:59:24 +02:00
|
|
|
print STDERR " -r reverse Z stacking. Also swaps X and Y.\n";
|
2012-03-19 04:05:40 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-19 20:22:03 +02:00
|
|
|
if ($ARGV[0] eq "-r") {
|
|
|
|
shift @ARGV;
|
|
|
|
$reverse = 1;
|
|
|
|
}
|
2012-08-22 19:51:16 +03:00
|
|
|
if ($ARGV[0] eq "-t") {
|
|
|
|
shift @ARGV;
|
|
|
|
$top = shift @ARGV;
|
|
|
|
}
|
2012-03-19 20:59:24 +02:00
|
|
|
&usage if $ARGV[0] =~ /^-[^0-9]/;
|
|
|
|
|
|
|
|
while ($ARGV[0] =~ /=/) {
|
|
|
|
$map{$`} = $';
|
|
|
|
shift @ARGV;
|
|
|
|
}
|
2012-03-19 20:22:03 +02:00
|
|
|
|
2012-03-19 04:05:40 +02:00
|
|
|
$pfx = shift @ARGV;
|
|
|
|
&usage unless defined $pfx;
|
|
|
|
|
|
|
|
$skip = 1;
|
|
|
|
while (<>) {
|
|
|
|
if (/^# $pfx.*?(\d+(\.\d*)?)\s*$/) {
|
|
|
|
$z = $1;
|
|
|
|
$skip = 0;
|
|
|
|
} elsif (/^# /) {
|
|
|
|
$skip = 1;
|
|
|
|
}
|
|
|
|
next if $skip;
|
2012-03-19 20:22:03 +02:00
|
|
|
$z{$z} .= $_;
|
|
|
|
$zmax = $z if $z > $zmax || !defined $zmax;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($reverse) {
|
|
|
|
for (keys %z) {
|
|
|
|
($t{$zmax-$_} = $z{$_}) =~
|
|
|
|
s/^(-?\d+(\.\d*)?)\s+(-?\d+(\.\d*)?)/$3 $1/mg;
|
|
|
|
}
|
|
|
|
%z = %t;
|
2012-03-21 06:02:43 +02:00
|
|
|
print "0 0 $zmax\n\n";
|
2012-08-22 19:51:16 +03:00
|
|
|
} elsif (defined $top) {
|
|
|
|
for (keys %z) {
|
|
|
|
($t{$top-$_} = $z{$_}) =~
|
|
|
|
s/^(-?\d+(\.\d*)?)\s+(-?\d+(\.\d*)?)/$1 $3/mg;
|
|
|
|
}
|
|
|
|
%z = %t;
|
2012-03-21 06:02:43 +02:00
|
|
|
} else {
|
|
|
|
print "0 0 0\n\n";
|
2012-03-19 20:22:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# sort, to make output easier to examine manually
|
|
|
|
for (sort { $b <=> $a } keys %z) {
|
2012-03-19 20:59:24 +02:00
|
|
|
$s = defined $map{$_} ? $map{$_} : $_;
|
|
|
|
$z{$_} =~ s/\s+-?\d+(\.\d*)?$/$& $s/gm;
|
2012-03-19 20:22:03 +02:00
|
|
|
print $z{$_};
|
2012-03-19 04:05:40 +02:00
|
|
|
}
|