Skip to content

Commit

Permalink
Changed format decorators interface:
Browse files Browse the repository at this point in the history
1. Removed the 'Decorator' sufix from class names:
   FillTypeDecorator -> FillType
   PenDecorator      -> Pen
2. Arguments to formatters are now parameter values, not HPGL commands:
   FillTypeDecorator(FT(x))   -> FillType(x)

Interface stays the same:
   ft = FillType(x)
   ft(shape)

 _______________________
< More cleanup pending. >
 -----------------------
  \
   \
       __     
      UooU\.'@@@@@@`.
      \__/(@@@@@@@@@@)
           (@@@@@@@@)
           `YY~~~~YY'
            ||    ||
  • Loading branch information
victoradan committed Jun 5, 2011
1 parent 6f1b1a9 commit ffb8dbe
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 80 deletions.
2 changes: 1 addition & 1 deletion chiplotle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from chiplotle.hpgl.commands import *
from chiplotle.hpgl.pen import Pen
from chiplotle.hpgl.decorators import *
#from chiplotle.hpgl.decorators import *

#from chiplotle.hpgl.compound import *
#from chiplotle.hpgl.compound.decorators import *
Expand Down
40 changes: 0 additions & 40 deletions chiplotle/core/interfaces/decoratable.py

This file was deleted.

5 changes: 1 addition & 4 deletions chiplotle/core/interfaces/formatdecorator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from chiplotle.geometry.core.shape import _Shape

class FormatDecorator(object):
'''FormatDecorators know how to wrap Decoratable objects with additional
'''FormatDecorators know how to wrap Shape objects with additional
formatting stuff.
- `sticky` = False reverts the formatting to a previous state or
some default.
'''

@property
Expand Down
12 changes: 4 additions & 8 deletions chiplotle/geometry/shapes/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from chiplotle.geometry.core.group import Group
from chiplotle.geometry.core.label import Label
from chiplotle.geometry.core.coordinate import Coordinate
from chiplotle.hpgl.pen import Pen
from chiplotle.hpgl.decorators import PenDecorator

## TODO should this be a decorator carried around by the shape?

Expand All @@ -31,11 +29,10 @@ def annotation(shape):

class _Annotation(object):

def __init__(self, shape, charwidth = 0.05, charheight = 0.1, pen = 6):
def __init__(self, shape, charwidth = 0.05, charheight = 0.1):
self.shape = shape
self.charwidth = charwidth
self.charheight = charheight
self.pen = pen


def _annotate_structure(self):
Expand All @@ -54,7 +51,6 @@ def _annotate_properties(self):

fields = '\n\r'.join([cr, cd, mn, mx, ws, hs, ])
label = Label(fields, self.charwidth, self.charheight)
PenDecorator(Pen(self.pen))(label)
offset(label, self.shape.bottom_left)
return label

Expand All @@ -67,7 +63,6 @@ def _annotate_center(self):
c = circle(20)
cr = cross(50, 50)
mark = Group([c, cr, label])
PenDecorator(Pen(self.pen))(mark)

offset(label, coord)
offset(c, coord)
Expand All @@ -84,7 +79,6 @@ def _annotate_centroid(self):
r = rectangle(20, 20)
cr = cross(50, 50)
mark = Group([r, cr, label])
PenDecorator(Pen(self.pen))(mark)

offset(label, coord)
offset(r, coord)
Expand All @@ -106,15 +100,17 @@ def annotation(self):
## demo
if __name__ == '__main__':
from chiplotle import *
from chiplotle.hpgl.decorators import Pen
from random import randint
coords = [(randint(0, 4000), randint(0, 4000)) for i in range(20)]
p = bezier_path(coords, 1)
r = rectangle(1000, 400)
offset(r, (-2000, 1000))
g1 = Group([r, p])
an = annotation(g1)
Pen(2)(an)

g2 = Group([g1, an])
PenDecorator(Pen(1))(g2)
Pen(1)(g2)

io.view(g2)
6 changes: 3 additions & 3 deletions chiplotle/hpgl/decorators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .pen import PenDecorator
from .linetype import LineTypeDecorator
from .filltype import FillTypeDecorator
from .pen import Pen
from .linetype import LineType
from .filltype import FillType
14 changes: 9 additions & 5 deletions chiplotle/hpgl/decorators/filltype.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from chiplotle.hpgl.commands import FT
from chiplotle.core.interfaces.formatdecorator import FormatDecorator

class FillTypeDecorator(FormatDecorator):

class FillType(FormatDecorator):

def __init__(self, filltype):
__doc__ = FT.__doc__

def __init__(self, filltype=None, space=None, angle=None):
FormatDecorator.__init__(self)
if not isinstance(filltype, FT):
raise TypeError
self.filltype = filltype
self.space = space
self.angle = angle


@property
def _subcommands(self):
return [self.filltype]
return [FT(self.filltype, self.space, self.angle)]
17 changes: 9 additions & 8 deletions chiplotle/hpgl/decorators/linetype.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from chiplotle.hpgl.commands import LT
from chiplotle.core.interfaces.formatdecorator import FormatDecorator

class LineTypeDecorator(FormatDecorator):
class LineType(FormatDecorator):

def __init__(self, linetype):
__doc__ = LT.__doc__

def __init__(self, linetype=None, length=4):
FormatDecorator.__init__(self)
if not isinstance(linetype, LT):
raise TypeError
self.linetype = linetype
self.linetype = linetype
self.length = length

@property
def _subcommands(self):
return [self.linetype]
return [LT(self.linetype, self.length)]



Expand All @@ -23,10 +24,10 @@ def _subcommands(self):

r1 = rectangle(1000, 200)
r2 = rectangle(300, 800)
ltd = LineTypeDecorator(LT(2, .5))
ltd = LineType(2, .5)
ltd(r1)
g = Group([r1, r2])
ltd = LineTypeDecorator(LT(3, .1))
ltd = LineType(3, .1)
ltd(g)
print g.format
io.view(g)
32 changes: 21 additions & 11 deletions chiplotle/hpgl/decorators/pen.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
from chiplotle.hpgl.pen import Pen
from chiplotle.hpgl.pen import Pen as HPGLPen
from chiplotle.core.interfaces.formatdecorator import FormatDecorator

class PenDecorator(FormatDecorator):
'''The PenDecorator wraps HPGL Pen properties around a given Shape.
class Pen(FormatDecorator):
'''The Pen wraps HPGL Pen properties around a given Shape.
- `pen` is the pen number to use.
- `sticky` boolean; set to False to set plotter back to default values
at the end of the decorated shape. Set to True to skip reset.
'''

def __init__(self, pen):
if not isinstance(pen, Pen):
raise TypeError
def __init__(self,
number,
velocity = None,
force = None,
acceleration= None,
thickness = None):
FormatDecorator.__init__(self)
self.pen = pen
self.number = number
self.velocity = velocity
self.force = force
self.acceleration = acceleration
self.thickness = thickness

@property
def _subcommands(self):
return self.pen._subcommands
#return [self.pen]
p = HPGLPen(self.number,
self.velocity,
self.force,
self.acceleration,
self.thickness)
return [p]



## DEMO
if __name__ == '__main__':
from chiplotle.geometry.shapes.rectangle import rectangle

p = Pen(2, 3, 4, 5, 0.1)
pd = PenDecorator(p)
pd = Pen(2, 3, 4, 5, 0.1)
r = rectangle(100, 20)
pd(r)
print r.format

0 comments on commit ffb8dbe

Please sign in to comment.