1 from __future__ import absolute_import
4 import wx, sys, os, math, threading, subprocess, time
6 from util import profile
7 from util import sliceRun
8 from util import exporer
9 from util import gcodeInterpreter
11 class sliceProgessPanel(wx.Panel):
12 def __init__(self, mainWindow, parent, filelist):
13 wx.Panel.__init__(self, parent, -1)
14 self.mainWindow = mainWindow
15 self.filelist = filelist
18 #How long does each step take compared to the others. This is used to make a better scaled progress bar, and guess time left.
19 self.sliceStepTimeFactor = {
20 'start': 3.3713991642,
21 'slice': 15.4984838963,
22 'preface': 5.17178297043,
23 'inset': 116.362634182,
24 'fill': 215.702672005,
25 'multiply': 21.9536788464,
26 'speed': 12.759510994,
27 'raft': 31.4580039978,
28 'skirt': 19.3436040878,
31 'comb': 23.7805759907,
33 'dimension': 90.4914340973
35 self.totalRunTimeFactor = 0
36 for v in self.sliceStepTimeFactor.itervalues():
37 self.totalRunTimeFactor += v
39 box = wx.StaticBox(self, -1, filelist[0])
40 self.sizer = wx.StaticBoxSizer(box, wx.HORIZONTAL)
42 mainSizer = wx.BoxSizer(wx.VERTICAL)
43 mainSizer.Add(self.sizer, 0, flag=wx.EXPAND)
45 self.statusText = wx.StaticText(self, -1, "Starting...")
46 self.progressGauge = wx.Gauge(self, -1)
47 self.progressGauge.SetRange(10000 * len(filelist))
48 self.abortButton = wx.Button(self, -1, "X", style=wx.BU_EXACTFIT)
49 self.sizer.Add(self.statusText, 2, flag=wx.ALIGN_CENTER )
50 self.sizer.Add(self.progressGauge, 2)
51 self.sizer.Add(self.abortButton, 0)
53 self.Bind(wx.EVT_BUTTON, self.OnAbort, self.abortButton)
55 self.SetSizer(mainSizer)
56 self.prevStep = 'start'
57 self.totalDoneFactor = 0.0
58 self.startTime = time.time()
59 if profile.getPreference('save_profile') == 'True':
60 profile.saveGlobalProfile(self.filelist[0][: self.filelist[0].rfind('.')] + "_profile.ini")
62 oldProfile = profile.getGlobalProfileString()
63 for filename in self.filelist:
64 idx = self.filelist.index(filename)
67 profile.putProfileSetting('fan_enabled', 'False')
68 profile.putProfileSetting('skirt_line_count', '0')
69 profile.putProfileSetting('machine_center_x', profile.getProfileSettingFloat('machine_center_x') - float(profile.getPreference('extruder_offset_x%d' % (idx))))
70 profile.putProfileSetting('machine_center_y', profile.getProfileSettingFloat('machine_center_y') - float(profile.getPreference('extruder_offset_y%d' % (idx))))
71 profile.putProfileSetting('alternative_center', self.filelist[0])
72 if len(self.filelist) > 1:
73 profile.putProfileSetting('add_start_end_gcode', 'False')
74 profile.putProfileSetting('gcode_extension', 'multi_extrude_tmp')
75 cmdList.append(sliceRun.getSliceCommand(filename))
76 profile.loadGlobalProfileFromString(oldProfile)
77 self.thread = WorkerThread(self, filelist, cmdList)
81 self.mainWindow.removeSliceProgress(self)
85 def OnShowGCode(self, e):
86 self.mainWindow.preview3d.loadModelFiles(self.filelist)
87 self.mainWindow.preview3d.setViewMode("GCode")
89 def OnShowLog(self, e):
90 LogWindow('\n'.join(self.progressLog))
92 def OnOpenFileLocation(self, e):
93 exporer.openExporer(self.filelist[0][: self.filelist[0].rfind('.')] + "_export.gcode")
95 def OnSliceDone(self, result):
96 self.progressGauge.Destroy()
97 self.abortButton.Destroy()
98 self.progressLog = result.progressLog
99 self.logButton = wx.Button(self, -1, "Show Log")
100 self.abortButton = wx.Button(self, -1, "X", style=wx.BU_EXACTFIT)
101 self.Bind(wx.EVT_BUTTON, self.OnShowLog, self.logButton)
102 self.Bind(wx.EVT_BUTTON, self.OnAbort, self.abortButton)
103 self.sizer.Add(self.logButton, 0)
104 if result.returnCode == 0:
105 status = "Ready: Filament: %.2fm %.2fg" % (result.gcode.extrusionAmount / 1000, result.gcode.calculateWeight() * 1000)
106 status += " Print time: %02d:%02d" % (int(result.gcode.totalMoveTimeMinute / 60), int(result.gcode.totalMoveTimeMinute % 60))
107 cost = result.gcode.calculateCost()
109 status += " Cost: %s" % (cost)
110 self.statusText.SetLabel(status)
111 if exporer.hasExporer():
112 self.openFileLocationButton = wx.Button(self, -1, "Open file location")
113 self.Bind(wx.EVT_BUTTON, self.OnOpenFileLocation, self.openFileLocationButton)
114 self.sizer.Add(self.openFileLocationButton, 0)
115 self.showButton = wx.Button(self, -1, "Show result")
116 self.Bind(wx.EVT_BUTTON, self.OnShowGCode, self.showButton)
117 self.sizer.Add(self.showButton, 0)
119 self.statusText.SetLabel("Something went wrong during slicing!")
120 self.sizer.Add(self.abortButton, 0)
124 if self.mainWindow.preview3d.loadReModelFiles(self.filelist):
125 self.mainWindow.preview3d.setViewMode("GCode")
127 def SetProgress(self, stepName, layer, maxLayer):
128 if self.prevStep != stepName:
129 self.totalDoneFactor += self.sliceStepTimeFactor[self.prevStep]
130 newTime = time.time()
131 #print "#####" + str(newTime-self.startTime) + " " + self.prevStep + " -> " + stepName
132 self.startTime = newTime
133 self.prevStep = stepName
135 progresValue = ((self.totalDoneFactor + self.sliceStepTimeFactor[stepName] * layer / maxLayer) / self.totalRunTimeFactor) * 10000
136 self.progressGauge.SetValue(int(progresValue))
137 self.statusText.SetLabel(stepName + " [" + str(layer) + "/" + str(maxLayer) + "]")
139 class WorkerThread(threading.Thread):
140 def __init__(self, notifyWindow, filelist, cmdList):
141 threading.Thread.__init__(self)
142 self.filelist = filelist
143 self.notifyWindow = notifyWindow
144 self.cmdList = cmdList
149 p = subprocess.Popen(self.cmdList[self.fileIdx], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
150 line = p.stdout.readline()
151 self.progressLog = []
153 while(len(line) > 0):
155 if line[0:9] == "Progress[" and line[-1:] == "]":
156 progress = line[9:-1].split(":")
157 if len(progress) > 2:
158 maxValue = int(progress[2])
159 wx.CallAfter(self.notifyWindow.SetProgress, progress[0], int(progress[1]), maxValue)
162 self.progressLog.append(line)
163 wx.CallAfter(self.notifyWindow.statusText.SetLabel, line)
164 if self.notifyWindow.abort:
166 wx.CallAfter(self.notifyWindow.statusText.SetLabel, "Aborted by user.")
168 line = p.stdout.readline()
169 self.returnCode = p.wait()
170 logfile = open(self.filelist[self.fileIdx][: self.filelist[self.fileIdx].rfind('.')] + "_export.log", "w")
171 for logLine in self.progressLog:
172 logfile.write(logLine)
176 if self.fileIdx == len(self.cmdList):
177 if len(self.filelist) > 1:
178 self._stitchMultiExtruder()
179 self.gcode = gcodeInterpreter.gcode()
180 self.gcode.load(self.filelist[0][:self.filelist[0].rfind('.')]+'_export.gcode')
181 wx.CallAfter(self.notifyWindow.OnSliceDone, self)
185 def _stitchMultiExtruder(self):
187 resultFile = open(self.filelist[0][:self.filelist[0].rfind('.')]+'_export.gcode', "w")
188 resultFile.write(';TYPE:CUSTOM\n')
189 resultFile.write(profile.getAlterationFileContents('start.gcode'))
190 for filename in self.filelist:
191 files.append(open(filename[:filename.rfind('.')]+'_export.multi_extrude_tmp', "r"))
194 resultFile.write('T%d\n' % (currentExtruder))
203 if line.startswith(';LAYER:'):
206 nextExtruder = files.index(f)
207 resultFile.write(';LAYER:%d\n' % (layerNr))
208 resultFile.write(';EXTRUDER:%d\n' % (nextExtruder))
209 if nextExtruder != currentExtruder:
210 resultFile.write("G1 E-5 F5000\n")
211 resultFile.write("G92 E0\n")
212 resultFile.write("T%d\n" % (nextExtruder))
213 resultFile.write("G1 E5 F5000\n")
214 resultFile.write("G92 E0\n")
215 currentExtruder = nextExtruder
217 resultFile.write(line)
221 for filename in self.filelist:
222 os.remove(filename[:filename.rfind('.')]+'_export.multi_extrude_tmp')
223 resultFile.write(';TYPE:CUSTOM\n')
224 resultFile.write(profile.getAlterationFileContents('end.gcode'))
227 class LogWindow(wx.Frame):
228 def __init__(self, logText):
229 super(LogWindow, self).__init__(None, title="Slice log")
230 self.textBox = wx.TextCtrl(self, -1, logText, style=wx.TE_MULTILINE|wx.TE_DONTWRAP|wx.TE_READONLY)
231 self.SetSize((400,300))