Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

API - counting child parts in assemblies 1

Status
Not open for further replies.

Jimijames

Mechanical
Jan 17, 2002
20
Our company uses an Excel template to submit BOM requests (trust me...this is a SW question, not excel). The only required info is the part number and quantity. I wrote a macro to assign custom properties to parts and assemblies, one of them being the part num. I would like write an excel macro to attach to the open Solidworks assembly and then grab the part num custom prop and qty of the child parts/subassemblies. For example, if there are 3 of part number 111-1234 in an assembly, I want to grab that p/n and the fact that there are 3 occurances of the part.

I think I have all of it figured out except for counting the children. Any hints or tricks do do this with VBA? Is there any way to access the "quantity" in the SW BOMs without actually creating one? I want to have this new macro work on assemblies and not require users to create a drawing and insert a BOM.
 
Replies continue below

Recommended for you

If you are "traversing" your assembly to ask each part it's part number, then you already know how many of them their are. Don't you?

If you are capturing the data from each and putting the info on a line in Excel, then doing the next, then just have Excel count them and delete the extras.

If you are capturing the data into an array, you can have it count them.

Is this what you need help on?
 
I still consider myself a VB novice so bear with me... if I traverse the assembly and put the data in an array then, yes I have the total number of children, but can I extract how many times a particular part is repeated?

I thought of maybe having Excel count and delete extras, but I was hoping there was a SW function to do this since SW does count the parts when inserting a BOM in a drawing.
 
If your assy drawings have BOM then you can read the part # & quantity from BOM directly, whithout traversing the assy. Check out thread559-20992 for how to read BOM.

Andrew
 
Hi, I saw your question and think that I might be able to offer some help because I overcame a similar issue not long ago.

I've been putting in some time working on a BOM tool of my own that works on assembly files to output a text or an excel file. The reason for this is that we do external BOMs at my company because assembly drawings of any significant size are just plain impractical to work with.

I pretty much read the assembly contents into a text array and run through a nested loop that compares each individual element in the array to all of the other elements.

To generate quantities for unique values in an assembly traversal you'll need two arrays. One of the arrays contains everything found in the assembly traversal (I call this the raw data array) and the other is every unique value encountered in the first array. You can find a code snippet on the web (I think that it's on FreeVBCode.com) for a function that generates the unique array from the raw data passed into the function from the first array.

Then what you might want to do is this.

-Create a nested loop that passes each individual element from the unique array into another loop that runs a comparison between the element passed and every element in the raw data array.

-During this second loop when a string comparison finds a match the first time set a quantity variable to 1, a boolean flag to true (you'll need this to know if the element passed has been encountered yet), capture the current value of the quantity variable somewhere (e.g. another array or array element) and continue the loop.

-Then each time the element passed in encounters a match, increment the quantity variable by 1 (e.g. iQty = iQty + 1) and update the captured quantity data.

Sorry I don't have my code with me today or I'd provide an example. I do remember the basic algorithms though which I hope at least help get you started in the right direction. I'm not an expert either so there may be a better way to go about doing what you want to do (though I'm not a great advocate of creating an assembly drawing if it isn't necessary).
 
Jimijames:

Did you resolve this problem yet? If not, do you also need it to traverse through sub-assemblies within the main assembly or are you just looking for the top level components?
DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Thanks RawheadRex, sounds like this is what I was looking for. It's ok you didn't have sample code, I prefer the challenge of trying to figure it out myself.

And dsi: I only need the top level comps, this should do the trick right?
 
Yes, you should be able to loop through the top level components rather easily. Sounds like your up to the challange, so just stop back if you need some help with the coding.

Good luck... DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Glad that my post made enough sense to be of assistance. Please post again if you run into any trouble spots in coding and I'll be glad to help out more if I can.
 
Look for this sample program, it installs with SW and should be on your computer. You should find it very useful for your application.

C:\Program Files\SolidWorks\samples\visualbasic\bom

Bill Briggs, CSWP
bbriggs@cybllings.com
 
I am trying to do something similar to this. The only place I'm stuck is counting within the assembly traversal. My basic traversal program is one that I copied from the back of the SolidWorks API training book. I am trying to count within that traversal loop but everytime the program returns to the top line of the traversal loop my counter is reset to zero. I need it not to reset. I think it resets because of the Dim command yet the counter variable is invalid without the Dim command. I'm screwed either way. Any ideas what I'm doing wrong??

I'm at home right now so I don't have my code handy but I can paste it tomorrow if anyone needs it for reference.

Thanks,
Jeff
 
Maybe use a variable of broader scope. (Translation: move your initial Dim statement outside of the Sub.) Or maybe you need to define your variable as one of your Sub's arguments.

[bat]"When everyone is thinking alike, no one is thinking very much." --Eckhard Schwarz (1930--2004)[bat]
 
I parse assemblies daily to create part nests for Sheet metal manufacturing.

The way I personally do it is to return the Path/filename, and referenced configuration, of each component, to see if it matches another. Im not sure Id want to rely on a custom property to decide if a component is "Unique".

I set up a User Defined Type with the variables I need; including FileName, PathName, and referenced config. (I also store other data as well). I then create a dynamic array of the UDT's.

Recursing thru the assembly, get each Component object, *ensure it is not suppressed or an envelope*, get its Pathname/Referenced Config, then use that Info to loop thru each UDT(component) you've already found and stored in your array.

If you find a previously stored component which has matching Pathspecs AND cfg name, increment its qty property and quit the loop.

If you reach the end of the loop and have not found a match, REDIM PRESERVE your UDT array and add a new component.

A couple of things: to get a components custom properties, you may wish to get its MODELDOC. And if your models have multiple configs, *do not assume* the model will open to the correct part config.

If using part configs, you can do some pretty interesting things with Custom Properties if you include the ability to use BOTH document-level props AND config-level props, and the heirarchy between them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor