Example #1
0
        /// <summary>
        /// Reads point data from the specified file and stores it in the object.
        /// </summary>
        /// <param name="anglesInDegrees">Specifies whether the angles in a G330 line are in degrees or radians (default).</param>
        /// <exception cref="Exception">
        /// Thrown for any of the following reasons:<br></br><br></br>
        /// Multiple control characters found for a single point.<br></br>Point has no corresponding G801 line in MSR file.<br></br>
        /// Missing G800 line in MSR file after parsing points.<br></br>No points found in MSR file.
        /// </exception>
        private void ReadFile(bool anglesInDegrees)
        {
            //Read the lines in from the file
            List <string> lines = null;

            lines = ReadTextLines();

            Int32 numberOfStarts = default(Int32);
            Int32 numberOfEnds   = default(Int32);

            foreach (string line in lines)
            {
                if (line.ToLowerInvariant().Contains("start"))
                {
                    numberOfStarts = numberOfStarts + 1;
                }

                if (line.ToLowerInvariant().Contains("end"))
                {
                    numberOfEnds = numberOfEnds + 1;
                }
            }

            if (numberOfStarts > 1)
            {
                throw new Exception(string.Format("MSR File '{0}' has more than one START line.", Path));
            }

            if (numberOfEnds > 1)
            {
                throw new Exception(string.Format("MSR File '{0}' has more than one END line.", Path));
            }

            Angles matrix = null;

            _points      = new List <PointData>();
            _headerLines = new List <string>();

            bool isStillInHeader = true;

            for (int lineIndex = 0; lineIndex <= lines.Count - 1; lineIndex++)
            {
                string line = null;
                line = lines[lineIndex].Trim();

                PointData point = null;

                if (line.StartsWith("G330"))
                {
                    // G330 line specifies the orientation of the machine for and following points

                    isStillInHeader = false;

                    //Read in the euler angles
                    Radian a = default(Radian);
                    Radian b = default(Radian);
                    Radian c = default(Radian);

                    string characters      = "";
                    char   lastControlChar = '~';
                    for (int charIndex = 4; charIndex <= line.Length - 1; charIndex++)
                    {
                        if (IsControlChar(line[charIndex]))
                        {
                            switch (lastControlChar)
                            {
                            case 'A':
                                if (anglesInDegrees)
                                {
                                    a = (Degree)Convert.ToDouble(characters);
                                }
                                else
                                {
                                    a = Convert.ToDouble(characters);
                                }
                                break;

                            case 'B':
                                if (anglesInDegrees)
                                {
                                    b = (Degree)Convert.ToDouble(characters);
                                }
                                else
                                {
                                    b = Convert.ToDouble(characters);
                                }
                                break;

                            case 'C':
                                if (anglesInDegrees)
                                {
                                    c = (Degree)Convert.ToDouble(characters);
                                }
                                else
                                {
                                    c = Convert.ToDouble(characters);
                                }
                                break;
                            }
                            lastControlChar = line[charIndex];
                            characters      = "";
                        }
                        else if (line[charIndex] != ' ')
                        {
                            characters += line[charIndex];
                        }
                    }
                    matrix = new Angles(a, b, c, Conventions.XYZ);
                }
                else if (line.StartsWith("G800"))
                {
                    // G800 line is the nominal point and is followed by the G801 line which is the measured point

                    isStillInHeader = false;

                    //Parse the characters in the line
                    point = new PointData();

                    string characters             = "";
                    char   lastControlChar        = '~';
                    var    controlCharactersFound = new List <string>();
                    for (int charIndex = 4; charIndex <= line.Length - 1; charIndex++)
                    {
                        if (IsControlChar(line[charIndex]))
                        {
                            if (controlCharactersFound.Contains(line[charIndex].ToString()))
                            {
                                throw new Exception(
                                          string.Format(
                                              "Control character '{0}' found multiple times for point number {1} in MSR File '{2}'",
                                              line[charIndex],
                                              _points.Count + 1,
                                              Path));
                            }
                            controlCharactersFound.Add(line[charIndex].ToString());
                            StoreValue800(ref point, lastControlChar, characters);
                            lastControlChar = line[charIndex];
                            characters      = "";
                        }
                        else if (line[charIndex] != ' ')
                        {
                            characters += line[charIndex];
                        }
                    }
                    StoreValue800(ref point, lastControlChar, characters);

                    line = lines[lineIndex + 1].Trim();
                    if (line.StartsWith("G801"))
                    {
                        characters             = "";
                        lastControlChar        = '~';
                        controlCharactersFound = new List <string>();
                        for (int intChar = 4; intChar <= line.Length - 1; intChar++)
                        {
                            if (IsControlChar(line[intChar]))
                            {
                                if (controlCharactersFound.Contains(line[intChar].ToString()))
                                {
                                    throw new Exception(
                                              string.Format(
                                                  "Control character '{0}' found multiple times for point number {1} in MSR File '{2}'",
                                                  line[intChar],
                                                  _points.Count + 1,
                                                  Path));
                                }
                                controlCharactersFound.Add(line[intChar].ToString());
                                StoreValue801(ref point, lastControlChar, characters);
                                lastControlChar = line[intChar];
                                characters      = "";
                            }
                            else if (line[intChar] != ' ')
                            {
                                characters += line[intChar];
                            }
                        }
                        StoreValue801(ref point, lastControlChar, characters);

                        //Store the point
                        if (matrix != null)
                        {
                            point.ApplyMatrix(matrix);
                        }
                        point.CalculateMeasuredSurfacePoint();
                        _points.Add(point);

                        lineIndex += 1;
                    }
                    else
                    {
                        // Throw an exception as all 800 lines must be followed by an 801 line
                        throw new Exception(string.Format("Point number {0} has no G801 line in MSR File '{1}'",
                                                          point.PointNumber,
                                                          Path));
                    }
                }
                else if (line.StartsWith("G801"))
                {
                    // We have lost a line here so throw an exception
                    throw new Exception(string.Format("Missing G800 line after reading {0} points in MSR File '{1}'",
                                                      _points.Count,
                                                      Path));
                }
                else if (isStillInHeader)
                {
                    _headerLines.Add(lines[lineIndex]);
                }
            }

            // Throw an exception if no points were found in the file
            if (_points.Count == 0)
            {
                throw new Exception(string.Format("No points found in MSR File '{0}'", Path));
            }
        }