diff options
author | jvasile <jvasile> | 2008-07-17 20:09:48 +0000 |
---|---|---|
committer | jvasile <jvasile@cb376a5e-1013-0410-a455-b6b1f9ac8223> | 2008-07-17 20:09:48 +0000 |
commit | 840db79e4f13b92ec39923dd571ab748abd57750 (patch) | |
tree | 97def0cd60c817d26eb441dd8d8e25e5b14b5c84 | |
parent | a68e040248d637afae2aafe5fa60251c5df950f1 (diff) | |
download | reprap-840db79e4f13b92ec39923dd571ab748abd57750.tar.gz reprap-840db79e4f13b92ec39923dd571ab748abd57750.zip |
0.2 seems to be working
git-svn-id: https://reprap.svn.sourceforge.net/svnroot/reprap@1764 cb376a5e-1013-0410-a455-b6b1f9ac8223
-rw-r--r-- | trunk/users/vasile/brlcad/brlcad.py | 123 | ||||
-rw-r--r-- | trunk/users/vasile/brlcad/primitive.py | 134 |
2 files changed, 182 insertions, 75 deletions
diff --git a/trunk/users/vasile/brlcad/brlcad.py b/trunk/users/vasile/brlcad/brlcad.py index afa0ec2a..d1fcbb12 100644 --- a/trunk/users/vasile/brlcad/brlcad.py +++ b/trunk/users/vasile/brlcad/brlcad.py @@ -122,7 +122,7 @@ class Shape(): TODO: Every shape needs a default vertex for translation and rotation purposes. ''' - self.name = build_name('shape', 's', **kwargs) + self.name = build_name('shape', '', **kwargs) if not hasattr(self, 'statements'): self.statements = [] @@ -141,7 +141,7 @@ class Shape(): print >>sys.stderr, self.name, 'contains ', type(a) sys.exit(2) - Shape.guess_vertex(self) + Shape.guess_vertex(self) ## called this way because we don't want this overridden. if not hasattr(self, 'vertex'): sys.stderr.write('Shape (%s) needs vertex\n' % (self.name)) @@ -164,6 +164,9 @@ class Shape(): print >>sys.stderr, '%s: rotate takes 1 or 2 tuples/lists.' % (self.name) sys.exit(2) + if 'combination' in kwargs: + self.combination(kwargs['combination']) + def __str__(self): '''Accessing an Object as a string will yield all the statements and the children's statements.''' @@ -171,12 +174,35 @@ class Shape(): return ''.join([str(c) for c in self.children]) + \ ''.join([str(s) for s in self.statements]) - def combination(self, command): - '''Unimplemented and/or Untested: Add a statement to the stack making this - a combination. Populate self.name with the name of that - combination.''' + def _sub_add_union(self, other, op): + if type(other)==str: + return ' '.join([self.name, op, other]) + if hasattr(other, 'isshape') and other.isshape: + return ' '.join([self.name, op, other.name]) + + print >>sys.stderr, 'Shape +/-/union a string or a Shape, but not a ', type(other) + sys.exit(2) + def __sub__(self, other): return self._sub_add_union(other, '-') + def __add__(self, other): return self._sub_add_union(other, '+') + def __mul__ (self, other): return self._sub_add_union(other, 'u') + def union (self, other): return self._sub_add_union(other, 'u') + + def _combo_region(self, command, cr): + if hasattr(self, 'hasgrc'): + print >>sys.stderr, self.name, 'already has a region/combination/group!' + sys.exit(2) + self.statements.append(Kill(self.name)) + self.statements.append(Statement(cr, (self.name, command))) + self.hasgrc = 1 + return self.name - self.statements.append(Statement('c', (command,))) + def region(self, command): + '''Add a region statement to this Shape.''' + return self._combo_region(command, 'r') + + def combination(self, command): + '''Add a combination statement to this Shape.''' + return self._combo_region(command, 'c') def group(self): '''Take all the Shapes that comprise this item, and make a group of @@ -184,6 +210,10 @@ class Shape(): If kwargs['group']==True, this will be done as part of __init__''' + if hasattr(self, 'hasgrc'): + print >>sys.stderr, self.name, 'already has a region/combination/group!' + sys.exit(2) + self.statements.append(Kill(self.name)) args = [self.name] @@ -196,6 +226,7 @@ class Shape(): sys.exit(2) self.statements.append(Statement('g', args)) + self.hasgrc = 1 return self.name def guess_vertex(self): @@ -223,7 +254,10 @@ class Shape(): '''translate is a tuple containing the amount to move along each axis. vertex is the point from which we move. All other Shapes and Shapes that make up this Shape are moved relative to the - vertex.''' + vertex. + + Not implemented yet. And maybe translate should be more like + the mged translate.''' if vertex == None: vertex = self.vertex @@ -232,74 +266,13 @@ class Shape(): if hasattr(c, 'rotate'): c.translate(translate, vertex) +############################################ +## Grab the primitive classes +for p in sys.path: + if os.path.exists(p + 'primitive.py') and p[-7:-1] == 'brlcad': + exec(file(p + 'primitive.py').read()) -class Primitive(Shape): - "Primitive shapes executed in one or more statements." - isprimitive=True - def __init__(self, shape, args=[], **kwargs): - self.name = build_name(shape, 's', **kwargs) - self.statements = [] - - self.guess_vertex(args) - - Shape.__init__(self, ( Kill(self.name), - Statement('in %s %s' % (self.name, shape),args),), - name=self.name, **kwargs) - - def guess_vertex(self, args): - ## Guess at the vertex. If this won't find the right vertex, - ## you'll have to set it manually. - i = 0 - while not hasattr(self, 'vertex') and i < len(args): - if type(args[i]) == tuple and len(args[i]) == 3: - self.vertex = args[i] - i += 1 - - def rotate(self, rotation, vertex=None): - if vertex == None: - vertex = self.vertex - - xrot, yrot, zrot = rotation - xvert, yvert, zvert = vertex - - self.statements.append('sed %s\nkeypoint %f %f %f\nrot %f %f %f\naccept\n' % ( - self.name, xvert, yvert, zvert, xrot, yrot, zrot)) - - def translate(self, xtra, ytra=None, ztra=None): - '''Translate shape to new coordinates. - - Note that this is different from MGED's internal translate - statement. MGED moves the shape to the indicated coordinates. - This function moves the shape relative to the current - coordinates. translate(1, 2, -3) moves the shape 1 to the - right, 2 up and 3 back.''' - - if type(xtra) == tuple: - xtra, ytra, ztra = xtra - - self.statements.append('sed %s\ntra %f %f %f\naccept' % ( - self.name, xtra, ytra, ztra)) - -class Cone(Primitive): - def __init__(self, vertex, height_vector, base_radius, top_radius, **kwargs): - Primitive.__init__(self, "trc", (vertex, height_vector, base_radius, top_radius), **kwargs) - -class Cylinder(Primitive): - def __init__(self, vertex, height_vector, radius, **kwargs): - Primitive.__init__(self, "rcc", (vertex, height_vector, radius), **kwargs) - -class Box(Primitive): - def __init__(self, xmin, xmax, ymin, ymax, zmin, zmax, **kwargs): - self.vertex = (xmin, ymin, zmin) - if not 'basename' in kwargs: - kwargs['basename'] = 'box' - Primitive.__init__(self, "rpp", (xmin, xmax, ymin, ymax, zmin, zmax), **kwargs) - -class Arb8(Primitive): - def __init__(self, v1, v2, v3, v4, v5, v6, v7, v8, **kwargs): - Primitive.__init__(self, "arb8", (v1, v2, v3, v4, v5, v6, v7, v8), **kwargs) - - +###################### ## Implement some simplistic commands from string import Template commands = { diff --git a/trunk/users/vasile/brlcad/primitive.py b/trunk/users/vasile/brlcad/primitive.py new file mode 100644 index 00000000..e08ffe37 --- /dev/null +++ b/trunk/users/vasile/brlcad/primitive.py @@ -0,0 +1,134 @@ +## Copyright (C) 2008 James Vasile <james@hackervisions.org>' + +## This is a freed work; 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 + +## No need to import this file. It gets included in brlcad.py with +## this snippet: +## for p in sys.path: +## if os.path.exists(p + 'primitive.py') and p[-7:-1] == 'brlcad': +## exec(file(p + 'primitive.py').read()) + +class Primitive(Shape): + "Primitive shapes executed in one or more statements." + isprimitive=True + def __init__(self, shape, args=[], **kwargs): + self.name = build_name(shape, 's', **kwargs) + self.statements = [] + + self.guess_vertex(args) + + Shape.__init__(self, ( Kill(self.name), + Statement('in %s %s' % (self.name, shape),args),), + name=self.name, **kwargs) + + def guess_vertex(self, args): + ## Guess at the vertex. If this won't find the right vertex, + ## you'll have to set it manually. + i = 0 + while not hasattr(self, 'vertex') and i < len(args): + if type(args[i]) == tuple and len(args[i]) == 3: + self.vertex = args[i] + i += 1 + + def rotate(self, rotation, vertex=None): + if vertex == None: + vertex = self.vertex + + xrot, yrot, zrot = rotation + xvert, yvert, zvert = vertex + + self.statements.append('sed %s\nkeypoint %f %f %f\nrot %f %f %f\naccept\n' % ( + self.name, xvert, yvert, zvert, xrot, yrot, zrot)) + + def translate(self, xtra, ytra=None, ztra=None): + '''Translate shape to new coordinates. + + Note that this is different from MGED's internal translate + statement. MGED moves the shape to the indicated coordinates. + This function moves the shape relative to the current + coordinates. translate(1, 2, -3) moves the shape 1 to the + right, 2 up and 3 back.''' + + if type(xtra) == tuple: + xtra, ytra, ztra = xtra + + self.statements.append('sed %s\ntra %f %f %f\naccept' % ( + self.name, xtra, ytra, ztra)) + +class Box(Primitive): + def __init__(self, xmin, xmax, ymin, ymax, zmin, zmax, **kwargs): + self.vertex = (xmin, ymin, zmin) + if not 'basename' in kwargs: + kwargs['basename'] = 'box' + Primitive.__init__(self, "rpp", (xmin, xmax, ymin, ymax, zmin, zmax), **kwargs) +class Cone(Primitive): + def __init__(self, vertex, height_vector, base_radius, top_radius, **kwargs): + Primitive.__init__(self, "trc", (vertex, height_vector, base_radius, top_radius), **kwargs) +class Cone_elliptical(Primitive): + 'Truncated elliptical cone' + def __init__(self): + pass +class Cone_general(Primitive): + def __init__(self): + pass +class Cylinder_elliptical(Primitive): + 'This is a right elliptical cylinder' + def __init__(self): + pass +class Cylinder_hyperbolic(Primitive): + def __init__(self): + pass +class Cylinder_parabolic(Primitive): + def __init__(self): + pass +class Ellipsoid_foci(Primitive): + def __init__(self, focus_1, focus_2, chord_length, **kwargs): + 'chord_length must be > distance between foci' + Primitive.__init__(self, "ellg", (focus_1, focus_2, chord_length), **kwargs) +class Hyperboloid(Primitive): + def __init__(self): + pass +class Paraboloid(Primitive): + def __init__(self): + pass +class Particle(Primitive): + def __init__(self): + pass +class Sphere(Primitive): + def __init__(self): + pass +class Torus(Primitive): + def __init__(self): + pass +class Torus_elliptical(Primitive): + def __init__(self): + pass + +## Implement some primitive shapes +from string import Template +primitives = { +'Arb4':['arb4', 'v1, v2, v3, v4'], +'Arb5':['arb5', 'v1, v2, v3, v4, v5'], +'Arb6':['arb6', 'v1, v2, v3, v4, v5, v'], +'Arb7':['arb7', 'v1, v2, v3, v4, v5, v6, v7'], +'Arb8':['arb8', 'v1, v2, v3, v4, v5, v6, v7, v8'], +'Ellipsoid':['ell', 'vertex, avector, bvector, cvector'], +'Ellipsoid_radius':['ell1', 'vertex, radius'], +'Cylinder':['rcc', 'vertex, height_vector, radius'] +} +for p in primitives: + exec Template('''class $classname(Primitive): + def __init__(self, $args, **kwargs): + Primitive.__init__(self, "$mged", ($args), **kwargs)'''). \ + substitute(classname = p, mged = primitives[p][0], args = primitives[p][1]) |