7 Replies Latest reply on Mar 31, 2017 11:39 AM by Deepak Gupta

    SolidWorks VBA - check if a "View" exists on a drawing

    Darren Smith

      Can anybody help???

       

      Say that I have a blank drawing (SLDDRW) open containing no views at (it is a completely blank sheet)

       

      How can I check, using a VBA macro, if no drawing views are on the sheet as I need this for part of a much larger macro.

       

      Many thanks in advance!!!!

       

      Darren

        • Re: SolidWorks VBA - check if a "View" exists on a drawing
          Deepak Gupta

          Option Explicit

           

          Sub main()

              Dim swApp                   As SldWorks.SldWorks

              Dim swModel                 As SldWorks.ModelDoc2

              Dim swDraw                  As SldWorks.DrawingDoc

              Dim viewCount               As Long

           

              Set swApp = CreateObject("SldWorks.Application")

              Set swModel = swApp.ActiveDoc

              Set swDraw = swModel

             

              viewCount = swDraw.GetViewCount

                 

                  If viewCount = 1 Then

                          MsgBox "No Views Found On This Drawing!!"

                  End If

          End Sub

            • Re: SolidWorks VBA - check if a "View" exists on a drawing
              Xavier Rodríguez Nieto

              What happens if you have a multiple sheets drawing? How can you know if there is any view in an specific sheet?

               

              For instance: 2 sheets, sheet #1 has 0 views and sheet #2 has 3 views. How can you know the number of views in sheet #1? "GetViewCount" would return 5.

               

              I've tried with "ISheet:GetViews", but the thing is that this method returns an array even if there is no view in the drawing sheet. This array contains, in my case, 10 "standard orientations" like "Front", "Back" and so on.

                • Re: SolidWorks VBA - check if a "View" exists on a drawing
                  Deepak Gupta

                  You can traverse thru each sheet and get the view count.

                    • Re: SolidWorks VBA - check if a "View" exists on a drawing
                      Xavier Rodríguez Nieto

                      I don't think so, as in each sheet I will always get the same result, the "ISheet:GetViews" returns the view in the whole drawing (in my example I will always get "5").

                       

                      However, I found this code example in the forum that solved it out with "GetFirstView" and "GetNextView" from IView:

                       

                      Option Explicit

                      Dim swApp      As SldWorks.SldWorks
                      Dim swModel    As SldWorks.ModelDoc2
                      Dim swDrawing  As SldWorks.DrawingDoc
                      Dim swView      As SldWorks.View
                      Dim Errors      As Long

                      Sub main()
                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc

                      ' Is document active?
                      If swModel Is Nothing Then
                      swApp.SendMsgToUser2 "A Drawing file must be open.", swMbWarning, swMbOk
                      Exit Sub
                      End If

                      ' Is it a part document?
                      If swModel.GetType <> SwConst.swDocDRAWING Then
                      swApp.SendMsgToUser2 "A Drawing file must be open.", swMbWarning, swMbOk
                      Exit Sub
                      End If

                      Set swDrawing = swModel
                      Set swView = swDrawing.GetFirstView
                      Set swView = swView.GetNextView
                      If swView Is Nothing Then
                      MsgBox "Please insert a Model View first!"
                      End
                      Else
                      swApp.ActivateDoc3 swView.GetReferencedModelName, False, swRebuildOnActivation_e.swUserDecision, Errors
                      End If
                           
                      End Sub

                        • Re: SolidWorks VBA - check if a "View" exists on a drawing
                          Deepak Gupta

                          Not using ViewCount but traversing thru views on each sheet only using GetFirstView/GetNextView and get the count of i.

                           

                          What end result you're looking for with view count?

                           

                           

                          • Re: SolidWorks VBA - check if a "View" exists on a drawing
                            Deepak Gupta

                            Here is what I mean

                             

                            Option Explicit
                            Dim swApp As SldWorks.SldWorks
                            Dim swModel As SldWorks.ModelDoc2
                            Dim swDraw As SldWorks.DrawingDoc
                            Dim swView As SldWorks.View
                            Dim vSheets As Variant
                            Dim i As Integer
                            Dim j As Integer
                            
                            Sub main()
                                Set swApp = Application.SldWorks
                                Set swModel = swApp.ActiveDoc
                                Set swDraw = swModel
                                
                                vSheets = swDraw.GetSheetNames
                                For i = 1 To swDraw.GetSheetCount
                                    swDraw.ActivateSheet vSheets(i - 1)
                                    Set swView = swDraw.GetFirstView
                                    Set swView = swView.GetNextView
                                        j = 0
                                        Do While Not swView Is Nothing
                                        j = j + 1
                                        Set swView = swView.GetNextView
                                        Loop
                                        Debug.Print "View count on Sheet " & i & " is " & j
                                Next i
                            End Sub