- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
How do I check if a parameter exists?
As goes the title, I have tried to mimic the shared version:
SharedVariable.Exists("VariableName")
by doing this:
If Parameter.Exists(Connection, "Hex_AFc") Then MsgBox("this parameter exists") Else MsgBox("this parameter does not exist") End If
but that doesn't work. The best I've managed is a try catch:
Try If Parameter(Connection, "Hex_AFc") MsgBox("this parameter exists") End If Catch MsgBox("this parameter does not exist") End Try
but it's not very slick. Is there a better way?
Thanks,
Harvey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
If you are willing to use the Inventor API (in an Ilogic rule) then you could try something like this:
Dim doc As PartDocument = ThisApplication.ActiveDocument
Dim def As PartComponentDefinition = doc.ComponentDefinition
Dim parameters = def.Parameters.Cast(Of Parameter)
Dim exsits = parameters.Any(Function(p) p.Name.Equals("test1"))
If exsits Then
MsgBox("Parameter exist")
End If
Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Blog: hjalte.nl - github.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I’m on holiday and don’t have a computer here. But just 2 quick points. Are you running inventor 2025? It has some late binding issues.
this code (as it is) only works on part.
Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Blog: hjalte.nl - github.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
good monring,
here is the code i use to check if a parameter exists, it uses a loop to check a name. i've found it works well and is robust.
'set ref to active doc Dim oActiveDoc As Document = ThisApplication.ActiveDocument 'set ref to user parameters Dim oUserParams As UserParameters = oActiveDoc.ComponentDefinition.Parameters.UserParameters 'set ref to user parameter Dim oUserParam As UserParameter 'set ref to counter Dim i As Integer i = 1 'set up param search name Dim oParamSearchName As String = "TestName" For Each oUserParam In oUserParams If oUserParam.Name = oParamSearchName Then Call MsgBox("User Paramater - " & oParamSearchName & " - Exist.", vbOKOnly, "Check User Paramaters") Exit For End If 'if count = i then param doesn't exisit trip message box If i = oUserParams.Count Then Call MsgBox("User Paramater - " & oParamSearchName & " - Does NOT Exist.", vbOKOnly, "Check User Paramaters") i = i + 1 Next
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Function ParameterExists(paramName As String) As Boolean
Dim doc As Document = ThisDoc.Document
Dim params As Parameters
If doc.DocumentType = Inventor.DocumentTypeEnum.kDrawingDocumentObject Then
params = doc.Parameters
Else
params = doc.ComponentDefinition.Parameters
End If
For Each p As Parameter In params
If p.Name = paramName Then Return True
Next p
Return False
End Function 'ParameterExists
You can add this function to your code - it'll return True if the parameter name you give it exists in the document.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I typically just use a try/catch to try and set the value, and then catch the error when it does not exist and add it.
Keep in mind that drawings and models have the user parameter set organized a bit differently, since drawings do not contain a component definition.
If Thisapplication.ActiveDocument.DocumentType = DocumentTypeenum.kDrawingDocumentObject Then oUserParams = ThisDoc.Document.Parameters.UserParameters Else oUserParams = ThisDoc.Document.ComponentDefinition.Parameters.UserParameters End If dValue = 99 Try Parameter( "Foo") = dValue Catch oParam = oUserParams.AddByValue("Foo", dValue, UnitsTypeEnum.kDefaultDisplayLengthUnits) End Try
If you are working with assembly occurrences, then something like this will work
Dim oOcc As ComponentOccurrence = Component.InventorComponent("MyPart") oUserParams = oOcc.Definition.Document.ComponentDefinition.Parameters.UserParameters dValue = 99 Try Parameter(oOcc.Name, "Foo") = dValue Catch oParam = oUserParams.AddByValue("Foo", dValue, UnitsTypeEnum.kDefaultDisplayLengthUnits) End Try
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
@harvey_craig2RCUH wrote:Try If Parameter(Connection, "Hex_AFc") MsgBox("this parameter exists") End If Catch MsgBox("this parameter does not exist") End Try
You can make this look a little better by using a single line IF-statement. You don't need an IF...End If-construction when there's not a block of code in/between it. #Just a suggestion.
Personally, I use a dedicated sub procedure similar to @ryan.rittenhouse
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Thanks @Curtis_Waguespack
I'm trying another way like @AndrewHumiston and @ryan.rittenhouse because I'm using "On Error Resume Next" Syntax.
Thanks all of you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @JelteDeJong
I'm working on Inventor 2023. I'm trying find some new way to check existed parameters. I'm very curious about your codes. It was really different from my previous understanding.
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
good morning,
I want to caution you on using 'on error' handling.
In my experience it is better to solve the issue that is causing the error than to resume when an error has happened.
i will share why. the company i work for has a configurable sales CAD system that populates a model with other assemblies that are controlled by turning off/on visibility. I know terrible idea, but its been around for about 10 years, all the code is located in one rule that is approx. 1000 lines long, and uses 'on error; & "goto" syntax. While it seems like a good practice to get the job done, in reality it is 'lazy' coding. When you are the person coming behind code, it is tough to follow those kinds of event handlers. Its taken way to many hours to find the simplest bugs to flush out how it works and why.
i hope this helps, please reach out with any questions, I don't pretend to know everything, but I will sure try to answer your question!
@AndrewHumiston - this post has been edited due to Community Rules & Etiquette violation.