Ejemplo n.º 1
0
        /// <summary>
        /// Initialize this style file object with the content of a file.
        /// </summary>
        /// <param name="file">The file to read the definition from.</param>
        /// <returns>The number of styles that were loaded.</returns>
        int Create(StreamReader file)
        {
            // If we previously created stuff, get rid of it.
            ResetContents();

            // Load an index for standard colour names
            Dictionary <string, Color> colors = LoadColors();

            // Create the index of entity and/or layer name vs it's style
            m_StyleLookup = new Dictionary <string, Style>(100);

            string             line;
            StyleEntry         entry        = new StyleEntry();
            StringBuilder      errors       = new StringBuilder(1000);
            List <DashPattern> dashPatterns = new List <DashPattern>(100);
            List <Style>       styles       = new List <Style>(100);

            while ((line = file.ReadLine()) != null)
            {
                int nToken = entry.Parse(line);
                if (nToken == 0)
                {
                    continue;
                }

                if (nToken < 0)
                {
                    errors.AppendLine(String.Format("Invalid style entry: {0}", line));
                    continue;
                }

                // If we've got the definition of a dash style,
                // parse it and add to the list of styles
                if (entry.IsDashStyle)
                {
                    DashPattern dp = new DashPattern();
                    if (dp.Create(entry))
                    {
                        dashPatterns.Add(dp);
                    }
                    else
                    {
                        errors.AppendLine(String.Format("Cannot parse style definition: {0}", line));
                    }
                }
                else
                {
                    // Only entity & layer entries are currently supported

                    string itemName;

                    if (entry.IsEntityEntry)
                    {
                        itemName = entry.EntityToken;
                    }
                    else if (entry.IsLayerEntry)
                    {
                        itemName = entry.LayerToken;
                    }
                    else
                    {
                        continue;
                    }

                    // Both entity & layer entries should have some sort of colour (either a
                    // named colour, or an RGB value)

                    Color  col        = Color.Black;
                    bool   colDefined = false;
                    string colName    = entry.ColToken;

                    if (colName.Length == 0)
                    {
                        string rgb = entry.RGBToken;
                        colDefined = TryParseRGB(rgb, out col);
                    }
                    else
                    {
                        string fmtcol = FormatColorName(colName);
                        colDefined = colors.ContainsKey(fmtcol);
                        if (colDefined)
                        {
                            col = colors[fmtcol];
                        }
                    }

                    if (!colDefined)
                    {
                        errors.AppendLine(String.Format("Invalid colour in: {0}", line));
                        continue;
                    }

                    // Get any line pattern
                    DashPattern dp  = null;
                    string      pat = entry.DashToken;
                    if (pat.Length > 0)
                    {
                        dp = dashPatterns.Find(d => d.Name == pat);
                        if (dp == null)
                        {
                            errors.AppendLine(String.Format("Cannot find dash pattern for: {0}", line));
                            continue;
                        }
                    }

                    // Get any line weight (sanity check forces it
                    // to be in the range (0.01,10.0))

                    float  fwt = -1.0F;
                    string wt  = entry.WtToken;
                    if (wt.Length > 0)
                    {
                        if (!Single.TryParse(wt, out fwt) || fwt < 0.01F || fwt > 10.0F)
                        {
                            errors.AppendLine(String.Format("Invalid line weight: {0}", line));
                            continue;
                        }
                    }

                    // Create a suitable style
                    Style style;

                    if (fwt < 0.01F && dp == null)
                    {
                        style = new Style(col);
                    }
                    else
                    {
                        style = new LineStyle(col, fwt, dp);
                    }

                    // If we already have an identical style, use that instead
                    Style prevStyle = styles.Find(s => style.IsMatch(s));
                    if (prevStyle != null)
                    {
                        m_StyleLookup[itemName] = prevStyle;
                    }
                    else
                    {
                        styles.Add(style);
                        m_StyleLookup[itemName] = style;
                    }
                }
            }

            m_DashPatterns = dashPatterns.ToArray();
            m_Styles       = styles.ToArray();

            if (errors.Length > 0)
            {
                throw new Exception(errors.ToString());
            }

            // Return the number of styles that we loaded.
            return(m_Styles.Length);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initialize this style file object with the content of a file.
        /// </summary>
        /// <param name="file">The file to read the definition from.</param>
        /// <returns>The number of styles that were loaded.</returns>
        int Create(StreamReader file)
        {
            // If we previously created stuff, get rid of it.
            ResetContents();

            // Load an index for standard colour names
            Dictionary<string, Color> colors = LoadColors();

            // Create the index of entity and/or layer name vs it's style
            m_StyleLookup = new Dictionary<string, Style>(100);

            string line;
            StyleEntry entry = new StyleEntry();
            StringBuilder errors = new StringBuilder(1000);
            List<DashPattern> dashPatterns = new List<DashPattern>(100);
            List<Style> styles = new List<Style>(100);

            while ((line=file.ReadLine())!=null)
            {
                int nToken = entry.Parse(line);
                if (nToken == 0)
                    continue;

                if (nToken < 0)
                {
                    errors.AppendLine(String.Format("Invalid style entry: {0}", line));
                    continue;
                }

                // If we've got the definition of a dash style,
                // parse it and add to the list of styles
                if (entry.IsDashStyle)
                {
                    DashPattern dp = new DashPattern();
                    if (dp.Create(entry))
                        dashPatterns.Add(dp);
                    else
                        errors.AppendLine(String.Format("Cannot parse style definition: {0}", line));

                }
                else
                {
                    // Only entity & layer entries are currently supported

                    string itemName;

                    if (entry.IsEntityEntry)
                        itemName = entry.EntityToken;
                    else if (entry.IsLayerEntry)
                        itemName = entry.LayerToken;
                    else
                        continue;

                    // Both entity & layer entries should have some sort of colour (either a
                    // named colour, or an RGB value)

                    Color col = Color.Black;
                    bool colDefined = false;
                    string colName = entry.ColToken;

                    if (colName.Length==0)
                    {
                        string rgb = entry.RGBToken;
                        colDefined = TryParseRGB(rgb, out col);
                    }
                    else
                    {
                        string fmtcol = FormatColorName(colName);
                        colDefined = colors.ContainsKey(fmtcol);
                        if (colDefined)
                            col = colors[fmtcol];
                    }

                    if (!colDefined)
                    {
                        errors.AppendLine(String.Format("Invalid colour in: {0}", line));
                        continue;
                    }

                    // Get any line pattern
                    DashPattern dp = null;
                    string pat = entry.DashToken;
                    if (pat.Length > 0)
                    {
                        dp = dashPatterns.Find(d => d.Name==pat);
                        if (dp==null)
                        {
                            errors.AppendLine(String.Format("Cannot find dash pattern for: {0}", line));
                            continue;
                        }
                    }

                    // Get any line weight (sanity check forces it
                    // to be in the range (0.01,10.0))

                    float fwt = -1.0F;
                    string wt = entry.WtToken;
                    if (wt.Length>0)
                    {
                        if (!Single.TryParse(wt, out fwt) || fwt<0.01F || fwt>10.0F)
                        {
                            errors.AppendLine(String.Format("Invalid line weight: {0}", line));
                            continue;
                        }
                    }

                    // Create a suitable style
                    Style style;

                    if (fwt < 0.01F && dp==null)
                        style = new Style(col);
                    else
                        style = new LineStyle(col, fwt, dp);

                    // If we already have an identical style, use that instead
                    Style prevStyle = styles.Find(s => style.IsMatch(s));
                    if (prevStyle!=null)
                        m_StyleLookup[itemName] = prevStyle;
                    else
                    {
                        styles.Add(style);
                        m_StyleLookup[itemName] = style;
                    }
                }
            }

            m_DashPatterns = dashPatterns.ToArray();
            m_Styles = styles.ToArray();

            if (errors.Length>0)
                throw new Exception(errors.ToString());

            // Return the number of styles that we loaded.
            return m_Styles.Length;
        }