ArcTool工具中,有一个Erase工具。即实现这样的操作:
用起来非常方便。但是在做ArcEngine开发时,如果用GP来做,总觉的别扭。正好同学要实现一个这样的功能,问我能否作。顺便就做了这一块。研究了一下。ArcEngine的ITopologicalOperator接口中,没有Erase方法,所以来了一个迂回操作。基本思路如下:
1、获取要裁切的图层和裁切框。
2、用裁切框在图层中查找与之相交的要素。
3、对相交的要素,逐一与裁切框相切,求出相切图形。
4、用相切的图形与源要素求差,获取外围要素。
5、获取的外围要素再赋予源要素。实现擦除处理。
源代码如下:
public class EraseClass
{
///<summary>
///裁切框
///</summary>
private IEnvelope _Envelope;
public IEnvelope pEnvelope
{
get { return _Envelope; }
set { _Envelope = value; }
}
///<summary>
///被裁切图层
///</summary>
private IFeatureClass _FeatureClass;
public IFeatureClass pFeatureClass
{
get { return _FeatureClass; }
set { _FeatureClass = value; }
}
public EraseClass()
{ }
public EraseClass(IEnvelope pEnvelope, IFeatureClass pFeatureClass)
{
_FeatureClass = pFeatureClass;
_Envelope = pEnvelope;
}
public void EraseOper()
{
ISpatialFilter tSF = new SpatialFilterClass();
tSF.Geometry = _Envelope;
tSF.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
//求出与裁切框相交要素
IFeatureCursor tFeatureCursor = _FeatureClass.Search(tSF, false);
IFeature tFeature = tFeatureCursor.NextFeature();
while (tFeature != null)
{
IGeometry tGeo2 = tFeature.ShapeCopy;
ITopologicalOperator tTope2 = tGeo2 as ITopologicalOperator;
tTope2.Simplify();
IGeometry tGeo = tFeature.ShapeCopy;
ITopologicalOperator tTope = tGeo as ITopologicalOperator;
tTope.Simplify();
//用Envelope对要素进行裁切
tTope.Clip(this._Envelope);
IGeometry tGeoClip = (IGeometry)tTope;
//用裁切出来的要素,再与其源要素进行求差处理,即得到外围要素
IGeometry tGeoDe = tTope2.Difference(tGeoClip);
//把外围要素赋予源要素
tFeature.Shape = tGeoDe;
tFeature.Store();
tFeature = tFeatureCursor.NextFeature();
}
ReleaseCom(tFeatureCursor);
}
private void ReleaseCom(object o)
{
if (o != null)
{
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0)
{ }
}
}
}