5 Replies Latest reply on Aug 11, 2015 2:11 PM by Josh Hixon

    Trying to create a macro to change drawing units to inches 3 decimal places, can someone help me out?

    Josh Hixon

      I am really new at trying to write macros, but am not great at using it with the solidworks API yet. So i am hoping to get someone to write a macro for me. We change from fraction (IN) to decimal (.123) in our drawings when we go from customer to production. I am hoping to get  a macro that will make this a single click action. Also, if possible add notes in the macro to allow me to learn what is happening. I am working in solidworks 2015.  Trying to kill two birds with one stone here.  thank you in advance for the help. 

        • Re: Trying to create a macro to change drawing units to inches 3 decimal places, can someone help me out?
          Matt Peneguy

          I pieced this together some time ago from web resources.

          I was trying to get it to scan the document and set all reference dims to a different tolerance than the model dimensions.

          I only got as far as scanning selected dimensions.

          You should be able to take it and modify it to suit your needs:



          Dim myInstance As IModelDoc2

          Dim myLength As Integer



          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swSelMgr As SldWorks.SelectionMgr

          Dim swDisplayDimension As SldWorks.DisplayDimension

          Dim swDimension As SldWorks.Dimension

          Dim swDimensionTolerance As SldWorks.DimensionTolerance

          ' scans selected dimensions

          ' if the dimension is a model dimension, it sets the tolerance LIMIT to

          ' 4 decimal places







          Sub main()



              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swSelMgr = swModel.SelectionManager



              Dim i As Integer


              For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)

                  If swSelMgr.GetSelectedObjectType3(i, -1) = swSelDIMENSIONS Then

                  Set swDisplayDimension = swSelMgr.GetSelectedObject6(i, 0)

                 'make sure we have a dimension


                      Set swDimension = swDisplayDimension.GetDimension

                      Set swDimensionTolerance = swDimension.Tolerance

                      'MsgBox (swDimension.DrivenState)

                      myDriven = swDimension.DrivenState



                      If myDriven = 1 Then 'Reference dimension, set to two decimal place

                          'myTol = 2

                          'Skip driven dimensions

                          'swDimensionTolerance.Type = swTolNONE ' set no tolerance

                      ElseIf myDriven = 2 Then 'Model dimension, set to 4 decimal places

                          myTol = 4

                          swDimensionTolerance.Type = swTolLIMIT ' set toleranced limit


                      End If



                      myval1 = swDisplayDimension.SetUnits(False, swINCHES, swDECIMAL, 1, False)

                      myval = swDisplayDimension.SetPrecision2(myTol, myTol, myTol, myTol)


                      'MsgBox (swDimensionTolerance.Type)

                  End If

              Next i



          swModel.ClearSelection2 (True)

          End Sub