MACRO LIBRARY

Relocate view to true center after extents are modified

Free public access. No account or subscription required.

Files

Relocate view to true center after extents are modified

Code

Module: mMain

'Demonstrates how to get the new "center" coordinates of a view after its extents have
'   modified. The view center (retrieved by IView::Position) does not update after
'   the view extents are modified due to configuration change or Select Bodies. This
'   is a known bug (SPR 682112).
    
'Preconditions: View is selected.
'Result: The center of the view's current extents is relocated to the same point as
'   IView::Position.
    
'Notes: The second method is used simply to move a current view to the origin,
'   thereby providing a more intuitive location for testing.
    
'Written by Keith Rice
'www.cadsharp.com
'CADSharp LLC
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swView As SldWorks.View
Dim vCenter_Old As Variant
Dim vOutline_New As Variant
Dim x_old As Double, y_old As Double
Dim x_new As Double, y_new As Double
Dim x_final As Double, y_final As Double
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swView = swSelMgr.GetSelectedObject6(1, -1)
    
    vCenter_Old = swView.Position
    x_old = vCenter_Old(0)
    y_old = vCenter_Old(1)
    
    vOutline_New = swView.GetOutline
    
    x_new = (((vOutline_New(2) - vOutline_New(0)) / 2) + vOutline_New(0))
    y_new = (((vOutline_New(3) - vOutline_New(1)) / 2) + vOutline_New(1))
    
    x_final = x_old - x_new + x_old
    y_final = y_old - y_new + y_old
    
    Debug.Print "x_old: " & Round(x_old, 6), "y_old: " & Round(y_old, 6)
    Debug.Print "x_new: " & Round(x_new, 6), "y_new: " & Round(y_new, 6)
    Debug.Print "x_final: " & Round(x_final, 6), "y_final: " & Round(y_final, 6)
    
    Dim dblPos(1) As Double
    dblPos(0) = x_final
    dblPos(1) = y_final
    
    swView.Position = dblPos
End Sub
    
Sub CenterViewAtOrigin()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swView = swSelMgr.GetSelectedObject6(1, -1)
    
    Dim dblPos(1) As Double
    dblPos(0) = 0
    dblPos(1) = 0
    
    swView.Position = dblPos
End Sub