- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Community,
My goal is to update three custom iProperties in all the sheet metal parts of an assembly.
If a custom iProperty is missing, it needs to be added and then its value set.
I have evaluated using iLogic at the part level, but prefer to use VBA from the assembly level.
I am using VBA because of its better debugging (learning) interface. Therefore, no Try-Catch clauses, only On Error statements.
The three custom iProperties are:
- Customer: the customer name for the job
- Material: a classification code based on the sheet metal material
- Grade : a classification code also based on the sheet metal material
I am using Brian Ekins' ( @ekinsb ) code to get the references in the assembly.
https://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html
This gets all the file references in the assembly, including those in subassemblies.
I am using more code from Brian to update the custom iProperty.
https://modthemachine.typepad.com/my_weblog/2010/02/custom-iproperties.html
The UpdateCustomiProperty code works well when tested in a part document, but fails for me when called from the assembly level. No errors occur, but the iProperty is not added/updated.
Can anyone offer suggestions as to how to modify the UpdateCustomiProperty code to get the sheet metal part custom iProperties updated/added from the assembly level?
Here is what I have so far:
Option Explicit Public Sub ProcessReferences() 'Initialize strings Dim strCustomerName As String strCustomerName = "NONE SELECTED" 'Simplified here, but normally set through a UserForm 'Get the active assembly Dim oAsmDoc As AssemblyDocument Set oAsmDoc = ThisApplication.ActiveDocument
'yes, I will test here to ensure active doc is an assembly
'Get all of the referenced documents Dim oRefDocs As DocumentsEnumerator Set oRefDocs = oAsmDoc.AllReferencedDocuments 'Iterate through the list of documents Dim oRefDoc As Document For Each oRefDoc In oRefDocs 'Test if reference is a sheet metal part If oRefDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Call UpdateCustomiProperty(oRefDoc, "CUSTOMER", strCustomerName) End If 'Update document oRefDoc.Update Next End Sub Public Sub UpdateCustomiProperty(ByRef Doc As Document, ByRef PropertyName As String, ByRef PropertyValue As Variant) 'String 'Get the custom property set Dim customPropSet As PropertySet Set customPropSet = Doc.PropertySets.Item("Inventor User Defined Properties") 'Get the existing property, if it exists. Dim prop As Property On Error Resume Next Set prop = customPropSet.Item(PropertyName) 'Check to see if the above call failed. If it failed 'then the property doesn't exist. If Err.Number <> 0 Then 'Failed to get the existing property so create a new one. Call customPropSet.Add(PropertyValue, PropertyName) Else 'Change the value of the existing property. prop.Value = PropertyValue End If 'Update document Doc.Update End Sub
To test, just add some sheet metal parts to an assembly, add the code to VBA editor in the assembly, and then run macro named ProcessReferences.
Thank you for your time and attention. I look forward to the replies.
Kind regards,
Jerry
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
Solved! Go to Solution.