上一篇我詳細講解了如何創(chuàng)建一個插件,但是無界面無按鈕,這種插件適合配合事件偷偷的在后臺做點什么事情。今天這篇講一下如何增加一些按鈕到工具欄、菜單上去。
先告訴大家這個東西注冊表在哪,因為solidworks在這方面做的不太好,插件你改個名字,就有多個工具欄在solidworks上面了,如果后面有些沒用的工具欄名稱,它的圖標還會亂跑,就可以到這里面去刪除沒用的名稱。
準備圖標
有菜單,肯定要圖標,也方便用戶更好的識別。
這里我就拿之前demo中的示例圖標了
在項目目錄下新建一個icons的文件夾,把圖標放進來,高版本是推薦這6種圖標的,舊版api示例中是大圖標和兩圖標2種格式就好了,當然代碼寫法也有點區(qū)別:
mainicon是插件的主圖標,用在工具條顯示的的前面
還有個地方顯示的,但官方的圖標沒有顯示,應該是因為圖標位深(8的可以顯示,32的顯示不了)的問題,具體沒研究,而且插件名稱中如果有.,那么在dll中資源識別的時候會導致名稱不一樣,solidwroks也不能識別插件的圖標。
然后
再設置為如果較新則復制
這樣,我們再次生成插件的時候,這些圖標就會復制到輸出的目錄中區(qū),當然大家也可以按api幫助中的把這些設置為資源文件。
我這樣是方便我換圖標。
代碼處理
先增加幾個私用的變量,用于命令管理 插件CookieID ,工具組ID 以及命令id數(shù)組,還有圖標的路徑。
private ICommandManager iCmdMgr = null;
/// <summary>
/// 插件cookie
/// </summary>
private int addinCookieID;
public int mainCmdGroupID = 5001;
//本示例只有3個命名,三個圖標。
public int[] mainItemIds = new int[3] { 1002, 1003, 1004 };
/// <summary>
/// 主圖標的6種尺寸
/// </summary>
private string[] mainIcons = new string[6];
/// <summary>
/// 工具欄圖標帶6種尺寸文件
/// </summary>
private string[] icons = new string[6];
然后修改ConnectToSW的代碼,注釋掉之前加載過程中的提示,并在此增加命令。
/// <summary>
/// 連接到SolidWorks
/// </summary>
/// <param name="ThisSW"></param>
/// <param name="Cookie"></param>
/// <returns></returns>
public bool ConnectToSW(object ThisSW, int Cookie)
{
iSwApp = (ISldWorks)ThisSW;
//iSwApp.SendMsgToUser("SolidWorks正在加載此插件...");
addinCookieID = Cookie;
iSwApp.SetAddinCallbackInfo(0, this, addinCookieID);
iCmdMgr = iSwApp.GetCommandManager(addinCookieID);
AddCommandMgr();
return true;
}
然后就是增加AddCommandMgr EnableFunction FunctionProxy 這三方方法,用于執(zhí)行增加命令的菜單,命令的可用性,以及功能的代理。
/// <summary>
/// 增加命令
/// </summary>
public void AddCommandMgr()
{
try
{
ICommandGroup cmdGroup;
//如果要支持多語言,就在這里下功夫
string Title = "Addin Study";
string ToolTip = "Addin Study ToolTip";
int[] docTypes = new int[]{(int)swDocumentTypes_e.swDocASSEMBLY,
(int)swDocumentTypes_e.swDocDRAWING,
(int)swDocumentTypes_e.swDocPART};
int cmdGroupErr = 0;
bool ignorePrevious = false;
object registryIDs;
//通過id從注冊表獲取工具欄的信息,并返回之前的命令id
bool getDataResult = iCmdMgr.GetGroupDataFromRegistry(mainCmdGroupID, out registryIDs);
//當前版本的插件id
var knownIDs = mainItemIds;
if (getDataResult)
{
//如果命令id集不一樣了,那么就要忽略,后面要重新建立
if (!CompareIDs((int[])registryIDs, knownIDs)) //if the IDs don't match, reset the commandGroup
{
ignorePrevious = true;
}
}
cmdGroup = iCmdMgr.CreateCommandGroup2(mainCmdGroupID, Title, ToolTip, "", -1, ignorePrevious, ref cmdGroupErr);
// 設置對應的圖標帶 ,后面增加命令的時候就是傳遞的圖標帶的序號,從0開始
icons[0] = $@"{RegDllPath()}\icons\toolbar20x.png";// iBmp.CreateFileFromResourceBitmap("toolbar20x.png", thisAssembly);
icons[1] = $@"{RegDllPath()}\icons\toolbar32x.png";// iBmp.CreateFileFromResourceBitmap("toolbar32x.png", thisAssembly);
icons[2] = $@"{RegDllPath()}\icons\toolbar40x.png";// iBmp.CreateFileFromResourceBitmap("toolbar40x.png", thisAssembly);
icons[3] = $@"{RegDllPath()}\icons\toolbar64x.png";// iBmp.CreateFileFromResourceBitmap("toolbar64x.png", thisAssembly);
icons[4] = $@"{RegDllPath()}\icons\toolbar96x.png";// iBmp.CreateFileFromResourceBitmap("toolbar96x.png", thisAssembly);
icons[5] = $@"{RegDllPath()}\icons\toolbar128x.png";//iBmp.CreateFileFromResourceBitmap("toolbar128x.png", thisAssembly);
mainIcons[0] = $@"{RegDllPath()}\icons\mainicon_20.png";//iBmp.CreateFileFromResourceBitmap("mainicon_20.png", thisAssembly);
mainIcons[1] = $@"{RegDllPath()}\icons\mainicon_32.png";//iBmp.CreateFileFromResourceBitmap("mainicon_32.png", thisAssembly);
mainIcons[2] = $@"{RegDllPath()}\icons\mainicon_40.png";//iBmp.CreateFileFromResourceBitmap("mainicon_40.png", thisAssembly);
mainIcons[3] = $@"{RegDllPath()}\icons\mainicon_64.png";//iBmp.CreateFileFromResourceBitmap("mainicon_64.png", thisAssembly);
mainIcons[4] = $@"{RegDllPath()}\icons\mainicon_96.png";//iBmp.CreateFileFromResourceBitmap("mainicon_96.png", thisAssembly);
mainIcons[5] = $@"{RegDllPath()}\icons\mainicon_128.png";//iBmp.CreateFileFromResourceBitmap("mainicon_128.png", thisAssembly);
cmdGroup.MainIconList = mainIcons;
cmdGroup.IconList = icons;
//菜單的類型有哪些 菜單 工具條
int menuToolbarOption = (int)(swCommandItemType_e.swMenuItem | swCommandItemType_e.swToolbarItem);
//菜單
List<int> cmdIndexs = new List<int>();
//API提示的信息有誤
//第一個參數(shù)是菜單里面的名稱
//第三個參數(shù)是提示信息
//第四個參數(shù)是工具條上的名稱
var tempCmdIndex1 = cmdGroup.AddCommandItem2("Cmd1", -1, "Cmd Tooltip1", "Cmd-1", 0, $"FunctionProxy({mainItemIds[0]})", $@"EnableFunction({mainItemIds[0]})", mainItemIds[0], menuToolbarOption);
var tempCmdIndex2 = cmdGroup.AddCommandItem2("Cmd2", -1, "Cmd Tooltip2", "Cmd-2", 1, $"FunctionProxy({mainItemIds[1]})", $@"EnableFunction({mainItemIds[1]})", mainItemIds[1], menuToolbarOption);
var tempCmdIndex3 = cmdGroup.AddCommandItem2("Cmd3", -1, "Cmd Tooltip3", "Cmd-3", 2, $"FunctionProxy({mainItemIds[2]})", $@"EnableFunction({mainItemIds[2]})", mainItemIds[2], menuToolbarOption);
cmdIndexs.Add(tempCmdIndex1);
cmdIndexs.Add(tempCmdIndex2);
cmdIndexs.Add(tempCmdIndex3);
cmdGroup.HasToolbar = true;
cmdGroup.HasMenu = true;
cmdGroup.Activate();
//增加到工具條,是通過每個文檔對象來增加的。 比如零件 裝配 工程圖
bool bResult;
foreach (int type in docTypes)
{
CommandTab cmdTab;
cmdTab = iCmdMgr.GetCommandTab(type, Title);
//如果已經(jīng)存在,并且id命令有變化,需要移除之后 ,重新增加。
if (cmdTab != null & !getDataResult | ignorePrevious)
{
bool res = iCmdMgr.RemoveCommandTab(TabToRemove: cmdTab);
cmdTab = null;
}
//工具欄為空時,重新增加
if (cmdTab == null)
{
cmdTab = iCmdMgr.AddCommandTab(type, Title);
CommandTabBox cmdBox = cmdTab.AddCommandTabBox();
List<int> cmdIDs = new List<int>();
//工具欄樣式,
List<int> showTextType = new List<int>();
for (int i = 0; i < mainItemIds.Length; i++)
{
cmdIDs.Add(cmdGroup.get_CommandID(i));
showTextType.Add((int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow);
}
bResult = cmdBox.AddCommands(cmdIDs.ToArray(), showTextType.ToArray());
CommandTabBox cmdBox1 = cmdTab.AddCommandTabBox();
//這個是加分割線,記得從后往前,因為分割后最前的id集變少了。
//cmdTab.AddSeparator(cmdBox1, cmdIDs[0]);
}
}
}
catch (Exception ex)
{
SwApp.SendMsgToUser(ex.StackTrace);
}
}
/// <summary>
/// 決定此命令在該環(huán)境下是否可用
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public int EnableFunction(string data)
{
int commandType = int.Parse(data);
return 1;
}
/// <summary>
/// 通過用戶點擊的菜單id來執(zhí)行不同的動作
/// </summary>
/// <param name="data"></param>
public void FunctionProxy(string data)
{
int commandId = int.Parse(data);
switch (commandId)
{
case 1002:
SwApp.SendMsgToUser("Cmd1 Click");
break;
case 1003:
SwApp.SendMsgToUser("Cmd2 Click");
break;
case 1004:
SwApp.SendMsgToUser("Cmd3 Click");
break;
}
}
最后把斷開的寫一下
現(xiàn)在應該差不多了,生成一下:
如果不放心,就再安裝 一下:
再次打開SolidWorks.
然后新建一個零件:
點擊 一下Cmd-2
好了,下面就是具體的開發(fā)邏輯處理了,和exe程序基本是一樣的。
基本上就可以了,源代碼已經(jīng)上傳。
https://gitee.com/painezeng/SolidWorksAddinStudy文章來源:http://www.zghlxwxcb.cn/news/detail-609977.html
有什么問題歡迎來聊。文章來源地址http://www.zghlxwxcb.cn/news/detail-609977.html
到了這里,關于C# SolidWorks 二次開發(fā) -從零開始創(chuàng)建一個插件(2)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!