NShape Programmer Tasks
Creating the Shape Class

<< Click to Display Table of Contents >>

Navigation:  Programmer Tasks > Developing a New Shape Class >

NShape Programmer Tasks
Creating the Shape Class

Previous pageReturn to chapter overviewNext page

There are two important decisions at this point. The first is whether the new shape will be linear shape or a planar shape. You can find detailed explanations for these two alternatives in "Shapes" or chose based on this quick rule: If the new shape look rather like a line, go with the linear shape. If it makes sense to assign a width and height to it, choose the planar shape.

The second decision is about the base class for the new shape. NShape provides a set of general purpose base classes or you might already have some suitable base classes by yourself. The possible base classes from the NShape framework are:

DiamondBase: For shapes that have a diamond shape

EllipseBase: For shapes that are elliptical

IsoscelesTriangleBase: For shapes based on an isosceles triangle

TextBase: For shapes that display text

RectangleBase: For all planar shapes that are more or less rectangular.

DiameterShapeBase: For all shapes that are as wide as high, e.g. square and circle

CaptionedShapeBase: For shapes that display a text.

PathBasedShape: For shapes that do not fit in one of the above categories but draw themselves based on a .NET graphics path.

PolylineBase: For all connector shapes that consist of one or more straight line segments.

CircularArcBase: For connector shapes that behave like a arc of a circle.

LineShapeBase: For line- and connector shapes.

ImageBasedShape: For shapes based on vector image files.

ShapeBase: For shapes that do not fit into any other category and/or shapes that want to draw themselves in another way than with a graphics path.

Shape: For shapes that should be rebuilt from scratch. You have to implement more or less all of the methods and properties by yourself.

To create the shape class and the creation method

1.Create a new "Class Library" project.

In contrast to applications, shape libraries must target the same .NET Framework version as the NShape assemblies.
The current NShape version targets .NET Framework 2.0.
We also recommend to set the target platform to "Any CPU".

2.Add a new class derived from one of the base classes listed above.

3.Add a protected internal constructor that expects the template and the shape type for its parameters. Implement for now by calling the base class.

4.Add a protected internal copy constructor for the new shape class, which for now just calls the base class.

5.Implement the method Clone (from ICloneable) by returning a deep copy of the passed object.

6.In order to keep the process of registering shapes more simple, add a static public method called CreateInstance with all the constructor parameters.

static public MyShape CreateInstance(ShapeType shapeType, Template template) {
 return new MyShape(shapeType, template);

7.Add a static NShapeLibraryInitializer class to your library and implement it. Add the shape type creation call of your shape to the NShapeInitializer.Initialize method.

8.Compile the library, run the NShape Designer, create a new project and load the new shape library. You should now see the new shape in the toolbox.

The next step is to draw the shape, which is described in "Shaping the Shape".

See Also

Programmer Tasks: Developing a new shape class