chiark / gitweb /
824aa1e0c0c11111d2e623cb31d1a46dc5d4061b
[cura.git] / Cura / util / sliceRun.py
1 from __future__ import absolute_import
2
3 import platform, os, subprocess, sys
4
5 if not hasattr(sys, 'frozen'):
6         cura_sf_path = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../cura_sf/"))
7         if cura_sf_path not in sys.path:
8                 sys.path.append(cura_sf_path)
9         from skeinforge_application.skeinforge_utilities import skeinforge_craft
10
11 from util import profile
12
13 #How long does each step take compared to the others. This is used to make a better scaled progress bar, and guess time left.
14 sliceStepTimeFactor = {
15         'start': 3.3713991642,
16         'slice': 15.4984838963,
17         'preface': 5.17178297043,
18         'inset': 116.362634182,
19         'fill': 215.702672005,
20         'multiply': 21.9536788464,
21         'speed': 12.759510994,
22         'raft': 31.4580039978,
23         'skirt': 19.3436040878,
24         'skin': 1.0,
25         'joris': 1.0,
26         'dwindle': 1.0,
27         'comb': 23.7805759907,
28         'cool': 27.148763895,
29         'dimension': 90.4914340973
30 }
31
32 totalRunTimeFactor = 0
33 for v in sliceStepTimeFactor.values():
34         totalRunTimeFactor += v
35
36 def getPyPyExe():
37         "Return the path to the pypy executable if we can find it. Else return False"
38         if platform.system() == "Windows":
39                 exeName = "pypy.exe"
40                 pypyExe = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../pypy/pypy.exe"))
41         else:
42                 exeName = "pypy"
43                 pypyExe = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../pypy/bin/pypy"))
44         if os.path.exists(pypyExe):
45                 return pypyExe
46
47         path = os.environ['PATH']
48         paths = path.split(os.pathsep)
49         for p in paths:
50                 pypyExe = os.path.join(p, exeName)
51                 if os.path.exists(pypyExe):
52                         return pypyExe 
53         return False
54
55 def getSlic3rExe():
56         "Return the path to the pypy executable if we can find it. Else return False"
57         if platform.system() == "Windows":
58                 exeName = "slic3r.exe"
59                 slic3rExe = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../Slic3r/bin/slic3r.exe"));
60         else:
61                 exeName = "slic3r"
62                 slic3rExe = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../Slic3r/bin/slic3r"));
63         if os.path.exists(slic3rExe):
64                 return slic3rExe
65
66         path = os.environ['PATH']
67         paths = path.split(os.pathsep)
68         for p in paths:
69                 slic3rExe = os.path.join(p, exeName)
70                 if os.path.exists(slic3rExe):
71                         return slic3rExe
72         return False
73
74 def runSlice(fileNames):
75         "Run the slicer on the files. If we are running with PyPy then just do the slicing action. If we are running as Python, try to find pypy."
76         pypyExe = getPyPyExe()
77         for fileName in fileNames:
78                 if fileName.startswith("#UTF8#"):
79                         fileName = unicode(fileName[6:], "utf-8")
80                 if platform.python_implementation() == "PyPy":
81                         skeinforge_craft.writeOutput(fileName)
82                 elif pypyExe == False:
83                         if not hasattr(sys, 'frozen'):
84                                 print("************************************************")
85                                 print("* Failed to find pypy, so slicing with python! *")
86                                 print("************************************************")
87                                 skeinforge_craft.writeOutput(fileName)
88                                 print("************************************************")
89                                 print("* Failed to find pypy, so sliced with python!  *")
90                                 print("************************************************")
91                         else:
92                                 print("******************************************************************")
93                                 print("* Failed to find pypy, we need pypy to slice with a frozen build *")
94                                 print("* Place pypy in the same directory as Cura so Cura can find it.  *")
95                                 print("******************************************************************")
96                                 sys.exit(1)
97                 else:
98                         subprocess.call(getSliceCommand(fileName))
99
100 def getExportFilename(filename, ext = "gcode"):
101         return "%s_export.%s" % (filename[: filename.rfind('.')], ext)
102
103 #Get a short filename in 8.3 format for proper saving on SD.
104 def getShortFilename(filename):
105         ext = filename[filename.rfind('.'):]
106         filename = filename[: filename.rfind('.')]
107         return filename[:8] + ext[:2]
108
109 def getSliceCommand(filename):
110         if profile.getPreference('slicer').startswith('Slic3r') and getSlic3rExe() != False:
111                 slic3rExe = getSlic3rExe()
112                 if slic3rExe == False:
113                         return False
114                 cmd = [slic3rExe,
115                         '--output-filename-format', '[input_filename_base]_export.gcode',
116                         '--nozzle-diameter', str(profile.calculateEdgeWidth()),
117                         '--print-center', '%s,%s' % (profile.getProfileSetting('machine_center_x'), profile.getProfileSetting('machine_center_y')),
118                         '--z-offset', '0',
119                         '--gcode-flavor', 'reprap',
120                         '--gcode-comments',
121                         '--filament-diameter', profile.getProfileSetting('filament_diameter'),
122                         '--extrusion-multiplier', str(1.0 / float(profile.getProfileSetting('filament_density'))),
123                         '--temperature', profile.getProfileSetting('print_temperature'),
124                         '--travel-speed', profile.getProfileSetting('travel_speed'),
125                         '--perimeter-speed', profile.getProfileSetting('print_speed'),
126                         '--small-perimeter-speed', profile.getProfileSetting('print_speed'),
127                         '--infill-speed', profile.getProfileSetting('print_speed'),
128                         '--solid-infill-speed', profile.getProfileSetting('print_speed'),
129                         '--bridge-speed', profile.getProfileSetting('print_speed'),
130                         '--bottom-layer-speed-ratio', str(float(profile.getProfileSetting('bottom_layer_speed')) / float(profile.getProfileSetting('print_speed'))),
131                         '--layer-height', profile.getProfileSetting('layer_height'),
132                         '--first-layer-height-ratio', '1.0',
133                         '--infill-every-layers', '1',
134                         '--perimeters', str(profile.calculateLineCount()),
135                         '--solid-layers', str(profile.calculateSolidLayerCount()),
136                         '--fill-density', str(float(profile.getProfileSetting('fill_density'))/100),
137                         '--fill-angle', '45',
138                         '--fill-pattern', 'rectilinear', #rectilinear line concentric hilbertcurve archimedeanchords octagramspiral
139                         '--solid-fill-pattern', 'rectilinear',
140                         '--start-gcode', profile.getAlterationFilePath('start.gcode'),
141                         '--end-gcode', profile.getAlterationFilePath('end.gcode'),
142                         '--retract-length', profile.getProfileSetting('retraction_amount'),
143                         '--retract-speed', str(int(float(profile.getProfileSetting('retraction_speed')))),
144                         '--retract-restart-extra', profile.getProfileSetting('retraction_extra'),
145                         '--retract-before-travel', profile.getProfileSetting('retraction_min_travel'),
146                         '--retract-lift', '0',
147                         '--slowdown-below-layer-time', profile.getProfileSetting('cool_min_layer_time'),
148                         '--min-print-speed', profile.getProfileSetting('cool_min_feedrate'),
149                         '--skirts', profile.getProfileSetting('skirt_line_count'),
150                         '--skirt-distance', str(int(float(profile.getProfileSetting('skirt_gap')))),
151                         '--skirt-height', '1',
152                         '--scale', profile.getProfileSetting('model_scale'),
153                         '--rotate', profile.getProfileSetting('model_rotate_base'),
154                         '--duplicate-x', profile.getProfileSetting('model_multiply_x'),
155                         '--duplicate-y', profile.getProfileSetting('model_multiply_y'),
156                         '--duplicate-distance', '10']
157                 if profile.getProfileSetting('support') != 'None':
158                         cmd.extend(['--support-material'])
159                 cmd.extend([filename])
160                 return cmd
161         else:
162                 pypyExe = getPyPyExe()
163                 if pypyExe == False:
164                         pypyExe = sys.executable
165                 
166                 #In case we have a frozen exe, then argv[0] points to the executable, but we want to give pypy a real script file.
167                 if hasattr(sys, 'frozen'):
168                         mainScriptFile = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../..", "cura_sf.zip"))
169                 else:
170                         mainScriptFile = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", os.path.split(sys.argv[0])[1]))
171                 cmd = [pypyExe, mainScriptFile, '-p', profile.getGlobalProfileString(), '-s']
172                 if platform.system() == "Windows":
173                         try:
174                                 cmd.append(str(filename))
175                         except UnicodeEncodeError:
176                                 cmd.append("#UTF8#" + filename.encode("utf-8"))
177                 else:
178                         cmd.append(filename)
179                 return cmd
180