How to run a rule with vb.net

How to run a rule with vb.net

fakeru
Advocate Advocate
3,126 Views
16 Replies
Message 1 of 17

How to run a rule with vb.net

fakeru
Advocate
Advocate
Hi
I recently started to learn programming for inventor.
I use "my first plugin for inventor" tutorial. There are a lot of things to learn. However I want already to create a add-in for inventor, so i have a new ribbon tab with some custom button commands. For that I use a visual basic project template.
There is one important thing I can't do and that is to make an ilogic rule from a part to run when the button is clicked.
There is a code for macro which works fine, but when I insert the code into the visual studio, it doesn't work.
Please help if you can. The goal is to call the ilogic form from a part, and probably a rule for that is required.


Regards
Alexandru
Autodesk Inventor 2015 Certified Professional
0 Likes
Accepted solutions (1)
3,127 Views
16 Replies
Replies (16)
Message 2 of 17

ewiggers
Enthusiast
Enthusiast

Hello Alexandru,

 

What is excactly your goal with this? It is possible to run a rule from an Addin but isn't it better to embed the code in your addin? Can you show the code you want to use?

 

Regards,

Eelco

0 Likes
Message 3 of 17

fakeru
Advocate
Advocate

I'm trying to open an ilogic form within a part through a button click.

 

In macros editor I use this code and it works with no problems

Capture.JPG

 

When inserted into Visual Basic Express 2010, under the button click action, it doesnt' work. Too many errors.

 

I will post a screenshot of that, when I'm home.

 

 

Autodesk Inventor 2015 Certified Professional
0 Likes
Message 4 of 17

fakeru
Advocate
Advocate

So I'm pasting the code above in the space indicated in the screenshot, replacing the message Hello! I guess that is completely wrong.

 Untitled.jpg

Autodesk Inventor 2015 Certified Professional
0 Likes
Message 5 of 17

ewiggers
Enthusiast
Enthusiast
Can you show me the code you want to insert. The space you want to insert the code is correct if you want your code to be executed with the button click
0 Likes
Message 6 of 17

ewiggers
Enthusiast
Enthusiast
Sorry, I replayed to soon.

The code in the image is VBA code and that can't be inserted without editing.

When I am at home I will edit the code and post this.
0 Likes
Message 7 of 17

ewiggers
Enthusiast
Enthusiast
Accepted solution

Hello,

 

Paste the following code into the Activate sub in your InventorAddinServer Class:

AddinGlobal.oInvApplication = addInSiteObject.Application

 

Paste the following code into your AddinGlobal Class:

Public Shared oInvApplication As Inventor.Application

Public Shared Function GetiLogicAddin(_Application As Inventor.Application) As Object Dim oAddIn As Inventor.ApplicationAddIn Try oAddIn = _Application.ApplicationAddIns.ItemById("{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}") If (oAddIn Is Nothing) Then Return Nothing End If oAddIn.Activate() Return oAddIn.Automation Catch Return Nothing End Try End Function

 

Paste the following code into your ButtonActions Class and replace the Button1_Execute code:

Public Shared Sub Button1_Execute()
Dim iLogicAuto As Object
Dim oDoc As Inventor.Document

' Set active document
oDoc = AddinGlobal.oInvApplication.ActiveDocument
If (oDoc Is Nothing) Then
    MsgBox("Missing Inventor Document")
    Exit Sub
End If

iLogicAuto = AddinGlobal.GetiLogicAddin(AddinGlobal.oInvApplication)
If (iLogicAuto IsNot Nothing) Then
    iLogicAuto.RunRule(oDoc, "Show")
End If
End Sub

 

I think this should work for you, not completely sure because I didn't had time to test it so maybe I overlooked something. If you get errors, please post them the next time you reply.

 

Regards,

Eelco

Message 8 of 17

fakeru
Advocate
Advocate

Hi Eelco

I tried the codes. Initially it seems ok. I hit debug, then hit the button in inventor, then this appears:

Untitled2.jpg

 

 

You told to put in some codes in the InventorAddinServer and AddinGlobal, but those where not empty. So this is what I have there, with red I marked the code from you.

For AddinGlobal:

Imports System.Collections.Generic
Imports System.Runtime.InteropServices

Imports Inventor

'Namespace InventorNetAddinVB2
Public Class AddinGlobal
    Public Shared InventorApp As Inventor.Application
    Public Shared oInvApplication As Inventor.Application
    Public Shared RibbonPanelId As String
    Public Shared RibbonPanel As RibbonPanel
    Public Shared ButtonList As New List(Of InventorButton)()

    Private Shared mClassId As String
    Public Shared Property ClassId() As String
        Get
            If Not String.IsNullOrEmpty(mClassId) Then
                Return AddinGlobal.mClassId
            Else
                Throw New System.Exception("The addin server class id hasn't been gotten yet!")
            End If
        End Get
        Set(ByVal value As String)
            AddinGlobal.mClassId = value
        End Set
    End Property

    Public Shared Sub GetAddinClassId(ByVal t As Type)
        Dim guidAtt As GuidAttribute = DirectCast(GuidAttribute.GetCustomAttribute(t, GetType(GuidAttribute)), GuidAttribute)
        mClassId = "{" & guidAtt.Value & "}"
    End Sub

    Public Shared Function GetiLogicAddin(ByVal _Application As Inventor.Application) As Object
        Dim oAddIn As Inventor.ApplicationAddIn
        Try
            oAddIn = _Application.ApplicationAddIns.ItemById("{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}")
            If (oAddIn Is Nothing) Then
                Return Nothing
            End If
            oAddIn.Activate()
            Return oAddIn.Automation
        Catch
            Return Nothing
        End Try
    End Function
End Class
'End Namespace

 InventorAddinServer:

Untitled3.jpg

Autodesk Inventor 2015 Certified Professional
0 Likes
Message 9 of 17

ewiggers
Enthusiast
Enthusiast

Hi Alexandru,

 

The Error is because the oInvApplication is not assigned at the start of your addin.

 

You didn't place the Inventor Application object/variable oInvApplication in the InventorAddinServer Class as I said in my previous post, you still have the Inventor Application object/variable InventorApp placed there. That is no problem, leave it there and delete the Inventor Application object/variable oInvApplication I told you to place in the AddinGlobal Class. You don't need two of them.

 

The next thing you need to do is replace the Inventor Application object/variable oInvApplication in the code I provided you with the Inventor Application object/variable InventorApp and your code will work.

 

Regards,

Eelco

Message 10 of 17

fakeru
Advocate
Advocate

yes, it worked! thank you very much!

Autodesk Inventor 2015 Certified Professional
0 Likes
Message 11 of 17

fakeru
Advocate
Advocate

could an option be added to the code? suppose the active document is an assembly that has some parts that have inside them a rule named "show" and in this case the option would that when you click the button, you must select in window or browser the desired part. 

if the active document is part, then the action should be the same as above.

 

thanks in advance!

Autodesk Inventor 2015 Certified Professional
0 Likes
Message 12 of 17

ewiggers
Enthusiast
Enthusiast

Hi,

 

Can you be more specific in what you want? Do you want to select a part that has the internal rule "Show" present? Why do you want that, to run that rule from the part or?

 

Regards,

Eelco

0 Likes
Message 13 of 17

fakeru
Advocate
Advocate
Hi,
So suppose you have an assembly open and some parts have inside them a rule named "show" and when the button1 is clicked (i will have that button in assembly enviroment also) the user must be prompted to select the part that has the rule "show" and then that rule from the selected part should be run. If the selected part has no rule "show" nothing should happen.
When the active document is a part, the code must be the same as you provided, simply running the rule.
I think there should be an extra if then condition at the start, checking the document type. If the active document is assembly do this, if active document is part do this.

I hope that brings more clarity.
Autodesk Inventor 2015 Certified Professional
0 Likes
Message 14 of 17

ewiggers
Enthusiast
Enthusiast

Hi,

 

Yes it is clear now Smiley Happy. All the things you want can be done but I don't want to start programming for you from scratch. So what is the code you have so far, can you show it? I will help you if you get stuck.

 

Start to get an object witch will hold the occurences from the assembly and loop through all members to check if the rule "Show is present. If present then you can start that rule or present a dialog to the user.

 

Regards,

Eelco

0 Likes
Message 15 of 17

fakeru
Advocate
Advocate

I thought about working further with the code you gave it to me:

 

Public Shared Sub Button1_Execute()
Dim iLogicAuto As Object
Dim oDoc As Inventor.Document

' Set active document
oDoc = AddinGlobal.oInvApplication.ActiveDocument
If (oDoc Is Nothing) Then
    MsgBox("Missing Inventor Document")
    Exit Sub
End If

iLogicAuto = AddinGlobal.GetiLogicAddin(AddinGlobal.oInvApplication)
If (iLogicAuto IsNot Nothing) Then
    iLogicAuto.RunRule(oDoc, "Show")
End If
End Sub

I'm already very thankful for that, i don't expect that you to write me codes. I know I must learn to do it myself, i just need more time for that.

Autodesk Inventor 2015 Certified Professional
0 Likes
Message 16 of 17

fakeru
Advocate
Advocate

This is what I could build at this moment, but I'm stuck of course.

The code is for the assembly file only, and I want to run the rule from the selected part when I click the button.

 

I don't know why it doesn't work. At least it does show the message box "Please select a component before running this rule." when there is nothing selected.

 

Public Shared Sub Button1_Execute()
        Dim iLogicAuto As Object
      
        Dim oDoc As Inventor.Document
        ' Set active document
        oDoc = AddinGlobal.InventorApp.ActiveDocument

        If (oDoc Is Nothing) Then
            MsgBox("Missing Inventor Document")
            Exit Sub
        End If

        If AddinGlobal.InventorApp.ActiveDocument.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                        Dim occ As ComponentOccurrence
            Try
                occ = AddinGlobal.InventorApp.ActiveDocument.SelectSet.Item(1)
            Catch
                MessageBox.Show("Please select a component before running this rule.", "iLogic")
                Return
            End Try

            If (TypeOf occ Is ComponentOccurrenceProxy) Then
                iLogicAuto = AddinGlobal.GetiLogicAddin(AddinGlobal.InventorApp)
                iLogicAuto.RunRule(occ, "Show")
            End If

        End If



    End Sub

 

Any ideas?

Thanks!

Autodesk Inventor 2015 Certified Professional
0 Likes
Message 17 of 17

fakeru
Advocate
Advocate

No ideas on this? Please help, I can't understand why it doesn't work...

Autodesk Inventor 2015 Certified Professional
0 Likes