25 Replies Latest reply on Oct 18, 2018 2:58 PM by Ben Langdon

    Macro to Load Dimension Styles

    Ben Langdon

      So i want to make a macro that will load the styles i have already made for dimensions. i know its just a couple clicks to imput them into a new drawing, but i would like to have it load them in with a shortcut just so i don't have to do the same thing all the time when opening old documents that i need to load them into.

       

      I tried recording it but it didn't take down any of the things i did so i don't really have anything to start off of

        • Re: Macro to Load Dimension Styles
          Deepak Gupta

          I think you need to use LoadStyle method.

          • Re: Macro to Load Dimension Styles
            Gennadiy Tsybikov

            Use IAnnotation::LoadStyle method. You have to first access IAnnotation Interface and then use LoadStyle method.

            I created a macro that Loads Style for dimensions. Just assign strPathName to your template path and try if it works for you. See below:

             

            Sub main()

                Dim swApp As SldWorks.SldWorks

                Dim swModel As SldWorks.ModelDoc2

                Dim swDrawDoc As SldWorks.DrawingDoc

                Dim swAnnot As SldWorks.Annotation

                Dim swSheet As SldWorks.Sheet

                Dim swView As SldWorks.View

                Dim vViews As Variant

                Dim vAnnots As Variant

                Dim intAnnotCount As Integer

                Dim strSheetNames() As String

                Dim intSheetCount As Integer

                Dim strPathName As String

                Dim i As Integer

                Dim j As Integer

                Dim k As Integer

               

                strPathName = "C:\..."

               

                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc

                Set swDrawDoc = swModel

                intSheetCount = swDrawDoc.GetSheetCount

                ReDim strSheetNames(1 To intSheetCount) As String

                strSheetNames = swDrawDoc.GetSheetNames

               

                For i = 0 To intSheetCount - 1

                    Set swSheet = swDrawDoc.Sheet(strSheetNames(i))

                    vViews = swSheet.GetViews

                   

                    For j = 0 To UBound(vViews)

                        Set swView = vViews(j)

                        vAnnots = swView.GetAnnotations

                        intAnnotCount = swView.GetAnnotationCount

                       

                        If intAnnotCount > 0 Then

                            For k = 0 To UBound(vAnnots)

                                Set swAnnot = vAnnots(k)

                                swAnnot.LoadStyle (strPathName)

                            Next k

                        End If

                    Next j

                Next i

                     

            End Sub

              • Re: Macro to Load Dimension Styles
                Deepak Gupta

                Does it work for you? I can not get the LoadStyle method to work?

                  • Re: Macro to Load Dimension Styles
                    Gennadiy Tsybikov

                    Yes it works fine for me.

                      • Re: Macro to Load Dimension Styles
                        Deepak Gupta

                        Gennadiy Tsybikov wrote:

                         

                        Yes it works fine for me.

                        Your SOLIDWORKS version please

                          • Re: Macro to Load Dimension Styles
                            Gennadiy Tsybikov

                            SOLIDWORKS Pro 2017 x64 Edition, SP 5.0

                              • Re: Macro to Load Dimension Styles
                                Deepak Gupta

                                I've tried on 2017 as well but still not working. Here are my codes:

                                Option Explicit

                                Dim SwApp           As SldWorks.SldWorks

                                Dim swModel         As SldWorks.ModelDoc2

                                Dim SwView          As View

                                Dim swDispDim       As SldWorks.DisplayDimension

                                Dim swDim           As SldWorks.Dimension

                                Dim myAnnotation    As SldWorks.Annotation

                                Sub Main()

                                 

                                  Set SwApp = Application.SldWorks

                                  Set swModel = SwApp.ActiveDoc

                                  Set SwView = swModel.GetFirstView

                                  Set SwView = SwView.GetNextView

                                  Set swDispDim = SwView.GetFirstDisplayDimension

                                  Set myAnnotation = swDispDim.GetAnnotation()

                                  myAnnotation.LoadStyle "C:\Users\DEEPAK\Desktop\Style1.sldstl"

                                 

                                End Sub

                                  • Re: Macro to Load Dimension Styles
                                    Gennadiy Tsybikov

                                    I've tried your code and it works on my SolidWorks version as well.

                                    What is not working for you? Is the template not loading or you're getting an error on LoadStyle method?

                                    • Re: Macro to Load Dimension Styles
                                      Ben Langdon
                                      Gennadiy Tsybikov

                                       

                                        • Re: Macro to Load Dimension Styles
                                          Deepak Gupta

                                          Are you using model items Or the dimensions were added into the drawing?

                                          • Re: Macro to Load Dimension Styles
                                            Gennadiy Tsybikov

                                            Thank you Deepack for pointing that out. Our two macros work only on dimensions added into the drawing. So if you want to change dimension style of model dimensions you have to do run the macro at a part level. It's weird but I have to first run macro to Load Style at a part level and also run second macro to Load Style at a drawing level. Then I was able to see it in the style drop down property.

                                             

                                            Use these two lines at a part level:

                                            Set myAnnotation = swModel.GetFirstAnnotation2

                                            myAnnotation.LoadStyle "Template Path"

                                              • Re: Macro to Load Dimension Styles
                                                Deepak Gupta

                                                Gennadiy Tsybikov wrote:

                                                 

                                                Thank you Deepak for pointing that out. Our two macros work only on dimensions added into the drawing. So if you want to change dimension style of model dimensions you have to do run the macro at a part level. It's weird but I have to first run macro to Load Style at a part level and also run second macro to Load Style at a drawing level. Then I was able to see it in the style drop down property.

                                                But if it works manually then it should work via API as well.

                                                  • Re: Macro to Load Dimension Styles
                                                    Ben Langdon

                                                    Gennadiy Tsybikov

                                                     

                                                    I'm not to sure what you guys are saying. I just want to open old drawings, hit the macro, then have my dimension styles load so that i can change dimensions. I have already loaded all the styles i want into my templates. man i really love dim styles, its only been like a week or two knowing about them but dam i am kicking myself for not knowing this earlier. Sooooooooooo many fixed old documents with correct tolerances done with out dim style

                                                     

                                                      • Re: Macro to Load Dimension Styles
                                                        Deepak Gupta

                                                        Ben, try the following codes in an active drawing. In the codes below, the macro is loading 3 styles. Change the style path and add more lines if there are many styles you want to load.

                                                         

                                                        Option Explicit
                                                        
                                                        Dim SwApp          As SldWorks.SldWorks
                                                        Dim swModel        As SldWorks.ModelDoc2
                                                        Dim SwView          As View
                                                        Dim swDispDim      As SldWorks.DisplayDimension
                                                        Dim swDim          As SldWorks.Dimension
                                                        Dim myAnnotation    As SldWorks.Annotation
                                                        
                                                        Sub Main()
                                                        
                                                          Set SwApp = Application.SldWorks
                                                          Set swModel = SwApp.ActiveDoc
                                                          Set SwView = swModel.GetFirstView
                                                          Set SwView = SwView.GetNextView
                                                          Set swDispDim = SwView.GetFirstDisplayDimension
                                                          Set myAnnotation = swDispDim.GetAnnotation()
                                                          myAnnotation.LoadStyle "C:\Users\DEEPAK\Desktop\Style1.sldstl"
                                                          myAnnotation.LoadStyle "C:\Users\DEEPAK\Desktop\Style2.sldstl"
                                                          myAnnotation.LoadStyle "C:\Users\DEEPAK\Desktop\Style3.sldstl"
                                                        
                                                        End Sub
                                                        
                                                          • Re: Macro to Load Dimension Styles
                                                            Ben Langdon

                                                            So i put your code into my macro file and i couldn't get it to work, i deleted stuff and added stuff at the top of the code but nothing really worked. the variable SwViewSwView says its not defined. I took a pic just to be clear what i have written in your code.Capture56566.JPG

                                                              • Re: Macro to Load Dimension Styles
                                                                Deepak Gupta

                                                                Ben Langdon wrote:

                                                                 

                                                                So i put your code into my macro file and i couldn't get it to work, i deleted stuff and added stuff at the top of the code but nothing really worked. the variable SwViewSwView says its not defined. I took a pic just to be clear what i have written in your code.

                                                                Sorry Ben, looks like the codes got messed up.

                                                                 

                                                                Try these and change path as needed.

                                                                 

                                                                Option Explicit 

                                                                 

                                                                Dim SwApp          As SldWorks.SldWorks 

                                                                Dim swModel        As SldWorks.ModelDoc2 

                                                                Dim SwView          As View 

                                                                Dim swDispDim      As SldWorks.DisplayDimension 

                                                                Dim swDim          As SldWorks.Dimension 

                                                                Dim myAnnotation    As SldWorks.Annotation 

                                                                 

                                                                Sub Main() 

                                                                 

                                                                  Set SwApp = Application.SldWorks 

                                                                  Set swModel = SwApp.ActiveDoc 

                                                                  Set SwView = swModel.GetFirstView 

                                                                  Set SwView = SwView.GetNextView 

                                                                  Set swDispDim = SwView.GetFirstDisplayDimension 

                                                                  Set myAnnotation = swDispDim.GetAnnotation() 

                                                                  myAnnotation.LoadStyle "C:\Users\DEEPAK\Desktop\Style1.sldstl" 

                                                                 

                                                                End Sub 

                                                                • Re: Macro to Load Dimension Styles
                                                                  Deepak Gupta

                                                                  Ben Langdon wrote:

                                                                   

                                                                  So i put your code into my macro file and i couldn't get it to work, i deleted stuff and added stuff at the top of the code but nothing really worked. the variable SwViewSwView says its not defined. I took a pic just to be clear what i have written in your code.

                                                                  Sorry Ben, looks like the codes got messed up.

                                                                   

                                                                  Try these and change path as needed.

                                                                   

                                                                  Option Explicit 

                                                                   

                                                                  Dim SwApp          As SldWorks.SldWorks 

                                                                  Dim swModel        As SldWorks.ModelDoc2 

                                                                  Dim SwView          As View 

                                                                  Dim swDispDim      As SldWorks.DisplayDimension 

                                                                  Dim swDim          As SldWorks.Dimension 

                                                                  Dim myAnnotation    As SldWorks.Annotation 

                                                                   

                                                                  Sub Main() 

                                                                   

                                                                    Set SwApp = Application.SldWorks 

                                                                    Set swModel = SwApp.ActiveDoc 

                                                                    Set SwView = swModel.GetFirstView 

                                                                    Set SwView = SwView.GetNextView 

                                                                    Set swDispDim = SwView.GetFirstDisplayDimension 

                                                                    Set myAnnotation = swDispDim.GetAnnotation() 

                                                                    myAnnotation.LoadStyle "C:\Users\DEEPAK\Desktop\Style1.sldstl" 

                                                                   

                                                                  End Sub 

                                                                    • Re: Macro to Load Dimension Styles
                                                                      Ben Langdon

                                                                      thanks so much, its working now. i do have one question though, can i have it not change the first dimension when i run the macro. right now when i run the macro the first dimension changes to whatever the last loaded dim style is. i am guesing it has something to do with SwView.GetFirstDisplayDimension but everything i changed it didn't work.