1 Reply Latest reply on Jan 10, 2015 12:58 PM by Keith Rice

    How to select a part within an assembly model?

    Fivezdw Fivezdw

      I'm writing a program using Visual studio C# to select a part in an assembly document. Then, I need to move the part along each axis and do a collision detection with other parts.

      I think I need to use select by ID method.  I have tried but couldn't get the returned component2 object. Can anybody teach me how to select a part by using the part's name? would appreciate the help.

        • Re: How to select a part within an assembly model?
          Keith Rice

          Hi,

           

          The following code will select all instances of a component given the name specified in the COMPONENT_NAME constant. For example, if your component is "testing_block.SLDPRT", then you would specify simply "testing_block".

           

          Const COMPONENT_NAME As String = "testing_block"
          
          Dim swApp As SldWorks.SldWorks
          Dim swModel As SldWorks.ModelDoc2
          Dim swAssy As SldWorks.AssemblyDoc
          Dim swComp As SldWorks.Component2
          Dim i As Integer
          Dim vComps As Variant
          Dim vSplit As Variant
              
          Sub main()
              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Set swAssy = swModel
              
              swModel.ClearSelection2 True
              
              vComps = swAssy.GetComponents(False)
              For i = 0 To UBound(vComps)
                  Set swComp = vComps(i)
                  vSplit = Split(swComp.Name2, "/")
                  vSplit = Split(vSplit(UBound(vSplit)), "-")
                  If COMPONENT_NAME = vSplit(0) Then swComp.Select (True)
              Next i
          End Sub
          

           

          Keith

          SolidWorks API Tutorials