6 Replies Latest reply on Jan 18, 2017 7:34 AM by William Cruz

    Orientation to origin API

    Kai Jordan

      Hi Guys

      The example shows the simple selection of a part or an assembly.

      I do not want to select an item.

      I want to select many element.

      I find in the API help is not the right solution maybe somebody help me.

      Here is the example:


      This example shows how to align an assembly component to the assembly origin and planes.



      ' Preconditions: Assembly document is open and

      '                an assembly component is selected.


      ' Postconditions: Selected assembly component is

      '                aligned to the assembly origin and planes.


      Option Explicit


      Sub main()

          Dim swApp                   As SldWorks.SldWorks

          Dim swMathUtil              As SldWorks.MathUtility

          Dim swModel                 As SldWorks.ModelDoc2

          Dim swSelMgr                As SldWorks.SelectionMgr

          Dim swComp                  As SldWorks.Component2

          Dim swXform                 As SldWorks.MathTransform

          Dim i                       As Long

          Dim bRet                    As Boolean


          Set swApp = Application.SldWorks

          Set swMathUtil = swApp.GetMathUtility

          Set swModel = swApp.ActiveDoc

          Set swSelMgr = swModel.SelectionManager

          Set swComp = swSelMgr.GetSelectedObjectsComponent3(1, 0)

          Set swXform = swMathUtil.CreateTransform(Nothing)    ' create unit transform


          swComp.Transform2 = swXform


          ' Rebuild to see updated transform

          bRet = swModel.EditRebuild3: Debug.Assert bRet

      End Sub



      Greetings Kai Jordan



        • Re: Orientation to origin API
          Amen Allah Jlili

          I'll assume that you're looking at how to select many components?


          You can start by getting an array of all components in your assembly using .GetComponents(False) from your main assembly object.

          Clear your selection first, and then traverse the components array and then select the components that match your selection criteria.
          You can use IComponent2::Select to select a component or IModelDoc2Extension::SelectByID2.




          • Re: Orientation to origin API
            William Cruz

            To select everything on the screen, you can also use the IAssemblyDoc - Putting 100 as your parameter will select everything on the screen. See the following link.


            2016 SOLIDWORKS API Help - SelectComponentsBySize Method (IAssemblyDoc)

            • Re: Orientation to origin API
              Kai Jordan

              Ok the different possibilities of the selection I have try and it works well.

              I want to use the command for all selected parts, not just from the part / assembly that was selected first


              Set swXform = swMathUtil.CreateTransform (Nothing) 'createUnitTransform

              SwComp.Transform2 = swXform

              Greetings Kai

                • Re: Orientation to origin API
                  Amen Allah Jlili

                  I don't understand what you are trying to do? Are you trying to get the coordinates of a entity with regards with to your main assembly?

                    • Re: Orientation to origin API
                      Kai Jordan

                      I want to select a number of parts X and point to the origin of the main assembly

                        • Re: Orientation to origin API
                          William Cruz

                          Put your parts in a list and iterate through them and send them to the method below. It will return the origin.


                                  internal double[] getOriginPointInfo(Component2 comp)
                                          double[] mPT = null;
                                          double[,,] pt = new double[0, 0, 0];
                                          MathUtility math = (MathUtility)_swApp.GetMathUtility();
                                          MathTransform trany = comp.Transform2;
                                          MathPoint mPoint = (MathPoint)math.CreatePoint(pt);
                                          MathPoint tranPoint = mPoint.IMultiplyTransform(trany);
                                          mPT = (double[])tranPoint.ArrayData;
                                          return mPT;
                                      catch (Exception ex)
                                          return null;