3 Replies Latest reply on Aug 22, 2014 10:53 AM by Deepak Gupta

    Select part by name

    Sam Kuhn

      I am trying to write a macro that replaces a specified component in an assembly.  The API help has a nice example of how to do the replace.  However, that only replaces the selected component.  I am trying to replace the part based on the name, say part-1@assembly. 

       

      I think I need to use SelectByID2 and get a Component2, but I am having trouble putting it all together.  Can anyone help me out here?

        • Re: Select part by name
          Deepak Gupta

          Try these:

           

          Option Explicit

          Sub main()

              Const sFileName             As String = "C:\Samples\123.sldprt"

              Dim swApp                   As SldWorks.SldWorks

              Dim swModel                 As SldWorks.ModelDoc2

              Dim swAssy                  As SldWorks.AssemblyDoc

              Dim swSelMgr                As SldWorks.SelectionMgr

              Dim swSelComp               As SldWorks.Component2

              Dim swSelModel              As SldWorks.ModelDoc2

              Dim nErrors                 As Long

              Dim bRet                    As Boolean

             

              Set swApp = CreateObject("SldWorks.Application")

              Set swModel = swApp.ActiveDoc

              Set swAssy = swModel

              Set swSelMgr = swModel.SelectionManager

             

              swModel.Extension.SelectByID2 "Part1.SLDPRT", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0

              Set swSelComp = swSelMgr.GetSelectedObjectsComponent(1)

              bRet = swAssy.ReplaceComponents(sFileName, "", True, True)       

          End Sub