Hello,
I have recently started to write inventor addins in vb.net.
At the moment try a iLogic rule call. But i cant find an vb.net-example for this here or anywhere else.
I created the rule and now i want to run it.
I would be very happy if someone could help me with this problem!
Hello,
It's not as simple as just running the rule. The following code should get you what you need. I use this code in a third party VB.Net 2008 app. It should work for an addin. I hope this helps.
Private iLogicAuto As Object
Private invApp As Inventor.Application
Public Sub RuniLogicRule(ByVal RuleName As String)
On Error Resume Next
If iLogicAuto Is Nothing Then
iLogicAuto = GetiLogicAutomation(invApp)
End If
If iLogicAuto Is Nothing Then Exit Sub
Dim oDoc As Inventor.Document = invApp.ActiveDocument
iLogicAuto.RunRule(oDoc, RuleName)
oDoc = Nothing
On Error GoTo 0
End Sub
Private Function GetiLogicAutomation(ByVal oApplication As Inventor.Application) As Object
'Get the addins collection
Dim addIns As ApplicationAddIns
addIns = oApplication.ApplicationAddIns
'Find the add-in you are looking for
Dim customAddIn As ApplicationAddIn = Nothing
Dim i As Long
For i = 1 To addIns.Count
If (addIns(i).ShortDisplayName = "iLogic") Then
customAddIn = addIns.Item(i)
Exit For
End If
Next i
If (customAddIn Is Nothing) Then Return Nothing
customAddIn.Activate()
GetiLogicAutomation = customAddIn.Automation
End Function
RuniLogicRule("Update Title Block Rule")
I tried running this with vb.net 2013 and it desn't do anything (doesn't run the rule).
It seems like when it runs the GetIlogicAutomation sub, it does not return the ilogic object... because if I test the value of ilogicauto after it runs that sub, the value is still Nothing.
It also seems that the invapp object isn't being assigned anything? I am used to doing "invapp=thisapplication" after the dim statement, but vb.net says that "thisapplication" is not declared.
Here is what I have as a class library to call from ilogic itself:
Imports Inventor Public Class InventorAPI Private iLogicAuto As Object = Nothing Private invApp As Inventor.Application Public Sub RuniLogicRule(ByVal RuleName As String) On Error Resume Next invApp = Application If iLogicAuto Is Nothing Then iLogicAuto = GetiLogicAutomation(invApp) End If If iLogicAuto Is Nothing Then Exit Sub Dim oDoc As Inventor.Document = invApp.ActiveDocument iLogicAuto.RunRule(oDoc, RuleName) oDoc = Nothing On Error GoTo 0 End Sub Function ReadInvParam(ParamName As String) As String On Error Resume Next 'If iLogicAuto Is Nothing Then iLogicAuto = GetiLogicAutomation(invApp) 'End If If iLogicAuto Is Nothing Then Return "Unable to get Ilogic object" Dim oDoc As Inventor.Document = invApp.ActiveDocument Return iLogicAuto.ParamValue(oDoc, ParamName) End Function Private Function GetiLogicAutomation(ByVal oApplication As Inventor.Application) As Object 'Get the addins collection Dim addIns As ApplicationAddIns addIns = oApplication.ApplicationAddIns 'Find the add-in you are looking for Dim customAddIn As ApplicationAddIn = Nothing Dim i As Long For i = 1 To addIns.Count MsgBox(addIns(i)) If (addIns(i).ShortDisplayName = "iLogic") Then customAddIn = addIns.Item(i) Exit For End If Next i If (customAddIn Is Nothing) Then Return Nothing customAddIn.Activate() GetiLogicAutomation = customAddIn.Automation Return customAddIn.Automation End Function End Class
I am then calling that library with:
AddReference "C:\_Vaultwip\vb_addons\InventorAPI.dll" Dim oLib As New InventorAPI.InventorAPI MessageBox.Show(oLib.ReadInvParam("test")) Call oLib.RuniLogicRule("myRule")
and what I get is that it cannot get the ilogic object, and then nothing.
Thanks!
I've actually done something similar to this in C#. You need to assign the Inventor reference to you library.
In your class constructor I would pass the current Inventor Application object to the library like so.
Public Class InventorAPI Private iLogicAuto As Object = Nothing Private invApp As Inventor.Application Public Sub New(ByRef app As Inventor.Application) 'Set the app to the app supplied by the rule invApp = app 'Set the iLogic Automation iLogicAuto = invApp.ApplicationAddins.ItemsById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}").Automation End Sub Public Sub RuniLogicRule(ByVal RuleName As String) On Error Resume Next
Dim oDoc As Inventor.Document = invApp.ActiveDocument iLogicAuto.RunRule(oDoc, RuleName) oDoc = Nothing On Error GoTo 0 End Sub Function ReadInvParam(ParamName As String) As String On Error Resume Next Return iLogicAuto.ParamValue(oDoc, ParamName) End Function End Class
In the iLogic rule
AddReference "C:\_Vaultwip\vb_addons\InventorAPI.dll" Dim oLib As New InventorAPI.InventorAPI(ThisApplication) MessageBox.Show(oLib.ReadInvParam("test")) Call oLib.RuniLogicRule("myRule")