8 Replies Latest reply on Oct 11, 2012 6:36 AM by John Pesaturo

    Help with creating a basic Macro ...

    John Pesaturo

      I could use some help creating a basic Macro. I've attempted to create it within existing drawings, but it reproduces varying results in different drawings. Some drawings it selects most items, and some drawings it select almost none. I'm hoping to create a Macro that will work across the board for me and other in my workplace. The issue has arisen from finding a work station that was generating colored dimensions and annotations without anyone's knowledge. Now the problem exists of fixing any of these drawings as they are found. I'll list what I would like to do in order of operation, and hopefully this will be enough info for a good working Macro. If I need to specify anything else please let me know.

       

      1. Under the "Edit" Pull down - Select All or Ctrl A (essentially, I want to select all dimensions and annotations)
      2. On the "Line Format" toolbar - Change Line Color to "Default" (this is to change all dimensions and annotations to the default system settings)
      3. Stop Macro

       

      That's it ... I know it's very basic, but it just doesn't seem to work right for me as mentioned above. Any help would be greatly appreciated.

        • Re: Help with creating a basic Macro ...
          Michael Jeffries

          Try this:

           

           

           

          Dim swApp               As SldWorks.SldWorks

          Dim swModel             As ModelDoc2

          Dim swDraw              As DrawingDoc

          Dim swSheet             As Sheet

          Dim swView              As View

           

           

          Dim vSheets             As Variant

          Dim swDispDim           As DisplayDimension

          Dim swAnn               As Annotation

          Dim swDim               As Dimension

          Dim sTextFormat As TextFormat

           

           

          Dim swColorTable As ColorTable

          Dim standardCount As Long

          Dim count As Long

          Dim colorName As String

          Dim colorRef As Long

           

           

          Sub main()

           

           

          Set swApp = New SldWorks.SldWorks

          Set swModel = swApp.ActiveDoc

           

           

          Select Case swModel.GetType

              Case swDocumentTypes_e.swDocDRAWING

                  Set swDraw = swModel

                  vSheets = swDraw.GetSheetNames

                  swDraw.ActivateSheet vSheets(0)

                  Set swSheet = swDraw.GetCurrentSheet

              Case Else

                  swApp.SendMsgToUser ("Open a drawing document and try again.")

                  Exit Sub

          End Select

          i = 0

           

           

          'Set swColorTable = swApp.GetColorTable

          '

          'standardCount = swColorTable.GetBasicColorCount

          'Debug.Print "SolidWorks standard user-interface element and COLORREF value:"

          '' Iterate through standard colors

          'For count = 0 To (standardCount - 1)

          '    ' Get the entry name

          '    colorName = swColorTable.GetNameAtIndex(count)

          '    If colorName <> "" Then

          '        ' Get the entry's COLORREF

          '        colorRef = swColorTable.GetColorRefAtIndex(count)

          '        Debug.Print "  " & colorName & " : " & colorRef

          '    Else

          '    End If

          'Next count

           

           

           

           

          While Not swSheet Is Nothing

          'For i = 0 To UBound(vSheets)

              Debug.Print "**************************************************"

              Debug.Print "********************** " & swSheet.GetName & " ********************"

              Debug.Print "**************************************************"

              Set swView = swDraw.GetFirstView

              While Not swView Is Nothing

                  Debug.Print swView.GetName2

                  Set swDispDim = swView.GetFirstDisplayDimension5

                 

                  '************ DISPLAY DIMENSION DATA ****************

                 

                  While Not swDispDim Is Nothing

                      Set swAnn = swDispDim.GetAnnotation

                      Set swDim = swDispDim.GetDimension

                      swAnn.Select True

                 

                      Debug.Print "      Display Dimension: " & swDim.FullName

                      swModel.SelectByName swSelectType_e.swSelDIMENSIONS, swDim.FullName

                      swAnn.Color = 0 'Black

                     

                     

                      swAnn.SetTextFormat 0, True, swAnn.GetTextFormat(0)

                      Set swDispDim = swDispDim.GetNext3

                  Wend

                 

                 

                  '************ ANNOTATION DATA ****************

                  Set swAnn = swView.GetFirstAnnotation

                

                 

                  While Not swAnn Is Nothing

                      Select Case swAnn.GetType

                          Case swAnnotationType_e.swNote

                              Debug.Print swAnn.GetName

                              swAnn.Select True

                           

                              swAnn.UseDocDispFrame = True

                              swAnn.UseDocDispLeader = True

                             

                              swAnn.SetTextFormat 0, True, swAnn.GetTextFormat(0)

                              swAnn.Color = 0

                          Case Else

                              'do nothing

                      End Select

                     

                     

              

                     

                      Set swAnn = swAnn.GetNext

                  Wend

                 

                 

                  Set swView = swView.GetNextView

              Wend

           

           

              Select Case i

                  Case UBound(vSheets)

                      'DO NOTHING

                     

                      Exit Sub

                  Case Else

              End Select

                

              i = i + 1

              swDraw.ActivateSheet vSheets(i)

              Set swSheet = swDraw.GetCurrentSheet

          Wend

          'Next i

           

           

           

           

          End Sub

            • Re: Help with creating a basic Macro ...
              John Pesaturo

              That works, but it literally grabs everything in the entire drawing ... Is there a way to just select the dimensions and annotations that are on the sheet, but avoid everything that's locked inside of the sheet format?

               

              Oh, and I must say that is one heck of a Macro. I didn't think my "basic Macro" would be nearly as involved.

                • Re: Help with creating a basic Macro ...
                  Michael Jeffries

                  It shouldn't grab everything in the drawing, only Annotations and Dimensions (and their annotations).  Does it reset the color?  I was having trouble getting it to override the color if the color was set by the format bar.  I suspect it grabbed stuff in your title block because it's part of the drawing and not a template?  If you right click >>> Edit sheet format, is it blank?

                   

                  Mine only grabbed the dimensions and notes:

                   

                  Untitled.png

                    • Re: Help with creating a basic Macro ...
                      John Pesaturo

                      Yes, it reset the color OK. It just grabbed everything within the sheet format as well and changed colors and sizes. I've attached the document I used to trouble shoot the problem to begin with for reference as to the sheet format setup.

                       

                      On a side note, someone had mentioned to attach zip files in a group rather then individually, but I can't seem to figure out how to do that. The selection box when attaching documents won't let me ctrl select multiple items.

                        • Re: Help with creating a basic Macro ...
                          Michael Jeffries

                          Ok, not sure why it's grabbing items in your Sheet Format versus mine, but the simple solution (since you're just trying to fix color) is to comment out or delete the following lines:

                           

                          'swAnn.SetTextFormat 0, True, swAnn.GetTextFormat(0)

                           

                          Furthermore, you don't need it to select while it goes along, so you can also comment out the following lines:

                           

                          'swAnn.Select True

                           

                           

                          I'll look into why it's changing your title block later, but keep in mind this fix will still change the color of the title block items (keep in mind, this macro loops through all of the views on a sheet and gets the annotations and dimensions, changes the color, then cycles to the next annotation >>> display dimension >>> view >>> sheet).  If you have more than one sheet, it will change them as well.

                           

                          The loop:

                          **********************************************

                          Sub Main()

                          ModelDoc2

                              

                          DrawingDoc

                               Sheet

                                    View   

                                         Display Dimension

                                         Annotation (Notes Only)

                                    Next View

                               Next Sheet

                          End Sub

                          **********************************************

                           

                          To add .zip files together, highlight the files in windows explorer >> right click >>> send to >>> Compressed Zip Folder

                            • Re: Help with creating a basic Macro ...
                              John Pesaturo

                              I have to admit to being in over my head here. To be honest it would be just as easy to manually window select everything on screen and simply change the line color to default. I had just hoped there was a way to get a quick Macro to do the same. I've tried the Macro "record" button a number of times now, and it just doesn't work the same way on every drawing. All in all, it simply leaves me more room to broaden my knowledge base and learn some programming language. I thank you for your time all the same and greatly appreciate the help.