Hello Everyone!
I'm a bit stuck with something here and I hope you can help me out guys! My problem is that I have to modify only some specific details in a txt file (actually it is a wrl file but it can be considered as a txt file). There is a lot of text there but I just need to modify the point coordinates and leave the rest of the text as it is. Reading the "sample file" at some stage we will see some blocks that look like the below:
Blah blah blah...
I'm a bit stuck with something here and I hope you can help me out guys! My problem is that I have to modify only some specific details in a txt file (actually it is a wrl file but it can be considered as a txt file). There is a lot of text there but I just need to modify the point coordinates and leave the rest of the text as it is. Reading the "sample file" at some stage we will see some blocks that look like the below:
Blah blah blah...
coord Coordinate {
point [
# bottom
-1.0 -1.0 1.0, #vertex 0
1.0 -1.0 1.0, #vertex 1
1.0 -1.0 -1.0, #vertex 2
-1.0 -1.0 -1.0, #vertex 3
# top
0.0 1.0 0.0 #vertex 4
]
}
point [
# bottom
-1.0 -1.0 1.0, #vertex 0
1.0 -1.0 1.0, #vertex 1
1.0 -1.0 -1.0, #vertex 2
-1.0 -1.0 -1.0, #vertex 3
# top
0.0 1.0 0.0 #vertex 4
]
}
blah, blah, blah...
where the numbers are (X,Y,Z) coordinates. What I have to do is to change those figures for (X,0,sqrt(X^2+Z^2)) points. Therefore the expected result (test file) will be as follows:
blah, blah, blah...
where the numbers are (X,Y,Z) coordinates. What I have to do is to change those figures for (X,0,sqrt(X^2+Z^2)) points. Therefore the expected result (test file) will be as follows:
blah, blah, blah...
coord Coordinate {
point [
# bottom
-1.0 0.0 1.4142, #vertex 0
1.0 0.0 1.4142, #vertex 1
1.0 0.0 1.4142, #vertex 2
-1.0 0.0 1.4142, #vertex 3
# top
0.0 0.0 0.0 #vertex 4
]
}
point [
# bottom
-1.0 0.0 1.4142, #vertex 0
1.0 0.0 1.4142, #vertex 1
1.0 0.0 1.4142, #vertex 2
-1.0 0.0 1.4142, #vertex 3
# top
0.0 0.0 0.0 #vertex 4
]
}
blah, blah, blah...
So in my opinion it can be good to have two files: sample.txt and test.txt where the sample file will never be modified whereas the test.txt will be the same as sample.txt but including the modifications as explained above. The idea can be to read the sample.txt, store the info and then start copying each line in the test file until we get to the points coordinates (which needs to be modified) and continuying with this process until the next set of coordinates.
The problem is that I do not know how to change x,y,z coordinates from the sample.txt to (X,0,sqrt(X^2+Z^2)) and to copy them in the test.txt. Please see some code below.
If you could give me a hand with this I would be extremely greatful!
Many thanks.
So in my opinion it can be good to have two files: sample.txt and test.txt where the sample file will never be modified whereas the test.txt will be the same as sample.txt but including the modifications as explained above. The idea can be to read the sample.txt, store the info and then start copying each line in the test file until we get to the points coordinates (which needs to be modified) and continuying with this process until the next set of coordinates.
The problem is that I do not know how to change x,y,z coordinates from the sample.txt to (X,0,sqrt(X^2+Z^2)) and to copy them in the test.txt. Please see some code below.
If you could give me a hand with this I would be extremely greatful!
Many thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | //Read a Text File///////////////////////////////////////////////////////////////////////////////////////////////////using System;using System.IO;namespace readwriteapp{ class Class1 { [STAThread] static void Main(string[] args) { String line; try { //Pass the file path and file name to the StreamReader constructor StreamReader sr = new StreamReader("C:\\Sample.txt"); //Read the first line of text line = sr.ReadLine(); //Continue to read until you reach end of file while (line != null) { //write the lie to console window Console.WriteLine(line); //Read the next line line = sr.ReadLine(); } //close the file sr.Close(); Console.ReadLine(); } catch(Exception e) { Console.WriteLine("Exception: " + e.Message); } finally { Console.WriteLine("Executing finally block."); } } }}//Write a text file////////////////////////////////////////////////////////////////////////////////////////////////////using System;using System.IO;using System.Text;namespace readwriteapp{ class Class1 { [STAThread] static void Main(string[] args) { Int64 x; try { //Open the File StreamWriter sw = new StreamWriter("C:\\Test.txt", true, Encoding.ASCII); //Writeout all the lines changing the coordinates.///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// HOW CAN I DO THIS???///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //close the file sw.Close(); } catch(Exception e) { Console.WriteLine("Exception: " + e.Message); } finally { Console.WriteLine("Executing finally block."); } } }} |

HappyCode HappyCodePosted May 12, 2014, 6:57 AM
VulpesPosted May 8, 2014, 8:27 PM
HappyCode HappyCodePosted May 8, 2014, 9:31 AM
I have been developing the previous code a bit more and now I have gotten stuck again with something else.
The thing is that I have to run a test to say if a set of coordinates go clockwise or counter-clockwise.
Let's say that the coordinates are generally as follows:
POINT 0 = (Ax, Az)
POINT 1 = (Bx, Bz)
POINT 3 = (Cx, Cz)
Then I have to do:
if(bx - ax )(cz - az ) - (bz - az )(cx - ax ) > 0 then POINT 0-POINT 1-POINT 2 are counterclockwise.Return set of points
if(bx - ax )(cz - az ) - (bz - az )(cx - ax ) < 0 then POINT 0-POINT 1-POINT 2 are clockwise.Return set of points
if(bx - ax )(cz - az ) - (bz - az )(cx - ax ) < 0 then POINT 0-POINT 1-POINT 2 are coolinear.Return set of points
The text file is the same as discussed before but adding the "coordindex" field. See below:
Blah blah
point
[
0 0.0 128.588459085565, #POINT 0 -<(this # indiquates a comment, #POINT1 etc doesn't
25 0.0 134.979628462965, #POINT 1 appear in the txt file.The "," will appear
0 0.0 134.979628462965, #POINT 3 though. I only want the coordinates x and z
25 0.0 128.588459085565, (not the middle one))
25 0.0 140.100717207301,
0 0.0 140.100717207301,
25 0.0 143.87494372892,
0 0.0 143.87494372892,
25 0.0 146.245863353464,
0 0.0 146.245863353464,
25 0.0 147.181161545899,
0 0.0 147.181161545899,
25 0.0 146.668001779529,
0 0.0 146.668001779529,
25 0.0 144.711312619297,
0 0.0 144.711312619297,
25 0.0 141.340784276868,
0 0.0 141.340784276868,
25 0.0 136.604499944182,
0 0.0 136.604499944182,
25 0.0 130.573315654463,
0 0.0 130.573315654463,
25 0.0 123.341482952817,
0 0.0 123.341482952817,
25 0.0 115.031289382498,
0 0.0 115.031289382498,
25 0.0 105.793445439687,
0 0.0 105.793445439687,
]
blah blah
coordIndex [
0,1,2,-1, # Those index define how the points are connected. For example
1,4,2,-1, # POINT 0 -> POINT 1 -> POINT 2.The -1 on the right is not
2,4,5,-1, # important and it will not be used.
4,6,5,-1,
5,6,7,-1,
6,8,7,-1,
7,8,9,-1,
8,10,9,-1,
9,10,11,-1,
10,12,11,-1,
11,12,13,-1,
12,14,13,-1,
13,14,15,-1,
14,16,15,-1,
15,16,17,-1,
16,18,17,-1,
17,18,19,-1,
18,20,19,-1,
19,20,21,-1,
20,22,21,-1,
21,22,23,-1,
22,24,23,-1,
23,24,25,-1,
24,26,25,-1,
25,26,27,-1,
0,3,1,-1,
]
blah blah
So, my concern is: Is there a way to fit this test in the previous code?
I am completely lost about how to do this... if you could help me out that would be much appreciated.
Many many thanks!!!
HappyCode HappyCodePosted May 2, 2014, 9:31 AM
VulpesPosted May 2, 2014, 9:02 AM
HappyCode HappyCodePosted May 2, 2014, 6:35 AM
Many thanks for your answer Vulpes! Back from my trip I have given this a go and I have still a little problem regarding how to jump to specific lines of code. Please note that everything that contains # in the text
file is a comment and we don't mind loosing it. Also it is not important to keep the "tab spaces" (the text can be justified to the right). For this reason I have created an intermediate file with the text justified to the left to work
with it and with no comments to make it simpler. I forgot to say that sometimes we also have blank lines between the numbers will have ). So now we can start from there. Now the data I need to work with looks like that (as in interfile.txt):
[/code]
Blah blah blah...
point
[
//Sometimes you have an empty lines here and sometimes you don't.
-1.0
-1.0 1.0,
1.0 -1.0 1.0,
1.0 -1.0 -1.0,
//Same thing, sometimes we
can have empty line in between the lines containing the coordinates.
-1.0
-1.0 -1.0,
0.0 1.0 0.0
]
blah, blah, blah...
[/code]
I have tried the following:
[/code]
using (var
sourceFile = new StreamReader(interFile))
using (var destinationFile = new
StreamWriter(outputFile))
{
/////////////////////////////////////////////////////////////////////////////
HOW
CAN I WRITE THE OUTPUT FILE WITHOUT ANY BLANK LINE??? WE CAN LEAVE
THEM
/////////////////////////////////////////////////////////////////////////////
while
(!sourceFile.EndOfStream)
{
string line;
line =
sourceFile.ReadLine();
if (line.StartsWith("point"))
{
/////////////////////////////////////////////////////////////////////
how
can I skip the next two
lines??
/////////////////////////////////////////////////////////////////////
//This
will do the operations (I have tried this and works well)
var parts1 =
line.Split(',');
var parts2 = parts1[0].Split(new[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
// Transform points by doing
x,0,sqrt(y2+z2)
var x = Convert.ToDouble(parts2[0]);
var y =
Convert.ToDouble(parts2[1]);
var z = Convert.ToDouble(parts2[2]);
line =
string.Format("{0} 0.0 {1}, {2}", x, Math.Sqrt(Math.Pow(y, 2) + Math.Pow(z, 2)),
parts1[1]);
destinationFile.WriteLine(line);
/////////////////////////////////////////////////////////////////////////////////////////////////////
WHEN
WE FINISH WITH THE BLOCK OF NUMBERS WHE SHOULD STOP DOING THE
OPERATION
///////////////////////////////////////////////////////////////////////////////////////////////////////
}
else
{
destinationFile.WriteLine(line);
// This will write all the remaining lines
}
[/code]
So that is the story!
Any help would be more than welcome!!!
Many thanks!
VulpesPosted Apr 22, 2014, 1:31 PM
I'v assumed that in the #vertex 4 line, there should be a comma after the 3 points just as there is in the other #vertex lines. If there isn't then, the program will need to be changed: