iLogic: Reusing functions

iLogic: Reusing functions

bhendriksMQVTY
Participant Participant
855 Views
7 Replies
Message 1 of 8

iLogic: Reusing functions

bhendriksMQVTY
Participant
Participant

Hello Everybody,

 

I have a function that I reuse in several of my iLogic rules. To make my code easier to read I'd like to to be able to call it as a custom function without defining it in the rule. Is this possible within iLogic? I don't want to save it as a custom snippet, because it pastes the whole code again, making my code no shorter.

0 Likes
Accepted solutions (1)
856 Views
7 Replies
Replies (7)
Message 2 of 8

FINET_Laurent
Advisor
Advisor

Evening,

 

I don't have a clue about how to do this.

What you can do instead (this is how i procced) is making blocks of code and add those snippets which allows to show/hide what are inside.

 

'[ 

'Huge block of code you wish to see disapear in  one click

']

Regards,

 

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 3 of 8

blandb
Mentor
Mentor

Can you save it as an external rule, and just run that rule when needed in your code?

Autodesk Certified Professional
0 Likes
Message 4 of 8

bhendriksMQVTY
Participant
Participant

My function is already in the shape of

Private Function FunctionName(InputVariable) As String
     Do stuff
End Function

 Sadly the function "unfolds" every time I close the code, so it doesn't stay hidden.

Apart from that I'd like to be able to just reuse code by calling a function without having to declare it every time.

0 Likes
Message 5 of 8

FINET_Laurent
Advisor
Advisor

You can write the function as you did once at the bottom of the code and call the function through your code when needed.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 6 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Yes, there is a solution for this, but it can be a little complicated to set up.

I usually just use that same strategy that @blandb  mentioned, but there is another option made just for this situation too.

I will attempt to describe the steps needed to set this up:

  • Place your code for that function in an external rule
  • While your still in the rule editor dialog, and that rule is open, go to the Options tab (at the top of the dialog), and check the checkbox next to the option "Straight VB code".
  • You will need to change "Private Function" to "Public Function", at the start of your code
  • Now on the line just above your Public Function, type "Class ThisRule" (without the quotes)
  • Then on the next line after the last line of your function block, type "End Class" (without the quotes)
  • Now save that rule, but copy the name of that Function to the clipboard or temporary new Text document, because we will need it when we call it from another rule.
  • Now in order to have that function available within another rule:
    • At the top of the other rule (or in the Header of that rule, because that's where this line of code will go once you get done typing it), type "AddVbFile" (without the quotes), followed by a single space, then the full path and file name of that external rule you defined the function in (including the file extension).  The path and file name should all be within quotes, as you would with any string.

Here is the simplest example I can think of:

Copy and paste this code into an empty external iLogic rule

Class ThisRule
	Public Function GetFullFileName(ByRef oDoc As Inventor.Document) As String
		If oDoc IsNot Nothing Then
			GetFullFileName = oDoc.FullFileName
		End If
	End Function
End Class

Then check the checkbox next to that setting within the Options tab, called "Straight VB code".

Then Save the rule (without running it).

Now get the full file name and file extension of that rule you just created, and have it ready to insert into the top of the next rule.

Now create a new empty rule (local or external, it doesn't matter), and paste the following code into it:

AddVbFile "S:\Engineering\SHARED\External iLogic\REF FUNCTIONS\GetFullFileName.txt"

MsgBox(GetFullFileName(ThisDoc.Document))

And replace the file path and name string with the one for that last rule you created.

(The active document must be saved before it will retrieve a FullFileName from it, otherwise it will just return a blank string.)

Now run this last rule.

It should have just shown you a message that showed the full file name of the active document.

Notice that you didn't have to specify the referenced function in any special way.  It just functions exactly the same as if it were defined within the same rule.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

If you have time, please... Vote For My IDEAS 💡and Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 8

bhendriksMQVTY
Participant
Participant

Wonderful, that's what I was looking for. Thank you very much!

0 Likes
Message 8 of 8

WCrihfield
Mentor
Mentor

Good to hear.  Always glad to have helped someone out, when I can. 🙂

Another note, on this subject...

If your external rules are all set up correctly within the [Tools tab > Options panel > iLogic Configuration > External Rule Directories], so that they all show up on your iLogic browser > External Rules tab, then you don't really have to specify the whole path or the file extension of the external rule (as long as the file extension is either ".iLogicVb", or ".txt", or ".vb").  So, after the AddVbFile, you could just have the name of the external rule (in quotes), without its path or file extension, and it will still work.  But when you include the full path and extension, your rule could probably be stored just about anywhere.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes