Option Explicit
Global g_pStateLayer As IFeatureLayer
Global g_pCountyLayer As IFeatureLayer
Global g_pTractLayer As IFeatureLayer
Public Sub Tutorial()
' This procedure is called when user clicks on the
' customized button
'
' (1) Edit the following constants to match your
' environment
Const c_strDataPath = "E:\arcgis\arcdata\cd3\usa\"
Const c_strStateFileName = "dtl_st.shp"
Const c_strStateLayerName = "State"
Const c_strCountyFileName = "dtl_cnty.shp"
Const c_strCountyLayerName = "County"
Const c_strTractFileName = "tracts.shp"
Const c_strTractLayerName = "Census Tract"
'
' (2) Load the shape files if necessary
Dim pLayer As IFeatureLayer
' State shape file
Set pLayer = GetLayer(c_strStateLayerName)
If pLayer Is Nothing Then
Set pLayer = AddShapeFile(c_strDataPath, _
c_strStateFileName, c_strStateLayerName)
If pLayer Is Nothing Then
MsgBox "Unable to locate " & c_strDataPath & _
c_strStateFileName & " shape file."
Exit Sub
End If
End If
Set g_pStateLayer = pLayer
' County shape file
Set pLayer = GetLayer(c_strCountyLayerName)
If pLayer Is Nothing Then
Set pLayer = AddShapeFile(c_strDataPath, _
c_strCountyFileName, c_strCountyLayerName)
If pLayer Is Nothing Then
MsgBox "Unable to locate " & c_strDataPath & _
c_strCountyFileName & " shape file."
Exit Sub
End If
' Make county invisible
pLayer.Visible = False
End If
Set g_pCountyLayer = pLayer
' Census tract shape file
Set pLayer = GetLayer(c_strTractLayerName)
If pLayer Is Nothing Then
Set pLayer = AddShapeFile(c_strDataPath, _
c_strTractFileName, c_strTractLayerName)
If pLayer Is Nothing Then
MsgBox "Unable to locate " & c_strDataPath & _
c_strTractFileName & " shape file."
Exit Sub
End If
' Make tract invisible
pLayer.Visible = False
End If
Set g_pTractLayer = pLayer
'
' (3) Display the user interface form and populate
' its combo boxes
frmClassify.PopulateClassCountCombo
frmClassify.PopulateClassificationCombo
frmClassify.PopulateStateCombo
frmClassify.Show
End Sub
Private Function GetLayer(strLayerName As String) As IFeatureLayer
' This function accepts a layer name and returns
' the layer if available, otherwise returns "Nothing".
'
' (1) Access the document's map
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Set pMxDoc = Application.Document
Set pMap = pMxDoc.FocusMap
'
' (2) Search through layers for the given layer name
Dim lngIndex As Long
Set GetLayer = Nothing
For lngIndex = 0 To pMap.LayerCount - 1
If pMap.Layer(lngIndex).Name = strLayerName Then
Set GetLayer = pMap.Layer(lngIndex)
Exit For
End If
Next lngIndex
End Function
Private Function AddShapeFile(strPath As String, _
strFile As String, strName As String) As IFeatureLayer
' This function adds the specified shapefile and
' returns the layer. It returns "Nothing" if not
' successful.
'
' (1) Make sure the shape file exist
If Len(Dir(strPath & strFile)) = 0 Then
' File does not exist
Set AddShapeFile = Nothing
Exit Function
End If
'
' (2) Create a workspace to represent the datasource
Dim pWorkspaceFactory As IWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace
Set pWorkspaceFactory = New ShapefileWorkspaceFactory
Set pFeatureWorkspace = _
pWorkspaceFactory.OpenFromFile(strPath, 0)
'
' (3) Access the shape file through a feature layer
Dim pClass As IFeatureClass
Dim pFeatureLayer As IFeatureLayer
Set pClass = pFeatureWorkspace.OpenFeatureClass(strFile)
Set pFeatureLayer = New FeatureLayer
Set pFeatureLayer.FeatureClass = pClass
pFeatureLayer.Name = strName
'
' (4) Add layer to the map
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Set pMxDoc = Application.Document
Set pMap = pMxDoc.FocusMap
pMap.AddLayer pFeatureLayer
Set AddShapeFile = pFeatureLayer
End Function
ArcObject常用VBA代码
需积分: 9 49 浏览量
2008-12-08
17:22:18
上传
评论
收藏 23KB RAR 举报
xcc1028
- 粉丝: 0
- 资源: 1
最新资源
- 采用P-f和Q-V滞控的去中心化逆变器型交流微电网的模拟(Simulink仿真实现)
- 彩虹聚合二级域名DNS管理系统源码v1.3
- 【TOF相机笔记3】Simulink使用方法
- 算法部署-基于C++和Python使用ONNXRuntime部署RT-DETR目标检测算法-附项目源码-优质项目实战.zip
- Bitree.cpp
- 改变浏览器大小,图片(img)内容居中显示
- 全景分割-基于FAIR-DETR对Cityscapes数据集进行微调实现全景分割-附项目源码-优质项目实战.zip
- Tru master.m4a
- 基于ELMAN神经网络的用气量预测,基于ELMAN的天然气消费量预测(代码完整,数据齐全)
- 基于Vue3+ThreeJS实现机械臂控制和预览+源码+开发文档+代码解析(高分优秀项目)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈