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)
{ }
}
}
}