KiCad PCB EDA Suite
gendrill_gerber_writer.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2017 Jean_Pierre Charras <jp.charras at wanadoo.fr>
5  * Copyright (C) 1992-2017 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 
30 #include <fctsys.h>
31 
32 #include <vector>
33 
34 #include <plotter.h>
35 #include <kicad_string.h>
36 #include <pcb_edit_frame.h>
37 #include <pgm_base.h>
38 #include <build_version.h>
39 
40 #include <class_board.h>
41 
42 #include <pcbplot.h>
43 #include <pcbnew.h>
44 #include <gendrill_gerber_writer.h>
46 #include <reporter.h>
47 #include <gbr_metadata.h>
48 #include <class_module.h>
49 
50 
52  : GENDRILL_WRITER_BASE( aPcb )
53 {
55  m_conversionUnits = 1.0;
56  m_unitsMetric = true;
57  m_drillFileExtension = "gbr";
58  m_merge_PTH_NPTH = false;
59 }
60 
61 
62 void GERBER_WRITER::CreateDrillandMapFilesSet( const wxString& aPlotDirectory,
63  bool aGenDrill, bool aGenMap,
64  REPORTER * aReporter )
65 {
66  // Note: In Gerber drill files, NPTH and PTH are always separate files
67  m_merge_PTH_NPTH = false;
68 
69  wxFileName fn;
70  wxString msg;
71 
72  std::vector<DRILL_LAYER_PAIR> hole_sets = getUniqueLayerPairs();
73 
74  // append a pair representing the NPTH set of holes, for separate drill files.
75  // (Gerber drill files are separate files for PTH and NPTH)
76  hole_sets.push_back( DRILL_LAYER_PAIR( F_Cu, B_Cu ) );
77 
78  for( std::vector<DRILL_LAYER_PAIR>::const_iterator it = hole_sets.begin();
79  it != hole_sets.end(); ++it )
80  {
81  DRILL_LAYER_PAIR pair = *it;
82  // For separate drill files, the last layer pair is the NPTH drill file.
83  bool doing_npth = ( it == hole_sets.end() - 1 );
84 
85  buildHolesList( pair, doing_npth );
86 
87  // The file is created if it has holes, or if it is the non plated drill file
88  // to be sure the NPTH file is up to date in separate files mode.
89  if( getHolesCount() > 0 || doing_npth )
90  {
91  fn = getDrillFileName( pair, doing_npth, false );
92  fn.SetPath( aPlotDirectory );
93 
94  if( aGenDrill )
95  {
96  wxString fullFilename = fn.GetFullPath();
97 
98  int result = createDrillFile( fullFilename, doing_npth, pair );
99 
100  if( result < 0 )
101  {
102  if( aReporter )
103  {
104  msg.Printf( _( "** Unable to create %s **\n" ), fullFilename );
105  aReporter->Report( msg );
106  }
107  break;
108  }
109  else
110  {
111  if( aReporter )
112  {
113  msg.Printf( _( "Create file %s\n" ), fullFilename );
114  aReporter->Report( msg );
115  }
116  }
117 
118  }
119  }
120  }
121 
122  if( aGenMap )
123  CreateMapFilesSet( aPlotDirectory, aReporter );
124 }
125 
126 // A helper class to transform an oblong hole to a segment
127 static void convertOblong2Segment( wxSize aSize, double aOrient, wxPoint& aStart, wxPoint& aEnd );
128 
129 int GERBER_WRITER::createDrillFile( wxString& aFullFilename, bool aIsNpth,
130  DRILL_LAYER_PAIR aLayerPair )
131 {
132  int holes_count;
133 
134  LOCALE_IO dummy; // Use the standard notation for double numbers
135 
136  GERBER_PLOTTER plotter;
137 
138  // Gerber drill file imply X2 format:
139  plotter.UseX2format( true );
140  plotter.UseX2NetAttributes( true );
141 
142  // Add the standard X2 header, without FileFunction
143  AddGerberX2Header( &plotter, m_pcb );
144  plotter.SetViewport( m_offset, IU_PER_MILS/10, /* scale */ 1.0, /* mirror */false );
145  // has meaning only for gerber plotter. Must be called only after SetViewport
146  plotter.SetGerberCoordinatesFormat( 6 );
147  plotter.SetCreator( wxT( "PCBNEW" ) );
148 
149  // Add the standard X2 FileFunction for drill files
150  // %TF.FileFunction,Plated[NonPlated],layer1num,layer2num,PTH[NPTH][Blind][Buried],Drill[Route][Mixed]*%
151  wxString text = BuildFileFunctionAttributeString( aLayerPair, aIsNpth );
152  plotter.AddLineToHeader( text );
153 
154  // Add file polarity (positive)
155  text = "%TF.FilePolarity,Positive*%";
156  plotter.AddLineToHeader( text );
157 
158  if( !plotter.OpenFile( aFullFilename ) )
159  return -1;
160 
161  plotter.StartPlot();
162 
163  holes_count = 0;
164 
165  wxPoint hole_pos;
166  bool last_item_is_via = true; // a flag to clear object attributes when a via hole is created.
167 
168  for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
169  {
170  HOLE_INFO& hole_descr = m_holeListBuffer[ii];
171  hole_pos = hole_descr.m_Hole_Pos;
172 
173  // Manage the aperture attributes: in drill files 3 attributes can be used:
174  // "ViaDrill", only for vias, not pads
175  // "ComponentDrill", only for Through Holes pads
176  // "Slot" for oblong holes;
177  GBR_METADATA gbr_metadata;
178 
179  if( dyn_cast<const VIA*>(hole_descr.m_ItemParent ) )
180  {
182 
183  if( !last_item_is_via )
184  {
185  // be sure the current object attribute is cleared for vias
186  plotter.EndBlock( NULL );
187  }
188 
189  last_item_is_via = true;
190  }
191  else if( dyn_cast<const D_PAD*>( hole_descr.m_ItemParent ) )
192  {
193  last_item_is_via = false;
194 
195  if( hole_descr.m_Hole_Shape )
197  else
199 
200  // Add object attribute: component reference to pads (mainly usefull for users)
201  const D_PAD* pad = dyn_cast<const D_PAD*>( hole_descr.m_ItemParent );
202  wxString ref = pad->GetParent()->GetReference();
203 
204 #if 0 // Set to 1 to force a dummy reference for the parent pad.
205  if( ref.IsEmpty() )
206  ref = "<undefinedref>";
207 #endif
208  gbr_metadata.SetCmpReference( ref );
210  }
211 
212  if( hole_descr.m_Hole_Shape )
213  {
214  #if 0 // set to 1 to use flashed oblong holes.
215  // Currently not possible for hole orient != 0 or 90 deg
216  // Use flashed oblong hole
217  plotter.FlashPadOval( hole_pos, hole_descr.m_Hole_Size,
218  hole_descr.m_Hole_Orient, FILLED, &gbr_metadata );
219  #else
220  // Use routing for oblong hole (Slots)
221  wxPoint start, end;
223  hole_descr.m_Hole_Orient, start, end );
224  int width = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
225 
226  if ( width == 0 )
227  continue;
228 
229  plotter.ThickSegment( start+hole_pos, end+hole_pos,
230  width, FILLED, &gbr_metadata );
231  #endif
232  }
233  else
234  {
235  int diam = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
236  plotter.FlashPadCircle( hole_pos, diam, FILLED, &gbr_metadata );
237  }
238 
239  holes_count++;
240  }
241 
242  plotter.EndPlot();
243 
244  return holes_count;
245 }
246 
247 
248 void convertOblong2Segment( wxSize aSize, double aOrient, wxPoint& aStart, wxPoint& aEnd )
249 {
250  wxSize size( aSize );
251  double orient = aOrient;
252 
253  /* The pad will be drawn as an oblong shape with size.y > size.x
254  * (Oval vertical orientation 0)
255  */
256  if( size.x > size.y )
257  {
258  std::swap( size.x, size.y );
259  orient = AddAngles( orient, 900 );
260  }
261 
262  int deltaxy = size.y - size.x; // distance between centers of the oval
263 
264  int cx = 0;
265  int cy = deltaxy / 2;
266  RotatePoint( &cx, &cy, orient );
267  aStart = wxPoint( cx, cy );
268  cx = 0; cy = -deltaxy / 2;
269  RotatePoint( &cx, &cy, orient );
270  aEnd = wxPoint( cx, cy );
271 }
272 
273 
274 void GERBER_WRITER::SetFormat( int aRightDigits )
275 {
276  /* Set conversion scale depending on drill file units */
277  m_conversionUnits = 1.0 / IU_PER_MM; // Gerber units = mm
278 
279  // Set precison (unit is mm).
280  m_precision.m_lhs = 4;
281  m_precision.m_rhs = aRightDigits == 6 ? 6 : 5;
282 }
283 
284 
285 const wxString GERBER_WRITER::getDrillFileName( DRILL_LAYER_PAIR aPair, bool aNPTH,
286  bool aMerge_PTH_NPTH ) const
287 {
288  // Gerber files extension is always .gbr.
289  // Therefore, to mark drill files, add "-drl" to the filename.
290  wxFileName fname( GENDRILL_WRITER_BASE::getDrillFileName( aPair, aNPTH, aMerge_PTH_NPTH ) );
291  fname.SetName( fname.GetName() + "-drl" );
292 
293  return fname.GetFullPath();
294 }
a class to handle special data (items attributes) during plot.
BOARD_ITEM * m_ItemParent
virtual void SetCreator(const wxString &aCreator)
Definition: plotter.h:161
virtual void ThickSegment(const wxPoint &start, const wxPoint &end, int width, EDA_DRAW_MODE_T tracemode, void *aData) override
void UseX2NetAttributes(bool aEnable)
Definition: plotter.h:1138
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown...
Definition: common.h:180
virtual void EndBlock(void *aData) override
calling this function allows one to define the end of a group of drawing items the group is started b...
print info associated to a component (TO.C attribute)
virtual bool OpenFile(const wxString &aFullFilename)
Open or create the plot file aFullFilename.
Definition: plotter.cpp:79
static void convertOblong2Segment(wxSize aSize, double aOrient, wxPoint &aStart, wxPoint &aEnd)
void SetNetAttribType(int aNetAttribType)
Definition: gbr_metadata.h:152
Class BOARD to handle a board.
virtual const wxString getDrillFileName(DRILL_LAYER_PAIR aPair, bool aNPTH, bool aMerge_PTH_NPTH) const
MODULE * GetParent() const
Definition: class_pad.h:162
void AddGerberX2Header(PLOTTER *aPlotter, const BOARD *aBoard, bool aUseX1CompatibilityMode)
Calculates some X2 attributes, as defined in the Gerber file format specification J4 (chapter 5) and ...
Definition: pcbplot.cpp:280
void UseX2format(bool aEnable)
Definition: plotter.h:1137
aperture used for via holes in drill files
Definition: gbr_metadata.h:96
void SetFormat(int aRightDigits=6)
Function SetFormat Initialize internal parameters to match the given format.
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
void AddLineToHeader(const wxString &aExtraString)
Function AddLineToHeader Add a line to the list of free lines to print at the beginning of the file...
Definition: plotter.h:176
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
void CreateMapFilesSet(const wxString &aPlotDirectory, REPORTER *aReporter=NULL)
Function CreateMapFilesSet Creates the full set of map files for the board, in PS, PDF ...
virtual void FlashPadOval(const wxPoint &pos, const wxSize &size, double orient, EDA_DRAW_MODE_T trace_mode, void *aData) override
Filled oval flashes are handled as aperture in the 90 degree positions only.
Casted dyn_cast(From aObject)
Function dyn_cast()
Definition: typeinfo.h:61
virtual bool StartPlot() override
Function StartPlot Write GERBER header to file initialize global variable g_Plot_PlotOutputFile.
Board plot function definition file.
const wxString BuildFileFunctionAttributeString(DRILL_LAYER_PAIR aLayerPair, bool aIsNpth, bool aCompatNCdrill=false) const
std::vector< DRILL_LAYER_PAIR > getUniqueLayerPairs() const
Get unique layer pairs by examining the micro and blind_buried vias.
T AddAngles(T a1, T2 a2)
Add two angles (keeping the result normalized). T2 is here.
Definition: trigo.h:288
aperture used for pad holes in drill files
Definition: gbr_metadata.h:97
Definition of file extensions used in Kicad.
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
std::pair< PCB_LAYER_ID, PCB_LAYER_ID > DRILL_LAYER_PAIR
virtual void FlashPadCircle(const wxPoint &pos, int diametre, EDA_DRAW_MODE_T trace_mode, void *aData) override
Filled circular flashes are stored as apertures.
void buildHolesList(DRILL_LAYER_PAIR aLayerPair, bool aGenerateNPTH_list)
Function BuildHolesList Create the list of holes and tools for a given board The list is sorted by in...
void CreateDrillandMapFilesSet(const wxString &aPlotDirectory, bool aGenDrill, bool aGenMap, REPORTER *aReporter=NULL)
Function CreateDrillandMapFilesSet Creates the full set of Excellon drill file for the board filename...
see class PGM_BASE
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:171
virtual const wxString getDrillFileName(DRILL_LAYER_PAIR aPair, bool aNPTH, bool aMerge_PTH_NPTH) const override
const wxString & GetReference() const
Function GetReference.
Definition: class_module.h:462
virtual bool EndPlot() override
#define IU_PER_MILS
Definition: plotter.cpp:134
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
virtual void SetGerberCoordinatesFormat(int aResolution, bool aUseInches=false) override
Function SetGerberCoordinatesFormat selection of Gerber units and resolution (number of digits in man...
void SetApertureAttrib(GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB aApertAttribute)
Definition: gbr_metadata.h:142
Classes used in drill files, map files and report files generation.
Module description (excepted pads)
GERBER_WRITER(BOARD *aPcb)
std::vector< HOLE_INFO > m_holeListBuffer
aperture used for oblong holes in drill files
Definition: gbr_metadata.h:98
void SetCmpReference(const wxString &aComponentRef)
Definition: gbr_metadata.h:164
int createDrillFile(wxString &aFullFilename, bool aIsNpth, DRILL_LAYER_PAIR aLayerPair)
Function createDrillFile Creates an Excellon drill file.
#define min(a, b)
Definition: auxiliary.h:85
GENDRILL_WRITER_BASE is a class to create drill maps and drill report, and a helper class to created ...