7 Replies Latest reply on May 27, 2016 2:14 PM by Deepak Gupta

    API for adding equation to line

    Lasse Nielsen

      Hi there,

       

      I have 3 construction lines

       

      Line 1: D1@sketch1 is 17mm

      Line 2: D2@sketch1 is 180mm

      Line 3: I need this line to be equal to Line2/Line1 using a macro

       

      Apparently the following macro doesn't do the job.

      swEquationMgr.Add -1, """D3@Sketch1""=""D2@Sketch1""/""D1@Skech1"""

       

      What am I doing wrong? Please help.

        • Re: API for adding equation to line
          Deepak Gupta

          Use these codes

           

          Option Explicit

              Dim SwApp           As SldWorks.SldWorks

              Dim Part            As SldWorks.ModelDoc2 

          Sub main()

              Set SwApp = Application.SldWorks

              Set Part = SwApp.ActiveDoc

              Part.GetEquationMgr.Add3 0, """D3@Sketch1"" = ""D2@Sketch1""/""D1@Sketch1""", True, swThisConfiguration, Empty

             End Sub

           

          'Replace  swThisConfiguration with swALLConfiguration if you need it for all configurations

            • Re: API for adding equation to line
              Lasse Nielsen

              This is my code so far.

              Option Explicit

               

              Dim swApp As SldWorks.SldWorks

              Dim swmodel As SldWorks.ModelDoc2

              Dim swSketchMgr As SldWorks.SketchManager

              Dim swsketchSeg As SldWorks.SketchSegment

              Dim swEquationMgr As SldWorks.EquationMgr

               

               

              Dim intSDR As Integer

              Dim intDiameter As Integer

               

              Dim dblSDR As Double

              Dim dblDiamter As Double

              Dim dblRadius As Double

               

               

               

              Sub main()

                  Set swApp = Application.SldWorks

                  Set swmodel = swApp.ActiveDoc

                  Set swSketchMgr = swmodel.SketchManager

                  Set swEquationMgr = swmodel.GetEquationMgr

               

               

                  'Turn off "input dimension value"

                  swApp.SetUserPreferenceToggle swInputDimValOnCreate, False

                 

                  'Select plane and insert sketch

                 swmodel.Extension.SelectByID2 "XY Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0

                 swSketchMgr.InsertSketch True

               

               

                  'turn on direct addition to database

                  swSketchMgr.AddToDB = True

                 

                  intSDR = 17

                  intDiameter = 180

                 

                  dblDiamter = intDiameter / 1000

                  dblRadius = dblDiamter / 2

                  dblSDR = dblDiamter / intSDR

                 

                 

                  'Create construction lines

                  Set swsketchSeg = swSketchMgr.CreateCenterLine(0, 0, 0, intSDR / 1000, 0, 0)

                  swsketchSeg.Color = 16711935

                  swmodel.SketchAddConstraints "sgHORIZONTAL2D"

                  swmodel.AddHorizontalDimension2 dblSDR / 2, -0.005, 0

                 

                      'Coincident to origin

                      swmodel.Extension.SelectByID2 "", "EXTSKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0

                      swmodel.Extension.SelectByID2 "", "SKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0

                      swmodel.SketchAddConstraints "sgCOINCIDENT"

                 

                  Set swsketchSeg = swSketchMgr.CreateCenterLine(0, 0, 0, 0, dblRadius - dblSDR, 0)

                  swsketchSeg.Color = 16711935

                  swmodel.SketchAddConstraints "sgVERTICAL2D"

                     

                  Set swsketchSeg = swSketchMgr.CreateCenterLine(0, dblRadius - dblSDR, 0, 0, dblRadius, 0)

                  swsketchSeg.Color = 16711935

                  swmodel.SketchAddConstraints "sgVERTICAL2D"

                 

                 

                  'Outer tube diameter

                  swSketchMgr.CreateCircle 0, 0, 0, 0, dblRadius, 0

                 

                      'Coincident to construction lines

                      swmodel.Extension.SelectByID2 "", "SKETCHPOINT", 0, dblRadius, 0, False, 0, Nothing, 0

                      swmodel.Extension.SelectByID2 "", "SKETCHSEGMENT", 0, dblRadius, 0, True, 0, Nothing, 0

                      swmodel.SketchAddConstraints "sgCOINCIDENT"

                 

                      'Adding dimension

                      swmodel.ClearSelection2 True

                      swmodel.Extension.SelectByID2 "", "SKETCHSEGMENT", 0, dblRadius, 0, True, 0, Nothing, 0

                      swmodel.AddDimension2 dblRadius * -1, dblRadius * -1, 0

                 

                 

                  'Inner tube diameter

                  swSketchMgr.CreateCircle 0, 0, 0, 0, dblRadius - dblSDR, 0

                 

                      'Coincident to construction lines

                      swmodel.Extension.SelectByID2 "", "SKETCHPOINT", 0, dblRadius - dblSDR, 0, False, 0, Nothing, 0

                      swmodel.Extension.SelectByID2 "", "SKETCHSEGMENT", 0, dblRadius - dblSDR, 0, True, 0, Nothing, 0

                      swmodel.SketchAddConstraints "sgCOINCIDENT"

                     

                      'Adding dimension

                      swmodel.ClearSelection2 True

                      swmodel.Extension.SelectByID2 "", "SKETCHSEGMENT", 0, (2 * dblRadius - dblSDR) / 2, 0, False, 0, Nothing, 0

                      swmodel.AddDimension2 dblRadius * -1, (2 * dblRadius - dblSDR) / 2, 0

               

                  swEquationMgr.Add3 0, """D3@Sketch1"" = ""D2@Sketch1""/""D1@Sketch1""", True, swAllConfiguration, Empty

              End Sub

               

              But the equation is not added to the third line when i run the code. What am I doing wrong? :S