Volume 3: Programming with the Visio Object Model disclaimer


Getting a Selection Object



Download 0.78 Mb.
Page17/36
Date29.01.2017
Size0.78 Mb.
#11974
1   ...   13   14   15   16   17   18   19   20   ...   36

Getting a Selection Object


The Selection object, or set of shapes selected in a window, can be accessed through the Selection property of the Window object.

The easiest way to get the Selection object is to access the selection in the current active window:

Dim oSelection as Visio.Selection

Set oSelection = Application.ActiveWindow.Selection
One very good use for a Selection object is to allow users to indicate the shapes they want to work with. You can also create a Selection object through automation as a means of working with a subset of shapes on the page.

The order of items in the collection follows the order in which the corresponding shapes are selected in the drawing window. The first shape selected is the first item in the collection. If the shapes have been selected by dragging out a selection rectangle, then they are put in the selection in front to back order, with the forward-most shape being the first shape in the selection.

Probably the most important thing to keep in mind about a Selection object is this: It’s not bound to the selection in the drawing window. After you get a Selection object, the user can change the selection, but the changes won’t be reflected in your program unless you get another Selection object. For this reason, it’s best to get a Selection object just before you plan to use it.

Working with Windows and Selected Shapes


An Application object has a Windows collection, which includes all of the drawing, ShapeSheet, stencil, and icon edit windows that are open in the instance. One thing to remember about the Windows collection is that it may grow or shrink by more than one item at a time, depending on other actions.

You can iterate through a Windows collection in the usual way, but more often you’ll be interested in a particular kind of window. You can check the Window object’s Type property to find out what kind of window it is. For a drawing window, you may also want to check its SubType property to make sure you have the right kind of drawing window.

Note:

If the type property of a Window object returns any value other than visDrawing, the subtype property returns the same value as the type property.

Not surprisingly, the ActiveWindow property returns the active window. If your program has just run an instance of Visio and created a drawing, you can probably assume that the active window is a drawing window. However, it is always good to make sure. Many of a Window object’s methods are only relevant in a drawing window.

Here are some ways to utilize a Window object’s properties and methods:



  • Use the Activate method of a Window object to make it the active window

  • Get the document displayed in the window by getting its Document property

  • Change the magnification with the Zoom property

  • Get shapes selected in a drawing window by getting the Selection property

Getting Cells with CellsSRC


You used the Cells property in the last module to get cells by name. This is almost always the preferred way to get a particular cell. However, sometimes you might want to iterate through rows or cells, or even sections such as in a shape with multiple Geometry sections. You can do this using section, row, and cell indexes.

The example on the slide uses CellsSRC to get the Controls.X3 cell. (Rows are indexed starting with the row index constant as the base, so you need to add an integer offset for the second and subsequent rows.) This might seem like a lot of work to get a single cell, but iterating through each of the Control section rows and getting the same cell from each one is easier using CellsSRC. All you have to do is increment the row offset instead of constructing a cell reference string.

You can also use these indexes to see if the shape has a particular section, row, or cell, or use them to add sections to a shape, as you’ll see later in this session.


Try it! CellsSRC

  1. From your student CD, open \Demos\Vol3\Getting Data from Drawings\Collections.vsd.

  2. Select one of the shapes on the drawing page and run the macro View > Macros > Sample_Code.SampleCode.PrintGeometry.

  3. Open the Immediate window (also known as the Debug window) in VBA. The macro prints all cell formulas from the Geometry section to the Immediate window.

Determining Shape Proximity


Visio offers several properties for determining shape proximity.



  • DistanceFrom property of the shape returns the distance between two shapes

  • SpatialRelation property describes the relationship between two shapes

  • SpatialNeighbors property of a shape returns shapes meeting relationship criteria (For an example, see Spatial Relation Office.vsd)

  • SpatialSearch property of shape, page, or master returns shapes near a point (For an example, see Which Shapes are in View.vsd)

10.

SpatialRelation


The SpatialRelation property is used to determine the relationship between two shapes. It can determine if two shapes overlap or touch, or if one is inside the other.


Demonstration: Shape Relationships

  1. Open \Demos\Vol3\Getting Data from Drawings\Spatial Relation.vsd.

  2. Note: Before you establish the shape relationship, both shapes need to be selected.

  3. Move the small square so its edge touches the edge of the large square, and click the Relationship button.

  4. Move the small square so it overlaps the large square, and click the Relationship button.

  5. Move the small square so it is within the bounds of the large square, and click the Relationship button.

  6. Repeat step five, but this time, change the shape selection order.


Download 0.78 Mb.

Share with your friends:
1   ...   13   14   15   16   17   18   19   20   ...   36




The database is protected by copyright ©ininet.org 2024
send message

    Main page