﻿ Geometry
Nevron Open Vision Documentation
Diagram / Diagram DOM / Drawings / Shapes / Shape Elements / Geometry
In This Topic
Geometry
In This Topic

The geometry of a shape is represented by the NGeometry class an instance of which can be obtained from the Geometry property of each shape. The geometry is responsible for the appearance (fill, stroke, shadow and arrowheads) of a shape and for generating an NGraphicsPath for the shape. The current path can be obtained from the Path property of the geometry.

The NGeometry object is a collection for objects that derive from the base NGeometryCommand class. In this way the path represented by the geometry is defined by a sequence of geometry commands. There are two distinct types of geometry commands - plotter commands and draw box commands. that are discussed in the next section.

Geometry Appearance

You can hide the fill, the stroke, the shadow and/or the arrowheads of each figure in a shape geometry by setting the respective Show... properties to false. All geometry commands that start (NMoveTo) or draw a new figure (NDrawRectangle, NDrawEllipseNDrawPath etc.) have these properties.

If ShowFill is true, the area surrounded by the geometry will be filled with the filling specified by the Fill property. The FillRule property specifies the algorithm used to define whether a point is inside the geometry or not. By default it is set to EvenOdd.

If ShowStroke is true, the geometry outline will be stroked with the Stroke property.

If ShowShadow is true, the area surrounded by the geometry will drop the shadow specified by the Shadow property.

If ShowArrowheads is true, the geometry will display the arrowheads specified by the BeginArrowhead and EndArrowhead properties at the first and last geometry points respectively.

For example, to hide the fill of a figure started with NMoveTo, you can use the following piece of code:

Hide Fill of a Figure
Copy Code
```NMoveTo moveTo = new NMoveTo();
moveTo.ShowFill = false;
```

Any graphics path created by the NGeometry object can have round corners, which is controlled by the CornerRounding property.

The code fragment below demonstrates how to create a shape with yellow color fill, blue 3 DIPs (Device Independent Pixels) wide stroke and red text: Create shape and set fill and stroke
Copy Code
```// Create a rectangle shape
NShapeFactory basicShapeFactory = new NBasicShapeFactory();
NShape shape = basicShapeFactory.CreateShape((int)ENBasicShape.Rectangle);

// Set geometry fill and stroke
shape.Geometry.Fill = new NColorFill(NColor.Yellow);
shape.Geometry.Stroke = new NStroke(3, NColor.Blue);

// Set text and text fill
shape.Text = "My Shape";
shape.TextBlock.Fill = new NColorFill(NColor.Red);

// Position the page and add it to the active page
shape.SetPinPoint(new NPoint(200, 200));
```
Geometry Commands

As mentioned there are two distinct types of geometry commands - plotter commands and draw box commands.

Plotter Commands

Plotter commands are intended to be placed in a sequence and define the path of a shape via primitive drawing commands such as move to, line to, arc to etc that resemble the commands of a plotter machine. All plotter commands derive from the NPlotterCommand class. Each plotter command has X and Y properties that define the location of the command. Depending on the plotter command, it may have additional properties that control the curve plotted by the command. Plotter commands are summarized by the following table:

Command Class Command Description
NMoveTo Moves the plotter to a new position without connecting the points (starts a new figure). This is typically the first command in a plotter based geometry. The NMoveTo command has a ClosePrevFigure property which indicates whether the previous figure in the path needs to be closed.
NLineTo Draws a line from the previous plotter location to the current position.
NCubicBezierTo Draws a cubic bezier curve from the previous plotter location to the current position. The first and second control points of the cubic bezier are controlled by the FirstControlX, FirstControlY, SecondControlX and SecondControlY properties.
NArcTo Draws a circular arc from the previous plotter location to the current position. The arc is controlled by the Bow parameter which is distance of the arc from the line formed by previous command location and this command location.
NCircularArcTo Draws a circular arc from the previous plotter location to the current position. The arc is controlled by a control point, through which the circular arc passes.  The control point is defined by the ControlX and ControlY properties.
NEllipticalArcTo Draws an elliptical arc from the previous plotter location to the current position. The arc is controlled by a control point, through which the circular arc passes and two additional values - the Ratio between the major and minor ellipse axes and the Angle which the major axis forms with the X axis.  The control point is defined by the ControlX and ControlY properties.

The following example demonstrates how to draw a rectangle using plotter commands:

Draw Rectangle with Plotter Commands
Copy Code
```NShape shape = new NShape();
shape.Init2DShape();
shape.Geometry.CloseLastFigure = true;
```

Draw Box Commands

Draw box commands are intended to create entire path figures or paths. All drawing box commands derive from the NDrawBoxCommand class. Each drawing box command has a "drawing box", which is specified by the Left, Top, Right and Bottom properties of the drawing box command. The draw box command "draws" a figure or path inside this drawing box. The NDrawBoxCommand command also has a ClosePrevFigure property, which indicates whether the previous figure in the path needs to be closed. Draw box commands are summarized by the following table:

 Command Class Command Description NDrawRectangle Draws a rectangle inside the draw box. NDrawEllipse Draws an ellipse inside the draw box. NDrawPolyline Draws a polyline inside the draw box. The polyline is defined by set of NPoint objects that are relative to the draw box. NDrawPolygon Draws a polygon inside the draw box. The polygon is defined by set of NPoint objects that are relative to the draw box. NDrawPath Draws a path inside the draw box. The path is defined by set of NGraphicsPathPoint objects that are relative to the draw box.

The following example draws an ellipse using draw box commands:

Draw Ellipse Using Draw Box commands
Copy Code
```NShape shape = new NShape();
shape.Init2DShape();
```

Drawing Commands with Relative Coordinates

When the geometry command Relative property is true, the X and Y properties of plotter commands and the Left, Top, Right, Bottom properties of draw box commands, as well as other properties that define command "locations" are in shape "relative" coordinates. This helps you create scalable geometries that are defined as "factors" from the shape Width and Height. Although this can also be achieved via expressions (for example Width*0.5, Height*0.3), using relative coordinates is more memory efficient and fast to calculate.

To add commands as relative to a geometry you can use the AddRelative method of the geometry or the Rel...To methods like RelMoveTo, RelLineTo, RelCubicBezierTo, etc. These methods will automatically set the Relative property of the passed geometry command to true.

The previous two examples can be rewritten with relative commands as follows:

Relative Commands
Copy Code
```// Draw rectangle using relative plotter commands to fill the shape Width/Height box
NShape rect = new NShape();
rect.Init2DShape();
rect.Geometry.RelMoveTo(0, 0);
rect.Geometry.RelLineTo(1, 0);
rect.Geometry.RelLineTo(1, 1);
rect.Geometry.RelLineTo(0, 1);
rect.Geometry.CloseLastFigure = true;

// Draw ellipse using relative draw box commands to fill the shape Width/Height box
NShape ellipse = new NShape();
ellipse.Init2DShape();