# How to generate gcode only for first layer?

As first layer is so important, I am looking for an easy way to generate the gcode to print just the first layer. I see that with Slic3r you can cut from a Z But for test purposes I prefer just selecting a number of layers to be generated so I can easily generate different "first layer(s) tests" with different first layer(s) settings (width, height, speed, flow....) The only way I achieve it is editing the gcode. Any help? Thanks

• Are you doing this to set Z-height, bed levelling, etc., or do you have reasons to evaluate the first layer for each particular object being printed? – Carl Witthoft Jan 12 '18 at 13:36

I understand your question like this:

I know I could cut the mesh and just slice the bottom of my model, but since I am interested in a given number of layers and the heigh of a layer may change according to settings (e.g.: 0.2mm, 0.1mm, 0.05mm...), I want to find a way to generate an arbitrary number of layers from the full model. I use slic3r.

If my understanding is correct, then you can achieve what you want with a few steps.

Use verbose GCODE

The setting is under "Print settings → Output Options". This will output gcode with comments in it.

Save the finishing gcode of a valid printing job

Basically, open a valid gcode file, and save the last few lines (comments will help you to understand which ones, it changes from printer to printer) in a separate file (gcode.tail). These lines are typically those that move away the nozzle from the print, disable the heating element, the steppers and the part cooling fan.

Prepare the first-lines.sh script

#! /usr/bin/env sh
sed -e '/move to next layer (3)/,$d'$1 > /tmp/gcode.tmp
echo ~/gcode.tail >> /tmp/gcode.tmp
echo /tmp/gcode.tmp


What this script does is:

• take a file name from the command line (\$1) and savie into gcode.tmp only the part of it up to and excluding the line saying "move to the next layer (3)" (you should actually use the number of layers you actually want here, 3 is just an example). Again, the presence of such a line depends from you generating "verbose gcode".
• append to gcode.tmp the content of the file gcode.tail (here replace ~/ with the actual path on your machine.
• output as a stream the full content of gcode.tmp

Set your printer to automatically run the script onto the generated gcode

This setting is again under "Print settings → Output Options". You have to type in the full path to first-lines.sh. Also remember to make the script executable (chmod +x first-lines.sh).

You can also hover over the textbox to get additional information of how you can access slic3r variables there (for example you may want to read the layer height from the settings and compute within the script the number of layers you want to keep).

Profit

:)

Final notes:

• I tried the sed command and have post-processing scripts running on my gcode myself, so it should work, but I haven't tried the full procedure myself, if you encounter bugs please leave a comment so I can fix the answer for everybody. :)
• I use slic3r Prusa Edition (I believe these settings are the same, but just in case... you may wish to download that version.
• All of the above should work out-of-the-box on all mainstream Linux distributions and OSX. For windows, it has been suggested in the comments to install CygWin.
• Since this procedure still slices the full model and then throw away most of it, you could make it faster by only slicing a reasonably thick "bottom part" of your model. For example: say that you know you will never want to print more than 5 layers and never with a layer height past 0.3mm... in this case you could only keep the bottom 2mm of your model and you'd be safe for all other combinations of layers and layer heights. Don't keep exactly 1.5mm though, as this is likely to generate a different top layer than the one in the full model.

Good luck! :)

• Thanks mac. I got an easy workaround as well from the Slic3r Github. The suggested solution is quite straight forward: Just adding a small Perl script as postprocessing that drops all gcode of layers not to be printed. – Luis Rosety Jan 12 '18 at 8:54
• That's pretty much the same suggestion than mine, just slower (as it parses the full file rather than splicing it. :) (and less readable because is perl ;) ) – mac Jan 12 '18 at 9:04
• OSX uses bash by default in Terminal; for Windows I recommend installing CygWin. – Carl Witthoft Jan 12 '18 at 13:37
• @CarlWitthoft - Thanks, added in the main body of the answer. – mac Jan 12 '18 at 16:33
• @LuisRosety - Since you solved the issue with this method (post-processing of the gcode via slic3r settings), consider either approving this answer or writing your own and approving that instead, so that others will have a clear indication this problem has a solution. :) – mac Jan 16 '18 at 0:46

You could just insert an M30 at the end of layer 1, and the job will end there.

• I have only one suggestion to add. Appart from stopping the gcode execution it would be advised stopping heaters, fan etc. M107 M104 S0 ;extruder heater off M140 S0 ;heated bed heater off (if you have it) G91 ;relative positioning G1 E-1 F300 ;retract the filament a bit before lifting the nozzle M30 I am not a gcode expert but I think it would be convenient doing something like this. – Luis Rosety Jan 13 '18 at 17:59
• That's correct @LuisRosety. Throwing the end gcode in the middle of the file achieves the same result than properly splicing the file (as suggested in my answer). The limitations of just throwing it in the file like this is that you get to keep around hundreds of Megabytes of unused GCODE (beside having to do it manually, which is what the OP wanted to avoid). – mac Jan 13 '18 at 20:37
• True, this is a manual solution. I offer it because it is much simpler than manually deleting thousands of lines of gcode. One could also add M104 and M140 and other commands to return the printer to a standard state, but the implication was that the user will tweak settings and re-run repeatedly until the proper parameters are identified. – Davo Jan 14 '18 at 15:01