Create parameters that exists

Create parameters that exists

henrikubbe4438
Advocate Advocate
919 Views
5 Replies
Message 1 of 6

Create parameters that exists

henrikubbe4438
Advocate
Advocate

Hi all.

I have been looking for some time to get iLogic to create me some parameters, but only if the don't exists already. I had a version working with try-catch, but it fel ... wrong to use that method. All the searches i made came close, but didn't give me any cigarrs. I came up with this, maybe someone has a similar problem and can put it to some use.

I'm no programmer so any bad methods is mea culpa. It works through all my test trials, though and I'm happy with it so far. Any addition input is of course welcome.

 

Come to think of it - maybe should add a test for proper file type...

 

'Creates parameters from a list
'It works with most ordinary units

'set up variables
Dim myArray As New ArrayList 
oMyParam = ThisDoc.Document.ComponentDefinition.Parameters
Parameter.Quiet = True

'Parameters list, change the habitants, but keep the system.
myArray.Add("p1")
myArray.Add("10")
myArray.Add("mm") 

myArray.Add("p2")
myArray.Add("20")
myArray.Add("deg")

myArray.Add("p3")
myArray.Add("30")
myArray.Add("in")

myArray.Add("p4")
myArray.Add("40")
myArray.Add("ul")

myArray.Add("p5")
myArray.Add("50")
myArray.Add("kg")

For j = 0 To myArray.Count - 1 Step 3
	If Parameter.Value(myArray(j)) Is Nothing Then	
		oMyParam.UserParameters.AddByExpression(myArray(j), myArray(j + 1), myArray(j + 2))
		Parameter.Param(myArray(j)).ExposedAsProperty=True 
	End If 
Next 

 

920 Views
5 Replies
Replies (5)
Message 2 of 6

JelteDeJong
Mentor
Mentor

just a thought

Public Class ThisRule
	Private parameters As parameters
	
	Public Sub Main()
		Dim doc As Document = thisdoc.document
		If (doc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject And
				doc.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then
			MsgBox("This is not a Part or Assembly file. Can't add parameters")
			Return
		End If
		
		parameters = ThisDoc.Document.ComponentDefinition.Parameters
		Parameter.Quiet = True

		addParameter("p1","10","mm")
		addParameter("p2","20","deg")
		addParameter("p3","30","in")
		addParameter("p4","40" ,"ul" )
		addParameter("p5","50","kg")
	End Sub

	Private Sub addParameter(name As String, Expression As String, UnitsSpecifier As String)	
		parameters.UserParameters.AddByExpression(name, Expression, UnitsSpecifier)
		Parameter.Param(name).ExposedAsProperty = True 
	End Sub
End Class

 

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.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 6

henrikubbe4438
Advocate
Advocate

Neat solution, I could'nt get the public variable to work, so I ditched the sub variant I was working on, thank you for clearing that up for me. 

Only trouble is: Run it twice and you get double amount of parameters with "_1" at the end. Gotta have the IF-statement to check if parameter already exists.

 

Public Class ThisRule
	Private parameters As parameters
	
	Public Sub Main()
		Dim doc As Document = thisdoc.document
		If (doc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject And
				doc.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then
			MsgBox("This is not a Part or Assembly file. Can't add parameters")
			Return
		End If
		
		parameters = ThisDoc.Document.ComponentDefinition.Parameters
		Parameter.Quiet = True

		addParameter("p1","10","mm")
		addParameter("p2","20","deg")
		addParameter("p3","30","in")
		addParameter("p4","40" ,"ul" )
		addParameter("p5","50","kg")
	End Sub

	Private Sub addParameter(name As String, Expression As String, UnitsSpecifier As String)
		If parameter(name) Is Nothing Then
			parameters.UserParameters.AddByExpression(name, Expression, UnitsSpecifier)
			Parameter.Param(name).ExposedAsProperty = True 
		End If
	End Sub
End Class

 

0 Likes
Message 4 of 6

JhoelForshav
Mentor
Mentor

Hi @henrikubbe4438 

I think you're looking for something like this?

 

Public Class ThisRule
	Private parameters As parameters
	
	Public Sub Main()
		Dim doc As Document = thisdoc.document
		If (doc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject And
				doc.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then
			MsgBox("This is not a Part or Assembly file. Can't add parameters")
			Return
		End If
		
		parameters = ThisDoc.Document.ComponentDefinition.Parameters
		Parameter.Quiet = True

		addParameter("p1","10","mm")
		addParameter("p2","20","deg")
		addParameter("p3","30","in")
		addParameter("p4","40" ,"ul" )
		addParameter("p5","50","kg")
	End Sub

	Private Sub addParameter(name As String, Expression As String, UnitsSpecifier As String)
		If parameters.OfType(Of Inventor.Parameter)().Where(Function(x) x.Name = name).Count = 0
			parameters.UserParameters.AddByExpression(name, Expression, UnitsSpecifier)
			Parameter.Param(name).ExposedAsProperty = True 
		End If
	End Sub
End Class
Message 5 of 6

henrikubbe4438
Advocate
Advocate

Hi there.

 

What does 

If parameters.OfType(Of Inventor.Parameter)().Where(Function(x) x.Name = name).Count = 0

do that 

If parameter(name) Is Nothing Then

does not? 

I guess the code gets a bit more stable for different situations?

 

0 Likes
Message 6 of 6

JhoelForshav
Mentor
Mentor

Hi @henrikubbe4438 

Sorry, I tried the code with

If parameter(name) Is Nothing Then

and got an error so i thought you were asking for some other way to check if the parameter exists. But now when I test your line it works fine so just ignore my reply. Your line looks cleaner 🙂

It was just a misunderstanding on my part