Extruder.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package Slic3r::Extruder;
  2. use Moo;
  3. use Slic3r::ExtrusionPath ':roles';
  4. use Slic3r::Geometry qw(scale unscale);
  5. has 'layer' => (is => 'rw');
  6. has 'shift_x' => (is => 'rw', default => sub {0} );
  7. has 'shift_y' => (is => 'rw', default => sub {0} );
  8. has 'z' => (is => 'rw', default => sub {0} );
  9. has 'speed' => (is => 'rw');
  10. has 'extrusion_distance' => (is => 'rw', default => sub {0} );
  11. has 'elapsed_time' => (is => 'rw', default => sub {0} ); # seconds
  12. has 'total_extrusion_length' => (is => 'rw', default => sub {0} );
  13. has 'retracted' => (is => 'rw', default => sub {1} ); # this spits out some plastic at start
  14. has 'lifted' => (is => 'rw', default => sub {0} );
  15. has 'last_pos' => (is => 'rw', default => sub { Slic3r::Point->new(0,0) } );
  16. has 'last_speed' => (is => 'rw', default => sub {""});
  17. has 'last_fan_speed' => (is => 'rw', default => sub {0});
  18. has 'dec' => (is => 'ro', default => sub { 3 } );
  19. # calculate speeds (mm/min)
  20. has 'speeds' => (
  21. is => 'ro',
  22. default => sub {{
  23. travel => 60 * Slic3r::Config->get('travel_speed'),
  24. perimeter => 60 * Slic3r::Config->get('perimeter_speed'),
  25. small_perimeter => 60 * Slic3r::Config->get('small_perimeter_speed'),
  26. infill => 60 * Slic3r::Config->get('infill_speed'),
  27. solid_infill => 60 * Slic3r::Config->get('solid_infill_speed'),
  28. top_solid_infill => 60 * Slic3r::Config->get('top_solid_infill_speed'),
  29. bridge => 60 * Slic3r::Config->get('bridge_speed'),
  30. retract => 60 * Slic3r::Config->get('retract_speed'),
  31. }},
  32. );
  33. my %role_speeds = (
  34. &EXTR_ROLE_PERIMETER => 'perimeter',
  35. &EXTR_ROLE_SMALLPERIMETER => 'small_perimeter',
  36. &EXTR_ROLE_CONTOUR_INTERNAL_PERIMETER => 'perimeter',
  37. &EXTR_ROLE_FILL => 'infill',
  38. &EXTR_ROLE_SOLIDFILL => 'solid_infill',
  39. &EXTR_ROLE_TOPSOLIDFILL => 'top_solid_infill',
  40. &EXTR_ROLE_BRIDGE => 'bridge',
  41. &EXTR_ROLE_SKIRT => 'perimeter',
  42. &EXTR_ROLE_SUPPORTMATERIAL => 'perimeter',
  43. );
  44. use Slic3r::Geometry qw(points_coincide PI X Y);
  45. sub change_layer {
  46. my $self = shift;
  47. my ($layer) = @_;
  48. $self->layer($layer);
  49. my $z = $Slic3r::z_offset + $layer->print_z * $Slic3r::scaling_factor;
  50. my $gcode = "";
  51. $gcode .= $self->retract(move_z => $z);
  52. $gcode .= $self->G0(undef, $z, 0, 'move to next layer (' . $layer->id . ')')
  53. if $self->z != $z && !$self->lifted;
  54. $gcode .= Slic3r::Config->replace_options($Slic3r::layer_gcode) . "\n"
  55. if $Slic3r::layer_gcode;
  56. return $gcode;
  57. }
  58. sub extrude {
  59. my $self = shift;
  60. if ($_[0]->isa('Slic3r::ExtrusionLoop')) {
  61. $self->extrude_loop(@_);
  62. } else {
  63. $_[0]->deserialize;
  64. $self->extrude_path(@_);
  65. }
  66. }
  67. sub extrude_loop {
  68. my $self = shift;
  69. my ($loop, $description) = @_;
  70. # extrude all loops ccw
  71. $loop->deserialize;
  72. $loop->polygon->make_counter_clockwise;
  73. # find the point of the loop that is closest to the current extruder position
  74. # or randomize if requested
  75. my $last_pos = $self->last_pos;
  76. if ($Slic3r::randomize_start && $loop->role == EXTR_ROLE_CONTOUR_INTERNAL_PERIMETER) {
  77. srand $self->layer->id * 10;
  78. $last_pos = Slic3r::Point->new(scale $Slic3r::print_center->[X], scale $Slic3r::bed_size->[Y]);
  79. $last_pos->rotate(rand(2*PI), $Slic3r::print_center);
  80. }
  81. my $start_at = $loop->nearest_point_to($last_pos);
  82. # split the loop at the starting point and make a path
  83. my $extrusion_path = $loop->split_at($start_at);
  84. $extrusion_path->deserialize;
  85. # clip the path to avoid the extruder to get exactly on the first point of the loop;
  86. # if polyline was shorter than the clipping distance we'd get a null polyline, so
  87. # we discard it in that case
  88. $extrusion_path->clip_end(scale($self->layer ? $self->layer->flow->width : $Slic3r::flow->width) * 0.15);
  89. return '' if !@{$extrusion_path->polyline};
  90. # extrude along the path
  91. return $self->extrude_path($extrusion_path, $description);
  92. }
  93. sub extrude_path {
  94. my $self = shift;
  95. my ($path, $description, $recursive) = @_;
  96. $path->merge_continuous_lines;
  97. # detect arcs
  98. if ($Slic3r::gcode_arcs && !$recursive) {
  99. my $gcode = "";
  100. foreach my $arc_path ($path->detect_arcs) {
  101. $arc_path->deserialize;
  102. $gcode .= $self->extrude_path($arc_path, $description, 1);
  103. }
  104. return $gcode;
  105. }
  106. my $gcode = "";
  107. # retract if distance from previous position is greater or equal to the one
  108. # specified by the user *and* to the maximum distance between infill lines
  109. {
  110. my $distance_from_last_pos = $self->last_pos->distance_to($path->points->[0]) * $Slic3r::scaling_factor;
  111. my $distance_threshold = $Slic3r::retract_before_travel;
  112. $distance_threshold = 2 * ($self->layer ? $self->layer->flow->width : $Slic3r::flow->width) / $Slic3r::fill_density * sqrt(2)
  113. if 0 && $Slic3r::fill_density > 0 && $description =~ /fill/;
  114. if ($distance_from_last_pos >= $distance_threshold) {
  115. $gcode .= $self->retract(travel_to => $path->points->[0]);
  116. }
  117. }
  118. # go to first point of extrusion path
  119. $gcode .= $self->G0($path->points->[0], undef, 0, "move to first $description point")
  120. if !points_coincide($self->last_pos, $path->points->[0]);
  121. # compensate retraction
  122. $gcode .= $self->unretract if $self->retracted;
  123. # calculate extrusion length per distance unit
  124. my $s = $path->flow_spacing || $self->layer ? $self->layer->flow->spacing : $Slic3r::flow->spacing;
  125. my $h = $path->depth_layers * $self->layer->height;
  126. my $w = ($s - ($self->layer ? $self->layer->flow->min_spacing : $Slic3r::flow->min_spacing) * $Slic3r::overlap_factor) / (1 - $Slic3r::overlap_factor);
  127. my $area;
  128. if ($path->role == EXTR_ROLE_BRIDGE) {
  129. $area = ($s**2) * PI/4;
  130. } elsif ($w >= ($Slic3r::nozzle_diameter + $h)) {
  131. # rectangle with semicircles at the ends
  132. $area = $w * $h + ($h**2) / 4 * (PI - 4);
  133. } else {
  134. # rectangle with shrunk semicircles at the ends
  135. $area = $Slic3r::nozzle_diameter * $h * (1 - PI/4) + $h * $w * PI/4;
  136. }
  137. my $e = $Slic3r::scaling_factor
  138. * $area
  139. * $Slic3r::extrusion_multiplier
  140. * (4 / (($Slic3r::filament_diameter ** 2) * PI));
  141. # extrude arc or line
  142. $self->speed( $role_speeds{$path->role} || die "Unknown role: " . $path->role );
  143. my $path_length = 0;
  144. if ($path->isa('Slic3r::ExtrusionPath::Arc')) {
  145. $path_length = $path->length;
  146. $gcode .= $self->G2_G3($path->points->[-1], $path->orientation,
  147. $path->center, $e * $path_length, $description);
  148. } else {
  149. foreach my $line ($path->lines) {
  150. my $line_length = $line->length;
  151. $path_length += $line_length;
  152. $gcode .= $self->G1($line->b, undef, $e * $line_length, $description);
  153. }
  154. }
  155. if ($Slic3r::cooling) {
  156. my $path_time = unscale($path_length) / $self->speeds->{$self->last_speed} * 60;
  157. if ($self->layer->id == 0) {
  158. $path_time = $Slic3r::first_layer_speed =~ /^(\d+(?:\.\d+)?)%$/
  159. ? $path_time / ($1/100)
  160. : unscale($path_length) / $Slic3r::first_layer_speed * 60;
  161. }
  162. $self->elapsed_time($self->elapsed_time + $path_time);
  163. }
  164. return $gcode;
  165. }
  166. sub retract {
  167. my $self = shift;
  168. my %params = @_;
  169. return "" unless $Slic3r::retract_length > 0
  170. && !$self->retracted;
  171. # prepare moves
  172. $self->speed('retract');
  173. my $retract = [undef, undef, -$Slic3r::retract_length, "retract"];
  174. my $lift = ($Slic3r::retract_lift == 0 || defined $params{move_z})
  175. ? undef
  176. : [undef, $self->z + $Slic3r::retract_lift, 0, 'lift plate during retraction'];
  177. my $gcode = "";
  178. if (($Slic3r::g0 || $Slic3r::gcode_flavor eq 'mach3') && $params{travel_to}) {
  179. if ($lift) {
  180. # combine lift and retract
  181. $lift->[2] = $retract->[2];
  182. $gcode .= $self->G0(@$lift);
  183. } else {
  184. # combine travel and retract
  185. my $travel = [$params{travel_to}, undef, $retract->[2], 'travel and retract'];
  186. $gcode .= $self->G0(@$travel);
  187. }
  188. } elsif (($Slic3r::g0 || $Slic3r::gcode_flavor eq 'mach3') && defined $params{move_z}) {
  189. # combine Z change and retraction
  190. my $travel = [undef, $params{move_z}, $retract->[2], 'change layer and retract'];
  191. $gcode .= $self->G0(@$travel);
  192. } else {
  193. $gcode .= $self->G1(@$retract);
  194. if (defined $params{move_z} && $Slic3r::retract_lift > 0) {
  195. my $travel = [undef, $params{move_z} + $Slic3r::retract_lift, 0, 'move to next layer (' . $self->layer->id . ') and lift'];
  196. $gcode .= $self->G0(@$travel);
  197. $self->lifted(1);
  198. } elsif ($lift) {
  199. $gcode .= $self->G1(@$lift);
  200. }
  201. }
  202. $self->retracted(1);
  203. $self->lifted(1) if $lift;
  204. # reset extrusion distance during retracts
  205. # this makes sure we leave sufficient precision in the firmware
  206. if (!$Slic3r::use_relative_e_distances && $Slic3r::gcode_flavor ne 'mach3') {
  207. $gcode .= "G92 " . $Slic3r::extrusion_axis . "0\n" if $Slic3r::extrusion_axis;
  208. $self->extrusion_distance(0);
  209. }
  210. return $gcode;
  211. }
  212. sub unretract {
  213. my $self = shift;
  214. $self->retracted(0);
  215. my $gcode = "";
  216. if ($self->lifted) {
  217. $gcode .= $self->G0(undef, $self->z - $Slic3r::retract_lift, 0, 'restore layer Z');
  218. $self->lifted(0);
  219. }
  220. $self->speed('retract');
  221. $gcode .= $self->G0(undef, undef, ($Slic3r::retract_length + $Slic3r::retract_restart_extra),
  222. "compensate retraction");
  223. return $gcode;
  224. }
  225. sub set_acceleration {
  226. my $self = shift;
  227. my ($acceleration) = @_;
  228. return "" unless $Slic3r::acceleration;
  229. return sprintf "M201 E%s%s\n",
  230. $acceleration, ($Slic3r::gcode_comments ? ' ; adjust acceleration' : '');
  231. }
  232. sub G0 {
  233. my $self = shift;
  234. return $self->G1(@_) if !($Slic3r::g0 || $Slic3r::gcode_flavor eq 'mach3');
  235. return $self->_G0_G1("G0", @_);
  236. }
  237. sub G1 {
  238. my $self = shift;
  239. return $self->_G0_G1("G1", @_);
  240. }
  241. sub _G0_G1 {
  242. my $self = shift;
  243. my ($gcode, $point, $z, $e, $comment) = @_;
  244. my $dec = $self->dec;
  245. if ($point) {
  246. $gcode .= sprintf " X%.${dec}f Y%.${dec}f",
  247. ($point->x * $Slic3r::scaling_factor) + $self->shift_x,
  248. ($point->y * $Slic3r::scaling_factor) + $self->shift_y; #**
  249. $self->last_pos($point);
  250. }
  251. if (defined $z && $z != $self->z) {
  252. $self->z($z);
  253. $gcode .= sprintf " Z%.${dec}f", $z;
  254. }
  255. return $self->_Gx($gcode, $e, $comment);
  256. }
  257. sub G2_G3 {
  258. my $self = shift;
  259. my ($point, $orientation, $center, $e, $comment) = @_;
  260. my $dec = $self->dec;
  261. my $gcode = $orientation eq 'cw' ? "G2" : "G3";
  262. $gcode .= sprintf " X%.${dec}f Y%.${dec}f",
  263. ($point->x * $Slic3r::scaling_factor) + $self->shift_x,
  264. ($point->y * $Slic3r::scaling_factor) + $self->shift_y; #**
  265. # XY distance of the center from the start position
  266. $gcode .= sprintf " I%.${dec}f J%.${dec}f",
  267. ($center->[X] - $self->last_pos->[X]) * $Slic3r::scaling_factor,
  268. ($center->[Y] - $self->last_pos->[Y]) * $Slic3r::scaling_factor;
  269. $self->last_pos($point);
  270. return $self->_Gx($gcode, $e, $comment);
  271. }
  272. sub _Gx {
  273. my $self = shift;
  274. my ($gcode, $e, $comment) = @_;
  275. my $dec = $self->dec;
  276. # determine speed
  277. my $speed = ($e ? $self->speed : 'travel');
  278. # output speed if it's different from last one used
  279. # (goal: reduce gcode size)
  280. my $append_bridge_off = 0;
  281. if ($speed ne $self->last_speed) {
  282. if ($speed eq 'bridge') {
  283. $gcode = ";_BRIDGE_FAN_START\n$gcode";
  284. } elsif ($self->last_speed eq 'bridge') {
  285. $append_bridge_off = 1;
  286. }
  287. # apply the speed reduction for print moves on bottom layer
  288. my $speed_f = $self->speeds->{$speed};
  289. if ($e && $self->layer->id == 0 && $comment !~ /retract/) {
  290. $speed_f = $Slic3r::first_layer_speed =~ /^(\d+(?:\.\d+)?)%$/
  291. ? ($speed_f * $1/100)
  292. : $Slic3r::first_layer_speed * 60;
  293. }
  294. $gcode .= sprintf " F%.${dec}f", $speed_f;
  295. $self->last_speed($speed);
  296. }
  297. # output extrusion distance
  298. if ($e && $Slic3r::extrusion_axis) {
  299. $self->extrusion_distance(0) if $Slic3r::use_relative_e_distances;
  300. $self->extrusion_distance($self->extrusion_distance + $e);
  301. $self->total_extrusion_length($self->total_extrusion_length + $e);
  302. $gcode .= sprintf " %s%.5f", $Slic3r::extrusion_axis, $self->extrusion_distance;
  303. }
  304. $gcode .= sprintf " ; %s", $comment if $comment && $Slic3r::gcode_comments;
  305. if ($append_bridge_off) {
  306. $gcode .= "\n;_BRIDGE_FAN_END";
  307. }
  308. return "$gcode\n";
  309. }
  310. sub set_tool {
  311. my $self = shift;
  312. my ($tool) = @_;
  313. return $self->retract . sprintf "T%d%s\n", $tool, ($Slic3r::gcode_comments ? ' ; change tool' : '');
  314. }
  315. sub set_fan {
  316. my $self = shift;
  317. my ($speed, $dont_save) = @_;
  318. if ($self->last_fan_speed != $speed || $dont_save) {
  319. $self->last_fan_speed($speed) if !$dont_save;
  320. if ($speed == 0) {
  321. return sprintf "M107%s\n", ($Slic3r::gcode_comments ? ' ; disable fan' : '');
  322. } else {
  323. return sprintf "M106 %s%d%s\n", ($Slic3r::gcode_flavor eq 'mach3' ? 'P' : 'S'),
  324. (255 * $speed / 100), ($Slic3r::gcode_comments ? ' ; enable fan' : '');
  325. }
  326. }
  327. return "";
  328. }
  329. sub set_temperature {
  330. my $self = shift;
  331. my ($temperature, $wait) = @_;
  332. return "" if $wait && $Slic3r::gcode_flavor eq 'makerbot';
  333. my ($code, $comment) = $wait
  334. ? ('M109', 'wait for temperature to be reached')
  335. : ('M104', 'set temperature');
  336. return sprintf "$code %s%d ; $comment\n",
  337. ($Slic3r::gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature;
  338. }
  339. sub set_bed_temperature {
  340. my $self = shift;
  341. my ($temperature, $wait) = @_;
  342. my ($code, $comment) = $wait
  343. ? (($Slic3r::gcode_flavor eq 'makerbot' ? 'M109' : 'M190'), 'wait for bed temperature to be reached')
  344. : ('M140', 'set bed temperature');
  345. return sprintf "$code %s%d ; $comment\n",
  346. ($Slic3r::gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature;
  347. }
  348. 1;