Untitled diff
242 लाइनें
Imports Inventor
Imports Inventor
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports Microsoft.Win32
Namespace InventorAddIn5
Namespace InventorAddIn5
<ProgIdAttribute("InventorAddIn5.StandardAddInServer"),
<ProgIdAttribute("InventorAddIn5.StandardAddInServer"),
GuidAttribute("19467361-dc55-421f-91c6-bfbfecf301df")>
GuidAttribute("19467361-dc55-421f-91c6-bfbfecf301df")>
Public Class StandardAddInServer
Public Class InventorAddinServer
Implements Inventor.ApplicationAddInServer
Implements Inventor.ApplicationAddInServer
Private WithEvents m_uiEvents As UserInterfaceEvents
Private WithEvents m_uiEvents As UserInterfaceEvents
Sub AddPanelToToolsTab()
Sub AddPanelToToolsTab()
' Get the ribbon associated with the part document
' Get the ribbon associated with the part document
Dim oPartRibbon As Ribbon
Dim oPartRibbon As Ribbon
oPartRibbon = ThisApplication.UserInterfaceManager.Ribbons.Item("Part")
oPartRibbon = ThisApplication.UserInterfaceManager.Ribbons.Item("Part")
' Get the "Tools" tab
' Get the "Tools" tab
Dim oTab As RibbonTab
Dim oTab As RibbonTab
oTab = oPartRibbon.RibbonTabs.Item("id_TabTools")
oTab = oPartRibbon.RibbonTabs.Item("id_TabTools")
' Create a panel named "Update", positioned after the "Measure" panel in the Tools tab.
' Create a panel named "Update", positioned after the "Measure" panel in the Tools tab.
Dim oPanel As RibbonPanel
Dim oPanel As RibbonPanel
oPanel = oTab.RibbonPanels.Add("Update", "ToolsTabUpdatePanel", "SampleClientId", "id_PanelP_ToolsMeasure")
oPanel = oTab.RibbonPanels.Add("Update", "ToolsTabUpdatePanel", "SampleClientId", "id_PanelP_ToolsMeasure")
' Get the update commands
' Get the update commands
Dim oDef1 As ButtonDefinition
Dim oDef1 As ButtonDefinition
oDef1 = ThisApplication.CommandManager.ControlDefinitions.Item("AppLocalUpdateCmd")
oDef1 = ThisApplication.CommandManager.ControlDefinitions.Item("AppLocalUpdateCmd")
Dim oDef2 As ButtonDefinition
Dim oDef2 As ButtonDefinition
oDef2 = ThisApplication.CommandManager.ControlDefinitions.Item("AppUpdateMassPropertiesCmd")
oDef2 = ThisApplication.CommandManager.ControlDefinitions.Item("AppUpdateMassPropertiesCmd")
Dim oDefs As ObjectCollection
Dim oDefs As ObjectCollection
oDefs = ThisApplication.TransientObjects.CreateObjectCollection
oDefs = ThisApplication.TransientObjects.CreateObjectCollection
oDefs.Add(oDef1)
oDefs.Add(oDef1)
oDefs.Add(oDef2)
oDefs.Add(oDef2)
' Create a split button control
' Create a split button control
Call oPanel.CommandControls.AddSplitButton(oDef1, oDefs, True)
Call oPanel.CommandControls.AddSplitButton(oDef1, oDefs, True)
' Get the rebuild command
' Get the rebuild command
Dim oDef3 As ButtonDefinition
Dim oDef3 As ButtonDefinition
oDef3 = ThisApplication.CommandManager.ControlDefinitions.Item("AppRebuildAllWrapperCmd")
oDef3 = ThisApplication.CommandManager.ControlDefinitions.Item("AppRebuildAllWrapperCmd")
' Create a button control
' Create a button control
Call oPanel.CommandControls.AddButton(oDef3, True)
Call oPanel.CommandControls.AddButton(oDef3, True)
End Sub
End Sub
#Region "ApplicationAddInServer Members"
#Region "ApplicationAddInServer Members"
' This method is called by Inventor when it loads the AddIn. The AddInSiteObject provides access
' This method is called by Inventor when it loads the AddIn. The AddInSiteObject provides access
' to the Inventor Application object. The FirstTime flag indicates if the AddIn is loaded for
' to the Inventor Application object. The FirstTime flag indicates if the AddIn is loaded for
' the first time. However, with the introduction of the ribbon this argument is always true.
' the first time. However, with the introduction of the ribbon this argument is always true.
Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
' Initialize AddIn members.
' Initialize AddIn members.
g_inventorApplication = addInSiteObject.Application
ThisApplication = addInSiteObject.Application
' Connect to the user-interface events to handle a ribbon reset.
' Connect to the user-interface events to handle a ribbon reset.
m_uiEvents = g_inventorApplication.UserInterfaceManager.UserInterfaceEvents
m_uiEvents = ThisApplication.UserInterfaceManager.UserInterfaceEvents
' TODO: Add button definitions.
' TODO: Add button definitions.
' Sample to illustrate creating a button definition.
' Sample to illustrate creating a button definition.
'Dim largeIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.YourBigImage)
'Dim largeIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.YourBigImage)
'Dim smallIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.YourSmallImage)
'Dim smallIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.YourSmallImage)
'Dim controlDefs As Inventor.ControlDefinitions = g_inventorApplication.CommandManager.ControlDefinitions
'Dim controlDefs As Inventor.ControlDefinitions = g_inventorApplication.CommandManager.ControlDefinitions
'm_sampleButton = controlDefs.AddButtonDefinition("Command Name", "Internal Name", CommandTypesEnum.kShapeEditCmdType, AddInClientID)
'm_sampleButton = controlDefs.AddButtonDefinition("Command Name", "Internal Name", CommandTypesEnum.kShapeEditCmdType, AddInClientID)
' Add to the user interface, if it's the first time.
' Add to the user interface, if it's the first time.
'If firstTime Then
If firstTime Then
'AddToUserInterface()
AddToUserInterface()
'End If
End If
End Sub
End Sub
' This method is called by Inventor when the AddIn is unloaded. The AddIn will be
' This method is called by Inventor when the AddIn is unloaded. The AddIn will be
' unloaded either manually by the user or when the Inventor session is terminated.
' unloaded either manually by the user or when the Inventor session is terminated.
Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate
Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate
' TODO: Add ApplicationAddInServer.Deactivate implementation
' TODO: Add ApplicationAddInServer.Deactivate implementation
' Release objects.
' Release objects.
m_uiEvents = Nothing
m_uiEvents = Nothing
g_inventorApplication = Nothing
ThisApplication = Nothing
System.GC.Collect()
System.GC.Collect()
System.GC.WaitForPendingFinalizers()
System.GC.WaitForPendingFinalizers()
End Sub
End Sub
' This property is provided to allow the AddIn to expose an API of its own to other
' This property is provided to allow the AddIn to expose an API of its own to other
' programs. Typically, this would be done by implementing the AddIn's API
' programs. Typically, this would be done by implementing the AddIn's API
' interface in a class and returning that class object through this property.
' interface in a class and returning that class object through this property.
Public ReadOnly Property Automation() As Object Implements Inventor.ApplicationAddInServer.Automation
Public ReadOnly Property Automation() As Object Implements Inventor.ApplicationAddInServer.Automation
Get
Get
Return Nothing
Return Nothing
End Get
End Get
End Property
End Property
Public Property ThisApplication As Object
'Public Property ThisApplication As Object
' Note:this method is now obsolete, you should use the
' Note:this method is now obsolete, you should use the
' ControlDefinition functionality for implementing commands.
' ControlDefinition functionality for implementing commands.
Public Sub ExecuteCommand(ByVal commandID As Integer) Implements Inventor.ApplicationAddInServer.ExecuteCommand
Public Sub ExecuteCommand(ByVal commandID As Integer) Implements Inventor.ApplicationAddInServer.ExecuteCommand
End Sub
End Sub
#End Region
#End Region
#Region "User interface definition"
#Region "User interface definition"
' Sub where the user-interface creation is done. This is called when
' Sub where the user-interface creation is done. This is called when
' the add-in loaded and also if the user interface is reset.
' the add-in loaded and also if the user interface is reset.
Private Sub AddToUserInterface()
Private Sub AddToUserInterface()
AddPanelToToolsTab()
AddPanelToToolsTab()
End Sub
End Sub
Private Sub m_uiEvents_OnResetRibbonInterface(Context As NameValueMap) Handles m_uiEvents.OnResetRibbonInterface
Private Sub m_uiEvents_OnResetRibbonInterface(Context As NameValueMap) Handles m_uiEvents.OnResetRibbonInterface
' The ribbon was reset, so add back the add-ins user-interface.
' The ribbon was reset, so add back the add-ins user-interface.
AddToUserInterface()
AddToUserInterface()
End Sub
End Sub
#End Region
#End Region
End Class
End Class
End Namespace
End Namespace
Public Module Globals
Public Module Globals
' Inventor application object.
' Inventor application object.
Public g_inventorApplication As Inventor.Application
Public ThisApplication As Inventor.Application
#Region "Function to get the add-in client ID."
#Region "Function to get the add-in client ID."
' This function uses reflection to get the GuidAttribute associated with the add-in.
' This function uses reflection to get the GuidAttribute associated with the add-in.
Public Function AddInClientID() As String
Public Function AddInClientID() As String
Dim guid As String = ""
Dim guid As String = ""
Try
Try
Dim t As Type = GetType(InventorAddIn5.StandardAddInServer)
Dim t As Type = GetType(InventorAddIn5.InventorAddinServer)
Dim customAttributes() As Object = t.GetCustomAttributes(GetType(GuidAttribute), False)
Dim customAttributes() As Object = t.GetCustomAttributes(GetType(GuidAttribute), False)
Dim guidAttribute As GuidAttribute = CType(customAttributes(0), GuidAttribute)
Dim guidAttribute As GuidAttribute = CType(customAttributes(0), GuidAttribute)
guid = "{" + guidAttribute.Value.ToString() + "}"
guid = "{" + guidAttribute.Value.ToString() + "}"
Catch
Catch
End Try
End Try
Return guid
Return guid
End Function
End Function
#End Region
#End Region
#Region "hWnd Wrapper Class"
#Region "hWnd Wrapper Class"
' This class is used to wrap a Win32 hWnd as a .Net IWind32Window class.
' This class is used to wrap a Win32 hWnd as a .Net IWind32Window class.
' This is primarily used for parenting a dialog to the Inventor window.
' This is primarily used for parenting a dialog to the Inventor window.
'
'
' For example:
' For example:
' myForm.Show(New WindowWrapper(g_inventorApplication.MainFrameHWND))
' myForm.Show(New WindowWrapper(g_inventorApplication.MainFrameHWND))
'
'
Public Class WindowWrapper
Public Class WindowWrapper
Implements System.Windows.Forms.IWin32Window
Implements System.Windows.Forms.IWin32Window
Public Sub New(ByVal handle As IntPtr)
Public Sub New(ByVal handle As IntPtr)
_hwnd = handle
_hwnd = handle
End Sub
End Sub
Public ReadOnly Property Handle() As IntPtr _
Public ReadOnly Property Handle() As IntPtr _
Implements System.Windows.Forms.IWin32Window.Handle
Implements System.Windows.Forms.IWin32Window.Handle
Get
Get
Return _hwnd
Return _hwnd
End Get
End Get
End Property
End Property
Private _hwnd As IntPtr
Private _hwnd As IntPtr
End Class
End Class
#End Region
#End Region
#Region "Image Converter"
#Region "Image Converter"
' Class used to convert bitmaps and icons from their .Net native types into
' Class used to convert bitmaps and icons from their .Net native types into
' an IPictureDisp object which is what the Inventor API requires. A typical
' an IPictureDisp object which is what the Inventor API requires. A typical
' usage is shown below where MyIcon is a bitmap or icon that's available
' usage is shown below where MyIcon is a bitmap or icon that's available
' as a resource of the project.
' as a resource of the project.
'
'
' Dim smallIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.MyIcon)
' Dim smallIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.MyIcon)
Public NotInheritable Class PictureDispConverter
Public NotInheritable Class PictureDispConverter
<DllImport("OleAut32.dll", EntryPoint:="OleCreatePictureIndirect", ExactSpelling:=True, PreserveSig:=False)> _
<DllImport("OleAut32.dll", EntryPoint:="OleCreatePictureIndirect", ExactSpelling:=True, PreserveSig:=False)> _
Private Shared Function OleCreatePictureIndirect( _
Private Shared Function OleCreatePictureIndirect( _
<MarshalAs(UnmanagedType.AsAny)> ByVal picdesc As Object, _
<MarshalAs(UnmanagedType.AsAny)> ByVal picdesc As Object, _
ByRef iid As Guid, _
ByRef iid As Guid, _
<MarshalAs(UnmanagedType.Bool)> ByVal fOwn As Boolean) As stdole.IPictureDisp
<MarshalAs(UnmanagedType.Bool)> ByVal fOwn As Boolean) As stdole.IPictureDisp
End Function
End Function
Shared iPictureDispGuid As Guid = GetType(stdole.IPictureDisp).GUID
Shared iPictureDispGuid As Guid = GetType(stdole.IPictureDisp).GUID
Private NotInheritable Class PICTDESC
Private NotInheritable Class PICTDESC
Private Sub New()
Private Sub New()
End Sub
End Sub
'Picture Types
'Picture Types
Public Const PICTYPE_BITMAP As Short = 1
Public Const PICTYPE_BITMAP As Short = 1
Public Const PICTYPE_ICON As Short = 3
Public Const PICTYPE_ICON As Short = 3
<StructLayout(LayoutKind.Sequential)> _
<StructLayout(LayoutKind.Sequential)> _
Public Class Icon
Public Class Icon
Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Icon))
Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Icon))
Friend picType As Integer = PICTDESC.PICTYPE_ICON
Friend picType As Integer = PICTDESC.PICTYPE_ICON
Friend hicon As IntPtr = IntPtr.Zero
Friend hicon As IntPtr = IntPtr.Zero
Friend unused1 As Integer
Friend unused1 As Integer
Friend unused2 As Integer
Friend unused2 As Integer
Friend Sub New(ByVal icon As System.Drawing.Icon)
Friend Sub New(ByVal icon As System.Drawing.Icon)
Me.hicon = icon.ToBitmap().GetHicon()
Me.hicon = icon.ToBitmap().GetHicon()
End Sub
End Sub
End Class
End Class
<StructLayout(LayoutKind.Sequential)> _
<StructLayout(LayoutKind.Sequential)> _
Public Class Bitmap
Public Class Bitmap
Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Bitmap))
Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Bitmap))
Friend picType As Integer = PICTDESC.PICTYPE_BITMAP
Friend picType As Integer = PICTDESC.PICTYPE_BITMAP
Friend hbitmap As IntPtr = IntPtr.Zero
Friend hbitmap As IntPtr = IntPtr.Zero
Friend hpal As IntPtr = IntPtr.Zero
Friend hpal As IntPtr = IntPtr.Zero
Friend unused As Integer
Friend unused As Integer
Friend Sub New(ByVal bitmap As System.Drawing.Bitmap)
Friend Sub New(ByVal bitmap As System.Drawing.Bitmap)
Me.hbitmap = bitmap.GetHbitmap()
Me.hbitmap = bitmap.GetHbitmap()
End Sub
End Sub
End Class
End Class
End Class
End Class
Public Shared Function ToIPictureDisp(ByVal icon As System.Drawing.Icon) As stdole.IPictureDisp
Public Shared Function ToIPictureDisp(ByVal icon As System.Drawing.Icon) As stdole.IPictureDisp
Dim pictIcon As New PICTDESC.Icon(icon)
Dim pictIcon As New PICTDESC.Icon(icon)
Return OleCreatePictureIndirect(pictIcon, iPictureDispGuid, True)
Return OleCreatePictureIndirect(pictIcon, iPictureDispGuid, True)
End Function
End Function
Public Shared Function ToIPictureDisp(ByVal bmp As System.Drawing.Bitmap) As stdole.IPictureDisp
Public Shared Function ToIPictureDisp(ByVal bmp As System.Drawing.Bitmap) As stdole.IPictureDisp
Dim pictBmp As New PICTDESC.Bitmap(bmp)
Dim pictBmp As New PICTDESC.Bitmap(bmp)
Return OleCreatePictureIndirect(pictBmp, iPictureDispGuid, True)
Return OleCreatePictureIndirect(pictBmp, iPictureDispGuid, True)
End Function
End Function
End Class
End Class
#End Region
#End Region
End Module
End Module