Can't get ITopologicalOperator4.Cut2 to work.

482
4
Jump to solution
01-17-2024 07:24 AM
Mikael-J
New Contributor II

Hi,

I want to split a polygon by a line, but I can't get ITopologicalOperator4.Cut2 to work. The outPolygon variable becomes null. Test data is in a file geodatabase. Any ideas?

var mxd = ArcMap.Application.Document as IMxDocument;

IFeatureClass inFc = ((IFeatureLayer)mxd.FocusMap.Layer[0]).FeatureClass;    // One simple polygon

IFeatureClass lineFc = ((IFeatureLayer)mxd.FocusMap.Layer[1]).FeatureClass; // One simple polyline stretching over the polygon

 

IFeatureCursor inCur = inFc.Search(null, true);

IFeature inFtr = inCur.NextFeature();

IPolygon polygon = inFtr.ShapeCopy as IPolygon;

 

IFeatureCursor lineCur = lineFc.Search(null, true);

IFeature lineFtr = lineCur.NextFeature();

IPolyline polyline = lineFtr.ShapeCopy as IPolyline;

 

ITopologicalOperator4 topo = polygon as ITopologicalOperator4;

IGeometryCollection geomColl = topo.Cut2(polyline);

IPolygon outPolygon = geomColl as IPolygon; // outPolygon is null

0 Kudos
1 Solution

Accepted Solutions
Mikael-J
New Contributor II

Couldn't get the ITopologicalOperator4.Cut2 to work. So I used the ITopologicalOperator2.Cut(splitLine, out p1, out p2) instead. That works almost as expected. It splits the polygon in two pieces p1 and p2. The only weird thing is that it dissolves any polygon parts in p1 and p2, that is not expected at all. But it solved my problem. Many thanks.

/Mikael

View solution in original post

0 Kudos
4 Replies
BrentHoskisson
Occasional Contributor III

My question is:  Is geomColl null?  Or do you have two polygons in that collection?

The "As" operator will set outPolygon to null if geomColl cannot be cast into an IPolygon type.

If geomColl.GeometryCount = 2, you might need to pull out one polygon at a time.

Brent Hoskisson

 

Mikael-J
New Contributor II

Hi, and thank you for the response.

The input data is only one simple rectangle polygon in a file geodatabase, and the resulting clipped output polygon becomes null (IPolygon outPolygon = geomColl as IPolygon; // outPolygon is null). I want the output to be one multipart polygon. But I will try to pull out each polygon part and then put them together again.

0 Kudos
Mikael-J
New Contributor II

Hi,

I tried to pull out each geometry and to put them together again using a geometrycollection, se below. All geometries are known_simple, and the output geomColl collection do have a geometry count of 2. But at the first AddGeometry row ArcMap crashes. 

...

ITopologicalOperator4 topo = polygon as ITopologicalOperator4;
IGeometryCollection geomColl = topo.Cut2(polyline);

object obj = Type.Missing;
IGeometryCollection gc = new PolygonClass();
gc.AddGeometry(geomColl.get_Geometry(0), ref obj, ref obj);
gc.AddGeometry(geomColl.get_Geometry(1), ref obj, ref obj);
gc.GeometriesChanged();

IPolygon p = gc as IPolygon;

Mikael

0 Kudos
Mikael-J
New Contributor II

Couldn't get the ITopologicalOperator4.Cut2 to work. So I used the ITopologicalOperator2.Cut(splitLine, out p1, out p2) instead. That works almost as expected. It splits the polygon in two pieces p1 and p2. The only weird thing is that it dissolves any polygon parts in p1 and p2, that is not expected at all. But it solved my problem. Many thanks.

/Mikael

0 Kudos