判断Visio图纸上是否图纸有设备(存在图元), 有两种方法,一种是通过Window对象的SelectAll方法,在判断选择的集合是否不为空即可,如下:
/// <summary>
/// 判断是否有对象选定
/// </summary>
/// <param name="window"></param>
/// <returns></returns>
[CLSCompliant( false)]
public static bool HasShapeInWindow(Window window)
{
bool result = false;
try
{
window.SelectAll();
result = (window.Selection.Count > 0);
window.DeselectAll();
}
catch
{ ;}
return result;
}
另外一种方法是判断页面对象Page的Shapes对象集合是否大于0即可,如下:
/// 判断是否有对象选定
/// </summary>
/// <param name="window"></param>
/// <returns></returns>
[CLSCompliant( false)]
public static bool HasShapeInWindow(Window window)
{
bool result = false;
try
{
window.SelectAll();
result = (window.Selection.Count > 0);
window.DeselectAll();
}
catch
{ ;}
return result;
}
Visio.Page page = VisioUtility.GetPage(VisDocument,
"
馈线图
");
if (page.Shapes.Count <= 0)
{
MessageUtil.ShowTips("图纸上没有设备");
return;
}
if (page.Shapes.Count <= 0)
{
MessageUtil.ShowTips("图纸上没有设备");
return;
}
/// <summary>
/// 获取Visio文档制定的页面对象
/// </summary>
/// <param name="visDocument"></param>
/// <param name="pageType"></param>
/// <returns></returns>
[CLSCompliant( false)]
public static Page GetPage(Document visDocument, string pageType)
{
Page visPage;
for (int i = 1; i <= visDocument.Pages.Count; i++)
{
visPage = visDocument.Pages[i];
if (FormulaStringToString(visPage.PageSheet.get_Cells(CUST_PROP_PREFIX + "绘图页").Formula) == pageType)
{
return visPage;
}
}
return null;
}
两种方法基本上效果一样, 第二种方法可能速度会快一些,如果你在Visio的Document对象事件中有处理逻辑的时候, 如下所示, 而且有的情况下,你还不能用第一种方式来进行判断:
/// 获取Visio文档制定的页面对象
/// </summary>
/// <param name="visDocument"></param>
/// <param name="pageType"></param>
/// <returns></returns>
[CLSCompliant( false)]
public static Page GetPage(Document visDocument, string pageType)
{
Page visPage;
for (int i = 1; i <= visDocument.Pages.Count; i++)
{
visPage = visDocument.Pages[i];
if (FormulaStringToString(visPage.PageSheet.get_Cells(CUST_PROP_PREFIX + "绘图页").Formula) == pageType)
{
return visPage;
}
}
return null;
}
this.CtrlDrawing.SelectionChanged +=
new EVisOcx_SelectionChangedEventHandler(ctrlDrawing_SelectionChanged);
本文转自博客园伍华聪的博客,原文链接:C#进行Visio二次开发之判断图纸是否有设备,如需转载请自行联系原博主。