4 Replies Latest reply on Oct 11, 2018 7:12 AM by Denis Dandrea

    Move the parts in the assembly work space X, Y, Z.

    Denis Dandrea

      Hello everyone,!

       

      My question is how to create a program in VBA to move components within an assembly. I would like to move the parts, without using the solidworks program.

       

      Thanks in advance.

       

      Denis

       

      distance

       

       

      absolute coordinates

       

        • Re: Move the parts in the assembly work space X, Y, Z.
          Deepak Gupta

          I'm not very much sure on how to do this but you would have to use IComponent2::Transform2 to set the position of the component.

            • Re: Move the parts in the assembly work space X, Y, Z.
              Denis Dandrea

              Thank you for your kind reply.

               

              I'm happy to know you, MR Gupta, I've been following you for a few years. Let's go back to the macro, I recovered a macro on the Forum, which rotates the objects that I think the logic or the commands are there. I do not know the comnado to be change wheel with move

               

              '***********************************************************************

              Option Explicit

              Const PI                As Double = 3.14159

              Const RadPerDeg         As Double = PI / 180#

              Sub main()

                  Dim swApp                       As SldWorks.SldWorks

                  Dim swModel                     As SldWorks.ModelDoc2

                  Dim swSelMgr                    As SldWorks.SelectionMgr

                

                  Dim swMath As SldWorks.MathUtility

                

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                  Set swSelMgr = swModel.SelectionManager

                

                  Dim swComp As SldWorks.Component2

                  Set swComp = swSelMgr.GetSelectedObjectsComponent2(1)

                

                  Set swMath = swApp.GetMathUtility

                

                  Dim compTransform As SldWorks.MathTransform

                  Set compTransform = swComp.Transform2

                

                  Dim swVect As MathVector

                

               

                  Dim I As Long

               

                  'possibility of entering the degrees of rotation asse "X"

               

                  I = InputBox("INSERISCI L'ANGOLO")

                

                  Dim dirArr(2) As Double

                  dirArr(0) = 0#

                  dirArr(1) = 0#

                  dirArr(2) = 1

               

               

               

               

              '''''I think that from here it is necessary to replace the wheel function, with another that you move''''''

               

               

               

               

               

               

                 'Rotate about z axis {0,0,1}

                

                  Set swVect = swMath.CreateVector((dirArr))

                  Set swVect = swVect.MultiplyTransform(compTransform)

                  'rotate the z axis into the component's reference frame

                

                  Dim vData  As Variant

                

                  dirArr(0) = 0#

                  dirArr(1) = 0#

                  dirArr(2) = 0#

                  'rotate about origin

               

                  vData = dirArr

                  Dim swPoint As SldWorks.MathPoint

                  Set swPoint = swMath.CreatePoint(vData)

                  Set swPoint = swPoint.MultiplyTransform(compTransform)

                  Dim swXform As SldWorks.MathTransform

                

              '    Set swXform = swMath.CreateTransformRotateAxis(swPoint, swVect, 90# * RadPerDeg)

                  Set swXform = swMath.CreateTransformRotateAxis(swPoint, swVect, I * RadPerDeg)

                  swComp.Transform2 = compTransform.Multiply(swXform)

                  swModel.EditRebuild3

              End Sub