17 Replies Latest reply on Dec 27, 2018 2:44 AM by Artem Taturevych

    Rename All Section and Detail Views with API

    Dave Paul

      I have a situation that comes up from time to time where a number of sheets are added to a drawing with additional section and detail views.  This creates a situation where the view names and not in alphabetical order thru the sheets.  I have routines where I traverse the drawing sheets to perform some operation.  What I'm looking for is the command sequence for renaming the section and detail views.

      For instance: Starting on "Sheet1", rename all the section views from A to X1.  Then update all the detail views labels from X2 - X3.  Next sheet...

      What are the commands to rename the a section and/or detail views?

       

      Dave

        • Re: Rename All Section and Detail Views with API
          Gabriel Bueno

          Artem Taturevych  and Deepak Gupta,

           

          sorry everyone to reopen this conversation, but with the idea, I'd like to know how to change just the details labels to number instead of letters.

           

          Select Case swView.Type

                  Case swDrawingViewTypes_e.swDrawingSectionView

                            Dim swSectionView As SldWorks.DrSection

                            Set swSectionView = swView.GetSection

                           Debug.Print swSectionView.GetLabel

                           swSectionView.SetLabel2 GetNextName

                 Case swDrawingViewTypes_e.swDrawingDetailView

                         Dim swDetailedView As SldWorks.DetailCircle

                         Set swDetailedView = swView.GetDetail

                         Debug.Print swDetailedView.GetLabel

                         swDetailedView.SetLabel GetNextName  - "I guess that I have to change this part with a rule to choose a number that was not used yet"

           

          my idea is to change this last line, but I need still something else to verify the numbers, check if the number was used or not and use the next one, or just add a last number used +1.

           

           

          Const A_CHAR As Integer = 65

          Const Z_CHAR As Integer = 90

           

          Dim swApp As SldWorks.SldWorks

          Dim swDraw As SldWorks.DrawingDoc

          Dim swSheet As SldWorks.Sheet

           

          Dim curChar As Integer

           

          Sub main()

           

             Set swApp = Application.SldWorks

           

             Set swDraw = swApp.ActiveDoc  

           

             If swDraw Is Nothing Then

                      MsgBox "Please open drawing document"

                      End

             End If

           

            Dim vSheetNames As Variant

            vSheetNames = swDraw.GetSheetNames

            

            Dim sheetInd As Integer

            curChar = A_CHAR 

           

             For sheetInd = 0 To UBound(vSheetNames)

            

             swDraw.ActivateSheet vSheetNames(sheetInd)

             Set swSheet = swDraw.Sheet(vSheetNames(sheetInd))

           

             Dim vViews As Variant

             vViews = swSheet.GetViews

            

             Dim i As Integer  

           

             For i = 0 To UBound(vViews)  

           

                  Dim swView As SldWorks.View 

           

                  Set swView = vViews(i)  

                  Select Case swView.Type

                            Case swDrawingViewTypes_e.swDrawingSectionView

                                 Dim swSectionView As SldWorks.DrSection

                                 Set swSectionView = swView.GetSection

                                Debug.Print swSectionView.GetLabel

                                swSectionView.SetLabel2 GetNextName

                            Case swDrawingViewTypes_e.swDrawingDetailView

                                 Dim swDetailedView As SldWorks.DetailCircle

                                 Set swDetailedView = swView.GetDetail

                                Debug.Print swDetailedView.GetLabel

                                swDetailedView.SetLabel GetNextName

                       End Select

           

                  Next

           

             Next

           

             swDraw.ForceRebuild

           

          End Sub

           

          Function GetNextName() As String

           

             If curChar > Z_CHAR Then

                      MsgBox ("Overflow")

                      End

             End If

           

             GetNextName = Chr(curChar) & curText

           

             curChar = curChar + 1

           

          End Function

            • Re: Rename All Section and Detail Views with API
              Deepak Gupta

              You can add a variable

              Dim nNumView As Long

              nNumView = 1

              and then use

              swDetailedView.SetLabel ("DetailView" & nNumView)

              And can check if nNumView is used or not. If used, then bump by +1, check and repeat. If not then use the nNumView value and next view.

                • Re: Rename All Section and Detail Views with API
                  Gabriel Bueno

                  Thanks! I did what you said changed the code and works fine now!

                   

                  Here is the code changed with everything I've done to work.

                   

                  I don't know if there is any way to make the code see all sheets at once, but I already can work with this!

                  maybe there is a easy way to do this!

                   

                  I found out what was the problem, It works perfect now!

                   

                  THANKS!!

                   

                  Const A_CHAR As Integer = 65

                  Const Z_CHAR As Integer = 90

                   

                  Dim swApp As SldWorks.SldWorks

                  Dim swDraw As SldWorks.DrawingDoc

                  Dim swSheet As SldWorks.Sheet

                  Dim curChar As Integer

                  Dim nNumView As Long

                  Dim curNum As Long

                   

                  Sub main()   

                      Set swApp = Application.SldWorks

                      Set swDraw = swApp.ActiveDoc   

                      If swDraw Is Nothing Then

                          MsgBox "Please open drawing document"

                          End

                      End If       

                      Dim vSheetNames As Variant

                      vSheetNames = swDraw.GetSheetNames   

                      Dim sheetInd As Integer

                      curChar = A_CHAR

                      curNum = 1           

                      For sheetInd = 0 To UBound(vSheetNames)       

                          swDraw.ActivateSheet vSheetNames(sheetInd)

                          Set swSheet = swDraw.Sheet(vSheetNames(sheetInd))

                          Dim vViews As Variant

                          vViews = swSheet.GetViews       

                          Dim i As Integer   

                          For i = 0 To UBound(vViews)           

                              Dim swView As SldWorks.View           

                              Set swView = vViews(i)           

                              Select Case swView.Type

                                  Case swDrawingViewTypes_e.swDrawingSectionView

                                      Dim swSectionView As SldWorks.DrSection

                                      Set swSectionView = swView.GetSection

                                      Debug.Print swSectionView.GetLabel

                                      swSectionView.SetLabel2 (GetNextName)

                                  Case swDrawingViewTypes_e.swDrawingDetailView

                                      Dim swDetailedView As SldWorks.DetailCircle

                                      Set swDetailedView = swView.GetDetail

                                      Debug.Print swDetailedView.GetLabel

                                      swDetailedView.SetLabel ("DETALHE " & GetNextNumber) 'swDetailedView.SetLabel GetNextName

                              End Select           

                          Next   

                      Next   

                      swDraw.ForceRebuild

                  End Sub

                   

                  Function GetNextName() As String

                      If curChar > Z_CHAR Then

                          MsgBox ("Overflow")

                          End

                      End If

                      GetNextName = Chr(curChar) & curText

                      curChar = curChar + 1

                  End Function

                   

                  Function GetNextNumber() As String

                      GetNextNumber = curNum

                      curNum = curNum + 1

                  End Function

                    • Re: Rename All Section and Detail Views with API
                      William MacDonald

                      Long after the fact with this one, but here is my addition. This code, based on the code I found here, will add on a "A" prefix after the alphabet is used up then continue on. Removed unnecessary code and pulled the dims out to the start so you're not re-declaring on every loop to speed things up.

                       

                       

                       

                      Dim swApp           As SldWorks.SldWorks

                      Dim swDraw          As SldWorks.DrawingDoc

                      Dim swSheet         As SldWorks.Sheet

                      Dim swView          As SldWorks.View

                      Dim swSectionView   As SldWorks.DrSection

                      Dim swDetailedView  As SldWorks.DetailCircle

                       

                      Dim curChar         As Integer

                      Dim sheetInd        As Integer

                      Dim i               As Integer

                      Const A_CHAR        As Integer = 65

                      Const Z_CHAR        As Integer = 90

                       

                      Dim prefix          As String

                       

                      Dim vViews          As Variant

                      Dim vSheetNames     As Variant

                       

                      Sub main()

                       

                          Set swApp = Application.SldWorks

                          Set swDraw = swApp.ActiveDoc

                        

                          'this is a prefix for sections and details

                          prefix = ""

                       

                          If swDraw Is Nothing Then

                              MsgBox "Please open drawing document"

                              End

                          End If

                       

                          vSheetNames = swDraw.GetSheetNames

                          curChar = A_CHAR

                       

                          For sheetInd = 0 To UBound(vSheetNames)

                        

                              swDraw.ActivateSheet vSheetNames(sheetInd)

                              Set swSheet = swDraw.Sheet(vSheetNames(sheetInd))

                              vViews = swSheet.GetViews

                                    

                              For i = 0 To UBound(vViews)

                                

                                  Set swView = vViews(i)

                                

                                  'This if statement will add an A prefix to any existing prefixes when the count goes past Z

                                  If curChar > 90 Then

                                              prefix = prefix & "A"

                                              curChar = A_CHAR

                                          End If

                                        

                                  Select Case swView.Type

                       

                                      Case swDrawingViewTypes_e.swDrawingSectionView

                                          Set swSectionView = swView.GetSection

                                          swSectionView.SetLabel2 (prefix & GetNextName)

                       

                                      Case swDrawingViewTypes_e.swDrawingDetailView

                                          Set swDetailedView = swView.GetDetail

                                          swDetailedView.SetLabel (prefix & GetNextName)

                                        

                                  End Select

                              Next

                          Next

                       

                          swDraw.ForceRebuild

                       

                      End Sub

                       

                      Function GetNextName() As String

                       

                          GetNextName = Chr(curChar) & curText

                          curChar = curChar + 1

                       

                      End Function