3D Engraving Gcode Z-Inversion Python Program

This is different - it’s not a question!
It’s a free offer!
I just finished whipping up a little Python program that looks like it succeeded at inverting the z-axis values (that lie below zero) about a Z-level mid-point.
Back story…
Just got a SainSmart 3018 up-n-running, made a fine-looking ‘dragon.nc’ in a block of hardwood (after using G-Code_Ripper to scale down Z a bit).
I’m also a potter - saw that the dragon would be better as a negative - could then use it to press clay into - make tiles.
BUT… G-Code_Ripper did not do ‘negative scaling’. :frowning:
SO… after a couple hours with Geany and python, I have a program. :slight_smile:
How do I get it to you? Where do we upload things? Can you contact me if you want to have a look at it? It’s really small - doesn’t do much but seems to do it well. I say ‘seems’ because my 3018 is still busy for a few hours so haven’t actually tested it yet, but the gcode file looks like it did the job.

OK - got a clue as to how to post code, so here goes…

#!/usr/bin python

#THIS PROGRAM offered by MakerMarlyn to fellow Makers.

#It can pick out the 'Z' if 'Zxxx' or 'Fxxx' is the last
# thing on a line - maybe following X,Y commands.
#Fortunately, this seems to be S.O.P. in G-code world.

# goes through a gcode file (fileIN) ,
# gets Z-value out of a line
# gets 'most negative' value (minZ)
# divides it by 2 - assigns to 'zMid'

#THEN...
# goes through fileIN again,
# if Z is below zero, flips Z about zMid,
# writes line to fileOUT = ("inv_" + fileIN).

# YOUR FILE NAME GOES HERE...
#Oh yes, it is good if this program and your file are in the same directory/folder.
fileIN = "dragon.nc"

fileOUT="inv_"+fileIN
minZ=0.0

fIN=open(fileIN,"r")
fOUT=open(fileOUT,"w")
#what's the minimum Z value?
with fIN:
    for line in fIN:
       L=line.strip()
        try:
            zSplit=L.split("Z")
            try:
                fSplit=zSplit[1].split("F")
                zVal=float(fSplit[0])
            except:
                zVal=float(zSplit[1])
                pass
                
            if (zVal<minZ): minZ=zVal
        except: pass
fIN.close()    

print ("minZ=",minZ)
zMid=float(minZ/2.0)
print ("zMid=",zMid)

fIN=open(fileIN,"r")
with fIN:
    for line in fIN:
       L=line.strip()
        eol="\n"
        zVal=0
        try:
            zSplit=L.split("Z")
            try:
                fSplit=zSplit[1].split("F")
                zVal=float(fSplit[0])
                eol="F"+fSplit[1]+"\n"
            except:
                zVal=float(zSplit[1])
                pass
                
            if (zVal<0):
                zVal=(zMid-zVal)+zMid    
# set the precision -  '4f' is four places, '3f' is three places.                
               L=zSplit[0]+ "Z"+"{0:.3f}".format(zVal) 
                       
        except:
            pass
               
       L=L+eol
        fOUT.write(L)
        
fIN.close()
fOUT.close()     


2 Likes

Welcome! Thanks for suggesting sharing!

If it’s a single python script, you could just paste it into a post, like this:

```python
<paste your python between two lines starting with triple single back quotes
```

That will show the program here, kind of like this:

def hello():
    print("hello, world")
hello()

If you think this is something you would like to get bug reports on and fix over time, it could be worth posting it to GitHub and posting about it here.

2 Likes

Addendum… ‘Oh, what the heck, may as well scale the Z too.’

#!/usr/bin python

#THIS PROGRAM offered by MakerMarlyn to fellow Makers.

#It can pick out the 'Z' if it's the last 
# thing on a line , or if it's only followed by the 'F'eed rate.
#Fortunately, this seems to be S.O.P. in G-code world.

# goes through a gcode file (fileIN) ,
# gets Z-value out of a line
# gets 'most negative' value (minZ)
# SCALES it (Zscale)
# divides it by 2 - assigns to 'zMid'

#THEN...
# goes through fileIN again,
# if Z is below zero, (scales it and) flips it about zMid,
# writes line to fileOUT = ("inv_" + fileIN).

#####################
fileIN = "dragon.nc"
Zscale=1.0
#####################

fileOUT="inv_"+fileIN
minZ=0.0

fIN=open(fileIN,"r")
fOUT=open(fileOUT,"w")
#what's the minimum Z value?
with fIN:
    for line in fIN:
        L=line.strip()
        try:
            zSplit=L.split("Z")
            try:
                fSplit=zSplit[1].split("F")
                zVal=float(fSplit[0])
            except:
                zVal=float(zSplit[1])
                pass
                
            if (zVal<minZ): minZ=zVal
        except: pass
fIN.close()    

print ("minZ=",minZ)
minZ=minZ*Zscale
print ("minZ scaled=",minZ)
zMid=float(minZ/2.0)
print ("zMid scaled=",zMid)

fIN=open(fileIN,"r")
with fIN:
    for line in fIN:
        L=line.strip()
        eol="\n"
        zVal=0
        try:
            zSplit=L.split("Z")
            try:
                fSplit=zSplit[1].split("F")
                zVal=float(fSplit[0])*Zscale
                eol="F"+fSplit[1]+"\n"
            except:
                zVal=float(zSplit[1])*Zscale
                pass
                
            if (zVal<0.0):
                zVal=(zMid-zVal)+zMid    
# set the precision -  '4f' is four places, '3f' is three places.                
                L=zSplit[0]+ "Z"+"{0:.3f}".format(zVal) 
                       
        except:
            pass
               
        L=L+eol
        fOUT.write(L)
        
fIN.close()
fOUT.close()     

3 Likes