如何使用C#调用Microsoft.Office.Interop.Visio设置Visio关联形状多重性显示
C# 基于Microsoft.Office.Interop.Visio实现关联形状操作方案
前置准备
开发前需先在项目中引入Microsoft.Office.Interop.Visio程序集,并在代码文件头部添加命名空间引用:
using Microsoft.Office.Interop.Visio;
以下示例中的associationShape均为已获取的目标关联形状Shape实例。
功能1:开启关联形状的多重性(multiplicity)显示
UML关联形状的show multiplicity是内置布尔属性,直接修改对应形状单元格值即可生效:
// 标准UML关联形状属性配置 associationShape.CellsU["Prop.ShowMultiplicity"].FormulaU = "1"; // 若上述属性不存在,可替换为低版本Visio兼容的属性名 // associationShape.CellsU["Prop.UMLAssociationShowMultiplicity"].FormulaU = "1";
将FormulaU值设为"1"为开启显示,设为"0"为关闭显示。
功能2:访问关联形状的4个定位点
Visio中关联形状默认包含4个连接定位点,可通过ConnectionPoints集合直接遍历获取坐标:
// 遍历所有定位点(默认共4个) for (int i = 0; i < associationShape.ConnectionPoints.Count; i++) { // 可根据需要修改VisUnitCodes切换坐标单位,示例用毫米 double pointX = (double)associationShape.ConnectionPoints[i].Cells["X"].Result[VisUnitCodes.visMillimeters]; double pointY = (double)associationShape.ConnectionPoints[i].Cells["Y"].Result[VisUnitCodes.visMillimeters]; // 此处可添加自定义业务逻辑处理坐标值 }
如果需要获取关联连接线的起止端点坐标,可直接访问形状的起止坐标单元格:
// 连接线起点坐标 double beginX = (double)associationShape.CellsU["BeginX"].Result[VisUnitCodes.visMillimeters]; double beginY = (double)associationShape.CellsU["BeginY"].Result[VisUnitCodes.visMillimeters]; // 连接线终点坐标 double endX = (double)associationShape.CellsU["EndX"].Result[VisUnitCodes.visMillimeters]; double endY = (double)associationShape.CellsU["EndY"].Result[VisUnitCodes.visMillimeters];
注意:如果访问属性时抛出异常,请先确认
associationShape确实为UML关联类型的形状,可通过associationShape.NameU属性校验形状类型是否匹配。
内容的提问来源于stack exchange,提问作者Priya
相关产品推荐
相关产品推荐

