когда все будет написано нормально на объектном языке, их будет гораздо проще вылавливать, сейчас там просто каша с 100500 костылей, чтобы что-то работало:
Код: Выделить всё
def generate_gcode(self, curve, layer, depth):
Zauto_scale = self.Zauto_scale[layer]
tool = self.tools[layer][0]
g = ""
def c(c):
c = [c[i] if i<len(c) else None for i in range(6)]
if c[5] == 0 : c[5]=None
s,s1 = [" X", " Y", " Z", " I", " J", " K"], ["","","","","",""]
m,a = [1,1,self.options.Zscale*Zauto_scale,1,1,self.options.Zscale*Zauto_scale], [0,0,self.options.Zoffset,0,0,0]
r = ''
for i in range(6):
if c[i]!=None:
r += s[i] + ("%f" % (c[i]*m[i]+a[i])) + s1[i]
return r
def calculate_angle(a, current_a) :
return min(
[abs(a-current_a%pi2+pi2), a+current_a-current_a%pi2+pi2],
[abs(a-current_a%pi2-pi2), a+current_a-current_a%pi2-pi2],
[abs(a-current_a%pi2), a+current_a-current_a%pi2])[1]
def get_tangent_knife_turn_gcode(s,si,tool,current_a, depth) :
# get tangent at start point
if s[1] == 'line' :
a = atan2_(si[0][0]-s[0][0],si[0][1]-s[0][1])
else :
if s[3]<0 : # CW
a = atan2_(s[2][1]-s[0][1],-s[2][0]+s[0][0]) + pi
else: #CCW
a = atan2_(-s[2][1]+s[0][1],s[2][0]-s[0][0]) + pi
# calculate all vars
a = calculate_angle(a, current_a)
axis4 = " A%s"%((a+s[3])*tool['4th axis scale']+tool['4th axis offset']) if s[1]=="arc" else ""
if abs((a-current_a)%pi2)<1e-5 or abs((a-current_a)%pi2 - pi2)<1e-5 :
g = ""
else :
g = "A%s (Turn knife)\n" % (a*tool['4th axis scale']+tool['4th axis offset'])
if tool['lift knife at corner']!=0. :
g = "G00 Z%s (Lift up)\n"%(depth+tool['lift knife at corner']) + "G00 "+ g + "G01 Z%s (Penetrate back)\n"%depth
else :
g = "G01 "+g
return a, axis4, g
if len(curve)==0 : return ""
try :
self.last_used_tool == None
except :
self.last_used_tool = None
print_("working on curve")
print_(curve)
if tool != self.last_used_tool :
g += ( "(Change tool to %s)\n" % re.sub("\"'\(\)\\\\"," ",tool["name"]) ) + tool["tool change gcode"] + "\n"
lg, zs, f = 'G00', self.options.Zsafe, " F%f"%tool['feed']
current_a = 0
go_to_safe_distance = "G00" + c([None,None,zs]) + "\n"
penetration_feed = " F%s"%tool['penetration feed']
for i in range(1,len(curve)):
# Creating Gcode for curve between s=curve[i-1] and si=curve[i] start at s[0] end at s[4]=si[0]
s, si = curve[i-1], curve[i]
if point_to_point_d2(s[0],si[0]) < 1e-7 : continue
feed = f if lg not in ['G01','G02','G03'] else ''
if s[1] == 'move':
g += go_to_safe_distance + "G00" + c(si[0]) + "\n" + tool['gcode before path'] + "\n"
lg = 'G00'
elif s[1] == 'end':
g += go_to_safe_distance + tool['gcode after path'] + "\n"
lg = 'G00'
elif s[1] == 'line':
if tool['4th axis meaning'] == "tangent knife" :
current_a, axis4, g_ = get_tangent_knife_turn_gcode(s,si,tool,current_a, depth)
g+=g_
if lg=="G00": g += "G01" + c([None,None,s[5][0]+depth]) + penetration_feed +"(Penetrate)\n"
g += "G01" +c(si[0]+[s[5][1]+depth]) + feed + "\n"
lg = 'G01'
elif s[1] == 'arc':
r = [(s[2][0]-s[0][0]), (s[2][1]-s[0][1])]
if tool['4th axis meaning'] == "tangent knife" :
current_a, axis4, g_ = get_tangent_knife_turn_gcode(s,si,tool,current_a, depth)
g+=g_
current_a = current_a+s[3]
else : axis4 = ""
if lg=="G00": g += "G01" + c([None,None,s[5][0]+depth]) + penetration_feed + "(Penetrate)\n"
if (r[0]**2 + r[1]**2)>self.options.min_arc_radius**2:
r1, r2 = (P(s[0])-P(s[2])), (P(si[0])-P(s[2]))
if abs(r1.mag()-r2.mag()) < 0.001 :
g += ("G02" if s[3]<0 else "G03") + c(si[0]+[ s[5][1]+depth, (s[2][0]-s[0][0]),(s[2][1]-s[0][1]) ]) + feed + axis4 + "\n"
else:
r = (r1.mag()+r2.mag())/2
g += ("G02" if s[3]<0 else "G03") + c(si[0]+[s[5][1]+depth]) + " R%f" % (r) + feed + axis4 + "\n"
lg = 'G02'
else:
if tool['4th axis meaning'] == "tangent knife" :
current_a, axis4, g_ = get_tangent_knife_turn_gcode(s[:1]+["line"]+s[2:],si,tool,current_a, depth)
g+=g_
g += "G01" +c(si[0]+[s[5][1]+depth]) + feed + "\n"
lg = 'G01'
if si[1] == 'end':
g += go_to_safe_distance + tool['gcode after path'] + "\n"
return g