You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

493 lines
11 KiB

  1. /******************************************************************************
  2. ** libDXFrw - Library to read/write DXF files (ascii & binary) **
  3. ** **
  4. ** Copyright (C) 2011 Rallaz, rallazz@gmail.com **
  5. ** **
  6. ** This library is free software, licensed under the terms of the GNU **
  7. ** General Public License as published by the Free Software Foundation, **
  8. ** either version 2 of the License, or (at your option) any later version. **
  9. ** You should have received a copy of the GNU General Public License **
  10. ** along with this program. If not, see <http://www.gnu.org/licenses/>. **
  11. ******************************************************************************/
  12. #ifndef DRW_BASE_H
  13. #define DRW_BASE_H
  14. #define DRW_VERSION "0.5.13"
  15. #include <string>
  16. #include <cmath>
  17. #define UTF8STRING std::string
  18. #define DRW_UNUSED( x ) (void) x
  19. #if defined(WIN64) || defined(_WIN64) || defined(__WIN64__)
  20. # define DRW_WIN
  21. #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
  22. # define DRW_WIN
  23. #elif defined(__MWERKS__) && defined(__INTEL__)
  24. # define DRW_WIN
  25. #else
  26. # define DRW_POSIX
  27. #endif
  28. #ifndef M_PI
  29. #define M_PI 3.141592653589793238462643
  30. #endif
  31. #ifndef M_PI_2
  32. #define M_PI_2 1.57079632679489661923
  33. #endif
  34. #define M_PIx2 6.283185307179586 // 2*PI
  35. #define ARAD 57.29577951308232
  36. namespace DRW {
  37. // ! Version numbers for the DXF Format.
  38. enum Version
  39. {
  40. UNKNOWNV, /*!< UNKNOWN VERSION. */
  41. AC1006, /*!< R10. */
  42. AC1009, /*!< R11 & R12. */
  43. AC1012, /*!< R13. */
  44. AC1014, /*!< R14. */
  45. AC1015, /*!< ACAD 2000. */
  46. AC1018, /*!< ACAD 2004. */
  47. AC1021, /*!< ACAD 2007. */
  48. AC1024 /*!< ACAD 2010. */
  49. };
  50. enum error
  51. {
  52. BAD_NONE, /*!< No error. */
  53. BAD_UNKNOWN, /*!< UNKNOWN. */
  54. BAD_OPEN, /*!< error opening file. */
  55. BAD_VERSION, /*!< unsupported version. */
  56. BAD_READ_FILE_HEADER, /*!< error in file header read process. */
  57. BAD_READ_HEADER, /*!< error in header vars read process. */
  58. BAD_READ_OFFSETS, /*!< error in object map read process. */
  59. BAD_READ_CLASSES, /*!< error in classes read process. */
  60. BAD_READ_TABLES, /*!< error in tables read process. */
  61. BAD_READ_ENTITIES /*!< error in entities read process. */
  62. };
  63. }
  64. // ! Class to handle 3D coordinate point
  65. /*!
  66. * Class to handle 3D coordinate point
  67. * @author Rallaz
  68. */
  69. class DRW_Coord
  70. {
  71. public:
  72. DRW_Coord(): x(0), y(0), z(0) {}
  73. DRW_Coord( double ix, double iy, double iz )
  74. {
  75. x = ix; y = iy; z = iz;
  76. }
  77. DRW_Coord operator =( const DRW_Coord& data )
  78. {
  79. x = data.x; y = data.y; z = data.z;
  80. return *this;
  81. }
  82. /*!< convert to unitary vector */
  83. void unitize()
  84. {
  85. double dist;
  86. dist = sqrt( x * x + y * y + z * z );
  87. if( dist > 0.0 )
  88. {
  89. x = x / dist;
  90. y = y / dist;
  91. z = z / dist;
  92. }
  93. }
  94. public:
  95. double x;
  96. double y;
  97. double z;
  98. };
  99. // ! Class to handle vertex
  100. /*!
  101. * Class to handle vertex for lwpolyline entity
  102. * @author Rallaz
  103. */
  104. class DRW_Vertex2D
  105. {
  106. public:
  107. DRW_Vertex2D()
  108. {
  109. // eType = DRW::LWPOLYLINE;
  110. x = y = stawidth = endwidth = bulge = 0.0;
  111. }
  112. DRW_Vertex2D( double sx, double sy, double b )
  113. {
  114. stawidth = endwidth = 0;
  115. x = sx;
  116. y = sy;
  117. bulge = b;
  118. }
  119. public:
  120. double x; /*!< x coordinate, code 10 */
  121. double y; /*!< y coordinate, code 20 */
  122. double stawidth; /*!< Start width, code 40 */
  123. double endwidth; /*!< End width, code 41 */
  124. double bulge; /*!< bulge, code 42 */
  125. };
  126. // ! Class to handle header vars
  127. /*!
  128. * Class to handle header vars
  129. * @author Rallaz
  130. */
  131. class DRW_Variant
  132. {
  133. public:
  134. enum TYPE
  135. {
  136. STRING,
  137. INTEGER,
  138. DOUBLE,
  139. COORD,
  140. INVALID
  141. };
  142. DRW_Variant()
  143. {
  144. type = INVALID;
  145. code = 0;
  146. }
  147. DRW_Variant( const DRW_Variant& d )
  148. {
  149. code = d.code;
  150. type = d.type;
  151. if( d.type == COORD ) vdata = d.vdata;
  152. if( d.type == STRING ) sdata = d.sdata;
  153. content = d.content;
  154. }
  155. DRW_Variant( int c, UTF8STRING s ) { addString( s ); code = c; }
  156. DRW_Variant( int c, int i ) { addInt( i ); code = c; }
  157. DRW_Variant( int c, double d ) { addDouble( d ); code = c; }
  158. DRW_Variant( int c, double x, double y, double z )
  159. {
  160. setType( COORD ); vdata.x = x; vdata.y = y;
  161. vdata.z = z; content.v = &vdata; code = c;
  162. }
  163. ~DRW_Variant()
  164. {
  165. }
  166. void addString( UTF8STRING s ) { setType( STRING ); sdata = s; content.s = &sdata; }
  167. void addInt( int i ) { setType( INTEGER ); content.i = i; }
  168. void addDouble( double d ) { setType( DOUBLE ); content.d = d; }
  169. void addCoord()
  170. {
  171. setType( COORD ); vdata.x = 0.0; vdata.y = 0.0; vdata.z = 0.0; content.v =
  172. &vdata;
  173. }
  174. void addCoord( DRW_Coord v ) { setType( COORD ); vdata = v; content.v = &vdata; }
  175. void setType( enum TYPE t ) { type = t; }
  176. void setCoordX( double d ) { if( type == COORD ) vdata.x = d; }
  177. void setCoordY( double d ) { if( type == COORD ) vdata.y = d; }
  178. void setCoordZ( double d ) { if( type == COORD ) vdata.z = d; }
  179. private:
  180. typedef union
  181. {
  182. UTF8STRING* s;
  183. int i;
  184. double d;
  185. DRW_Coord* v;
  186. } DRW_VarContent;
  187. public:
  188. DRW_VarContent content;
  189. enum TYPE type;
  190. int code; /*!< dxf code of this value*/
  191. private:
  192. std::string sdata;
  193. DRW_Coord vdata;
  194. };
  195. // ! Class to convert between line width and integer
  196. /*!
  197. * Class to convert between line width and integer
  198. * verifing valid values, if value is not valid
  199. * returns widthDefault.
  200. * @author Rallaz
  201. */
  202. class DRW_LW_Conv
  203. {
  204. public:
  205. enum lineWidth
  206. {
  207. width00 = 0, /*!< 0.00mm (dxf 0)*/
  208. width01 = 1, /*!< 0.05mm (dxf 5)*/
  209. width02 = 2, /*!< 0.09mm (dxf 9)*/
  210. width03 = 3, /*!< 0.13mm (dxf 13)*/
  211. width04 = 4, /*!< 0.15mm (dxf 15)*/
  212. width05 = 5, /*!< 0.18mm (dxf 18)*/
  213. width06 = 6, /*!< 0.20mm (dxf 20)*/
  214. width07 = 7, /*!< 0.25mm (dxf 25)*/
  215. width08 = 8, /*!< 0.30mm (dxf 30)*/
  216. width09 = 9, /*!< 0.35mm (dxf 35)*/
  217. width10 = 10, /*!< 0.40mm (dxf 40)*/
  218. width11 = 11, /*!< 0.50mm (dxf 50)*/
  219. width12 = 12, /*!< 0.53mm (dxf 53)*/
  220. width13 = 13, /*!< 0.60mm (dxf 60)*/
  221. width14 = 14, /*!< 0.70mm (dxf 70)*/
  222. width15 = 15, /*!< 0.80mm (dxf 80)*/
  223. width16 = 16, /*!< 0.90mm (dxf 90)*/
  224. width17 = 17, /*!< 1.00mm (dxf 100)*/
  225. width18 = 18, /*!< 1.06mm (dxf 106)*/
  226. width19 = 19, /*!< 1.20mm (dxf 120)*/
  227. width20 = 20, /*!< 1.40mm (dxf 140)*/
  228. width21 = 21, /*!< 1.58mm (dxf 158)*/
  229. width22 = 22, /*!< 2.00mm (dxf 200)*/
  230. width23 = 23, /*!< 2.11mm (dxf 211)*/
  231. widthByLayer = 29, /*!< by layer (dxf -1) */
  232. widthByBlock = 30, /*!< by block (dxf -2) */
  233. widthDefault = 31 /*!< by default (dxf -3) */
  234. };
  235. static int lineWidth2dxfInt( enum lineWidth lw )
  236. {
  237. switch( lw )
  238. {
  239. case widthByLayer:
  240. return -1;
  241. case widthByBlock:
  242. return -2;
  243. case widthDefault:
  244. return -3;
  245. case width00:
  246. return 0;
  247. case width01:
  248. return 5;
  249. case width02:
  250. return 9;
  251. case width03:
  252. return 13;
  253. case width04:
  254. return 15;
  255. case width05:
  256. return 18;
  257. case width06:
  258. return 20;
  259. case width07:
  260. return 25;
  261. case width08:
  262. return 30;
  263. case width09:
  264. return 35;
  265. case width10:
  266. return 40;
  267. case width11:
  268. return 50;
  269. case width12:
  270. return 53;
  271. case width13:
  272. return 60;
  273. case width14:
  274. return 70;
  275. case width15:
  276. return 80;
  277. case width16:
  278. return 90;
  279. case width17:
  280. return 100;
  281. case width18:
  282. return 106;
  283. case width19:
  284. return 120;
  285. case width20:
  286. return 140;
  287. case width21:
  288. return 158;
  289. case width22:
  290. return 200;
  291. case width23:
  292. return 211;
  293. default:
  294. return -3;
  295. }
  296. return static_cast<int> (lw);
  297. }
  298. static int lineWidth2dwgInt( enum lineWidth lw )
  299. {
  300. return static_cast<int> (lw);
  301. }
  302. static enum lineWidth dxfInt2lineWidth( int i )
  303. {
  304. if( i<0 )
  305. {
  306. if( i==-1 )
  307. return widthByLayer;
  308. else if( i==-2 )
  309. return widthByBlock;
  310. else if( i==-3 )
  311. return widthDefault;
  312. }
  313. else if( i<3 )
  314. {
  315. return width00;
  316. }
  317. else if( i<7 )
  318. {
  319. return width01;
  320. }
  321. else if( i<11 )
  322. {
  323. return width02;
  324. }
  325. else if( i<14 )
  326. {
  327. return width03;
  328. }
  329. else if( i<16 )
  330. {
  331. return width04;
  332. }
  333. else if( i<19 )
  334. {
  335. return width05;
  336. }
  337. else if( i<22 )
  338. {
  339. return width06;
  340. }
  341. else if( i<27 )
  342. {
  343. return width07;
  344. }
  345. else if( i<32 )
  346. {
  347. return width08;
  348. }
  349. else if( i<37 )
  350. {
  351. return width09;
  352. }
  353. else if( i<45 )
  354. {
  355. return width10;
  356. }
  357. else if( i<52 )
  358. {
  359. return width11;
  360. }
  361. else if( i<57 )
  362. {
  363. return width12;
  364. }
  365. else if( i<65 )
  366. {
  367. return width13;
  368. }
  369. else if( i<75 )
  370. {
  371. return width14;
  372. }
  373. else if( i<85 )
  374. {
  375. return width15;
  376. }
  377. else if( i<95 )
  378. {
  379. return width16;
  380. }
  381. else if( i<103 )
  382. {
  383. return width17;
  384. }
  385. else if( i<112 )
  386. {
  387. return width18;
  388. }
  389. else if( i<130 )
  390. {
  391. return width19;
  392. }
  393. else if( i<149 )
  394. {
  395. return width20;
  396. }
  397. else if( i<180 )
  398. {
  399. return width21;
  400. }
  401. else if( i<205 )
  402. {
  403. return width22;
  404. }
  405. else
  406. {
  407. return width23;
  408. }
  409. // default by default
  410. return widthDefault;
  411. }
  412. static enum lineWidth dwgInt2lineWidth( int i )
  413. {
  414. if( (i>-1 && i<24) || (i>28 && i<32) )
  415. {
  416. return static_cast<lineWidth> (i);
  417. }
  418. // default by default
  419. return widthDefault;
  420. }
  421. };
  422. #endif
  423. // EOF