3 Replies Latest reply on May 19, 2015 4:45 PM by Luigi Piron

    Add component in assembly with Translation

    Milan K

      Hi all.

       

      Three days ago I used method AddComponents3 to add a number of components into assembly with defined translation vectors. Everything was fine as long as I remember. As a guide I used this example http://help.solidworks.com/2014/English/api/sldworksapi/Add_Components_Example_VB.htm

       

      Two next days I was massaging the VBA macro and when I finally finished it and reran it, the translation stopped to work. So I have written a simple trial macro again but to my surprise it doesn't work!

      As a part I simply create a cube with 1mm sides. At a one corner I define the Coordinate System named CS2. After submitting the macro shown below, the cubes is placed into the assembly with the CS2 coinciding with the assembly origin. Please does anyone know why is not the cube moved by 0.002 mm in Z-axis as defined in translation vector in the transformation matrix?? What is wrong with the macro?

       

      Thanks

      Milan

       

      ===

      Dim swApp As SldWorks.SldWorks

      Dim assemb As SldWorks.IAssemblyDoc

       

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

       

      Dim compNames(0) As String

      Dim compXforms(15) As Double

      Dim compCoordSysNames(0) As String

      Dim vcompNames As Variant

      Dim vcompXforms As Variant

      Dim vcompCoordSysNames As Variant

      Dim vcomponents As Variant

       

      Sub main()

       

      Set swApp = Application.SldWorks

      Set assemb = swApp.NewDocument("C:\ProgramData\SolidWorks\SolidWorks 2011\templates\Assembly.asmdot", 0, 0, 0)

      swApp.ActivateDoc2 assemb.GetTitle, False, longstatus

       

      compNames(0) = "D:\TheCars\3D\SolidWorksModels\cube.SLDPRT"

       

      ' Define the transformation matrix. See the IMathTransform API documentation.

       

      ' Add a rotational diagonal unit matrix (zero rotation) to the transformation matrix

      compXforms(0) = 1#

      compXforms(1) = 0#

      compXforms(2) = 0#

      compXforms(3) = 0#

      compXforms(4) = 1#

      compXforms(5) = 0#

      compXforms(6) = 0#

      compXforms(7) = 0#

      compXforms(8) = 1#

       

      ' Add a translation vector to the transformation matrix

      compXforms(9) = 0#

      compXforms(10) = 0#

      compXforms(11) = 0.002

       

      ' Add a scaling factor to the transform

      compXforms(12) = 1#

       

      ' The last three elements of the transformation matrix are unused

       

      ' Register the component's coordinate system name

      compCoordSysNames(0) = "CS2"

       

      ' Add the component to the assembly.

      vcompNames = compNames

      vcompXforms = compXforms

      vcompCoordSysNames = compCoordSysNames

      vcomponents = assemb.AddComponents3((vcompNames), (vcompXforms), (vcompCoordSysNames))

       

       

      End Sub

       

      ===

        • Re: Add component in assembly with Translation
          Artem Taturevych

          Hi Milan,

           

          I guess SolidWorks prioritizes the coordinate names and in case the array is not empty uses this for positions otherwise uses matrix transformation. So this cannot be combined. You may need to compose the translation matrix with the coordinate system matrix in your code before insertion.

          ______________________________________________

          Regards, Artem Taturevych | Snr. Developer | IC3D ANZ

           

          IC3DSteel – New Steel Solution for SolidWorks

          translationXpert – SolidWorks files language translator

          LinkedIn - SolidWorks API Group

           

           

            • Re: Add component in assembly with Translation
              Milan K

              Hi Artem.

               

              You are correct. My belief was that both works together, i.e. at first, the origins are matched and in the next step the component is translated by a distance given in the transformation matrix. Perhaps in a method AddComponents4...

               

              Thanks!!

              Milan

                • Re: Add component in assembly with Translation
                  Luigi Piron

                  I' m sorry

                  but i continue to don't understand

                  why the macro doesn't works

                  ===

                  Dim swApp As SldWorks.SldWorks

                  Dim assemb As SldWorks.IAssemblyDoc

                   

                  Dim boolstatus As Boolean

                  Dim longstatus As Long, longwarnings As Long

                   

                  Dim compNames(0) As String

                  Dim compXforms(15) As Double

                  Dim compCoordSysNames(0) As String

                  Dim vcompNames As Variant

                  Dim vcompXforms As Variant

                  Dim vcompCoordSysNames As Variant

                  Dim vcomponents As Variant

                   

                  Sub main()

                   

                  Set swApp = Application.SldWorks

                  Set assemb = swApp.NewDocument("C:\ProgramData\SolidWorks\SolidWorks 2011\templates\Assembly.asmdot", 0, 0, 0)

                  swApp.ActivateDoc2 assemb.GetTitle, False, longstatus

                   

                  compNames(0) = "D:\TheCars\3D\SolidWorksModels\cube.SLDPRT"

                   

                  ' Define the transformation matrix. See the IMathTransform API documentation.

                   

                  ' Add a rotational diagonal unit matrix (zero rotation) to the transformation matrix

                  compXforms(0) = 1#

                  compXforms(1) = 0#

                  compXforms(2) = 0#

                  compXforms(3) = 0#

                  compXforms(4) = 1#

                  compXforms(5) = 0#

                  compXforms(6) = 0#

                  compXforms(7) = 0#

                  compXforms(8) = 1#

                   

                  ' Add a translation vector to the transformation matrix

                  compXforms(9) = 0#

                  compXforms(10) = 0#

                  compXforms(11) = 0.002

                   

                  ' Add a scaling factor to the transform

                  compXforms(12) = 1#

                   

                  ' The last three elements of the transformation matrix are unused

                   

                  ' Register the component's coordinate system name

                  compCoordSysNames(0) = "CS2"

                   

                  ' Add the component to the assembly.

                  vcompNames = compNames

                  vcompXforms = compXforms

                  vcompCoordSysNames = compCoordSysNames

                  vcomponents = assemb.AddComponents3((vcompNames), (vcompXforms), (vcompCoordSysNames))

                   

                   

                  End Sub