<p>I understand your question like this:</p>

<blockquote>
  <p>I know I could cut the mesh and just slice the bottom of my model, but since I am interested in a given <strong>number of layers</strong> 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.</p>
</blockquote>

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

<p><strong>Use verbose GCODE</strong></p>

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

<p><strong>Save the finishing gcode of a valid printing job</strong></p>

<p>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 (<code>gcode.tail</code>).  These lines are typically those that move away the nozzle from the print, disable the heating element, the steppers and the part cooling fan.</p>

<p><strong>Prepare the <code>first-lines.sh</code> script</strong></p>

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

<p>What this script does is:</p>

<ul>
<li>take a file name from the command line (<code>$1</code>) and savie into <code>gcode.tmp</code> 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, <code>3</code> is just an example).  Again, the presence of such a line depends from you generating "verbose gcode".</li>
<li>append to <code>gcode.tmp</code> the content of the file <code>gcode.tail</code> (here replace <code>~/</code> with the actual path on your machine.</li>
<li>output as a stream the full content of <code>gcode.tmp</code></li>
</ul>

<p><strong>Set your printer to automatically run the script onto the generated gcode</strong></p>

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

<p>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).</p>

<p><strong>Profit</strong></p>

<p>:)</p>

<p>Final notes:</p>

<ul>
<li>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. :)</li>
<li>I use slic3r Prusa Edition (I believe these settings are the same, but just in case... you may wish to <a href="https://github.com/prusa3d/Slic3r/releases" rel="nofollow noreferrer">download</a> that version.</li>
<li>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 <a href="http://www.cygwin.com/" rel="nofollow noreferrer">CygWin</a>.</li>
<li>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 <em>exactly</em> 1.5mm though, as this is likely to generate a different top layer than the one in the full model.</li>
</ul>

<p>Good luck! :)</p>

CC BY-SA 3.0
2018-01-16T00:46:06.280Z
@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
CC BY-SA 3.0
2018-01-12T08:54:44.627Z
Thanks mac.   
I got an easy workaround as well from the [Slic3r Github](https://github.com/alexrj/Slic3r/issues/2755#issuecomment-357093913).     
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
CC BY-SA 3.0
2018-01-12T09:04:08.940Z
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
CC BY-SA 3.0
2018-01-12T13:37:47.150Z
OSX uses bash by default in Terminal;  for Windows I recommend installing CygWin.
Carl Witthoft
CC BY-SA 3.0
2018-01-12T16:33:25.607Z
@CarlWitthoft - Thanks, added in the main body of the answer.
mac
CC BY-SA 3.0
2018-01-11T23:27:03.123Z
2018-01-12T16:32:11.727Z
2018-01-12T16:32:11.727Z
mac
<p>You could just insert an M30 at the end of layer 1, and the job will end there.</p>

CC BY-SA 3.0
2018-01-13T20:37:25.807Z
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
CC BY-SA 3.0
2018-01-14T15:01:10.640Z
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
CC BY-SA 3.0
2018-01-13T17:59:27.343Z
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
CC BY-SA 3.0
2018-01-12T21:04:53.217Z
2018-01-12T21:04:53.217Z
Davo
<p>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</p>

CC BY-SA 3.0
2018-01-12T13:36:33.703Z
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
<slic3r>
How to generate gcode only for first layer?
Luis Rosety
# How to generate gcode only for first layer?

(Asked by: username_0 on 2018-01-11)

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

**Tags:** slic3r

> **Comments:**
> **username_1**: 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?  

---

## 2 answers

### Answer by username_2 on 2018-01-11. Score: 1 | Accepted answer

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](https://github.com/prusa3d/Slic3r/releases) 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](http://www.cygwin.com/).
* 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! :)

> **Comments:**
> **username_0**: Thanks mac.    I got an easy workaround as well from the [Slic3r Github](https://github.com/alexrj/Slic3r/issues/2755#issuecomment-357093913).      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.  
> **username_2**: 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 ;) )  
> **username_1**: OSX uses bash by default in Terminal;  for Windows I recommend installing CygWin.  
> **username_2**: @username_1 - Thanks, added in the main body of the answer.  
> **username_2**: @username_0 - 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. :)  

---

### Answer by username_3 on 2018-01-12. Score: 0

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

> **Comments:**
> **username_0**: 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.  
> **username_2**: That's correct @username_0.  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).  
> **username_3**: 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.  

---

