Engine中IFeatureClass.Search(filter, Recycling)方法中Recycling-转载
参数Recycling为True的时候理解为传引用,为False的时候理解为传值。因此在应用的时候应该注意的“传值”和“传址”的差异。
我们知道引用传递(“传址”)效率较高,因此在绘画要素的时候可以采用
True参数。但当要进行遍历后将Feature的Geometry加入某个集合或插入到其他
FeatureClass的时候,必须使用传值调用,即参数为False,否则我们加入的都是最后
一个变量中的Geometry(因为传地址嘛)。这个我们可以用如下VBA代码试验得出结
论。
Private Sub UISearch_Click()
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pWS As IFeatureWorkspace
Dim pWSEdit As IWorkspaceEdit
Dim pDataset As IDataset
Dim pMap As IMap
Set pMap = pDoc.FocusMap
Dim pFLayer As IFeatureLayer
Set pFLayer = pMap.Layer(0)
Dim pFC As IFeatureClass
Dim pFeatureCursor As IFeatureCursor
Set pFC = pFLayer.FeatureClass
Dim pColl As Collection
Set pColl = New Collection
Set pFeatureCursor = pFC.Search(Nothing, False) ',True) '
Dim pFeat As IFeature
Set pFeat = pFeatureCursor.NextFeature
Dim i As Long
i = 0
Dim dArea As Double
Dim pPolygon As IPolygon
Dim pArea As IArea
dArea = 0
Set pFeat = pFeatureCursor.NextFeature
Do Until pFeat Is Nothing
i = i + 1
Set pPolygon = pFeat.Shape
Set pArea = pPolygon
'MsgBox pArea.Area
dArea = dArea + pArea.Area
Call pColl.Add(pPolygon)
Set pFeat = pFeatureCursor.NextFeature
Loop
'MsgBox i & ":" & dArea
Dim pPolygon2 As IPolygon
For i = 1 To pColl.Count
Set pPolygon2 = pColl.Item(i)
Set pArea = pPolygon2
'pFC.Search(Nothing, False)的时候,每次弹出面积信息不等,因为集合
(pColl)中的每个Geometry是不一样的,原因在于参数设为False是传值调用
'pFC.Search(Nothing, True)的时候,每次弹出面积信息相等,是因为集合
(pColl)中的每个Geometry是一样的,其实就是遍历的最后一个IGeometry,原因在于参数
设为False是传地址调用
MsgBox pArea.Area
Next i
End Sub
可以自己换一下Search方法的第二个参数就看出结果有所不同了,这个试验是
加入VB的Collection集合,如果插入到其他FeatureClass也是一样的。