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.first, pair.second );
99 
100  if( result < 0 )
101  {
102  if( aReporter )
103  {
104  msg.Printf( _( "** Unable to create %s **\n" ), GetChars( fullFilename ) );
105  aReporter->Report( msg );
106  }
107  break;
108  }
109  else
110  {
111  if( aReporter )
112  {
113  msg.Printf( _( "Create file %s\n" ), GetChars( 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  int aLayer1, int aLayer2 )
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.UseX2Attributes( 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( "%TF.FileFunction," );
152 
153  if( aIsNpth )
154  text << "NonPlated,";
155  else
156  text << "Plated,";
157 
158  // In Gerber files, layers num are 1 to copper layer count instead of F_Cu to B_Cu
159  // (0 to copper layer count-1)
160  // Note also for a n copper layers board, gerber layers num are 1 ... n
161  aLayer1 += 1;
162 
163  if( aLayer2 == B_Cu )
164  aLayer2 = m_pcb->GetCopperLayerCount();
165  else
166  aLayer2 += 1;
167 
168  text << aLayer1 << ",";
169  text << aLayer2 << ",";
170 
171  // Now add PTH or NPTH or Blind or Buried attribute
172  int toplayer = 1;
173  int bottomlayer = m_pcb->GetCopperLayerCount();
174 
175  if( aIsNpth )
176  text << "NPTH";
177  else if( aLayer1 == toplayer && aLayer2 == bottomlayer )
178  text << "PTH";
179  else if( aLayer1 == toplayer || aLayer2 == bottomlayer )
180  text << "Blind";
181  else
182  text << "Buried";
183 
184  // Now add Drill or Route or Mixed:
185  // file containing only round holes have Drill attribute
186  // file containing only oblong holes have Routed attribute
187  // file containing both holes have Mixed attribute
188  bool hasOblong = false;
189  bool hasDrill = false;
190 
191  for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
192  {
193  HOLE_INFO& hole_descr = m_holeListBuffer[ii];
194 
195  if( hole_descr.m_Hole_Shape ) // m_Hole_Shape not 0 is an oblong hole)
196  hasOblong = true;
197  else
198  hasDrill = true;
199  }
200 
201  if( hasOblong && hasDrill )
202  text << ",Mixed";
203  else if( hasDrill )
204  text << ",Drill";
205  else if( hasOblong )
206  text << ",Route";
207 
208  // else: empty file.
209 
210  // End of .FileFunction attribute:
211  text << "*%";
212 
213  plotter.AddLineToHeader( text );
214 
215  // Add file polarity (positive)
216  text = "%TF.FilePolarity,Positive*%";
217  plotter.AddLineToHeader( text );
218 
219  if( !plotter.OpenFile( aFullFilename ) )
220  return -1;
221 
222  plotter.StartPlot();
223 
224  holes_count = 0;
225 
226  wxPoint hole_pos;
227  bool last_item_is_via = true; // a flag to clear object attributes when a via hole is created.
228 
229  for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
230  {
231  HOLE_INFO& hole_descr = m_holeListBuffer[ii];
232  hole_pos = hole_descr.m_Hole_Pos;
233 
234  // Manage the aperture attributes: in drill files 3 attributes can be used:
235  // "ViaDrill", only for vias, not pads
236  // "ComponentDrill", only for Through Holes pads
237  // "Slot" for oblong holes;
238  GBR_METADATA gbr_metadata;
239 
240  if( dyn_cast<const VIA*>(hole_descr.m_ItemParent ) )
241  {
243 
244  if( !last_item_is_via )
245  {
246  // be sure the current object attribute is cleared for vias
247  plotter.EndBlock( NULL );
248  }
249 
250  last_item_is_via = true;
251  }
252  else if( dyn_cast<const D_PAD*>( hole_descr.m_ItemParent ) )
253  {
254  last_item_is_via = false;
255 
256  if( hole_descr.m_Hole_Shape )
258  else
260 
261  // Add object attribute: component reference to pads (mainly usefull for users)
262  const D_PAD* pad = dyn_cast<const D_PAD*>( hole_descr.m_ItemParent );
263  wxString ref = pad->GetParent()->GetReference();
264 
265 #if 0 // Set to 1 to force a dummy reference for the parent pad.
266  if( ref.IsEmpty() )
267  ref = "<undefinedref>";
268 #endif
269  gbr_metadata.SetCmpReference( ref );
271  }
272 
273  if( hole_descr.m_Hole_Shape )
274  {
275  #if 0 // set to 1 to use flashed oblong holes.
276  // Currently not possible for hole orient != 0 or 90 deg
277  // Use flashed oblong hole
278  plotter.FlashPadOval( hole_pos, hole_descr.m_Hole_Size,
279  hole_descr.m_Hole_Orient, FILLED, &gbr_metadata );
280  #else
281  // Use routing for oblong hole (Slots)
282  wxPoint start, end;
284  hole_descr.m_Hole_Orient, start, end );
285  int width = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
286 
287  if ( width == 0 )
288  continue;
289 
290  plotter.ThickSegment( start+hole_pos, end+hole_pos,
291  width, FILLED, &gbr_metadata );
292  #endif
293  }
294  else
295  {
296  int diam = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
297  plotter.FlashPadCircle( hole_pos, diam, FILLED, &gbr_metadata );
298  }
299 
300  holes_count++;
301  }
302 
303  plotter.EndPlot();
304 
305  return holes_count;
306 }
307 
308 
309 void convertOblong2Segment( wxSize aSize, double aOrient, wxPoint& aStart, wxPoint& aEnd )
310 {
311  wxSize size( aSize );
312  double orient = aOrient;
313 
314  /* The pad will be drawn as an oblong shape with size.y > size.x
315  * (Oval vertical orientation 0)
316  */
317  if( size.x > size.y )
318  {
319  std::swap( size.x, size.y );
320  orient = AddAngles( orient, 900 );
321  }
322 
323  int deltaxy = size.y - size.x; // distance between centers of the oval
324 
325  int cx = 0;
326  int cy = deltaxy / 2;
327  RotatePoint( &cx, &cy, orient );
328  aStart = wxPoint( cx, cy );
329  cx = 0; cy = -deltaxy / 2;
330  RotatePoint( &cx, &cy, orient );
331  aEnd = wxPoint( cx, cy );
332 }
333 
334 
335 void GERBER_WRITER::SetFormat( int aRightDigits )
336 {
337  /* Set conversion scale depending on drill file units */
338  m_conversionUnits = 1.0 / IU_PER_MM; // Gerber units = mm
339 
340  // Set precison (unit is mm).
341  m_precision.m_lhs = 4;
342  m_precision.m_rhs = aRightDigits == 6 ? 6 : 5;
343 }
344 
345 
346 const wxString GERBER_WRITER::getDrillFileName( DRILL_LAYER_PAIR aPair, bool aNPTH,
347  bool aMerge_PTH_NPTH ) const
348 {
349  // Gerber files extension is always .gbr.
350  // Therefore, to mark drill files, add "-drl" to the filename.
351  wxFileName fname( GENDRILL_WRITER_BASE::getDrillFileName( aPair, aNPTH, aMerge_PTH_NPTH ) );
352  fname.SetName( fname.GetName() + "-drl" );
353 
354  return fname.GetFullPath();
355 }
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:179
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:129
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
int GetCopperLayerCount() const
Function GetCopperLayerCount.
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
aperture used for via holes in drill files
Definition: gbr_metadata.h:73
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.
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:74
The common library.
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...
int createDrillFile(wxString &aFullFilename, bool aIsNpth, int aLayer1, int aLayer2)
Function createDrillFile Creates an Excellon drill file.
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...
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
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:170
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:463
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:119
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:75
void SetCmpReference(const wxString &aComponentRef)
Definition: gbr_metadata.h:141
void UseX2Attributes(bool aEnable)
Definition: plotter.h:1137
#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 ...