|
ARCGIS学习
GIS的官方网站:http://www.esri.com/
Drew在学习GIS时参考的主要帮助文档:
软件中包含有 ArcObjects Developer Help
网上 http://arcobjectsonline.esri.com/
有 VC, VB, VBA语言写的例子程序,其中的About ArcObjects Component Help包含接口函数的定义及说明。
ARCReader 打开地图文件Published Map Document,xxx.pmf 如打开:arcgis\ArcTutor\ArcReader and Publisher\Gulf of St. Lawrence.pmf
ARCMAP打开arcmap map document,xxx.mxd, Published Map Document,xxx.pmf,
tamplate Document,xxx.mxt
用Add data打开图层lyr,支持多种文件格式,,xxx.shp,用鼠标选Identify后移动鼠标到要查询的元素点击,可以读出该Object定义,如location等的内容。Tools\Reports\Create Report可能是Table表。ArcCatalog中Preview,下面的下拉框,选Table,可以察看详细的Table表
VC比VB更适合ARCGIS开发,资料原文如下:
Visual Basic is a very productive tool, especially for user interface development, but there are limitations, to what can be done in Visual Basic. In the majority of cases, these limitations will not affect developers customizing and extending ArcObjects, with the exception of Custom Features. Many of the limitations have to do with the development environment itself. Debugging Visual Basic code is not as flexible as Visual C++. Using Visual Basic in a large development environment with many developers is not a productive as Visual C++, since partial compilations of projects are not supported. If one file is changed in a Visual Basic project, all the files must be recompiled. Since Visual Basic hides much of the interaction with COM away inside the Visual Basic Virtual Machine, low-level COM plumbing code cannot be written in Visual Basic.
VC中利用ATL创建COM的步骤
1)启动VC++
2)开始创建New->ATL COM Wizard->给出工程文件名如DisplayCommands -〉Finish;
3)现在创建了以下类:
DllMain
DllCanUnloadNow
DllGetClassObject
DllRegisterServer
DllUnregisterServer
4)使用Insert->New ATL Object,选择一个simple Object组件类,Next->出现C++和COM两列,只需在C++列头一项short name中键入该类的名称,如ZoomIn,其它的项的内容自动生成。
5)选择旁边的Attribues,定义客户接口(interface to Comstom),这保证接口IZoomIn直接从IUnknown而不是从IDispatch继承。这是所有ArcObject的默认方式,
6)点击OK完成。这样创建了三个新文件ZoomIn.cpp, ZoomIn.h, 和 ZoomIn.rgs。
在DisplayCommands.cpp映射中已经加入要完成的接口,
BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_ZoomIn, CZoomIn)
END_OBJECT_MAP()
注册脚本(registry script)在文件ZoomIn.rgs 中,形式如下,当服务器注册或取消注册,就要执行这个脚本文件。
HKCR
{
DisplayCommands.ZoomIn.1 = s 'ZoomIn Class'
{
CLSID = s '{36EBB573-8ED3-4CD5-8DAC-35171C246A23}'
}
DisplayCommands.ZoomIn = s 'ZoomIn Class'
{
CLSID = s '{36EBB573-8ED3-4CD5-8DAC-35171C246A23}'
CurVer = s 'DisplayCommands.ZoomIn.1'
}
NoRemove CLSID
{
ForceRemove {36EBB573-8ED3-4CD5-8DAC-35171C246A23} = s 'ZoomIn Class'
{
ProgID = s 'DisplayCommands.ZoomIn.1'
VersionIndependentProgID = s 'DisplayCommands.ZoomIn'
InprocServer32 = s '%MODULE%'
{
val ThreadingModel = s 'Apartment'
}
'TypeLib' = s '{B3CB6685-5525-40C8-BF94-4F5F7102E858}'
}
}
}
7)保证以上步骤正确,按F7编译程序。
8)在workspace浏览上选Class View,选类CZoomIn,鼠标右键,选菜单Implement Interface .来显示克供选择的类库,出现DISPLAYCOMMANDSLIB对话框,点击Add Typelib 按钮。在类库对话框中选ESRI Object Library[1.0], ->OK esriCore中选ICommand ->OK
在DisplayCommands.idl中把IUnknown 做位默认的接口,改为
coclass ZoomIn
{
[default] interface IUnknown;
interface IZoomIn;
interface ICommand;
};
9)在文件DisplayCommands.idl中library DISPLAYCOMMANDSLib加入esri的核心库esriCore.olb,加入后有如下形式:
library DISPLAYCOMMANDSLib
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
importlib("D:\ArcGIS\arcexe83\bin\esriCore.olb");
..........................
}
11)去除头文件ZoonIn.h中的#import语句#import "D:\arcgis\arcexe83\bin\esriCore.olb"
raw_interfaces_only, raw_native_types, no_namespace, named_guids
添加以下代码到stdafx.h 中
#pragma warning(push)
#pragma warning(disable : 4146)
#pragma warning(disable : 4192)
#import "D:\ArcGIS\arcexe83\bin\esriCore.olb" \
raw_interfaces_only, \
raw_native_types, \
no_namespace, \
named_guids, \
exclude("OLE_COLOR", "OLE_HANDLE")
#pragma warning(pop)
12)菜单Insert->Resource->Bitmap->Import,指定一个Bitmap文件作为ZoonIn的图例。在property中指定ID为IDB_ZOOMIN.
添加下面变量到ZoomIn.h中
private:
HBITMAP m_hBitmap;
ZoomIN.cpp中添加
CZoomIn()
{
m_hBitmap = ::LoadBitmap(_Module.m_hInst, MAKEINTRESOURCE(IDB_ZOOMIN));
}
~CZoomIn()
{
DeleteObject(m_hBitmap);
}
Next, add a member variable that will hold a reference to the application. This reference is the IApplication interface,
which is passed to the command when it is created.
IApplicationPtr m_ipApp;
18)在ZoomIn.h中加入头文件
#include "C:\ArcGIS\arcexe83\ArcObjects Developer Kit\Kits\CATIDS\ArcCATIDs.h"
ZoomIn类中加入:
BEGIN_CATEGORY_MAP(CZoomIn)
IMPLEMENTED_CATEGORY(__uuidof(CATID_MxCommands))
END_CATEGORY_MAP()
19)相应的函数做如下更改:
STDMETHODIMP CZoomIn::get_Enabled(VARIANT_BOOL * Enabled)
{
if (Enabled == NULL)
return E_POINTER;
*Enabled = VARIANT_TRUE; // Enable the tool always
return S_OK;
}
Note the use of the API calls used to create a BSTR.
STDMETHODIMP CZoomIn::get_Name(BSTR * Name)
{
if (Name == NULL)
return E_POINTER;
*Name = ::SysAllocString(L"Exploring ArcObjects_Zoom In");
return S_OK;
}
A simple cast is all that is required here to coerce the bitmap handle into an OLE_HANDLE variable.
STDMETHODIMP CZoomIn::get_Bitmap(OLE_HANDLE * Bitmap)
{
if (Bitmap == NULL)
return E_POINTER;
*Bitmap = (OLE_HANDLE) m_hBitmap;
return S_OK;
}
The OnCreate method is passed the IDispatch interface of the object. Using the QueryInterface support of the smart pointer,
it is a simple matter to set the member variable to be the hook. The smart pointer handles the QI.
STDMETHODIMP CZoomIn::OnCreate(IDispatch * hook)
{
m_ipApp = hook;
return S_OK;
}
The OnClick method is implemented to zoom the display by a factor of two. There is no error checking to simplify the code.
STDMETHODIMP CZoomIn::OnClick()
{
// HRESULT checking omitted for clarity
IDocumentPtr ipDoc;
m_ipApp->get_Document(&ipDoc);
IMxDocumentPtr ipMxDoc(ipDoc);
IActiveViewPtr ipActiveView;
ipMxDoc->get_ActiveView(&ipActiveView);
IEnvelopePtr ipEnv;
ipActiveView->get_Extent(&ipEnv);
ipEnv->Expand(0.5, 0.5, VARIANT_TRUE);
ipActiveView->put_Extent(ipEnv);
ipActiveView->Refresh();
return S_OK;
}
最后编译程序。生成DisplayCommands.dll文件。
登记xxx.dll文件的方法(此方法只是用于添加定制命令,工具栏,菜单等)add custom commands, toolbars, and menus
在Arcmap环境中,打开菜单tools\Customize 对话框, 点击 Add 按钮,找到刚刚编译的 dll文件,这个程序命令就会加到ArcMap,出现在对话框Misc.中. 将这个命令拖到菜单显示条上,按该项,可执行程序。
登记xxx.dll文件的另一种方法(和Component Categories有关的)
To register a component using Regsvr32
From the Taskbar click Start>Run
Type:
regsvr32 <your file path name>
For example:
regsvr32 D:\Samples\Application Framework\Sample Extension\SampleExtension.dll
Click OK.
编译VBA程序,VBA是ARCGIS自带的开发环境。
具体操作如下:
1)打开ArcMap 或 ArcCatalog,菜单Tools->Macros->Vasual Basic Editor
2)Vasual Basic Editor 中project\Modules\下新建一个Module,给出Module名字。
3)Tools\Micros,给出Micro 名字,下面下拉框选project(VBA project)。创建,Creat。
4)一个sub的代码会出现在代码窗口。
调试sample中 pathfinder 程序时,先将VC的xxx.dll注册。然后做VBA程序。再加入tools bar,
VBA程序中NetObjD4.PathFinder改为:NETOBJVC6Lib.PathFinder
path finder 需要装载Geometric Network图。即图中要有节点等信息。
在VC程序中,屏蔽这一行,if (ipNetWeight == NULL) return E_FAIL;这样即使没有weigtht name,程序也能正确运行。
|