Hello Rob and Engilic,
I am happy to share my code but there is so much. I have continued to develop what this rule can do, and it has grown to almost 700 lines of code.
The biggest advancement is that I figured out a way to make the rule Alacarte. I now use this code for almost all of my automation. I send the rule a list of tasks from other external rules, and it does only what is asked to the assembly (or part) and then all children in the case that it is running from an assembly.
The first rule I will post below is a rule that we call "Update All". The rule builds a task list, then calls the main rule that does all of the tasks. We have many rules now that have this task list and we just comment out the tasks we don't want. For example, the rule that only changes sheet metal gauge of all parts in an assembly will have everything commented out except "GUAGE". It will call the same rule as "Update All" but will only do one task as it loops through everything. Here is the calling rule:
Sub Main()
Dim TaskList As Inventor.NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
TaskList.Clear
TaskList.Add("task1", "SCREENSHOT")
TaskList.Add("task3", "USERFORM")
TaskList.Add("task4", "MASS")
TaskList.Add("task5", "PARAMETERS")
TaskList.Add("task6", "SUBFORM")
TaskList.Add("task7", "TITLE")
TaskList.Add("task8", "TEMPLATES")
TaskList.Add("task9", "GAUGE")
'TaskList.Add("task10", "RENAMEPARAMETERS")
iLogicVb.RunExternalRule("Update", TaskList)
End Sub
Below is the "Update" rule that is called at the end of the rule above (heavily trimmed for length).
Here are a few key lines with explanation.
First:
oDesTrackProps("Designer").Value = iProperties.Value("Project", "Designer")
The left side of the equal sign is the iProperty "Designer" of each child and the right is the iPropery "Designer" of the parent assembly.
second:
OpenDoc.componentdefinition.parameters.UserParameters("T_2").value = Parameter("T_2")
The left side of the equal sign is the child parameter named "T_2" and the right is the parent parameter of the same name. (Note: child parameters accessed this way will always be metric. You may need to convert even if all your settings are imperial)
Sub main()
Dim oDoc As Document
Dim oAsmDoc As AssemblyDocument
Dim oPartDoc As PartDocument
oDoc = ThisDoc.Document
Dim OpenDoc As Document
'Make sure code is running on an assembly or Part
If oDoc.DocumentType = kPartDocumentObject Then
GoTo Part_Code
ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
oAsmDoc = ThisApplication.ActiveDocument
ElseIf oDoc.DocumentType = kDrawingDocumentObject Then
MsgBox("This is a Drawing",,"Ending Run")
Exit Sub
End If
'■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
'Tasks for Assembly File
'■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
'[YOUR CODE FOR PARENT ASSEMBLY HERE] - I WILL LEAVE ONE EXAMPLE BELOW
'Calling an external rule to capture a screenshot of the model and save it to our server but only if the calling rule asks for "SCREENSHOT"
If IsInArrayIndex("SCREENSHOT", DoTasks) > 0 Then
'1. Take a screen shot ■■ ╫╫╫╫╫╫╫╫╫╫"SCREENSHOT"╫╫╫╫╫╫╫╫╫╫
uu = Len(oAsmDoc.DisplayName)
If uu = 8 Then 'Only take capture of top level models (all top level models have 8 digit display names)
iLogicVb.RunExternalRule("Image Capture")
End If
End If
'■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
'Now loop through all assembly children
'■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
For Each OpenDoc In oAsmDoc.AllReferencedDocuments
'''Tasks for ALL sub assemblies and parts
'putting the full file name of each item in the assembly into a shared STRING variable that can be used in external rules
SharedVariable("WorkDoc") = OpenDoc.FullFileName
' setting up propery sets so that we can interact with iProperties of each item
' 'See list at bottom (below sub) of what iProperties are in what set
Dim oPropSets As PropertySets = OpenDoc.PropertySets
Dim oSumInfo As PropertySet = oPropSets.Item("Inventor Summary Information")
Dim oDocSumInfo As PropertySet = oPropSets.Item("Inventor Document Summary Information")
Dim oDesTrackProps As PropertySet = oPropSets.Item("Design Tracking Properties")
Dim oUserDefProps As PropertySet = oPropSets.Item("Inventor User Defined Properties")
'[YOUR CODE FOR EACH ITEM HERE - LEAVING IN ONE EXAMPLE BELOW]
If IsInArrayIndex("SUBFORM", DoTasks) > 0 Then
'5. Update Form iProperties (Job#, Engineer)■■ ╫╫╫╫╫╫╫╫╫╫"SUBFORM"╫╫╫╫╫╫╫╫╫╫
If Parameter("T_1") = "" Or Parameter("T_1") = Nothing Then
'do nothing
Else
oDesTrackProps("Project").Value = Parameter("T_1")
Try
OpenDoc.componentdefinition.parameters.UserParameters("T_1").value = Parameter("T_1") 'update sub assembly userform
Catch
End Try
End If
If Parameter("T_2") = "" Or Parameter("T_2") = Nothing Then
'Do nothing
Else
oDesTrackProps("Designer").Value = iProperties.Value("Project", "Designer")
Try
OpenDoc.componentdefinition.parameters.UserParameters("T_2").value = Parameter("T_2") 'update sub assembly userform
Catch
End Try
End If
End If
'END OF LOOP
Next
'update all
iLogicVb.UpdateWhenDone = True
iLogicVb.DocumentUpdate
'zoom all (GIVES USER VISUAL THAT RULE IS FINISHED RUNNING
ThisApplication.ActiveView.Fit
'STOP THE RULE HERE
Exit Sub
'«««««««««««««««««■«■«■««««««■«««««««■«■«■««««■■■■■■■««««■«■«■««««««««««««««««««««««««««««««««««««««««««««««««««««««
'«««««««««««««««««■««««■««««■««■«««««■««««■««««««■««««««■«««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
'«««««««««««««««««■«■«■««««■«■■«■««««■«■«■«««««««■«««««««■«■«■««««««««««««««««««««««««««««««««««««««««««««««««««««««
'«««««««««««««««««■«««««««■««««««■«««■«««■«««««««■««««««««««««■«««««««««««««««««««««««««««««««««««««««««««««««««««««
'«««««««««««««««««■«««««««■««««««■«««■««««■««««««■«««««««■«■«■««««««««««««««««««««««««««««««««««««««««««««««««««««««
Part_Code: 'Code below only runs when this rule is called from a part file.
'==================================================
End Sub
'USE THIS AS REFERENCE LIST TO PROPERLY CALL IPROPERTIES - DOES NOT MATCH CURRENT IPROPERTY TABS IN SOFTWARE
'''1) Inventor Summary Information
''' 1) Title
''' 2) Subject
''' 3) Author
''' 4) Keywords
''' 5) Comments
''' 6) Last Saved By
''' 7) Revision Number
''' 8) Thumbnail
'''2) Inventor Document Summary Information
''' 1) Category
''' 2) Manager
''' 3) Company
'''3) Design Tracking Properties
''' 1) Creation Time
''' 2) Part Number
''' 3) Project
''' 4) Cost Center
''' 5) Checked By
''' 6) Date Checked
''' 7) Engr Approved By
''' 8) Engr Date Approved
''' 9) User Status
''' 10) Material
''' 11) Part Property Revision Id
''' 12) Catalog Web Link
''' 13) Part Icon
''' 14) Description
''' 15) Vendor
''' 16) Document SubType
''' 17) Document SubType Name
''' 18) Proxy Refresh Date
''' 19) Mfg Approved By
''' 20) Mfg Date Approved
''' 21) Cost
''' 22) Standard
''' 23) Design Status
''' 24) Designer
''' 25) Engineer
''' 26) Authority
''' 27) Parameterized Template
''' 28) Template Row
''' 29) External Property Revision Id
''' 30) Standard Revision
''' 31) Manufacturer
''' 32) Standards Organization
''' 33) Language
''' 34) Defer Updates
''' 35) Size Designation
''' 36) Categories
''' 37) Stock Number
''' 38) Weld Material
''' 39) Mass
''' 40) SurfaceArea
''' 41) Volume
''' 42) Density
''' 43) Valid MassProps
''' 44) Flat Pattern Width
''' 45) Flat Pattern Length
''' 46) Flat Pattern Area
''' 47) Sheet Metal Rule
''' 48) Last Updated With
''' 49) Sheet Metal Width
''' 50) Sheet Metal Length
''' 51) Sheet Metal Area
''' 52) Material Identifier
''' 53) Appearance
''' 54) Flat Pattern Defer Update
'''4) Inventor User Defined Properties
Public Function IsInArrayIndex(stringToFind As String, arr As Object) As Long
IsInArrayIndex = -1
Dim i As Long
For i = LBound(arr, 1) To UBound(arr, 1)
If arr(i) = stringToFind Then
IsInArrayIndex = i
Exit Function
End If
Next i
End Function
If you are having trouble with child iProperties it is likely an issue of using the correct "Property Sets". See the lines near the top that dim all of the ProperySets and then see the index at the bottom of what propertyset to use. These do NOT align with the current iProperties tab groups.
Much of the code above I have farmed from the forums and other online sources. I only claim a small amount of it as my own.
I hope this helps. Feel free to ask questions.