### Author Topic: VISIO CHALLENGE: Design Recursive Algorithm  (Read 8367 times)

0 Members and 1 Guest are viewing this topic.

#### wapperdude

• Global Moderator
• Hero Member
• Posts: 4837
• Ideas Visio-lized into solutions
##### VISIO CHALLENGE: Design Recursive Algorithm
« on: June 19, 2016, 06:14:43 PM »
Hello Visio Fans!

Welcome to the first Visio challenge.  The goal:  develop a recursive algorithm to search thru ALL shapes on a page, and list them.  Debug.print is fine.  The code must search the entire page and find all shapes, even though they may be part of a group, or even a nested group.  The program must be well-behaved.  Obviously, endless looping is not well behaved.  Nor is loop aborting due to error.  It should also be compatible with V2007 and older.

Let's see how creative you can be.

I've attached a file that has a bunch of shapes, grouped and otherwise.  It would make a good common basis for code development.

Yes.  I do have a solution.  But, being a non-programmer, it may lack a certain amount of refinement.  Does seem to be well-behaved, though.

Have fun!
Wapperdude
« Last Edit: June 20, 2016, 12:38:38 AM by wapperdude »
Visio 2019 Pro

#### Surrogate

• Hero Member
• Posts: 1803
##### Re: VISIO CHALLENGE: Design Recursive Algorithm
« Reply #1 on: June 20, 2016, 01:28:20 AM »
I just modify code from post
Code
``Sub Main()   ShapesList ActivePage.ShapesEnd SubSub ShapesList(ByVal shps As Shapes)   Dim sh As Shape   For Each sh In shps       If sh.Shapes.Count <> 0 Then ShapesList sh.Shapes       Debug.Print sh.ID, sh.Name   Next shEnd Sub``

#### Yacine

• Hero Member
• Posts: 3208
##### Re: VISIO CHALLENGE: Design Recursive Algorithm
« Reply #2 on: June 20, 2016, 06:26:32 AM »
Yacine

#### wapperdude

• Global Moderator
• Hero Member
• Posts: 4837
• Ideas Visio-lized into solutions
##### Re: VISIO CHALLENGE: Design Recursive Algorithm
« Reply #3 on: June 20, 2016, 09:19:27 AM »
@ Yacine:  Well, I did forget about that topic.  I will look at  again, and Surrogate's response.  But, technically, your code doesn't do the listing.  😵  ...  😇

Hey!  It's my post, I can make the rules as I want.  😱  😕  😠  😉  Just call me .... uh ... Mr. Grumpy.  Ha!

LOL

Grumpy (aka Wapperdude)

Visio 2019 Pro

#### Yacine

• Hero Member
• Posts: 3208
##### Re: VISIO CHALLENGE: Design Recursive Algorithm
« Reply #4 on: June 20, 2016, 10:05:41 AM »
As you wish, Master of the Post.
Yacine

#### wapperdude

• Global Moderator
• Hero Member
• Posts: 4837
• Ideas Visio-lized into solutions
##### Re: VISIO CHALLENGE: Design Recursive Algorithm
« Reply #5 on: June 20, 2016, 12:17:47 PM »
As promised, I looked at both responses.  Both did the job of finding shapes.  The Surrogate code provides a slightly more complete listing as it finds all shapes.  To be fair, Yacine code was quasi-pseudocode, and as such, may have some limitations as a result of my editing. I know, Bad dog.  Bad dog.  No dog yummies for me!

If so, my apologies, Yacine.

While both work well, my unstated intention, was to exclude "group" shapes, as, while they do have shape ID's, e.g., sheet.979, technically, they aren't shapes, but rather a collection of shapes.  So, I am including an updated file, which has both codes included.  If either Surrogate, or Yacine, or anyone else, would like to update, contribute to this post with code that excludes the group shapes, that would still be of interest.  If not, I will update the post a little later, with code I developed that does exclude the groups.

Note, there are no prizes.  Simple searching thru shapes doesn't find all shapes, and requires a recursive algorithm to push down into the groups.  Such code is not easily found on this forum or via web searching.  So, this post was really about readily finding such code.

Thanks to both Surrogate and Yacine.

Wapperdude
Visio 2019 Pro

#### Yacine

• Hero Member
• Posts: 3208
##### Re: VISIO CHALLENGE: Design Recursive Algorithm
« Reply #6 on: June 20, 2016, 12:57:09 PM »
NO PRIZES!?!
Yacine

#### wapperdude

• Global Moderator
• Hero Member
• Posts: 4837
• Ideas Visio-lized into solutions
##### Re: VISIO CHALLENGE: Design Recursive Algorithm
« Reply #7 on: June 20, 2016, 01:10:55 PM »
Yeah.  I thought about a Ferrari, but, decided that would be inappropriate.  Not keeping with the Green Earth ideals.
Visio 2019 Pro

#### wapperdude

• Global Moderator
• Hero Member
• Posts: 4837
• Ideas Visio-lized into solutions
##### Re: VISIO CHALLENGE: Design Recursive Algorithm
« Reply #8 on: June 20, 2016, 01:39:09 PM »
Well, I decided to try some simple changes to both sets of code.  Result, both list only the "actual" shapes, ignore the "group shapes".

Change was simple.  I like both solutions better than my own, so, here is updated file with updated code.  My code not included.

Since, I modified the code, and both give the same results, I declare this contest over, a tie, with an assist from me.  Should a prize become available, it will be donated to yet unspecified worthy cause!

wapperdude

Surrogate based code:
Code
``Sub Main()' Code provided by Surrogate    ShapesList ActivePage.ShapesEnd SubSub ShapesList(ByVal shps As Shapes)    Dim sh As Shape    For Each sh In shps        If sh.Shapes.Count = 0 Then            Debug.Print sh.ID, sh.Name        End If        ShapesList sh.Shapes    Next shEnd Sub``

Yacine based code:  (could be cleaner by tiding up some of my edits)
Code
``Sub MainLoop()' Code created by Yacine, updated by Wapperdude    Dim shp As Visio.Shape        For Each shp In ActivePage.Shapes 'note the difference to activewindow        If shp.Shapes.Count = 0 Then            Debug.Print shp.Name        End If                actualProcessingAndDeeperLoop shp    Next shpEnd SubSub actualProcessingAndDeeperLoop(shp As Visio.Shape) 'this one is recursive    Dim subShp As Visio.Shape    Dim subShps As Visio.Shapes    Set subShps = shp.Shapes    For Each subShp In subShps        If subShp.Shapes.Count = 0 Then            Debug.Print subShp.Name        End If        actualProcessingAndDeeperLoop subShp    Next subShpEnd Sub``
« Last Edit: June 20, 2016, 01:50:12 PM by wapperdude »
Visio 2019 Pro

#### Yacine

• Hero Member
• Posts: 3208
##### Re: VISIO CHALLENGE: Design Recursive Algorithm
« Reply #9 on: June 20, 2016, 02:23:10 PM »
Surrogate's is shorter as it passes a "shapes" instead of a "shape" object.
He deserves the Ferrari. May be he can give me a ride.
Yacine

#### wapperdude

• Global Moderator
• Hero Member
• Posts: 4837
• Ideas Visio-lized into solutions
##### Re: VISIO CHALLENGE: Design Recursive Algorithm
« Reply #10 on: June 20, 2016, 02:31:42 PM »
This was interesting for me.. better understanding of recursive loops, calling subs, and passing arguments.

Wapperdude
Visio 2019 Pro

#### Surrogate

• Hero Member
• Posts: 1803
##### Re: VISIO CHALLENGE: Design Recursive Algorithm
« Reply #11 on: June 21, 2016, 03:19:44 AM »
No, it is not mine code.
Scooter is not mine, I just posted an ANNOUNCEMENT!

That code posted in russian visio forum by user 9rey. I find that code there

UPDATE: i shared that code early
Thanks to 9rey for recursion macro
« Last Edit: February 05, 2022, 01:51:26 AM by Surrogate »

#### wapperdude

• Global Moderator
• Hero Member
• Posts: 4837
• Ideas Visio-lized into solutions
##### Re: VISIO CHALLENGE: Design Recursive Algorithm
« Reply #12 on: June 21, 2016, 09:00:46 AM »
Guess I'm off the hook for  Ferrari.  ...  That's!  😁

Wapperdude
Visio 2019 Pro