在项目工程中可以看到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. 文件和目录之设置用户ID和设置组ID

    与一个进程相关联的ID有6个或更多,它们如表4-4所示: 表4-4 与每个进程相关联的用户ID和组ID 实际用户ID                            我们实际上是谁 实际组ID ...

  2. php正则测试demo、动态函数

    <?php error_reporting (E_ALL); ini_set ('display_errors', 'on');?><meta http-equiv="Co ...

  3. 如何使Android Studio项目发布到Jcenter中

    Android仓库 简单的普及下关于android的依赖仓库,有两种分别是Jcenter与Maven Central其实不管是Jcenter还是Maven Central都是Maven库. Jcent ...

  4. WebService地址变成计算机名的解决办法

    WCF 4.0 has solved this issue in some instances with a new config option that use Request Headers: & ...

  5. Win7:“找不到该项目”错误解决大法

    1.将以下代码复制粘贴到一新建的txt记事本文档中,并另存为del.bat文件(或者你喜欢的名字),注意扩展名为批处理文件bat. DEL /F /A /Q \\?\%1RD /S /Q \\?\%1 ...

  6. 猪满满 购物APP

    猪满满是专注“省钱,赚钱”的购物App,使用自定义tabar分为四大类,分别是首页,超返,发现,我的. 首页:使用UItableview,自定义cell展示商品. 超返:自定义Button分为综合,返 ...

  7. Hadoop基于Protocol Buffer的RPC实现代码分析-Server端

    http://yanbohappy.sinaapp.com/?p=110 最新版本的Hadoop代码中已经默认了Protocol buffer(以下简称PB,http://code.google.co ...

  8. 一步一步搭建 OAuth 认证服务器

    http://www.fising.cn/2011/03/%E4%B8%80%E6%AD%A5%E4%B8%80%E6%AD%A5%E6%90%AD%E5%BB%BA-oauth-%E8%AE%A4% ...

  9. AWK 脚本编写习惯

    教训总结: 不能忽略了脚本语言的编写规范! 创建数组的时候初始化,特别是在for循环中使用的数组: u_count[; g_count[; 认真对待对象,特别是数组的命名: username_to_d ...

  10. Linux中Oracle数据库备份还原

    一.备份Oracle数据库 1.使用数据库管理员账户登录 sqlplus system/system@orcl as sysdba; 2.创建备份目录,并指定备份目录(bak_dir)的物理路径 cr ...