Last active
September 6, 2018 22:48
-
-
Save CADSharp/82b80cd738cb3a997c2579e05a804670 to your computer and use it in GitHub Desktop.
Addin example without CADSharpTools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class Constants | |
Friend Const MenuItem_Command1 As String = "Command 1" | |
Friend Const MenuItem_Command2 As String = "Command 2" | |
Friend Const MenuItem_Command3 As String = "Command 3" | |
Friend Const MenuItem_About As String = "About" | |
Friend Const AboutText As String = "Command 1 is available in part documents." + vbNewLine + | |
"Command 2 is available in part and assembly documents." + vbNewLine + | |
"Command 3 is available in all documents." | |
Friend Const MenuCallback As String = "RunMenuCommands" | |
End Class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports SolidWorks.Interop.swpublished | |
Imports SolidWorks.Interop.sldworks | |
Imports SolidWorks.Interop.swconst | |
Imports System.Runtime.InteropServices | |
<Guid("e2d74c00-b201-4058-98f5-7439c403611f")> <ComVisible(True)> | |
Public Class Main | |
Implements SwAddin | |
Shared _app As SldWorks | |
Friend Shared ReadOnly Property App As SldWorks | |
Get | |
Return _app | |
End Get | |
End Property | |
Public Function ConnectToSW(ThisSW As Object, Cookie As Integer) As Boolean Implements ISwAddin.ConnectToSW | |
_app = ThisSW | |
CreateUI(Cookie) | |
Return True | |
End Function | |
Public Function DisconnectFromSW() As Boolean Implements ISwAddin.DisconnectFromSW | |
RemoveUI() | |
Marshal.ReleaseComObject(_app) | |
GC.Collect() | |
GC.WaitForPendingFinalizers() | |
GC.Collect() | |
GC.WaitForPendingFinalizers() | |
Return True | |
End Function | |
<ComRegisterFunction()> | |
Private Shared Sub RegisterFunction(t As Type) | |
Dim hklm As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine | |
Dim hkcu As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser | |
Dim keyname As String = "SOFTWARE\SolidWorks\Addins\{" + t.GUID.ToString() + "}" | |
Dim addinkey As Microsoft.Win32.RegistryKey = hklm.CreateSubKey(keyname) | |
addinkey.SetValue(Nothing, 1) | |
addinkey.SetValue("Description", "VB.NET addin example") | |
addinkey.SetValue("Title", "VB.NET Addin") | |
keyname = "Software\SolidWorks\AddInsStartup\{" + t.GUID.ToString() + "}" | |
addinkey = hkcu.CreateSubKey(keyname) | |
addinkey.SetValue(Nothing, 1) | |
End Sub | |
<ComUnregisterFunction()> | |
Private Shared Sub UnregisterFunction(t As Type) | |
Dim hkcu As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser | |
Dim keyname As String = "SOFTWARE\SolidWorks\AddinsStartup\{" + t.GUID.ToString() + "}" | |
hkcu.DeleteSubKey(keyname) | |
Dim hklm As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine | |
keyname = "SOFTWARE\SolidWorks\Addins\{" + t.GUID.ToString() + "}" | |
hklm.DeleteSubKey(keyname) | |
End Sub | |
Private Sub CreateUI(cookie As Integer) | |
App.SetAddinCallbackInfo(0, Me, cookie) | |
For i As swDocumentTypes_e = 0 To 3 | |
App.AddMenu(i, Utility.GetProductName(), -1) | |
Select Case i | |
Case swDocumentTypes_e.swDocNONE | |
App.AddMenuItem4(i, cookie, Constants.MenuItem_About + "@" + Utility.GetProductName(), -1, | |
Constants.MenuCallback + "(" + Constants.MenuItem_About + ")", | |
Nothing, Nothing, Nothing) | |
Case swDocumentTypes_e.swDocPART | |
App.AddMenuItem4(i, cookie, Constants.MenuItem_Command1 + "@" + Utility.GetProductName(), -1, | |
Constants.MenuCallback + "(" + Constants.MenuItem_Command1 + ")", | |
Nothing, Nothing, Nothing) | |
App.AddMenuItem4(i, cookie, Constants.MenuItem_Command2 + "@" + Utility.GetProductName(), -1, | |
Constants.MenuCallback + "(" + Constants.MenuItem_Command2 + ")", | |
Nothing, Nothing, Nothing) | |
Case swDocumentTypes_e.swDocASSEMBLY | |
App.AddMenuItem4(i, cookie, Constants.MenuItem_Command2 + "@" + Utility.GetProductName(), -1, | |
Constants.MenuCallback + "(" + Constants.MenuItem_Command2 + ")", | |
Nothing, Nothing, Nothing) | |
Case swDocumentTypes_e.swDocDRAWING | |
App.AddMenuItem4(i, cookie, Constants.MenuItem_Command3 + "@" + Utility.GetProductName(), -1, | |
Constants.MenuCallback + "(" + Constants.MenuItem_Command3 + ")", | |
Nothing, Nothing, Nothing) | |
End Select | |
Next | |
End Sub | |
Private Sub RemoveUI() | |
For i As swDocumentTypes_e = 0 To 3 | |
App.RemoveMenu(i, Utility.GetProductName(), Nothing) | |
Next | |
End Sub | |
Sub RunMenuCommands(data As String) | |
Select Case data | |
Case Constants.MenuItem_Command1 | |
Utility.ShowMessage("Action 1") | |
Case Constants.MenuItem_Command2 | |
Utility.ShowMessage("Action 2") | |
Case Constants.MenuItem_Command3 | |
Utility.ShowMessage("Action 3") | |
Case Constants.MenuItem_About | |
Utility.ShowMessage("Version: " + Utility.GetFileVersion() + vbNewLine + Constants.AboutText) | |
End Select | |
End Sub | |
End Class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.Reflection | |
Imports System.Windows.Forms | |
Class Utility | |
Shared Sub ShowMessage(text As String) | |
MessageBox.Show(text, GetProductName()) | |
End Sub | |
Shared Function GetProductName() As String | |
Return FileVersionInfo.GetVersionInfo(Assembly.GetCallingAssembly().Location).ProductName | |
End Function | |
Shared Function GetFileVersion() As String | |
Return FileVersionInfo.GetVersionInfo(Assembly.GetCallingAssembly().Location).FileVersion | |
End Function | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment