Visio Guy

Solution-specific Visio Discussions => Flowcharting => Topic started by: o4sythe on February 19, 2015, 12:35:49 PM

Title: Counting all connections leading to a shape
Post by: o4sythe on February 19, 2015, 12:35:49 PM
Hello all,

New user of visio, I've spent the last hour looking for a solution via Google and this forum and no luck!

I'm looking for a way to count the number of connections that have lead to a particular shape so that I can use this to weight the importance of each process within a flow chart.

For example, if the shape labelled 'Negotiation' has three incoming connections, each of which have one incoming connection of their own, I would want to be told that the 'Negotiation' shape has six connections leading to it - any ideas?

Thanks in advance,
Oli
Title: Re: Counting all connections leading to a shape
Post by: wapperdude on February 19, 2015, 04:07:38 PM
Hi Oli...

It's only necessary to place a post once.  The categories are just vague, collections.  Most everyone just looks at the current posts or uses the search feature as there are so many posts to sort through.

As to your problem, it will take a macro or code.  All connection information is "contained" not in the actual shape but in the connectors attached to the shape.  It could be either a connector or some other 1-D line variant.  In addition, since you really want to explore the number of branches (or roots), connected to a shape, and the number of sub-levels is undefined and can vary, the algorithm needs to be "recursive", I think.

One basic attack would be to select a shape, then loop thru the page connections collection, identify which connectors are attached to that shape, and store both the connector ID, the connector end, and the total number of connections made.  Then, for each attached connector, go thru and find what shapes are attached to the opposite end, store those.  Select each opposite end shape and repeat the process, except now, the total count ought to be reduced by one for each sub-level shape, as it's "lead-in" connection has been previous counted.  The process re-iterates until shapes with only one connection are found, presumably, these are the terminal shapes

Hope this helps.  It will take some programming skills.

Wapperdude
Title: Re: Counting all connections leading to a shape
Post by: o4sythe on February 20, 2015, 10:38:50 AM
Thanks for the guidance  wapperdude :-)

That sounds somewhat over my ability at this stage but good to know all the same - Definitely something I can come back to when my programming skills have been improved.
Title: Re: Counting all connections leading to a shape
Post by: Thomas Winkel on February 20, 2015, 02:25:27 PM
Hi,

with Visio >= 2010 this is easy, see attached document.

Regards,
Thomas
Title: Re: Counting all connections leading to a shape
Post by: Croc on February 20, 2015, 02:54:13 PM
In Visio 2007 it is more difficult :)
https://www.dropbox.com/sh/ukbtvo3n2zh8o72/AAAWno-D3MdTtUs-YF2zEqUta?dl=0
Title: Re: Counting all connections leading to a shape
Post by: Yacine on February 20, 2015, 08:54:45 PM
How unfair. Password protect macros? What's it worth? Just show that it can be made?
Title: Re: Counting all connections leading to a shape
Post by: Croc on February 21, 2015, 05:23:17 AM
The code in the .vss contains a lot of garbage. This may prevent an inexperienced user. I just cut it out of the unfinished work and did not clean.
Stencil can be used to counting the connectors, as a "black box".
Password = rbeat
---------------------------------------------
P.S.
Perhaps is needed to explain, that the second parameter in the line
Set c = ConnServ.stnModule.GetTree(ShpNameID, 2)
specified the direction of connectors.
1 - incoming
2 - outgoing
3 - all
Title: Re: Counting all connections leading to a shape
Post by: wapperdude on February 21, 2015, 08:36:03 PM
Here's an alternative approach.  It doesn't care about direction of connectors, or even position with in a tree.  It reports all connections in the tree.  It's a bit of a cheat, because, it "tags" each shape and connector with a color, keeps count, ignores redundancies (which the connects object always provides), and then does a "bail out" to exit the macro. 

It does use a redim dynamic array to store shapes.  Could have done same for connectors, but, I thought there might be some merit to the coloring.  Unfortunately, I could not determine a way to dynamically loop thru the analysis, so, punched out of the loop at the end.

There could be some simplification on the redundancy testing, but execution for now is fast.

The non-programmer strikes again!   :o

Wapperdude
Title: Re: Counting all connections leading to a shape
Post by: wapperdude on February 22, 2015, 01:19:50 AM
OK.  Fixed the loop issue.  Enhanced the color feature...now the 1st level connections are different from the indirect connections.

Wapperdude
Title: Re: Counting all connections leading to a shape
Post by: o4sythe on February 22, 2015, 11:55:08 AM
These are really interesting, just spent an hour going through the code inch by inch and Just beginning to get my head around it. I'm super impressed by the extent of the effort you're all putting in to helping me, thank you all very much!

wapperdude, your solution seems closest to what I'm looking for - The two issues I'm still having is that that I need only incoming connections leading to the shape to be counted, also I've so far been unable to replicate the results within my existing Visio 2013 flow chart.

Any ideas?

Thanks again!
Title: Re: Counting all connections leading to a shape
Post by: wapperdude on February 22, 2015, 05:30:44 PM
I don't have V2013. My understanding is the V2013 has VBA features that are supposed to make incoming / outgoing determination easier.

In your drawing, what measures (if any) do you take to show (establish) if a connection is incoming or outgoing?  If it is merely by the direction of the connector arrowhead, assuming that the arrowhead is always on the "end" and never the "beginning", once you've identified a connector, you can check keep only connectors that end on your shape.  But, that's a little involved unless V2013 really does simplify things.

Not sure why it doesn't work in V2013.  Don't think there's any thing unique to the code.  Did you copy the code to your drawing, or the drawing to my file?  I'd suggest copying the code from my file to your file.  Then, tile your drawing window and the VB window.  You can always step thru the code, (F8), and execute line by line.  You can watch what each step does in the drawing, and also see where the code breaks.

There's probably better debug methods, but this works for me.

Wapperdude
Title: Re: Counting all connections leading to a shape
Post by: wapperdude on February 23, 2015, 03:01:26 AM
OK.  That wasn't so bad! 

Here's updated file and code.  There is a variable "m", near the beginning, that is hard coded and sets the incoming / outgoing connector type recognition.  This needs to be set manually.

Wapperdude
Title: Re: Counting all connections leading to a shape
Post by: wapperdude on February 23, 2015, 06:29:10 AM
BTW, in V2007, I tried this with basic shapes, flow charts, cross-functional charts, org charts...worked every time.  Tried using connectors or line shapes...no problem.  Doesn't like have two shapes directly glued together...expected...but, I did not put in a test condition for that case.

So, if your V2013 drawing still fails, either someone else with V2013 will have to come the rescue, or, try saving it as V2007, and I can try and find the problem.

But, the code is very generic, should run fine.

Wapperdude
Title: Re: Counting all connections leading to a shape
Post by: o4sythe on February 24, 2015, 12:54:41 PM
wapperdude you are fantastic! This works exactly as I wanted - I had to adjust my diagram slightly as it didn't like counting two connections that originated in the same shape (my start shape goes to two decisions and in a few cases the processes lower down the line connect back to both of these decisions)

That was easily fixed however, I've simply disconnected the start shape from the rest, it was essentially just a title anyway.
Title: Re: Counting all connections leading to a shape
Post by: o4sythe on February 24, 2015, 01:14:59 PM
It seems I may have spoken a bit too early. You're still pretty fantastic though :-D

I've found there are some unavoidable occasions where one shape has connections to two other shapes when then both link to the same shape on the next tier down. i.e. A links to B and C, B and C both link to D. If I run the macro on D I should get "4" but instead I see:

Run-time error '91':
Object variable or With block variable not set

I'm trying to get my head around the specifics...I think that the macro is attempting to change the colour of 'A' twice, so would the solution be to somehow detect if 'A' is already blue (if blue is the right colour!) and if yes then skip the colour change command.

Very interested to know if I'm on the right lines or not!
Title: Re: Counting all connections leading to a shape
Post by: wapperdude on February 24, 2015, 04:26:47 PM
The macro checks the color of both the connectors and the shapes.  It does this to avoid entering a shape more than once into the shape array, and to avoid multiple counting of any connector.  As such, closed loops will create an error,  some or every (???) time. 

The way the macro works is
  1. Start with selected shape, look at every connector
  2. Only examine connectors that meet the direction requirement, i.e., "m".
  3. Check the connector to see if it's been used before, if so, skip.
  4. Check the opposite end of the connector.
       A. If shape is the originating shape, ignore.  The connects connection always includes the originating shape.
       B. Check to see if the shape has already been used, i.e., its color, if so, ignore
       C. Everything OK, increment "j", the shape array index,redim the array, add new shape.
       D. Increment the connector count by one

Macro loops thru every connector, counts each new connector, adds shape to shape array, then loops thru the shape array until all shapes in the array, "k", have been checked.  It terminates when "k" exceeds "j".  A closed loop throws off the, "j", "k" relationship.

It's a simple macro, not anticipating existence of loops.  Some special conditional requirements are needed...
   1. Allow original shape, possibly
   2. Allow double counting of shape, but not re-add to the array, otherwise, end up with endless, repeating loop.

I would recommend making a simple, 3 shape loop, step thru program, see where/why it breaks, then establish some conditional test to allow that scenario.

Since I'm not in front of my computer, the order of the steps may not be quite right....disclaimer!!! 
Title: Re: Counting all connections leading to a shape
Post by: wapperdude on February 24, 2015, 05:20:37 PM
Well, the closed loop condition turned out to be simple change. So, the code should run, it did for my quick test, attached.  Also, doesn't fail for connectors with less than 2 connections.

Sheet.7 is the shape that would initiate closed loop response.

Wapperdude
Title: Re: Counting all connections leading to a shape
Post by: o4sythe on February 25, 2015, 03:22:43 PM
Perfect. Enough said!

Thank you very much wapperdude, you're such a great help :-)
Title: Re: Counting all connections leading to a shape
Post by: wapperdude on March 05, 2015, 01:48:27 AM
Probably a closed topic, but, there was one feature I didn't like...having to edit the macro to change the connection type from incoming or outgoing.  So, here's an update that pops up a non-modal  UserForm.  The form reminds you to select a shape, which can be done without having to close the form.  It also gives you 3 direction options:  incoming, outgoing, or both.  The direct connects are in one color and the indirect connects are in a 2nd color.

If nothing else, it's easier and more fun to use!   :o

Wapperdude