14 Replies Latest reply on Jan 4, 2019 10:02 PM by P. Ramesh

    Sketch update through API

    P. Ramesh

      I have 2 sketches which gets the input through two text boxes and it updates the value in sketch when i click a command button.

      I have to click the command button more than twice to update the sketch for the given value.

      How to update the sketch for the given value in single click through API.

        • Re: Sketch update through API
          Deepak Gupta

          Try adding a rebuild function and see if that helps. If still not working then share your macro along with sample file to debug.

          • Re: Sketch update through API
            Christian Chu

            The input is directly from the enter value or from equation?

            • Re: Sketch update through API
              Fifi Riri

              Could you post the code?

              Otherwise try this:

              It should update "D1@Sketch1" and "D1@Sketch2"

               

              Dim swApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim boolstatus As Boolean

              Sub main()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Dim myDimension As SldWorks.Dimension

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

              myDimension.SystemValue = InputBox("Value1 (in mm)?") / 1000

              Set myDimension = swModel.Parameter("D1@Sketch2")

              myDimension.SystemValue = InputBox("Value2 (in mm)?") / 1000

              boolstatus = swModel.EditRebuild3

              End Sub

                • Re: Sketch update through API
                  P. Ramesh

                  Private Sub CommandButton1_Click()

                  Dim swApp As Object

                  Dim Part As Object

                  Dim boolstatus As Boolean

                  Dim longstatus As Long, longwarnings As Long

                   

                   

                  Set swApp = Application.SldWorks

                  Set Part = swApp.ActiveDoc

                  Dim myDimension As Object

                  Set myDimension = Part.Parameter("D20@Sketch1")

                  myDimension.SystemValue = h.Text / 1000

                  Part.Parameter("D39@Sketch1").SystemValue = h.Text / 1000

                  q.Text = (Part.Parameter("D21@Sketch1").SystemValue) * 1000

                  Dim myDimension1 As Object

                  Set myDimension1 = Part.Parameter("D18@Sketch3")

                  myDimension1.SystemValue = Part.Parameter("D21@Sketch1").SystemValue

                  Dim pactual As Double

                  pactual = (Part.Parameter("D38@Sketch3").SystemValue) * 1000

                  p.Text = pactual

                  Dim head(9) As Double

                  head(1) = Part.Parameter("D3@Sketch1").SystemValue

                  head(2) = Part.Parameter("D4@Sketch1").SystemValue

                  head(3) = Part.Parameter("D5@Sketch1").SystemValue

                  head(4) = Part.Parameter("D6@Sketch1").SystemValue

                  head(5) = Part.Parameter("D7@Sketch1").SystemValue

                  head(6) = Part.Parameter("D11@Sketch1").SystemValue

                  head(7) = Part.Parameter("D8@Sketch1").SystemValue

                  head(8) = Part.Parameter("D9@Sketch1").SystemValue

                  head(9) = Part.Parameter("D10@Sketch1").SystemValue

                  Dim disc(8) As Double

                  disc(1) = Part.Parameter("D12@Sketch1").SystemValue

                  disc(2) = Part.Parameter("D13@Sketch1").SystemValue

                  disc(3) = Part.Parameter("D14@Sketch1").SystemValue

                  disc(4) = Part.Parameter("D15@Sketch1").SystemValue

                  disc(5) = Part.Parameter("D16@Sketch1").SystemValue

                  disc(6) = Part.Parameter("D17@Sketch1").SystemValue

                  disc(7) = Part.Parameter("D18@Sketch1").SystemValue

                  disc(8) = Part.Parameter("D19@Sketch1").SystemValue

                  Dim power(9) As Double

                  power(1) = Part.Parameter("D1@Sketch3").SystemValue

                  power(2) = Part.Parameter("D2@Sketch3").SystemValue

                  power(3) = Part.Parameter("D3@Sketch3").SystemValue

                  power(4) = Part.Parameter("D4@Sketch3").SystemValue

                  power(5) = Part.Parameter("D5@Sketch3").SystemValue

                  power(6) = Part.Parameter("D6@Sketch3").SystemValue

                  power(7) = Part.Parameter("D7@Sketch3").SystemValue

                  power(8) = Part.Parameter("D8@Sketch3").SystemValue

                  power(9) = Part.Parameter("D9@Sketch3").SystemValue

                   

                   

                  Dim i As Double

                  i = 50

                  Dim j As Double

                  Dim k As Double

                  Dim L As Double

                  Dim m As Double

                  Dim pv As Double

                   

                   

                  k = f.Text / 50

                  L = k * k

                  m = k * k * k

                  Part.Parameter("D22@Sketch1").SystemValue = head(1) * L

                  Part.Parameter("D23@Sketch1").SystemValue = head(2) * L

                  Part.Parameter("D24@Sketch1").SystemValue = head(3) * L

                  Part.Parameter("D25@Sketch1").SystemValue = head(4) * L

                  Part.Parameter("D26@Sketch1").SystemValue = head(5) * L

                  Part.Parameter("D27@Sketch1").SystemValue = head(6) * L

                  Part.Parameter("D28@Sketch1").SystemValue = head(7) * L

                  Part.Parameter("D29@Sketch1").SystemValue = head(8) * L

                  Part.Parameter("D30@Sketch1").SystemValue = head(9) * L

                  Part.Parameter("D31@Sketch1").SystemValue = disc(1) * k

                  Part.Parameter("D32@Sketch1").SystemValue = disc(2) * k

                  Part.Parameter("D33@Sketch1").SystemValue = disc(3) * k

                  Part.Parameter("D34@Sketch1").SystemValue = disc(4) * k

                  Part.Parameter("D35@Sketch1").SystemValue = disc(5) * k

                  Part.Parameter("D36@Sketch1").SystemValue = disc(6) * k

                  Part.Parameter("D37@Sketch1").SystemValue = disc(7) * k

                  Part.Parameter("D38@Sketch1").SystemValue = disc(8) * k

                  Part.Parameter("D20@Sketch3").SystemValue = power(1) * m

                  Part.Parameter("D21@Sketch3").SystemValue = power(2) * m

                  Part.Parameter("D22@Sketch3").SystemValue = power(3) * m

                  Part.Parameter("D23@Sketch3").SystemValue = power(4) * m

                  Part.Parameter("D24@Sketch3").SystemValue = power(5) * m

                  Part.Parameter("D25@Sketch3").SystemValue = power(6) * m

                  Part.Parameter("D26@Sketch3").SystemValue = power(7) * m

                  Part.Parameter("D27@Sketch3").SystemValue = power(8) * m

                  Part.Parameter("D28@Sketch3").SystemValue = power(9) * m

                  Part.Parameter("D29@Sketch3").SystemValue = disc(1) * k

                  Part.Parameter("D30@Sketch3").SystemValue = disc(2) * k

                  Part.Parameter("D31@Sketch3").SystemValue = disc(3) * k

                  Part.Parameter("D19@Sketch3").SystemValue = disc(4) * k

                  Part.Parameter("D33@Sketch3").SystemValue = disc(5) * k

                  Part.Parameter("D34@Sketch3").SystemValue = disc(6) * k

                  Part.Parameter("D35@Sketch3").SystemValue = disc(7) * k

                  Part.Parameter("D36@Sketch3").SystemValue = disc(8) * k

                   

                   

                  Part.Parameter("D32@Sketch3").SystemValue = Part.Parameter("D40@Sketch1").SystemValue

                  pv = Part.Parameter("D37@Sketch3").SystemValue

                  powernew.Text = pv

                  dischargenew.Text = (Part.Parameter("D40@Sketch1").SystemValue) * 1000

                  boolstatus = Part.ForceRebuild3(True)

                   

                   

                  End Sub

                   

                  Code for your reference

                    • Re: Sketch update through API
                      Deepak Gupta

                      Add files to test the codes.

                      • Re: Sketch update through API
                        Fifi Riri

                        Which dimension(s) or sketch(s) is not updated?

                        • Re: Sketch update through API
                          Fifi Riri

                          You need to modify sketch1 first, then sketch3

                          Put a rebuild between the 2 for good mesure

                          This works:

                           

                          Private Sub CommandButton1_Click()

                          Dim swApp As SldWorks.SldWorks

                          Dim Part As SldWorks.ModelDoc2

                          Dim boolstatus As Boolean

                          Set swApp = Application.SldWorks

                          Set Part = swApp.ActiveDoc

                           

                          Part.Parameter("D20@Sketch1").SystemValue = h.Text / 1000

                          Part.Parameter("D39@Sketch1").SystemValue = h.Text / 1000

                           

                          Dim k As Double

                          Dim L As Double

                          Dim m As Double

                          k = f.Text / 50

                          L = k * k

                          m = k * k * k

                           

                          Dim head(9) As Double

                          head(1) = Part.Parameter("D3@Sketch1").SystemValue

                          head(2) = Part.Parameter("D4@Sketch1").SystemValue

                          head(3) = Part.Parameter("D5@Sketch1").SystemValue

                          head(4) = Part.Parameter("D6@Sketch1").SystemValue

                          head(5) = Part.Parameter("D7@Sketch1").SystemValue

                          head(6) = Part.Parameter("D11@Sketch1").SystemValue

                          head(7) = Part.Parameter("D8@Sketch1").SystemValue

                          head(8) = Part.Parameter("D9@Sketch1").SystemValue

                          head(9) = Part.Parameter("D10@Sketch1").SystemValue

                           

                          Part.Parameter("D22@Sketch1").SystemValue = head(1) * L

                          Part.Parameter("D23@Sketch1").SystemValue = head(2) * L

                          Part.Parameter("D24@Sketch1").SystemValue = head(3) * L

                          Part.Parameter("D25@Sketch1").SystemValue = head(4) * L

                          Part.Parameter("D26@Sketch1").SystemValue = head(5) * L

                          Part.Parameter("D27@Sketch1").SystemValue = head(6) * L

                          Part.Parameter("D28@Sketch1").SystemValue = head(7) * L

                          Part.Parameter("D29@Sketch1").SystemValue = head(8) * L

                          Part.Parameter("D30@Sketch1").SystemValue = head(9) * L

                           

                          Dim disc(8) As Double

                          disc(1) = Part.Parameter("D12@Sketch1").SystemValue

                          disc(2) = Part.Parameter("D13@Sketch1").SystemValue

                          disc(3) = Part.Parameter("D14@Sketch1").SystemValue

                          disc(4) = Part.Parameter("D15@Sketch1").SystemValue

                          disc(5) = Part.Parameter("D16@Sketch1").SystemValue

                          disc(6) = Part.Parameter("D17@Sketch1").SystemValue

                          disc(7) = Part.Parameter("D18@Sketch1").SystemValue

                          disc(8) = Part.Parameter("D19@Sketch1").SystemValue

                           

                          Part.Parameter("D31@Sketch1").SystemValue = disc(1) * k

                          Part.Parameter("D32@Sketch1").SystemValue = disc(2) * k

                          Part.Parameter("D33@Sketch1").SystemValue = disc(3) * k

                          Part.Parameter("D34@Sketch1").SystemValue = disc(4) * k

                          Part.Parameter("D35@Sketch1").SystemValue = disc(5) * k

                          Part.Parameter("D36@Sketch1").SystemValue = disc(6) * k

                          Part.Parameter("D37@Sketch1").SystemValue = disc(7) * k

                          Part.Parameter("D38@Sketch1").SystemValue = disc(8) * k

                           

                          q.Text = (Part.Parameter("D21@Sketch1").SystemValue) * 1000

                          boolstatus = Part.ForceRebuild3(True)

                           

                          Part.Parameter("D18@Sketch3").SystemValue = Part.Parameter("D21@Sketch1").SystemValue

                          Part.Parameter("D32@Sketch3").SystemValue = Part.Parameter("D40@Sketch1").SystemValue

                           

                          Part.Parameter("D29@Sketch3").SystemValue = disc(1) * k

                          Part.Parameter("D30@Sketch3").SystemValue = disc(2) * k

                          Part.Parameter("D31@Sketch3").SystemValue = disc(3) * k

                          Part.Parameter("D19@Sketch3").SystemValue = disc(4) * k

                          Part.Parameter("D33@Sketch3").SystemValue = disc(5) * k

                          Part.Parameter("D34@Sketch3").SystemValue = disc(6) * k

                          Part.Parameter("D35@Sketch3").SystemValue = disc(7) * k

                          Part.Parameter("D36@Sketch3").SystemValue = disc(8) * k

                           

                          Dim power(9) As Double

                          power(1) = Part.Parameter("D1@Sketch3").SystemValue

                          power(2) = Part.Parameter("D2@Sketch3").SystemValue

                          power(3) = Part.Parameter("D3@Sketch3").SystemValue

                          power(4) = Part.Parameter("D4@Sketch3").SystemValue

                          power(5) = Part.Parameter("D5@Sketch3").SystemValue

                          power(6) = Part.Parameter("D6@Sketch3").SystemValue

                          power(7) = Part.Parameter("D7@Sketch3").SystemValue

                          power(8) = Part.Parameter("D8@Sketch3").SystemValue

                          power(9) = Part.Parameter("D9@Sketch3").SystemValue

                           

                          Part.Parameter("D20@Sketch3").SystemValue = power(1) * m

                          Part.Parameter("D21@Sketch3").SystemValue = power(2) * m

                          Part.Parameter("D22@Sketch3").SystemValue = power(3) * m

                          Part.Parameter("D23@Sketch3").SystemValue = power(4) * m

                          Part.Parameter("D24@Sketch3").SystemValue = power(5) * m

                          Part.Parameter("D25@Sketch3").SystemValue = power(6) * m

                          Part.Parameter("D26@Sketch3").SystemValue = power(7) * m

                          Part.Parameter("D27@Sketch3").SystemValue = power(8) * m

                          Part.Parameter("D28@Sketch3").SystemValue = power(9) * m

                           

                          p.Text = (Part.Parameter("D38@Sketch3").SystemValue) * 1000

                          powernew.Text = Part.Parameter("D37@Sketch3").SystemValue

                          dischargenew.Text = (Part.Parameter("D40@Sketch1").SystemValue) * 1000

                          boolstatus = Part.ForceRebuild3(True)

                          End Sub

                          • Re: Sketch update through API
                            Josh Brady

                            Just another thought... It doesn't look like your code does much of anything, programming-wise.  It's just straight calculation.  I didn't dig super-deep into it, but on the surface I don't see anything that couldn't be done with equations and global variables.  Then there's no need for the macro.

                        • Re: Sketch update through API
                          P. Ramesh

                          Thank you all