private List<string> ExportSymbol(IStyleGallery sg,string symbolType, int col, int row, int size,int width, int labelsize, int label_h, int size_w, int size_h, double height, string cat, IPageLayout3 layout)
        {
            List<string> pdfpages = new List<string>();
            IGraphicsContainer con = layout as IGraphicsContainer;
            IPage page = layout.Page;
            int r = 0;
            int c = 0;
            int p = 0;
            IEnumStyleGalleryItem items = sg.get_Items(symbolType, "", cat);
            IStyleGalleryItem item = items.Next();
            while (item != null)
            {
                #region Symbol
                if (symbolType == "Marker Symbols")
                {
                    IMarkerElement mele = new MarkerElementClass();
                    IPoint ptn = new PointClass();
                    ptn.PutCoords(c * size_w + size / 2, height - (r * size_h + size / 2));
                    IElement ele_i = mele as IElement;
                    ele_i.Geometry = ptn;
                    IMarkerSymbol sym_m = item.Item as IMarkerSymbol;
                    sym_m.Size = size;
                    mele.Symbol = sym_m;
                    con.AddElement(ele_i, 0);
                }
                else if (symbolType == "Line Symbols")
                {
                    ILineElement lele = new LineElementClass();
                    IPolyline line = new PolylineClass();
                    IPoint ptn_f = new PointClass();
                    ptn_f.PutCoords(c * size_w + size / 4, height - (r * size_h + size * 3 / 4));
                    IPoint ptn_t = new PointClass();
                    ptn_t.PutCoords(c * size_w + size * 3 / 4, height - (r * size_h + size / 4));
                    line.FromPoint = ptn_f;
                    line.ToPoint = ptn_t;
                    IElement ele_i = lele as IElement;
                    ele_i.Geometry = line;
                    ILineSymbol sym_l = item.Item as ILineSymbol;
                    sym_l.Width = width;
                    lele.Symbol = sym_l;
                    con.AddElement(ele_i, 0);
                }
                else if (symbolType == "Fill Symbols")
                {
                    IRectangleElement fele = new RectangleElementClass();
                    IFillShapeElement fsele = fele as IFillShapeElement;
                    IEnvelope env_f = new EnvelopeClass();
                    env_f.PutCoords(c * size_w + size / 4, height - (r * size_h + size * 3 / 4), c * size_w + size * 3 / 4, height - (r * size_h + size / 4));
                    IElement ele_i = fele as IElement;
                    ele_i.Geometry = env_f;
                    IFillSymbol sym_f = item.Item as IFillSymbol;
                    fsele.Symbol = sym_f;
                    con.AddElement(ele_i, 0);
                }

                #endregion

                if (cbGrid.Checked)
                {
                    #region Grid
                    IEnvelope env_g = new EnvelopeClass();
                    env_g.PutCoords(c * size_w, height - r * size_h, c * size_w + size_w, height - r * size_h - size_h);
                    IRectangleElement gele = new RectangleElementClass();
                    IElement ele_g = gele as IElement;
                    ele_g.Geometry = env_g;
                    IFillSymbol sym_g = new SimpleFillSymbolClass();
                    IRgbColor color_g = new RgbColorClass();
                    color_g.NullColor = true;
                    sym_g.Color = color_g;
                    IFillShapeElement fshp_g = ele_g as IFillShapeElement;
                    fshp_g.Symbol = sym_g;
                    con.AddElement(ele_g, 0);
                    #endregion
                }

                #region Label
                ITextElement tele = new ParagraphTextElementClass();
                IElement ele_t = tele as IElement;
                tele.Text = item.Name;
                IEnvelope env_l = new EnvelopeClass();
                env_l.PutCoords(c * size_w, height - r * size_h - size_h + label_h, c * size_w + size_w, height - r * size_h - size_h);
                env_l.PutCoords(c * size_w, height - r * size_h - size_h + label_h, c * size_w + size_w, height - r * size_h - size_h);
                env_l.PutCoords(c * size_w, height - r * size_h - size_h + label_h, c * size_w + size_w, height - r * size_h - size_h);
                env_l.PutCoords(c * size_w, height - r * size_h - size_h + label_h, c * size_w + size_w, height - r * size_h - size_h);
                env_l.PutCoords(c * size_w, height - r * size_h - size_h + label_h, c * size_w + size_w, height - r * size_h - size_h);
                ele_t.Geometry = env_l;
                ITextSymbol sym_label = new TextSymbolClass();
                sym_label.Size = labelsize/2;
                IRgbColor color = new RgbColorClass();
                color.NullColor = true;
                ISimpleLineSymbol sym_border = new SimpleLineSymbolClass();
                sym_border.Color = color;
                ISymbolBorder border = new SymbolBorderClass();
                border.LineSymbol = sym_border;
                IFrameProperties fp = ele_t as IFrameProperties;
                fp.Border = border;
                con.AddElement(ele_t, 0);
                #endregion

                if (c < col - 1)
                {
                    c++;
                }
                else
                {
                    c = 0;
                    if (r < row - 1)
                    {
                        r++;
                    }
                    else
                    {
                        IActiveView av = layout as IActiveView;
                        string temp_pdf = System.IO.Path.GetTempFileName() + ".pdf";
                        ExportPDF(av, temp_pdf);
                        pdfpages.Add(temp_pdf);
                        con.DeleteAllElements();
                        r = 0;
                        c = 0;
                        p++;
                    }
                }
                item = items.Next();
            }
            if (con.Next() != null)
            {
                IActiveView av = layout as IActiveView;
                string temp_pdf = System.IO.Path.GetTempFileName() + ".pdf";
                ExportPDF(av, temp_pdf);
                pdfpages.Add(temp_pdf);
                con.DeleteAllElements();
            }
            return pdfpages;
        }
Beispiel #2
0
        private static void DrawMapUnits(IEnvelope Envelope, bool showText, double ColumnX)
        {
            // BOX DIMENSIONS AND UNIFORM SYMBOL ITEMS
            double Text2BoxY = 0.2; //Y distance between the bottom of text and the next box
            double Text2BoxX = 0.1; //X distance between a box and the text that describes it
            double BoxX      = 0.4; //Width
            double BoxY      = 0.3; //Height

            // Setup a black color object, black outline
            IRgbColor BlackInsides = new ESRI.ArcGIS.Display.RgbColorClass();

            BlackInsides.Blue  = 0;
            BlackInsides.Red   = 0;
            BlackInsides.Green = 0;

            ILineSymbol BlackOutsides = new SimpleLineSymbolClass();

            BlackOutsides.Width = 1;
            BlackOutsides.Color = BlackInsides;

            // Whole bunch of variables to use while going through the loop below...
            #region Variables Galore!!!
            IMxDocument  Doc        = ArcMap.Document;
            IPageLayout  pageLayout = Doc.ActiveView as IPageLayout;
            IPageLayout3 pl3        = Doc.ActivatedView as IPageLayout3;

            IGraphicsContainer       GraphicsContainer = pageLayout as IGraphicsContainer;
            IGraphicsContainerSelect gCS;
            gCS = (IGraphicsContainerSelect)GraphicsContainer;

            double Xcoord, Ycoord;
            Xcoord = Envelope.XMin;
            Ycoord = Envelope.YMax;
            double IndentTerm   = 0;
            IPoint Point        = null;
            double StringLength = 0;
            string LegendText   = "";
            string ItemName     = "";
            //string ItemDesc = "";
            IElement                    Ele        = null;
            IEnvelope                   TempEnv    = null;
            IRgbColor                   BoxColr    = null;
            ISimpleFillSymbol           FillSym    = new ESRI.ArcGIS.Display.SimpleFillSymbolClass();
            IFillShapeElement           FillEle    = null;
            IEnvelope                   FillEnv    = new EnvelopeClass();
            WKSEnvelope                 Patch      = new WKSEnvelope();
            IGeometry                   Geo        = null;
            string                      LabelText  = "";
            ESRI.ArcGIS.Geometry.IPoint LabelPoint = new ESRI.ArcGIS.Geometry.PointClass();

            // Get the transparency of the MapUnitPolys Layer
            double transparency = 100;
            try
            {
                IFeatureLayer polyLayer    = commonFunctions.FindFeatureLayer(ArcMap.Editor.EditWorkspace, "MapUnitPolys");
                ILayerEffects layerEffects = polyLayer as ILayerEffects;
                transparency = layerEffects.Transparency;
            }
            catch { }

            #endregion

            pl3.DelayEvents = true;

            // Get a reference to the DescriptionOfMapUnits entries
            var sortedDmuEntries = GetDmuSortedByHierarchy();

            try
            {
                // Loop through legend records
                foreach (KeyValuePair <string, DescriptionOfMapUnitsAccess.DescriptionOfMapUnit> aDictionaryEntry in sortedDmuEntries)
                {
                    // Grab the DMU entry itself
                    DescriptionOfMapUnitsAccess.DescriptionOfMapUnit aDescription = aDictionaryEntry.Value;
                    bool isHeading = (aDescription.ParagraphStyle.Contains("Heading"));


                    // Find out how far to indent the legend item
                    // Strip periods from the HierarchyKey, divide by 4, which is the length of a segment of the key
                    IndentTerm = aDescription.HierarchyKey.Replace(".", "").Length / 4;

                    // Get the coordinates of the text for the legend entry - upper left corner
                    // Xcoord starts at Envelope.XMin, Ycoord is Envelope.YMax: Upper left corner
                    Point = new PointClass();
                    double xAdditions = 0;
                    if (isHeading)
                    {
                        // Xcoord plus (indentation), Ycoord
                        xAdditions = 0.2 * (IndentTerm - 1);
                    }
                    else
                    {
                        //Xcoord plus (indentation) + (Box width and margin), Ycoord
                        xAdditions = 0.2 * (IndentTerm - 1) + BoxX + Text2BoxX;
                    }
                    Point.PutCoords(Xcoord + xAdditions, Ycoord);

                    // StringLength is the width remaining in the envelope in which the text has to fit IN PIXELS.
                    StringLength = 72 * (Envelope.Width - xAdditions);

                    // Fix a couple of special characters in the legend string.
                    // Then amalgamate item name and description
                    ItemName = FixLegendTextCharacters(aDescription.Name);
                    if (!isHeading)
                    {
                        LegendText = ItemName + " - " + FixLegendTextCharacters(aDescription.Description);
                    }
                    else
                    {
                        LegendText = ItemName;
                    }



                    // Format the legend text if it is not a heading. If it is, we're fine.
                    if (!isHeading)
                    {
                        LegendText = GetFormattedString(LegendText, "Arial", 8, StringLength, 8);
                    }

                    // Boldify the ItemName
                    LegendText = LegendText.Replace(ItemName, "<bol>" + ItemName + "</bol>");

                    // If the StratCorDiagram is being drawn
                    if (showText == false)
                    {
                        LegendText   = "."; // placeholder
                        StringLength = 1;
                    }

                    // See if this legend item should be placed on a new column
                    Ele = MakeTextElement(Point, LegendText, "Arial") as IElement;

                    TempEnv = new EnvelopeClass();
                    Ele.QueryBounds(Doc.ActiveView.ScreenDisplay, TempEnv);

                    // If the height of the formatted text is larger than the box + space specified
                    if (TempEnv.Height > BoxY + Text2BoxY)
                    {
                        // If the text will spill out below the envelope drawn by the user
                        if (Ycoord - TempEnv.Height < Envelope.YMin)
                        {
                            // Move to a new column - the last number is a fudge factor, looks like it is in inches
                            Xcoord = Xcoord + BoxX + Text2BoxX + StringLength / 72 + ColumnX;
                            // Move to the top
                            Ycoord = Envelope.YMax;
                            // Recreate the text element
                            Point.PutCoords(Xcoord + 0.2 * (IndentTerm - 1) + BoxX + Text2BoxX, Ycoord);
                            Ele = MakeTextElement(Point, LegendText, "Arial") as IElement;
                        }
                    }
                    else // The height of the formatted text is not larger than the box + space defined
                    {
                        // If the box itself will spill out below the envelope drawn by the user
                        if (Ycoord - (BoxY + Text2BoxY) < Envelope.YMin)
                        {
                            // Move to a new column
                            Xcoord = Xcoord + BoxX + Text2BoxX + StringLength / 72 + ColumnX;
                            // Move to the top
                            Ycoord = Envelope.YMax;
                            // Recreate the text element
                            Point.PutCoords(Xcoord + 0.2 * (IndentTerm - 1) + BoxX + Text2BoxX, Ycoord);
                            Ele = MakeTextElement(Point, LegendText, "Arial") as IElement;
                        }
                    }

                    // Only write the legend text if the legend is being drawn (not the StratCorDiagram)
                    if (showText == true)
                    {
                        GraphicsContainer.AddElement(Ele, 0);
                    }

                    if (!isHeading)
                    {
                        FillEnv = new EnvelopeClass();

                        Patch.XMin = Point.X - BoxX - Text2BoxX;
                        Patch.YMax = Point.Y;
                        Patch.XMax = Point.X - Text2BoxX;
                        Patch.YMin = Point.Y - BoxY;
                        FillEnv.PutCoords(Patch.XMin, Patch.YMin, Patch.XMax, Patch.YMax);
                        Geo = FillEnv as IGeometry;

                        // Get the color of the box
                        BoxColr = new RgbColorClass();
                        if (aDescription.AreaFillRGB == null)
                        {
                            BoxColr.Red   = 255;
                            BoxColr.Green = 0;
                            BoxColr.Blue  = 0;
                        }
                        else
                        {
                            BoxColr.Red   = int.Parse(aDescription.AreaFillRGB.Split(';')[0]);
                            BoxColr.Green = int.Parse(aDescription.AreaFillRGB.Split(';')[1]);
                            BoxColr.Blue  = int.Parse(aDescription.AreaFillRGB.Split(';')[2]);
                        }

                        // Set the transparency for the legend color boxes
                        BoxColr.Red   = (int)((255 - BoxColr.Red) * transparency / 100 + BoxColr.Red);
                        BoxColr.Green = (int)((255 - BoxColr.Green) * transparency / 100 + BoxColr.Green);
                        BoxColr.Blue  = (int)((255 - BoxColr.Blue) * transparency / 100 + BoxColr.Blue);

                        // Draw the fill
                        FillSym         = new SimpleFillSymbolClass();
                        FillSym.Color   = BoxColr;
                        FillSym.Style   = esriSimpleFillStyle.esriSFSSolid;
                        FillSym.Outline = BlackOutsides;

                        FillEle = CreateFillElement(Geo, FillSym) as IFillShapeElement;

                        // Label the box
                        LabelText = aDescription.Label;

                        // Subscripting!!

                        for (int i = 0; i < LabelText.Length; i++)
                        {
                            string thisBit = LabelText.Substring(i, 1);
                            int    num;
                            if (int.TryParse(thisBit, out num)) // Checks if the character is numeric
                            {
                                LabelText = LabelText.Replace(thisBit, "<sub>" + thisBit + "</sub>");
                                i         = i + 5;
                            }
                        }
                        LabelText = LabelText.Replace("ir", "i<sub>r</sub>");
                        LabelText = LabelText.Replace("yc", "y<sub>c</sub>");

                        // Center the label
                        LabelPoint   = new PointClass();
                        LabelPoint.X = Point.X - BoxX / 2 - Text2BoxX;
                        LabelPoint.Y = Point.Y - BoxY / 2;

                        //LabelText = GetFormattedString(LabelText, "FGDCGeoAge", 8, StringLength, 0);
                        Ele = MakeTextElement(LabelPoint, LabelText, "FGDCGeoAge", true) as IElement;

                        // Add the box and label
                        IGroupElement3 group = new GroupElementClass();
                        group.AddElement(FillEle as IElement);
                        group.AddElement(Ele);
                        GraphicsContainer.AddElement(group as IElement, 0);
                    }

                    // Do a partial refresh
                    //Doc.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

                    // Setup the Y coordinates for the next entry
                    // if the height of this item's text is bigger than the minimum distance between text and box
                    if (TempEnv.Height > Text2BoxY)
                    {
                        // Subtract the box height and the text height to get the new ycoord
                        Ycoord = Ycoord - (TempEnv.Height + Text2BoxY);
                    }
                    else
                    {
                        if (isHeading)
                        {
                            Ycoord = Ycoord - (TempEnv.Height + Text2BoxY);
                        }
                        else
                        {
                            Ycoord = Ycoord - (BoxY + Text2BoxY);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                pl3.DelayEvents = false;
                string errItem = e.StackTrace + " " + ItemName;
                MessageBox.Show("Legend Draw Map Unit Error: " + errItem);
            }

            pl3.DelayEvents = false;
            gCS.UnselectAllElements();
            // Done, refresh, turn off the tool
            Doc.ActiveView.Refresh();
            ArcMap.Application.CurrentTool = null;
        }