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 #include <class_track.h>
42 
43 #include <pcbplot.h>
44 #include <pcbnew.h>
45 #include <gendrill_gerber_writer.h>
47 #include <reporter.h>
48 #include <gbr_metadata.h>
49 #include <class_module.h>
50 
51 
53  : GENDRILL_WRITER_BASE( aPcb )
54 {
56  m_conversionUnits = 1.0;
57  m_unitsMetric = true;
58  m_drillFileExtension = "gbr";
59  m_merge_PTH_NPTH = false;
60 }
61 
62 
63 void GERBER_WRITER::CreateDrillandMapFilesSet( const wxString& aPlotDirectory,
64  bool aGenDrill, bool aGenMap,
65  REPORTER * aReporter )
66 {
67  // Note: In Gerber drill files, NPTH and PTH are always separate files
68  m_merge_PTH_NPTH = false;
69 
70  wxFileName fn;
71  wxString msg;
72 
73  std::vector<DRILL_LAYER_PAIR> hole_sets = getUniqueLayerPairs();
74 
75  // append a pair representing the NPTH set of holes, for separate drill files.
76  // (Gerber drill files are separate files for PTH and NPTH)
77  hole_sets.emplace_back( F_Cu, B_Cu );
78 
79  for( std::vector<DRILL_LAYER_PAIR>::const_iterator it = hole_sets.begin();
80  it != hole_sets.end(); ++it )
81  {
82  DRILL_LAYER_PAIR pair = *it;
83  // For separate drill files, the last layer pair is the NPTH drill file.
84  bool doing_npth = ( it == hole_sets.end() - 1 );
85 
86  buildHolesList( pair, doing_npth );
87 
88  // The file is created if it has holes, or if it is the non plated drill file
89  // to be sure the NPTH file is up to date in separate files mode.
90  if( getHolesCount() > 0 || doing_npth )
91  {
92  fn = getDrillFileName( pair, doing_npth, false );
93  fn.SetPath( aPlotDirectory );
94 
95  if( aGenDrill )
96  {
97  wxString fullFilename = fn.GetFullPath();
98 
99  int result = createDrillFile( fullFilename, doing_npth, pair );
100 
101  if( result < 0 )
102  {
103  if( aReporter )
104  {
105  msg.Printf( _( "** Unable to create %s **\n" ), fullFilename );
106  aReporter->Report( msg );
107  }
108  break;
109  }
110  else
111  {
112  if( aReporter )
113  {
114  msg.Printf( _( "Create file %s\n" ), fullFilename );
115  aReporter->Report( msg );
116  }
117  }
118 
119  }
120  }
121  }
122 
123  if( aGenMap )
124  CreateMapFilesSet( aPlotDirectory, aReporter );
125 }
126 
127 // A helper class to transform an oblong hole to a segment
128 static void convertOblong2Segment( wxSize aSize, double aOrient, wxPoint& aStart, wxPoint& aEnd );
129 
130 int GERBER_WRITER::createDrillFile( wxString& aFullFilename, bool aIsNpth,
131  DRILL_LAYER_PAIR aLayerPair )
132 {
133  int holes_count;
134 
135  LOCALE_IO dummy; // Use the standard notation for double numbers
136 
137  GERBER_PLOTTER plotter;
138 
139  // Gerber drill file imply X2 format:
140  plotter.UseX2format( true );
141  plotter.UseX2NetAttributes( true );
142 
143  // Add the standard X2 header, without FileFunction
144  AddGerberX2Header( &plotter, m_pcb );
145  plotter.SetViewport( m_offset, IU_PER_MILS/10, /* scale */ 1.0, /* mirror */false );
146  // has meaning only for gerber plotter. Must be called only after SetViewport
147  plotter.SetGerberCoordinatesFormat( 6 );
148  plotter.SetCreator( wxT( "PCBNEW" ) );
149 
150  // Add the standard X2 FileFunction for drill files
151  // %TF.FileFunction,Plated[NonPlated],layer1num,layer2num,PTH[NPTH][Blind][Buried],Drill[Route][Mixed]*%
152  wxString text = BuildFileFunctionAttributeString( aLayerPair, aIsNpth );
153  plotter.AddLineToHeader( text );
154 
155  // Add file polarity (positive)
156  text = "%TF.FilePolarity,Positive*%";
157  plotter.AddLineToHeader( text );
158 
159  if( !plotter.OpenFile( aFullFilename ) )
160  return -1;
161 
162  plotter.StartPlot();
163 
164  holes_count = 0;
165 
166  wxPoint hole_pos;
167  bool last_item_is_via = true; // a flag to clear object attributes when a via hole is created.
168 
169  for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
170  {
171  HOLE_INFO& hole_descr = m_holeListBuffer[ii];
172  hole_pos = hole_descr.m_Hole_Pos;
173 
174  // Manage the aperture attributes: in drill files 3 attributes can be used:
175  // "ViaDrill", only for vias, not pads
176  // "ComponentDrill", only for Through Holes pads
177  // "Slot" for oblong holes;
178  GBR_METADATA gbr_metadata;
179 
180  if( dyn_cast<const VIA*>(hole_descr.m_ItemParent ) )
181  {
183 
184  if( !last_item_is_via )
185  {
186  // be sure the current object attribute is cleared for vias
187  plotter.EndBlock( NULL );
188  }
189 
190  last_item_is_via = true;
191  }
192  else if( dyn_cast<const D_PAD*>( hole_descr.m_ItemParent ) )
193  {
194  last_item_is_via = false;
195  const D_PAD* pad = dyn_cast<const D_PAD*>( hole_descr.m_ItemParent );
196 
197  if( pad->GetProperty() == PAD_PROP_CASTELLATED )
199  else
200  {
201  // Good practice of oblong pad holes (slots) is to use a specific aperture for routing, not used
202  // in drill commands
203  if( hole_descr.m_Hole_Shape )
205  else
207  }
208 
209  // Add object attribute: component reference to pads (mainly usefull for users)
210  wxString ref = pad->GetParent()->GetReference();
211 
212  gbr_metadata.SetCmpReference( ref );
214  }
215 
216  if( hole_descr.m_Hole_Shape )
217  {
218  #if 0 // set to 1 to use flashed oblong holes.
219  // Currently not possible for hole orient != 0 or 90 deg
220  // Use flashed oblong hole
221  plotter.FlashPadOval( hole_pos, hole_descr.m_Hole_Size,
222  hole_descr.m_Hole_Orient, FILLED, &gbr_metadata );
223  #else
224  // Use routing for oblong hole (Slots)
225  wxPoint start, end;
227  hole_descr.m_Hole_Orient, start, end );
228  int width = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
229 
230  if ( width == 0 )
231  continue;
232 
233  plotter.ThickSegment( start+hole_pos, end+hole_pos,
234  width, FILLED, &gbr_metadata );
235  #endif
236  }
237  else
238  {
239  int diam = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
240  plotter.FlashPadCircle( hole_pos, diam, FILLED, &gbr_metadata );
241  }
242 
243  holes_count++;
244  }
245 
246  plotter.EndPlot();
247 
248  return holes_count;
249 }
250 
251 
252 void convertOblong2Segment( wxSize aSize, double aOrient, wxPoint& aStart, wxPoint& aEnd )
253 {
254  wxSize size( aSize );
255  double orient = aOrient;
256 
257  /* The pad will be drawn as an oblong shape with size.y > size.x
258  * (Oval vertical orientation 0)
259  */
260  if( size.x > size.y )
261  {
262  std::swap( size.x, size.y );
263  orient = AddAngles( orient, 900 );
264  }
265 
266  int deltaxy = size.y - size.x; // distance between centers of the oval
267 
268  int cx = 0;
269  int cy = deltaxy / 2;
270  RotatePoint( &cx, &cy, orient );
271  aStart = wxPoint( cx, cy );
272  cx = 0; cy = -deltaxy / 2;
273  RotatePoint( &cx, &cy, orient );
274  aEnd = wxPoint( cx, cy );
275 }
276 
277 
278 void GERBER_WRITER::SetFormat( int aRightDigits )
279 {
280  /* Set conversion scale depending on drill file units */
281  m_conversionUnits = 1.0 / IU_PER_MM; // Gerber units = mm
282 
283  // Set precison (unit is mm).
284  m_precision.m_lhs = 4;
285  m_precision.m_rhs = aRightDigits == 6 ? 6 : 5;
286 }
287 
288 
289 const wxString GERBER_WRITER::getDrillFileName( DRILL_LAYER_PAIR aPair, bool aNPTH,
290  bool aMerge_PTH_NPTH ) const
291 {
292  // Gerber files extension is always .gbr.
293  // Therefore, to mark drill files, add "-drl" to the filename.
294  wxFileName fname( GENDRILL_WRITER_BASE::getDrillFileName( aPair, aNPTH, aMerge_PTH_NPTH ) );
295  fname.SetName( fname.GetName() + "-drl" );
296 
297  return fname.GetFullPath();
298 }
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:180
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:1297
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:202
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:1296
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:195
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:62
void CreateMapFilesSet(const wxString &aPlotDirectory, REPORTER *aReporter=NULL)
Function CreateMapFilesSet Creates the full set of map files for the board, in PS,...
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
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.
Definitions for tracks, vias and zones.
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:304
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:452
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:163
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:33
#define IU_PER_MILS
Definition: plotter.cpp:137
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 ...