public DTMObject removeObject() { DTMObject elem = _DTMList[_DTMList.Count - 1]; if ( !_DTMList.Remove( elem ) ) Console.WriteLine( "Cannot remove DTMObject from List!" ); return elem; }
public bool readDTMFile( string path ) { if ( !File.Exists( path ) ) { onFileNotFound( path ); return false; } Stopwatch watch = new Stopwatch(); watch.Start(); Console.WriteLine( "Start reading DTM file..." ); string STRFileName = Path.GetDirectoryName(path); int lineNr = 0; DTMObject dtm = new DTMObject(); dtm.objects = new Dictionary<int, int[]>(); string[] lines = File.ReadAllLines( path ); foreach ( string line in lines ) { lineNr++; // ignore second and third line if ( lineNr == 2 ) continue; string[] parts = line.Split( ',' ); if ( lineNr == 3 ) { dtm.objectNumber = Convert.ToInt32( parts[1] ); continue; } // first line in file -> read String filename! if ( lineNr == 1 ) { STRFileName = Path.Combine(STRFileName, line.Split( ',' )[0]); // string-file is not exisiting? -> Exception if ( !File.Exists( STRFileName ) ) { onFileNotFound( STRFileName ); return false; } continue; } if ( parts.Length > 0 ) { switch ( parts[0] ) { case "TRISOLATION": // New trisolation information dtm.configLine = line.TrimEnd(); // if more than one trisolation per object -> insert and create a new object if ( dtm.objects.Count > 0 ) { addObject( dtm ); DTMObject dtm2 = new DTMObject(); dtm2.objects = new Dictionary<int, int[]>(); dtm2.objectNumber = dtm.objectNumber; dtm = dtm2; } break; case "END": case "OBJECT": // save object to List, first occurance in line 3 is skipped before! addObject( dtm ); if ( parts[0] == "OBJECT" ) { dtm = new DTMObject(); dtm.objects = new Dictionary<int, int[]>(); dtm.objectNumber = Convert.ToInt32( parts[1] ); } break; default: // add triangle to the list int[] numbers; // dtm with neighbouring information ? numbers[6] : numbers[3] try { if ( parts.Length > 6 ) numbers = new int[] { Convert.ToInt32( parts[1] ), Convert.ToInt32( parts[2] ), Convert.ToInt32( parts[3] ), Convert.ToInt32( parts[4] ), Convert.ToInt32( parts[5] ), Convert.ToInt32( parts[6] ) }; else numbers = new int[] { Convert.ToInt32( parts[1] ), Convert.ToInt32( parts[2] ), Convert.ToInt32( parts[3] ) }; } catch ( Exception ex ) { Console.WriteLine( "An exception occured!" ); Console.WriteLine( ex.Message ); foreach ( string key in ex.Data.Keys ) Console.WriteLine( key + " - " + ex.Data[key] ); Console.WriteLine( "Conversation line: \"" + line + "\"" ); return false; } // Console.WriteLine( "Numbers: " + numbers?.Length.ToString() ); // Console.WriteLine( "Parts: " + parts?.Length.ToString() ); try { dtm.objects.Add( Convert.ToInt32( parts[0] ), numbers ); } catch ( Exception ex ) { Console.WriteLine( "An exception occured!" ); Console.WriteLine( ex.Message ); foreach ( string key in ex.Data.Keys ) Console.WriteLine( key + " - " + ex.Data[key] ); Console.WriteLine( "Conversation line: \"" + line + "\"" ); return false; } break; } } } watch.Stop(); Console.WriteLine( "Finished readind DTM file.\nFound {0} new Objects in the DTM file!\n", _DTMList.Count ); Console.WriteLine( "Time: {0} ms", watch.ElapsedMilliseconds ); Console.WriteLine( "Start reading string file..." ); watch.Restart(); lineNr = 0; lines = File.ReadAllLines( STRFileName ); foreach ( string line in lines ) { lineNr++; // ignore first 2 lines if ( lineNr < 3 ) continue; string[] parts = line.Split( ',' ); if ( parts.Length < 4 ) { Console.WriteLine( "To Less parts! Ignoring..." ); Console.WriteLine( "Line: " + line ); continue; } if ( parts[0] == "0" ) continue; try { if ( _StringDict.ContainsKey( lineNr - 2 ) ) { KeyExistingException ex = new KeyExistingException( "Key " + ( lineNr + 2 ) + " is existing in dictionary!" ); ex.Data.Add( "Time", DateTime.Now ); ex.Data.Add( "Key", lineNr + 2 ); ex.Data.Add( "Existing Object", _StringDict[lineNr + 2] ); ex.Data.Add( "Current Line", line ); throw ex; } StringObject strObj = new StringObject(); // ATTENTION: SURPAC first stores Y-Coordinate, after that X-Coordinate strObj.Y = ( float ) Convert.ToDouble( parts[1] ); strObj.X = ( float ) Convert.ToDouble( parts[2] ); strObj.Z = ( float ) Convert.ToDouble( parts[3] ); strObj.ID = Convert.ToInt32( parts[0] ); _StringDict[lineNr - 2] = strObj; } catch ( KeyExistingException ex ) { Console.WriteLine( ex.Message ); foreach ( string key in ex.Data.Keys ) Console.WriteLine( key + " - " + ex.Data[key] ); Console.WriteLine( "Skipping this line...\n" ); continue; } catch ( Exception ex ) { Console.WriteLine( "An exception occured!" ); Console.WriteLine( ex.Message ); foreach ( string key in ex.Data.Keys ) Console.WriteLine( key + " - " + ex.Data[key] ); Console.WriteLine( "Conversation line: \"" + line + "\"" ); return false; } } // foreach ( int index in _StringDict.Keys ) { // Console.WriteLine( _StringDict[index].ID + " - " + _StringDict[index].X + " - " + _StringDict[index].Y + " - " + _StringDict[index].Z ); // } watch.Stop(); Console.WriteLine( "Finished reading string file.\nFound {0} vertices in the string file!\n", _StringDict.Count ); Console.WriteLine( "Time: {0} ms", watch.ElapsedMilliseconds ); return true; }
public void addObject( DTMObject obj ) { _DTMList.Add( obj ); }
public bool readSKUAFile( string path ) { if ( !File.Exists( path ) ) { onFileNotFound( path ); return false; } Console.WriteLine( "Start reading SKUA-GOCAD surface file..." ); DTMObject dtm = new DTMObject(); dtm.objects = new Dictionary<int, int[]>(); int OIDField = 0, properties = 0, lastVertexID = 0, existingVerts = 0, triangleNr = 0; bool first = true; List<int> idList = new List<int>(); string[] lines = File.ReadAllLines( path ); foreach ( string line in lines ) { //Console.Write( line + " ... " ); // line == "END" ? save and new DTM; reset also OIDField : continue; string trimed = line.TrimEnd(); if ( trimed == "END" ) { //Console.WriteLine( "END" ); if ( OIDField != 0 ) { int most = idList.GroupBy( i => i ).OrderByDescending( grp => grp.Count() ).Select( grp => grp.Key ).First(); dtm.objectNumber = most; //Console.WriteLine( "Setting objectNr to {0}", most ); } else { dtm.objectNumber = 32000; //Console.WriteLine( "OID not available. Setting objectNr to 32000" ); } addObject( dtm ); dtm = new DTMObject(); dtm.objects = new Dictionary<int, int[]>(); OIDField = 0; properties = 0; first = true; existingVerts += lastVertexID; lastVertexID = 0; idList.Clear(); continue; } // line == "TFACE" ? save and new DTM; DON'T reset also OIDField : continue; // skip for first TFACE if ( trimed == "TFACE" ) { //Console.WriteLine( "TFACE" ); if ( first ) { //Console.WriteLine( "... First occurance..." ); first = false; continue; } if ( OIDField != 0 ) { int most = idList.GroupBy( i => i ).OrderByDescending( grp => grp.Count() ).Select( grp => grp.Key ).First(); dtm.objectNumber = most; //Console.WriteLine( "Setting objectNr to {0}", most ); } else { dtm.objectNumber = 32000; //Console.WriteLine( "OID not available. Setting objectNr to 32000" ); } addObject( dtm ); dtm = new DTMObject(); dtm.objects = new Dictionary<int, int[]>(); idList.Clear(); continue; } string[] parts = line.Split( ' ' ); if ( parts.Length < 2 ) { //Console.WriteLine( "< 2" ); continue; } bool failure = false; StringObject strObj; try { switch ( parts[0] ) { case ( "PROPERTIES" ): //Console.WriteLine( "PROPERTIES" ); properties++; if ( parts[1] == "OID" ) OIDField = properties; break; case ( "VRTX" ): //Console.WriteLine( "VRTX" ); if ( parts.Length < 5 ) { failure = true; continue; } strObj = new StringObject(); strObj.X = ( float ) Convert.ToDouble( parts[2] ); strObj.Y = ( float ) Convert.ToDouble( parts[3] ); strObj.Z = ( float ) Convert.ToDouble( parts[4] ); lastVertexID = Convert.ToInt32( parts[1] ); ; strObj.ID = 32000; idList.Add( strObj.ID ); _StringDict.Add( lastVertexID + existingVerts, strObj ); break; case ( "PVRTX" ): //Console.WriteLine( "PVRTX" ); if ( parts.Length < ( 5 + OIDField ) ) { failure = true; break; } strObj = new StringObject(); strObj.X = ( float ) Convert.ToDouble( parts[2] ); strObj.Y = ( float ) Convert.ToDouble( parts[3] ); strObj.Z = ( float ) Convert.ToDouble( parts[4] ); lastVertexID = Convert.ToInt32( parts[1] ); strObj.ID = ( OIDField != 0 ) ? Convert.ToInt32( parts[4 + OIDField] ) : 32000; idList.Add( strObj.ID ); _StringDict.Add( lastVertexID + existingVerts, strObj ); break; case ( "TRGL" ): //Console.WriteLine( "TRGL" ); if ( parts.Length < 4 ) { failure = true; break; } triangleNr++; int[] points = { Convert.ToInt32( parts[1] ) + existingVerts, Convert.ToInt32( parts[2] ) + existingVerts, Convert.ToInt32( parts[3] ) + existingVerts }; dtm.objects.Add( triangleNr, points ); break; default: //Console.WriteLine( "default" ); break; } } catch ( IndexOutOfRangeException ex ) { Console.WriteLine( "Index out of range Exception occured!" ); Console.WriteLine( ex.Message ); foreach ( string key in ex.Data.Keys ) Console.WriteLine( key + " - " + ex.Data[key] ); Console.WriteLine( "Conversation line: \"" + line + "\"" ); return false; } catch ( Exception ex ) { Console.WriteLine( "An exception occured!" ); Console.WriteLine( ex.Message ); foreach ( string key in ex.Data.Keys ) Console.WriteLine( key + " - " + ex.Data[key] ); Console.WriteLine( "Conversation line: \"" + line + "\"" ); return false; } if ( failure ) { Console.WriteLine( "WARNING: Cannot read line:" ); Console.WriteLine( line ); continue; } } return true; }