-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgcode2im_2.py
More file actions
181 lines (166 loc) · 7.11 KB
/
Copy pathgcode2im_2.py
File metadata and controls
181 lines (166 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#-------------------------------------------------------------------------------
# Name: module2
# Purpose:
#
# Author: Holoratte
#
# Created: 08/03/2020
# Copyright: (c) Holoratte 2020
# Licence: <GPLv3>
#-------------------------------------------------------------------------------
import Tkinter, Tkconstants, tkFileDialog
import re
import PIL
from PIL import ImageDraw, Image, ImageOps
import math
import os
def gcode2dict(filename):
thisGcodeLine= {}
gCodeDict =[]
separator = '(M|G|X|Y|Z|I|J|K|F|S|P|;)'
for line in open(filename,"rb"):
thisGcodeLine= {}
lineList = re.split(separator,line.rstrip('\n '))
for i in range(len(lineList)):
if lineList[i] == (";" or "(" or ""):
break
try:
if lineList[i] in separator: thisGcodeLine[lineList[i]] = float(lineList[i+1].rstrip('\n '))
except:
pass
gCodeDict.append(thisGcodeLine)
return gCodeDict
def replace(filename, gCodeDict, GValue, axis, searchValue, newValue):
posX, posY,posZ,i,j,k,g = 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0
a_list = ("M","G", "X", "Y", "Z", "I", "J", "K", "F", "S", "P")
if searchValue == "*":
for l in gCodeDict:
if l.get('G', None) != None:
g = l.get('G')
l["G"] = int(l.get("G"))
if (l.get('G', None) == GValue) or (g == GValue) :
l[axis]= newValue
filename, file_extension = os.path.splitext(filename)
filename = filename + "_" + str(GValue)+str(axis)+str(newValue) + ".nc"
with open(filename,"w") as f:
for l in gCodeDict:
lsorted =[(key, l[key]) for key in a_list if key in l]
for key, value in lsorted:
f.write(key + str(value) + " ")
f.write("\n")
def dict2image(gCodeDict, draw):
## print gCodeDict
posX, posY,posZ,i,j,k,g = 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0
scaler = 1.0
seqX = [x.get('X', 0) for x in gCodeDict]
seqI = [x.get('I', 0) for x in gCodeDict]
minX = min(seqX)
maxX = max(seqX)
seqY = [x.get('Y', 0) for x in gCodeDict]
seqJ = [x.get('J', 0) for x in gCodeDict]
minY = min(seqY)
maxY = max(seqY)
seqZ = [x.get('Z', 0) for x in gCodeDict]
minZ = min(seqZ)
maxZ = max(seqZ)
offsetX = 0.0-minX
offsetY = 0.0-minY
offsetZ = 0.0-minZ
dictScaler = ["X","Y","Z","I","J","K"]
colorZ =0
## print minX, maxX, minY, maxY
if maxX - minX == 0: maxX= 1e-200
if maxY - minY == 0: maxY= 1e-200
scaler = min(sizeX/(maxX - minX),sizeY/(maxY - minY))
if maxZ-minZ == 0: scalerZcolor = 1
elif maxZ-minZ > 0: scalerZcolor = 255/(minZ-maxZ)/scaler
else: scalerZcolor = 255/(maxZ-minZ)/scaler
for l in gCodeDict:
for key, value in l.iteritems():
if key =="X":
l[key]= (value + offsetX)* scaler
elif key =="Y":
l[key]= (value + offsetY) * scaler
elif key =="Z":
l[key]= (value + offsetZ)* scaler
elif key in dictScaler:
l[key]= value * scaler
if l.get('X', None) != None or l.get('Y', None) != None or l.get('Z', None) != None or l.get('I', None) != None or l.get('J', None) != None or l.get('K', None) != None:
if l.get('X', None) == None: l["X"]= posX
if l.get('Y', None) == None: l["Y"]= posY
if l.get('Z', None) == None: l["Z"]= posZ
if l.get('I', None) == None: l["I"]= i
if l.get('J', None) == None: l["J"]= j
if l.get('K', None) == None: l["K"]= k
if l.get('G', None) == None: l["G"]= g
colorZ = int(l.get("Z")*scalerZcolor)
## colorZ =0
## print colorZ
if l.get('G', None) == 1:
draw.line([posX, posY,l.get('X'),l.get('Y')],fill=colorZ, width=linewidth)
elif l.get('G', None) == 3:
centerX = posX+l.get("I")
centerY = posY+l.get("J")
if (posX-centerX) == 0:
centerX+=1e-200
if (posY-centerY) == 0:
centerY+=1e-200
startAngle = math.degrees(math.atan2((posY-centerY),(posX-centerX)))
if (l.get("X")-centerX) == 0:
centerX+=1e-200
if (l.get("Y")-centerY) == 0:
centerY+=1e-200
stopAngle = math.degrees(math.atan2((l.get("Y")-centerY),(l.get("X")-centerX)))
if startAngle == stopAngle: stopAngle += 360
radius = math.sqrt(l.get("I")**2+l.get("J")**2)
draw.arc([centerX-radius, centerY-radius, centerX+radius, centerY+radius], startAngle, stopAngle, fill=colorZ, width=linewidth)
elif l.get('G', None) == 2:
centerX = posX+l.get("I") if l.get("I") != None else posX+i
centerY = posY+l.get("J") if l.get("J") != None else posX+j
## print centerX,centerY, "hallo"
if (posX-centerX) == 0:
centerX+=1e-200
startAngle = math.degrees(math.atan2((posY-centerY),(posX-centerX)))
if (l.get("X")-centerX) == 0:
centerX+=1e-200
stopAngle = math.degrees(math.atan2((l.get("Y")-centerY),(l.get("X")-centerX)))
if startAngle == stopAngle: stopAngle += 360
if l.get("I") == None and l.get("J") != None:
radius = math.sqrt(i**2+l.get("J")**2)
elif l.get("I") != None and l.get("J") == None:
radius = math.sqrt(l.get("I")**2+j**2)
else:
radius = math.sqrt(l.get("I")**2+l.get("J")**2)
draw.arc([centerX-radius, centerY-radius, centerX+radius, centerY+radius], stopAngle, startAngle, fill=colorZ, width=linewidth)
## else: print l
if l.get('X', None) != None: posX = l.get('X')
if l.get('Y', None) != None: posY = l.get('Y')
if l.get('Z', None) != None: posZ = l.get('Z')
if l.get('I', None) != None: i= l.get("I")
if l.get('J', None) != None: j= l.get("J")
if l.get('K', None) != None: k= l.get("K")
if l.get('G', None) != None: g = l.get('G')
## print posX, posY,posZ,i,j,k,g
## return image
if __name__ == '__main__':
sizeX = 7000
sizeY = 4000
image = Image.new(mode = "L", size = (sizeX, sizeY),color="white")
draw = ImageDraw.Draw(image)
root = Tkinter.Tk()
root.withdraw()
filename = ""
filename = tkFileDialog.askopenfilename(initialdir = "./",title = "Select file",
filetypes = (("nc-Files",".nc .cnc"),("all files","*.*")));
root.destroy()
if filename != "":
linewidth = 1
gdict = gcode2dict(filename)
dict2image(gcode2dict(filename),draw)
image = image.transpose(PIL.Image.FLIP_TOP_BOTTOM)
image.show()
filename, file_extension = os.path.splitext(filename)
filename = filename + ".png"
print filename
image.save(filename)
## replace(filename,gdict,1,"Z","*",0.05)