News:

BB code in posts seems to be working again!
I haven't turned on every single tag, so please let me know if there are any that are used/needed but not activated.

Main Menu

How to add a shape to a shapes object

Started by Yacine, January 19, 2015, 07:09:48 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Yacine

Hi guys,
would someone know how to build up a shapes collection from individual shapes?
I want a function to return a collection of shapes and since the object "shapes" exists, I wanted to use it as return parameter instead of an array or another construct.
Unfortunately the shapes object does not seem to have an Add method or anything comparable.
Even using the activewindow.selection and asserting it to the variable does not work.
Any idea?
Yacine

jik_ff

Can you not use the Selection object?  Not sure if this will work but try

Dim aGroup as Visio.Shapes
Dim a Selection as visio.Selection

<code to select the objects you want into the selection object>

set aGroup = ActiveWindow.Selection



I have not tried this, but just a thought...

JohnGoldsmith

Hi Yacine,

Are you talking about VBA or .NET (or other)?  If VBA then you 've got the Collection or Dictionary objects, or you could use an array of ID's.  In C# you could use List<Shape> or other array / collection types.

What are you wanting to do with the collection once you've got it?  If you ultimately need a Selection object to do something in the UI then, as @jik_ff suggests, the Visio.Selection object is probably a good way to go.  Have you had a look at the Page.CreateSelection method, which can take an 'empty' selection type?  Once you've got your selection object you can use its Select method to add new items to the 'collection'.

Best regards

John
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/

Yacine

Hi Jik and John,
thank you for the reply.


I am in VBA. There you can declare a variable as "Shapes", which contains obviously individual shapes.
But obviously this construct is only meant to be filled by visio itself, vba code can only read it.


The 2 hints from John are very usefull - collections and empty selections.


Speaking about the collection, assigning it an other collection did not work without, instead I had to iterate and assign value by value.


Here's the code:



Public Sub test()
Dim shp As Shape, shp2 As Shape, shp3 As Shape
Dim connShps As Collection
Dim connShp As Shape
Dim conn As Connect
Dim h As Variant
Dim coll As Collection


    For Each shp In ActivePage.Shapes
        If shp.CellExists("user.type", False) Then
            If shp.Cells("user.type").ResultStr("") = "tree" Then
                Debug.Print "Shape "; shp.Name; " is connected to: "
                Set connShps = getConnectedShapes(shp)
                If Not connShps Is Nothing Then
                    For Each connShp In connShps
                        Debug.Print connShp.Name
                    Next connShp
                End If
            End If
        End If
    Next shp


End Sub


Public Function getConnectedShapes(shp As Shape) As Collection
    Dim i As Integer
    Dim ConObj As Visio.Connect
    Dim FromConShp As Visio.Shape
    Dim ToConShp As Visio.Shape
    Dim conShp As Visio.Shape
    Dim temp As Variant
    Dim tempColl As Collection
       
        ActiveWindow.DeselectAll
        For i = 1 To shp.FromConnects.Count                              'Get connectors, lines glued to selected shape
            Set FromConShp = shp.FromConnects(i).FromSheet
            For Each ConObj In FromConShp.Connects                          'Get shape connected to opposite end
                Set ToConShp = ConObj.ToSheet
                If ToConShp <> shp Then                                  'Skip selected shape; already colored.
                    ActiveWindow.Select ToConShp, visSelect
                End If
            Next ConObj
        Next i
    If ActiveWindow.Selection.Count > 0 Then
        Set tempColl = New Collection
        For Each conShp In ActiveWindow.Selection
            tempColl.Add conShp
        Next conShp
        Set getConnectedShapes = tempColl
    End If
   
    ActiveWindow.DeselectAll


End Function



Thanks again,
Yacine
Yacine

JohnGoldsmith

Hi Yacine,

You shouldn't need to use (or change) the Window selection in this case I don't think.  Here's a slightly modified version of your method:

Public Function getConnectedShapes(shp As Shape) As Collection
    Dim i As Integer
    Dim ConObj As Visio.Connect
    Dim FromConShp As Visio.Shape
    Dim ToConShp As Visio.Shape
    Dim conShp As Visio.Shape
    Dim tempColl As Collection
       
    For i = 1 To shp.FromConnects.Count                              'Get connectors, lines glued to selected shape
        Set FromConShp = shp.FromConnects(i).FromSheet
        For Each ConObj In FromConShp.Connects                          'Get shape connected to opposite end
            Set ToConShp = ConObj.ToSheet
            If ToConShp <> shp Then                                  'Skip selected shape; already colored.
                If tempColl Is Nothing Then
                    Set tempColl = New Collection
                End If
                tempColl.Add ToConShp
            End If
        Next ConObj
    Next i

    Set getConnectedShapes = tempColl

End Function


Also, I can't remember what version you're on but for 2010 and above then you might want to have a look at the Shape.ConnectedShapes method.

Best regards

John
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/

Yacine

#5
Hi John,
you're right. The selection commands were remainings from my first trials.
And I'm too greedy to replace my old V2007. So no Shape.ConnectShapes for me.  :(
Cheers,
Yacine
Yacine

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 242 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1115KB.
Tokens: post-login.
Cache hits: 14: 0.00115s for 26,764 bytes (show)
Cache misses: 3: (show)
Queries used: 16.

[Show Queries]