summaryrefslogtreecommitdiff
path: root/trunk/users/vasile/stl2gcode/stl2gcode
blob: fec91efa93b3bff46c15fff03fd6b86d72716cc5 (plain)
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
#!/usr/bin/python

## stl2gcode generates gcode from stereolithographic triangles.  This
## gcode can be fed to CNC fab machines to create physical 3D models.
## stl2gcode also generates intermediate file formats (i.e. pov and
## png files).

## Copyright (C) 2008 James Vasile <james@hackervisions.org>.  This 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 any later
## version.

## This 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 should have received a copy of the
## GNU General Public License along with the work; if not, write to
## the Free Software Foundation, Inc., 51 Franklin Street, 5th Floor,
## Boston, MA 02110-1301 USA
##
## Not tested on Windows or Mac boxes.
## 
## Needs PIL, the Python Imaging Library (aptitude install
## python-imaging)
##
## stl2gcode includes some code from image-to-gcode.py.  That code is
## Copyright (C) 2005 Chris Radek (chris@timeguy.com)

## TODO: Handle file globs for -f

import sys
import getopt
import stl2gcode

## Defaults
step = 0.002 # this is the x and y step length for the gcode conversion
depth = 0.008 # I think this is color depth
x, y = 0, 0 # starting offset for x and y for gcode

#tolerance = 0.000000001
inside_vector = [-0.5, 0.68, 0.5] # TODO: calculate this
inside_vector = [0, 0, 0]
target = 'gcode'  # target format should be one of inc, pov, png, gcode

filename=''

step_x = step_y = step_z = 1 # crude resolution, but we need a default

VERSION = 0.1

def help():
    print '''\nBy specifying a file with [inc|pov|png] extension, you can skip
initial steps.  And by using the --target option, you can end the
processing at whatever step you need.  If intermediary files are what
you're looking for, there's no need to keep processing all the way to
gcode.

Example: 'stl2gcode -f object.inc -t png' generates the pov and png
files but not the inc or gcode files.'''

def usage():
    print '''Slices 3D stl models into layers and generates gcode for each layer.

stl2gcode [dstxy] --file
    -d --depth d\t\t\tcolor depth (not implemented)
    -f --file f\t\t\t\tfilename (required)
    -h --help\t\t\thelp
    -s --step s\t\t\t\tx/y step size of your fab machine (not implemented)
    -t --target [inc|pov|png|gcode]\ttarget format
    -x --x x\t\t\t\tinitial x value (not implemented)
    -y --y y\t\t\t\tinitial y value (not implemented)'''

def do_args(argv):
    if len(argv) == 0: usage(); sys.exit()
    try:
        opts, args = getopt.getopt(argv, "hd:f:s:t:x:y:",
                                   ["help", "file=", "depth=", "step=", "target=",
                                    "x=", "y="])
    except getopt.GetoptError, err:
        print str(err); usage(); sys.exit(2)
    global filename
    for o, a in opts:
        if o in ("-h", "--help"): usage(); help(); sys.exit()
        elif o in ("-d", "--depth"): global depth; depth = float(a)
        elif o in ("-f", "--filename"): global filename; filename = a; 
        elif o in ("-s", "--step"): global step; step = float(a);
        elif o in ("-t", "--target"): global target; target = a;
        elif o in ("-x", "--x"): global x; x = float(a)
        elif o in ("-y", "--y"): global y; y = float(a)
        else: assert False, "unhandled option"
    if filename == "":
        print "Must specify filename.  Use -f option."
        usage()
        sys.exit(2)
    if target not in (['inc', 'pov', 'png', 'gcode']):
        print "Target must be inc|pov|png|gcode."
        usage()
        sys.exit(2)

###############################################################################

if __name__ == "__main__":
    do_args(sys.argv[1:])
    convert_object = stl2gcode.stl2gcode({
            'depth': depth, 'filename': filename, 
            'target': target, 'step_x': step_x, 'step_y': step_y, 'step_z': step_z,
            'x': x, 'y': y})
    convert_object.convert()