chiark / gitweb /
Fix pyserial 3.0 compatibility issue
[cura.git] / Cura / cura.py
1 #!/usr/bin/python
2 """
3 This page is in the table of contents.
4 ==Overview==
5 ===Introduction===
6 Cura is a AGPL tool chain to generate a GCode path for 3D printing. Older versions of Cura where based on Skeinforge.
7 Versions up from 13.05 are based on a C++ engine called CuraEngine.
8 """
9 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
10
11 from optparse import OptionParser
12
13 from Cura.util import profile
14
15 def main():
16         """
17         Main Cura entry point. Parses arguments, and starts GUI or slicing process depending on the arguments.
18         """
19         parser = OptionParser(usage="usage: %prog [options] <filename>.stl")
20         parser.add_option("-i", "--ini", action="store", type="string", dest="profileini",
21                 help="Load settings from a profile ini file")
22         parser.add_option("-p", "--profile", action="store", type="string", dest="profile",
23                 help="Internal option, do not use!")
24         parser.add_option("-s", "--slice", action="store_true", dest="slice",
25                 help="Slice the given files instead of opening them in Cura")
26         parser.add_option("-o", "--output", action="store", type="string", dest="output",
27                 help="path to write sliced file to")
28         parser.add_option("--serialCommunication", action="store", type="string", dest="serialCommunication",
29                 help="Start commandline serial monitor")
30
31         (options, args) = parser.parse_args()
32
33         if options.serialCommunication:
34                 from Cura import serialCommunication
35                 port, baud = options.serialCommunication.split(':')
36                 serialCommunication.startMonitor(port, baud)
37                 return
38
39         print "load preferences from " + profile.getPreferencePath()
40         profile.loadPreferences(profile.getPreferencePath())
41
42         if options.profile is not None:
43                 profile.setProfileFromString(options.profile)
44         elif options.profileini is not None:
45                 profile.loadProfile(options.profileini)
46         else:
47                 profile.loadProfile(profile.getDefaultProfilePath(), True)
48
49         if options.slice is not None:
50                 from Cura.util import sliceEngine
51                 from Cura.util import objectScene
52                 from Cura.util import meshLoader
53                 import shutil
54
55                 def commandlineProgressCallback(progress):
56                         if progress >= 0:
57                                 #print 'Preparing: %d%%' % (progress * 100)
58                                 pass
59                 scene = objectScene.Scene()
60                 scene.updateMachineDimensions()
61                 engine = sliceEngine.Engine(commandlineProgressCallback)
62                 for m in meshLoader.loadMeshes(args[0]):
63                         scene.add(m)
64                 engine.runEngine(scene)
65                 engine.wait()
66
67                 if not options.output:
68                         options.output = args[0] + profile.getGCodeExtension()
69                 with open(options.output, "wb") as f:
70                         gcode = engine.getResult().getGCode()
71                         while True:
72                                 data = gcode.read()
73                                 if len(data) == 0:
74                                         break
75                                 f.write(data)
76                 print 'GCode file saved : %s' % options.output
77
78                 engine.cleanup()
79         else:
80                 from Cura.gui import app
81                 app.CuraApp(args).MainLoop()
82
83 if __name__ == '__main__':
84         import os
85         import sys
86
87         # On Windows, the PATH variable can cause the search path for dlls
88         # to give priority to dlls from other applications and it will cause
89         # the bundled python dlls not to be loaded.
90         # More specifically, anyone with Haskell Platform installed will not
91         # be able to launch Cura because glut32.dll from Haskell is incompatible
92         # with the bundled py-opengl and will cause Cura to crash
93         if sys.platform.startswith('win'):
94                 os.environ['PATH'] = ''
95         main()