1
0
mirror of git://projects.qi-hardware.com/cae-tools.git synced 2024-12-23 12:58:58 +02:00
cae-tools/sl2/slicer.pl
2015-09-29 12:09:34 -03:00

178 lines
4.0 KiB
Perl
Executable File

#!/usr/bin/perl
#
# slicer.pl - Standalone STL to Gnuplot slicer
#
# Written 2015 by Werner Almesberger
# Copyright 2015 by Werner Almesberger
#
# This program//library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
$epsilon = 0.0001; # cutting offset
$height = 10; # height of workpiece
$margin = 5; # margin of workpiece box
$end = 0; # offset to add at the last layer
$flip = 1; # flip piece
$z0 = -35; # reference tool position
$z_step = 1;
#----- Read the STL mesh ------------------------------------------------------
$xmin = $xmax = $ymin = $ymax = $zmin = $zmax = undef;
$v_n = 0;
$e_n = 0;
while (<>) {
if (/\bfacet/) {
undef @f;
next;
}
if (/endfacet/) {
if ($f[2] == $f[5] && $f[2] == $f[8]) {
$z_level{$f[2]} = 1;
} else {
push(@m, [ @f ]);
}
next;
}
if (/vertex\s+/) {
my @tmp = split(/\s+/, $');
($tmp[0], $tmp[2]) = (-$tmp[0], -$tmp[2]) if $flip;
$xmin = $tmp[0] unless defined $xmin && $xmin < $tmp[0];
$xmax = $tmp[0] unless defined $xmax && $xmax > $tmp[0];
$ymin = $tmp[1] unless defined $ymin && $ymin < $tmp[1];
$ymax = $tmp[1] unless defined $ymax && $ymax > $tmp[1];
$zmin = $tmp[2] unless defined $zmin && $zmin < $tmp[2];
$zmax = $tmp[2] unless defined $zmax && $zmax > $tmp[2];
push(@f, @tmp);
next;
}
}
print STDERR "bbox\t($xmin, $ymin, $zmin)\n\t($xmax, $ymax, $zmax)\n";
#----- Calculate Z offset -----------------------------------------------------
# align with bottom (zmin == 0), z_pos = height - zoff
$z_off = $z0 - $zmin - $height;
$z_pos = $height + $zmin;
#----- Perform the slicing ----------------------------------------------------
sub cut
{
local ($z, $a, $b, @f) = @_;
if ($f[$a + 2] < $z && $f[$b + 2] > $z) {
my $dx = $f[$b] - $f[$a];
my $dy = $f[$b + 1] - $f[$a + 1];
my $dz = $f[$b + 2] - $f[$a + 2];
my $f = ($z - $f[$a + 2]) / $dz;
return [ $dx * $f + $f[$a], $dy * $f + $f[$a + 1] ];
}
if ($f[$a + 2] > $z && $f[$b + 2] < $z) {
return &cut($z, $b, $a, @f);
}
return ();
}
sub remove
{
local ($a, $b) = @_;
#print STDERR "\tremove $b from $a (", join(",", @{ $next{$a} }), "\n";
my @tmp = grep($_ ne $b, @{ $next{$a} });
if ($#tmp == -1) {
delete $next{$a};
} else {
$next{$a} = [ @tmp ];
}
}
@z_levels = sort { $b <=> $a } keys %z_level;
for $level (@z_levels) {
my $z_cut = $level + $epsilon;
print STDERR "level $level (cut at $z_cut)\n";
undef %path;
for (@m) {
my @f = @{ $_ };
my @p = &cut($z_cut, 0, 3, @f);
push(@p, &cut($z_cut, 0, 6, @f));
push(@p, &cut($z_cut, 3, 6, @f));
next if $#p < 1;
die "BAD $#p" if $#p > 1;
my $a = "$p[0][0] $p[0][1]";
my $b = "$p[1][0] $p[1][1]";
push(@{ $path{$a} }, $b);
push(@{ $path{$b} }, $a);
# print STDERR "$z: ($a) to ($b)\n";
}
while (1) {
if (defined $z_step) {
$z_pos = $z_pos - $z_step > $level ?
$z_pos - $z_step : $level;
} else {
$z_pos = $level;
}
my $z = $z_pos + $z_off;
$z += $end if $z_pos == $z_levels[$#z_levels];
print STDERR "\t$z_pos @ $z\n";
if (defined $margin) {
print $xmin - $margin, " ", $ymin - $margin, " $z\n";
print $xmax + $margin, " ", $ymin - $margin, " $z\n";
print $xmax + $margin, " ", $ymax + $margin, " $z\n";
print $xmin - $margin, " ", $ymax + $margin, " $z\n";
print $xmin - $margin, " ", $ymin - $margin, " $z\n\n";
}
%next = %path;
while (1) {
my @k = keys %next;
last if $#k == -1;
my $p0 = $k[0];
$p = $p0;
while (1) {
my $next = $next{$p}[0];
print "$p $z\n";
# print STDERR "at $p\n";
# print STDERR "\tnext $next\n";
die "open path" unless defined $next;
&remove($p, $next);
&remove($next, $p);
last if $p0 eq $next;
$p = $next;
}
print "$p0 $z\n";
print "\n";
}
last if $z_pos == $level;
}
}