chiark / gitweb /
Merge pull request #3 from daid/SteamEngine
[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("-r", "--print", action="store", type="string", dest="printfile",
23                 help="Open the printing interface, instead of the normal cura interface.")
24         parser.add_option("-p", "--profile", action="store", type="string", dest="profile",
25                 help="Internal option, do not use!")
26         parser.add_option("-s", "--slice", action="store_true", dest="slice",
27                 help="Slice the given files instead of opening them in Cura")
28         parser.add_option("-o", "--output", action="store", type="string", dest="output",
29                 help="path to write sliced file to")
30         parser.add_option("--serialCommunication", action="store", type="string", dest="serialCommunication",
31                 help="Start commandline serial monitor")
32
33         (options, args) = parser.parse_args()
34
35         if options.serialCommunication:
36                 from Cura import serialCommunication
37                 port, baud = options.serialCommunication.split(':')
38                 serialCommunication.startMonitor(port, baud)
39                 return
40
41         print "load preferences from " + profile.getPreferencePath()
42         profile.loadPreferences(profile.getPreferencePath())
43
44         if options.profile is not None:
45                 profile.setProfileFromString(options.profile)
46         elif options.profileini is not None:
47                 profile.loadProfile(options.profileini)
48         else:
49                 profile.loadProfile(profile.getDefaultProfilePath(), True)
50
51         if options.printfile is not None:
52                 from Cura.gui import printWindow
53                 printWindow.startPrintInterface(options.printfile)
54         elif options.slice is not None:
55                 from Cura.util import sliceEngine
56                 from Cura.util import objectScene
57                 from Cura.util import meshLoader
58                 import shutil
59
60                 def commandlineProgressCallback(progress):
61                         if progress >= 0:
62                                 #print 'Preparing: %d%%' % (progress * 100)
63                                 pass
64                 scene = objectScene.Scene()
65                 scene.updateMachineDimensions()
66                 engine = sliceEngine.Engine(commandlineProgressCallback)
67                 for m in meshLoader.loadMeshes(args[0]):
68                         scene.add(m)
69                 engine.runEngine(scene)
70                 engine.wait()
71
72                 if not options.output:
73                         options.output = args[0] + profile.getGCodeExtension()
74                 with open(options.output, "wb") as f:
75                         gcode = engine.getResult().getGCode()
76                         while True:
77                                 data = gcode.read()
78                                 if len(data) == 0:
79                                         break
80                                 f.write(data)
81                 print 'GCode file saved : %s' % options.output
82
83                 engine.cleanup()
84         else:
85                 from Cura.gui import app
86                 app.CuraApp(args).MainLoop()
87
88 if __name__ == '__main__':
89         main()