Hi Jerry,
You can put anything in shared variables, including document-based data.
Generally you will run a rule that sets a given shared variable before another rule that consumes it. So it should be safe to open one assembly, run rules, close it, and then open another. If the assemblies are similar, the rules in the second assembly should overwrite the shared variables set by the first. But if you want to be safe: before opening the second assembly, run a rule that calls SharedVariable.RemoveAll.
If you want to maintain a separate set for each assembly, you could do it by keeping a custom Dictionary for each assembly.
In my example above, I have a single top-level shared variable named "AllVars822".
Instead of that, you could create a separate dictionary for each top-level assembly, named by the assembly filename. The only limitation is that this will not work if the assembly has not been saved. Also, because it uses ThisApplication.ActiveDocument, it won't work on Inventor Server (e.g. Design Automation or Vault job processor). Maybe there's a way to avoid using ActiveDocument. But for desktop Inventor, this might be worth a try.
In the top-level assembly, you could have a rule like this:
Dim fullAssemblyName = ThisDoc.Document.FullFileName
If String.IsNullOrEmpty(fullAssemblyName) Then Exit Sub
If Not SharedVariable.Exists(fullAssemblyName) Then
SharedVariable(fullAssemblyName) = New Dictionary(Of String, Object)
End If
And in subassemblies and parts:
Dim fullAssemblyName = ThisApplication.ActiveDocument.FullFileName
If String.IsNullOrEmpty(fullAssemblyName) Then Exit Sub ' or do something else that does not require shared variables.
If Not SharedVariable.Exists(fullAssemblyName) Then Exit Sub
Dim dict As Dictionary(Of String, Object) = SharedVariable(fullAssemblyName)
' read or write values in dict, for example:
dict("NewValue3") = Date.Now
This works as long as the ActiveDocument is the assembly. If you open a component part in its own window, it becomes the active document. Then it will lose this connection to the shared variables from the assembly. However, you can activate the assembly again and successfully use the shared variables.
Update: note that ThisDoc.Document is used in the rule in the top-level assembly, but ActiveDocument is used in the component documents. This makes sure that the top-level document owns its variables. ThisDoc.Document will work even if the assembly is not the active document.
Another option: instead of shared variables you could use transient Attributes.
If you set attributes on the top-level assembly, you can use ThisApplication.ActiveDocument to find them from rules in a component document.
Mike Deck
Software Developer
Autodesk, Inc.