Inventor API provides many events in various objects. The Sketch Events can be used to monitor Sketch activities such as sketch change. In this post, let’s see how to manipulate these Inventor Sketch Events using .NET, particularly VB.NET here.
We create the event handlers for each available Inventor Sketch Event and provide registers and un-registers like the following:
Imports System
Imports System.Text
Imports System.Linq
Imports System.Xml
Imports System.Reflection
Imports System.ComponentModel
Imports System.Collections
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Media.Imaging
Imports System.Windows.Forms
Imports System.IO
Imports Microsoft.Win32
Imports System.Runtime.InteropServices
Imports Inventor
Public Class SketchEventHanlder1
Dim mEventsObj As SketchEvents
Public Sub New(ByVal obj As SketchEvents)
mEventsObj = obj
End Sub
Public Sub Register()
AddHandler mEventsObj.OnSketchChange, AddressOf SketchEvents_OnSketchChange_Handler
AddHandler mEventsObj.OnSketch3DSolve, AddressOf SketchEvents_OnSketch3DSolve_Handler
AddHandler mEventsObj.OnSketch3DChange, AddressOf SketchEvents_OnSketch3DChange_Handler
AddHandler mEventsObj.OnNewSketch3D, AddressOf SketchEvents_OnNewSketch3D_Handler
AddHandler mEventsObj.OnNewSketch, AddressOf SketchEvents_OnNewSketch_Handler
AddHandler mEventsObj.OnDelete, AddressOf SketchEvents_OnDelete_Handler
End Sub
Public Sub UnRegister()
RemoveHandler mEventsObj.OnSketchChange, AddressOf SketchEvents_OnSketchChange_Handler
RemoveHandler mEventsObj.OnSketch3DSolve, AddressOf SketchEvents_OnSketch3DSolve_Handler
RemoveHandler mEventsObj.OnSketch3DChange, AddressOf SketchEvents_OnSketch3DChange_Handler
RemoveHandler mEventsObj.OnNewSketch3D, AddressOf SketchEvents_OnNewSketch3D_Handler
RemoveHandler mEventsObj.OnNewSketch, AddressOf SketchEvents_OnNewSketch_Handler
RemoveHandler mEventsObj.OnDelete, AddressOf SketchEvents_OnDelete_Handler
End Sub
Public Sub SketchEvents_OnDelete_Handler(ByVal DocumentObject As Inventor._Document, ByVal Entity As Object, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum)
MessageBox.Show("OnDelete")
End Sub
Public Sub SketchEvents_OnNewSketch_Handler(ByVal DocumentObject As Inventor._Document, ByVal Sketch As Inventor.Sketch, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum)
MessageBox.Show("OnNewSketch")
End Sub
Public Sub SketchEvents_OnNewSketch3D_Handler(ByVal DocumentObject As Inventor._Document, ByVal Sketch3D As Inventor.Sketch3D, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum)
MessageBox.Show("OnNewSketch3D")
End Sub
Public Sub SketchEvents_OnSketch3DChange_Handler(ByVal DocumentObject As Inventor._Document, ByVal Sketch3D As Inventor.Sketch3D, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum)
MessageBox.Show("OnSketch3DChange")
End Sub
Public Sub SketchEvents_OnSketch3DSolve_Handler(ByVal DocumentObject As Inventor._Document, ByVal Sketch As Object, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum)
MessageBox.Show("OnSketch3DSolve")
End Sub
Public Sub SketchEvents_OnSketchChange_Handler(ByVal DocumentObject As Inventor._Document, ByVal Sketch As Inventor.Sketch, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum)
MessageBox.Show("OnSketchChange")
End Sub
End Class
With all the even handlers put together in a central class and the register and un-register methods it provides, things become all straightforward now. We create an instance of the Sketch event handler, register all those events through calling the Register() method of the handler instance in the Activate implementation, and un-register them accordingly through calling the UnRegister() method in the Deactivate() implementation of the Inventor Addin Server.
Now we are ready to go. We make the Inventor assembly loadable through creating an addin manifest file, deploy it to the right place, launch Inventor, and play around the Transaction events that are just registered as above.
The Inventor .NET Addin Wizard (InventorNetAddinWizard) can generate Inventor .NET addin projects automatically including addin server interface implementations, COM registry free treatment, a nice sample Ribbon Button, and manifest file creation and deployment. Right after that, the Inventor addin assembly is ready to be launched by the specified Inventor and debugged by the Visual Studio Debugger simply through pressing the F5 key.
The Inventor .NET Addin Wizard (InventorNetAddinWizard) also provides item wizards to help generate various event handlers including the Sketch even handler as demonstrated here automatically and reliably.
The list of its cool features and the link to the installer download can be found from the A Wizard for Inventor .NET Addin page.
Posted by: |