Most wished for API call - Does this iProperty Exist?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I know that there some excellent programmers on this forum and I would welcome their opinion.
In my work, lots of our iLogic rules are around checking iProperties or adding iProperties. But if you try and read a iProperty that doesn't exist, then you get an error. We want to check if it exists first, and if it doesn't then add it and set it to a default value. If there is already data there, I don't want to overwrite it.
The way that is often shown using a try/catch as sample below. I don't know if this is elegant computationally, because you are almost expecting an error. I am not so fond of this construct if I have to traverse through a big assembly.
Try Dim myprojectname As String = iProperties.Value("Switchyard:2", "Custom", "myproject") Catch iProperties.Value("230KV Switchyard:2", "Custom", "myproject") = "2343" End Try
I sometimes write it as a function and just search for the iProperty I am looking for. I guess that this is probably what is happening internally when I look for the property using a construct like Property.Item("name"). Finding it by name still has to loop through the properties until it funds a match.
Private Function PropertyExists(PropSet As PropertySet, PropertyName As String) As Boolean Dim propcount As Integer Dim prop As Inventor.Property ' check if property of that name exists For propcount = 1 To PropSet.Count prop = PropSet.Item(propcount) If prop.Name = PropertyName Return True 'early exit returning true End If Next Return False' found = false End Function
This appears more graceful in that I am not doing the error handling.
My question is two parts:
1. What am I missing? Is there a neat clean way of checking for iProperty existence?
2. Is checking if it exists something to put on the Inventor Ideas wishlist. Right now, a Propertyset has member functions for Add and Delete. How about Add / Delete/ Exists?
As I type this, I think I suspect that this is a pretty big ask, because it would affect how all the collections are constructed. But would that be so bad? Does a Workpoint of this name already exist? Can I add a feature with this name, or does that name already exist? It seems to me it would be convenient.
What does the community think?