PIE SDK算法的自定義擴展

 

1.算法功能簡介

    算法的自定義擴展容許用戶自主開發新的算法。自定義的算法必須實現PIE.SystemAlgo.BaseSystemAlgo基礎類,這樣才能被PIE的算法管理器調用起來。算法

2.算法功能實現說明

2.1. 示例簡介

    本算法用於實現柵格影像的拷貝。參數類「AlgoParams.cs」用於存放要拷貝的路徑和拷貝到的路徑,算法類「Algo.cs」用於執行算法,窗體類「FormTest.cs」用於接收用戶輸入的要拷貝的路徑和拷貝到的路徑,Command類「AlgoCommand.cs」用於實現算法調用。ide

    算法類「Algo.cs」繼承自BaseSystemAlgo,該類實現了ISystemAlgo, ISystemAlgoEvents兩個接口。如下對BaseSystemAlgo類的屬性,方法和事件進行介紹:函數

屬性介紹測試

Descriptionthis

Stringspa

描述3d

Namecode

Stringorm

名稱視頻

Params

String

參數

方法介紹

Execute()

Bool

執行算法,返回結果:是否執行成功

GetErrorInfo(ref int errCode, ref string errMsg)

Void

獲取錯誤信息,參數: errCode:錯誤信息編號,errMsg:錯誤信息描述

事件介紹

OnExecuteCompleted

OnExecuteCompletedEventHandler

執行完成事件

OnProgressChanged

OnProgressChangedEventHandler

進度變化事件

2.2. 實現步驟

第一步

編寫參數類「AlgoParams.cs」

第二步

構造算法類「Algo.cs」

第三步

新建窗體類「FormTest.cs」

第四步

寫Command類實現算法調用

2.3. 示例代碼

項目路徑

百度雲盤地址下/PIE示例程序/10.算法調用/算法的調用方式/Algo.AlgoTest

數據路徑

百度雲盤地址下/PIE示例數據/柵格數據/04.World/ World.tif

視頻路徑

百度雲盤地址下/PIE視頻教程/10.算法調用/算法的調用方式/算法的自定義擴展.avi

示例代碼

  1         //(一)編寫參數類「AlgoParams.cs」
  2          public class AlgoParams
  3     {
  4         /// <summary>
  5         /// 要拷貝的路徑
  6         /// </summary>
  7         public string OpenFilePath;
  8 
  9         /// <summary>
 10         /// 拷貝到的路徑
 11         /// </summary>
 12         public string SaveFilePath;
 13     }
 14          //(二)構造算法類「Algo.cs」
 15 public class Algo : PIE.SystemAlgo.BaseSystemAlgo
 16     {
 17         /// <summary>
 18         /// 構造函數
 19         /// </summary>
 20         public Algo()
 21         {
 22 
 23         }
 24 
 25         /// <summary>
 26         /// 執行算法
 27         /// </summary>
 28         /// <returns>是否執行成功</returns> 
 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         /// <summary>
 41         /// 算法參數
 42         /// </summary>
 43         private AlgoTest.AlgoParams m_Parm = new AlgoParams();
 44 
 45         /// <summary>
 46         /// 構造函數
 47         /// </summary>
 48         public FormTest()
 49         {
 50             InitializeComponent();
 51         }
 52 
 53         /// <summary>
 54         /// 算法參數
 55         /// </summary>
 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: DesktopCommand
116     {
117         /// <summary>
118         /// 構造函數
119         /// </summary>
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         /// <summary>
130         /// 單擊方法
131         /// </summary>
132         public override void OnClick()
133         {
134             #region 一、參數設置
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             #endregion
149 
150             //二、算法執行
151             PIE.AxControls.IStatusBar statusBar = m_Application.StatusBar;
152             statusBar.ShowProgress(0, 100, "");
153             PIE.SystemAlgo.AlgoFactory.Instance().AsynExecuteAlgo(algo);
154         }
155 
156         /// <summary>
157         /// 算法執行完成事件
158         /// </summary>
159         /// <param name="algo">算法</param>
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             //三、結果顯示
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         /// <summary>
178         /// 算法進度變化事件
179         /// </summary>
180         /// <param name="complete">進度</param>
181         /// <param name="msg">進度信息</param>
182         /// <param name="algo">算法</param>
183         /// <returns>int</returns>
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     }
View Code

2.4. 示例截圖

相關文章
相關標籤/搜索