1.功能简介
在一个项目中,是通过小组成员共同开发的,难以避免的是当项目功能集成的时候会出现很多兼容性问题,开发讲究高内聚低耦合,利用Command、Tool和Control的使用,可以提升集成的效率,PIE SDK包含大量的已经封装好的Command和Tool,调用简单易于实现,调试方便。
什么时候会用到Command?Command,命令,功能不需要鼠标和地图进行交互,例如全图显示,点击按钮地图接收到命令会自动全图显示;而Tool(工具)则相反,需要鼠标和地图进行交互,例如地图的漫游功能,拉框放大功能等,需要鼠标在地图上操作才会做出反应。
Control是包含了控件的Command,比如比例尺控制按钮、图像透明度控制按钮,通过操作Control中的控件即可实现地图的操作。
下面具体介绍组件式开发中如何调用PIE SDK的Command和Tool,以及Control的拓展应用。
2.功能实现说明
2.1. 实现思路及原理说明
Command的调用 |
|
第一步 |
New一个Command对象 |
第二步 |
绑定地图,传递地图控件对象(创建插件对象OnCreate) |
第三步 |
调用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进行关联即可 |
2.2. 核心接口与方法
接口/类 |
方法/属性 |
说明 |
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对象 |
2.3. 示例代码
项目路径 |
百度云盘地址下/PIE示例程序/ 06.Command、Tool、Control的调用和扩展 |
数据路径 |
百度云盘地址下/PIE示例数据/栅格数据/ |
视频路径 |
百度云盘地址下/PIE视频教程/ 06.Command、Tool、Control的调用和扩展/xx..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 /// <summary> 25 /// Command调用自定义拓展(如何自己写Command,一加载数据为例) 26 /// </summary> 27 /// <param name="sender"></param> 28 /// <param name="e"></param> 29 private void btn_CommandEx_Click(object sender, EventArgs e) 30 { 31 PIE.SystemUI.ICommand cmd = new Oper.AddDataCommand(); 32 cmd.OnCreate(mapControlMain); 33 cmd.OnClick(); 34 } 35 /// <summary> 36 /// Tool调用自定义拓展(如何自己写Tool——以绘制面元素为例) 37 /// </summary> 38 /// <param name="sender"></param> 39 /// <param name="e"></param> 40 private void btn_ToolEx_Click(object sender, EventArgs e) 41 { 42 PIE.SystemUI.ITool tool = new Oper.ClipImageTool(mapControlMain); 43 (tool as ICommand).OnCreate(mapControlMain); 44 mapControlMain.CurrentTool = tool; 45 } 46 47 /// <summary> 48 /// Control的拓展(以比例尺Control为例) 49 /// </summary> 50 /// <param name="sender"></param> 51 /// <param name="e"></param> 52 private void tbn_ControlEx_Click(object sender, EventArgs e) 53 { 54 Oper.MapScaleCommandControl mapScaleComtrol= new Oper.MapScaleCommandControl(); 55 mapScaleComtrol.Control = this.combox_MapScale;//将界面的比例尺控件与mapScaleControl绑定 56 mapScaleComtrol.OnCreate(mapControlMain); 57 } 58 59 拓展类: 60 using PIE.Carto; 61 using PIE.Controls; 62 using System; 63 using System.Collections.Generic; 64 using System.Linq; 65 using System.Text; 66 using System.Windows.Forms; 67 namespace CommandAndToolDemo.Oper 68 { 69 class AddDataCommand : BaseCommand 70 { 71 /// <summary> 72 /// 构造函数 73 /// </summary> 74 public AddDataCommand() 75 { 76 this.Caption = "加载数据"; 77 this.ToolTip = "加载数据"; 78 this.Checked = true; 79 this.Enabled = false; 80 // this.Image = ""; 81 this.Name = "加载数据"; 82 } 83 /// <summary> 84 /// 创建插件对象 85 /// </summary> 86 /// <param name="hook"></param> 87 public override void OnCreate(object hook) 88 { 89 if (hook == null) return; 90 if (!(hook is PIE.Carto.IPmdContents)) return; 91 this.Enabled = true; 92 m_Hook = hook; 93 m_HookHelper.Hook = hook; 94 } 95 /// <summary> 96 /// 点击事件 97 /// </summary> 98 public override void OnClick() 99 { 100 OpenFileDialog openDialog = new OpenFileDialog(); 101 openDialog.Title = "加载数据"; 102 openDialog.Filter = "Raster File|*.img;*.tif;*.dat;*.tiff|Shape File|*.shp|所有文件|*.*"; 103 if (openDialog.ShowDialog() != DialogResult.OK) return; 104 ILayer layer = LayerFactory.CreateDefaultLayer(openDialog.FileName); 105 if (layer == null) return; 106 m_HookHelper.ActiveView.FocusMap.AddLayer(layer); 107 m_HookHelper.ActiveView.PartialRefresh(ViewDrawPhaseType.ViewAll); 108 } 109 } 110 } 111 112 using PIE.AxControls; 113 using PIE.Carto; 114 using PIE.Display; 115 using PIE.Geometry; 116 using System; 117 using System.Collections.Generic; 118 using System.Linq; 119 using System.Text; 120 using System.Windows.Forms; 121 122 namespace CommandAndToolDemo.Oper 123 { 124 class DrawElementTool : PIE.Controls.BaseTool 125 { 126 #region 成员变量 127 /// <summary> 128 /// 面元素 129 /// </summary> 130 /// 131 IPolygonElement m_PolygonEle = null; 132 /// <summary> 133 /// MapControl对象 134 /// </summary> 135 IMapControl m_MapControl = null; 136 #endregion 137 /// <summary> 138 ///构造函数 139 /// </summary> 140 /// <param name="mapControl"></param> 141 public DrawElementTool(IMapControl mapControl) 142 { 143 // this.Cursor = "";//鼠标样式 144 m_MapControl = mapControl; 145 } 146 /// <summary> 147 /// 鼠标点击事件 148 /// </summary> 149 /// <param name="sender"></param> 150 /// <param name="e"></param> 151 public override void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 152 { 153 if (e.Button == MouseButtons.Left)//左键 154 { 155 if (m_MapControl == null) return; 156 m_PolygonEle = new PolygonElement(); 157 IPolygon polygon = m_MapControl.TrackPolygon(); 158 m_PolygonEle.Symbol = SystemSymbolSetting.Instance.DefaultFillSymbol; 159 m_PolygonEle.Geometry = polygon as IGeometry; 160 161 m_HookHelper.ActiveView.GraphicsContainer.AddElement(m_PolygonEle); 162 m_HookHelper.ActiveView.PartialRefresh(ViewDrawPhaseType.ViewAll); 163 } 164 } 165 } 166 } 167 168 using System; 169 using System.Collections.Generic; 170 using System.Linq; 171 using System.Text; 172 namespace CommandAndToolDemo.Oper 173 { 174 class MapScaleCommandControl : PIE.Controls.BaseCommandControl 175 { 176 #region 成员变量 177 /// <summary> 178 /// ToolStripComboBox 179 /// </summary> 180 private System.Windows.Forms.ToolStripComboBox m_ToolStripComboxItem = null; 181 #endregion 182 /// <summary> 183 /// 构造函数 184 /// </summary> 185 public MapScaleCommandControl() 186 { 187 this.Caption = ""; 188 this.Name = ""; 189 this.Checked = false; 190 this.Enabled = false; 191 } 192 /// <summary> 193 /// Control 194 /// </summary> 195 public override object Control 196 { 197 get 198 { 199 return m_ToolStripComboxItem; 200 } 201 set 202 { 203 m_ToolStripComboxItem = value as System.Windows.Forms.ToolStripComboBox; 204 } 205 } 206 /// <summary> 207 /// 是否可用 208 /// </summary> 209 public override bool Enabled 210 { 211 get 212 { 213 if (m_Hook == null || m_HookHelper.ActiveView.FocusMap.LayerCount < 1) return false; 214 return true; 215 } 216 protected set 217 { 218 base.Enabled = value; 219 } 220 } 221 222 /// <summary> 223 /// 创建插件对象 224 /// </summary> 225 /// <param name="hook"></param> 226 public override void OnCreate(object hook) 227 { 228 if (hook == null) return; 229 if (!(hook is PIE.Carto.IPmdContents)) return; 230 this.Enabled = true; 231 m_Hook = hook; 232 m_HookHelper.Hook = hook; 233 if (m_ToolStripComboxItem == null) return; 234 System.Windows.Forms.ToolStripComboBox comboxItem=this.m_ToolStripComboxItem as System.Windows.Forms.ToolStripComboBox; 235 if (comboxItem==null)return; 236 237 comboxItem.Items.Add("1:500"); 238 comboxItem.Items.Add("1:1000"); 239 comboxItem.Items.Add("1:5000"); 240 comboxItem.Items.Add("1:10000"); 241 comboxItem.Items.Add("1:50000"); 242 comboxItem.Items.Add("1:100000"); 243 comboxItem.Items.Add("1:500000"); 244 comboxItem.Items.Add("1:1000000"); 245 comboxItem.TextChanged-=comboxItem_TextChanged; 246 comboxItem.TextChanged+=comboxItem_TextChanged; 247 } 248 249 /// <summary> 250 /// 比例尺文本变化事件 251 /// </summary> 252 /// <param name="sender"></param> 253 /// <param name="e"></param> 254 void comboxItem_TextChanged(object sender, EventArgs e) 255 { 256 //获取选中的比例尺 257 string strScale = m_ToolStripComboxItem.Text.ToString(); 258 int count = strScale.Length; 259 if (count < 3) return; 260 string str = strScale.Substring(2,count-2); 261 double scale = Convert.ToDouble(str); 262 if (scale < 1) return; 263 //改变地图的比例尺并更新 264 m_HookHelper.ActiveView.DisplayTransformation.MapScale = scale; m_HookHelper.ActiveView.PartialRefresh(PIE.Carto.ViewDrawPhaseType.ViewAll); 265 } 266 /// <summary> 267 /// 点击事件 268 /// </summary> 269 public override void OnClick() 270 { 271 base.OnClick(); 272 } 273 } 274 } |
2.4. 示例截图
注:上图显示了Command拓展(以加载数据为例),Tool拓展(以绘制面元素为例),Control拓展(以比例尺为例)的应用