private static double ParseDouble(IgesFile file, string str, ref int index, double defaultValue = 0.0) { if (index < str.Length && (str[index] == file.FieldDelimiter || str[index] == file.RecordDelimiter)) { // swallow the delimiter and return the default index++; return(defaultValue); } SwallowWhitespace(str, ref index); var sb = new StringBuilder(); for (; index < str.Length; index++) { var c = str[index]; if (c == file.FieldDelimiter || c == file.RecordDelimiter) { index++; // swallow it break; } sb.Append(c); } var doubleString = sb.ToString(); if (string.IsNullOrWhiteSpace(doubleString)) { return(defaultValue); } else { return(IgesParser.ParseDoubleStrict(sb.ToString())); } }
internal static DateTime ParseDateTime(string value, DateTime defaultValue) { if (string.IsNullOrEmpty(value)) { return(DateTime.Now); } var match = dateTimeReg.Match(value); if (!match.Success) { throw new IgesException("Invalid date/time format"); } Debug.Assert(match.Groups.Count == 9); int year = IgesParser.ParseIntStrict(match.Groups[1].Value); int month = IgesParser.ParseIntStrict(match.Groups[4].Value); int day = IgesParser.ParseIntStrict(match.Groups[5].Value); int hour = IgesParser.ParseIntStrict(match.Groups[6].Value); int minute = IgesParser.ParseIntStrict(match.Groups[7].Value); int second = IgesParser.ParseIntStrict(match.Groups[8].Value); if (match.Groups[1].Value.Length == 2) { year += 1900; } return(new DateTime(year, month, day, hour, minute, second)); }
private static string ParseString(IgesFile file, string str, ref int index, string defaultValue = null) { if (index < str.Length && (str[index] == file.FieldDelimiter || str[index] == file.RecordDelimiter)) { // swallow the delimiter and return the default index++; return(defaultValue); } SwallowWhitespace(str, ref index); var sb = new StringBuilder(); // parse length for (; index < str.Length; index++) { var c = str[index]; if (c == IgesFile.StringSentinelCharacter) { index++; // swallow H break; } if (!char.IsDigit(c)) { throw new IgesException("Expected digit"); } sb.Append(c); } var lengthString = sb.ToString(); if (string.IsNullOrWhiteSpace(lengthString)) { return(defaultValue); } int length = IgesParser.ParseIntStrict(lengthString); sb.Clear(); // parse content var value = str.Substring(index, length); index += length; // verify delimiter and swallow if (index == str.Length - 1) { SwallowDelimiter(str, file.RecordDelimiter, ref index); } else { SwallowDelimiter(str, file.FieldDelimiter, ref index); } return(value); }
public static int IntegerOrDefault(List <string> values, int index, int defaultValue) { if (index < values.Count) { return(IgesParser.ParseIntOrDefault(values[index], defaultValue)); } return(defaultValue); }
public static double DoubleOrDefault(List <string> values, int index, double defaultValue) { if (index < values.Count) { return(IgesParser.ParseDoubleOrDefault(values[index], defaultValue)); } return(defaultValue); }
private static int ParseInt(IgesFile file, string str, ref int index, int defaultValue = 0) { if (index < str.Length && (str[index] == file.FieldDelimiter || str[index] == file.RecordDelimiter)) { // swallow the delimiter and return the default index++; return(defaultValue); } SwallowWhitespace(str, ref index); var sb = new StringBuilder(); for (; index < str.Length; index++) { var c = str[index]; if (c == file.FieldDelimiter || c == file.RecordDelimiter) { index++; // swallow it break; } if (!char.IsDigit(c)) { throw new IgesException("Expected digit"); } sb.Append(c); } var intString = sb.ToString(); if (string.IsNullOrWhiteSpace(intString)) { return(defaultValue); } else { return(IgesParser.ParseIntStrict(sb.ToString())); } }
public static IgesDirectoryData FromRawLines(string line1, string line2) { var dir = new IgesDirectoryData(); var entityTypeNumber = IgesParser.ParseIntStrict(GetField(line1, 1)); dir.EntityType = (IgesEntityType)entityTypeNumber; dir.ParameterPointer = IgesParser.ParseIntStrict(GetField(line1, 2)); dir.Structure = IgesParser.ParseIntStrict(GetField(line1, 3)); dir.LineFontPattern = IgesParser.ParseIntStrict(GetField(line1, 4)); dir.Level = IgesParser.ParseIntStrict(GetField(line1, 5)); dir.View = IgesParser.ParseIntStrict(GetField(line1, 6)); dir.TransformationMatrixPointer = IgesParser.ParseIntStrict(GetField(line1, 7)); dir.LableDisplay = IgesParser.ParseIntStrict(GetField(line1, 8)); dir.StatusNumber = GetField(line1, 9); dir.LineWeight = IgesParser.ParseIntStrict(GetField(line2, 2)); dir.Color = IgesParser.ParseIntStrict(GetField(line2, 3)); dir.LineCount = IgesParser.ParseIntStrict(GetField(line2, 4)); dir.FormNumber = IgesParser.ParseIntStrict(GetField(line2, 5)); dir.EntityLabel = GetField(line2, 8, null); dir.EntitySubscript = IgesParser.ParseUIntStrict(GetField(line2, 9)); return(dir); }
public static IgesFile Load(Stream stream) { var file = new IgesFile(); var allLines = new StreamReader(stream).ReadToEnd().Split("\n".ToCharArray()).Select(s => s.TrimEnd()).Where(line => !string.IsNullOrEmpty(line)); string terminateLine = null; var startLines = new List <string>(); var globalLines = new List <string>(); var directoryLines = new List <string>(); var parameterLines = new List <string>(); var sectionLines = new Dictionary <IgesSectionType, List <string> >() { { IgesSectionType.Start, startLines }, { IgesSectionType.Global, globalLines }, { IgesSectionType.Directory, directoryLines }, { IgesSectionType.Parameter, parameterLines } }; foreach (var line in allLines) { if (line.Length != 80) { throw new IgesException("Expected line length of 80 characters."); } var data = line.Substring(0, IgesFile.MaxDataLength); var sectionType = SectionTypeFromCharacter(line[IgesFile.MaxDataLength]); var lineNumber = IgesParser.ParseIntStrict(line.Substring(IgesFile.MaxDataLength + 1).TrimStart()); if (sectionType == IgesSectionType.Terminate) { if (terminateLine != null) { throw new IgesException("Unexpected duplicate terminate line"); } terminateLine = data; // verify terminate data and quit var startCount = IgesParser.ParseIntStrict(terminateLine.Substring(1, 7)); var globalCount = IgesParser.ParseIntStrict(terminateLine.Substring(9, 7)); var directoryCount = IgesParser.ParseIntStrict(terminateLine.Substring(17, 7)); var parameterCount = IgesParser.ParseIntStrict(terminateLine.Substring(25, 7)); if (startLines.Count != startCount) { throw new IgesException("Incorrect number of start lines reported"); } if (globalLines.Count != globalCount) { throw new IgesException("Incorrect number of global lines reported"); } if (directoryLines.Count != directoryCount) { throw new IgesException("Incorrect number of directory lines reported"); } if (parameterLines.Count != parameterCount) { throw new IgesException("Incorrect number of parameter lines reported"); } break; } else { if (sectionType == IgesSectionType.Parameter) { data = data.Substring(0, data.Length - 8); // parameter data doesn't need its last 8 bytes } sectionLines[sectionType].Add(data); if (sectionLines[sectionType].Count != lineNumber) { throw new IgesException("Unordered line number"); } } } // don't worry if terminate line isn't present ParseGlobalLines(file, globalLines); var directoryEntries = ParseDirectoryLines(directoryLines); var parameterMap = PrepareParameterLines(parameterLines, directoryEntries, file.FieldDelimiter, file.RecordDelimiter); PopulateEntities(file, directoryEntries, parameterMap); return(file); }
private static Dictionary <int, Tuple <List <string>, string> > PrepareParameterLines(List <string> parameterLines, IEnumerable <IgesDirectoryData> directoryEntires, char fieldDelimiter, char recordDelimiter) { var map = new Dictionary <int, Tuple <List <string>, string> >(); var fields = new List <string>(); var state = ParameterParseState.ParsingEntityNumber; var current = new StringBuilder(); int entityNumber = 0; int stringLength = 0; foreach (var dir in directoryEntires) { var lowerBound = dir.ParameterPointer - 1; var upperBound = lowerBound + dir.LineCount; for (int i = lowerBound; i < upperBound; i++) { var line = parameterLines[i]; // for each character for (int j = 0; j < line.Length; j++) { var ch = line[j]; if (state == ParameterParseState.ParsingString) { if (current.Length == stringLength) { fields.Add(current.ToString()); current.Clear(); if (ch == fieldDelimiter) { state = ParameterParseState.ParsingNumber; } else if (ch == recordDelimiter) { state = ParameterParseState.ParsingComment; } else { Debug.Assert(false, "expected field or record delimiter"); } } else { current.Append(ch); } } else if (state == ParameterParseState.ParsingComment) { current.Append(ch); } else { if (ch == fieldDelimiter) { if (state == ParameterParseState.ParsingEntityNumber) { entityNumber = IgesParser.ParseIntStrict(current.ToString()); } else { fields.Add(current.ToString()); } state = ParameterParseState.ParsingNumber; current.Clear(); } else if (ch == recordDelimiter) { if (state == ParameterParseState.ParsingEntityNumber) { entityNumber = IgesParser.ParseIntStrict(current.ToString()); } else { fields.Add(current.ToString()); } state = ParameterParseState.ParsingComment; current.Clear(); } else if (ch == IgesFile.StringSentinelCharacter) { stringLength = IgesParser.ParseIntStrict(current.ToString()); current.Clear(); state = ParameterParseState.ParsingString; } else { current.Append(ch); } } } } var comment = current.ToString().Trim(); // clean up escaped comemnt values comment = comment.Replace("\\\\", "\\"); comment = comment.Replace("\\n", "\n"); comment = comment.Replace("\\r", "\r"); comment = comment.Replace("\\t", "\t"); comment = comment.Replace("\\v", "\v"); comment = comment.Replace("\\f", "\f"); current.Clear(); map[dir.ParameterPointer] = Tuple.Create(fields, string.IsNullOrEmpty(comment) ? null : comment); fields = new List <string>(); state = ParameterParseState.ParsingEntityNumber; } return(map); }