-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvert.cs
172 lines (132 loc) · 5.98 KB
/
Convert.cs
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
namespace XYZtoSawSimTsConverter
{
class Convert
{
private Dictionary<Single, List<Tuple<Single, Single>>> _profileSet;
public Dictionary<Single, List<Tuple<Single, Single>>> ProfileSet
{
get { return _profileSet; }
}
//read the text file one line at a time
public void readXYZ(string pathToXYZ)
{
//sawSim is in thousandths of inch, so multiply xyz coordinates by 1000
//for each xyz z coordinate create another key value set for the dictionary with the single key being the z coordinate and all of the xy values fed into a list of tuples
string line;
_profileSet = new Dictionary<float, List<Tuple<float, float>>>();
System.IO.StreamReader xyzFile = new System.IO.StreamReader(pathToXYZ);
while ((line = xyzFile.ReadLine()) != null)
{
//split the line into x, y, and z components
string[] coordArray = splitLine(line);
//save all points at the same z coordinate to a dictionary with key = z-value and value = tuple of x,y values
Single zVal = Single.Parse(coordArray[2]);
//multiply by 1000
zVal = zVal * 1000;
//check if any profiles have been added to the dictionary yet
// if no coordinates have been added, then add the first coordinate
// if a coordinate has been added the revise the dictionary
if (_profileSet.ContainsKey(zVal))
{
_profileSet[zVal].Add(xyAtZ(coordArray));
}
else
{
//instantiate a new list of profile points and create a new z-value entry in the dictionary
List<Tuple<Single, Single>> newProfileSet = new List<Tuple<float, float>>();
newProfileSet.Add(xyAtZ(coordArray));
_profileSet.Add(zVal, newProfileSet);
}
}
}
/// <summary>
/// Splits a line from an xyz file into an array containing x, y, and z coordinates. [0] = x, [1] = y, [2] = z
/// </summary>
/// <param name="txtLine"></param>
/// <returns></returns>
string[] splitLine(string txtLine)
{
string[] pointArray = txtLine.Split(" ");
return pointArray;
}
/// <summary>
/// using an array of xyz coordinates, creates a tuple of xy coordinates and returns
/// </summary>
/// <param name="coordArray"></param>
/// <returns></returns>
Tuple<Single, Single> xyAtZ(string[] coordArray)
{
Single xVal = Single.Parse(coordArray[0]);
Single yVal = Single.Parse(coordArray[1]);
//multiply by 1000 for thousandths
xVal = xVal * 1000;
yVal = yVal * 1000;
//round to nearest integer
xVal = Single.Parse(Math.Round(xVal, 0).ToString());
yVal = Single.Parse(Math.Round(yVal, 0).ToString());
Tuple<Single, Single> xyPoints = new Tuple<float, float>(xVal, yVal);
return xyPoints;
}
/// <summary>
/// takes the dictionary of profiles from the xyz read operation (Key = Unique z value, Value = List of x,y coordinates in tuples) and writes to a SawSim format file
/// </summary>
/// <param name="pathToDestination"></param>
public void writeSawSimFile(string pathToDestination, int logNum, int countNum)
{
//create a list containing one string for each line in the SawSim file
List<String> SawSimLine = new List<string>();
//add the first line
SawSimLine.Add("StartLog " + logNum);
//create a length index
Single lastLength = 0;
foreach (KeyValuePair<Single, List<Tuple<Single, Single>>> profile in ProfileSet)
{
string zString = profile.Key.ToString();
//start a profile line using the current z value
string currentProfile = getSpacesforZWord(zString) + zString;
//for a given z value, concatenate all of the x, y coordinates on the end of the line
foreach(Tuple<Single, Single> coordinate in profile.Value)
{
string xyString = coordinate.Item1.ToString() + "," + coordinate.Item2.ToString();
//get the spaces for the xy word, and add the word to the current line string
currentProfile = currentProfile + getSpacesforXYWord(xyString) + xyString;
}
SawSimLine.Add(currentProfile);
lastLength = profile.Key;
}
lastLength = lastLength + 1000;
SawSimLine.Add("EndLog " + lastLength);
string pathToFile = string.Format(pathToDestination + "\\" + countNum + "_" + logNum + ".txt");
System.IO.File.WriteAllLines(pathToFile , SawSimLine.ToArray());
}
string getSpacesforXYWord(string word)
{
int numSpaces = 0;
int numChars = word.Length;
numSpaces = 16 - numChars;
string spaceString = "";
for (int i = 0; i < numSpaces; i++)
{
spaceString = spaceString + " ";
}
return spaceString;
}
string getSpacesforZWord(string word)
{
int numSpaces = 0;
int numChars = word.Length;
numSpaces = 6 - numChars;
string spaceString = "";
for(int i = 0; i < numSpaces; i++)
{
spaceString = spaceString + " ";
}
return spaceString;
}
}
}