-
Notifications
You must be signed in to change notification settings - Fork 0
/
gCodeFile.cpp
122 lines (111 loc) · 2.24 KB
/
gCodeFile.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
#include "gCodeFile.hh"
#include <fstream>
#include <iostream>
#include <string>
gCodeFile::gCodeFile( const string& fileName):
fileName(fileName),
absolute( true)
{
commands = new vector<gCommand*>();
}
gCodeFile::~gCodeFile()
{
gCommand* com;
while( !commands->empty())
{
com = commands->back();
commands->pop_back();
delete com;
}
delete commands;
}
int gCodeFile::readFile()
{
ifstream* is = new ifstream( this->fileName);
char c;
int counter = 0;
unsigned int N;
char lastChar = 0;
string number = "";
bool inParan = false;
while( is->get(c))
{
if( c == '(') inParan = true;
else if( c == ')') inParan = false;
if( c == 32 || c == 10 || inParan) continue;
else if( c > 64 && c < 91)
{
if( lastChar != 0) this->evalLast( lastChar, N, number);
number = "";
lastChar = c;
}
else if( (c > 47 && c < 58) || c == '.' || c == '-')
{
number += c;
}
}
evalLast( lastChar, N, number);
return counter;
}
int gCodeFile::evalLast( const char c, unsigned int& N, const string number)
{
switch( c)
{
case 'N':
N = stoi( number);
break;
case 'G':
this->addNewCommand( stoi( number), N);
break;
case 'X':
this->commands->back()->setX( stod(number));
break;
case 'Y':
this->commands->back()->setY( stod(number));
break;
case 'Z':
this->commands->back()->setZ( stod(number));
break;
case 'I':
this->commands->back()->setI( stod(number));
break;
case 'J':
this->commands->back()->setJ( stod(number));
break;
default:
cout << "Command " << c << " not known!" << endl;
}
return 1;
}
int gCodeFile::addNewCommand( unsigned int g, unsigned int n)
{
gCommand* gC = new gCommand( g, n);
if( this->absolute && !this->commands->empty()){
gC->setX( this->commands->back()->getX());
gC->setY( this->commands->back()->getY());
gC->setZ( this->commands->back()->getZ());
gC->setI( 0);
gC->setJ( 0);
}
else
{
gC->setX( 0);
gC->setY( 0);
gC->setZ( 0);
gC->setI( 0);
gC->setJ( 0);
}
this->commands->push_back( gC);
return 1;
}
void gCodeFile::printCommands()
{
for( unsigned int i = 0; i < this->commands->size(); i++)
{
cout << commands->at(i)->toString() << endl;
}
}
vector<gCommand*>* gCodeFile::getCommands()
{
return this->commands;
}