Attribute VB_Name = "Module1"
Option Explicit
Sub CheckforSpatialIndex()
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
' マップ内の最上位レイヤを取得します。
Dim pLayer As IFeatureLayer
Set pLayer = pDoc.FocusMap.Layer(0)
Dim pFc As IFeatureClass
Set pFc = pLayer.FeatureClass
' シェープファイルがすでに空間インデックスを持っているかどうかを調べます。
Dim pIndexes As IIndexes
Dim pEnumIndex As IEnumIndex
Set pIndexes = pFc.Indexes
Set pIndexes = pFc.Indexes
Call AddMyIndex(pFc, "CODE") ' pFc.ShapeFieldName)
' If pIndexes.FindIndexesByFieldName("CODE").Next Is Nothing Then
' Debug.Print pFc.AliasName
'' Call AddMyIndex(pFc, pFc.ShapeFieldName)
' End If
End Sub
Public Sub AddMyIndex(pFc As IFeatureClass, strFieldName As String)
' フィールドを設定します。
Dim pFields As IFields
Dim pFieldsEdit As IFieldsEdit
Dim pField As IField
Dim lfld As Long
Set pFields = New Fields
Set pFieldsEdit = pFields
pFieldsEdit.FieldCount = 1
lfld = pFc.FindField(strFieldName)
Set pField = pFc.Fields.Field(lfld)
Set pFieldsEdit.Field(0) = pField
Dim pIndex As IIndex
Dim pIndexEdit As IIndexEdit
Set pIndex = New Index
'QI for IIndexEdit
Set pIndexEdit = pIndex
With pIndexEdit
Set .Fields = pFields
.Name = "Idx_1"
End With
' フィーチャクラスに空間インデックスを追加します。
pFc.AddIndex pIndex
End Sub
記事
