分享四個 Visio 二次開發高頻需求的精簡解法:Shape 線條顏色設定、取得連接關係、遍歷 ShapeData 所有屬性定義、批次關閉開啟的模具文件。
兩種等效寫法,建議使用第一種簡潔語法:
// 簡潔寫法:透過 CellsU 直接存取
shape.CellsU["LineColor"].ResultIU = (double)VisDefaultColors.visDarkGreen; // 綠色
// 完整寫法:透過 Section/Row/Cell 索引
shape.CellsSRC[(short)VisSectionIndices.visSectionObject,
(short)VisRowIndices.visRowLine,
(short)VisCellIndices.visLineColor].FormulaU = "4";
VisDefaultColors 列舉值對照:visBlack=0, visBlue=4, visCyan=7, visDarkGreen=9, visDarkRed=8, visDarkBlue=10, visDarkYellow=11, visDarkCyan=13, visDarkGray=19...
每個 Shape 擁有 Connects(輸入連接)與 FromConnects(輸出連接)集合,每個 Connect 物件含 ToSheet 與 FromSheet 指向連接的 Shape:
string connectedIds = ";";
if (IsSpecialEquip(shape)) {
foreach (Connect conn in shape.Connects) {
connectedIds += GetConnectedShapeId(shape, conn.ToSheet);
connectedIds += GetConnectedShapeId(shape, conn.FromSheet);
}
foreach (Connect conn in shape.FromConnects) {
connectedIds += GetConnectedShapeId(shape, conn.ToSheet);
connectedIds += GetConnectedShapeId(shape, conn.FromSheet);
}
}
private string GetConnectedShapeId(Shape source, Shape target) {
if (target != null && source.ID != target.ID
&& ShapeCellExist(target, "設備類型")
&& IsSpecialEquip(target)) {
return target.ID + ";";
}
return "";
}
ShapeData (Custom Properties) 以 Row 為單位,每 Row 包含 Label、Prompt、Value、Format、Type、SortKey、Invis 等 Cell:
Dictionary props = new Dictionary();
short propSection = (short)VisSectionIndices.visSectionProp;
for (short row = 0; row < shape.RowCount[propSection]; row++) {
if (shape.CellsSRCExists[propSection, row, (short)VisCellIndices.visCustPropsLabel, 0] != 0) {
var info = new StencilPropertyInfo();
info.Name = FormulaStringToString(shape.CellsSRC[propSection, row, (short)VisCellIndices.visCustPropsLabel].RowNameU);
info.Prompt = FormulaStringToString(shape.CellsSRC[propSection, row, (short)VisCellIndices.visCustPropsPrompt].FormulaU);
info.Format = FormulaStringToString(shape.CellsSRC[propSection, row, (short)VisCellIndices.visCustPropsFormat].FormulaU);
info.Value = FormulaStringToString(shape.CellsSRC[propSection, row, (short)VisCellIndices.visCustPropsValue].FormulaU);
info.SortKey = FormulaStringToString(shape.CellsSRC[propSection, row, (short)VisCellIndices.visCustPropsSortKey].FormulaU);
info.PropType = (PropType)shape.CellsSRC[propSection, row, (short)VisCellIndices.visCustPropsType].ResultInt[VisUnitCodes.visNumber];
info.InVisible = "True".Equals(FormulaStringToString(
shape.CellsSRC[propSection, row, (short)VisCellIndices.visCustPropsInvis].FormulaU), StringComparison.OrdinalIgnoreCase);
info.PropRowID = row;
if (!props.ContainsKey(info.Name)) props.Add(info.Name, info);
}
}
return props;
透過 Documents.GetNames 取得檔名陣列,篩選 .vss 副檔名逐一關閉:
public bool CloseAllStencils(Application visApp) {
Array names = new string[0];
visApp.Documents.GetNames(out names);
foreach (object file in names) {
string fileName = file.ToString();
if (fileName.IndexOf(".vss", StringComparison.OrdinalIgnoreCase) >= 0) {
Document doc = visApp.Documents[fileName];
if (doc != null) doc.Close();
}
}
return true;
}
這四個技巧覆蓋了視覺回饋、拓撲分析、屬性元資料存取、環境管理四大核心場景,建議收藏備查。