Shape Geometry

Most drawing programs are based on two-dimensional geometry. When you draw an object, the program records the object as a collection of horizontal and vertical locations. These locations, called vertices in Microsoft® Visio®, are measured from a point of origin on the page and are connected with line segments, just as if you were drawing the object on a piece of graph paper.

A sequence of line or curve segments that connect a shape's vertices is called a path. Each path corresponds to a Geometry section, which you can view in a ShapeSheet® window or access through Automation. Each vertex defining a path corresponds to a row of its Geometry section. A path can be closed or open, and a shape can have more than one path (and therefore more than one Geometry section); for details, see Chapter 6, Grouping and Merging Shapes.

A sequence of line or curve segments that connect a shape's vertices is called a path.

A sequence of line or curve segments that connect a shape's vertices is called a path.

What makes Visio different from other drawing programs is that you can use formulas to control vertex locations. Instead of simply recording a new position when a shape is moved or sized, Visio can calculate a vertex in relation to other vertices or other shapes, or constrain it to a fixed position on the page. The ability to describe shapes with formulas opens many possibilities for making shapes behave in complex and sophisticated ways.

The following illustration shows a simple example. In the rectangle on the left, width and height are independent of each other—changing one won't affect the other. However, in the rectangle on the right, height is calculated with a formula that refers to its width. Changing the shape's width will cause its height formula to be recalculated and the shape's height to change.

In rectangle on left, height and width are independent of each other. In rectangle on right, height and width recalculate based on your formula.

When you create a formula for a shape, Visio recalculates the shape's vertices on the basis of your formula.

  1. Width = 1
  1. Height = 1
  1. Width = 3
  1. Height = Width * 0.5

In this section…

Describing Shapes in a Coordinate System

Representing Shape Geometry with Formulas

Representing a Shape's Position on a Page

Describing Shapes in a Coordinate System

Among the most useful and powerful formulas are those that control a shape's size or position. Each vertex of a shape is recorded as a pair of x,y coordinates. When you move the shape or change its size, Microsoft Visio records the changes to the shape's vertices and redraws the shape at its new position or size. To move, flip, rotate, or resize a Visio shape with formulas, you must describe the shape in terms of the coordinate system.

Visio uses different coordinate systems to describe a shape. Depending on what you want to do to a shape, you might need to work with the following three coordinate systems.

Local coordinates A shape's width and height define the local coordinate system. The origin is the lower-left corner of the shape's width-height box. The Geometry section uses formulas to describe the local coordinates of the vertices for the paths that make up a shape. By modifying these formulas, you can control a shape's appearance, no matter where the shape is positioned on the drawing page.

Page coordinates The location of a shape or group on the drawing page is described in page coordinates, which have their origin at the lower-left corner of the page. Page coordinates are displayed on the ruler in the units of measure specified in the Page Setup dialog box (Page Properties tab).

Parent coordinates Visio also identifies the position of an object relative to its parent. For shapes on a drawing page, parent coordinates are equivalent to page coordinates. However, if a shape is in a group, its parent is the group, not the page, so its parent coordinates are the local coordinates of the group. In this case, the origin of the parent coordinate system is the lower-left corner of the group's width-height box.

Different coordinate systems used by Visio to identify shape vertices and position

Visio uses different coordinate systems to identify shape vertices and position.

  1. Width-height box
  1. Local coordinates
  1. Page coordinates

Tip You cannot move the origin of the page coordinate system. However, you can change the zero point of the coordinates displayed on the rulers by holding down CTRL and dragging the crossbar at the intersection of the two rulers. Moving the zero point has no effect on the page coordinate system, but it can be useful for measuring the distance between shapes.

TOP

Representing Shape Geometry with Formulas

Microsoft Visio represents a shape's width, height, and position on the page with formulas in its Shape Transform section. Visio usually expresses the value of each vertex in each shape's Geometry section as a fraction of the shape's width or height. When you move, resize, or rotate a shape, Visio writes new formulas in the Shape Transform section, and then reevaluates the vertex formulas in the Geometry section. When you edit a particular vertex, new formulas are generated for that vertex expressed in adjusted factors of Width and Height.

For example, consider the rectangle in the following illustration, shown with its Shape Transform and Geometry sections.

A rectangle with its Shape Transform and Geometry sections

A rectangle with its Shape Transform and Geometry sections

Notice that the rectangle has = 3 in. in its Width cell and the formula =Width * 1 in two of its Geometry cells. If the shape is stretched horizontally on the drawing page, the value of the Width cell changes, which changes the value of the local coordinates specified in the Geometry section. The Geometry formula, however, remains = Width * 1. The Geometry formulas that represent vertices of the shape are all local coordinates, expressed in terms of the shape's width and height.

Note Row types in the Geometry section describe how a segment of a path should be drawn. In the preceding example, the row types MoveTo and LineTo describe straight line segments. Imagine telling someone how to draw a rectangle on a sheet of paper. You would say something like "Move the pen to coordinates 0, 0, then draw a line to coordinates 3, 0..." and so on.

Row types that describe curves (especially curves drawn with the Freeform tool) are more complex than those describing straight lines, and certain row types can represent many vertices with a single row. For details about these Geometry row types and the Geometry section, see the ShapeSheet Reference in the Microsoft Visio Developer Reference (on the Help menu, click Developer Reference).

TOP

Representing a Shape's Position on a Page

The position of a shape on the page is described by its pin, or center of rotation. Microsoft Visio uses two coordinates (PinX, PinY and LocPinX, LocPinY) in the Shape Transform section to store the location of a shape's pin:

  • The PinX and PinY cells store the pin's x and y locations with respect to its parent, which can be a group or page. That is, PinX and PinY are expressed in parent coordinates. PinX and PinY represent the shape's position with respect to its parent. If the shape is moved, the values of PinX and PinY change.
  • The LocPinX and LocPinY cells store the pin's x and y positions with respect to the shape. That is, LocPinX and LocPinY are expressed in local coordinates. LocPinX and LocPinY represent the point around which the shape pivots if you rotate the shape. Like a shape's Geometry formulas, LocPinX and LocPinY are formulas that reference the shape's width and height. If the shape is moved, its LocPinX and LocPinY formulas don't change.

The pin describes a shape's position in local and parent coordinates.

The pin describes a shape's position in local and parent coordinates.

  1. The local coordinates of the pin describe this point (Width * 0.5, Height * 0.5).
  1. The parent coordinates of the pin define this point.

To visualize how the pin works, imagine attaching a 3-by-5 index card to a sheet of paper by pressing a pin through the card and then through the paper. You can describe the location of the card on the paper with respect to the holes created by the pin.

That's how the pin works in Visio. The local coordinates of the pin (the hole in the card) are (LocPinX, LocPinY). The parent coordinates (the hole in the paper) are (PinX, PinY). If you pin the card to a different part of the paper—the equivalent of moving a shape on a page—the card's hole doesn't move with respect to the card. That is, the pin's local coordinates do not change. However, a new pinhole is formed on the paper, because the pin's parent coordinates have changed.

Using formulas to move a shape

When you move a shape on a page using the mouse, Visio updates the values of PinX and PinY to reflect the new position of the shape on the page. To use formulas to move a shape, you set the values of PinX and PinY. For example, to move the arrow in the following figure up the page by 1 inch, you might use this formula:

Or, you could tie the arrow's position on the page to the width of the page with a formula such as this one:

By default, the pin is the center of the shape, which Visio expresses as formulas that use local coordinates (Width*0.5, Height*0.5). You can move a shape's pin in any of the following ways:

  • Write new formulas in the LocPinX and LocPinY cells.
  • Choose a Pin Pos option in the Size & Position window. To display this window for a selected shape, click Size & Position Window on the View menu, or right-click a shape, point to View, and then click Size & Position Window on the shortcut menu.
  • Drag the pin with the Rotation tool in the drawing window.

If you move the pin by using the Rotation tool in the drawing window, the values of PinX and PinY also change so that the shape stays in the same position on the page.

The Shape Transform section includes the local and parent coordinates of the pin.

The Shape Transform section includes the local and parent coordinates of the pin.

  1. The parent coordinates of the pin
  1. The local coordinates of the pin

The values of the PinX and PinY cells correspond to the values shown in the X and Y options in the Size & Position window. You can change the values of PinX and PinY by changing values of X and Y in this window.

Preventing shape movement by protecting formulas

When a user moves or stretches a shape, Visio writes new values to the Shape Transform section and overwrites the affected cells' formulas, including those in the PinX and PinY cells. To protect a shape's formulas from being overwritten (thereby preventing a user from moving the shape), use the GUARD function.

For example, to guard the formulas shown in the previous section (Using formulas to move a shape):

  • PinY = GUARD(1.5 in.)
  • PinX = GUARD(ThePage!PageWidth - 5 in.)

Tip You can set the LockRotate, LockMoveX, and LockMoveY cells to prevent users from rotating or moving the shape and guard other formulas to protect shapes from other user actions. For details about protection locks and the GUARD function, see Using Locks to Limit Shape Behavior later in this chapter.