Cancel
Showing results for 
Search instead for 
Did you mean: 

Check if a attribute exist or not

Valued Contributor
Valued Contributor

Is it possible to check if a attribute exist or not without geting Unknown identifier

3 REPLIES

Re: Check if a attribute exist or not

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom
Use getAttrNo. If it returns a positive value, the attribute exists.

Gert Nomden
Senior Consultant | Tecnomatix Product Manager
cardsplmsolutions.com

Re: Check if a attribute exist or not

Gears Phenom Gears Phenom
Gears Phenom

I prefer using isNameUnique, which will work on all objects (even if they cannot have user-defined attributes, like the comment-attributes --> in that case you will get the "unknown identifier" error as well if you try to use getAttrNo) and it also checks for standard attributes. With getAttrNo you can only check for user-defined attributes.

However note that isNameUnique will also check whether there is an object with the given name, when you use it on folders or networks.

Cheers, Alex

____________________________________________________________
Alex Dilg, Consultant at SimPlan AG (www.SimPlan.de)
This post was good and/or helpful to you? Please provide some Kudos, thank you!

Re: Check if a attribute exist or not

Gears Phenom Gears Phenom
Gears Phenom

Hello,

 

The simplest approach which does not have many limitations would be ExecuteSilent, the following lines will tell you if the objects provided has a attribute called Tooltip on them or not.

print ExecuteSilent("param o: Object -> boolean; var text: string := o.Tooltip; return true;", SingleProc)
print ExecuteSilent("param o: Object -> boolean; var text: string := o.Tooltip; return true;", SingleProc1)

 

Using this you can run any code you want, but as soon as it crashes it will return the default value, so in this case if setting the value a local variable to the value of the attribute you are checking for fails then false is returned, if not then the code finishes running and true is returned.

 

While if all you want to do is to get the value then you could change the following code:

print SingleProc.Name, " - ", SingleProc.Tooltip
print SingleProc1.Name, " - ", SingleProc1.Tooltip

into:

print SingleProc.Name, " - ", ExecuteSilent("param o: Object -> string; return o.Tooltip", SingleProc)
print SingleProc1.Name, " - ", ExecuteSilent("param o: Object -> string; return o.Tooltip", SingleProc1)

which will do the same thing but if the Tooltip attribute does not exist then it will return the default value for the string data type.

 

JamesM