切断解析

//切断解析の例
//FindFlowUnreachedElements メソッド実行後にマウスクリックすると例外が発生し
//処理が中断される。これを防ぐにはマウス操作を禁止するしかない
//例として Win32API の EnableWindow を使用する

using System.Runtime.InteropServices;

public static void Search(IApplication pApp)
{
    ArcMap.Application.CurrentTool = null;

    IMxDocument pMxDoc;
    IMap pMap;
            
    IFeatureLayer pFeatureLayer;
    IFeatureClass pFeatureClass;
    INetworkClass pNetworkClass;
    IGeometricNetwork pGeometricNetwork;
    INetwork pNetwork;

    ITraceFlowSolverGEN pTraceFlowSolverGEN;
    INetSolver pNetSolver;
    INetElements pNetElements;

    IFeatureCursor pFeatureCursor;
    IFeature pFeature;

    INetFlag pNetFlag;
    IJunctionFlag[] pJunctionFlags;

    IEnumNetEID pUnConnectJuncEIDs;
    IEnumNetEID pUnConnectEdgeEIDs;

    //マップ取得
    pMxDoc = pApp.Document as IMxDocument;
    pMap = pMxDoc.FocusMap;

    //TOCの一番上のフィーチャレイヤを取得
    pFeatureLayer = pMap.get_Layer(0) as IFeatureLayer;

    //フィーチャレイヤからジオメトリックネットワーク取得
    pFeatureClass = pFeatureLayer.FeatureClass;
    pNetworkClass = pFeatureClass as INetworkClass;
    pGeometricNetwork = pNetworkClass.GeometricNetwork;
    pNetwork = pGeometricNetwork.Network;

    //TraceFlowSolverGEN作成
    pTraceFlowSolverGEN = new TraceFlowSolverClass();
    pNetSolver = pTraceFlowSolverGEN as INetSolver;
    pNetSolver.SourceNetwork = pNetwork;
    pNetElements = pNetwork as INetElements;

    //最初のフィーチャ取得
    pFeatureCursor = pFeatureClass.Search(null, false);
    pFeature = pFeatureCursor.NextFeature();

    //ジャンクションフラグ作成
    pNetFlag = new JunctionFlagClass();
    pNetFlag.UserClassID = pFeatureClass.FeatureClassID;
    pNetFlag.UserID = pFeature.OID;
    pNetFlag.UserSubID = 0;
    pJunctionFlags = new IJunctionFlag[] { pNetFlag as IJunctionFlag };

    //ジャンクションフラグを追加
    pTraceFlowSolverGEN.PutJunctionOrigins(ref pJunctionFlags);

    pTraceFlowSolverGEN.TraceIndeterminateFlow = true;

    try
    {
        EnableWindow((System.IntPtr)ArcMap.Application.hWnd, false);


        /*
         * 切断解析実行
         * この処理中にArcMapをクリックすると例外が発生
         */
        for (int i = 1; i < 100; i++)
        {
            pTraceFlowSolverGEN.FindFlowUnreachedElements(
                esriFlowMethod.esriFMConnected, esriFlowElements.esriFEJunctionsAndEdges,
                out pUnConnectJuncEIDs, out pUnConnectEdgeEIDs);

            MessageBox.Show("UnConnect Jnctions Count: " + pUnConnectJuncEIDs.Count.ToString() + "\nUnConnectEdge Count: " + pUnConnectEdgeEIDs.Count.ToString());

        }
        EnableWindow((System.IntPtr)ArcMap.Application.hWnd, true);
        MessageBox.Show("OK");

    }
    catch (System.Exception Err)
    {
        EnableWindow((System.IntPtr)ArcMap.Application.hWnd, true);
        MessageBox.Show(Err.Message);
    }
}