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.emplace_back( 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  const D_PAD* pad = dyn_cast<const D_PAD*>( hole_descr.m_ItemParent );
195 
196  if( pad->GetProperty() == PAD_PROP_CASTELLATED )
198  else
199  {
200  // Good practice of oblong pad holes (slots) is to use a specific aperture for routing, not used
201  // in drill commands
202  if( hole_descr.m_Hole_Shape )
204  else
206  }
207 
208  // Add object attribute: component reference to pads (mainly usefull for users)
209  wxString ref = pad->GetParent()->GetReference();
210 
211  gbr_metadata.SetCmpReference( ref );
213  }
214 
215  if( hole_descr.m_Hole_Shape )
216  {
217  #if 0 // set to 1 to use flashed oblong holes.
218  // Currently not possible for hole orient != 0 or 90 deg
219  // Use flashed oblong hole
220  plotter.FlashPadOval( hole_pos, hole_descr.m_Hole_Size,
221  hole_descr.m_Hole_Orient, FILLED, &gbr_metadata );
222  #else
223  // Use routing for oblong hole (Slots)
224  wxPoint start, end;
226  hole_descr.m_Hole_Orient, start, end );
227  int width = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
228 
229  if ( width == 0 )
230  continue;
231 
232  plotter.ThickSegment( start+hole_pos, end+hole_pos,
233  width, FILLED, &gbr_metadata );
234  #endif
235  }
236  else
237  {
238  int diam = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
239  plotter.FlashPadCircle( hole_pos, diam, FILLED, &gbr_metadata );
240  }
241 
242  holes_count++;
243  }
244 
245  plotter.EndPlot();
246 
247  return holes_count;
248 }
249 
250 
251 void convertOblong2Segment( wxSize aSize, double aOrient, wxPoint& aStart, wxPoint& aEnd )
252 {
253  wxSize size( aSize );
254  double orient = aOrient;
255 
256  /* The pad will be drawn as an oblong shape with size.y > size.x
257  * (Oval vertical orientation 0)
258  */
259  if( size.x > size.y )
260  {
261  std::swap( size.x, size.y );
262  orient = AddAngles( orient, 900 );
263  }
264 
265  int deltaxy = size.y - size.x; // distance between centers of the oval
266 
267  int cx = 0;
268  int cy = deltaxy / 2;
269  RotatePoint( &cx, &cy, orient );
270  aStart = wxPoint( cx, cy );
271  cx = 0; cy = -deltaxy / 2;
272  RotatePoint( &cx, &cy, orient );
273  aEnd = wxPoint( cx, cy );
274 }
275 
276 
277 void GERBER_WRITER::SetFormat( int aRightDigits )
278 {
279  /* Set conversion scale depending on drill file units */
280  m_conversionUnits = 1.0 / IU_PER_MM; // Gerber units = mm
281 
282  // Set precison (unit is mm).
283  m_precision.m_lhs = 4;
284  m_precision.m_rhs = aRightDigits == 6 ? 6 : 5;
285 }
286 
287 
288 const wxString GERBER_WRITER::getDrillFileName( DRILL_LAYER_PAIR aPair, bool aNPTH,
289  bool aMerge_PTH_NPTH ) const
290 {
291  // Gerber files extension is always .gbr.
292  // Therefore, to mark drill files, add "-drl" to the filename.
293  wxFileName fname( GENDRILL_WRITER_BASE::getDrillFileName( aPair, aNPTH, aMerge_PTH_NPTH ) );
294  fname.SetName( fname.GetName() + "-drl" );
295 
296  return fname.GetFullPath();
297 }
a class to handle special data (items attributes) during plot.
BOARD_ITEM * m_ItemParent
virtual const wxString getDrillFileName(DRILL_LAYER_PAIR aPair, bool aNPTH, bool aMerge_PTH_NPTH) const
virtual void SetCreator(const wxString &aCreator)
Definition: plotter.h:167
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:1282
aperture used for pads oblong holes in drill files
Definition: gbr_metadata.h:105
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:90
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:82
static void convertOblong2Segment(wxSize aSize, double aOrient, wxPoint &aStart, wxPoint &aEnd)
void SetNetAttribType(int aNetAttribType)
Definition: gbr_metadata.h:170
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:282
void UseX2format(bool aEnable)
Definition: plotter.h:1281
aperture used for via holes in drill files
Definition: gbr_metadata.h:103
void SetFormat(int aRightDigits=6)
Function SetFormat Initialize internal parameters to match the given format.
aperture used for castellated pads in drill files
Definition: gbr_metadata.h:101
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:182
std::vector< DRILL_LAYER_PAIR > getUniqueLayerPairs() const
Get unique layer pairs by examining the micro and blind_buried vias.
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
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,...
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.
virtual bool StartPlot() override
Function StartPlot Write GERBER header to file initialize global variable g_Plot_PlotOutputFile.
const wxString GetReference() const
Function GetReference.
Definition: class_module.h:436
a pad with a castellated through hole
Definition: pad_shapes.h:84
Board plot function definition file.
#define NULL
T AddAngles(T a1, T2 a2)
Add two angles (keeping the result normalized). T2 is here.
Definition: trigo.h:299
aperture used for pad holes in drill files
Definition: gbr_metadata.h:104
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.
PAD_PROP_T GetProperty() const
Definition: class_pad.h:448
std::pair< PCB_LAYER_ID, PCB_LAYER_ID > DRILL_LAYER_PAIR
const wxString BuildFileFunctionAttributeString(DRILL_LAYER_PAIR aLayerPair, bool aIsNpth, bool aCompatNCdrill=false) const
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...
MODULE * GetParent() const
Definition: class_pad.h:167
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 to draw a dummy shape when a LIB_PART is not found in library.
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:160
virtual const wxString getDrillFileName(DRILL_LAYER_PAIR aPair, bool aNPTH, bool aMerge_PTH_NPTH) const override
virtual bool EndPlot() override
#define _(s)
Definition: 3d_actions.cpp:31
#define IU_PER_MILS
Definition: plotter.cpp:137
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:160
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
void SetCmpReference(const wxString &aComponentRef)
Definition: gbr_metadata.h:192
int createDrillFile(wxString &aFullFilename, bool aIsNpth, DRILL_LAYER_PAIR aLayerPair)
Function createDrillFile Creates an Excellon drill file.
GENDRILL_WRITER_BASE is a class to create drill maps and drill report, and a helper class to created ...