-
Notifications
You must be signed in to change notification settings - Fork 7
/
dxf.cpp
211 lines (181 loc) · 6.23 KB
/
dxf.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "dxf.h"
#include "utils.h"
#include <QByteArray>
#include <QString>
DXF::DXF() :
mCurrentSection(eSection::eUNKNOWN),
mCurrentEntity(eEntity::eUNKNOWN)
{
}
bool DXF::hasNext() {
return !mF.atEnd();
}
QString DXF::next() {
if(!hasNext())
throw "End of file!!!";
return QString::fromUtf8(mF.readLine()).trimmed();
}
PolySegs& DXF::segments()
{
return mSegments;
}
void DXF::processDXF(const QString& fname)
{
if(!QFile::exists(fname))
return;
mLines.clear();
mSegments.clear();
mF.setFileName(fname);
if(mF.open(QFile::ReadOnly)) {
try {
while(hasNext()) {
QString line = next();
if(mCurrentSection != eSection::eENTITIES) {
if(line == "ENTITIES") {
mCurrentSection = eSection::eENTITIES;
continue;
}
} else if(mCurrentSection == eSection::eENTITIES) {
if(line == "POINT") {
mCurrentEntity = eEntity::ePOINT;
processPointEntity();
} else if(line == "ARC") {
mCurrentEntity = eEntity::eARC;
processArcEntity(); // discovered the arc entity, now process it
} else if(line == "LINE") {
mCurrentEntity = eEntity::eLINE;
processLineEntity(); // discovered the line entity, now process it
} else if(line == "LWPOLYLINE") {
mCurrentEntity = eEntity::eLWPOLYLINE;
processLwPolyLineEntity(); // discovered the lw polyline entity, now process it
} else if(line == "ENDSEC") {
mCurrentSection = eSection::eUNKNOWN;
}
}
}
} catch(const char* e) {
qd << e;
}
mF.close();
}
sortSegments();
}
void DXF::processLwPolyLineEntity()
{
int code = -1;
float x1, y1;
int numPoints = -1;
std::vector<QVector2D> points;
while(code != 0) {
if(numPoints == 0) break;
switch(code) {
case 5: qd << next(); break;
case 90: numPoints = next().toInt(); break;
case 70: next(); break; // polyline flag, 0=default, 1=closed, 2=plinegen
case 43: next(); break; // constant width
case 10: x1 = next().toFloat(); break;
case 20: y1 = next().toFloat(); points.push_back({x1, y1}); numPoints--; break;
case 0: qd << "end of lwpolyline entity"; break;
default: qd << "dxf:" << code << next(); break;
}
}
for(int i=0; i<points.size()-1; i++) {
mSegments.push_back({points[i],points[i+1]});
}
}
void DXF::processPointEntity()
{
int code = -1;
float x, y, z;
while(code != 0) {
code = next().toInt();
switch(code) {
case 10: x = next().toFloat(); break;
case 20: y = next().toFloat(); break;
case 30: z = next().toFloat(); break;
case 0: qd << "end of point entity"; break;
default: qd << "dxf:" << code << next(); break;
}
}
}
void DXF::processArcEntity() {
int code = -1;
float x, y, z, radius, startAngle, endAngle;
while(code != 0) {
code = next().toInt();
switch(code) {
case 10: x = next().toFloat(); break;
case 20: y = next().toFloat(); break;
case 30: z = next().toFloat(); break;
case 40: radius = next().toFloat(); break;
case 50: startAngle = next().toFloat(); break;
case 51: endAngle = next().toFloat(); break;
case 0: qd << "end of arc entity"; break;
default: qd << "dxf:" << code << next(); break;
}
}
const float numSegments = 10;
float angleIncrement = (endAngle - startAngle) / numSegments;
// Check for clockwise or counterclockwise rotation
if (angleIncrement < 0.0f) {
// Adjust for CCW rotation
angleIncrement = (endAngle + 360.0f - startAngle) / numSegments;
}
for (int i = 0; i < numSegments; ++i) {
float angle1 = startAngle + i * angleIncrement;
float angle2 = startAngle + (i + 1) * angleIncrement;
QVector2D p1, p2;
p1.setX(x + radius * cos(angle1 * M_PI / 180.0f));
p1.setY(y + radius * sin(angle1 * M_PI / 180.0f));
p2.setX(x + radius * cos(angle2 * M_PI / 180.0f));
p2.setY(y + radius * sin(angle2 * M_PI / 180.0f));
mSegments.push_back({ p1, p2 });
}
}
void DXF::processLineEntity() {
int code = -1;
float x1, y1, z1, x2, y2, z2;
while(code != 0) {
code = next().toInt();
switch(code) {
case 10: x1 = next().toFloat(); break;
case 11: x2 = next().toFloat(); break;
case 20: y1 = next().toFloat(); break;
case 21: y2 = next().toFloat(); break;
case 30: z1 = next().toFloat(); break;
case 31: z2 = next().toFloat(); break;
case 0: qd << "end of line entity"; break;
default: qd << "dxf:" << code << next(); break;
}
}
mSegments.push_back({ {x1, y1}, {x2, y2} });
}
void DXF::sortSegments()
{
if(mSegments.empty())
return;
PolySegs sorted;
sorted.push_back(mSegments[0]);
mSegments.erase(mSegments.begin());
while(mSegments.size()) {
QVector2D lastPoint = sorted.back().p1();
float minDist = std::numeric_limits<float>::max();
auto closestSegment = mSegments.begin();
bool flipSegment = false;
for(auto it = mSegments.begin(); it!= mSegments.end(); ++it) {
float distToStart = (lastPoint - it->p[0]).length();
float distToEnd = (lastPoint - it->p[1]).length();
if (distToStart < minDist || distToEnd < minDist) {
closestSegment = it;
minDist = std::min(distToStart, distToEnd);
flipSegment = distToEnd < distToStart; // Flip segment if the end is closer
}
}
if(flipSegment) {
closestSegment->swap();
}
sorted.push_back(*closestSegment);
mSegments.erase(closestSegment);
}
mSegments = sorted;
}