5 Replies Latest reply on May 15, 2016 7:37 AM by Deepak Gupta

    retrieving the part component names of all drawing views in a multi sheet drawing

    John Smith

      I have the following code to retrieve the part component names of all drawing views in a multi sheet drawing. it works fine for views containing assemblies only but when it comes to views of part components it returns NULL for the array "vDrawChildCompArr", your help is appreciated, thanks.

       

      Dim sheetCount As Long

          Dim viewCount As Long

          Dim currentSheetNum  As Integer

          currentSheetNum = 0

          Dim ss As Variant

          ss = swDraw.GetViews                                              'The return value is an array of arrays with a length equal to the number of sheets in the drawing document.

          For sheetCount = LBound(ss) To UBound(ss)           'goes through all sheets

              currentSheetNum = currentSheetNum + 1

              Dim vv As Variant                                                    'defines a variable to explore through the views of each sheet

              vv = ss(sheetCount)                                                'gets views of each sheet

              For viewCount = 1 To UBound(vv)                          'first element of the array is the sheet itself and we dont need it

                  Debug.Print currentSheetNum

                  Set swView = vv(viewCount)

                  Set swRootDrawComp = swView.RootDrawingComponent

                  vDrawChildCompArr = swRootDrawComp.GetChildren

                          For Each vDrawChildComp In vDrawChildCompArr

                              Set swDrawComp = vDrawChildComp

                              Set swComp = swDrawComp.Component

                              strName = swComp.Name2

                              Debug.Print (strName)

                          Next vDrawChildComp

                 Next viewCount

          Next sheetCount

        • Re: retrieving the part component names of all drawing views in a multi sheet drawing
          Deepak Gupta

          Are you looking to get the children also of the view (this would work for assembly view) OR just the main model used in the view?

            • Re: retrieving the part component names of all drawing views in a multi sheet drawing
              John Smith

              just the main model. but it wouldn't hurt if I could get the children as well, but that not a priority.

                • Re: retrieving the part component names of all drawing views in a multi sheet drawing
                  Deepak Gupta

                  Does this helps

                   

                  Option Explicit
                      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 vSheetName              As Variant
                      Dim i                      As Long
                  
                  Sub main()
                  
                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                     
                      If swModel Is Nothing Then
                      MsgBox "No Active Drawing Found, Closing the Macro!"
                      End
                      End If
                     
                      If swModel.GetType <> 3 Then
                      MsgBox "No Active Drawing Found, Closing the Macro!"
                      End
                      End If
                         
                      Set swDraw = swModel
                      vSheetName = swDraw.GetSheetNames
                      For i = 0 To UBound(vSheetName)
                          Set swView = swDraw.GetFirstView
                          Set swView = swView.GetNextView
                  
                  
                              While Not swView Is Nothing
                                  Debug.Print "      " & swView.GetReferencedModelName
                                  Set swView = swView.GetNextView
                              Wend
                      Next i
                  End Sub
                  
                    • Re: retrieving the part component names of all drawing views in a multi sheet drawing
                      John Smith
                      Option Explicit
                          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 vSheetName              As Variant
                          Dim i                      As Long
                         Dim status                  As Boolean
                          Dim modelName               As String
                      Sub main()
                      
                          Set swApp = Application.SldWorks
                          Set swModel = swApp.ActiveDoc
                          Set swDraw = swModel
                          vSheetName = swDraw.GetSheetNames
                          For i = 0 To UBound(vSheetName)
                              status = swDraw.ActivateSheet(vSheetName(i))
                              Set swView = swDraw.GetFirstView
                              Set swView = swView.GetNextView
                                  While Not swView Is Nothing
                                       modelName = Left(swView.GetReferencedModelName, InStrRev(swView.GetReferencedModelName, "\"))
                                       modelName = Right(swView.GetReferencedModelName, Len(swView.GetReferencedModelName) - Len(modelName))
                                       modelName = Left(modelName, InStr(modelName, ".") - 1)
                                      Debug.Print "      " & modelName
                                      Set swView = swView.GetNextView
                                  Wend
                          Next i
                      End Sub
                      

                       

                      so it works perfectly now.  since I wanted the name of the components only, not the full path, I had to make a few small modification to your answer Deepak Gupta. the other thing is that the IF statement would return an error ( If swModel.GetType <> 3 Is Nothing Then) so I deleted it as well. other than that it was all perfect. thank you so much for your help.