在一個項目中,是經過小組成員共同開發的,難以免的是當項目功能集成的時候會出現不少兼容性問題,開發講究高內聚低耦合,利用Command、Tool和Control的使用,能夠提高集成的效率,PIE SDK包含大量的已經封裝好的Command和Tool,調用簡單易於實現,調試方便。ide
何時會用到Command?Command,命令,功能不須要鼠標和地圖進行交互,例如全圖顯示,點擊按鈕地圖接收到命令會自動全圖顯示;而Tool(工具)則相反,須要鼠標和地圖進行交互,例如地圖的漫遊功能,拉框放大功能等,須要鼠標在地圖上操做纔會作出反應。函數
Control是包含了控件的Command,好比比例尺控制按鈕、圖像透明度控制按鈕,經過操做Control中的控件便可實現地圖的操做。工具
下面具體介紹組件式開發中如何調用PIE SDK的Command和Tool,以及Control的拓展應用。this
Command的調用spa |
|
第一步插件 |
New一個Command對象設計 |
第二步調試 |
綁定地圖,傳遞地圖控件對象(建立插件對象OnCreate)code |
第三步orm |
調用OnClick方法 |
Tool的調用 |
|
第一步 |
New一個Tool對象 |
第二步 |
綁定地圖,傳遞地圖控件對象,(需將Tool對象轉化成ICommand,調用OnCreate方法) |
第三步 |
設置地圖當前的Tool爲new的新的Tool |
Command的拓展 |
|
第一步 |
新建一個Command的類,繼承PIE.Control.BaseCommand |
第二步 |
重寫OnCreate(),OnClick()方法 |
第三步 |
調用:根據上面的Command調用方法進行調用 |
Tool的拓展 |
|
第一步 |
新建Tool的類,繼承PIE.Controls.BaseTool |
第二步 |
根據需求能夠重寫MouseDown,MouseMove,MoudeUp等鼠標事件 |
第三步 |
調用:根據表格上面的Tool的調用方式進行調用 |
Control的拓展 |
|
第一步 |
新建一個Control的類,並繼承PIE.Controls.BaseCommandControl |
第二步 |
根據需求重寫Control對象,綁定地圖控件對象OnCreate,Enabled屬性等 |
第三步 |
調用:根據Control新建的控件類型A在界面上也設計一個相同的控件B,在調用的時候將B於A進行關聯便可 |
接口/類 |
方法/屬性 |
說明 |
PIE.SystemUI.ICommand |
OnClick |
點擊事件 |
OnCreate |
建立插件對象 |
|
PIE. lContros |
FullExtentCommand |
全圖顯示命令 |
PanTool |
漫遊工具 |
|
PIE.Controls. BaseCommand |
OnCreate |
建立插件對象 |
OnClick |
點擊事件 |
|
PIE.Controls. BaseTool |
OnMouseDown |
鼠標按下事件 |
OnMouseMove |
鼠標移動事件 |
|
OnMouseUp等 |
鼠標彈起事件 |
|
PIE.Controls. BaseCommandControl |
Control |
Control對象 |
項目路徑 |
百度雲盤地址下/PIE示例程序/ 06.Command、Tool、Control的調用和擴展 |
數據路徑 |
百度雲盤地址下/PIE示例數據/柵格數據/04.World/World.tif |
視頻路徑 |
百度雲盤地址下/PIE視頻教程/06.Command、Tool、Control的調用和擴展.avi |
示例代碼 |
|
1 FormMain.cs: 2 /// <summary> 3 /// Command調用——全圖顯示爲例 4 /// </summary> 5 /// <param name="sender"></param> 6 /// <param name="e"></param> 7 private void btn_Command_Click(object sender, EventArgs e) 8 { 9 PIE.SystemUI.ICommand cmd = new PIE.Controls.FullExtentCommand(); 10 cmd.OnCreate(mapControlMain); 11 cmd.OnClick(); 12 } 13 /// <summary> 14 /// Tool調用——以漫遊爲例 15 /// </summary> 16 /// <param name="sender"></param> 17 /// <param name="e"></param> 18 private void btn_Tool_Click(object sender, EventArgs e) 19 { 20 PIE.SystemUI.ITool tool = new PIE.Controls.PanTool(); 21 (tool as ICommand).OnCreate(mapControlMain); 22 mapControlMain.CurrentTool = tool; 23 } 24 25 /// <summary> 26 /// Command調用自定義拓展(如何本身寫Command,一加載數據爲例) 27 /// </summary> 28 /// <param name="sender"></param> 29 /// <param name="e"></param> 30 private void btn_CommandEx_Click(object sender, EventArgs e) 31 { 32 PIE.SystemUI.ICommand cmd = new Oper.AddDataCommand(); 33 cmd.OnCreate(mapControlMain); 34 cmd.OnClick(); 35 } 36 37 /// <summary> 38 /// Tool調用自定義拓展(如何本身寫Tool——以繪製面元素爲例) 39 /// </summary> 40 /// <param name="sender"></param> 41 /// <param name="e"></param> 42 private void btn_ToolEx_Click(object sender, EventArgs e) 43 { 44 PIE.SystemUI.ITool tool = new Oper.DrawElementTool(mapControlMain); 45 (tool as ICommand).OnCreate(mapControlMain); 46 mapControlMain.CurrentTool = tool; 47 } 48 49 /// <summary> 50 /// Control的拓展(以比例尺Control爲例) 51 /// </summary> 52 /// <param name="sender"></param> 53 /// <param name="e"></param> 54 private void tbn_ControlEx_Click(object sender, EventArgs e) 55 { 56 Oper.MapScaleCommandControl mapScaleComtrol= new Oper.MapScaleCommandControl(); 57 mapScaleComtrol.Control = this.combox_MapScale;//將界面的比例尺控件與mapScaleControl綁定 58 mapScaleComtrol.OnCreate(mapControlMain); 59 } 60 61 拓展類: 62 using PIE.Carto; 63 using PIE.Controls; 64 using System; 65 using System.Collections.Generic; 66 using System.Linq; 67 using System.Text; 68 using System.Windows.Forms; 69 namespace CommandAndToolDemo.Oper 70 { 71 class AddDataCommand : BaseCommand 72 { 73 /// <summary> 74 /// 構造函數 75 /// </summary> 76 public AddDataCommand() 77 { 78 this.Caption = "加載數據"; 79 this.ToolTip = "加載數據"; 80 this.Checked = true; 81 this.Enabled = false; 82 // this.Image = ""; 83 this.Name = "加載數據"; 84 } 85 86 /// <summary> 87 /// 建立插件對象 88 /// </summary> 89 /// <param name="hook"></param> 90 public override void OnCreate(object hook) 91 { 92 if (hook == null) return; 93 if (!(hook is PIE.Carto.IPmdContents)) return; 94 95 this.Enabled = true; 96 m_Hook = hook; 97 m_HookHelper.Hook = hook; 98 } 99 100 /// <summary> 101 /// 點擊事件 102 /// </summary> 103 public override void OnClick() 104 { 105 OpenFileDialog openDialog = new OpenFileDialog(); 106 openDialog.Title = "加載數據"; 107 openDialog.Filter = "Raster File|*.img;*.tif;*.dat;*.tiff|Shape File|*.shp|全部文件|*.*"; 108 if (openDialog.ShowDialog() != DialogResult.OK) return; 109 110 ILayer layer = LayerFactory.CreateDefaultLayer(openDialog.FileName); 111 if (layer == null) return; 112 m_HookHelper.ActiveView.FocusMap.AddLayer(layer); 113 m_HookHelper.ActiveView.PartialRefresh(ViewDrawPhaseType.ViewAll); 114 } 115 } 116 } 117 118 using PIE.AxControls; 119 using PIE.Carto; 120 using PIE.Display; 121 using PIE.Geometry; 122 using System; 123 using System.Collections.Generic; 124 using System.Linq; 125 using System.Text; 126 using System.Windows.Forms; 127 128 namespace CommandAndToolDemo.Oper 129 { 130 class DrawElementTool : PIE.Controls.BaseTool 131 { 132 #region 成員變量 133 134 /// <summary> 135 /// 面元素 136 /// </summary> 137 /// 138 IPolygonElement m_PolygonEle = null; 139 140 /// <summary> 141 /// MapControl對象 142 /// </summary> 143 IMapControl m_MapControl = null; 144 145 #endregion 146 /// <summary> 147 ///構造函數 148 /// </summary> 149 /// <param name="mapControl"></param> 150 public DrawElementTool(IMapControl mapControl) 151 { 152 // this.Cursor = "";//鼠標樣式 153 m_MapControl = mapControl; 154 } 155 156 /// <summary> 157 /// 鼠標點擊事件 158 /// </summary> 159 /// <param name="sender"></param> 160 /// <param name="e"></param> 161 public override void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 162 { 163 if (e.Button == MouseButtons.Left)//左鍵 164 { 165 if (m_MapControl == null) return; 166 m_PolygonEle = new PolygonElement(); 167 IPolygon polygon = m_MapControl.TrackPolygon(); 168 m_PolygonEle.Symbol = SystemSymbolSetting.Instance.DefaultFillSymbol; 169 m_PolygonEle.Geometry = polygon as IGeometry; 170 171 m_HookHelper.ActiveView.GraphicsContainer.AddElement(m_PolygonEle); 172 m_HookHelper.ActiveView.PartialRefresh(ViewDrawPhaseType.ViewAll); 173 } 174 } 175 } 176 } 177 178 using System; 179 using System.Collections.Generic; 180 using System.Linq; 181 using System.Text; 182 183 namespace CommandAndToolDemo.Oper 184 { 185 class MapScaleCommandControl : PIE.Controls.BaseCommandControl 186 { 187 #region 成員變量 188 189 /// <summary> 190 /// ToolStripComboBox 191 /// </summary> 192 private System.Windows.Forms.ToolStripComboBox m_ToolStripComboxItem = null; 193 #endregion 194 195 /// <summary> 196 /// 構造函數 197 /// </summary> 198 public MapScaleCommandControl() 199 { 200 this.Caption = ""; 201 this.Name = ""; 202 this.Checked = false; 203 this.Enabled = false; 204 } 205 206 /// <summary> 207 /// Control 208 /// </summary> 209 public override object Control 210 { 211 get 212 { 213 return m_ToolStripComboxItem; 214 } 215 set 216 { 217 m_ToolStripComboxItem = value as System.Windows.Forms.ToolStripComboBox; 218 } 219 } 220 221 /// <summary> 222 /// 是否可用 223 /// </summary> 224 public override bool Enabled 225 { 226 get 227 { 228 if (m_Hook == null || m_HookHelper.ActiveView.FocusMap.LayerCount < 1) return false; 229 return true; 230 } 231 protected set 232 { 233 base.Enabled = value; 234 } 235 } 236 237 /// <summary> 238 /// 建立插件對象 239 /// </summary> 240 /// <param name="hook"></param> 241 public override void OnCreate(object hook) 242 { 243 if (hook == null) return; 244 if (!(hook is PIE.Carto.IPmdContents)) return; 245 this.Enabled = true; 246 m_Hook = hook; 247 m_HookHelper.Hook = hook; 248 if (m_ToolStripComboxItem == null) return; 249 System.Windows.Forms.ToolStripComboBox comboxItem=this.m_ToolStripComboxItem as System.Windows.Forms.ToolStripComboBox; 250 if (comboxItem==null)return; 251 252 comboxItem.Items.Add("1:500"); 253 comboxItem.Items.Add("1:1000"); 254 comboxItem.Items.Add("1:5000"); 255 comboxItem.Items.Add("1:10000"); 256 comboxItem.Items.Add("1:50000"); 257 comboxItem.Items.Add("1:100000"); 258 comboxItem.Items.Add("1:500000"); 259 comboxItem.Items.Add("1:1000000"); 260 comboxItem.TextChanged-=comboxItem_TextChanged; 261 comboxItem.TextChanged+=comboxItem_TextChanged; 262 } 263 264 /// <summary> 265 /// 比例尺文本變化事件 266 /// </summary> 267 /// <param name="sender"></param> 268 /// <param name="e"></param> 269 void comboxItem_TextChanged(object sender, EventArgs e) 270 { 271 //獲取選中的比例尺 272 string strScale = m_ToolStripComboxItem.Text.ToString(); 273 int count = strScale.Length; 274 if (count < 3) return; 275 string str = strScale.Substring(2,count-2); 276 double scale = Convert.ToDouble(str); 277 if (scale < 1) return; 278 279 //改變地圖的比例尺並更新 280 m_HookHelper.ActiveView.DisplayTransformation.MapScale = scale; 281 m_HookHelper.ActiveView.PartialRefresh(PIE.Carto.ViewDrawPhaseType.ViewAll); 282 } 283 284 /// <summary> 285 /// 點擊事件 286 /// </summary> 287 public override void OnClick() 288 { 289 base.OnClick(); 290 } 291 } 292 } |
注:上圖顯示了Command拓展(以加載數據爲例),Tool拓展(以繪製面元素爲例),Control拓展(以比例尺爲例)的應用