wireframe.pl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/perl
  2. # This script exports experimental G-code for wireframe printing
  3. # (inspired by the brilliant WirePrint concept)
  4. use strict;
  5. use warnings;
  6. BEGIN {
  7. use FindBin;
  8. use lib "$FindBin::Bin/../lib";
  9. use local::lib "$FindBin::Bin/../local-lib";
  10. }
  11. use Getopt::Long qw(:config no_auto_abbrev);
  12. use Slic3r;
  13. use Slic3r::ExtrusionPath ':roles';
  14. use Slic3r::Geometry qw(scale unscale X Y PI);
  15. my %opt = (
  16. step_height => 5,
  17. nozzle_angle => 30,
  18. nozzle_width => 10,
  19. first_layer_height => 0.3,
  20. );
  21. {
  22. my %options = (
  23. 'help' => sub { usage() },
  24. 'output|o=s' => \$opt{output_file},
  25. 'step-height|h=f' => \$opt{step_height},
  26. 'nozzle-angle|a=f' => \$opt{nozzle_angle},
  27. 'nozzle-width|w=f' => \$opt{nozzle_width},
  28. 'first-layer-height=f' => \$opt{first_layer_height},
  29. );
  30. GetOptions(%options) or usage(1);
  31. $opt{output_file} or usage(1);
  32. $ARGV[0] or usage(1);
  33. }
  34. {
  35. # load model
  36. my $model = Slic3r::Model->read_from_file($ARGV[0]);
  37. $model->center_instances_around_point(Slic3r::Pointf->new(100,100));
  38. my $mesh = $model->mesh;
  39. $mesh->translate(0, 0, -$mesh->bounding_box->z_min);
  40. # get slices
  41. my @z = ();
  42. my $z_max = $mesh->bounding_box->z_max;
  43. for (my $z = $opt{first_layer_height}; $z <= $z_max; $z += $opt{step_height}) {
  44. push @z, $z;
  45. }
  46. my @slices = @{$mesh->slice(\@z)};
  47. my $flow = Slic3r::Flow->new(
  48. width => 0.35,
  49. height => 0.35,
  50. nozzle_diameter => 0.35,
  51. bridge => 1,
  52. );
  53. my $config = Slic3r::Config::Print->new;
  54. $config->set('gcode_comments', 1);
  55. open my $fh, '>', $opt{output_file};
  56. my $gcodegen = Slic3r::GCode->new(
  57. enable_loop_clipping => 0, # better bonding
  58. );
  59. $gcodegen->apply_print_config($config);
  60. $gcodegen->set_extruders([0]);
  61. print $fh $gcodegen->set_extruder(0);
  62. print $fh $gcodegen->writer->preamble;
  63. my $e = $gcodegen->writer->extruder->e_per_mm3 * $flow->mm3_per_mm;
  64. foreach my $layer_id (0..$#z) {
  65. my $z = $z[$layer_id];
  66. foreach my $island (@{$slices[$layer_id]}) {
  67. foreach my $polygon (@$island) {
  68. if ($layer_id > 0) {
  69. # find the lower polygon that we want to connect to this one
  70. my $lower = $slices[$layer_id-1]->[0]->contour; # 't was easy, wasn't it?
  71. my $lower_z = $z[$layer_id-1];
  72. {
  73. my @points = ();
  74. # keep all points with strong angles
  75. {
  76. my @pp = @$polygon;
  77. foreach my $i (0..$#pp) {
  78. push @points, $pp[$i-1] if abs($pp[$i-1]->ccw_angle($pp[$i-2], $pp[$i]) - PI) > PI/3;
  79. }
  80. }
  81. $polygon = Slic3r::Polygon->new(@points);
  82. }
  83. #$polygon = Slic3r::Polygon->new(@{$polygon->split_at_first_point->equally_spaced_points(scale $opt{nozzle_width})});
  84. # find vertical lines
  85. my @vertical = ();
  86. foreach my $point (@{$polygon}) {
  87. push @vertical, Slic3r::Line->new($point->projection_onto_polygon($lower), $point);
  88. }
  89. next if !@vertical;
  90. my @points = ();
  91. foreach my $line (@vertical) {
  92. push @points, Slic3r::Pointf3->new(
  93. unscale($line->a->x),
  94. unscale($line->a->y), #))
  95. $lower_z,
  96. );
  97. push @points, Slic3r::Pointf3->new(
  98. unscale($line->b->x),
  99. unscale($line->b->y), #))
  100. $z,
  101. );
  102. }
  103. # reappend first point as destination of the last diagonal segment
  104. push @points, Slic3r::Pointf3->new(
  105. unscale($vertical[0]->a->x),
  106. unscale($vertical[0]->a->y), #))
  107. $lower_z,
  108. );
  109. # move to the position of the first vertical line
  110. print $fh $gcodegen->writer->travel_to_xyz(shift @points);
  111. # extrude segments
  112. foreach my $point (@points) {
  113. print $fh $gcodegen->writer->extrude_to_xyz($point, $e * $gcodegen->writer->get_position->distance_to($point));
  114. }
  115. }
  116. }
  117. print $fh $gcodegen->writer->travel_to_z($z);
  118. foreach my $polygon (@$island) {
  119. #my $polyline = $polygon->split_at_vertex(Slic3r::Point->new_scale(@{$gcodegen->writer->get_position}[0,1]));
  120. my $polyline = $polygon->split_at_first_point;
  121. print $fh $gcodegen->writer->travel_to_xy(Slic3r::Pointf->new_unscale(@{ $polyline->first_point }), "move to first contour point");
  122. foreach my $line (@{$polyline->lines}) {
  123. my $point = Slic3r::Pointf->new_unscale(@{ $line->b });
  124. print $fh $gcodegen->writer->extrude_to_xy($point, $e * unscale($line->length));
  125. }
  126. }
  127. }
  128. }
  129. close $fh;
  130. }
  131. sub usage {
  132. my ($exit_code) = @_;
  133. print <<"EOF";
  134. Usage: wireframe.pl [ OPTIONS ] file.stl
  135. --help Output this usage screen and exit
  136. --output, -o Write to the specified file
  137. --step-height, -h Use the specified step height
  138. --nozzle-angle, -a Max nozzle angle
  139. --nozzle-width, -w External nozzle diameter
  140. EOF
  141. exit ($exit_code || 0);
  142. }
  143. __END__