根据线进行分割

public static void SplitByLine(IMap map,IPolyline line, ILayer layer)
{
if (line != null)
{
IFeatureLayer pFeatureLayer = layer as IFeatureLayer;

//划线
IGeometry pline = (IGeometry)line;

//拿到划线范围内的所有feature
IGeometry pGeometry = pline as IGeometry;
IMap pMap = map;
pMap.SelectByShape(pGeometry, null, false);
//转成IRealtionOperator接口,以便在遍历时判断几何位置关系。
IRelationalOperator rela = (pline as IPolyline) as IRelationalOperator;

//拿到切割目标集合
IEnumFeature pEnumFeature = (IEnumFeature)pMap.FeatureSelection;
IFeature pFeature = pEnumFeature.Next();
//依次遍历
while (pFeature != null)
{
if (rela.Crosses(pFeature.Shape))//判断是否Cross
{
try
{
//把面转成ITopologicalOperator,使用Cut函数
ITopologicalOperator topo = (pFeature.Shape as IPolygon) as ITopologicalOperator;

//新建两个面,作为切割函数的参数,切割后的两个对象保存于这两个面之中
IGeometry pLeftGeometry = new PolygonClass();
IGeometry pRightGeometry = new PolygonClass();
topo.Cut((pline as IPolyline), out pLeftGeometry, out pRightGeometry);

//由FeatureClass新建Feature
IFeature pFeatureLeft = pFeatureLayer.FeatureClass.CreateFeature();
IFeature pFeatureRight = pFeatureLayer.FeatureClass.CreateFeature();

//保存
pFeatureLeft.Shape = pLeftGeometry;
pFeatureRight.Shape = pRightGeometry;
pFeatureLeft.Store();
pFeatureRight.Store();

//删除
pFeature.Delete();
}
catch (Exception ms)
{
MessageBox.Show(ms.Message);
}
}

pFeature = pEnumFeature.Next();
}

//清除选择
IFeatureSelection pFeatureSelectionClear = layer as IFeatureSelection;
pFeatureSelectionClear.Clear();
ArcMap.Document.ActiveView.Refresh();
}
}

posted @ 2022-03-28 11:55  南山种豆8  阅读(94)  评论(0)    收藏  举报