Select standard plane (e.g. Front) by type using SOLIDWORKS API

Edit ArticleEdit Article

Right plane selected in the graphics view
Right plane selected in the graphics view

It is not recommended to select the standard planes by their names as names are not consistent and may be changed in the template (e.g. different localization or standard).

This example demonstrates how to select standard plane (Top, Front or Right) using SOLIDWORKS API by specifying its type so the selection will be consistent regardless of the plane name.

This macro works based on the fact that the default SOLIDWORKS planes are always ordered the same way, i.e. Front, Top and Right planes are the first planes in the model, positioned before the origin feature and cannot be reordered or removed.

Public Enum swPlanes_e
    Front = 1
    Top = 2
    Right = 3
End Enum

Dim swApp As SldWorks.SldWorks
Sub main()
    Set swApp = Application.SldWorks

    Dim swModel As SldWorks.ModelDoc2
    Set swModel = swApp.ActiveDoc
    If Not swModel Is Nothing Then
        If swModel.GetType() = swDocumentTypes_e.swDocASSEMBLY Or _
            swModel.GetType() = swDocumentTypes_e.swDocPART Then
            SelectPlane swModel, swPlanes_e.Right
            MsgBox "Only assemblies and parts are supported"
        End If
        MsgBox "Please open part or assembly"
    End If
End Sub

Sub SelectPlane(model As SldWorks.ModelDoc2, planeType As swPlanes_e)

    Dim planeIndex As Integer
    Dim swFeat As SldWorks.Feature
    Set swFeat = model.FirstFeature

    Do While Not swFeat Is Nothing

        If swFeat.GetTypeName = "RefPlane" Then

            planeIndex = planeIndex + 1
            If CInt(planeType) = planeIndex Then

                swFeat.Select2 False, 0

                Exit Sub

            End If

        End If
        Set swFeat = swFeat.GetNextFeature

End Sub


All articles and code at CodeStack are now open-source and hosted on GitHub. If you want to contribute by modifying existing articles and code snippets, submitting new ones, reporting errors and bugs etc. please follow this blog post for more information. We appreciate any contribution.

Product of Xarial Product of Xarial