chiark / gitweb /
Merge branch 'master' into SteamEngine
[cura.git] / Cura / util / sliceEngine.py
1 import subprocess
2 import time
3 import numpy
4 import os
5 import warnings
6 import threading
7
8 from Cura.util import profile
9
10 def getEngineFilename():
11         return 'C:/Software/Cura_SteamEngine/_bin/Release/Cura_SteamEngine.exe'
12
13 def getTempFilename():
14         warnings.simplefilter('ignore')
15         ret = os.tempnam(None, "Cura_Tmp")
16         warnings.simplefilter('default')
17         return ret
18
19 class Slicer(object):
20         def __init__(self, progressCallback):
21                 self._process = None
22                 self._thread = None
23                 self._callback = progressCallback
24                 self._binaryStorageFilename = getTempFilename()
25                 self._exportFilename = getTempFilename()
26                 self._progressSteps = ['inset', 'skin', 'export']
27
28         def abortSlicer(self):
29                 if self._process is not None:
30                         self._process.terminate()
31                         self._thread.join()
32
33         def getGCodeFilename(self):
34                 return self._exportFilename
35
36         def runSlicer(self, scene):
37                 self.abortSlicer()
38                 self._callback(0.0, False)
39
40                 commandList = [getEngineFilename(), '-vv']
41                 for k, v in self._engineSettings().iteritems():
42                         commandList += ['-s', '%s=%s' % (k, str(v))]
43                 commandList += ['-o', self._exportFilename]
44                 commandList += ['-b', self._binaryStorageFilename]
45                 self._objCount = 0
46                 with open(self._binaryStorageFilename, "wb") as f:
47                         for obj in scene._objectList:
48                                 if not scene.checkPlatform(obj):
49                                         continue
50                                 for mesh in obj._meshList:
51                                         n = numpy.array(mesh.vertexCount, numpy.int32)
52                                         f.write(n.tostring())
53                                         f.write(mesh.vertexes.tostring())
54                                 pos = obj.getPosition() * 1000
55                                 pos += numpy.array([profile.getPreferenceFloat('machine_width') * 1000 / 2, profile.getPreferenceFloat('machine_depth') * 1000 / 2])
56                                 commandList += ['-m', ','.join(map(str, obj._matrix.getA().flatten()))]
57                                 commandList += ['-s', 'posx=%d' % int(pos[0]), '-s', 'posy=%d' % int(pos[1]), '#']
58                                 self._objCount += 1
59                 if self._objCount > 0:
60                         print ' '.join(commandList)
61                         self._process = self._runSliceProcess(commandList)
62                         self._thread = threading.Thread(target=self._watchProcess)
63                         self._thread.daemon = True
64                         self._thread.start()
65
66         def _watchProcess(self):
67                 self._callback(0.0, False)
68                 line = self._process.stdout.readline()
69                 while len(line):
70                         line = line.strip()
71                         if line.startswith('Progress:'):
72                                 line = line.split(':')
73                                 progressValue = float(line[2]) / float(line[3])
74                                 progressValue /= len(self._progressSteps)
75                                 progressValue += 1.0 / len(self._progressSteps) * self._progressSteps.index(line[1])
76                                 self._callback(progressValue, False)
77                         else:
78                                 print '#', line.strip()
79                         line = self._process.stdout.readline()
80                 for line in self._process.stderr:
81                         print line.strip()
82                 returnCode = self._process.wait()
83                 print returnCode
84                 if returnCode == 0:
85                         self._callback(1.0, True)
86                 else:
87                         self._callback(0.0, False)
88                 self._process = None
89
90         def _engineSettings(self):
91                 return {
92                         'layerThickness': int(profile.getProfileSettingFloat('layer_height') * 1000),
93                         'initialLayerThickness': int(profile.getProfileSettingFloat('bottom_thickness') * 1000),
94                         'filamentDiameter': int(profile.getProfileSettingFloat('filament_diameter') * 1000),
95                         'extrusionWidth': int(profile.calculateEdgeWidth() * 1000),
96                         'insetCount': int(profile.calculateLineCount()),
97                         'downSkinCount': int(profile.calculateSolidLayerCount()),
98                         'upSkinCount': int(profile.calculateSolidLayerCount()),
99                         'sparseInfillLineDistance': int(100 * profile.calculateEdgeWidth() * 1000 / profile.getProfileSettingFloat('fill_density')),
100                         'skirtDistance': int(profile.getProfileSettingFloat('skirt_gap') * 1000),
101                         'skirtLineCount': int(profile.getProfileSettingFloat('skirt_line_count')),
102                         'initialSpeedupLayers': int(4),
103                         'initialLayerSpeed': int(profile.getProfileSettingFloat('bottom_layer_speed')),
104                         'printSpeed': int(profile.getProfileSettingFloat('print_speed')),
105                         'moveSpeed': int(profile.getProfileSettingFloat('travel_speed')),
106                         'fanOnLayerNr': int(profile.getProfileSettingFloat('fan_layer')),
107                         'supportAngle': int(-1) if profile.getProfileSetting('support') == 'None' else int(60),
108                         'supportEverywhere': int(1) if profile.getProfileSetting('support') == 'Everywhere' else int(0),
109                         'retractionAmount': int(profile.getProfileSettingFloat('retraction_amount') * 1000),
110                         'retractionSpeed': int(profile.getProfileSettingFloat('retraction_speed')),
111                         'objectSink': int(profile.getProfileSettingFloat('object_sink') * 1000),
112                 }
113
114         def _runSliceProcess(self, cmdList):
115                 kwargs = {}
116                 if subprocess.mswindows:
117                         su = subprocess.STARTUPINFO()
118                         su.dwFlags |= subprocess.STARTF_USESHOWWINDOW
119                         su.wShowWindow = subprocess.SW_HIDE
120                         kwargs['startupinfo'] = su
121                         kwargs['creationflags'] = 0x00004000 #BELOW_NORMAL_PRIORITY_CLASS
122                 return subprocess.Popen(cmdList, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)