Root/cameo/zstack.pl

1#!/usr/bin/perl
2#
3# zstack.pl - Convert 2.5D paths into a stack with limited Z steps
4#
5# Written 2012 by Werner Almesberger
6# Copyright 2012 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
15sub usage
16{
17    print STDERR "usage: $0 [-r] z-start z-maxstep [file ...]\n";
18    print STDERR " -r reverse Z stacking. Also swaps X and Y.\n";
19    exit(1);
20}
21
22
23sub issue
24{
25    local ($c, $z) = @_;
26    my $t;
27
28    #
29    # The order here is a bit arbitrary. We sort to make the outcome
30    # more predictable but it shouldn't really matter.
31    #
32
33    ($t = $z{$c}) =~ s/\s+-?\d+(\.\d*)?$/ $z/mg;
34    print $t;
35}
36
37
38if ($ARGV[0] eq "-r") {
39    shift @ARGV;
40    $reverse = 1;
41}
42&usage if $ARGV[0] =~ /^-[^0-9]/;
43
44$z0 = shift @ARGV;
45$zs = shift @ARGV;
46
47$z0 =~ s/mm$//;
48$zs =~ s/mm$//;
49
50&usage unless $zs > 0;
51
52while (<>) {
53    if (/^\s*$/) {
54        $z{$z} .= "$s\n";
55        undef $s;
56    }
57    if (/^(-?\d+(\.\d*)?)\s+(-?\d+(\.\d*)?)\s+(-?\d+(\.\d*)?)/) {
58        $x = $1 unless defined $x;
59        $y = $3 unless defined $y;
60        $z = $5;
61    }
62    $s .= $_;
63    $zmax = $z if $z > $zmax || !defined $zmax;
64}
65$z{$z} .= "$s\n";
66
67if ($reverse) {
68    for (keys %z) {
69        ($t{$zmax-$_} = $z{$_}) =~
70            s/^(-?\d+(\.\d*)?)\s+(-?\d+(\.\d*)?)\s+/$3 $1 /mg;
71    }
72    %z = %t;
73}
74
75print "$x $y $z0\n\n";
76
77$z = $z0;
78for $c (sort { $b <=> $a } keys %z) {
79    while ($z > $c+$zs) {
80        $z -= $zs;
81        &issue($c, $z);
82    }
83    $z = $c;
84    &issue($c, $z);
85}
86

Archive Download this file

Branches:
master



interactive