IFeatureClass.Search(IQuery Filter,bool Recycling)中的Recycling
IFeatureClass的Search方法在AE开发中经常用到,但是对Search方法的其中一个参数bool Recycling一直不是很理解,用的时候也很随意,感觉true与false没啥区别。
ArcObjects API Reference for .NET帮助文档上这么解释:
The recycling parameter controls row object allocation behavior. Recycling cursors rehydrate a single feature object on each fetch and can be used to optimize read-only access, for example, when drawing. It is illegal to maintain a reference on a feature object returned by a recycling cursor across multiple calls to NextFeature on the cursor. Features returned by a recycling cursor should not be modified. Non-recycling cursors return a separate feature object on each fetch. The features returned by a non-recycling cursor may be modified and stored with polymorphic behavior
The Geodatabase guarantees "unique instance semantics" on non-recycling feature objects fetched during an edit session. In other words, if the feature retrieved by a search cursor has already been instantiated and is being referenced by the calling application, then a reference to the existing feature object is returned.
Non-recycling feature cursors returned from the Search method *MUST* be used when copying features from the cursor into an insert cursor of another class. This is because a recycling cursor reuses the same geometry and under some circumstances all of the features inserted into the insert cursor may have the same geometry. Using a non-recycling cursor ensures that each geometry is unique.
上面这段话大至意思为在查询时能使用true优化,其它情况最好使用false。在复制要素类时使用true会丢失图形。
下面是实际测试:将面图层的图斑融合为一个组合图斑
准备了一个面图层:
代码:
private void FeatureCursorTest(IFeatureClass featureClass)
{
using (ComReleaser comReleaser = new ComReleaser())
{
object missing = Type.Missing;
//IFeatureCursor cursor = featureClass.Search(null, true);
IFeatureCursor cursor = featureClass.Search(null, false);
comReleaser.ManageLifetime(cursor);
IFeature feature = null;
IGeometryCollection geometries = new GeometryBagClass();
ITopologicalOperator unionedGeometry = new PolygonClass();
while ((feature = cursor.NextFeature()) != null)
{
geometries.AddGeometry(feature.Shape, ref missing, ref missing);
}
unionedGeometry.ConstructUnion(geometries as IEnumGeometry);
IFeature unionedFeature = featureClass.CreateFeature();
unionedFeature.Shape = unionedGeometry as IGeometry;
unionedFeature.Store();
}
}
结果:
确实,在Recycling为true时只保留了最后一个几何图形。就像网上很多大神说的传值和传址吧。即参数Recycling为True的时候理解为传引用,为False的时候理解为传值。在使用true时,如果feature已存在则会使用该对象,而false确保每次返回唯一图形。