1 //(一)编写参数类“AlgoParams.cs” 2 public class AlgoParams 3 { 4 /// 5 /// 要拷贝的路径 6 /// 7 public string OpenFilePath; 8 9 /// 10 /// 拷贝到的路径 11 /// 12 public string SaveFilePath; 13 } 14 //(二)构造算法类“Algo.cs” 15 public class Algo : PIE.SystemAlgo.BaseSystemAlgo 16 { 17 /// 18 /// 构造函数 19 /// 20 public Algo() 21 { 22 23 } 24 25 /// 26 /// 执行算法 27 /// 28 /// 是否执行成功 29 public override bool Execute() 30 { 31 AlgoParams m_AlgoParams = (AlgoParams)this.Params; 32 if (string.IsNullOrEmpty(m_AlgoParams.OpenFilePath) || string.IsNullOrEmpty(m_AlgoParams.SaveFilePath)) return false; 33 File.Copy(m_AlgoParams.OpenFilePath, m_AlgoParams.SaveFilePath, true); 34 return true; 35 } 36 } 37 //(三)新建窗体类“FormTest.cs” 38 public partial class FormTest : Form 39 { 40 /// 41 /// 算法参数 42 /// 43 private AlgoTest.AlgoParams m_Parm = new AlgoParams(); 44 45 /// 46 /// 构造函数 47 /// 48 public FormTest() 49 { 50 InitializeComponent(); 51 } 52 53 /// 54 /// 算法参数 55 /// 56 public AlgoParams Params 57 { 58 get { return m_Parm; } 59 set { this.m_Parm = value; } 60 } 61 62 private void buttonEdit_save_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) 63 { 64 SaveFileDialog save = new SaveFileDialog(); 65 save.Title = "另存为"; 66 save.Filter = "Raster Files|*.tif"; 67 save.FileName = ""; 68 if (save.ShowDialog() == DialogResult.OK) 69 { 70 this.buttonEdit_save.Text = save.FileName; 71 } 72 } 73 74 private void simpleButton_ok_Click(object sender, EventArgs e) 75 { 76 if (string.IsNullOrEmpty(this.buttonEdit_tif.Text) || string.IsNullOrEmpty(this.buttonEdit_save.Text)) 77 { 78 MessageBox.Show("请输入路径!"); 79 return; 80 } 81 m_Parm.OpenFilePath = this.buttonEdit_tif.Text; 82 m_Parm.SaveFilePath = this.buttonEdit_save.Text; 83 this.DialogResult = DialogResult.OK; 84 } 85 86 private void simpleButton_cancel_Click(object sender, EventArgs e) 87 { 88 this.Close(); 89 } 90 91 private void buttonEdit_tif_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) 92 { 93 OpenFileDialog open = new OpenFileDialog(); 94 open.Filter = "Raster Files|*.tif"; 95 open.Title = "选择栅格文件"; 96 open.Multiselect = false; 97 if (open.ShowDialog() != DialogResult.OK) return; 98 string filePath = open.FileName; 99 100 IRasterDataset dataSet = DatasetFactory.OpenRasterDataset(filePath, OpenMode.ReadOnly);101 if (dataSet == null)102 {103 MessageBox.Show("文件打开失败!");104 return;105 }106 if (!dataSet.Open(filePath, OpenMode.ReadOnly))107 {108 MessageBox.Show("文件打开失败!");109 return;110 }111 this.buttonEdit_tif.Text = filePath;112 }113 }114 //(四)写Command类实现算法调用115 public class AlgoCommand: DesktopCommand116 {117 /// 118 /// 构造函数119 /// 120 public AlgoCommand()121 {122 this.Caption = "测试算法";123 this.Name = "AlgoCommand";124 this.ToolTip = "测试算法";125 this.Checked = false;126 this.Enabled = false;127 }128 129 /// 130 /// 单击方法131 /// 132 public override void OnClick()133 {134 #region 1、参数设置135 if (!this.Enabled) return;136 FormTest frmTest = new FormTest();137 if (frmTest.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;138 139 AlgoParams info = frmTest.Params;140 PIE.SystemAlgo.ISystemAlgo algo = PIE.SystemAlgo.AlgoFactory.Instance().CreateAlgo("AlgoTest.dll", "AlgoTest.Algo");141 if (algo == null) return;142 143 PIE.SystemAlgo.ISystemAlgoEvents algoEvents = algo as PIE.SystemAlgo.ISystemAlgoEvents;144 algo.Name = "测试算法";145 algo.Params = info;146 algoEvents.OnExecuteCompleted += OnAlgoExcuteCompleted;147 algoEvents.OnProgressChanged += OnAlgoProgressChanged;148 #endregion149 150 //2、算法执行151 PIE.AxControls.IStatusBar statusBar = m_Application.StatusBar;152 statusBar.ShowProgress(0, 100, "");153 PIE.SystemAlgo.AlgoFactory.Instance().AsynExecuteAlgo(algo);154 }155 156 /// 157 /// 算法执行完成事件158 /// 159 /// 算法160 private void OnAlgoExcuteCompleted(PIE.SystemAlgo.ISystemAlgo algo)161 {162 PIE.AxControls.IStatusBar statusBar = m_Application.StatusBar;163 statusBar.UpdateProgress(100, "");164 statusBar.HideProgress();165 166 //3、结果显示167 AlgoParams info = (AlgoParams)algo.Params;168 PIE.Carto.ILayer layer = PIE.Carto.LayerFactory.CreateDefaultLayer(info.SaveFilePath);169 m_HookHelper.FocusMap.AddLayer(layer);170 m_HookHelper.ActiveView.PartialRefresh(PIE.Carto.ViewDrawPhaseType.ViewAll);171 172 PIE.SystemAlgo.ISystemAlgoEvents algoEvents = algo as PIE.SystemAlgo.ISystemAlgoEvents;173 algoEvents.OnExecuteCompleted -= OnAlgoExcuteCompleted;174 algoEvents.OnProgressChanged -= OnAlgoProgressChanged;175 }176 177 /// 178 /// 算法进度变化事件179 /// 180 /// 进度181 /// 进度信息182 /// 算法183 /// int 184 private int OnAlgoProgressChanged(double complete, string msg, PIE.SystemAlgo.ISystemAlgo algo)185 {186 PIE.AxControls.IStatusBar statusBar = m_Application.StatusBar;187 statusBar.UpdateProgress((int)complete, msg);188 return 0;189 }190 }