C'è sicuramente qualcosa di sospetto qui con 10.0 sp2.
Posso lanciare un sacchetto di geometria su IRelationalOperator2, altrimenti questo test direbbe "impossibile eseguire il cast", inoltre otterrei un'eccezione di riferimento null.
Tuttavia, alla prima chiamata a IsNear ricevo l'eccezione.
System.InvalidCastException occurred
Message=Unable to cast COM object of type 'ESRI.ArcGIS.Geometry.GeometryBagClass' to interface type 'ESRI.ArcGIS.Geometry.IRelationalOperator2'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{839F5C7E-ED5F-4B3F-8F97-C0A9CC4817EE}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Source=ESRI.ArcGIS.Geometry
Ecco il test:
private void GBTest(IPoint pnt)
{
// this line does not fail
var relop = new GeometryBagClass() as IRelationalOperator2;
if (relop == null)
Debug.Print("unable to cast"); // this doesn't print out
var coll = relop as IGeometryCollection;
coll.AddGeometry(pnt);
//((ISpatialIndex)coll).AllowIndexing = true;
//((ISpatialIndex)coll).Invalidate();
var pnt2 = ((IClone)pnt).Clone() as IPoint;
((ITransform2D)pnt2).Move(100.0, 100.0);
if (relop.IsNear(pnt2, 1000.0)) // exception here
Debug.Print("test 1 fail");
if (!relop.IsNear(pnt2, 10.0))
Debug.Print("test 2 fail");
}
Ecco un altro test. Notare come posso trasmettere senza eccezioni, ma ottenere un'eccezione InvalidCast quando si chiama IsNear.
private void GBTest(IPoint pnt)
{
var coll = new GeometryBagClass();
((IGeometry)coll).SpatialReference = pnt.SpatialReference;
coll.AddGeometry(pnt);
coll.GeometriesChanged();
var relop = (IRelationalOperator2)coll; // would expect the exception here
var pnt2 = ((IClone)pnt).Clone() as IPoint;
((ITransform2D)pnt2).Move(100.0, 100.0);
if (relop.IsNear(pnt2, 1000.0)) // exception here
Debug.Print("test 1 fail");
if (!relop.IsNear(pnt2, 10.0))
Debug.Print("test 2 fail");
}