C#与Arcgis Engine10.2-----------管道爆裂位置分析

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;




using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.NetworkAnalysis;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.SystemUI;


namespace 爆管分析
{
    public partial class Form1 : Form
    {


        private INetSolver netSolver;
        private IGeometricNetwork geometricNetwork;
        private ITraceFlowSolverGEN traceFlowSolverGEN;
        private List<int> listEdgeBarrierEIDs;
        private List<int> listBurstPipeEIDs;
        private List<int> listClosedValveEIDs;


        public Form1()
        {
            InitializeComponent();
            traceFlowSolverGEN = new TraceFlowSolverClass();
            netSolver = traceFlowSolverGEN as INetSolver;
            listBurstPipeEIDs = new List<int>();
            listClosedValveEIDs = new List<int>();
        }


        private void ToolStripMenuItemOpen_Click(object sender, EventArgs e)
        {
            //选择需要打开的地图文档,并加载文档中的几何网络
            OpenFileDialog openFilgeDialog = new OpenFileDialog();
            openFilgeDialog.Filter = "ArcMap Document (*.mxd)|*.mxd";
            if (openFilgeDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                axMapControl1.LoadMxFile(openFilgeDialog.FileName);
                LoadGeometricNetwork();
            }
        }
        public static IColor GetColorByRGBValue(int red, int green, int blue)
        {
            IRgbColor rgbColor = new RgbColorClass();
            rgbColor.Red = red;
            rgbColor.Green = green;
            rgbColor.Blue = blue;
            return rgbColor as IColor;
        }
        private int GetFeatureClassIDByName(string featureClassName, IMap map)
        {
            int id = 0;
            IFeatureLayer featureLayer;
            for (int i = 0; i < map.LayerCount; i++)
            {
                featureLayer = map.get_Layer(i) as IFeatureLayer;
                if (featureLayer.FeatureClass.AliasName == featureClassName)
                    id = featureLayer.FeatureClass.FeatureClassID;
            }
            return id;
        }
        private IFeatureClass GetFeatureClassByID(int userClassID, IMap map)
        {
            IFeatureClass featureClass = null;
            for (int i = 0; i < map.LayerCount; i++)
            {
                featureClass = ((IFeatureLayer)map.get_Layer(i)).FeatureClass;
                if (featureClass.FeatureClassID == userClassID)
                    return featureClass;
            }
            return null;
        }
        private void DrawTraceResults(IEnumNetEID junctionEIDs, IEnumNetEID edgeEIDs, IColor color)
        {
            INetElements netElements = geometricNetwork.Network as INetElements;
            int userClassID = -1;
            int userID = -1;
            int userSubID = -1;
            int eid = -1;
            IFeature feature;
            IFeatureClass featureClass;
            //设置管点和管线显示的Symbol
            ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass();
            simpleMarkerSymbol.Color = color;
            simpleMarkerSymbol.Size = 6;
            simpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;
            ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
            simpleLineSymbol.Color = color;
            simpleLineSymbol.Width = 2;
            simpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
            IElement element;
            //如果分析结果中包含管点,则查询其对应的空间要素并绘制
            if (junctionEIDs != null)
            {
                for (int i = 0; i < junctionEIDs.Count; i++)
                {
                    eid = junctionEIDs.Next();
                    netElements.QueryIDs(eid, esriElementType.esriETJunction, out userClassID, out userID, out userSubID);
                    featureClass = GetFeatureClassByID(userClassID, axMapControl1.Map);
                    if (featureClass != null)
                    {
                        feature = featureClass.GetFeature(userID);


                        element = new MarkerElementClass();
                        element.Geometry = feature.Shape;
                        ((IMarkerElement)element).Symbol = simpleMarkerSymbol;
                        ((IElementProperties)element).Name = "Result";
                        ((IGraphicsContainer)axMapControl1.ActiveView).AddElement(element, 0);
                    }
                }
            }
            //如果分析结果中包含管线,则查询其对应的空间要素并绘制
            if (edgeEIDs != null)
            {
                for (int i = 0; i < edgeEIDs.Count; i++)
                {
                    eid = edgeEIDs.Next();
                    netElements.QueryIDs(eid, esriElementType.esriETEdge, out userClassID, out userID, out userSubID);
                    featureClass = GetFeatureClassByID(userClassID, axMapControl1.Map);
                    feature = featureClass.GetFeature(userID);


                    element = new LineElementClass();
                    element.Geometry = feature.Shape;
                    ((ILineElement)element).Symbol = simpleLineSymbol;
                    ((IElementProperties)element).Name = "Result";
                    ((IGraphicsContainer)axMapControl1.ActiveView).AddElement(element, 0);
                }
            }
            //刷新地图中的图形
            axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, axMapControl1.ActiveView.Extent);
        }
        private void ClearElements(IActiveView activeView, string elementName)
        {
            IGraphicsContainer graphicsContainer = activeView as IGraphicsContainer;
            graphicsContainer.Reset();
            IElement element = graphicsContainer.Next();
            while (element != null)
            {
                if (((IElementProperties)element).Name == elementName)
                    graphicsContainer.DeleteElement(element);
                element = graphicsContainer.Next();
            }
            activeView.Refresh();
        }
        private void LoadGeometricNetwork()
        {
            //得到当前几何网络所在的FeatureDataset
            IMap map = axMapControl1.Map;
            IFeatureLayer featureLayer = map.get_Layer(0) as IFeatureLayer;
            IFeatureDataset featureDataset = featureLayer.FeatureClass.FeatureDataset;
            //得到接口转换获得当前所加载的几何网络
            INetworkCollection2 networkCollection2 = featureDataset as INetworkCollection2;
            geometricNetwork = networkCollection2.get_GeometricNetwork(0);
            //得到网络分析器所使用的逻辑网络
            netSolver.SourceNetwork = geometricNetwork.Network;
            //通过接口转换得到几何网络的名称,并加载到相应控件中
            IDataset dataset = geometricNetwork as IDataset;
            toolStripComboBoxNetworksName.Items.Add(dataset.Name);
            toolStripComboBoxNetworksName.SelectedIndex = 0;
        }


        private void toolStripButtonAddBGFlag_Click(object sender, EventArgs e)
        {
            ToolSelectBurstPipe toolSelectBurstPipe = new ToolSelectBurstPipe();
            toolSelectBurstPipe.GeometricNetwork = geometricNetwork;
            toolSelectBurstPipe.ListBurstPipeEIDs = listBurstPipeEIDs;
            toolSelectBurstPipe.OnCreate(axMapControl1.Object);
            axMapControl1.CurrentTool = toolSelectBurstPipe as ITool;
            this.toolStripStatusLabel.Text = "请选择爆管的管线";
        }


        private void ToolStripMenuItemBurstAnalysis_Click(object sender, EventArgs e)
        {
            try
            {
                ClearElements(axMapControl1.ActiveView, "BurstResult");
                ClearElements(axMapControl1.ActiveView, "Result");
                //先清空所分析网络的管点标识
                IJunctionFlag[] junctionFlagArray = new IJunctionFlag[0];
                traceFlowSolverGEN.PutJunctionOrigins(ref junctionFlagArray);
                //查询并定义所需关闭的阀门为网络分析标识
                INetElements netElements = geometricNetwork.Network as INetElements;
                int userClassID = 0;
                int userID = 0;
                int userSubID = 0;
                netElements.QueryIDs(listBurstPipeEIDs[0], esriElementType.esriETEdge, out userClassID, out userID, out userSubID);
                INetFlag edgeFlag = new EdgeFlagClass();
                edgeFlag.UserClassID = userClassID;
                edgeFlag.UserID = userID;
                edgeFlag.UserSubID = userSubID;
                IEdgeFlag[] edgeFlagArray = new IEdgeFlag[1];
                edgeFlagArray[0] = edgeFlag as IEdgeFlag;
                traceFlowSolverGEN.PutEdgeOrigins(ref edgeFlagArray);
                //首先进行上游追踪分析,返回并用蓝色绘制上游的所有管点和管线
                IEnumNetEID upstreamJunctionEIDs = new EnumNetEIDArrayClass();
                IEnumNetEID upstreamEdgeEIDs = new EnumNetEIDArrayClass();
                int count = edgeFlagArray.Length;
                object[] segmentCosts = new object[count];
                traceFlowSolverGEN.FindSource(esriFlowMethod.esriFMUpstream, esriShortestPathObjFn.esriSPObjFnMinSum,
                    out upstreamJunctionEIDs, out upstreamEdgeEIDs, count, ref segmentCosts);
                DrawTraceResults(upstreamJunctionEIDs, upstreamEdgeEIDs, GetColorByRGBValue(0, 0, 255));
                //再进行下游追踪分析,以便得到下游的阀门
                IEnumNetEID downstreamJunctionEIDs = new EnumNetEIDArrayClass();
                IEnumNetEID downstreamEdgeEIDs = new EnumNetEIDArrayClass();
                traceFlowSolverGEN.FindFlowElements(esriFlowMethod.esriFMDownstream, esriFlowElements.esriFEJunctions,
                    out downstreamJunctionEIDs, out downstreamEdgeEIDs);
                //对分析结果进行遍历和判断,如果该网络元素对应的空间要素为阀门,则其为需要关闭的阀门,直接结束方法的运行            
                StringBuilder sb = new StringBuilder();
                GetClosedValves(upstreamJunctionEIDs, netElements, userClassID, userID, userSubID, true, sb);
                GetClosedValves(downstreamJunctionEIDs, netElements, userClassID, userID, userSubID, false, sb);
                MessageBox.Show(sb.ToString());
            }
            catch { }
        }


        private void ToolStripMenuItemAffectArea_Click(object sender, EventArgs e)
        {
            try
            {
                //首先查询并定义所需关闭的阀门为网络分析标识
                INetElements netElements = geometricNetwork.Network as INetElements;
                int userClassID = 0;
                int userID = 0;
                int userSubID = 0;
                IJunctionFlag[] junctionFlagArray = new IJunctionFlag[listClosedValveEIDs.Count];
                for (int i = 0; i < listClosedValveEIDs.Count; i++)
                {
                    netElements.QueryIDs(listClosedValveEIDs[i], esriElementType.esriETJunction, out userClassID, out userID, out userSubID);
                    INetFlag junctionFlag = new JunctionFlagClass();
                    junctionFlag.UserClassID = userClassID;
                    junctionFlag.UserID = userID;
                    junctionFlag.UserSubID = userSubID;
                    junctionFlagArray[i] = junctionFlag as IJunctionFlag;
                }
                traceFlowSolverGEN.PutJunctionOrigins(ref junctionFlagArray);
                //再进行下游追踪分析,返回并用红色绘制分析结果
                IEnumNetEID junctionEIDs = new EnumNetEIDArrayClass();
                IEnumNetEID edgeEIDs = new EnumNetEIDArrayClass();
                traceFlowSolverGEN.FindFlowElements(esriFlowMethod.esriFMDownstream, esriFlowElements.esriFEJunctionsAndEdges,
                            out junctionEIDs, out edgeEIDs);
                DrawTraceResults(junctionEIDs, edgeEIDs, GetColorByRGBValue(255, 0, 0));
            }
            catch { }
        }


        private void ToolStripMenuItemClearBurstAnalysisResults_Click(object sender, EventArgs e)
        {
            //清除爆管分析的各类标识和结果,并清空爆管和阀门元素记录
            ClearElements(axMapControl1.ActiveView, "BurstPipe");
            ClearElements(axMapControl1.ActiveView, "BurstResult");
            ClearElements(axMapControl1.ActiveView, "Result");
            listBurstPipeEIDs.Clear();
            listClosedValveEIDs.Clear();
        }
         private void GetClosedValves(IEnumNetEID junctionEIDs, INetElements netElements,
            int userClassID, int userID, int userSubID, bool isUpstreamValve, StringBuilder sb)
        {
            int eid = -1;
            junctionEIDs.Reset();
            for (int i = 0; i < junctionEIDs.Count; i++)
            {
                eid = junctionEIDs.Next();
                netElements.QueryIDs(eid, esriElementType.esriETJunction, out userClassID, out userID, out userSubID);
                if (userClassID == GetFeatureClassIDByName("famen", axMapControl1.Map))
                {
                    listClosedValveEIDs.Add(eid);
                    IFeature feature = GetFeatureClassByID(userClassID, axMapControl1.Map).GetFeature(userID);
                    ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass();
                    simpleMarkerSymbol.Color = Form1.GetColorByRGBValue(255, 0, 0);
                    simpleMarkerSymbol.Size = 12;
                    simpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSSquare;
                    IElement element = new MarkerElementClass();
                    element.Geometry = feature.Shape;
                    ((IMarkerElement)element).Symbol = simpleMarkerSymbol;
                    ((IElementProperties)element).Name = "BurstResult";
                    ((IGraphicsContainer)axMapControl1.ActiveView).AddElement(element, 0);
                    axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, axMapControl1.ActiveView.Extent);
                    sb.AppendLine("应关闭的阀门编号为:" + feature.get_Value(feature.Fields.FindField("阀门编号")).ToString());
                    return;
                }
            }
        }


       
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值