List of current SharedVariables?

Skadborg.NTI
Advocate

List of current SharedVariables?

Skadborg.NTI
Advocate
Advocate

Is there a way to get a list of all SharedVariables currently defined?

 

If I only consider my own code, then I know when it is ok to call SharedVariable.RemoveAll(). But this might destroy code outside my control.

Reply
Accepted solutions (2)
451 Views
15 Replies
Replies (15)

WCrihfield
Mentor
Mentor

Very good question.  I would also like to know if this is possible.  Often when I have code that is creating/using them, I will write something into the iLogic Log window about that event, to make it easier to recall when they were created, what their names are, and what their values are, and when their values get changed.  However, I do not have a code based routine for extracting only that specific data from the iLogic Log, when there may be tons of other data in it, for checking the status of all of them.  Most likely only the folks at Autodesk would know if this was possible, because they know how that data is being managed behind the scenes, and wrote the programming to support the existing iLogic tools.  They may have a good reason for not having a tool like that, or they may have simply not thought about it yet, or there may not be a good way to do it yet.  Lots of possibilities.

 

And since those tools are iLogic API based, instead of Inventor API based, I am not sure if we have an alternate route to accessing the resources that manage them, or an alternate Inventor API tool that can be used to mimic that functionality in the short term that might give us more control over all instances in existence.  I know that we can copy stuff up into the system 'clipboard', and retrieve the last thing we sent up there, but not sure about ways to 'assign names' to pieces of data that way.

Perhaps @MjDeck would know more about this.

Another thoughts would be to search within the Inventor Ideas community for this 'Idea', and if not found, add an idea in there for this.  If an idea like this already exists, you can vote for it.  If you create an idea for this, you can post a link to it here, so it will be easier for some folks to find it and vote for it.  I would certainly vote for that Idea.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

MjDeck
Autodesk
Autodesk

It would be easy for us to add a method to list all SharedVariables. I'll double check to see if an Idea already exists.

This is an iLogic-only function. It doesn't use anything in the Inventor API.
Here's a way to get more control over the names and values: create your own Dictionary and store values in it. A Dictionary is used to implement SharedVariables. You can add your own on top of that.
Here's sample code. In one rule:

If Not SharedVariable.Exists("AllVars822") Then
	SharedVariable("AllVars822") = New Dictionary(Of String, Object)
End If

"AllVars822" was chosen just to try to get a unique name.

Then other rules can have code like:


SharedVariable("AllVars822")("Text1") = "Test"
SharedVariable("AllVars822")("Value2") = 503

Dim retrieved = SharedVariable("AllVars822")("Value2") 
Logger.Info("retrieved = {0}", retrieved)

Dim dict As Dictionary(Of String, Object) = SharedVariable("AllVars822")

dict("NewValue3") = Date.Now

For Each nameValuePair In dict
	Logger.Info("{0} = {1}", nameValuePair.Key, nameValuePair.Value)
Next 


You would be using just one SharedVariable as a starting point, and building everything on top of that.
Here's the documentation for the Dictionary class.




Mike Deck
Software Developer
Autodesk, Inc.

WCrihfield
Mentor
Mentor

Thanks Mike.  Brilliant idea.  I knew that we could store a Document as a value, and a List(of Object) as a value of a 'SharedVariable', but had not considered the possibility of using a Dictionary(Of String, Object) as the value of a SharedVariable, then using that to keep track of either all required 'shared' data, or the names of all 'SharedVariables'.

I had also looked for the term SharedVariable in the Inventor Ideas forum, but only found one post, and it was not for this idea.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Skadborg.NTI
Advocate
Advocate

Thank you for your suggested workaround. It is surely usefull.

How ever I find it a sort of "add on" to SharedVariable. So I still think we could use a possibilty to get a list of all SharedVariables.

I couldn't find an existing idea on this, so I have created it now: https://forums.autodesk.com/t5/inventor-ideas/list-of-current-sharedvariables/idi-p/13137020

0 Likes

JBerns
Advisor
Advisor

@WCrihfield / @MjDeck ,

 

SharedVariable Help page:

 

  • iLogic shared variables are shared between rules and stored in memory. Unlike Inventor parameters, they are not associated with any part or assembly. 

I created a SharedVariable in a new assembly. I was surprised to discover the SharedVariable was accessible in a second new assembly.

 

My workflow currently uses an assembly template that creates a few SharedVariables (SVs). If SVs are not unique in a new document, then two assemblies cannot be edited simultaneously because they may need different values. If the first assembly is closed and then a new assembly created, the SVs are still present. I will have to run code to reset or clear my shared variables. Or ask users to exit and restart Inventor for a new assembly.

 

Would you agree with these statements? Is this true for Inv 2022 and newer? I may explore dictionaries discussed above to "associate" a collection of SVs for each assembly.

 

Thank you for your time and attention. I look forward to your reply.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes

Skadborg.NTI
Advocate
Advocate

This is precisely the idea with SharedVariables 🙂 Use them to store information that is session based, not document based.

0 Likes

WCrihfield
Mentor
Mentor

I suspect you can use them however you want, as long as you keep in mind that once they are created, they will continue to reside in Inventor's session memory until you specifically remove them (ISharedVariable.Remove Method or ISharedVariable.RemoveAll Method), or set its value to Nothing, or that session of Inventor ends.  Since they are not document based, they do not care what documents are opened or closed.

I assume that once the iLogic add-in loads, that Dictionary(Of String, Object) behind that system likely gets instantiated (an instance created), but with no entries in it yet.  Then as we use that 'SharedVariable' RuleObject in our rules, that adds entries, removes entries, or changes the values of entries.  Then once we either unload that add-in, or close Inventor, the resources/data it was holding onto get released, and disposed of.  Seems rather simple in theory.  Sort of like when you have a really large rule, with multiple Sub / Function type routines, and have declared a variable between the routines, or at the Class (or higher) level, then your routines are able to access that variable and edit its value, as long as that rule is running.  Only the variable for the Dictionary is at the highest level of the add-in, which gets loaded either when we start Inventor, or when we open/create the first Document, since that is when the iLogic user interface elements first become visible.

So, using a single SharedVariable to store data that is document specific, and there may  be multiple documents using that same SharedVariable, that sounds like it could be tricky to manage.  I like to use the New GUID.ToString system sometimes, to keep track of my iLogic rule based event handlers, since it seems like I can not create my own add-ins, due to corporate security policies.  The Document.InternalName is sort of similar, but unfortunately, it is possible for more than one document to have the same InternalName, so not a super reliable one to use.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

JBerns
Advisor
Advisor

@Skadborg.NTI,

Ackn.

So Inventor cannot limit SVs to the current document?

That would explain the unusual behavior when I created a second assembly which read the SV values from the first assembly instead of default values.

Thanks for the clarification. I may need a reset SV rule users will run after starting a second assembly.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes

JBerns
Advisor
Advisor

@Skadborg.NTI,

I was using Visual Studio to step through Mike's code.

Is any of this info useful to access existing SVs?

JBerns_0-1733775482234.png

 

 

-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes

MjDeck
Autodesk
Autodesk
Accepted solution

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.

JBerns
Advisor
Advisor

@MjDeck (Mike),

Thanks for the quick reply and the example code.

I will explore implementing your code.

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes

MjDeck
Autodesk
Autodesk
Accepted solution

Note that I made an update in the sample code that I originally posted on 12-09-2024.


Mike Deck
Software Developer
Autodesk, Inc.

CGBenner
Community Manager
Community Manager

@JBerns 

Did the information provided answer your question? If so, please use Accept Solution so that others may find this in the future. Thank you very much!

BTW... nice bobble head avatar! 😂


Chris Benner
Industry Community Manager – Design & Manufacturing


If a response answers your question, please use  ACCEPT SOLUTION  to assist other users later.


Also be generous with Likes!  Thank you and enjoy!


Become an Autodesk Fusion Insider
Inventor/Beta Feedback Project
0 Likes

JBerns
Advisor
Advisor

@CGBenner (Chris),

 

Since I am not the OP, I do not have access to the [Accept Solution] button.

However, the information was helpful and I have given Likes to the solutions offered.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional

CGBenner
Community Manager
Community Manager

@JBerns Thanks Jerry!


Chris Benner
Industry Community Manager – Design & Manufacturing


If a response answers your question, please use  ACCEPT SOLUTION  to assist other users later.


Also be generous with Likes!  Thank you and enjoy!


Become an Autodesk Fusion Insider
Inventor/Beta Feedback Project
0 Likes