Knoche

BUG in IGeometryBridge2.ReplacePoints in v10?

Discussion created by Knoche on Jun 7, 2011
Latest reply on May 2, 2012 by bennetjo
I have a piece of code that was working just fine in 9.2/3.  Now, using it on the same data that I had before, it's giving me an error "object reference not set to an instance.."

Here's the code :  (it craps out on the IGeometryBridge2.ReplacePoints).  This is the 3rd bug I've found since going to 10, btw.

   public static void SnapLineToClosestPointsUsingCenterpointRecursion(ref IPolyline pline, IPointCollection4 pointCol,ref ISegmentCollection finalPoly,
                                                                            double lengthThreshold, double SearchRadius )
        {
            try
            {
                int newPartIdx;
                int newSegIdx;
                bool splitHappened;

                // this means that each of the new segments resulting from a split in the middle is required.
                if (lengthThreshold > pline.Length / 2)
                {
                    pline.SimplifyNetwork();
                    finalPoly.AddSegmentCollection(pline as ISegmentCollection);
                    return;
                }
                ISegmentCollection segcoldb = pline as ISegmentCollection;

                pline.SplitAtDistance(pline.Length / 2, false, false, out splitHappened, out newPartIdx, out newSegIdx);
                IPoint centerPoint = ((IPointCollection4)pline).get_Point(1);
                HitTestResults htr = new HitTestResults();
                // keep increasing the search radius until a point is found.
                while (htr.hitPartIndex == -1)
                {  
                    htr = FindClosestPoint(esriGeometryHitPartType.esriGeometryPartVertex, centerPoint, (IGeometry)pointCol, SearchRadius);
                    SearchRadius = SearchRadius * 1.25;
                }
                IPointCollection4 plineAsPoints = pline as IPointCollection4;

                // the closest point to the center point in the line is the endpoint so that segment is where it needs to be.
                IRelationalOperator relop = htr.hitPoint as IRelationalOperator;
                if (relop.Equals(pline.FromPoint) || relop.Equals(pline.ToPoint))
                    return;

                IPoint[] newCenterPoint = new IPoint[1];
                newCenterPoint[0] = htr.hitPoint;

                // only doing this for the geometry bridge thing
                IPoint[] points = new IPoint[plineAsPoints.PointCount];
                for (int i = 0; i < plineAsPoints.PointCount; i++)
                    points[i] = pointCol.get_Point(i);

                IGeometryBridge2 geometryBridge = new GeometryEnvironmentClass();
                geometryBridge.AddPoints(plineAsPoints, ref points);
                geometryBridge.ReplacePoints(plineAsPoints, 1, 1, ref newCenterPoint);

                // these are each of the new segments that was created by inserting a new center point on the segment and adjusting 
                // it to fit the closest point on the pointCol

                IPolyline seg1 = ConvertSegmentToPolyline((pline as ISegmentCollection).get_Segment(0));
                IPolyline seg2 = ConvertSegmentToPolyline((pline as ISegmentCollection).get_Segment(1));

                // call this function again to split them further.
                SnapLineToClosestPointsUsingCenterpointRecursion(ref seg1, pointCol, ref finalPoly, lengthThreshold, SearchRadius);
                SnapLineToClosestPointsUsingCenterpointRecursion(ref seg2, pointCol, ref finalPoly, lengthThreshold, SearchRadius);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Outcomes