在項目工程中可以看到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 = 0; bool ignorePrevious = false; object registryIDs; //get the ID information stored in the registry bool getDataResult = iCmdMgr.GetGroupDataFromRegistry(mainCmdGroupID, out registryIDs); int[] knownIDs = new int[2] { 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, "", 5, 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("自定義用戶屬性", 1, "填寫自定義用戶屬性", "填寫自定義用戶屬性", 0, "WriteAttribute","", mainItemID1, menuToolbarOption);//添加主菜單下的子菜單
cmdIndex1 = cmdGroup.AddCommandItem2("寫入PDM數據庫", 2, "寫入PDM數據庫", "寫入PDM數據庫", 1, "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[3]; int[] TextType = new int[3]; cmdIDs[0] = cmdGroup.get_CommandID(cmdIndex0); TextType[0] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextHorizontal; cmdIDs[1] = cmdGroup.get_CommandID(cmdIndex1); TextType[1] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextHorizontal; cmdIDs[2] = cmdGroup.ToolbarId; TextType[2] = (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的計算機上使用。