Visio - Getting shape's in a layer

Asked By jarle on 04-Aug-08 07:13 AM
Hey
I'm building a Visio diagram that's mostly handled through macros. Most of
the features is simulated by making layers in-/visible. But as the diagram
gets larger the performance drops drastically, and one of the problems is
that I can't seem to get a list of all shapes in a layer, so what I do now is
to look at ALL shapes in the diagram, and go through all of the layers they
are part of, to check if they are part of the layer I'm working with. Doing
this takes a while, even with loops in a macro, isn't there any way to do
this faster?


Paul Herber replied on 04-Aug-08 11:59 AM
On Mon, 4 Aug 2008 04:13:00 -0700, jarlen


That is the normal, correct way to do it,, however, iterating through
the shapes shouldn't take too long, even with hundreds of shapes on a
page.

Another way would be to make all layers invisble except for the
required layer(s), then select all shapes and iterate through the
selection.

Individual macros have an overhead for Undo and setting up document
references so re-writing the macros might be worth while.


--
Regards, Paul Herber, Sandrila Ltd.
Electrical for Visio          http://www.electrical.sandrila.co.uk/
sbmack replied on 05-Aug-08 07:00 AM
On Aug 4, 11:59=A0am, Paul Herber
of
am
s
ow is
hey
ing
o
.uk/

jarlen,

To add to what Paul has said, most Visio novices make the assumption
that a Layer is a "container" of Shapes.  In Shape selection using VB,
a Layer is not considered a container.  Rather more like a property of
the Shape.  So you can't cycle through a Layer with For Each loops
like you would expect to.  However a Group is a Shape that contains
other Shapes.  So you can cycle through a Group using For Each and
operate on every Shape member of the Group.  So you could create a
Group with the same Shape objects as layer to do what you want.  The
guys here have more efficient ways of doing things.  But understanding
Layers is the point I want to make here.

BTW, I was a novice caught up in Layer confusion until guys like Paul
set me straight.

SteveM
jarle replied on 05-Aug-08 03:19 AM
Ok, it seems to me like it takes a while, I have a few hundred shapes.
I got about 100 different boxes and stuff, that I'm showing all of the time,
what I'm switching on/off is their connectors , and datagraphics for both
connectors, and all the other shapes.

I'm not sure what you mean by this:

Are you talking about references to objects in my diagram, like shapes and
layers?
In the beginning of the function I save the reference for the layer I'm
working with, and I then save the individual shapes and the layers they are
part of, while iterating through it all (only on shape and one layer saved at
the time), I'm not sure I can put down the reference count any.
sbmack replied on 08-Aug-08 11:39 PM
me,
d
re
d at
t of
gram
is
now is
they
Doing
do
co.uk/

I've been away from Visio for a while.  But I was able to dig out a
couple of examples for you.  Here's one in which the Shapes associated
with a Layer are Selected:

Sub ShapesInLayer()
Dim vShp As Shape
Dim vsoSelection1 As Visio.Selection
'  RedShapes is the name of th e Layer
Set vsoSelection1 =3D
Application.ActiveWindow.Page.CreateSelection(visSelTypeByLayer,
visSelModeSkipSuper, "RedShapes")
For Each vShp In vsoSelection1
vShp.Name =3D vShp.CellsSRC(Visio.visSectionProp, 0,
visCustPropsValue)
Debug.Print vShp.Name & "  " & vShp.NameU
Next
End Sub

In the above case the technique is to use a Selection as the Shape

Here's an example of a Group being a Shape that is also a container of
Shapes:

For Each vShape In grpShape.Shapes
shapeNum =3D vShape.Name
vShape.CellsSRC(Visio.visSectionUser, 2, visUserValue) =3D
modelVars(shapeNum).Activity
vShape.DataGraphic =3D ActiveDocument.Masters("DG_IsBasic")
Next

A lot of good sample code can be found here:

http://visio.mvps.org/VBA.htm

Note that at that site, there are 2 examples of selecting Shapes in a
Layer.  The first one doesn't work properly.

Also note that a DataGraphic is NOT explicitly assigned to a Layer
with its parent Shape.  So if you cycle through a Layer to hide the
Shapes, the DataGraphic   Objects will remain on the screen. So when
you cycle through the Shapes, you must also set the Shape DataGraphic
to Nothing.

Lastly, the are many ways to do things in Visio.  Different guys have
different methods.  Given that the Visio Help System stinks, I think
most guys do trial and error until they got something that works and
then stick with it.

'SteveM
John... Visio MVP replied on 05-Aug-08 08:49 AM
Interesting site, but that guy needs to get his act together, the website is
long overdo for an update.  ;-)

So were you able to determine why the second example does not work properly?

John... Visio MVP
http://Visio.MVPS.org
jarle replied on 05-Aug-08 09:34 AM
sbmack > Your first example is just what I need. As per your note I need the
just the shapes themselves, I'd just hide/show the layer. Thanks for the
example, it seems to work fine.
I've looked at the site you linked to, but I'm not too happy about it. Like
you say, the first of his examples describing my problem doesn't work,
because he selects all the shapes on the page containing the layer. Not just
the shapes in the layer itself. :-)
sbmack replied on 08-Aug-08 11:39 PM
is
ly?

John,

That was a while ago.  But I think the For Each code acted on Shapes
that were not part of the Layer.  Rather than do a diagnosis, I just
used a different strategy.

SteveM