forked from robotsinthesun/monkeyprint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonkeyprint.py
executable file
·213 lines (167 loc) · 6.97 KB
/
monkeyprint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/python
# -*- coding: latin-1 -*-
#
# Copyright (c) 2015-2016 Paul Bomke
# Distributed under the GNU GPL v2.
#
# This file is part of monkeyprint.
#
# monkeyprint is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# monkeyprint is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You have received a copy of the GNU General Public License
# along with monkeyprint. If not, see <http://www.gnu.org/licenses/>.
import sys, os, getopt # Needed to parse command line arguments.
import time
import monkeyprintModelHandling
import monkeyprintSettings
import monkeyprintGui
import monkeyprintGuiHelper
def main(argv):
if __name__ == "__main__":
# Test if this file is executed as the main application contrary to being executed as a model from inside another file.
# Check command line arguments to see if this is a standalone or gui instance.
# -h: print usage instructions.
# -f: specify mkp file to open on startup.
# -p, --print: run print process without gui. You have to pass the file name of an mkp file to print.
try:
opts, args = getopt.getopt(argv,"hdsf:p:",["server", "file=", "print="])
except getopt.GetoptError:
usage()
sys.exit(2)
# Act according to commandline options.
if len(opts) != 0:
# Check if debug option was given.
debugOption = False
for opt, arg in opts:
if (opt=="-d"):
debugOption = True
# If debug flag was the only option...
if len(opts) == 1 and debugOption:
# ...start with debug flag.
runGui(debug=debugOption)
# If there are more options...
else:
# ...evaluate them.
for opt, arg in opts:
if (opt=="-h"):
# Display help.
usage()
sys.exit(2)
elif (opt in ("-f", "--file")):
# Run Gui with project file.
runGui(filename=arg, debug=debugOption)
elif (opt in ("-p", "--print")):
# Run non gui with project file and start print.
runNoGui(filename=arg, debug=debugOption)
elif (opt in ("-s", "--server")):
# Start server that listens for commands via socket.
runServerNoGui(debug=debugOption)
# If no options present, just run with GUI.
else:
runGui()
def runGui(filename=None, debug=False):
# Create a debug console text buffer.
console = monkeyprintGuiHelper.consoleText()
# console = None
# Create settings dictionary object for machine and program settings.
programSettings = monkeyprintSettings.programSettings(console)
# Create version message.
print "Starting Monkeyprint " + str(programSettings['versionMajor'].getValue()) + "." + str(programSettings['versionMinor'].getValue()) + "." + str(programSettings['revision'].getValue()) + " with GUI."
console.addLine("You are using Monkeyprint " + str(programSettings['versionMajor'].getValue()) + "." + str(programSettings['versionMinor'].getValue()) + "." + str(programSettings['revision'].getValue()))
# Get current working directory and set paths.
# Is this still relevant?
cwd = os.getcwd()
programSettings['localMkpPath'].setValue(cwd + "/currentPrint.mkp")
# Update settings from file.
programSettings.readFile(cwd)
# Get cwd for exe
programSettings['installDir'].setValue(getInstallDir())
print "Running from " + programSettings['installDir'].getValue()
# Set debug mode if specified.
if debug:
print "Debug mode active."
programSettings['debug'].setValue(True)
else:
programSettings['debug'].setValue(False)
# Create model collection object.
# This object contains model data and settings data for each model.
# Pass program settings.
modelCollection = monkeyprintModelHandling.modelCollection(programSettings, console)
# Create splash screen for given interval.
# Get version string first.
versionString = "Monkeyprint version " + str(programSettings['versionMajor'].getValue()) + "." + str(programSettings['versionMinor'].getValue()) + "." + str(programSettings['revision'].getValue())
# Create gui.
gui = monkeyprintGui.gui(modelCollection, programSettings, console, filename)
# Start the gui main loop.
gui.main()
def runNoGui(filename=None, debug=False):
print "Starting without Gui."
# Create settings dictionary object for machine and program settings.
programSettings = monkeyprintSettings.programSettings()
# Update settings from file.
programSettings.readFile()
# Set debug mode if specified.
if debug==True:
programSettings['debug'].value = debug
print "Debug mode active."
else:
programSettings['debug'].value = False
# Create model collection object.
# This object contains model data and settings data for each model.
# Pass program settings.
modelCollection = monkeyprintModelHandling.modelCollection(programSettings)
#TODO disable this...
modelCollection.jobSettings['exposureTime'].value = 0.1
print ("Exposure time: " + str(modelCollection.jobSettings['exposureTime'].value) + ".")
# Load project file.
# TODO: test if file is mkp.
modelCollection.loadProject(filename)
print ("Project file: " + str(filename) + " loaded successfully.")
print "Found the following models:"
for model in modelCollection:
if model != 'default':
print (" " + model)
# Start the slicer.
modelCollection.updateSliceStack()
print ("Starting slicer.")
# Wait for all slicer threads to finish.
while(modelCollection.slicerRunning()):
modelCollection.checkSlicerThreads()
time.sleep(.2)
sys.stdout.write('.')
sys.stdout.flush()
# Start print process when slicers are done.
print "\nSlicer done. Starting print process."
# Create the projector window.
gui = monkeyprintGui.noGui(programSettings, modelCollection)
print "Print process done. Thank you for using Monkeyprint."
def runServerNoGui(port="5553", debug=False):
printerServer = monkeyprintPiServer.monkeyprintPiServer(port, debug)
def usage():
print "\nCommand line option not recognized.\n"
print "Usage: monkeyprint.py <options>\n"
print "<no option>: Start GUI."
print "-h: Show this help text."
print "-f or --file <filename.mkp>: Start GUI and load project file."
print "-p or --print <filename.mkp>: Start without GUI and run a print job."
print "-s or --server Start a monkeyprint server that prints incoming files."
print "-d: Run in debug mode without stepper motion"
print " and shutter servo. This will overwrite"
print " the debug option in the settings menu."
# https://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile
def getInstallDir():
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except AttributeError:
base_path = os.path.abspath(".")
return base_path
main(sys.argv[1:])