4 Replies Latest reply on Apr 25, 2014 9:26 AM by Peter Houwen

    VBA get comps by component reference

    Peter Houwen

      I am working with Printed Circuit Board assemblies (from PADS Layout through Circuitworks).


      I need to make a VBA call from PADS Layout VB to select or get a component in Solidworks using it's Component Reference.


      i.e., I have resistor "R12" selected in PADS, I want to also select it in Solidworks.


      I can't seem to find a get or select that allows me to filter by the Component Reference.  I will need to do this for quite a few parts, so I would rather not loop through a few hundred components in Solidworks for each reference designator if I don't have to.





        • Re: VBA get comps by component reference
          Artem Taturevych

          Try this macro:


          Const COMP_REF As String = "R12"


          Dim swApp As SldWorks.SldWorks

          Dim swAssy As SldWorks.AssemblyDoc


          Sub main()


              Set swApp = Application.SldWorks


              Set swAssy = swApp.ActiveDoc


              Dim vComps As Variant


              vComps = swAssy.GetComponents(False)


              Dim i As Integer


              swAssy.ClearSelection2 False


              For i = 0 To UBound(vComps)


                  Dim swComp As SldWorks.Component2

                  Set swComp = vComps(i)

                  If swComp.ComponentReference = COMP_REF Then

                      swComp.Select4 True, Nothing, False

                  End If




          End Sub


          Regards, Artem Taturevych | Snr. Developer | IC3D ANZ


          IC3DSteel – New Steel Solution for SolidWorks

          translationXpert – SolidWorks files language translator

          LinkedIn - SolidWorks API Group

            • Re: VBA get comps by component reference
              Peter Houwen



              Thanks, but that's what I was trying to avoid.  I could have hundreds of components, so I would need to do that hundreds of times.  That's why I'm looking for a way to select by the reference.


              boolstatus = Part.Extension.SelectByID2(...... here's where I tried using ComponentReference

                • Re: VBA get comps by component reference
                  Keith Rice



                  I have experience writing macros that process huge assemblies. If your assembly is a few hundred components, and you only have to run this macro occasionally, you should be fine.


                  If you have to run this macro many times within the SAME assembly, and during that time you are not adding new components that would need to be searched for, then the macro could be modified to generate a list of all component names and their corresponding reference numbers and store that data as a string in an invisible attribute (or even in a text file on your hard drive if you wanted). When the macro is run, it will first see if this attribute exists. If yes, then it will search the attribute string for the component reference you specify and select the associated component using IModelDocExtension::SelectByID2. Unless you had thousands of pairs in the string, the search would be nearly instantaneous to you.



                  SolidWorks API Tutorials

                    • Re: VBA get comps by component reference
                      Peter Houwen

                      It looks like I'll have to do it the long way.  That's not a huge problem, I just get frustrated when I know there is information in there and I can't access it!  I was hoping someone knew an undocumented object.


                      I ended up storing the values I want to look for in an array, since that's a smaller set than the components in the assembly.


                      Thanks, all!