-
Notifications
You must be signed in to change notification settings - Fork 0
/
STLLoader.cs
130 lines (119 loc) · 4 KB
/
STLLoader.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.IO;
using OpenTK;
namespace HalfEdgeConverter
{
/// <summary>
/// STLのローダ
/// </summary>
public class STLLoader
{
/// <summary>
/// ファイルパス
/// </summary>
string FilePath;
/// <summary>
/// 頂点情報
/// </summary>
List<Vector3> vertex;
/// <summary>
/// 頂点情報
/// </summary>
public ReadOnlyCollection<Vector3> Vertex
{
get
{
return vertex.AsReadOnly();
}
}
/// <summary>
/// Constructor.
/// </summary>
public STLLoader(string filePath)
{
FilePath = filePath;
}
/// <summary>
/// 読み込み処理
/// </summary>
/// <returns>Success</returns>
public bool Load()
{
if (Path.GetExtension(FilePath).ToLower() != ".stl")
{
Console.WriteLine("not stl format");
return false;
}
try
{
vertex = new List<Vector3>();
String[] parser = File.ReadAllLines(FilePath, System.Text.Encoding.GetEncoding("Shift_JIS"));
ReadData(parser);
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
}
/// <summary>
/// STLデータの読み込み
/// </summary>
private void ReadData(String[] parser)
{
int lineCounter = 0;
try
{
String[] line;
Vector3 pos;
while (parser.Length != lineCounter)
{
line = parser[lineCounter].Split(' ');
line = line.Where(p => !(String.IsNullOrWhiteSpace(p) || String.IsNullOrEmpty(p))).ToArray();
for (int i = 0; i < line.Length; i++)
{
if (line[i] == "outer" && line[i + 1] == "loop") break;
if (line[i] == "solid" || line[i] == "endloop" || line[i] == "endfacet") break;
if (line[i] == "facet" && line[i + 1] == "normal") break; // 法線は自前で計算
if (line[i] == "vertex")
{
pos = new Vector3(float.Parse(line[i + 1]), float.Parse(line[i + 2]), float.Parse(line[i + 3]));
vertex.Add(pos);
}
}
lineCounter++;
}
}
catch (Exception)
{
throw new FileLoadException(lineCounter + "行目でエラー");
}
}
/// <summary>
/// STLデータの書き込み
/// </summary>
/// <param name="position"></param>
/// <param name="normal"></param>
public void Write(List<Vector3> position, List<Vector3> normal)
{
StreamWriter write = new StreamWriter("testfile.stl");
write.WriteLine("solid stl");
for (int i = 0; i < position.Count; i += 3)
{
write.WriteLine("facet normal " + normal[i].X + " " + normal[i].Y + " " + normal[i].Z);
write.WriteLine("outer loop");
write.WriteLine("vertex " + position[i].X + " " + position[i].Y + " " + position[i].Z);
write.WriteLine("vertex " + position[i + 1].X + " " + position[i + 1].Y + " " + position[i + 1].Z);
write.WriteLine("vertex " + position[i + 2].X + " " + position[i + 2].Y + " " + position[i + 2].Z);
write.WriteLine("endloop");
write.WriteLine("endfacet");
}
write.WriteLine("endsolid vcg");
write.Close();
}
}
}