-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdxfcreationadapter.cpp
More file actions
278 lines (253 loc) · 8.81 KB
/
dxfcreationadapter.cpp
File metadata and controls
278 lines (253 loc) · 8.81 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "dxfcreationadapter.h"
#include <stdio.h>
#include <QDebug>
#include <QLineF>
#include <QTransform>
#include <QPainter>
#include <QLabel>
#include <QApplication>
#include <QRegularExpression>
#include "pdmalgorithmutil.h"
#include "beziercurve2arcs/beziercurvetoarcs.h"
DxfCreationAdapter::DxfCreationAdapter()
{
}
QMap<QString, GraphicsPrimitive> DxfCreationAdapter::getAllLayers()
{
return mLayers;
}
QMap<QString, GraphicsPrimitive> DxfCreationAdapter::getAllBlock()
{
return mBlockItems;
}
GraphicsPrimitive DxfCreationAdapter::getBlock(const QString &iName)
{
return mBlockItems.value(iName);
}
void DxfCreationAdapter::addLayer(const DL_LayerData &iData)
{
QString name = iData.name.c_str();
GraphicsPrimitive primitive;
primitive.name = name;
mLayers[name] = primitive;
}
void DxfCreationAdapter::addPoint(const DL_PointData &iData)
{
Q_UNUSED(iData);
}
void DxfCreationAdapter::addLine(const DL_LineData &iData)
{
addPrimitiveLine(attributes.getLayer().c_str(), iData.x1, iData.y1, iData.x2, iData.y2);
}
void DxfCreationAdapter::addArc(const DL_ArcData &iData)
{
QPointF startPos = PdmAlgorithmUtil::getPosByCircleAngle(iData.cx, iData.cy, iData.radius, iData.angle1);
QPointF endPos = PdmAlgorithmUtil::getPosByCircleAngle(iData.cx, iData.cy, iData.radius, iData.angle2);
addPrimitiveArc(attributes.getLayer().c_str(), QPointF(iData.cx, iData.cy), startPos, endPos);
}
void DxfCreationAdapter::addCircle(const DL_CircleData &iData)
{
addPrimitiveArc(attributes.getLayer().c_str(), iData.cx, iData.cy, iData.radius, 0, 0);
}
void DxfCreationAdapter::addEllipse(const DL_EllipseData &iData)
{
QPointF cPoint(iData.cx, iData.cy);
QPointF sPoint = cPoint + QPointF(iData.mx, iData.my);
QLineF sLine(cPoint, sPoint);
qreal r1 = sLine.length();
qreal r2 = r1 * iData.ratio;
qreal alpha = sLine.angle();
QPainterPath path;
qreal startAngle = iData.angle1 * 180 / M_PI;
qreal endAngle = iData.angle2 * 180 / M_PI;
if (startAngle > endAngle) {
endAngle += 360;
}
path.arcTo(-r1, -r2, 2 * r1, 2 * r2, startAngle, endAngle - startAngle);
QTransform trans;
trans.scale(1,-1);
trans.translate(cPoint.x(), -cPoint.y());
trans.rotate(alpha);
path = trans.map(path);
GraphicsPrimitive &primitive = getGraphicsPrimitive(attributes.getLayer().c_str());
QPointF lastPos;
for (int i = 0; i < path.elementCount(); ++i) {
QPainterPath::Element element = path.elementAt(i);
QPointF pos(element.x, element.y);
if (element.type == QPainterPath::CurveToElement) {
QPointF controlPosA = pos;
QPainterPath::Element controlElement = path.elementAt(++i);
QPointF controlPosB(controlElement.x, controlElement.y);
controlElement = path.elementAt(++i);
pos = QPointF(controlElement.x, controlElement.y);
primitive.path.moveTo(lastPos);
primitive.path.cubicTo(controlPosA, controlPosB, pos);
}
lastPos = pos;
}
}
void DxfCreationAdapter::addPolyline(const DL_PolylineData &iData)
{
Q_UNUSED(iData);
mIsFirstVertex = true;
mCurrentMode = Polyline;
mIsClosePoly = iData.flags == 1;
}
void DxfCreationAdapter::addVertex(const DL_VertexData &iData)
{
if (mIsFirstVertex) {
mIsFirstVertex = false;
mFirstVertex = iData;
GraphicsPrimitive &primitive = getGraphicsPrimitive(attributes.getLayer().c_str());
primitive.path.moveTo(iData.x, iData.y);
} else {
QPointF startPos(mLastVertex.x, mLastVertex.y);
QPointF endPos(iData.x, iData.y);
qreal bluge = mLastVertex.bulge;
addPrimitivePolyline(startPos, endPos, bluge);
}
mLastVertex = iData;
}
void DxfCreationAdapter::addBlock(const DL_BlockData &iData)
{
mBlockName = iData.name.c_str();
if (mBlockName == "1") {
int i=0;
++i;
}
GraphicsPrimitive primitive;
primitive.name = mBlockName;
mBlockItems[mBlockName] = primitive;
}
void DxfCreationAdapter::endBlock()
{
mBlockName.clear();
}
void DxfCreationAdapter::endSection()
{
}
void DxfCreationAdapter::endEntity()
{
if (mCurrentMode == Polyline && mIsClosePoly) {
QPointF startPos(mLastVertex.x, mLastVertex.y);
QPointF endPos(mFirstVertex.x, mFirstVertex.y);
qreal bluge = mLastVertex.bulge;
addPrimitivePolyline(startPos, endPos, bluge);
}
mCurrentMode = NoneMode;
}
void DxfCreationAdapter::addInsert(const DL_InsertData &iData)
{
GraphicsPrimitive &primitive = getGraphicsPrimitive(attributes.getLayer().c_str());
GraphicsItem item;
item.name = iData.name.c_str();
item.angle = iData.angle;
item.pos = QPointF(iData.ipx, iData.ipy);
item.sx = iData.sx;
item.sy = iData.sy;
primitive.items.append(item);
}
void DxfCreationAdapter::printAttributes()
{
printf(" Attributes: Layer: %s, ", attributes.getLayer().c_str());
printf(" Color: ");
if (attributes.getColor()==256) {
printf("BYLAYER");
} else if (attributes.getColor()==0) {
printf("BYBLOCK");
} else {
printf("%d", attributes.getColor());
}
printf(" Width: ");
if (attributes.getWidth()==-1) {
printf("BYLAYER");
} else if (attributes.getWidth()==-2) {
printf("BYBLOCK");
} else if (attributes.getWidth()==-3) {
printf("DEFAULT");
} else {
printf("%d", attributes.getWidth());
}
printf(" Type: %s\n", attributes.getLinetype().c_str());
}
GraphicsPrimitive &DxfCreationAdapter::getGraphicsPrimitive(const QString &iPrimitiveName)
{
if (mBlockName.isEmpty()) {
return mLayers[iPrimitiveName];
} else {
return mBlockItems[mBlockName];
}
}
void DxfCreationAdapter::addPrimitiveLine(const QString &iPrimitiveName, qreal iX1, qreal iY1, qreal iX2, qreal iY2)
{
GraphicsPrimitive &primitive = getGraphicsPrimitive(iPrimitiveName);
primitive.path.moveTo(iX1, iY1);
primitive.path.lineTo(iX2, iY2);
}
void DxfCreationAdapter::addPrimitiveArc(const QString &iPrimitiveName, qreal iCx, qreal iCy, qreal iRadius,
qreal iStartAngle, qreal iEndAngle, const QString &iType)
{
GraphicsPrimitive &primitive = getGraphicsPrimitive(iPrimitiveName);
if (iEndAngle < iStartAngle) {
iEndAngle += 360;
}
qreal arcLength = iEndAngle - iStartAngle;
if (iType == "G03") {
arcLength -= 360;
}
primitive.path.moveTo(PdmAlgorithmUtil::getPosByCircleAngle(iCx, iCy, iRadius, iStartAngle, true));
primitive.path.arcTo(QRectF(iCx-iRadius,iCy-iRadius,2*iRadius,2*iRadius), iStartAngle, arcLength);
}
void DxfCreationAdapter::addPrimitiveArc(const QString &iPrimitiveName, const QPointF &iCenter, const QPointF &iStartPos,
const QPointF &iEndPos, const QString &iType)
{
addPrimitiveArc(iPrimitiveName, iCenter.x(), iCenter.y(), QLineF(iCenter, iEndPos).length(),
QLineF(iCenter, iStartPos).angle(), QLineF(iCenter, iEndPos).angle(), iType);
}
void DxfCreationAdapter::addPrimitivePolyline(const QPointF &iStartPos, const QPointF &iEndPos, qreal iBluge)
{
qreal bluge = iBluge;
QPointF startPos = iStartPos;
QPointF endPos = iEndPos;
if (bluge == 0 || QLineF(iStartPos, iEndPos).length() < 0.1) {
addPrimitiveLine(attributes.getLayer().c_str(), startPos.x(), startPos.y(), endPos.x(), endPos.y());
} else {
qreal dis = QLineF(startPos, endPos).length();
qreal radius = (dis / 4) * (bluge + (1/bluge));
//已知半径和两点求圆心
qreal R = qAbs(radius);
qreal x1 = startPos.x();
qreal y1= startPos.y();
qreal x2 = endPos.x();
qreal y2 = endPos.y();
double c1 = (x2*x2 - x1*x1 + y2*y2 - y1*y1) / (2 *(x2 - x1));
double c2 = (y2 - y1) / (x2 - x1); //斜率
double A = (c2*c2 + 1);
double B = (2 * x1*c2 - 2 * c1*c2 - 2 * y1);
double C = x1*x1 - 2 * x1*c1 + c1*c1 + y1*y1 - R*R;
qreal cy1 = (-B + sqrt(B*B - 4 * A*C)) / (2 * A);
qreal cy2 = (-B - sqrt(B*B - 4 * A*C)) / (2 * A);
qreal y = 0;
qreal tolerance = 0.0001;
if (startPos.x() + tolerance < endPos.x()) {
y = cy1 > cy2 ? cy1 : cy2;
} else if (startPos.x() - tolerance > endPos.x()) {
y = cy1 < cy2 ? cy1 : cy2;
} else {
qreal cx1 = c1 - c2 * cy1;
qreal cx2 = c1 - c2 * cy2;
if (startPos.y() < endPos.y()) {
y = cx1 > cx2 ? cy1 : cy2;
} else {
y = cx1 < cx2 ? cy1 : cy2;
}
}
QString type = "G03";
if (bluge < 0) {
y = y == cy1 ? cy2 : cy1;
type = "G02";
}
qreal x = c1 - c2 * y;
addPrimitiveArc(attributes.getLayer().c_str(), QPointF(x, y), startPos, endPos, type);
}
}