Hello folks,
I'm trying to run a check to see if a parameter exists, and if not, create the parameter and give it an initial value. That initial value is then represented in a form's textbox where a user can modify if need be. If the parameter exists, I just need to reed the existing parameter into the textbox. The code below works, except that it errors every time it's run, and creates the parameter over and over with a suffix.
I do believe the code:
Dim invParameterName As Parameters invParameterName = oParam.Item("Thickness_A_FX")
Is what is messing things up, but can't seem to find a solution. Any help would be greatly appreciated.
The relevent code (I believe):
Public Class GlobalParameters Public Thickness_A_FX As Parameter
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load _invApp = Marshal.GetActiveObject("Inventor.Application") ' Get the Parameters collection object. Dim oParam As Parameters = _ _invApp.ActiveDocument. ComponentDefinition.Parameters Dim oUserParams As UserParameters oUserParams = _invApp.ActiveDocument. ComponentDefinition.Parameters. UserParameters Dim invPartDoc As PartDocument invPartDoc = _invApp.ActiveDocument ' Add any initialization after the InitializeComponent() call. _invApp = Marshal.GetActiveObject("Inventor.Application") ' Get the Parameters collection object. ' Attempt to get an existing custom property named "Thickness_A_FX". On Error Resume Next Dim invParameterName As Parameters invParameterName = oParam.Item("Thickness_A_FX") If Err.Number <> 0 Then MsgBox("Error") ' Failed to get the property, which means it doesn't exist ' so we'll create it and give it a reasonable initial value. oParam = oUserParams.AddByExpression _ ("Thickness_A_FX", ".875", "in") 'Get the value of Thickness_A_FX into the TextBox on the form Thickness_A_FX = oParam.Item("Thickness_A_FX") Else 'Nada End If 'Add the current value to the Textbox. txtThicknessA.Text = CStr(Thickness_A_FX.Expression) ' Update the document. _invApp.ActiveDocument.Update() End Sub
Thanks1
Solved! Go to Solution.
Solved by MRanda. Go to Solution.
I'm far from an expert, but I get my parameter information another way:
Dim oTargetDoc As DrawingDocument
Set oTargetDoc = ThisApplication.ActiveDocument
Variable = oTargetDoc.PropertySets.Item("User Defined Properties").Item("For").Value
I'm not saying the method you're using is wrong (i've no idea to be honest), just that all the references i've seen have been in the manner above.
The inference that it keeps generating the parameter over and over suggests that whether or not your parameter exists, it's returning an error that isn't 1.
I assume you've tried removing the add code and just replacing it with something like message boxes to ket you know the If statement is doing what you want it to. If you're not able to get to the else statement, it would seem your error number is generating what you think it is.
Thanks for the reply mslosar!
Your code got me thinking in a different direction, and the problem is now solved.
Dim invParameterName As String invParameterName = oParam.Item("Thickness_A_FX").Name If Err.Number <> 0 Then MsgBox("Error") ' Failed to get the property, which means it doesn't exist ' so we'll create it and give it a reasonable initial value. oParam = oUserParams.AddByExpression _ ("Thickness_A_FX", ".875", "in") 'Get the value of Thickness_A_FX into the TextBox on the form Thickness_A_FX = oParam.Item("Thickness_A_FX") Else Thickness_A_FX = oParam.Item("Thickness_A_FX") End If
I believe I was mixing the parameter's Value and Name. At any rate, it works. Thank you for your time, it's greatly appreciated!
Mark