一、一个AO扩展模块开发的基本步骤
(1)新建工程,添加AO的引用;
(2)实现需要的接口,添加具体的代码;
(3)编译为DLL,注册该COM组件,并在ArcGIS的omponentcategories注册(也可以使用界面的自定义对话框浏览定位到具体的dll来注册);
(4)测试和调试。
二、Base Command与base Tool的区别
前者不需要用鼠标等与地图交互,如全图功能,后者则则是需要鼠标或者键盘与地图经常交互的工具。
OnCreate:方法中传递的参数hook成为绑定到这个Command控件的交互对象,被保存在一个IHookHelper成员变量中,通过该成员的Hook属性即可获得对交互对象(往往是MapControl的引用)。
三、已经实现的三个实例
1.创建定制的按钮button,实现要素feature的过滤显示;
属于base command项目,因为这是对地图操作的工具,所以arcgis项目导向要选
关键代码如下:
public override void OnClick()
{
// TODO: Add CommandFilter.OnClick implementation
ILayer lyr = m_hookHelper.FocusMap.get_Layer(0);
IFeatureLayerDefinition fLyrDef = lyr as IFeatureLayerDefinition;
fLyrDef.DefinitionExpression = "enrollment>= 2000";
m_hookHelper.ActiveView.Refresh();
}
2.创建定制的工具tool,实现Polygon要素查询;
属于base tool项目,关键代码如下:
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
// TODO: Add PT_Select.OnMouseDown implementation
IRubberBand polygonRubber = new RubberPolygonClass();
IPolygon polygon = polygonRubber.TrackNew(m_hookHelper.ActiveView.ScreenDisplay, null) as IPolygon;
ISpatialFilter spFilter = new SpatialFilterClass();
spFilter.Geometry = polygon;
spFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
ILayer layer = m_hookHelper.FocusMap.get_Layer(0);//0是最上面的图层,就是如果有点图层一定选点图层
IFeatureSelection fSel = layer as IFeatureSelection;
fSel.SelectFeatures(spFilter,
esriSelectionResultEnum.esriSelectionResultNew, false);
fSel.SelectionSet.Refresh();
m_hookHelper.ActiveView.Refresh();
}
3.创建定制的工具identify,实现点击要素查询名字;
属于base tool项目,关键代码如下:
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
// TODO: Add PT_Identify.OnMouseDown implementation
IIdentify pIdentify;
IPoint pPoint;
IArray pIDArray;
IFeatureIdentifyObj pFeatueObj;
IIdentifyObj pIObj;
IActiveView activeView = (IActiveView)m_hookHelper.ActiveView;
pIdentify = (IIdentify)m_hookHelper.FocusMap.get_Layer(0);
pPoint = new PointClass();
//通过点选择Feature,得把屏幕的点的坐标转成有投影等坐标系的点才能选得到Feature
pPoint = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
pIDArray = pIdentify.Identify((IGeometry)pPoint);
if (pIDArray != null) //如果鼠标点击的是对象
{
pFeatueObj = (IFeatureIdentifyObj)pIDArray.get_Element(0); //取得要素
pIObj = (IIdentifyObj)pFeatueObj;
pIObj.Flash(activeView.ScreenDisplay); //闪烁效果
MessageBox.Show("Layer:" + pIObj.Layer.Name + "\n" + "Feature Name:" + pIObj.Name); //弹出信息
}
else //如果鼠标点击的是空白
MessageBox.Show("没有值");
}
4.创建定制的工具条
属于base toolbar项目,关键代码如下:
public MyArcGISToolbar()
{
//
// TODO: Define your toolbar here by adding items
//
AddItem("esriArcMapUI.ZoomInTool");
BeginGroup(); //Separator
AddItem("{FBF8C3FB-0480-11D2-8D21-080009EE4E51}", 1); //undo command
AddItem(new Guid("FBF8C3FB-0480-11D2-8D21-080009EE4E51"), 2); //redo command
AddItem("PT_EngineClassLibrary2.PT_Filter");
AddItem("PT_EngineClassLibrary2.PT_Select");//namespace.class
}
四、调用已写好的工具
1、在vs中生成解决方案,成功后可以在该项目的文件夹下bin-debug目录下看到.dll和.tld文件的生成
2、
在ArcMap环境下加载这个客户化组件。其一般步骤如下:
1、在Customize对话框 “Toolbars”或者“Commands”页,然后点击<Add From File>
2、如果加载是“Commands”, 可以将其拖置于任何工具条上
3、如果加载是“ToolBars”,则可在ArcMap中显示
打开ArcMap客户化Customize对话框,添加按钮到ArcMap工具条上。