8 Replies Latest reply on Sep 8, 2017 1:01 PM by Mike Landro

    Macro to add equation using named dimension variable

    Mike Landro

      Hi All,

         I am new to macros and am looking for a little help getting a macro to work the way I want it to. I am using an input box for the user to input the sketch number that contains a named dimension I want to use in the equation. I am able to get all of the variables to show up the way I want in the code but do not know how to get them to write into the equation list in my part when I am using the variables. If I hard code the sketch names they write into the equation list fine. I am thinking I do not have the variables called out properly...

       

      Thanks, Mike

       

      Sub main()

      Set swApp = Application.SldWorks

      Set Part = swApp.ActiveDoc

      boolstatus = Part.Extension.SelectByID2("Equations", "EQNFOLDER", 0, 0, 0, False, 0, Nothing, 0)

      Dim nmbText As String

          nmbText = InputBox("Specify The Sketch Number") 'user enters sketch number with the named dimensions in it

          Dim nmb As Integer
          Dim wnText As String
          Dim lnText As String
          Dim WText As String
          Dim LText As String

          nmb = CInt(nmbText) 'this is the sketch number
          wn = "W@Sketch" & nmb 'full width sketch name
          ln = "L@Sketch" & nmb 'full length sketch name

      Dim swEquationMgr As Object

      Set swEquationMgr = Part.GetEquationMgr()

      'the following worked as long as my sketch is named Sketch1 frequently this is something other than Sketch1
      'swEquationMgr.Add -1, """SquareFeet""= ""W@Sketch1"" * ""L@Sketch1"" / 144"

      'the follwing does not work attempting to use variables to make the equation


      swEquationMgr.Add -1, """SquareFeet""= wn * nmb / 144"

       

      boolstatus = Part.ForceRebuild()

      Part.Save

      End Sub

        • Re: Macro to add equation using named dimension variable
          Nilesh Patel

          Hi Mike,

           

          If you want use variables in the equation then you will need to add them first before you can use them. In your code above, if you want to use 'wn' and 'ln' in the equation, then you will need to add them first using equation manager.

           

          I believe 'swEquationMgr.Add -1, """SquareFeet""= wn * nmb / 144"' should be 'swEquationMgr.Add -1, """SquareFeet""= wn * ln/ 144"'

           

          Hope this helps.

           

          Regards,

            • Re: Macro to add equation using named dimension variable
              Mike Landro

              Hello Nilesh,

                   Thanks for the reply. Assuming the user entered sketch number 3 in the input box this is what I would like the equation to look like in the equation manager after running this macro  "SquareFeet"= "W@Sketch3" * "L@Sketch3" / 144 I only want to use the variables in the code to combine the dimension name with the sketch number coming from the user input box. is there a way to get the variable values to be used in the formula?      

               

                  nmb = CInt(nmbText)           'this is the sketch number coming from the user input box

                  wn = "W@Sketch" & nmb           'full width dimension sketch name

                   ln = "L@Sketch" & nmb           'full length dimension sketch name

               

                   swEquationMgr.Add -1, """SquareFeet""= ""wn"" * ""ln""/ 144"      'Need the variable values for wn and ln to be written in the equation

               

              Thanks, Mike

                  

                

               

               

               

                • Re: Macro to add equation using named dimension variable
                  Nilesh Patel

                  Hi Mike,

                   

                  If you just want to use variables 'wn' and 'ln' for calculation purpose then it is possible but you will have to get the dimension values not the name, but if you want to add an equation  """SquareFeet""= ""wn"" * ""ln""/ 144", then you will need to add 'wn' and 'ln' as global variables in equation manager using IEquationMgr.Add or Add2 method first. Here are some test codes:

                   

                  Option Explicit
                  
                  
                  Sub main()
                  
                  
                      Dim swApp       As SldWorks.SldWorks
                      Dim swModel     As ModelDoc2
                      Dim swSelMgr    As SelectionMgr
                      Dim swDisDim    As DisplayDimension
                      Dim swDim       As Dimension
                      Dim swEqnMgr    As EquationMgr
                      Dim wn          As Double
                      Dim ln          As Double
                      Dim SqrFeet     As Double
                  
                  
                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                      
                      'Add equations
                      Set swEqnMgr = swModel.GetEquationMgr
                      Debug.Print swEqnMgr.Add2(-1, """SquareFeet"" = ""W@Sketch1"" * ""L@Sketch1"" / 144", False)
                      Debug.Print swEqnMgr.Add2(-1, """wn""=""W@Sketch1""", False)
                      Debug.Print swEqnMgr.Add2(-1, """ln""=""L@Sketch1""", False)
                      Debug.Print swEqnMgr.Add2(-1, """SquareFeet1"" = ""wn"" * ""ln"" / 144", False)
                      swEqnMgr.EvaluateAll
                      
                      'Calculate SquareFeet using variable.
                      Set swSelMgr = swModel.SelectionManager
                      
                      'Select "W@Sketch"
                      swModel.Extension.SelectByID2 "W@Sketch1", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0
                      Set swDisDim = swSelMgr.GetSelectedObject6(1, -1)
                      Set swDim = swDisDim.GetDimension2(0)
                      Debug.Print swDim.FullName
                      wn = swDim.GetSystemValue2("") * 1000
                      swModel.ClearSelection2 True
                      
                      'Select "L@Sketch"
                      swModel.Extension.SelectByID2 "L@Sketch1", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0
                      Set swDisDim = swSelMgr.GetSelectedObject6(1, -1)
                      Set swDim = swDisDim.GetDimension2(0)
                      Debug.Print swDim.FullName
                      ln = swDim.GetSystemValue2("") * 1000
                      swModel.ClearSelection2 True
                      
                      'Calculates Square Feet
                      SqrFeet = wn * ln / 144
                      
                      Debug.Print SqrFeet
                      
                      swModel.EditRebuild3
                  End Sub
                  

                   

                  Assuming that you have 'Sketch1' and two dimensions 'W@Sketch1' and 'L@Sketch1'. The first equation 'Debug.Print swEqnMgr.Add2(-1, """SquareFeet"" = ""W@Sketch1"" * ""L@Sketch1"" / 144", False)' will be added without any trouble and you will see '0' printed in the immediate window which is the index of the equation (0 based).

                   

                  If you comment out next two lines 'Debug.Print swEqnMgr.Add2(-1, """wn""=""W@Sketch1""", False)' and 'Debug.Print swEqnMgr.Add2(-1, """ln""=""L@Sketch1""", False)' and then try to add equation 'Debug.Print swEqnMgr.Add2(-1, """SquareFeet1"" = ""wn"" * ""ln"" / 144", False)', it will fail and print '-1' in immediate window. To add this equation you must have to un-comment other two lines.

                   

                  Second part of the macro gets dimension values of both dimensions 'W@Sketch1' and 'L@Sketch1', assigns them to 'wn' and 'ln' variables and then calculates square feet.

                   

                  Hope this is clear.

                    • Re: Macro to add equation using named dimension variable
                      Mike Landro

                      Nilesh,

                          Sorry for not getting back to you sooner.... When we use this macro the sketch name may not always be sketch 1.  I am trying to use an input box to have the user enter the sketch number that is being used and put that into our square foot equation.

                       

                      Thanks for your help!

                        • Re: Macro to add equation using named dimension variable
                          Deepak Gupta

                          You can use InputBox and then use the value for the sketch number.

                           

                          Dim sNumber As String

                          sNumber = InputBox("Enter Sketch Number", "Enter Sketch Number", sNumber)

                          sNumber = "Sketch" & sNumber

                          And now replace Sketch1 with sNumber

                            • Re: Macro to add equation using named dimension variable
                              Mike Landro

                              Deepak,

                              I don't think I have the formatting correct to use the sNumber variable in the code where the equations are being added. How should that be called out in the code?

                               

                              sNumber = InputBox("Enter Sketch Number", "Enter Sketch Number", sNumber)
                                  sNumber = "Sketch" & sNumber

                                  Set swApp = Application.SldWorks
                                  Set swModel = swApp.ActiveDoc

                                  'Add equations
                                  Set swEqnMgr = swModel.GetEquationMgr
                                  Debug.Print swEqnMgr.Add2(-1, """SquareFeet"" = ""W@sNumber"" * ""L@sNumber"" / 144", False)
                                  Debug.Print swEqnMgr.Add2(-1, """wn""=""W@sNumber""", False)
                                  Debug.Print swEqnMgr.Add2(-1, """ln""=""L@sNumber""", False)
                                  Debug.Print swEqnMgr.Add2(-1, """SquareFeet1"" = ""wn"" * ""ln"" / 144", False)
                                  swEqnMgr.EvaluateAll

                               

                               

                               

                              Thanks, Mike

                                • Re: Macro to add equation using named dimension variable
                                  Nilesh Patel

                                  Hi Mike,

                                   

                                  Try Following:

                                   

                                  Option Explicit
                                  
                                  Sub main()
                                  
                                      Dim swApp      As SldWorks.SldWorks
                                      Dim swModel    As ModelDoc2
                                      Dim swSelMgr    As SelectionMgr
                                      Dim swDisDim    As DisplayDimension
                                      Dim swDim      As Dimension
                                      Dim swEqnMgr    As EquationMgr
                                      Dim sNumber    As String
                                      Dim sWidth      As String
                                      Dim sLength    As String
                                      Dim sSqFeet    As String
                                  
                                      sNumber = InputBox("Enter Sketch Number", "Enter Sketch Number", sNumber)
                                      sWidth = Chr(34) & "W@Sketch" & sNumber & Chr(34)
                                      sLength = Chr(34) & "L@Sketch" & sNumber & Chr(34)
                                      sSqFeet = sWidth & "*" & sLength & "/144"
                                  
                                      Set swApp = Application.SldWorks
                                      Set swModel = swApp.ActiveDoc
                                       
                                      'Add equations
                                      Set swEqnMgr = swModel.GetEquationMgr
                                      Debug.Print swEqnMgr.Add2(-1, """SquareFeet"" =" & sSqFeet, False)
                                      Debug.Print swEqnMgr.Add2(-1, """wn""=" & sWidth, False)
                                      Debug.Print swEqnMgr.Add2(-1, """ln""=" & sLength, False)
                                      Debug.Print swEqnMgr.Add2(-1, """SquareFeet1"" = ""wn"" * ""ln"" / 144", False)
                                      swEqnMgr.EvaluateAll
                                       
                                      swModel.EditRebuild3
                                  End Sub