Exemple #1
0
        /// <summary>
        /// Occurs when this tool is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            try
            {
                m_hookHelper      = new HookHelperClass();
                m_hookHelper.Hook = hook;
                if (m_hookHelper.ActiveView == null)
                {
                    m_hookHelper = null;
                }
            }
            catch
            {
                m_hookHelper = null;
            }

            if (m_hookHelper == null)
            {
                base.m_enabled = false;
            }
            else
            {
                base.m_enabled = true;
            }

            // TODO:  Add other initialization code
            pFWorkspace = NetWorkAnalysClass.MemoryWorkspace as IFeatureWorkspace;

            //如果未建立FeatureClass 则创建
            try
            {
                inputFClass = pFWorkspace.OpenFeatureClass("Stops");
            }
            catch (Exception e)
            {
                inputFClass = NetWorkAnalysClass.CreateFeatureClass(pFWorkspace, "Stops");
            }
        }
        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add ShortPathSolveCommand.OnClick implementation
            IFeatureWorkspace pFWorkspace = NetWorkAnalysClass.MemoryWorkspace as IFeatureWorkspace;


            //networkDataset = NetWorkAnalysClass.OpenPathNetworkDataset("RouteNetwork", "BaseData");
            networkDataset = NetWorkAnalysClass.NetWorkDataSet;

            //通过网络数据集创建网络分析上下文
            m_NAContext = NetWorkAnalysClass.CreatePathSolverContext(networkDataset);


            //打开要素数据集

            //如果这两个报异常 说明FeatureClass为空 新建
            try
            {
                inputFClass = pFWorkspace.OpenFeatureClass("Stops");
            }
            catch (Exception e)
            {
                inputFClass = NetWorkAnalysClass.CreateFeatureClass(pFWorkspace, "Stops");
            }


            try
            {
                barriesFClass = pFWorkspace.OpenFeatureClass("Barries");
            }
            catch (Exception e)
            {
                barriesFClass = NetWorkAnalysClass.CreateFeatureClass(pFWorkspace, "Barries");
            }

            if (IfLayerExist("NetworkDataset") == false)
            {
                ILayer        layer;
                INetworkLayer networkLayer;
                networkLayer = new NetworkLayerClass();
                networkLayer.NetworkDataset = networkDataset;
                layer      = networkLayer as ILayer;
                layer.Name = "NetworkDataset";
                m_hookHelper.ActiveView.FocusMap.AddLayer(layer);
                layer.Visible = false;
            }

            if (IfLayerExist(m_NAContext.Solver.DisplayName) == true)
            {
                for (int i = 0; i < m_hookHelper.FocusMap.LayerCount; i++)
                {
                    if (m_hookHelper.FocusMap.get_Layer(i).Name == m_NAContext.Solver.DisplayName)
                    {
                        m_hookHelper.FocusMap.DeleteLayer(m_hookHelper.FocusMap.get_Layer(i));
                    }
                }
            }

            INALayer naLayer = m_NAContext.Solver.CreateLayer(m_NAContext);
            ILayer   pLayer  = naLayer as ILayer;

            pLayer.Name = m_NAContext.Solver.DisplayName;
            m_hookHelper.ActiveView.FocusMap.AddLayer(pLayer);

            if (inputFClass.FeatureCount(null) < 2)
            {
                MessageBox.Show("站点数过少!");
                return;
            }

            IGPMessages gpMessages = new GPMessagesClass();

            //加载站点要素,并设置容差
            NetWorkAnalysClass.LoadNANetworkLocations("Stops", inputFClass, m_NAContext, 80);
            //加载障碍点要素,并设置容差
            NetWorkAnalysClass.LoadNANetworkLocations("Barriers", barriesFClass, m_NAContext, 5);
            //创建网络分析对象
            INASolver naSolver = m_NAContext.Solver;

            try
            {
                naSolver.Solve(m_NAContext, gpMessages, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show("未能找到有效路径" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                return;
            }

            for (int i = 0; i < m_hookHelper.FocusMap.LayerCount; i++)
            {
                if (m_hookHelper.FocusMap.get_Layer(i).Name == m_NAContext.Solver.DisplayName)
                {
                    ICompositeLayer pCompositeLayer = m_hookHelper.FocusMap.get_Layer(i) as ICompositeLayer;
                    {
                        for (int t = 0; t < pCompositeLayer.Count; t++)
                        {
                            ILayer pResultLayer = pCompositeLayer.get_Layer(t);
                            if (pResultLayer.Name == "Stops" || pResultLayer.Name == "Barriers")
                            {
                                pResultLayer.Visible = false;
                                continue;
                            }
                        }
                    }
                }
            }

            IGeoDataset geoDataset;
            IEnvelope   envelope;

            geoDataset = m_NAContext.NAClasses.get_ItemByName("Routes") as IGeoDataset;
            envelope   = geoDataset.Extent;
            if (!envelope.IsEmpty)
            {
                envelope.Expand(1.1, 1.1, true);
            }
            m_hookHelper.ActiveView.Extent = envelope;
            m_hookHelper.ActiveView.Refresh();
        }