多写博客,其实还是蛮好的习惯的,当初大学的时候导师就叫我写,但是就是懒,大学的时候,谁不是魔兽或者LOL呢,是吧,哈哈哈。
好了,接着上一篇,我继续写。
摘要: (转发请注明来源:http://www.cnblogs.com/EminemJK/)
1、创建Doc对象
using Microsoft.Office.Interop.Visio;short openModel=(short)VisOpenSaveArgs.visOpenDocked | (short)VisOpenSaveArgs.visOpenRO; //设置打开模式InvisibleApp app=new InvisibleApp();Document visio=app.Document.OpenEx(filePath,openModel);
这样就可以获取到visio的Document对象了,这里我并没有使用ApplicationClass 这个类中的打开方法去打开文件,因为
ApplicationClass app=new ApplicationClass();Document visio=app.Document.OpenEx(filePath,openModel);
这样子打开visio的话,new的时候前台会创建一个空的visio程序来等待一个文件的打开,体验感觉很不好,即使加上
app.Visible=false;
也会一闪而过,所以,这里使用visio的另一个接口InvisibleApp来后台打开文件,前台是感觉不到的。
2、读取visio图纸中的线段
1 //存储器件信息的实体类 2 public Class ShapeInfo 3 { 4 public string DeviceName { get;set;} //器件名称 5 public string Position { get;set;} //位置 6 public string DeviceDetail{ get;set;} //形状数据 7 public string Label { get;set;} //备注 8 //其他信息……(根据需要) 9 }10 11 //存储线信息的实体类12 public Class ShapeLine13 {14 public string LConTxt{ get,set;} //线文本15 public string LShapeDataName{ get,set;} //形状数据名称16 public string LPosition { get,set;} //线位置17 public string LLabel{ get,set;} //备注信息18 public ShapeInfo FShapeInfo;19 public ShapeInfo TShapeInfo; //线段两端所连接的形状元素20 //其他信息……(根据需要)21 }22 //存储器件信息23 Dictionary> visioInfoDic = null; 24 //存储线段信息25 Dictionary > visioLineDic = null;26 27 好了,做好前提工作之后,开干,前面一篇已经说到大体的操作思路,读取线在这个方法内28 29 if(sp.Connects.Count>0)30 {31 ShapeLine spL=new ShapeLine();32 spL.LConTxt=sp.Text;33 spL.LShapeDataName=sp.Name;34 spL.LPosition=getShapeCellPosition(sp); //获取位置,和器件一样。共用35 getPointInfo(sp,spL,false); //后面讲解,获取线段两端的器件的信息36 spL.LLabel=GetShapeCellProp(sp); //获取形状数据信息,和器件一样。共用37 }
先讲共用的方法吧,获取位置,其实在这里,获取位置对于我来说,并没什么用,可能对其他人想要操作visio的才有需要,所以还是讲吧。
////// 获取图形位置信息 /// private static string GetShapLoaclInfo(Shape shape) { //依次取出“PinX”、“PinY”、“Width”、“Height”、“LocPinX”、“LocPinY”、“Angle”、“FlipX”、“FlipY” string shapLocalinfo = ""; for (int j = 0; j < 2; j++) { Cell cex = shape.get_CellsSRC((short)VisSectionIndices.visSectionObject, (short)VisRowIndices.visRowXFormOut, (short)j); if (j > 0) shapLocalinfo += ","; shapLocalinfo += cex.ResultIU.ToString(); } return shapLocalinfo; }
也行大家发现了,这个
shape.get_CellsSRC((short)VisSectionIndices.visSectionObject, (short)VisRowIndices.visRowXFormOut,(short)j);里面,几个参数,这是方法【get_CellsSRC】是接下来我们一直用到来获取元素的信息的,这个方法里面参数很有意思,上一篇我有提到大家【显示ShapeSheet】,都是从这里面来枚举的,【显示ShapeSheet】下面查看可以看到是一个一个表来分类的。第一个参数: 代表VisSectionIndices对象下的枚举值,也就是【显示ShapeSheet】下面的表名,可以这么理解;第二个参数: 代表表中的行;第三个参数: 代表表中的列。明白这点就行了,枚举值还可以参考微软官网的给的,我们都可以通过类似这种操作来获取。////// 获取图形属性/// private static string GetShapeCellProp(Shape shapeTarget) { string info = ""; for (int i = 0; i < shapeTarget.get_RowCount((short)VisSectionIndices.visSectionProp); i++) { Cell cellKey = shapeTarget.get_CellsSRC((short)VisSectionIndices.visSectionProp, (short)i, (short)2); Cell cellValue = shapeTarget.get_CellsSRC((short)VisSectionIndices.visSectionProp, (short)i, (short)VisCellIndices.visUserValue); if (i > 0) info += ";"; info += FormulaForString(cellKey.Formula) + ":" + FormulaForString(cellValue.Formula); } return info; }
形状数据例图: