From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Access any property of any class BY NAME

Solved!
Go to solution

Hello everybody,

 

While developing some generic scripting tools, I had the need of a function to read/write a property of a class type by its name. For example, given a refnum to a control, read the value of the property "Caption.Text", or given an LVClass "MyDevice.lvclass", set the property "Timeout (ms)" to "1000". Basically read/write dynamically any property of any type that supports property nodes, given the property name.

 

After some research, I concluded there is no generic way of doing that. So, as such functionality has already been asked and declined (https://forums.ni.com/t5/LabVIEW-Idea-Exchange/Set-Get-Property-By-Name/idi-p/2762750), my idea was to share my first attempt of implementation here, so anyone needing this functionality could get, improve and share the code with others.

 

I came up with a VI that can read any property of the "Generic" class (and subclasses) by generating property accessors on-the-fly in a temp directory using VI Scripting and reusing them if the property has already been requested. The main issues I would like to improve are the following (by order of priority):

 

1. Optimize execution speed and memory consumption (sometimes gets slow, need to restart LV to make it faster again)

2. Generalize it to all classes that can be selected for a property node, also including user LVClasses...

3. Add full support for nested properties (maybe by cascading several accessors)

...

4. Make it work within the runtime engine ??? (so not generating VIs at runtime anymore)

 

So, any idea to improve the code ? (LV 2018)

0 Kudos
Message 1 of 9
(2,893 Views)
Solution
Accepted by topic author raphschru

No need to script the action in a new VI!

 

There are two methods GetProperty\SetProperty that gets\sets a property on any class by it's ID. It's inputs are the ID and a variant.

 

You can get all supported property IDs from a class node.

 

So, make a LUT for each class in the entire hierarchy, and all it's properties vs it's IDs. So, classname+property vs ID. Use a map or old school variant attributes.

 

Look up a class's property ID by it's name, and invoke Get\SetProperty.

 

BTW this only works for Generic and it's children. Other VI server classes (Project, ProjectItem, etc.) don't have these methods.

0 Kudos
Message 2 of 9
(2,826 Views)

This might be helpful.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 3 of 9
(2,795 Views)

Has there been any development on this since you posted this / or an example VI?
I've had an attempt myself (by editing OPs code) but can't seem to grasp how I would implement the use of GetProperty\SetProperty. Would your method work in the runtime engine?

Cheers

0 Kudos
Message 4 of 9
(1,225 Views)

At this time, it was just for recreational purpose, so I have not tested how it performs in production or in the runtime engine.

Here is my implementation of the accepted solution where I can do something like this:

 

raphschru_0-1682699875096.png

raphschru_1-1682699915263.png

 

I can also traverse sub-objects and cluster elements by using a dotted notation.
Also, keep in mind that you have to run the given script at least once to allow the creation of a property look-up table for your current version of LabVIEW.

Message 5 of 9
(1,194 Views)

Since you're now in LV20,why not use a map?

0 Kudos
Message 6 of 9
(1,131 Views)

If you look at my code, you'll see that I have used a map of map to store all property names and ids for each class.

0 Kudos
Message 7 of 9
(1,124 Views)

@raphschru wrote:

If you look at my code, you'll see that I have used a map of map to store all property names and ids for each class.


I did look at the code you provided an image of, and you can use a map there too.

 

So, now I see it's just the example you've made that indeed doesn't need performance...

0 Kudos
Message 8 of 9
(1,096 Views)

This is perfect thanks. Used it for a fire and forget animation library.

0 Kudos
Message 9 of 9
(1,086 Views)