4 Replies Latest reply on Aug 9, 2018 4:37 PM by Danniel Sims

    Moving bodies in a part api

    Vivek Marukurti

      I'm trying to move an extrusion(see picture) from its origin to a 3D sketch point. While the 3D sketch point is known I'm having issues defining the Selectbyray method as I don't know the initial position of the extrusion. Is there anyway to automate the selection?


      Thanks in advance.

        • Re: Moving bodies in a part api
          Danniel Sims

          You could traverse the feature tree and find the extrusion, but then you'd have to do more to find points related to it.

          I chopped this down, I usually have it count between the Origin and Mates, but this might give you some ideas - particularly GetTypeName


          Sub List_FeaturesII()

          Set swApp = Application.SldWorks

          Set swModeler = swApp.GetModeler

          Set swModel = swApp.ActiveDoc

          Set swModelDocExt = swModel.Extension

          Set swFeatMgr = swModel.FeatureManager

          count = swFeatMgr.GetFeatureCount(True)

          featArr = swFeatMgr.GetFeatures(True)

          Set swSelMgr = swModel.SelectionManager

          Dim test As Feature

          Dim test1

          Dim i, t


          i = 1


          Do Until i = count

              Debug.Print featArr(i).Name & " " & featArr(i).GetTypeName



          i = i + 1




          MsgBox "Process Complete!"

            • Re: Moving bodies in a part api
              Vivek Marukurti



              Thanks for the reply. Perhaps I should elaborate more.My problem is not the extrusion. I want to Lpattern the extrusion so I want to move it a control point as shown in the figure above. I want to use the feature Move/copy for that and mate the points. Please find below the macro recorded code... Since the extrusion may change position in later cases, I need to automate SlectByRay method . But I do not know how to find the ray points. Is there any method to do that.


              Thank you,



              Sub main()



              Set swApp = Application.SldWorks



              Set Part = swApp.ActiveDoc

              Dim COSMOSWORKSObj As Object

              Dim CWAddinCallBackObj As Object

              Set CWAddinCallBackObj = swApp.GetAddInObject("CosmosWorks.CosmosWorks")

              Set COSMOSWORKSObj = CWAddinCallBackObj.COSMOSWORKS

              boolstatus = Part.Extension.SelectByID2("Extrude-Thin1", "SOLIDBODY", -6.5850556811613E-03, 1.08094746720155E-03, 6.13490355868862E-03, True, 0, Nothing, 0)

              boolstatus = Part.Extension.SelectByRay(-9.17219230461175E-03, 2.39764719793811E-05, 5.80854225518124E-06, -7.14286430792388E-03, 0.186696264736081, -0.982391716283822, 5.93496220073515E-04, 1, True, 0, 0)

              boolstatus = Part.Extension.SelectByID2("Point3@3DSketch1", "EXTSKETCHPOINT", -2.80440119478869E-02, 0, -2.80440119478869E-02, True, 0, Nothing, 0)

              Dim Component As Object

              Dim FeatureData As Object

              Dim varFeature As Object

              Part.ClearSelection2 True

              boolstatus = Part.Extension.SelectByID2("Extrude-Thin1", "SOLIDBODY", -6.5850556811613E-03, 1.08094746720155E-03, 6.13490355868862E-03, False, 1, Nothing, 0)

              Set varFeature = Part.FeatureManager.InsertMoveCopyBody2(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, False, 1)

              Set FeatureData = varFeature.GetDefinition()

              boolstatus = Part.Extension.SelectByRay(0.009, 0, 0, -7.14286430792388E-03, 0.186696264736081, -0.982391716283822, 5.93496220073515E-04, 1, False, 1, 0)

              boolstatus = Part.Extension.SelectByID2("Point3@3DSketch1", "EXTSKETCHPOINT", -2.80440119478869E-02, 0, -2.80440119478869E-02, True, 1, Nothing, 0)

              FeatureData.AddMate Nothing, 1, 2, 0, 0, longstatus

              varFeature.ModifyDefinition FeatureData, Part, Component

              StudyManagerObj = Nothing

              ActiveDocObj = Nothing

              Set CWAddinCallBackObj = Nothing

              Set COSMOSWORKSObj = Nothing

              End Sub

                • Re: Moving bodies in a part api
                  Danniel Sims

                  Sorry to say I've never used SelecByRay.  I would think SelectByID2 is easier, but that's just the method I typically use.  I get the sweep it does to grab the first object, but I'm not sure how you would get the direction.  You should be able to get a point from the cylinder, or it's base sketch. 

              • Re: Moving bodies in a part api
                Josh Brady

                How about GetBodies2?