首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

Visio 開發實用技巧集(一):顏色設定、連接關係、屬性遍歷、模具關閉

     編輯:關於C#

分享四個 Visio 二次開發高頻需求的精簡解法:Shape 線條顏色設定、取得連接關係、遍歷 ShapeData 所有屬性定義、批次關閉開啟的模具文件。

1. 設定 Shape 線條顏色

兩種等效寫法,建議使用第一種簡潔語法:


// 簡潔寫法:透過 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...

2. 取得 Shape 連接關係

每個 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 "";
}

3. 遍歷 ShapeData 區段取得完整屬性定義

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;

4. 批次關閉所有開啟的模具

透過 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;
}

這四個技巧覆蓋了視覺回饋、拓撲分析、屬性元資料存取、環境管理四大核心場景,建議收藏備查。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved