Scott Perman

SnapView attempt

Discussion created by Scott Perman on Nov 16, 2020

Based on comments I've seen in a few threads, I thought others might find this useful.  This is my attempt to replicate the NX command Snap View (I think that is what it is called.)  

 

This macro looks at your current model view (it does not care what, if anything, you have selected) and moves to the closest normal view.  (It also snaps to the 45 degree views.  That is not intended behavior, so I call it an added feature.)  It does NOT decide that you are looking at things upside down, or that you really want to look at that plane from the other side.  It moves to the closest possible matching view.  

 

I'm not much of a programmer.  Even when I was writing a lot of code, I would find examples and beat on them until it worked.  Be gentle. 

 

 

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModView As SldWorks.ModelView
Dim swMathUtil As SldWorks.MathUtility
Dim vTransform As Variant
Dim TranData(15) As Double
Dim Reord(15) As Double
Dim nTransform As SldWorks.MathTransform
Dim counter As Integer

 

Sub SnapView1()

Set swApp = Application.SldWorks
Set swMathUtil = swApp.GetMathUtility
Set swModel = swApp.ActiveDoc

 

'get current view data
Set swModView = swModel.ActiveView
vTransform = swModView.Orientation3.ArrayData

 

'make orientation normal to closest planar view
For counter = 0 To 8
Select Case vTransform(counter)
Case Is < -0.5
TranData(counter) = -1
Case Is > 0.5
TranData(counter) = 1
Case Else
TranData(counter) = 0
End Select
Next counter

 

'make the rest of the array same as read values
For counter = 9 To 15
TranData(counter) = vTransform(counter)
Next counter

 

'create transformation
Set nTransform = swMathUtil.CreateTransform(TranData)

 

'update view
swModView.Orientation3 = nTransform

'redraw
swModel.GraphicsRedraw2

 

'cleanup
Set nTransform = Nothing
Set swModel = Nothing
Set swMathUtil = Nothing
Set swApp = Nothing

End Sub

Outcomes