如何使用C#进行Visio二次开发
Visio在VB6中的开发有很多现成的项目及代码,但在.NET领域相对比较滞后,但是从Visio SDK2007发布以来,给予Visio的C#开发逐渐多了起来,虽然和VB6的开发思路有一定的相似,不过平台不一样,做出来的东西差别还是有一定的距离,虽然总体慢一点,但是,DotNET框架的逐渐强大以及更多的DotNet程序和开发人员的加入,带给大家更高的整合价值以及更美好的发展未来。
本人将会在C#领域将Visio的二次开发进行下去,希望大家一起学习研究,碰撞出更美的火花
Visio Drawing Control控件使用
Visio开发必备
Visio SDK 2007
VisSDK.chm
Visio Code Samples Library.chm
Visio文档操作
查看ShapeSheet属性及帮助内容
宏的录制
Visio的几个重要对象
Application
Window (Application.ActiveWindow)
Document (Application.ActiveDocument)
Master、Shape
Visio XML格式文件分析
Master格式
Pages/Shapes格式
图纸的XML文档中,Master后面的Shapes集合中只有一个Shape对象
图纸的Shapes集合有多个对象,每个对象的NameU和Name值可能不一样,一般使用NameU
Visio基本操作的实现
VisApplication = this.ctrlDrawing.Document.Application;
VisWindow = VisApplication.ActiveWindow;
VisDocument = VisApplication.ActiveDocument;//Settings
VisApplication.Settings.ShowShapeSearchPane = false; //显示搜索形状窗体
VisApplication.Settings.EnableAutoConnect = false; //自动链接(2007默认)
VisApplication.Settings.StencilBackgroundColor = 10070188; //vbGrayText//文档各种显示
VisApplication.DoCmd((short)VisUICmds.visCmdViewRulers);
VisApplication.DoCmd((short)VisUICmds.visCmdViewGrid);
VisApplication.DoCmd((short)VisUICmds.ShowGuides);
VisApplication.DoCmd((short)VisUICmds.ShowConnectPoints);
VisApplication.DoCmd((short)VisUICmds.ShowPageBreaks);//各种窗口
VisApplication.DoCmd((short)VisUICmds.visCmdShapesWindow);
VisApplication.DoCmd((short)VisUICmds.visCmdPanZoom);
VisApplication.DoCmd((short)VisUICmds.visCmdCustProp);
VisApplication.DoCmd((short)VisUICmds.visCmdSizePos);SendKeys.Send("^(x)");
//VisApplication.DoCmd((short)VisUICmds.visCmdUFEditCut);
SendKeys.Send("{DELETE}");
//VisApplication.DoCmd((short)VisUICmds.visCmdUFEditClear);
SendKeys.Send("^+(p)");
//VisApplication.DoCmd(VisUICmds.visCmdFormatPainter);
SendKeys.Send("^(z)");
//VisApplication.DoCmd(VisUICmds.visCmdEditUndo);
调用工具条对象、菜单对象的方法
Application.CommandBars
Microsoft.Office.Core.CommandBars共享Office对象模型
使用CommandBar代替UIObject
CommandBar对象中,菜单及工具条是同一个东西
CommandBar、CommandBarButton、 CommandBarComboBox、CommandBarControl、 和 CommandBarPopup
示例:执行视图中的工具条的所有按钮事件。
Microsoft.Office.Core.CommandBars commandBars;
commandBars = (Microsoft.Office.Core.CommandBars)VisApplication.CommandBars;foreach (Microsoft.Office.Core.CommandBarControl control in commandBars["View"].Controls)
{Microsoft.Office.Core.CommandBarButton button = control as Microsoft.Office.Core.CommandBarButton;if (button != null){button.Execute();}
}StringBuilder sb = new StringBuilder();
foreach (Microsoft.Office.Core.CommandBar bar in commandBars)
{sb.Append(string.Format("CommandBar Name:{0}\r\n", bar.Name));foreach(Microsoft.Office.Core.CommandBarControl control in bar.Controls){Microsoft.Office.Core.CommandBarButton button = control as Microsoft.Office.Core.CommandBarButton;if(button != null){sb.Append(string.Format("Button Name:{0} \r\n", button.Caption));} }
}
Form2 frm = new Form2();
frm.txtContent.Text = sb.ToString();
frm.Show();short flags = (short)VisOpenSaveArgs.visOpenDocked | (short)VisOpenSaveArgs.visOpenRO;
StencilOpenEx(wndVisio.Application, flags);/// <summary>
/// 打开模具的公共方法
/// </summary>
/// <param name="visApp">按引用调用的VisioApplication对象</param>
/// <param name="flags">打开的模式</param>
private void StencilOpenEx(Application visApp, short flags)
{List<string> stencilList = GetStencils();string stencilFileName;foreach(string stencil in stencilList){stencilFileName = GetStencilsFileName(stencil);if(!string.IsNullOrEmpty(stencilFileName)){visApp.Documents.OpenEx(Portal.gc.gStencileFileBasePath + stencilFileName, flags);}}
}//关闭模具文件
visApp.Documents["Switch.vss"].Close();
visApp.Documents["Span.vss"].Close();
visApp.Documents["Line.vss"].Close();
visApp.Documents["Label.vss"].Close();
visApp.Documents["Construct.vss"].Close();
visApp.Documents["Monitor.vss"].Close();
Visio Shape的属性操作
StringToFormulaForString、FormulaStringToString函数
访问属性
设置属性
添加属性
//列出模具组
this.cmbStencilGroup.Items.Clear();
List<string> stencilGroups = stencil.GetStencils();
foreach (string group in stencilGroups)
{this.cmbStencilGroup.Items.Add(group);
}//根据模具组列出模具
string stencilName = stencil.GetStencilsFileName(this.cmbStencilGroup.Text);
this.cmbStencil.Items.Clear();
string tempName;
foreach (Master master in visApp.Documents[stencilName].Masters)
{tempName = master.Name;if (!stencil.IsExcludeItem(tempName)){this.cmbStencil.Items.Add(tempName);}
}//根据模具,获取对应的属性集合,遍历属性集合,列出属性名称
string stencilName = stencil.GetStencilsFileName(this.cmbStencilGroup.Text);
string masterName = this.cmbStencil.Text;
Visio.Shape shape = visApp.Documents[stencilName].Masters[masterName].Shapes[1];
if (shape != null)
{List<StencilPropertyInfo> propInfos = property.GetPropertyCollection(shape);foreach (StencilPropertyInfo info in propInfos){this.cmbProperty.Items.Add(info.Name);}
}//根据模具、模具属性,列出对应的属性信息
string stencilName = stencil.GetStencilsFileName(this.cmbStencilGroup.Text);
string masterName = this.cmbStencil.Text;
Visio.Shape shape = visApp.Documents[stencilName].Masters[masterName].Shapes[1];
StencilPropertyInfo info = property.GetProperty(shape, this.cmbProperty.Text);
if (info != null)
{this.txtName.Text = info.Name;//属性名称this.txtValue.Text = info.Value;//属性值this.txtFormat.Text = info.Format;//属性格式this.txtSortKey.Text = info.Sort;//属性的排序this.txtPrompt.Text = info.Prompt;//属性的提示信息
}//根据模具,获取属性对象集合
public List<StencilPropertyInfo> GetPropertyCollection(Visio.Shape shape)
{List<StencilPropertyInfo> list = new List<StencilPropertyInfo>();StencilPropertyInfo propertyInfo;Visio.Cell shapeCell;short shortSectionProp = (short)VisSectionIndices.visSectionProp;if (shape != null){for (short i = 0; i < shape.get_RowCount(shortSectionProp) - 1; i++ ){if (shape.get_CellsSRCExists(shortSectionProp, i, (short)VisCellIndices.visCustPropsLabel, 0) != 0){propertyInfo = new StencilPropertyInfo();shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsLabel);propertyInfo.Name = VisioUtility.FormulaStringToString(shapeCell.RowNameU);shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsPrompt);propertyInfo.Prompt = VisioUtility.FormulaStringToString(shapeCell.FormulaU);shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsFormat);propertyInfo.Format = VisioUtility.FormulaStringToString(shapeCell.FormulaU);shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsValue);propertyInfo.Value = VisioUtility.FormulaStringToString(shapeCell.FormulaU);shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsSortKey);propertyInfo.Sort = VisioUtility.FormulaStringToString(shapeCell.FormulaU);//shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsType);//propertyInfo.PropType = VisioUtility.FormulaStringToString(shapeCell.FormulaU);//shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsInvis);//propertyInfo.InVisible = VisioUtility.FormulaStringToString(shapeCell.FormulaU);//..list.Add(propertyInfo);}}}return list;
}//根据模具和属性名称,获取属性对象信息
public StencilPropertyInfo GetProperty(Visio.Shape shape, string propertyName)
{List<StencilPropertyInfo> list = GetPropertyCollection(shape);StencilPropertyInfo propertyInfo = null;foreach(StencilPropertyInfo tempInfo in list){if (tempInfo.Name == propertyName){propertyInfo = tempInfo;break;}}return propertyInfo;
}
如何使用C#进行Visio二次开发相关推荐
- C#进行Visio二次开发之判断图纸是否有设备
判断Visio图纸上是否图纸有设备(存在图元), 有两种方法,一种是通过Window对象的SelectAll方法,在判断选择的集合是否不为空即可,如下: /// <summary ...
- C#进行Visio二次开发之知识点考核试题
本人做过多年的Visio二次开发,根据个人的经验总结及项目积累,写了一些小小的随笔文章(具体可参考<伍华聪的Visio二次开发文章>,上月有幸给深圳一个团队做了两天的Visio培训,当了一 ...
- Visio二次开发(二)----Shape的添加和连接
先说一说为什么我要用到Visio的二次开发,现在做的项目设计到了一些电子地图,下面的这张图片是美工画的一张地铁里面门禁布局图,而这些图在做项目的时候是需要用Visio画的,有提前画好的直接加载到项目中 ...
- C#进行Visio二次开发之图纸打印
几种打印模式 Visio的二次开发中,我们往往需要打印文档,这也是正常不过的需求,Visio的Document对象提供了下面几种打印模式: Constant Value Description vis ...
- Visio二次开发(一)----巧用Visio宏
最近做的项目中需要用到Visio二次开发,安装了Visio的SDK,还有从网上找了一些这方面的博客,Visio的SDK是全英文的,看着有点费劲,很多的东西都不知道该如何去找,不过网上的一些博客还是有一 ...
- visio二次开发___事件篇___事件分类
用visio进行二次开发,不免要用到事件.这里把visio对象模型的所有事件列出来,方便大家查找.第一个事件列表的是按事件名称字母排序的:第二个代码片段是根据分类来展示的.需要注意的是,控件本身有一些 ...
- C#进行Visio二次开发之电气线路停电分析逻辑
停电分析,顾名思义,是对图纸进行停电的逻辑分析.在电气化线路中,一条线路是从一个电源出来,连接着很多很多的设备的,进行停电分析,有两个重要的作用:一是看图纸上的Shape元件是否连接正常,二是看哪些设 ...
- Visio二次开发Java-画图画线(java+com4j)
// 创建Visio对象IVApplication visio = ClassFactory.createApplication();//创建新的空白文档[创建一个不基于模板的新绘图]IVDocume ...
- 基于Visio的二次开发
基于Visio的二次开发 前一段时间,由于项目的需要:学习了一些关于Visio二次开发的知识:现在工具基本成形了,也算告一段落了:因此想总结一下关于Visio的二次开发的一些基本知识: 对于基于Vis ...
- et操作 python wps_拿起来就用的office二次开发(python,win32com使用经历总结)
通过使用office的开发接口,让我深深的领悟到了office的强大(应该wps也有二次开发的接口).每每看到工作中比较繁琐的office操作,总是想做个vba快捷键或者做个文档处理程序,从繁重的文本 ...
最新文章
- win7硬盘安装ubuntu双系统——注意项
- MongoDB增加用户认证:增加用户、删除用户、修改用户密码、读写权限、只读权限...
- php根据位置获取经纬度(百度地图)
- SiftGPU:编译SiftGPU出现问题-无法解析的外部符号 glutInit
- springboot redis shiro 实现 单点登录
- MyBatis之输入(parameterType)与输出(resultType、resultMap)映射
- python-循环-通过while循环完成一个电子钟的模拟
- SQL Server上的审计表和数据版本控制
- “光伏发电改变生活” 在农村市场如何理解?
- JSON.stringify和JSON.parse之间的区别
- escape()、encodeURI()、encodeURIComponent()区别
- 性能测试--jmeter如何发送get请求【3】
- centos7.x-firewalld防火墙常用命令收集
- javaeye API
- 【C语言】基于51/52单片机实现楼梯灯控制程序
- ubuntu常用功能安装集锦
- 《计算机入门》模拟卷 b卷,《计算机入门》模拟试卷B.doc
- 用Visual C#.NET编写服务器日期控件
- 操作系统-课堂笔记-磁盘调度(南航)
- 如何在iphone上模拟定位