Workflow for using Visual Studio to write iLogic Rules

Workflow for using Visual Studio to write iLogic Rules

waynehelley
Collaborator Collaborator
2,138 Views
6 Replies
Message 1 of 7

Workflow for using Visual Studio to write iLogic Rules

waynehelley
Collaborator
Collaborator

Hi all,

 

I have been working on a workflow to allow me to work in Visual Studio instead of the iLogic interface.

 

Firstly, I create classes which referance the iLogic class librarys.  Example...

 

Imports Inventor

Imports Autodesk.iLogic.Interfaces

 

Public Class UCSDKS_EDGE

 

Sub PROCESS(ThisDoc As Inventor.PartDocument, _invApp As Application, iLogicVb As ILowLevelSupport, iProperties As IiProperties)

 

iProperties.Value("Project", "Part Number") = "TEST1"

iLogicVb.DocumentUpdate()

 

End Sub

End Class

 

I can then, with Build events, get my classes to be automatically coppied to a set location so that I can call them up from internal iLogic rules. Example...

 

AddVbFile "Z:\Working Folder\ILOGIC EXTERNAL RULES\Residential\UCSDKS\UCSDKS_EDGE.vb"

Sub Main()

Dim edge As New UCSDKS_EDGE

edge.PROCESS(ThisDoc.Document, ThisApplication, iLogicVb, iProperties)

End Sub

 

This is all great but I have one small issue in that I want to be able to Debug from inside Visual Studio.  To do this I somehow need to pass the 'iLogicVb' and 'iProperties' objects into the class.

Module TestModule

Sub Main()

Dim _invApp As Inventor.Application

Dim _started As Boolean = False

 

Dim ucs As New UCSDKS.UCSDKS_EDGE 

 

' Add any initialization after the InitializeComponent() call.

Try

_invApp = Marshal.GetActiveObject("Inventor.Application")

Catch ex As Exception

Try

Dim invAppType As Type =

GetTypeFromProgID("Inventor.Application")

_invApp = CreateInstance(invAppType)

_invApp.Visible = True

'Note: if you shut down the Inventor session that was started

'this(way) there is still an Inventor.exe running. We will use

'this Boolean to test whether or not the Inventor App will

'need to be shut down.

_started = True

Catch ex2 As Exception

MsgBox(ex2.ToString())

MsgBox("Unable to get or start Inventor")

End Try

End Try

 

ucs.PROCESS(_invApp.ActiveDocument, _invApp, iLogicVb, iProperties)

'Release references

_invApp = Nothing

End Sub

End Module

 

Does anybody know how to do this??  Thanks in advance 🙂

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
0 Likes
2,139 Views
6 Replies
Replies (6)
Message 2 of 7

waynehelley
Collaborator
Collaborator

In other words, I know I have to declare the iProperties and ILowLevel support Interfaces but I don't know what to set them as.

 

Dim iProperties As IiProperties

iProperties = _invApp.ActiveDocument....?????

 

Dim iLogicVb As ILowLevelSupport

iLogicVb = _invApp.ActiveDocument....????? 

 

ucs.PROCESS(_invApp.ActiveDocument, _invApp, iLogicVb, iProperties)

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
0 Likes
Message 3 of 7

waynehelley
Collaborator
Collaborator

I can loop through the add-ins to find the iLogic add-in but I can't seem to find anything relating to interfaces...

 

Dim oAddIn As ApplicationAddIn

For Each oAddIn In _invApp.ApplicationAddIns

If oAddIn.DisplayName = "iLogic" Then

iProperties = oAddIn...?????

End If

Next

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
0 Likes
Message 4 of 7

rjay75
Collaborator
Collaborator

It's possible to do what you want to do. But think about it simpler.

 

First to do what you want realize that while you are in Visual Studio you are outside of the iLogic environment so things work differently. So things must be in classes and modules and subs/functions. Next you can't specify units as part of your number literals. (No 2.5 in) Also you have complete direct access to the full API so it may be simpler to only pass what you need at a minimum (often this is just the document) and use the API to get the rest.

 

But do  what you wanted try not to grab more than you need. Your class knows nothing of iLogic and has to get a reference for everything from somewhere. The only place to get any of the iLogic specific things like the iLogicVb and iProperties object is to explicitly pass it to your sub. The interfaces that pertain to things in a rule don't exist as actual objects that you can iterate through.

 

First so you don't have to instantiate any classes put your vb code in modules or as Shared subs in classes.

 

Here's an example. 

 

The Visual Studio Code:

Imports Autodesk.iLogic.Interfaces
Imports Inventor

Public Module ilTest
    Sub Process(ThisDoc As Inventor.PartDocument, iLogicVb As ILowLevelSupport, iProperties As IiProperties)

        iProperties.Value("Project", "Part Number") = "TEST1"
        iLogicVb.DocumentUpdate()

    End Sub

'Sample Sub Stub to get the Application Object easier
Sub SubStub(ThisDoc As Inventor.Document)
Dim ThisApplication As Inventor.Application = ThisDoc.Parent
End Sub

End Module

The iLogic Rule Code

 

AddReference "ilTest.dll"

ilTest.Process(ThisDoc.Document, iLogicVb, iProperties)

 

Your TestModule module is not needed.

 

1. Compile your VB code and place the DLL and the PDB file in your iLogic folder.

 

2. Just attach VB to the Inventor process before invoking your rule. (In Visual Studio under the Tools or Debug menu. "Attach to Process" Command)

 

 

3. Place a break point in your module code inside a sub or function at the location you want to start debugging.  

 

When you run the rule VB wil break at the point .

 

I do use this method occassionaly to test code immediately instead of creating a full Addin or creating a lot of extra work just to get my library a reference to the inventor application.

Message 5 of 7

waynehelley
Collaborator
Collaborator

Hi Rodney, thanks very much for taking the time to respond.

 

Your workflow for compiling to DLLs and debugging sounds great and I want to work like that in the future.  For the time being I am set on exporting my Visual Basic Source Files for each class to be called up within iLogic.  I know this might seem like bad practise and bad for performance but if you could see our system and workflow in action, it might make a little bit more sense.

 

We have a repository of 100s of External iLogic Rules which we share and deploy through a BitBucket server.  These rules get triggered from within each component which work their logic based on iProperties passed down from the higher levels.  Using Build Events to drop the VB source files into the repository working folder works quite well where as if we are going to change to calling up DLLs, we probably need to think about converting the core our iLogic rules in Visual Studio.  I don't think we are quite ready for that just now!

 

The reason behind the test module is that I just want to be able to see where exceptions are happening and get better feedback than I would if I was working in the iLogic interface. 

 

For now, is there a way for me to pass in the interfaces from my test module?  Or when you say, 'The only place to get any of the iLogic specific things like the iLogicVb and iProperties object is to explicitly pass it to your sub', does this mean that these referances must come from the iLogic interface?

 

Thanks,

 

Wayne

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
0 Likes
Message 6 of 7

waynehelley
Collaborator
Collaborator

Hi Rodney,

 

I have been taking advantage of your workflow suggestion of Attaching Visual Studio to the Inventor process.  It has been very effective so thankyou very much!

 

I just have one problem with it and I was wondering if you have a solution...

 

As soon as Inventor uses my DLL file, it becomes locked and I can't rebuild it.  This means I have to restart Inventor everytime I need to make an edit and rebuild.  Do you know if there is a way to break Inventors reference to my DLL?

 

Thanks

 

Wayne

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
0 Likes
Message 7 of 7

DavidTruyens
Advocate
Advocate
Maybe I 'm making things too easy. But I usually create everything with visual studio (a form or a module) . Once that is done I create an ilogic rule that runs the exe (shell("c:\.....exe"). If I would like to debug I run it from VS...


David Truyens
Technical Specialist
Twitter | LinkedIn | IDCM | GitHub

0 Likes