Message 1 of 5
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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:
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
Solved! Go to Solution.