Eliminate Redundant Z axis in Slicer

I’ve created a slicer and it generated G code that looks like this and has the Z axis on every line


I’d like to remove the redundancy and have the Z display once at the beginning of every layer, like this


How can it be accomplished?

To slice or splice that is the question! :rofl:

that’s what happens when I don’t have enough coffee :slight_smile: … Thank you for pointing out my misspelling in such a polite and professional manor.

OK, so you could try the following:

  • split your g-code file line by line
  • then split each line by white space, which will give you 4 individual parts
    • only for the first line, put the last piece on the line before it and omit it for the rest of the lines
    • keep the other three pieces and rejoin them with white space in between

If you upload a sample file, I can show you how.

Are you talking about doing this manually or format it in grasshopper?


You can use notepad or excel.
Try to import your file to notepad and delete the z axis by replace command,ctrl+h, (find Z13.350 and repace by nothing - leave the box blank) and save your edited file.

Something like this?

Eliminate Redundant Z axis in Slicer.gh (14.3 KB)

Something like that, but need it to work every time the Z changes

End Result needs to look like this

@ Nosorozec With thousands of lines of coordinates…Too time consuming to do it Manually

with some regex:

Maybe you can adapt it for you requirements.
gcode.gh (6.0 KB)


@oberbichler Thank you, this is perfect.

@oberbichler II know very little python … made one small edit to your code and your gocod.gh works great except for a bug or two …

Extra spaces where they are unwanted

Also the text in the last image is supposed to have parentheses around it, but they disappear.
. Any thoughts on how to fix it?
Much appreciated

I am a bit busy at the moment but maybe I will find some time during the next days. Can you send me an example GH file including the gcode to reproduce this images?

BTW: What is the parentheses code doing? I only use a basic subset of gcode.


I have a Friend that knows python he was able to fix it, see below …as for the Parentheses The printer doesn’t read and information within the Parentheses, that’s where notes for the user are added.

from Rhino.Geometry import *
import re

arg_pattern = re.compile(r"(?P[A-Z])\s*(?P\d*(?:.\d*)?)", re.IGNORECASE)

state = dict(
G = None, # Use G1 as default command from the beginning
M = None,
S = None,
X = None, # Outcomment attributes to disable ‘memory’
Y = None,
Z = None,

gcode =

def add_token(tokens, key, value):
if key in state:
if state[key] == value:
state[key] =value

token = str(key) + str(value)

for line in lines:
tokens =

for t in line.split():
    token_match = re.match(arg_pattern, t)
    if token_match:
        key, val = token_match.groups()
        if key and val:
            add_token(tokens, key, val)

new_line = ' '.join(tokens)

if new_line: