How to import code from another file?

How to import code from another file?

Leonardo_Czuy
Advocate Advocate
714 Views
8 Replies
Message 1 of 9

How to import code from another file?

Leonardo_Czuy
Advocate
Advocate

Hello,

 

I have a small issue where I occasionally copy code from other files and paste it into the file I am currently working on. How can I create a file with general functions that I can import into the code I am currently working on?

0 Likes
Accepted solutions (3)
715 Views
8 Replies
Replies (8)
Message 2 of 9

Frederick_Law
Mentor
Mentor
0 Likes
Message 3 of 9

Leonardo_Czuy
Advocate
Advocate

Yes, in iLogic.

For example, in JavaScript, when working with two files, I can import a function from one file into another using the following syntax:

 

const { functionName } = require("../../filename.Js")

0 Likes
Message 4 of 9

Frederick_Law
Mentor
Mentor
Accepted solution

Run Rules

To run a rule manually, right-click the rule in the iLogic Browser, and select Run Rule.

To ensure that the parameters in the rule are synchronized with the model, right-click and select Regenerate Rule before running the rule. To synchronize the parameters in all rules with your model, select Regenerate All Rules.

To run another rule explicitly within a rule, use the iLogicVb.RunRule function.

 

https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-C5ADE109-10E9-41A0-BC4A-BE73AA68A1C7

0 Likes
Message 5 of 9

WCrihfield
Mentor
Mentor
Accepted solution

Here is another online help link that likely mentions the functionality you are looking for.  It sounds like you may want the functionality of the AddVbRule or AddVbFile mentioned there.  I use this sometimes myself.  Created external iLogic rules, turned on their 'Straight VB Code' option, laid out the code more strictly, like what is required in Visual Studio, then saved them.  Then we can reference the tools in those external rules that are formatted special that way using the AddVbFile, followed by the file name of the external rule, in the header of the current rule to use its resources.

https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-32B66838-22E4-4A0A-B5BB-862350C76B36 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 9

WCrihfield
Mentor
Mentor
Accepted solution

The idea of running another rule from your current rule is also a really useful.  But where that idea really gets interesting, is when you add in the added functionality of being able to 'send' data to that other rule, then optionally also get data back from that other rule.  There are a couple ways of doing that also, if needed.  One way is by using the iLogic SharedVariable objects.  Another is with the NameValueMap as arguments when calling the other rule to run, then using the RuleArguments in the other rule, as a means of 'receiving' the data from the rule that sent it.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 9

Leonardo_Czuy
Advocate
Advocate

@WCrihfield, automatically imports statements, but why can't access  ThisApplication.ActiveDocument.DocumentType

' Modular Functions File

' <IsStraightVb>True</IsStraightVb>
Module AuxiliarFunctions

    Public Function verifyDocument(oDocumentType As Object)
        If ThisApplication.ActiveDocument.DocumentType <> oDocumentType Then
			MessageBox.Show("Este não é um documento de montagem!", "iLogic", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return True
		End If

        Return False
    End Function

End Module

' Other File
Public Class ThisRule
    Public Sub Main()
        If verifyDocument(kAssemblyDocumentObject) Then Exit Sub
    End Sub
End Class

 

0 Likes
Message 8 of 9

WCrihfield
Mentor
Mentor

There are several common objects/variables that are defined for us behind the scenes within every iLogic rule, that we never see how their values are set.  These are known as 'Rule Objects', and you can see a list of most of them at the following link.

https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=4ccb78b0-c35c-4d38-943a-854117da6ced 

In order to use those objects/variables within our externally sources files, we must re-declare those variables locally in our external resources, then 'pass' these objects from the regular iLogic rule, to the external resource, as a way of setting the values of those variables in our external resources.  There may be a couple we can get the values of in other ways, but that is the primary way.  So, if your external resource is a Class, you might use its 'New' Sub routine to 'set' the values of those variables.  Or a 'Initialize' routine for setting their values.  Or just have public properties where you can set their values directly from the regular rule.

 

Edit:  The 'ThisApplication' variable is not listed on that linked page, but is a similar type of object reference, and is often handled the same way.  Or, some folks just declare their own variable to use as Inventor.Application, then use something like GetObject() or CreateObject() to set its value.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 9

Frederick_Law
Mentor
Mentor

The code should be:

 

If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then

 

https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-9342126D-5B1B-4A54-B6FF-D83E43A7C508

 

 

0 Likes