#
# Copyright (c) 2002, 2003, 2004, 2005 Art Haas
#
# This file is part of PythonCAD.
#
# PythonCAD 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 2 of the License, or
# (at your option) any later version.
#
# PythonCAD 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 PythonCAD; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# tool stuff
#

import math
import types
import array

from PythonCAD.Generic import util
from PythonCAD.Generic.point import Point
from PythonCAD.Generic.segment import Segment
from PythonCAD.Generic.circle import Circle
from PythonCAD.Generic.arc import Arc
from PythonCAD.Generic.leader import Leader
from PythonCAD.Generic.polyline import Polyline
from PythonCAD.Generic.hcline import HCLine
from PythonCAD.Generic.vcline import VCLine
from PythonCAD.Generic.acline import ACLine
from PythonCAD.Generic.cline import CLine
from PythonCAD.Generic.ccircle import CCircle
from PythonCAD.Generic.text import TextStyle, TextBlock
from PythonCAD.Generic import dimension
from PythonCAD.Generic.layer import Layer
from PythonCAD.Generic import tangent

class Tool(object):
    """A generic tool object.

This class is meant to be a base class for tools. A Tool
instance the following attributes:

list: A list the tool can use to store objects
handlers: A dictionary used to store functions

A Tool object has the following methods:

{set/get/del/has}Handler(): Store/retrive/delete/test a handler for an event.
clearHandlers(): Unset all the handlers in the tool.
reset(): Restore the tool to a a default state.
initialize(): Retore the tool to its original state.
{set/get}Location(): Store/retrieve an image-based coordinate pair.
{set/get}CurrentPoint(): Store/retrieve a screen-based coordinate pair.
clearCurrentPoint(): Set the screen-based coordinate to None.
create(): Instantiate the object the tool is designed to create.
    """
    def __init__(self):
        """Instantiate a Tool.

t = Tool()
        """
        super(Tool, self).__init__()
        self.__objlist = []
        self.__objtype = None
        self.__handlers = {}
        self.__location = None
        self.__curpoint = None
        self.__points = []
        self.__xpts = array.array('d')
        self.__ypts = array.array('d')
        self.__shift = None

    def __len__(self):
        """Return the number of objects in the list via len().
        """
        return len(self.__objlist)

    def __iter__(self):
        """Make the Tool iterable.
        """
        return iter(self.__objlist)

    def getList(self):
        """Return the Tool's object list.

getList()
        """
        return self.__objlist

    list = property(getList, None, None, "Tool object list.")

    def reset(self):
        """Restore the Tool to its initial state.

reset()

This function purges the Tool's object list and handler dictionary.
        """
        del self.__objlist[:]
        self.__handlers.clear()
        self.__location = None
        self.__curpoint = None
        del self.__points[:]
        del self.__xpts[:]
        del self.__ypts[:]
        self.__shift = None

    def initialize(self):
        self.reset()

    def setHandler(self, key, func):
        """Set a handler for the Tool.

setHandler(key, func)

There are two arguments for this function:

key: A string used to identify a particular action
func: A function object

There are no restrictions on what the function 'func' does,
the argument count, etc. Any call to setHandler() with
a key that is already stored replaces the old 'func' argument
with the new one. The function argument may be None, and
the key argument must be a string.
        """
        if key is None:
            raise ValueError, "Key value cannot be None."
        if not isinstance(key, str):
            raise TypeError, "Invalid key: " + `key`
        if func and not isinstance(func, types.FunctionType):
            raise TypeError, "Invalid function: " + `func`
        self.__handlers[key] = func

    def getHandler(self, key):
        """Return the function for a particular key.

getHandler(key)

Given argument 'key', the function associated with it is
returned. A KeyError is raised if the argument 'key' had
not be used to store a function.
        """
        if not isinstance(key, str):
            raise TypeError, "Invalid argument to getHandler(): " + `key`
        if not self.__handlers.has_key(key):
            raise KeyError, "Invalid key '%s'" % key
        return self.__handlers[key]

    def delHandler(self, key):
        """Delete the handler associated with a particular key

delHandler(key)

The argument 'key' should be a string.
        """
        if self.__handlers.has_key(key):
            del self.__handlers[key]

    def hasHandler(self, key):
        """Check if there is a handler stored for a given key.

hasHandler(key)

The 'key' argument must be a string. The function returns 1
if there is a handler for that key, 0 otherwise.
        """
        _k = key
        if not isinstance(_k, str):
            raise TypeError, "Invalid argument to hasHandler(): " + `key`
        return self.__handlers.has_key(_k)

    def clearHandlers(self):
        """Unset all the handlers for the tool.

clearHandlers()

This function does not alter the Tool's object list.
        """
        self.__handlers.clear()

    def pushObject(self, obj):
        """Add an object to the Tool's object list.

pushObject(obj)
        """
        self.__objlist.append(obj)

    def popObject(self):
        """Remove the last object on the Tool's object list.

popObject()

If the object list is empty, this function returns None.
        """
        _obj = None
        if len(self.__objlist):
            _obj = self.__objlist.pop()
        return _obj

    def delObjects(self):
        """Remove all objects from the Tool's object list.

delObjects()

This function does not alter the Tool's handlers.
        """
        del self.__objlist[:]

    def getObject(self, idx):
        """Access an object in the tool.

getObject(idx)

The argument 'idx' is the index into the list of
stored objects.
        """
        return self.__objlist[idx]

    def setLocation(self, x, y):
        """Store an x/y location in the tool.

setLocation(x,y)

Store an x-y coordinate in the tool. Both arguments
should be floats
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__location = (_x,_y)

    def getLocation(self):
        """Return the stored location in the tool

getLocation()
        """
        return self.__location

    def clearLocation(self):
        """Reset the location to an empty value.

clearLocation()
        """
        self.__location = None

    def setCurrentPoint(self, x, y):
        """Set the tool's current point.

setCurrentPoint(x,y)

Store an x-y coordinate in the tool. Both arguments
should be int.
        """
        _x = x
        if not isinstance(_x, int):
            _x = int(x)
        _y = y
        if not isinstance(_y, int):
            _y = int(y)
        self.__curpoint = (_x, _y)

    def getCurrentPoint(self):
        """Return the tool's current point value.

getCurrentPoint()
        """
        return self.__curpoint

    def clearCurrentPoint(self):
        """Reset the current point to an empty value

clearCurrentPoint()
        """
        self.__curpoint = None

    def create(self, image):
        """Create an object the tool is designed to construct.

create(image)

The argument 'image' is an image in which the newly created object
will be added. In the Tool class, this method does nothing. It is
meant to be overriden in classed using the Tool class as a base
class.
        """
        pass # override

class PointTool(Tool):
    """A specialized tool for drawing Point objects.

The PointTool class is derived from the Tool class, so it
shares the methods and attributes of that class. The PointTool
class has the following additional methods:

{get/set}Point(): Get/Set a x/y coordinate in the tool.
    """
    def __init__(self):
        super(PointTool, self).__init__()
        self.__point = None

    def setPoint(self, x, y):
        """Store an x/y coordinate in the tool

setPoint(x, y)

Arguments 'x' and 'y' should be floats.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__point = (_x, _y)

    def getPoint(self):
        """Get the stored x/y coordinates from the tool.

getPoint()

This method returns a tuple containing the values passed in
with the setPoint() method, or None if that method has not
been invoked.
        """
        return self.__point

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends Tool::reset().
        """
        super(PointTool, self).reset()
        self.__point = None

    def create(self, image):
        """Create a new Point and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        if self.__point is not None:
            _active_layer = image.getActiveLayer()
            _x, _y = self.__point
            _p = _active_layer.find('point', _x, _y)
            if _p is None:
                _p = Point(_x, _y)
                _active_layer.addObject(_p)
            self.reset()

class SegmentTool(Tool):
    """A Specialized tool for drawing Segment objects.

The SegmentTool class is derived from the Tool class, so
it shares the attributes and methods of that class. The
SegmentTool class has the following additional methods:

{get/set}FirstPoint(): Get/Set the first point of the Segment.
{get/set}SecondPoint(): Get/Set the second point of the Segment.
    """
    def __init__(self):
        super(SegmentTool, self).__init__()
        self.__first_point = None
        self.__second_point = None

    def setFirstPoint(self, x, y):
        """Store the first point of the Segment.

setFirstPoint(x, y)

Arguments 'x' and 'y' should be floats.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__first_point = (_x, _y)

    def getFirstPoint(self):
        """Get the first point of the Segment.

getFirstPoint()

This method returns a tuple holding the coordinates stored
by invoking the setFirstPoint() method, or None if that method
has not been invoked.
        """
        return self.__first_point

    def setSecondPoint(self, x, y):
        """Store the second point of the Segment.

setSecondPoint(x, y)

Arguments 'x' and 'y' should be floats. If the
tool has not had the first point set with setFirstPoint(),
a ValueError exception is raised.
        """
        if self.__first_point is None:
            raise ValueError, "SegmentTool first point is not set."
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__second_point = (_x, _y)

    def getSecondPoint(self):
        """Get the second point of the Segment.

getSecondPoint()

This method returns a tuple holding the coordinates stored
by invoking the setSecondPoint() method, or None if that method
has not been invoked.
        """
        return self.__second_point

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends Tool::reset().
        """
        super(SegmentTool, self).reset()
        self.__first_point = None
        self.__second_point = None

    def create(self, image):
        """Create a new Segment and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        if (self.__first_point is not None and
            self.__second_point is not None):
            _active_layer = image.getActiveLayer()
            _x1, _y1 = self.__first_point
            _x2, _y2 = self.__second_point
            if _active_layer.find('segment', _x1, _y1, _x2, _y2) is None:
                _p1 = _active_layer.find('point', _x1, _y1)
                if _p1 is None:
                    _p1 = Point(_x1, _y1)
                    _active_layer.addObject(_p1)
                _p2 = _active_layer.find('point', _x2, _y2)
                if _p2 is None:
                    _p2 = Point(_x2, _y2)
                    _active_layer.addObject(_p2)
                _s = image.getOption('LINE_STYLE')
                _l = image.getOption('LINE_TYPE')
                _c = image.getOption('LINE_COLOR')
                _t = image.getOption('LINE_THICKNESS')
                _seg = Segment(_p1, _p2, _s, _l, _c, _t)
                _active_layer.addObject(_seg)
            self.reset()

class RectangleTool(SegmentTool):
    """A Specialized tool for drawing rectangles.

The RectangleTool is derived from the SegmentTool, so it
shares all the methods and attributes of that class. A
RectangleTool creates four Segments in the shape of
a rectangle in the image.
    """
    def __init__(self):
        super(RectangleTool, self).__init__()

    def create(self, image):
        """Create Segments and add them to the image.

create(image)

This method overrides the SegmentTool::create() method.
        """
        _p1 = self.getFirstPoint()
        _p2 = self.getSecondPoint()
        if _p1 is not None and _p2 is not None:
            _x1, _y1 = _p1
            _x2, _y2 = _p2
            _active_layer = image.getActiveLayer()
            _p1 = _active_layer.find('point', _x1, _y1)
            if _p1 is None:
                _p1 = Point(_x1, _y1)
                _active_layer.addObject(_p1)
            _p2 = _active_layer.find('point', _x1, _y2)
            if _p2 is None:
                _p2 = Point(_x1, _y2)
                _active_layer.addObject(_p2)
            _p3 = _active_layer.find('point', _x2, _y2)
            if _p3 is None:
                _p3 = Point(_x2, _y2)
                _active_layer.addObject(_p3)
            _p4 = _active_layer.find('point', _x2, _y1)
            if _p4 is None:
                _p4 = Point(_x2, _y1)
                _active_layer.addObject(_p4)
            _s = image.getOption('LINE_STYLE')
            _l = image.getOption('LINE_TYPE')
            _c = image.getOption('LINE_COLOR')
            _t = image.getOption('LINE_THICKNESS')
            _segment = Segment
            _s1 = _active_layer.find('segment', _p1.x, _p1.y, _p2.x, _p2.y)
            _s2 = _active_layer.find('segment', _p2.x, _p2.y, _p3.x, _p3.y)
            _s3 = _active_layer.find('segment', _p3.x, _p3.y, _p4.x, _p4.y)
            _s4 = _active_layer.find('segment', _p4.x, _p4.y, _p1.x, _p1.y)
            if _s1 is None:
                _seg = _segment(_p1, _p2, _s, _l, _c, _t)
                _active_layer.addObject(_seg)
            if _s2 is None:
                _seg = _segment(_p2, _p3, _s, _l, _c, _t)
                _active_layer.addObject(_seg)
            if _s3 is None:
                _seg = _segment(_p3, _p4, _s, _l, _c, _t)
                _active_layer.addObject(_seg)
            if _s4 is None:
                _seg = _segment(_p4, _p1, _s, _l, _c, _t)
                _active_layer.addObject(_seg)
            self.reset()

class CircleTool(Tool):
    """A Specialized tool for drawing Circle objects.

The CircleTool is derived from the Tool class, so it shares
all the methods and attributes of that class. The CircleTool
class has the following addtional methods:

{set/get}Center(): Set/Get the center point location of the circle.
{set/get}Radius(): Set/Get the radius of the circle.
    """
    def __init__(self):
        super(CircleTool, self).__init__()
        self.__center = None
        self.__radius = None

    def setCenter(self, x, y):
        """Set the center point location of the circle.

setCenter(x, y)

The arguments 'x' and 'y' give the location for the center
of the circle.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__center = (_x, _y)

    def getCenter(self):
        """Get the center point location of the circle.

getCenter()

This method returns the coordinates stored with the setCenter()
method, or None if that method has not been called.
        """
        return self.__center

    def setRadius(self, radius):
        """Set the radius of the circle.

setRadius(radius)

The argument "radius" must be a float value greater than 0.0
        """
        _r = radius
        if not isinstance(_r, float):
            _r = float(radius)
        if not _r > 0.0:
            raise ValueError, "Invalid radius: %g" % _r
        self.__radius = _r

    def getRadius(self):
        """Get the radius of the circle.

getRadius()

This method returns the value specified from the setRadius()
call, or None if that method has not been invoked.
        """
        return self.__radius

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends Tool::reset().
        """
        super(CircleTool, self).reset()
        self.__center = None
        self.__radius = None

    def create(self, image):
        """Create a new Circle and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        if (self.__center is not None and
            self.__radius is not None):
            _active_layer = image.getActiveLayer()
            _x, _y = self.__center
            _r = self.__radius
            if _active_layer.find('circle', _x, _y, _r) is None:
                _cp = _active_layer.find('point', _x, _y)
                if _cp is None:
                    _cp = Point(_x, _y)
                    _active_layer.addObject(_cp)
                _s = image.getOption('LINE_STYLE')
                _l = image.getOption('LINE_TYPE')
                _c = image.getOption('LINE_COLOR')
                _t = image.getOption('LINE_THICKNESS')
                _circle = Circle(_cp, _r, _s, _l, _c, _t)
                _active_layer.addObject(_circle)
            self.reset()

class TwoPointCircleTool(CircleTool):
    """A specialized class for drawing Circles between two points.

The TwoPointCircleTool class is derived from the CircleTool
class, so it shares all the methods and attributes of that
class. The TwoPointCircleTool class has the following addtional
methods:

{set/get}FirstPoint(): Set/Get the first point used to define the circle.
{set/get}SecondPoint(): Set/Get the second point used to define the circle.
    """
    def __init__(self):
        super(TwoPointCircleTool, self).__init__()
        self.__first_point = None
        self.__second_point = None

    def setFirstPoint(self, x, y):
        """Set the first point used to define the location of the circle.

setFirstPoint(x, y)

Arguments 'x' and 'y' give the location of a point.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__first_point = (_x, _y)

    def getFirstPoint(self):
        """Get the first point used to define the location of the circle.

getFirstPoint()

This method returns a tuple holding the values used when the
setFirstPoint() method was called, or None if that method has
not yet been used.
        """
        return self.__first_point

    def setSecondPoint(self, x, y):
        """Set the second point used to define the location of the circle.

setSecondPoint(x, y)

Arguments 'x' and 'y' give the location of a point. Invoking
this method before the setFirstPoint() method will raise a
ValueError.
        """
        if self.__first_point is None:
            raise ValueError, "First point is not set"
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        _x1, _y1 = self.__first_point
        _xc = (_x + _x1)/2.0
        _yc = (_y + _y1)/2.0
        _radius = math.hypot((_x - _x1), (_y - _y1))/2.0
        self.setCenter(_xc, _yc)
        self.setRadius(_radius)
        self.__second_point = (_x, _y)

    def getSecondPoint(self):
        """Get the second point used to define the location of the circle.

getSecondPoint()

This method returns a tuple holding the values used when the
setSecondPoint() method was called, or None if that method has
not yet been used.
        """
        return self.__second_point

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends CircleTool::reset().
        """
        super(TwoPointCircleTool, self).reset()
        self.__first_point = None
        self.__second_point = None

class ArcTool(CircleTool):
    """A specialized tool for drawing Arc objects.

The ArcTool is Derived from the CircleTool class, so it shares
all the attributes and methods of that class. The ArcTool class
has the following addtional methods:

{set/get}StartAngle(): Set/Get the start angle of the arc
{set/get}EndAngle(): Set/Get the end angle of the arc.
    """
    def __init__(self):
        super(ArcTool, self).__init__()
        self.__start_angle = None
        self.__end_angle = None

    def setStartAngle(self, angle):
        """Set the start angle of the arc.

setStartAngle(angle)

The argument 'angle' should be a float value between 0.0 and 360.0
        """
        _angle = util.make_c_angle(angle)
        self.__start_angle = _angle

    def getStartAngle(self):
        """Return the start angle of the arc.

getStartAngle()

This method returns the value defined in the previous setStartAngle()
call, or None if that method has not been called.
        """
        return self.__start_angle

    def setEndAngle(self, angle):
        """Set the start angle of the arc.

setStartAngle(angle)

The argument 'angle' should be a float value between 0.0 and 360.0
        """
        _angle = util.make_c_angle(angle)
        self.__end_angle = _angle

    def getEndAngle(self):
        """Return the end angle of the arc.

getEndAngle()

This method returns the value defined in the previous setEndAngle()
call, or None if that method has not been called.
        """
        return self.__end_angle

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends CircleTool::reset().
        """
        super(ArcTool, self).reset()
        self.__start_angle = None
        self.__end_angle = None

    def create(self, image):
        """Create a new Arc and add it to the image.

create(image)

This method overrides the CircleTool::create() method.
        """
        _center = self.getCenter()
        _radius = self.getRadius()
        _sa = self.__start_angle
        _ea = self.__end_angle
        if (_center is not None and
            _radius is not None and
            _sa is not None and
            _ea is not None):
            _active_layer = image.getActiveLayer()
            _x, _y = _center
            if _active_layer.find('arc', _x, _y, _radius, _sa, _ea) is None:
                _cp = _active_layer.find('point', _x, _y)
                if _cp is None:
                    _cp = Point(_x, _y)
                    _active_layer.addObject(_cp)
                _s = image.getOption('LINE_STYLE')
                _l = image.getOption('LINE_TYPE')
                _c = image.getOption('LINE_COLOR')
                _t = image.getOption('LINE_THICKNESS')
                _arc = Arc(_cp, _radius, _sa, _ea, _s, _l, _c, _t)
                for _ep in _arc.getEndpoints():
                    _ex, _ey = _ep
                    _lp = _active_layer.find('point', _ex, _ey)
                    if _lp is None:
                        _lp = Point(_ex, _ey)
                        _active_layer.addObject(_lp)
                _active_layer.addObject(_arc)
            self.reset()

class LeaderTool(Tool):
    """A specialized tool for drawing Leader objects.

The LeaderTool class is derived from the Tool class, so it
shares the methods and attributes of that class. The LeaderTool
class has the following addtional methods:

{set/get}FirstPoint(): Set/Get the first point of the Leader.
{set/get}MidPoint(): Set/Get the second point of the Leader.
{set/get}FinalPoint(): Set/Get the final point of the Leader.
    """
    def __init__(self):
        super(LeaderTool, self).__init__()
        self.__start_point = None
        self.__mid_point = None
        self.__end_point = None

    def setFirstPoint(self, x, y):
        """Set the first point used to define the Leader.

setFirstPoint(x, y)

Arguments 'x' and 'y' give the location of a point.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__start_point = (_x, _y)

    def getFirstPoint(self):
        """Get the first point used to define the Leader.

getFirstPoint()

This method returns a tuple holding the values used when the
setFirstPoint() method was called, or None if that method has
not yet been used.
        """
        return self.__start_point

    def setMidPoint(self, x, y):
        """Set the second point used to define the Leader.

setMidPoint(x, y)

Arguments 'x' and 'y' give the location of a point. If the
first point has not been set this method raises a ValueError.
        """
        if self.__start_point is None:
            raise ValueError, "First point not set in LeaderTool."
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__mid_point = (_x, _y)

    def getMidPoint(self):
        """Get the second point used to define the Leader.

getMidPoint()

This method returns a tuple holding the values used when the
setMidPoint() method was called, or None if that method has
not yet been used.
        """
        return self.__mid_point

    def setFinalPoint(self, x, y):
        """Set the first point used to final point of the Leader.

setFinalPoint(x, y)

Arguments 'x' and 'y' give the location of a point. This method
raises an error if the first point or second point have not been
set.
        """
        if self.__start_point is None:
            raise ValueError, "First point not set in LeaderTool."
        if self.__mid_point is None:
            raise ValueError, "Second point not set in LeaderTool."
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y

        if not isinstance(_y, float):
            _y = float(y)
        self.__end_point = (_x, _y)

    def getFinalPoint(self):
        """Get the third point used to define the Leader.

getFinalPoint()

This method returns a tuple holding the values used when the
setFinalPoint() method was called, or None if that method has
not yet been used.
        """
        return self.__end_point

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends Tool::reset().
        """
        super(LeaderTool, self).reset()
        self.__start_point = None
        self.__mid_point = None
        self.__end_point = None

    def create(self, image):
        """Create a new Leader and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        if (self.__start_point is not None and
            self.__mid_point is not None and
            self.__end_point is not None):
            _active_layer = image.getActiveLayer()
            _x1, _y1 = self.__start_point
            _x2, _y2 = self.__mid_point
            _x3, _y3 = self.__end_point
            if _active_layer.find('leader', _x1, _y1, _x2, _y2, _x3, _y3) is None:
                _p1 = _active_layer.find('point', _x1, _y1)
                if _p1 is None:
                    _p1 = Point(_x1, _y1)
                    _active_layer.addObject(_p1)
                _p2 = _active_layer.find('point', _x2, _y2)
                if _p2 is None:
                    _p2 = Point(_x2, _y2)
                    _active_layer.addObject(_p2)
                _p3 = _active_layer.find('point', _x3, _y3)
                if _p3 is None:
                    _p3 = Point(_x3, _y3)
                    _active_layer.addObject(_p3)
                _size = image.getOption('LEADER_ARROW_SIZE')
                _s = image.getOption('LINE_STYLE')
                _l = image.getOption('LINE_TYPE')
                _c = image.getOption('LINE_COLOR')
                _t = image.getOption('LINE_THICKNESS')
                _leader = Leader(_p1, _p2, _p3, _size, _s, _l, _c, _t)
                _active_layer.addObject(_leader)
            self.reset()

class PolylineTool(Tool):
    """A specialized tool for drawing Polyline objects.

The PolylineTool class is derived from the Tool class, so it
shares all the attributes and methods of that class. The PolylineTool
class has the following addtional methods:

storePoint(): Store a point used to define the Polyline.
getPoint(): Retrieve a point used to define the Polyline.
getLastPoint(): Retrieve the last point used to define the Polyline.
getPoints(): Get the list of points that define the Polyline.
    """
    def __init__(self):
        super(PolylineTool, self).__init__()
        self.__points = []

    def __len__(self):
        return len(self.__points)

    def storePoint(self, x, y):
        """Store a point that will define a Polyline.

storePoint(x, y)

The arguments 'x' and 'y' should be float values. There is
no limit as to how long a Polyline should be, so each invocation
of this method appends the values to the list of stored points.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__points.append((_x, _y))

    def getPoint(self, i):
        """Retrieve a point used to define a Polyline.

getPoint(i)

Argument "i" represents the index in the list of points that
defines the polyline. Negative indicies will get points from
last-to-first. Using an invalid index will raise an error.

This method returns a tuple holding the x/y coordinates.
        """
        return self.__points[i]

    def getPoints(self):
        """Get all the points that define the Polyline.

getPoints()

This method returns a list of tuples holding the x/y coordinates
of all the points that define the Polyline.
        """
        return self.__points[:]

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends Tool::reset().
        """
        super(PolylineTool, self).reset()
        del self.__points[:]

    def create(self, image):
        """Create a new Polyline and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        if len(self.__points):
            _pts = []
            _active_layer = image.getActiveLayer()
            if _active_layer.find('polyline', self.__points) is None:
                for _pt in self.__points:
                    _x, _y = _pt
                    _p = _active_layer.find('point', _x, _y)
                    if _p is None:
                        _p = Point(_x, _y)
                        _active_layer.addObject(_p)
                    _pts.append(_p)
                _s = image.getOption('LINE_STYLE')
                _l = image.getOption('LINE_TYPE')
                _c = image.getOption('LINE_COLOR')
                _t = image.getOption('LINE_THICKNESS')
                _pline = Polyline(_pts, _s, _l, _c, _t)
                _active_layer.addObject(_pline)
            self.reset()

class PolygonTool(Tool):
    """A specialized to for creating Polygons from Segments.

The PolygonTool will create an uniformly sized polygon from Segment
entities. The minimum number of sides is three, creating an equilateral
triangle. There is no maximum number of sides, though realistically any
polygon with more than 20 or so sides is unlikely to be drawn. As
the PolygonTool is derived from the Tool class, it shares all the attributes
and method of that class. The PolygonTool has the following additional
methods:

{get/set}SideCount(): Get/Set the number of sides in the polygon.
{get/set}External() Get/Set if the polygon is drawn inside or outside a circle.
{get/set}Center(): Get/Set the center location of the polygon.
getCoords(): Get the coordinates of the polygon corners.
    """
    def __init__(self):
        super(PolygonTool, self).__init__()
        self.__nsides = None
        self.__increment = None
        self.__external = False
        self.__center = None
        self.__xpts = array.array("d")
        self.__ypts = array.array("d")

    def setSideCount(self, count):
        """Set the number of sides of the polygon to create.

setSideCount(count)

Argument "count" should be an integer value greater than 2.
        """
        _count = count
        if not isinstance(_count, int):
            _count = int(count)
        if _count < 3:
            raise ValueError, "Invalid count: %d" % _count
        self.__nsides = _count
        self.__increment = (360.0/float(_count)) * (math.pi/180.0)
        for _i in range(_count):
            self.__xpts.insert(_i, 0.0)
            self.__ypts.insert(_i, 0.0)

    def getSideCount(self):
        """Get the number of sides of the polygon to be created.

getSideCount()

A ValueError exception is raised if the side count has not been
set with setSideCount()
        """
        if self.__nsides is None:
            raise ValueError, "No side count defined."
        return self.__nsides

    def setExternal(self):
        """Create the polygon on the outside of a reference circle.

setExternal()

By default the polygon is drawing completely contained within a
circle. Invoking this method will created the polygon so that all
sides are outside the circle.
        """
        self.__external = True

    def getExternal(self):
        """Test if the polygon will be created outside a circle.

getExternal()

If the setExternal() method has been called, this method will
return True. By default this method will return False.
        """
        return self.__external

    def setCenter(self, x, y):
        """Define the center of the polygon.

setCenter(x, y)

Arguments 'x' and 'y' should be float values.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__center = (_x, _y)

    def getCenter(self):
        """Retrieve the center of the polygon to be created.

getCenter()

This method returns a tuple holding two float values containing
the 'x' and 'y' coordinates of the polygon center. A ValueError
is raised if the center has not been set with a prior call to setCenter().
        """
        if self.__center is None:
            raise ValueError, "Center is undefined."
        return self.__center

    def getCoord(self, i):
        """Get one of the coordinates of the polygon corners.

getCoord(i)

Argument "i" should be an integer value such that:

0 <= i <= number of polygon sides
        """
        _x = self.__xpts[i]
        _y = self.__ypts[i]
        return _x, _y

    def setLocation(self, x, y):
        """Set the tool location.

setLocation(x, y)

This method extends Tool::setLocation() and calculates the polygon
points.
        """
        super(PolygonTool, self).setLocation(x, y)
        _x, _y = self.getLocation()
        _count = self.__nsides
        _inc = self.__increment
        if self.__external:
            _offset = _inc/2.0
        else:
            _offset = 0.0
        _cx, _cy = self.__center
        _xsep = _x - _cx
        _ysep = _y - _cy
        _angle = math.atan2(_ysep, _xsep) + _offset
        _rad = math.hypot(_xsep, _ysep)/math.cos(_offset)
        _xp = self.__xpts
        _yp = self.__ypts
        for _i in range(_count):
            _xp[_i] = _cx + (_rad * math.cos(_angle))
            _yp[_i] = _cy + (_rad * math.sin(_angle))
            _angle = _angle + _inc

    def create(self, image):
        """Create a Polygon from Segments and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        if len(self.__xpts):
            _active_layer = image.getActiveLayer()
            _s = image.getOption('LINE_STYLE')
            _l = image.getOption('LINE_TYPE')
            _c = image.getOption('LINE_COLOR')
            _t = image.getOption('LINE_THICKNESS')
            _count = self.__nsides
            _xp = self.__xpts
            _yp = self.__ypts
            _x = _xp[0]
            _y = _yp[0]
            #
            # find starting point ...
            #
            _p0 = _active_layer.find('point', _x, _y)
            if _p0 is None:
                _p0 = Point(_x, _y)
                _active_layer.addObject(_p0)
            #
            # make segments for all the points ...
            #
            _p1 = _p0
            for _i in range(1, _count):
                _x = _xp[_i]
                _y = _yp[_i]
                _pi = _active_layer.find('point', _x, _y)
                if _pi is None:
                    _pi = Point(_x, _y)
                    _active_layer.addObject(_pi)
                _seg = Segment(_p1, _pi, _s, _l, _c, _t)
                _active_layer.addObject(_seg)
                _p1 = _pi
            #
            # now add closing segment ...
            #
            _seg = Segment(_p1, _p0, _s, _l, _c, _t)
            _active_layer.addObject(_seg)
            self.reset()

    def reset(self):
        """Restore the PolygonTool to its original state.

reset()

This method extends Tool::reset()
        """
        super(PolygonTool, self).reset()
        # self.__nsides = None
        # self.__increment = None
        # self.__external = False # make this adjustable?
        self.__center = None
        for _i in range(self.__nsides):
            self.__xpts[_i] = 0.0
            self.__ypts[_i] = 0.0

class HCLineTool(PointTool):
    """A specialized tool for drawing HCLine objects.

The HCLineTool class is derived from the PointTool class
so it shares all the attributes and methods of that class.

There are no additional methods for this class.
    """
    def __init__(self):
        super(HCLineTool, self).__init__()

    def create(self, image):
        """Create a new HCLine and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        _p = self.getPoint()
        if _p is not None:
            _active_layer = image.getActiveLayer()
            _x, _y = _p
            if _active_layer.find('hcline', _y) is None:
                _pt = _active_layer.find('point', _x, _y)
                if _pt is None:
                    _pt = Point(_x, _y)
                    _active_layer.addObject(_pt)
                _hcl = HCLine(_pt)
                _active_layer.addObject(_hcl)
            self.reset()

class VCLineTool(PointTool):
    """A specialized tool for drawing VCLine objects.

The VCLineTool class is derived from the PointTool class
so it shares all the attributes and methods of that class.

There are no additional methods for this class.
    """
    def __init__(self):
        super(VCLineTool, self).__init__()

    def create(self, image):
        """Create a new VCLine and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        _p = self.getPoint()
        if _p is not None:
            _active_layer = image.getActiveLayer()
            _x, _y = _p
            if _active_layer.find('vcline', _x) is None:
                _pt = _active_layer.find('point', _x, _y)
                if _pt is None:
                    _pt = Point(_x, _y)
                    _active_layer.addObject(_pt)
                _vcl = VCLine(_pt)
                _active_layer.addObject(_vcl)
            self.reset()

class ACLineTool(PointTool):
    """A specialized tool for drawing ACLine objects.

The ACLineTool class is derived from the PointTool class
so it shares all the attributes and methods of that class.
The ACLineTool class has the following addtional methods:

{set/get}Angle(): Set/Get the angle of the ACLine.
    """
    def __init__(self):
        super(ACLineTool, self).__init__()
        self.__angle = None

    def setLocation(self, x, y):
        """Set the location of the Tool.

setLocation(x, y)

This method extends the Tool::setLocation() method.
        """
        super(ACLineTool, self).setLocation(x, y)
        _loc = self.getLocation()
        if _loc is None:
            return
        _x, _y = _loc
        _x1, _y1 = self.getPoint()
        if abs(_y - _y1) < 1e-10: # horizontal
            self.__angle = 0.0
        elif abs(_x - _x1) < 1e-10: # vertical
            self.__angle = 90.0
        else:
            _slope = 180.0/math.pi * math.atan2((_y - _y1), (_x - _x1))
            self.__angle = util.make_angle(_slope)

    def setAngle(self, angle):
        """Set the angle for the ACLine.

setAngle(angle)

The argument 'angle' should be a float where -90.0 < angle < 90.0
        """
        _angle = util.make_angle(angle)
        self.__angle = _angle

    def getAngle(self):
        """Get the angle for the ACLine.

getAngle()

This method returns a float.
        """
        return self.__angle

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends PointTool::reset().
        """
        super(ACLineTool, self).reset()
        self.__angle = None

    def create(self, image):
        """Create a new ACLine and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        _p = self.getPoint()
        if (_p is not None and
            self.__angle is not None):
            _active_layer = image.getActiveLayer()
            _x, _y = _p
            if _active_layer.find('acline', _x, _y, self.__angle) is None:
                _pt = _active_layer.find('point', _x, _y)
                if _pt is None:
                    _pt = Point(_x, _y)
                    _active_layer.addObject(_pt)
                _acl = ACLine(_pt, self.__angle)
                _active_layer.addObject(_acl)
            self.reset()

class CLineTool(SegmentTool):
    """A specialized tool for drawing CLine objects.

The CLineTool class is derived from the SegmentTool class,
so it shares all the attributes and methods of that class.

There are no extra methods for the CLineTool class.
    """
    def __init__(self):
        super(CLineTool, self).__init__()

    def create(self, image):
        """Create a new CLine and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        _p1 = self.getFirstPoint()
        _p2 = self.getSecondPoint()
        if _p1 is not None and _p2 is not None:
            _active_layer = image.getActiveLayer()
            _x1, _y1 = _p1
            _x2, _y2 = _p2
            if _active_layer.find('cline', _x1, _y1, _x2, _y2) is None:
                _p1 = _active_layer.find('point', _x1, _y1)
                if _p1 is None:
                    _p1 = Point(_x1, _y1)
                    _active_layer.addObject(_p1)
                _p2 = _active_layer.find('point', _x2, _y2)
                if _p2 is None:
                    _p2 = Point(_x2, _y2)
                    _active_layer.addObject(_p2)
                _cline = CLine(_p1, _p2)
                _active_layer.addObject(_cline)
            self.reset()

class CCircleTool(CircleTool):
    """A specialized tool for drawing CCircle objects.

The CCircleTool class is derived from the CircleTool class,
so it shares all the attributes and methods of that class.

There are no additional methods for the CCircleTool class.
    """
    def __init__(self):
        super(CCircleTool, self).__init__()

    def create(self, image):
        """Create a new CCircle and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        _active_layer = image.getActiveLayer()
        _x, _y = self.getCenter()
        _radius = self.getRadius()
        if _active_layer.find('ccircle', _x, _y, _radius) is None:
            _cp = _active_layer.find('point', _x, _y)
            if _cp is None:
                _cp = Point(_x, _y)
                _active_layer.addObject(_cp)
            _ccircle = CCircle(_cp, _radius)
            _active_layer.addObject(_ccircle)
        self.reset()

class TwoPointCCircleTool(TwoPointCircleTool):
    """A specialized tool for drawing CCircle objects between two points.

The TwoPointCCircleTool class is derived from the TwoPointCircleTool
class, so it shares all the attributes and methods of that class.
There are no additional methods for the TwoPointCCircleTool class.
    """
    def __init__(self):
        super(TwoPointCCircleTool, self).__init__()

    def create(self, image):
        """Create a new CCircle and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        _center = self.getCenter()
        _radius = self.getRadius()
        if _center is not None and _radius is not None:
            _active_layer = image.getActiveLayer()
            _x, _y = _center
            if _active_layer.find('ccircle', _x, _y, _radius) is None:
                _cp = _active_layer.find('point', _x, _y)
                if _cp is None:
                    _cp = Point(_x, _y)
                    _active_layer.addObject(_cp)
                _ccircle = CCircle(_cp, _radius)
                _active_layer.addObject(_ccircle)
            self.reset()

class ParallelOffsetTool(Tool):
    """A specialized tool for creating parallel construction lines.

The ParallelOffsetTool will create a construction line parallel
to another construction line a fixed distance from the original
construction line. The type of the new construction line will match
that of the original.

The ParallelOffsetTool is derived from the Tool class, so it shares
all the attributes and methods of that class. The ParallelOffsetTool
has the following addtional methods:

{set/get}Offset(): Set/Get the distance between the construction lines.
{set/get}ConstructionLine(): Set/Get the original construction line
{set/get}ReferencePoint(): Set/Get the point to define where the new
                           construction line will go.
    """

    def __init__(self):
        super(ParallelOffsetTool, self).__init__()
        self.__refpt = None
        self.__offset = None
        self.__conline = None

    def setOffset(self, offset):
        """Store the displacement in the tool.

setOffset(offset)

Argument 'offset' must be a float.
        """
        _offset = offset
        if not isinstance(_offset, float):
            _offset = float(offset)
        self.__offset = _offset

    def getOffset(self):
        """Return the stored offset from the tool.

getOffset()

This method will raise a ValueError exception if the offset has
not been set with setOffset()
        """
        _offset = self.__offset
        if _offset is None:
            raise ValueError, "Offset is not defined."
        return _offset

    def setConstructionLine(self, conline):
        """Store the reference construction line in the tool.

setConstructionLine(conline)

Argument 'conline' must be a VCLine, HCLine, ACLine, or CLine object.
        """
        if not isinstance(conline, (HCLine, VCLine, ACLine, CLine)):
            raise TypeError, "Invalid construction line: " + `conline`
        self.__conline = conline

    def getConstructionLine(self):
        """Retrieve the stored construction line from the tool.

getConstructionLine()

A ValueError exception is raised if the construction line has not been
set with the setConstructionLine() method.
        """
        _conline = self.__conline
        if _conline is None:
            raise ValueError, "Construction line is not defined."
        return _conline

    def setReferencePoint(self, x, y):
        """Store the reference point for positioning the new construction line.

setReferencePoint(x, y)

Arguments 'x' and 'y' give the coordinates of a reference point
used to determine where the new construction line will be placed.
Both arguments should be floats.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__refpt = (_x, _y)

    def getReferencePoint(self):
        """Retreive the reference point from the tool.

getReferencePoint()

This method returns a tuple containing the values stored from
the setReferencePoint() call. This method will raise a ValueError
exception if the reference point has not been set.
        """
        _refpt = self.__refpt
        if _refpt is None:
            raise ValueError, "No reference point defined."
        return _refpt

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends Tool::reset().
        """
        super(ParallelOffsetTool, self).reset()
        self.__refpt = None
        self.__offset = None
        self.__conline = None

    def create(self, image):
        """Create a parallel construction line in an image.

create(image)

This method overrides the Tool::create() method.
        """
        _offset = self.__offset
        _conline = self.__conline
        _refpt = self.__refpt
        if (_offset is not None and
            _conline is not None and
            _refpt is not None):
            _active_layer = image.getActiveLayer()
            _rx, _ry = _refpt
            _lp1 = _lp2 = _ncl = None
            _addp1 = _addp2 = False
            if isinstance(_conline, HCLine):
                _x, _y = _conline.getLocation().getCoords()
                if _ry > _y:
                    _yn = _y + _offset
                else:
                    _yn = _y - _offset
                if _active_layer.find('hcline', _yn) is None:
                    _lp1 = _active_layer.find('point', _x, _yn)
                    if _lp1 is None:
                        _lp1 = Point(_x, _yn)
                        _addp1 = True
                    _ncl = HCLine(_lp1)
            elif isinstance(_conline, VCLine):
                _x, _y = _conline.getLocation().getCoords()
                if _rx > _x:
                    _xn = _x + _offset
                else:
                    _xn = _x - _offset
                if _active_layer.find('vcline', _xn) is None:
                    _lp1 = _active_layer.find('point', _xn, _y)
                    if _lp1 is None:
                        _lp1 = Point(_xn, _y)
                        _addp1 = True
                    _ncl = VCLine(_lp1)
            elif isinstance(_conline, ACLine):
                _x, _y = _conline.getLocation().getCoords()
                _angle = _conline.getAngle()
                if abs(_angle) < 1e-10: # horizontal
                    _dx = 0.0
                    _dy = _offset
                elif abs(abs(_angle) - 90.0) < 1e-10: # vertical
                    _dx = _offset
                    _dy = 0.0
                else:
                    _slope = math.tan(_angle * (math.pi/180.0))
                    _yint = _y - (_slope * _x)
                    #
                    # p1 => (_x, _y)
                    # p2 => (0, _yint)
                    #
                    # redefine angle from p1 to p2 ...
                    _angle = math.atan2((_yint - _y), -_x)
                    if _angle < 0.0:
                        _angle = _angle + (2.0 * math.pi)
                    _sqlen = math.hypot(_x, (_y - _yint))
                    _sn = ((_y - _ry) * (0.0 - _x)) - ((_x - _rx) * (_yint - _y))
                    _s = _sn/_sqlen
                    if _s < 0.0:
                        _perp = _angle + (math.pi/2.0)
                    else:
                        _perp = _angle - (math.pi/2.0)
                    _dx = _offset * math.cos(_perp)
                    _dy = _offset * math.sin(_perp)
                    _angle = _conline.getAngle() # reset variable
                _xn = _x + _dx
                _yn = _y + _dy
                if _active_layer.find('acline', _xn, _yn, _angle) is None:
                    _lp1 = _active_layer.find('point', _xn, _yn)
                    if _lp1 is None:
                        _lp1 = Point(_xn, _yn)
                        _addp1 = True
                    _ncl = ACLine(_lp1, _angle)
            elif isinstance(_conline, CLine):
                _p1, _p2 = _conline.getKeypoints()
                _x1, _y1 = _p1.getCoords()
                _x2, _y2 = _p2.getCoords()
                if abs(_x2 - _x1) < 1e-10: # vertical
                    _dx = _offset
                    _dy = 0.0
                elif abs(_y2 - _y1) < 1e-10: # horizontal
                    _dx = 0.0
                    _dy = _offset
                else:
                    _angle = math.atan2((_y2 - _y1), (_x2 - _x1))
                    if _angle < 0.0:
                        _angle = _angle + (2.0 * math.pi)
                    _sqlen = math.hypot((_x2 - _x1), (_y2 - _y1))
                    _sn = ((_y1 - _ry) * (_x2 - _x1)) - ((_x1 - _rx) * (_y2 - _y1))
                    _s = _sn/_sqlen
                    if _s < 0.0:
                        _perp = _angle + (math.pi/2.0)
                    else:
                        _perp = _angle - (math.pi/2.0)
                    _dx = math.cos(_perp) * _offset
                    _dy = math.sin(_perp) * _offset
                _x1n = _x1 + _dx
                _y1n = _y1 + _dy
                _x2n = _x2 + _dx
                _y2n = _y2 + _dy
                if _active_layer.find('cline', _x1n, _y1n, _x2n, _y2n) is None:
                    _lp1 = _active_layer.find('point', _x1n, _y1n)
                    if _lp1 is None:
                        _lp1 = Point(_x1n, _y1n)
                        _addp1 = True
                    _lp2 = _active_layer.find('point', _x2n, _y2n)
                    if _lp2 is None:
                        _lp2 = Point(_x2n, _y2n)
                        _addp2 = True
                    _ncl = CLine(_lp1, _lp2)
            else:
                raise TypeError, "Unknown construction line: " + `_conline`
            if _ncl is not None:
                if _lp1 is not None and _addp1:
                    _active_layer.addObject(_lp1)
                if _lp2 is not None and _addp2:
                    _active_layer.addObject(_lp2)
                _active_layer.addObject(_ncl)
            self.reset()

class TangentCircleTool(Tool):
    """A specialized class for creating tangent construction circles.

This class is meant to be a base class for tools that create tangent
construction circles. It is derived from the tool class so it shares
all the attributes and methods of that class. This class has the
following additional methods:

{set/get}Center(): Set/Get the center of the tangent circle.
{set/get}Radius(): Set/Get the radius of the tangent circle.
{set/get}PixelRect(): Set/Get the screen rectangle for drawing the circle.
    """
    def __init__(self):
        super(TangentCircleTool, self).__init__()
        self.__center = None
        self.__radius = None
        self.__rect = None

    def setCenter(self, x, y):
        """Store the tangent circle center point in the tool.

setCenter(x, y)

Arguments 'x' and 'y' should be floats.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(_y)
        self.__center = (_x, _y)

    def getCenter(self):
        """Return the center of the tangent circle.

getCenter()

This method returns a tuple holding two floats, the first
is the 'x' coordinate of the center, the second is the 'y'
coordinate. If the tool has not yet been invoked with a
setLocation() call, this method returns None.
        """
        return self.__center

    def setRadius(self, radius):
        """Store the radius in the tool.

setRadius(radius)

Argument "radius" should be a float.
        """
        _radius = radius
        if not isinstance(_radius, float):
            _radius = float(radius)
        self.__radius = radius

    def getRadius(self):
        """Return the center of the tangent circle.

getRadius()

This method returns a float giving the radius of the tangent
circle, or None if the radius is not set.
        """
        return self.__radius

    def setPixelRect(self, xmin, ymin, width, height):
        """Store the screen coordinates used to draw the circle.

setPixelRect(xmin, ymin, width, height)

All the arguments should be integer values.
        """
        _xmin = xmin
        if not isinstance(_xmin, int):
            _xmin = int(xmin)
        _ymin = ymin
        if not isinstance(_ymin, int):
            _ymin = int(ymin)
        _width = width
        if not isinstance(_width, int):
            _width = int(_width)
        _height = height
        if not isinstance(_height, int):
            _height = int(height)
        self.__rect = (_xmin, _ymin, _width, _height)

    def getPixelRect(self):
        """Return the screen boundary of the circle to draw

getPixelRect(self)

This method will return a tuple holding four integer values:

xmin, ymin, width, height

If the rectangle has not been set by calling setPixelRect(), then
this method will return None.
        """
        return self.__rect

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends Tool::reset().
        """
        super(TangentCircleTool, self).reset()
        self.__center = None
        self.__radius = None
        self.__rect = None

    def create(self, image):
        """Create a new CCircle and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        _active_layer = image.getActiveLayer()
        _x, _y = self.__center
        _radius = self.__radius
        if _active_layer.find('ccircle', _x, _y, _radius) is None:
            _cp = _active_layer.find('point', _x, _y)
            if _cp is None:
                _cp = Point(_x, _y)
                _active_layer.addObject(_cp)
            _ccircle = CCircle(_cp, _radius)
            _active_layer.addObject(_ccircle)
        self.reset()

class TangentCCircleTool(TangentCircleTool):
    """A specialized tool for creating tangent construction circles.

The TangentCCircleTool will create a construction circle tangent
to a construction line or a construction circle.

The TangentCCircleTool is derived from the TangentCircleTool class, so
it shares all the attributes and methods of that class. The TangentCCircleTool
has the following addtional methods:

{set/get}ConstructionObject(): Set/Get the reference construction object.
    """
    def __init__(self):
        super(TangentCCircleTool, self).__init__()
        self.__conobj = None

    def setConstructionLine(self, conobj):
        """Store the reference construction object in the tool.

setConstructionLine(conobj)

Argument 'conobj' must be a VCLine, HCLine, ACLine, CLine,
or CCircle object.
        """
        if not isinstance(conobj, (HCLine, VCLine, ACLine, CLine, CCircle)):
            raise TypeError, "Invalid construction line: " + `conobj`
        self.__conobj = conobj

    def getConstructionLine(self):
        """Retrieve the stored construction line from the tool.

getConstructionLine()

A ValueError exception is raised if the construction line has not been
set with the setConstructionLine() method.
        """
        _conobj = self.__conobj
        if _conobj is None:
            raise ValueError, "Construction object is not defined."
        return _conobj

    def setLocation(self, x, y):
        """Store an x/y coordinate pair in the tool.

setLocation(x, y)

Arguments 'x' and 'y' should be floats. This method extends
the TangentCircleTool::setLocation() methods.
        """
        super(TangentCCircleTool, self).setLocation(x, y)
        _tx, _ty = self.getLocation()
        _conobj = self.__conobj
        _cx = _cy = _radius = None
        if isinstance(_conobj, HCLine):
            _x, _y = _conobj.getLocation().getCoords()
            _cx = _tx
            _cy = (_ty + _y)/2.0
            _radius = abs(_ty - _y)/2.0
        elif isinstance(_conobj, VCLine):
            _x, _y = _conobj.getLocation().getCoords()
            _cx = (_tx + _x)/2.0
            _cy = _ty
            _radius = abs(_tx - _x)/2.0
        elif isinstance(_conobj, (ACLine, CLine)):
            _px, _py = _conobj.getProjection(_tx, _ty)
            _cx = (_tx + _px)/2.0
            _cy = (_ty + _py)/2.0
            _radius = math.hypot((_tx - _px), (_ty - _py))/2.0
        elif isinstance(_conobj, CCircle):
            _ccx, _ccy = _conobj.getCenter().getCoords()
            _rad = _conobj.getRadius()
            _sep = math.hypot((_tx - _ccx), (_ty - _ccy))
            if _sep < 1e-10:
                return
            _angle = math.atan2((_ty - _ccy), (_tx - _ccx))
            _px = _ccx + (_rad * math.cos(_angle))
            _py = _ccy + (_rad * math.sin(_angle))
            _cx = (_tx + _px)/2.0
            _cy = (_ty + _py)/2.0
            _radius = math.hypot((_tx - _px), (_ty - _py))/2.0
        else:
            raise TypeError, "Unknown construction object: " + `_conobj`
        self.setCenter(_cx, _cy)
        self.setRadius(_radius)

class TwoPointTangentCCircleTool(TangentCircleTool):
    """A specialized tool for creating tangent construction circles.

The TwoPointTangentCCircleTool will create a construction circle tangent
to two construction lines or a construction line and a construction
circle if such a tangent circle can be created.

The TwoPointTangentCCircleTool is derived from the TangentCircleTool
class, so it shares all the attributes and methods of that class. This
class also has the following addtional methods:

{set/get}FirstConObject(): Set/Get the first construction object.
{set/get}SecondConObject(): Set/Get the second constuction object.
    """
    def __init__(self):
        super(TwoPointTangentCCircleTool, self).__init__()
        self.__cobj1 = None
        self.__cobj2 = None

    def setFirstConObject(self, conobj):
        """Store the first reference construction object in the tool.

setFirstConObject(conobj)

Argument 'conobj' must be a VCLine, HCLine, ACLine, CLine, or CCircle object.
        """
        if not isinstance(conobj, (HCLine, VCLine, ACLine, CLine, CCircle)):
            raise TypeError, "Invalid construction line: " + `conobj`
        self.__cobj1 = conobj

    def getFirstConObject(self):
        """Retreive the first construction object from the tool.

getFirstConObject()
        """
        return self.__cobj1

    def setSecondConObject(self, conobj):
        """Store the second reference construction object in the tool.

setSecondConObject(conobj)

Argument 'conobj' must be a VCLine, HCLine, ACLine, or a CLine object.
Drawing a tangent circle against two CCircle objects is not yet supported.
A ValueError exception will be raised if this method is called before the
first construction object has been set.
        """
        if self.__cobj1 is None:
            raise ValueError, "First construction object not set."
        if not isinstance(conobj, (HCLine, VCLine, ACLine, CLine)):
            raise TypeError, "Invalid construction line: " + `conobj`
        self.__cobj2 = conobj

    def getSecondConObject(self):
        """Retreive the second construction object from the tool.

getSecondConObject()
        """
        return self.__cobj2

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends the TangentCircleTool::reset() method.
        """
        super(TwoPointTangentCCircleTool, self).reset()
        self.__cobj1 = None
        self.__cobj2 = None

    def setLocation(self, x, y):
        """Store an x/y coordinate pair in the tool.

setLocation(x, y)

Arguments 'x' and 'y' should be floats. This method extends
the TangentCircleTool::setLocation() methods.
        """
        super(TwoPointTangentCCircleTool, self).setLocation(x, y)
        _tx, _ty = self.getLocation()
        _obja = self.__cobj1
        _objb = self.__cobj2
        _tandata = tangent.calc_tangent_circle(_obja, _objb, _tx, _ty)
        if _tandata is not None:
            _cx, _cy, _radius = _tandata
            self.setCenter(_cx, _cy)
            self.setRadius(_radius)


class CCircleTangentLineTool(Tool):
    """A specialized class for creating tangent lines to construction circles.

This class is a specialized class that handles creating construction
lines around circles. There can be at most four possible tangent lines.
There are two tangent lines if the circles overlap, and no tangent
lines if one circle is inside another. As this tool is derived from
the Tool class it shares all the attributes and methods of that
class. The CCircleTangentLineTool class has the following additional
methods:

{get/set}FirstCCircle(): Get/Set the first CCircle in the tool.
{get/set}SecondCCircle(): Get/Set the second CCircle in the tool.
    """
    def __init__(self):
        super(CCircleTangentLineTool, self).__init__()
        self.__circ1 = None
        self.__circ2 = None
        self.__tanpts = []

    def setFirstCCircle(self, ccircle):
        """Store the first construction circle in the tool.

setFirstCCircle(ccircle)

Argument 'ccircle' must be a CCircle object.
        """
        if not isinstance(ccircle, CCircle):
            raise TypeError, "Invalid construction circle: " + `ccircle`
        self.__circ1 = ccircle

    def getFirstCCircle(self):
        """Retreive the first construction circle from the tool.

getFirstCCircle()
        """
        return self.__circ1

    def setSecondCCircle(self, ccircle):
        """Store the second construction circle in the tool.

setSecondCCircle(ccircle)

Argument 'ccircle' must be a CCircle object. A ValueError exception will
be raised if this method is called before the first construction circle
has been set.
        """
        if self.__circ1 is None:
            raise ValueError, "First construction circle not set."
        if not isinstance(ccircle, CCircle):
            raise TypeError, "Invalid construction circle: " + `ccircle`
        self.__circ2 = ccircle
        #
        # calculate the tangent points if they exist
        #
        _cc1 = self.__circ1
        _cc2 = self.__circ2
        _cx1, _cy1 = _cc1.getCenter().getCoords()
        _r1 = _cc1.getRadius()
        _cx2, _cy2 = _cc2.getCenter().getCoords()
        _r2 = _cc2.getRadius()
        _sep = math.hypot((_cx2 - _cx1), (_cy2 - _cy1))
        _angle = math.atan2((_cy2 - _cy1), (_cx2 - _cx1))
        _sine = math.sin(_angle)
        _cosine = math.cos(_angle)
        #
        # tangent points are calculated as if the first circle
        # center is (0, 0) and both circle centers on the x-axis,
        # so the points need to be transformed to the correct coordinates
        #
        _tanpts = self.__tanpts
        del _tanpts[:] # make sure it is empty ...
        _tansets = tangent.calc_two_circle_tangents(_r1, _r2, _sep)
        for _set in _tansets:
            _x1, _y1, _x2, _y2 = _set
            _tx1 = ((_x1 * _cosine) - (_y1 * _sine)) + _cx1
            _ty1 = ((_x1 * _sine) + (_y1 * _cosine)) + _cy1
            _tx2 = ((_x2 * _cosine) - (_y2 * _sine)) + _cx1
            _ty2 = ((_x2 * _sine) + (_y2 * _cosine)) + _cy1
            _tanpts.append((_tx1, _ty1, _tx2, _ty2))


    def getSecondCCircle(self):
        """Retreive the second construction circle from the tool.

getSecondCCircle()
        """
        return self.__circ2

    def hasTangentPoints(self):
        """Test if tangent points were found for the two circles.

hasTangentPoints()
        """
        _val = False
        if len(self.__tanpts):
            _val = True
        return _val

    def getTangentPoints(self):
        """Return the tangent points calculated for two-circle tangency.

getTangentPoints()

This method returns a list of tuples holding four float values:

(x1, y1, x2, y2)

A tangent line can be drawn between the two circles from (x1, y1) to (x2, y2).
        """
        return self.__tanpts[:]

    def create(self, image):
        """Create the tangent line for two circles.

create(image)
        """
        _x, _y = self.getLocation()
        _tanpts = self.__tanpts
        if not len(_tanpts):
            raise ValueError, "No tangent points defined."
        _minsep = _px1 = _py1 = _px2 = _py2 = None
        for _set in _tanpts:
            _x1, _y1, _x2, _y2 = _set
            _sqlen = pow((_x2 - _x1), 2) + pow((_y2 - _y1), 2)
            _rn = ((_x - _x1) * (_x2 - _x1)) + ((_y - _y1) * (_y2 - _y1))
            _r = _rn/_sqlen
            _px = _x1 + _r * (_x2 - _x1)
            _py = _y1 + _r * (_y2 - _y1)
            _sep = math.hypot((_px - _x), (_py - _y))
            if _minsep is None or _sep < _minsep:
                _minsep = _sep
                _px1 = _x1
                _py1 = _y1
                _px2 = _x2
                _py2 = _y2
        _active_layer = image.getActiveLayer()
        _p1 = _active_layer.find('point', _px1, _py1)            
        if _p1 is None:
            _p1 = Point(_px1, _py1)
            _active_layer.addObject(_p1)
        _p2 = _active_layer.find('point', _px2, _py2)
        if _p2 is None:
            _p2 = Point(_px2, _py2)
            _active_layer.addObject(_p2)
        _active_layer.addObject(CLine(_p1, _p2))

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends Tool::reset().
        """
        super(CCircleTangentLineTool, self).reset()
        self.__circ1 = None
        self.__circ2 = None
        del self.__tanpts[:]

class DimensionTool(Tool):
    """A base class for tools creating Dimension objects.

The DimensionTool class is meant to be a base class for classes
that will create Dimension objects. The DimensionTool class is
derived from the Tool class, so it shares all the attributes and
methods of that class. The DimensionTool class has the following
additional methods:

setDimension(): Store a dimension object in the tool
getDimension(): Retrieve a stored dimension object.
setDimPrefs(): Apply the current dimension preferences on a stored dimension.
    """
    def __init__(self):
        super(DimensionTool, self).__init__()
        self.__dim = None

    def _setDimension(self, dim):
        """Store a dimension in the tool.

setDimension(dim)

The argument 'dim' must be a Dimension object.
        """
        if not isinstance(dim, dimension.Dimension):
            raise TypeError, "Invalid Dimension object: " + str(dim)
        self.__dim = dim

    def getDimension(self):
        """Retrieve the stored dimension object from the tool.

getDimension()

This method returns the stored Dimension or None.
        """
        return self.__dim

    def setDimPrefs(self, image):
        """Apply the current dimension options to the stored dimension.

setDimPrefs(image)

The argument 'image' is an image option in which the current dimension
preferences are retrieved.
        """
        _dim = self.__dim
        if _dim is None:
            raise ValueError, "No dimension stored in tool."
        _pds = _dim.getPrimaryDimstring()
        _pds.mute()
        _pds.setFamily(image.getOption('DIM_PRIMARY_FONT_FAMILY'))
        _pds.setWeight(image.getOption('DIM_PRIMARY_FONT_WEIGHT'))
        _pds.setStyle(image.getOption('DIM_PRIMARY_FONT_STYLE'))
        _pds.setColor(image.getOption('DIM_PRIMARY_FONT_COLOR'))
        _pds.setSize(image.getOption('DIM_PRIMARY_TEXT_SIZE'))
        _pds.setAlignment(image.getOption('DIM_PRIMARY_TEXT_ALIGNMENT'))
        _pds.setPrecision(image.getOption('DIM_PRIMARY_PRECISION'))
        _pds.setUnits(image.getOption('DIM_PRIMARY_UNITS'))
        _pds.setPrintZero(image.getOption('DIM_PRIMARY_LEADING_ZERO'))
        _pds.setPrintDecimal(image.getOption('DIM_PRIMARY_TRAILING_DECIMAL'))
        _pds.unmute()
        _sds = _dim.getSecondaryDimstring()
        _sds.mute()
        _sds.setFamily(image.getOption('DIM_SECONDARY_FONT_FAMILY'))
        _sds.setWeight(image.getOption('DIM_SECONDARY_FONT_WEIGHT'))
        _sds.setStyle(image.getOption('DIM_SECONDARY_FONT_STYLE'))
        _sds.setColor(image.getOption('DIM_SECONDARY_FONT_COLOR'))
        _sds.setSize(image.getOption('DIM_SECONDARY_TEXT_SIZE'))
        _sds.setAlignment(image.getOption('DIM_SECONDARY_TEXT_ALIGNMENT'))
        _sds.setPrecision(image.getOption('DIM_SECONDARY_PRECISION'))
        _sds.setUnits(image.getOption('DIM_SECONDARY_UNITS'))
        _sds.setPrintZero(image.getOption('DIM_SECONDARY_LEADING_ZERO'))
        _sds.setPrintDecimal(image.getOption('DIM_SECONDARY_TRAILING_DECIMAL'))
        _sds.unmute()
        _dim.setOffset(image.getOption('DIM_OFFSET'))
        _dim.setExtension(image.getOption('DIM_EXTENSION'))
        _dim.setColor(image.getOption('DIM_COLOR'))
        _dim.setPosition(image.getOption('DIM_POSITION'))
        _dim.setEndpointType(image.getOption('DIM_ENDPOINT'))
        _dim.setEndpointSize(image.getOption('DIM_ENDPOINT_SIZE'))
        _dim.setDualDimMode(image.getOption('DIM_DUAL_MODE'))

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method sets the DimensionTool dimension to None.
        """
        super(DimensionTool, self).reset()
        self.__dim = None

class LinearDimensionTool(DimensionTool):
    """A specialized tool for drawing LinearDimension objects.

The LinearDimensionTool is derived from the DimensionTool and
Tool, class, so it shares all the attributes and methods of those classes.
The LinearDimensionTool class has the following addtional methods:

{set/get}FirstPoint(): Set/Get the first point in the LinearDimension
{set/get}SecondPoint(): Set/Get the second point in the LinearDimension.
{set/get}DimPosition(): Set/Get the location of the dimension text.
    """
    def __init__(self):
        super(LinearDimensionTool, self).__init__()
        self.__layer1 = None
        self.__point1 = None
        self.__layer2 = None
        self.__point2 = None
        self.__position = None

    def setFirstPoint(self, layer, point):
        """Store the first point for the LinearDimension.

setFirstPoint(layer, point):

The argument 'layer' is a Layer object containing the 'point' argument.
        """
        if not isinstance (layer, Layer):
            raise TypeError, "Invalid Layer: " + str(layer)
        if not isinstance (point, Point):
            raise TypeError, "Invalid Point: " + str(point)
        _lp = layer.findObject(point)
        if _lp is not point:
            raise ValueError, "Point not found in layer."
        self.__layer1 = layer
        self.__point1 = point

    def getFirstPoint(self):
        """Return the first point for the LinearDimension.

getFirstPoint()

This method returns a tuple of two objects: the first object
is a Layer, the second object is a Point.
        """
        return self.__layer1, self.__point1

    def setSecondPoint(self, layer, point):
        """Store the second point for the LinearDimension.

setSecondPoint(layer, point):

The argument 'layer' is a Layer object containing the 'point' argument.
        """
        if self.__layer1 is None or self.__point1 is None:
            raise ValueError, "First point not set for LinearDimension."
        if not isinstance (layer, Layer):
            raise TypeError, "Invalid Layer: " + str(layer)
        if not isinstance (point, Point):
            raise TypeError, "Invalid Point: " + str(point)
        _lp = layer.findObject(point)
        if _lp is not point:
            raise ValueError, "Point not found in layer."
        self.__layer2 = layer
        self.__point2 = point

    def getSecondPoint(self):
        """Return the second point for the LinearDimension.

getSecondPoint()

This method returns a tuple of two objects: the first object
is a Layer, the second object is a Point.
        """
        return self.__layer2, self.__point2

    def setDimPosition(self, x, y):
        """Store the point where the dimension text will be located.

setDimPosition(x, y)

Arguments 'x' and 'y' should be float values.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__position = (_x, _y)

    def getDimPosition(self):
        """Retrieve where the dimension text should be placed.

getDimPosition()

This method returns a tuple containing the x/y coodindates defined
by the setDimPosition() call, or None if that method has not been invoked.
        """
        return self.__position

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends the reset() methods of its base classes.
        """
        super(LinearDimensionTool, self).reset()
        self.__layer1 = None
        self.__point1 = None
        self.__layer2 = None
        self.__point2 = None
        self.__position = None

    def makeDimension(self, image):
        """Create a LinearDimension based on the stored tool values.

makeDimension(image)

The argument 'image' is an image object where the dimension will be used.
        """
        _l1 = self.__layer1
        _p1 = self.__point1
        _l2 = self.__layer2
        _p2 = self.__point2
        _x, _y = self.__position
        if (_l1 is not None and
            _p1 is not None and
            _l2 is not None and
            _p2 is not None and
            _x is not None and
            _y is not None):
            _ds = image.getOption('DIM_STYLE')
            _ldim = dimension.LinearDimension(_l1, _p1, _l2, _p2,
                                                      _x, _y, _ds)
            self._setDimension(_ldim)

    def create(self, image):
        """Create a new LinearDimension and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        _ldim = self.getDimension()
        if _ldim is not None:
            self.setDimPrefs(image)
            _pds = _ldim.getPrimaryDimstring()
            _pds.mute()
            try:
                _pds.setPrefix(image.getOption('DIM_PRIMARY_PREFIX'))
                _pds.setSuffix(image.getOption('DIM_PRIMARY_SUFFIX'))
            finally:
                _pds.unmute()
            _sds = _ldim.getSecondaryDimstring()
            _sds.mute()
            try:
                _sds.setPrefix(image.getOption('DIM_SECONDARY_PREFIX'))
                _sds.setSuffix(image.getOption('DIM_SECONDARY_SUFFIX'))
            finally:
                _sds.unmute()
            image.addObject(_ldim)
            self.reset()

class HorizontalDimensionTool(LinearDimensionTool):
    """A specialized tool for drawing HorizontalDimension objects.

The HorizontalDimensionTool is derived from the LinearDimensionTool
and the Tool classes, so it shares all the attributes and methods of
those class.

There are no additional methods for the HorizontalDimension class.
    """
    def __init__(self):
        super(HorizontalDimensionTool, self).__init__()

    def makeDimension(self, image):
        """Create a HorizontalDimension based on the stored tool values.

makeDimension(image)

The argument 'image' is an image object where the dimension willbe used.
        """
        _l1, _p1 = self.getFirstPoint()
        _l2, _p2 = self.getSecondPoint()
        _x, _y = self.getDimPosition()
        if (_l1 is not None and
            _p1 is not None and
            _l2 is not None and
            _p2 is not None and
            _x is not None and
            _y is not None):
            # print "makeDimension(): x: %g; y: %g" % (_x, _y)
            _ds = image.getOption('DIM_STYLE')
            if _ds is None:
                raise RuntimeError, "No DIM_STYLE_SET"
            else:
                _hdim = dimension.HorizontalDimension(_l1, _p1, _l2, _p2, _x, _y, _ds)
                self._setDimension(_hdim)

    def create(self, image):
        """Create a new HorizontalDimension and add it to the image.

create(image)

This method overrides the LinearDimensionTool::create() method.
        """
        _hdim = self.getDimension()
        if _hdim is not None:
            self.setDimPrefs(image)
            _pds = _hdim.getPrimaryDimstring()
            _pds.mute()
            try:
                _pds.setPrefix(image.getOption('DIM_PRIMARY_PREFIX'))
                _pds.setSuffix(image.getOption('DIM_PRIMARY_SUFFIX'))
            finally:
                _pds.unmute()
            _sds = _hdim.getSecondaryDimstring()
            _sds.mute()
            try:
                _sds.setPrefix(image.getOption('DIM_SECONDARY_PREFIX'))
                _sds.setSuffix(image.getOption('DIM_SECONDARY_SUFFIX'))
            finally:
                _sds.unmute()
            image.addObject(_hdim)
            self.reset()

class VerticalDimensionTool(LinearDimensionTool):
    """A specialized tool for drawing VerticalDimension objects.

The VerticalalDimensionTool is derived from the LinearDimensionTool
and the Tool classes, so it shares all the attributes and methods of
those class.

There are no additional methods for the VerticalalDimension class.
    """
    def __init__(self):
        super(VerticalDimensionTool, self).__init__()

    def makeDimension(self, image):
        """Create a VerticalDimension based on the stored tool values.

makeDimension(image)

The argument 'image' is an image object where the dimension will be used.
        """
        _l1, _p1 = self.getFirstPoint()
        _l2, _p2 = self.getSecondPoint()
        _x, _y = self.getDimPosition()
        if (_l1 is not None and
            _p1 is not None and
            _l2 is not None and
            _p2 is not None and
            _x is not None and
            _y is not None):
            _ds = image.getOption('DIM_STYLE')
            _vdim = dimension.VerticalDimension(_l1, _p1, _l2, _p2,
                                                        _x, _y, _ds)
            self._setDimension(_vdim)

    def create(self, image):
        """Create a new VerticalDimension and add it to the image.

create(image)

This method overrides the LinearDimensionTool::create() method.
        """
        _vdim = self.getDimension()
        if _vdim is not None:
            self.setDimPrefs(image)
            _pds = _vdim.getPrimaryDimstring()
            _pds.mute()
            try:
                _pds.setPrefix(image.getOption('DIM_PRIMARY_PREFIX'))
                _pds.setSuffix(image.getOption('DIM_PRIMARY_SUFFIX'))
            finally:
                _pds.unmute()
            _sds = _vdim.getSecondaryDimstring()
            _sds.mute()
            try:
                _sds.setPrefix(image.getOption('DIM_SECONDARY_PREFIX'))
                _sds.setSuffix(image.getOption('DIM_SECONDARY_SUFFIX'))
            finally:
                _sds.unmute()
            image.addObject(_vdim)
            self.reset()

class RadialDimensionTool(DimensionTool):
    """A specialized tool for drawing RadialDimension objects.

The RadialDimensionTool class is derived from the DimensionTool class
and Tool class, so it shares all the attributes and methods of those
classes. The RadialDimensionTool class has the following additional
methods:

{set/get}DimObject(): Set/Get the circular object being dimensioned.
{set/get}DimPosition(): Set/Get the location of the dimension text.
    """
    def __init__(self):
        super(RadialDimensionTool, self).__init__()
        self.__layer = None
        self.__cobj = None
        self.__position = None

    def setDimObject(self, layer, cobj):
        """Store the Circle or Arc that the RadialDimension will describe.

setDimObject(layer, cobj):

The argument 'layer' is a Layer object containing the 'cobj' argument.
        """
        if not isinstance (layer, Layer):
            raise TypeError, "Invalid Layer: " + str(layer)
        if not isinstance (cobj, (Circle, Arc)):
            raise TypeError, "Invalid Circle or Arc: " + str(cobj)
        _lc = layer.findObject(cobj)
        if _lc is not cobj:
            raise ValueError, "Circle/Arc not found in layer."
        if not isinstance(cobj, Circle):
            raise TypeError, "Invalid radial dimension object: " + str(cobj)
        self.__layer = layer
        self.__cobj = cobj

    def getDimObject(self):
        """Return the object the RadialDimension will define.

getDimObject()

This method returns a tuple of two objects: the first object
is a Layer, the second object is either a Circle or an Arc
        """
        return self.__layer, self.__cobj

    def setDimPosition(self, x, y):
        """Store the point where the dimension text will be located.

setDimPosition(x, y)

Arguments 'x' and 'y' should be float values.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__position = (_x, _y)

    def getDimPosition(self):
        """Retrieve where the dimension text should be placed.

getDimPosition()

This method returns a tuple containing the x/y coodindates defined
by the setDimPosition() call, or None if that method has not been
invoked.
        """
        return self.__position

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends the reset() methods of its base classes.
        """
        super(RadialDimensionTool, self).reset()
        self.__layer = None
        self.__cobj = None
        self.__position = None

    def makeDimension(self, image):
        """Create a RadialDimension based on the stored tool values.

makeDimension(image)

The argument 'image' is an image object where the dimension will
be used.
        """
        _layer = self.__layer
        _cobj = self.__cobj
        _x, _y = self.__position
        if (_layer is not None and
            _cobj is not None and
            _x is not None and
            _y is not None):
            _ds = image.getOption('DIM_STYLE')
            _rdim = dimension.RadialDimension(_layer, _cobj,
                                                      _x, _y, _ds)
            self._setDimension(_rdim)

    def create(self, image):
        """Create a new RadialDimension and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        _rdim = self.getDimension()
        if _rdim is not None:
            self.setDimPrefs(image)
            _pds = _rdim.getPrimaryDimstring()
            _pds.mute()
            try:
                _pds.setPrefix(image.getOption('RADIAL_DIM_PRIMARY_PREFIX'))
                _pds.setSuffix(image.getOption('RADIAL_DIM_PRIMARY_SUFFIX'))
            finally:
                _pds.unmute()
            _sds = _rdim.getSecondaryDimstring()
            _sds.mute()
            try:
                _sds.setPrefix(image.getOption('RADIAL_DIM_SECONDARY_PREFIX'))
                _sds.setSuffix(image.getOption('RADIAL_DIM_SECONDARY_SUFFIX'))
            finally:
                _sds.unmute()
            _rdim.setDiaMode(image.getOption('RADIAL_DIM_DIA_MODE'))
            image.addObject(_rdim)
            self.reset()

class AngularDimensionTool(LinearDimensionTool):
    """A specialized tool for drawing AngularDimension objects.

The AngularDimensionTool class is derived from the LinearDimensionTool
class, so it shares all the attributes and methods of that class. The
AngularDimensionTool class has the following additional methods:

{set/get}VertexPoint(): Set/Get the vertex point used by the dimension
    """
    def __init__(self):
        super(AngularDimensionTool, self).__init__()
        self.__vertex_layer = None
        self.__vertex_point = None

    def setVertexPoint(self, layer, point):
        """Store the vertex point for the AngularDimension.

setVertexPoint(layer, point):

The argument 'layer' is a Layer object containing the 'point' argument.
        """
        if point not in layer:
            raise ValueError, "Point not found in layer."
        self.__vertex_layer = layer
        self.__vertex_point = point

    def getVertexPoint(self):
        """Return the vertex point for the AngularDimension.

getVertexPoint()

This method returns a tuple of two objects: the first object
is a Layer, the second object is a Point.
        """

        return self.__vertex_layer, self.__vertex_point

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends LinearDimensionTool::reset().
        """
        super(AngularDimensionTool, self).reset()
        self.__vertex_layer = None
        self.__vertex_point = None

    def makeDimension(self, image):
        """Create an AngularDimension based on the stored tool values.

makeDimension(image)

The argument 'image' is an image object where the dimension will be used.
        """
        _vl = self.__vertex_layer
        _vp = self.__vertex_point
        _l1, _p1 = self.getFirstPoint()
        _l2, _p2 = self.getSecondPoint()
        _x, _y = self.getDimPosition()
        if (_vl is not None and
            _vp is not None and
            _l1 is not None and
            _p1 is not None and
            _l2 is not None and
            _p2 is not None and
            _x is not None and
            _y is not None):
            _ds = image.getOption('DIM_STYLE')
            _adim = dimension.AngularDimension(_vl, _vp, _l1, _p1,
                                                       _l2, _p2, _x, _y, _ds)
            self._setDimension(_adim)

    def create(self, image):
        """Create a new AngularDimension and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        _adim = self.getDimension()
        if _adim is not None:
            self.setDimPrefs(image)
            _pds = _adim.getPrimaryDimstring()
            _pds.mute()
            try:
                _pds.setPrefix(image.getOption('ANGULAR_DIM_PRIMARY_PREFIX'))
                _pds.setSuffix(image.getOption('ANGULAR_DIM_PRIMARY_SUFFIX'))
            finally:
                _pds.unmute()
            _sds = _adim.getSecondaryDimstring()
            _sds.mute()
            try:
                _sds.setPrefix(image.getOption('ANGULAR_DIM_SECONDARY_PREFIX'))
                _sds.setSuffix(image.getOption('ANGULAR_DIM_SECONDARY_SUFFIX'))
            finally:
                _sds.unmute()
            image.addObject(_adim)
            self.reset()

#
# printing/plotting tools
#

class PlotTool(Tool):
    """A tool for defining plot regions
    """
    def __init__(self):
        super(PlotTool, self).__init__()
        self.__c1 = None
        self.__c2 = None

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends Tool::reset().
        """
        super(PlotTool, self).reset()
        self.__c1 = None
        self.__c2 = None

    def setFirstCorner(self, x, y):
        """Store the first corner of the plot region.

setFirstCorner(x, y)

Arguments 'x' and 'y' should be floats.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__c1 = (_x, _y)

    def getFirstCorner(self):
        """Return the first corner of the plot region.

getFirstCorner()
        """
        if self.__c1 is None:
            raise ValueError, "First corner not defined"
        return self.__c1
    
    def setSecondCorner(self, x, y):
        """Store the second corner of the plot region.

setSecondCorner(x, y)

Arguments 'x' and 'y' should be floats.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__c2 = (_x, _y)

    def getSecondCorner(self):
        """Return the second corner of the plot region.

getSecondCorner()
        """
        if self.__c2 is None:
            raise ValueError, "Second corner not defined"
        return self.__c2
    
#
# entity modification tools
#

class RegionTool(Tool):
    """A base class for a tool that can store a defined region.

The RegionTool class is designed to be a base class for tools that
need to store a rectangular region. The RegionTool class is derived
from the Tool class, so it shares all the attributes and methods of
that classs. The RegionTool class has the following additional methods:

{set/get}Region(): Set/Get a stored rectangular region
    """
    def __init__(self):
        super(RegionTool, self).__init__()
        self.__region = None

    def setRegion(self, xmin, ymin, xmax, ymax):
        """Store a rectangular region in the RegionTool.

setRegion(xmin, ymin, xmax, ymax)

xmin: The minimum x-coordinate value
ymin: The minimum y-coordinate value
xmax: The maximum x-coordinate value
ymax: The maximum y-coordinate value

All the arguments should be floats. If xmax < xmin or ymax < ymin
a ValueError exception is raised.
        """
        _xmin = xmin
        if not isinstance(_xmin, float):
            _xmin = float(xmin)
        _ymin = ymin
        if not isinstance(_ymin, float):
            _ymin = float(ymin)
        _xmax = xmax
        if not isinstance(_xmax, float):
            _xmax = float(xmax)
        if _xmax < _xmin:
            raise ValueError, "Invalid values: xmax < xmin"
        _ymax = ymax
        if not isinstance(_ymax, float):
            _ymax = float(ymax)
        if _ymax < _ymin:
            raise ValueError, "Invalid values: ymax < ymin"
        self.__region = (_xmin, _ymin, _xmax, _ymax)

    def getRegion(self):
        """Retrieve the stored rectangular region in the tool.

getRegion()

This method returns a tuple containing four float values:

(xmin, ymin, xmax, ymax)
        """
        return self.__region

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method resets the RegionTool region to None.
        """
        super(RegionTool, self).reset()
        self.__region = None

class MoveTool(RegionTool):
    """A specialized class for moving objects.

The MoveTool class is derived from the Tool and RegionTool classes,
so it shares all the attributes and methods of those classes. The
MoveTool class has the following additional methods:

{set/get}Distance(): Set/Get the values to move objects.
    """
    def __init__(self):
        super(MoveTool, self).__init__()
        self.__distance = None

    def setDistance(self, x, y):
        """Store the distance to move objects in the tool.

setDistance(x, y)

Arguments 'x' and 'y' should be floats, and represent the amount
to move objects in the x-coordinate and y-coordinate axes.
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        self.__distance = (_x, _y)

    def getDistance(self):
        """Get the displacement stored in the tool.

getDistance()

This method returns a tuple containing two float values.

(xdisp, ydisp)

If this method is called before the setDistance() method is
used, a ValueError exception is raised.
        """
        if self.__distance is None:
            raise ValueError, "No distance stored in tool."
        return self.__distance

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends the reset() method of its base classes
        """
        super(MoveTool, self).reset()
        self.__distance = None

class HorizontalMoveTool(MoveTool):
    """A specialized class for moving objects horizontally.

The HorizontalMoveTool is derived from the MoveTool class, so
it shares all the attributes and methods of that class.

There are no additional methods for this class.
    """
    def __init__(self):
        super(HorizontalMoveTool, self).__init__()

    def setDistance(self, x, y):
        """Store the distance to move objects in the tool.

setDistance(x, y)

This method extends the MoveTool::setDistance() method by
enforcing a y-axis displacement of 0.0
        """
        _x = x
        if not isinstance(_x, float):
            _x = float(x)
        super(HorizontalMoveTool, self).setDistance(_x, 0.0)

class VerticalMoveTool(MoveTool):
    """A specialized class for moving objects vertically.

The VerticalMoveTool is derived from the MoveTool class, so
it shares all the attributes and methods of that class.

There are no additional methods for this class.

    """
    def __init__(self):
        super(VerticalMoveTool, self).__init__()

    def setDistance(self, x, y):
        """Store the distance to move objects in the tool.

setDistance(x, y)

This method extends the MoveTool::setDistance() method by
enforcing an x-axis displacement of 0.0
        """
        _y = y
        if not isinstance(_y, float):
            _y = float(y)
        super(VerticalMoveTool, self).setDistance(0.0, _y)

class StretchTool(MoveTool):
    """A specialized class for stretching objects

The StretchTool class is derived from the MoveTool class, so
it shares all the attributes and methods of that class.

There are no additional methods for this class.
    """
    def __init__(self):
        super(StretchTool, self).__init__()

class HorizontalStretchTool(HorizontalMoveTool):
    """A specialized class for stretching objects horizontally.

The HorizontalStretchTool class is derived from the HorizontalMoveTool
class, so it shares all the attributes and methods of that class.

There are no additional methods for this class.
    """
    def __init__(self):
        super(HorizontalStretchTool, self).__init__()

class VerticalStretchTool(VerticalMoveTool):
    """A specialized class for stretching objects horizontally.

The VerticalStretchTool class is derived from the VerticalMoveTool
class, so it shares all the attributes and methods of that class.

There are no additional methods for this class.
    """
    def __init__(self):
        super(VerticalStretchTool, self).__init__()

class DeleteTool(RegionTool):
    """A specialized class for deleting objects.

The DeleteTool class is derived from the Tool and RegionTool classes,
so it shares all the attributes and methods of those classes.

There are no additional methods for this class.
    """
    def __init__(self):
        super(DeleteTool, self).__init__()

class SplitTool(RegionTool):
    """A specialized class for splitting objects.

The SplitTool class is derived from the Tool and RegionTool classes,
so it shares all the attributes and methods of those classes.

There are no additional methods for this class.
    """
    def __init__(self):
        super(SplitTool, self).__init__()

class MirrorTool(RegionTool):
    """A specialized class for mirroring objects.

The MirrorTool class is derived from the Tool and RegionTool classes,
so it shares all the attributes and methods of those classes. The
MirrorTool class has the following additional methods:

{set/get}MirrorLine(): Set/Get the construction line used for mirroring
    """
    def __init__(self):
        super(MirrorTool, self).__init__()
        self.__mirrorline = None

    def setMirrorLine(self, mline):
        """Store the mirroring construction line in the tool.

setMirrorLine(mline)

The argument 'mline' must be a construction line, otherwise
a TypeError exception is raised.
        """
        if not isinstance(mline, (HCLine, VCLine, ACLine, CLine)):
            raise TypeError, "Invalid mirror line: " + `mline`
        self.__mirrorline = mline

    def getMirrorLine(self):
        """Retrieve the mirroring construction line from the tool.

getMirrorLine()

If the mirroring construction line has not been set, a ValueError
exception is raised.
        """
        if self.__mirrorline is None:
            raise ValueError, "No mirror line set."
        return self.__mirrorline

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends the RegionTool::reset() method.
        """
        super(MirrorTool, self).reset()
        self.__mirrorline = None

class TextTool(Tool):
    """A specialized class for entering text.

The TextTool class is derived from the Tool class, so it shares
that attributes and methods of that class. The TextTool class has
the following additional methods:

{set/get}Text(): Set/Get the text string in the tool.
hasText(): Test if the tool has stored a text string
{set/get}TextLocation(): Set/Get where the text is to be placed.
{set/get}Alignment(): Set/Get the alignment of the text
{set/get}Angle(): Set/Get the angle at which the text is to be displayed.
{set/get}Bounds(): Set/Get the width and height of the text
{set/get}PixelSize(): Set/Get the a rectangular region bounding the text.
{set/get}Layout(): Set/Get the formatted text string display.
    """
    def __init__(self):
        super(TextTool, self).__init__()
        self.__text = None
        self.__location = None
        self.__align = None
        self.__angle = None
        self.__bounds = None
        self.__pixel_size = None
        self.__layout = None

    def setText(self, text):
        """Store some text in the tool.

setText(text)

The argument 'text' should be a unicode object.
        """
        _text = text
        if not isinstance(_text, unicode):
            _text = unicode(text)
        self.__text = _text

    def getText(self):
        """Retrieve the stored text from the TextTool.

getText()

If no text has been stored, this method raises a ValueError exception.
        """
        if self.__text is None:
            raise ValueError, "No text stored in TextTool."
        return self.__text

    def hasText(self):
        """Test if the tool has stored a text string.

hasText()
        """
        return self.__text is not None

    def setTextLocation(self, x, y):
        """Store the location where the text will be placed.

setTextLocation(x, y)

The arguments 'x' and 'y' should be float values.
        """
        _x, _y = util.make_coords(x, y)
        self.__location = (_x, _y)

    def getTextLocation(self):
        """Retrieve the location where the text will be placed.

getTextLocation()

This method returns a tuple holding two floats:

(x, y)


A ValueError exception is raised if this method is called prior to
setting the text location with setTextLocation().
        """
        if self.__location is None:
            raise ValueError, "No text location defined."
        return self.__location

    def setAngle(self, angle):
        """Store the angle at which the text will be displayed.

setAngle(angle)

Argument 'angle' should be a float. The argument will be adjusted
so that -360.0 < angle < 360.0
        """
        _angle = angle
        if not isinstance(_angle, float):
            _angle = float(angle)
        if _angle > 360.0 or _angle < -360.0:
            _r, _i = math.modf(_angle/360.0)
            _angle = _r * 360.0
        self.__angle = _angle

    def getAngle(self):
        """Retrieve the angle at which the text will be displayed.

getAngle()

A ValueError exception is raised if this method is called before
the angle has been set by setAngle()
        """
        if self.__angle is None:
            raise ValueError, "No angle defined."
        return self.__angle

    def getBounds(self):
        """Return the width and height of the TextBlock.

getBounds()        
        """
        if self.__bounds is None:
            raise ValueError, "TextBlock bounds not defined."
        return self.__bounds

    def setBounds(self, width, height):
        """Set the width and height of the TextBlock.

setBounds(width, height):

Arguments 'width' and 'height' should be positive float values.
        """
        _w = width
        if not isinstance(_w, float):
            _w = float(width)
        if _w < 0.0:
            raise ValueError, "Invalid width: %g" % _w
        _h = height
        if not isinstance(_h, float):
            _h = float(height)
        if _h < 0.0:
            raise ValueError, "Invalid height: %g" % _h
        self.__bounds = (_w, _h)
        
    def setPixelSize(self, width, height):
        """Store a screen-size rectangular boundary for the text.

setPixelSize(width, height)

Arguments 'width' and 'height' should be positive integer values.

This method is somewhat GTK specific ...
        """
        _width = width
        if not isinstance(_width, int):
            _width = int(width)
        if _width < 0:
            raise ValueError, "Invalid width: %d" % _width
        _height = height
        if not isinstance(_height, int):
            _height = int(height)
        if _height < 0:
            raise ValueError, "Invalid height: %d" % _height
        self.__pixel_size = (_width, _height)

    def getPixelSize(self):
        """Retrieve the stored rectangular region of text.

getPixelSize()

A ValueError exception is raised if this method is called before
the size has been set by setPixelSize()
        """
        if self.__pixel_size is None:
            raise ValueError, "Pixel size is not defined."
        return self.__pixel_size

    def getAlignment(self):
        """Return the current text alignment

getAlignment()        
        """
        return self.__align

    def setAlignment(self, align):
        """Set left, center, or right line justification.

setAlignment(align)

The argument 'align' should be one of

TextStyle.ALIGN_LEFT
TextStyle.ALIGN_CENTER
TextStyle.ALIGN_RIGHT
        """
        if not isinstance(align, int):
            raise TypeError, "Invalid text object alignment: " + str(align)
        if (align != TextStyle.ALIGN_LEFT and
            align != TextStyle.ALIGN_CENTER and
            align != TextStyle.ALIGN_RIGHT):
            raise ValueError, "Invalid justification: " + str(align)
        self.__align = align
        
    def setLayout(self, layout):
        """Store a formatted layout string for the text.

setLayout()

This method is very GTK/Pango specific ...
        """
        self.__layout = layout

    def getLayout(self):
        """Retrieve the formatted layout for the text string.

getLayout()

This method is very GTK/Pango specific ...
        """
        return self.__layout

    def create(self, image):
        """Create a new TextBlock and add it to the image.

create(image)

This method overrides the Tool::create() method.
        """
        _text = self.getText()
        _x, _y = self.getTextLocation()
        _textstyle = image.getOption('TEXT_STYLE')
        # print "textstyle: " + `_textstyle`
        _tb = TextBlock(_x, _y, _text, _textstyle)
        _tb.setFamily(image.getOption('FONT_FAMILY'))
        _tb.setStyle(image.getOption('FONT_STYLE'))
        _tb.setWeight(image.getOption('FONT_WEIGHT'))
        _tb.setColor(image.getOption('FONT_COLOR'))
        _tb.setSize(image.getOption('TEXT_SIZE'))
        _tb.setAngle(image.getOption('TEXT_ANGLE'))
        _tb.setAlignment(image.getOption('TEXT_ALIGNMENT'))
        image.addObject(_tb)
        self.reset()

    def reset(self):
        """Restore the tool to its initial state.

reset()

This method extends Tool::reset().
        """
        super(TextTool, self).reset()
        self.__text = None
        self.__location = None
        self.__align = None
        self.__angle = None
        self.__bounds = None
        self.__pixel_size = None
        self.__layout = None
