C#进行Visio二次开发的常见问题处理

简介:
1. Visio属性值的转换问题
    做过Visio开发的人知道,Visio中的属性值也就是Cell.Formula的值通常包含两对双引号的(如""XX""), 如果要将属性的值转换正常的字符串值,那么需要去除双引号。因此从Visio的Cell的Formula值中得到的字符串需要经过下面方法处理一下:
None.gif         public  static  string FormulaStringToString( string formula)
ExpandedBlockStart.gif         {
InBlock.gif            const string OneQuote = "\"";
InBlock.gif
            const string TwoQuotes = "\"\"";
InBlock.gif            string convertedFormula = "";
InBlock.gif
InBlock.gif            try
ExpandedSubBlockStart.gif            {
InBlock.gif                convertedFormula = formula;
InBlock.gif                if (convertedFormula.StartsWith(OneQuote) && convertedFormula.EndsWith(OneQuote))
ExpandedSubBlockStart.gif                {
InBlock.gif                    convertedFormula = convertedFormula.Substring(1, (convertedFormula.Length - 2));
InBlock.gif                    convertedFormula = convertedFormula.Replace(TwoQuotes, OneQuote);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            catch (Exception err)
ExpandedSubBlockStart.gif            {
InBlock.gif                convertedFormula = "";
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            return convertedFormula;
ExpandedBlockEnd.gif        }
如果是写入到Visio的Cell的Formula中,那么要经过反过程,如下所示:
None.gif        public  static  string StringToFormulaForString( string input)
ExpandedBlockStart.gif         {
InBlock.gif            const string quote = "\"";
InBlock.gif
            string result = "";
InBlock.gif
InBlock.gif            if (input == null)
ExpandedSubBlockStart.gif            {
InBlock.gif                return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            result = input.Replace(quote, (quote + quote));
InBlock.gif            result = quote + result + quote;
InBlock.gif
InBlock.gif            return result;
ExpandedBlockEnd.gif        }
2、获取指定形状指定Cell的值 。除了方法1,还有下面一种方法可以获取Cell的Value值。这种方法比使用Formula获取字符串的方式要好,是因为在Visio2007中下拉列表“资产归属”.对应的Cell的Value可能是INDEX(0,Prop.资产归属.Format),但是如果使用下面的方法就可以正常获取到它具体的值了。
None.gif         public  static  string GetShapeCellValue(Shape shapeTarget,  string strCellType)
ExpandedBlockStart.gif         {
InBlock.gif        const string CUST_PROP_PREFIX = "Prop.";
InBlock.gif            string shapeCellValue = string.Empty;
InBlock.gif
InBlock.gif            if (shapeTarget.get_CellExistsU(CUST_PROP_PREFIX + strCellType, (short)VisExistsFlags.visExistsAnywhere) != 0)
ExpandedSubBlockStart.gif            {
InBlock.gif                shapeCellValue = FormulaStringToString(shapeTarget.get_CellsU(CUST_PROP_PREFIX + strCellType).get_ResultStr(VisUnitCodes.visNoCast));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            return shapeCellValue;
ExpandedBlockEnd.gif        }
3、给指定的Shape赋值。 方法2是读取,当然还需要写入到指定Shape,指定Cell的值
None.gif         public  static  bool SetShapeCellValue(Shape shapeTarget,  string strCellType,  string cellValue)
ExpandedBlockStart.gif         {
InBlock.gif         const string CUST_PROP_PREFIX = "Prop.";
InBlock.gif            bool breturn = false;
InBlock.gif            if (shapeTarget.get_CellExistsU(CUST_PROP_PREFIX + strCellType, (short)VisExistsFlags.visExistsAnywhere) != 0)
ExpandedSubBlockStart.gif            {
InBlock.gif                shapeTarget.get_CellsU(CUST_PROP_PREFIX + strCellType + ".Value").Formula = StringToFormulaForString(cellValue);
ExpandedSubBlockEnd.gif            }

InBlock.gif            return true;
ExpandedBlockEnd.gif        }
4、判断形状某个属性是否存在。 有时候在做一些操作前,需要判断某个属性是否存在,以免访问指定的Cell不存在而抛出异常。
None.gif         public  static  bool ShapeCellExist(Shape shapeTarget,  string strCellType)
ExpandedBlockStart.gif         {
InBlock.gif        const string CUST_PROP_PREFIX = "Prop.";
InBlock.gif            bool breturn = false
InBlock.gif            if (shapeTarget.get_CellExistsU(CUST_PROP_PREFIX + strCellType, (short)VisExistsFlags.visExistsAnywhere) != 0)
ExpandedSubBlockStart.gif            {
InBlock.gif                breturn = true;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            return breturn;
ExpandedBlockEnd.gif        }
5、取当前操作属性所在的行。 Cell的行号有时候非常重要,因此有必要提供一个函数获取对应Cell在ShapeData中的行号。
None.gif         public  static  int GetCustomPropRow(Shape shapeTarget,  string propName)
ExpandedBlockStart.gif         {
InBlock.gif        const string CUST_PROP_PREFIX = "Prop.";
InBlock.gif            int intCustomRow = -1;
InBlock.gif            if (shapeTarget.get_CellExistsU(CUST_PROP_PREFIX + propName, (short)VisExistsFlags.visExistsAnywhere) != 0)
ExpandedSubBlockStart.gif            {
InBlock.gif                intCustomRow = shapeTarget.get_CellsRowIndexU(CUST_PROP_PREFIX + propName);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedBlockEnd.gif            return intCustomRow;
6、判断Visio图纸上是否有形状图元存在。 如果图纸上没有形状图元,你进行操作的时候可能会抛出“请求被禁用”的异常,因此可以操作前先判断有设备在图纸上为妙。
None.gif         public  static  bool HasShapeInWindow(Window window)
ExpandedBlockStart.gif         {
InBlock.gif            bool result = false;
InBlock.gif            try
ExpandedSubBlockStart.gif            {
InBlock.gif                window.SelectAll();
InBlock.gif                result = (window.Selection.Count > 0);
InBlock.gif                window.DeselectAll();
ExpandedSubBlockEnd.gif            }

InBlock.gif            catch
ExpandedSubBlockStart.gif            { ;}
InBlock.gif
InBlock.gif            return result;
ExpandedBlockEnd.gif        }
7、其他的一些功能设置
None.gif         // Visio2007的形状窗口中去除搜索形状功能
None.gif
            VisApplication.Settings.ShowShapeSearchPane =  false;
None.gif
ContractedBlock.gif             Visio2003的ShowShapeSearchPane实现方式
None.gif
None.gif         // 屏蔽Visio2007中的动态连接的功能(默认有)
None.gif
            VisApplication.Settings.EnableAutoConnect =  false;  
None.gif            VisApplication.Settings.StencilBackgroundColor =  10070188;
8、Name和NameU属性的差别
   Visio中很多属性都有一个同名+U的属性名称,一般情况下最好使用这个名称如NameU,因此这个是一个唯一的名字,有时候你会发现Name相同,但他们就是不一样,因为他们的NameU名称不一样的。

9、遇到不明白的操作或者属性,多用Visio文档的宏记录功能,然后对VBA代码进行分析和调试。

本文转自博客园伍华聪的博客,原文链接:C#进行Visio二次开发的常见问题处理,如需转载请自行联系原博主。



目录
相关文章
|
10月前
|
C#
CAD2015 C#二次开发 字体变形
CAD2015 C#二次开发 字体变形
|
10月前
|
C# C++
[记录]c/c++和c#联调常见问题
[记录]c/c++和c#联调常见问题
基于C#的ArcEngine二次开发56:双击属性表跳转目标要素并闪烁
基于C#的ArcEngine二次开发56:双击属性表跳转目标要素并闪烁
基于C#的ArcEngine二次开发56:双击属性表跳转目标要素并闪烁
基于C#的ArcEngine二次开发54:IStatusBar状态栏接口的使用
基于C#的ArcEngine二次开发54:IStatusBar状态栏接口的使用
基于C#的ArcEngine二次开发54:IStatusBar状态栏接口的使用
|
NoSQL 数据处理 C#
基于C#的ArcEngine二次开发52:GDB数据处理过程中与Name相关的操作
基于C#的ArcEngine二次开发52:GDB数据处理过程中与Name相关的操作
基于C#的ArcEngine二次开发52:GDB数据处理过程中与Name相关的操作
基于C#的ArcEngine二次开发51:获取图层字段唯一值列表(Get Unique Values)
基于C#的ArcEngine二次开发51:获取图层字段唯一值列表(Get Unique Values)
基于C#的ArcEngine二次开发51:获取图层字段唯一值列表(Get Unique Values)
|
算法 C#
基于C#的ArcEngine二次开发50:生成面空洞连接线
基于C#的ArcEngine二次开发50:生成面空洞连接线
基于C#的ArcEngine二次开发50:生成面空洞连接线
|
存储 NoSQL Unix
基于C#的ArcEngine二次开发50:MDB创建新要素类及“无当前记录”异常处理
基于C#的ArcEngine二次开发50:MDB创建新要素类及“无当前记录”异常处理
基于C#的ArcEngine二次开发50:MDB创建新要素类及“无当前记录”异常处理
|
C# 开发工具
基于C#的ArcEngine二次开发47:Addins常见问题解决方案集锦
基于C#的ArcEngine二次开发47:Addins常见问题解决方案集锦
基于C#的ArcEngine二次开发47:Addins常见问题解决方案集锦
|
存储 C# Python
基于C#的ArcEngine二次开发46:编辑内容回撤与炸开multipart feature
基于C#的ArcEngine二次开发46:编辑内容回撤与炸开multipart feature
基于C#的ArcEngine二次开发46:编辑内容回撤与炸开multipart feature