MACRO LIBRARY

Get a view's orientation using transforms

Free public access. No account or subscription required.

Files

Get a view's orientation using transforms

Code

'Determine the orientation of a view in a drawing. Credit given to Rajat Jain.
'https://forum.solidworks.com/thread/49728?tstart=0
    
'Modified by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
'Preconditions: Drawing with drawing views is open
'Results: see immediate window, use following guide:
    
'(1,0,0) - Right View
'(-1,0,0) - Left View
'(0,1,0) - Top View
'(0,-1,0) - Bottom View
'(0,0,1) - Front View
'(0,0,-1) - Back View
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swSheet As SldWorks.Sheet
Dim swView As SldWorks.View
Dim swMathUtil As SldWorks.MathUtility
Dim swTransform As SldWorks.MathTransform
Dim swMathVect As SldWorks.MathVector
Dim vViews As Variant
Dim dblPts(2) As Double
Dim vPts As Variant
Dim i As Integer
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swMathUtil = swApp.GetMathUtility
    Set swSheet = swDraw.GetCurrentSheet
    vViews = swSheet.GetViews
    For i = 0 To UBound(vViews)
        Set swView = vViews(i)
        dblPts(0) = 0
        dblPts(1) = 0
        dblPts(2) = 1
        Set swMathVect = swMathUtil.CreateVector(dblPts)
        Set swTransform = swView.ModelToViewTransform
        Set swTransform = swTransform.Inverse   'to move from view to model
        Set swMathVect = swMathVect.MultiplyTransform(swTransform)
        Set swMathVect = swMathVect.Normalise   'to remove scaling
        vPts = swMathVect.ArrayData
        Debug.Print vPts(0) & " " & vPts(1) & " " & vPts(2)
    Next i
End Sub
    
'Notes: If you are getting more results in the immediate window than you expect, including _
    non-integer values in unit vector, you need to open drawing palette and click the red X, _
    then re-run macro.