Ok, here's the full extent of my code. It's designed to read in the coordinates from a txt file, whose values are separated by spaces, and take each individual string from a line read in from a stream to populate a string array. An array for each line will be used to populate a dictionary whose values I just want to spit out to the command line at this point.
However, things aren't going too great. If anyone can spot the errors, please be as brutal as necessary.
Cheers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using System.IO;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
///Notes:
///1. Ensure only actual values are in the file. No suffixes eg. "X = 787878.989" = Invalid
///2. Verify the specified separator is correct for the file to be read from
///3. We could add parameter for filetype later on
namespace Multi_block_insertion
{
public class MultiB
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
StreamReader coordsFile;
string path;
[CommandMethod("MultiB")]
public void multiB()
{
Form1 form1 = new Form1();
form1.Activate();
form1.ShowDialog();
StreamReader coordsFile;
}
public StreamReader getCoordsFile(string fileName) //Creates a stream for the selected coordinates file
{
using (FileStream coordsFileFs = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
using (StreamReader coordsFileSr = new StreamReader(coordsFileFs))
{
coordsFile = coordsFileSr;
return coordsFileSr;
}
}
}
public string altGetCoordsFile()
{
OpenFileDialog coordsFile = new OpenFileDialog("Select Coords File", null, "txt", "Coords Selection", OpenFileDialog.OpenFileDialogFlags.AllowAnyExtension);
if (coordsFile.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
throw new System.Exception("Error or User Cancelled");
}
path = coordsFile.Filename.ToString();
return path;
}
public Dictionary<int, string[]> createCoordsList(string separator) //This method separates the stream into coords/attributes for each block
{
Dictionary<int, string[]> coordsList = new Dictionary<int, string[]>();
int numOfLines = calculateLinesInStream(); //Assign the number of lines in the stream to this integer
StreamReader str = new StreamReader(File.Open(path, FileMode.Open));
for (int i = 1; i < numOfLines; i++)//Loop for splitting each line into individual components - coordinates, attributes etc
{
string thisLine = str.ReadLine();
int charactersThisLine = thisLine.Length;
Queue<char> thisLineQueue = new Queue<char>(thisLine.ToCharArray());//Convert to a queue for brevity of manipulation
int numOfCharOnThisLine = thisLineQueue.Count;//Count of the characters so we know when to quit
int numOfSeparatorsOnThisLine = 0;//Count of separators so we know number of values to add in the array for this line
foreach (Char thisChar in thisLineQueue)//Loop to count the separators
{
if (thisChar.ToString() == separator)
{
numOfSeparatorsOnThisLine++;
}
}
string[] thisLinesValues = new string[numOfSeparatorsOnThisLine + 1];//An array for each of the values on this line (coords, attributes)
bool endOfLine = false;
string thisValue = "";
int count = 0;
int thisLineValues = 0;
while (!endOfLine)//Keep looping until we reach the end of the line
{
for (int a = 0; a < thisLineQueue.Count(); a++) //Loop to assess and extract each char of the queue
{
if (count == charactersThisLine )//Check whether we have reached the end of the line
{
coordsList.Add(charactersThisLine, thisLinesValues); //End of line reached, so add all values for the this line to the Dictionary
endOfLine = true;//When we "break", the exit conditions for while(!endOfLine) will be met
break;//Effectively ending extrapolation of this line
}
if (thisLineQueue.Peek().ToString() == separator)//If the next element is a separator we need to add thisValue to our array and reset it
{
thisLineQueue.Dequeue();
thisLinesValues[thisLineValues] = thisValue;
thisLineValues++;
count++;
thisValue = "";
}
//We now know the next element is valid and needs to be added to our string
thisValue += thisLineQueue.Dequeue();//So that's what we do here - concurrently removing the value from the queue
count++;
}
}
}
return coordsList;
}
private int calculateLinesInStream()
{
int numOfLines = 0;
using (StreamReader tempStream = new StreamReader(File.Open(path, FileMode.Open))) //Create a new stream using our parameter to quantify number of lines
{
bool isError = false;
string tempString = "";//Populate this string with the current line of the stream
while (true)//This Loop is to determine only the number of lines in this stream
{
try
{
bool endLines = tempStream.EndOfStream;
if (endLines)
{
isError = true;
break;
}
tempString = tempStream.ReadLine();//Assigned the string for the current line of the stream
}
catch
{
throw new System.Exception("There was a problem");
}
if (tempString == null)
{
break;//The line was empty so we exit the loop
}
numOfLines++;//The line wasn't empty so we increment this integer
}
}
return numOfLines;
}
[CommandMethod("TestFile")]
public void testFile()
{
Dictionary<int, string[]> coordsList;
using (StreamReader sr = getCoordsFile(altGetCoordsFile()))
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage(sr.ToString());
coordsList = new Dictionary<int, string[]>(createCoordsList(" "));
}
foreach (KeyValuePair<int, string[]> pair in coordsList)
{
for (int i = 0; i < pair.Value.Count() - 1; i++)
{
ed.WriteMessage(pair.Value[i].ToString());
}
}
{
}
}
}
}