-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathstlAsciiReader.cpp
executable file
·129 lines (89 loc) · 2.8 KB
/
stlAsciiReader.cpp
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
// Copyright(c) 2005-2006. All Rights Reserved
// By Jean-René Bédard (https://github.com/jrbedard/3d-converter)
#include "StdAfx.h"
#include "stlAsciiReader.h"
using namespace boost::spirit;
CStlAsciiReader::CStlAsciiReader(CFile* pFile, const fs::path& fileName)
{
CStlFile* pStlFile = static_cast<CStlFile*>(pFile);
m_pStlFile = pStlFile;
m_fileName = fileName;
}
bool CStlAsciiReader::Read()
{
OBJ_ASSERT(m_pStlFile);
if(!m_pStlFile)
return false;
std::string fileName(m_fileName.string()); // Extract native file path string
const int maxline = 1000;
char line[maxline];
MSG_INFO("Reading Ascii .STL file '" << fileName << "'.");
m_ifs.open(fileName.c_str());
if( !m_ifs.is_open() )
{
MSG_ERROR("Couldn't open Ascii .STL file '" << fileName << "'");
return true; // There is not stl file
}
int lineNum = 0;
while( m_ifs )
{
++lineNum;
m_ifs.getline( line, maxline ); // get the curent line pointer
std::string command;
parse_info<char const*> info = parse( line, *space_p >> +alpha_p[ append(command) ] );
if( !info.hit ) // if parsing was'nt successful
continue;
const char* curPos = info.stop;
//MSG_DEBUG("'" << command << "'");
// Object Solid
if( command == "solid" ) // new object declaration
{
std::string name;
parse_info<char const*> info = parse( curPos,
*graph_p[ append(name) ], space_p );
if( !info.full )
{
MSG_WARNING("Empty object name, " << fileName << ", line: " << lineNum);
continue;
}
// Create the new material
CStlFile::CSolid solid(name);
m_pStlFile->GetSolidVector().push_back(solid);
}
// Face and Normal
if( command == "facet" ) // Normal declaration
{
Vector3D normal(3);
parse_info<char const*> info = parse( curPos, "normal " >>
real_p[assign(normal[0])] >> real_p[assign(normal[1])] >> real_p[assign(normal[2])],
space_p );
if( !info.full )
{
MSG_WARNING("Invalid normal, file: '" << fileName << "', line: " << lineNum);
continue;
}
CStlFile::CSolid::CFacet newFacet;
m_pStlFile->GetCurrentSolid().GetFacetVector().push_back(newFacet);
m_pStlFile->GetCurrentSolid().GetCurrentFacet().m_normal = normal;
}
// Vertex
if( command == "vertex" ) // Vertex declaration
{
Vector3D vertex(3);
parse_info<char const*> info = parse( curPos,
real_p[assign(vertex[0])] >> real_p[assign(vertex[1])] >> real_p[assign(vertex[2])],
space_p );
if( !info.full )
{
MSG_WARNING("Invalid vertex, file: '" << fileName << "', line: " << lineNum);
continue;
}
m_pStlFile->GetCurrentSolid().GetCurrentFacet().m_vertices.push_back( vertex );
}
}
return true;
}
bool CStlAsciiReader::ReadDebug()
{
return true;
}