Author Topic: Visio VBA puzzle - can this be solved with VBA?  (Read 593 times)

0 Members and 1 Guest are viewing this topic.

Visisthebest

  • Hero Member
  • *****
  • Posts: 663
Visio VBA puzzle - can this be solved with VBA?
« on: November 01, 2022, 12:22:23 PM »
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

  • Hero Member
  • *****
  • Posts: 1636
    • ShapeSheet™ Knowledge Base
Re: Visio VBA puzzle - can this be solved with VBA?
« Reply #1 on: November 01, 2022, 02:49:13 PM »
Did you try Spatial methods? There is flags for check shapes relations...

Nikolay

  • Hero Member
  • *****
  • Posts: 1136
    • UnmanagedVisio
Re: Visio VBA puzzle - can this be solved with VBA?
« Reply #2 on: November 01, 2022, 04:19:48 PM »
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:
Code
For Each s In ActivePage.Shapes
    Debug.Print s.Text
Next

OUTPUT:
Code
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
« Last Edit: November 01, 2022, 04:26:55 PM by Nikolay »

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4477
  • Ideas Visio-lized into solutions
Re: Visio VBA puzzle - can this be solved with VBA?
« Reply #3 on: November 01, 2022, 05:20:11 PM »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4477
  • Ideas Visio-lized into solutions
Re: Visio VBA puzzle - can this be solved with VBA?
« Reply #4 on: November 01, 2022, 05:31:12 PM »
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

  • Hero Member
  • *****
  • Posts: 663
Re: Visio VBA puzzle - can this be solved with VBA?
« Reply #5 on: November 02, 2022, 07:57:46 AM »
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.
« Last Edit: November 02, 2022, 09:52:11 AM by Visisthebest »
Visio 2021 Professional

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4477
  • Ideas Visio-lized into solutions
Re: Visio VBA puzzle - can this be solved with VBA?
« Reply #6 on: November 02, 2022, 12:58:01 PM »
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