-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathX2MergeDialog.py
359 lines (321 loc) · 14.7 KB
/
X2MergeDialog.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python
from skeinforge.fabmetheus_utilities import archive
from subprocess import STDOUT
import printrun.printcore
import printrun.pronsole
import sys
import re
import os
import shutil
import subprocess
import wx
class X2MergeDialog(wx.Dialog, printrun.pronsole.pronsole):
'''Gcode mixer for dual extruder prints.'''
def __init__(self, *args, **kwds):
printrun.pronsole.pronsole.__init__(self)
kwds["style"] = wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.MINIMIZE_BOX | wx.RESIZE_BORDER
wx.Dialog.__init__(self, *args, **kwds)
self.okButton = wx.Button(self, wx.ID_OK, "Generate and Load")
self.cancelButton = wx.Button(self, wx.ID_CANCEL, "Cancel")
self.Bind(wx.EVT_BUTTON, self.OnExit, self.cancelButton)
self.Bind(wx.EVT_BUTTON, self.OnSave, self.okButton)
self.scrollbarPanel = wx.ScrolledWindow(self, -1, style=wx.TAB_TRAVERSAL)
self.browseButton1 = wx.Button(self.scrollbarPanel, wx.ID_ANY, "...", name = 'base')
self.browseButton2 = wx.Button(self.scrollbarPanel, wx.ID_ANY, "...", name = 'ins')
self.Bind(wx.EVT_BUTTON, self.onBrowse, self.browseButton1)
self.Bind(wx.EVT_BUTTON, self.onBrowse, self.browseButton2)
self.settings.colorOn = ''
self.settings.colorOff = ''
self.settings.basePenGcode = 'base_penultimate.g'
self.settings.insPenGcode = 'insert_penutimate.g'
self.mixedGcode = None
self.load_default_rc(".x2mergerc")
self.altpath = archive.getSettingsPath('alterations')
self.altfiles = []
for (dirpath, dirnames, filenames) in os.walk(self.altpath):
self.altfiles.extend([elem for elem in filenames if 'on' in elem.lower() or 'off' in elem.lower()])
break
if len(self.settings.colorOn) == 0 and len(self.altfiles) > 1:
self.settings.colorOn = self.altfiles[1]
if len(self.settings.colorOff) == 0 and len(self.altfiles) > 0:
self.settings.colorOff = self.altfiles[0]
self.__set_properties()
self.__do_layout()
self.Center()
self.Show()
def __set_properties(self):
self.SetTitle("X2 Merge")
# For some reason the dialog size is not consistent between Windows and Linux - this is a hack to get it working
self.SetMinSize(wx.DLG_SZE(self, (465, 220)))
self.SetPosition((0, 0))
def __do_layout(self):
mainSizer = wx.BoxSizer(wx.VERTICAL)
self.scrollbarPanel.SetScrollRate(10, 10)
self.scrollbarPanel.SetSizer(self.getFileSettings())
mainSizer.Add(self.scrollbarPanel, 1, wx.EXPAND | wx.ALL, 5)
actionsSizer = wx.BoxSizer(wx.HORIZONTAL)
actionsSizer.Add(self.okButton, 0, 0, 0)
actionsSizer.Add(self.cancelButton, 0, wx.LEFT, 10)
mainSizer.Add(actionsSizer, 0, wx.ALIGN_RIGHT | wx.ALL, 5)
self.SetSizer(mainSizer)
self.Layout()
def makeUpMixFileName(self, filename):
if (re.search("penultimate[^/\\\]*$", filename) != None):
mixed = re.sub("penultimate([^/\\\\]*)$", "mixed\\1", filename)
else:
mixed = re.sub("(\\.[^\\./\\\\]*)$", "_mixed.g", filename)
if (mixed == None):
mixed = "mixed.g"
self.outFileTc.SetValue(mixed)
def Mix(self, out):
fn_base = self.settings.basePenGcode
fn_color = self.settings.insPenGcode
fn_alt_start = os.path.join(self.altpath, self.settings.colorOn)
fn_alt_end = os.path.join(self.altpath, self.settings.colorOff)
mixed_layers = 0
if (len(fn_alt_start) == 0):
fn_alt_start = os.path.join(self.altpath, 'pla_c_on.gcode')
if (len(fn_alt_end) == 0):
fn_alt_end = os.path.join(self.altpath, 'pla_c_off.gcode')
ALTSTART = open(fn_alt_start, 'r')
ALTEND = open(fn_alt_end, 'r')
BASE = open(fn_base, 'r')
COLOR = open(fn_color, 'r')
# Read in the alt start and stop files
alt_start = '';
for l in ALTSTART:
l = l.rstrip('\n').rstrip('\r')
l = re.sub(';.*', '', l).strip()
if (len(l) > 0):
alt_start += (l + '\n')
ALTSTART.close()
alt_stop = ''
for l in ALTEND:
l = l.rstrip('\n').rstrip('\r')
l = re.sub('[;(].*', '', l).strip()
if (len(l) > 0):
alt_stop += (l + '\n')
ALTEND.close()
# Read in the layers of the color model, format: (<layer> 0.32 )
color_layer = {}
layer_height = 0.0
layer_code = '';
in_layer_code = False
g_one_seen = False
for l in COLOR:
if (not in_layer_code) and ('<layer>' in l):
in_layer_code = True;
elif in_layer_code and ('</layer>' in l):
in_layer_code = False;
elif not in_layer_code:
continue
if '</layer>' in l:
if g_one_seen:
color_layer[layer_height] = layer_code
layer_code = ''
g_one_seen = False
continue
m = re.match(r'.*<layer>\s+([\d\.]+)', l)
if m:
layer_height = float(m.group(1))
l = l.rstrip('\n').rstrip('\r')
l = re.sub('[;(].*', '', l).strip()
if (len(l) > 0):
layer_code += (l + '\n')
if l.startswith('g1') or l.startswith('G1'):
g_one_seen = True
COLOR.close()
# Read and print the base gcode lines doing what the script is designed for
layer_height = 0.0
layer_ins_on = False
for l in BASE:
# capture the layer height
m = re.match(r'.*<layer>\s+([\d\.]+)', l)
if m:
layer_height = float(m.group(1))
continue
# if done with the layer add matching color part layer
if ('</layer>' in l):
if (layer_height in color_layer) and (len(color_layer[layer_height]) > 0):
if (not layer_ins_on):
print >>out, alt_start
layer_ins_on = True
print >>out, color_layer[layer_height]
mixed_layers += 1
continue
l = l.rstrip('\n').rstrip('\r')
l = re.sub('[;(].*', '', l).strip()
if (len(l) > 0):
if (layer_ins_on):
print >>out, alt_stop
layer_ins_on = False
print >>out, l
BASE.close()
return mixed_layers
def onBrowse(self, e):
if ('base' == e.GetEventObject().GetName()):
filename = self.baseTc.GetValue()
dname = "Path to base penultimate gcode file"
usefilter = 1
if ('ins' == e.GetEventObject().GetName()):
filename = self.insTc.GetValue()
dname = "Path to insert penultimate gcode file"
usefilter = 1
basedir = "."
try:
basedir=os.path.split(filename)[0]
except:
pass
dlg=wx.FileDialog(self,dname,basedir,style=wx.FD_OPEN|wx.FD_FILE_MUST_EXIST)
if (usefilter == 1):
dlg.SetWildcard("GCODE files (;*.gcode;*.gco;*.g;)")
if(dlg.ShowModal() == wx.ID_OK):
name=dlg.GetPath()
if not(os.path.exists(name)):
wx.MessageBox("File not found!","Error")
return
path = os.path.split(name)[0]
else:
return
filename = name
if ('base' == e.GetEventObject().GetName()):
self.baseTc.SetValue(filename)
self.makeUpMixFileName(filename)
if ('ins' == e.GetEventObject().GetName()):
self.insTc.SetValue(filename)
def getFileSettings(self):
settingsSizer = wx.BoxSizer(wx.VERTICAL)
settingsRow = 0
infoStaticBox = wx.StaticBox(self.scrollbarPanel, -1, "X2 Merge Info")
infoStaticBoxSizer = wx.StaticBoxSizer(infoStaticBox, wx.VERTICAL)
infoSizer = wx.GridBagSizer(hgap=1, vgap=1)
infoSizer.AddGrowableCol(0)
text = "This script mixes Skeinforge penultimate gcode files \
and produces printing instructions for 2 extruder prints. \
The 'base' (to be printed by Ext 0) and 'insert' (to be printed by Ext 1) \
files should be Skeinforge penultimate gcode files (see Skeinforge Export settings) \
sliced at the same layer height.\n\
The extruder 1 on/off files (they are picked from the Skeinforge alterations folder) are inserted \
when the extruder has to switch from 0 to 1 and then back to 0 while printing a layer. \n\
The resulted merged gcode is stripped and automatically loaded for printing by pronterface UI. \
It is also saved in the automatically generated file named as shown in the 'Output file name' box."
infoText = wx.StaticText(self.scrollbarPanel, -1, text)
infoStaticBoxSizer.SetMinSize((600, -1))
infoText.Wrap(600)
infoSizer.Add(infoText, pos=(0, 0))
infoStaticBoxSizer.Add(infoSizer, 1, wx.EXPAND | wx.ALL, 0)
settingsSizer.Add(infoStaticBoxSizer, 1, wx.EXPAND | wx.ALL, 0)
parametersBoxSizer = wx.BoxSizer(wx.VERTICAL)
settingRow = wx.BoxSizer(wx.VERTICAL)
settingSizer = wx.BoxSizer(wx.HORIZONTAL)
settingLabel = wx.StaticText(self.scrollbarPanel, -1, "Base penultimate gcode:")
settingLabel.Wrap(200)
settingSizer.Add(settingLabel, 0, flag=wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)
textCtrl = wx.TextCtrl(self.scrollbarPanel, value=self.settings.basePenGcode, size=(200, -1))
self.baseTc = textCtrl
settingSizer.Add(textCtrl, 0, flag=wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)
settingSizer.Add(self.browseButton1, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 10)
settingRow.Add(settingSizer, 0, wx.TOP, 10)
parametersBoxSizer.Add(settingRow, 0, wx.EXPAND, 0)
settingRow = wx.BoxSizer(wx.VERTICAL)
settingSizer = wx.BoxSizer(wx.HORIZONTAL)
settingLabel = wx.StaticText(self.scrollbarPanel, -1, "Insert penultimate gcode:")
settingLabel.Wrap(200)
settingSizer.Add(settingLabel, 0, flag=wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)
textCtrl = wx.TextCtrl(self.scrollbarPanel, value=self.settings.insPenGcode, size=(200, -1))
self.insTc = textCtrl
settingSizer.Add(textCtrl, 0, flag=wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)
settingSizer.Add(self.browseButton2, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 10)
settingRow.Add(settingSizer, 0, wx.TOP, 10)
parametersBoxSizer.Add(settingRow, 0, wx.EXPAND, 0)
settingRow = wx.BoxSizer(wx.VERTICAL)
settingSizer = wx.BoxSizer(wx.HORIZONTAL)
settingLabel = wx.StaticText(self.scrollbarPanel, -1, "Ext 1 On gcode:")
settingLabel.Wrap(200)
settingSizer.Add(settingLabel, 0, flag=wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)
textCtrl = wx.ComboBox(self.scrollbarPanel, value=self.settings.colorOn, choices=self.altfiles, style=wx.CB_DROPDOWN, size=(200, -1))
#textCtrl = wx.TextCtrl(self.scrollbarPanel, value=self.settings.colorOn, size=(200, -1))
self.extOnTc = textCtrl
settingSizer.Add(textCtrl, 0, flag=wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)
settingRow.Add(settingSizer, 0, wx.TOP, 10)
parametersBoxSizer.Add(settingRow, 0, wx.EXPAND, 0)
settingRow = wx.BoxSizer(wx.VERTICAL)
settingSizer = wx.BoxSizer(wx.HORIZONTAL)
settingLabel = wx.StaticText(self.scrollbarPanel, -1, "Ext 1 Off gcode:")
settingLabel.Wrap(200)
settingSizer.Add(settingLabel, 0, flag=wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)
textCtrl = wx.ComboBox(self.scrollbarPanel, value=self.settings.colorOff, choices=self.altfiles, style=wx.CB_DROPDOWN, size=(200, -1))
#textCtrl = wx.TextCtrl(self.scrollbarPanel, value=self.settings.colorOff, size=(200, -1))
self.extOffTc = textCtrl
settingSizer.Add(textCtrl, 0, flag=wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)
settingRow.Add(settingSizer, 0, wx.TOP, 10)
parametersBoxSizer.Add(settingRow, 0, wx.EXPAND, 0)
settingRow = wx.BoxSizer(wx.VERTICAL)
settingSizer = wx.BoxSizer(wx.HORIZONTAL)
settingLabel = wx.StaticText(self.scrollbarPanel, -1, "Output file name:")
settingLabel.Wrap(200)
settingSizer.Add(settingLabel, 0, flag=wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)
textCtrl = wx.TextCtrl(self.scrollbarPanel, value='', size=(150, -1))
self.outFileTc = textCtrl
settingSizer.Add(textCtrl, 0, flag=wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)
settingRow.Add(settingSizer, 0, wx.TOP, 10)
parametersBoxSizer.Add(settingRow, 0, wx.EXPAND, 0)
self.makeUpMixFileName(self.settings.basePenGcode)
settingsSizer.Add(parametersBoxSizer, 0, wx.EXPAND | wx.ALL, 0)
return settingsSizer
def OnExit(self, e):
self.EndModal(-1)
self.Destroy()
def OnSave(self, e):
self.settings.colorOn = self.extOnTc.GetValue()
self.settings.colorOff = self.extOffTc.GetValue()
self.settings.basePenGcode = self.baseTc.GetValue()
self.settings.insPenGcode = self.insTc.GetValue()
self.set("colorOn", self.settings.colorOn)
self.set("colorOff", self.settings.colorOff)
self.set("basePenGcode", self.settings.basePenGcode)
self.set("insPenGcode", self.settings.insPenGcode)
self.altpath = archive.getSettingsPath('alterations')
mixed = self.outFileTc.GetValue()
try:
out=open(mixed,"w+")
out.write("")
except IOError,x:
print str(x)
self.EndModal(2)
self.Destroy()
return
print "Starting gcode mixing script..."
try:
mixed_layers = self.Mix(out)
code = 0
except IOError, e:
print e.errno
print e
code = -1
except:
print "The script has failed. Internal error!"
code = -1
if (code != 0):
try:
os.remove(mixed)
except:
pass
self.EndModal(2)
self.Destroy()
else:
out.close()
print "Done, successfully mixed %d layers" % mixed_layers
self.mixedGcode = mixed
self.EndModal(1)
self.Destroy()
def GetPath(self):
return self.mixedGcode
class SkeinforgeX2MergeApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
return True
if __name__ == "__main__":
skeinforgeX2MergeApp = SkeinforgeX2MergeApp(0)
X2MergeDialog(None, -1, "").ShowModal()
exit()