1

I need to draw this rectangle on layoutview :

I need this :

I try this but it doesn't work. How can I draw this rectangle ?

        IPoint p1 = new PointClass();

        p1.X = 9.9936; p1.Y = 5.8777;

        IPoint p2 = new PointClass();

        p2.X = p1.X; p2.Y = p1.Y + 2.62;

        IPoint p3 = new PointClass();

        p3.X = p1.X + 0.152 ; p3.Y = p1.Y;

        IPoint p4 = new PointClass();

        p4.X = p3.X; p4.Y = p3.Y + 2.62;

        IPointCollection ring = new RingClass();
        ring.AddPoint(p1);
        ring.AddPoint(p2);
        ring.AddPoint(p3);
        ring.AddPoint(p4);

        IRgbColor color = new RgbColorClass();
        color.Red = 255; color.Blue = 28; color.Green = 20;

        ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();
        simpleFillSymbol.Color = color;

        IElement element = new PolygonElement();
        element.Geometry = ring as IGeometry;  // I try this.
        element. ???  // I need to apply symbol on this element. but it hasn't symbol property

        IMxDocument mxdoc = ArcMap.Application.Document as IMxDocument;
        IPageLayout pageLayout = new PageLayout();
        pageLayout = mxdoc.PageLayout;
        IGraphicsContainer graphicsContainer = pageLayout as IGraphicsContainer;
        graphicsContainer.AddElement(element, 0);


        IActiveView activeView = pageLayout as IActiveView;

        activeView.Refresh();

This solution has problem at :

    IElement element = new PolygonElement();
    element.Geometry = ring as IGeometry;  
    ((IFillShapeElement)element).Symbol = (IFillSymbol)simpleFillSymbol;

@Berend's solution is working..

  • 1
    Are you saying it does not draw at all or it does not draw in the symbol you want it? Looking at your code you create the element, you create a simple fill but you do not apply it and you simply add the graphic to the page layout and I assume it has used some default colouring? – Hornbydd Sep 29 '15 at 9:20
  • When I run this Add-in, ArcMap is crash..(gives an error). You right, I created polygon element, I created simple fill but I can't apply it..I'm trying to draw this with RingClass now.. But I couldn't yet.. – Umut Sep 29 '15 at 10:02
  • 1
    Try using a PolygonClass instead of a RingClass? It would be helpful if you edit your question and add the error message. – Hornbydd Sep 29 '15 at 10:16
  • @Umut, what is the error you get (On which line)? Please include it in your question – Farid Cheraghi Sep 29 '15 at 10:44
  • 1
    These look like errors from visual studio, not some .net run time error (I could be wrong)? I see you are using 10.3 have you upgraded visual studio recently? You should be using visual studio 2012. – Hornbydd Sep 29 '15 at 15:44
2

Personally I'd prefer a shorter version:

private void AddRectangle()
{
    var element = new RectangleElementClass
    {
        Geometry = new EnvelopeClass {XMin = 9.9936, YMin = 5.8777, Width = 0.152, Height = 2.62},
        Symbol = new SimpleFillSymbolClass {Color = new RgbColorClass {Red = 255, Green = 20, Blue = 28}}
    };

    var mxdoc = ArcMap.Application.Document as IMxDocument;
    var graphicsContainer = mxdoc.PageLayout as IGraphicsContainer;
    graphicsContainer.AddElement(element, 0);

    (mxdoc.PageLayout as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewGraphics, element, null);
}
  • Thank you so much @Berend It's working ! shorter and good solution.. – Umut Oct 1 '15 at 6:13
1

Try

((IFillShapeElement)element).Symbol = (IFillSymbol)simpleFillSymbol;
  • Thank you for solution but it didnt'k work :/ It has an error at : " IElement element = new PolygonElement(); element.Geometry = ring as IGeometry; ((IFillShapeElement)element).Symbol = (IFillSymbol)simpleFillSymbol; " – Umut Oct 1 '15 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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