在项目工程中可以看到SwAddin.cs文件。这个文件是插件的核心文件,包括插件的名称,注册表项,菜单,以及菜单的回调函数都在该文件中实现。
1.修改插件的名称和描述
Guid为插件生成后注册到注册表的项,由系统自动生成。
Description为插件的描述信息
Title为插件的名称。
修改完成,安装插件后会在注册表中看到如下信息
2.添加插件菜单
添加插件菜单项是在方法“AddCommandMgr()”中,示例代码如下:
public void AddCommandMgr()
{
if (iBmp == null)
iBmp = new BitmapHandler();
Assembly thisAssembly;
int cmdIndex0, cmdIndex1;
string Title = "PDM", ToolTip = "SolidWorks_PDM";
ICommandGroup cmdGroup; int[] docTypes = new int[]{(int)swDocumentTypes_e.swDocASSEMBLY,
(int)swDocumentTypes_e.swDocDRAWING,
(int)swDocumentTypes_e.swDocPART}; thisAssembly = System.Reflection.Assembly.GetAssembly(this.GetType()); int cmdGroupErr = ;
bool ignorePrevious = false; object registryIDs;
//get the ID information stored in the registry
bool getDataResult = iCmdMgr.GetGroupDataFromRegistry(mainCmdGroupID, out registryIDs); int[] knownIDs = new int[] { mainItemID1, mainItemID2 }; if (getDataResult)
{
if (!CompareIDs((int[])registryIDs, knownIDs)) //if the IDs don't match, reset the commandGroup
{
ignorePrevious = true;
}
} cmdGroup = iCmdMgr.CreateCommandGroup2(mainCmdGroupID, Title, ToolTip, "", , ignorePrevious, ref cmdGroupErr);//添加主菜单 cmdGroup.LargeIconList = iBmp.CreateFileFromResourceBitmap("SolidWorks_PDM.ToolbarLarge.bmp", thisAssembly);
cmdGroup.SmallIconList = iBmp.CreateFileFromResourceBitmap("SolidWorks_PDM.ToolbarSmall.bmp", thisAssembly);
cmdGroup.LargeMainIcon = iBmp.CreateFileFromResourceBitmap("SolidWorks_PDM.MainIconLarge.bmp", thisAssembly);
cmdGroup.SmallMainIcon = iBmp.CreateFileFromResourceBitmap("SolidWorks_PDM.MainIconSmall.bmp", thisAssembly); int menuToolbarOption = (int)(swCommandItemType_e.swMenuItem | swCommandItemType_e.swToolbarItem);
//cmdIndex0 = cmdGroup.AddCommandItem2("CreateCube", -1, "Create a cube", "Create cube", 0, "CreateCube", "", mainItemID1, menuToolbarOption);
//cmdIndex1 = cmdGroup.AddCommandItem2("Show PMP", -1, "Display sample property manager", "Show PMP", 2, "ShowPMP", "EnablePMP", mainItemID2, menuToolbarOption); cmdIndex0 = cmdGroup.AddCommandItem2("自定义用户属性", , "填写自定义用户属性", "填写自定义用户属性", , "WriteAttribute","", mainItemID1, menuToolbarOption);//添加主菜单下的子菜单
cmdIndex1 = cmdGroup.AddCommandItem2("写入PDM数据库", , "写入PDM数据库", "写入PDM数据库", , "FillSQL", "", mainItemID2, menuToolbarOption);//添加主菜单下的子菜单
cmdGroup.HasToolbar = true;
cmdGroup.HasMenu = true;
cmdGroup.Activate(); bool bResult; #region
//FlyoutGroup flyGroup = iCmdMgr.CreateFlyoutGroup(flyoutGroupID, "Dynamic Flyout", "Flyout Tooltip", "Flyout Hint",
// cmdGroup.SmallMainIcon, cmdGroup.LargeMainIcon, cmdGroup.SmallIconList, cmdGroup.LargeIconList, "FlyoutCallback", "FlyoutEnable"); //flyGroup.AddCommandItem("FlyoutCommand 1", "test", 0, "FlyoutCommandItem1", "FlyoutEnableCommandItem1"); //flyGroup.FlyoutType = (int)swCommandFlyoutStyle_e.swCommandFlyoutStyle_Simple; foreach (int type in docTypes)
{
CommandTab cmdTab; cmdTab = iCmdMgr.GetCommandTab(type, Title); if (cmdTab != null & !getDataResult | ignorePrevious)//if tab exists, but we have ignored the registry info (or changed command group ID), re-create the tab. Otherwise the ids won't matchup and the tab will be blank
{
bool res = iCmdMgr.RemoveCommandTab(cmdTab);
cmdTab = null;
} //if cmdTab is null, must be first load (possibly after reset), add the commands to the tabs
if (cmdTab == null)
{
cmdTab = iCmdMgr.AddCommandTab(type, Title); CommandTabBox cmdBox = cmdTab.AddCommandTabBox(); int[] cmdIDs = new int[];
int[] TextType = new int[]; cmdIDs[] = cmdGroup.get_CommandID(cmdIndex0); TextType[] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextHorizontal; cmdIDs[] = cmdGroup.get_CommandID(cmdIndex1); TextType[] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextHorizontal; cmdIDs[] = cmdGroup.ToolbarId; TextType[] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextHorizontal | (int)swCommandTabButtonFlyoutStyle_e.swCommandTabButton_ActionFlyout; bResult = cmdBox.AddCommands(cmdIDs, TextType); //CommandTabBox cmdBox1 = cmdTab.AddCommandTabBox();
//cmdIDs = new int[1];
//TextType = new int[1]; //cmdIDs[0] = flyGroup.CmdID;
//TextType[0] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow | (int)swCommandTabButtonFlyoutStyle_e.swCommandTabButton_ActionFlyout; //bResult = cmdBox1.AddCommands(cmdIDs, TextType); //cmdTab.AddSeparator(cmdBox1, cmdIDs[0]); } }
#endregion
thisAssembly = null; }
添加完成之后运行程序即可看到刚刚添加的菜单项
3.回调函数
创建菜单后,由于没有给菜单绑定任何事件,单击不会产生任何效果。于是第二部要做的就是给自定义菜单添加事件,即回调函数。以菜单“自定义用户属性”为例,在添加菜单时有一个参数会指定其回调函数的函数名,如图5.2所示,该处利用反射机制在单击该菜单时调用具有该函数名的函数。
添加相应的回调函数事件
public void WriteAttribute()
{
//ITaskpaneView pTaskPanView;
//pTaskPanView = iSwApp.CreateTaskpaneView2("", "自定义用户属性");
Form1 TaskPanWinFormControl = new Form1();
TaskPanWinFormControl.iSwApp = iSwApp;
TaskPanWinFormControl.ShowDialog(); //pTaskPanView.DisplayWindowFromHandle(TaskPanWinFormControl.Handle.ToInt64());
//TaskPanUserControl = (UserControl1)pTaskPanView.GetControl();
//Debug.Print(TaskPanUserControl.Name);
}
运行程序,单击“自定义属性”,即可看到如下图所示的效果
这样,一个最简单的插件就完成了,最后将插件打包即可在安装有SolidWorks的计算机上使用。