public bool RunTool(string toolName, IVariantArray parameters, ITrackCancel TC, bool showResultDialog) { GeoProcessor gp = new GeoProcessor(); IGeoProcessorResult result = null; // Execute the tool try { result = (IGeoProcessorResult)gp.Execute(toolName, parameters, TC); string re = result.GetOutput(0).GetAsText(); if (showResultDialog) { ReturnMessages(result, ""); } if (result.MaxSeverity == 2) //error { return(false); } else { return(true); } } catch (COMException err) { MessageBox.Show(err.Message + " in RunTool"); ReturnMessages(result, ""); } catch (Exception err) { MessageBox.Show(err.Message + " in RunTool"); ReturnMessages(result, ""); } return(false); }
/// <summary>Executes a GP tool with user cancel exception handling</summary> public IGeoProcessorResult ExecuteTool(string tool, IVariantArray paramArray, string messageName = "") { CheckCancel(); try { string message = string.Empty; if (!string.IsNullOrEmpty(messageName)) { message = RESMGR.GetString(messageName); } if (string.IsNullOrEmpty(message)) { message = tool; } StartTaskMessage(message); return(GP.Execute(tool, paramArray, _trackCancel)); } catch { CheckCancel(); throw; } finally { EndTaskMessage(); } }
//获得转换的方法和转换的参数 private void GetTnsInfo(ref string strMethod, ref IVariantArray pArray) { pArray = new VarArrayClass(); if (this.cmboTnsMethod.SelectedIndex < 0) { return; } DataTable dt = this.dtTns.DataSource as DataTable; strMethod = this.cmboTnsMethod.Text; if (strMethod == "基于地心三参数转换法") { pArray.Add("PARAMETER['X_Axis_Translation'," + GetTnsValue("X偏移(米)", dt).ToString() + "]"); pArray.Add("PARAMETER['Y_Axis_Translation'," + GetTnsValue("Y偏移(米)", dt).ToString() + "]"); pArray.Add("PARAMETER['Z_Axis_Translation'," + GetTnsValue("Z偏移(米)", dt).ToString() + "]"); } else if (strMethod == "位置矢量法") { pArray.Add("PARAMETER['X_Axis_Translation'," + GetTnsValue("X偏移(米)", dt).ToString() + "]"); pArray.Add("PARAMETER['Y_Axis_Translation'," + GetTnsValue("Y偏移(米)", dt).ToString() + "]"); pArray.Add("PARAMETER['Z_Axis_Translation'," + GetTnsValue("Z偏移(米)", dt).ToString() + "]"); pArray.Add("PARAMETER['X_Axis_Rotation'," + GetTnsValue("X旋转(秒)", dt).ToString() + "]"); pArray.Add("PARAMETER['Y_Axis_Rotation'," + GetTnsValue("Y旋转(秒)", dt).ToString() + "]"); pArray.Add("PARAMETER['Z_Axis_Rotation'," + GetTnsValue("Z旋转(秒)", dt).ToString() + "]"); pArray.Add("PARAMETER['Scale_Difference'," + GetTnsValue("比例因子(百万分率)", dt).ToString() + "]"); } else if (strMethod == "") { } string strArr = ""; for (int i = 0; i < pArray.Count; i++) { string strTemp = pArray.get_Element(i).ToString(); if (strArr == "") { strArr = strTemp; } else { strArr = strArr + "," + strTemp; } } strMethod = "GEOGTRAN[METHOD['" + GetMethod(strMethod) + "']," + strArr; }
/// <summary> /// Deserialize the argument object from the stream provided /// </summary> /// <param name="data">Xml stream to deserialize the argument object from</param> public void Deserialize(IXMLSerializeData data) { int nameIndex = data.Find("Names"); int valIndex = data.Find("Values"); if (nameIndex != -1 && valIndex != -1) { IStringArray myNames = (IStringArray)data.GetVariant(nameIndex); IVariantArray myValues = (IVariantArray)data.GetVariant(valIndex); for (int i = 0; i < myNames.Count; ++i) { myProperties.SetProperty(myNames.get_Element(i), myValues.get_Element(i)); } } }
protected virtual void runTool(Geoprocessor geoprocessor, string name, IGPProcess process, IVariantArray param, ITrackCancel TC) { // Set the overwrite output option to true geoprocessor.OverwriteOutput = true; // Execute the tool try { geoprocessor.Execute(name, param, TC);//"Clip_analysis" ReturnMessages(geoprocessor); } catch (Exception err) { Console.WriteLine(err.Message); ReturnMessages(geoprocessor); } }
//gp的执行 private void ExecuteGP(IVariantArray parameters, string strToolName) { ITrackCancel pTrackCancel = null; //找到tool ESRI.ArcGIS.Geoprocessing.GeoProcessor _geoPro = new GeoProcessor(); pTrackCancel = new TrackCancelClass(); IVariantArray pVArray = new VarArrayClass(); IGPEnvironmentManager pgpEnv = new GPEnvironmentManager(); IGPMessages pGpMessages; //= _geoPro.Validate(parameters, false, pgpEnv); IGPComHelper pGPCOMHelper = new GpDispatch(); //这里是关键,如果不赋值的话,那么就会报错 IGPEnvironmentManager pEnvMgr = pGPCOMHelper.EnvironmentManager; pgpEnv.PersistAll = true; pGpMessages = new GPMessagesClass(); // Execute the model tool by name. _geoPro.Execute(strToolName, parameters, pTrackCancel); }
/// <summary> /// 执行自定义Model GP /// </summary> /// <param name="parameters">GP 输入参数</param> /// <param name="gpname">GP 名字</param> /// <param name="modelPath">工具箱路径</param> /// <param name="tempFolder">临时工作区路径</param> /// <returns></returns> public bool RunModelTool(string modelPath, string tempFolder, IVariantArray parameters, string gpname) { if (parameters == null) { return(false); } Geoprocessor geoprocessor = new Geoprocessor(); geoprocessor.OverwriteOutput = true; geoprocessor.SetEnvironmentValue("scratchworkspace", tempFolder); try { geoprocessor.AddToolbox(modelPath); geoprocessor.Execute(gpname, parameters, null); Marshal.ReleaseComObject(parameters); return(true); } catch { return(false); } }
/// <summary>Executes a GP tool with user cancel exception handling</summary> public IGeoProcessorResult ExecuteTool(string tool, IVariantArray paramArray, string messageName = "") { CheckCancel(); try { string message = string.Empty; if (!string.IsNullOrEmpty(messageName)) message = RESMGR.GetString(messageName); if (string.IsNullOrEmpty(message)) message = tool; StartTaskMessage(message); return GP.Execute(tool, paramArray, _trackCancel); } catch { CheckCancel(); throw; } finally { EndTaskMessage(); } }
/// <summary> /// Get all the properties associated with a particular band of the dataset. /// </summary> /// <param name="dataset">Dataset to get the property from.</param> /// <param name="bandIndex">band for which to get all properties.</param> /// <param name="bandKeys">String Array passed in by reference to fill with all keys.</param> /// <param name="bandProperties">Object array passed in by reference to fill with all properties.</param> static void GetAllBandProperties(IDataset dataset, int bandIndex, ref IStringArray bandKeys, ref IVariantArray bandProperties) { IRasterKeyProperties rasterKeyProps = (IRasterKeyProperties)dataset; rasterKeyProps.GetAllBandProperties(bandIndex, out bandKeys, out bandProperties); }
/// <summary> /// Get all the properties associated with the dataset. /// </summary> /// <param name="dataset">Dataset to get the property from.</param> /// <param name="allKeys">String Array passed in by reference to fill with all keys.</param> /// <param name="allProperties">Object array passed in by reference to fill with all properties.</param> static void GetAllProperties(IDataset dataset, ref IStringArray allKeys, ref IVariantArray allProperties) { IRasterKeyProperties rasterKeyProps = (IRasterKeyProperties)dataset; rasterKeyProps.GetAllProperties(out allKeys, out allProperties); }