4

I have an annotation Layer of Geometry Type Polygon. There are few hundred features in it and I need to shrink each of them to a smaller size (for example reducing them by 50%). I am trying to use the following sample code but getting

object reference not set to an instance of an object

error at the line

pFeatureCursor = pGeoFeatureLayer.FeatureClass.Search(pSpatialfilter, false);

because of which I am not able to test and make sure if the logic written after this line is correct or not.

Is there sample code or a correction in given code I can use to achieve the desired result?

I am using C#, ArcGIS SDK for .NET version 10.2, .NET Framework 3.5 and Visual Studio 2010.

 protected override void OnClick()
        {
            try
            {
                IMxDocument pMxDocument = ArcMap.Application.Document as IMxDocument;
                IActiveView pActiveView = (IActiveView)pMxDocument.FocusMap;

            string AnnoLayerName = "";
            AnnoLayerName = "MainPipeAnno";

            IAnnotationLayer pAnnotationLayer = null;               

            ISpatialFilter pSpatialfilter = new SpatialFilterClass();
            pSpatialfilter.WhereClause = "annotationClassID = 'Leader Line'";

            pAnnotationLayer = GetAnnotationLayer(AnnoLayerName);

            IFeatureCursor pFeatureCursor = null;
            IGeoFeatureLayer pGeoFeatureLayer = pAnnotationLayer as IGeoFeatureLayer;

            pFeatureCursor = pGeoFeatureLayer.FeatureClass.Search(pSpatialfilter, false);

            IFeature pFeature = null;
            IAnnotationFeature pAnnotationFeature = pFeature as IAnnotationFeature;            


            while ((pFeature = pFeatureCursor.NextFeature()) != null)
            {                 
                IGeometry pFeatureGeom = pFeature.ShapeCopy;

                // Create a point to act as origin at the centre of this polygon or collection of polygons using IArea interface
                IArea pFeatureArea = (IArea)pFeatureGeom;
                IPoint origin = pFeatureArea.Centroid;

                //Cast the feature geometry into the correct Interface
                ITransform2D transformScalePoint = pFeatureGeom as ITransform2D;

                //Perform Scale 
                transformScalePoint.Scale(origin, .5, .5);

                // now the object stored in pFeatureGeom is reduced to 50% of size scaled around the centre of the feature
                pFeature.Shape = pFeatureGeom; // update the features' geometry

                pFeature.Store();
                pActiveView.Refresh();
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }


  

    //This Function fetches the Annotationlayer from ArcMap depending upon Layer Name passed
    public static IAnnotationLayer GetAnnotationLayer(string strAnnotationLayerName)
    {
        IMxDocument pMxDocument = ArcMap.Application.Document as IMxDocument;
        IMap pMap = pMxDocument.FocusMap;

        IAnnotationLayer retAnnotationLayer = null;

        for (int i = 0; i < pMap.LayerCount; i++)
        {
            ILayer tempLayer = pMap.get_Layer(i);

            if (tempLayer.Name == strAnnotationLayerName)
            {                  
                retAnnotationLayer = (IAnnotationLayer)tempLayer;
                return retAnnotationLayer;
            }
        }

        return retAnnotationLayer;
    }

1 Answer 1

2

Looking at your code you initialise the spatialfilter with a where clause but you don't specify the spatial relationship or geometry, the clue is in the name...

If you are only selecting features based upon an SQL query as you have "annotationClassID = 'Leader Line'" then use an IQueryFilter object instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.