#! /usr/bin/python # # rcheck_nc.py # # Oct/26/99 # # import sys import string import regex import math # # # ------------------------------------------------------------------------- class Psw: def __init__(self, radius_check =0, \ position = [0.0,0.0,0.0],\ pos_before= [0.0,0.0,0.0], \ vector_start = [0.0,0.0,0.0], \ vector_unit = [0.0,0.0,0.0], \ vector_before= [0.0,0.0,0.0], \ absolute_flag = 0, \ gcode_status = 0, \ n_count = 0, \ flag_g41 = 0): self.radius_check = radius_check self.position = position self.pos_before = pos_before self.vector_start = vector_start self.vector_unit = vector_unit self.vector_before = vector_before self.absolute_flag = absolute_flag self.gcode_status = gcode_status self.n_count = n_count self.flag_g41 = flag_g41 # def unit_vector (self): ll = math.hypot (self.vector_start[0],self.vector_start[1]) if (0.0 < ll): self.vector_unit[0] = self.vector_start[0] / ll self.vector_unit[1] = self.vector_start[1] / ll else: self.vector_unit[0] = 0.0 self.vector_unit[1] = 0.0 # def status_copy_proc (self,e_point): for it in range (2): self.pos_before[it] = self.position[it] self.vector_before[it] = self.vector_unit[it] self.position[it] = e_point[it] # ------------------------------------------------------------------------- def gcode_define (in_string,pos_a): pos_b = regex.search ("[\(A-Z]",in_string[pos_a+1:]) if (pos_b == -1): gcode_ch = in_string[pos_a+1:-1] else: gcode_ch = in_string[pos_a+1:pos_a+pos_b+1] gcode = string.atoi (gcode_ch) return gcode # ------------------------------------------------------------------------- def exist_check (in_string,ch_key,value): rvalue = regex.search (ch_key,in_string) if (0 <= rvalue): value[0] = gcode_define (in_string,rvalue) return (rvalue) # ------------------------------------------------------------------------- def xyz_parser (in_string,status_aa,e_point): e_point[0] = status_aa.position[0] e_point[1] = status_aa.position[1] e_point[2] = status_aa.position[2] ch_key = ['X','Y','Z'] value = [0] ratio_nc = 1000.0 # for it in range (3): if (exist_check (in_string,ch_key[it],value) != -1): if (status_aa.absolute_flag == 0): e_point[it] = value[0] / ratio_nc else: e_point[it] = e_point[it] + value[0] / ratio_nc # ------------------------------------------------------------------------- def offset_check_proc (in_string,status_aa,vector_b): vector_a = [0.0,0.0] vector_new = [0.0,0.0] pos_new = [0.0,0.0] pos_new2= [0.0,0.0] ll = 10000.0 vector_a[0] = status_aa.vector_before[0] vector_a[1] = status_aa.vector_before[1] pos_new[0] = status_aa.position[0] - status_aa.pos_before[0] pos_new[1] = status_aa.position[1] - status_aa.pos_before[1] pos_new2[0] = pos_new[0] * vector_a[0] + pos_new[1] * vector_a[1] pos_new2[1] = -pos_new[0] * vector_a[1] + pos_new[1] * vector_a[0] vector_new[0] = vector_b[0] * vector_a[0] + vector_b[1] * vector_a[1]; vector_new[1] = -vector_b[0] * vector_a[1] + vector_b[1] * vector_a[0]; if (0.0 < vector_new[1]): ll = pos_new2[0] / vector_new[1] # if (ll < status.radius_check): sys.stderr.write ("offset_max = %f\t" % ll) sys.stderr.write (in_string) # ------------------------------------------------------------------------- def move01 (line_a,status_aa): e_point = [0.0,0.0,0.0] xyz_parser (line_a,status_aa,e_point) # status_aa.vector_start[0] = e_point[0] - status_aa.position[0] status_aa.vector_start[1] = e_point[1] - status_aa.position[1] # status_aa.unit_vector () # if (status_aa.flag_g41 != 0): offset_check_proc (line_a,status_aa,status_aa.vector_unit) # status_aa.status_copy_proc (e_point) # ------------------------------------------------------------------------- def circle_parser (in_string,status_aa,center_point,e_point): value = [0] ratio_nc = 1000.0 icode = 0.0 jcode = 0.0 xyz_parser (in_string,status_aa,e_point) if (exist_check (in_string,'I',value) != -1): icode = value[0] / ratio_nc if (exist_check (in_string,'J',value) != -1): jcode = value[0] / ratio_nc # center_point[0] = status.position[0] + icode center_point[1] = status.position[1] + jcode # if (status_aa.gcode_status == 3): status_aa.vector_start[0] = jcode status_aa.vector_start[1] = -icode else: status_aa.vector_start[0] = -jcode status_aa.vector_start[1] = icode # ------------------------------------------------------------------------- def circle_proc (line_a,status_aa): start_vector = [0.0,0.0] center_point = [0.0,0.0,0.0] e_point = [0.0,0.0,0.0] circle_parser (line_a,status_aa,center_point,e_point) # status_aa.unit_vector () # if (status_aa.flag_g41 != 0): offset_check_proc (line_a,status_aa,status_aa.vector_unit) # status_aa.status_copy_proc (e_point) # ------------------------------------------------------------------------- def dispatch_proc (gcode,line_a,status_aa): if ((gcode == 0) or (gcode == 1) or (gcode == 17) \ or (gcode == 40) or (gcode == 41)): status_aa.gcode_status = 1 move01 (line_a,status_aa) elif (gcode == 90): status_aa.absolute_flag = 0 status_aa.gcode_status = 1 move01 (line_a,status_aa) elif (gcode == 91): status_aa.absolute_flag = 1 move01 (line_a,status_aa) elif (gcode == 92): tmp_flag = status_aa.absolute_flag status_aa.absolute_flag = 0 move01 (line_a,status_aa) status_aa.absolute_flag = tmp_flag elif ((gcode == 2) or (gcode == 3)): status_aa.gcode_status = gcode circle_proc (line_a,status_aa) else: sys.stderr.write ("*** error ***\t") sys.stderr.write (line_a) # ------------------------------------------------------------------------- # [4-4]: def hantei (line_a,status_aa): pos_a = regex.search ("G",line_a) if (pos_a != -1): if (regex.search ("G40",line_a) != -1): status_aa.flag_g41 = 0 if (regex.search ("G41",line_a) != -1): status_aa.flag_g41 = 1 # gcode = gcode_define (line_a,pos_a) dispatch_proc (gcode,line_a,status_aa) # elif (status_aa.gcode_status == 2): circle_proc (line_a,status_aa) elif (status_aa.gcode_status == 3): circle_proc (line_a,status_aa) else: status_aa.gcode_status = 1 move01 (line_a,status_aa) # ------------------------------------------------------------------------- # [4]: def proc01 (status_aa): while (1): line = sys.stdin.readline () if (line == ""): break if ((line[0:1] != '%') and (line[0:1] != '!') \ and (line[0:1] != '(') and (line[0:1] != 'M')): hantei (line,status_aa) status_aa.n_count = status_aa.n_count + 1 # ------------------------------------------------------------------------- # status = Psw (15.0 ,[124.0,234.0,456.0],[124.0,234.0,456.0],[0.0,0.0,0.0]) # sys.stderr.write ("*** rcheck_nc *** Oct/26/99 ***\n") # if (len (sys.argv) == 1): sys.stderr.write ("Use: rcheck_nc 12.0 ***\n") sys.exit (1) # status.radius_check = string.atof (sys.argv[1]) sys.stderr.write ("radius_check = %.3f\n" % status.radius_check) # proc01 (status) # sys.stderr.write ("*** rcheck_nc *** end ***\n")