After looking for a good way of doing a custom Autocad toolbar for our application, the solution I got it was a mix of some solutions I found in the web. In few words, what you need is a Autocad customization file (*.cui) with your personalized toolbar and icon files. Create this file and create a command in your application to upload it and Autocad will take care about the rest for you as its own toolbar. Doing this way, you need to load the toolbar just once and do not wore about taking care of it.
An easy way to create a new *.cui file is typing "cui" in the Autocad command line. In the Cutomize User Interface window, go to the Transfer tab and create you file. TIP: to run your command without the need of typing return put "_" before it. (eg. OPENWINDOW = _OPENWINDOW)
After that, use the code below to upload the *.cui file and find the your icons files and copy them to the right location.
NOTE: You'll need to add AcCui.dll as an assembly reference to your project for this code to build.
[CommandMethod("SHOWTOOLBAR")]
public void cmdShowToolbar()
{
try
{
//Get the default path for the icons files
DirectoryInfo dir = new DirectoryInfo(Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("*_TOOLPALETTEPATH").ToString());
dir = new DirectoryInfo(dir.Parent.FullName + "\\Icons");
if (dir.Exists)
{
//GetIcon files path and copy them to the default Autocad icon path
DirectoryInfo dirIcons = new DirectoryInfo("C:\Icons");
FileInfo[] icons = dirIcons.GetFiles("*.bmp");
foreach (FileInfo icon in icons)
icon.CopyTo(dir.ToString() + "\\" + icon.Name, true);
string myCuiFile = "C:\MyFile.cui";
string myCuiFileToSend = "C:/MyFile.cui";
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
string mainCui = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("MENUNAME") + ".cui";
CustomizationSection cs = new CustomizationSection(mainCui);
PartialCuiFileCollection pcfc = cs.PartialCuiFiles;
//Check if the *.cui file is already loaded, if not check if it exists and then load it
if (pcfc.Contains(myCuiFile))
{
ed.WriteMessage("\nCustomization file \"" + myCuiFile + "\" already loaded.");
}
else
{
if (System.IO.File.Exists(myCuiFile))
{
ed.WriteMessage("\nCustomization file \"" + myCuiFile + "\" exists - loading it.");
LoadMyCui(myCuiFileToSend);
}
}
}
}
catch (System.Exception ex)
{
MessageBox.Show("Error loading toolbar.\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void LoadMyCui(string cuiFile)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
object oldCmdEcho = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("CMDECHO");
object oldFileDia = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("FILEDIA");
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CMDECHO", 0);
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("FILEDIA", 0);
doc.SendStringToExecute("_.cuiload \"" + cuiFile + "\" ", false, false, false);
doc.SendStringToExecute("(setvar \"FILEDIA\" " + oldFileDia.ToString() + ")(princ) ", false, false, false);
doc.SendStringToExecute("(setvar \"CMDECHO\" " + oldCmdEcho.ToString() + ")(princ) ", false, false, false);
}
This code based in the "Creating a partial CUI file using .NET and loading it inside AutoCAD" post from 'Through The Interface' blog. (http://through-the-interface.typepad.com/through_the_interface/2007/05/creating_a_part.html) and others information found on the web.