Rule to create parameters in Parts and Drawings

Rule to create parameters in Parts and Drawings

gabriele.tittonel
Contributor Contributor
641 Views
4 Replies
Message 1 of 5

Rule to create parameters in Parts and Drawings

gabriele.tittonel
Contributor
Contributor

Hello everyone!

 

I've written a rule that creates parameters both in parts and drawings, combining pieces of code from here and there. It seems working, and that's a good thing 😊 but... I don't quite understand how...

 

For example, if I declare the "oParam" variable (Dim oParam As UserParameter), the rule doesn't work:

gabrieletittonel_0-1685004857036.png

Why? Is it ok not to declare a variable? What happens actually?

 

Another doubt: I've used two methods, AddByValue and AddByExpression to set the parameter's value depending on their type. Is it correct?

 

I also don't fully understand the "Try" code (just copied somewhere):

Parameter(Name) = Parameter(Name)

It works, but it's not something I'd figure out by myself.

 

Here the complete code:

Sub Main
	
	ParameterCreate("txtPara", "Hello World!")
	ParameterCreate("mmPara", 45, "mm")
	ParameterCreate("booPara", True)
	ParameterCreate("emptyPara", "")
	ParameterCreate("ulPara", 15)

End Sub

Function ParameterCreate(Name As String, Value As Object, Optional Unit As String = "ul")

'	This rule works on every kind of Inventor files: Part, Assembly and Drawing
'	If a parameter doens't exist, then it will be created.
'	If a parameter exists, then it won't be changed.

	Try 	' Check if parameter exists.
		
		Parameter(Name) = Parameter(Name)
		
	Catch 	' Parameter not found, so create it.
		
		If TypeName(Value) = "Boolean" 	Then Unit = "BOOLEAN"
		If TypeName(Value) = "String" 	Then Unit = "TEXT" 
			
		Dim oDoc As Document = ThisDoc.Document
'		Dim oParam As UserParameter
		
		If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
			oParam = oDoc.Parameters.UserParameters	' Drawing file.
		Else
			oParam = oDoc.ComponentDefinition.Parameters.UserParameters
		End If

		Select Case Unit 
			Case "BOOLEAN", "TEXT" 	: oParam.AddByValue(Name, Value, Unit)
			Case Else 				: oParam.AddByExpression(Name, Value, Unit)
		End Select
		
	End Try

End Function

 

0 Likes
Accepted solutions (1)
642 Views
4 Replies
Replies (4)
Message 2 of 5

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi @gabriele.tittonel . I rewrote your rule a bit, I hope it's what you wanted.

You are getting the error (Dim oParam As UserParameter) because you are trying to set UserParameters to UserParameter.

 

 

Sub Main
	Dim oDoc As Document = ThisApplication.ActiveDocument
	ParameterCreate(oDoc, "txtPara", "Hello World!")
	ParameterCreate(oDoc, "mmPara", 45, "mm")
	ParameterCreate(oDoc, "booPara", True)
	ParameterCreate(oDoc, "emptyPara", "")
	ParameterCreate(oDoc, "ulPara", 15)
End Sub

Function ParameterCreate(ByVal oDoc As Document, ByVal Name As String, ByVal Value As Object, ByVal Optional Unit As String = "ul")

	Dim oParams As UserParameters
	If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
		oParams = oDoc.Parameters.UserParameters
	Else
		oParams = oDoc.ComponentDefinition.Parameters.UserParameters
	End If
	If oParams.Count <> 0 Then
		For i As Integer = 1 To oParams.Count
			If oParams.Item(i).Name = Name Then Exit Function
		Next
	End If
	
	If TypeName(Value) = "Boolean" 	Then Unit = "BOOLEAN"
	If TypeName(Value) = "String" 	Then Unit = "TEXT"	
	Select Case Unit 
	Case "BOOLEAN", "TEXT" 	: oParams.AddByValue(Name, Value, Unit)
	Case Else 				: oParams.AddByExpression(Name, Value, Unit)
	End Select
	
End Function

 

 

 

The difference in value(2) and expression(1):

Param(mainAssemb).png

In the example, the Try - Catch operator was used because an error is possible in the Try section. In the case of an error, the code execution goes to Carch.

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 3 of 5

gabriele.tittonel
Contributor
Contributor

Hi @Andrii_Humeniuk , thanks for your feedback!

I like the For/Next approach instead of the Try/Catch. I didn't think about it!

Some questions:

  • oDoc declaration:
    • Can be put inside the function? (the less I write... 😃)
    • ThisApplication.ActiveDocument vs ThisDoc.Document?
  • ByVal: I've read the definition, but I still don't quite understand when I should use it.

PS: UserParameterS... 🙈🙈🙈 I'm not going to tell you much time I've spent figuring out what the problem was... 😂

0 Likes
Message 4 of 5

Frederick_Law
Mentor
Mentor

Try/Catch try to see if the Parameter exist.  If you try to access non exist parameter, program will crash.

For/Next might take longer to run if there are lots of Parameters.  ie the one you want is at the end of the list or not exist.

It may not be faster because IV is doing the same in background.

 

VB allow DIM everywhere but don't do that.  Keep them at the top.

Because a new DIM will wipe out old DIM if they are the same name.  Hell for debugging.

 

ByVal - copy of the variable, changes won't change original

ByRef - use the variable, changes apply to original

Message 5 of 5

Andrii_Humeniuk
Advisor
Advisor
  • oDoc declaration:
    • Your code is your rules, you can do anything, but from the point of view of programming culture, for better understanding of your code by other people, it is better to put "ThisApplication " in Sub.
    • ThisApplication.ActiveDocument - the code calls the active document, ThisDoc.Document - the code calls the document in which the code is located.
  • VBA(iLogic) automatically determines ByVal or ByRef you do not have to define the export type of the variable.

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature