Code:
def theParse():
#=========================================================
# This section below, is to gather all control values for the parse,
# from the controlncv.prg file
# #=========================================================
def ncvzDepth():
with open('c:/PyNC/program files/controlncv.prg','r') as fob:
getcontrolvalues = fob.readlines()
fob.close()
thecontrolncvlist = (getcontrolvalues)
thencvlist = thecontrolncvlist[1]
line = "".join (thencvlist)
dsearch = Find( r' *-*\.*\d\d*\.*\d* *', line)
if dsearch != "":
dstring = (dsearch)
dstrip = (dstring).strip()
list (dstrip)
dword = list (dstring)
if dword [0] .isdigit or dword [1].isdigit:
dlistnum = dword [1: ]
dstringnum = "".join (dlistnum)
dnum = (dstringnum).strip()
d = float (dnum)
return d
else:
d = " "
return d
#==============================================================
#==============================================================
def ncvPlunge():
with open('c:/PyNC/program files/controlncv.prg','r') as fob:
getcontrolvalues = fob.readlines()
fob.close()
thecontrolncvlist = (getcontrolvalues)
thencvlist = thecontrolncvlist[2]
line = "".join (thencvlist)
dsearch = Find( r' *-*\.*\d\d*\.*\d* *', line)
if dsearch != "":
dstring = (dsearch)
dstrip = (dstring).strip()
list (dstrip)
dword = list (dstring)
if dword [0] .isdigit or dword [1].isdigit:
dlistnum = dword [1: ]
dstringnum = "".join (dlistnum)
dnum = (dstringnum).strip()
d = float (dnum)
return d
else:
d = " "
return d
#==============================================================
#==============================================================
def ncvMill():
with open('c:/PyNC/program files/controlncv.prg','r') as fob:
getcontrolvalues = fob.readlines()
fob.close()
thecontrolncvlist = (getcontrolvalues)
thencvlist = thecontrolncvlist[3]
line = "".join (thencvlist)
dsearch = Find( r' *-*\.*\d\d*\.*\d* *', line)
if dsearch != "":
dstring = (dsearch)
dstrip = (dstring).strip()
list (dstrip)
dword = list (dstring)
if dword [0] .isdigit or dword [1].isdigit:
dlistnum = dword [1: ]
dstringnum = "".join (dlistnum)
dnum = (dstringnum).strip()
d = float (dnum)
return d
else:
d = " "
return d
#==============================================================
#==============================================================
def ncvzClear():
with open('c:/PyNC/program files/controlncv.prg','r') as fob:
getcontrolvalues = fob.readlines()
fob.close()
thecontrolncvlist = (getcontrolvalues)
thencvlist = thecontrolncvlist[4]
line = "".join (thencvlist)
dsearch = Find( r' *-*\.*\d\d*\.*\d* *', line)
if dsearch != "":
dstring = (dsearch)
dstrip = (dstring).strip()
list (dstrip)
dword = list (dstring)
if dword [0] .isdigit or dword [1].isdigit:
dlistnum = dword [1: ]
dstringnum = "".join (dlistnum)
dnum = (dstringnum).strip()
d = float (dnum)
return d
else:
d = " "
return d
#==============================================================
#==============================================================
def ncvzLift():
with open('c:/PyNC/program files/controlncv.prg','r') as fob:
getcontrolvalues = fob.readlines()
fob.close()
thecontrolncvlist = (getcontrolvalues)
thencvlist = thecontrolncvlist[5]
line = "".join (thencvlist)
dsearch = Find( r' *-*\.*\d\d*\.*\d* *', line)
if dsearch != "":
dstring = (dsearch)
dstrip = (dstring).strip()
list (dstrip)
dword = list (dstring)
if dword [0] .isdigit or dword [1].isdigit:
dlistnum = dword [1: ]
dstringnum = "".join (dlistnum)
dnum = (dstringnum).strip()
d = float (dnum)
return d
else:
d = " "
return d
#==============================================================
#==============================================================
def ncvrapiDown():
with open('c:/PyNC/program files/controlncv.prg','r') as fob:
getcontrolvalues = fob.readlines()
fob.close()
thecontrolncvlist = (getcontrolvalues)
thencvlist = thecontrolncvlist[6]
line = "".join (thencvlist)
dsearch = Find( r' *-*\.*\d\d*\.*\d* *', line)
if dsearch != "":
dstring = (dsearch)
dstrip = (dstring).strip()
list (dstrip)
dword = list (dstring)
if dword [0] .isdigit or dword [1].isdigit:
dlistnum = dword [1: ]
dstringnum = "".join (dlistnum)
dnum = (dstringnum).strip()
d = float (dnum)
return d
else:
d = " "
return d
#==============================================================
# #=======================================================================
# # Place calls to the ncv search functions, here;
# #=======================================================================
# #=======================================================================
# # Now create some usefull 'macro' style, output lines from 'ncv values'
# # It's just a matter of deciding how you want the lines to read.
# #=======================================================================
def theplungeLine():
floatplungez = ncvPlunge()
plungez = str(floatplungez)
plungezletter = " F"
theplungeline = plungezletter + plungez + '\n'
return theplungeline
# #
def themilLine():
floatmill = ncvMill()
millxy = str(floatmill)
milletter = " F"
themilline = milletter + millxy + '\n'
return themilline
# #
def thezdepthLine():
floatdepthz = ncvzDepth()
depthz = str(floatdepthz)
depthzletter = " Z"
thezdepthline = depthzletter + depthz + theplungeLine()
return thezdepthline
# #
def thezclearLine():
floatclearz = ncvzDepth()
clearz = str(floatclearz)
clearzletter = " G0 Z"
thezclearline = clearzletter + clearz + '\n'
return thezclearline
# #
def thezheightLine():
floatliftz = ncvzClear()
liftz = str(floatliftz)
liftzletter = " G0 Z"
thezheightline = liftzletter + liftz + '\n'
return thezheightline
# #
def thezdownLine():
floatrapidz = ncvrapiDown()
rapidz = str(floatrapidz)
rapidzletter = " G0 Z"
thezdownline = rapidzletter + rapidz + '\n'
return thezdownline
# #==================================================================
# # Place the full functions for the G-code file line searches, here;
# #==================================================================
def gSearch():
gsearch = Find( r'G *\.*\d\d*\.*\d* *', line)
if gsearch != "":
gstring = (gsearch)
gstrip = (gstring).strip()
list (gstrip)
gword = list (gstrip)
if gword [1] .isdigit:
gletter = " G"
glistnum = gword [1: ]
gstringnum = "".join (glistnum)
gnum = (gstringnum).strip()
g = float (gnum)
gout = gletter + gnum
return gout,g
else:
gletter = ''
gnum = ''
g = ''
gout = ''
return gout,g
# #========================================================
# #========================================================
def mSearch():
msearch = Find( r'M *\.*\d\d*\.*\d* *', line)
if msearch != "":
mstring = (msearch)
mstrip = (mstring).strip()
list (mstrip)
mword = list (mstrip)
if mword [1] .isdigit:
mletter = " M"
mlistnum = mword [1: ]
mstringnum = "".join (mlistnum)
mnum = (mstringnum).strip()
m = float (mnum)
mout = mletter + mnum
return mout,m
else:
mletter = ""
mnum = ""
m = ""
mout = mletter + mnum
return mout,m
# #========================================================
# #========================================================
def xSearch():
xsearch = Find( r'X *-*\.*\d\d*\.*\d* *', line)
if xsearch != "":
xstring = (xsearch)
xstrip = (xstring).strip()
list (xstrip)
xword = list (xstring)
if xword [1].isdigit or xword [2].isdigit:
xletter = " X"
xlistnum = xword [1: ]
xstringnum = "".join (xlistnum)
xnum = (xstringnum).strip()
x = float (xnum)
xout = xletter + xnum
return xout,x
else:
xletter = ""
xnum = ""
x = ""
xout = xletter + xnum
return xout,x
# #========================================================
# #========================================================
def ySearch():
ysearch = Find( r'Y *-*\.*\d\d*\.*\d* *', line)
if ysearch != "":
ystring = (ysearch)
ystrip = (ystring).strip()
list (ystrip)
yword = list (ystring)
if yword [1] .isdigit or yword [2].isdigit:
yletter = " Y"
ylistnum = yword [1: ]
ystringnum = "".join (ylistnum)
ynum = (ystringnum).strip()
y = float (ynum)
yout = yletter + ynum
return yout,y
else:
yletter = ""
ynum = ""
y = ""
yout = yletter + ynum
return yout,y
# #========================================================
# #========================================================
def zSearch():
zsearch = Find( r'Z *-*\.*\d\d*\.*\d* *', line)
if zsearch != "":
zstring = (zsearch)
zstrip = (zstring).strip()
list (zstrip)
zword = list (zstring)
if zword [1] .isdigit or zword [2].isdigit:
zletter = " Z"
zlistnum = zword [1:8]
zstringnum = "".join (zlistnum)
znum = (zstringnum).strip()
z = float (znum)
zout = zletter + znum
linez = z
return zout,z
else:
zletter = ""
znum = ""
z = ""
zout = zletter + znum
return zout,z
# #========================================================
# #========================================================
def iSearch():
isearch = Find( r'I *-*\.*\d\d*\.*\d* *', line)
if isearch != "":
istring = (isearch)
istrip = (istring).strip()
list (istrip)
iword = list (istring)
if iword [1] .isdigit or iword [2].isdigit:
iletter = " I"
ilistnum = iword [1: ]
istringnum = "".join (ilistnum)
inum = (istringnum).strip()
i = float (inum)
iout = iletter + inum
return iout,i
else:
iletter = ""
inum = ""
i = ""
iout = iletter + inum
return iout,i
# #========================================================
# #========================================================
def jSearch():
jsearch = Find( r'J *-*\.*\d\d*\.*\d* *', line)
if jsearch != "":
jstring = (jsearch)
jstrip = (jstring).strip()
list (jstrip)
jword = list (jstring)
if jword [1] .isdigit or jword [2].isdigit:
jletter = " J"
jlistnum = jword [1: ]
jstringnum = "".join (jlistnum)
jnum = (jstringnum).strip()
j = float (jnum)
jout = jletter + jnum
return jout,j
else:
jletter = ""
jnum = ""
j = ""
jout = jletter + jnum
return jout,j
# #========================================================
# #========================================================
def fSearch():
fsearch = Find( r'F *\.*\d\d*\.*\d* *', line)
if fsearch != "":
fstring = (fsearch)
fstrip = (fstring).strip()
list (fstrip)
fword = list (fstrip)
if fword [1] .isdigit:
fletter = " F"
flistnum = fword [1: ]
fstringnum = "".join (flistnum)
fnum = (fstringnum).strip()
f = float (fnum)
fout = fletter + fnum
return fout,f
else:
fletter = ""
fnum = ""
f = ""
fout = fletter + fnum
return fout,f
# #=======================================================================
# # create variables with initialising code strings if you wish to.
# # These can also be assembled using 'additional raw_input' from the user
# #=======================================================================
platform = ' G54\n'
plane = ' G17\n'
units = ' G21\n'
ijdists = ' G90\n'
initialines = platform + plane + units + ijdists
# #=======================================================================
# # create variables with file end, code strings if you wish to.
# # These can also be assembled using 'additional raw_input' from the user
# #=======================================================================
raisez = ' G0 Z15.00\n'
gotohome = ' G0 X0.00 Y0.00\n'
endfile = ' M30\n ()\n'
filendlines = raisez + gotohome + endfile
# #=========================================================
# Now open 'parsefile.prg' to read each line (1 at a time)
# #=========================================================
fileIN = open('c:/PyNC/program files/parsefile.prg','r')
line1 = fileIN.readline()
cont = 0
initialcontrol = 0
endoutput =0
# #============================================================
# # And we need an output file to write to.
# # The file will be created in the 'output files' folder.
# # To make sure the file is good to go,
# # a check is made for a possible 'in use already', exception.
# # May as well write some initialising lines to the file,
# # at the same time.
# #============================================================
anexception=0
fob =''
if initialcontrol == 0:
cont = 1
initialcontrol = 1
try:
fob = open('c:/PyNC/output files/001PyNC.tap','w')
try:
fob.writelines(initialines)
print initialines
finally:
anexception=0
except IOError:
anexception=1
pass
# #==========================================================
# # Now for the 'while' loop
# # Pay close attention to the new line indentation,
# # from here on in.
# # Using these: # # can help check you're indentation.
# #==========================================================
while line1 != '' and anexception == 0:
# # new indent
line = line1.upper()
# #==============================================================
# # Place calls to the ncv search functions, here;
# #==============================================================
zdepth = ncvzDepth()
zclear = ncvzClear()
zheight = ncvzLift()
zdownmin = ncvrapiDown()
# #==============================================================
# # Place calls to word and macro line creation functions, here;
# #==============================================================
plunge = theplungeLine()
mill = themilLine()
#==========================
zdownline = thezdownLine()
#==========================
zheightline = thezheightLine()
#==========================
zdepthline = thezdepthLine()
#==========================
zclearline = thezclearLine()
#==========================
# #===============================================================
# # extract the G-code words from file line searches, here;
# #===============================================================
getg = ''
lineg = ''
mygsearch = gSearch()
if mygsearch != ('',''):
mygsearchlist = list (mygsearch)
getg = mygsearchlist [0]
thegnum = mygsearchlist [1: ]
lineg = thegnum[0]
gpresent = 0
g = None
if lineg !='':
gpresent = 1
g = lineg
# #=======================
getm = ''
linem = ''
mymsearch = mSearch()
if mymsearch != ('',''):
mymsearchlist = list (mymsearch)
getm = mymsearchlist [0]
themnum = mymsearchlist [1: ]
linem = themnum[0]
mpresent = 0
m = None
if linem !='':
mpresent = 1
m = linem
# #=======================
getx = ''
linex = ''
myxsearch = xSearch()
if myxsearch != ('',''):
myxsearchlist = list (myxsearch)
getx = myxsearchlist [0]
thexnum = myxsearchlist [1: ]
linex = thexnum[0]
xpresent = 0
x = None
if linex !='':
xpresent = 1
x = linex
# #=======================
gety = ''
liney = ''
myysearch = ySearch()
if myysearch != ('',''):
myysearchlist = list (myysearch)
gety = myysearchlist [0]
theynum = myysearchlist [1: ]
liney = theynum[0]
ypresent = 0
y = None
if liney !='':
ypresent = 1
y = liney
# #=======================
getz = ''
linez = ''
myzsearch = zSearch()
if myzsearch != ('',''):
myzsearchlist = list (myzsearch)
getz = myzsearchlist [0]
theznum = myzsearchlist [1: ]
linez = theznum[0]
ypresent = 0
z = None
if linez !='':
zpresent = 1
z = linez
# #=======================
geti = ''
linei = ''
myisearch = iSearch()
if myisearch != ('',''):
myisearchlist = list (myisearch)
geti = myisearchlist [0]
theinum = myisearchlist [1: ]
linei = theinum[0]
ipresent = 0
i = None
if linei !='':
ipresent = 1
i = linei
# #=======================
getj = ''
linej = ''
myjsearch = jSearch()
if myjsearch != ('',''):
myjsearchlist = list (myjsearch)
getj = myjsearchlist [0]
thejnum = myjsearchlist [1: ]
linej = thejnum[0]
jpresent = 0
j = None
if linej !='':
jpresent = 1
j = linej
# #=======================
getf = ''
linef = ''
myfsearch = fSearch()
if myfsearch != ('',''):
myfsearchlist = list (myfsearch)
getf = myfsearchlist [0]
thefnum = myfsearchlist [1: ]
linef = thefnum[0]
fpresent = 0
f = None
if linef !='':
fpresent = 1
f = linef
# #=============================================================
# # This is the output line variable
# #=============================================================
outline = ''
# #=============================================================
# # This section is where you place All line arguments
# #=============================================================
# # Below are a few G-code line arguments.
# # MANY arguments would be needed for complete success.
# # On average, as many as 12 would be needed.
# # The 1st arg., gets the user's 'mill speed' added,
# # but only for 1 variety of G-code line seen.
# # Every argument you create will result in a different output.
# #=============================================================
if gpresent == 1 and g > 0:
if g < 4:
outline = getg+getx+gety+getz+geti+getj+mill
fob.writelines(outline)
print outline
if gpresent == 1 and g == 0:
outline = getg+getx+gety+getz+'\n'
fob.writelines(outline)
print outline
if gpresent == 0 and mpresent == 0:
outline = getg+getx+gety+getz+geti+getj+'\n'
fob.writelines(outline)
print outline
if mpresent == 1:
outline = getm+'\n'
fob.writelines(outline)
print outline
# #===========================================================
# # When all line arguments are done;
# # Clear all extraction line variables before the 'loop back'
# #===========================================================
gpresent=mpresent=xpresent=ypresent=zpresent=ipresent=jpresent=fpresent=0
getg=getm=getx=gety=getz=geti=getj=getf=''
lineg=linem=linex=liney=linez=linei=linej=linef=''
g=m=x=y=z=i=j=f=None
outline = ''
# # The 'while loop' is now restarted with;
line1 = fileIN.readline()
# #=====================================================================
# # 'while' looping will end, when there are no more file lines to read.
# # The indentation shifts left, (we are no longer in the 'while' loop)
# #=====================================================================
if endoutput == 1 and anexception == 0:
print filendlines
fob.writelines(filendlines)
fob.close()
print "\n\nThe Import File has been processed.\n\n"
if endoutput == 0 and anexception == 0:
print filendlines
fob.writelines(filendlines)
fob.close()
print "\n\nThe Import File has been processed.\n\n"
if anexception == 1:
cls()
print """
'PyNC hasn't crashed'
This is a 'file open ERROR' message.
The file called '001PyNC.tap', cannot be written to.
It must be available for writing to at all times.
Another program may be using it. Probably 'Mach3'.
If you have loaded '001PyNC.tap' to Mach,
check that you have.. 'closed G-Code'.
Then, try processing your code file again.
You can rename the file '001PyNC.tap' if you want to.
You can load the newly named file to Mach, instead of
'001PyNC.tap'.
You can leave that file loaded in Mach.
PyNC will automatically create a new output file'
"""
#=================================================================
# End of theParse, returning to the menu.
#=================================================================