本文主要分享一些在Visio二次开发中碰到的各种小问题及其解决方法:
1. 为图元设置颜色
在一些分析中,如电气线路分析中,需要根据不同的状态改变图元的颜色,那么如何改变指定图元的设备颜色呢?下面提供代码解决该问题。
............
上面的代码是比较简洁的写法,当然也可以使用下面这种方式:
每个设备Shape都有一个Connects和FromConnects集合,该集合是Connect对象集合,每个Connect有ToSheet和FromSheet属性,分别是指向一个Shape对象,我们如果要获取设备的关联关系,就是需要判断这些Connect的ToSheet和FromSheet属性。
如下代码:
我们知道,每个图元Shape甚至Page对象都有很多自定义属性,你可以通过在Visio的开发模式中查看ShapeSheet查看到。而所有这些属性中,每行又代表一个属性的各种定义信息,如Label是什么,Prompt(提示)是什么,Value(值)是什么,Type(类型)是什么,这就有点类似于我们在数据库定义一个字段,需要指定字段的名称,类型等等,那如果我们需要把这些信息保存下来,我们该如何获取呢?下面举例说明:
1. 为图元设置颜色
在一些分析中,如电气线路分析中,需要根据不同的状态改变图元的颜色,那么如何改变指定图元的设备颜色呢?下面提供代码解决该问题。
shape.get_CellsU(
"
LineColor
").ResultIU = (
double)VisDefaultColors.visDarkGreen;
//
有电(绿色)
其中VisDefaultColors是一个枚举,有很多颜色,可以查下SDK,也可以使用其对应的数值来代替
Constant | Value | Description |
---|---|---|
visBlack |
0 |
Black |
visBlue |
4 |
Blue |
visCyan |
7 |
Cyan |
visDarkBlue |
10 |
Dark blue |
visDarkCyan |
13 |
Dark cyan |
visDarkGray |
19 |
Dark gray |
visDarkGreen |
9 |
Dark green |
visDarkRed |
8 |
Dark red |
visDarkYellow |
11 |
Dark yellow |
上面的代码是比较简洁的写法,当然也可以使用下面这种方式:
shape.get_CellsSRC((
short)VisSectionIndices.visSectionObject, (
short)VisRowIndices.visRowLine,(
short)VisCellIndices.visLineColor).FormulaU =
4
2. 获取图元设备的连接关系
每个设备Shape都有一个Connects和FromConnects集合,该集合是Connect对象集合,每个Connect有ToSheet和FromSheet属性,分别是指向一个Shape对象,我们如果要获取设备的关联关系,就是需要判断这些Connect的ToSheet和FromSheet属性。
如下代码:
string strShapes =
"
;
";
if (IsSpecialEquipTypeInShape(shape))
{
foreach (Visio.Connect connect in shape.Connects)
{
strShapes += GetConnectsShapes(shape, connect.ToSheet); //检查接入的设备
strShapes += GetConnectsShapes(shape, connect.FromSheet); //检查接出的设备
}
foreach (Visio.Connect connect in shape.FromConnects)
{
strShapes += GetConnectsShapes(shape, connect.ToSheet); //检查接入的设备
strShapes += GetConnectsShapes(shape, connect.FromSheet); //检查接出的设备
}
}
if (IsSpecialEquipTypeInShape(shape))
{
foreach (Visio.Connect connect in shape.Connects)
{
strShapes += GetConnectsShapes(shape, connect.ToSheet); //检查接入的设备
strShapes += GetConnectsShapes(shape, connect.FromSheet); //检查接出的设备
}
foreach (Visio.Connect connect in shape.FromConnects)
{
strShapes += GetConnectsShapes(shape, connect.ToSheet); //检查接入的设备
strShapes += GetConnectsShapes(shape, connect.FromSheet); //检查接出的设备
}
}
/// <summary>
/// 获取与当前的图元连接(接入或接出)的所有相关设备
/// </summary>
private string GetConnectsShapes(Visio.Shape shape, Visio.Shape toFromSheet)
{
string strShapes = string.Empty;
bool exist = VisioUtility.ShapeCellExist(toFromSheet, "设备类型");
bool isSpecial = IsSpecialEquipTypeInShape(toFromSheet);
if (toFromSheet != null && exist && isSpecial)
{
//Visio图元的连接集合,会存放自己本身的,所以此处需要判断。
if (shape.ID != toFromSheet.ID)
{
strShapes += string.Format("{0};", toFromSheet.ID);
}
}
return strShapes;
}
3. 获取图元的属性集合
/// 获取与当前的图元连接(接入或接出)的所有相关设备
/// </summary>
private string GetConnectsShapes(Visio.Shape shape, Visio.Shape toFromSheet)
{
string strShapes = string.Empty;
bool exist = VisioUtility.ShapeCellExist(toFromSheet, "设备类型");
bool isSpecial = IsSpecialEquipTypeInShape(toFromSheet);
if (toFromSheet != null && exist && isSpecial)
{
//Visio图元的连接集合,会存放自己本身的,所以此处需要判断。
if (shape.ID != toFromSheet.ID)
{
strShapes += string.Format("{0};", toFromSheet.ID);
}
}
return strShapes;
}
我们知道,每个图元Shape甚至Page对象都有很多自定义属性,你可以通过在Visio的开发模式中查看ShapeSheet查看到。而所有这些属性中,每行又代表一个属性的各种定义信息,如Label是什么,Prompt(提示)是什么,Value(值)是什么,Type(类型)是什么,这就有点类似于我们在数据库定义一个字段,需要指定字段的名称,类型等等,那如果我们需要把这些信息保存下来,我们该如何获取呢?下面举例说明:
Dictionary<
string, StencilPropertyInfo> list =
new Dictionary<
string, StencilPropertyInfo>();
StencilPropertyInfo propertyInfo;
Visio.Cell shapeCell;
short shortSectionProp = ( short)VisSectionIndices.visSectionProp;
if (shape != null)
{
for (short i = 0; i < shape.get_RowCount(shortSectionProp); 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.SortKey = VisioUtility.FormulaStringToString(shapeCell.FormulaU);
shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsType);
propertyInfo.PropType = (PropType)shapeCell.get_ResultInt((short)VisUnitCodes.visNumber, 0);
shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsInvis);
if ("True".Equals(VisioUtility.FormulaStringToString(shapeCell.FormulaU), StringComparison.OrdinalIgnoreCase))
{
propertyInfo.InVisible = true;
}
propertyInfo.PropRowID = i;
if(!list.ContainsKey(propertyInfo.Name))
{
list.Add(propertyInfo.Name, propertyInfo);
}
}
}
}
return list;
StencilPropertyInfo propertyInfo;
Visio.Cell shapeCell;
short shortSectionProp = ( short)VisSectionIndices.visSectionProp;
if (shape != null)
{
for (short i = 0; i < shape.get_RowCount(shortSectionProp); 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.SortKey = VisioUtility.FormulaStringToString(shapeCell.FormulaU);
shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsType);
propertyInfo.PropType = (PropType)shapeCell.get_ResultInt((short)VisUnitCodes.visNumber, 0);
shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsInvis);
if ("True".Equals(VisioUtility.FormulaStringToString(shapeCell.FormulaU), StringComparison.OrdinalIgnoreCase))
{
propertyInfo.InVisible = true;
}
propertyInfo.PropRowID = i;
if(!list.ContainsKey(propertyInfo.Name))
{
list.Add(propertyInfo.Name, propertyInfo);
}
}
}
}
return list;
4. 关闭视图中打开的所有模具
一般来说,一个Visio文档,一般会打开很多模具窗口,用来辅助画图的,我们有时候不小心关闭一些,又有可能打开多几个,那么你是如何记住这些打开的模具文件的呢,我们要如何关闭全部呢,你可以使用TryCatch来关闭每个文件,即使它可能已经关闭了,这种才保证不会出错;我们不太喜欢暴力,还有没有更好的方法呢,让它自己知道那些可以关闭的呢?
/// <summary>
/// 关闭视图中打开的所有模具
/// </summary>
/// <param name="visApp"></param>
/// <returns></returns>
public bool CloseAllStencileDocument(Application visApp)
{
string[] strs = new string[0];
Array arr = strs as Array;
visApp.Documents.GetNames(out arr);
Document visDoc;
foreach (object file in arr)
{
if (file.ToString().IndexOf(".vss", StringComparison.OrdinalIgnoreCase) > 0)
{
visDoc = visApp.Documents[file];
if (visDoc != null)
{
visDoc.Close();
}
}
}
return true;
}
/// 关闭视图中打开的所有模具
/// </summary>
/// <param name="visApp"></param>
/// <returns></returns>
public bool CloseAllStencileDocument(Application visApp)
{
string[] strs = new string[0];
Array arr = strs as Array;
visApp.Documents.GetNames(out arr);
Document visDoc;
foreach (object file in arr)
{
if (file.ToString().IndexOf(".vss", StringComparison.OrdinalIgnoreCase) > 0)
{
visDoc = visApp.Documents[file];
if (visDoc != null)
{
visDoc.Close();
}
}
}
return true;
}
5. 管理Visio内置的窗口
Visio控件提供了很多内置的窗口,你可以根据需要显示或者隐藏它,如我们常常看到的图元属性窗口、形状查询窗口、模具管理窗口等等,很典型的例子,我们很多时候不需要那个形状查询窗口,想把它隐藏,那么这些该如何操作呢?
我前面写过的文章C#进行Visio二次开发的常见问题处理 有说明,如下所示:
//
Visio2007的形状窗口中去除搜索形状功能
VisApplication.Settings.ShowShapeSearchPane = false;
Visio2003的ShowShapeSearchPane实现方式 Visio2003的ShowShapeSearchPane实现方式
VisApplication.Settings.ShowShapeSearchPane = false;
Visio2003的ShowShapeSearchPane实现方式 Visio2003的ShowShapeSearchPane实现方式
还有一种方式可以管理窗口,如下面代码,对各种内置的窗口实现了统一的管理
for (
int i = drawingControl.Window.Windows.Count; i >
0; i--)
{
Window visWindow;
int windowType;
visWindow = drawingControl.Window.Windows.get_ItemEx(i);
windowType = visWindow.Type;
if (windowType == (int) VisWinTypes.visAnchorBarBuiltIn) {
switch (visWindow.ID)
{
case (int) VisWinTypes.visWinIDCustProp:
case (int) VisWinTypes.visWinIDDrawingExplorer:
case (int) VisWinTypes.visWinIDMasterExplorer:
case (int) VisWinTypes.visWinIDPanZoom:
case (int) VisWinTypes.visWinIDShapeSearch:
case (int) VisWinTypes.visWinIDSizePos:
case (int) VisWinTypes.visWinIDStencilExplorer:
visWindow.Visible = false;
break;
default:
break;
}
}
}
Window visWindow;
int windowType;
visWindow = drawingControl.Window.Windows.get_ItemEx(i);
windowType = visWindow.Type;
if (windowType == (int) VisWinTypes.visAnchorBarBuiltIn) {
switch (visWindow.ID)
{
case (int) VisWinTypes.visWinIDCustProp:
case (int) VisWinTypes.visWinIDDrawingExplorer:
case (int) VisWinTypes.visWinIDMasterExplorer:
case (int) VisWinTypes.visWinIDPanZoom:
case (int) VisWinTypes.visWinIDShapeSearch:
case (int) VisWinTypes.visWinIDSizePos:
case (int) VisWinTypes.visWinIDStencilExplorer:
visWindow.Visible = false;
break;
default:
break;
}
}
}
这下明白了多少了呢,要饮水思源哦
本文转自博客园伍华聪的博客,原文链接:C#进行Visio二次开发之鸡毛蒜皮(一),如需转载请自行联系原博主。