API - counting child parts in assemblies
API - counting child parts in assemblies
(OP)
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.
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.






RE: API - counting child parts in assemblies
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?
RE: API - counting child parts in assemblies
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.
RE: API - counting child parts in assemblies
Andrew
RE: API - counting child parts in assemblies
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).
RE: API - counting child parts in assemblies
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.
RE: API - counting child parts in assemblies
And dsi: I only need the top level comps, this should do the trick right?
RE: API - counting child parts in assemblies
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.
RE: API - counting child parts in assemblies
RE: API - counting child parts in assemblies
C:\Program Files\SolidWorks\samples\visualbasic\bom
Bill Briggs, CSWP
bbriggs@cybllings.com
RE: API - counting child parts in assemblies
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
RE: API - counting child parts in assemblies
http://www.EsoxRepublic.com
RE: API - counting child parts in assemblies
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.