Print.pm 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. package Slic3r::Print;
  2. use Moo;
  3. use File::Basename qw(basename fileparse);
  4. use File::Spec;
  5. use List::Util qw(max first);
  6. use Math::ConvexHull::MonotoneChain qw(convex_hull);
  7. use Slic3r::ExtrusionPath ':roles';
  8. use Slic3r::Geometry qw(X Y Z X1 Y1 X2 Y2 MIN PI scale unscale move_points nearest_point);
  9. use Slic3r::Geometry::Clipper qw(diff_ex union_ex intersection_ex offset JT_ROUND JT_SQUARE);
  10. use Time::HiRes qw(gettimeofday tv_interval);
  11. has 'config' => (is => 'rw', default => sub { Slic3r::Config->new_from_defaults }, trigger => 1);
  12. has 'extra_variables' => (is => 'rw', default => sub {{}});
  13. has 'objects' => (is => 'rw', default => sub {[]});
  14. has 'total_extrusion_length' => (is => 'rw');
  15. has 'processing_time' => (is => 'rw');
  16. has 'extruders' => (is => 'rw', default => sub {[]});
  17. has 'regions' => (is => 'rw', default => sub {[]});
  18. has 'support_material_flow' => (is => 'rw');
  19. has 'first_layer_support_material_flow' => (is => 'rw');
  20. has 'has_support_material' => (is => 'lazy');
  21. has 'fill_maker' => (is => 'lazy');
  22. # ordered collection of extrusion paths to build skirt loops
  23. has 'skirt' => (
  24. is => 'rw',
  25. #isa => 'ArrayRef[Slic3r::ExtrusionLoop]',
  26. default => sub { [] },
  27. );
  28. # ordered collection of extrusion paths to build a brim
  29. has 'brim' => (
  30. is => 'rw',
  31. #isa => 'ArrayRef[Slic3r::ExtrusionLoop]',
  32. default => sub { [] },
  33. );
  34. sub BUILD {
  35. my $self = shift;
  36. # call this manually because the 'default' coderef doesn't trigger the trigger
  37. $self->_trigger_config;
  38. }
  39. sub _trigger_config {
  40. my $self = shift;
  41. # store config in a handy place
  42. $Slic3r::Config = $self->config;
  43. # legacy with existing config files
  44. $self->config->set('first_layer_height', $self->config->layer_height)
  45. if !$self->config->first_layer_height;
  46. $self->config->set_ifndef('small_perimeter_speed', $self->config->perimeter_speed);
  47. $self->config->set_ifndef('bridge_speed', $self->config->infill_speed);
  48. $self->config->set_ifndef('solid_infill_speed', $self->config->infill_speed);
  49. $self->config->set_ifndef('top_solid_infill_speed', $self->config->solid_infill_speed);
  50. $self->config->set_ifndef('top_solid_layers', $self->config->solid_layers);
  51. $self->config->set_ifndef('bottom_solid_layers', $self->config->solid_layers);
  52. # G-code flavors
  53. $self->config->set('extrusion_axis', 'A') if $self->config->gcode_flavor eq 'mach3';
  54. $self->config->set('extrusion_axis', '') if $self->config->gcode_flavor eq 'no-extrusion';
  55. }
  56. sub _build_has_support_material {
  57. my $self = shift;
  58. return $self->config->support_material
  59. || $self->config->raft_layers > 0
  60. || $self->config->support_material_enforce_layers > 0;
  61. }
  62. sub _build_fill_maker {
  63. my $self = shift;
  64. return Slic3r::Fill->new(print => $self);
  65. }
  66. sub add_model {
  67. my $self = shift;
  68. my ($model) = @_;
  69. # append/merge materials and preserve a mapping between the original material ID
  70. # and our numeric material index
  71. my %materials = ();
  72. {
  73. my @material_ids = sort keys %{$model->materials};
  74. @material_ids = (0) if !@material_ids;
  75. for (my $i = $self->regions_count; $i < @material_ids; $i++) {
  76. push @{$self->regions}, Slic3r::Print::Region->new;
  77. $materials{$material_ids[$i]} = $#{$self->regions};
  78. }
  79. }
  80. foreach my $object (@{ $model->objects }) {
  81. my @meshes = (); # by region_id
  82. foreach my $volume (@{$object->volumes}) {
  83. # should the object contain multiple volumes of the same material, merge them
  84. my $region_id = defined $volume->material_id ? $materials{$volume->material_id} : 0;
  85. my $mesh = $volume->mesh->clone;
  86. $meshes[$region_id] = $meshes[$region_id]
  87. ? Slic3r::TriangleMesh->merge($meshes[$region_id], $mesh)
  88. : $mesh;
  89. }
  90. foreach my $mesh (@meshes) {
  91. next unless $mesh;
  92. $mesh->check_manifoldness;
  93. if ($object->instances) {
  94. # we ignore the per-instance rotation currently and only
  95. # consider the first one
  96. $mesh->rotate($object->instances->[0]->rotation);
  97. }
  98. $mesh->rotate($Slic3r::Config->rotate);
  99. $mesh->scale($Slic3r::Config->scale / &Slic3r::SCALING_FACTOR);
  100. }
  101. my @defined_meshes = grep defined $_, @meshes;
  102. my $complete_mesh = @defined_meshes == 1 ? $defined_meshes[0] : Slic3r::TriangleMesh->merge(@defined_meshes);
  103. # initialize print object
  104. my $print_object = Slic3r::Print::Object->new(
  105. print => $self,
  106. meshes => [ @meshes ],
  107. size => [ $complete_mesh->size ],
  108. input_file => $object->input_file,
  109. layer_height_ranges => $object->layer_height_ranges,
  110. );
  111. push @{$self->objects}, $print_object;
  112. # align object to origin
  113. {
  114. my @extents = $complete_mesh->extents;
  115. foreach my $mesh (grep defined $_, @meshes) {
  116. $mesh->move(map -$extents[$_][MIN], X,Y,Z);
  117. }
  118. }
  119. if ($object->instances) {
  120. # replace the default [0,0] instance with the custom ones
  121. $print_object->copies([ map [ scale $_->offset->[X], scale $_->offset->[Y] ], @{$object->instances} ]);
  122. }
  123. }
  124. }
  125. sub validate {
  126. my $self = shift;
  127. if ($Slic3r::Config->complete_objects) {
  128. # check horizontal clearance
  129. {
  130. my @a = ();
  131. for my $obj_idx (0 .. $#{$self->objects}) {
  132. my $clearance;
  133. {
  134. my @points = map [ @$_[X,Y] ], map @{$_->vertices}, @{$self->objects->[$obj_idx]->meshes};
  135. my $convex_hull = Slic3r::Polygon->new(convex_hull(\@points));
  136. ($clearance) = map Slic3r::Polygon->new($_),
  137. Slic3r::Geometry::Clipper::offset(
  138. [$convex_hull], scale $Slic3r::Config->extruder_clearance_radius / 2, 1, JT_ROUND);
  139. }
  140. for my $copy (@{$self->objects->[$obj_idx]->copies}) {
  141. my $copy_clearance = $clearance->clone;
  142. $copy_clearance->translate(@$copy);
  143. if (@{ intersection_ex(\@a, [$copy_clearance]) }) {
  144. die "Some objects are too close; your extruder will collide with them.\n";
  145. }
  146. @a = map @$_, @{union_ex([ @a, $copy_clearance ])};
  147. }
  148. }
  149. }
  150. # check vertical clearance
  151. {
  152. my @obj_copies = $self->object_copies;
  153. pop @obj_copies; # ignore the last copy: its height doesn't matter
  154. my $scaled_clearance = scale $Slic3r::Config->extruder_clearance_height;
  155. if (grep { +($_->size)[Z] > $scaled_clearance } map @{$self->objects->[$_->[0]]->meshes}, @obj_copies) {
  156. die "Some objects are too tall and cannot be printed without extruder collisions.\n";
  157. }
  158. }
  159. }
  160. }
  161. sub init_extruders {
  162. my $self = shift;
  163. # map regions to extruders (ghetto mapping for now)
  164. my %extruder_mapping = map { $_ => $_ } 0..$#{$self->regions};
  165. # initialize all extruder(s) we need
  166. my @used_extruders = (
  167. 0,
  168. (map $self->config->get("${_}_extruder")-1, qw(perimeter infill support_material)),
  169. (values %extruder_mapping),
  170. );
  171. for my $extruder_id (keys %{{ map {$_ => 1} @used_extruders }}) {
  172. $self->extruders->[$extruder_id] = Slic3r::Extruder->new(
  173. id => $extruder_id,
  174. map { $_ => $self->config->get($_)->[$extruder_id] // $self->config->get($_)->[0] } #/
  175. @{&Slic3r::Extruder::OPTIONS}
  176. );
  177. }
  178. # calculate regions' flows
  179. for my $region_id (0 .. $#{$self->regions}) {
  180. my $region = $self->regions->[$region_id];
  181. # per-role extruders and flows
  182. for (qw(perimeter infill solid_infill top_infill)) {
  183. my $extruder_name = $_;
  184. $extruder_name =~ s/^(?:solid|top)_//;
  185. $region->extruders->{$_} = ($self->regions_count > 1)
  186. ? $self->extruders->[$extruder_mapping{$region_id}]
  187. : $self->extruders->[$self->config->get("${extruder_name}_extruder")-1];
  188. $region->flows->{$_} = $region->extruders->{$_}->make_flow(
  189. width => $self->config->get("${_}_extrusion_width") || $self->config->extrusion_width,
  190. role => $_,
  191. );
  192. $region->first_layer_flows->{$_} = $region->extruders->{$_}->make_flow(
  193. layer_height => $self->config->get_value('first_layer_height'),
  194. width => $self->config->first_layer_extrusion_width,
  195. role => $_,
  196. ) if $self->config->first_layer_extrusion_width;
  197. }
  198. }
  199. # calculate support material flow
  200. if ($self->has_support_material) {
  201. my $extruder = $self->extruders->[$self->config->support_material_extruder-1];
  202. $self->support_material_flow($extruder->make_flow(
  203. width => $self->config->support_material_extrusion_width || $self->config->extrusion_width,
  204. role => 'support_material',
  205. ));
  206. $self->first_layer_support_material_flow($extruder->make_flow(
  207. layer_height => $self->config->get_value('first_layer_height'),
  208. width => $self->config->first_layer_extrusion_width,
  209. role => 'support_material',
  210. ));
  211. }
  212. }
  213. sub object_copies {
  214. my $self = shift;
  215. my @oc = ();
  216. for my $obj_idx (0 .. $#{$self->objects}) {
  217. push @oc, map [ $obj_idx, $_ ], @{$self->objects->[$obj_idx]->copies};
  218. }
  219. return @oc;
  220. }
  221. sub layer_count {
  222. my $self = shift;
  223. return max(map { scalar @{$_->layers} } @{$self->objects});
  224. }
  225. sub regions_count {
  226. my $self = shift;
  227. return scalar @{$self->regions};
  228. }
  229. sub duplicate {
  230. my $self = shift;
  231. if ($Slic3r::Config->duplicate_grid->[X] > 1 || $Slic3r::Config->duplicate_grid->[Y] > 1) {
  232. if (@{$self->objects} > 1) {
  233. die "Grid duplication is not supported with multiple objects\n";
  234. }
  235. my $object = $self->objects->[0];
  236. # generate offsets for copies
  237. my $dist = scale $Slic3r::Config->duplicate_distance;
  238. @{$self->objects->[0]->copies} = ();
  239. for my $x_copy (1..$Slic3r::Config->duplicate_grid->[X]) {
  240. for my $y_copy (1..$Slic3r::Config->duplicate_grid->[Y]) {
  241. push @{$self->objects->[0]->copies}, [
  242. ($object->size->[X] + $dist) * ($x_copy-1),
  243. ($object->size->[Y] + $dist) * ($y_copy-1),
  244. ];
  245. }
  246. }
  247. } elsif ($Slic3r::Config->duplicate > 1) {
  248. foreach my $object (@{$self->objects}) {
  249. @{$object->copies} = map [0,0], 1..$Slic3r::Config->duplicate;
  250. }
  251. $self->arrange_objects;
  252. }
  253. }
  254. sub arrange_objects {
  255. my $self = shift;
  256. my $total_parts = scalar map @{$_->copies}, @{$self->objects};
  257. my $partx = max(map $_->size->[X], @{$self->objects});
  258. my $party = max(map $_->size->[Y], @{$self->objects});
  259. my @positions = Slic3r::Geometry::arrange
  260. ($total_parts, $partx, $party, (map scale $_, @{$Slic3r::Config->bed_size}), scale $Slic3r::Config->min_object_distance, $self->config);
  261. @{$_->copies} = splice @positions, 0, scalar @{$_->copies} for @{$self->objects};
  262. }
  263. sub bounding_box {
  264. my $self = shift;
  265. my @points = ();
  266. foreach my $obj_idx (0 .. $#{$self->objects}) {
  267. my $object = $self->objects->[$obj_idx];
  268. foreach my $copy (@{$self->objects->[$obj_idx]->copies}) {
  269. push @points,
  270. [ $copy->[X], $copy->[Y] ],
  271. [ $copy->[X] + $object->size->[X], $copy->[Y] ],
  272. [ $copy->[X] + $object->size->[X], $copy->[Y] + $object->size->[Y] ],
  273. [ $copy->[X], $copy->[Y] + $object->size->[Y] ];
  274. }
  275. }
  276. return Slic3r::Geometry::bounding_box(\@points);
  277. }
  278. sub size {
  279. my $self = shift;
  280. my @bb = $self->bounding_box;
  281. return [ $bb[X2] - $bb[X1], $bb[Y2] - $bb[Y1] ];
  282. }
  283. sub _simplify_slices {
  284. my $self = shift;
  285. my ($distance) = @_;
  286. foreach my $layer (map @{$_->layers}, @{$self->objects}) {
  287. @$_ = map $_->simplify($distance), @$_
  288. for $layer->slices, (map $_->slices, @{$layer->regions});
  289. }
  290. }
  291. sub export_gcode {
  292. my $self = shift;
  293. my %params = @_;
  294. $self->init_extruders;
  295. my $status_cb = $params{status_cb} || sub {};
  296. my $t0 = [gettimeofday];
  297. # skein the STL into layers
  298. # each layer has surfaces with holes
  299. $status_cb->(10, "Processing triangulated mesh");
  300. $_->slice for @{$self->objects};
  301. if ($Slic3r::Config->resolution) {
  302. $status_cb->(15, "Simplifying input");
  303. $self->_simplify_slices(scale $Slic3r::Config->resolution);
  304. }
  305. # make perimeters
  306. # this will add a set of extrusion loops to each layer
  307. # as well as generate infill boundaries
  308. $status_cb->(20, "Generating perimeters");
  309. $_->make_perimeters for @{$self->objects};
  310. # simplify slices (both layer and region slices),
  311. # we only need the max resolution for perimeters
  312. $self->_simplify_slices(&Slic3r::SCALED_RESOLUTION);
  313. # this will assign a type (top/bottom/internal) to $layerm->slices
  314. # and transform $layerm->fill_surfaces from expolygon
  315. # to typed top/bottom/internal surfaces;
  316. $status_cb->(30, "Detecting solid surfaces");
  317. $_->detect_surfaces_type for @{$self->objects};
  318. # decide what surfaces are to be filled
  319. $status_cb->(35, "Preparing infill surfaces");
  320. $_->prepare_fill_surfaces for map @{$_->regions}, map @{$_->layers}, @{$self->objects};
  321. # this will detect bridges and reverse bridges
  322. # and rearrange top/bottom/internal surfaces
  323. $status_cb->(45, "Detect bridges");
  324. $_->process_external_surfaces for map @{$_->regions}, map @{$_->layers}, @{$self->objects};
  325. # detect which fill surfaces are near external layers
  326. # they will be split in internal and internal-solid surfaces
  327. $status_cb->(60, "Generating horizontal shells");
  328. $_->discover_horizontal_shells for @{$self->objects};
  329. $_->clip_fill_surfaces for @{$self->objects};
  330. # the following step needs to be done before combination because it may need
  331. # to remove only half of the combined infill
  332. $_->bridge_over_infill for @{$self->objects};
  333. # combine fill surfaces to honor the "infill every N layers" option
  334. $status_cb->(70, "Combining infill");
  335. $_->combine_infill for @{$self->objects};
  336. # this will generate extrusion paths for each layer
  337. $status_cb->(80, "Infilling layers");
  338. {
  339. my $fill_maker = $self->fill_maker;
  340. Slic3r::parallelize(
  341. items => sub {
  342. my @items = (); # [obj_idx, layer_id]
  343. for my $obj_idx (0 .. $#{$self->objects}) {
  344. for my $region_id (0 .. ($self->regions_count-1)) {
  345. push @items, map [$obj_idx, $_, $region_id], 0..($self->objects->[$obj_idx]->layer_count-1);
  346. }
  347. }
  348. @items;
  349. },
  350. thread_cb => sub {
  351. my $q = shift;
  352. $Slic3r::Geometry::Clipper::clipper = Math::Clipper->new;
  353. my $fills = {};
  354. while (defined (my $obj_layer = $q->dequeue)) {
  355. my ($obj_idx, $layer_id, $region_id) = @$obj_layer;
  356. $fills->{$obj_idx} ||= {};
  357. $fills->{$obj_idx}{$layer_id} ||= {};
  358. $fills->{$obj_idx}{$layer_id}{$region_id} = [
  359. $fill_maker->make_fill($self->objects->[$obj_idx]->layers->[$layer_id]->regions->[$region_id]),
  360. ];
  361. }
  362. return $fills;
  363. },
  364. collect_cb => sub {
  365. my $fills = shift;
  366. foreach my $obj_idx (keys %$fills) {
  367. my $object = $self->objects->[$obj_idx];
  368. foreach my $layer_id (keys %{$fills->{$obj_idx}}) {
  369. my $layer = $object->layers->[$layer_id];
  370. foreach my $region_id (keys %{$fills->{$obj_idx}{$layer_id}}) {
  371. $layer->regions->[$region_id]->fills($fills->{$obj_idx}{$layer_id}{$region_id});
  372. }
  373. }
  374. }
  375. },
  376. no_threads_cb => sub {
  377. foreach my $layerm (map @{$_->regions}, map @{$_->layers}, @{$self->objects}) {
  378. $layerm->fills([ $fill_maker->make_fill($layerm) ]);
  379. }
  380. },
  381. );
  382. }
  383. # generate support material
  384. if ($self->has_support_material) {
  385. $status_cb->(85, "Generating support material");
  386. $_->generate_support_material for @{$self->objects};
  387. }
  388. # free memory (note that support material needs fill_surfaces)
  389. $_->fill_surfaces(undef) for map @{$_->regions}, map @{$_->layers}, @{$self->objects};
  390. # make skirt
  391. $status_cb->(88, "Generating skirt");
  392. $self->make_skirt;
  393. $self->make_brim; # must come after make_skirt
  394. # time to make some statistics
  395. if (0) {
  396. eval "use Devel::Size";
  397. print "MEMORY USAGE:\n";
  398. printf " meshes = %.1fMb\n", List::Util::sum(map Devel::Size::total_size($_->meshes), @{$self->objects})/1024/1024;
  399. printf " layer slices = %.1fMb\n", List::Util::sum(map Devel::Size::total_size($_->slices), map @{$_->layers}, @{$self->objects})/1024/1024;
  400. printf " region slices = %.1fMb\n", List::Util::sum(map Devel::Size::total_size($_->slices), map @{$_->regions}, map @{$_->layers}, @{$self->objects})/1024/1024;
  401. printf " perimeters = %.1fMb\n", List::Util::sum(map Devel::Size::total_size($_->perimeters), map @{$_->regions}, map @{$_->layers}, @{$self->objects})/1024/1024;
  402. printf " fills = %.1fMb\n", List::Util::sum(map Devel::Size::total_size($_->fills), map @{$_->regions}, map @{$_->layers}, @{$self->objects})/1024/1024;
  403. printf " print object = %.1fMb\n", Devel::Size::total_size($self)/1024/1024;
  404. }
  405. if (0) {
  406. eval "use Slic3r::Test::SectionCut";
  407. Slic3r::Test::SectionCut->new(print => $self)->export_svg("section_cut.svg");
  408. }
  409. # output everything to a G-code file
  410. my $output_file = $self->expanded_output_filepath($params{output_file});
  411. $status_cb->(90, "Exporting G-code" . ($output_file ? " to $output_file" : ""));
  412. $self->write_gcode($params{output_fh} || $output_file);
  413. # run post-processing scripts
  414. if (@{$Slic3r::Config->post_process}) {
  415. $status_cb->(95, "Running post-processing scripts");
  416. $Slic3r::Config->setenv;
  417. for (@{$Slic3r::Config->post_process}) {
  418. Slic3r::debugf " '%s' '%s'\n", $_, $output_file;
  419. system($_, $output_file);
  420. }
  421. }
  422. # output some statistics
  423. unless ($params{quiet}) {
  424. $self->processing_time(tv_interval($t0));
  425. printf "Done. Process took %d minutes and %.3f seconds\n",
  426. int($self->processing_time/60),
  427. $self->processing_time - int($self->processing_time/60)*60;
  428. # TODO: more statistics!
  429. printf "Filament required: %.1fmm (%.1fcm3)\n",
  430. $self->total_extrusion_length, $self->total_extrusion_volume;
  431. }
  432. }
  433. sub export_svg {
  434. my $self = shift;
  435. my %params = @_;
  436. # this shouldn't be needed, but we're currently relying on ->make_surfaces() which
  437. # calls ->perimeter_flow
  438. $self->init_extruders;
  439. $_->slice for @{$self->objects};
  440. $self->arrange_objects;
  441. my $output_file = $self->expanded_output_filepath($params{output_file});
  442. $output_file =~ s/\.gcode$/.svg/i;
  443. Slic3r::open(\my $fh, ">", $output_file) or die "Failed to open $output_file for writing\n";
  444. print "Exporting to $output_file...";
  445. my $print_size = $self->size;
  446. print $fh sprintf <<"EOF", unscale($print_size->[X]), unscale($print_size->[Y]);
  447. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  448. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
  449. <svg width="%s" height="%s" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:slic3r="http://slic3r.org/namespaces/slic3r">
  450. <!--
  451. Generated using Slic3r $Slic3r::VERSION
  452. http://slic3r.org/
  453. -->
  454. EOF
  455. my $print_polygon = sub {
  456. my ($polygon, $type) = @_;
  457. printf $fh qq{ <polygon slic3r:type="%s" points="%s" style="fill: %s" />\n},
  458. $type, (join ' ', map { join ',', map unscale $_, @$_ } @$polygon),
  459. ($type eq 'contour' ? 'white' : 'black');
  460. };
  461. my @previous_layer_slices = ();
  462. for my $layer_id (0..$self->layer_count-1) {
  463. my @layers = map $_->layers->[$layer_id], @{$self->objects};
  464. printf $fh qq{ <g id="layer%d" slic3r:z="%s">\n}, $layer_id, unscale +(grep defined $_, @layers)[0]->slice_z;
  465. my @current_layer_slices = ();
  466. for my $obj_idx (0 .. $#{$self->objects}) {
  467. my $layer = $self->objects->[$obj_idx]->layers->[$layer_id] or next;
  468. # sort slices so that the outermost ones come first
  469. my @slices = sort { $a->contour->encloses_point($b->contour->[0]) ? 0 : 1 } @{$layer->slices};
  470. foreach my $copy (@{$self->objects->[$obj_idx]->copies}) {
  471. foreach my $slice (@slices) {
  472. my $expolygon = $slice->clone;
  473. $expolygon->translate(@$copy);
  474. $print_polygon->($expolygon->contour, 'contour');
  475. $print_polygon->($_, 'hole') for $expolygon->holes;
  476. push @current_layer_slices, $expolygon;
  477. }
  478. }
  479. }
  480. # generate support material
  481. if ($self->has_support_material && $layer_id > 0) {
  482. my (@supported_slices, @unsupported_slices) = ();
  483. foreach my $expolygon (@current_layer_slices) {
  484. my $intersection = intersection_ex(
  485. [ map @$_, @previous_layer_slices ],
  486. $expolygon,
  487. );
  488. @$intersection
  489. ? push @supported_slices, $expolygon
  490. : push @unsupported_slices, $expolygon;
  491. }
  492. my @supported_points = map @$_, @$_, @supported_slices;
  493. foreach my $expolygon (@unsupported_slices) {
  494. # look for the nearest point to this island among all
  495. # supported points
  496. my $support_point = nearest_point($expolygon->contour->[0], \@supported_points)
  497. or next;
  498. my $anchor_point = nearest_point($support_point, $expolygon->contour);
  499. printf $fh qq{ <line x1="%s" y1="%s" x2="%s" y2="%s" style="stroke-width: 2; stroke: white" />\n},
  500. map @$_, $support_point, $anchor_point;
  501. }
  502. }
  503. print $fh qq{ </g>\n};
  504. @previous_layer_slices = @current_layer_slices;
  505. }
  506. print $fh "</svg>\n";
  507. close $fh;
  508. print "Done.\n";
  509. }
  510. sub make_skirt {
  511. my $self = shift;
  512. return unless $Slic3r::Config->skirts > 0;
  513. # collect points from all layers contained in skirt height
  514. my @points = ();
  515. foreach my $obj_idx (0 .. $#{$self->objects}) {
  516. my $skirt_height = $Slic3r::Config->skirt_height;
  517. $skirt_height = $self->objects->[$obj_idx]->layer_count if $skirt_height > $self->objects->[$obj_idx]->layer_count;
  518. my @layers = map $self->objects->[$obj_idx]->layers->[$_], 0..($skirt_height-1);
  519. my @layer_points = (
  520. (map @$_, map @$_, map @{$_->slices}, @layers),
  521. (map @$_, map @{$_->thin_walls}, map @{$_->regions}, @layers),
  522. (map @{$_->unpack->polyline}, map @{$_->support_fills->paths}, grep $_->support_fills, @layers),
  523. );
  524. push @points, map move_points($_, @layer_points), @{$self->objects->[$obj_idx]->copies};
  525. }
  526. return if @points < 3; # at least three points required for a convex hull
  527. # find out convex hull
  528. my $convex_hull = convex_hull(\@points);
  529. my @extruded_length = (); # for each extruder
  530. # TODO: use each extruder's own flow
  531. my $spacing = $self->objects->[0]->layers->[0]->regions->[0]->perimeter_flow->spacing;
  532. my $first_layer_height = $Slic3r::Config->get_value('first_layer_height');
  533. my @extruders_e_per_mm = ();
  534. my $extruder_idx = 0;
  535. # draw outlines from outside to inside
  536. # loop while we have less skirts than required or any extruder hasn't reached the min length if any
  537. my $distance = scale $Slic3r::Config->skirt_distance;
  538. for (my $i = $Slic3r::Config->skirts; $i > 0; $i--) {
  539. $distance += scale $spacing;
  540. my ($loop) = Slic3r::Geometry::Clipper::offset([$convex_hull], $distance, 0.0001, JT_ROUND);
  541. push @{$self->skirt}, Slic3r::ExtrusionLoop->pack(
  542. polygon => Slic3r::Polygon->new(@$loop),
  543. role => EXTR_ROLE_SKIRT,
  544. flow_spacing => $spacing,
  545. );
  546. if ($Slic3r::Config->min_skirt_length > 0) {
  547. bless $loop, 'Slic3r::Polygon';
  548. $extruded_length[$extruder_idx] ||= 0;
  549. $extruders_e_per_mm[$extruder_idx] ||= $self->extruders->[$extruder_idx]->e_per_mm($spacing, $first_layer_height);
  550. $extruded_length[$extruder_idx] += unscale $loop->length * $extruders_e_per_mm[$extruder_idx];
  551. $i++ if defined first { ($extruded_length[$_] // 0) < $Slic3r::Config->min_skirt_length } 0 .. $#{$self->extruders};
  552. if ($extruded_length[$extruder_idx] >= $Slic3r::Config->min_skirt_length) {
  553. if ($extruder_idx < $#{$self->extruders}) {
  554. $extruder_idx++;
  555. next;
  556. }
  557. }
  558. }
  559. }
  560. @{$self->skirt} = reverse @{$self->skirt};
  561. }
  562. sub make_brim {
  563. my $self = shift;
  564. return unless $Slic3r::Config->brim_width > 0;
  565. my $flow = $self->objects->[0]->layers->[0]->regions->[0]->perimeter_flow;
  566. my $grow_distance = $flow->scaled_width / 2;
  567. my @islands = (); # array of polygons
  568. foreach my $obj_idx (0 .. $#{$self->objects}) {
  569. my $layer0 = $self->objects->[$obj_idx]->layers->[0];
  570. my @object_islands = (
  571. (map $_->contour, @{$layer0->slices}),
  572. (map { $_->isa('Slic3r::Polygon') ? $_ : $_->grow($grow_distance) } map @{$_->thin_walls}, @{$layer0->regions}),
  573. (map $_->unpack->polyline->grow($grow_distance), map @{$_->support_fills->paths}, grep $_->support_fills, $layer0),
  574. );
  575. foreach my $copy (@{$self->objects->[$obj_idx]->copies}) {
  576. push @islands, map $_->clone->translate(@$copy), @object_islands;
  577. }
  578. }
  579. # if brim touches skirt, make it around skirt too
  580. # TODO: calculate actual skirt width (using each extruder's flow in multi-extruder setups)
  581. if ($Slic3r::Config->skirt_distance + (($Slic3r::Config->skirts - 1) * $flow->spacing) <= $Slic3r::Config->brim_width) {
  582. push @islands, map $_->unpack->split_at_first_point->polyline->grow($grow_distance), @{$self->skirt};
  583. }
  584. my $num_loops = sprintf "%.0f", $Slic3r::Config->brim_width / $flow->width;
  585. for my $i (reverse 1 .. $num_loops) {
  586. # JT_SQUARE ensures no vertex is outside the given offset distance
  587. push @{$self->brim}, Slic3r::ExtrusionLoop->pack(
  588. polygon => Slic3r::Polygon->new($_),
  589. role => EXTR_ROLE_SKIRT,
  590. flow_spacing => $flow->spacing,
  591. ) for Slic3r::Geometry::Clipper::offset(\@islands, ($i - 0.5) * $flow->scaled_spacing, undef, JT_SQUARE); # -0.5 because islands are not represented by their centerlines
  592. # TODO: we need the offset inwards/offset outwards logic to avoid overlapping extrusions
  593. }
  594. }
  595. sub write_gcode {
  596. my $self = shift;
  597. my ($file) = @_;
  598. # open output gcode file if we weren't supplied a file-handle
  599. my $fh;
  600. if (ref $file eq 'IO::Scalar') {
  601. $fh = $file;
  602. } else {
  603. Slic3r::open(\$fh, ">", $file)
  604. or die "Failed to open $file for writing\n";
  605. }
  606. # write some information
  607. my @lt = localtime;
  608. printf $fh "; generated by Slic3r $Slic3r::VERSION on %04d-%02d-%02d at %02d:%02d:%02d\n\n",
  609. $lt[5] + 1900, $lt[4]+1, $lt[3], $lt[2], $lt[1], $lt[0];
  610. print $fh "; $_\n" foreach split /\R/, $Slic3r::Config->notes;
  611. print $fh "\n" if $Slic3r::Config->notes;
  612. for (qw(layer_height perimeters top_solid_layers bottom_solid_layers fill_density perimeter_speed infill_speed travel_speed)) {
  613. printf $fh "; %s = %s\n", $_, $Slic3r::Config->$_;
  614. }
  615. for (qw(nozzle_diameter filament_diameter extrusion_multiplier)) {
  616. printf $fh "; %s = %s\n", $_, $Slic3r::Config->$_->[0];
  617. }
  618. printf $fh "; perimeters extrusion width = %.2fmm\n", $self->regions->[0]->flows->{perimeter}->width;
  619. printf $fh "; infill extrusion width = %.2fmm\n", $self->regions->[0]->flows->{infill}->width;
  620. printf $fh "; solid infill extrusion width = %.2fmm\n", $self->regions->[0]->flows->{solid_infill}->width;
  621. printf $fh "; top infill extrusion width = %.2fmm\n", $self->regions->[0]->flows->{top_infill}->width;
  622. printf $fh "; support material extrusion width = %.2fmm\n", $self->support_material_flow->width
  623. if $self->support_material_flow;
  624. printf $fh "; first layer extrusion width = %.2fmm\n", $self->regions->[0]->first_layer_flows->{perimeter}->width
  625. if $self->regions->[0]->first_layer_flows->{perimeter};
  626. print $fh "\n";
  627. # set up our extruder object
  628. my $gcodegen = Slic3r::GCode->new(
  629. multiple_extruders => (@{$self->extruders} > 1),
  630. layer_count => $self->layer_count,
  631. );
  632. print $fh "G21 ; set units to millimeters\n";
  633. print $fh $gcodegen->set_fan(0, 1) if $Slic3r::Config->cooling && $Slic3r::Config->disable_fan_first_layers;
  634. # write start commands to file
  635. printf $fh $gcodegen->set_bed_temperature($Slic3r::Config->first_layer_bed_temperature, 1),
  636. if $Slic3r::Config->first_layer_bed_temperature && $Slic3r::Config->start_gcode !~ /M(?:190|140)/i;
  637. my $print_first_layer_temperature = sub {
  638. for my $t (grep $self->extruders->[$_], 0 .. $#{$Slic3r::Config->first_layer_temperature}) {
  639. printf $fh $gcodegen->set_temperature($self->extruders->[$t]->first_layer_temperature, 0, $t)
  640. if $self->extruders->[$t]->first_layer_temperature;
  641. }
  642. };
  643. $print_first_layer_temperature->() if $Slic3r::Config->start_gcode !~ /M(?:109|104)/i;
  644. printf $fh "%s\n", $Slic3r::Config->replace_options($Slic3r::Config->start_gcode);
  645. for my $t (grep $self->extruders->[$_], 0 .. $#{$Slic3r::Config->first_layer_temperature}) {
  646. printf $fh $gcodegen->set_temperature($self->extruders->[$t]->first_layer_temperature, 1, $t)
  647. if $self->extruders->[$t]->first_layer_temperature && $Slic3r::Config->start_gcode !~ /M(?:109|104)/i;
  648. }
  649. print $fh "G90 ; use absolute coordinates\n";
  650. if ($Slic3r::Config->gcode_flavor =~ /^(?:reprap|teacup)$/) {
  651. printf $fh $gcodegen->reset_e;
  652. if ($Slic3r::Config->gcode_flavor =~ /^(?:reprap|makerbot|sailfish)$/) {
  653. if ($Slic3r::Config->use_relative_e_distances) {
  654. print $fh "M83 ; use relative distances for extrusion\n";
  655. } else {
  656. print $fh "M82 ; use absolute distances for extrusion\n";
  657. }
  658. }
  659. }
  660. # calculate X,Y shift to center print around specified origin
  661. my @print_bb = $self->bounding_box;
  662. my @shift = (
  663. $Slic3r::Config->print_center->[X] - (unscale ($print_bb[X2] - $print_bb[X1]) / 2) - unscale $print_bb[X1],
  664. $Slic3r::Config->print_center->[Y] - (unscale ($print_bb[Y2] - $print_bb[Y1]) / 2) - unscale $print_bb[Y1],
  665. );
  666. # initialize a motion planner for object-to-object travel moves
  667. if ($Slic3r::Config->avoid_crossing_perimeters) {
  668. my $distance_from_objects = 1;
  669. # compute the offsetted convex hull for each object and repeat it for each copy.
  670. my @islands = ();
  671. foreach my $obj_idx (0 .. $#{$self->objects}) {
  672. my $convex_hull = convex_hull([
  673. map @{$_->contour}, map @{$_->slices}, @{$self->objects->[$obj_idx]->layers},
  674. ]);
  675. # discard layers only containing thin walls (offset would fail on an empty polygon)
  676. if (@$convex_hull) {
  677. my @island = Slic3r::ExPolygon->new($convex_hull)
  678. ->translate(scale $shift[X], scale $shift[Y])
  679. ->offset_ex(scale $distance_from_objects, 1, JT_SQUARE);
  680. foreach my $copy (@{ $self->objects->[$obj_idx]->copies }) {
  681. push @islands, map $_->clone->translate(@$copy), @island;
  682. }
  683. }
  684. }
  685. $gcodegen->external_mp(Slic3r::GCode::MotionPlanner->new(
  686. islands => union_ex([ map @$_, @islands ]),
  687. no_internal => 1,
  688. ));
  689. }
  690. # prepare the logic to print one layer
  691. my $skirt_done = 0; # count of skirt layers done
  692. my $brim_done = 0;
  693. my $second_layer_things_done = 0;
  694. my $last_obj_copy = "";
  695. my $extrude_layer = sub {
  696. my ($layer, $object_copies) = @_;
  697. my $gcode = "";
  698. if (!$second_layer_things_done && $layer->id == 1) {
  699. for my $t (grep $self->extruders->[$_], 0 .. $#{$Slic3r::Config->temperature}) {
  700. $gcode .= $gcodegen->set_temperature($self->extruders->[$t]->temperature, 0, $t)
  701. if $self->extruders->[$t]->temperature && $self->extruders->[$t]->temperature != $self->extruders->[$t]->first_layer_temperature;
  702. }
  703. $gcode .= $gcodegen->set_bed_temperature($Slic3r::Config->bed_temperature)
  704. if $Slic3r::Config->bed_temperature && $Slic3r::Config->bed_temperature != $Slic3r::Config->first_layer_bed_temperature;
  705. $second_layer_things_done = 1;
  706. }
  707. # set new layer, but don't move Z as support material contact areas may need an intermediate one
  708. $gcode .= $gcodegen->change_layer($layer);
  709. # prepare callback to call as soon as a Z command is generated
  710. $gcodegen->move_z_callback(sub {
  711. $gcodegen->move_z_callback(undef); # circular ref or not?
  712. return "" if !$Slic3r::Config->layer_gcode;
  713. return $Slic3r::Config->replace_options($Slic3r::Config->layer_gcode) . "\n";
  714. });
  715. # extrude skirt
  716. if ($skirt_done < $Slic3r::Config->skirt_height) {
  717. $gcodegen->set_shift(@shift);
  718. $gcode .= $gcodegen->set_extruder($self->extruders->[0]); # move_z requires extruder
  719. $gcode .= $gcodegen->move_z($gcodegen->layer->print_z);
  720. # skip skirt if we have a large brim
  721. if ($layer->id < $Slic3r::Config->skirt_height) {
  722. # distribute skirt loops across all extruders
  723. for my $i (0 .. $#{$self->skirt}) {
  724. # when printing layers > 0 ignore 'min_skirt_length' and
  725. # just use the 'skirts' setting; also just use the current extruder
  726. last if ($layer->id > 0) && ($i >= $Slic3r::Config->skirts);
  727. $gcode .= $gcodegen->set_extruder($self->extruders->[ ($i/@{$self->extruders}) % @{$self->extruders} ])
  728. if $layer->id == 0;
  729. $gcode .= $gcodegen->extrude_loop($self->skirt->[$i], 'skirt');
  730. }
  731. }
  732. $skirt_done++;
  733. $gcodegen->straight_once(1);
  734. }
  735. # extrude brim
  736. if (!$brim_done) {
  737. $gcode .= $gcodegen->set_extruder($self->extruders->[$Slic3r::Config->support_material_extruder-1]); # move_z requires extruder
  738. $gcode .= $gcodegen->move_z($gcodegen->layer->print_z);
  739. $gcodegen->set_shift(@shift);
  740. $gcode .= $gcodegen->extrude_loop($_, 'brim') for @{$self->brim};
  741. $brim_done = 1;
  742. $gcodegen->straight_once(1);
  743. }
  744. for my $copy (@$object_copies) {
  745. $gcodegen->new_object(1) if $last_obj_copy && $last_obj_copy ne "$copy";
  746. $last_obj_copy = "$copy";
  747. $gcodegen->set_shift(map $shift[$_] + unscale $copy->[$_], X,Y);
  748. # extrude support material before other things because it might use a lower Z
  749. # and also because we avoid travelling on other things when printing it
  750. if ($self->has_support_material) {
  751. $gcode .= $gcodegen->move_z($layer->support_material_contact_z)
  752. if ($layer->support_contact_fills && @{ $layer->support_contact_fills->paths });
  753. $gcode .= $gcodegen->set_extruder($self->extruders->[$Slic3r::Config->support_material_extruder-1]);
  754. if ($layer->support_contact_fills) {
  755. $gcode .= $gcodegen->extrude_path($_, 'support material contact area')
  756. for $layer->support_contact_fills->chained_path($gcodegen->last_pos);
  757. }
  758. $gcode .= $gcodegen->move_z($layer->print_z);
  759. if ($layer->support_fills) {
  760. $gcode .= $gcodegen->extrude_path($_, 'support material')
  761. for $layer->support_fills->chained_path($gcodegen->last_pos);
  762. }
  763. }
  764. # set actual Z - this will force a retraction
  765. $gcode .= $gcodegen->move_z($layer->print_z);
  766. # tweak region ordering to save toolchanges
  767. my @region_ids = 0 .. ($self->regions_count-1);
  768. if ($gcodegen->multiple_extruders) {
  769. my $last_extruder = $gcodegen->extruder;
  770. my $best_region_id = first { $self->regions->[$_]->extruders->{perimeter} eq $last_extruder } @region_ids;
  771. @region_ids = ($best_region_id, grep $_ != $best_region_id, @region_ids) if $best_region_id;
  772. }
  773. foreach my $region_id (@region_ids) {
  774. my $layerm = $layer->regions->[$region_id];
  775. my $region = $self->regions->[$region_id];
  776. my @islands = ();
  777. if ($Slic3r::Config->avoid_crossing_perimeters) {
  778. push @islands, map +{ perimeters => [], fills => [] }, @{$layer->slices};
  779. PERIMETER: foreach my $perimeter (@{$layerm->perimeters}) {
  780. my $p = $perimeter->unpack;
  781. for my $i (0 .. $#{$layer->slices}-1) {
  782. if ($layer->slices->[$i]->contour->encloses_point($p->first_point)) {
  783. push @{ $islands[$i]{perimeters} }, $p;
  784. next PERIMETER;
  785. }
  786. }
  787. push @{ $islands[-1]{perimeters} }, $p; # optimization
  788. }
  789. FILL: foreach my $fill (@{$layerm->fills}) {
  790. my $f = $fill->unpack;
  791. for my $i (0 .. $#{$layer->slices}-1) {
  792. if ($layer->slices->[$i]->contour->encloses_point($f->first_point)) {
  793. push @{ $islands[$i]{fills} }, $f;
  794. next FILL;
  795. }
  796. }
  797. push @{ $islands[-1]{fills} }, $f; # optimization
  798. }
  799. } else {
  800. push @islands, {
  801. perimeters => $layerm->perimeters,
  802. fills => $layerm->fills,
  803. };
  804. }
  805. foreach my $island (@islands) {
  806. my $extrude_perimeters = sub {
  807. return if !@{ $island->{perimeters} };
  808. $gcode .= $gcodegen->set_extruder($region->extruders->{perimeter});
  809. $gcode .= $gcodegen->extrude($_, 'perimeter') for @{ $island->{perimeters} };
  810. };
  811. my $extrude_fills = sub {
  812. return if !@{ $island->{fills} };
  813. $gcode .= $gcodegen->set_extruder($region->extruders->{infill});
  814. for my $fill (@{ $island->{fills} }) {
  815. if ($fill->isa('Slic3r::ExtrusionPath::Collection')) {
  816. $gcode .= $gcodegen->extrude($_, 'fill')
  817. for $fill->chained_path($gcodegen->last_pos);
  818. } else {
  819. $gcode .= $gcodegen->extrude($fill, 'fill') ;
  820. }
  821. }
  822. };
  823. # give priority to infill if we were already using its extruder and it wouldn't
  824. # be good for perimeters
  825. if ($Slic3r::Config->infill_first
  826. || ($gcodegen->multiple_extruders && $region->extruders->{infill} eq $gcodegen->extruder) && $region->extruders->{infill} ne $region->extruders->{perimeter}) {
  827. $extrude_fills->();
  828. $extrude_perimeters->();
  829. } else {
  830. $extrude_perimeters->();
  831. $extrude_fills->();
  832. }
  833. }
  834. }
  835. }
  836. return $gcode;
  837. };
  838. # do all objects for each layer
  839. if ($Slic3r::Config->complete_objects) {
  840. # print objects from the smallest to the tallest to avoid collisions
  841. # when moving onto next object starting point
  842. my @obj_idx = sort { $self->objects->[$a]->layer_count <=> $self->objects->[$b]->layer_count } 0..$#{$self->objects};
  843. my $finished_objects = 0;
  844. for my $obj_idx (@obj_idx) {
  845. for my $copy (@{ $self->objects->[$obj_idx]->copies }) {
  846. # move to the origin position for the copy we're going to print.
  847. # this happens before Z goes down to layer 0 again, so that
  848. # no collision happens hopefully.
  849. if ($finished_objects > 0) {
  850. $gcodegen->set_shift(map $shift[$_] + unscale $copy->[$_], X,Y);
  851. print $fh $gcodegen->retract;
  852. print $fh $gcodegen->G0(Slic3r::Point->new(0,0), undef, 0, 'move to origin position for next object');
  853. }
  854. my $buffer = Slic3r::GCode::CoolingBuffer->new(
  855. config => $Slic3r::Config,
  856. gcodegen => $gcodegen,
  857. );
  858. for my $layer (@{$self->objects->[$obj_idx]->layers}) {
  859. # if we are printing the bottom layer of an object, and we have already finished
  860. # another one, set first layer temperatures. this happens before the Z move
  861. # is triggered, so machine has more time to reach such temperatures
  862. if ($layer->id == 0 && $finished_objects > 0) {
  863. printf $fh $gcodegen->set_bed_temperature($Slic3r::Config->first_layer_bed_temperature),
  864. if $Slic3r::Config->first_layer_bed_temperature;
  865. $print_first_layer_temperature->();
  866. }
  867. print $fh $buffer->append($extrude_layer->($layer, [$copy]), $layer);
  868. }
  869. print $fh $buffer->flush;
  870. $finished_objects++;
  871. }
  872. }
  873. } else {
  874. my $buffer = Slic3r::GCode::CoolingBuffer->new(
  875. config => $Slic3r::Config,
  876. gcodegen => $gcodegen,
  877. );
  878. print $fh $buffer->append($extrude_layer->($_, $_->object->copies), $_)
  879. for sort { $a->print_z <=> $b->print_z } map @{$_->layers}, @{$self->objects};
  880. print $fh $buffer->flush;
  881. }
  882. # save statistic data
  883. $self->total_extrusion_length($gcodegen->total_extrusion_length);
  884. # write end commands to file
  885. print $fh $gcodegen->retract if $gcodegen->extruder; # empty prints don't even set an extruder
  886. print $fh $gcodegen->set_fan(0);
  887. printf $fh "%s\n", $Slic3r::Config->replace_options($Slic3r::Config->end_gcode);
  888. printf $fh "; filament used = %.1fmm (%.1fcm3)\n",
  889. $self->total_extrusion_length, $self->total_extrusion_volume;
  890. if ($Slic3r::Config->gcode_comments) {
  891. # append full config
  892. print $fh "\n";
  893. foreach my $opt_key (sort keys %{$Slic3r::Config}) {
  894. next if $Slic3r::Config::Options->{$opt_key}{shortcut};
  895. next if $Slic3r::Config::Options->{$opt_key}{gui_only};
  896. printf $fh "; %s = %s\n", $opt_key, $Slic3r::Config->serialize($opt_key);
  897. }
  898. }
  899. # close our gcode file
  900. close $fh;
  901. }
  902. sub total_extrusion_volume {
  903. my $self = shift;
  904. return $self->total_extrusion_length * ($self->extruders->[0]->filament_diameter**2) * PI/4 / 1000;
  905. }
  906. # this method will return the supplied input file path after expanding its
  907. # format variables with their values
  908. sub expanded_output_filepath {
  909. my $self = shift;
  910. my ($path, $input_file) = @_;
  911. # if no input file was supplied, take the first one from our objects
  912. $input_file ||= $self->objects->[0]->input_file;
  913. return undef if !defined $input_file;
  914. # if output path is an existing directory, we take that and append
  915. # the specified filename format
  916. $path = File::Spec->join($path, $Slic3r::Config->output_filename_format) if ($path && -d $path);
  917. # if no explicit output file was defined, we take the input
  918. # file directory and append the specified filename format
  919. $path ||= (fileparse($input_file))[1] . $Slic3r::Config->output_filename_format;
  920. my $input_filename = my $input_filename_base = basename($input_file);
  921. $input_filename_base =~ s/\.(?:stl|amf(?:\.xml)?)$//i;
  922. return $Slic3r::Config->replace_options($path, {
  923. input_filename => $input_filename,
  924. input_filename_base => $input_filename_base,
  925. %{ $self->extra_variables },
  926. });
  927. }
  928. 1;