Visio VBA puzzle - can this be solved with VBA?

Started by Visisthebest, November 01, 2022, 05:22:23 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Visisthebest

To make a Visio chemical process Process Flow Diagram, I took an existing diagram and recreated it in Visio. I did this by putting an image of the diagram in Visio then overlaying it with a white rectangle with some transparency, so I had a greyish background of the original diagram image.

I recreated the diagram in Visio but unfortunately while doing this I seemed to have copied the diagram and now it is doubled behind the white rectangle.

There are a few hundred shapes in the diagram, how can i with VBA:

- Do a multiselect of only the visible shapes
- Delete all shapes behind the white rectangle

I looked for a Z order indicated for shapes in Visio VBA but I cannot find any way to solve this programmatically.

Multi-selecting then copying the visible shapes is just a nightmare many are small lines, a regular multi-select also selects the invisible shapes behind the rectangle unfortunately.

Quite the Visio VBA puzzle, anyone know any solution much appreciated! :D
Visio 2021 Professional

Surrogate

Did you try Spatial methods? There is flags for check shapes relations...

Nikolay

#2
If I understand your question correctly. Z-index is defined in Visio simply by the shape order.
Meaning, you can probably delete just everything that is preceding the rectangle



CODE:

For Each s In ActivePage.Shapes
    Debug.Print s.Text
Next


OUTPUT:

22
1
0 <<<<<<<<<<
11
2


Or if it is just 100 shapes, I would go ahead and delete manually ;D
You can use Drawing Explorer window to help you with that

wapperdude

There is no direct shapesheet function.  Spatial neighborhood should work via vba.  There is work around, see http://visguy.com/vgforum/index.php?topic=8428.0.  Basically, use manual intervention, select top shape and send to back.  Then select new top shape and delete.   Codewise, loop thru shapes, select 1 shape, spatialnbr to see what's below it & then decide to keep or delete.

In future, put the the original diagram on a background page.  That will isolate it.

Visio 2019 Pro

wapperdude

You might modify Nikolay's code to print shape id, PinX, and PinY.
That will give position of all shapes, and from that, find overlapping shapes.  Then, in the DEW, delete one of the overlappers.  With more modification, program could select a shape, loop thru rest, delete what's underneath.... Hmmmm.  Sounds like a spatial neighborhood...which allows tolerances. You know, close enough!
Visio 2019 Pro

Visisthebest

#5
Thank you everyone for the ideas I was not aware of the possibilities that spatial relations offer for identifying and touching shapes and some clever workarounds to use these.

-> actually spatial relations can be very handy when importing an SVG then reading through an SVG diagram. The shapes imported from a SVG file are not grouped and/or connected/glued (as they are in Visio) in the SVG's I've come across but spatial relations allow me to identify shapes that belong in a group! (and just as important, connection lines between shapes and what is connected to what)

Nikolay's solution is the most straightforward one in this case, that the order of the shapes in the Page's shape collection says something about the Z order of the shapes is also I was also not aware of!

I thought the order of the page's shape collection was about the order in which they were added or something, had no idea this order contains info about the Z order of the shapes.
Visio 2021 Professional

wapperdude

Z-order is initially related to the order in which a shape is added to the page.  The most recent at the top and the first at the bottom.  The newest is always added at top.  But subsequentLy, all ordering can change pending user assignment.  It ought to be pointed out, no two shapes can have identical z-order.
Visio 2019 Pro