Advertisement
danfalck

mill_read.py

Oct 21st, 2011
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.50 KB | None | 0 0
  1. ################################################################################
  2. # iso_read.py
  3. #
  4. # Simple ISO NC code parsing
  5. #
  6. # Hirutso Enni, 2009-01-13
  7.  
  8. import freecad_read as nc
  9. import re
  10. import sys
  11.  
  12. ################################################################################
  13. class ParserIso(nc.Parser):
  14.  
  15.     def __init__(self):
  16.         nc.Parser.__init__(self)
  17.  
  18.         self.pattern_main = re.compile('([(!;].*|\s+|[a-zA-Z0-9_:](?:[+-])?\d*(?:\.\d*)?|\w\#\d+|\(.*?\)|\#\d+\=(?:[+-])?\d*(?:\.\d*)?)')
  19.  
  20.         #if ( or ! or ; at least one space or a letter followed by some character or not followed by a +/- followed by decimal, with a possible decimal point
  21.          #  followed by a possible deimcal, or a letter followed by # with a decimal . deimcal
  22.         # add your character here > [(!;] for comments char
  23.         # then look for the 'comment' function towards the end of the file and add another elif
  24.  
  25.     def ParseWord(self, word):
  26.         if (word[0] == 'A' or word[0] == 'a'):
  27.             self.col = "axis"
  28.             self.a = eval(word[1:])
  29.             self.move = True
  30.         elif (word[0] == 'B' or word[0] == 'b'):
  31.             self.col = "axis"
  32.             self.b = eval(word[1:])
  33.             self.move = True
  34.         elif (word[0] == 'C' or word[0] == 'c'):
  35.             self.col = "axis"
  36.             self.c = eval(word[1:])
  37.             self.move = True
  38.         elif (word[0] == 'F' or word[0] == 'f'):
  39.             self.col = "axis"
  40.             self.f = eval(word[1:])
  41.             self.move = True
  42.         elif (word == 'G0' or word == 'G00' or word == 'g0' or word == 'g00'):
  43.             self.path_col = "rapid"
  44.             self.col = "rapid"
  45.             self.arc = 0
  46.         elif (word == 'G1' or word == 'G01' or word == 'g1' or word == 'g01'):
  47.             self.path_col = "feed"
  48.             self.col = "feed"
  49.             self.arc = 0
  50.         elif (word == 'G2' or word == 'G02' or word == 'g2' or word == 'g02' or word == 'G12' or word == 'g12'):
  51.             self.path_col = "feed"
  52.             self.col = "feed"
  53.             self.arc = -1
  54.         elif (word == 'G3' or word == 'G03' or word == 'g3' or word == 'g03' or word == 'G13' or word == 'g13'):
  55.             self.path_col = "feed"
  56.             self.col = "feed"
  57.             self.arc = +1
  58.         elif (word == 'G10' or word == 'g10'):
  59.             self.no_move = True                
  60.         elif (word == 'L1' or word == 'l1'):
  61.             self.no_move = True
  62.         elif (word == 'G61.1' or word == 'g61.1' or word == 'G61' or word == 'g61' or word == 'G64' or word == 'g64'):
  63.             self.no_move = True
  64.         elif (word == 'G20' or word == 'G70'):
  65.             self.col = "prep"
  66.             self.set_mode(units=25.4)
  67.         elif (word == 'G21' or word == 'G71'):
  68.             self.col = "prep"
  69.             self.set_mode(units=1.0)
  70.         elif (word == 'G81' or word == 'g81'):
  71.             self.drill = True
  72.             self.no_move = True
  73.             self.path_col = "feed"
  74.             self.col = "feed"
  75.         elif (word == 'G82' or word == 'g82'):
  76.             self.drill = True;
  77.             self.no_move = True
  78.             self.path_col = "feed"
  79.             self.col = "feed"
  80.         elif (word == 'G83' or word == 'g83'):
  81.             self.drill = True
  82.             self.no_move = True
  83.             self.path_col = "feed"
  84.             self.col = "feed"
  85.         elif (word == 'G90' or word == 'g90'):
  86.             self.absolute()
  87.         elif (word == 'G91' or word == 'g91'):
  88.             self.incremental()
  89.         elif (word[0] == 'G') : col = "prep"
  90.         elif (word[0] == 'I' or word[0] == 'i'):
  91.             self.col = "axis"
  92.             self.i = eval(word[1:])
  93.             self.move = True
  94.         elif (word[0] == 'J' or word[0] == 'j'):
  95.             self.col = "axis"
  96.             self.j = eval(word[1:])
  97.             self.move = True
  98.         elif (word[0] == 'K' or word[0] == 'k'):
  99.             self.col = "axis"
  100.             self.k = eval(word[1:])
  101.             self.move = True
  102.         elif (word[0] == 'M') : self.col = "misc"
  103.         elif (word[0] == 'N') : self.col = "blocknum"
  104.         elif (word[0] == 'O') : self.col = "program"
  105.         elif (word[0] == 'P' or word[0] == 'p'):
  106.              if (self.no_move != True):
  107.                  self.col = "axis"
  108.                  self.p = eval(word[1:])
  109.                  self.move = True
  110.         elif (word[0] == 'Q' or word[0] == 'q'):
  111.              if (self.no_move != True):
  112.                  self.col = "axis"
  113.                  self.q = eval(word[1:])
  114.                  self.move = True
  115.         elif (word[0] == 'R' or word[0] == 'r'):
  116.             self.col = "axis"
  117.             self.r = eval(word[1:])
  118.             self.move = True
  119.         elif (word[0] == 'S' or word[0] == 's'):
  120.             self.col = "axis"
  121.             self.s = eval(word[1:])
  122.             self.move = True
  123.         elif (word[0] == 'T') :
  124.             self.col = "tool"
  125.             self.set_tool( eval(word[1:]) )
  126.         elif (word[0] == 'X' or word[0] == 'x'):
  127.             self.col = "axis"
  128.             self.x = eval(word[1:])
  129.             self.move = True
  130.         elif (word[0] == 'Y' or word[0] == 'y'):
  131.             self.col = "axis"
  132.             self.y = eval(word[1:])
  133.             self.move = True
  134.         elif (word[0] == 'Z' or word[0] == 'z'):
  135.             self.col = "axis"
  136.             self.z = eval(word[1:])
  137.             self.move = True
  138.         elif (word[0] == '(') : (self.col, self.cdata) = ("comment", True)
  139.         elif (word[0] == '!') : (self.col, self.cdata) = ("comment", True)
  140.         elif (word[0] == ';') : (self.col, self.cdata) = ("comment", True)
  141.         elif (word[0] == '#') : self.col = "variable"
  142.         elif (word[0] == ':') : self.col = "blocknum"
  143.         elif (ord(word[0]) <= 32) : self.cdata = True
  144.  
  145.     def Parse(self, name, oname=None):
  146.         self.files_open(name,oname)
  147.        
  148.         #self.begin_ncblock()
  149.         #self.begin_path(None)
  150.         #self.add_line(z=500)
  151.         #self.end_path()
  152.         #self.end_ncblock()
  153.        
  154.         self.path_col = None
  155.         self.f = None
  156.         self.arc = 0
  157.  
  158.         while (self.readline()):
  159.            
  160.             self.a = None
  161.             self.b = None
  162.             self.c = None
  163.             self.i = None
  164.             self.j = None
  165.             self.k = None
  166.             self.p = None
  167.             self.q = None
  168.             self.r = None
  169.             self.s = None
  170.             self.x = None
  171.             self.y = None
  172.             self.z = None
  173.  
  174.             #self.begin_ncblock()
  175.  
  176.             self.move = False
  177.             self.drill = False
  178.             self.no_move = False
  179.  
  180.             words = self.pattern_main.findall(self.line)
  181.             for word in words:
  182.                 self.col = None
  183.                 self.cdata = False
  184.                 self.ParseWord(word)
  185.                 #self.add_text(word, self.col, self.cdata)
  186.                
  187.             if (self.path_col == "rapid"):
  188.                 self.line_style("0")
  189.             else:
  190.                 self.line_style("1")
  191.                
  192.  
  193.             if (self.drill):
  194.                 self.begin_path("rapid")
  195.                 self.add_line(self.x, self.y, self.r)
  196.                 self.end_path()
  197.  
  198.                 self.begin_path("feed")
  199.                 self.add_line(self.x, self.y, self.z)
  200.                 self.end_path()
  201.  
  202.                 self.begin_path("feed")
  203.                 self.add_line(self.x, self.y, self.r)
  204.                 self.end_path()
  205.             else:
  206.                 if (self.move and not self.no_move):
  207.                     self.begin_path(self.path_col)
  208.                     if (self.arc==-1):
  209.                         self.add_arc(self.x, self.y, self.z, self.i, self.j, self.k, self.r, self.arc)
  210.                     elif (self.arc==1):
  211.                         #self.add_arc(x, y, z, i, j, k, -r, arc) #if you want to use arcs with R values uncomment the first part of this line and comment the next one
  212.                         self.add_arc(self.x, self.y, self.z, self.i, self.j, self.k, self.r, self.arc)
  213.                     else:
  214.                         self.add_line(self.x, self.y, self.z, self.a, self.b, self.c)
  215.                        
  216.                    
  217.                 self.end_path()
  218.  
  219.             #self.end_ncblock()
  220.  
  221.         self.files_close()
  222.  
  223.  
  224. ################################################################################
  225.  
  226. if __name__ == '__main__':
  227.    
  228.     parser = ParserIso()
  229.     if len(sys.argv)>2:
  230.         parser.Parse(sys.argv[1],sys.argv[2])
  231.     else:
  232.         parser.Parse(sys.argv[1])
  233.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement