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.

881 lines
27 KiB

  1. /**
  2. * @file common_plotDXF_functions.cpp
  3. * @brief KiCad: Common plot DXF Routines.
  4. */
  5. /*
  6. * This program source code file is part of KiCad, a free EDA CAD application.
  7. *
  8. * Copyright (C) 2016 KiCad Developers, see CHANGELOG.TXT for contributors.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, you may find one here:
  22. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. * or you may search the http://www.gnu.org website for the version 2 license,
  24. * or you may write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  26. */
  27. #include <fctsys.h>
  28. #include <gr_basic.h>
  29. #include <trigo.h>
  30. #include <wxstruct.h>
  31. #include <base_struct.h>
  32. #include <plot_common.h>
  33. #include <macros.h>
  34. #include <kicad_string.h>
  35. #include <convert_basic_shapes_to_polygon.h>
  36. /**
  37. * Oblique angle for DXF native text
  38. * (I don't remember if 15 degrees is the ISO value... it looks nice anyway)
  39. */
  40. static const double DXF_OBLIQUE_ANGLE = 15;
  41. /* The layer/colors palette. The acad/DXF palette is divided in 3 zones:
  42. - The primary colors (1 - 9)
  43. - An HSV zone (10-250, 5 values x 2 saturations x 10 hues
  44. - Greys (251 - 255)
  45. There is *no* black... the white does it on paper, usually, and
  46. anyway it depends on the plotter configuration, since DXF colors
  47. are meant to be logical only (they represent *both* line color and
  48. width); later version with plot styles only complicate the matter!
  49. As usual, brown and magenta/purple are difficult to place since
  50. they are actually variations of other colors.
  51. */
  52. static const struct
  53. {
  54. const char *name;
  55. int color;
  56. } dxf_layer[NBCOLORS] =
  57. {
  58. { "BLACK", 7 }, // In DXF, color 7 is *both* white and black!
  59. { "GRAY1", 251 },
  60. { "GRAY2", 8 },
  61. { "GRAY3", 9 },
  62. { "WHITE", 7 },
  63. { "LYELLOW", 51 },
  64. { "BLUE1", 178 },
  65. { "GREEN1", 98 },
  66. { "CYAN1", 138 },
  67. { "RED1", 18 },
  68. { "MAGENTA1", 228 },
  69. { "BROWN1", 58 },
  70. { "BLUE2", 5 },
  71. { "GREEN2", 3 },
  72. { "CYAN2", 4 },
  73. { "RED2", 1 },
  74. { "MAGENTA2", 6 },
  75. { "BROWN2", 54 },
  76. { "BLUE3", 171 },
  77. { "GREEN3", 91 },
  78. { "CYAN3", 131 },
  79. { "RED3", 11 },
  80. { "MAGENTA3", 221 },
  81. { "YELLOW3", 2 },
  82. { "BLUE4", 5 },
  83. { "GREEN4", 3 },
  84. { "CYAN4", 4 },
  85. { "RED4", 1 },
  86. { "MAGENTA4", 6 },
  87. { "YELLOW4", 2 }
  88. };
  89. // A helper function to create a color name acceptable in DXF files
  90. // DXF files do not use a RGB definition
  91. static wxString getDXFColorName( COLOR4D aColor )
  92. {
  93. EDA_COLOR_T color = ColorFindNearest( int( aColor.r*255 ),
  94. int( aColor.g*255 ),
  95. int( aColor.b*255 ) );
  96. wxString cname( dxf_layer[color].name );
  97. return cname;
  98. }
  99. /**
  100. * Set the scale/position for the DXF plot
  101. * The DXF engine doesn't support line widths and mirroring. The output
  102. * coordinate system is in the first quadrant (in mm)
  103. */
  104. void DXF_PLOTTER::SetViewport( const wxPoint& aOffset, double aIusPerDecimil,
  105. double aScale, bool aMirror )
  106. {
  107. plotOffset = aOffset;
  108. plotScale = aScale;
  109. /* DXF paper is 'virtual' so there is no need of a paper size.
  110. Also this way we can handle the aux origin which can be useful
  111. (for example when aligning to a mechanical drawing) */
  112. paperSize.x = 0;
  113. paperSize.y = 0;
  114. /* Like paper size DXF units are abstract too. Anyway there is a
  115. * system variable (MEASUREMENT) which will be set to 1 to indicate
  116. * metric units */
  117. m_IUsPerDecimil = aIusPerDecimil;
  118. iuPerDeviceUnit = 1.0 / aIusPerDecimil; // Gives a DXF in decimils
  119. iuPerDeviceUnit *= 0.00254; // ... now in mm
  120. SetDefaultLineWidth( 0 ); // No line width on DXF
  121. m_plotMirror = false; // No mirroring on DXF
  122. m_currentColor = COLOR4D::BLACK;
  123. }
  124. /**
  125. * Opens the DXF plot with a skeleton header
  126. */
  127. bool DXF_PLOTTER::StartPlot()
  128. {
  129. wxASSERT( outputFile );
  130. // DXF HEADER - Boilerplate
  131. // Defines the minimum for drawing i.e. the angle system and the
  132. // continuous linetype
  133. fputs( " 0\n"
  134. "SECTION\n"
  135. " 2\n"
  136. "HEADER\n"
  137. " 9\n"
  138. "$ANGBASE\n"
  139. " 50\n"
  140. "0.0\n"
  141. " 9\n"
  142. "$ANGDIR\n"
  143. " 70\n"
  144. " 1\n"
  145. " 9\n"
  146. "$MEASUREMENT\n"
  147. " 70\n"
  148. "0\n"
  149. " 0\n" // This means 'metric units'
  150. "ENDSEC\n"
  151. " 0\n"
  152. "SECTION\n"
  153. " 2\n"
  154. "TABLES\n"
  155. " 0\n"
  156. "TABLE\n"
  157. " 2\n"
  158. "LTYPE\n"
  159. " 70\n"
  160. "1\n"
  161. " 0\n"
  162. "LTYPE\n"
  163. " 2\n"
  164. "CONTINUOUS\n"
  165. " 70\n"
  166. "0\n"
  167. " 3\n"
  168. "Solid line\n"
  169. " 72\n"
  170. "65\n"
  171. " 73\n"
  172. "0\n"
  173. " 40\n"
  174. "0.0\n"
  175. " 0\n"
  176. "ENDTAB\n",
  177. outputFile );
  178. // Text styles table
  179. // Defines 4 text styles, one for each bold/italic combination
  180. fputs( " 0\n"
  181. "TABLE\n"
  182. " 2\n"
  183. "STYLE\n"
  184. " 70\n"
  185. "4\n", outputFile );
  186. static const char *style_name[4] = {"KICAD", "KICADB", "KICADI", "KICADBI"};
  187. for(int i = 0; i < 4; i++ )
  188. {
  189. fprintf( outputFile,
  190. " 0\n"
  191. "STYLE\n"
  192. " 2\n"
  193. "%s\n" // Style name
  194. " 70\n"
  195. "0\n" // Standard flags
  196. " 40\n"
  197. "0\n" // Non-fixed height text
  198. " 41\n"
  199. "1\n" // Width factor (base)
  200. " 42\n"
  201. "1\n" // Last height (mandatory)
  202. " 50\n"
  203. "%g\n" // Oblique angle
  204. " 71\n"
  205. "0\n" // Generation flags (default)
  206. " 3\n"
  207. // The standard ISO font (when kicad is build with it
  208. // the dxf text in acad matches *perfectly*)
  209. "isocp.shx\n", // Font name (when not bigfont)
  210. // Apply a 15 degree angle to italic text
  211. style_name[i], i < 2 ? 0 : DXF_OBLIQUE_ANGLE );
  212. }
  213. // Layer table - one layer per color
  214. fprintf( outputFile,
  215. " 0\n"
  216. "ENDTAB\n"
  217. " 0\n"
  218. "TABLE\n"
  219. " 2\n"
  220. "LAYER\n"
  221. " 70\n"
  222. "%d\n", NBCOLORS );
  223. /* The layer/colors palette. The acad/DXF palette is divided in 3 zones:
  224. - The primary colors (1 - 9)
  225. - An HSV zone (10-250, 5 values x 2 saturations x 10 hues
  226. - Greys (251 - 255)
  227. */
  228. for( EDA_COLOR_T i = BLACK; i < NBCOLORS; i = NextColor(i) )
  229. {
  230. fprintf( outputFile,
  231. " 0\n"
  232. "LAYER\n"
  233. " 2\n"
  234. "%s\n" // Layer name
  235. " 70\n"
  236. "0\n" // Standard flags
  237. " 62\n"
  238. "%d\n" // Color number
  239. " 6\n"
  240. "CONTINUOUS\n",// Linetype name
  241. dxf_layer[i].name, dxf_layer[i].color );
  242. }
  243. // End of layer table, begin entities
  244. fputs( " 0\n"
  245. "ENDTAB\n"
  246. " 0\n"
  247. "ENDSEC\n"
  248. " 0\n"
  249. "SECTION\n"
  250. " 2\n"
  251. "ENTITIES\n", outputFile );
  252. return true;
  253. }
  254. bool DXF_PLOTTER::EndPlot()
  255. {
  256. wxASSERT( outputFile );
  257. // DXF FOOTER
  258. fputs( " 0\n"
  259. "ENDSEC\n"
  260. " 0\n"
  261. "EOF\n", outputFile );
  262. fclose( outputFile );
  263. outputFile = NULL;
  264. return true;
  265. }
  266. /**
  267. * The DXF exporter handles 'colors' as layers...
  268. */
  269. void DXF_PLOTTER::SetColor( COLOR4D color )
  270. {
  271. if( ( colorMode )
  272. || ( color == COLOR4D::BLACK )
  273. || ( color == COLOR4D::WHITE ) )
  274. {
  275. m_currentColor = color;
  276. }
  277. else
  278. m_currentColor = COLOR4D::BLACK;
  279. }
  280. /**
  281. * DXF rectangle: fill not supported
  282. */
  283. void DXF_PLOTTER::Rect( const wxPoint& p1, const wxPoint& p2, FILL_T fill, int width )
  284. {
  285. wxASSERT( outputFile );
  286. MoveTo( p1 );
  287. LineTo( wxPoint( p1.x, p2.y ) );
  288. LineTo( wxPoint( p2.x, p2.y ) );
  289. LineTo( wxPoint( p2.x, p1.y ) );
  290. FinishTo( wxPoint( p1.x, p1.y ) );
  291. }
  292. /**
  293. * DXF circle: full functionality; it even does 'fills' drawing a
  294. * circle with a dual-arc polyline wide as the radius.
  295. *
  296. * I could use this trick to do other filled primitives
  297. */
  298. void DXF_PLOTTER::Circle( const wxPoint& centre, int diameter, FILL_T fill, int width )
  299. {
  300. wxASSERT( outputFile );
  301. double radius = userToDeviceSize( diameter / 2 );
  302. DPOINT centre_dev = userToDeviceCoordinates( centre );
  303. if( radius > 0 )
  304. {
  305. wxString cname = getDXFColorName( m_currentColor );
  306. if( !fill )
  307. {
  308. fprintf( outputFile, "0\nCIRCLE\n8\n%s\n10\n%g\n20\n%g\n40\n%g\n",
  309. TO_UTF8( cname ),
  310. centre_dev.x, centre_dev.y, radius );
  311. }
  312. if( fill == FILLED_SHAPE )
  313. {
  314. double r = radius*0.5;
  315. fprintf( outputFile, "0\nPOLYLINE\n");
  316. fprintf( outputFile, "8\n%s\n66\n1\n70\n1\n", TO_UTF8( cname ));
  317. fprintf( outputFile, "40\n%g\n41\n%g\n", radius, radius);
  318. fprintf( outputFile, "0\nVERTEX\n8\n%s\n", TO_UTF8( cname ));
  319. fprintf( outputFile, "10\n%g\n 20\n%g\n42\n1.0\n",
  320. centre_dev.x-r, centre_dev.y );
  321. fprintf( outputFile, "0\nVERTEX\n8\n%s\n", TO_UTF8( cname ));
  322. fprintf( outputFile, "10\n%g\n 20\n%g\n42\n1.0\n",
  323. centre_dev.x+r, centre_dev.y );
  324. fprintf( outputFile, "0\nSEQEND\n");
  325. }
  326. }
  327. }
  328. /**
  329. * DXF polygon: doesn't fill it but at least it close the filled ones
  330. * DXF does not know thick outline.
  331. * It does not know thhick segments, therefore filled polygons with thick outline
  332. * are converted to inflated polygon by aWidth/2
  333. */
  334. void DXF_PLOTTER::PlotPoly( const std::vector<wxPoint>& aCornerList,
  335. FILL_T aFill, int aWidth, void * aData )
  336. {
  337. if( aCornerList.size() <= 1 )
  338. return;
  339. unsigned last = aCornerList.size() - 1;
  340. // Plot outlines with lines (thickness = 0) to define the polygon
  341. if( aWidth <= 0 )
  342. {
  343. MoveTo( aCornerList[0] );
  344. for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
  345. LineTo( aCornerList[ii] );
  346. // Close polygon if 'fill' requested
  347. if( aFill )
  348. {
  349. if( aCornerList[last] != aCornerList[0] )
  350. LineTo( aCornerList[0] );
  351. }
  352. PenFinish();
  353. return;
  354. }
  355. // if the polygon outline has thickness, and is not filled
  356. // (i.e. is a polyline) plot outlines with thick segments
  357. if( aWidth > 0 && !aFill )
  358. {
  359. MoveTo( aCornerList[0] );
  360. for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
  361. ThickSegment( aCornerList[ii-1], aCornerList[ii],
  362. aWidth, FILLED, NULL );
  363. return;
  364. }
  365. // The polygon outline has thickness, and is filled
  366. // Build and plot the polygon which contains the initial
  367. // polygon and its thick outline
  368. SHAPE_POLY_SET bufferOutline;
  369. SHAPE_POLY_SET bufferPolybase;
  370. const int circleToSegmentsCount = 16;
  371. bufferPolybase.NewOutline();
  372. // enter outline as polygon:
  373. for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
  374. {
  375. TransformRoundedEndsSegmentToPolygon( bufferOutline,
  376. aCornerList[ii-1], aCornerList[ii], circleToSegmentsCount, aWidth );
  377. }
  378. // enter the initial polygon:
  379. for( unsigned ii = 0; ii < aCornerList.size(); ii++ )
  380. {
  381. bufferPolybase.Append( aCornerList[ii] );
  382. }
  383. // Merge polygons to build the polygon which contains the initial
  384. // polygon and its thick outline
  385. // create the outline which contains thick outline:
  386. bufferPolybase.BooleanAdd( bufferOutline, SHAPE_POLY_SET::PM_FAST );
  387. bufferPolybase.Fracture( SHAPE_POLY_SET::PM_FAST );
  388. if( bufferPolybase.OutlineCount() < 1 ) // should not happen
  389. return;
  390. const SHAPE_LINE_CHAIN& path = bufferPolybase.COutline( 0 );
  391. if( path.PointCount() < 2 ) // should not happen
  392. return;
  393. // Now, output the final polygon to DXF file:
  394. last = path.PointCount() - 1;
  395. VECTOR2I point = path.CPoint( 0 );
  396. wxPoint startPoint( point.x, point.y );
  397. MoveTo( startPoint );
  398. for( int ii = 1; ii < path.PointCount(); ii++ )
  399. {
  400. point = path.CPoint( ii );
  401. LineTo( wxPoint( point.x, point.y ) );
  402. }
  403. // Close polygon, if needed
  404. point = path.CPoint( last );
  405. wxPoint endPoint( point.x, point.y );
  406. if( endPoint != startPoint )
  407. LineTo( startPoint );
  408. PenFinish();
  409. }
  410. void DXF_PLOTTER::PenTo( const wxPoint& pos, char plume )
  411. {
  412. wxASSERT( outputFile );
  413. if( plume == 'Z' )
  414. {
  415. return;
  416. }
  417. DPOINT pos_dev = userToDeviceCoordinates( pos );
  418. DPOINT pen_lastpos_dev = userToDeviceCoordinates( penLastpos );
  419. if( penLastpos != pos && plume == 'D' )
  420. {
  421. // DXF LINE
  422. wxString cname = getDXFColorName( m_currentColor );
  423. fprintf( outputFile, "0\nLINE\n8\n%s\n10\n%g\n20\n%g\n11\n%g\n21\n%g\n",
  424. TO_UTF8( cname ),
  425. pen_lastpos_dev.x, pen_lastpos_dev.y, pos_dev.x, pos_dev.y );
  426. }
  427. penLastpos = pos;
  428. }
  429. /**
  430. * Dashed lines are not (yet) supported by DXF_PLOTTER
  431. */
  432. void DXF_PLOTTER::SetDash( bool dashed )
  433. {
  434. // NOP for now
  435. }
  436. void DXF_PLOTTER::ThickSegment( const wxPoint& aStart, const wxPoint& aEnd, int aWidth,
  437. EDA_DRAW_MODE_T aPlotMode, void* aData )
  438. {
  439. MoveTo( aStart );
  440. FinishTo( aEnd );
  441. }
  442. /* Plot an arc in DXF format
  443. * Filling is not supported
  444. */
  445. void DXF_PLOTTER::Arc( const wxPoint& centre, double StAngle, double EndAngle, int radius,
  446. FILL_T fill, int width )
  447. {
  448. wxASSERT( outputFile );
  449. if( radius <= 0 )
  450. return;
  451. // In DXF, arcs are drawn CCW.
  452. // In Kicad, arcs are CW or CCW
  453. // If StAngle > EndAngle, it is CW. So transform it to CCW
  454. if( StAngle > EndAngle )
  455. {
  456. std::swap( StAngle, EndAngle );
  457. }
  458. DPOINT centre_dev = userToDeviceCoordinates( centre );
  459. double radius_dev = userToDeviceSize( radius );
  460. // Emit a DXF ARC entity
  461. wxString cname = getDXFColorName( m_currentColor );
  462. fprintf( outputFile,
  463. "0\nARC\n8\n%s\n10\n%g\n20\n%g\n40\n%g\n50\n%g\n51\n%g\n",
  464. TO_UTF8( cname ),
  465. centre_dev.x, centre_dev.y, radius_dev,
  466. StAngle / 10.0, EndAngle / 10.0 );
  467. }
  468. /**
  469. * DXF oval pad: always done in sketch mode
  470. */
  471. void DXF_PLOTTER::FlashPadOval( const wxPoint& pos, const wxSize& aSize, double orient,
  472. EDA_DRAW_MODE_T trace_mode, void* aData )
  473. {
  474. wxASSERT( outputFile );
  475. wxSize size( aSize );
  476. /* The chip is reduced to an oval tablet with size.y > size.x
  477. * (Oval vertical orientation 0) */
  478. if( size.x > size.y )
  479. {
  480. std::swap( size.x, size.y );
  481. orient = AddAngles( orient, 900 );
  482. }
  483. sketchOval( pos, size, orient, -1 );
  484. }
  485. /**
  486. * DXF round pad: always done in sketch mode; it could be filled but it isn't
  487. * pretty if other kinds of pad aren't...
  488. */
  489. void DXF_PLOTTER::FlashPadCircle( const wxPoint& pos, int diametre,
  490. EDA_DRAW_MODE_T trace_mode, void* aData )
  491. {
  492. wxASSERT( outputFile );
  493. Circle( pos, diametre, NO_FILL );
  494. }
  495. /**
  496. * DXF rectangular pad: alwayd done in sketch mode
  497. */
  498. void DXF_PLOTTER::FlashPadRect( const wxPoint& pos, const wxSize& padsize,
  499. double orient, EDA_DRAW_MODE_T trace_mode, void* aData )
  500. {
  501. wxASSERT( outputFile );
  502. wxSize size;
  503. int ox, oy, fx, fy;
  504. size.x = padsize.x / 2;
  505. size.y = padsize.y / 2;
  506. if( size.x < 0 )
  507. size.x = 0;
  508. if( size.y < 0 )
  509. size.y = 0;
  510. // If a dimension is zero, the trace is reduced to 1 line
  511. if( size.x == 0 )
  512. {
  513. ox = pos.x;
  514. oy = pos.y - size.y;
  515. RotatePoint( &ox, &oy, pos.x, pos.y, orient );
  516. fx = pos.x;
  517. fy = pos.y + size.y;
  518. RotatePoint( &fx, &fy, pos.x, pos.y, orient );
  519. MoveTo( wxPoint( ox, oy ) );
  520. FinishTo( wxPoint( fx, fy ) );
  521. return;
  522. }
  523. if( size.y == 0 )
  524. {
  525. ox = pos.x - size.x;
  526. oy = pos.y;
  527. RotatePoint( &ox, &oy, pos.x, pos.y, orient );
  528. fx = pos.x + size.x;
  529. fy = pos.y;
  530. RotatePoint( &fx, &fy, pos.x, pos.y, orient );
  531. MoveTo( wxPoint( ox, oy ) );
  532. FinishTo( wxPoint( fx, fy ) );
  533. return;
  534. }
  535. ox = pos.x - size.x;
  536. oy = pos.y - size.y;
  537. RotatePoint( &ox, &oy, pos.x, pos.y, orient );
  538. MoveTo( wxPoint( ox, oy ) );
  539. fx = pos.x - size.x;
  540. fy = pos.y + size.y;
  541. RotatePoint( &fx, &fy, pos.x, pos.y, orient );
  542. LineTo( wxPoint( fx, fy ) );
  543. fx = pos.x + size.x;
  544. fy = pos.y + size.y;
  545. RotatePoint( &fx, &fy, pos.x, pos.y, orient );
  546. LineTo( wxPoint( fx, fy ) );
  547. fx = pos.x + size.x;
  548. fy = pos.y - size.y;
  549. RotatePoint( &fx, &fy, pos.x, pos.y, orient );
  550. LineTo( wxPoint( fx, fy ) );
  551. FinishTo( wxPoint( ox, oy ) );
  552. }
  553. void DXF_PLOTTER::FlashPadRoundRect( const wxPoint& aPadPos, const wxSize& aSize,
  554. int aCornerRadius, double aOrient,
  555. EDA_DRAW_MODE_T aTraceMode, void* aData )
  556. {
  557. SHAPE_POLY_SET outline;
  558. const int segmentToCircleCount = 64;
  559. TransformRoundRectToPolygon( outline, aPadPos, aSize, aOrient,
  560. aCornerRadius, segmentToCircleCount );
  561. // TransformRoundRectToPolygon creates only one convex polygon
  562. SHAPE_LINE_CHAIN& poly = outline.Outline( 0 );
  563. MoveTo( wxPoint( poly.Point( 0 ).x, poly.Point( 0 ).y ) );
  564. for( int ii = 1; ii < poly.PointCount(); ++ii )
  565. LineTo( wxPoint( poly.Point( ii ).x, poly.Point( ii ).y ) );
  566. FinishTo( wxPoint( poly.Point( 0 ).x, poly.Point( 0 ).y ) );
  567. }
  568. void DXF_PLOTTER::FlashPadCustom( const wxPoint& aPadPos, const wxSize& aSize,
  569. SHAPE_POLY_SET* aPolygons,
  570. EDA_DRAW_MODE_T aTraceMode, void* aData )
  571. {
  572. for( int cnt = 0; cnt < aPolygons->OutlineCount(); ++cnt )
  573. {
  574. SHAPE_LINE_CHAIN& poly = aPolygons->Outline( cnt );
  575. MoveTo( wxPoint( poly.Point( 0 ).x, poly.Point( 0 ).y ) );
  576. for( int ii = 1; ii < poly.PointCount(); ++ii )
  577. LineTo( wxPoint( poly.Point( ii ).x, poly.Point( ii ).y ) );
  578. FinishTo(wxPoint( poly.Point( 0 ).x, poly.Point( 0 ).y ) );
  579. }
  580. }
  581. /**
  582. * DXF trapezoidal pad: only sketch mode is supported
  583. */
  584. void DXF_PLOTTER::FlashPadTrapez( const wxPoint& aPadPos, const wxPoint *aCorners,
  585. double aPadOrient, EDA_DRAW_MODE_T aTrace_Mode, void* aData )
  586. {
  587. wxASSERT( outputFile );
  588. wxPoint coord[4]; /* coord actual corners of a trapezoidal trace */
  589. for( int ii = 0; ii < 4; ii++ )
  590. {
  591. coord[ii] = aCorners[ii];
  592. RotatePoint( &coord[ii], aPadOrient );
  593. coord[ii] += aPadPos;
  594. }
  595. // Plot edge:
  596. MoveTo( coord[0] );
  597. LineTo( coord[1] );
  598. LineTo( coord[2] );
  599. LineTo( coord[3] );
  600. FinishTo( coord[0] );
  601. }
  602. /**
  603. * Checks if a given string contains non-ASCII characters.
  604. * FIXME: the performance of this code is really poor, but in this case it can be
  605. * acceptable because the plot operation is not called very often.
  606. * @param string String to check
  607. * @return true if it contains some non-ASCII character, false if all characters are
  608. * inside ASCII range (<=255).
  609. */
  610. bool containsNonAsciiChars( const wxString& string )
  611. {
  612. for( unsigned i = 0; i < string.length(); i++ )
  613. {
  614. wchar_t ch = string[i];
  615. if( ch > 255 )
  616. return true;
  617. }
  618. return false;
  619. }
  620. void DXF_PLOTTER::Text( const wxPoint& aPos,
  621. COLOR4D aColor,
  622. const wxString& aText,
  623. double aOrient,
  624. const wxSize& aSize,
  625. enum EDA_TEXT_HJUSTIFY_T aH_justify,
  626. enum EDA_TEXT_VJUSTIFY_T aV_justify,
  627. int aWidth,
  628. bool aItalic,
  629. bool aBold,
  630. bool aMultilineAllowed,
  631. void* aData )
  632. {
  633. // Fix me: see how to use DXF text mode for multiline texts
  634. if( aMultilineAllowed && !aText.Contains( wxT( "\n" ) ) )
  635. aMultilineAllowed = false; // the text has only one line.
  636. if( textAsLines || containsNonAsciiChars( aText ) || aMultilineAllowed )
  637. {
  638. // output text as graphics.
  639. // Perhaps multiline texts could be handled as DXF text entity
  640. // but I do not want spend time about this (JPC)
  641. PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify,
  642. aWidth, aItalic, aBold, aMultilineAllowed );
  643. }
  644. else
  645. {
  646. /* Emit text as a text entity. This loses formatting and shape but it's
  647. more useful as a CAD object */
  648. DPOINT origin_dev = userToDeviceCoordinates( aPos );
  649. SetColor( aColor );
  650. wxString cname = getDXFColorName( m_currentColor );
  651. DPOINT size_dev = userToDeviceSize( aSize );
  652. int h_code = 0, v_code = 0;
  653. switch( aH_justify )
  654. {
  655. case GR_TEXT_HJUSTIFY_LEFT:
  656. h_code = 0;
  657. break;
  658. case GR_TEXT_HJUSTIFY_CENTER:
  659. h_code = 1;
  660. break;
  661. case GR_TEXT_HJUSTIFY_RIGHT:
  662. h_code = 2;
  663. break;
  664. }
  665. switch( aV_justify )
  666. {
  667. case GR_TEXT_VJUSTIFY_TOP:
  668. v_code = 3;
  669. break;
  670. case GR_TEXT_VJUSTIFY_CENTER:
  671. v_code = 2;
  672. break;
  673. case GR_TEXT_VJUSTIFY_BOTTOM:
  674. v_code = 1;
  675. break;
  676. }
  677. // Position, size, rotation and alignment
  678. // The two alignment point usages is somewhat idiot (see the DXF ref)
  679. // Anyway since we don't use the fit/aligned options, they're the same
  680. fprintf( outputFile,
  681. " 0\n"
  682. "TEXT\n"
  683. " 7\n"
  684. "%s\n" // Text style
  685. " 8\n"
  686. "%s\n" // Layer name
  687. " 10\n"
  688. "%g\n" // First point X
  689. " 11\n"
  690. "%g\n" // Second point X
  691. " 20\n"
  692. "%g\n" // First point Y
  693. " 21\n"
  694. "%g\n" // Second point Y
  695. " 40\n"
  696. "%g\n" // Text height
  697. " 41\n"
  698. "%g\n" // Width factor
  699. " 50\n"
  700. "%g\n" // Rotation
  701. " 51\n"
  702. "%g\n" // Oblique angle
  703. " 71\n"
  704. "%d\n" // Mirror flags
  705. " 72\n"
  706. "%d\n" // H alignment
  707. " 73\n"
  708. "%d\n", // V alignment
  709. aBold ? (aItalic ? "KICADBI" : "KICADB")
  710. : (aItalic ? "KICADI" : "KICAD"),
  711. TO_UTF8( cname ),
  712. origin_dev.x, origin_dev.x,
  713. origin_dev.y, origin_dev.y,
  714. size_dev.y, fabs( size_dev.x / size_dev.y ),
  715. aOrient / 10.0,
  716. aItalic ? DXF_OBLIQUE_ANGLE : 0,
  717. size_dev.x < 0 ? 2 : 0, // X mirror flag
  718. h_code, v_code );
  719. /* There are two issue in emitting the text:
  720. - Our overline character (~) must be converted to the appropriate
  721. control sequence %%O or %%o
  722. - Text encoding in DXF is more or less unspecified since depends on
  723. the DXF declared version, the acad version reading it *and* some
  724. system variables to be put in the header handled only by newer acads
  725. Also before R15 unicode simply is not supported (you need to use
  726. bigfonts which are a massive PITA). Common denominator solution:
  727. use Latin1 (and however someone could choke on it, anyway). Sorry
  728. for the extended latin people. If somewant want to try fixing this
  729. recent version seems to use UTF-8 (and not UCS2 like the rest of
  730. Windows)
  731. XXX Actually there is a *third* issue: older DXF formats are limited
  732. to 255 bytes records (it was later raised to 2048); since I'm lazy
  733. and text so long is not probable I just don't implement this rule.
  734. If someone is interested in fixing this, you have to emit the first
  735. partial lines with group code 3 (max 250 bytes each) and then finish
  736. with a group code 1 (less than 250 bytes). The DXF refs explains it
  737. in no more details...
  738. */
  739. bool overlining = false;
  740. fputs( " 1\n", outputFile );
  741. for( unsigned i = 0; i < aText.length(); i++ )
  742. {
  743. /* Here I do a bad thing: writing the output one byte at a time!
  744. but today I'm lazy and I have no idea on how to coerce a Unicode
  745. wxString to spit out latin1 encoded text ...
  746. Atleast stdio is *supposed* to do output buffering, so there is
  747. hope is not too slow */
  748. wchar_t ch = aText[i];
  749. if( ch > 255 )
  750. {
  751. // I can't encode this...
  752. putc( '?', outputFile );
  753. }
  754. else
  755. {
  756. if( ch == '~' )
  757. {
  758. // Handle the overline toggle
  759. fputs( overlining ? "%%o" : "%%O", outputFile );
  760. overlining = !overlining;
  761. }
  762. else
  763. {
  764. putc( ch, outputFile );
  765. }
  766. }
  767. }
  768. putc( '\n', outputFile );
  769. }
  770. }