Beispiel #1
0
        protected virtual void OnLinetypeSegmentAddedEvent(LinetypeSegment item)
        {
            LinetypeSegmentAddedEventHandler ae = this.LinetypeSegmentAdded;

            if (ae != null)
            {
                ae(this, new LinetypeSegmentChangeEventArgs(item));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Creates a new line type from the definition in a LIN file.
        /// </summary>
        /// <param name="file">Lin file where the definition is located.</param>
        /// <param name="linetypeName">Name of the line type definition to read (ignore case).</param>
        /// <returns>The linetype defined in the LIN file with the specified name, null if the linetype has not been found in the linetype definitions file.</returns>
        public static Linetype Load(string file, string linetypeName)
        {
            Linetype linetype = null;
            List <LinetypeSegment> segments = new List <LinetypeSegment>();

            using (StreamReader reader = new StreamReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), true))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    if (line == null)
                    {
                        throw new FileLoadException("Unknown error reading LIN file.", file);
                    }
                    // lines starting with semicolons are comments
                    if (line.StartsWith(";"))
                    {
                        continue;
                    }
                    // every line type definition starts with '*'
                    if (!line.StartsWith("*"))
                    {
                        continue;
                    }

                    // reading line type name and description
                    int    endName     = line.IndexOf(','); // the first semicolon divides the name from the description that might contain more semicolons
                    string name        = line.Substring(1, endName - 1);
                    string description = line.Substring(endName + 1, line.Length - endName - 1);

                    // remove start and end spaces
                    description = description.Trim();

                    if (name.Equals(linetypeName, StringComparison.OrdinalIgnoreCase))
                    {
                        // we have found the line type name, the next line of the file contains the line type definition
                        line = reader.ReadLine();
                        if (line == null)
                        {
                            throw new FileLoadException("Unknown error reading LIN file.", file);
                        }

                        string[] tokens = line.Split(',');

                        // the index 0 is always A (alignment field)
                        for (int i = 1; i < tokens.Length; i++)
                        {
                            double length;
                            if (double.TryParse(tokens[i], out length))
                            {
                                // is the length followed by a shape o text segment
                                if (i + 1 < tokens.Length)
                                {
                                    if (tokens[i + 1].StartsWith("[", StringComparison.CurrentCultureIgnoreCase))
                                    {
                                        StringBuilder data = new StringBuilder();
                                        // there are two kinds of complex linetype Text and Shape,
                                        // the data is enclosed in brackets
                                        bool end = false;
                                        while (!end)
                                        {
                                            data.Append(tokens[++i]);
                                            data.Append(",");
                                            if (i >= tokens.Length)
                                            {
                                                return(null); // text and shape data must be enclosed by brackets
                                            }
                                            if (tokens[i].EndsWith("]"))
                                            {
                                                data.Append(tokens[i]);
                                                end = true;
                                            }
                                        }
                                        LinetypeSegment segment = ReadLineTypeComplexSegment(data.ToString(), length);
                                        if (segment != null)
                                        {
                                            segments.Add(segment);
                                        }
                                    }
                                    else
                                    {
                                        segments.Add(new LinetypeSimpleSegment(length));
                                    }
                                }
                                else
                                {
                                    segments.Add(new LinetypeSimpleSegment(length));
                                }
                            }
                            else
                            {
                                throw new FormatException("The linetype definition is not well formatted.");
                            }
                        }
                        linetype = new Linetype(name, segments, description);
                        break;
                    }
                }
            }
            return(linetype);
        }
Beispiel #3
0
        /// <summary>
        /// Creates a new line type from the definition in a LIN file.
        /// </summary>
        /// <param name="file">Lin file where the definition is located.</param>
        /// <param name="linetypeName">Name of the line type definition to read (ignore case).</param>
        /// <returns>The linetype defined in the LIN file with the specified name, null if the linetype has not been found in the linetype definitions file.</returns>
        public static Linetype Load(string file, string linetypeName)
        {
            if (string.IsNullOrEmpty(file))
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (!string.Equals(Path.GetExtension(file), ".LIN", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ArgumentException("The linetype definitions file must have the extension LIN.", nameof(file));
            }

            if (string.IsNullOrEmpty(linetypeName))
            {
                return(null);
            }

            Linetype linetype = null;
            List <LinetypeSegment> segments = new List <LinetypeSegment>();

            using (StreamReader reader = new StreamReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), true))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    if (line == null)
                    {
                        throw new FileLoadException("Unknown error reading LIN file.", file);
                    }

                    // every line type definition starts with '*'
                    if (!line.StartsWith("*"))
                    {
                        continue;
                    }

                    // reading line type name and description
                    int    endName     = line.IndexOf(','); // the first comma divides the name from the description that might contain more commas
                    string name        = line.Substring(1, endName - 1);
                    string description = line.Substring(endName + 1, line.Length - endName - 1);

                    // remove start and end spaces
                    description = description.Trim();

                    if (name.Equals(linetypeName, StringComparison.OrdinalIgnoreCase))
                    {
                        // we have found the line type name, the next line of the file contains the line type definition
                        line = reader.ReadLine();
                        if (line == null)
                        {
                            throw new FileLoadException("Unknown error reading LIN file.", file);
                        }

                        string[] tokens = System.Text.RegularExpressions.Regex.Split(line, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");

                        // the index 0 is always A (alignment field)
                        for (int i = 1; i < tokens.Length; i++)
                        {
                            double length;
                            if (double.TryParse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture, out length))
                            {
                                // is the length followed by a shape or text segment
                                if (i + 1 < tokens.Length)
                                {
                                    if (tokens[i + 1].StartsWith("["))
                                    {
                                        List <string> data = new List <string>();

                                        // there are two kinds of complex linetype Text and Shape, the data is enclosed in brackets
                                        for (++i; i < tokens.Length; i++)
                                        {
                                            data.Add(tokens[i]);

                                            // text and shape data must be enclosed by brackets
                                            if (i >= tokens.Length)
                                            {
                                                throw new FormatException("The linetype definition is not well formatted.");
                                            }

                                            if (tokens[i].EndsWith("]"))
                                            {
                                                break;
                                            }
                                        }
                                        LinetypeSegment segment = ReadLineTypeComplexSegment(data.ToArray(), length);
                                        if (segment != null)
                                        {
                                            segments.Add(segment);
                                        }
                                    }
                                    else
                                    {
                                        segments.Add(new LinetypeSimpleSegment(length));
                                    }
                                }
                                else
                                {
                                    segments.Add(new LinetypeSimpleSegment(length));
                                }
                            }
                            else
                            {
                                throw new FormatException("The linetype definition is not well formatted.");
                            }
                        }
                        linetype = new Linetype(name, segments, description);
                        break;
                    }
                }
            }
            return(linetype);
        }
 /// <summary>
 /// Initializes a new instance of <c>LinetypeSegmentChangeEventArgs</c>.
 /// </summary>
 /// <param name="item">The item that is being added or removed from the line type segment list.</param>
 public LinetypeSegmentChangeEventArgs(LinetypeSegment item)
 {
     this.item = item;
 }