-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgcode2im_simplyfied.py
More file actions
195 lines (172 loc) · 7.4 KB
/
gcode2im_simplyfied.py
File metadata and controls
195 lines (172 loc) · 7.4 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
181
182
183
184
185
186
187
188
189
190
191
192
193
#-------------------------------------------------------------------------------
# Name: module2
# Purpose:
#
# Author: Holoratte
#
# Created: 16/08/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
import g1tog23NoGui as g1tog23
import sys
def gcode2dict(filename):
thisGcodeLine= {}
gCodeDict =[]
separator = '(M|G|X|Y|Z|I|J|K|F|S|P|;)'
regex = re.compile(separator,flags= re.IGNORECASE)
for line in open(filename,"rb"):
thisGcodeLine= {}
lineList = regex.split(line.rstrip('\n ').upper())
for i in range(len(lineList)):
if lineList[i] == (";" or "(" or ""):
break
try:
if lineList[i] in separator: thisGcodeLine[lineList[i].upper()] = float(lineList[i+1].rstrip('\n '))
except:
pass
gCodeDict.append(thisGcodeLine)
return gCodeDict
def replace(filename, outputfilename, 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
with open(outputfilename,"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):
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
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/(maxZ-minZ)/scaler
else: scalerZcolor = 255/(minZ-maxZ)/scaler
print scalerZcolor, maxZ,minZ
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)
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)))
radius = math.sqrt(l.get("I")**2+l.get("J")**2)
if startAngle == stopAngle:
stopAngle += 360
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")
centerY = posY+l.get("J")
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
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)
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')
## return image
if __name__ == '__main__':
sizeX = 7000
sizeY = 4000
linewidth = 1
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 != "":
with open(filename, 'r') as file:
input_line_count = sum(1 for line in file)
print input_line_count
gdict = gcode2dict(filename)
dict2image(gcode2dict(filename),draw)
image = image.transpose(PIL.Image.FLIP_TOP_BOTTOM)
image.show()
filenameNoExt, file_extension = os.path.splitext(filename)
filenamePNG = filenameNoExt + ".png"
print filename
image.save(filenamePNG)
outputfilename = filenameNoExt + "_replaced" +".nc"
replace(filename, outputfilename, gdict,1,"Z","*",-0.05)
dict2image(gcode2dict(outputfilename),draw)
image.show()
SIMPLYFYGCODE = g1tog23.Gcode()
SIMPLYFYGCODE.load(outputfilename,filenameNoExt +"_simplified" + ".nc" )
## SIMPLYFYGCODE.load(filename,filenameNoExt +"_simplified" + ".nc" )
image = Image.new(mode = "L", size = (sizeX, sizeY),color="white")
draw = ImageDraw.Draw(image)
dict2image(gcode2dict(filenameNoExt +"_simplified" + ".nc"),draw)
image = image.transpose(PIL.Image.FLIP_TOP_BOTTOM)
image.show()
image.save(filenameNoExt +"_simplified" + ".png")