Volume 3: Programming with the Visio Object Model disclaimer



Download 0.78 Mb.
Page13/36
Date29.01.2017
Size0.78 Mb.
#11974
1   ...   9   10   11   12   13   14   15   16   ...   36

Getting the Cell to Glue


Here are a few examples of how you can indicate various parts of a shape you want to glue.

In this example, Controls.X1 refers to the first row of the Controls section in the ShapeSheet. If the shape had more than one control handle, you can indicate a different row. To do this, the control handle must be able to be glued (some aren’t), which depends on the CanGlue setting in the ShapeSheet.

Control handles and the begin and end points of 1-D shapes are defined by two cells—one for X and one for Y. You do not need both cells in order to glue the item, and you can use either one.

If you’re gluing a 2-D shape to a guide or guide point, you can indicate which edge of the shape you want to glue. The shape edge that you specify needs to be appropriate for the type of guide—AlignLeft, AlignCenter, or AlignRight for a vertical guide, and AlignTop, AlignMiddle, or AlignBottom for a horizontal one.

For example, if you try to glue the right edge of a shape to a horizontal guide, you’ll get an Inappropriate Target Cell error. You may not get this error with a guide point, but you will get odd results.


Gluing to the Other Shape


One type of cell you can glue to using the GlueTo method is a connection point. As when specifying the cell to glue, you only need one cell of the X,Y pair.

This example does not have a Cell object to represent the glue destination. If it did, the statement would be:

celObj1.GlueTo celObj2


You can glue to the same items you can in Visio, such as a vertex (Geometry1.Xn or Geometry1.Yn).

If you want to glue to a selection handle, use AlignLeft, AlignRight, AlignTop, or AlignBottom to indicate the selection handle in the center of that edge of the shape (rather than the whole edge). AlignCenter and AlignMiddle both glue to the center of the shape.

Internally, a guide is a shape, like anything else on the page. To create a guide you use the AddGuide method of a Page object. If you check a guide’s Type property, it returns visTypeGuide. If you want to glue to a guide, use GuidePosX for a horizontal guide or GuidePosY for a vertical one. For a guide point, you can use either.

GlueToPos takes slightly different arguments: a Shape object and the X,Y coordinates of a position relative to the shape’s width-height box. In this case, 0.5, 0.5 indicate the center of the shape’s width and height.

AutoConnect Mode


The AutoConnect feature added can be accessed programmatically as well as through the user interface. Use Shape.AutoConnect to simulate Auto-Connect.
Try It!

Below are two procedures for connecting a dynamic connector between two shapes. To run the code open the file Autoconnect.vsd from the Samples folder for the class. Execute the macros and see that the connector is placed and glued.

Sub ConnectWithGlue()

'Glue a connector between two shapes using the GlueTo method

Dim vsoConnector As Visio.Shape

'Drop the connector shape

Set vsoConnector = Application.ActivePage.Drop _

(Application.ActiveDocument.Masters.ItemU _

("Dynamic connector"), 0#, 0#)

Dim vsoCellFrom As Visio.Cell

Dim vsoCellTo As Visio.Cell

'Get references for the cell to glue from and the cell to glue to for

'the beginning of the connector

Set vsoCellFrom = vsoConnector.CellsU("BeginX")

Set vsoCellTo = _

Application.ActivePage.Shapes.ItemFromID(1).CellsSRC(7, 2, 0)

'Glue the cells

vsoCellFrom.GlueTo vsoCellTo

'Get references for the cell to glue from and the cell to glue to for 'the end of the connector

Set vsoCellFrom = vsoConnector.CellsU("EndX")

Set vsoCellTo = _

Application.ActivePage.Shapes.ItemFromID(2).CellsSRC(7, 2, 0)

'Glue the cells

vsoCellFrom.GlueTo vsoCellTo

End Sub
Sub ConnectWithAutoConnect()

'Glue a connector between two shapes using Autoconnect

Dim vsoShapeFrom As Visio.Shape

Dim vsoShapeTo As Visio.Shape

'Get a reference to the two shapes

Set vsoShapeFrom = Application.ActivePage.Shapes.Item("Process")

Set vsoShapeTo = Application.ActivePage.Shapes.Item("Decision")

vsoShapeFrom.Autoconnect vsoShapeTo, visAutoConnectDirNone

End Sub


A quick inspection reveals that it takes about one-half the lines of code to glue shapes using Autoconnect. The reason for this is that the Autoconnect method hooks up both ends of the connector with one command. Using the GlueTo method requires that each end of the connector be handled separately.

Enable/Disable Autoconnect


Autoconnect can be enabled or disabled within the File > Options > Advanced tab. When Autoconnect is turned on, hovering the mouse over a shape presents the four autoconnect triangles around the shape. Clicking on one of these triangles connects the shape to another shape. When autoconnect is turned off these triangles do not appear.

Below are VBA code snippets for turning autoconnect on or off.

Sub EnableAutoConnect()

Application.Settings.EnableAutoConnect = True

End Sub

Sub DisableAutoConnect()



Application.Settings.EnableAutoConnect = False

End Sub


Note:

It is not necessary for EnableAutoConnect to be set to True in order to use the Autoconnect method through automation.


Download 0.78 Mb.

Share with your friends:
1   ...   9   10   11   12   13   14   15   16   ...   36




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

    Main page