################################################################################
# nc_read.py
#
# Base class for NC code parsing
#
# Hirutso Enni, 2009-01-13
################################################################################
class Parser:
def __init__(self):
self.currentx = 0.0
self.currenty = 0.0
self.currentz = 0.0
self.absolute_flag = True
############################################################################
## Internals
def files_open(self, name, oname=None):
if (oname == None ):
oname = (name+'.nc.xml')
self.file_in = open(name, 'r')
self.file_out = open(oname, 'w')
self.file_out.write('\n')
self.file_out.write('\n')
def files_close(self):
self.file_out.write('\n')
self.file_in.close()
self.file_out.close()
def readline(self):
self.line = self.file_in.readline().rstrip()
if (len(self.line)) : return True
else : return False
def write(self, s):
self.file_out.write(s)
############################################################################
## Xml
def begin_ncblock(self):
self.file_out.write('\t\n')
def end_ncblock(self):
self.file_out.write('\t\n')
def add_text(self, s, col=None, cdata=False):
s.replace('&', '&')
s.replace('"', '"')
s.replace('<', '<')
s.replace('>', '>')
if (cdata) : (cd1, cd2) = ('')
else : (cd1, cd2) = ('', '')
if (col != None) : self.file_out.write('\t\t'+cd1+s+cd2+'\n')
else : self.file_out.write('\t\t'+cd1+s+cd2+'\n')
def set_mode(self, units=None):
self.file_out.write('\t\t\n')
def set_tool(self, number=None):
self.file_out.write('\t\t\n')
def begin_path(self, col=None):
if (col != None) : self.file_out.write('\t\t\n')
else : self.file_out.write('\t\t\n')
def end_path(self):
self.file_out.write('\t\t\n')
def add_line(self, x=None, y=None, z=None, a=None, b=None, c=None):
if (x == None and y == None and z == None and a == None and b == None and c == None) : return
self.file_out.write('\t\t\t\n')
def add_lathe_increment_line(self, u=None, w=None):
# needed for representing U and W moves in lathe code- these are non modal incremental moves
# U == X and W == Z
if (u == None and w == None ) : return
self.file_out.write('\t\t\t\n')
def add_arc(self, x=None, y=None, z=None, i=None, j=None, k=None, r=None, d=None):
if (x == None and y == None and z == None and i == None and j == None and k == None and r == None and d == None) : return
self.file_out.write('\t\t\t\n')
def incremental(self):
self.absolute_flag = False
def absolute(self):
self.absolute_flag = True