Best Practices for making custom Addin Commands

Best Practices for making custom Addin Commands

MistakenEngineer
Enthusiast Enthusiast
382 Views
1 Reply
Message 1 of 2

Best Practices for making custom Addin Commands

MistakenEngineer
Enthusiast
Enthusiast

I've been making lots of VBA code and want to switch to addins. I am a mechanical engineer and not a programmer and I want to know if I am going about executing my custom commands correctly. I have already added a button to the ribbon via the tutorials and taken care of the GUI and have no problems there. What I want to know is from a high level standpoint, am I implementing the code correctly?

 

Here is the sub-module that is fired when the button is clicked

Private Sub NewCommand_OnExecute(Context As NameValueMap) Handles NewCommand.OnExecute
	' Error Checking
	' Code that checks if it is the right document, right environment, right selection, no other code running, etc.
	
	' Create a change processor and execute it. (Code not shown)
	
	'  Fire the change processor
	            Try
			' This fires a sub module. Every action or edit that is done within this sub-module to inventor is wrapped within one undo transaction
		           m_ChangeProcessor.Execute(g_inventorApplication.ActiveEditDocument)
		
	            Catch ex As Exception
			' This code is fired if the m_ChangeProcessor.Execute sub module's Succeeded argument is set to false
	            End Try
End Sub

This then fires this:

Private Sub m_ChangeProcessor_OnExecute(Document As _Document, Context As NameValueMap, ByRef Succeeded As Boolean) Handles m_ChangeProcessor.OnExecute
	' Error Checking
	' Code that checks if the right things are selected
	
	
	' Create a new dialog
	m_dialog = New iMateEditorDialog(g_inventorApplication.ActiveEditDocument)
	m_dialog.Show(New WindowWrapper(g_inventorApplication.MainFrameHWND))
	
	' This loop waits for the dialog to close. I do this so that all the actions done by the dialog can be undone
	Do While m_dialog.IsFinished = False ' Custom property within the dialog that fires when closed
	            g_inventorApplication.UserInterfaceManager.DoEvents()
	Loop
	
	' If the command was cancelled or interupted then set Succeeded to false which will then abort the transaction and throw an error
	If m_dialog.WasProblem = True Then
		Succeeded = False
	Else
		Succeeded = True
	End If
	
	' Code for clean up
	
End Sub

 

Basically, when the command is executed, I create a changeProcessor that does work on Inventor through a dialog. Because I add the DoEvents loop, it effectively makes this a synchronous command. I did this instead of creating transactions to support undo because:

  • More robust?
  • no transaction rules to follow
    • Ex. can close documents and open documents

Also, some of my custom commands run InteractionEvents and some do not because the InteractionEvents can be quite limiting for some commands.

 

The problem is that I have heard that you shouldn't be running DoEvents within VB.net code. Is what I am doing correct or should I do something different?

0 Likes
383 Views
1 Reply
Reply (1)
Message 2 of 2

BrianEkins
Mentor
Mentor

Personally, I wouldn't bother using the change processor functionality.  It adds a lot of complexity for probably no benefit for you.  The main thing you get from the ChangeProcessor is that it groups a series of API calls into a single transaction that the user can undo.  It's much simpler to use the functionality on the TransactionManager object to do the same thing.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes