I'd like to create a macro that I can run from within an assembly that will loop through each BOM item and add the quantity req'd to a custom property within the component. I'm very familiar with VBA from Office, but I'm new to it with respect to Inventor so I'm pretty fuzzy with object titles and options. Can someone assist with the coding?
Here's an outline of what I'd like to do:
Execute Macro from within Assembly file
For Each BOM Item to # of BOM Items
If Vendor = "Make" then
Set Component Custom Property "Qty_Reqd" = Total Qty from BOM
Next BOM Item
Can someone help and/or Is there an easier way?
Solved! Go to Solution.
Solved! by MegaJerk. See the answer in context.
Sorry for the long delay on this. My weekend turned out to be full of thrills and chills, leaving little time for Inventor.
No matter, I think that I've cracked. Give the following code a try and tell me if it works correctly for your full assemblies.
Solved! by MegaJerk. See the answer in context.
I would suggest that instead of trying to tie this into an iTrigger, you simply set it up on a normal ilogic Rule Event Trigger (perhaps even as an external rule).
Go to the Manage tab, and select Event Triggers (iLogic section above iTrigger). Double click on Before Save Document to open up the Rule Association Browser, and select the iLogic rule that contains my code. After that , just make sure that the top check box “Run these rules when events occur” is checked on the main Rules Triggered by Events box, then hit OK.
As for learning these things, it has been a combination of a few very awesome blogs, this community, and stumbling around the Inventor Object Library in VBA.
After I get out of work, I will gladly compile a list of references and places you can go to get you on track to learning the ins and outs of Inventor API stuffs.
Also, if you are satisfied with the above code for your needs, please accept that post as a solution as it will help others who are looking to do the same thing in the future (Gotta keep the community tidy!).
More posts from me later on.
Peace for now.
The below example is code that I use to look at each part in an assembly, tally up how many of each is being used, and throw that total into the Authority iProperty of each part. The only reason that I used a stock (vanilla) iProperty as the value holder is so I can more easily access it from the drawing where I need to display totals next to things like flat patterns, etc. etc.
However, if you don’t need to show these values on the drawings, then we’ll go with a custom iprop. Right now I’m just going to post the code that I have as I am at work. I also would like a little more information from you before I customize this to match your needs better. As it is, the code now looks for all parts that are able to be modified (IE: non-suppressed parts / non-library content center files), but you mention that you may have something in place that determines which parts you’ll be using (Vendor?). So are you using another iProperty, or are you too also just using what hasn’t been suppressed by a Level of Detail to determine which parts will actually be manufactured?
Once you provide that info I will be able to customize this for you, or you can set off to customizing it on your own. I know that a few posts back I helped someone with something similar, so if you click on my user name and look for my posts you might be able to find some other interesting information back there.
Dim openDoc As Document openDoc = ThisDoc.Document Dim docFile As Document If openDoc.DocumentType = 12291 Then For Each docFile In openDoc.AllReferencedDocuments If docFile.IsModifiable = True Then If docFile.DocumentType = 12290 Then Dim assemblyDoc As AssemblyDocument assemblyDoc = openDoc Dim assemblyDef As AssemblyComponentDefinition assemblyDef = assemblyDoc.ComponentDefinition Dim partDoc As PartDocument partDoc = ThisApplication.Documents.Open(docFile.FullFileNam
e, False) Dim partQty As ComponentOccurrencesEnumerator partQty = assemblyDef.Occurrences.AllReferencedOccurrences(p artDoc) 'MessageBox.Show(partQty.Count, "") If IsNumeric(partQty.Count) = True Then If CDblAny(partQty.Count) <> CDblAny(iProperties.Value(docFile.DisplayName, "Project", "Authority")) Then iProperties.Value(docFile.DisplayName, "Project", "Authority") = partQty.Count partDoc.Close Else partDoc.Close End If End If Else Dim docDisplayName As String If docFile.LevelOfDetailName <> "Master" Then docDisplayName = (Left(docFile.DisplayName, Len(docFile.DisplayName) - Len(docFile.LevelOfDetailName) - 3)) 'MessageBox.Show( (Left(docFile.DisplayName, Len(docFile.DisplayName) - Len(docFile.LevelOfDetailName) - 3)) ,"") Else docDisplayName = docFile.DisplayName 'MessageBox.Show(docFile.DisplayName,"") End If If iProperties.Value(docDisplayname, "Project", "Authority") <> "Assembly" Then iProperties.Value( docDisplayName, "Project", "Authority") = "Assembly" End If End If End If Next Else MessageBox.Show("You must have a valid Assembly document open before using this code!", "File Type Mismatch!",MessageBoxButtons.OK,MessageBoxIcon.Exc lamation) End If
Thanks MegaJerk, your assertions are right on target.
The end goal is to place "Quantity Req'd: X" on the print for the component without me having to manually do it. I don't use the "Authority" field, so I guess I really don't care much about which particular field that we use to populate the data.
And yes, for custom components, I typically enter the word "Make" in the vendor field for the component model. This will then differentiate the custom components in the parts lists on my assembly prints.
So I planned on using the Vendor field as a trigger point for the modification. But your way looks to be more sophisticated, so that will probably work too.
Thanks very much for your help! I'll paste your code in and run some tests and see how it goes.
Wait, is this VBA or iLogic?
It won't run in VBA. I get errors in the first couple lines, the MessageBox command at the bottom is bad, and I need to change most of the variable setting steps to utilize the "Set" method.
Am I missing something?
Ahhh, well that explains it.... I'm even less experienced with this. So I create a rule with this pasted in, right? How do you trigger it?
OK, so I've got that stuff figured out and I've got a rule created. However, in my test assembly, it's getting hung up on a phantom sub-assembly created by the bolted joint wizard.
The error is:
*** iProperties: The component named "Bolted Connection" was not found.***
Can we filter out components that are phantom?
Ah! I’ve never messed with virtual components. If you’re using Inventor 2012 and happen to be able to post a small assembly that has everything needed, do so. Otherwise, when I get home I will mess around with recreating (and fixing) said error.
Well I'm messing around a little myself with the iLogic code (my first time) and it looks pretty handy......
However, it looks like most of the fields that I would use to differentiate "make" parts for this code are unavailable to iLogic. It doesn't look like I can choose the "BOM Structure" field or the "Vendor" field. Am I wrong?
I'm using IV2012, but I really can't post my assembly because it's got customer proprietary stuff in it. I'll see if I can get around to making up a test one with just some plates and a bolted joiint in it.
Here's a simple assembly with two flat plates and a bolted joint holding them together. Same issue is present with the iLogic rule. I'll be messing around a bit too over the weekend.
Thanks again for your help!
Access a broad range of knowledge to help get the most out of your products and services.