VBA run External Rule from Existing Addin

VBA run External Rule from Existing Addin

j.romo
Advocate Advocate
772 Views
6 Replies
Message 1 of 7

VBA run External Rule from Existing Addin

j.romo
Advocate
Advocate

For  my Purchased parts downloaded from the web I made an Add-in that changes the part to purchased and adds some custom Iprops, now that Im working with MS Access I want to add that part to a Parts list from the project from a button in the Add-in but Cant figuer out how to call a external rule to run frm the click of the button.

purchased.JPG

here is the code in the Default.ivb-COMPRAS (code)


Private Sub CommandButton1_Click()

Dim invDoc As Document
Set invDoc = ThisApplication.ActiveDocument


' Get the user defined (custom) property set.
Dim invCustomPropertySet As PropertySet
Set invCustomPropertySet = invDoc.PropertySets.Item("Inventor User Defined Properties")

' Declare some variables that will contain the various values.
Dim strProv As String
Dim STRTEL As String
Dim strEmail As String
Dim strTE As String
Dim strWBL As String



' Set values for the variables.
strProv = "Some sample text."
STRTEL = TextBox1.Value
strEmail = "Some sample text."
strTE = "Some sample text."
strWBL = "Some sample text."


' Create the properties.

Set invProperty = invCustomPropertySet.Add(strProv, "PROVEEDOR")
Set invProperty = invCustomPropertySet.Add(STRTEL, "TELEFONO")
Set invProperty = invCustomPropertySet.Add(strEmail, "E-MAIL")
Set invProperty = invCustomPropertySet.Add(strTE, "TIEMPO DE ENTREGA")
Set invProperty = invCustomPropertySet.Add(strWBL, "WEB")



End Sub

Private Sub CommandButton2_Click()

' Get the active document.
Dim doc As Document

Set doc = ThisApplication.ActiveDocument

' Get the custom property set.
Dim customPropSet As PropertySet
Set customPropSet = doc.PropertySets.Item( _
"Inventor User Defined Properties")

 

 

' Get the property named "TELEFONO".
Dim customProp1 As Property
Dim customProp2 As Property
Dim customProp3 As Property
Dim customProp4 As Property
Dim customProp5 As Property



Set customProp1 = customPropSet.Item("TELEFONO")
Set customProp2 = customPropSet.Item("PROVEEDOR")
Set customProp3 = customPropSet.Item("E-MAIL")
Set customProp4 = customPropSet.Item("TIEMPO DE ENTREGA")
Set customProp5 = customPropSet.Item("WEB")



' Set the value of the property.

customProp1.Value = TextBox1.Value
customProp2.Value = TextBox2.Value
customProp3.Value = TextBox3.Value
customProp4.Value = TextBox4.Value
customProp5.Value = TextBox5.Value


End Sub

 

 


Private Sub CommandButton3_Click()

' Get the active document.
Dim doc As Document
Set doc = ThisApplication.ActiveDocument


doc.ComponentDefinition.BOMStructure = 51973
End Sub

Private Sub database_Click()

 

 

End Sub

0 Likes
Accepted solutions (2)
773 Views
6 Replies
Replies (6)
Message 2 of 7

clutsa
Collaborator
Collaborator
Call mySubName
'or
Call myModuleName.mySubName

I'm missing something. 

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

Message 3 of 7

j.romo
Advocate
Advocate

I found This rule but i cant make it to work as I want

The code is from Owner2229

I made a new Module  

Public Sub ComprasRule()

    'ilogic rule name:
     RuniLogic ("X:\RECURSOS INVENTOR\Design Data\iLogic\EXTERNAL RULES\ComprasDb.iLogicVb")
     
End Sub
Public Sub RuniLogic(ByVal RuleName As String)
    Dim iLogicAuto As Object
    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
    If oDoc Is Nothing Then
        MsgBox "Missing Inventor Document"
        Exit Sub
    End If
    Set iLogicAuto = GetiLogicAddin(ThisApplication)
    If (iLogicAuto Is Nothing) Then Exit Sub
    iLogicAuto.RunExternalRule oDoc, RuleName
End Sub
 
Public Function GetiLogicAddin(oApplication As Inventor.Application) As Object
    Dim addIn As ApplicationAddIn
    On Error GoTo NotFound
    Set addIn = oApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}")
    If (addIn Is Nothing) Then Exit Function
    addIn.Activate
    Set GetiLogicAddin = addIn.Automation
    Exit Function
NotFound:
End Function

and in the button code 

Private Sub database_Click()

 Call ComprasRules.ComprasRule



End Sub

but I doesnt work it sends me thiscatastrophe.JPG

 

0 Likes
Message 4 of 7

clutsa
Collaborator
Collaborator
Accepted solution

I can't really test this but try dropping the full file path down to just the external rule name

Public Sub ComprasRule()
    'ilogic rule name:
     RuniLogic ("ComprasDb")
End Sub
If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 5 of 7

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@j.room,

 

I found that code in below forum post is working fine.

 

https://forums.autodesk.com/t5/inventor-customization/run-ilogic-external-rule-with-inventor-vba/td-...

 

Public Sub RunRule()
    RuniLogic ("C:\Users\t_gc\Desktop\Sample.iLogicVb")
End Sub

Public Sub RuniLogic(ByVal RuleName As String)
    Dim iLogicAuto As Object
    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
    If oDoc Is Nothing Then
        MsgBox "Missing Inventor Document"
        Exit Sub
    End If
    Set iLogicAuto = GetiLogicAddin(ThisApplication)
    If (iLogicAuto Is Nothing) Then Exit Sub
    iLogicAuto.RunExternalRule oDoc, RuleName
End Sub
 
Public Function GetiLogicAddin(oApplication As Inventor.Application) As Object
    Dim addIn As ApplicationAddIn
    On Error GoTo NotFound
    Set addIn = oApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}")
    If (addIn Is Nothing) Then Exit Function
    addIn.Activate
    Set GetiLogicAddin = addIn.Automation
    Exit Function
NotFound:
End Function

Why below code is needed? Is there any class "ComprasRules"?

 

Private Sub database_Click()

 Call ComprasRules.ComprasRule

End Sub

Function can be called directly using below code.

Private Sub database_Click()

 Call ComprasRule

End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 6 of 7

clutsa
Collaborator
Collaborator

Are you sure the error happens in the code you have posted and not in 

"X:\RECURSOS INVENTOR\Design Data\iLogic\EXTERNAL RULES\ComprasDb.iLogicVb"   

 Maybe try making a simple external rule that just posts back a message box that says "Done" and see if that gives you any trouble

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 7 of 7

j.romo
Advocate
Advocate

Thanks A lot Chandra and Clutsa,

The action will be a Button in the User Form

I inserted the code as follows

 Public Sub RunRule()
RuniLogic ("X:\RECURSOS INVENTOR\Design Data\iLogic\EXTERNAL RULES\ComprasDb.iLogicVb")
End Sub

Public Sub RuniLogic(ByVal RuleName As String)
Dim iLogicAuto As Object
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
If oDoc Is Nothing Then
MsgBox "Missing Inventor Document"
Exit Sub
End If
Set iLogicAuto = GetiLogicAddin(ThisApplication)
If (iLogicAuto Is Nothing) Then Exit Sub
iLogicAuto.RunExternalRule oDoc, RuleName
End Sub

Public Function GetiLogicAddin(oApplication As Inventor.Application) As Object
Dim addIn As ApplicationAddIn
On Error GoTo NotFound
Set addIn = oApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}")
If (addIn Is Nothing) Then Exit Function
addIn.Activate
Set GetiLogicAddin = addIn.Automation
Exit Function
NotFound:
End Function






Private Sub CommandButton3_Click()

' Get the active document.
Dim doc As Document
Set doc = ThisApplication.ActiveDocument


doc.ComponentDefinition.BOMStructure = 51973
End Sub

Private Sub CommandButton4_Click()

Call RunRule

'MsgBox "Missing Inventor Document"
End Sub

And It worked directly in the User Form 

Thanks a lot guys

0 Likes