Want to replace i logic rule via vba

Want to replace i logic rule via vba

k14348
Advocate Advocate
847 Views
7 Replies
Message 1 of 8

Want to replace i logic rule via vba

k14348
Advocate
Advocate

Hi,

  I have one assembly with ilogic rule MKA_Rule0. Now i want to edit that ilogic rule by using vba code. Rule details attached. Can anybody help?

 

-Karthikeyan M

0 Likes
Accepted solutions (3)
848 Views
7 Replies
Replies (7)
Message 2 of 8

dean.morrison
Advocate
Advocate

Hi,

 

Put the New rule text into a .txt file, and change the reference (Ruletext = ) in the code below.

 

VBA code below will replace the text in your rule MKA_Rule0 with the text in the .txt file you create.

 

Hope this helps.

 

Dean.

 

 

Sub Change_MKA_Rule()

Dim iLogicAuto As Object
Set iLogicAuto = GetiLogicAddin(ThisApplication)
If (iLogicAuto Is Nothing) Then Exit Sub

Dim doc As Document
Set doc = ThisApplication.ActiveEditDocument

Dim RuleName As String
Dim ruleText As String
Dim Rule As Object

RuleName = "MKA_Rule0"
Set Rule = iLogicAuto.GetRule(doc, RuleName)

ruleText = ReadAllText("C:\TestRule.txt")

Rule.Text = ruleText

End Sub

0 Likes
Message 3 of 8

k14348
Advocate
Advocate

Compile error

 Sub or function not defined.

 

Code is not working

Sub Change_MKA_Rule()
Dim iLogicAuto As Object
Set iLogicAuto = GetiLogicAddin(ThisApplication)
If (iLogicAuto Is Nothing) Then Exit Sub

Dim doc As Document
Set doc = ThisApplication.ActiveEditDocument
Dim RuleName As String
Dim ruleText As String
Dim Rule As Object
RuleName = "MKA_Rule0"
Set Rule = iLogicAuto.GetRule(doc, RuleName)
ruleText = ReadAllText("C:\Users\mukarthikeyan\Desktop\MKA_Rule.txt")
Rule.Text = ruleText
End Sub

 

0 Likes
Message 4 of 8

Anonymous
Not applicable
Accepted solution

You have missed out the function in you code to get the addin. Try this code which might be helpful.

Public Sub RuniLogic()
  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
  Dim oRule As Object
Set oRule = iLogicAuto.getrule(oDoc, "MKA_Rule0")
oRule.Text = "iproperties.value(""Custom"", ""Air_wieght"")=Ceil((iproperties.mass+(21.156*5))*1.03)"
Beep
End Sub
 
Function GetiLogicAddin(oApplication As Inventor.Application) As Object
Dim addIns As ApplicationAddIns
Set addIns = oApplication.ApplicationAddIns
'Find the add-in you are looking for
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
Message 5 of 8

Anonymous
Not applicable

Here is another code in which you can edit your ilogic code from the text file.

 

Public Sub RuniLogic()
    Dim iLogicAuto As Object
    Dim oDoc As Document
  
'********Memeory allocated**************
        Set oDoc = ThisApplication.ActiveDocument
        If oDoc Is Nothing Then
          MsgBox "Missing Inventor Document"
          Exit Sub
        End If
  
'********Memeory allocated**************
        Set iLogicAuto = GetiLogicAddin(ThisApplication)
        If (iLogicAuto Is Nothing) Then Exit Sub
    Dim oText As String, oFileName As String, oTextLine As String
        oFileName = "E:\test.txt" 'Add your ilogic code in this text file
        Open oFileName For Input As #1
        Do Until EOF(1)
            Line Input #1, oTextLine
            oText = "" & oTextLine
        Loop
        Close #1
    Dim oRule As Object
  
'********Memeory allocated**************
        Set oRule = iLogicAuto.getrule(oDoc, "MKA_Rule0")
        oRule.Text = oText

'Release all the memory allocated to free the memory
        Set oDoc = Nothing
        Set iLogicAuto = Nothing
        Set oRule = Nothing
        Set addIn = Nothing
        Set addIns = Nothing
        Set GetiLogicAddin = Nothing
End Sub
 
Function GetiLogicAddin(oApplication As Inventor.Application) As Object
    Dim addIns As ApplicationAddIns
    
'********Memeory allocated**************
        Set addIns = oApplication.ApplicationAddIns
'Find the add-in you are looking for
    Dim addIn As ApplicationAddIn
        On Error GoTo NotFound
        
'********Memeory allocated**************
        Set addIn = oApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}")
        If (addIn Is Nothing) Then Exit Function
        addIn.Activate
        
'********Memeory allocated**************
        Set GetiLogicAddin = addIn.Automation
        Exit Function
NotFound:
End Function
0 Likes
Message 6 of 8

k14348
Advocate
Advocate

Error

0 Likes
Message 7 of 8

Anonymous
Not applicable
Accepted solution

Please comment out these lines it will resolve the issue

 

        'Set addIn = Nothing
        'Set addIns = Nothing
        'Set GetiLogicAddin = Nothing

 I forget to do that.

0 Likes
Message 8 of 8

dean.morrison
Advocate
Advocate
Accepted solution

Sorry i did forget about the function.

 

here it is.

 

Function GetiLogicAddin(oApplication As Inventor.Application) As Object
' Get addin collection & addin variables
Dim addIns As ApplicationAddIns
Set addIns = oApplication.ApplicationAddIns
Dim addIn As ApplicationAddIn
Dim customAddIn As ApplicationAddIn

For Each addIn In addIns
If (addIn.ClassIdString = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}") Then
Set customAddIn = addIn
Exit For
End If
Next

If (customAddIn Is Nothing) Then Exit Function
customAddIn.Activate

Set GetiLogicAddin = customAddIn.Automation

End Function

 

 

Hopefully this resolves your issue. I did test it, but forgot to paste the function as well.

 

Dean.

0 Likes