Add Toolbars Example (C#)
This example shows how to use an add-in to add toolbars to a part document.
//--------------------------------------------------------------------------
// Preconditions:
// 1. Verify that the SOLIDWORKS API SDK add-in templates for your version
// of Microsoft Visual Studio are installed.
// 2. In Microsoft Visual Studio, create a C# project using the
// SwCSharpAddin template.
// 3. Name the project AddToolbarCSharp.
// 4. Copy and paste SwAddin into SwAddin.cs of your project.
// 5. Click Project > AddToolbarCSharp Properties > Debug.
// 6. Select Start external program and type the pathname of your
// SOLIDWORKS executable.
// 7. Replace the toolbarImages array elements with the pathnames of
// your image files.
// 8. Press F5 to start debugging this add-in.
//
// Postconditions:
// 1. In SOLIDWORKS, click File > New > Part > OK.
// 2. Click the toolbar button located below the main menu.
// 3. Click OK to close the message box.
// 4. Click Tools > Customize.
// a. Select a different icon size in Icon size.
// b. Click OK three times.
// c. Click the toolbar button located below the main menu.
// d. Click OK to close the message box.
// 5. Repeat step 4, but select a different icon size for step 4a.
//---------------------------------------------------------------------------
//SwAddin
using System;
using System.Runtime.InteropServices;
using System.Collections;
using System.Reflection;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swpublished;
using SolidWorks.Interop.swconst;
using SolidWorksTools;
using SolidWorksTools.File;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
namespace AddToolbarCSharp
{
/// <summary>
/// Summary description for AddToolbarCSharp
/// </summary>
[Guid("d05b35ff-9a43-40ed-9c02-ebb886be116d"), ComVisible(true)]
[SwAddin(
Description = "AddToolbarCSharp description",
Title = "AddToolbarCSharp",
LoadAtStartup = true
)]
public class SwAddin : ISwAddin
{
#region Local Variables
ISldWorks iSwApp = null;
int addinID = 0;
BitmapHandler iBmp;
#region Event Handler Variables
Hashtable openDocs = new Hashtable();
SolidWorks.Interop.sldworks.SldWorks SwEventPtr = null;
#endregion
// Public properties
public ISldWorks SwApp
{
get { return iSwApp; }
}
public Hashtable OpenDocs
{
get { return openDocs; }
}
#endregion
#region SolidWorks Registration
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type t)
{
#region Get Custom Attribute: SwAddinAttribute
SwAddinAttribute SWattr = null;
Type type = typeof(SwAddin);
foreach (System.Attribute attr in type.GetCustomAttributes(false))
{
if (attr is SwAddinAttribute)
{
SWattr = attr as SwAddinAttribute;
break;
}
}
#endregion
try
{
Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser;
string keyname = "SOFTWARE\\SolidWorks\\Addins\\{" + t.GUID.ToString() + "}";
Microsoft.Win32.RegistryKey addinkey = hklm.CreateSubKey(keyname);
addinkey.SetValue(null, 0);
addinkey.SetValue("Description", SWattr.Description);
addinkey.SetValue("Title", SWattr.Title);
keyname = "Software\\SolidWorks\\AddInsStartup\\{" + t.GUID.ToString() + "}";
addinkey = hkcu.CreateSubKey(keyname);
addinkey.SetValue(null, Convert.ToInt32(SWattr.LoadAtStartup), Microsoft.Win32.RegistryValueKind.DWord);
}
catch (System.NullReferenceException nl)
{
Console.WriteLine("There was a problem registering this dll: SWattr is null. \n\"" + nl.Message + "\"");
System.Windows.Forms.MessageBox.Show("There was a problem registering this dll: SWattr is null.\n\"" + nl.Message + "\"");
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
System.Windows.Forms.MessageBox.Show("There was a problem registering the function: \n\"" + e.Message + "\"");
}
}
[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type t)
{
try
{
Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser;
string keyname = "SOFTWARE\\SolidWorks\\Addins\\{" + t.GUID.ToString() + "}";
hklm.DeleteSubKey(keyname);
keyname = "Software\\SolidWorks\\AddInsStartup\\{" + t.GUID.ToString() + "}";
hkcu.DeleteSubKey(keyname);
}
catch (System.NullReferenceException nl)
{
Console.WriteLine("There was a problem unregistering this dll: " + nl.Message);
System.Windows.Forms.MessageBox.Show("There was a problem unregistering this dll: \n\"" + nl.Message + "\"");
}
catch (System.Exception e)
{
Console.WriteLine("There was a problem unregistering this dll: " + e.Message);
System.Windows.Forms.MessageBox.Show("There was a problem unregistering this dll: \n\"" + e.Message + "\"");
}
}
#endregion
#region ISwAddin Implementation
public SwAddin()
{
}
public bool ConnectToSW(object ThisSW, int cookie)
{
iSwApp = (ISldWorks)ThisSW;
addinID = cookie;
// Set up callbacks
iSwApp.SetAddinCallbackInfo2(0, this, addinID);
#region Set up the toolbar
AddToolBar();
#endregion
#region Set up the Event Handlers
SwEventPtr = (SolidWorks.Interop.sldworks.SldWorks)iSwApp;
openDocs = new Hashtable();
AttachEventHandlers();
#endregion
return true;
}
public bool DisconnectFromSW()
{
DetachEventHandlers();
System.Runtime.InteropServices.Marshal.ReleaseComObject(iSwApp);
iSwApp = null;
// The add-in must call GC.Collect() here to retrieve all managed code pointers
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
return true;
}
#endregion
#region UI Methods
// Add toolbar using ISldWorks::AddToolBar5 method
public void AddToolBar()
{
Assembly thisAssembly1;
string[] toolbarImages = new string[3];
bool bret = false;
int iToolbarId = 0;
thisAssembly1 = System.Reflection.Assembly.GetAssembly(this.GetType());
toolbarImages[0] = "Pathname_to_toolbar_nxn_image";
toolbarImages[1] = "Pathname_to_toolbar_nnxnn_image";
toolbarImages[2] = "Pathname_to_toolbar_nnnxnnn_image";
iToolbarId = iSwApp.AddToolbar5(addinID, "Test Toolbar", toolbarImages, 0, (int)swDocTemplateTypes_e.swDocTemplateTypePART);
bret = iSwApp.AddToolbarCommand2(addinID, iToolbarId, 0, "ButtonCallback", "ButtonEnableMethod", "Test toolbar ToolTip", "Hint string for test toolbar");
}
public void ButtonCallback()
{
MessageBox.Show("Button callback function called.");
}
public int ButtonEnableMethod()
{
return 1;
}
public bool CompareIDs(int[] storedIDs, int[] addinIDs)
{
List<int> storedList = new List<int>(storedIDs);
List<int> addinList = new List<int>(addinIDs);
addinList.Sort();
storedList.Sort();
if (addinList.Count != storedList.Count)
{
return false;
}
else
{
for (int i = 0; i < addinList.Count; i++)
{
if (addinList[i] != storedList[i])
{
return false;
}
}
}
return true;
}
#endregion
#region Event Methods
public bool AttachEventHandlers()
{
AttachSwEvents();
// Listen for events on all currently open documents
AttachEventsToAllDocuments();
return true;
}
private bool AttachSwEvents()
{
try
{
SwEventPtr.ActiveDocChangeNotify += new DSldWorksEvents_ActiveDocChangeNotifyEventHandler(ActiveDocChangeNotify);
SwEventPtr.DocumentLoadNotify2 += new DSldWorksEvents_DocumentLoadNotify2EventHandler(DocumentLoadNotify2);
SwEventPtr.FileNewNotify2 += new DSldWorksEvents_FileNewNotify2EventHandler(FileNewNotify2);
SwEventPtr.ActiveModelDocChangeNotify += new DSldWorksEvents_ActiveModelDocChangeNotifyEventHandler(ActiveModelDocChangeNotify);
SwEventPtr.FileOpenPostNotify += new DSldWorksEvents_FileOpenPostNotifyEventHandler(FileOpenPostNotify);
SwEventPtr.FileOpenPreNotify += new DSldWorksEvents_FileOpenPreNotifyEventHandler(FileOpenPreNotify);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
private bool DetachSwEvents()
{
try
{
SwEventPtr.ActiveDocChangeNotify -= new DSldWorksEvents_ActiveDocChangeNotifyEventHandler(ActiveDocChangeNotify);
SwEventPtr.DocumentLoadNotify2 -= new DSldWorksEvents_DocumentLoadNotify2EventHandler(DocumentLoadNotify2);
SwEventPtr.FileNewNotify2 -= new DSldWorksEvents_FileNewNotify2EventHandler(FileNewNotify2);
SwEventPtr.ActiveModelDocChangeNotify -= new DSldWorksEvents_ActiveModelDocChangeNotifyEventHandler(ActiveModelDocChangeNotify);
SwEventPtr.FileOpenPostNotify -= new DSldWorksEvents_FileOpenPostNotifyEventHandler(FileOpenPostNotify);
SwEventPtr.FileOpenPreNotify += new DSldWorksEvents_FileOpenPreNotifyEventHandler(FileOpenPreNotify);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
public void AttachEventsToAllDocuments()
{
ModelDoc2 modDoc = (ModelDoc2)iSwApp.GetFirstDocument();
while (modDoc != null)
{
if (!openDocs.Contains(modDoc))
{
AttachModelDocEventHandler(modDoc);
}
else if (openDocs.Contains(modDoc))
{
bool connected = false;
DocumentEventHandler docHandler = (DocumentEventHandler)openDocs[modDoc];
if (docHandler != null)
{
connected = docHandler.ConnectModelViews();
}
}
modDoc = (ModelDoc2)modDoc.GetNext();
}
}
public bool AttachModelDocEventHandler(ModelDoc2 modDoc)
{
if (modDoc == null)
return false;
DocumentEventHandler docHandler = null;
if (!openDocs.Contains(modDoc))
{
switch (modDoc.GetType())
{
case (int)swDocumentTypes_e.swDocPART:
{
docHandler = new PartEventHandler(modDoc, this);
break;
}
case (int)swDocumentTypes_e.swDocASSEMBLY:
{
docHandler = new AssemblyEventHandler(modDoc, this);
break;
}
case (int)swDocumentTypes_e.swDocDRAWING:
{
docHandler = new DrawingEventHandler(modDoc, this);
break;
}
default:
{
// Unsupported document type
return false;
}
}
docHandler.AttachEventHandlers();
openDocs.Add(modDoc, docHandler);
}
return true;
}
public bool DetachModelEventHandler(ModelDoc2 modDoc)
{
DocumentEventHandler docHandler;
docHandler = (DocumentEventHandler)openDocs[modDoc];
openDocs.Remove(modDoc);
modDoc = null;
docHandler = null;
return true;
}
public bool DetachEventHandlers()
{
DetachSwEvents();
// Close events on all currently open documents
DocumentEventHandler docHandler;
int numKeys = openDocs.Count;
object[] keys = new Object[numKeys];
// Remove all document event handlers
openDocs.Keys.CopyTo(keys, 0);
foreach (ModelDoc2 key in keys)
{
docHandler = (DocumentEventHandler)openDocs[key];
// Remove the pair from the hash
docHandler.DetachEventHandlers();
docHandler = null;
}
return true;
}
#endregion
#region Event Handlers
//Events
public int ActiveDocChangeNotify()
{
return 0;
}
public int DocumentLoadNotify2(string docTitle, string docPath)
{
return 0;
}
int FileOpenPostNotify(string FileName)
{
AttachEventsToAllDocuments();
return 0;
}
int FileOpenPreNotify(String FileName)
{
return 0;
}
public int FileNewNotify2(object newDoc, int docType, string templateName)
{
AttachEventsToAllDocuments();
return 0;
}
public int ActiveModelDocChangeNotify()
{
return 0;
}
#endregion
}
}