2 Replies Latest reply on Sep 4, 2013 4:44 PM by Chris Johnson

    Creating an Equation based on a measurement?

    Chris Johnson

      Hi All,,

       

      I must be doing something wrong and could use a little help.

      I want to measure the diameter of a selected edge and then use that dia. in an equation but its not working and I have no idea why.

       

       

      Here is the test code I set up to goof around with... see anything wrong?

       

      Option Explicit

          Dim swApp As Object

          Dim swmodel As Object

          Dim boolstatus As Boolean

          Dim longstatus As Long, longwarnings As Long

          Dim Measure                     As Measure

         

          Sub main()

          Set swApp = Application.SldWorks

          Set swmodel = swApp.ActiveDoc

          Set Measure = swmodel.Extension.CreateMeasure

       

      Measure.ArcOption = 0

       

      'PreSelect Edge and Run Macro

      'Measure and Store selected Edge Diameter for later use

                  boolstatus = Measure.Calculate(Nothing)

              Dim myMeasurement As Variant

                  myMeasurement = Measure.Diameter

                  swmodel.ClearSelection2 True

         

         

           

              Dim swEquationMgr As Object

                  Set swEquationMgr = swmodel.GetEquationMgr()

                      swEquationMgr.Add2 -1, """D1@Sketch2"" = myMeasurement - 0.5", True

        

      'this shows that the measurement exists..      

          Debug.Print myMeasurement

         

          boolstatus = swmodel.ForceRebuild()

         

          End Sub

       

      T

      Thank you

      Chris

        • Re: Creating an Equation based on a measurement?
          Jason Kerns

          Option Explicit

              Dim swApp                   As SldWorks.SldWorks

              Dim swModel                 As SldWorks.ModelDoc2

              Dim boolstatus As Boolean

              Dim longstatus As Long, longwarnings As Long

              Dim Measure                     As Measure

              Dim swEquationMgr As SldWorks.EquationMgr

              Dim lValue   As Long

              Dim Eqstring As String

            

              Sub main()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set Measure = swModel.Extension.CreateMeasure

           

          Measure.ArcOption = 0

           

          'PreSelect Edge and Run Macro

          'Measure and Store selected Edge Diameter for later use

                      boolstatus = Measure.Calculate(Nothing)

                  Dim myMeasurement As Variant

                      myMeasurement = Measure.Diameter

                      'swModel.ClearSelection2 True

                     

                      Eqstring = """D1@Sketch1""" & " = " & myMeasurement - 0.5

                 

                      Set swEquationMgr = swModel.GetEquationMgr()

                       lValue = swEquationMgr.Add2(-1, Eqstring, True)

           

          'this shows that the measurement exists..

              Debug.Print Eqstring

             swEquationMgr.EvaluateAll

              'boolstatus = swModel.EditRebuild3

              boolstatus = swModel.ForceRebuild()

              End Sub