Calling Functions from VBA or External rules in local iLogic Rule

Calling Functions from VBA or External rules in local iLogic Rule

s.riebl
Participant Participant
2,948 Views
12 Replies
Message 1 of 13

Calling Functions from VBA or External rules in local iLogic Rule

s.riebl
Participant
Participant

Hello guys,

Im posting on behalf of my company here.
I am trying to create a library of Functions to add a part, delete a part and create iMates for added parts.
Right now i have a hard time to figure out the best way to do this.
External rules seem to be the ideal way, but i read that it has to be a straight vb file to be added in the local iLogic rule editor.
Would it be better if i add them to VBA macros and call the functions locally?

I also could not find a proper code to call functions which has ThisApplication  and ThisDoc objects properly from external rules or VBA Macros.

Any help would be appreciated.
Please look into into it if someone finds free time.

Best,
Vamsi


These are the functions that i am trying to store externally and call them locally in required assemblies.

Sub Main()
	
End Sub

Function Add(ComponentName As String)
	oACD = ThisDoc.Document.ComponentDefinition
	oPath = ThisDoc.Path & "\"
	oFile = ComponentName
	oTG = ThisApplication.TransientGeometry
	oM = oTG.CreateMatrixoM.SetTranslation(oTG.CreateVector(0, 0, 0))
	oOcc = oACD.Occurances.Add(oPath & oFile, oM)
End Function

Function Check(ComponentName As String)
	oACD = ThisDoc.Document.ComponentDefinition
	For i = 1 To 20
		Try
			entity = oACD.Occurances.ItemByName(ComponentName & ":" & i)
			entity.Delete
		Catch
		End Try
	Next
End Function

Function DoiMates(Component1 As String, Component2 As String, MateName As String)
	oACD = ThisDoc.Document.ComponentDefinition
	Comp1 = oACD.Occurances.ItemByName(Component1)
	Comp2 = oACD.Occurances.ItemByName(Component2)
	i = 1
	For Each iMateDefinition In Comp1.iMateDefinitions
		If iMateDefinition.Name = MateName Then
			iMate1 = Comp1.iMateDefinitions.Item(i)
			Exit For
		End If
	i = i + 1
	Next
	i = 1
	For Each iMateDefinition In Comp2.iMateDefinitions
		If iMateDefinition.Name = MateName Then
			iMate1 = Comp2.iMateDefinitions.Item(i)
			Exit For
		End If
	i = i + 1
	Next
	oiMateResults = oACD.iMateResults.AddByTwoiMates(iMate1, iMate2)
End Function



 

0 Likes
Accepted solutions (1)
2,949 Views
12 Replies
Replies (12)
Message 2 of 13

s.riebl
Participant
Participant

@JhoelForshav

Any help here please?

0 Likes
Message 3 of 13

WCrihfield
Mentor
Mentor

Both ways have their pros & cons.  Going the VBA route, would be more intuitive for someone who is fairly new to programming for Inventor, because it's easier to create and call external Subs, Functions, Classes, and easier to use external references without having to add special code to retrieve the reference at the top of every Main code.

 

I have used external Subs & Functions within iLogic too, and when going that route, there are several special things you have to do to make it work.

  • The external rule must have the "Straight VB Code" option turned on (within the iLogic Rule Editor's per/rule options)
    • The rule using that externally referenced rule does not have to have the "Straight VB Code" option turned on.
  • The code within that external rule must be enclosed within the "Class ThisRule" & "End Class".
    • The rule using that externally referenced rule does not have to be enclosed within the "Class ThisRule" & "End Class"
  • The rule referencing that external rule must include a line of code at the top which works similar to AddReference, which looks something like the following:
    • AddVbFile "full path & file name of the external rule file to reference"

However, you can also run other local rules, external rules, or VBA macros from within an ilogic rule too, and you can pass data back and forth between those other rules/macros and the current rule.

 

I personally very much prefer writing code using vb.net & iLogic, rather than VBA, so I normally choose to go the iLogic route when possible.  The iLogic code (which uses vb.net) can often be used with, very minimal changes, within Inventor Add-in's too.  Both coding formats descended from visual basic, so there are a lot of similarities.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 13

JhoelForshav
Mentor
Mentor
Accepted solution

You can do like this:

 

Extract and save attached .iLogicVB-file anywhere.

If you want to call functions from it, add it at the top of your iLogic rule:

AddVbFile "C:\Yourpath\SpecialFunctions.iLogicVB"

Then the first line of your rule should be this, to set ThisDoc and ThisApplication in the SpecialFunctions Module:

SpecialFunctions.GetAppAndDoc(ThisDoc, ThisApplication)

Then you're good to go. Just call the functions with SpecialFunctions.FunctionName

 

Example:

SpecialFunctions.PNG

Message 5 of 13

WCrihfield
Mentor
Mentor

@s.riebl

Yes. You can include several Subs, Functions, Classes, etc within one external rule file if you like, as long as it's done right.  I guess I didn't mention that.  Another positive point for the vb.net & iLogic side. 😊

@JhoelForshav  Good point Joel.  Also, I noticed you're using "Public Module" instead of just "Class ThisRule".  I am intrigued.  I would like to know more about this technique, and what difference it makes. 🤔

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 13

s.riebl
Participant
Participant

@WCrihfield 
Thanks a lot for the information.
I definitely have a lot to explore, as i am new to programming outside iLogic

0 Likes
Message 7 of 13

JhoelForshav
Mentor
Mentor

Hi @WCrihfield 

If I use a moule (Which is basically just a collection of subs/functions) I can use the functions without creating an instance of it in the code first - as I'd have to do with a class.

Changing it to a class would require a row in the iLogic rule such as:

Dim oSpecialFunctions As New SpecialFunctions

And then I'd have to use oSpecialFunctions to call anything in the iLogicVB-file.

It's just an unnessecary extra step and also It would have to keep this object (oSpecialFunctions) in memory.

 

Both ways works though 🙂

Message 8 of 13

s.riebl
Participant
Participant

Hi Jhoel!

Thats a lot for the solution.
But i still managed to get an error which says:
Member not found.

Error2.JPG


Error.JPG


 This is how i executed the commands from you.
Sorry for german dialog boxes though 😅

Rule.JPG
Any insights about why i still get this error?


0 Likes
Message 9 of 13

JhoelForshav
Mentor
Mentor

Hi @s.riebl 

I believe it's just the Add-function that fails.

Is the assembly you're running the rule in saved?

Does the file "End_Unten_Rivet.iam" exist in the same folder as the assembly you're running the rule in?

0 Likes
Message 10 of 13

s.riebl
Participant
Participant

Hi Jhoel,

Yes i tried with other function too,
they didnt seem to be working.
I made a quick restart of Inventor.

Now it seems to work fine. 😃

Thanks a lot 🙂

Message 11 of 13

WCrihfield
Mentor
Mentor

@JhoelForshav 

FYI

When I use "Class ThisRule" and "End Class" around the code of my external iLogic rule that I want to reference within another rule, I'm not having to create an instance of anything before I use the Sub or Function from that reference within another rule.  All I have to do is have that AddVBFile line at the top (in the header), then just using that Sub or Fuction is automatically recognized and works.  And I don't have to call anything by ModuleName.SubName(), just SubName().  I believe this is because ThisRule is always implied within all iLogic rules, without having to specify or call it out.  This is why you can use "iLogicVb" and "InventorVb" to access the Automation object (and others) without using like this: ThisRule.iLogicVb or ThisRule.InventorVb.

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 12 of 13

prudhvi_galiFSSED
Participant
Participant

Hey there, 

I followed the steps mentioned above. But it doesn't work for me.

Here's what I want to achieve. Could you please help me with it?

Here's my main rule:

Dim partName As String = "redChain1"
Dim searchDescription As String = "Red Chain - " & 600 * nSections
Dim searchFolders As String() = {"$/Designs/User/Standard Red Chain Conveyor/Parts/Red Chains"}
Dim result As String = searchDownloadAndReplace(partName, searchDescription, searchFolders)
MessageBox.Show(result, "Title")

'nSections' is a parameter that I will be receiving from the user input form on the assembly. 

And here's the public function I want to define as an external rule:

Function searchDownloadAndReplace(partName As String, searchDescription As String, searchFolders As String()) As String
    ' Build one to many name/value pairs of Property/Value as search criteria
    Dim mSearchParams As New System.Collections.Generic.Dictionary(Of String, String) ' Add UDP.DisplayName, Value Pairs
    mSearchParams.Add("Description", searchDescription) ' Applies to file 001002.ipt
    ' ...add as many as required to enable a unique search result

    ' Returns full file name in local working folder (download enforces override, if local file exists)
    Dim mVaultFile As String = iLogicVault.GetFileBySearchCriteria(mSearchParams, True, False, searchFolders)
    
    If mVaultFile Is Nothing Then
        Logger.Error("Vault file search: File not found - Please double check that file can be found with search criteria applied.")
        Return "File not found"
    Else
        Logger.Info("File " & mVaultFile & " found by search and downloaded to local workspace.")
        Component.Replace(partName, mVaultFile, True)
        Return mVaultFile
    End If
End Function

 

0 Likes
Message 13 of 13

prudhvi_galiFSSED
Participant
Participant

Hey there, 

I followed the steps mentioned above. But it doesn't work for me.

Here's what I want to achieve. Could you please help me with it?

Here's my main rule:

 

 

Dim partName As String = "redChain1"
Dim searchDescription As String = "Red Chain - " & 600 * nSections
Dim searchFolders As String() = {"$/Designs/User/Standard Red Chain Conveyor/Parts/Red Chains"}
Dim result As String = searchDownloadAndReplace(partName, searchDescription, searchFolders)
MessageBox.Show(result, "Title")

 

 

'nSections' is a parameter that I will be receiving from the user input form on the assembly. 

And here's the public function I want to define as an external rule:

 

 

Function searchDownloadAndReplace(partName As String, searchDescription As String, searchFolders As String()) As String
    ' Build one to many name/value pairs of Property/Value as search criteria
    Dim mSearchParams As New System.Collections.Generic.Dictionary(Of String, String) ' Add UDP.DisplayName, Value Pairs
    mSearchParams.Add("Description", searchDescription) ' Applies to file 001002.ipt
    ' ...add as many as required to enable a unique search result

    ' Returns full file name in local working folder (download enforces override, if local file exists)
    Dim mVaultFile As String = iLogicVault.GetFileBySearchCriteria(mSearchParams, True, False, searchFolders)
    
    If mVaultFile Is Nothing Then
        Logger.Error("Vault file search: File not found - Please double check that file can be found with search criteria applied.")
        Return "File not found"
    Else
        Logger.Info("File " & mVaultFile & " found by search and downloaded to local workspace.")
        Component.Replace(partName, mVaultFile, True)
        Return mVaultFile
    End If
End Function

 

0 Likes