Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Most wished for API call - Does this iProperty Exist?

2 REPLIES 2
Reply
Message 1 of 3
wfajber
208 Views, 2 Replies

Most wished for API call - Does this iProperty Exist?

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?

 

 

 

 

2 REPLIES 2
Message 2 of 3
Curtis_Waguespack
in reply to: wfajber

Hi @wfajber

I like your idea of adding an Exists function.

 

Recall though, that internally this line checks for iProperty and creates it if it does not exist. If it does exist it just sets it. So there is often no need to check for it.

iProperties.Value("cw_Block1:1", "Custom", "Test McTester") = 9999

 

To test to see if a property exists without setting it, I typically use something like this ( I think your Exists function idea would be better though).

Try : x = iProperties.Value("cw_Block1:1", "Custom", "Son Of Test McTester") IsNot Nothing: _
Catch : x = False: End Try

MsgBox("property exits = " & x, , "ilogic")

 I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

Message 3 of 3
jcantrellP5GHY
in reply to: wfajber

Thanks, Curtis!  This worked a treat for me.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report