Define Custom Function for Use In Parameters [fx]

Define Custom Function for Use In Parameters [fx]

rcolon9E4ZX
Advocate Advocate
2,621 Views
8 Replies
Message 1 of 9

Define Custom Function for Use In Parameters [fx]

rcolon9E4ZX
Advocate
Advocate

Hello,

 

I am modeling a cable track, see image below. I used to add rotational joints and angle ranges for each connected module, but then I can hardly drag the thing half the time. I want to position the top end of the track by adjusting a parameter and all the angles solve. Can I define my own function for use in Parameters? Possibly with iLogic? I have 40 angles, and I want them to be either 0deg or a constant around 20deg. I am going to use a negative power function and the sign() function to define the subset that gets the nonzero angle. If I cannot use a custom function, then I have to put all the logic for the function in all 40 angles, where it would be much nicer to use MyFunction(x)*MyAngle for each. Please let me know if there is a way to do this or something similar.

 

rcolon9E4ZX_0-1625672315044.png

 

Thank you.

Rafael

 

WIN10 INV PRO 2019 64-Bit

Build 330, Release 2019.4.3

0 Likes
2,622 Views
8 Replies
Replies (8)
Message 2 of 9

SharkDesign
Mentor
Mentor
Message 3 of 9

rcolon9E4ZX
Advocate
Advocate

Thank you @SharkDesign for the quick response. I looked through the link you gave, and it is a much quicker, simpler solution. I was halfway through my approach so I went ahead with it. I also like how my approach keeps everything contained in a single file without links.

 

Here is my list of parameters. You can see how the power function is evident in each row. A_CurveStart is a model parameter representing a plane offset for the sole purpose of overriding in a Positional Rep.

ParametersParameters

Fully OpenFully Open

Fully ClosedFully Closed

 

I would still like to represent the logic as a user defined function as it will make it quicker to make adjustments. Any help with this would be greatly appreciated.

 

Thanks in advance.

Rafael

0 Likes
Message 4 of 9

SharkDesign
Mentor
Mentor
Tried a few times to do drag chains and decided it's not worth it haha!
  Inventor Certified Professional
0 Likes
Message 5 of 9

A.Acheson
Mentor
Mentor

Both methods will work with ilogic and you can always upgrade to the assembly file if your doing a lot of sets. The BOM is always a nice feature for adding up the links. Can you supply a part file with parameters named etc or at least start off the basic equations you want to use in math form. You may even get away with a function in the parameter dialogue and a form if you want to keep it simple but getting the formats right in there can be tedious at best. 

Are you familiar with the ilogic editor?

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 6 of 9

rcolon9E4ZX
Advocate
Advocate

@A.Acheson ,

 

Thank you for the reply. First off, I made this cable track as an assembly with 42 links.

 

The expression I want for each angle is <sign(-( x - Root1 ) * ( x - Root2 ))>*MyAngle where x is an enumeration for each angle and Root1 and Root2 are custom parameters. The purpose of this <coefficient> is to specify the subset of angle constraints that get a nonzero angle, i.e. where the bend takes place. For example, I provided a power function graph with Root1=2 and Root2=5, and performing the sign() would result in a nonzero output of 1 for x=3 and x=4. Therefore Angle03 and Angle04 evaluate an angle of MyAngle and all others will be zero.

Power Function.png

 

I would like to specify a function called Subset() and use Subset(x)*MyAngle for each angle, where x is the enumeration. I would need only to adjust the function definition instead of all 42 expressions if I wanted to add more logic. Is it possible to define the function in iLogic and call it from the Parameters expressions?

 

Thank you.

Rafael

0 Likes
Message 7 of 9

bradeneuropeArthur
Mentor
Mentor

In the programming help you find this:

User-Defined Functions in Parameter Expressions

User-Defined Functions in Parameter Expressions

Introduction

In Autodesk Inventor's Parameters command, you can specify an expression for a parameter. Expressions can be a specific value or an equation that references other parameters and uses functions. Earlier releases of Inventor limited the use of functions in expressions to a small set of built-in functions such as sine, cosine, minimum and maximum. Now, you can write your own functions using VBA in parametric expressions. These user-defined functions can be used in the expressions just like any of the built-in functions.

While you can make your functions powerful using all the capabilities of VBA, you must be aware of the requirements and rules specific to these functions, which are described in the rest of this section.

 

Writing User-Defined Functions

You must write the functions in the code module called "Functions." This code module is automatically created by Inventor for all document projects. It will initially be empty. Any parameters in the document will be able to use functions contained within this module. The parameters in a document cannot, however, use functions created in a documented project associated with another document. Because of the parameter's dependency on the function used in an expression, the function must exist in the document project in the document containing the parameter. Also, the function must be a public function. You can use all capabilities of VBA and the VBA development environment while writing these functions.

The user-defined function must take at least one argument, but it can have any number of additional arguments. All arguments are treated as input and their data type as Double. The return type of the function must be Double.

The Parameter command converts the return value of the user-defined function to a unit-less number.

The function below demonstrates a correctly defined function.

Public Function Sample(Arg1 As Double, Arg2 As Double) As Double
    'Use the input arguments to compute a value.
    If Arg1 < 5 Then
        Sample = Arg2 
    ElseIf Arg1 < 10 Then
        Sample = Arg2 * 2
    Else
        Sample = Arg2 * 3
    End If
End Function

This function takes two arguments as input, uses these values to compute a new value, and returns it as the result.

Handling Units in the Function

One thing to pay particular attention to when writing VBA functions that will be used in parameter equations is the units. All of the Inventor API deals exclusively with internal database units. For example, if you use the API to obtain the length of an edge, the result is returned in centimeters. Centimeters are the internal database units for length. Internally all lengths are computed and saved as centimeters. Using the Document Settings command, the user can choose which units they want to use for that document. For example, let's say that inches are chosen as the length unit. When the user enters a length in a dialog, Autodesk Inventor will assume it is inches and when displaying results it will be in inches. In both cases a conversion is being done to and from centimeters during the reading of the input and the display of the output. The advantage of this behavior is that the units the user chooses for a document can be changed at any time without any impact on the data in the document. It also allows the mixture of parts using different units within assemblies. For a more complete discussion of units, see Units of Measure.

The VBA functions for parameter equations need to take this behavior into consideration because all values passed into the function will be in database units. This may make writing functions a little more confusing until you understand the way units are used in Autodesk Inventor, but soon you will discover the advantage of them working correctly regardless of the document unit settings. Let's look at the previous sample to better understand this behavior and how to correctly write a function.

This function has two input arguments. If we look at the use of this function in the Parameters dialog, you can see that two parameters are passed in as input values for these arguments. These two parameters happen to define length units and in this case have the values 1 in and 2 in. When Inventor calls the VBA function these values will be converted to length database units (centimeters). So the values the function receives will be 2.54 and 5.08. If one of the input arguments happened to be the parameter d3, the value received by the function would be 0.03490658, which is 2 deg converted to radians. Radians are the internal database unit for angles.

Understanding that these values are input as centimeters is important in this particular function because it is comparing these values in some logic statements. The If statement checks to see if Arg1 is less than 5. It would be a common mistake to assume you are checking to see if Arg1 is less than 5 inches. Instead it is checking to see if Arg1 is less than 5 centimeters. If the comparison needed to be for 5 inches the If statement could use 5 * 2.54 instead. The important thing to remember is that within the VBA function, you should always work in the world of Autodesk Inventor's database units: centimeters and radians.

Debugging

Debugging user-defined functions can easily be performed by placing break points within the function. If you set a break point, performing an operation within Autodesk Inventor that causes the function to be called will place you in debugging mode within the VBA environment.

In addition to setting break points, if for some reason the function should fail, you will be allowed to enter debug mode at that point and correct the function and continue processing.

Limitations

One limitation that has been deliberately imposed on user-defined functions in parameter expressions is that they cannot use any of the Autodesk Inventor API within the function. For example, you cannot have a function that will directly edit another parameter or change the suppressed state of a feature. Any calls to the Autodesk Inventor API will fail within the context of the function.

The reason for this limitation is that the function is called during the compute process of Autodesk Inventor. Calls to the API while Autodesk Inventor is in this state can cause problems and the results can be unreliable. For example, let's say you have a part made up from 10 features. When this part is entirely recomputed each feature is computed in sequence. Based on dependencies, Autodesk Inventor determines when to compute the value of a parameter. Let's say in this case the parameter that uses the user-defined function is computed after the fifth feature is computed. The model is only partially computed and is not in an editable state. Even query operations of the model will be unreliable in this state since they would only provide a snapshot at the point of what the model looks like, not the final result. And since you don't know when the parameter will be computed, you don't know what the point of that snapshot would be.

Although you cannot use the Autodesk Inventor API within the function, there are no other limitations. For example, the function could perform queries and obtain values from an Access database, or look up information from Excel.

 

Using User-Defined Functions

The VBA function is used the same way as the built-in functions are used in Autodesk Inventor. One requirement that is easily overlooked is that the separator between arguments is a semi-colon, not a comma.

The figure below illustrates using the user-defined function in the Parameters dialog.

 

 

 

 

You can also use the user-defined functions with the Edit Dimensions command. The following figure shows the Edit Dimension dialog box using a function:

 

 

 

 

While using the function in Parameters and Edit Dimensions dialog boxes, you need not enter "VBA:" before the function. Autodesk Inventor automatically prefixes the function with "VBA:".

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

Message 8 of 9

rcolon9E4ZX
Advocate
Advocate

@bradeneuropeArthur ,

 

Thank you for the reply. I made the following function in the Function module of my assembly DocumentProject.

Public Function Subset(iEnum As Double, dRoot1 As Double, dRoot2 As Double) As Double
'Lengths are in centimeters and angles are in radians. 2*pi radians = 180 degrees

On Error GoTo InputError
Dim dValue As Double
dValue = -1 * (iEnum - dRoot1) * (iEnum - dRoot2)

If dValue > 0 Then
    Subset = 1
Else
    Subset = 0
End If

Exit Function
InputError:
    Subset = 0
End Function

 

Now the call in the expression looks like this: CurveAngle * VBA:Subset(x;Root1;Root2). I was hoping to increase the complexity of the logic contained in the function while keeping the call the same. If I use additional parameters in the function, they have to pass through as arguments. I cannot get them from ThisDocument.ComponentDefinition.Parameters within the Function module. This means when I add an input, I have to change the call for all 42 expressions. This is an unfortunate limitation of this method. I may as well leave the whole function definition in each expression for now.

 

Thanks for everyone's help.

Rafael

0 Likes
Message 9 of 9

rcolon9E4ZX
Advocate
Advocate

Also disregard the inequality in my second line comment. Pi radians = 180 degrees.

0 Likes