This repository has been archived by the owner on Nov 29, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
print_gcode.py
executable file
·71 lines (59 loc) · 2.33 KB
/
print_gcode.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
#!/usr/bin/env python
from datetime import datetime
import os
import sys
lib_path = os.path.abspath(os.path.dirname(__file__) + '/../vendor/s3g')
sys.path.append(lib_path)
import makerbot_driver
import serial
import serial.tools.list_ports as lp
import optparse
parser = optparse.OptionParser()
parser.add_option("-f", "--filename", dest="filename",
help="gcode file to print", default=False)
parser.add_option("-m", "--machine", dest="machine",
help="machine type to scan for, example ReplicatorSingle", default="The Replicator")
parser.add_option("-p", "--port", dest="port",
help="The port you want to connect to (OPTIONAL)", default=None)
parser.add_option("-s", "--sequences", dest="sequences",
help="Flag to not use makerbot_driver's start/end sequences",
default=True, action="store_false")
(options, args) = parser.parse_args()
if options.port is None:
md = makerbot_driver.MachineDetector()
md.scan(options.machine)
port = md.get_first_machine()
if port is None:
print "Can't Find %s" % (options.machine)
sys.exit()
else:
port = options.port
factory = makerbot_driver.MachineFactory()
obj = factory.build_from_port(port)
assembler = makerbot_driver.GcodeAssembler(getattr(obj, 'profile'))
start, end, variables = assembler.assemble_recipe()
start_gcode = assembler.assemble_start_sequence(start)
end_gcode = assembler.assemble_end_sequence(end)
filename = os.path.basename(options.filename)
filename = os.path.splitext(filename)[0]
parser = getattr(obj, 'gcodeparser')
parser.environment.update(variables)
parser.state.values["build_name"] = filename[:15]
if options.sequences:
for line in start_gcode:
parser.execute_line(line)
print "==%s==> Starting gcode stream" % datetime.now()
with open(options.filename) as f:
lines = list(f)
num_lines = len(lines)
interval = round(num_lines * 0.01)
for n, line in enumerate(lines):
parser.execute_line(line)
percent = round(float(n) / float(num_lines) * 100.0)
if n % interval == 0:
print "==%s==> Sent %d/%d [%d%%]" % (datetime.now(), n, num_lines, percent)
sys.stdout.flush()
print "==%s==> Gcode stream finished" % datetime.now()
if options.sequences:
for line in end_gcode:
parser.execute_line(line)