阅读:13053回复:33
VBA+AO入门50例完全注释版
<P>网上下的码,自己加的注。</P>
<P>初学,瞎搞,不好,见笑。</P> <P>和跟我一样的初学者探讨一下怎么入门最快最好,为中国GIS教育事业添一根小火柴</P> <P>1.<BR>Sub MyMacro()<BR> Dim pMxDocument As IMxDocument '地图文档<BR> Set pMxDocument = Application.Document '获取当前应用程序的文档 <BR> MsgBox pMxDocument.FocusMap.Name '显示当前地图的名称<BR>End Sub</P> <P><BR>2.<BR>Sub MyMacro()<BR> Dim pMxDocument As IMxDocument '地图文档<BR> Dim pMaps As IMaps '地图集<BR> Dim pMap As IMap '地图<BR> Set pMxDocument = Application.Document '获取当前应用程序的文档<BR> Set pMaps = pMxDocument.Maps '获取当前地图文档的地图集<BR> If pMaps.Count > 1 Then '如果该地图集的地图数大于1<BR> Set pMap = pMaps.Item(1) '获取该地图集中的第一幅地图<BR> MsgBox pMap.Name '显示该地图的名称<BR> End If<BR>End Sub</P> <P><BR>3.<BR>Sub MyMacro()<BR> Dim pMxDocument As IMxDocument '地图文档<BR> Dim pMap As IMap '地图<BR> Dim lCount As Long<BR> Dim lIndex As Long<BR> Set pMxDocument = Application.Document '获取当前应用程序的文档<BR> Set pMap = pMxDocument.FocusMap '获取当前地图<BR> lCount = 0<BR> For lIndex = 0 To (pMap.LayerCount - 1)<BR> If TypeOf pMap.Layer(lIndex) Is IFeatureLayer Then '如果当前地图的第lIndex层的类型是IFeatureLayer<BR> lCount = lCount + 1 '计数器加1<BR> End If<BR> Next lIndex<BR> MsgBox "Number of the feature layers " ; _<BR> "in the active map: " ; lCount '显示当前地图的要素层的总数<BR>End Sub</P> <P><BR>4.<BR>Sub MyMacro()<BR> Dim pMxDocument As IMxDocument '获取当前应用程序的文档<BR> Dim pMaps As IMaps '地图集<BR> Dim pMap As IMap '地图<BR> On Error GoTo SUB_ERROR '错误处理<BR> Set pMxDocument = Application.Document '获取当前应用程序的文档<BR> Set pMaps = pMxDocument.Maps '获取当前地图文档的地图集<BR> Set pMap = pMaps.Item(1) '获取该地图集中的第一幅地图<BR> MsgBox pMap.Name '显示该地图的名称<BR> Exit Sub<BR>SUB_ERROR: '行标签<BR> MsgBox "Error: " ; Err.Number ; "-" ; Err.Description '显示错误数和错误信息<BR>End Sub</P> <P><BR>5.<BR>'是图层可视<BR>Public Sub MakeLayerVisible()<BR> Dim pMxDocument As IMxDocument '地图文档<BR> Dim pMap As IMap '地图<BR> Dim pFeatureLayer As IFeatureLayer '要素层<BR> Dim pActiveView As IActiveView '活动视图<BR> Dim pContentsView As IContentsView '窗口内容表<BR> <BR> '获取地图的第一层<BR> Set pMxDocument = ThisDocument '获取当前应用程序的文档<BR> Set pMap = pMxDocument.FocusMap '获取当前地图<BR> Set pFeatureLayer = pMap.Layer(0) '获取当前地图的第一层 <BR> <BR> '如果要素层不可见,则使其可见<BR> If Not pFeatureLayer.Visible Then<BR> pFeatureLayer.Visible = True<BR> End If<BR> <BR> '刷新地图<BR> Set pActiveView = pMap '将当前地图设为活动地图<BR> pActiveView.Refresh '刷新<BR> <BR> '刷新窗口内容表<BR> Set pContentsView = pMxDocument.CurrentContentsView '获取当前地图文档的窗口内容表<BR> pContentsView.Refresh pFeatureLayer '刷新<BR>End Sub</P> <P><BR>6.<BR>'按NAME查询要素<BR>Private Function GetCountyFeature(pFeatureLayer As IFeatureLayer, strCountyName As String) As IFeature<BR> <BR> '查找要素类<BR> Dim pFeatureClass As IFeatureClass '要素类<BR> Dim pQueryFilter As IQueryFilter '查询过滤器<BR> Dim pFeatureCursor As IFeatureCursor<BR> <BR> Set pFeatureClass = pFeatureLayer.FeatureClass '从要素层获取要素类<BR> Set pQueryFilter = New QueryFilter '创建一个新的查询过滤器<BR> pQueryFilter.WhereClause = "NAME = '" ; strCountyName ; "'" '按郡名查找<BR> Set pFeatureCursor = pFeatureClass.Search (pQueryFilter, False) '获取查询到的要素对象<BR> <BR> '获取要素<BR> Dim pFeature As IFeature '要素<BR> <BR> Set pFeature = pFeatureCursor.NextFeature '获取查询结果的下一个要素<BR> If pFeature Is Nothing Then '如果该要素不存在 <BR> Set GetCountyFeature = Nothing '返回值设为空<BR> Else<BR> Set GetCountyFeature = pFeature '将该要素设为返回值<BR> End If<BR>End Function</P> <P><BR>未完待续</P> |
|
1楼#
发布于:2005-11-16 11:51
希望看到楼主继续翻译下去,<img src="images/post/smile/dvbbs/em01.gif" /><img src="images/post/smile/dvbbs/em02.gif" />
|
|
|
2楼#
发布于:2005-11-16 22:32
<P>上帝啊!!总统竟然光临寒舍……</P>
<P><img src="images/post/smile/dvbbs/em50.gif" /><img src="images/post/smile/dvbbs/em50.gif" /><img src="images/post/smile/dvbbs/em50.gif" /><img src="images/post/smile/dvbbs/em50.gif" /></P> <P>'放大/缩小<BR>Sub MyZoom()</P> <P> Dim pDoc As IMxDocument '地图文档<BR> Dim pActiveView As IActiveView '活动地图<BR> Dim pEnv As IEnvelope '显示范围</P> <P> Set pDoc = Application.Document '获取当前文档,等同于ThisDoucument<BR> Set pActiveView = pDoc.ActiveView '获取当前活动地图 <BR> <BR> Set pEnv = pActiveView.Extent '获取当前显示范围<BR> pEnv.Expand 0.5, 0.5, True '按比例放大两倍,把0.5改为2则为缩小一半<BR> pActiveView.Extent = pEnv '更新显示范围<BR> pActiveView.Refresh '刷新</P> <P>End Sub</P> <P><BR>MxApplication代表ArcMap本身,只管理一个文档MxDocument(ArcMap是单文档界面)。MxDocument管理一组Map对象和一个PageLayout对象。在数据视图下,ActiveView是一个Map;而在页面视图下,ActiveView是PageLayout。无论在何种视图下,总是只有一个FocusMap,显示操作都是对ActiveView进行。</P> <P>'全图:<BR>Sub FullExtentPlus()</P> <P> Dim pDoc As IMxDocument '地图文档<BR> Dim pActiveView As IActiveView '活动地图</P> <P> Set pDoc = Application.Document '获取当前地图文档<BR> Set pActiveView = pDoc.activeView '获取当前活动地图<BR> <BR> pActiveView.Extent = pDoc.ActiveView.FullExtent '全图显示<BR> pActiveView.Refresh '刷新当前视图</P> <P>End Sub</P> <P><BR>'清除图层<BR>Private Sub ClearLayers() </P> <P> Dim pDoc As IMxDocument '地图文档<BR> Dim pActiveView as IActiveView '活动地图 <BR> Dim pMap As IMap '地图</P> <P> Set pDoc = Application.Document '获取当前地图文档<BR> Set pActiveView = pDoc.ActiveView '获取当前活动地图</P> <P> If TypeOf pActiveView Is IMap Then '如果当前活动地图为数据视图模式 <BR> Set pMap = pActiveView '获取当前地图 <BR> pMap.ClearLayers '清除所有图层 <BR> pDoc.UpdateContents '更新窗口内容表 <BR> pActiveView.Refresh '刷新 <BR> End If </P> <P>End Sub</P> <P>'查找图层<BR>Function FindLayer(map As IMap, name As String) As ILayer</P> <P> Dim i As Integer </P> <P> For i = 0 To map.LayerCount - 1 '第一层的索引为1 <BR> If map.Layer(i).name = name Then '如果第i层的名称为name <BR> Set FindLayer = map.Layer(i) '获取并返回该层 <BR> Exit Function <BR> End If <BR> Next </P> <P>End Function</P> <P>'添加图层<BR>Sub AddLayer() </P> <P> Dim wksFact As IWorkspaceFactory '工作空间管理器<BR> Dim wks As IFeatureWorkspace '要素工作空间<BR> Dim fc As IFeatureClass '要素类<BR> Dim lyr As IFeatureLayer '要素层<BR> Dim ds As IDataset '数据集<BR> Dim mxDoc As IMxDocument '地图文档<BR> Dim map As IMap '地图</P> <P> Set wksFact = New ShapefileWorkspaceFactory '创建Shape工作空间管理器 <BR> Set wks = wksFact.OpenFromFile(“c:\Data\shp”, 0) '获取工作空间 <BR> Set fc = wks.OpenFeatureClass(“BigCypress”) '获取要素类 <BR> Set lyr = New FeatureLayer '创建要素层 <BR> Set lyr.FeatureClass = fc '向要素层中添加要素类 <BR> Set ds = fc '获取数据集 <BR> lyr.Name = ds.Name '用要素类的名称命名要素层<BR> Set pDoc = Application.Document '获取当前地图文档 <BR> Set mxmap = mxDoc.FocusMap '获取当前地图 <BR> map.AddLayer lyr '添加图层<BR> <BR>End Sub</P> <P>'添加文本<BR>Private Sub Hello()<BR> <BR> Dim pDoc As IMxDocument '地图文档<BR> Dim pActiveView As IActiveView '活动地图<BR> Dim sym As ITextSymbol '文本符号<BR> Dim bnds As IArea '面</P> <P> Set pDoc = Application.Document '获取当前地图文档<BR> Set pActiveView = pDoc.activeView '获取当前活动地图</P> <P> Set sym = New TextSymbol '创建文本符号<BR> sym.Font.size = 18 '设置字体大小</P> <P> With pActiveView.ScreenDisplay '对显示屏操作<BR> Set bnds = .DisplayTransformation.VisibleBounds '获取可视范围<BR> .StartDrawing .hDC, esriNoScreenCache<BR> .SetSymbol sym '设置要绘制的符号<BR> .DrawText bnds.Centroid, "Hello" '添加文本<BR> .FinishDrawing '完成绘制<BR> End With</P> <P>End Sub</P> <P>'选择要素<BR>Sub SelectFeatures()</P> <P> Dim mxDoc As IMxDocument '地图文档<BR> Dim lyr As IFeatureLayer '要素层<BR> Dim sel As IFeatureSelection '选择集<BR> Dim filter As IQueryFilter '查询过滤器<BR> Dim selEvents As ISelectionEvents '???</P> <P> Set mxDoc = Application.Document '获取当前地图文档<BR> Set lyr = FindLayer(mxDoc.FocusMap, "BUILDING") '调用FindLayer函数查找图层<BR> Set sel = lyr '将找到的图层设为选择集<BR> Set filter = New QueryFilter '创建查询过滤器<BR> filter.WhereClause = "BDNAME ='实验楼A'" '设置where子句<BR> sel.SelectFeatures filter, esriSelectionResultNew, False '选中满足条件的要素<BR> mxDoc.ActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing '绘出选中的要素<BR> Set selEvents = mxDoc.FocusMap '???<BR> selEvents.SelectionChanged '通知系统选择已经改变了</P> <P>End Sub</P> <P><BR>'监听</P> <P>Dim WithEvents g_Map As map</P> <P>Private Sub UIButtonControl1_Click()<BR> Dim mxDoc As IMxDocument '地图文档<BR> Dim lyr As IFeatureLayer '要素层<BR> Dim sel As IFeatureSelection '选择集<BR> Dim filter As IQueryFilter '查询过滤器<BR> Dim selEvents As ISelectionEvents '???</P> <P> Set g_Map = mxDoc.FocusMap '获取当前地图</P> <P> Set mxDoc = Application.Document '获取当前地图文档<BR> Set lyr = FindLayer(mxDoc.FocusMap, "BUILDING") '调用FindLayer函数查找图层<BR> Set sel = lyr '将找到的图层设为选择集<BR> Set filter = New QueryFilter '创建查询过滤器<BR> filter.WhereClause = "BDNAME ='实验楼A'" '设置where子句<BR> sel.SelectFeatures filter, esriSelectionResultNew, False '选中满足条件的要素<BR> mxDoc.activeView.PartialRefresh esriViewGeoSelection, Nothing, Nothing '绘出选中的要素<BR> Set selEvents = mxDoc.FocusMap '???<BR> selEvents.SelectionChanged '通知系统选择已经改变了</P> <P>End Sub</P> <P>'查找图层<BR>Function FindLayer(map As IMap, name As String) As ILayer</P> <P> Dim i As Integer</P> <P> For i = 0 To map.LayerCount - 1 '第一层的索引为1<BR> If map.Layer(i).name = name Then '如果第i层的名称为name<BR> Set FindLayer = map.Layer(i) '获取并返回该层<BR> Exit Function<BR> End If<BR> Next</P> <P>End Function</P> <P>Private Sub g_Map_SelectionChanged()</P> <P> Dim activeView As IActiveView '活动地图<BR> Dim featureEnum As IEnumFeature '列举的要素?<BR> Dim feat As IFeature '要素<BR> Dim index As Long<BR> Dim Msg As String</P> <P> Set activeView = g_Map '获取当前地图<BR> Set featureEnum = activeView.Selection '列举所选的要素<BR> featureEnum.Reset '还原至初始顺序<BR> Set feat = featureEnum.Next '获取选择集中第一个要素<BR> Do While Not feat Is Nothing '如果要素存在 <BR> index = feat.Fields.FindField(“Name”) '获取Name字段的索引值 <BR> If index <> -1 Then MsgBox Msg ; chr(13) ; chr(10) ; feat.Value(index) '显示该要素的Name <BR> Set feat = featureEnum.Next '移至选择集中的下一个要素 <BR> Loop </P> <P>End Sub</P> |
|
3楼#
发布于:2005-11-17 14:30
<P>呵呵</P>
<P>翻译的这么详细,当初学的时候有这个看就好了~~~</P> <P>辛苦了</P> |
|
|
4楼#
发布于:2005-11-30 11:34
<P>对初学者应该是个好东西,想起我上半年学的时候,多辛苦哦!</P>
|
|
6楼#
发布于:2005-12-17 12:02
希望看到楼主继续翻译下去<img src="images/post/smile/dvbbs/em02.gif" />
|
|
7楼#
发布于:2005-12-25 14:57
<P>可是我对 Document 的添加怎么做</P>
|
|
8楼#
发布于:2006-01-06 14:01
<P>顶</P>
|
|
9楼#
发布于:2006-01-09 20:59
<P>谢谢楼主,偶正在学习!</P><img src="images/post/smile/dvbbs/em01.gif" /><img src="images/post/smile/dvbbs/em02.gif" />
|
|
上一页
下一页