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.

183 lines
6.3 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2007-2014 Jean-Pierre Charras jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <convert_to_biu.h>
  25. #include <geometry/shape_poly_set.h>
  26. #include <layer_ids.h>
  27. struct EXPORT_VIA
  28. {
  29. EXPORT_VIA( const wxPoint& aPos, int aSize, int aDrill ) :
  30. m_Pos( aPos ),
  31. m_Size( aSize ),
  32. m_Drill( aDrill )
  33. { }
  34. wxPoint m_Pos;
  35. int m_Size;
  36. int m_Drill;
  37. };
  38. class GERBER_DRAW_ITEM;
  39. class GERBVIEW_FRAME;
  40. /**
  41. * A helper class to export a Gerber set of files to Pcbnew.
  42. */
  43. class GBR_TO_PCB_EXPORTER
  44. {
  45. public:
  46. GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName );
  47. ~GBR_TO_PCB_EXPORTER();
  48. /**
  49. * Save a board from a set of Gerber images.
  50. */
  51. bool ExportPcb( const LAYER_NUM* aLayerLookUpTable, int aCopperLayers );
  52. private:
  53. /**
  54. * Collect holes from a drill layer.
  55. *
  56. * We'll use these later when writing pads & vias. Many holes will be pads, but we have
  57. * no way to create those without footprints, and creating a footprint per pad is not
  58. * really viable. We use vias to mimic holes, with the loss of any hole shape (as we only
  59. * have round holes in vias at present). We start out with a via size minimally larger
  60. * than the hole. We'll leave it this way if the pad gets drawn as a copper polygon, or
  61. * increase it to the proper size if it has a circular, concentric copper flashing.
  62. */
  63. void collect_hole( const GERBER_DRAW_ITEM* aGbrItem );
  64. /**
  65. * Write a via to the board file.
  66. *
  67. * Some of these will represent actual vias while others are used to represent
  68. * holes in pads. (We can't generate actual pads because the Gerbers don't contain
  69. * info on how to group them into footprints.)
  70. */
  71. void export_via( const EXPORT_VIA& aVia );
  72. /**
  73. * Write a non copper line or arc to the board file.
  74. *
  75. * @param aGbrItem is the Gerber item (line, arc) to export.
  76. * @param aLayer is the technical layer to use.
  77. */
  78. void export_non_copper_item( const GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
  79. /**
  80. * Write a non-copper polygon to the board file.
  81. *
  82. * @param aLayer is the technical layer to use.
  83. */
  84. void writePcbPolygon( const SHAPE_POLY_SET& aPolys, LAYER_NUM aLayer,
  85. const wxPoint& aOffset = { 0, 0 } );
  86. /**
  87. * Write a filled circle to the board file (with line thickness = 0).
  88. *
  89. * @param aCenterPosition is the actual position of the filled circle,
  90. * given by <round_flashed_shape>->GetABPosition()
  91. * @param aRadius is the circle radius.
  92. * @param aLayer is the layer to use.
  93. */
  94. void writePcbFilledCircle( const VECTOR2I& aCenterPosition, int aRadius,
  95. LAYER_NUM aLayer );
  96. /**
  97. * Write a zone item to the board file.
  98. *
  99. * @warning This is experimental for tests only.
  100. *
  101. * @param aGbrItem is the Gerber item (line, arc) to export.
  102. * @param aLayer is the technical layer to use.
  103. */
  104. void writePcbZoneItem( const GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
  105. /**
  106. * Write a track (or via) to the board file.
  107. *
  108. * @param aGbrItem is the Gerber item (line, arc, flashed) to export.
  109. * @param aLayer is the copper layer to use.
  110. */
  111. void export_copper_item( const GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
  112. /**
  113. * Write a synthetic pad to the board file.
  114. *
  115. * We can't create real pads because the Gerbers don't store grouping/footprint info.
  116. * So we synthesize a pad with a via for the hole (if present) and a copper polygon for
  117. * the pad.
  118. *
  119. * @param aGbrItem is the flashed Gerber item to export.
  120. */
  121. void export_flashed_copper_item( const GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
  122. /**
  123. * Write a track (not via) to the board file.
  124. *
  125. * @param aGbrItem is the Gerber item (line only) to export.
  126. * @param aLayer is the copper layer to use.
  127. */
  128. void export_segline_copper_item( const GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
  129. /**
  130. * Write a set of tracks (arcs are approximated by track segments) to the board file.
  131. *
  132. * @param aGbrItem is the Gerber item (arc only) to export.
  133. * @param aLayer is the copper layer to use
  134. */
  135. void export_segarc_copper_item( const GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
  136. /**
  137. * Basic write function to write a a #PCB_TRACK to the board file from a non flashed item.
  138. */
  139. void writeCopperLineItem( const wxPoint& aStart, const wxPoint& aEnd,
  140. int aWidth, LAYER_NUM aLayer );
  141. /**
  142. * Write a very basic header to the board file.
  143. */
  144. void writePcbHeader( const LAYER_NUM* aLayerLookUpTable );
  145. /**
  146. * Map GerbView internal units to millimeters for Pcbnew board files.
  147. *
  148. * @param aValue is a coordinate value to convert in mm.
  149. */
  150. double MapToPcbUnits( int aValue ) const
  151. {
  152. return aValue / IU_PER_MM;
  153. }
  154. private:
  155. GERBVIEW_FRAME* m_gerbview_frame; // the main gerber frame
  156. wxString m_pcb_file_name; // BOARD file to write to
  157. FILE* m_fp; // the board file
  158. int m_pcbCopperLayersCount;
  159. std::vector<EXPORT_VIA> m_vias;
  160. };