Root/cameo/fped2stl.pl

1#!/usr/bin/perl
2#
3# fped2stl.pl - Convert fped 2D stacks to STL meshes
4#
5# Written 2013 by Werner Almesberger
6# Copyright 2013 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
15use POSIX;
16
17sub usage
18{
19    print STDERR "usage: $0 [-x] [-y] prefix [file ...]\n\n";
20    exit(1);
21}
22
23
24sub flush
25{
26    local ($name, $z, $z1, $s) = @_;
27
28    return unless defined $s;
29    $z = sprintf("%e", $z);
30    $z1 = sprintf("%e", $z1);
31print STDERR "$name $z ...\n";
32    $^F = 20;
33    pipe SR, SW;
34    pipe RR, RW;
35    $pid = fork();
36    if (!$pid) {
37        close SW;
38        close RR;
39        $sn = fileno SR;
40        $rn = fileno RW;
41        open(PIPE, "|cameo >/dev/fd/$rn") || die "cameo: $!";
42        print PIPE "gnuplot 0mm /dev/fd/$sn\n";
43        print PIPE "stl\n";
44        close PIPE;
45        exit;
46    }
47    close SR;
48    close RW;
49
50    print SW $s;
51    close SW;
52
53    while (<RR>) {
54        s/cameo/$name/;
55        s/(vertex.*)0\.0*e\+00$/$1$z/ || s/(vertex.*)1\.0*e\+00$/$1$z1/;
56        print;
57    }
58    close RR;
59}
60
61
62while ($ARGV[0] =~ /^-/) {
63    if ($ARGV[0] eq "-x") {
64        $flip_x = 1;
65    } elsif ($ARGV[0] eq "-y") {
66        $flip_y = 1;
67    } elsif ($ARGV[0] =~ /^-[^0-9]/) {
68        last;
69    } else {
70        &usage;
71    }
72    shift;
73}
74
75$pfx = shift @ARGV;
76&usage unless defined $pfx;
77
78$skip = 1;
79while (<>) {
80    if (/^# $pfx(.*?)-(\d+(\.\d*)?)\s*$/) {
81        $z = $2;
82        $name{$z} = $1;
83        undef $s{$z};
84        $skip = 0;
85    } elsif (/^# /) {
86        $skip = 1;
87    }
88    next if $skip;
89    next if /^#/;
90    $s{$z} .= $_;
91    if (/^(-?[0-9]*\.[0-9]*)\s+(-?[0-9]*\.[0-9]*)/) {
92        $xmin = $1 if $1 < $xmin || !defined $xmin;
93        $xmax = $1 if $1 > $xmax || !defined $xmax;
94        $ymin = $2 if $2 < $ymin || !defined $ymin;
95        $ymax = $2 if $2 > $ymax || !defined $ymax;
96    }
97}
98
99for $z (keys %s) {
100    undef $t;
101    for $s (split(/\n/, $s{$z})) {
102        if ($s =~ /^(-?[0-9]*\.[0-9]*)\s+(-?[0-9]*\.[0-9]*)/) {
103            $s = sprintf("%.6f", ($xmax+$xmin)/2-$1)." $2"
104                if $flip_y;
105
106            # re-scan, so that we can flip on both axes
107            die unless
108                $s =~ /^(-?[0-9]*\.[0-9]*)\s+(-?[0-9]*\.[0-9]*)/;
109            $s = "$1 ".(($ymax+$ymin)/2-$2) if $flip_x;
110        }
111        $t .= "$s\n";
112    }
113    $s{$z} = $t;
114}
115
116undef $last;
117for $z (sort { $b <=> $a } keys %s) {
118    &flush($name{$last}, $last, $z, $s{$last}) if defined $last;
119    $last = $z;
120}
121&flush($name{$last}, $last, 0, $s{$last});
122

Archive Download this file

Branches:
master



interactive