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.

3985 lines
97 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. #include "libdxfrw.h"
  13. #include <fstream>
  14. #include <algorithm>
  15. #include <sstream>
  16. #include "intern/drw_textcodec.h"
  17. #include "intern/dxfreader.h"
  18. #include "intern/dxfwriter.h"
  19. #include <assert.h>
  20. #ifdef DRW_DBG
  21. #include <iostream> // for debug
  22. #define DBG( a ) std::cerr << a
  23. #else
  24. #define DBG( a )
  25. #endif
  26. #define FIRSTHANDLE 48
  27. /*enum sections {
  28. * secUnknown,
  29. * secHeader,
  30. * secTables,
  31. * secBlocks,
  32. * secEntities,
  33. * secObjects
  34. * };*/
  35. dxfRW::dxfRW( const char* name )
  36. {
  37. fileName = name;
  38. reader = NULL;
  39. writer = NULL;
  40. applyExt = false;
  41. elParts = 128; // parts munber when convert ellipse to polyline
  42. version = DRW::UNKNOWNV;
  43. binary = false;
  44. iface = NULL;
  45. entCount = 0;
  46. wlayer0 = false;
  47. dimstyleStd = false;
  48. writingBlock = false;
  49. currHandle = 0;
  50. }
  51. dxfRW::~dxfRW()
  52. {
  53. if( reader != NULL )
  54. delete reader;
  55. if( writer != NULL )
  56. delete writer;
  57. for( std::vector<DRW_ImageDef*>::iterator it = imageDef.begin(); it!=imageDef.end(); ++it )
  58. delete *it;
  59. imageDef.clear();
  60. }
  61. bool dxfRW::read( DRW_Interface* interface_, bool ext )
  62. {
  63. assert( fileName.empty() == false );
  64. bool isOk = false;
  65. applyExt = ext;
  66. std::ifstream filestr;
  67. if( interface_ == NULL )
  68. return isOk;
  69. DBG( "dxfRW::read 1def\n" );
  70. filestr.open( fileName.c_str(), std::ios_base::in | std::ios::binary );
  71. if( !filestr.is_open() )
  72. return isOk;
  73. if( !filestr.good() )
  74. return isOk;
  75. char line[22];
  76. char line2[22] = "AutoCAD Binary DXF\r\n";
  77. line2[20] = (char) 26;
  78. line2[21] = '\0';
  79. filestr.read( line, 22 );
  80. filestr.close();
  81. iface = interface_;
  82. DBG( "dxfRW::read 2\n" );
  83. if( strcmp( line, line2 ) == 0 )
  84. {
  85. filestr.open( fileName.c_str(), std::ios_base::in | std::ios::binary );
  86. binary = true;
  87. // skip sentinel
  88. filestr.seekg( 22, std::ios::beg );
  89. reader = new dxfReaderBinary( &filestr );
  90. DBG( "dxfRW::read binary file\n" );
  91. }
  92. else
  93. {
  94. binary = false;
  95. filestr.open( fileName.c_str(), std::ios_base::in );
  96. reader = new dxfReaderAscii( &filestr );
  97. }
  98. isOk = processDxf();
  99. filestr.close();
  100. delete reader;
  101. reader = NULL;
  102. return isOk;
  103. }
  104. bool dxfRW::write( DRW_Interface* interface_, DRW::Version ver, bool bin )
  105. {
  106. bool isOk = false;
  107. std::ofstream filestr;
  108. version = ver;
  109. binary = bin;
  110. iface = interface_;
  111. if( binary )
  112. {
  113. filestr.open( fileName.c_str(), std::ios_base::out | std::ios::binary | std::ios::trunc );
  114. // write sentinel
  115. filestr << "AutoCAD Binary DXF\r\n" << (char) 26 << '\0';
  116. writer = new dxfWriterBinary( &filestr );
  117. DBG( "dxfRW::read binary file\n" );
  118. }
  119. else
  120. {
  121. filestr.open( fileName.c_str(), std::ios_base::out | std::ios::trunc );
  122. writer = new dxfWriterAscii( &filestr );
  123. std::string comm = std::string( "dxfrw " ) + std::string( DRW_VERSION );
  124. writer->writeString( 999, comm );
  125. }
  126. DRW_Header header;
  127. iface->writeHeader( header );
  128. writer->writeString( 0, "SECTION" );
  129. entCount = FIRSTHANDLE;
  130. header.write( writer, version );
  131. writer->writeString( 0, "ENDSEC" );
  132. writer->writeString( 0, "SECTION" );
  133. writer->writeString( 2, "CLASSES" );
  134. writer->writeString( 0, "ENDSEC" );
  135. writer->writeString( 0, "SECTION" );
  136. writer->writeString( 2, "TABLES" );
  137. writeTables();
  138. writer->writeString( 0, "ENDSEC" );
  139. writer->writeString( 0, "SECTION" );
  140. writer->writeString( 2, "BLOCKS" );
  141. writeBlocks();
  142. writer->writeString( 0, "ENDSEC" );
  143. writer->writeString( 0, "SECTION" );
  144. writer->writeString( 2, "ENTITIES" );
  145. iface->writeEntities();
  146. writer->writeString( 0, "ENDSEC" );
  147. if( version > DRW::AC1009 )
  148. {
  149. writer->writeString( 0, "SECTION" );
  150. writer->writeString( 2, "OBJECTS" );
  151. writeObjects();
  152. writer->writeString( 0, "ENDSEC" );
  153. }
  154. writer->writeString( 0, "EOF" );
  155. filestr.flush();
  156. filestr.close();
  157. isOk = true;
  158. delete writer;
  159. writer = NULL;
  160. return isOk;
  161. }
  162. bool dxfRW::writeEntity( DRW_Entity* ent )
  163. {
  164. ent->handle = ++entCount;
  165. writer->writeString( 5, toHexStr( ent->handle ) );
  166. if( version > DRW::AC1009 )
  167. {
  168. writer->writeString( 100, "AcDbEntity" );
  169. }
  170. if( ent->space == 1 )
  171. writer->writeInt16( 67, 1 );
  172. if( version > DRW::AC1009 )
  173. {
  174. writer->writeUtf8String( 8, ent->layer );
  175. writer->writeUtf8String( 6, ent->lineType );
  176. }
  177. else
  178. {
  179. writer->writeUtf8Caps( 8, ent->layer );
  180. writer->writeUtf8Caps( 6, ent->lineType );
  181. }
  182. writer->writeInt16( 62, ent->color );
  183. if( version > DRW::AC1015 && ent->color24 >= 0 )
  184. {
  185. writer->writeInt32( 420, ent->color24 );
  186. }
  187. if( version > DRW::AC1014 )
  188. {
  189. writer->writeInt16( 370, DRW_LW_Conv::lineWidth2dxfInt( ent->lWeight ) );
  190. }
  191. return true;
  192. }
  193. bool dxfRW::writeLineType( DRW_LType* ent )
  194. {
  195. std::string strname = ent->name;
  196. transform( strname.begin(), strname.end(), strname.begin(), ::toupper );
  197. // do not write linetypes handled by library
  198. if( strname == "BYLAYER" || strname == "BYBLOCK" || strname == "CONTINUOUS" )
  199. {
  200. return true;
  201. }
  202. writer->writeString( 0, "LTYPE" );
  203. if( version > DRW::AC1009 )
  204. {
  205. writer->writeString( 5, toHexStr( ++entCount ) );
  206. if( version > DRW::AC1012 )
  207. {
  208. writer->writeString( 330, "5" );
  209. }
  210. writer->writeString( 100, "AcDbSymbolTableRecord" );
  211. writer->writeString( 100, "AcDbLinetypeTableRecord" );
  212. writer->writeUtf8String( 2, ent->name );
  213. }
  214. else
  215. writer->writeUtf8Caps( 2, ent->name );
  216. writer->writeInt16( 70, ent->flags );
  217. writer->writeUtf8String( 3, ent->desc );
  218. ent->update();
  219. writer->writeInt16( 72, 65 );
  220. writer->writeInt16( 73, ent->size );
  221. writer->writeDouble( 40, ent->length );
  222. for( unsigned int i = 0; i< ent->path.size(); i++ )
  223. {
  224. writer->writeDouble( 49, ent->path.at( i ) );
  225. if( version > DRW::AC1009 )
  226. {
  227. writer->writeInt16( 74, 0 );
  228. }
  229. }
  230. return true;
  231. }
  232. bool dxfRW::writeLayer( DRW_Layer* ent )
  233. {
  234. writer->writeString( 0, "LAYER" );
  235. if( !wlayer0 && ent->name == "0" )
  236. {
  237. wlayer0 = true;
  238. if( version > DRW::AC1009 )
  239. {
  240. writer->writeString( 5, "10" );
  241. }
  242. }
  243. else
  244. {
  245. if( version > DRW::AC1009 )
  246. {
  247. writer->writeString( 5, toHexStr( ++entCount ) );
  248. }
  249. }
  250. if( version > DRW::AC1012 )
  251. {
  252. writer->writeString( 330, "2" );
  253. }
  254. if( version > DRW::AC1009 )
  255. {
  256. writer->writeString( 100, "AcDbSymbolTableRecord" );
  257. writer->writeString( 100, "AcDbLayerTableRecord" );
  258. writer->writeUtf8String( 2, ent->name );
  259. }
  260. else
  261. {
  262. writer->writeUtf8Caps( 2, ent->name );
  263. }
  264. writer->writeInt16( 70, ent->flags );
  265. writer->writeInt16( 62, ent->color );
  266. if( version > DRW::AC1015 && ent->color24 >= 0 )
  267. {
  268. writer->writeInt32( 420, ent->color24 );
  269. }
  270. if( version > DRW::AC1009 )
  271. {
  272. writer->writeUtf8String( 6, ent->lineType );
  273. if( !ent->plotF )
  274. writer->writeBool( 290, ent->plotF );
  275. writer->writeInt16( 370, DRW_LW_Conv::lineWidth2dxfInt( ent->lWeight ) );
  276. writer->writeString( 390, "F" );
  277. }
  278. else
  279. writer->writeUtf8Caps( 6, ent->lineType );
  280. if( !ent->extData.empty() )
  281. {
  282. writeExtData( ent->extData );
  283. }
  284. // writer->writeString(347, "10012");
  285. return true;
  286. }
  287. bool dxfRW::writeTextstyle( DRW_Textstyle* ent )
  288. {
  289. writer->writeString( 0, "STYLE" );
  290. if( !dimstyleStd )
  291. {
  292. // stringstream cause crash in OS/X, bug#3597944
  293. std::string name = ent->name;
  294. transform( name.begin(), name.end(), name.begin(), toupper );
  295. if( name == "STANDARD" )
  296. dimstyleStd = true;
  297. }
  298. if( version > DRW::AC1009 )
  299. {
  300. writer->writeString( 5, toHexStr( ++entCount ) );
  301. }
  302. if( version > DRW::AC1012 )
  303. {
  304. writer->writeString( 330, "2" );
  305. }
  306. if( version > DRW::AC1009 )
  307. {
  308. writer->writeString( 100, "AcDbSymbolTableRecord" );
  309. writer->writeString( 100, "AcDbTextStyleTableRecord" );
  310. writer->writeUtf8String( 2, ent->name );
  311. }
  312. else
  313. {
  314. writer->writeUtf8Caps( 2, ent->name );
  315. }
  316. writer->writeInt16( 70, ent->flags );
  317. writer->writeDouble( 40, ent->height );
  318. writer->writeDouble( 41, ent->width );
  319. writer->writeDouble( 50, ent->oblique );
  320. writer->writeInt16( 71, ent->genFlag );
  321. writer->writeDouble( 42, ent->lastHeight );
  322. if( version > DRW::AC1009 )
  323. {
  324. writer->writeUtf8String( 3, ent->font );
  325. writer->writeUtf8String( 4, ent->bigFont );
  326. if( ent->fontFamily != 0 )
  327. writer->writeInt32( 1071, ent->fontFamily );
  328. }
  329. else
  330. {
  331. writer->writeUtf8Caps( 3, ent->font );
  332. writer->writeUtf8Caps( 4, ent->bigFont );
  333. }
  334. return true;
  335. }
  336. bool dxfRW::writeVport( DRW_Vport* ent )
  337. {
  338. if( !dimstyleStd )
  339. {
  340. ent->name = "*ACTIVE";
  341. dimstyleStd = true;
  342. }
  343. writer->writeString( 0, "VPORT" );
  344. if( version > DRW::AC1009 )
  345. {
  346. writer->writeString( 5, toHexStr( ++entCount ) );
  347. if( version > DRW::AC1012 )
  348. writer->writeString( 330, "2" );
  349. writer->writeString( 100, "AcDbSymbolTableRecord" );
  350. writer->writeString( 100, "AcDbViewportTableRecord" );
  351. writer->writeUtf8String( 2, ent->name );
  352. }
  353. else
  354. writer->writeUtf8Caps( 2, ent->name );
  355. writer->writeInt16( 70, ent->flags );
  356. writer->writeDouble( 10, ent->lowerLeft.x );
  357. writer->writeDouble( 20, ent->lowerLeft.y );
  358. writer->writeDouble( 11, ent->UpperRight.x );
  359. writer->writeDouble( 21, ent->UpperRight.y );
  360. writer->writeDouble( 12, ent->center.x );
  361. writer->writeDouble( 22, ent->center.y );
  362. writer->writeDouble( 13, ent->snapBase.x );
  363. writer->writeDouble( 23, ent->snapBase.y );
  364. writer->writeDouble( 14, ent->snapSpacing.x );
  365. writer->writeDouble( 24, ent->snapSpacing.y );
  366. writer->writeDouble( 15, ent->gridSpacing.x );
  367. writer->writeDouble( 25, ent->gridSpacing.y );
  368. writer->writeDouble( 16, ent->viewDir.x );
  369. writer->writeDouble( 26, ent->viewDir.y );
  370. writer->writeDouble( 36, ent->viewDir.z );
  371. writer->writeDouble( 17, ent->viewTarget.z );
  372. writer->writeDouble( 27, ent->viewTarget.z );
  373. writer->writeDouble( 37, ent->viewTarget.z );
  374. writer->writeDouble( 40, ent->height );
  375. writer->writeDouble( 41, ent->ratio );
  376. writer->writeDouble( 42, ent->lensHeight );
  377. writer->writeDouble( 43, ent->frontClip );
  378. writer->writeDouble( 44, ent->backClip );
  379. writer->writeDouble( 50, ent->snapAngle );
  380. writer->writeDouble( 51, ent->twistAngle );
  381. writer->writeInt16( 71, ent->viewMode );
  382. writer->writeInt16( 72, ent->circleZoom );
  383. writer->writeInt16( 73, ent->fastZoom );
  384. writer->writeInt16( 74, ent->ucsIcon );
  385. writer->writeInt16( 75, ent->snap );
  386. writer->writeInt16( 76, ent->grid );
  387. writer->writeInt16( 77, ent->snapStyle );
  388. writer->writeInt16( 78, ent->snapIsopair );
  389. if( version > DRW::AC1014 )
  390. {
  391. writer->writeInt16( 281, 0 );
  392. writer->writeInt16( 65, 1 );
  393. writer->writeDouble( 110, 0.0 );
  394. writer->writeDouble( 120, 0.0 );
  395. writer->writeDouble( 130, 0.0 );
  396. writer->writeDouble( 111, 1.0 );
  397. writer->writeDouble( 121, 0.0 );
  398. writer->writeDouble( 131, 0.0 );
  399. writer->writeDouble( 112, 0.0 );
  400. writer->writeDouble( 122, 1.0 );
  401. writer->writeDouble( 132, 0.0 );
  402. writer->writeInt16( 79, 0 );
  403. writer->writeDouble( 146, 0.0 );
  404. if( version > DRW::AC1018 )
  405. {
  406. writer->writeString( 348, "10020" );
  407. writer->writeInt16( 60, ent->gridBehavior ); // v2007 undocummented see DRW_Vport class
  408. writer->writeInt16( 61, 5 );
  409. writer->writeBool( 292, 1 );
  410. writer->writeInt16( 282, 1 );
  411. writer->writeDouble( 141, 0.0 );
  412. writer->writeDouble( 142, 0.0 );
  413. writer->writeInt16( 63, 250 );
  414. writer->writeInt32( 421, 3358443 );
  415. }
  416. }
  417. return true;
  418. }
  419. bool dxfRW::writeDimstyle( DRW_Dimstyle* ent )
  420. {
  421. writer->writeString( 0, "DIMSTYLE" );
  422. if( !dimstyleStd )
  423. {
  424. std::string name = ent->name;
  425. std::transform( name.begin(), name.end(), name.begin(), ::toupper );
  426. if( name == "STANDARD" )
  427. dimstyleStd = true;
  428. }
  429. if( version > DRW::AC1009 )
  430. {
  431. writer->writeString( 105, toHexStr( ++entCount ) );
  432. }
  433. if( version > DRW::AC1012 )
  434. {
  435. writer->writeString( 330, "A" );
  436. }
  437. if( version > DRW::AC1009 )
  438. {
  439. writer->writeString( 100, "AcDbSymbolTableRecord" );
  440. writer->writeString( 100, "AcDbDimStyleTableRecord" );
  441. writer->writeUtf8String( 2, ent->name );
  442. }
  443. else
  444. writer->writeUtf8Caps( 2, ent->name );
  445. writer->writeInt16( 70, ent->flags );
  446. if( version == DRW::AC1009 || !( ent->dimpost.empty() ) )
  447. writer->writeUtf8String( 3, ent->dimpost );
  448. if( version == DRW::AC1009 || !( ent->dimapost.empty() ) )
  449. writer->writeUtf8String( 4, ent->dimapost );
  450. if( version == DRW::AC1009 || !( ent->dimblk.empty() ) )
  451. writer->writeUtf8String( 5, ent->dimblk );
  452. if( version == DRW::AC1009 || !( ent->dimblk1.empty() ) )
  453. writer->writeUtf8String( 6, ent->dimblk1 );
  454. if( version == DRW::AC1009 || !( ent->dimblk2.empty() ) )
  455. writer->writeUtf8String( 7, ent->dimblk2 );
  456. writer->writeDouble( 40, ent->dimscale );
  457. writer->writeDouble( 41, ent->dimasz );
  458. writer->writeDouble( 42, ent->dimexo );
  459. writer->writeDouble( 43, ent->dimdli );
  460. writer->writeDouble( 44, ent->dimexe );
  461. writer->writeDouble( 45, ent->dimrnd );
  462. writer->writeDouble( 46, ent->dimdle );
  463. writer->writeDouble( 47, ent->dimtp );
  464. writer->writeDouble( 48, ent->dimtm );
  465. writer->writeDouble( 140, ent->dimtxt );
  466. writer->writeDouble( 141, ent->dimcen );
  467. writer->writeDouble( 142, ent->dimtsz );
  468. writer->writeDouble( 143, ent->dimaltf );
  469. writer->writeDouble( 144, ent->dimlfac );
  470. writer->writeDouble( 145, ent->dimtvp );
  471. writer->writeDouble( 146, ent->dimtfac );
  472. writer->writeDouble( 147, ent->dimgap );
  473. if( version > DRW::AC1014 )
  474. {
  475. writer->writeDouble( 148, ent->dimaltrnd );
  476. }
  477. writer->writeInt16( 71, ent->dimtol );
  478. writer->writeInt16( 72, ent->dimlim );
  479. writer->writeInt16( 73, ent->dimtih );
  480. writer->writeInt16( 74, ent->dimtoh );
  481. writer->writeInt16( 75, ent->dimse1 );
  482. writer->writeInt16( 76, ent->dimse2 );
  483. writer->writeInt16( 77, ent->dimtad );
  484. writer->writeInt16( 78, ent->dimzin );
  485. if( version > DRW::AC1014 )
  486. {
  487. writer->writeInt16( 79, ent->dimazin );
  488. }
  489. writer->writeInt16( 170, ent->dimalt );
  490. writer->writeInt16( 171, ent->dimaltd );
  491. writer->writeInt16( 172, ent->dimtofl );
  492. writer->writeInt16( 173, ent->dimsah );
  493. writer->writeInt16( 174, ent->dimtix );
  494. writer->writeInt16( 175, ent->dimsoxd );
  495. writer->writeInt16( 176, ent->dimclrd );
  496. writer->writeInt16( 177, ent->dimclre );
  497. writer->writeInt16( 178, ent->dimclrt );
  498. if( version > DRW::AC1014 )
  499. {
  500. writer->writeInt16( 179, ent->dimadec );
  501. }
  502. if( version > DRW::AC1009 )
  503. {
  504. if( version < DRW::AC1015 )
  505. writer->writeInt16( 270, ent->dimunit );
  506. writer->writeInt16( 271, ent->dimdec );
  507. writer->writeInt16( 272, ent->dimtdec );
  508. writer->writeInt16( 273, ent->dimaltu );
  509. writer->writeInt16( 274, ent->dimalttd );
  510. writer->writeInt16( 275, ent->dimaunit );
  511. }
  512. if( version > DRW::AC1014 )
  513. {
  514. writer->writeInt16( 276, ent->dimfrac );
  515. writer->writeInt16( 277, ent->dimlunit );
  516. writer->writeInt16( 278, ent->dimdsep );
  517. writer->writeInt16( 279, ent->dimtmove );
  518. }
  519. if( version > DRW::AC1009 )
  520. {
  521. writer->writeInt16( 280, ent->dimjust );
  522. writer->writeInt16( 281, ent->dimsd1 );
  523. writer->writeInt16( 282, ent->dimsd2 );
  524. writer->writeInt16( 283, ent->dimtolj );
  525. writer->writeInt16( 284, ent->dimtzin );
  526. writer->writeInt16( 285, ent->dimaltz );
  527. writer->writeInt16( 286, ent->dimaltttz );
  528. if( version < DRW::AC1015 )
  529. writer->writeInt16( 287, ent->dimfit );
  530. writer->writeInt16( 288, ent->dimupt );
  531. }
  532. if( version > DRW::AC1014 )
  533. {
  534. writer->writeInt16( 289, ent->dimatfit );
  535. }
  536. if( version > DRW::AC1009 )
  537. {
  538. writer->writeUtf8String( 340, ent->dimtxsty );
  539. }
  540. if( version > DRW::AC1014 )
  541. {
  542. writer->writeUtf8String( 341, ent->dimldrblk );
  543. writer->writeInt16( 371, ent->dimlwd );
  544. writer->writeInt16( 372, ent->dimlwe );
  545. }
  546. return true;
  547. }
  548. bool dxfRW::writeAppId( DRW_AppId* ent )
  549. {
  550. writer->writeString( 0, "APPID" );
  551. if( version > DRW::AC1009 )
  552. {
  553. writer->writeString( 5, toHexStr( ++entCount ) );
  554. if( version > DRW::AC1014 )
  555. {
  556. writer->writeString( 330, "9" );
  557. }
  558. writer->writeString( 100, "AcDbSymbolTableRecord" );
  559. writer->writeString( 100, "AcDbRegAppTableRecord" );
  560. writer->writeUtf8String( 2, ent->name );
  561. }
  562. else
  563. {
  564. writer->writeUtf8Caps( 2, ent->name );
  565. }
  566. writer->writeInt16( 70, ent->flags );
  567. return true;
  568. }
  569. bool dxfRW::writePoint( DRW_Point* ent )
  570. {
  571. writer->writeString( 0, "POINT" );
  572. writeEntity( ent );
  573. if( version > DRW::AC1009 )
  574. {
  575. writer->writeString( 100, "AcDbPoint" );
  576. }
  577. writer->writeDouble( 10, ent->basePoint.x );
  578. writer->writeDouble( 20, ent->basePoint.y );
  579. if( ent->basePoint.z != 0.0 )
  580. {
  581. writer->writeDouble( 30, ent->basePoint.z );
  582. }
  583. return true;
  584. }
  585. bool dxfRW::writeLine( DRW_Line* ent )
  586. {
  587. writer->writeString( 0, "LINE" );
  588. writeEntity( ent );
  589. if( version > DRW::AC1009 )
  590. {
  591. writer->writeString( 100, "AcDbLine" );
  592. }
  593. writer->writeDouble( 10, ent->basePoint.x );
  594. writer->writeDouble( 20, ent->basePoint.y );
  595. if( ent->basePoint.z != 0.0 || ent->secPoint.z != 0.0 )
  596. {
  597. writer->writeDouble( 30, ent->basePoint.z );
  598. writer->writeDouble( 11, ent->secPoint.x );
  599. writer->writeDouble( 21, ent->secPoint.y );
  600. writer->writeDouble( 31, ent->secPoint.z );
  601. }
  602. else
  603. {
  604. writer->writeDouble( 11, ent->secPoint.x );
  605. writer->writeDouble( 21, ent->secPoint.y );
  606. }
  607. return true;
  608. }
  609. bool dxfRW::writeRay( DRW_Ray* ent )
  610. {
  611. writer->writeString( 0, "RAY" );
  612. writeEntity( ent );
  613. if( version > DRW::AC1009 )
  614. {
  615. writer->writeString( 100, "AcDbRay" );
  616. }
  617. DRW_Coord crd = ent->secPoint;
  618. crd.unitize();
  619. writer->writeDouble( 10, ent->basePoint.x );
  620. writer->writeDouble( 20, ent->basePoint.y );
  621. if( ent->basePoint.z != 0.0 || ent->secPoint.z != 0.0 )
  622. {
  623. writer->writeDouble( 30, ent->basePoint.z );
  624. writer->writeDouble( 11, crd.x );
  625. writer->writeDouble( 21, crd.y );
  626. writer->writeDouble( 31, crd.z );
  627. }
  628. else
  629. {
  630. writer->writeDouble( 11, crd.x );
  631. writer->writeDouble( 21, crd.y );
  632. }
  633. return true;
  634. }
  635. bool dxfRW::writeXline( DRW_Xline* ent )
  636. {
  637. writer->writeString( 0, "XLINE" );
  638. writeEntity( ent );
  639. if( version > DRW::AC1009 )
  640. {
  641. writer->writeString( 100, "AcDbXline" );
  642. }
  643. DRW_Coord crd = ent->secPoint;
  644. crd.unitize();
  645. writer->writeDouble( 10, ent->basePoint.x );
  646. writer->writeDouble( 20, ent->basePoint.y );
  647. if( ent->basePoint.z != 0.0 || ent->secPoint.z != 0.0 )
  648. {
  649. writer->writeDouble( 30, ent->basePoint.z );
  650. writer->writeDouble( 11, crd.x );
  651. writer->writeDouble( 21, crd.y );
  652. writer->writeDouble( 31, crd.z );
  653. }
  654. else
  655. {
  656. writer->writeDouble( 11, crd.x );
  657. writer->writeDouble( 21, crd.y );
  658. }
  659. return true;
  660. }
  661. bool dxfRW::writeCircle( DRW_Circle* ent )
  662. {
  663. writer->writeString( 0, "CIRCLE" );
  664. writeEntity( ent );
  665. if( version > DRW::AC1009 )
  666. {
  667. writer->writeString( 100, "AcDbCircle" );
  668. }
  669. writer->writeDouble( 10, ent->basePoint.x );
  670. writer->writeDouble( 20, ent->basePoint.y );
  671. if( ent->basePoint.z != 0.0 )
  672. {
  673. writer->writeDouble( 30, ent->basePoint.z );
  674. }
  675. writer->writeDouble( 40, ent->radious );
  676. return true;
  677. }
  678. bool dxfRW::writeArc( DRW_Arc* ent )
  679. {
  680. writer->writeString( 0, "ARC" );
  681. writeEntity( ent );
  682. if( version > DRW::AC1009 )
  683. {
  684. writer->writeString( 100, "AcDbCircle" );
  685. }
  686. writer->writeDouble( 10, ent->basePoint.x );
  687. writer->writeDouble( 20, ent->basePoint.y );
  688. if( ent->basePoint.z != 0.0 )
  689. {
  690. writer->writeDouble( 30, ent->basePoint.z );
  691. }
  692. writer->writeDouble( 40, ent->radious );
  693. if( version > DRW::AC1009 )
  694. {
  695. writer->writeString( 100, "AcDbArc" );
  696. }
  697. writer->writeDouble( 50, ent->staangle * ARAD );
  698. writer->writeDouble( 51, ent->endangle * ARAD );
  699. return true;
  700. }
  701. bool dxfRW::writeEllipse( DRW_Ellipse* ent )
  702. {
  703. // verify axis/ratio and params for full ellipse
  704. ent->correctAxis();
  705. if( version > DRW::AC1009 )
  706. {
  707. writer->writeString( 0, "ELLIPSE" );
  708. writeEntity( ent );
  709. if( version > DRW::AC1009 )
  710. {
  711. writer->writeString( 100, "AcDbEllipse" );
  712. }
  713. writer->writeDouble( 10, ent->basePoint.x );
  714. writer->writeDouble( 20, ent->basePoint.y );
  715. writer->writeDouble( 30, ent->basePoint.z );
  716. writer->writeDouble( 11, ent->secPoint.x );
  717. writer->writeDouble( 21, ent->secPoint.y );
  718. writer->writeDouble( 31, ent->secPoint.z );
  719. writer->writeDouble( 40, ent->ratio );
  720. writer->writeDouble( 41, ent->staparam );
  721. writer->writeDouble( 42, ent->endparam );
  722. }
  723. else
  724. {
  725. DRW_Polyline pol;
  726. // RLZ: copy properties
  727. ent->toPolyline( &pol, elParts );
  728. writePolyline( &pol );
  729. }
  730. return true;
  731. }
  732. bool dxfRW::writeTrace( DRW_Trace* ent )
  733. {
  734. writer->writeString( 0, "TRACE" );
  735. writeEntity( ent );
  736. if( version > DRW::AC1009 )
  737. {
  738. writer->writeString( 100, "AcDbTrace" );
  739. }
  740. writer->writeDouble( 10, ent->basePoint.x );
  741. writer->writeDouble( 20, ent->basePoint.y );
  742. writer->writeDouble( 30, ent->basePoint.z );
  743. writer->writeDouble( 11, ent->secPoint.x );
  744. writer->writeDouble( 21, ent->secPoint.y );
  745. writer->writeDouble( 31, ent->secPoint.z );
  746. writer->writeDouble( 12, ent->thirdPoint.x );
  747. writer->writeDouble( 22, ent->thirdPoint.y );
  748. writer->writeDouble( 32, ent->thirdPoint.z );
  749. writer->writeDouble( 13, ent->fourPoint.x );
  750. writer->writeDouble( 23, ent->fourPoint.y );
  751. writer->writeDouble( 33, ent->fourPoint.z );
  752. return true;
  753. }
  754. bool dxfRW::writeSolid( DRW_Solid* ent )
  755. {
  756. writer->writeString( 0, "SOLID" );
  757. writeEntity( ent );
  758. if( version > DRW::AC1009 )
  759. {
  760. writer->writeString( 100, "AcDbTrace" );
  761. }
  762. writer->writeDouble( 10, ent->basePoint.x );
  763. writer->writeDouble( 20, ent->basePoint.y );
  764. writer->writeDouble( 30, ent->basePoint.z );
  765. writer->writeDouble( 11, ent->secPoint.x );
  766. writer->writeDouble( 21, ent->secPoint.y );
  767. writer->writeDouble( 31, ent->secPoint.z );
  768. writer->writeDouble( 12, ent->thirdPoint.x );
  769. writer->writeDouble( 22, ent->thirdPoint.y );
  770. writer->writeDouble( 32, ent->thirdPoint.z );
  771. writer->writeDouble( 13, ent->fourPoint.x );
  772. writer->writeDouble( 23, ent->fourPoint.y );
  773. writer->writeDouble( 33, ent->fourPoint.z );
  774. return true;
  775. }
  776. bool dxfRW::write3dface( DRW_3Dface* ent )
  777. {
  778. writer->writeString( 0, "3DFACE" );
  779. writeEntity( ent );
  780. if( version > DRW::AC1009 )
  781. {
  782. writer->writeString( 100, "AcDbFace" );
  783. }
  784. writer->writeDouble( 10, ent->basePoint.x );
  785. writer->writeDouble( 20, ent->basePoint.y );
  786. writer->writeDouble( 30, ent->basePoint.z );
  787. writer->writeDouble( 11, ent->secPoint.x );
  788. writer->writeDouble( 21, ent->secPoint.y );
  789. writer->writeDouble( 31, ent->secPoint.z );
  790. writer->writeDouble( 12, ent->thirdPoint.x );
  791. writer->writeDouble( 22, ent->thirdPoint.y );
  792. writer->writeDouble( 32, ent->thirdPoint.z );
  793. writer->writeDouble( 13, ent->fourPoint.x );
  794. writer->writeDouble( 23, ent->fourPoint.y );
  795. writer->writeDouble( 33, ent->fourPoint.z );
  796. writer->writeInt16( 70, ent->invisibleflag );
  797. return true;
  798. }
  799. bool dxfRW::writeLWPolyline( DRW_LWPolyline* ent )
  800. {
  801. if( version > DRW::AC1009 )
  802. {
  803. writer->writeString( 0, "LWPOLYLINE" );
  804. writeEntity( ent );
  805. if( version > DRW::AC1009 )
  806. {
  807. writer->writeString( 100, "AcDbPolyline" );
  808. }
  809. ent->vertexnum = ent->vertlist.size();
  810. writer->writeInt32( 90, ent->vertexnum );
  811. writer->writeInt16( 70, ent->flags );
  812. writer->writeDouble( 43, ent->width );
  813. if( ent->elevation != 0 )
  814. writer->writeDouble( 38, ent->elevation );
  815. if( ent->thickness != 0 )
  816. writer->writeDouble( 39, ent->thickness );
  817. for( int i = 0; i< ent->vertexnum; i++ )
  818. {
  819. DRW_Vertex2D* v = ent->vertlist.at( i );
  820. writer->writeDouble( 10, v->x );
  821. writer->writeDouble( 20, v->y );
  822. if( v->stawidth != 0 )
  823. writer->writeDouble( 40, v->stawidth );
  824. if( v->endwidth != 0 )
  825. writer->writeDouble( 41, v->endwidth );
  826. if( v->bulge != 0 )
  827. writer->writeDouble( 42, v->bulge );
  828. }
  829. }
  830. else
  831. {
  832. // RLZ: TODO convert lwpolyline in polyline (not exist in acad 12)
  833. }
  834. return true;
  835. }
  836. bool dxfRW::writePolyline( DRW_Polyline* ent )
  837. {
  838. writer->writeString( 0, "POLYLINE" );
  839. writeEntity( ent );
  840. if( version > DRW::AC1009 )
  841. {
  842. if( ent->flags & 8 || ent->flags & 16 )
  843. writer->writeString( 100, "AcDb2dPolyline" );
  844. else
  845. writer->writeString( 100, "AcDb3dPolyline" );
  846. }
  847. else
  848. writer->writeInt16( 66, 1 );
  849. writer->writeDouble( 10, 0.0 );
  850. writer->writeDouble( 20, 0.0 );
  851. writer->writeDouble( 30, ent->basePoint.z );
  852. if( ent->thickness != 0 )
  853. {
  854. writer->writeDouble( 39, ent->thickness );
  855. }
  856. writer->writeInt16( 70, ent->flags );
  857. if( ent->defstawidth != 0 )
  858. {
  859. writer->writeDouble( 40, ent->defstawidth );
  860. }
  861. if( ent->defendwidth != 0 )
  862. {
  863. writer->writeDouble( 41, ent->defendwidth );
  864. }
  865. if( ent->flags & 16 || ent->flags & 32 )
  866. {
  867. writer->writeInt16( 71, ent->vertexcount );
  868. writer->writeInt16( 72, ent->facecount );
  869. }
  870. if( ent->smoothM != 0 )
  871. {
  872. writer->writeInt16( 73, ent->smoothM );
  873. }
  874. if( ent->smoothN != 0 )
  875. {
  876. writer->writeInt16( 74, ent->smoothN );
  877. }
  878. if( ent->curvetype != 0 )
  879. {
  880. writer->writeInt16( 75, ent->curvetype );
  881. }
  882. DRW_Coord crd = ent->extPoint;
  883. if( crd.x != 0 || crd.y != 0 || crd.z != 1 )
  884. {
  885. writer->writeDouble( 210, crd.x );
  886. writer->writeDouble( 220, crd.y );
  887. writer->writeDouble( 230, crd.z );
  888. }
  889. int vertexnum = ent->vertlist.size();
  890. for( int i = 0; i< vertexnum; i++ )
  891. {
  892. DRW_Vertex* v = ent->vertlist.at( i );
  893. writer->writeString( 0, "VERTEX" );
  894. writeEntity( ent );
  895. if( version > DRW::AC1009 )
  896. writer->writeString( 100, "AcDbVertex" );
  897. if( (v->flags & 128) && !(v->flags & 64) )
  898. {
  899. writer->writeDouble( 10, 0 );
  900. writer->writeDouble( 20, 0 );
  901. writer->writeDouble( 30, 0 );
  902. }
  903. else
  904. {
  905. writer->writeDouble( 10, v->basePoint.x );
  906. writer->writeDouble( 20, v->basePoint.y );
  907. writer->writeDouble( 30, v->basePoint.z );
  908. }
  909. if( v->stawidth != 0 )
  910. writer->writeDouble( 40, v->stawidth );
  911. if( v->endwidth != 0 )
  912. writer->writeDouble( 41, v->endwidth );
  913. if( v->bulge != 0 )
  914. writer->writeDouble( 42, v->bulge );
  915. if( v->flags != 0 )
  916. {
  917. writer->writeInt16( 70, ent->flags );
  918. }
  919. if( v->flags & 2 )
  920. {
  921. writer->writeDouble( 50, v->tgdir );
  922. }
  923. if( v->flags & 128 )
  924. {
  925. if( v->vindex1 != 0 )
  926. {
  927. writer->writeInt16( 71, v->vindex1 );
  928. }
  929. if( v->vindex2 != 0 )
  930. {
  931. writer->writeInt16( 72, v->vindex2 );
  932. }
  933. if( v->vindex3 != 0 )
  934. {
  935. writer->writeInt16( 73, v->vindex3 );
  936. }
  937. if( v->vindex4 != 0 )
  938. {
  939. writer->writeInt16( 74, v->vindex4 );
  940. }
  941. if( !(v->flags & 64) )
  942. {
  943. writer->writeInt32( 91, v->identifier );
  944. }
  945. }
  946. }
  947. writer->writeString( 0, "SEQEND" );
  948. writeEntity( ent );
  949. return true;
  950. }
  951. bool dxfRW::writeSpline( DRW_Spline* ent )
  952. {
  953. if( version > DRW::AC1009 )
  954. {
  955. writer->writeString( 0, "SPLINE" );
  956. writeEntity( ent );
  957. if( version > DRW::AC1009 )
  958. {
  959. writer->writeString( 100, "AcDbSpline" );
  960. }
  961. writer->writeDouble( 210, ent->ex );
  962. writer->writeDouble( 220, ent->ey );
  963. writer->writeDouble( 230, ent->ez );
  964. writer->writeInt16( 70, ent->flags );
  965. writer->writeInt16( 71, ent->degree );
  966. writer->writeInt16( 72, ent->nknots );
  967. writer->writeInt16( 73, ent->ncontrol );
  968. writer->writeInt16( 74, ent->nfit );
  969. writer->writeDouble( 42, ent->tolknot );
  970. writer->writeDouble( 43, ent->tolcontrol );
  971. // RLZ: warning check if nknots are correct and ncontrol
  972. for( int i = 0; i< ent->nknots; i++ )
  973. {
  974. writer->writeDouble( 40, ent->knotslist.at( i ) );
  975. }
  976. for( int i = 0; i< ent->ncontrol; i++ )
  977. {
  978. DRW_Coord* crd = ent->controllist.at( i );
  979. writer->writeDouble( 10, crd->x );
  980. writer->writeDouble( 20, crd->y );
  981. writer->writeDouble( 30, crd->z );
  982. }
  983. }
  984. else
  985. {
  986. // RLZ: TODO convert spline in polyline (not exist in acad 12)
  987. }
  988. return true;
  989. }
  990. bool dxfRW::writeHatch( DRW_Hatch* ent )
  991. {
  992. if( version > DRW::AC1009 )
  993. {
  994. writer->writeString( 0, "HATCH" );
  995. writeEntity( ent );
  996. writer->writeString( 100, "AcDbHatch" );
  997. writer->writeDouble( 10, 0.0 );
  998. writer->writeDouble( 20, 0.0 );
  999. writer->writeDouble( 30, ent->basePoint.z );
  1000. writer->writeDouble( 210, ent->extPoint.x );
  1001. writer->writeDouble( 220, ent->extPoint.y );
  1002. writer->writeDouble( 230, ent->extPoint.z );
  1003. writer->writeString( 2, ent->name );
  1004. writer->writeInt16( 70, ent->solid );
  1005. writer->writeInt16( 71, ent->associative );
  1006. ent->loopsnum = ent->looplist.size();
  1007. writer->writeInt16( 91, ent->loopsnum );
  1008. // write paths data
  1009. for( int i = 0; i< ent->loopsnum; i++ )
  1010. {
  1011. DRW_HatchLoop* loop = ent->looplist.at( i );
  1012. writer->writeInt16( 92, loop->type );
  1013. if( (loop->type & 2) == 2 )
  1014. {
  1015. // RLZ: polyline boundary writeme
  1016. }
  1017. else
  1018. {
  1019. // boundary path
  1020. loop->update();
  1021. writer->writeInt16( 93, loop->numedges );
  1022. for( int j = 0; j<loop->numedges; ++j )
  1023. {
  1024. switch( ( loop->objlist.at( j ) )->eType )
  1025. {
  1026. case DRW::LINE:
  1027. {
  1028. writer->writeInt16( 72, 1 );
  1029. DRW_Line* l = (DRW_Line*) loop->objlist.at( j );
  1030. writer->writeDouble( 10, l->basePoint.x );
  1031. writer->writeDouble( 20, l->basePoint.y );
  1032. writer->writeDouble( 11, l->secPoint.x );
  1033. writer->writeDouble( 21, l->secPoint.y );
  1034. break;
  1035. }
  1036. case DRW::ARC:
  1037. {
  1038. writer->writeInt16( 72, 2 );
  1039. DRW_Arc* a = (DRW_Arc*) loop->objlist.at( j );
  1040. writer->writeDouble( 10, a->basePoint.x );
  1041. writer->writeDouble( 20, a->basePoint.y );
  1042. writer->writeDouble( 40, a->radious );
  1043. writer->writeDouble( 50, a->staangle * ARAD );
  1044. writer->writeDouble( 51, a->endangle * ARAD );
  1045. writer->writeInt16( 73, a->isccw );
  1046. break;
  1047. }
  1048. case DRW::ELLIPSE:
  1049. {
  1050. writer->writeInt16( 72, 3 );
  1051. DRW_Ellipse* a = (DRW_Ellipse*) loop->objlist.at( j );
  1052. a->correctAxis();
  1053. writer->writeDouble( 10, a->basePoint.x );
  1054. writer->writeDouble( 20, a->basePoint.y );
  1055. writer->writeDouble( 11, a->secPoint.x );
  1056. writer->writeDouble( 21, a->secPoint.y );
  1057. writer->writeDouble( 40, a->ratio );
  1058. writer->writeDouble( 50, a->staparam * ARAD );
  1059. writer->writeDouble( 51, a->endparam * ARAD );
  1060. writer->writeInt16( 73, a->isccw );
  1061. break;
  1062. }
  1063. case DRW::SPLINE:
  1064. // RLZ: spline boundary writeme
  1065. // writer->writeInt16(72, 4);
  1066. break;
  1067. default:
  1068. break;
  1069. }
  1070. }
  1071. writer->writeInt16( 97, 0 );
  1072. }
  1073. }
  1074. writer->writeInt16( 75, ent->hstyle );
  1075. writer->writeInt16( 76, ent->hpattern );
  1076. if( !ent->solid )
  1077. {
  1078. writer->writeDouble( 52, ent->angle );
  1079. writer->writeDouble( 41, ent->scale );
  1080. writer->writeInt16( 77, ent->doubleflag );
  1081. writer->writeInt16( 78, ent->deflines );
  1082. }
  1083. /* if (ent->deflines > 0){
  1084. * writer->writeInt16(78, ent->deflines);
  1085. * }*/
  1086. writer->writeInt32( 98, 0 );
  1087. }
  1088. else
  1089. {
  1090. // RLZ: TODO verify in acad12
  1091. }
  1092. return true;
  1093. }
  1094. bool dxfRW::writeLeader( DRW_Leader* ent )
  1095. {
  1096. if( version > DRW::AC1009 )
  1097. {
  1098. writer->writeString( 0, "LEADER" );
  1099. writeEntity( ent );
  1100. writer->writeString( 100, "AcDbLeader" );
  1101. writer->writeUtf8String( 3, ent->style );
  1102. writer->writeInt16( 71, ent->arrow );
  1103. writer->writeInt16( 72, ent->leadertype );
  1104. writer->writeInt16( 73, ent->flag );
  1105. writer->writeInt16( 74, ent->hookline );
  1106. writer->writeInt16( 75, ent->hookflag );
  1107. writer->writeDouble( 40, ent->textheight );
  1108. writer->writeDouble( 41, ent->textwidth );
  1109. writer->writeDouble( 76, ent->vertnum );
  1110. writer->writeDouble( 76, ent->vertexlist.size() );
  1111. for( unsigned int i = 0; i<ent->vertexlist.size(); i++ )
  1112. {
  1113. DRW_Coord* vert = ent->vertexlist.at( i );
  1114. writer->writeDouble( 10, vert->x );
  1115. writer->writeDouble( 20, vert->y );
  1116. writer->writeDouble( 30, vert->z );
  1117. }
  1118. }
  1119. else
  1120. {
  1121. // RLZ: todo not supported by acad 12 saved as unnamed block
  1122. }
  1123. return true;
  1124. }
  1125. bool dxfRW::writeDimension( DRW_Dimension* ent )
  1126. {
  1127. if( version > DRW::AC1009 )
  1128. {
  1129. writer->writeString( 0, "DIMENSION" );
  1130. writeEntity( ent );
  1131. writer->writeString( 100, "AcDbDimension" );
  1132. if( !ent->getName().empty() )
  1133. {
  1134. writer->writeString( 2, ent->getName() );
  1135. }
  1136. writer->writeDouble( 10, ent->getDefPoint().x );
  1137. writer->writeDouble( 20, ent->getDefPoint().y );
  1138. writer->writeDouble( 30, ent->getDefPoint().z );
  1139. writer->writeDouble( 11, ent->getTextPoint().x );
  1140. writer->writeDouble( 21, ent->getTextPoint().y );
  1141. writer->writeDouble( 31, ent->getTextPoint().z );
  1142. if( !(ent->type & 32) )
  1143. ent->type = ent->type + 32;
  1144. writer->writeInt16( 70, ent->type );
  1145. if( !( ent->getText().empty() ) )
  1146. writer->writeUtf8String( 1, ent->getText() );
  1147. writer->writeInt16( 71, ent->getAlign() );
  1148. if( ent->getTextLineStyle() != 1 )
  1149. writer->writeInt16( 72, ent->getTextLineStyle() );
  1150. if( ent->getTextLineFactor() != 1 )
  1151. writer->writeDouble( 41, ent->getTextLineFactor() );
  1152. writer->writeUtf8String( 3, ent->getStyle() );
  1153. if( ent->getDir() != 0 )
  1154. writer->writeDouble( 53, ent->getDir() );
  1155. writer->writeDouble( 210, ent->getExtrusion().x );
  1156. writer->writeDouble( 220, ent->getExtrusion().y );
  1157. writer->writeDouble( 230, ent->getExtrusion().z );
  1158. switch( ent->eType )
  1159. {
  1160. case DRW::DIMALIGNED:
  1161. case DRW::DIMLINEAR:
  1162. {
  1163. DRW_DimAligned* dd = (DRW_DimAligned*) ent;
  1164. writer->writeString( 100, "AcDbAlignedDimension" );
  1165. DRW_Coord crd = dd->getClonepoint();
  1166. if( crd.x != 0 || crd.y != 0 || crd.z != 0 )
  1167. {
  1168. writer->writeDouble( 12, crd.x );
  1169. writer->writeDouble( 22, crd.y );
  1170. writer->writeDouble( 32, crd.z );
  1171. }
  1172. writer->writeDouble( 13, dd->getDef1Point().x );
  1173. writer->writeDouble( 23, dd->getDef1Point().y );
  1174. writer->writeDouble( 33, dd->getDef1Point().z );
  1175. writer->writeDouble( 14, dd->getDef2Point().x );
  1176. writer->writeDouble( 24, dd->getDef2Point().y );
  1177. writer->writeDouble( 34, dd->getDef2Point().z );
  1178. if( ent->eType == DRW::DIMLINEAR )
  1179. {
  1180. DRW_DimLinear* dl = (DRW_DimLinear*) ent;
  1181. if( dl->getAngle() != 0 )
  1182. writer->writeDouble( 50, dl->getAngle() );
  1183. if( dl->getOblique() != 0 )
  1184. writer->writeDouble( 52, dl->getOblique() );
  1185. writer->writeString( 100, "AcDbRotatedDimension" );
  1186. }
  1187. break;
  1188. }
  1189. case DRW::DIMRADIAL:
  1190. {
  1191. DRW_DimRadial* dd = (DRW_DimRadial*) ent;
  1192. writer->writeString( 100, "AcDbRadialDimension" );
  1193. writer->writeDouble( 15, dd->getDiameterPoint().x );
  1194. writer->writeDouble( 25, dd->getDiameterPoint().y );
  1195. writer->writeDouble( 35, dd->getDiameterPoint().z );
  1196. writer->writeDouble( 40, dd->getLeaderLength() );
  1197. break;
  1198. }
  1199. case DRW::DIMDIAMETRIC:
  1200. {
  1201. DRW_DimDiametric* dd = (DRW_DimDiametric*) ent;
  1202. writer->writeString( 100, "AcDbDiametricDimension" );
  1203. writer->writeDouble( 15, dd->getDiameter1Point().x );
  1204. writer->writeDouble( 25, dd->getDiameter1Point().y );
  1205. writer->writeDouble( 35, dd->getDiameter1Point().z );
  1206. writer->writeDouble( 40, dd->getLeaderLength() );
  1207. break;
  1208. }
  1209. case DRW::DIMANGULAR:
  1210. {
  1211. DRW_DimAngular* dd = (DRW_DimAngular*) ent;
  1212. writer->writeString( 100, "AcDb2LineAngularDimension" );
  1213. writer->writeDouble( 13, dd->getFirstLine1().x );
  1214. writer->writeDouble( 23, dd->getFirstLine1().y );
  1215. writer->writeDouble( 33, dd->getFirstLine1().z );
  1216. writer->writeDouble( 14, dd->getFirstLine2().x );
  1217. writer->writeDouble( 24, dd->getFirstLine2().y );
  1218. writer->writeDouble( 34, dd->getFirstLine2().z );
  1219. writer->writeDouble( 15, dd->getSecondLine1().x );
  1220. writer->writeDouble( 25, dd->getSecondLine1().y );
  1221. writer->writeDouble( 35, dd->getSecondLine1().z );
  1222. writer->writeDouble( 16, dd->getDimPoint().x );
  1223. writer->writeDouble( 26, dd->getDimPoint().y );
  1224. writer->writeDouble( 36, dd->getDimPoint().z );
  1225. break;
  1226. }
  1227. case DRW::DIMANGULAR3P:
  1228. {
  1229. DRW_DimAngular3p* dd = (DRW_DimAngular3p*) ent;
  1230. writer->writeDouble( 13, dd->getFirstLine().x );
  1231. writer->writeDouble( 23, dd->getFirstLine().y );
  1232. writer->writeDouble( 33, dd->getFirstLine().z );
  1233. writer->writeDouble( 14, dd->getSecondLine().x );
  1234. writer->writeDouble( 24, dd->getSecondLine().y );
  1235. writer->writeDouble( 34, dd->getSecondLine().z );
  1236. writer->writeDouble( 15, dd->getVertexPoint().x );
  1237. writer->writeDouble( 25, dd->getVertexPoint().y );
  1238. writer->writeDouble( 35, dd->getVertexPoint().z );
  1239. break;
  1240. }
  1241. case DRW::DIMORDINATE:
  1242. {
  1243. DRW_DimOrdinate* dd = (DRW_DimOrdinate*) ent;
  1244. writer->writeString( 100, "AcDbOrdinateDimension" );
  1245. writer->writeDouble( 13, dd->getFirstLine().x );
  1246. writer->writeDouble( 23, dd->getFirstLine().y );
  1247. writer->writeDouble( 33, dd->getFirstLine().z );
  1248. writer->writeDouble( 14, dd->getSecondLine().x );
  1249. writer->writeDouble( 24, dd->getSecondLine().y );
  1250. writer->writeDouble( 34, dd->getSecondLine().z );
  1251. break;
  1252. }
  1253. default:
  1254. break;
  1255. }
  1256. }
  1257. else
  1258. {
  1259. // RLZ: todo not supported by acad 12 saved as unnamed block
  1260. }
  1261. return true;
  1262. }
  1263. bool dxfRW::writeInsert( DRW_Insert* ent )
  1264. {
  1265. writer->writeString( 0, "INSERT" );
  1266. writeEntity( ent );
  1267. if( version > DRW::AC1009 )
  1268. {
  1269. writer->writeString( 100, "AcDbBlockReference" );
  1270. writer->writeUtf8String( 2, ent->name );
  1271. }
  1272. else
  1273. writer->writeUtf8Caps( 2, ent->name );
  1274. writer->writeDouble( 10, ent->basePoint.x );
  1275. writer->writeDouble( 20, ent->basePoint.y );
  1276. writer->writeDouble( 30, ent->basePoint.z );
  1277. writer->writeDouble( 41, ent->xscale );
  1278. writer->writeDouble( 42, ent->yscale );
  1279. writer->writeDouble( 43, ent->zscale );
  1280. writer->writeDouble( 50, ent->angle );
  1281. writer->writeInt16( 70, ent->colcount );
  1282. writer->writeInt16( 71, ent->rowcount );
  1283. writer->writeDouble( 44, ent->colspace );
  1284. writer->writeDouble( 45, ent->rowspace );
  1285. return true;
  1286. }
  1287. bool dxfRW::writeText( DRW_Text* ent )
  1288. {
  1289. writer->writeString( 0, "TEXT" );
  1290. writeEntity( ent );
  1291. if( version > DRW::AC1009 )
  1292. {
  1293. writer->writeString( 100, "AcDbText" );
  1294. }
  1295. // writer->writeDouble(39, ent->thickness);
  1296. writer->writeDouble( 10, ent->basePoint.x );
  1297. writer->writeDouble( 20, ent->basePoint.y );
  1298. writer->writeDouble( 30, ent->basePoint.z );
  1299. writer->writeDouble( 40, ent->height );
  1300. writer->writeUtf8String( 1, ent->text );
  1301. writer->writeDouble( 50, ent->angle );
  1302. writer->writeDouble( 41, ent->widthscale );
  1303. writer->writeDouble( 51, ent->oblique );
  1304. if( version > DRW::AC1009 )
  1305. writer->writeUtf8String( 7, ent->style );
  1306. else
  1307. writer->writeUtf8Caps( 7, ent->style );
  1308. writer->writeInt16( 71, ent->textgen );
  1309. if( ent->alignH != DRW_Text::HLeft )
  1310. {
  1311. writer->writeInt16( 72, ent->alignH );
  1312. }
  1313. if( ent->alignH != DRW_Text::HLeft || ent->alignV != DRW_Text::VBaseLine )
  1314. {
  1315. writer->writeDouble( 11, ent->secPoint.x );
  1316. writer->writeDouble( 21, ent->secPoint.y );
  1317. writer->writeDouble( 31, ent->secPoint.z );
  1318. }
  1319. writer->writeDouble( 210, ent->extPoint.x );
  1320. writer->writeDouble( 220, ent->extPoint.y );
  1321. writer->writeDouble( 230, ent->extPoint.z );
  1322. if( version > DRW::AC1009 )
  1323. {
  1324. writer->writeString( 100, "AcDbText" );
  1325. }
  1326. if( ent->alignV != DRW_Text::VBaseLine )
  1327. {
  1328. writer->writeInt16( 73, ent->alignV );
  1329. }
  1330. return true;
  1331. }
  1332. bool dxfRW::writeMText( DRW_MText* ent )
  1333. {
  1334. if( version > DRW::AC1009 )
  1335. {
  1336. writer->writeString( 0, "MTEXT" );
  1337. writeEntity( ent );
  1338. writer->writeString( 100, "AcDbMText" );
  1339. writer->writeDouble( 10, ent->basePoint.x );
  1340. writer->writeDouble( 20, ent->basePoint.y );
  1341. writer->writeDouble( 30, ent->basePoint.z );
  1342. writer->writeDouble( 40, ent->height );
  1343. writer->writeDouble( 41, ent->widthscale );
  1344. writer->writeInt16( 71, ent->textgen );
  1345. writer->writeInt16( 72, ent->alignH );
  1346. std::string text = writer->fromUtf8String( ent->text );
  1347. int i;
  1348. for( i = 0; (text.size() - i) > 250; )
  1349. {
  1350. writer->writeString( 3, text.substr( i, 250 ) );
  1351. i += 250;
  1352. }
  1353. writer->writeString( 1, text.substr( i ) );
  1354. writer->writeString( 7, ent->style );
  1355. writer->writeDouble( 210, ent->extPoint.x );
  1356. writer->writeDouble( 220, ent->extPoint.y );
  1357. writer->writeDouble( 230, ent->extPoint.z );
  1358. writer->writeDouble( 50, ent->angle );
  1359. writer->writeInt16( 73, ent->alignV );
  1360. writer->writeDouble( 44, ent->interlin );
  1361. // RLZ ... 11, 21, 31 needed?
  1362. }
  1363. else
  1364. {
  1365. // RLZ: TODO convert mtext in text lines (not exist in acad 12)
  1366. }
  1367. return true;
  1368. }
  1369. bool dxfRW::writeViewport( DRW_Viewport* ent )
  1370. {
  1371. writer->writeString( 0, "VIEWPORT" );
  1372. writeEntity( ent );
  1373. if( version > DRW::AC1009 )
  1374. {
  1375. writer->writeString( 100, "AcDbViewport" );
  1376. }
  1377. writer->writeDouble( 10, ent->basePoint.x );
  1378. writer->writeDouble( 20, ent->basePoint.y );
  1379. if( ent->basePoint.z != 0.0 )
  1380. writer->writeDouble( 30, ent->basePoint.z );
  1381. writer->writeDouble( 40, ent->pswidth );
  1382. writer->writeDouble( 41, ent->psheight );
  1383. writer->writeInt16( 68, ent->vpstatus );
  1384. writer->writeInt16( 69, ent->vpID );
  1385. writer->writeDouble( 12, ent->centerPX ); // RLZ: verify if exist in V12
  1386. writer->writeDouble( 22, ent->centerPY ); // RLZ: verify if exist in V12
  1387. return true;
  1388. }
  1389. DRW_ImageDef* dxfRW::writeImage( DRW_Image* ent, std::string name )
  1390. {
  1391. if( version > DRW::AC1009 )
  1392. {
  1393. // search if exist imagedef with this mane (image inserted more than 1 time)
  1394. // RLZ: imagedef_reactor seem needed to read in acad
  1395. DRW_ImageDef* id = NULL;
  1396. for( unsigned int i = 0; i<imageDef.size(); i++ )
  1397. {
  1398. if( imageDef.at( i )->name == name )
  1399. {
  1400. id = imageDef.at( i );
  1401. continue;
  1402. }
  1403. }
  1404. if( id == NULL )
  1405. {
  1406. id = new DRW_ImageDef();
  1407. imageDef.push_back( id );
  1408. id->handle = toHexStr( ++entCount );
  1409. }
  1410. id->name = name;
  1411. std::string idReactor = toHexStr( ++entCount );
  1412. writer->writeString( 0, "IMAGE" );
  1413. writeEntity( ent );
  1414. writer->writeString( 100, "AcDbRasterImage" );
  1415. writer->writeDouble( 10, ent->basePoint.x );
  1416. writer->writeDouble( 20, ent->basePoint.y );
  1417. writer->writeDouble( 30, ent->basePoint.z );
  1418. writer->writeDouble( 11, ent->secPoint.x );
  1419. writer->writeDouble( 21, ent->secPoint.y );
  1420. writer->writeDouble( 31, ent->secPoint.z );
  1421. writer->writeDouble( 12, ent->vx );
  1422. writer->writeDouble( 22, ent->vy );
  1423. writer->writeDouble( 32, ent->vz );
  1424. writer->writeDouble( 13, ent->sizeu );
  1425. writer->writeDouble( 23, ent->sizev );
  1426. writer->writeString( 340, id->handle );
  1427. writer->writeInt16( 70, 1 );
  1428. writer->writeInt16( 280, ent->clip );
  1429. writer->writeInt16( 281, ent->brightness );
  1430. writer->writeInt16( 282, ent->contrast );
  1431. writer->writeInt16( 283, ent->fade );
  1432. writer->writeString( 360, idReactor );
  1433. id->reactors[idReactor] = toHexStr( ent->handle );
  1434. return id;
  1435. }
  1436. return NULL; // not exist in acad 12
  1437. }
  1438. bool dxfRW::writeBlockRecord( std::string name )
  1439. {
  1440. if( version > DRW::AC1009 )
  1441. {
  1442. writer->writeString( 0, "BLOCK_RECORD" );
  1443. writer->writeString( 5, toHexStr( ++entCount ) );
  1444. blockMap[name] = entCount;
  1445. entCount = 2 + entCount; // reserve 2 for BLOCK & ENDBLOCK
  1446. if( version > DRW::AC1014 )
  1447. {
  1448. writer->writeString( 330, "1" );
  1449. }
  1450. writer->writeString( 100, "AcDbSymbolTableRecord" );
  1451. writer->writeString( 100, "AcDbBlockTableRecord" );
  1452. writer->writeUtf8String( 2, name );
  1453. if( version > DRW::AC1018 )
  1454. {
  1455. // writer->writeInt16(340, 22);
  1456. writer->writeInt16( 70, 0 );
  1457. writer->writeInt16( 280, 1 );
  1458. writer->writeInt16( 281, 0 );
  1459. }
  1460. }
  1461. return true;
  1462. }
  1463. bool dxfRW::writeBlock( DRW_Block* bk )
  1464. {
  1465. if( writingBlock )
  1466. {
  1467. writer->writeString( 0, "ENDBLK" );
  1468. if( version > DRW::AC1009 )
  1469. {
  1470. writer->writeString( 5, toHexStr( currHandle + 2 ) );
  1471. if( version > DRW::AC1014 )
  1472. {
  1473. writer->writeString( 330, toHexStr( currHandle ) );
  1474. }
  1475. writer->writeString( 100, "AcDbEntity" );
  1476. }
  1477. writer->writeString( 8, "0" );
  1478. if( version > DRW::AC1009 )
  1479. {
  1480. writer->writeString( 100, "AcDbBlockEnd" );
  1481. }
  1482. }
  1483. writingBlock = true;
  1484. writer->writeString( 0, "BLOCK" );
  1485. if( version > DRW::AC1009 )
  1486. {
  1487. currHandle = ( *( blockMap.find( bk->name ) ) ).second;
  1488. writer->writeString( 5, toHexStr( currHandle + 1 ) );
  1489. if( version > DRW::AC1014 )
  1490. {
  1491. writer->writeString( 330, toHexStr( currHandle ) );
  1492. }
  1493. writer->writeString( 100, "AcDbEntity" );
  1494. }
  1495. writer->writeString( 8, "0" );
  1496. if( version > DRW::AC1009 )
  1497. {
  1498. writer->writeString( 100, "AcDbBlockBegin" );
  1499. writer->writeUtf8String( 2, bk->name );
  1500. }
  1501. else
  1502. writer->writeUtf8Caps( 2, bk->name );
  1503. writer->writeInt16( 70, bk->flags );
  1504. writer->writeDouble( 10, bk->basePoint.x );
  1505. writer->writeDouble( 20, bk->basePoint.y );
  1506. if( bk->basePoint.z != 0.0 )
  1507. {
  1508. writer->writeDouble( 30, bk->basePoint.z );
  1509. }
  1510. if( version > DRW::AC1009 )
  1511. writer->writeUtf8String( 3, bk->name );
  1512. else
  1513. writer->writeUtf8Caps( 3, bk->name );
  1514. writer->writeString( 1, "" );
  1515. return true;
  1516. }
  1517. bool dxfRW::writeTables()
  1518. {
  1519. writer->writeString( 0, "TABLE" );
  1520. writer->writeString( 2, "VPORT" );
  1521. if( version > DRW::AC1009 )
  1522. {
  1523. writer->writeString( 5, "8" );
  1524. if( version > DRW::AC1014 )
  1525. {
  1526. writer->writeString( 330, "0" );
  1527. }
  1528. writer->writeString( 100, "AcDbSymbolTable" );
  1529. }
  1530. writer->writeInt16( 70, 1 ); // end table def
  1531. /*** VPORT ***/
  1532. dimstyleStd = false;
  1533. iface->writeVports();
  1534. if( !dimstyleStd )
  1535. {
  1536. DRW_Vport portact;
  1537. portact.name = "*ACTIVE";
  1538. writeVport( &portact );
  1539. }
  1540. writer->writeString( 0, "ENDTAB" );
  1541. /*** LTYPE ***/
  1542. writer->writeString( 0, "TABLE" );
  1543. writer->writeString( 2, "LTYPE" );
  1544. if( version > DRW::AC1009 )
  1545. {
  1546. writer->writeString( 5, "5" );
  1547. if( version > DRW::AC1014 )
  1548. {
  1549. writer->writeString( 330, "0" );
  1550. }
  1551. writer->writeString( 100, "AcDbSymbolTable" );
  1552. }
  1553. writer->writeInt16( 70, 4 ); // end table def
  1554. // Mandatory linetypes
  1555. writer->writeString( 0, "LTYPE" );
  1556. if( version > DRW::AC1009 )
  1557. {
  1558. writer->writeString( 5, "14" );
  1559. if( version > DRW::AC1014 )
  1560. {
  1561. writer->writeString( 330, "5" );
  1562. }
  1563. writer->writeString( 100, "AcDbSymbolTableRecord" );
  1564. writer->writeString( 100, "AcDbLinetypeTableRecord" );
  1565. writer->writeString( 2, "ByBlock" );
  1566. }
  1567. else
  1568. writer->writeString( 2, "BYBLOCK" );
  1569. writer->writeInt16( 70, 0 );
  1570. writer->writeString( 3, "" );
  1571. writer->writeInt16( 72, 65 );
  1572. writer->writeInt16( 73, 0 );
  1573. writer->writeDouble( 40, 0.0 );
  1574. writer->writeString( 0, "LTYPE" );
  1575. if( version > DRW::AC1009 )
  1576. {
  1577. writer->writeString( 5, "15" );
  1578. if( version > DRW::AC1014 )
  1579. {
  1580. writer->writeString( 330, "5" );
  1581. }
  1582. writer->writeString( 100, "AcDbSymbolTableRecord" );
  1583. writer->writeString( 100, "AcDbLinetypeTableRecord" );
  1584. writer->writeString( 2, "ByLayer" );
  1585. }
  1586. else
  1587. writer->writeString( 2, "BYLAYER" );
  1588. writer->writeInt16( 70, 0 );
  1589. writer->writeString( 3, "" );
  1590. writer->writeInt16( 72, 65 );
  1591. writer->writeInt16( 73, 0 );
  1592. writer->writeDouble( 40, 0.0 );
  1593. writer->writeString( 0, "LTYPE" );
  1594. if( version > DRW::AC1009 )
  1595. {
  1596. writer->writeString( 5, "16" );
  1597. if( version > DRW::AC1014 )
  1598. {
  1599. writer->writeString( 330, "5" );
  1600. }
  1601. writer->writeString( 100, "AcDbSymbolTableRecord" );
  1602. writer->writeString( 100, "AcDbLinetypeTableRecord" );
  1603. writer->writeString( 2, "Continuous" );
  1604. }
  1605. else
  1606. {
  1607. writer->writeString( 2, "CONTINUOUS" );
  1608. }
  1609. writer->writeInt16( 70, 0 );
  1610. writer->writeString( 3, "Solid line" );
  1611. writer->writeInt16( 72, 65 );
  1612. writer->writeInt16( 73, 0 );
  1613. writer->writeDouble( 40, 0.0 );
  1614. // Aplication linetypes
  1615. iface->writeLTypes();
  1616. writer->writeString( 0, "ENDTAB" );
  1617. /*** LAYER ***/
  1618. writer->writeString( 0, "TABLE" );
  1619. writer->writeString( 2, "LAYER" );
  1620. if( version > DRW::AC1009 )
  1621. {
  1622. writer->writeString( 5, "2" );
  1623. if( version > DRW::AC1014 )
  1624. {
  1625. writer->writeString( 330, "0" );
  1626. }
  1627. writer->writeString( 100, "AcDbSymbolTable" );
  1628. }
  1629. writer->writeInt16( 70, 1 ); // end table def
  1630. wlayer0 = false;
  1631. iface->writeLayers();
  1632. if( !wlayer0 )
  1633. {
  1634. DRW_Layer lay0;
  1635. lay0.name = "0";
  1636. writeLayer( &lay0 );
  1637. }
  1638. writer->writeString( 0, "ENDTAB" );
  1639. /*** STYLE ***/
  1640. writer->writeString( 0, "TABLE" );
  1641. writer->writeString( 2, "STYLE" );
  1642. if( version > DRW::AC1009 )
  1643. {
  1644. writer->writeString( 5, "3" );
  1645. if( version > DRW::AC1014 )
  1646. {
  1647. writer->writeString( 330, "0" );
  1648. }
  1649. writer->writeString( 100, "AcDbSymbolTable" );
  1650. }
  1651. writer->writeInt16( 70, 3 ); // end table def
  1652. dimstyleStd = false;
  1653. iface->writeTextstyles();
  1654. if( !dimstyleStd )
  1655. {
  1656. DRW_Textstyle tsty;
  1657. tsty.name = "Standard";
  1658. writeTextstyle( &tsty );
  1659. }
  1660. writer->writeString( 0, "ENDTAB" );
  1661. writer->writeString( 0, "TABLE" );
  1662. writer->writeString( 2, "VIEW" );
  1663. if( version > DRW::AC1009 )
  1664. {
  1665. writer->writeString( 5, "6" );
  1666. if( version > DRW::AC1014 )
  1667. {
  1668. writer->writeString( 330, "0" );
  1669. }
  1670. writer->writeString( 100, "AcDbSymbolTable" );
  1671. }
  1672. writer->writeInt16( 70, 0 ); // end table def
  1673. writer->writeString( 0, "ENDTAB" );
  1674. writer->writeString( 0, "TABLE" );
  1675. writer->writeString( 2, "UCS" );
  1676. if( version > DRW::AC1009 )
  1677. {
  1678. writer->writeString( 5, "7" );
  1679. if( version > DRW::AC1014 )
  1680. {
  1681. writer->writeString( 330, "0" );
  1682. }
  1683. writer->writeString( 100, "AcDbSymbolTable" );
  1684. }
  1685. writer->writeInt16( 70, 0 ); // end table def
  1686. writer->writeString( 0, "ENDTAB" );
  1687. writer->writeString( 0, "TABLE" );
  1688. writer->writeString( 2, "APPID" );
  1689. if( version > DRW::AC1009 )
  1690. {
  1691. writer->writeString( 5, "9" );
  1692. if( version > DRW::AC1014 )
  1693. {
  1694. writer->writeString( 330, "0" );
  1695. }
  1696. writer->writeString( 100, "AcDbSymbolTable" );
  1697. }
  1698. writer->writeInt16( 70, 1 ); // end table def
  1699. writer->writeString( 0, "APPID" );
  1700. if( version > DRW::AC1009 )
  1701. {
  1702. writer->writeString( 5, "12" );
  1703. if( version > DRW::AC1014 )
  1704. {
  1705. writer->writeString( 330, "9" );
  1706. }
  1707. writer->writeString( 100, "AcDbSymbolTableRecord" );
  1708. writer->writeString( 100, "AcDbRegAppTableRecord" );
  1709. }
  1710. writer->writeString( 2, "ACAD" );
  1711. writer->writeInt16( 70, 0 );
  1712. iface->writeAppId();
  1713. writer->writeString( 0, "ENDTAB" );
  1714. writer->writeString( 0, "TABLE" );
  1715. writer->writeString( 2, "DIMSTYLE" );
  1716. if( version > DRW::AC1009 )
  1717. {
  1718. writer->writeString( 5, "A" );
  1719. if( version > DRW::AC1014 )
  1720. {
  1721. writer->writeString( 330, "0" );
  1722. }
  1723. writer->writeString( 100, "AcDbSymbolTable" );
  1724. }
  1725. writer->writeInt16( 70, 1 ); // end table def
  1726. if( version > DRW::AC1014 )
  1727. {
  1728. writer->writeString( 100, "AcDbDimStyleTable" );
  1729. writer->writeInt16( 71, 1 ); // end table def
  1730. }
  1731. dimstyleStd = false;
  1732. iface->writeDimstyles();
  1733. if( !dimstyleStd )
  1734. {
  1735. DRW_Dimstyle dsty;
  1736. dsty.name = "Standard";
  1737. writeDimstyle( &dsty );
  1738. }
  1739. writer->writeString( 0, "ENDTAB" );
  1740. if( version > DRW::AC1009 )
  1741. {
  1742. writer->writeString( 0, "TABLE" );
  1743. writer->writeString( 2, "BLOCK_RECORD" );
  1744. writer->writeString( 5, "1" );
  1745. if( version > DRW::AC1014 )
  1746. {
  1747. writer->writeString( 330, "0" );
  1748. }
  1749. writer->writeString( 100, "AcDbSymbolTable" );
  1750. writer->writeInt16( 70, 2 ); // end table def
  1751. writer->writeString( 0, "BLOCK_RECORD" );
  1752. writer->writeString( 5, "1F" );
  1753. if( version > DRW::AC1014 )
  1754. {
  1755. writer->writeString( 330, "1" );
  1756. }
  1757. writer->writeString( 100, "AcDbSymbolTableRecord" );
  1758. writer->writeString( 100, "AcDbBlockTableRecord" );
  1759. writer->writeString( 2, "*Model_Space" );
  1760. if( version > DRW::AC1018 )
  1761. {
  1762. // writer->writeInt16(340, 22);
  1763. writer->writeInt16( 70, 0 );
  1764. writer->writeInt16( 280, 1 );
  1765. writer->writeInt16( 281, 0 );
  1766. }
  1767. writer->writeString( 0, "BLOCK_RECORD" );
  1768. writer->writeString( 5, "1E" );
  1769. if( version > DRW::AC1014 )
  1770. {
  1771. writer->writeString( 330, "1" );
  1772. }
  1773. writer->writeString( 100, "AcDbSymbolTableRecord" );
  1774. writer->writeString( 100, "AcDbBlockTableRecord" );
  1775. writer->writeString( 2, "*Paper_Space" );
  1776. if( version > DRW::AC1018 )
  1777. {
  1778. // writer->writeInt16(340, 22);
  1779. writer->writeInt16( 70, 0 );
  1780. writer->writeInt16( 280, 1 );
  1781. writer->writeInt16( 281, 0 );
  1782. }
  1783. }
  1784. /* allways call writeBlockRecords to iface for prepare unnamed blocks */
  1785. iface->writeBlockRecords();
  1786. if( version > DRW::AC1009 )
  1787. {
  1788. writer->writeString( 0, "ENDTAB" );
  1789. }
  1790. return true;
  1791. }
  1792. bool dxfRW::writeBlocks()
  1793. {
  1794. writer->writeString( 0, "BLOCK" );
  1795. if( version > DRW::AC1009 )
  1796. {
  1797. writer->writeString( 5, "20" );
  1798. if( version > DRW::AC1014 )
  1799. {
  1800. writer->writeString( 330, "1F" );
  1801. }
  1802. writer->writeString( 100, "AcDbEntity" );
  1803. }
  1804. writer->writeString( 8, "0" );
  1805. if( version > DRW::AC1009 )
  1806. {
  1807. writer->writeString( 100, "AcDbBlockBegin" );
  1808. writer->writeString( 2, "*Model_Space" );
  1809. }
  1810. else
  1811. writer->writeString( 2, "$MODEL_SPACE" );
  1812. writer->writeInt16( 70, 0 );
  1813. writer->writeDouble( 10, 0.0 );
  1814. writer->writeDouble( 20, 0.0 );
  1815. writer->writeDouble( 30, 0.0 );
  1816. if( version > DRW::AC1009 )
  1817. writer->writeString( 3, "*Model_Space" );
  1818. else
  1819. writer->writeString( 3, "$MODEL_SPACE" );
  1820. writer->writeString( 1, "" );
  1821. writer->writeString( 0, "ENDBLK" );
  1822. if( version > DRW::AC1009 )
  1823. {
  1824. writer->writeString( 5, "21" );
  1825. if( version > DRW::AC1014 )
  1826. {
  1827. writer->writeString( 330, "1F" );
  1828. }
  1829. writer->writeString( 100, "AcDbEntity" );
  1830. }
  1831. writer->writeString( 8, "0" );
  1832. if( version > DRW::AC1009 )
  1833. writer->writeString( 100, "AcDbBlockEnd" );
  1834. writer->writeString( 0, "BLOCK" );
  1835. if( version > DRW::AC1009 )
  1836. {
  1837. writer->writeString( 5, "1C" );
  1838. if( version > DRW::AC1014 )
  1839. {
  1840. writer->writeString( 330, "1B" );
  1841. }
  1842. writer->writeString( 100, "AcDbEntity" );
  1843. }
  1844. writer->writeString( 8, "0" );
  1845. if( version > DRW::AC1009 )
  1846. {
  1847. writer->writeString( 100, "AcDbBlockBegin" );
  1848. writer->writeString( 2, "*Paper_Space" );
  1849. }
  1850. else
  1851. writer->writeString( 2, "$PAPER_SPACE" );
  1852. writer->writeInt16( 70, 0 );
  1853. writer->writeDouble( 10, 0.0 );
  1854. writer->writeDouble( 20, 0.0 );
  1855. writer->writeDouble( 30, 0.0 );
  1856. if( version > DRW::AC1009 )
  1857. writer->writeString( 3, "*Paper_Space" );
  1858. else
  1859. writer->writeString( 3, "$PAPER_SPACE" );
  1860. writer->writeString( 1, "" );
  1861. writer->writeString( 0, "ENDBLK" );
  1862. if( version > DRW::AC1009 )
  1863. {
  1864. writer->writeString( 5, "1D" );
  1865. if( version > DRW::AC1014 )
  1866. {
  1867. writer->writeString( 330, "1F" );
  1868. }
  1869. writer->writeString( 100, "AcDbEntity" );
  1870. }
  1871. writer->writeString( 8, "0" );
  1872. if( version > DRW::AC1009 )
  1873. writer->writeString( 100, "AcDbBlockEnd" );
  1874. writingBlock = false;
  1875. iface->writeBlocks();
  1876. if( writingBlock )
  1877. {
  1878. writingBlock = false;
  1879. writer->writeString( 0, "ENDBLK" );
  1880. if( version > DRW::AC1009 )
  1881. {
  1882. writer->writeString( 5, toHexStr( currHandle + 2 ) );
  1883. // writer->writeString(5, "1D");
  1884. if( version > DRW::AC1014 )
  1885. {
  1886. writer->writeString( 330, toHexStr( currHandle ) );
  1887. }
  1888. writer->writeString( 100, "AcDbEntity" );
  1889. }
  1890. writer->writeString( 8, "0" );
  1891. if( version > DRW::AC1009 )
  1892. writer->writeString( 100, "AcDbBlockEnd" );
  1893. }
  1894. return true;
  1895. }
  1896. bool dxfRW::writeObjects()
  1897. {
  1898. writer->writeString( 0, "DICTIONARY" );
  1899. std::string imgDictH;
  1900. writer->writeString( 5, "C" );
  1901. if( version > DRW::AC1014 )
  1902. {
  1903. writer->writeString( 330, "0" );
  1904. }
  1905. writer->writeString( 100, "AcDbDictionary" );
  1906. writer->writeInt16( 281, 1 );
  1907. writer->writeString( 3, "ACAD_GROUP" );
  1908. writer->writeString( 350, "D" );
  1909. if( imageDef.size() != 0 )
  1910. {
  1911. writer->writeString( 3, "ACAD_IMAGE_DICT" );
  1912. imgDictH = toHexStr( ++entCount );
  1913. writer->writeString( 350, imgDictH );
  1914. }
  1915. writer->writeString( 0, "DICTIONARY" );
  1916. writer->writeString( 5, "D" );
  1917. writer->writeString( 330, "C" );
  1918. writer->writeString( 100, "AcDbDictionary" );
  1919. writer->writeInt16( 281, 1 );
  1920. // write IMAGEDEF_REACTOR
  1921. for( unsigned int i = 0; i<imageDef.size(); i++ )
  1922. {
  1923. DRW_ImageDef* id = imageDef.at( i );
  1924. std::map<std::string, std::string>::iterator it;
  1925. for( it = id->reactors.begin(); it != id->reactors.end(); ++it )
  1926. {
  1927. writer->writeString( 0, "IMAGEDEF_REACTOR" );
  1928. writer->writeString( 5, (*it).first );
  1929. writer->writeString( 330, (*it).second );
  1930. writer->writeString( 100, "AcDbRasterImageDefReactor" );
  1931. writer->writeInt16( 90, 2 ); // version 2=R14 to v2010
  1932. writer->writeString( 330, (*it).second );
  1933. }
  1934. }
  1935. if( imageDef.size() != 0 )
  1936. {
  1937. writer->writeString( 0, "DICTIONARY" );
  1938. writer->writeString( 5, imgDictH );
  1939. writer->writeString( 330, "C" );
  1940. writer->writeString( 100, "AcDbDictionary" );
  1941. writer->writeInt16( 281, 1 );
  1942. for( unsigned int i = 0; i<imageDef.size(); i++ )
  1943. {
  1944. size_t f1, f2;
  1945. f1 = imageDef.at( i )->name.find_last_of( "/\\" );
  1946. f2 = imageDef.at( i )->name.find_last_of( '.' );
  1947. ++f1;
  1948. writer->writeString( 3, imageDef.at( i )->name.substr( f1, f2 - f1 ) );
  1949. writer->writeString( 350, imageDef.at( i )->handle );
  1950. }
  1951. }
  1952. for( unsigned int i = 0; i<imageDef.size(); i++ )
  1953. {
  1954. DRW_ImageDef* id = imageDef.at( i );
  1955. writer->writeString( 0, "IMAGEDEF" );
  1956. writer->writeString( 5, id->handle );
  1957. if( version > DRW::AC1014 )
  1958. {
  1959. // writer->writeString(330, "0"); handle to DICTIONARY
  1960. }
  1961. writer->writeString( 102, "{ACAD_REACTORS" );
  1962. std::map<std::string, std::string>::iterator it;
  1963. for( it = id->reactors.begin(); it != id->reactors.end(); ++it )
  1964. {
  1965. writer->writeString( 330, (*it).first );
  1966. }
  1967. writer->writeString( 102, "}" );
  1968. writer->writeString( 100, "AcDbRasterImageDef" );
  1969. writer->writeInt16( 90, 0 ); // version 0=R14 to v2010
  1970. writer->writeUtf8String( 1, id->name );
  1971. writer->writeDouble( 10, id->u );
  1972. writer->writeDouble( 20, id->v );
  1973. writer->writeDouble( 11, id->up );
  1974. writer->writeDouble( 21, id->vp );
  1975. writer->writeInt16( 280, id->loaded );
  1976. writer->writeInt16( 281, id->resolution );
  1977. }
  1978. // no more needed imageDef, delete it
  1979. while( !imageDef.empty() )
  1980. {
  1981. imageDef.pop_back();
  1982. }
  1983. return true;
  1984. }
  1985. bool dxfRW::writeExtData( const std::vector<DRW_Variant*>& ed )
  1986. {
  1987. for( std::vector<DRW_Variant*>::const_iterator it = ed.begin(); it!=ed.end(); ++it )
  1988. {
  1989. switch( (*it)->code )
  1990. {
  1991. case 1000:
  1992. case 1001:
  1993. case 1002:
  1994. case 1003:
  1995. case 1004:
  1996. case 1005:
  1997. {
  1998. int cc = (*it)->code;
  1999. if( (*it)->type == DRW_Variant::STRING )
  2000. writer->writeUtf8String( cc, *(*it)->content.s );
  2001. // writer->writeUtf8String((*it)->code, (*it)->content.s);
  2002. break;
  2003. }
  2004. case 1010:
  2005. case 1011:
  2006. case 1012:
  2007. case 1013:
  2008. if( (*it)->type == DRW_Variant::COORD )
  2009. {
  2010. writer->writeDouble( (*it)->code, (*it)->content.v->x );
  2011. writer->writeDouble( (*it)->code + 10, (*it)->content.v->y );
  2012. writer->writeDouble( (*it)->code + 20, (*it)->content.v->z );
  2013. }
  2014. break;
  2015. case 1040:
  2016. case 1041:
  2017. case 1042:
  2018. if( (*it)->type == DRW_Variant::DOUBLE )
  2019. writer->writeDouble( (*it)->code, (*it)->content.d );
  2020. break;
  2021. case 1070:
  2022. if( (*it)->type == DRW_Variant::INTEGER )
  2023. writer->writeInt16( (*it)->code, (*it)->content.i );
  2024. break;
  2025. case 1071:
  2026. if( (*it)->type == DRW_Variant::INTEGER )
  2027. writer->writeInt32( (*it)->code, (*it)->content.i );
  2028. break;
  2029. default:
  2030. break;
  2031. }
  2032. }
  2033. return true;
  2034. }
  2035. /********* Reader Process *********/
  2036. bool dxfRW::processDxf()
  2037. {
  2038. DBG( "dxfRW::processDxf() start processing dxf\n" );
  2039. int code;
  2040. bool more = true;
  2041. std::string sectionstr;
  2042. // section = secUnknown;
  2043. while( reader->readRec( &code, !binary ) )
  2044. {
  2045. DBG( code ); DBG( " processDxf\n" );
  2046. if( code == 999 )
  2047. {
  2048. header.addComment( reader->getString() );
  2049. }
  2050. else if( code == 0 )
  2051. {
  2052. sectionstr = reader->getString();
  2053. DBG( sectionstr ); DBG( " processDxf\n" );
  2054. if( sectionstr == "EOF" )
  2055. {
  2056. return true; // found EOF terminate
  2057. }
  2058. if( sectionstr == "SECTION" )
  2059. {
  2060. more = reader->readRec( &code, !binary );
  2061. DBG( code ); DBG( " processDxf\n" );
  2062. if( !more )
  2063. return false; // wrong dxf file
  2064. if( code == 2 )
  2065. {
  2066. sectionstr = reader->getString();
  2067. DBG( sectionstr ); DBG( " processDxf\n" );
  2068. // found section, process it
  2069. if( sectionstr == "HEADER" )
  2070. {
  2071. processHeader();
  2072. }
  2073. else if( sectionstr == "CLASSES" )
  2074. {
  2075. // processClasses();
  2076. }
  2077. else if( sectionstr == "TABLES" )
  2078. {
  2079. processTables();
  2080. }
  2081. else if( sectionstr == "BLOCKS" )
  2082. {
  2083. processBlocks();
  2084. }
  2085. else if( sectionstr == "ENTITIES" )
  2086. {
  2087. processEntities( false );
  2088. }
  2089. else if( sectionstr == "OBJECTS" )
  2090. {
  2091. processObjects();
  2092. }
  2093. }
  2094. }
  2095. }
  2096. /* if (!more)
  2097. * return true;*/
  2098. }
  2099. return true;
  2100. }
  2101. /********* Header Section *********/
  2102. bool dxfRW::processHeader()
  2103. {
  2104. DBG( "dxfRW::processHeader\n" );
  2105. int code;
  2106. std::string sectionstr;
  2107. while( reader->readRec( &code, !binary ) )
  2108. {
  2109. DBG( code ); DBG( " processHeader\n" );
  2110. if( code == 0 )
  2111. {
  2112. sectionstr = reader->getString();
  2113. DBG( sectionstr ); DBG( " processHeader\n\n" );
  2114. if( sectionstr == "ENDSEC" )
  2115. {
  2116. iface->addHeader( &header );
  2117. return true; // found ENDSEC terminate
  2118. }
  2119. }
  2120. else
  2121. header.parseCode( code, reader );
  2122. }
  2123. return true;
  2124. }
  2125. /********* Tables Section *********/
  2126. bool dxfRW::processTables()
  2127. {
  2128. DBG( "dxfRW::processTables\n" );
  2129. int code;
  2130. std::string sectionstr;
  2131. bool more = true;
  2132. while( reader->readRec( &code, !binary ) )
  2133. {
  2134. DBG( code ); DBG( "\n" );
  2135. if( code == 0 )
  2136. {
  2137. sectionstr = reader->getString();
  2138. DBG( sectionstr ); DBG( " processHeader\n\n" );
  2139. if( sectionstr == "TABLE" )
  2140. {
  2141. more = reader->readRec( &code, !binary );
  2142. DBG( code ); DBG( "\n" );
  2143. if( !more )
  2144. return false; // wrong dxf file
  2145. if( code == 2 )
  2146. {
  2147. sectionstr = reader->getString();
  2148. DBG( sectionstr ); DBG( " processHeader\n\n" );
  2149. // found section, process it
  2150. if( sectionstr == "LTYPE" )
  2151. {
  2152. processLType();
  2153. }
  2154. else if( sectionstr == "LAYER" )
  2155. {
  2156. processLayer();
  2157. }
  2158. else if( sectionstr == "STYLE" )
  2159. {
  2160. processTextStyle();
  2161. }
  2162. else if( sectionstr == "VPORT" )
  2163. {
  2164. processVports();
  2165. }
  2166. else if( sectionstr == "VIEW" )
  2167. {
  2168. // processView();
  2169. }
  2170. else if( sectionstr == "UCS" )
  2171. {
  2172. // processUCS();
  2173. }
  2174. else if( sectionstr == "APPID" )
  2175. {
  2176. processAppId();
  2177. }
  2178. else if( sectionstr == "DIMSTYLE" )
  2179. {
  2180. processDimStyle();
  2181. }
  2182. else if( sectionstr == "BLOCK_RECORD" )
  2183. {
  2184. // processBlockRecord();
  2185. }
  2186. }
  2187. }
  2188. else if( sectionstr == "ENDSEC" )
  2189. {
  2190. return true; // found ENDSEC terminate
  2191. }
  2192. }
  2193. }
  2194. return true;
  2195. }
  2196. bool dxfRW::processLType()
  2197. {
  2198. DBG( "dxfRW::processLType\n" );
  2199. int code;
  2200. std::string sectionstr;
  2201. bool reading = false;
  2202. DRW_LType ltype;
  2203. while( reader->readRec( &code, !binary ) )
  2204. {
  2205. DBG( code ); DBG( "\n" );
  2206. if( code == 0 )
  2207. {
  2208. if( reading )
  2209. {
  2210. ltype.update();
  2211. iface->addLType( ltype );
  2212. }
  2213. sectionstr = reader->getString();
  2214. DBG( sectionstr ); DBG( "\n" );
  2215. if( sectionstr == "LTYPE" )
  2216. {
  2217. reading = true;
  2218. ltype.reset();
  2219. }
  2220. else if( sectionstr == "ENDTAB" )
  2221. {
  2222. return true; // found ENDTAB terminate
  2223. }
  2224. }
  2225. else if( reading )
  2226. ltype.parseCode( code, reader );
  2227. }
  2228. return true;
  2229. }
  2230. bool dxfRW::processLayer()
  2231. {
  2232. DBG( "dxfRW::processLayer\n" );
  2233. int code;
  2234. std::string sectionstr;
  2235. bool reading = false;
  2236. DRW_Layer layer;
  2237. while( reader->readRec( &code, !binary ) )
  2238. {
  2239. DBG( code ); DBG( "\n" );
  2240. if( code == 0 )
  2241. {
  2242. if( reading )
  2243. iface->addLayer( layer );
  2244. sectionstr = reader->getString();
  2245. DBG( sectionstr ); DBG( "\n" );
  2246. if( sectionstr == "LAYER" )
  2247. {
  2248. reading = true;
  2249. layer.reset();
  2250. }
  2251. else if( sectionstr == "ENDTAB" )
  2252. {
  2253. return true; // found ENDTAB terminate
  2254. }
  2255. }
  2256. else if( reading )
  2257. layer.parseCode( code, reader );
  2258. }
  2259. return true;
  2260. }
  2261. bool dxfRW::processDimStyle()
  2262. {
  2263. DBG( "dxfRW::processDimStyle" );
  2264. int code;
  2265. std::string sectionstr;
  2266. bool reading = false;
  2267. DRW_Dimstyle dimSty;
  2268. while( reader->readRec( &code, !binary ) )
  2269. {
  2270. DBG( code ); DBG( "\n" );
  2271. if( code == 0 )
  2272. {
  2273. if( reading )
  2274. iface->addDimStyle( dimSty );
  2275. sectionstr = reader->getString();
  2276. DBG( sectionstr ); DBG( "\n" );
  2277. if( sectionstr == "DIMSTYLE" )
  2278. {
  2279. reading = true;
  2280. dimSty.reset();
  2281. }
  2282. else if( sectionstr == "ENDTAB" )
  2283. {
  2284. return true; // found ENDTAB terminate
  2285. }
  2286. }
  2287. else if( reading )
  2288. dimSty.parseCode( code, reader );
  2289. }
  2290. return true;
  2291. }
  2292. bool dxfRW::processTextStyle()
  2293. {
  2294. DBG( "dxfRW::processTextStyle" );
  2295. int code;
  2296. std::string sectionstr;
  2297. bool reading = false;
  2298. DRW_Textstyle TxtSty;
  2299. while( reader->readRec( &code, !binary ) )
  2300. {
  2301. DBG( code ); DBG( "\n" );
  2302. if( code == 0 )
  2303. {
  2304. if( reading )
  2305. iface->addTextStyle( TxtSty );
  2306. sectionstr = reader->getString();
  2307. DBG( sectionstr ); DBG( "\n" );
  2308. if( sectionstr == "STYLE" )
  2309. {
  2310. reading = true;
  2311. TxtSty.reset();
  2312. }
  2313. else if( sectionstr == "ENDTAB" )
  2314. {
  2315. return true; // found ENDTAB terminate
  2316. }
  2317. }
  2318. else if( reading )
  2319. TxtSty.parseCode( code, reader );
  2320. }
  2321. return true;
  2322. }
  2323. bool dxfRW::processVports()
  2324. {
  2325. DBG( "dxfRW::processVports" );
  2326. int code;
  2327. std::string sectionstr;
  2328. bool reading = false;
  2329. DRW_Vport vp;
  2330. while( reader->readRec( &code, !binary ) )
  2331. {
  2332. DBG( code ); DBG( "\n" );
  2333. if( code == 0 )
  2334. {
  2335. if( reading )
  2336. iface->addVport( vp );
  2337. sectionstr = reader->getString();
  2338. DBG( sectionstr ); DBG( "\n" );
  2339. if( sectionstr == "VPORT" )
  2340. {
  2341. reading = true;
  2342. vp.reset();
  2343. }
  2344. else if( sectionstr == "ENDTAB" )
  2345. {
  2346. return true; // found ENDTAB terminate
  2347. }
  2348. }
  2349. else if( reading )
  2350. vp.parseCode( code, reader );
  2351. }
  2352. return true;
  2353. }
  2354. bool dxfRW::processAppId()
  2355. {
  2356. DBG( "dxfRW::processAppId" );
  2357. int code;
  2358. std::string sectionstr;
  2359. bool reading = false;
  2360. DRW_AppId vp;
  2361. while( reader->readRec( &code, !binary ) )
  2362. {
  2363. DBG( code ); DBG( "\n" );
  2364. if( code == 0 )
  2365. {
  2366. if( reading )
  2367. iface->addAppId( vp );
  2368. sectionstr = reader->getString();
  2369. DBG( sectionstr ); DBG( "\n" );
  2370. if( sectionstr == "APPID" )
  2371. {
  2372. reading = true;
  2373. vp.reset();
  2374. }
  2375. else if( sectionstr == "ENDTAB" )
  2376. {
  2377. return true; // found ENDTAB terminate
  2378. }
  2379. }
  2380. else if( reading )
  2381. vp.parseCode( code, reader );
  2382. }
  2383. return true;
  2384. }
  2385. /********* Block Section *********/
  2386. bool dxfRW::processBlocks()
  2387. {
  2388. DBG( "dxfRW::processBlocks\n" );
  2389. int code;
  2390. std::string sectionstr;
  2391. while( reader->readRec( &code, !binary ) )
  2392. {
  2393. DBG( code ); DBG( "\n" );
  2394. if( code == 0 )
  2395. {
  2396. sectionstr = reader->getString();
  2397. DBG( sectionstr ); DBG( "\n" );
  2398. if( sectionstr == "BLOCK" )
  2399. {
  2400. processBlock();
  2401. }
  2402. else if( sectionstr == "ENDSEC" )
  2403. {
  2404. return true; // found ENDSEC terminate
  2405. }
  2406. }
  2407. }
  2408. return true;
  2409. }
  2410. bool dxfRW::processBlock()
  2411. {
  2412. DBG( "dxfRW::processBlock" );
  2413. int code;
  2414. DRW_Block block;
  2415. while( reader->readRec( &code, !binary ) )
  2416. {
  2417. DBG( code ); DBG( "\n" );
  2418. switch( code )
  2419. {
  2420. case 0:
  2421. {
  2422. nextentity = reader->getString();
  2423. DBG( nextentity ); DBG( "\n" );
  2424. iface->addBlock( block );
  2425. if( nextentity == "ENDBLK" )
  2426. {
  2427. iface->endBlock();
  2428. return true; // found ENDBLK, terminate
  2429. }
  2430. else
  2431. {
  2432. processEntities( true );
  2433. iface->endBlock();
  2434. return true; // found ENDBLK, terminate
  2435. }
  2436. }
  2437. default:
  2438. block.parseCode( code, reader );
  2439. break;
  2440. }
  2441. }
  2442. return true;
  2443. }
  2444. /********* Entities Section *********/
  2445. bool dxfRW::processEntities( bool isblock )
  2446. {
  2447. DBG( "dxfRW::processEntities\n" );
  2448. int code;
  2449. if( !reader->readRec( &code, !binary ) )
  2450. {
  2451. return false;
  2452. }
  2453. bool next = true;
  2454. if( code == 0 )
  2455. {
  2456. nextentity = reader->getString();
  2457. }
  2458. else if( !isblock )
  2459. {
  2460. return false; // first record in entities is 0
  2461. }
  2462. do {
  2463. if( nextentity == "ENDSEC" || nextentity == "ENDBLK" )
  2464. {
  2465. return true; // found ENDSEC or ENDBLK terminate
  2466. }
  2467. else if( nextentity == "POINT" )
  2468. {
  2469. processPoint();
  2470. }
  2471. else if( nextentity == "LINE" )
  2472. {
  2473. processLine();
  2474. }
  2475. else if( nextentity == "CIRCLE" )
  2476. {
  2477. processCircle();
  2478. }
  2479. else if( nextentity == "ARC" )
  2480. {
  2481. processArc();
  2482. }
  2483. else if( nextentity == "ELLIPSE" )
  2484. {
  2485. processEllipse();
  2486. }
  2487. else if( nextentity == "TRACE" )
  2488. {
  2489. processTrace();
  2490. }
  2491. else if( nextentity == "SOLID" )
  2492. {
  2493. processSolid();
  2494. }
  2495. else if( nextentity == "INSERT" )
  2496. {
  2497. processInsert();
  2498. }
  2499. else if( nextentity == "LWPOLYLINE" )
  2500. {
  2501. processLWPolyline();
  2502. }
  2503. else if( nextentity == "POLYLINE" )
  2504. {
  2505. processPolyline();
  2506. }
  2507. else if( nextentity == "TEXT" )
  2508. {
  2509. processText();
  2510. }
  2511. else if( nextentity == "MTEXT" )
  2512. {
  2513. processMText();
  2514. }
  2515. else if( nextentity == "HATCH" )
  2516. {
  2517. processHatch();
  2518. }
  2519. else if( nextentity == "SPLINE" )
  2520. {
  2521. processSpline();
  2522. }
  2523. else if( nextentity == "3DFACE" )
  2524. {
  2525. process3dface();
  2526. }
  2527. else if( nextentity == "VIEWPORT" )
  2528. {
  2529. processViewport();
  2530. }
  2531. else if( nextentity == "IMAGE" )
  2532. {
  2533. processImage();
  2534. }
  2535. else if( nextentity == "DIMENSION" )
  2536. {
  2537. processDimension();
  2538. }
  2539. else if( nextentity == "LEADER" )
  2540. {
  2541. processLeader();
  2542. }
  2543. else if( nextentity == "RAY" )
  2544. {
  2545. processRay();
  2546. }
  2547. else if( nextentity == "XLINE" )
  2548. {
  2549. processXline();
  2550. }
  2551. else
  2552. {
  2553. if( reader->readRec( &code, !binary ) )
  2554. {
  2555. if( code == 0 )
  2556. nextentity = reader->getString();
  2557. }
  2558. else
  2559. return false; // end of file without ENDSEC
  2560. }
  2561. } while( next );
  2562. return true;
  2563. }
  2564. bool dxfRW::processEllipse()
  2565. {
  2566. DBG( "dxfRW::processEllipse" );
  2567. int code;
  2568. DRW_Ellipse ellipse;
  2569. while( reader->readRec( &code, !binary ) )
  2570. {
  2571. DBG( code ); DBG( "\n" );
  2572. switch( code )
  2573. {
  2574. case 0:
  2575. {
  2576. nextentity = reader->getString();
  2577. DBG( nextentity ); DBG( "\n" );
  2578. if( applyExt )
  2579. ellipse.applyExtrusion();
  2580. iface->addEllipse( ellipse );
  2581. return true; // found new entity or ENDSEC, terminate
  2582. }
  2583. default:
  2584. ellipse.parseCode( code, reader );
  2585. break;
  2586. }
  2587. }
  2588. return true;
  2589. }
  2590. bool dxfRW::processTrace()
  2591. {
  2592. DBG( "dxfRW::processTrace" );
  2593. int code;
  2594. DRW_Trace trace;
  2595. while( reader->readRec( &code, !binary ) )
  2596. {
  2597. DBG( code ); DBG( "\n" );
  2598. switch( code )
  2599. {
  2600. case 0:
  2601. {
  2602. nextentity = reader->getString();
  2603. DBG( nextentity ); DBG( "\n" );
  2604. if( applyExt )
  2605. trace.applyExtrusion();
  2606. iface->addTrace( trace );
  2607. return true; // found new entity or ENDSEC, terminate
  2608. }
  2609. default:
  2610. trace.parseCode( code, reader );
  2611. break;
  2612. }
  2613. }
  2614. return true;
  2615. }
  2616. bool dxfRW::processSolid()
  2617. {
  2618. DBG( "dxfRW::processSolid" );
  2619. int code;
  2620. DRW_Solid solid;
  2621. while( reader->readRec( &code, !binary ) )
  2622. {
  2623. DBG( code ); DBG( "\n" );
  2624. switch( code )
  2625. {
  2626. case 0:
  2627. {
  2628. nextentity = reader->getString();
  2629. DBG( nextentity ); DBG( "\n" );
  2630. if( applyExt )
  2631. solid.applyExtrusion();
  2632. iface->addSolid( solid );
  2633. return true; // found new entity or ENDSEC, terminate
  2634. }
  2635. default:
  2636. solid.parseCode( code, reader );
  2637. break;
  2638. }
  2639. }
  2640. return true;
  2641. }
  2642. bool dxfRW::process3dface()
  2643. {
  2644. DBG( "dxfRW::process3dface" );
  2645. int code;
  2646. DRW_3Dface face;
  2647. while( reader->readRec( &code, !binary ) )
  2648. {
  2649. DBG( code ); DBG( "\n" );
  2650. switch( code )
  2651. {
  2652. case 0:
  2653. {
  2654. nextentity = reader->getString();
  2655. DBG( nextentity ); DBG( "\n" );
  2656. iface->add3dFace( face );
  2657. return true; // found new entity or ENDSEC, terminate
  2658. }
  2659. default:
  2660. face.parseCode( code, reader );
  2661. break;
  2662. }
  2663. }
  2664. return true;
  2665. }
  2666. bool dxfRW::processViewport()
  2667. {
  2668. DBG( "dxfRW::processViewport" );
  2669. int code;
  2670. DRW_Viewport vp;
  2671. while( reader->readRec( &code, !binary ) )
  2672. {
  2673. DBG( code ); DBG( "\n" );
  2674. switch( code )
  2675. {
  2676. case 0:
  2677. {
  2678. nextentity = reader->getString();
  2679. DBG( nextentity ); DBG( "\n" );
  2680. iface->addViewport( vp );
  2681. return true; // found new entity or ENDSEC, terminate
  2682. }
  2683. default:
  2684. vp.parseCode( code, reader );
  2685. break;
  2686. }
  2687. }
  2688. return true;
  2689. }
  2690. bool dxfRW::processPoint()
  2691. {
  2692. DBG( "dxfRW::processPoint\n" );
  2693. int code;
  2694. DRW_Point point;
  2695. while( reader->readRec( &code, !binary ) )
  2696. {
  2697. DBG( code ); DBG( "\n" );
  2698. switch( code )
  2699. {
  2700. case 0:
  2701. {
  2702. nextentity = reader->getString();
  2703. DBG( nextentity ); DBG( "\n" );
  2704. iface->addPoint( point );
  2705. return true; // found new entity or ENDSEC, terminate
  2706. }
  2707. default:
  2708. point.parseCode( code, reader );
  2709. break;
  2710. }
  2711. }
  2712. return true;
  2713. }
  2714. bool dxfRW::processLine()
  2715. {
  2716. DBG( "dxfRW::processLine\n" );
  2717. int code;
  2718. DRW_Line line;
  2719. while( reader->readRec( &code, !binary ) )
  2720. {
  2721. DBG( code ); DBG( "\n" );
  2722. switch( code )
  2723. {
  2724. case 0:
  2725. {
  2726. nextentity = reader->getString();
  2727. DBG( nextentity ); DBG( "\n" );
  2728. iface->addLine( line );
  2729. return true; // found new entity or ENDSEC, terminate
  2730. }
  2731. default:
  2732. line.parseCode( code, reader );
  2733. break;
  2734. }
  2735. }
  2736. return true;
  2737. }
  2738. bool dxfRW::processRay()
  2739. {
  2740. DBG( "dxfRW::processRay\n" );
  2741. int code;
  2742. DRW_Ray line;
  2743. while( reader->readRec( &code, !binary ) )
  2744. {
  2745. DBG( code ); DBG( "\n" );
  2746. switch( code )
  2747. {
  2748. case 0:
  2749. {
  2750. nextentity = reader->getString();
  2751. DBG( nextentity ); DBG( "\n" );
  2752. iface->addRay( line );
  2753. return true; // found new entity or ENDSEC, terminate
  2754. }
  2755. default:
  2756. line.parseCode( code, reader );
  2757. break;
  2758. }
  2759. }
  2760. return true;
  2761. }
  2762. bool dxfRW::processXline()
  2763. {
  2764. DBG( "dxfRW::processXline\n" );
  2765. int code;
  2766. DRW_Xline line;
  2767. while( reader->readRec( &code, !binary ) )
  2768. {
  2769. DBG( code ); DBG( "\n" );
  2770. switch( code )
  2771. {
  2772. case 0:
  2773. {
  2774. nextentity = reader->getString();
  2775. DBG( nextentity ); DBG( "\n" );
  2776. iface->addXline( line );
  2777. return true; // found new entity or ENDSEC, terminate
  2778. }
  2779. default:
  2780. line.parseCode( code, reader );
  2781. break;
  2782. }
  2783. }
  2784. return true;
  2785. }
  2786. bool dxfRW::processCircle()
  2787. {
  2788. DBG( "dxfRW::processPoint\n" );
  2789. int code;
  2790. DRW_Circle circle;
  2791. while( reader->readRec( &code, !binary ) )
  2792. {
  2793. DBG( code ); DBG( "\n" );
  2794. switch( code )
  2795. {
  2796. case 0:
  2797. {
  2798. nextentity = reader->getString();
  2799. DBG( nextentity ); DBG( "\n" );
  2800. if( applyExt )
  2801. circle.applyExtrusion();
  2802. iface->addCircle( circle );
  2803. return true; // found new entity or ENDSEC, terminate
  2804. }
  2805. default:
  2806. circle.parseCode( code, reader );
  2807. break;
  2808. }
  2809. }
  2810. return true;
  2811. }
  2812. bool dxfRW::processArc()
  2813. {
  2814. DBG( "dxfRW::processPoint\n" );
  2815. int code;
  2816. DRW_Arc arc;
  2817. while( reader->readRec( &code, !binary ) )
  2818. {
  2819. DBG( code ); DBG( "\n" );
  2820. switch( code )
  2821. {
  2822. case 0:
  2823. {
  2824. nextentity = reader->getString();
  2825. DBG( nextentity ); DBG( "\n" );
  2826. if( applyExt )
  2827. arc.applyExtrusion();
  2828. iface->addArc( arc );
  2829. return true; // found new entity or ENDSEC, terminate
  2830. }
  2831. default:
  2832. arc.parseCode( code, reader );
  2833. break;
  2834. }
  2835. }
  2836. return true;
  2837. }
  2838. bool dxfRW::processInsert()
  2839. {
  2840. DBG( "dxfRW::processInsert" );
  2841. int code;
  2842. DRW_Insert insert;
  2843. while( reader->readRec( &code, !binary ) )
  2844. {
  2845. DBG( code ); DBG( "\n" );
  2846. switch( code )
  2847. {
  2848. case 0:
  2849. {
  2850. nextentity = reader->getString();
  2851. DBG( nextentity ); DBG( "\n" );
  2852. iface->addInsert( insert );
  2853. return true; // found new entity or ENDSEC, terminate
  2854. }
  2855. default:
  2856. insert.parseCode( code, reader );
  2857. break;
  2858. }
  2859. }
  2860. return true;
  2861. }
  2862. bool dxfRW::processLWPolyline()
  2863. {
  2864. DBG( "dxfRW::processLWPolyline" );
  2865. int code;
  2866. DRW_LWPolyline pl;
  2867. while( reader->readRec( &code, !binary ) )
  2868. {
  2869. DBG( code ); DBG( "\n" );
  2870. switch( code )
  2871. {
  2872. case 0:
  2873. {
  2874. nextentity = reader->getString();
  2875. DBG( nextentity ); DBG( "\n" );
  2876. if( applyExt )
  2877. pl.applyExtrusion();
  2878. iface->addLWPolyline( pl );
  2879. return true; // found new entity or ENDSEC, terminate
  2880. }
  2881. default:
  2882. pl.parseCode( code, reader );
  2883. break;
  2884. }
  2885. }
  2886. return true;
  2887. }
  2888. bool dxfRW::processPolyline()
  2889. {
  2890. DBG( "dxfRW::processPolyline" );
  2891. int code;
  2892. DRW_Polyline pl;
  2893. while( reader->readRec( &code, !binary ) )
  2894. {
  2895. DBG( code ); DBG( "\n" );
  2896. switch( code )
  2897. {
  2898. case 0:
  2899. {
  2900. nextentity = reader->getString();
  2901. DBG( nextentity ); DBG( "\n" );
  2902. if( nextentity != "VERTEX" )
  2903. {
  2904. iface->addPolyline( pl );
  2905. return true; // found new entity or ENDSEC, terminate
  2906. }
  2907. else
  2908. {
  2909. processVertex( &pl );
  2910. }
  2911. }
  2912. // Fall through
  2913. default:
  2914. pl.parseCode( code, reader );
  2915. break;
  2916. }
  2917. }
  2918. return true;
  2919. }
  2920. bool dxfRW::processVertex( DRW_Polyline* pl )
  2921. {
  2922. DBG( "dxfRW::processVertex" );
  2923. int code;
  2924. DRW_Vertex* v = new DRW_Vertex();
  2925. while( reader->readRec( &code, !binary ) )
  2926. {
  2927. DBG( code ); DBG( "\n" );
  2928. switch( code )
  2929. {
  2930. case 0:
  2931. {
  2932. pl->appendVertex( v );
  2933. nextentity = reader->getString();
  2934. DBG( nextentity ); DBG( "\n" );
  2935. if( nextentity == "SEQEND" )
  2936. {
  2937. return true; // found SEQEND no more vertex, terminate
  2938. }
  2939. else if( nextentity == "VERTEX" )
  2940. {
  2941. v = new DRW_Vertex(); // another vertex
  2942. }
  2943. }
  2944. // Fall through
  2945. default:
  2946. v->parseCode( code, reader );
  2947. break;
  2948. }
  2949. }
  2950. return true;
  2951. }
  2952. bool dxfRW::processText()
  2953. {
  2954. DBG( "dxfRW::processText" );
  2955. int code;
  2956. DRW_Text txt;
  2957. while( reader->readRec( &code, !binary ) )
  2958. {
  2959. DBG( code ); DBG( "\n" );
  2960. switch( code )
  2961. {
  2962. case 0:
  2963. {
  2964. nextentity = reader->getString();
  2965. DBG( nextentity ); DBG( "\n" );
  2966. iface->addText( txt );
  2967. return true; // found new entity or ENDSEC, terminate
  2968. }
  2969. default:
  2970. txt.parseCode( code, reader );
  2971. break;
  2972. }
  2973. }
  2974. return true;
  2975. }
  2976. bool dxfRW::processMText()
  2977. {
  2978. DBG( "dxfRW::processMText" );
  2979. int code;
  2980. DRW_MText txt;
  2981. while( reader->readRec( &code, !binary ) )
  2982. {
  2983. DBG( code ); DBG( "\n" );
  2984. switch( code )
  2985. {
  2986. case 0:
  2987. {
  2988. nextentity = reader->getString();
  2989. DBG( nextentity ); DBG( "\n" );
  2990. txt.updateAngle();
  2991. iface->addMText( txt );
  2992. return true; // found new entity or ENDSEC, terminate
  2993. }
  2994. default:
  2995. txt.parseCode( code, reader );
  2996. break;
  2997. }
  2998. }
  2999. return true;
  3000. }
  3001. bool dxfRW::processHatch()
  3002. {
  3003. DBG( "dxfRW::processHatch" );
  3004. int code;
  3005. DRW_Hatch hatch;
  3006. while( reader->readRec( &code, !binary ) )
  3007. {
  3008. DBG( code ); DBG( "\n" );
  3009. switch( code )
  3010. {
  3011. case 0:
  3012. {
  3013. nextentity = reader->getString();
  3014. DBG( nextentity ); DBG( "\n" );
  3015. iface->addHatch( &hatch );
  3016. return true; // found new entity or ENDSEC, terminate
  3017. }
  3018. default:
  3019. hatch.parseCode( code, reader );
  3020. break;
  3021. }
  3022. }
  3023. return true;
  3024. }
  3025. bool dxfRW::processSpline()
  3026. {
  3027. DBG( "dxfRW::processSpline" );
  3028. int code;
  3029. DRW_Spline sp;
  3030. while( reader->readRec( &code, !binary ) )
  3031. {
  3032. DBG( code ); DBG( "\n" );
  3033. switch( code )
  3034. {
  3035. case 0:
  3036. {
  3037. nextentity = reader->getString();
  3038. DBG( nextentity ); DBG( "\n" );
  3039. iface->addSpline( &sp );
  3040. return true; // found new entity or ENDSEC, terminate
  3041. }
  3042. default:
  3043. sp.parseCode( code, reader );
  3044. break;
  3045. }
  3046. }
  3047. return true;
  3048. }
  3049. bool dxfRW::processImage()
  3050. {
  3051. DBG( "dxfRW::processImage" );
  3052. int code;
  3053. DRW_Image img;
  3054. while( reader->readRec( &code, !binary ) )
  3055. {
  3056. DBG( code ); DBG( "\n" );
  3057. switch( code )
  3058. {
  3059. case 0:
  3060. {
  3061. nextentity = reader->getString();
  3062. DBG( nextentity ); DBG( "\n" );
  3063. iface->addImage( &img );
  3064. return true; // found new entity or ENDSEC, terminate
  3065. }
  3066. default:
  3067. img.parseCode( code, reader );
  3068. break;
  3069. }
  3070. }
  3071. return true;
  3072. }
  3073. bool dxfRW::processDimension()
  3074. {
  3075. DBG( "dxfRW::processDimension" );
  3076. int code;
  3077. DRW_Dimension dim;
  3078. while( reader->readRec( &code, !binary ) )
  3079. {
  3080. DBG( code ); DBG( "\n" );
  3081. switch( code )
  3082. {
  3083. case 0:
  3084. {
  3085. nextentity = reader->getString();
  3086. DBG( nextentity ); DBG( "\n" );
  3087. int type = dim.type & 0x0F;
  3088. switch( type )
  3089. {
  3090. case 0:
  3091. {
  3092. DRW_DimLinear d( dim );
  3093. iface->addDimLinear( &d );
  3094. break;
  3095. }
  3096. case 1:
  3097. {
  3098. DRW_DimAligned d( dim );
  3099. iface->addDimAlign( &d );
  3100. break;
  3101. }
  3102. case 2:
  3103. {
  3104. DRW_DimAngular d( dim );
  3105. iface->addDimAngular( &d );
  3106. break;
  3107. }
  3108. case 3:
  3109. {
  3110. DRW_DimDiametric d( dim );
  3111. iface->addDimDiametric( &d );
  3112. break;
  3113. }
  3114. case 4:
  3115. {
  3116. DRW_DimRadial d( dim );
  3117. iface->addDimRadial( &d );
  3118. break;
  3119. }
  3120. case 5:
  3121. {
  3122. DRW_DimAngular3p d( dim );
  3123. iface->addDimAngular3P( &d );
  3124. break;
  3125. }
  3126. case 6:
  3127. {
  3128. DRW_DimOrdinate d( dim );
  3129. iface->addDimOrdinate( &d );
  3130. break;
  3131. }
  3132. }
  3133. return true; // found new entity or ENDSEC, terminate
  3134. }
  3135. default:
  3136. dim.parseCode( code, reader );
  3137. break;
  3138. }
  3139. }
  3140. return true;
  3141. }
  3142. bool dxfRW::processLeader()
  3143. {
  3144. DBG( "dxfRW::processLeader" );
  3145. int code;
  3146. DRW_Leader leader;
  3147. while( reader->readRec( &code, !binary ) )
  3148. {
  3149. DBG( code ); DBG( "\n" );
  3150. switch( code )
  3151. {
  3152. case 0:
  3153. {
  3154. nextentity = reader->getString();
  3155. DBG( nextentity ); DBG( "\n" );
  3156. iface->addLeader( &leader );
  3157. return true; // found new entity or ENDSEC, terminate
  3158. }
  3159. default:
  3160. leader.parseCode( code, reader );
  3161. break;
  3162. }
  3163. }
  3164. return true;
  3165. }
  3166. /********* Objects Section *********/
  3167. bool dxfRW::processObjects()
  3168. {
  3169. DBG( "dxfRW::processObjects\n" );
  3170. int code;
  3171. if( !reader->readRec( &code, !binary ) )
  3172. {
  3173. return false;
  3174. }
  3175. bool next = true;
  3176. if( code == 0 )
  3177. {
  3178. nextentity = reader->getString();
  3179. }
  3180. else
  3181. {
  3182. return false; // first record in objects is 0
  3183. }
  3184. do {
  3185. if( nextentity == "ENDSEC" )
  3186. {
  3187. return true; // found ENDSEC terminate
  3188. }
  3189. else if( nextentity == "IMAGEDEF" )
  3190. {
  3191. processImageDef();
  3192. }
  3193. else
  3194. {
  3195. if( reader->readRec( &code, !binary ) )
  3196. {
  3197. if( code == 0 )
  3198. nextentity = reader->getString();
  3199. }
  3200. else
  3201. return false; // end of file without ENDSEC
  3202. }
  3203. } while( next );
  3204. return true;
  3205. }
  3206. bool dxfRW::processImageDef()
  3207. {
  3208. DBG( "dxfRW::processImageDef" );
  3209. int code;
  3210. DRW_ImageDef img;
  3211. while( reader->readRec( &code, !binary ) )
  3212. {
  3213. DBG( code ); DBG( "\n" );
  3214. switch( code )
  3215. {
  3216. case 0:
  3217. {
  3218. nextentity = reader->getString();
  3219. DBG( nextentity ); DBG( "\n" );
  3220. iface->linkImage( &img );
  3221. return true; // found new entity or ENDSEC, terminate
  3222. }
  3223. default:
  3224. img.parseCode( code, reader );
  3225. break;
  3226. }
  3227. }
  3228. return true;
  3229. }
  3230. /** utility function
  3231. * convert a int to string in hex
  3232. **/
  3233. std::string dxfRW::toHexStr( int n )
  3234. {
  3235. #if defined(__APPLE__)
  3236. char buffer[9] = { '\0' };
  3237. snprintf( buffer, sizeof(buffer), "%X", n );
  3238. return std::string( buffer );
  3239. #else
  3240. std::ostringstream Convert;
  3241. Convert << std::uppercase << std::hex << n;
  3242. return Convert.str();
  3243. #endif
  3244. }