Inventor Customization

Inventor Customization

Reply
Distinguished Contributor
tdswanson
Posts: 148
Registered: ‎03-07-2012
Message 1 of 25 (982 Views)
Accepted Solution

Use VBA to Add Qty Req'd to Custom Properties for Components from the ****'y?

982 Views, 24 Replies
09-28-2012 09:04 AM

Hello:

 

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

  Endif

 

Next BOM Item

 


Can someone help and/or Is there an easier way?

 

Thanks!!

********************************************************
Inventor 2015 64-bit
Win 7 Pro 64-bit SP1

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. 


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. 

Valued Mentor
MegaJerk
Posts: 281
Registered: ‎01-26-2011
Message 2 of 25 (979 Views)

Re: Use VBA to Add Qty Req'd to Custom Properties for Components from the ****'y

09-28-2012 09:53 AM in reply to: tdswanson

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.FullFileName, False)
Dim partQty As ComponentOccurrencesEnumerator
partQty = assemblyDef.Occurrences.AllReferencedOccurrences(partDoc)
'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.Exclamation)
End If 

 

--------------------------------------------------------------------------------------

If my solution seems to remedy your problem, please press the Accept Solution button, -
as it increases my power levels and will eventually help to elevate me towards outer space.

Check out my iLogic injection tool here : http://goo.gl/ce1Qg
--------------------------------------------------------------------------------------
Distinguished Contributor
tdswanson
Posts: 148
Registered: ‎03-07-2012
Message 3 of 25 (976 Views)

Re: Use VBA to Add Qty Req'd to Custom Properties for Components from the ****'y

09-28-2012 10:02 AM in reply to: MegaJerk

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.

********************************************************
Inventor 2015 64-bit
Win 7 Pro 64-bit SP1
Distinguished Contributor
tdswanson
Posts: 148
Registered: ‎03-07-2012
Message 4 of 25 (972 Views)

Re: Use VBA to Add Qty Req'd to Custom Properties for Components from the ****'y

09-28-2012 10:18 AM in reply to: MegaJerk

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?

********************************************************
Inventor 2015 64-bit
Win 7 Pro 64-bit SP1
Valued Mentor
MegaJerk
Posts: 281
Registered: ‎01-26-2011
Message 5 of 25 (970 Views)

Re: Use VBA to Add Qty Req'd to Custom Properties for Components from the ****'y

09-28-2012 10:19 AM in reply to: tdswanson

iLogic

--------------------------------------------------------------------------------------

If my solution seems to remedy your problem, please press the Accept Solution button, -
as it increases my power levels and will eventually help to elevate me towards outer space.

Check out my iLogic injection tool here : http://goo.gl/ce1Qg
--------------------------------------------------------------------------------------
Distinguished Contributor
tdswanson
Posts: 148
Registered: ‎03-07-2012
Message 6 of 25 (968 Views)

Re: Use VBA to Add Qty Req'd to Custom Properties for Components from the ****'y

09-28-2012 10:21 AM in reply to: MegaJerk

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?

********************************************************
Inventor 2015 64-bit
Win 7 Pro 64-bit SP1
Distinguished Contributor
tdswanson
Posts: 148
Registered: ‎03-07-2012
Message 7 of 25 (964 Views)

Re: Use VBA to Add Qty Req'd to Custom Properties for Components from the ****'y

09-28-2012 10:27 AM in reply to: tdswanson

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?

 

Thx

********************************************************
Inventor 2015 64-bit
Win 7 Pro 64-bit SP1
Valued Mentor
MegaJerk
Posts: 281
Registered: ‎01-26-2011
Message 8 of 25 (961 Views)

Re: Use VBA to Add Qty Req'd to Custom Properties for Components from the ****'y

09-28-2012 10:38 AM in reply to: MegaJerk

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.

Darn! 

--------------------------------------------------------------------------------------

If my solution seems to remedy your problem, please press the Accept Solution button, -
as it increases my power levels and will eventually help to elevate me towards outer space.

Check out my iLogic injection tool here : http://goo.gl/ce1Qg
--------------------------------------------------------------------------------------
Distinguished Contributor
tdswanson
Posts: 148
Registered: ‎03-07-2012
Message 9 of 25 (959 Views)

Re: Use VBA to Add Qty Req'd to Custom Properties for Components from the ****'y

09-28-2012 10:41 AM in reply to: MegaJerk

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.

 

Thx!

********************************************************
Inventor 2015 64-bit
Win 7 Pro 64-bit SP1
Distinguished Contributor
tdswanson
Posts: 148
Registered: ‎03-07-2012
Message 10 of 25 (940 Views)

Re: Use VBA to Add Qty Req'd to Custom Properties for Components from the ****'y

09-28-2012 01:36 PM in reply to: tdswanson

OK:

 

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!

********************************************************
Inventor 2015 64-bit
Win 7 Pro 64-bit SP1
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.