在项目工程中可以看到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的计算机上使用。

基于C#的SolidWorks插件开发(2)--创建插件的更多相关文章

  1. 基于C#的SolidWorks插件开发(1)--SolidWorks API接口介绍

    这是两年前毕业时写的一篇关于SolidWorks插件开发与公司PDM集成的毕业设计,最近闲来无事拿出来整理一下,大神们可以略过. 1.1   SolidWorks API接口 正确调用SolidWor ...

  2. Tridiv:基于 Web 的 CSS 编辑器,创建炫丽 3D 图形

    Tridiv 是一个基于 Web 的编辑器,使用 CSS 创建 3D 形状.它提供了一个传统的四个面板的操作界面,给出了从每个平面的视图,以及一个预览窗格中示出的最终的效果.使用 Tridiv 可以创 ...

  3. 【eclipse插件开发实战】 Eclipse插件开发5——时间插件Timer开发实例详解

    Eclipse插件开发5--时间插件Timer开发实例详解 这里做的TimeHelper插件设定为在菜单栏.工具栏提供快捷方式,需要在相应地方设置扩展点,最后弹出窗体显示时间. 在上一篇文章里创建好了 ...

  4. Vue 基于node npm & vue-cli & element UI创建vue单页应用

    基于node npm & vue-cli & element UI创建vue单页应用 开发环境   Win 10   node-v10.15.3-x64.msi 下载地址: https ...

  5. 【odoo14】第三章、创建插件

    现在我们已经有了开发环境并了解了如何管理实例及数据库,现在让我们来学习下如何创建插件模块. 本章内容如下: 创建和安装模块 完成manifest文件 组织模块文件结构 添加模型 添加菜单及视图 添加访 ...

  6. 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表

    创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...

  7. 基于jquery的bootstrap在线文本编辑器插件Summernote

    Summernote是一个基于jquery的bootstrap超级简单WYSIWYG在线编辑器.Summernote非常的轻量级,大小只有30KB,支持Safari,Chrome,Firefox.Op ...

  8. MixItUp:超炫!基于 CSS3 & jQuery 的过滤和排序插件

    MixItUp 是一款轻量,但功能强大的 jQuery 插件,提供了对分类和有序内容的美丽的动画过滤和排序功能.特别适合用于作品集网站,画廊,图片博客以及任何的分类或有序内容. 它是如何工作的? Mi ...

  9. 基于Bootstrap简单实用的tags标签插件

    http://www.htmleaf.com/jQuery/ jQuery之家 自由分享jQuery.html5和css3的插件库 基于Bootstrap简单实用的tags标签插件

随机推荐

  1. [Effective C++ --020]宁以pass-by-reference-to-const替换pass-by-value

    前言: 我们都用过C的值传递方式,那么在C++情况下,值传递是怎样工作的呢? 比如: int foo(int x); int i; foo(i); 1.程序内部先取得i的一个副本 2.将副本传递到fo ...

  2. 网页媒体播放利器 - JW Player使用心得

    序言 前段时间在做一个网站项目,要求能在线播放视频.音乐,这个以前还真没接触过.恰好今天在坛子里闲逛时,发现有同志对这方面也感兴趣,遂把这些整理了下,同时自己也可以复习下. 播放器需要满足以下需求: ...

  3. if elseif else

    1)if,else if需要接判断表达式,只有在满足条件的时候执行而else语句是在前面条件都不满足的时候才能执行(2) 一个if...else if...else语句中可以有多个else if语句, ...

  4. UITableView优化那点事

    forkingdog关于UITableView优化的框架其实已经能够应用在一般的场景,且有蛮多的知识点供我们借鉴,借此站在巨人的肩膀上来分析一把. 至于UITableView的瓶颈在哪里,我相信网上随 ...

  5. 【暴力模拟】UVA 1594 - Ducci Sequence

    想麻烦了.这题真的那么水啊..直接暴力模拟,1000次(看了网上的200次就能A)后判断是否全为0,否则就是LOOP: #include <iostream> #include <s ...

  6. js ie8不支持项总结

    不支持filter,trim  要用jquery 的$filter,$trim 数组不能用for in 要用for 数组没有indextOf方法 不能使用关键字,如true ,default IE8 ...

  7. [原创]HTML标签总结!! 第一次画 尚需要改进 多关照

    HTML 页面标签总结  拿xMind写了一下午总结的   只能传图片了   CSS明天整理  后天看看能不能传上来 //======================================= ...

  8. sqlserver 理解数据集

    数据集分四类: 1.A∩B,既是所求数据集既在A中,又在B中 2.A∪B,既所求数据在数据集A中,或在数据集B中 3.A-B,既所求数据在数据集A中,不在数据集B中 4.B-A,既所求数据在数据集B中 ...

  9. C#之装箱和拆箱

    在实际编码过程中,有时候会出现装箱和拆箱操作.下面就类分别认识一下: 需要注意的是,类型转换和这个是不同的.Convert方法并没有发生装箱和拆箱操作,而是类型转换,包括int.parse等等. 装箱 ...

  10. ###Canny边缘检测算子

    开源中国. #@date: 2014-06-20 #@author: gerui #@email: forgerui@gmail.com 一.一阶微分边缘算子 1. 一阶微分边缘检测算子也称梯度边缘算 ...