execute vb.net from ribbon button

execute vb.net from ribbon button

TA.Fehr
Advocate Advocate
2,295 Views
9 Replies
Message 1 of 10

execute vb.net from ribbon button

TA.Fehr
Advocate
Advocate

* Disclaimer: My entire knowledge of code comes from google and trial and error * 

 

I've written a reasonably large program in vba which is able to modify nearly all aspects of any related drawing/model from open documents. I wanted to create this as an add-in and possibly distribute it but I'm having trouble finding support for the process.

 

I translated the code into vb.net and have created an .exe that functions perfectly. However, when implementing a button I've run into many issues.

I wasn't able to figure out how to create the button in my code, so I used a template created with Inventor add-in wizard. I imported all my forms and code to the template and have gotten the main form to show and function.

 

However, I noticed that when I first run the add-in from vs10 there are hundreds of errors showing up in the immediate window. The add-in still shows up, and functions so long as the code it runs is within the initial form. Any time the form calls code from a different form, or displays a different form, inventor crashes in its entirety.

I feel that I'm really close to being able to create an add-in that I can distribute, but I'm at a loss when it comes to implementing it from a button.

I've prettymuch checked out every resource I can find (which all seem to be quite outdated and terribly hard if not impossible to follow). And I can't seem to find a good walkthrough on how to take my .exe and turn it into an instalable add-in.

0 Likes
2,296 Views
9 Replies
Replies (9)
Message 2 of 10

GeorgK
Advisor
Advisor

Could you share the code to see whats wrong with it?

0 Likes
Message 3 of 10

TA.Fehr
Advocate
Advocate

Most certainly.

You'll have to forgive me as I'm not sure exactly what is all needed. I've included the main program that creates the exe (stand-alone) as well as the one with the script to create a button.

 

Both these programs have a reference to the progressbar attached as well.

 

Here's the screenshot of the references I have used:

Capture.PNG

 

It should be noted that some of the functions are linked to excel spreadsheet templates on our server that I haven't included, so buttons such as "Rename" and "Create Spreadsheet" will not function.

0 Likes
Message 4 of 10

TA.Fehr
Advocate
Advocate
Were you able to get the program to function?
0 Likes
Message 5 of 10

MechMachineMan
Advisor
Advisor

Workaround:

 

Use code below, then create a macro button for it.

 

 

Public Sub iLogicCollapseBrowser()

  RunExternaliLogicRule "CollapseBrowser"

End Sub

Sub RunExternaliLogicRule(ByVal ruleName As String)

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

Dim doc As Document
Set doc = ThisApplication.ActiveDocument

Dim i As Integer
i = iLogicAuto.RunExternalRule(doc, ruleName)

End Sub

Function GetiLogicAddin(oApplication As Inventor.Application) As Object
Set addIns = oApplication.ApplicationAddIns

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

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 6 of 10

TA.Fehr
Advocate
Advocate

Thanks for getting back to me.

 

My goal here is not to make a workaround, but to actually make the add-in function as it is supposed to (in regards to the ability to distribute and share).

I have created an .exe that functions as intended. It was my intention that reading Brian and Wayne's blog articles that there was a rather simple process of enabling a button the same way as I was able to make for the vba project.

 

Regardless,

 

I was able to get a little further with some playing around. I noticed that in order for the first form (Main) to show I needed to declare it as follows: 

Public Class ButtonActions
    Public Shared Sub Button1_Execute()
        'TODO: add code below for the button click callback.
        Dim Main As Main = New Main
        Main.Show()
    End Sub
End Class

This form functioned as expected. But when I needed to call a separate form (iProperties) the program would close. So in the Public Class of "Main", I declared:

    Dim iProperties As iProperties = New iProperties

and I was at least able to debug into the iProperties code.

 

In the iProperties form the code needs to run through a listbox contained within the "Main" userform. At this point the code would close just as before. So within the Public Sub that held the command to access the listbox in the "Main" userform, I declared:

Dim Main As Main = New Main

And this enabled the code to proceed. The issue here is that the code isn't able to read the listbox. I keep getting an item count of "0" for a listbox with multiple items.

 

I'm not sure why creating a button in inventor has caused the userforms to no longer communicate with each other.

 

0 Likes
Message 7 of 10

Owner2229
Advisor
Advisor

Hi, maybe this code below is what you're looking for. I've copied it from somewhere some time ago in order to use it later, but I didn't get to it yet, so I can't tell you how well/if does it work.

 

' Declare member variables.
Private m_inventorApplication As Inventor.Application
Private m_ClientID As String
Private WithEvents m_featureCountButtonDef As ButtonDefinition
Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, _
					ByVal firstTime As Boolean)
	' Initialize AddIn members.
	m_inventorApplication = addInSiteObject.Application
	' Get the ClassID for this add-in and save it in a
	' member variable to use wherever a ClientID is needed.
	m_ClientID = AddInGuid(GetType(StandardAddInServer))
	' Create the button definition.
	Dim controlDefs As ControlDefinitions
	controlDefs = m_inventorApplication.CommandManager.ControlDefinitions
	m_featureCountButtonDef = controlDefs.AddButtonDefinition( _
		"My Addin", _
		"AUAddInMyAddin", _
		CommandTypesEnum.kShapeEditCmdType, _
		m_ClientID, _
		"Runs the Addin.", _
		"My Addin")
	If firstTime Then
		' Create a new command bar (toolbar) and make it visible.
		Dim commandBars As CommandBars
		commandBars = m_inventorApplication.UserInterfaceManager.CommandBars
		Dim commandBar as CommandBar
		commandBar = commandBars.Add( "My Macros", "AUAddInMyMacros",,m_ClientID)
		commandBar.Visible = True
		' Add the control to the command bar.
		commandBar.Controls.AddButton(m_featureCountButtonDef)
    End If
End Sub

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 8 of 10

TA.Fehr
Advocate
Advocate

I appreciate the responses. However, the issue is not getting the button working nor the code running. It is accessing different userforms once the code runs. As I've mentioned, while this code was written as a stanalone .exe, there was no issue, only now that I have integrated it into the inventor ribbon do I have issues.

 

Perhaps the attached will explain a bit better. I've written a very simple add-in program that adds a button to the "tools" menu. It accesses the active part name and the associated drawing (or vise-versa). A button then opens a second form which pulls data from the first form. This functions as expected, but if you analize the code you'll notice that when form2 is accessed, I have to Dim Form1 as a new form and pass that variable to Form2 at which time Form1 must be re-initialized and repopulated even though the data is already available.

 

Is every other program I've written, so long as a form is open, any code has access to it's contents without having to re-initialize the form in its entirety.

 

I feel I'm missing something obviously big, because I can't imagine all the add-ins that have been created have had to go through this terribly arduous process that isn't required for a standalone program.

 

I hope I'm making sense here.

0 Likes
Message 9 of 10

smilinger
Advisor
Advisor

If all you need is just to pass information between Forms, there are several ways to do it, one way is to do it like this:

 

Form1:

 

 

Public Class Form1
    Dim _invapp As Inventor.Application = Marshal.GetActiveObject("Inventor.Application")
    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

    '......
    '......    Omission
    '......

    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
        Dim form2 As New Form2(txtOrigDraw.Text, txtOrigPart.Text)
        form2.Show()
    End Sub
End Class

 

 

Form2:

 

 

Public Class Form2
    Public Sub New(drawingText As String, partText As String)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        txtDrawing.Text = drawingText
        txtPart.Text = partText

    End Sub
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
End Class

 

 

The only problem of you is, indeed, you need to get some training of self-learning of VB.net. You are still writing code as if it's VBA.

0 Likes
Message 10 of 10

TA.Fehr
Advocate
Advocate

The data transfer between forms was simply an example to show how each form needed to be initialized in order to be read.

 

After some more searching I realized the difference between my .exe and the add-in was that the add-in application preferences are set to a Class Library application type (as opposed to a Windows form). Once I realized that it got me on the right path as the class library handles forms completely differently than the windows form. I found an article addressing this issue that I'm still working through here.

 

I'm still working through the intracacies of vb.net programming as opposed to VBA so naturally there will always be things to learn. But hopefully this thread will be helpful to someone with similar issues in the future.

 

Thanks all for your input.

0 Likes