Another interesting question in the newsgroups from a David Kelley recently set me athinking! So, I thought I’d share my response here.
First, the question:
“Programmatically, after I drop a new shape on the page, how can I then move into the "editing mode" tool to use the pencil (to allow moving the control points on a polygon). And once this is done (and the user has moved all the points to be where they should) how can I detect that every point has been edited/moved so that I can then automatically lock the polygon shape into place and prevent further movement (I understand the lock, just not how to detect the event)? My general need is to allow the user to refine the corner points to match an underlaying image before (in the next step) I do some math on the final position.”
I thought the first part, getting the Pencil Edit mode to become active, was going to be easy, but it turns out not so… Every shape has an EventDrop cell, in which you can add ShapeSheet formula. So, I initially entered =DOCMD(1221) because 1221 is the value of Visio.VisUICmds.visCmdDRLineTool. Therefore, my theory was, that this would change the mode to Pencil Edit whenever a copy of the shape was dropped. Not so. It never changes out of Pointer mode. However, if I wrapped the command in a method that can be called from CALLTHIS() then it does work!
I drew a simple rectangle, and added it as a new Master in my local document stencil, then renamed it as Mask. I then edited the Master shape, opened the ShapeSheet of the rectnagle, and entered the formula =CALLTHIS(“PencilEdit”) into the EventDrop cell, and added the PencilEdit method to a module in the documents VBA project:
Okay, so now Visio automatically enters Pencil Edit mode when the Master is dropped on the page or copied.
Now, the second bit … how to detect if each vertex has been edited. In the following screenshot, you can see that the PinX and PinY values, of the shape instance of my Mask Master, are blue, but all the rest are black. This is because they are the only values that are not inherited from the Master shape.
If I change the size of the shape by dragging a corner, then you can see that the Width and Height values also become blue because they are no longer inherited:
If I were to move a vertex within the rectangle, then you can see that two of the LineTo rows in the Geometry 1 section become local, ie not inherited, and turn blue.
If I were to move a vertex outside of the original rectangle, then all of the rows become local.
All of the points in a Visio ShapeSheet are measured relative to the bottom left corner of the enclosing rectangle. In this case, the width and height of the shape has also changed, thus the formulae in the X and Y cells have to change, even if they need to maintain their absolute position, because they are relative to the Width or Height values. For example, the second row had the formula Width*1 originally, when the Width was equal to 30 mm. Now that the width is 35 mm, the formula has changed to Width*0.8571, so that the value can stay at 30 mm. In other words, the vertex in the bottom right corner of the shape has not moved, but its formula has had to be updated just to stay where it is.
If I were to add (or delete) a vertex into the geometry, then again, all of the Geometry 1 section values become local.
Therefore, it is very difficult to detect if every vertex has been adjusted since it was originally dropped on to the page. In practice, I would probably position the bottom left corner over a required vertex position, and then alter all of the other vertices to suit. Even adding or deleting vertices as required. Consequently, row 1 may never be adjusted at all.
David wants to detect vertex changes, but the nearest event in the ShapeSheet is the EventXFMod cell, so I added a CALLTHIS(“CheckEdit”) formula in there. This will fire whenever the Width, Height, PinX or PinY changes. It will not fire if a vertex movement does not cause a resizing of the shape.
The CheckEdit method will loop through all of the vertices in the first geometry section, and check if the vertex is in a different position to the same vertex in the master shape. If all of the vertices have been moved relative to the previous vertex, then Visio is put back into Pointer Tool mode, and the vertices on the shape are locked from further editing.
Hopefully, this will answer the original question!
Looking forward to the launch of Visio 2010!