QElectroTech  0.70
createdxf.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "createdxf.h"
19 #include <QFile>
20 #include <QTextStream>
21 #include <QMessageBox>
22 #include <QString>
23 #include "exportdialog.h"
24 
25 
26 const double Createdxf::sheetWidth = 4000;
27 const double Createdxf::sheetHeight = 2700;
28 
29 double Createdxf::xScale = 1;
30 double Createdxf::yScale = 1;
31 
33 {
34 }
35 
36 
38 {
39 }
40 
41 /* Header section of every DXF file.*/
42 void Createdxf::dxfBegin (const QString& fileName)
43 {
44 
45  // Creation of an output stream object in text mode.
46  // Header section of every dxf file.
47  if (!fileName.isEmpty()) {
48  QFile file(fileName);
49  if (!file.open(QFile::WriteOnly)) {
50  // error message
51  QMessageBox errorFileOpen;
52  errorFileOpen.setIcon(QMessageBox::Warning);
53  errorFileOpen.setText("Error: "+fileName+" Could Not be Opened.");
54  errorFileOpen.setInformativeText("Close all Files and Try Again.");
55  errorFileOpen.exec();
56  exit(0);
57  } else {
58  QTextStream To_Dxf(&file);
59  To_Dxf << 999 << "\r\n";
60  To_Dxf << "QET" << "\r\n";
61  To_Dxf << 0 << "\r\n";
62  To_Dxf << "SECTION" << "\r\n";
63  To_Dxf << 2 << "\r\n";
64  To_Dxf << "HEADER" << "\r\n";
65  To_Dxf << 9 << "\r\n";
66  To_Dxf << "$ACADVER" << "\r\n";
67  To_Dxf << 1 << "\r\n";
68  To_Dxf << "AC1006" << "\r\n";
69  To_Dxf << 9 << "\r\n";
70  To_Dxf << "$INSBASE" << "\r\n";
71  To_Dxf << 10 << "\r\n";
72  To_Dxf << "0.0" << "\r\n";
73  To_Dxf << 20 << "\r\n";
74  To_Dxf << "0.0" << "\r\n";
75  To_Dxf << 30 << "\r\n";
76  To_Dxf << "0.0" << "\r\n";
77  To_Dxf << 9 << "\r\n";
78 
79  To_Dxf << "$EXTMIN" << "\r\n";
80  To_Dxf << 10 << "\r\n";
81  To_Dxf << "0.0" << "\r\n";
82  To_Dxf << 20 << "\r\n";
83  To_Dxf << "0.0" << "\r\n";
84  To_Dxf << 9 << "\r\n";
85  To_Dxf << "$EXTMAX" << "\r\n";
86  To_Dxf << 10 << "\r\n";
87  To_Dxf << "4000.0" << "\r\n";
88  To_Dxf << 20 << "\r\n";
89  To_Dxf << "4000.0" << "\r\n";
90 
91  To_Dxf << 9 << "\r\n";
92  To_Dxf << "$LIMMIN" << "\r\n";
93  To_Dxf << 10 << "\r\n";
94  To_Dxf << "0.0" << "\r\n";
95  To_Dxf << 20 << "\r\n";
96  To_Dxf << "0.0" << "\r\n";
97  To_Dxf << 9 << "\r\n";
98  To_Dxf << "$LIMMAX" << "\r\n";
99  To_Dxf << 10 << "\r\n";
100  To_Dxf << "4000.0" << "\r\n";
101  To_Dxf << 20 << "\r\n";
102  To_Dxf << "4000.0" << "\r\n";
103  To_Dxf << 0 << "\r\n";
104  To_Dxf << "ENDSEC" << "\r\n";
105  To_Dxf << 0 << "\r\n";
106  To_Dxf << "SECTION" << "\r\n";
107  To_Dxf << 2 << "\r\n";
108  To_Dxf << "TABLES" << "\r\n";
109  To_Dxf << 0 << "\r\n";
110  To_Dxf << "TABLE" << "\r\n";
111  To_Dxf << 2 << "\r\n";
112 
113  To_Dxf << "VPORT" << "\r\n";
114  To_Dxf << 70 << "\r\n";
115  To_Dxf << 1 << "\r\n";
116  To_Dxf << 0 << "\r\n";
117  To_Dxf << "VPORT" << "\r\n";
118  To_Dxf << 2 << "\r\n";
119  To_Dxf << "*ACTIVE" << "\r\n";
120  To_Dxf << 70 << "\r\n";
121  To_Dxf << 0 << "\r\n";
122  To_Dxf << 10 << "\r\n";
123  To_Dxf << 0.0 << "\r\n";
124  To_Dxf << 20 << "\r\n";
125  To_Dxf << 0.0 << "\r\n";
126  To_Dxf << 11 << "\r\n";
127  To_Dxf << 1.0 << "\r\n";
128  To_Dxf << 21 << "\r\n";
129  To_Dxf << 1.0 << "\r\n";
130  To_Dxf << 12 << "\r\n";
131  To_Dxf << 2000 << "\r\n";
132  To_Dxf << 22 << "\r\n";
133  To_Dxf << 1350 << "\r\n";
134  To_Dxf << 13 << "\r\n";
135  To_Dxf << 0.0 << "\r\n";
136  To_Dxf << 23 << "\r\n";
137  To_Dxf << 0.0 << "\r\n";
138  To_Dxf << 14 << "\r\n";
139  To_Dxf << 1.0 << "\r\n";
140  To_Dxf << 24 << "\r\n";
141  To_Dxf << 1.0 << "\r\n";
142  To_Dxf << 15 << "\r\n";
143  To_Dxf << 0.0 << "\r\n";
144  To_Dxf << 25 << "\r\n";
145  To_Dxf << 0.0 << "\r\n";
146  To_Dxf << 16 << "\r\n";
147  To_Dxf << 0.0 << "\r\n";
148  To_Dxf << 26 << "\r\n";
149  To_Dxf << 0.0 << "\r\n";
150  To_Dxf << 36 << "\r\n";
151  To_Dxf << 1.0 << "\r\n";
152  To_Dxf << 17 << "\r\n";
153  To_Dxf << 0.0 << "\r\n";
154  To_Dxf << 27 << "\r\n";
155  To_Dxf << 0.0 << "\r\n";
156  To_Dxf << 37 << "\r\n";
157  To_Dxf << 0.0 << "\r\n";
158  To_Dxf << 40 << "\r\n";
159  To_Dxf << 2732.5 << "\r\n";
160  To_Dxf << 41 << "\r\n";
161  To_Dxf << 2.558 << "\r\n";
162  To_Dxf << 42 << "\r\n";
163  To_Dxf << 50.0 << "\r\n";
164  To_Dxf << 43 << "\r\n";
165  To_Dxf << 0.0 << "\r\n";
166  To_Dxf << 44 << "\r\n";
167  To_Dxf << 0.0 << "\r\n";
168  To_Dxf << 50 << "\r\n";
169  To_Dxf << 0.0 << "\r\n";
170  To_Dxf << 51 << "\r\n";
171  To_Dxf << 0.0 << "\r\n";
172  To_Dxf << 71 << "\r\n";
173  To_Dxf << 0 << "\r\n";
174  To_Dxf << 72 << "\r\n";
175  To_Dxf << 100 << "\r\n";
176  To_Dxf << 73 << "\r\n";
177  To_Dxf << 1 << "\r\n";
178  To_Dxf << 74 << "\r\n";
179  To_Dxf << 1 << "\r\n";
180  To_Dxf << 75 << "\r\n";
181  To_Dxf << 0 << "\r\n";
182  To_Dxf << 76 << "\r\n";
183  To_Dxf << 0 << "\r\n";
184  To_Dxf << 77 << "\r\n";
185  To_Dxf << 0 << "\r\n";
186  To_Dxf << 78 << "\r\n";
187  To_Dxf << 0 << "\r\n";
188  To_Dxf << 0 << "\r\n";
189  To_Dxf << "ENDTAB" << "\r\n";
190  To_Dxf << 0 << "\r\n";
191  To_Dxf << "TABLE" << "\r\n";
192  To_Dxf << 2 << "\r\n";
193 
194  To_Dxf << "LTYPE" << "\r\n";
195  To_Dxf << 70 << "\r\n";
196  To_Dxf << 1 << "\r\n";
197  To_Dxf << 0 << "\r\n";
198  To_Dxf << "LTYPE" << "\r\n";
199  To_Dxf << 2 << "\r\n";
200  To_Dxf << "CONTINUOUS" << "\r\n";
201  To_Dxf << 70 << "\r\n";
202  To_Dxf << 64 << "\r\n";
203  To_Dxf << 3 << "\r\n";
204  To_Dxf << "Solid Line" << "\r\n";
205  To_Dxf << 72 << "\r\n";
206  To_Dxf << 65 << "\r\n";
207  To_Dxf << 73 << "\r\n";
208  To_Dxf << 0 << "\r\n";
209  To_Dxf << 40 << "\r\n";
210  To_Dxf << 0.00 << "\r\n";
211  To_Dxf << 0 << "\r\n";
212 
213  To_Dxf << "ENDTAB" << "\r\n";
214  To_Dxf << 0 << "\r\n";
215  To_Dxf << "ENDSEC" << "\r\n";
216  To_Dxf << 0 << "\r\n";
217  To_Dxf << "SECTION" << "\r\n";
218  To_Dxf << 2 << "\r\n";
219  To_Dxf << "BLOCKS" << "\r\n";
220  To_Dxf << 0 << "\r\n";
221  To_Dxf << "ENDSEC" << "\r\n";
222  To_Dxf << 0 << "\r\n";
223  To_Dxf << "SECTION" << "\r\n";
224  To_Dxf << 2 << "\r\n";
225  To_Dxf << "ENTITIES" << "\r\n";
226  file.close();
227  }
228  }
229 }
230 
231 /* End Section of every DXF File*/
232 void Createdxf::dxfEnd (const QString& fileName)
233 {
234  // Creation of an output stream object in text mode.
235  if (!fileName.isEmpty()) {
236  QFile file(fileName);
237  if (!file.open(QFile::Append)) {
238  // error message
239  QMessageBox errorFileOpen;
240  errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
241  errorFileOpen.setInformativeText("Close all Files and Re-Run");
242  errorFileOpen.exec();
243  } else {
244  QTextStream To_Dxf(&file);
245  To_Dxf << 0 << "\r\n";
246  To_Dxf << "ENDSEC" << "\r\n";
247  To_Dxf << 0 << "\r\n";
248  To_Dxf << "EOF";
249  file.close();
250  }
251  }
252 }
253 
254 
255 /* draw circle in dxf format*/
256 void Createdxf::drawCircle (const QString& fileName, double radius, double x, double y, int colour)
257 {
258  if (!fileName.isEmpty()) {
259  QFile file(fileName);
260  if (!file.open(QFile::Append)) {
261  // error message
262  QMessageBox errorFileOpen;
263  errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
264  errorFileOpen.setInformativeText("Close all Files and Re-Run");
265  errorFileOpen.exec();
266  } else {
267  QTextStream To_Dxf(&file);
268  // Draw the circle
269  To_Dxf << 0 << "\r\n";
270  To_Dxf << "CIRCLE" << "\r\n";
271  To_Dxf << 8 << "\r\n";
272  To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
273  To_Dxf << 62 << "\r\n";
274  To_Dxf << colour << "\r\n"; // Colour Code
275  To_Dxf << 10 << "\r\n"; // XYZ is the Center point of circle
276  To_Dxf << x << "\r\n"; // X in UCS (User Coordinate System)coordinates
277  To_Dxf << 20 << "\r\n";
278  To_Dxf << y << "\r\n"; // Y in UCS (User Coordinate System)coordinates
279  To_Dxf << 30 << "\r\n";
280  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
281  To_Dxf << 40 << "\r\n";
282  To_Dxf << radius << "\r\n"; // radius of circle
283  file.close();
284  }
285  }
286 }
287 
288 
289 /* draw line in DXF Format*/
290 void Createdxf::drawLine (const QString &fileName, double x1, double y1, double x2, double y2,const int &colour)
291 {
292  if (!fileName.isEmpty()) {
293  QFile file(fileName);
294  if (!file.open(QFile::Append)) {
295  // error message
296  QMessageBox errorFileOpen;
297  errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
298  errorFileOpen.setInformativeText("Close all Files and Re-Run");
299  errorFileOpen.exec();
300  } else {
301  QTextStream To_Dxf(&file);
302  // Draw the Line
303  To_Dxf << 0 << "\r\n";
304  To_Dxf << "LINE" << "\r\n";
305  To_Dxf << 8 << "\r\n";
306  To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
307  To_Dxf << 62 << "\r\n";
308  To_Dxf << colour << "\r\n"; // Colour Code
309  To_Dxf << 10 << "\r\n";
310  To_Dxf << x1 << "\r\n"; // X in UCS (User Coordinate System)coordinates
311  To_Dxf << 20 << "\r\n";
312  To_Dxf << y1 << "\r\n"; // Y in UCS (User Coordinate System)coordinates
313  To_Dxf << 30 << "\r\n";
314  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
315  To_Dxf << 11 << "\r\n";
316  To_Dxf << x2 << "\r\n"; // X in UCS (User Coordinate System)coordinates
317  To_Dxf << 21 << "\r\n";
318  To_Dxf << y2 << "\r\n"; // Y in UCS (User Coordinate System)coordinates
319  To_Dxf << 31 << "\r\n";
320  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
321  file.close();
322  }
323  }
324 }
325 
326 long Createdxf::RGBcodeTable[255]{
327  0x000000, 0xff0000, 0xffff00, 0x00ff00, 0x00ffff,
328  0x0000ff, 0xff00ff, 0xffffff, 0x414141, 0x808080,
329  0xff0000, 0xffaaaa, 0xbd0000, 0xbd7e7e, 0x810000,
330  0x815656, 0x680000, 0x684545, 0x4f0000, 0x4f3535,
331  0xff3f00, 0xffbfaa, 0xbd2e00, 0xbd8d7e, 0x811f00,
332  0x816056, 0x681900, 0x684e45, 0x4f1300, 0x4f3b35,
333  0xff7f00, 0xffd4aa, 0xbd5e00, 0xbd9d7e, 0x814000,
334  0x816b56, 0x683400, 0x685645, 0x4f2700, 0x4f4235,
335  0xffbf00, 0xffeaaa, 0xbd8d00, 0xbdad7e, 0x816000,
336  0x817656, 0x684e00, 0x685f45, 0x4f3b00, 0x4f4935,
337  0xffff00, 0xffffaa, 0xbdbd00, 0xbdbd7e, 0x818100,
338  0x818156, 0x686800, 0x686845, 0x4f4f00, 0x4f4f35,
339  0xbfff00, 0xeaffaa, 0x8dbd00, 0xadbd7e, 0x608100,
340  0x768156, 0x4e6800, 0x5f6845, 0x3b4f00, 0x494f35,
341  0x7fff00, 0xd4ffaa, 0x5ebd00, 0x9dbd7e, 0x408100,
342  0x6b8156, 0x346800, 0x566845, 0x274f00, 0x424f35,
343  0x3fff00, 0xbfffaa, 0x2ebd00, 0x8dbd7e, 0x1f8100,
344  0x608156, 0x196800, 0x4e6845, 0x134f00, 0x3b4f35,
345  0x00ff00, 0xaaffaa, 0x00bd00, 0x7ebd7e, 0x008100,
346  0x568156, 0x006800, 0x456845, 0x004f00, 0x354f35,
347  0x00ff3f, 0xaaffbf, 0x00bd2e, 0x7ebd8d, 0x00811f,
348  0x568160, 0x006819, 0x45684e, 0x004f13, 0x354f3b,
349  0x00ff7f, 0xaaffd4, 0x00bd5e, 0x7ebd9d, 0x008140,
350  0x56816b, 0x006834, 0x456856, 0x004f27, 0x354f42,
351  0x00ffbf, 0xaaffea, 0x00bd8d, 0x7ebdad, 0x008160,
352  0x568176, 0x00684e, 0x45685f, 0x004f3b, 0x354f49,
353  0x00ffff, 0xaaffff, 0x00bdbd, 0x7ebdbd, 0x008181,
354  0x568181, 0x006868, 0x456868, 0x004f4f, 0x354f4f,
355  0x00bfff, 0xaaeaff, 0x008dbd, 0x7eadbd, 0x006081,
356  0x567681, 0x004e68, 0x455f68, 0x003b4f, 0x35494f,
357  0x007fff, 0xaad4ff, 0x005ebd, 0x7e9dbd, 0x004081,
358  0x566b81, 0x003468, 0x455668, 0x00274f, 0x35424f,
359  0x003fff, 0xaabfff, 0x002ebd, 0x7e8dbd, 0x001f81,
360  0x566081, 0x001968, 0x454e68, 0x00134f, 0x353b4f,
361  0x0000ff, 0xaaaaff, 0x0000bd, 0x7e7ebd, 0x000081,
362  0x565681, 0x000068, 0x454568, 0x00004f, 0x35354f,
363  0x3f00ff, 0xbfaaff, 0x2e00bd, 0x8d7ebd, 0x1f0081,
364  0x605681, 0x190068, 0x4e4568, 0x13004f, 0x3b354f,
365  0x7f00ff, 0xd4aaff, 0x5e00bd, 0x9d7ebd, 0x400081,
366  0x6b5681, 0x340068, 0x564568, 0x27004f, 0x42354f,
367  0xbf00ff, 0xeeaaff, 0x8d00bd, 0xad7ebd, 0x600081,
368  0x765681, 0x4e0068, 0x5f4568, 0x3b004f, 0x49354f,
369  0xff00ff, 0xffaaff, 0xbd00bd, 0xbd7ebd, 0x810081,
370  0x815681, 0x680068, 0x684568, 0x4f004f, 0x4f354f,
371  0xff00bf, 0xffaaea, 0xbd008d, 0xbd7ead, 0x810060,
372  0x815676, 0x68004e, 0x68455f, 0x4f003b, 0x4f3549,
373  0xff007f, 0xffaad4, 0xbd005e, 0xbd7e9d, 0x810040,
374  0x81566b, 0x680034, 0x684556, 0x4f0027, 0x4f3542,
375  0xff003f, 0xffaabf, 0xbd002e, 0xbd7e8d, 0x81001f,
376  0x815660, 0x680019, 0x68454e, 0x4f0013, 0x4f353b,
377  0x333333, 0x505050, 0x696969, 0x828282, 0xbebebe
378 };
379 
389 int Createdxf::getcolorCode (const long red, const long green, const long blue)
390 {
391  long acirgb, r,g,b;
392  long mindst = 2147483647L;
393  long dst = 0;
394  int minndx = 0;
395  for ( int i = 0; i < 254; i++ )
396  {
397  acirgb = RGBcodeTable[i];
398  b = ( acirgb & 0xffL );
399  g = ( acirgb & 0xff00L ) >> 8;
400  r = acirgb >> 16;
401  dst = abs ( r-red) + abs ( g -green) + abs (b-blue);
402  if ( dst < mindst )
403  {
404  minndx = i;
405  mindst = dst;
406  }
407  }
408  return minndx;
409 }
410 
418 void Createdxf::drawLine(const QString &filepath, const QLineF &line, const int &colorcode) {
419  drawLine(filepath, line.p1().x() * xScale,
420  sheetHeight - (line.p1().y() * yScale),
421  line.p2().x() * xScale,
422  sheetHeight - (line.p2().y() * yScale),
423  colorcode);
424 }
425 
426 void Createdxf::drawArcEllipse(const QString &file_path, qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal spanAngle, qreal hotspot_x, qreal hotspot_y, qreal rotation_angle, const int &colorcode) {
427  // vector of parts of arc (stored as a pair of startAngle and spanAngle) for each quadrant.
428  QVector< QPair<qreal,qreal> > arc_parts_vector;
429 
430  if (spanAngle > 0) {
431  qreal start = startAngle;
432  qreal span;
433  int i;
434  for ( i = startAngle; i < startAngle+spanAngle; i++ ) {
435  int absolute_theta = (i > 0) ? i : -i;
436  if (absolute_theta == 0 || absolute_theta == 90 ||
437  absolute_theta == 180 || absolute_theta == 270 ||
438  absolute_theta == 360) {
439  span = i - start;
440  QPair<qreal, qreal> newPart(start,span);
441  arc_parts_vector.push_back(newPart);
442  start = i;
443  }
444  }
445  if (start != i) {
446  span = i - start;
447  QPair<qreal, qreal> newPart(start,span);
448  arc_parts_vector.push_back(newPart);
449  }
450  } else {
451  qreal start = startAngle;
452  qreal span;
453  int i;
454  for ( i = startAngle; i > startAngle+spanAngle; i-- ) {
455  int absolute_theta = (i > 0) ? i : -i;
456  if (absolute_theta == 0 || absolute_theta == 90 ||
457  absolute_theta == 180 || absolute_theta == 270 ||
458  absolute_theta == 360) {
459  span = i - start;
460  QPair<qreal, qreal> newPart(start,span);
461  arc_parts_vector.push_back(newPart);
462  start = i;
463  }
464  }
465  if (start != i) {
466  span = i - start;
467  QPair<qreal, qreal> newPart(start,span);
468  arc_parts_vector.push_back(newPart);
469  }
470  }
471 
472  for (int i = 0; i < arc_parts_vector.size(); i++) {
473 
474  QPair<qreal,qreal> arc = arc_parts_vector[i];
475  if (arc.second == 0)
476  continue;
477  qreal arc_startAngle = arc.first * 3.142/180;
478  qreal arc_spanAngle = arc.second * 3.142/180;
479 
480  qreal a = w/2;
481  qreal b = h/2;
482 
483  qreal x1 = x + w/2 + a*cos(arc_startAngle);
484  qreal y1 = y - h/2 + b*sin(arc_startAngle);
485  qreal x2 = x + w/2 + a*cos(arc_startAngle + arc_spanAngle);
486  qreal y2 = y - h/2 + b*sin(arc_startAngle + arc_spanAngle);
487 
488 
489  qreal mid_ellipse_x = x + w/2 + a*cos(arc_startAngle + arc_spanAngle/2);
490  qreal mid_ellipse_y = y - h/2 + b*sin(arc_startAngle + arc_spanAngle/2);
491  qreal mid_line_x = (x1+x2)/2;
492  qreal mid_line_y = (y1+y2)/2;
493 
494  qreal x3 = (mid_ellipse_x + mid_line_x)/2;
495  qreal y3 = (mid_ellipse_y + mid_line_y)/2;
496 
497  // find circumcenter of points (x1,y1), (x3,y3) and (x2,y2)
498  qreal a1 = 2*x2 - 2*x1;
499  qreal b1 = 2*y2 - 2*y1;
500  qreal c1 = x1*x1 + y1*y1 - x2*x2 - y2*y2;
501 
502  qreal a2 = 2*x3 - 2*x1;
503  qreal b2 = 2*y3 - 2*y1;
504  qreal c2 = x1*x1 + y1*y1 - x3*x3 - y3*y3;
505 
506  qreal center_x = (b1*c2 - b2*c1) / (a1*b2 - a2*b1);
507  qreal center_y = (a1*c2 - a2*c1) / (b1*a2 - b2*a1);
508 
509  qreal radius = sqrt( (x1-center_x)*(x1-center_x) + (y1-center_y)*(y1-center_y) );
510 
511  if ( x1 > center_x && y1 > center_y )
512  arc_startAngle = asin( (y1 - center_y) / radius );
513  else if ( x1 > center_x && y1 < center_y )
514  arc_startAngle = 3.142*2 - asin( (center_y - y1) / radius );
515  else if ( x1 < center_x && y1 < center_y )
516  arc_startAngle = 3.142 + asin( (center_y - y1) / radius );
517  else
518  arc_startAngle = 3.142 - asin( (y1 - center_y) / radius );
519 
520  qreal arc_endAngle;
521 
522  if ( x2 > center_x && y2 > center_y )
523  arc_endAngle = asin( (y2 - center_y) / radius );
524  else if ( x2 > center_x && y2 < center_y )
525  arc_endAngle = 3.142*2 - asin( (center_y - y2) / radius );
526  else if ( x2 < center_x && y2 < center_y )
527  arc_endAngle = 3.142 + asin( (center_y - y2) / radius );
528  else
529  arc_endAngle = 3.142 - asin( (y2 - center_y) / radius );
530 
531  if (arc_endAngle < arc_startAngle) {
532  qreal temp = arc_startAngle;
533  arc_startAngle = arc_endAngle;
534  arc_endAngle = temp;
535  }
536 
537  QPointF transformed_point = ExportDialog::rotation_transformed(center_x, center_y, hotspot_x, hotspot_y, rotation_angle);
538  center_x = transformed_point.x();
539  center_y = transformed_point.y();
540  arc_endAngle *= 180/3.142;
541  arc_startAngle *= 180/3.142;
542  arc_endAngle -= rotation_angle;
543  arc_startAngle -= rotation_angle;
544 
545  drawArc(file_path, center_x, center_y, radius, arc_startAngle, arc_endAngle, colorcode);
546  }
547 }
548 
556 void Createdxf::drawEllipse(const QString &filepath, const QRectF &rect, const int &colorcode) {
557  drawArcEllipse(filepath, rect.topLeft().x() * xScale,
558  sheetHeight - (rect.topLeft().y() * yScale),
559  rect.width() * xScale,
560  rect.height() * yScale,
561  0, 360, 0, 0, 0, colorcode);
562 }
563 
564 /* draw rectangle in dxf format */
565 void Createdxf::drawRectangle (const QString &fileName, double x1, double y1, double width, double height, const int &colour)
566 {
567  if (!fileName.isEmpty()) {
568  QFile file(fileName);
569  if (!file.open(QFile::Append)) {
570  // error message
571  QMessageBox errorFileOpen;
572  errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
573  errorFileOpen.setInformativeText("Close all Files and Re-Run");
574  errorFileOpen.exec();
575  } else {
576  QTextStream To_Dxf(&file);
577  // Draw the Rectangle
578  To_Dxf << 0 << "\r\n";
579  To_Dxf << "LINE" << "\r\n";
580  To_Dxf << 8 << "\r\n";
581  To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
582  To_Dxf << 62 << "\r\n";
583  To_Dxf << colour << "\r\n"; // Colour Code
584  To_Dxf << 10 << "\r\n";
585  To_Dxf << x1 << "\r\n"; // X in UCS (User Coordinate System)coordinates
586  To_Dxf << 20 << "\r\n";
587  To_Dxf << y1 << "\r\n"; // Y in UCS (User Coordinate System)coordinates
588  To_Dxf << 30 << "\r\n";
589  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
590  To_Dxf << 11 << "\r\n";
591  To_Dxf << x1+width << "\r\n"; // X in UCS (User Coordinate System)coordinates
592  To_Dxf << 21 << "\r\n";
593  To_Dxf << y1 << "\r\n"; // Y in UCS (User Coordinate System)coordinates
594  To_Dxf << 31 << "\r\n";
595  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
596  To_Dxf << 0 << "\r\n";
597  To_Dxf << "LINE" << "\r\n";
598  To_Dxf << 8 << "\r\n";
599  To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
600  To_Dxf << 62 << "\r\n";
601  To_Dxf << colour << "\r\n"; // Colour Code
602  To_Dxf << 10 << "\r\n";
603  To_Dxf << x1 << "\r\n"; // X in UCS (User Coordinate System)coordinates
604  To_Dxf << 20 << "\r\n";
605  To_Dxf << y1 << "\r\n"; // Y in UCS (User Coordinate System)coordinates
606  To_Dxf << 30 << "\r\n";
607  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
608  To_Dxf << 11 << "\r\n";
609  To_Dxf << x1 << "\r\n"; // X in UCS (User Coordinate System)coordinates
610  To_Dxf << 21 << "\r\n";
611  To_Dxf << y1+height << "\r\n"; // Y in UCS (User Coordinate System)coordinates
612  To_Dxf << 31 << "\r\n";
613  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
614  To_Dxf << 0 << "\r\n";
615  To_Dxf << "LINE" << "\r\n";
616  To_Dxf << 8 << "\r\n";
617  To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
618  To_Dxf << 62 << "\r\n";
619  To_Dxf << colour << "\r\n"; // Colour Code
620  To_Dxf << 10 << "\r\n";
621  To_Dxf << x1+width << "\r\n"; // X in UCS (User Coordinate System)coordinates
622  To_Dxf << 20 << "\r\n";
623  To_Dxf << y1 << "\r\n"; // Y in UCS (User Coordinate System)coordinates
624  To_Dxf << 30 << "\r\n";
625  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
626  To_Dxf << 11 << "\r\n";
627  To_Dxf << x1+width << "\r\n"; // X in UCS (User Coordinate System)coordinates
628  To_Dxf << 21 << "\r\n";
629  To_Dxf << y1+height << "\r\n"; // Y in UCS (User Coordinate System)coordinates
630  To_Dxf << 31 << "\r\n";
631  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
632  To_Dxf << 0 << "\r\n";
633  To_Dxf << "LINE" << "\r\n";
634  To_Dxf << 8 << "\r\n";
635  To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
636  To_Dxf << 62 << "\r\n";
637  To_Dxf << colour << "\r\n"; // Colour Code
638  To_Dxf << 10 << "\r\n";
639  To_Dxf << x1 << "\r\n"; // X in UCS (User Coordinate System)coordinates
640  To_Dxf << 20 << "\r\n";
641  To_Dxf << y1+height << "\r\n"; // Y in UCS (User Coordinate System)coordinates
642  To_Dxf << 30 << "\r\n";
643  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
644  To_Dxf << 11 << "\r\n";
645  To_Dxf << x1+width << "\r\n"; // X in UCS (User Coordinate System)coordinates
646  To_Dxf << 21 << "\r\n";
647  To_Dxf << y1+height << "\r\n"; // Y in UCS (User Coordinate System)coordinates
648  To_Dxf << 31 << "\r\n";
649  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
650  file.close();
651  }
652  }
653 }
654 
662 void Createdxf::drawRectangle(const QString &filepath, const QRectF &rect, const int &colorcode) {
663  drawRectangle(filepath, rect.bottomLeft().x() * xScale,
664  sheetHeight - (rect.bottomLeft().y() * yScale),
665  rect.width() * xScale,
666  rect.height() * yScale,
667  colorcode);
668 }
669 
670 /* draw arc in dx format */
671 void Createdxf::drawArc(const QString& fileName,double x,double y,double rad,double startAngle,double endAngle,int color)
672 {
673  if (!fileName.isEmpty()) {
674  QFile file(fileName);
675  if (!file.open(QFile::Append)) {
676  // error message
677  QMessageBox errorFileOpen;
678  errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
679  errorFileOpen.setInformativeText("Close all Files and Re-Run");
680  errorFileOpen.exec();
681  } else {
682  QTextStream To_Dxf(&file);
683  // Draw the arc
684  To_Dxf << 0 << "\r\n";
685  To_Dxf << "ARC" << "\r\n";
686  To_Dxf << 8 << "\r\n";
687  To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
688  To_Dxf << 62 << "\r\n";
689  To_Dxf << color << "\r\n"; // Colour Code
690  To_Dxf << 10 << "\r\n"; // XYZ is the Center point of circle
691  To_Dxf << x << "\r\n"; // X in UCS (User Coordinate System)coordinates
692  To_Dxf << 20 << "\r\n";
693  To_Dxf << y << "\r\n"; // Y in UCS (User Coordinate System)coordinates
694  To_Dxf << 30 << "\r\n";
695  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
696  To_Dxf << 40 << "\r\n";
697  To_Dxf << rad << "\r\n"; // radius of arc
698  To_Dxf << 50 << "\r\n";
699  To_Dxf << startAngle<< "\r\n"; // start angle
700  To_Dxf << 51 << "\r\n";
701  To_Dxf << endAngle << "\r\n"; // end angle
702  file.close();
703  }
704  }
705 }
706 
707 /* draw simple text in dxf format without any alignment specified */
708 void Createdxf::drawText(const QString& fileName, const QString& text,double x, double y, double height, double rotation, int colour)
709 {
710  if (!fileName.isEmpty()) {
711  QFile file(fileName);
712  if (!file.open(QFile::Append)) {
713  // error message
714  QMessageBox errorFileOpen;
715  errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
716  errorFileOpen.setInformativeText("Close all Files and Re-Run");
717  errorFileOpen.exec();
718  } else {
719  QTextStream To_Dxf(&file);
720  // Draw the circle
721  To_Dxf << 0 << "\r\n";
722  To_Dxf << "TEXT" << "\r\n";
723  To_Dxf << 8 << "\r\n";
724  To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
725  To_Dxf << 62 << "\r\n";
726  To_Dxf << colour << "\r\n"; // Colour Code
727  To_Dxf << 10 << "\r\n"; // XYZ
728  To_Dxf << x << "\r\n"; // X in UCS (User Coordinate System)coordinates
729  To_Dxf << 20 << "\r\n";
730  To_Dxf << y << "\r\n"; // Y in UCS (User Coordinate System)coordinates
731  To_Dxf << 30 << "\r\n";
732  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
733  To_Dxf << 40 << "\r\n";
734  To_Dxf << height << "\r\n"; // Text Height
735  To_Dxf << 1 << "\r\n";
736  To_Dxf << text << "\r\n"; // Text Value
737  To_Dxf << 50 << "\r\n";
738  To_Dxf << rotation << "\r\n"; // Text Rotation
739  file.close();
740  }
741  }
742 }
743 
744 /* draw aligned text in DXF Format */
745 // leftAlign flag added. If the alignment requested is 'fit to width' and the text length is very small,
746 // then the text is either centered or left-aligned, depnding on the value of leftAlign.
747 void Createdxf::drawTextAligned(const QString& fileName, const QString& text,double x, double y, double height, double rotation, double oblique,int hAlign, int vAlign, double xAlign,int colour,
748  bool leftAlign, float scale)
749 {
750  Q_UNUSED(scale);
751 
752  if (!fileName.isEmpty()) {
753  QFile file(fileName);
754  if (!file.open(QFile::Append)) {
755  // error message
756  QMessageBox errorFileOpen;
757  errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
758  errorFileOpen.setInformativeText("Close all Files and Re-Run");
759  errorFileOpen.exec();
760  } else {
761  QTextStream To_Dxf(&file);
762  // Draw the circle
763  To_Dxf << 0 << "\r\n";
764  To_Dxf << "TEXT" << "\r\n";
765  To_Dxf << 8 << "\r\n";
766  To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
767  To_Dxf << 62 << "\r\n";
768  To_Dxf << colour << "\r\n"; // Colour Code
769  To_Dxf << 10 << "\r\n"; // XYZ
770  To_Dxf << x << "\r\n"; // X in UCS (User Coordinate System)coordinates
771  To_Dxf << 20 << "\r\n";
772  To_Dxf << y << "\r\n"; // Y in UCS (User Coordinate System)coordinates
773  To_Dxf << 30 << "\r\n";
774  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
775  To_Dxf << 40 << "\r\n";
776  To_Dxf << height << "\r\n"; // Text Height
777  To_Dxf << 1 << "\r\n";
778  To_Dxf << text << "\r\n"; // Text Value
779  To_Dxf << 50 << "\r\n";
780  To_Dxf << rotation << "\r\n"; // Text Rotation
781  // If "Fit to width", then check if width of text < width specified then change it "center align or left align"
782  if (hAlign == 5) {
783  int xDiff = xAlign - x;
784  if (text.length() < xDiff/height && !leftAlign) {
785  hAlign = 1;
786  xAlign = (x+xAlign) / 2;
787  } else if (text.length() < xDiff/height && leftAlign) {
788  file.close();
789  return;
790  }
791  }
792 
793  To_Dxf << 51 << "\r\n";
794  To_Dxf << oblique << "\r\n"; // Text Obliqueness
795  To_Dxf << 72 << "\r\n";
796  To_Dxf << hAlign << "\r\n"; // Text Horizontal Alignment
797  To_Dxf << 73 << "\r\n";
798  To_Dxf << vAlign << "\r\n"; // Text Vertical Alignment
799 
800  if ((hAlign) || (vAlign)) { // Enter Second Point
801  To_Dxf << 11 << "\r\n"; // XYZ
802  To_Dxf << xAlign << "\r\n"; // X in UCS (User Coordinate System)coordinates
803  To_Dxf << 21 << "\r\n";
804  To_Dxf << y << "\r\n"; // Y in UCS (User Coordinate System)coordinates
805  To_Dxf << 31 << "\r\n";
806  To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
807  }
808  file.close();
809  }
810  }
811 }
static long RGBcodeTable[]
Definition: createdxf.h:53
static int getcolorCode(const long red, const long green, const long blue)
Createdxf::getcolorCode This function returns the ACI color which is the "nearest" color to the color...
Definition: createdxf.cpp:389
static void dxfEnd(const QString &)
Definition: createdxf.cpp:232
static void drawArc(const QString &, double x, double y, double rad, double startAngle, double endAngle, int color)
Definition: createdxf.cpp:671
static void drawRectangle(const QString &filepath, double, double, double, double, const int &colorcode)
Definition: createdxf.cpp:565
static void drawText(const QString &, const QString &, double, double, double, double, int)
Definition: createdxf.cpp:708
static const double sheetHeight
Definition: createdxf.h:56
static void drawArcEllipse(const QString &file_path, qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal spanAngle, qreal hotspot_x, qreal hotspot_y, qreal rotation_angle, const int &colorcode)
Definition: createdxf.cpp:426
static double yScale
Definition: createdxf.h:58
static double xScale
Definition: createdxf.h:57
static const double sheetWidth
Definition: createdxf.h:55
static QPointF rotation_transformed(qreal, qreal, qreal, qreal, qreal)
static void drawCircle(const QString &, double, double, double, int)
Definition: createdxf.cpp:256
static void drawEllipse(const QString &filepath, const QRectF &rect, const int &colorcode)
Createdxf::drawEllipse Conveniance function for draw ellipse.
Definition: createdxf.cpp:556
static void drawTextAligned(const QString &fileName, const QString &text, double x, double y, double height, double rotation, double oblique, int hAlign, int vAlign, double xAlign, int colour, bool leftAlign=false, float scale=0)
Definition: createdxf.cpp:747
static void drawLine(const QString &filapath, double, double, double, double, const int &clorcode)
Definition: createdxf.cpp:290
static void dxfBegin(const QString &)
Definition: createdxf.cpp:42