API: COUNT COMPONENTS IN ASSEMBLIES

Hi there,

Can somebody give me any suggestion with this?:

Suppose that i have an assembly doc named " Test Assembly"  and contains: 4 components named "Part1", 6 components named "Part2" and 12 components named "Bolts".

If I write for example:

Dim sldworks as Object

DIm part as object

set sldworks= Application.sldworks

set part = sldworks.activedoc

part.extension.Selectbyid2("Part1-1@TA","component",....)

I would like to know how I can obtain the number FOUR (number of components named Part1) using other method or property... (in excel would be something like this "part.extension.selectbyid2(....).count")

I have tried with getcomponentcount but do not works as i want (return the number of all components in the assembly and do not support the selectbyid2 method). I know that a BOM do this automatically but i need obtain the number to do a custom bill in excel !!

Thanks in advance

Omar

SolidworksApi macros
Answers 4
MM
MM 2013-12-06 Best Answer

I would use AssemblyDoc::GetComponents to get an array of all the components. Then, use a For Each ... Next loop to run an If function on each item of the array and use it to increment an integer count of how many components' names match "Part1".

ST
ST 2013-12-06

Have a look at GetRootComponent3 Method (IConfiguration) in the API help.

It contains 3 code samples for recursively looping through components of assemblies and sub-assemblies.

Then use Matt's suggestion of counting each part name you come across.


AT
AT 2013-12-07
OF
OF 2013-12-08

Hello everybody,

I solved the problem in two ways...

Matt,

I found a useful example at http://www.eng-tips.com/viewthread.cfm?qid=198808

this example get an array of all the components and then compare the selected component (through selectbyID2 method in my case) using getpathname, referencedconfiguration and getsuppression methods... Please check it!! works good... only was necessary edit some lines and adjust the code for my specific application.

Thank you very much Matt your tip was really good...

Artem,

I do not know if is exactly your method, but I tried to use this code:

Sub COUNT_COMPONENT()

Dim solidworks As Object

Dim part As Object

Dim boolstatus As Boolean

Set solidworks = Application.SldWorks

Set part = solidworks.ActiveDoc

boolstatus = True

i = 1

Do Until boolstatus = False

boolstatus = part.Extension.SelectByID2("Part1-" & i & "@TestAssembly", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

If boolstatus = True Then i = i + 1

Loop

MsgBox (i - 1)

End Sub

the only one problem with this code is that depends of the id instance order. As you can note, if there is no a constant step in the ID instance component, then the number of elements obtained will be incorrect. What would you add to fix the possible mistake?

Thanks a lot Artem...

Simon,

Undoubtly I will take a look to GetRootComponent method and then will comment to you... I never have used this method

Thanks

Regards and thanks for all...

Omar