![]() |
![]() ![]() |
|
Whar Did I Land? A wee exposé on Visios "HitTest" Functionality Ceuid Mile Failte an welcome back again. In
this article I'll discuss a new bit of functionality which came about with Visio 4.5 that
can work minor miracles for ye. This is the "HitTest" function available through
VBA. Now I suppose I should begin this by describing just what the HitTest function
is and what it can do for you. The
HitTest function is a Visio API call, which can test any shape that has been dropped,
either by dragging from a stencil, copied, or simply moved. This test will return an
integer, which tells you if the shape has been dropped; The target can be either a single shape, or can be analyzed
within a loop to look at a collection of shapes. Why then, would you need this functionality? Weel lads
an lassies
. Heres but a few of the potential uses for this function: These, and many more examples, are but a few of the reasons
why this technology is both helpful and important. So lets, then, have a wee look at
the function itself; its syntax and its required arguments:
Where: Object is the given target shape being tested X_test_position is the x ordinate of the location in Page Units being analysed Y_test_position is the y abscissa of the location in Page Units being analysed Tolerance is the distance away from the target x,y coordinate in internal units for a cushion IntegerReturnValue is an integer evaluating to one of the 3 following possibilities: visHitOutside (0), visHitOnBoundary (1) or visHitInside (2) Note that the object is an obtainable shape or group shape that has been dropped, copied or moved. The x, y and tolerance values are real numbers expressed in Visios internal units (inches). Note, also, that the object is a single object, not a collection. To effectively use this functionality, you will need to test against all potential objects that could be at the given x and y coordinate specified. It is therefore quite easy to test against all of the shapes in a given drawing. You simply obtain the drawings Shapes collection: Set AllShapes = Visio.ActivePage.Shapes and obtain the count property:
and then loop through the shapes obtaining the results of each test until you arrive at the desired result, taking action at that time. The code would look something like the following:
Now here ye at least see hoo it is implemented. Howere there are several things less than optimal in the above code. Firstly, this example tests against every single one o the wee shapes that live on the Documents page. This could be painstakingly slow if there weare several thousand shapes on the page. Secondly, because it tests against ALL shapes on the page, it tests against itsel. This will return a "False Positive" in the testing. I assume tha ye dinnae want this tae happen. It is akin to the home fertility test showin a wee plus sign. Ye dinnae want it to happen unless ye truly are expectin it, aye? Weel we can take steps to insure that the test shape itself is eliminated from the selection as follows: Select Case IntRetVal Case 0 MessageText = "Outside the Target Shape." Case 1 MessageText = "On the Boundary of the Target Shape." Case 2 MessageText = "In Filled Area of the Target Shape." End Select MsgBox "You have just Landed " & MessageText End If end the If test Next Counter End Sub Now as tae the issue of having to test against all o the shapes in a given Documents page It is best to build either a collection or an array of the shapes that you wish to test against, and THEN run the hit test against THAT selection/collection. This will greatly diminish the number of iterations through the loop and truly enhance the speed and functionality of your solution. One method of making this possible is to add a user-defined cell to each of the shapes that you wish to be members of the target selection/collection and then on Document Open build the selection/collection one time and use this public selection/collection as your test criteria loop. The code fragment below illustrates one methodology for implementing this: If iFoundShapes > 0 Then ReDim Preserve TLShapes(1 To iFoundShapes) MsgBox "Retrieved " & UBound(TLShapes) & " Shapes" End Sub So armed with this wee bit o knowledge, you should be able to set up a HitTest of your own and find numerous uses for this functionality. Keep tae mind that you are not going to be simply displaying message boxes, but rather, you will be using the properly located shape to potentially drill doon tae other cells within the target shapes shapesheet and pass that information off tae the subjects shapesheet in the form of Custom Property Cells, User-Defined Cells, potentially even changes in Geometry or formatting. As aways enjoy the process of making your shapes and solutions mir an mir powerfu. "Haste ye back." Dave "The Auld Scotsman" Edson
|
|
|
Copyright © 1998-2007 DBM & others | Disclaimer | Privacy | Re-publication | Trademarks | Webmaster | Home |