chiark / gitweb /
Fixed the imports.
[cura.git] / Cura / gui / batchRun.py
1 from __future__ import absolute_import
2
3 import wx, os, platform, types, webbrowser, math, subprocess, multiprocessing, threading, time, re, shutil
4
5 from Cura.util import profile
6 from Cura.util import sliceRun
7 from Cura.util import meshLoader
8 from Cura.gui import dropTarget
9
10 class batchRunWindow(wx.Frame):
11         def __init__(self, parent):
12                 super(batchRunWindow, self).__init__(parent, title='Cura - Batch run')
13                 
14                 self.list = []
15                 
16                 self.SetDropTarget(dropTarget.FileDropTarget(self.OnDropFiles, meshLoader.supportedExtensions()))
17                 
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())
23
24                 self.sizer = wx.GridBagSizer(2,2)
25                 self.panel.SetSizer(self.sizer)
26
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")
31
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)
36
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))
41
42                 self.sizer.AddGrowableCol(2)
43                 self.sizer.AddGrowableRow(0)
44
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
53                                 self._updateListbox()
54                 dlg.Destroy()
55         
56         def OnDropFiles(self, filenames):
57                 for filename in filenames:
58                         profile.putPreference('lastFile', filename)
59                         self.list.append(filename)
60                         self.selection = filename
61                         self._updateListbox()
62
63         def OnRemModel(self, e):
64                 if self.selection == None:
65                         return
66                 self.list.remove(self.selection)
67                 self._updateListbox()
68
69         def OnListSelect(self, e):
70                 if self.listbox.GetSelection() == -1:
71                         return
72                 self.selection = self.list[self.listbox.GetSelection()]
73
74         def _updateListbox(self):
75                 self.listbox.Clear()
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)
83                 else:
84                         self.selection = None
85                         self.listbox.SetSelection(-1)
86
87         def OnClose(self, e):
88                 self.Destroy()
89
90         def OnSlice(self, e):
91                 sliceCmdList = []
92                 for filename in self.list:
93                         sliceCmdList.append(sliceRun.getSliceCommand(filename))
94                 bspw = BatchSliceProgressWindow(self.list[:], sliceCmdList)
95                 bspw.Centre()
96                 bspw.Show(True)
97         
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))
102                 
103                 self.filenameList = filenameList
104                 self.sliceCmdList = sliceCmdList
105                 self.abort = False
106                 self.sliceStartTime = time.time()
107
108                 try:
109                         self.threadCount = multiprocessing.cpu_count() - 1
110                 except:
111                         self.threadCount = 1
112                 if self.threadCount < 1:
113                         self.threadCount = 1
114                 if self.threadCount > len(self.sliceCmdList):
115                         self.threadCount = len(self.sliceCmdList)
116                 self.cmdIndex = 0
117                 
118                 self.prevStep = []
119                 self.totalDoneFactor = []
120                 for i in xrange(0, self.threadCount):
121                         self.prevStep.append('start')
122                         self.totalDoneFactor.append(0.0)
123
124                 self.sizer = wx.GridBagSizer(2, 2) 
125                 self.progressGauge = []
126                 self.statusText = []
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)
140
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)
144
145                 self.Bind(wx.EVT_BUTTON, self.OnAbort, self.abortButton)
146                 self.SetSizer(self.sizer)
147                 self.Layout()
148                 self.Fit()
149                 
150                 threading.Thread(target=self.OnRunManager).start()
151
152         def OnAbort(self, e):
153                 if self.abort:
154                         self.Close()
155                 else:
156                         self.abort = True
157                         self.abortButton.SetLabel('Close')
158
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
164                 
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) + "]")
168         
169         def OnRunManager(self):
170                 threads = []
171                 for i in xrange(0, self.threadCount):
172                         threads.append(threading.Thread(target=self.OnRun, args=(i,)))
173
174                 for t in threads:
175                         t.start()
176                 for t in threads:
177                         t.join()
178
179                 self.abort = True
180                 sliceTime = time.time() - self.sliceStartTime
181                 status = "Build: %d models" % (len(self.sliceCmdList))
182                 status += "\nSlicing took: %02d:%02d" % (sliceTime / 60, sliceTime % 60)
183
184                 wx.CallAfter(self.statusText[0].SetLabel, status)
185                 wx.CallAfter(self.OnSliceDone)
186         
187         def OnRun(self, index):
188                 while self.cmdIndex < len(self.sliceCmdList):
189                         action = self.sliceCmdList[self.cmdIndex]
190                         self.cmdIndex += 1
191                         wx.CallAfter(self.SetTitle, "Building: [%d/%d]"  % (self.sliceCmdList.index(action) + 1, len(self.sliceCmdList)))
192
193                         p = sliceRun.startSliceCommandProcess(action)
194                         line = p.stdout.readline()
195                         maxValue = 1
196                         while(len(line) > 0):
197                                 line = line.rstrip()
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)
203                                 else:
204                                         wx.CallAfter(self.statusText[index].SetLabel, line)
205                                 if self.abort:
206                                         p.terminate()
207                                         wx.CallAfter(self.statusText[index].SetLabel, "Aborted by user.")
208                                         return
209                                 line = p.stdout.readline()
210                         self.returnCode = p.wait()
211                         
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)
216         
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)
226                 self.Layout()
227                 self.Fit()
228
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))