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
# 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_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
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
currentLine = 0.0
@ -104,7 +113,11 @@ def parseGcodeRaw(filePath, etch_definition = 0): # Gcode parser from Etch_Z_adj
travel_moves.append(path)
path = []
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
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 :
if etchMove == True :
if close_shapes : # Return to the start point
path.append(path[0])
etch_moves.append(path)
path = []
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)
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
closestMove_i = i
reverse = 1 # Flag set to reverse the path
#print "Using a reverse path did optimize!"
i = i + 1
path = etch_moves_in[closestMove_i] # Select the closest that has been found

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -14,6 +14,8 @@
# Begin modules
import sys
from subprocess import call
import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
@ -22,12 +24,18 @@ sys.path.append("../CycloneHost")
import GcodeParser as gcp
# 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/"
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)
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.set_antialiased(False) # turn off antialising