29 Replies Latest reply on Jul 20, 2015 7:32 AM by Deepak Gupta

    Note on drawing showing current sheet number and total sheets MINUS 1

    Peter Larsen

      Hi,

       

      I need to add a note on my drawings showing current sheet number and tototal sheet numbers using the syntax "Page X of Y".

       

      I can easily do that with these CustomProperties:

      Page $PRP:"SW-Current Sheet(Current Sheet)" of $PRP:"SW-Total Sheets(Total Sheets)"

       

      Questions:

      1. But I need to subtract 1 from Total Sheets since I have on sheet that should be left out - is that possible?
      2. A better solution for me is to say that Sheet names starting with "Doc*" and "Line*" should be left out of the total sheet numbers - but that is probably more complicated?

       

      Thanks in advance.

       

      /Peter

        • Re: Note on drawing showing current sheet number and total sheets MINUS 1
          Leon Wurr

          This code returns "j" as the number of sheets without "DOCS" or "LINE" on it's name.

           

           

          Now you just need to cycle through the sheets and use DrawingDoc::InsertNewNote2 to insert your notes.

           

          Option Explicit
          
          
          Dim swApp       As SldWorks.SldWorks
          Dim swDraw      As SldWorks.DrawingDoc
          Dim swSheet     As SldWorks.Sheet
          Dim swSelMgr    As SldWorks.SelectionMgr
          
          
          Dim intSheets   As Integer
          Dim i           As Integer
          Dim j           As Integer
          
          
          Dim strSheets() As String
          Dim strRmv1     As String
          Dim strRmv2     As String
          
          Sub main()
              Set swApp = Application.SldWorks
              Set swDraw = swApp.ActiveDoc
              
              intSheets = swDraw.GetSheetCount
              strSheets = swDraw.GetSheetNames
              j = 0
              strRmv1 = "DOCS"
              strRmv2 = "LINE"
              
              For i = 0 To intSheets - 1
                  If InStr(1, strSheets(i), strRmv1) < 1 And InStr(1, strSheets(i), strRmv2) < 1 Then j = j + 1
              Next i
              
              Debug.Print j
          
          
          End Sub
          
            • Re: Note on drawing showing current sheet number and total sheets MINUS 1
              John Stoltzfus

              Leon - Does it get the sheet names from the sheet tab ??

                • Re: Note on drawing showing current sheet number and total sheets MINUS 1
                  Leon Wurr

                  Yes, it gets the names that appear on the tabs:

                   

                  sheet.png

                   

                  The following line returns a matrix of strings with every sheet name on the active drawing document:

                   

                   

                  strSheets = swDraw.GetSheetNames

                   

                   

                   

                   

                    • Re: Note on drawing showing current sheet number and total sheets MINUS 1
                      John Stoltzfus

                      Thanks Leon -

                       

                      That's what I thought but it doesn't leave the "DOC" one out of the sequence... If it's the third tab it still says 3 of .....

                        • Re: Note on drawing showing current sheet number and total sheets MINUS 1
                          Leon Wurr

                          Not if you use another If statement to insert notes only on sheets that don't have "DOC" on it's name:

                           

                          x = 0
                          For k = 0 To intSheets - 1
                               If InStr(1, strSheets(k), strRmv1) < 1 And InStr(1, strSheets(k), strRmv2) < 1 Then
                                    x = x + 1
                                    strNote = "Page" & x & " of " & j
                                    'put the code here to insert note on drawing with the text of strNote
                               end if
                          Next k
                          
                              • Re: Note on drawing showing current sheet number and total sheets MINUS 1
                                Leon Wurr

                                Here:

                                 

                                Option Explicit
                                
                                
                                Dim swApp       As SldWorks.SldWorks
                                Dim swDraw      As SldWorks.DrawingDoc
                                Dim swSheet     As SldWorks.Sheet
                                Dim swSelMgr    As SldWorks.SelectionMgr
                                Dim swNote      As SldWorks.Note
                                
                                
                                Dim intSheets   As Integer
                                Dim i           As Integer
                                Dim j           As Integer
                                Dim k           As Integer
                                Dim x           As Integer
                                
                                
                                Dim strSheets() As String
                                Dim strRmv1     As String
                                Dim strRmv2     As String
                                Dim strNote     As String
                                
                                
                                Sub main()
                                    Set swApp = Application.SldWorks
                                    Set swDraw = swApp.ActiveDoc
                                    
                                    intSheets = swDraw.GetSheetCount
                                    strSheets = swDraw.GetSheetNames
                                    strRmv1 = "DOCS"
                                    strRmv2 = "LINE"
                                    
                                    j = 0
                                    For i = 0 To intSheets - 1
                                        If InStr(1, strSheets(i), strRmv1) < 1 And InStr(1, strSheets(i), strRmv2) < 1 Then j = j + 1
                                    Next i
                                    
                                    x = 0
                                    For k = 0 To intSheets - 1
                                        If InStr(1, strSheets(k), strRmv1) < 1 And InStr(1, strSheets(k), strRmv2) < 1 Then
                                            swDraw.ActivateSheet (strSheets(k))
                                            x = x + 1
                                            strNote = "Page " & x & " of " & j
                                            Debug.Print strNote
                                            Set swNote = swDraw.InsertNote(strNote)
                                        End If
                                    Next k
                                  
                                End Sub
                                
                                      • Re: Note on drawing showing current sheet number and total sheets MINUS 1
                                        Peter Larsen

                                        Hi Leon,

                                        Thank you very much for your work here!

                                        I am not used to work with scripts so how exactly do I "attach" it to a note on a drawing?

                                        And if I want it to exclude sheet names that begins with "Line" and "Doc" is that possible?

                                         

                                        Thanks.

                                         

                                          • Re: Note on drawing showing current sheet number and total sheets MINUS 1
                                            Leon Wurr

                                                 The current code is supposed to exclude sheets, that have "DOCS" and "LINE" on it's name, from the total number of sheets.

                                                 You can modify it for other words by modifying this lines:

                                             

                                                 strRmv1 = "DOCS"

                                                 strRmv2 = "LINE"

                                             

                                                 Whatever you put inside those quotes are the words the code will look for on the sheet's name, and it will keep the sheets that have a match for this words off the total number of sheets.

                                             

                                                 I'm note sure of what you meant by "how exactly do I 'attach' it to a note on a drawing".

                                             

                                                

                                              • Re: Note on drawing showing current sheet number and total sheets MINUS 1
                                                John Stoltzfus

                                                I'm not that familiar with the API side, however I already did make some modifications to different Macros..  I do have a few additional questions.

                                                 

                                                1.  In a multiple tab drawing file - does the DOC or LINE tab page need to be the first, last or any where in between?

                                                2.  In the Macro is there a difference from a few pages per file or 100 pages per file?

                                                 

                                                Thanks a lot

                                                • Re: Note on drawing showing current sheet number and total sheets MINUS 1
                                                  Peter Larsen

                                                  Leon,

                                                  1. I do not know how to put this macro into a drawing note/text box so it can be shown on a drawing sheet format as "Page X of Y" at the bottom of the sheet.
                                                  2. Can I update it by adding this at the end of the script:  swModel.ForceRebuild3 (True)
                                                  3. Regarding sheet names to be excluded, is it possible to write that all sheet names that BEGIN with "Line and "Doc" should be excluded, so that e.g. "Line 1", "Line 2", "Doc 1", "Doc 2" will all be excluded from total number of sheets?

                                                   

                                                  I hope I am not asking for too much here!

                                                  Thanks.

                                                  /Peter

                                                    • Re: Note on drawing showing current sheet number and total sheets MINUS 1
                                                      Leon Wurr

                                                      1-

                                                           Well, I see two ways of inserting the notes on your sheet.

                                                           The first way would be to search your sheet template for the text:          Page $PRP:"SW-Current Sheet(Current Sheet)" of $PRP:"SW-Total Sheets(Total Sheets)"

                                                           and replace it with:

                                                                "Page X of Y"

                                                           for every page of your drawing.

                                                       

                                                           The second way would be to insert the note on a fixed position based on the sheet size, given that the sheet doesn't already have a note with the text Page $PRP:"SW-Current Sheet(Current Sheet)" of $PRP:"SW-Total Sheets(Total Sheets)"

                                                       

                                                           Which one would work better for you? Remember that this won't be auto-updated, i.e., if you change the number of sheets or the sheet's position, the numbers shown on the note (Page X of Y) won't update.

                                                       

                                                      2-

                                                           Yes you could, but remove the parenthesis: swDraw.ForceRebuild3 True

                                                       

                                                      3-

                                                           The command InStr() is case sensitive, so make sure that you write the right text inside the quotes:

                                                          strRmv1 = "Doc"

                                                          strRmv2 = "Line"

                                                       

                                                           I've added some commands so that the code only excludes sheets with names that begin with "Doc" or "Line", try it out:

                                                       

                                                      Option Explicit
                                                      
                                                      
                                                      Dim swApp       As SldWorks.SldWorks
                                                      Dim swDraw      As SldWorks.DrawingDoc
                                                      Dim swSheet     As SldWorks.Sheet
                                                      Dim swSelMgr    As SldWorks.SelectionMgr
                                                      Dim swNote      As SldWorks.Note
                                                      
                                                      
                                                      Dim intSheets   As Integer
                                                      Dim i           As Integer
                                                      Dim j           As Integer
                                                      Dim k           As Integer
                                                      Dim x           As Integer
                                                      
                                                      
                                                      Dim strSheets() As String
                                                      Dim strRmv1     As String
                                                      Dim strRmv2     As String
                                                      Dim strNote     As String
                                                      
                                                      
                                                      Sub main()
                                                          Set swApp = Application.SldWorks
                                                          Set swDraw = swApp.ActiveDoc
                                                          
                                                          intSheets = swDraw.GetSheetCount
                                                          strSheets = swDraw.GetSheetNames
                                                          strRmv1 = "Doc"
                                                          strRmv2 = "Line"
                                                          
                                                          j = 0
                                                          For i = 0 To intSheets - 1
                                                              If InStr(1, Left(strSheets(i), Len(strRmv1)), strRmv1) < 1 And InStr(1, Left(strSheets(i), Len(strRmv2)), strRmv2) < 1 Then j = j + 1
                                                          Next i
                                                          
                                                          x = 0
                                                          For k = 0 To intSheets - 1
                                                              If InStr(1, Left(strSheets(k), Len(strRmv1)), strRmv1) < 1 And InStr(1, Left(strSheets(k), Len(strRmv2)), strRmv2) < 1 Then
                                                                  swDraw.ActivateSheet (strSheets(k))
                                                                  x = x + 1
                                                                  strNote = "Page " & x & " of " & j
                                                                  Set swNote = swDraw.InsertNote(strNote)
                                                              End If
                                                          Next k
                                                          
                                                          swDraw.ForceRebuild3 True
                                                          MsgBox "Notes successfully added!"
                                                      
                                                      
                                                      End Sub
                                                      
                                                        • Re: Note on drawing showing current sheet number and total sheets MINUS 1
                                                          Peter Larsen

                                                          Thanks a lot, Leon! I tested the script and it works perfect.

                                                           

                                                          1. It is best if the script inserts the note at a fixed position on the sheet format (not on the drawing itself as the script now does) so it cannot be moved.

                                                          2. How can we define the formatting of the note? See below screen shot.

                                                          3. If it needs to be updated, can I then delete the note and run the script again?

                                                           

                                                          Thanks again.

                                                            • Re: Note on drawing showing current sheet number and total sheets MINUS 1
                                                              Leon Wurr

                                                                   Damn, you owe me a beer dude lol, this macro got much longer than I thought it would be.

                                                               

                                                                   1 - Now the macro adds the notes to the Sheet Template, so you won't be able to move it. You will have to get to the "Edit Sheet Format" interface to move/delete it.

                                                                   The note's positions are set based on the sheet size, you'll have to adjust these values according to where you want to place them on your templates:

                                                               

                                                                  'Positions of notes, values are in meters by default:

                                                                  'A4 SHEET POSITION

                                                                  XA4 = 0.14      'This is the horizontal position of the upper left corner of note

                                                                  YA4 = 0.005     'This is the vertical position of the upper left corner of note

                                                               

                                                                  'A3 SHEET POSITION

                                                                  XA3 = 0.35

                                                                  YA3 = 0.005

                                                                 

                                                                  'A2 SHEET POSITION

                                                                  XA2 = 0.524

                                                                  YA2 = 0.005

                                                               

                                                                  'A1 SHEET POSITION

                                                                  XA1 = 0.771

                                                                  YA1 = 0.005

                                                               

                                                                  'A0 SHEET POSITION

                                                                  XA0 = 0.1119

                                                                  YA0 = 0.005

                                                               

                                                               

                                                                   2 - The notes are formatted as per request, minus the font type "Calibri", I couldn't get that to work:

                                                               

                                                                   strNote = "<FONT face=calibri style=B size=3.5>Page " & x & " of " & j

                                                                  

                                                                   3 - If you need to update the notes, just run the code again, it will delete the old notes and insert new ones with the correct number of sheets, but be careful as the code will delete any notes that begin with the word "Page" that are sitting on the sheet template.

                                                                   If you want, you can change this behavior by deleting the following lines, but then you will have to manually delete the outmoded notes.

                                                               

                                                                          'Routine to delete already existing notes that begins with the text "Page":

                                                                          Set swView = swDraw.GetFirstView

                                                                          Set swSheet = swDraw.GetCurrentSheet

                                                                          Set swNote = swView.GetFirstNote

                                                                          Do While Not swNote Is Nothing

                                                                              If InStr(1, Left(swNote.GetText, 4), "Page") > 0 Or InStr(1, Left(swNote.GetText, 4), "PAGE") > 0 Then

                                                                                  swModel.ClearSelection2 True

                                                                                  swModel.Extension.SelectByID2 swNote.GetName & "@" & swSheet.GetSheetFormatName, "NOTE", 0, 0, 0, False, 0, Nothing, 0

                                                                                  swModel.EditDelete

                                                                              End If

                                                                              Set swNote = swNote.GetNext

                                                                          Loop

                                                               

                                                                   Hope this helps.

                                                                 

                                                                   Hey John Stoltzfus , check out this changes, maybe now the macro will be more usable .

                                                              • Re: Note on drawing showing current sheet number and total sheets MINUS 1
                                                                Deepak Gupta

                                                                Here is what needs to be changed to get the note formatting work

                                                                 

                                                                Dim swTextFormat                As SldWorks.TextFormat

                                                                               

                                                                                    Set swNote = swDraw.InsertNote(strNote)

                                                                                    Set swAnno = swNote.GetAnnotation

                                                                                    Set swTextFormat = swAnno.GetTextFormat(0)

                                                                                    swTextFormat.CharHeight = 0.0035

                                                                                    swTextFormat.Bold = True

                                                                                    swTextFormat.TypeFaceName = "Calibri"

                                                                                    swAnno.SetTextFormat 0, False, swTextFormat                   

                                                                                   

                                                                            'Routine to insert the note based on the sheet size:

                                                                            varSheetProps = swSheet.GetProperties

                                                                            Select Case varSheetProps(0)

                                                                              

                                                                Case 7 'A4

                                                                Set swAnno = swNote.GetAnnotation

                                                                swAnno.SetPosition2 XA4, YA4, 0

                                                                                                       

                                                                Case 8 'A3                

                                                                Set swAnno = swNote.GetAnnotation

                                                                swAnno.SetPosition2 XA3, YA3, 0

                                                                                   

                                                                Case 9 'A2

                                                                Set swAnno = swNote.GetAnnotation

                                                                swAnno.SetPosition2 XA2, YA2, 0

                                                                                   

                                                                Case 10 'A1

                                                                Set swAnno = swNote.GetAnnotation

                                                                swAnno.SetPosition2 XA1, YA1, 0

                                                                                   

                                                                Case 11 'A0

                                                                Set swAnno = swNote.GetAnnotation

                                                                swAnno.SetPosition2 XA0, YA0, 0                  

                                                                                                       

                                                                End Select