Guarded object within a group still grows

Started by maclarkson, September 11, 2019, 02:47:19 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

maclarkson

Hi all,

I am creating a new template that has icons in the top left-hand corner. I have attached my template. can anyone tell me the best way i can get them to stay where they are in relation to their objects and to not grow change position even if the text gets bigger or the shape srinks or grows?

Many thanks

Mike

wapperdude

#1
There are at least 2 ways to do this...
1) most common:  select shape and icon and group.  This locks the icon to the shape.  Downside, extra shape added per each grouping.
2) less common:
     a) select shape and convert to group.  No extra shape, and the shape is the group.
     b) ribbon>Developer tab>Shape  Design (Behavior); set Group Behavior (Group Data) = Behind member shapes.
     c) with main shape still selected, add icon selection, then Add to Group.  Icon locked to shape.  Takes a few more steps, but, I think this is cleaner.

Based upon method (2):
Now to lock size and position:  open shapesheet for icon.
We will need literal size and positions, not values based upon width, height formulas!
>>  Set shapesheet to display values.
>>  Size:
          ** Width:  enter guard(actual value), e.g. guard(0.5 in)
          ** Height:  do the same

>> Position:  note, we want the icon's position to be fixed relative to left and top edges of main shape, regardless of its size and placement.
          ** PinX:  =LocPinX + some Xoffset value
          ** PinY:  =Sheet.1!Height*1 - (Height-LocPinY) - some Yoffset value  Where Sheet.1 is the main shape.  This will change with each icon/shape pair.  Note, for method (1), use the group name, Sheet.whatever, instead of the main shape name.

That should take care of it.
Visio 2019 Pro

vojo

Couple other options

a)    width cell = guard(width*1 - 20mm),  Height cell = guard(height*1 - 30mm)
       Placement will be locked as an offset from the northeast corner of the mother shape

b)    Be careful about guard     Width cell = guard(width *0.74).   In this equation, guard is defeated.
       so be careful about using guard this way.

wapperdude

...and for Method3.  For completeness.  See http://visguy.com/vgforum/index.php?topic=5167.msg20306#msg20306, reply #12.  This allows the option of moving the icon, but, it will track the new location.  Just adds some flexibility.
Visio 2019 Pro

wapperdude

Wrote a little program to go thru the method2 steps.  It requires that the main shape be selected first.  Also, the offset spacing is hard coded at the moment, = 0.125".

This ought speed things up. 
Visio 2019 Pro

maclarkson

That's Awesome, a terrible amount of work but I am getting through it. Would you know how i could create an action to hide the icon using a right-click menu item?

wapperdude

#6
Not difficult.  Could be added to the code...

The steps would be...
1) select Main shape
2) open shapesheet and add Actions Section
3) add new action, duh!
   This Action would point to the Geometry section of the Icon shape.  In the header that section, there's something like hide all.  This needs to get toggled true/false, i.e., 1 or 0.

Since there's a few steps involved, and there's a lot of shapes, recommend doing this in code.
You can use macro recorder to get the necessary code syntax.  Adding it to furnished code could happen at end of macro.  Plus, there are two existing  variables that refer to the shapes:  vMain and vIcon.
Visio 2019 Pro

wapperdude

#7
Here's updated code:

Sub Macro1()
'Select the "Main" shape.  Macro will find the iCon shape
'and the code will convert Main to group and add iCon.
'Finally, macro stores formulae into iCon to lock size and position.
    Dim intTolerance As Integer
    Dim vsoReturnedSelection As Visio.Selection
    Dim strSpatialRelation As String
    Dim intSpatialRelation As VisSpatialRelationCodes
   
    Set vMain = ActiveWindow.Selection(1)
    vMain.ConvertToGroup
    vMain.CellsU("DisplayMode").Formula = "1"

    strSpatialRelation = ""
    intTolerance = 0.05
    intSpatialRelation = visSpatialContain
    Set vsoReturnedSelection = vMain.SpatialNeighbors(intSpatialRelation, intTolerance, 0)
    For Each vNbr In vsoReturnedSelection
        Set vIcon = vNbr
        ActiveWindow.Select vIcon, visSelect
    Next

    ActiveWindow.Selection.AddToGroup

    vIcon.CellsU("Width").Formula = vIcon.CellsU("Width").ResultStr(visNone)
    vIcon.CellsU("Height").Formula = vIcon.CellsU("Height").ResultStr(visNone)
    vIcon.CellsU("PinX").FormulaU = Chr(34) & "LocPinX + 0.125" & Chr(34)
    vIcon.CellsU("PinY").FormulaU = Chr(34) & vMain & "!Height*1-(Height-LocPinY)-0.125" & Chr(34)

    vMain.AddSection visSectionAction
    vMain.AddRow visSectionAction, visRowLast, visTagDefault
    vMain.CellsSRC(visSectionAction, 0, visActionMenu).RowNameU = "HideIcon"
    vMain.CellsSRC(visSectionAction, 0, visActionAction).FormulaU = "SETF(GETREF(Actions.HideIcon.Checked),NOT(Actions.HideIcon.Checked))"
    vMain.CellsSRC(visSectionAction, 0, visActionMenu).FormulaU = "IF(Actions.HideIcon.Checked,""Show Icon"",""Hide Icon"")"
    vIcon.CellsSRC(visSectionFirstComponent, 0, 2).FormulaU = "IF(" & vMain & "!Actions.HideIcon.Checked,1,0)"


End Sub
Visio 2019 Pro

wapperdude

#8
Looked at your file more closely.  The Icon is a grouped shape.  It does not have a geometry section.  Thus, there is no shapesheet fcn to implement show hide feature.  That will require using code.

Edit:  There is a work-around.  Add a rectangle slightly larger than the Icon shape.  This could be either a separate shape or added to the Icon group as the upper most shape.  Then set the color to the same color as the Main shape and FillPattern to solid.  The line could be set to no line.  In the Main shape, add the Action command to toggle the Icon between Show and Hide.  This action would toggle the new rectangle's Geometry1.NoShow cell between True and False.  Because of the matching color, it effectively hides the Icon.

Not a perfect solution, but does accomplishe the desired visual effect.
Visio 2019 Pro

wapperdude

Here's updated code which will loop thru all shapes on the active page and create the desired Main/Icon grouping.
Sub Macro1()
' Macro runs thru all shapes on a page.
'Finds qualified Main/Icon pairs, creates grouping
'
    Dim intTolerance As Integer
    Dim vsoReturnedSelection As Visio.Selection
    Dim strSpatialRelation As String
    Dim intSpatialRelation As VisSpatialRelationCodes
   
    strSpatialRelation = ""
    intTolerance = 0.05
    intSpatialRelation = visSpatialContain
   
'    Set vMain = ActiveWindow.Selection(1)
    For Each vMain In ActivePage.Shapes
        'check to see if shape has containing neighbor.  If so select it.  Then get
        Set vsoReturnedSelection = vMain.SpatialNeighbors(intSpatialRelation, intTolerance, 0)
        If vsoReturnedSelection.Count = 1 Then
            Debug.Print "Main shape:  ", vMain.Name
            ActiveWindow.Select vMain, visSelect
            vMain.ConvertToGroup
            vMain.CellsU("DisplayMode").Formula = "1"
            For Each vNbr In vsoReturnedSelection
                Set vIcon = vNbr
                ActiveWindow.Select vIcon, visSelect
                Debug.Print "Icon:  ", vIcon.Name
            Next
               
            ActiveWindow.Selection.AddToGroup
       
            vIcon.CellsU("Width").Formula = vIcon.CellsU("Width").ResultStr(visNone)
            vIcon.CellsU("Height").Formula = vIcon.CellsU("Height").ResultStr(visNone)
            vIcon.CellsU("PinX").FormulaU = "LocPinX + 0.03125"
            vIcon.CellsU("PinY").FormulaU = vMain & "!Height*1-(Height-LocPinY)-0.03125"
       
        End If
        ActiveWindow.DeselectAll
       
    Next
End Sub
Visio 2019 Pro

Yacine

Yacine

maclarkson

Yasin lovely tool  :'( i just spent 5 hours doing it all by hand and just read your reply  :'(. O well her is the final outcome of the icon.


Yacine

Yes, sorry for my late answer. Unfortunately the search tools are still very limited. Good luck anyway.
Yacine

wapperdude

Also, the revised code that was provided does the same shape "locking" on All of the shapes at a single pass.  Tried and executed on your file.  So, 1 min to install the code (copy / paste), and 1 or 2 seconds to run.  That was a day before Yacine's post. 

Yacine's Tool is quite handy, even with the bugs, and is more general purpose/universal than my code...which was developed for this specific case. 

It would take a bit of code development, but it would be possible to create a shapesheet action to show/hide the icons.  Perhaps Yacine would modify his tool.  At the group top level, create an action entry to execute show hide.  Then drill down to each &  every sub shape and set the Geometry1.No Show to respond to the group Action setting.
Visio 2019 Pro

maclarkson

Hay Wapper I tried using your code on an old version and for the life of me couldn't get it to do anything. I have firstly grouped each icon. Do in need to do something to these to make them identify as icons, like changing the behaviour. I have the main object I have done nothing to these. Do in need to do anything to these? Should I then group an icon once I have done something with it and object together and then run you code?