-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcellonimporter.cpp
More file actions
191 lines (174 loc) · 5.71 KB
/
excellonimporter.cpp
File metadata and controls
191 lines (174 loc) · 5.71 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "excellonimporter.h"
#include "utils.h"
#include <QtCore>
Drill::Drill()
{
}
Drill::Drill( mpq_class x, mpq_class y, mpq_class dia ) : m_x(x), m_y(y), m_diameter(dia)
{
}
void Drill::setDrill( mpq_class x, mpq_class y, mpq_class dia )
{
m_x = x;
m_y = y;
m_diameter = dia;
}
ExcellonImporter::ExcellonImporter()
{
m_inHeader = false;
m_digits1 = 2; // default according to Excellon specification
m_digits2 = 4; // default according to Excellon specification
m_numberFormatExplicitlySet = false;
m_unit = inch; // default according to Excellon specification
m_zeros = leading; // default according to Excellon specification
m_zeros = trailing; // Eagle (Cadsoft) uses this default; TO BE VERIFIED WITH OTHER SOFTWARE!!!
m_currentTool = -1;
}
bool ExcellonImporter::import( QString filename )
{
if (filename.isEmpty())
return false;
QFile file( filename );
if (!file.open(QFile::ReadOnly))
return false;
QTextStream stream(&file);
QString line;
while (true) {
line = stream.readLine();
line = line.trimmed().toUpper();
if (line.isEmpty() && stream.atEnd())
break;
if (line.startsWith(";"))
break;
if (line == "M48") {
m_inHeader = true;
continue;
}
if (line == "M95") {
if (!m_inHeader)
qDebug() << "M95 command used at the wrong place.";
m_inHeader = false;
continue;
}
if (line == "M71") {
m_unit = metric;
if (!m_numberFormatExplicitlySet) {
// the file did not specify the number format
// use the default
m_digits1 = 3;
m_digits2 = 3;
}
continue;
}
if ((line == "M72") || (line == "M70")) {
m_unit = inch;
continue;
}
if (m_inHeader) {
if (line == "%") {
m_inHeader = false;
continue;
}
if (line.startsWith("METRIC")) {
m_unit = metric;
line = line.mid(7); // jump over "METRIC,"
}
if (line.startsWith("INCH")) {
m_unit = inch;
line = line.mid(5); // jump over "INCH,"
}
if (line == "TZ") {
m_zeros = trailing;
continue;
}
if (line == "LZ") {
m_zeros = leading;
continue;
}
QRegExp rx("^T([0-9]{1,2})C([0-9.]+)");
int idx = rx.indexIn(line);
if (idx != -1) {
// found tool definition command
int num = rx.cap(1).toInt();
QString dia = rx.cap(2);
if (m_tool.contains(num)) {
qDebug() << "Again found tool definition for tool number" << num << ". Replacing old definition.";
}
m_tool[num] = mpq_from_decimal_string(dia);
if (m_unit == metric)
m_tool[num] /= 1000; // convert from mm to m
else {
m_tool[num] *= 254;
m_tool[num] /= 10000; // convert from inch to m
}
continue;
}
} else {
// not inside header
QRegExp rx("^T([0-9]{1,2})$");
int idx = rx.indexIn(line);
if (idx != -1) {
// found tool change command
int num = rx.cap(1).toInt();
if (!m_tool.contains(num)) {
qDebug() << "Tool" << num << "not defined.";
}
m_currentTool = num;
continue;
}
rx.setPattern("^X([0-9.]+)Y([0-9.]+)$");
idx = rx.indexIn(line);
if (idx != -1) {
// drill at position
QString x_str = rx.cap(1);
QString y_str = rx.cap(2);
if (m_currentTool == -1) {
qDebug() << "Cannot drill at" << x_str << y_str << ". No drill.";
continue;
}
mpq_class dia = m_tool.value(m_currentTool);
mpq_class x = convert_into_mpq( x_str );
mpq_class y = convert_into_mpq( y_str );
if (m_unit == metric) {
x /= 1000; // convert from mm to m
y /= 1000; // convert from mm to m
} else {
x *= 254;
x /= 10000; // convert from inch to m
y *= 254;
y /= 10000; // convert from inch to m
}
m_drill << Drill(x,y,dia);
qDebug() << line << ": drilling at (in m) " << x.get_d() << y.get_d() << "dia:" << dia.get_d();
continue;
}
}
// command not handled
if (m_inHeader)
qDebug() << "unknown command in header:" << line;
else
qDebug() << "unknown command in body:" << line;
}
return true;
}
mpq_class ExcellonImporter::convert_into_mpq( QString x )
{
if (x.contains('.'))
return mpq_from_decimal_string(x);
if (m_zeros == leading) {
// leading zeros must be included
while (x.size() < m_digits1) {
// fill with '0'
x.append( '0' );
}
x.insert( m_digits1, '.' );
} else {
// trailing zeros must be included
while (x.size() < m_digits2) {
// fill with '0'
x.prepend('0');
}
x.insert( x.size() - m_digits2, '.' );
}
return mpq_from_decimal_string(x);
}