3 Replies Latest reply on Dec 11, 2012 4:19 PM by Keith Rice

    how do I get a solidworks sketch dimensional value

    Michael Skinner

      in order to use it in another sketch dimension function?

       

      Retreiving the value and setting the function using VB code?

        • Re: how do I get a solidworks sketch dimensional value
          Keith Rice

          Hi Michael,

           

          Welcome to the forums. The following code will change the value of D2@Sketch1 to the value of D1@Sketch1. You can change the names of the dimensions to whatever you need for it to work in your context.

           

          =========

           

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swDim As SldWorks.Dimension

          Dim vDimVal As Variant

           

          Sub main()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swDim = swModel.Parameter("D1@Sketch1")

              vDimVal = swDim.GetSystemValue3(swThisConfiguration, Empty)

              Set swDim = swModel.Parameter("D2@Sketch1")

              swDim.SetSystemValue3 vDimVal(0), swThisConfiguration, Empty

          End Sub

           

          =========

           

          Alternatively, if you need to programmatically determine the name of the dimension, you can use IDimension::GetNameForSelection or IDisplayDimension::GetNameForSelection. Note the difference between IDisplayDimension and IDimension. The former is what is visble to the user---it is the dimension annotation. The latter, however, is what actually has the dimension value.

           

          There's many more code examples that you might find useful at my web site, and we also cover this material in Lesson 4.1.

           

          Keith

          Video Tutorials for the SolidWorks API

            • Re: how do I get a solidworks sketch dimensional value
              Michael Skinner

              thanks Keith,

              maybe I didn't make myself clear enough in the beginning. So if the following makes the values of D1@Sketch1 & D2@Sketch1 the same then..

              =====

              Set swDim = swModel.Parameter("D1@Sketch1")

                  vDimVal = swDim.GetSystemValue3(swThisConfiguration, Empty)

                  Set swDim = swModel.Parameter("D2@Sketch1")

                  swDim.SetSystemValue3 vDimVal(0), swThisConfiguration, Empty

              =====

               

              then this will make the value of D2@Sketch1 equal to 1/4 the value of D1@Sketch1?

               

              =====

              Set swDim = swModel.Parameter("D1@Sketch1" * 0.25)

                  vDimVal = swDim.GetSystemValue3(swThisConfiguration, Empty)

                  Set swDim = swModel.Parameter("D2@Sketch1")

                  swDim.SetSystemValue3 vDimVal(0), swThisConfiguration, Empty

              =====

               

              thank you in advance

              Michael

                • Re: how do I get a solidworks sketch dimensional value
                  Keith Rice

                  Hi Michael,

                   

                  No, IModelDoc2::Parameter does not get or set the dimension value, it gets the IDimension pointer. So if your goal is to make D2 1/4th the size of D1 then you need this:

                   

                  =======

                   

                  Dim swApp As SldWorks.SldWorks

                  Dim swModel As SldWorks.ModelDoc2

                  Dim swDim As SldWorks.Dimension

                  Dim vDimVal As Variant

                   

                  Sub main()

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

                      Set swDim = swModel.Parameter("D1@Sketch1")

                      vDimVal = swDim.GetSystemValue3(swThisConfiguration, Empty)

                      Set swDim = swModel.Parameter("D2@Sketch1")

                      swDim.SetSystemValue3 vDimVal(0) * 0.25, swThisConfiguration, Empty

                  End Sub

                   

                  =======

                   

                  Keith

                  Video Tutorials for the SolidWorks API