Reliably putting a specific shape in the center of the window with code!

Started by Visisthebest, July 29, 2023, 10:15:11 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

wapperdude

Once I found my missing memory brain cells, extending the functionality of the SetViewRect() method for shapes that belong to a group.  Such shapes have their location defined by the group and not the page.

Thanks to VisioGuy article re subshape selection, http://www.visguy.com/2008/05/17/detect-sub-selected-shapes-programmatically/.  To obtain the desired coordinates, the macro uses the shape.TransformXYTo() function.  As with LocToLoc & LocToPar functions, for subgroup shapes, use the LocPin cells not the Pin cells.


Sub cntrIt()
'Adapted from John Goldsmith and Visisthebest
'Macro centers the activewindow containing the selected
'shape.  There are 2 methods to do the centering.
'Both give same results on limited basis.
'Macro assumes that
'   a) the current activewindow is the desired window
'   b) that the zoom level is unchanged.

'The SetViewRect() method needs additional coding to work with shapes that are members of a group.
'Below is simplified development to achieve that capability
'Macro modified to get page coordinates for a group member shape using
'shape.TransformXYTo function.
'User section added to accommodate LocToLoc() function to verify coordinate values.

    Dim vWin As Visio.Window
    Dim vPg As Visio.Page
    Dim pgPinX As Double
    Dim pgPinY As Double
    Dim vShp As Visio.Shape
    Dim dL As Double
    Dim dT As Double
    Dim dWid As Double
    Dim dHt As Double
    Dim cPinX As Visio.Cell
    Dim cPinY As Visio.Cell
    Dim vSel As Visio.Selection
   
    Set vWin = ActiveWindow
    Set vSel = vWin.Selection        'This is, default,  normal shape selection only; excludes subshapes
    If vSel.Count > 0 Then            'If count >0, then selected shape is normal, if 0, then shape is subshape& needs to mode change
        Set vShp = vSel.Item(1)
        Set cPinX = vShp.Cells("PinX") 
        Set cPinY = vShp.Cells("PinY")
    Else
        vSel.IterationMode = Visio.VisSelectMode.visSelModeOnlySub      'mode change:  getting subshape only
        Set vShp = vSel.Item(1)
        Set cPinX = vShp.Cells("LocPinX")                        'The trick was to use "LocPin" rather than "Pin" for subshape
        Set cPinY = vShp.Cells("LocPinY")
    End If
   
    vShp.TransformXYTo vPg, cPinX, cPinY, pgPinX, pgPinY            'This will fetch the pgPinX and pgPinY values for SetViewRect
'        Debug.Print vShp.Name.                                                'Optional "Immediate Window" vba print
'        Debug.Print "LocPins:  ", cPinX, cPinY
'        Debug.Print "Pg coords:  ", pgPinX, pgPinY
'        Debug.Print "LocToLoc LocPins:  ", vShp.Cells("User.Row_4").ResultStr(visNone)
   
    vWin.GetViewRect dL, dT, dWid, dHt.                'fetch current window size info
   
     dL = pgPinX - dWid * 0.5.                                   'calc new location for window centering
     dT = pgPinY + dHt * 0.5
   
     vWin.SetViewRect dL, dT, dWid, dHt.               'set window new location
'     vWin.CenterViewOnShape vShp, visCenterViewSelectShape      'This was previously shown to work as is, and not used

End Sub

Visio 2019 Pro

Surrogate


wapperdude

Rise/Fall Stack Ovrflw:  that was interesting.  Not sure what it proves though.  Looking at the traffic, aside from the anomalous step around May 2022, seems relatively steady.  So there does appear to be some outside accounting influence.

More interesting are the post and other metrics.  All of those show a steady decline.  However, Visio Guy Forum seems to also experience a volume decline. Seems like the number of days with no posts is increasing.  Certainly the number experienced responders has declined.  I think that's normal.  I suspect sales have dropped so fewer noobies. 
Visio 2019 Pro

Surrogate

Quote from: wapperdude on August 02, 2023, 08:43:01 PMHowever, Visio Guy Forum seems to also experience a volume decline.
IMHO The rise of Visio happened 25 years ago. I was a graduate student at the time.
Quote from: wapperdude on August 02, 2023, 08:43:01 PMHCertainly the number experienced responders has declined
Those who were older then are retired or about to retire. Or have changed their occupation or are now using other applications.
Since version 2010, MS has kept developer mode out of sight. They are more interested in having big sales to low skilled users.
Quote from: wapperdude on August 02, 2023, 08:43:01 PMI suspect sales have dropped so fewer noobies.
The answers to simple questions can be found in the official MS help information. Also, these questions have been answered many times over the 15 years of the VG forum. Such answers can be googled...

wapperdude

QuoteThose who were older then are retired or about to retire.
:o.  You talkin' bout me?  ??? :D ;)
QuoteOr have changed their occupation or are now using other applications.
:P. Say it ain't so!!!   >:(  :P
Visio 2019 Pro

Visisthebest

Microsoft giving Visio web 'lite edition' for free with Microsoft 365 will I think lead to a modest uptake of the desktop version as well. But it will take some time.

Visio desktop even today is a unique product, and new books about its advanced features are appearing this year:
https://www.amazon.com/Visualize-Complex-Processes-Microsoft-Visio/dp/1837631921/ref=sr_1_1?crid=29DTK2WYNY518&keywords=visio+david+parker&qid=1691102145&sprefix=%2Caps%2C504&sr=8-1

No revolutions but there is still a good bit of life in Visio desktop!
Visio 2021 Professional

Surrogate

@Mr. Wayne
I'm sad I didn't get to co-work with you. I didn't have a teacher like you. ;)
Quote from: Visisthebest on August 03, 2023, 10:37:28 PM
Microsoft giving Visio web 'lite edition' for free with Microsoft 365 will I think lead to a modest uptake of the desktop version as well. But it will take some time.
I don't know if I'll see further development of Visio in the desktop. :o

I haven't used Visio for a long time due to my work.
It's my hobby, and because I don't have enough time for hobbies, I don't take on serious projects. I am starting to experiment more in other areas. Maybe soon I will lose my reputation as a Visio guru. ;)

wapperdude

QuoteMaybe soon I will lose my reputation as a Visio guru.
. I doubt that will happen.  The well is deep and there's plenty of water.
Visio 2019 Pro