ArcMap擴(kuò)展開(kāi)發(fā)實(shí)習(xí)指導(dǎo)書(shū)_第1頁(yè)
ArcMap擴(kuò)展開(kāi)發(fā)實(shí)習(xí)指導(dǎo)書(shū)_第2頁(yè)
ArcMap擴(kuò)展開(kāi)發(fā)實(shí)習(xí)指導(dǎo)書(shū)_第3頁(yè)
ArcMap擴(kuò)展開(kāi)發(fā)實(shí)習(xí)指導(dǎo)書(shū)_第4頁(yè)
ArcMap擴(kuò)展開(kāi)發(fā)實(shí)習(xí)指導(dǎo)書(shū)_第5頁(yè)
已閱讀5頁(yè),還剩11頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、ArcMap拓展開(kāi)發(fā)實(shí)習(xí)指導(dǎo)書(shū)1、以ArcGIS C#插件開(kāi)發(fā)使用C#和ArcObject 10.1開(kāi)發(fā)ArcGIS插件,開(kāi)發(fā)的電腦上必須安裝ArcGIS基礎(chǔ)平臺(tái),同時(shí)必須安裝支持.NET的開(kāi)發(fā)庫(kù)。在這里我們使用Visual Studio 2010作為開(kāi)發(fā)環(huán)境。開(kāi)發(fā)步驟如下:1、啟動(dòng)Visual Studio 2010,創(chuàng)建Visual C# 的類(lèi)庫(kù)項(xiàng)目,名稱(chēng)為BufferExSolution,如圖2_1所示。2_1 創(chuàng)建項(xiàng)目2、在解決方案資源管理器中,選擇BufferExSolution項(xiàng)目,右鍵添加新建項(xiàng)目,選在ArcGIS模板下的Base Command模板,將名稱(chēng)設(shè)置為BufferEx

2、,如圖2_2所示,點(diǎn)擊“添加”按鈕,出現(xiàn)如圖2_3所示的選擇項(xiàng),選擇第二項(xiàng),其意義為在ArcMap、MapControl或者PageLayoutControl中都可以使用新建的插件。2_23、添加名為“BufferDlg”的Windows窗體,如圖2_4所示。這樣在資源管理器中就出現(xiàn)了圖2_44、為BufferDlg窗體添加控件,控件布局如圖2_5所示??丶傩匀绫?_1所示。圖2_5表2_1控件類(lèi)型Name屬性Text屬性其它屬性L(fǎng)abellblChoseLayer選擇圖層LabellblRadio緩沖區(qū)半徑LabellblOutPath輸出路徑ComboxcboLayersTextBoxt

3、xtBufferDistanceText=0.1ComboxcboUnitsItems= UnknownInchesPointsFeetYardsMilesNauticalMilesMillimetersCentimetersMetersKilometersDecimalDegreesDecimetersTextBoxtxtOutputPathEnable=falseButtonbtnOutputLayerButtonbtnBuffer輸出GroupBoxgpbTips提示TextBoxtxtMessagesMultiLineButtonbtnCancel關(guān)閉5、為修改BufferCmd類(lèi),首

4、先修改其構(gòu)造函數(shù):public BufferCmd() base.m_category = "緩沖區(qū)創(chuàng)建工具" base.m_caption = "緩沖區(qū)創(chuàng)建" base.m_message = "This should work in ArcMap/MapControl/PageLayoutControl" base.m_toolTip = "緩沖區(qū)創(chuàng)建" base.m_name = "BufferCmd" try string bitmapResourceName = GetType().N

5、ame + ".bmp" base.m_bitmap = new Bitmap(GetType(), bitmapResourceName); catch (Exception ex) System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap"); 6、為OnClick函數(shù)添加如下代碼:public override void OnClick() if (null = m_hookHelper) return;if (m_hookHelper.FocusMap.LayerCount &g

6、t; 0) BufferDlg bufferDlg = new BufferDlg(m_hookHelper); bufferDlg.Show(); 其中, IHookHelper m_hookHelper主要在用在自定義類(lèi)型于AO/AE帶的的ICommand或ITool等。在BufferCmd類(lèi)中,OnCreate函數(shù)將object類(lèi)型的參數(shù)hook傳遞給m_hookHelper,而m_hookHelper又可以作為參數(shù)在OnClick函數(shù)中傳遞給BufferDlg類(lèi),這樣就實(shí)現(xiàn)了ArcMap中數(shù)據(jù)向插件傳遞,從而實(shí)現(xiàn)插件對(duì)宿主程序中數(shù)據(jù)的操作、拓展了宿主程序的功能。OnCreate函數(shù)的代

7、碼如下:public override void OnCreate(object hook) if (hook = null) return;try m_hookHelper = new HookHelperClass(); m_hookHelper.Hook = hook; if (m_hookHelper.ActiveView = null) m_hookHelper = null; catch m_hookHelper = null; if (m_hookHelper = null) base.m_enabled = false; else base.m_enabled = true;O

8、nClick函數(shù)的作用就是:當(dāng)ArcMap中加載了該插件,只要點(diǎn)擊該插件的按鈕,則就執(zhí)行OnClick函數(shù),也就是將BufferDlg窗體調(diào)出來(lái)。下面,我們將為BufferDlg窗體添加控件響應(yīng)事件。7、修改BufferDlg類(lèi)的構(gòu)造函數(shù),使構(gòu)造函數(shù)有一個(gè)類(lèi)型為IHookHelper的參數(shù),實(shí)現(xiàn)從BufferCmd類(lèi)中接收宿主程序的數(shù)據(jù)。public BufferDlg(IHookHelper hookHelper) InitializeComponent(); m_hookHelper = hookHelper; 通過(guò)IHookHelper類(lèi)型的實(shí)例將宿主程序的對(duì)象傳到插件中的機(jī)制如下:IH

9、ookHelper m_hookHelper = new HookHelperClass();m_hookHelper.Hook = this.axMapControl1.Object ;這樣就可以把AxMapControl傳遞給其它要用到的地方,再通過(guò)IHookHelper.ActiveView和IHookHelper.FocusMap屬性來(lái)獲取IActiveView和IMap對(duì)象,通過(guò)這兩個(gè)接口進(jìn)行更一步的操作。8、完善BufferDlg類(lèi),為其添加相應(yīng)的字段,同時(shí)依次為表2_1中列出的控件添加相應(yīng)的事件響應(yīng),BufferDlg類(lèi)實(shí)現(xiàn)代碼如下:public partial class Bu

10、fferDlg : Form /委托機(jī)制,目的是在.Net環(huán)境中使用Win32函數(shù) DllImport("user32.dll") private static extern int PostMessage(IntPtr wnd, uint Msg, IntPtr wParam, IntPtr lParam);private IHookHelper m_hookHelper = null; private const uint WM_VSCROLL = 0x0115; private const uint SB_BOTTOM = 7; public BufferDlg(IH

11、ookHelper hookHelper) InitializeComponent(); m_hookHelper = hookHelper; private void bufferDlg_Load(object sender, EventArgs e) if (null = m_hookHelper | null = m_hookHelper.Hook | 0 = m_hookHelper.FocusMap.LayerCount) return;/將ArcMap中的圖層名稱(chēng)顯示到combox中 IEnumLayer layers = GetLayers(); layers.Reset();

12、ILayer layer = null; while (layer = layers.Next() != null) cboLayers.Items.Add(layer.Name); /設(shè)置默認(rèn)圖層 if (cboLayers.Items.Count > 0) cboLayers.SelectedIndex = 0; string tempDir = System.IO.Path.GetTempPath(); txtOutputPath.Text = System.IO.Path.Combine(tempDir, (string)cboLayers.SelectedItem + &quo

13、t;_buffer.shp"); /設(shè)置默認(rèn)的緩沖半徑單位 int units = Convert.ToInt32(m_hookHelper.FocusMap.MapUnits); cboUnits.SelectedIndex = units; private void btnOutputLayer_Click(object sender, EventArgs e) /設(shè)置輸出shp文件路徑 SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.CheckPathExists = true; saveDlg.Filter = &

14、quot;Shapefile (*.shp)|*.shp" saveDlg.OverwritePrompt = true; saveDlg.Title = "輸出圖層" saveDlg.RestoreDirectory = true; saveDlg.FileName = (string)cboLayers.SelectedItem + "_buffer.shp" DialogResult dr = saveDlg.ShowDialog(); if (dr = DialogResult.OK) txtOutputPath.Text = save

15、Dlg.FileName; private void btnBuffer_Click(object sender, EventArgs e) double bufferDistance; /獲取緩沖區(qū)半徑 double.TryParse(txtBufferDistance.Text, out bufferDistance); if (0.0 = bufferDistance) MessageBox.Show("緩沖區(qū)半徑不合法!"); return; /檢查輸出路徑合法性 if (!System.IO.Directory.Exists(System.IO.Path.GetD

16、irectoryName(txtOutputPath.Text) | ".shp" != System.IO.Path.GetExtension(txtOutputPath.Text) MessageBox.Show("輸出文件名不正確!"); return; /檢測(cè)宿主程序(ArcMap)中是否存在圖層 if (m_hookHelper.FocusMap.LayerCount = 0) return; /從ArcMap中獲得圖層 IFeatureLayer layer = GetFeatureLayer(string)cboLayers.Selecte

17、dItem); if (null = layer) txtMessages.Text += "Layer " + (string)cboLayers.SelectedItem + "不能被找到!rn" return; /設(shè)置消息框有滾動(dòng)條 ScrollToBottom(); txtMessages.Text += "rn分析開(kāi)始,這可能需要幾分鐘時(shí)間,請(qǐng)稍候.rn" txtMessages.Update(); /獲得geoprocessor實(shí)例 Geoprocessor gp = new Geoprocessor(); gp.Over

18、writeOutput = true; /創(chuàng)建緩沖區(qū)生成工具 ESRI.ArcGIS.AnalysisTools.Buffer buffer = new ESRI.ArcGIS.AnalysisTools.Buffer(layer, txtOutputPath.Text, Convert.ToString(bufferDistance) + " " + (string)cboUnits.SelectedItem); buffer.dissolve_option = "ALL"/這個(gè)要設(shè)成ALL,否則相交部分不會(huì)融合 /buffer.line_side =

19、 "FULL"/默認(rèn)是"FULL",最好不要改否則出錯(cuò) /buffer.line_end_type = "ROUND"/默認(rèn)是"ROUND",最好不要改否則出錯(cuò)/執(zhí)行緩沖區(qū)生成工具 IGeoProcessorResult results = null; try results = (IGeoProcessorResult)gp.Execute(buffer, null); catch (Exception ex) txtMessages.Text += "Failed to buffer layer: &

20、quot; + layer.Name + "rn" if (results != null &&results.Status != esriJobStatus.esriJobSucceeded) txtMessages.Text += "Failed to buffer layer: " + layer.Name + "rn" ScrollToBottom();txtMessages.Text += "rn分析完成.rn" txtMessages.Text += "-rn" Sc

21、rollToBottom(); private string ReturnMessages(Geoprocessor gp) StringBuilder sb = new StringBuilder(); if (gp.MessageCount > 0) for (int Count = 0; Count <= gp.MessageCount - 1; Count+) System.Diagnostics.Trace.WriteLine(gp.GetMessage(Count); sb.AppendFormat("0n", gp.GetMessage(Count

22、); return sb.ToString(); / <summary> / 根據(jù)名稱(chēng)獲取圖層對(duì)象 / </summary> / <param name="layerName"></param> / <returns></returns> private IFeatureLayer GetFeatureLayer(string layerName) /從ArcMap中獲得layers IEnumLayer layers = GetLayers(); layers.Reset(); ILayer laye

23、r = null; while (layer = layers.Next() != null) if (layer.Name = layerName) return layer as IFeatureLayer; return null; private IEnumLayer GetLayers() UID uid = new UIDClass(); uid.Value = "40A9E885-5533-11d0-98BE-00805F7CED21" IEnumLayer layers = m_hookHelper.FocusMap.get_Layers(uid, true); return la

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論