Exception casting/QI to IGeometry5

1380
6
Jump to solution
09-27-2012 12:40 PM
KeithGemeinhart
Occasional Contributor
Given the following code, I'm seeing InvalidCast exceptions on the members of IGeometry5 interface. The last line executes without exception, but if you look at the object in the watch window, then you can see the problem:

[ATTACH=CONFIG]18056[/ATTACH]

Am I doing something wrong or is this a bug? I first identified the problem when I was trying to serialize to Json, but it appears the serialization did not like the fact that all of these members of IGeometry5 are invalid.

This is ArcObjects 10.1 .... more specifically, I'm using the C/JMTK build, but I don't think that makes a difference.


            //pPointColl is the new polyline.             IPointCollection pPointColl = new PolylineClass();              //TODO:             IPoint pt = new Point();             pt.PutCoords(10, 0);             pt.Z = 1;             pt.ID = 1;             pPointColl.AddPoint(pt);              pt = new Point();             pt.PutCoords(10, 10);             pt.Z = 0;             pt.ID = 2;             pPointColl.AddPoint(pt);              pt = new Point();             pt.PutCoords(20, 10);             pt.Z = 0;              pt.ID = 3;             pPointColl.AddPoint(pt);              pt = new Point();             pt.PutCoords(20, 0);             pt.Z = 0;              pt.ID = 4;             pPointColl.AddPoint(pt);              (pPointColl as IPolyline).SpatialReference = MyCodeLib.DefaultSpatialReference();             (pPointColl as IZAware).ZAware = true;              var itopo = (ITopologicalOperator) pPointColl;             itopo.Simplify();              IGeometry5 geom5 = (IGeometry5) pPointColl; 
0 Kudos
1 Solution

Accepted Solutions
KeithGemeinhart
Occasional Contributor
I found the problem ... I was running this code inside a unit test (NUnit) via the Resharper test runner. When I run it outside of the unit test, it performs correctly. This is not the first anomalous occurrence we have found when unit testing ArcObjects code.

Thanks to all who replied. Your indications that it was working caused me to take a step back and reconsider everything that was going on -- not just focus on the code.

View solution in original post

0 Kudos
6 Replies
KenBuja
MVP Esteemed Contributor
I ran this version of the code in both 10.0 and 10.1 and it works properly.

[ATTACH=CONFIG]18058[/ATTACH]

    Private Sub GeometryTest()

        Dim pGeometry As ESRI.ArcGIS.Geometry.IGeometry5
        Dim pPointColl As ESRI.ArcGIS.Geometry.IPointCollection
        Dim pt As ESRI.ArcGIS.Geometry.IPoint
        Dim pTopo As ESRI.ArcGIS.Geometry.ITopologicalOperator

        Try
            pPointColl = New ESRI.ArcGIS.Geometry.Polyline

            pt = New ESRI.ArcGIS.Geometry.Point
            pt.PutCoords(10, 0)
            pt.Z = 1
            pt.ID = 1
            pPointColl.AddPoint(pt)

            pt = New ESRI.ArcGIS.Geometry.Point()
            pt.PutCoords(10, 10)
            pt.Z = 0
            pt.ID = 2
            pPointColl.AddPoint(pt)

            pt = New ESRI.ArcGIS.Geometry.Point()
            pt.PutCoords(20, 10)
            pt.Z = 0
            pt.ID = 3
            pPointColl.AddPoint(pt)

            pt = New ESRI.ArcGIS.Geometry.Point()
            pt.PutCoords(20, 0)
            pt.Z = 0
            pt.ID = 4
            pPointColl.AddPoint(pt)

            'TryCast(pPointColl, ESRI.ArcGIS.Geometry.IPolyline).SpatialReference = MyCodeLib.DefaultSpatialReference()
            'TryCast(pPointColl, IZAware).ZAware = True

            pTopo = DirectCast(pPointColl, ESRI.ArcGIS.Geometry.ITopologicalOperator)
            pTopo.Simplify()

            pGeometry = DirectCast(pPointColl, ESRI.ArcGIS.Geometry.IGeometry5)

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString)
        End Try
    End Sub
0 Kudos
KeithGemeinhart
Occasional Contributor
What do you get if you add this line at the end?

var centroid = geom5.CentroidEx;



I get ....
Unable to cast COM object of type 'ESRI.ArcGIS.Geometry.PolylineClass' to interface type 'ESRI.ArcGIS.Geometry.IGeometry5'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{134B247E-83F6-471C-9AD1-11C35312D5EF}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
0 Kudos
JohnHauck
Occasional Contributor II
While it doesn't seem like this would be due to the spatial reference that's one part that we cannot fully see with the sample code. Like kenbuja I just avoided setting this and also ran on 10.0 and 10.1 with no issues. Is this problem specific to IGeometry5 or do other IGeometry or even IPolyline interfaces also produce similar results?
0 Kudos
KeithGemeinhart
Occasional Contributor
If I comment the spatial reference call, it's still the same result. It is only on IGeometry5.  IGeometry and IPolyline are both OK.

I didn't want to overcomplicate it with the code for spatial reference, but here it is ... I didn't write it ... circa 2005 ...

 
        public IGeographicCoordinateSystem2 DefineWGS84GeoCoordsys()
       {
            ISpatialReferenceFactory2 spatRefFact = new SpatialReferenceEnvironmentClass();

            // Create WGS84 Geographic Coordinate System - esriSRGeoCS_WGS1984 - 'GCS_WGS_1984'.
            IGeographicCoordinateSystem2 pGeoCoordSys = (IGeographicCoordinateSystem2)spatRefFact.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);

            // Set the domain extents for the new dataset
            // You can use either the Domain or the FalseOriginAndUnits methods
            // (note that the xy domain must be square).
            pGeoCoordSys.SetFalseOriginAndUnits(-180, -90, 1000000);
            pGeoCoordSys.SetMFalseOriginAndUnits(1, 1);
            pGeoCoordSys.SetZFalseOriginAndUnits(1, 1);

            return pGeoCoordSys;
        }
0 Kudos
KenBuja
MVP Esteemed Contributor
I ran this code and got a message box with the coordinates 15, 6.66666666666667


    Private Sub GeometryTest()

        Dim pGeometry As ESRI.ArcGIS.Geometry.IGeometry5
        Dim pPointColl As ESRI.ArcGIS.Geometry.IPointCollection
        Dim pt As ESRI.ArcGIS.Geometry.IPoint
        Dim pTopo As ESRI.ArcGIS.Geometry.ITopologicalOperator

        Try
            pPointColl = New ESRI.ArcGIS.Geometry.Polyline

            pt = New ESRI.ArcGIS.Geometry.Point
            pt.PutCoords(10, 0)
            pt.Z = 1
            pt.ID = 1
            pPointColl.AddPoint(pt)

            pt = New ESRI.ArcGIS.Geometry.Point()
            pt.PutCoords(10, 10)
            pt.Z = 0
            pt.ID = 2
            pPointColl.AddPoint(pt)

            pt = New ESRI.ArcGIS.Geometry.Point()
            pt.PutCoords(20, 10)
            pt.Z = 0
            pt.ID = 3
            pPointColl.AddPoint(pt)

            pt = New ESRI.ArcGIS.Geometry.Point()
            pt.PutCoords(20, 0)
            pt.Z = 0
            pt.ID = 4
            pPointColl.AddPoint(pt)

            TryCast(pPointColl, ESRI.ArcGIS.Geometry.IPolyline).SpatialReference = DefineWGS84GeoCoordsys()
            TryCast(pPointColl, ESRI.ArcGIS.Geometry.IZAware).ZAware = True

            pTopo = DirectCast(pPointColl, ESRI.ArcGIS.Geometry.ITopologicalOperator)
            pTopo.Simplify()

            pGeometry = DirectCast(pPointColl, ESRI.ArcGIS.Geometry.IGeometry5)

            Dim centroid As ESRI.ArcGIS.Geometry.IPoint
            centroid = pGeometry.CentroidEx

            MsgBox(centroid.X & ", " & centroid.Y)
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Public Function DefineWGS84GeoCoordsys() As ESRI.ArcGIS.Geometry.IGeographicCoordinateSystem2
        Dim spatRefFact As ESRI.ArcGIS.Geometry.ISpatialReferenceFactory2 = New ESRI.ArcGIS.Geometry.SpatialReferenceEnvironment

        ' Create WGS84 Geographic Coordinate System - esriSRGeoCS_WGS1984 - 'GCS_WGS_1984'.
        Dim pGeoCoordSys As ESRI.ArcGIS.Geometry.IGeographicCoordinateSystem2 = DirectCast(spatRefFact.CreateGeographicCoordinateSystem(CInt(ESRI.ArcGIS.Geometry.esriSRGeoCSType.esriSRGeoCS_WGS1984)), ESRI.ArcGIS.Geometry.IGeographicCoordinateSystem2)

        ' Set the domain extents for the new dataset
        ' You can use either the Domain or the FalseOriginAndUnits methods
        ' (note that the xy domain must be square).
        pGeoCoordSys.SetFalseOriginAndUnits(-180, -90, 1000000)
        pGeoCoordSys.SetMFalseOriginAndUnits(1, 1)
        pGeoCoordSys.SetZFalseOriginAndUnits(1, 1)

        Return pGeoCoordSys
    End Function
0 Kudos
KeithGemeinhart
Occasional Contributor
I found the problem ... I was running this code inside a unit test (NUnit) via the Resharper test runner. When I run it outside of the unit test, it performs correctly. This is not the first anomalous occurrence we have found when unit testing ArcObjects code.

Thanks to all who replied. Your indications that it was working caused me to take a step back and reconsider everything that was going on -- not just focus on the code.
0 Kudos