在项目工程中可以看到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++ --008]别让异常逃离析构函数

    这章非常容易理解:因为C++并不禁止析构函数吐出异常,只是不鼓励这样做而已. 一.原因 假设我们有10个装着鸡蛋的容器,而且现在我们还想着把它在析构函数打烂. class Egg { public : ...

  2. 下拉列表(web),用jQuery实现

    <!DOCTYPE html><html><head>    <meta http-equiv="Content-Type" conten ...

  3. Android_ViewPager

    view1源代码及xml资源文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  4. debian 颜色设置

    root@debian:/# vi /root/.bashrc #export PS1='\h:\w\$ 'export PS1='\[\033[1;32;40m\]\u@\h:\w\$ \[\033 ...

  5. jvm内存模型及分配参数

    jvm内存模型 程序计数器:是一块很小的内存空间.当线程数量超过cpu数量时,线程之间根据时间片轮询抢夺cpu资源.每一个线程都必须用一个独立的程序计数器,用于记录下一条要运行的指令. java虚拟机 ...

  6. [转]在64位的环境中使用VS建立Web项目进行Oracle连接需要注意WebDev是32位的

    本文转自:http://www.cnblogs.com/studyzy/archive/2010/10/28/1863056.html 我们平时使用的都是32位的机器进行开发,装的都是32位的软件,但 ...

  7. Linux下解决permission denied问题

    由于权限问题,在linux下启动tomcat出现权限permission denied 提示解决方法如下: 1.cd 进入tomcat/bin目录 2.运行下面命令 sudo chmod 777 st ...

  8. 我终于理解了LISP『代码即数据|数据即代码』的含义

    以前我一直不能理解LISP里引用的作用,感觉引用和字符串没什么区别.比如:> (define (func)     'ok) > (func) 'ok 这里把引用ok当做了函数func的返 ...

  9. mysql查找重复

    중복된 것 모두 찾기    SELECT 필드명, count(*) FROM 테이블명  GROUP BY 필드명   mysql> SELECT t1, count(*) FROM tes ...

  10. (转)MySQL数据表中带LIKE的字符匹配查询

    MySQL数据表中带LIKE的字符匹配查询 2014年07月15日09:56    百科369 MySQL数据表中带LIKE的字符匹配查询 LIKE关键字可以匹配字符串是否相等. 如果字段的值与指定的 ...