/// <summary> /// Solves this instance. /// </summary> /// <returns>IFeatureClass.</returns> public IFeatureClass Solve() { log.WriteLog("Solving..."); IGPMessages gpMessages = new GPMessagesClass(); try { ConfigureSolverSettings(); m_NAContext.Solver.Solve(m_NAContext, gpMessages, null); return m_NAContext.NAClasses.get_ItemByName("SALines") as IFeatureClass; } catch (Exception e) { log.WriteLog("GP执行失败:" + e.Message); return null; } finally { ////记录GPMessages string sGPMessages = GetGPMessagesAsString(gpMessages); log.WriteLog("GP执行信息:" + sGPMessages); log.WriteLog("Solve完成!"); } } /// <summary> /// Prepare the solver /// </summary> private void ConfigureSolverSettings() { ConfigureSettingsSpecificToServiceAreaSolver(); ConfigureGenericSolverSettings(); UpdateContextAfterChangingSettings(); } /// <summary> /// Update settings that only apply to the Service Area /// </summary> private void ConfigureSettingsSpecificToServiceAreaSolver() { INAServiceAreaSolver naSASolver = m_NAContext.Solver as INAServiceAreaSolver; naSASolver.DefaultBreaks = ParseBreaks("3600"); naSASolver.MergeSimilarPolygonRanges = false; naSASolver.OutputPolygons = esriNAOutputPolygonType.esriNAOutputPolygonNone; naSASolver.OverlapLines = false; naSASolver.SplitLinesAtBreaks = true; naSASolver.TravelDirection = esriNATravelDirection.esriNATravelDirectionFromFacility; naSASolver.OutputLines = esriNAOutputLineType.esriNAOutputLineTrueShape; } /// <summary> /// Update settings that apply to all solvers /// </summary> private void ConfigureGenericSolverSettings() { INASolverSettings naSolverSettings = m_NAContext.Solver as INASolverSettings; naSolverSettings.ImpedanceAttributeName = "Time"; // set the oneway restriction, if necessary IStringArray restrictions = naSolverSettings.RestrictionAttributeNames; restrictions.RemoveAll(); naSolverSettings.RestrictionAttributeNames = restrictions; //naSolverSettings.RestrictUTurns = esriNetworkForwardStarBacktrack.esriNFSBNoBacktrack; } /// <summary> /// When the solver has been update, the context must be updated as well /// </summary> private void UpdateContextAfterChangingSettings() { IDatasetComponent datasetComponent = m_NAContext.NetworkDataset as IDatasetComponent; IDENetworkDataset deNetworkDataset = datasetComponent.DataElement as IDENetworkDataset; m_NAContext.Solver.UpdateContext(m_NAContext, deNetworkDataset, new GPMessagesClass()); } /// <summary> /// Prepare the text string for breaks /// </summary> /// <param name="p">The p.</param> /// <returns>IDoubleArray.</returns> private IDoubleArray ParseBreaks(string p) { String[] breaks = p.Split(' '); IDoubleArray pBrks = new DoubleArrayClass(); int firstIndex = breaks.GetLowerBound(0); int lastIndex = breaks.GetUpperBound(0); for (int splitIndex = firstIndex; splitIndex <= lastIndex; splitIndex++) { try { pBrks.Add(Convert.ToDouble(breaks[splitIndex])); } catch (FormatException) { log.WriteLog("Breaks are not properly formatted. Use only digits separated by spaces"); pBrks.RemoveAll(); return pBrks; } } return pBrks; } /// <summary> /// Gets the GP messages as string. /// </summary> /// <param name="gpMessages">The gp messages.</param> /// <returns>System.String.</returns> public string GetGPMessagesAsString(IGPMessages gpMessages) { // Gather Error/Warning/Informative Messages var messages = new StringBuilder(); if (gpMessages != null) { for (int i = 0; i < gpMessages.Count; i++) { IGPMessage gpMessage = gpMessages.GetMessage(i); string message = gpMessage.Description; switch (gpMessages.GetMessage(i).Type) { case esriGPMessageType.esriGPMessageTypeError: messages.AppendLine("Error " + gpMessage.ErrorCode + ": " + message); break; case esriGPMessageType.esriGPMessageTypeWarning: messages.AppendLine("Warning: " + message); break; default: messages.AppendLine("Information: " + message); break; } } } return messages.ToString(); }