vb.net accessing ilogic rules in document

vb.net accessing ilogic rules in document

snappyjazz
Collaborator Collaborator
764 Views
2 Replies
Message 1 of 3

vb.net accessing ilogic rules in document

snappyjazz
Collaborator
Collaborator

I am writing code to test a multitude situations. This code gets a parameter that will be changed; then pick a random number to put in that parameter; then run the ilogic rules in the assembly to process the new parameter value. I hope I didn't leave anything out and more importantly that, that made sense. The part that keeps failing is when I try and set up ilogic automation. It's a piecemeal from a couple of other posts that I can't figure out. The section of code I am struggling with is in region:"establish doc rules" under comment:"define ilogic automation vehicle".

 

Can you help?!?

P.S. I am aware that the loops are infinite, this is by design.

Imports System
Imports System.Type
Imports System.Activator
Imports SysInter = System.Runtime.InteropServices
Imports System.IO
Imports Inv = Inventor
Imports iLogic = Autodesk.iLogic
Imports Excel = Microsoft.Office.Interop.Excel

Public Sub RunStiffeners()
        'get inventor session
        Dim InvApp As Inv.Application = SysInter.Marshal.GetActiveObject("Inventor.Application")
        Dim InvDoc As Inv.AssemblyDocument = InvApp.ActiveDocument
        'Dim InvDoc As Inv.AssemblyDocument = Thisdoc.Document

        'Get the part user parameters
        Dim AssyUserParameters As Inv.UserParameters = InvDoc.ComponentDefinition.Parameters.UserParameters
        Dim AssyModelParameters As Inv.ModelParameters = InvDoc.ComponentDefinition.Parameters.ModelParameters
        Console.Write("Got Paramters" & vbNewLine & "User Parameter qty: " & AssyUserParameters.Count & vbNewLine & "Model Parameter qty: " & AssyModelParameters.Count & vbNewLine)
        Console.Write(vbNewLine)

#Region "Define UOM"
        'Inventor stores background units in metric, need this to convert back to SI
        Dim UoM As Inv.UnitsOfMeasure = InvDoc.UnitsOfMeasure
#End Region

#Region "User Parameters"
        Dim StiffFBQty As Inv.UserParameter = Nothing

        For Each RParam As Inv.UserParameter In AssyUserParameters
            If RParam.Name = "STIFF_EXT_F_B_QTY" Then
                StiffFBQty = RParam
                Console.Write(StiffFBQty.Name & "  :  " & StiffFBQty.Expression & vbNewLine)
            End If
        Next
        Console.Write(vbNewLine)
#End Region

#Region "Establish document rules"
        'iLogic rule names
        Dim iLPushParam As String = "Push Parameters to Components"
        Dim iLStiffs As String = "Stiffeners"

        'Define iLogic Automation vehicle
        Dim addinGUID As String = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"
        Dim addin As Inv.ApplicationAddIn = InvApp.ApplicationAddIns.ItemById(addinGUID)
        Dim iLogicAuto As iLogic.Automation.iLogicAutomation
        If addin IsNot Nothing Then
            addin.Activate()
            iLogicAuto = addin.Automation
        End If

        'Establish the ilogic rules
        Dim iLRulePushParam As iLogic.Interfaces.iLogicRule = iLogicAuto.GetRule(InvDoc, iLPushParam)
        Dim iLRuleStiffs As iLogic.Interfaces.iLogicRule = iLogicAuto.GetRule(InvDoc, iLStiffs)
#End Region

#Region "Change Qty and run rules"
        Dim SleepValue As Integer = 15000

        Do While 0 < 3
            'randomly select a numnber from the stiff qty list and change the qty parameter
            Do While 0 < 3 : Dim RandomNum As New Random : Dim NewStiffQty As Integer = RandomNum.Next(0, 10) : Select Case NewStiffQty : Case 0, 2, 4, 6, 8, 10 : StiffFBQty.Expression = NewStiffQty : Exit Do : End Select : Loop
            Console.WriteLine("New Stiffener Qty: " & StiffFBQty.Value)

            'Run the rules
            iLogicAuto.RunRuleDirect(iLRuleStiffs)
            iLogicAuto.RunRuleDirect(iLRulePushParam)
            InvDoc.Update()

            'pause code to allow the model to finish updating
            Threading.Thread.Sleep(SleepValue)
        Loop
#End Region
    End Sub

 

 

 

 

 

0 Likes
Accepted solutions (1)
765 Views
2 Replies
Replies (2)
Message 2 of 3

snappyjazz
Collaborator
Collaborator

I forgot to post the error I'm getting:

 

image.png

0 Likes
Message 3 of 3

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @snappyjazz 

 

If you just dim iLogicAuto And your rules as Object it should work 🙂

 

 

#Region "Establish document rules"
        'iLogic rule names
        Dim iLPushParam As String = "Rule0"
        Dim iLStiffs As String = "Stiffeners"

        'Define iLogic Automation vehicle
        Dim addinGUID As String = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"
        Dim addin As Inv.ApplicationAddIn = InvApp.ApplicationAddIns.ItemById(addinGUID)
        Dim iLogicAuto As Object
        If addin IsNot Nothing Then
            addin.Activate()
            iLogicAuto = addin.Automation
        End If

        'Establish the ilogic rules
        Dim iLRulePushParam As Object = iLogicAuto.GetRule(InvDoc, iLPushParam)
        Dim iLRuleStiffs As Object = iLogicAuto.GetRule(InvDoc, iLStiffs)

#End Region

I see now that i changed iLPushParam to "Rule0" for my testing... Just change that back and it should work

 

0 Likes