GCode.pm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. package Slic3r::GCode;
  2. use Moo;
  3. use List::Util qw(min max first);
  4. use Slic3r::ExtrusionLoop ':roles';
  5. use Slic3r::ExtrusionPath ':roles';
  6. use Slic3r::Geometry qw(epsilon scale unscale PI X Y B);
  7. use Slic3r::Geometry::Clipper qw(union_ex);
  8. # Origin of print coordinates expressed in unscaled G-code coordinates.
  9. # This affects the input arguments supplied to the extrude*() and travel_to()
  10. # methods.
  11. has 'origin' => (is => 'rw', default => sub { Slic3r::Pointf->new });
  12. has 'config' => (is => 'ro', default => sub { Slic3r::Config::Full->new });
  13. has 'writer' => (is => 'ro', default => sub { Slic3r::GCode::Writer->new });
  14. has 'placeholder_parser' => (is => 'rw', default => sub { Slic3r::GCode::PlaceholderParser->new });
  15. has 'ooze_prevention' => (is => 'rw', default => sub { Slic3r::GCode::OozePrevention->new });
  16. has 'wipe' => (is => 'rw', default => sub { Slic3r::GCode::Wipe->new });
  17. has 'avoid_crossing_perimeters' => (is => 'rw', default => sub { Slic3r::GCode::AvoidCrossingPerimeters->new });
  18. has 'enable_loop_clipping' => (is => 'rw', default => sub {1});
  19. has 'enable_cooling_markers' => (is =>'rw', default => sub {0});
  20. has 'layer_count' => (is => 'ro');
  21. has '_layer_index' => (is => 'rw', default => sub {-1}); # just a counter
  22. has 'layer' => (is => 'rw');
  23. has '_seam_position' => (is => 'ro', default => sub { {} }); # $object => pos
  24. has 'first_layer' => (is => 'rw', default => sub {0}); # this flag triggers first layer speeds
  25. has 'elapsed_time' => (is => 'rw', default => sub {0} ); # seconds
  26. has 'last_pos' => (is => 'rw', default => sub { Slic3r::Point->new(0,0) } );
  27. sub apply_print_config {
  28. my ($self, $print_config) = @_;
  29. $self->writer->apply_print_config($print_config);
  30. $self->config->apply_print_config($print_config);
  31. }
  32. sub set_extruders {
  33. my ($self, $extruder_ids) = @_;
  34. $self->writer->set_extruders($extruder_ids);
  35. # enable wipe path generation if any extruder has wipe enabled
  36. $self->wipe->enable(defined first { $self->config->get_at('wipe', $_) } @$extruder_ids);
  37. }
  38. sub set_origin {
  39. my ($self, $pointf) = @_;
  40. # if origin increases (goes towards right), last_pos decreases because it goes towards left
  41. my @translate = (
  42. scale ($self->origin->x - $pointf->x),
  43. scale ($self->origin->y - $pointf->y), #-
  44. );
  45. $self->last_pos->translate(@translate);
  46. $self->wipe->path->translate(@translate) if $self->wipe->path;
  47. $self->origin($pointf);
  48. }
  49. sub preamble {
  50. my ($self) = @_;
  51. my $gcode = $self->writer->preamble;
  52. # Perform a *silent* move to z_offset: we need this to initialize the Z
  53. # position of our writer object so that any initial lift taking place
  54. # before the first layer change will raise the extruder from the correct
  55. # initial Z instead of 0.
  56. $self->writer->travel_to_z($self->config->z_offset, '');
  57. return $gcode;
  58. }
  59. sub change_layer {
  60. my ($self, $layer) = @_;
  61. $self->layer($layer);
  62. $self->_layer_index($self->_layer_index + 1);
  63. $self->first_layer($layer->id == 0);
  64. # avoid computing islands and overhangs if they're not needed
  65. if ($self->config->avoid_crossing_perimeters) {
  66. $self->avoid_crossing_perimeters->init_layer_mp(
  67. union_ex([ map @$_, @{$layer->slices} ], 1),
  68. );
  69. }
  70. my $gcode = "";
  71. if (defined $self->layer_count) {
  72. $gcode .= $self->writer->update_progress($self->_layer_index, $self->layer_count);
  73. }
  74. my $z = $layer->print_z + $self->config->z_offset; # in unscaled coordinates
  75. if ($self->config->get_at('retract_layer_change', $self->writer->extruder->id) && $self->writer->will_move_z($z)) {
  76. $gcode .= $self->retract;
  77. }
  78. $gcode .= $self->writer->travel_to_z($z, 'move to next layer (' . $self->layer->id . ')');
  79. return $gcode;
  80. }
  81. sub extrude {
  82. my $self = shift;
  83. $_[0]->isa('Slic3r::ExtrusionLoop')
  84. ? $self->extrude_loop(@_)
  85. : $self->extrude_path(@_);
  86. }
  87. sub extrude_loop {
  88. my ($self, $loop, $description, $speed) = @_;
  89. # make a copy; don't modify the orientation of the original loop object otherwise
  90. # next copies (if any) would not detect the correct orientation
  91. $loop = $loop->clone;
  92. # extrude all loops ccw
  93. my $was_clockwise = $loop->make_counter_clockwise;
  94. # find the point of the loop that is closest to the current extruder position
  95. # or randomize if requested
  96. my $last_pos = $self->last_pos;
  97. if ($self->config->spiral_vase) {
  98. $loop->split_at($last_pos);
  99. } elsif ($self->config->seam_position eq 'nearest' || $self->config->seam_position eq 'aligned') {
  100. # simplify polygon in order to skip false positives in concave/convex detection
  101. my $polygon = $loop->polygon;
  102. my @simplified = @{$polygon->simplify(scale $self->config->get_at('nozzle_diameter', $self->writer->extruder->id)/2)};
  103. # concave vertices have priority
  104. my @candidates = map @{$_->concave_points(PI*4/3)}, @simplified;
  105. # if no concave points were found, look for convex vertices
  106. @candidates = map @{$_->convex_points(PI*2/3)}, @simplified if !@candidates;
  107. # retrieve the last start position for this object
  108. my $obj_ptr = 0;
  109. if (defined $self->layer) {
  110. $obj_ptr = $self->layer->object->ptr;
  111. if (defined $self->_seam_position->{$self->layer->object}) {
  112. $last_pos = $self->_seam_position->{$obj_ptr};
  113. }
  114. }
  115. my $point;
  116. if ($self->config->seam_position eq 'nearest') {
  117. @candidates = @$polygon if !@candidates;
  118. $point = $last_pos->nearest_point(\@candidates);
  119. if (!$loop->split_at_vertex($point)) {
  120. # On 32-bit Linux, Clipper will change some point coordinates by 1 unit
  121. # while performing simplify_polygons(), thus split_at_vertex() won't
  122. # find them anymore.
  123. $loop->split_at($point);
  124. }
  125. } elsif (@candidates) {
  126. my @non_overhang = grep !$loop->has_overhang_point($_), @candidates;
  127. @candidates = @non_overhang if @non_overhang;
  128. $point = $last_pos->nearest_point(\@candidates);
  129. if (!$loop->split_at_vertex($point)) {
  130. $loop->split_at($point);
  131. }
  132. } else {
  133. $point = $last_pos->projection_onto_polygon($polygon);
  134. $loop->split_at($point);
  135. }
  136. $self->_seam_position->{$obj_ptr} = $point;
  137. } elsif ($self->config->seam_position eq 'random') {
  138. if ($loop->role == EXTRL_ROLE_CONTOUR_INTERNAL_PERIMETER) {
  139. my $polygon = $loop->polygon;
  140. my $centroid = $polygon->centroid;
  141. $last_pos = Slic3r::Point->new($polygon->bounding_box->x_max, $centroid->y); #))
  142. $last_pos->rotate(rand(2*PI), $centroid);
  143. }
  144. $loop->split_at($last_pos);
  145. }
  146. # clip the path to avoid the extruder to get exactly on the first point of the loop;
  147. # if polyline was shorter than the clipping distance we'd get a null polyline, so
  148. # we discard it in that case
  149. my $clip_length = $self->enable_loop_clipping
  150. ? scale($self->config->get_at('nozzle_diameter', $self->writer->extruder->id)) * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER
  151. : 0;
  152. # get paths
  153. my @paths = @{$loop->clip_end($clip_length)};
  154. return '' if !@paths;
  155. # apply the small perimeter speed
  156. if ($paths[0]->is_perimeter && $loop->length <= &Slic3r::SMALL_PERIMETER_LENGTH) {
  157. $speed //= $self->config->get_abs_value('small_perimeter_speed');
  158. }
  159. # extrude along the path
  160. my $gcode = join '', map $self->_extrude_path($_, $description, $speed), @paths;
  161. # reset acceleration
  162. $gcode .= $self->writer->set_acceleration($self->config->default_acceleration);
  163. $self->wipe->path($paths[0]->polyline->clone) if $self->wipe->enable; # TODO: don't limit wipe to last path
  164. # make a little move inwards before leaving loop
  165. if ($paths[-1]->role == EXTR_ROLE_EXTERNAL_PERIMETER && defined $self->layer && $self->config->perimeters > 1) {
  166. my $last_path_polyline = $paths[-1]->polyline;
  167. # detect angle between last and first segment
  168. # the side depends on the original winding order of the polygon (left for contours, right for holes)
  169. my @points = $was_clockwise ? (-2, 1) : (1, -2);
  170. my $angle = Slic3r::Geometry::angle3points(@$last_path_polyline[0, @points]) / 3;
  171. $angle *= -1 if $was_clockwise;
  172. # create the destination point along the first segment and rotate it
  173. # we make sure we don't exceed the segment length because we don't know
  174. # the rotation of the second segment so we might cross the object boundary
  175. my $first_segment = Slic3r::Line->new(@$last_path_polyline[0,1]);
  176. my $distance = min(scale($self->config->get_at('nozzle_diameter', $self->writer->extruder->id)), $first_segment->length);
  177. my $point = $first_segment->point_at($distance);
  178. $point->rotate($angle, $last_path_polyline->first_point);
  179. # generate the travel move
  180. $gcode .= $self->travel_to($point, $paths[-1]->role, "move inwards before travel");
  181. }
  182. return $gcode;
  183. }
  184. sub extrude_path {
  185. my ($self, $path, $description, $speed) = @_;
  186. my $gcode = $self->_extrude_path($path, $description, $speed);
  187. # reset acceleration
  188. $gcode .= $self->writer->set_acceleration($self->config->default_acceleration);
  189. return $gcode;
  190. }
  191. sub _extrude_path {
  192. my ($self, $path, $description, $speed) = @_;
  193. $path->simplify(&Slic3r::SCALED_RESOLUTION);
  194. # go to first point of extrusion path
  195. my $gcode = "";
  196. {
  197. my $first_point = $path->first_point;
  198. $gcode .= $self->travel_to($first_point, $path->role, "move to first $description point")
  199. if !defined $self->last_pos || !$self->last_pos->coincides_with($first_point);
  200. }
  201. # compensate retraction
  202. $gcode .= $self->unretract;
  203. # adjust acceleration
  204. {
  205. my $acceleration;
  206. if ($self->config->first_layer_acceleration && $self->first_layer) {
  207. $acceleration = $self->config->first_layer_acceleration;
  208. } elsif ($self->config->perimeter_acceleration && $path->is_perimeter) {
  209. $acceleration = $self->config->perimeter_acceleration;
  210. } elsif ($self->config->bridge_acceleration && $path->is_bridge) {
  211. $acceleration = $self->config->bridge_acceleration;
  212. } elsif ($self->config->infill_acceleration && $path->is_infill) {
  213. $acceleration = $self->config->infill_acceleration;
  214. } else {
  215. $acceleration = $self->config->default_acceleration;
  216. }
  217. $gcode .= $self->writer->set_acceleration($acceleration);
  218. }
  219. # calculate extrusion length per distance unit
  220. my $e_per_mm = $self->writer->extruder->e_per_mm3 * $path->mm3_per_mm;
  221. $e_per_mm = 0 if !$self->writer->extrusion_axis;
  222. # set speed
  223. $speed //= -1;
  224. if ($speed == -1) {
  225. if ($path->role == EXTR_ROLE_PERIMETER) {
  226. $speed = $self->config->get_abs_value('perimeter_speed');
  227. } elsif ($path->role == EXTR_ROLE_EXTERNAL_PERIMETER) {
  228. $speed = $self->config->get_abs_value('external_perimeter_speed');
  229. } elsif ($path->role == EXTR_ROLE_OVERHANG_PERIMETER || $path->role == EXTR_ROLE_BRIDGE) {
  230. $speed = $self->config->get_abs_value('bridge_speed');
  231. } elsif ($path->role == EXTR_ROLE_FILL) {
  232. $speed = $self->config->get_abs_value('infill_speed');
  233. } elsif ($path->role == EXTR_ROLE_SOLIDFILL) {
  234. $speed = $self->config->get_abs_value('solid_infill_speed');
  235. } elsif ($path->role == EXTR_ROLE_TOPSOLIDFILL) {
  236. $speed = $self->config->get_abs_value('top_solid_infill_speed');
  237. } elsif ($path->role == EXTR_ROLE_GAPFILL) {
  238. $speed = $self->config->get_abs_value('gap_fill_speed');
  239. } else {
  240. die "Invalid speed";
  241. }
  242. }
  243. my $F = $speed * 60; # convert mm/sec to mm/min
  244. if ($self->first_layer) {
  245. $F = $self->config->get_abs_value_over('first_layer_speed', $F/60) * 60;
  246. }
  247. # extrude arc or line
  248. $gcode .= ";_BRIDGE_FAN_START\n" if $path->is_bridge && $self->enable_cooling_markers;
  249. my $path_length = unscale $path->length;
  250. {
  251. my $extruder_offset = $self->config->get_at('extruder_offset', $self->writer->extruder->id);
  252. $gcode .= $path->gcode($self->writer->extruder, $e_per_mm, $F,
  253. $self->origin->x - $extruder_offset->x,
  254. $self->origin->y - $extruder_offset->y, #-
  255. $self->writer->extrusion_axis,
  256. $self->config->gcode_comments ? " ; $description" : "");
  257. if ($self->wipe->enable) {
  258. $self->wipe->path($path->polyline->clone);
  259. $self->wipe->path->reverse;
  260. }
  261. }
  262. $gcode .= ";_BRIDGE_FAN_END\n" if $path->is_bridge && $self->enable_cooling_markers;
  263. $self->last_pos($path->last_point);
  264. if ($self->config->cooling) {
  265. my $path_time = $path_length / $F * 60;
  266. $self->elapsed_time($self->elapsed_time + $path_time);
  267. }
  268. return $gcode;
  269. }
  270. sub travel_to {
  271. my ($self, $point, $role, $comment) = @_;
  272. my $gcode = "";
  273. # Define the travel move as a line between current position and the taget point.
  274. # This is expressed in print coordinates, so it will need to be translated by
  275. # $self->origin in order to get G-code coordinates.
  276. my $travel = Slic3r::Line->new($self->last_pos, $point);
  277. # Skip retraction at all in the following cases:
  278. # - travel length is shorter than the configured threshold
  279. # - user has enabled "Only retract when crossing perimeters" and the travel move is
  280. # contained in a single island of the current layer *and* a single island in the
  281. # upper layer (so that ooze will not be visible)
  282. # - the path that will be extruded after this travel move is a support material
  283. # extrusion and the travel move is contained in a single support material island
  284. if ($travel->length < scale $self->config->get_at('retract_before_travel', $self->writer->extruder->id)
  285. || ($self->config->only_retract_when_crossing_perimeters
  286. && $self->config->fill_density > 0
  287. && defined($self->layer) && $self->layer->any_internal_region_slice_contains_line($travel))
  288. || (defined $role && $role == EXTR_ROLE_SUPPORTMATERIAL && $self->layer->support_islands->contains_line($travel))
  289. ) {
  290. # Just perform a straight travel move without any retraction.
  291. $gcode .= $self->writer->travel_to_xy($self->point_to_gcode($point), $comment);
  292. } elsif ($self->config->avoid_crossing_perimeters && !$self->avoid_crossing_perimeters->disable_once) {
  293. # If avoid_crossing_perimeters is enabled and the disable_once flag is not set
  294. # we need to plan a multi-segment travel move inside the configuration space.
  295. $gcode .= $self->avoid_crossing_perimeters->travel_to($self, $point, $comment);
  296. } else {
  297. # If avoid_crossing_perimeters is disabled or the disable_once flag is set,
  298. # perform a straight move with a retraction.
  299. $gcode .= $self->retract;
  300. $gcode .= $self->writer->travel_to_xy($self->point_to_gcode($point), $comment || '');
  301. }
  302. # Re-allow avoid_crossing_perimeters for the next travel moves
  303. $self->avoid_crossing_perimeters->disable_once(0);
  304. return $gcode;
  305. }
  306. sub retract {
  307. my ($self, $toolchange) = @_;
  308. return "" if !defined $self->writer->extruder;
  309. my $gcode = "";
  310. # wipe (if it's enabled for this extruder and we have a stored wipe path)
  311. if ($self->config->get_at('wipe', $self->writer->extruder->id) && $self->wipe->path) {
  312. $gcode .= $self->wipe->wipe($self, $toolchange);
  313. }
  314. # The parent class will decide whether we need to perform an actual retraction
  315. # (the extruder might be already retracted fully or partially). We call these
  316. # methods even if we performed wipe, since this will ensure the entire retraction
  317. # length is honored in case wipe path was too short.p
  318. $gcode .= $toolchange ? $self->writer->retract_for_toolchange : $self->writer->retract;
  319. $gcode .= $self->writer->reset_e;
  320. $gcode .= $self->writer->lift
  321. if $self->writer->extruder->retract_length > 0 || $self->config->use_firmware_retraction;
  322. return $gcode;
  323. }
  324. sub unretract {
  325. my ($self) = @_;
  326. my $gcode = "";
  327. $gcode .= $self->writer->unlift;
  328. $gcode .= $self->writer->unretract;
  329. return $gcode;
  330. }
  331. # convert a model-space scaled point into G-code coordinates
  332. sub point_to_gcode {
  333. my ($self, $point) = @_;
  334. my $extruder_offset = $self->config->get_at('extruder_offset', $self->writer->extruder->id);
  335. return Slic3r::Pointf->new(
  336. ($point->x * &Slic3r::SCALING_FACTOR) + $self->origin->x - $extruder_offset->x,
  337. ($point->y * &Slic3r::SCALING_FACTOR) + $self->origin->y - $extruder_offset->y, #**
  338. );
  339. }
  340. sub set_extruder {
  341. my ($self, $extruder_id) = @_;
  342. return "" if !$self->writer->need_toolchange($extruder_id);
  343. # if we are running a single-extruder setup, just set the extruder and return nothing
  344. if (!$self->writer->multiple_extruders) {
  345. return $self->writer->toolchange($extruder_id);
  346. }
  347. # prepend retraction on the current extruder
  348. my $gcode = $self->retract(1);
  349. # append custom toolchange G-code
  350. if (defined $self->writer->extruder && $self->config->toolchange_gcode) {
  351. $gcode .= sprintf "%s\n", $self->placeholder_parser->process($self->config->toolchange_gcode, {
  352. previous_extruder => $self->writer->extruder->id,
  353. next_extruder => $extruder_id,
  354. });
  355. }
  356. # if ooze prevention is enabled, park current extruder in the nearest
  357. # standby point and set it to the standby temperature
  358. $gcode .= $self->ooze_prevention->pre_toolchange($self)
  359. if $self->ooze_prevention->enable && defined $self->writer->extruder;
  360. # append the toolchange command
  361. $gcode .= $self->writer->toolchange($extruder_id);
  362. # set the new extruder to the operating temperature
  363. $gcode .= $self->ooze_prevention->post_toolchange($self)
  364. if $self->ooze_prevention->enable;
  365. return $gcode;
  366. }
  367. package Slic3r::GCode::OozePrevention;
  368. use Moo;
  369. use Slic3r::Geometry qw(scale);
  370. has 'enable' => (is => 'rw', default => sub { 0 });
  371. has 'standby_points' => (is => 'rw');
  372. sub pre_toolchange {
  373. my ($self, $gcodegen) = @_;
  374. my $gcode = "";
  375. # move to the nearest standby point
  376. if (@{$self->standby_points}) {
  377. my $last_pos = $gcodegen->last_pos->clone;
  378. $last_pos->translate(scale +$gcodegen->origin->x, scale +$gcodegen->origin->y); #))
  379. my $standby_point = $last_pos->nearest_point($self->standby_points);
  380. $standby_point->translate(scale -$gcodegen->origin->x, scale -$gcodegen->origin->y); #))
  381. $gcode .= $gcodegen->travel_to($standby_point);
  382. }
  383. if ($gcodegen->config->standby_temperature_delta != 0) {
  384. my $temp = defined $gcodegen->layer && $gcodegen->layer->id == 0
  385. ? $gcodegen->config->get_at('first_layer_temperature', $gcodegen->writer->extruder->id)
  386. : $gcodegen->config->get_at('temperature', $gcodegen->writer->extruder->id);
  387. # we assume that heating is always slower than cooling, so no need to block
  388. $gcode .= $gcodegen->writer->set_temperature($temp + $gcodegen->config->standby_temperature_delta, 0);
  389. }
  390. return $gcode;
  391. }
  392. sub post_toolchange {
  393. my ($self, $gcodegen) = @_;
  394. my $gcode = "";
  395. if ($gcodegen->config->standby_temperature_delta != 0) {
  396. my $temp = defined $gcodegen->layer && $gcodegen->layer->id == 0
  397. ? $gcodegen->config->get_at('first_layer_temperature', $gcodegen->writer->extruder->id)
  398. : $gcodegen->config->get_at('temperature', $gcodegen->writer->extruder->id);
  399. $gcode .= $gcodegen->writer->set_temperature($temp, 1);
  400. }
  401. return $gcode;
  402. }
  403. package Slic3r::GCode::Wipe;
  404. use Moo;
  405. use Slic3r::Geometry qw(scale);
  406. has 'enable' => (is => 'rw', default => sub { 0 });
  407. has 'path' => (is => 'rw');
  408. sub wipe {
  409. my ($self, $gcodegen, $toolchange) = @_;
  410. my $gcode = "";
  411. # Reduce feedrate a bit; travel speed is often too high to move on existing material.
  412. # Too fast = ripping of existing material; too slow = short wipe path, thus more blob.
  413. my $wipe_speed = $gcodegen->writer->config->get('travel_speed') * 0.8;
  414. # get the retraction length
  415. my $length = $toolchange
  416. ? $gcodegen->writer->extruder->retract_length_toolchange
  417. : $gcodegen->writer->extruder->retract_length;
  418. if ($length) {
  419. # Calculate how long we need to travel in order to consume the required
  420. # amount of retraction. In other words, how far do we move in XY at $wipe_speed
  421. # for the time needed to consume retract_length at retract_speed?
  422. my $wipe_dist = scale($length / $gcodegen->writer->extruder->retract_speed * $wipe_speed);
  423. # Take the stored wipe path and replace first point with the current actual position
  424. # (they might be different, for example, in case of loop clipping).
  425. my $wipe_path = Slic3r::Polyline->new(
  426. $gcodegen->last_pos,
  427. @{$self->path}[1..$#{$self->path}],
  428. );
  429. #
  430. $wipe_path->clip_end($wipe_path->length - $wipe_dist);
  431. # subdivide the retraction in segments
  432. my $retracted = 0;
  433. foreach my $line (@{$wipe_path->lines}) {
  434. my $segment_length = $line->length;
  435. # Reduce retraction length a bit to avoid effective retraction speed to be greater than the configured one
  436. # due to rounding (TODO: test and/or better math for this)
  437. my $dE = $length * ($segment_length / $wipe_dist) * 0.95;
  438. $gcode .= $gcodegen->writer->set_speed($wipe_speed*60);
  439. $gcode .= $gcodegen->writer->extrude_to_xy(
  440. $gcodegen->point_to_gcode($line->b),
  441. -$dE,
  442. 'retract' . ($gcodegen->enable_cooling_markers ? ';_WIPE' : ''),
  443. );
  444. $retracted += $dE;
  445. }
  446. $gcodegen->writer->extruder->set_retracted($gcodegen->writer->extruder->retracted + $retracted);
  447. }
  448. return $gcode;
  449. }
  450. package Slic3r::GCode::AvoidCrossingPerimeters;
  451. use Moo;
  452. has '_external_mp' => (is => 'rw');
  453. has '_layer_mp' => (is => 'rw');
  454. has 'use_external_mp' => (is => 'rw', default => sub {0});
  455. has 'use_external_mp_once' => (is => 'rw', default => sub {0}); # this flag triggers the use of the external configuration space for avoid_crossing_perimeters for the next travel move
  456. has 'disable_once' => (is => 'rw', default => sub {1}); # this flag disables avoid_crossing_perimeters just for the next travel move
  457. use Slic3r::Geometry qw(scale);
  458. sub init_external_mp {
  459. my ($self, $islands) = @_;
  460. $self->_external_mp(Slic3r::MotionPlanner->new($islands));
  461. }
  462. sub init_layer_mp {
  463. my ($self, $islands) = @_;
  464. $self->_layer_mp(Slic3r::MotionPlanner->new($islands));
  465. }
  466. sub travel_to {
  467. my ($self, $gcodegen, $point, $comment) = @_;
  468. my $gcode = "";
  469. if ($self->use_external_mp || $self->use_external_mp_once) {
  470. $self->use_external_mp_once(0);
  471. # represent $point in G-code coordinates
  472. $point = $point->clone;
  473. my $origin = $gcodegen->origin;
  474. $point->translate(map scale $_, @$origin);
  475. # calculate path (external_mp uses G-code coordinates so we set a temporary null origin)
  476. $gcodegen->set_origin(Slic3r::Pointf->new(0,0));
  477. $gcode .= $self->_plan($gcodegen, $self->_external_mp, $point, $comment);
  478. $gcodegen->set_origin($origin);
  479. } else {
  480. $gcode .= $self->_plan($gcodegen, $self->_layer_mp, $point, $comment);
  481. }
  482. return $gcode;
  483. }
  484. sub _plan {
  485. my ($self, $gcodegen, $mp, $point, $comment) = @_;
  486. my $gcode = "";
  487. my $travel = $mp->shortest_path($gcodegen->last_pos, $point);
  488. # if the path is not contained in a single island we need to retract
  489. $gcode .= $gcodegen->retract
  490. if !$gcodegen->config->only_retract_when_crossing_perimeters
  491. || !$gcodegen->layer->any_internal_region_slice_contains_polyline($travel);
  492. # append the actual path and return
  493. # use G1 because we rely on paths being straight (G0 may make round paths)
  494. $gcode .= join '',
  495. map $gcodegen->writer->travel_to_xy($gcodegen->point_to_gcode($_->b), $comment),
  496. @{$travel->lines};
  497. return $gcode;
  498. }
  499. 1;