More improvements regarding Gerber2gcode and the optimizer

pull/6/head
carlosgs 2013-05-29 12:03:41 +02:00
parent 4dcca04fa4
commit a828e2a0d1
9 changed files with 25026 additions and 8466 deletions

View File

@ -18,7 +18,7 @@
import os.path import os.path
# End modules # End modules
def parseGcodeRaw(filePath, etch_definition = 0): # Gcode parser from Etch_Z_adjust.1.8.py (modified by Carlosgs to output toolpaths) def parseGcodeRaw(filePath, etch_definition = 0, close_shapes = 0): # Gcode parser from Etch_Z_adjust.1.8.py (modified by Carlosgs to output toolpaths)
gcode_size = (0,0) gcode_size = (0,0)
gcode_origin = (0,0) gcode_origin = (0,0)
@ -67,6 +67,15 @@ def parseGcodeRaw(filePath, etch_definition = 0): # Gcode parser from Etch_Z_adj
elif Y_dest > Y_max : Y_max = Y_dest elif Y_dest > Y_max : Y_max = Y_dest
return Y_min, Y_max return Y_min, Y_max
def isSame(list1, list2): # Compare two lists, returns True if they have same values
i = 0
for val1 in list1:
val2 = list2[i]
if val1 != val2:
return False
i = i + 1
return True
etchMove = False etchMove = False
currentLine = 0.0 currentLine = 0.0
@ -104,7 +113,11 @@ def parseGcodeRaw(filePath, etch_definition = 0): # Gcode parser from Etch_Z_adj
travel_moves.append(path) travel_moves.append(path)
path = [] path = []
etchMove = True # Set etch mode etchMove = True # Set etch mode
path.append([X_dest,Y_dest,Z_dest]) path.append([X_dest,Y_dest,Z_dest])
destPoint = [X_dest,Y_dest,Z_dest]
if len(path) == 0 or isSame(destPoint,path[-1]) == False: # Don't add same point twice
path.append(destPoint)
# and now check for max and min X and Y values # and now check for max and min X and Y values
if is_first_X == True : if is_first_X == True :
@ -120,10 +133,17 @@ def parseGcodeRaw(filePath, etch_definition = 0): # Gcode parser from Etch_Z_adj
else : (Y_min, Y_max) = test_Y(Y_min, Y_max) else : (Y_min, Y_max) = test_Y(Y_min, Y_max)
else : else :
if etchMove == True : if etchMove == True :
if close_shapes : # Return to the start point
path.append(path[0])
etch_moves.append(path) etch_moves.append(path)
path = [] path = []
etchMove = False # Set travel mode etchMove = False # Set travel mode
path.append([X_dest,Y_dest,Z_dest]) path.append([X_dest,Y_dest,Z_dest])
destPoint = [X_dest,Y_dest,Z_dest]
if len(path) == 0 or isSame(destPoint,path[-1]) == False: # Don't add same point twice
path.append(destPoint)
#file_out.append(line) #file_out.append(line)
if is_first_X == False : if is_first_X == False :
@ -173,6 +193,7 @@ def optimize(etch_moves_in, origin=[0,0], travel_height = 5): # Optimizes the to
closest = distance closest = distance
closestMove_i = i closestMove_i = i
reverse = 1 # Flag set to reverse the path reverse = 1 # Flag set to reverse the path
#print "Using a reverse path did optimize!"
i = i + 1 i = i + 1
path = etch_moves_in[closestMove_i] # Select the closest that has been found path = etch_moves_in[closestMove_i] # Select the closest that has been found

View File

@ -10,7 +10,7 @@ import sys
import locale import locale
import re import re
TINY = 1e-6 TINY = 1e-6
SMALL = 1e-2 SMALL = 1e-3
TEST_POINTS1 =[] TEST_POINTS1 =[]
TEST_POINTS2 =[] TEST_POINTS2 =[]
TEST_POINT_R = 0.01 TEST_POINT_R = 0.01

View File

@ -1,5 +1,5 @@
(Generated by pygerber2gcode_cui_MOD.py ) (Generated by ./pygerber2gcode_cui_MOD.py )
( 2013-05-29 00:15:06 ) ( 2013-05-29 04:57:18 )
(Initialize) (Initialize)
G92 X0.000000 Y0.000000 Z0.000000 G92 X0.000000 Y0.000000 Z0.000000

View File

@ -1,5 +1,5 @@
(Generated by pygerber2gcode_cui_MOD.py ) (Generated by pygerber2gcode_cui_MOD.py )
( 2013-05-29 00:14:32 ) ( 2013-05-29 01:46:38 )
(Initialize) (Initialize)
G92 X0.000000 Y0.000000 Z0.000000 G92 X0.000000 Y0.000000 Z0.000000

View File

@ -1,5 +1,5 @@
(Generated by pygerber2gcode_cui_MOD.py ) (Generated by pygerber2gcode_cui_MOD.py )
( 2013-05-29 00:14:32 ) ( 2013-05-29 01:46:38 )
(Initialize) (Initialize)
G92 X0.000000 Y0.000000 Z0.000000 G92 X0.000000 Y0.000000 Z0.000000

View File

@ -17,7 +17,7 @@ import gerber_merge as gm
#Global Constant #Global Constant
HUGE = 1e10 HUGE = 1e10
TINY = 1e-6 TINY = 1e-6
SMALL = 1e-2 SMALL = 1e-3
MERGINE = 1e-4 MERGINE = 1e-4
INCH = 25.4 #mm INCH = 25.4 #mm
MIL = INCH/1000 MIL = INCH/1000
@ -62,7 +62,7 @@ GCODE_EXT = '*.ngc'
GDRILL_EXT = '*.ngc' GDRILL_EXT = '*.ngc'
GEDGE_EXT = '*.ngc' GEDGE_EXT = '*.ngc'
MM_PER_ARC_SEGMENT = 0.2 # mm MM_PER_ARC_SEGMENT = 0.05 # mm
#View #View
GERBER_COLOR = 'BLACK' #black GERBER_COLOR = 'BLACK' #black
@ -446,7 +446,7 @@ def read_Gerber(dirname,filename):
for gerber in data: for gerber in data:
if not gerber: if not gerber:
break break
#print gerber # print gerber
if (find(gerber, "%MOIN") != -1): if (find(gerber, "%MOIN") != -1):
IN_INCH_FLAG = 1 IN_INCH_FLAG = 1
@ -533,11 +533,15 @@ def parse_data(x,y,d):
if( gDCODE[int(gFIG_NUM)].atype == "C"): if( gDCODE[int(gFIG_NUM)].atype == "C"):
#Circle #Circle
gGCODES.append(GCODE(x,y,0,0,1,mod1,0)) gGCODES.append(GCODE(x,y,0,0,1,mod1,0))
else: elif(gDCODE[int(gFIG_NUM)].atype == "R"):
#elif(gDCODE[int(gFIG_NUM)].atype == "R"): # DIRTY PATCH, RENDERS OVALS AS RECTANGLES!
#Rect #Rect
#Change to line #Change to line
gGCODES.append(GCODE(x-mod1/2,y,x+mod1/2,y,4,mod1,mod2)) gGCODES.append(GCODE(x-mod1/2,y,x+mod1/2,y,4,mod1,mod2))
elif(gDCODE[int(gFIG_NUM)].atype == "O"):
#Rect
#Change to line
gGCODES.append(GCODE(x-mod1/2,y,x+mod1/2,y,6,mod1,mod2))
else: print "UNSUPPORTED TYPE:",gDCODE[int(gFIG_NUM)].atype
elif(d == "02" or d == "2"): elif(d == "02" or d == "2"):
#move w light off #move w light off
gGERBER_TMP_X = x gGERBER_TMP_X = x
@ -546,10 +550,13 @@ def parse_data(x,y,d):
#move w Light on #move w Light on
if(gDCODE[int(gFIG_NUM)].atype == "C"): if(gDCODE[int(gFIG_NUM)].atype == "C"):
gGCODES.append(GCODE(gGERBER_TMP_X,gGERBER_TMP_Y,x,y,3,mod1,mod2)) gGCODES.append(GCODE(gGERBER_TMP_X,gGERBER_TMP_Y,x,y,3,mod1,mod2))
else: elif(gDCODE[int(gFIG_NUM)].atype == "R"):
#elif(gDCODE[int(gFIG_NUM)].atype == "R"): # DIRTY PATCH, RENDERS OVALS AS RECTANGLES!
#Rect #Rect
gGCODES.append(GCODE(gGERBER_TMP_X,gGERBER_TMP_Y,x,y,4,mod1,mod2)) gGCODES.append(GCODE(gGERBER_TMP_X,gGERBER_TMP_Y,x,y,4,mod1,mod2))
elif(gDCODE[int(gFIG_NUM)].atype == "O"):
#Rect
gGCODES.append(GCODE(gGERBER_TMP_X,gGERBER_TMP_Y,x,y,6,mod1,mod2))
else: print "UNSUPPORTED TYPE:",gDCODE[int(gFIG_NUM)].atype
gGERBER_TMP_X = x gGERBER_TMP_X = x
gGERBER_TMP_Y = y gGERBER_TMP_Y = y
@ -580,6 +587,8 @@ def gerber2polygon(gGCODES):
line2poly(x1,y1,x2,y2,mod2/2,0,8) line2poly(x1,y1,x2,y2,mod2/2,0,8)
elif(gcode.gtype == 5): elif(gcode.gtype == 5):
line2poly(x1,y1,x2,y2,mod1/2,2,8) line2poly(x1,y1,x2,y2,mod1/2,2,8)
elif(gcode.gtype == 6): # Oval
line2poly(x1,y1,x2,y2,mod2/2,3,8)
return gPOLYGONS return gPOLYGONS
def line2poly(x1,y1,x2,y2,r,atype,ang_n): def line2poly(x1,y1,x2,y2,r,atype,ang_n):
@ -604,8 +613,21 @@ def line2poly(x1,y1,x2,y2,r,atype,ang_n):
points = points + [xa2,ya2,xa1,ya1] points = points + [xa2,ya2,xa1,ya1]
points = points + arc_points(x2,y2,r,ang+deg90,ang-deg90,ang_n) points = points + arc_points(x2,y2,r,ang+deg90,ang-deg90,ang_n)
points = points + [xa2,ya2] points = points + [xa2,ya2]
elif(atype==3): # Oval
width = abs(xa1-xb2)
height = abs(ya1-yb2)
if width > height:
points = points + arc_points(x1+r,y1,r,ang+3*deg90,ang+deg90,ang_n)
points = points + arc_points(x2-r,y2,r,ang+1*deg90,ang-deg90,ang_n)
points = points + [xa2+r,ya2]
else:
r = width/2
points = points + arc_points(x1+r,y1+r,r,ang+2*deg90,ang+deg90-deg90,ang_n)
points = points + arc_points(x2-r,y2-r,r,ang+0*deg90,ang-deg90-deg90,ang_n)
ya1=y1+r*sin(ang+deg90)
points = points + [xa1,ya1]
else: else:
points=(xa1,ya1,xb1,yb1,xb2,yb2,xa2,ya2,xa1,ya1) points = (xa1,ya1,xb1,yb1,xb2,yb2,xa2,ya2,xa1,ya1)
polygon(points) polygon(points)
def polygon(points): def polygon(points):
@ -642,11 +664,15 @@ def polygon(points):
def circle_points(cx,cy,r,points_num): def circle_points(cx,cy,r,points_num):
int(points_num) int(points_num)
points_num = int( (2.0*pi*r)/float(MM_PER_ARC_SEGMENT) ) # Automatic resolution (reduces gcode file size)
if points_num < 10 : new_points_num = int( (2.0*pi*float(r))/float(MM_PER_ARC_SEGMENT) ) # Automatic resolution (reduces gcode file size)
points_num = 10; if new_points_num < 8 :
elif points_num > 100 : new_points_num = 8;
points_num = 100; elif new_points_num > 100 :
new_points_num = 100;
# print "Modifyin CIRCLE points_num from",points_num,"to",new_points_num
points_num = new_points_num
# print "Circle: Radius:", str(r), "Points:", points_num # print "Circle: Radius:", str(r), "Points:", points_num
points=[] points=[]
# if(points_num <= 2): # if(points_num <= 2):
@ -654,7 +680,6 @@ def circle_points(cx,cy,r,points_num):
# return # return
i = points_num i = points_num
while i > 0: while i > 0:
cir_x=cx+r*cos(2.0*pi*float(i)/float(points_num))
cir_x=cx+r*cos(2.0*pi*float(i)/float(points_num)) cir_x=cx+r*cos(2.0*pi*float(i)/float(points_num))
cir_y=cy+r*sin(2.0*pi*float(i)/float(points_num)) cir_y=cy+r*sin(2.0*pi*float(i)/float(points_num))
points.extend([cir_x,cir_y]) points.extend([cir_x,cir_y])
@ -772,23 +797,31 @@ def move(x,y):
def arc_points(cx,cy,r,s_angle,e_angle,kaku): def arc_points(cx,cy,r,s_angle,e_angle,kaku):
int(kaku) int(kaku)
float(r)
float(cx)
float(cy)
arc_angle = abs(s_angle-e_angle) arc_angle = abs(s_angle-e_angle)
kaku = int( (2.0*pi*r)/(arc_angle*float(MM_PER_ARC_SEGMENT)) ) # Automatic resolution (reduces gcode file size)
if kaku < 5 : new_kaku = int( (2.0*pi*float(r))/(arc_angle*float(MM_PER_ARC_SEGMENT)) ) # Automatic resolution (reduces gcode file size)
kaku = 5; if new_kaku < 8 :
elif kaku > 100 : new_kaku = 8;
kaku = 100; elif new_kaku > 100 :
new_kaku = 100;
# print "Modifying ARC points from",kaku,"to",new_kaku
kaku = new_kaku
# print "Arc: Radius:", str(r), "Points:", kaku # print "Arc: Radius:", str(r), "Points:", kaku
points=[] points=[]
if(s_angle == e_angle): if(s_angle == e_angle):
print "Start and End angle are same" print "Start and End angle are same"
# if(kaku <= 2): if(kaku <= 2):
# print "Too small angle" print "Too small angle"
ang_step=(e_angle-s_angle)/(kaku-1) ang_step=float((float(e_angle)-float(s_angle))/float(kaku-1))
i = 0 i = 0
while i < kaku: while i < kaku:
arc_x=cx+r*cos(s_angle+ang_step*float(i)) arc_x=float(cx+r*cos(float(s_angle)+ang_step*float(i)))
arc_y=cy+r*sin(s_angle+ang_step*float(i)) arc_y=float(cy+r*sin(float(s_angle)+ang_step*float(i)))
points.extend([arc_x,arc_y]) points.extend([arc_x,arc_y])
i += 1 i += 1

View File

@ -14,6 +14,8 @@
# Begin modules # Begin modules
import sys import sys
from subprocess import call
import numpy as np import numpy as np
from scipy import interpolate from scipy import interpolate
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
@ -22,12 +24,18 @@ sys.path.append("../CycloneHost")
import GcodeParser as gcp import GcodeParser as gcp
# End modules # End modules
# Temporary path to speedup testing
#import os
#os.chdir("../GcodeGenerators/pyGerber2Gcode_CUI/")
#call(["pypy","./pygerber2gcode_cui_MOD.py"])
#os.chdir("../../gcode_Z_adjust")
filePath = "../GcodeGenerators/pyGerber2Gcode_CUI/out/" filePath = "../GcodeGenerators/pyGerber2Gcode_CUI/out/"
fileName = "printshield" # sys.argv[1] fileName = "printshield" # sys.argv[1]
def plotPoints(path_list, color, linewidth): # Thanks to pprzemek (http://stackoverflow.com/questions/2282727/draw-points-using-matplotlib-pyplot-x1-y1-x2-y2) def plotPoints(path_list, color, linewidth): # Thanks to pprzemek (http://stackoverflow.com/questions/2282727/draw-points-using-matplotlib-pyplot-x1-y1-x2-y2)
for path in path_list : for path in path_list :
a = np.array(path) a = np.array(path) # Give to plot() the points in the adequate format
line, = plt.plot(a[:,0], a[:,1], color, linewidth=linewidth*2) line, = plt.plot(a[:,0], a[:,1], color, linewidth=linewidth*2)
line.set_antialiased(False) # turn off antialising line.set_antialiased(False) # turn off antialising