chiark / gitweb /
Move SF into its own directory, to seperate SF and Cura. Rename newui to gui.
[cura.git] / Cura / gui / sliceProgessPanel.py
1 from __future__ import absolute_import
2 import __init__
3
4 import wx
5 import sys
6 import math
7 import threading
8 import subprocess
9 import time
10
11 from gui import sliceRun
12
13 class sliceProgessPanel(wx.Panel):
14         def __init__(self, mainWindow, parent, filename):
15                 wx.Panel.__init__(self, parent, -1)
16                 self.mainWindow = mainWindow
17                 self.filename = filename
18                 self.abort = False
19                 
20                 #How long does each step take compared to the others. This is used to make a better scaled progress bar, and guess time left.
21                 self.sliceStepTimeFactor = {
22                         'start': 3.3713991642,
23                         'slice': 15.4984838963,
24                         'preface': 5.17178297043,
25                         'inset': 116.362634182,
26                         'fill': 215.702672005,
27                         'multiply': 21.9536788464,
28                         'speed': 12.759510994,
29                         'raft': 31.4580039978,
30                         'skirt': 19.3436040878,
31                         'joris': 1.0,
32                         'comb': 23.7805759907,
33                         'cool': 27.148763895,
34                         'dimension': 90.4914340973
35                 }
36                 self.totalRunTimeFactor = 0
37                 for v in self.sliceStepTimeFactor.itervalues():
38                         self.totalRunTimeFactor += v
39
40                 box = wx.StaticBox(self, -1, filename)
41                 self.sizer = wx.StaticBoxSizer(box, wx.HORIZONTAL)
42
43                 mainSizer = wx.BoxSizer(wx.VERTICAL) 
44                 mainSizer.Add(self.sizer, 0, flag=wx.EXPAND)
45
46                 self.statusText = wx.StaticText(self, -1, "Starting...")
47                 self.progressGauge = wx.Gauge(self, -1)
48                 self.progressGauge.SetRange(10000)
49                 self.abortButton = wx.Button(self, -1, "X", style=wx.BU_EXACTFIT)
50                 self.sizer.Add(self.statusText, 2, flag=wx.ALIGN_CENTER )
51                 self.sizer.Add(self.progressGauge, 2)
52                 self.sizer.Add(self.abortButton, 0)
53
54                 self.Bind(wx.EVT_BUTTON, self.OnAbort, self.abortButton)
55
56                 self.SetSizer(mainSizer)
57                 self.prevStep = 'start'
58                 self.totalDoneFactor = 0.0
59                 self.startTime = time.time()
60                 self.thread = WorkerThread(self, filename)
61         
62         def OnAbort(self, e):
63                 if self.abort:
64                         self.mainWindow.removeSliceProgress(self)
65                 else:
66                         self.abort = True
67         
68         def OnShowGCode(self, e):
69                 self.mainWindow.preview3d.loadModelFile(self.filename)
70                 self.mainWindow.preview3d.setViewMode("GCode")
71         
72         def OnShowLog(self, e):
73                 LogWindow('\n'.join(self.progressLog))
74         
75         def OnSliceDone(self, result):
76                 self.progressGauge.Destroy()
77                 self.abortButton.Destroy()
78                 self.progressLog = result.progressLog
79                 self.logButton = wx.Button(self, -1, "Show Log")
80                 self.abortButton = wx.Button(self, -1, "X", style=wx.BU_EXACTFIT)
81                 self.Bind(wx.EVT_BUTTON, self.OnShowLog, self.logButton)
82                 self.Bind(wx.EVT_BUTTON, self.OnAbort, self.abortButton)
83                 self.sizer.Add(self.logButton, 0)
84                 if result.returnCode == 0:
85                         self.statusText.SetLabel("Ready.")
86                         self.showButton = wx.Button(self, -1, "Show result")
87                         self.Bind(wx.EVT_BUTTON, self.OnShowGCode, self.showButton)
88                         self.sizer.Add(self.showButton, 0)
89                 else:
90                         self.statusText.SetLabel("Something went wrong during slicing!")
91                 self.sizer.Add(self.abortButton, 0)
92                 self.sizer.Layout()
93                 self.Layout()
94                 self.abort = True
95                 if self.mainWindow.preview3d.loadReModelFile(self.filename):
96                         self.mainWindow.preview3d.setViewMode("GCode")
97         
98         def SetProgress(self, stepName, layer, maxLayer):
99                 if self.prevStep != stepName:
100                         self.totalDoneFactor += self.sliceStepTimeFactor[self.prevStep]
101                         newTime = time.time()
102                         #print "#####" + str(newTime-self.startTime) + " " + self.prevStep + " -> " + stepName
103                         self.startTime = newTime
104                         self.prevStep = stepName
105                 
106                 progresValue = ((self.totalDoneFactor + self.sliceStepTimeFactor[stepName] * layer / maxLayer) / self.totalRunTimeFactor) * 10000
107                 self.progressGauge.SetValue(int(progresValue))
108                 self.statusText.SetLabel(stepName + " [" + str(layer) + "/" + str(maxLayer) + "]")
109
110 class WorkerThread(threading.Thread):
111         def __init__(self, notifyWindow, filename):
112                 threading.Thread.__init__(self)
113                 self.filename = filename
114                 self.notifyWindow = notifyWindow
115                 self.start()
116
117         def run(self):
118                 print sliceRun.getSliceCommand(self.filename)
119                 p = subprocess.Popen(sliceRun.getSliceCommand(self.filename), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
120                 line = p.stdout.readline()
121                 maxValue = 1
122                 self.progressLog = []
123                 while(len(line) > 0):
124                         line = line.rstrip()
125                         if line[0:9] == "Progress[" and line[-1:] == "]":
126                                 progress = line[9:-1].split(":")
127                                 if len(progress) > 2:
128                                         maxValue = int(progress[2])
129                                 wx.CallAfter(self.notifyWindow.SetProgress, progress[0], int(progress[1]), maxValue)
130                         else:
131                                 print line
132                                 self.progressLog.append(line)
133                                 wx.CallAfter(self.notifyWindow.statusText.SetLabel, line)
134                         if self.notifyWindow.abort:
135                                 p.terminate()
136                                 wx.CallAfter(self.notifyWindow.statusText.SetLabel, "Aborted by user.")
137                                 return
138                         line = p.stdout.readline()
139                 self.returnCode = p.wait()
140                 logfile = open(self.filename[: self.filename.rfind('.')] + "_export.log", "w")
141                 for logLine in self.progressLog:
142                         logfile.write(logLine)
143                         logfile.write('\n')
144                 logfile.close()
145                 wx.CallAfter(self.notifyWindow.OnSliceDone, self)
146
147 class LogWindow(wx.Frame):
148         def __init__(self, logText):
149                 super(LogWindow, self).__init__(None, title="Slice log")
150                 self.textBox = wx.TextCtrl(self, -1, logText, style=wx.TE_MULTILINE|wx.TE_DONTWRAP|wx.TE_READONLY)
151                 self.SetSize((400,300))
152                 self.Centre()
153                 self.Show(True)
154