1 from __future__ import absolute_import
3 import wx, os, platform, types, webbrowser, math, subprocess, multiprocessing, threading, time, re, shutil
5 from Cura.util import profile
6 from Cura.util import sliceRun
7 from Cura.util import meshLoader
8 from Cura.gui import dropTarget
10 class batchRunWindow(wx.Frame):
11 def __init__(self, parent):
12 super(batchRunWindow, self).__init__(parent, title='Cura - Batch run')
16 self.SetDropTarget(dropTarget.FileDropTarget(self.OnDropFiles, meshLoader.supportedExtensions()))
18 wx.EVT_CLOSE(self, self.OnClose)
19 self.panel = wx.Panel(self, -1)
20 self.SetSizer(wx.BoxSizer(wx.VERTICAL))
21 self.GetSizer().Add(self.panel, 1, flag=wx.EXPAND)
22 #self.SetIcon(icon.getMainIcon())
24 self.sizer = wx.GridBagSizer(2,2)
25 self.panel.SetSizer(self.sizer)
27 self.listbox = wx.ListBox(self.panel, -1, choices=[])
28 self.addButton = wx.Button(self.panel, -1, "Add")
29 self.remButton = wx.Button(self.panel, -1, "Remove")
30 self.sliceButton = wx.Button(self.panel, -1, "Prepare all")
32 self.addButton.Bind(wx.EVT_BUTTON, self.OnAddModel)
33 self.remButton.Bind(wx.EVT_BUTTON, self.OnRemModel)
34 self.sliceButton.Bind(wx.EVT_BUTTON, self.OnSlice)
35 self.listbox.Bind(wx.EVT_LISTBOX, self.OnListSelect)
37 self.sizer.Add(self.listbox, (0,0), span=(1,3), flag=wx.EXPAND)
38 self.sizer.Add(self.addButton, (1,0), span=(1,1))
39 self.sizer.Add(self.remButton, (1,1), span=(1,1))
40 self.sizer.Add(self.sliceButton, (1,2), span=(1,1))
42 self.sizer.AddGrowableCol(2)
43 self.sizer.AddGrowableRow(0)
45 def OnAddModel(self, e):
46 dlg=wx.FileDialog(self, "Open file to batch prepare", os.path.split(profile.getPreference('lastFile'))[0], style=wx.FD_OPEN|wx.FD_FILE_MUST_EXIST|wx.FD_MULTIPLE)
47 dlg.SetWildcard("STL files (*.stl)|*.stl;*.STL")
48 if dlg.ShowModal() == wx.ID_OK:
49 for filename in dlg.GetPaths():
50 profile.putPreference('lastFile', filename)
51 self.list.append(filename)
52 self.selection = filename
56 def OnDropFiles(self, filenames):
57 for filename in filenames:
58 profile.putPreference('lastFile', filename)
59 self.list.append(filename)
60 self.selection = filename
63 def OnRemModel(self, e):
64 if self.selection == None:
66 self.list.remove(self.selection)
69 def OnListSelect(self, e):
70 if self.listbox.GetSelection() == -1:
72 self.selection = self.list[self.listbox.GetSelection()]
74 def _updateListbox(self):
76 for item in self.list:
77 self.listbox.AppendAndEnsureVisible(os.path.split(item)[1])
78 if self.selection in self.list:
79 self.listbox.SetSelection(self.list.index(self.selection))
80 elif len(self.list) > 0:
81 self.selection = self.list[0]
82 self.listbox.SetSelection(0)
85 self.listbox.SetSelection(-1)
92 for filename in self.list:
93 sliceCmdList.append(sliceRun.getSliceCommand(filename))
94 bspw = BatchSliceProgressWindow(self.list[:], sliceCmdList)
98 class BatchSliceProgressWindow(wx.Frame):
99 def __init__(self, filenameList, sliceCmdList):
100 super(BatchSliceProgressWindow, self).__init__(None, title='Cura')
101 self.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE))
103 self.filenameList = filenameList
104 self.sliceCmdList = sliceCmdList
106 self.sliceStartTime = time.time()
109 self.threadCount = multiprocessing.cpu_count() - 1
112 if self.threadCount < 1:
114 if self.threadCount > len(self.sliceCmdList):
115 self.threadCount = len(self.sliceCmdList)
119 self.totalDoneFactor = []
120 for i in xrange(0, self.threadCount):
121 self.prevStep.append('start')
122 self.totalDoneFactor.append(0.0)
124 self.sizer = wx.GridBagSizer(2, 2)
125 self.progressGauge = []
127 for i in xrange(0, self.threadCount):
128 self.statusText.append(wx.StaticText(self, -1, "Building: %d " % (len(self.sliceCmdList))))
129 self.progressGauge.append(wx.Gauge(self, -1))
130 self.progressGauge[i].SetRange(10000)
131 self.progressTextTotal = wx.StaticText(self, -1, "Done: 0/%d " % (len(self.sliceCmdList)))
132 self.progressGaugeTotal = wx.Gauge(self, -1)
133 self.progressGaugeTotal.SetRange(len(self.sliceCmdList))
134 self.abortButton = wx.Button(self, -1, "Abort")
135 for i in xrange(0, self.threadCount):
136 self.sizer.Add(self.statusText[i], (i*2,0), span=(1,4))
137 self.sizer.Add(self.progressGauge[i], (1+i*2, 0), span=(1,4), flag=wx.EXPAND)
138 self.sizer.Add(self.progressTextTotal, (self.threadCount*2,0), span=(1,4))
139 self.sizer.Add(self.progressGaugeTotal, (1+self.threadCount*2, 0), span=(1,4), flag=wx.EXPAND)
141 self.sizer.Add(self.abortButton, (2+self.threadCount*2,0), span=(1,4), flag=wx.ALIGN_CENTER)
142 self.sizer.AddGrowableCol(0)
143 self.sizer.AddGrowableRow(0)
145 self.Bind(wx.EVT_BUTTON, self.OnAbort, self.abortButton)
146 self.SetSizer(self.sizer)
150 threading.Thread(target=self.OnRunManager).start()
152 def OnAbort(self, e):
157 self.abortButton.SetLabel('Close')
159 def SetProgress(self, index, stepName, layer, maxLayer):
160 if self.prevStep[index] != stepName:
161 self.totalDoneFactor[index] += sliceRun.sliceStepTimeFactor[self.prevStep[index]]
162 newTime = time.time()
163 self.prevStep[index] = stepName
165 progresValue = ((self.totalDoneFactor[index] + sliceRun.sliceStepTimeFactor[stepName] * layer / maxLayer) / sliceRun.totalRunTimeFactor) * 10000
166 self.progressGauge[index].SetValue(int(progresValue))
167 self.statusText[index].SetLabel(stepName + " [" + str(layer) + "/" + str(maxLayer) + "]")
169 def OnRunManager(self):
171 for i in xrange(0, self.threadCount):
172 threads.append(threading.Thread(target=self.OnRun, args=(i,)))
180 sliceTime = time.time() - self.sliceStartTime
181 status = "Build: %d models" % (len(self.sliceCmdList))
182 status += "\nSlicing took: %02d:%02d" % (sliceTime / 60, sliceTime % 60)
184 wx.CallAfter(self.statusText[0].SetLabel, status)
185 wx.CallAfter(self.OnSliceDone)
187 def OnRun(self, index):
188 while self.cmdIndex < len(self.sliceCmdList):
189 action = self.sliceCmdList[self.cmdIndex]
191 wx.CallAfter(self.SetTitle, "Building: [%d/%d]" % (self.sliceCmdList.index(action) + 1, len(self.sliceCmdList)))
193 p = sliceRun.startSliceCommandProcess(action)
194 line = p.stdout.readline()
196 while(len(line) > 0):
198 if line[0:9] == "Progress[" and line[-1:] == "]":
199 progress = line[9:-1].split(":")
200 if len(progress) > 2:
201 maxValue = int(progress[2])
202 wx.CallAfter(self.SetProgress, index, progress[0], int(progress[1]), maxValue)
204 wx.CallAfter(self.statusText[index].SetLabel, line)
207 wx.CallAfter(self.statusText[index].SetLabel, "Aborted by user.")
209 line = p.stdout.readline()
210 self.returnCode = p.wait()
212 wx.CallAfter(self.progressGauge[index].SetValue, 10000)
213 self.totalDoneFactor[index] = 0.0
214 wx.CallAfter(self.progressTextTotal.SetLabel, "Done %d/%d" % (self.cmdIndex, len(self.sliceCmdList)))
215 wx.CallAfter(self.progressGaugeTotal.SetValue, self.cmdIndex)
217 def OnSliceDone(self):
218 self.abortButton.Destroy()
219 self.closeButton = wx.Button(self, -1, "Close")
220 self.sizer.Add(self.closeButton, (2+self.threadCount*2,0), span=(1,1))
221 if profile.getPreference('sdpath') != '':
222 self.copyToSDButton = wx.Button(self, -1, "To SDCard")
223 self.Bind(wx.EVT_BUTTON, self.OnCopyToSD, self.copyToSDButton)
224 self.sizer.Add(self.copyToSDButton, (2+self.threadCount*2,1), span=(1,1))
225 self.Bind(wx.EVT_BUTTON, self.OnAbort, self.closeButton)
229 def OnCopyToSD(self, e):
230 for f in self.filenameList:
231 exportFilename = sliceRun.getExportFilename(f)
232 filename = os.path.basename(exportFilename)
233 if profile.getPreference('sdshortnames') == 'True':
234 filename = sliceRun.getShortFilename(filename)
235 shutil.copy(exportFilename, os.path.join(profile.getPreference('sdpath'), filename))