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.

587 lines
19 KiB

  1. /**
  2. * Common plot library \n
  3. * Plot settings, postscript plotting, gerber plotting.
  4. *
  5. * @file plot_common.h
  6. */
  7. #ifndef PLOT_COMMON_H_
  8. #define PLOT_COMMON_H_
  9. #include <vector>
  10. #include <drawtxt.h>
  11. #include <common.h> // PAGE_INFO
  12. /**
  13. * Enum PlotFormat
  14. * is the set of supported output plot formats. They should be kept in order
  15. * of the radio buttons in the plot panel/windows.
  16. */
  17. enum PlotFormat {
  18. PLOT_FORMAT_HPGL,
  19. PLOT_FORMAT_GERBER,
  20. PLOT_FORMAT_POST,
  21. PLOT_FORMAT_DXF
  22. };
  23. class PLOTTER
  24. {
  25. public:
  26. PLOTTER( PlotFormat aPlotType );
  27. virtual ~PLOTTER()
  28. {
  29. // Emergency cleanup
  30. if( output_file )
  31. {
  32. fclose( output_file );
  33. }
  34. }
  35. /**
  36. * Function GetPlotterType
  37. * @return the format of the plot file
  38. */
  39. PlotFormat GetPlotterType() { return m_PlotType; }
  40. virtual bool start_plot( FILE* fout ) = 0;
  41. virtual bool end_plot() = 0;
  42. virtual void set_negative( bool _negative )
  43. {
  44. negative_mode = _negative;
  45. }
  46. virtual void set_color_mode( bool _color_mode )
  47. {
  48. color_mode = _color_mode;
  49. }
  50. bool get_color_mode() const
  51. {
  52. return color_mode;
  53. }
  54. void SetPageSettings( const PAGE_INFO& aPageSettings );
  55. virtual void set_current_line_width( int width ) = 0;
  56. virtual void set_default_line_width( int width ) = 0;
  57. virtual void set_color( int color ) = 0;
  58. virtual void set_dash( bool dashed ) = 0;
  59. virtual int get_current_line_width()
  60. {
  61. return current_pen_width;
  62. }
  63. virtual void set_plot_width_adj( double width )
  64. {
  65. }
  66. virtual double get_plot_width_adj()
  67. {
  68. return 0.;
  69. }
  70. virtual void set_creator( const wxString& _creator )
  71. {
  72. creator = _creator;
  73. }
  74. virtual void set_filename( const wxString& _filename )
  75. {
  76. filename = _filename;
  77. }
  78. /// Set the plot offset for the current plotting
  79. virtual void set_viewport( wxPoint aOffset, double aScale, bool aMirror ) = 0;
  80. // Standard primitives
  81. virtual void rect( wxPoint p1, wxPoint p2, FILL_T fill,
  82. int width = -1 ) = 0;
  83. virtual void circle( wxPoint pos, int diametre, FILL_T fill,
  84. int width = -1 ) = 0;
  85. virtual void arc( wxPoint centre, int StAngle, int EndAngle, int rayon,
  86. FILL_T fill, int width = -1 );
  87. /**
  88. * Function PlotPoly
  89. * @brief Draw a polygon ( filled or not )
  90. * @param aCornerList = corners list
  91. * @param aFill :if true : filled polygon
  92. * @param aWidth = line width
  93. */
  94. virtual void PlotPoly( std::vector< wxPoint >& aCornerList, FILL_T aFill, int aWidth = -1 ) = 0;
  95. /**
  96. * Function PlotImage
  97. * Only Postscript plotters can plot bitmaps
  98. * for plotters that cannot plot a bitmap, a rectangle is plotted
  99. * @brief Draw an image bitmap
  100. * @param aImage = the bitmap
  101. * @param aPos = position of the center of the bitmap
  102. * @param aScaleFactor = the scale factor to apply to the bitmap size
  103. * (this is not the plot scale factor)
  104. */
  105. virtual void PlotImage( wxImage & aImage, wxPoint aPos, double aScaleFactor ) = 0;
  106. virtual void thick_segment( wxPoint start, wxPoint end, int width,
  107. EDA_DRAW_MODE_T tracemode );
  108. virtual void thick_arc( wxPoint centre, int StAngle, int EndAngle, int rayon,
  109. int width, EDA_DRAW_MODE_T tracemode );
  110. virtual void thick_rect( wxPoint p1, wxPoint p2, int width,
  111. EDA_DRAW_MODE_T tracemode );
  112. virtual void thick_circle( wxPoint pos, int diametre, int width,
  113. EDA_DRAW_MODE_T tracemode );
  114. virtual void pen_to( wxPoint pos, char plume ) = 0;
  115. // Flash primitives
  116. virtual void flash_pad_circle( wxPoint pos, int diametre,
  117. EDA_DRAW_MODE_T trace_mode ) = 0;
  118. virtual void flash_pad_oval( wxPoint pos, wxSize size, int orient,
  119. EDA_DRAW_MODE_T trace_mode ) = 0;
  120. virtual void flash_pad_rect( wxPoint pos, wxSize size,
  121. int orient, EDA_DRAW_MODE_T trace_mode ) = 0;
  122. /** virtual function flash_pad_trapez
  123. * flash a trapezoidal pad
  124. * @param aPadPos = the position of the shape
  125. * @param aCorners = the list of 4 corners positions, relative to the shape position, pad orientation 0
  126. * @param aPadOrient = the rotation of the shape
  127. * @param aTrace_Mode = FILLED or SKETCH
  128. */
  129. virtual void flash_pad_trapez( wxPoint aPadPos, wxPoint aCorners[4],
  130. int aPadOrient, EDA_DRAW_MODE_T aTrace_Mode ) = 0;
  131. // Convenience functions
  132. void move_to( wxPoint pos )
  133. {
  134. pen_to( pos, 'U' );
  135. }
  136. void line_to( wxPoint pos )
  137. {
  138. pen_to( pos, 'D' );
  139. }
  140. void finish_to( wxPoint pos )
  141. {
  142. pen_to( pos, 'D' );
  143. pen_to( pos, 'Z' );
  144. }
  145. void pen_finish()
  146. {
  147. // Shortcut
  148. pen_to( wxPoint( 0, 0 ), 'Z' );
  149. }
  150. void text( const wxPoint& aPos,
  151. enum EDA_Colors aColor,
  152. const wxString& aText,
  153. int aOrient,
  154. const wxSize& aSize,
  155. enum EDA_TEXT_HJUSTIFY_T aH_justify,
  156. enum EDA_TEXT_VJUSTIFY_T aV_justify,
  157. int aWidth,
  158. bool aItalic,
  159. bool aBold );
  160. void marker( const wxPoint& position, int diametre, int aShapeId );
  161. /**
  162. * Function SetLayerPolarity
  163. * sets current Gerber layer polarity to positive or negative
  164. * by writing \%LPD*\% or \%LPC*\% to the Gerber file, respectively.
  165. * @param aPositive is the layer polarity and true for positive.
  166. */
  167. virtual void SetLayerPolarity( bool aPositive ) = 0;
  168. protected:
  169. // These are marker subcomponents
  170. void center_square( const wxPoint& position, int diametre, FILL_T fill );
  171. void center_lozenge( const wxPoint& position, int diametre, FILL_T fill );
  172. // Helper function for sketched filler segment
  173. void segment_as_oval( wxPoint start, wxPoint end, int width,
  174. EDA_DRAW_MODE_T tracemode );
  175. void sketch_oval( wxPoint pos, wxSize size, int orient, int width );
  176. virtual void user_to_device_coordinates( wxPoint& pos );
  177. virtual void user_to_device_size( wxSize& size );
  178. virtual double user_to_device_size( double size );
  179. PlotFormat m_PlotType;
  180. /// Plot scale
  181. double plot_scale;
  182. /// Device scale (from decimils to device units)
  183. double device_scale;
  184. /// Plot offset (in decimils)
  185. wxPoint plot_offset;
  186. /// Output file
  187. FILE* output_file;
  188. // Pen handling
  189. bool color_mode, negative_mode;
  190. int default_pen_width;
  191. int current_pen_width;
  192. char pen_state;
  193. wxPoint pen_lastpos;
  194. bool plotMirror;
  195. wxString creator;
  196. wxString filename;
  197. PAGE_INFO pageInfo;
  198. wxSize paper_size;
  199. };
  200. class HPGL_PLOTTER : public PLOTTER
  201. {
  202. public:
  203. HPGL_PLOTTER() :
  204. PLOTTER( PLOT_FORMAT_HPGL )
  205. {
  206. }
  207. virtual bool start_plot( FILE* fout );
  208. virtual bool end_plot();
  209. // HPGL doesn't handle line thickness or color
  210. virtual void set_current_line_width( int width )
  211. {
  212. // Handy override
  213. current_pen_width = wxRound( pen_diameter );
  214. }
  215. virtual void set_default_line_width( int width ) {};
  216. virtual void set_dash( bool dashed );
  217. virtual void set_color( int color ) {};
  218. virtual void set_pen_speed( int speed )
  219. {
  220. wxASSERT( output_file == 0 );
  221. pen_speed = speed;
  222. }
  223. virtual void set_pen_number( int number )
  224. {
  225. wxASSERT( output_file == 0 );
  226. pen_number = number;
  227. }
  228. virtual void set_pen_diameter( double diameter )
  229. {
  230. wxASSERT( output_file == 0 );
  231. pen_diameter = diameter;
  232. }
  233. virtual void set_pen_overlap( double overlap )
  234. {
  235. wxASSERT( output_file == 0 );
  236. pen_overlap = overlap;
  237. }
  238. virtual void set_viewport( wxPoint aOffset, double aScale, bool aMirror );
  239. virtual void rect( wxPoint p1, wxPoint p2, FILL_T fill, int width = -1 );
  240. virtual void circle( wxPoint pos, int diametre, FILL_T fill, int width = -1 );
  241. /*
  242. * Function PlotPoly
  243. * Draw a polygon (filled or not) in HPGL format
  244. * param aCornerList = corners list
  245. * param aFill :if true : filled polygon
  246. * param aWidth = line width
  247. */
  248. virtual void PlotPoly( std::vector< wxPoint >& aCornerList, FILL_T aFill, int aWidth = -1);
  249. /*
  250. * Function PlotImage
  251. * Only Postscript plotters can plot bitmaps
  252. * for plotters that cannot plot a bitmap, a rectangle is plotted
  253. * Draw an image bitmap
  254. * param aImage = the bitmap
  255. * param aPos = position of the center of the bitmap
  256. * param aScaleFactor = the scale factor to apply to the bitmap size
  257. * (this is not the plot scale factor)
  258. */
  259. virtual void PlotImage( wxImage & aImage, wxPoint aPos, double aScaleFactor );
  260. virtual void thick_segment( wxPoint start, wxPoint end, int width,
  261. EDA_DRAW_MODE_T tracemode );
  262. virtual void arc( wxPoint centre, int StAngle, int EndAngle, int rayon,
  263. FILL_T fill, int width = -1 );
  264. virtual void pen_to( wxPoint pos, char plume );
  265. virtual void flash_pad_circle( wxPoint pos, int diametre,
  266. EDA_DRAW_MODE_T trace_mode );
  267. virtual void flash_pad_oval( wxPoint pos, wxSize size, int orient,
  268. EDA_DRAW_MODE_T trace_mode );
  269. virtual void flash_pad_rect( wxPoint pos, wxSize size,
  270. int orient, EDA_DRAW_MODE_T trace_mode );
  271. virtual void flash_pad_trapez( wxPoint aPadPos, wxPoint aCorners[4],
  272. int aPadOrient, EDA_DRAW_MODE_T aTrace_Mode );
  273. virtual void SetLayerPolarity( bool aPositive ) {}
  274. protected:
  275. void pen_control( int plume );
  276. int pen_speed;
  277. int pen_number;
  278. double pen_diameter;
  279. double pen_overlap;
  280. };
  281. class PS_PLOTTER : public PLOTTER
  282. {
  283. public:
  284. PS_PLOTTER() :
  285. PLOTTER( PLOT_FORMAT_POST )
  286. {
  287. plot_scale_adjX = 1;
  288. plot_scale_adjY = 1;
  289. }
  290. virtual bool start_plot( FILE* fout );
  291. virtual bool end_plot();
  292. virtual void set_current_line_width( int width );
  293. virtual void set_default_line_width( int width );
  294. virtual void set_dash( bool dashed );
  295. virtual void set_color( int color );
  296. void set_scale_adjust( double scaleX, double scaleY )
  297. {
  298. plot_scale_adjX = scaleX;
  299. plot_scale_adjY = scaleY;
  300. }
  301. virtual void set_plot_width_adj( double width )
  302. {
  303. plot_width_adj = width;
  304. }
  305. virtual double get_plot_width_adj()
  306. {
  307. return plot_width_adj;
  308. }
  309. virtual void set_viewport( wxPoint aOffset, double aScale, bool aMirror );
  310. virtual void rect( wxPoint p1, wxPoint p2, FILL_T fill, int width = -1 );
  311. virtual void circle( wxPoint pos, int diametre, FILL_T fill, int width = -1 );
  312. virtual void arc( wxPoint centre, int StAngle, int EndAngle, int rayon,
  313. FILL_T fill, int width = -1 );
  314. /*
  315. * Function PlotPoly
  316. * Draw a polygon (filled or not) in POSTSCRIPT format
  317. * param aCornerList = corners list
  318. * param aFill :if true : filled polygon
  319. * param aWidth = line width
  320. */
  321. virtual void PlotPoly( std::vector< wxPoint >& aCornerList, FILL_T aFill, int aWidth = -1);
  322. /*
  323. * Function PlotImage
  324. * Only Postscript plotters can plot bitmaps
  325. * for plotters that cannot plot a bitmap, a rectangle is plotted
  326. * Draw an image bitmap
  327. * param aImage = the bitmap
  328. * param aPos = position of the center of the bitmap
  329. * param aScaleFactor = the scale factor to apply to the bitmap size
  330. * (this is not the plot scale factor)
  331. */
  332. virtual void PlotImage( wxImage & aImage, wxPoint aPos, double aScaleFactor );
  333. virtual void pen_to( wxPoint pos, char plume );
  334. virtual void flash_pad_circle( wxPoint pos, int diametre,
  335. EDA_DRAW_MODE_T trace_mode );
  336. virtual void flash_pad_oval( wxPoint pos, wxSize size, int orient,
  337. EDA_DRAW_MODE_T trace_mode );
  338. virtual void flash_pad_rect( wxPoint pos, wxSize size,
  339. int orient, EDA_DRAW_MODE_T trace_mode );
  340. virtual void flash_pad_trapez( wxPoint aPadPos, wxPoint aCorners[4],
  341. int aPadOrient, EDA_DRAW_MODE_T aTrace_Mode );
  342. virtual void SetLayerPolarity( bool aPositive ) {}
  343. protected:
  344. double plot_scale_adjX, plot_scale_adjY;
  345. double plot_width_adj;
  346. };
  347. /* Class to handle a D_CODE when plotting a board : */
  348. #define FIRST_DCODE_VALUE 10 // D_CODE < 10 is a command, D_CODE >= 10 is a tool
  349. struct APERTURE
  350. {
  351. enum Aperture_Type {
  352. Circle = 1,
  353. Rect = 2,
  354. Plotting = 3,
  355. Oval = 4
  356. };
  357. wxSize size; // horiz and Vert size
  358. Aperture_Type type; // Type ( Line, rect , circulaire , ovale .. )
  359. int D_code; // code number ( >= 10 );
  360. /* Trivia question: WHY Gerber decided to use D instead of the usual T for
  361. * tool change? */
  362. };
  363. class GERBER_PLOTTER : public PLOTTER
  364. {
  365. public:
  366. GERBER_PLOTTER() :
  367. PLOTTER( PLOT_FORMAT_GERBER )
  368. {
  369. work_file = 0;
  370. final_file = 0;
  371. current_aperture = apertures.end();
  372. }
  373. virtual bool start_plot( FILE* fout );
  374. virtual bool end_plot();
  375. virtual void set_current_line_width( int width );
  376. virtual void set_default_line_width( int width );
  377. // RS274X has no dashing, nor colours
  378. virtual void set_dash( bool dashed ) {};
  379. virtual void set_color( int color ) {};
  380. virtual void set_viewport( wxPoint aOffset, double aScale, bool aMirror );
  381. virtual void rect( wxPoint p1, wxPoint p2, FILL_T fill, int width = -1 );
  382. virtual void circle( wxPoint pos, int diametre, FILL_T fill, int width = -1 );
  383. /*
  384. * Function PlotPoly
  385. * Draw a polygon (filled or not) in GERBER format
  386. * param aCornerList = corners list
  387. * param aFill :if true : filled polygon
  388. * param aWidth = line width
  389. */
  390. virtual void PlotPoly( std::vector< wxPoint >& aCornerList, FILL_T aFill, int aWidth = -1);
  391. /*
  392. * Function PlotImage
  393. * Only Postscript plotters can plot bitmaps
  394. * for plotters that cannot plot a bitmap, a rectangle is plotted
  395. * Draw an image bitmap
  396. * param aImage = the bitmap
  397. * param aPos = position of the center of the bitmap
  398. * param aScaleFactor = the scale factor to apply to the bitmap size
  399. * (this is not the plot scale factor)
  400. */
  401. virtual void PlotImage( wxImage & aImage, wxPoint aPos, double aScaleFactor );
  402. virtual void pen_to( wxPoint pos, char plume );
  403. virtual void flash_pad_circle( wxPoint pos, int diametre,
  404. EDA_DRAW_MODE_T trace_mode );
  405. virtual void flash_pad_oval( wxPoint pos, wxSize size, int orient,
  406. EDA_DRAW_MODE_T trace_mode );
  407. virtual void flash_pad_rect( wxPoint pos, wxSize size,
  408. int orient, EDA_DRAW_MODE_T trace_mode );
  409. virtual void flash_pad_trapez( wxPoint aPadPos, wxPoint aCorners[4],
  410. int aPadOrient, EDA_DRAW_MODE_T aTrace_Mode );
  411. virtual void SetLayerPolarity( bool aPositive );
  412. protected:
  413. void select_aperture( const wxSize& size, APERTURE::Aperture_Type type );
  414. std::vector<APERTURE>::iterator
  415. get_aperture( const wxSize& size, APERTURE::Aperture_Type type );
  416. FILE* work_file, * final_file;
  417. wxString m_workFilename;
  418. void write_aperture_list();
  419. std::vector<APERTURE> apertures;
  420. std::vector<APERTURE>::iterator current_aperture;
  421. };
  422. class DXF_PLOTTER : public PLOTTER
  423. {
  424. public:
  425. DXF_PLOTTER() :
  426. PLOTTER( PLOT_FORMAT_DXF )
  427. {
  428. }
  429. virtual bool start_plot( FILE* fout );
  430. virtual bool end_plot();
  431. // For now we don't use 'thick' primitives, so no line width
  432. virtual void set_current_line_width( int width )
  433. {
  434. // Handy override
  435. current_pen_width = 0;
  436. }
  437. virtual void set_default_line_width( int width )
  438. {
  439. // DXF lines are infinitesimal
  440. default_pen_width = 0;
  441. }
  442. virtual void set_dash( bool dashed );
  443. virtual void set_color( int color );
  444. virtual void set_viewport( wxPoint aOffset, double aScale, bool aMirror );
  445. virtual void rect( wxPoint p1, wxPoint p2, FILL_T fill, int width = -1 );
  446. virtual void circle( wxPoint pos, int diametre, FILL_T fill, int width = -1 );
  447. /*
  448. * Function PlotPoly
  449. * Draw a polygon (filled or not) in DXF format
  450. * param aCornerList = corners list
  451. * param aFill :if true : filled polygon
  452. * param aWidth = line width
  453. */
  454. virtual void PlotPoly( std::vector< wxPoint >& aCornerList, FILL_T aFill, int aWidth = -1 );
  455. /*
  456. * Function PlotImage
  457. * Only Postscript plotters can plot bitmaps
  458. * for plotters that cannot plot a bitmap, a rectangle is plotted
  459. * Draw an image bitmap
  460. * param aImage = the bitmap
  461. * param aPos = position of the center of the bitmap
  462. * param aScaleFactor = the scale factor to apply to the bitmap size
  463. * (this is not the plot scale factor)
  464. */
  465. virtual void PlotImage( wxImage & aImage, wxPoint aPos, double aScaleFactor );
  466. virtual void thick_segment( wxPoint start, wxPoint end, int width,
  467. EDA_DRAW_MODE_T tracemode );
  468. virtual void arc( wxPoint centre, int StAngle, int EndAngle, int rayon,
  469. FILL_T fill, int width = -1 );
  470. virtual void pen_to( wxPoint pos, char plume );
  471. virtual void flash_pad_circle( wxPoint pos, int diametre,
  472. EDA_DRAW_MODE_T trace_mode );
  473. virtual void flash_pad_oval( wxPoint pos, wxSize size, int orient,
  474. EDA_DRAW_MODE_T trace_mode );
  475. virtual void flash_pad_rect( wxPoint pos, wxSize size,
  476. int orient, EDA_DRAW_MODE_T trace_mode );
  477. virtual void flash_pad_trapez( wxPoint aPadPos, wxPoint aCorners[4],
  478. int aPadOrient, EDA_DRAW_MODE_T aTrace_Mode );
  479. virtual void SetLayerPolarity( bool aPositive ) {}
  480. protected:
  481. int current_color;
  482. };
  483. #endif // PLOT_COMMON_H_