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 <vector>
31 
32 #include <plotter.h>
33 #include <plotters_specific.h>
34 #include <kicad_string.h>
35 #include <locale_io.h>
36 #include <pcb_edit_frame.h>
37 #include <pgm_base.h>
38 #include <board.h>
39 #include <track.h>
40 #include <pcbplot.h>
41 #include <gendrill_gerber_writer.h>
42 #include <reporter.h>
43 #include <gbr_metadata.h>
44 
45 
47  : GENDRILL_WRITER_BASE( aPcb )
48 {
50  m_conversionUnits = 1.0;
51  m_unitsMetric = true;
52  m_drillFileExtension = "gbr";
53  m_merge_PTH_NPTH = false;
54 }
55 
56 
57 void GERBER_WRITER::CreateDrillandMapFilesSet( const wxString& aPlotDirectory, bool aGenDrill,
58  bool aGenMap, REPORTER* aReporter )
59 {
60  // Note: In Gerber drill files, NPTH and PTH are always separate files
61  m_merge_PTH_NPTH = false;
62 
63  wxFileName fn;
64  wxString msg;
65 
66  std::vector<DRILL_LAYER_PAIR> hole_sets = getUniqueLayerPairs();
67 
68  // append a pair representing the NPTH set of holes, for separate drill files.
69  // (Gerber drill files are separate files for PTH and NPTH)
70  hole_sets.emplace_back( F_Cu, B_Cu );
71 
72  for( std::vector<DRILL_LAYER_PAIR>::const_iterator it = hole_sets.begin();
73  it != hole_sets.end(); ++it )
74  {
75  DRILL_LAYER_PAIR pair = *it;
76  // For separate drill files, the last layer pair is the NPTH drill file.
77  bool doing_npth = ( it == hole_sets.end() - 1 );
78 
79  buildHolesList( pair, doing_npth );
80 
81  // The file is created if it has holes, or if it is the non plated drill file
82  // to be sure the NPTH file is up to date in separate files mode.
83  // Also a PTH drill/map file is always created, to be sure at least one plated hole drill file
84  // is created (do not create any PTH drill file can be seen as not working drill generator).
85  if( getHolesCount() > 0 || doing_npth || pair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
86  {
87  fn = getDrillFileName( pair, doing_npth, false );
88  fn.SetPath( aPlotDirectory );
89 
90  if( aGenDrill )
91  {
92  wxString fullFilename = fn.GetFullPath();
93 
94  int result = createDrillFile( fullFilename, doing_npth, pair );
95 
96  if( result < 0 )
97  {
98  if( aReporter )
99  {
100  msg.Printf( _( "** Unable to create %s **\n" ), fullFilename );
101  aReporter->Report( msg );
102  }
103  break;
104  }
105  else
106  {
107  if( aReporter )
108  {
109  msg.Printf( _( "Create file %s\n" ), fullFilename );
110  aReporter->Report( msg );
111  }
112  }
113 
114  }
115  }
116  }
117 
118  if( aGenMap )
119  CreateMapFilesSet( aPlotDirectory, aReporter );
120 }
121 
122 // A helper class to transform an oblong hole to a segment
123 static void convertOblong2Segment( wxSize aSize, double aOrient, wxPoint& aStart, wxPoint& aEnd );
124 
125 int GERBER_WRITER::createDrillFile( wxString& aFullFilename, bool aIsNpth,
126  DRILL_LAYER_PAIR aLayerPair )
127 {
128  int holes_count;
129 
130  LOCALE_IO dummy; // Use the standard notation for double numbers
131 
132  GERBER_PLOTTER plotter;
133 
134  // Gerber drill file imply X2 format:
135  plotter.UseX2format( true );
136  plotter.UseX2NetAttributes( true );
137 
138  // Add the standard X2 header, without FileFunction
139  AddGerberX2Header( &plotter, m_pcb );
140  plotter.SetViewport( m_offset, IU_PER_MILS/10, /* scale */ 1.0, /* mirror */false );
141  // has meaning only for gerber plotter. Must be called only after SetViewport
142  plotter.SetGerberCoordinatesFormat( 6 );
143  plotter.SetCreator( wxT( "PCBNEW" ) );
144 
145  // Add the standard X2 FileFunction for drill files
146  // %TF.FileFunction,Plated[NonPlated],layer1num,layer2num,PTH[NPTH][Blind][Buried],Drill[Route][Mixed]*%
147  wxString text = BuildFileFunctionAttributeString( aLayerPair, aIsNpth );
148  plotter.AddLineToHeader( text );
149 
150  // Add file polarity (positive)
151  text = "%TF.FilePolarity,Positive*%";
152  plotter.AddLineToHeader( text );
153 
154  if( !plotter.OpenFile( aFullFilename ) )
155  return -1;
156 
157  plotter.StartPlot();
158 
159  holes_count = 0;
160 
161  wxPoint hole_pos;
162  bool last_item_is_via = true; // a flag to clear object attributes when a via hole is created.
163 
164  for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
165  {
166  HOLE_INFO& hole_descr = m_holeListBuffer[ii];
167  hole_pos = hole_descr.m_Hole_Pos;
168 
169  // Manage the aperture attributes: in drill files 3 attributes can be used:
170  // "ViaDrill", only for vias, not pads
171  // "ComponentDrill", only for Through Holes pads
172  // "Slot" for oblong holes;
173  GBR_METADATA gbr_metadata;
174 
175  if( dyn_cast<const VIA*>(hole_descr.m_ItemParent ) )
176  {
178 
179  if( !last_item_is_via )
180  {
181  // be sure the current object attribute is cleared for vias
182  plotter.EndBlock( NULL );
183  }
184 
185  last_item_is_via = true;
186  }
187  else if( dyn_cast<const PAD*>( hole_descr.m_ItemParent ) )
188  {
189  last_item_is_via = false;
190  const PAD* pad = dyn_cast<const PAD*>( hole_descr.m_ItemParent );
191 
192  if( pad->GetProperty() == PAD_PROP_CASTELLATED )
194  else
195  {
196  // Good practice of oblong pad holes (slots) is to use a specific aperture for routing, not used
197  // in drill commands
198  if( hole_descr.m_Hole_Shape )
200  else
202  }
203 
204  // Add object attribute: component reference to pads (mainly usefull for users)
205  wxString ref = pad->GetParent()->GetReference();
206 
207  gbr_metadata.SetCmpReference( ref );
209  }
210 
211  if( hole_descr.m_Hole_Shape )
212  {
213  #if 0 // set to 1 to use flashed oblong holes.
214  // Currently not possible for hole orient != 0 or 90 deg
215  // Use flashed oblong hole
216  plotter.FlashPadOval( hole_pos, hole_descr.m_Hole_Size,
217  hole_descr.m_Hole_Orient, FILLED, &gbr_metadata );
218  #else
219  // Use routing for oblong hole (Slots)
220  wxPoint start, end;
222  hole_descr.m_Hole_Orient, start, end );
223  int width = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
224 
225  if ( width == 0 )
226  continue;
227 
228  plotter.ThickSegment( start+hole_pos, end+hole_pos,
229  width, FILLED, &gbr_metadata );
230  #endif
231  }
232  else
233  {
234  int diam = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
235  plotter.FlashPadCircle( hole_pos, diam, FILLED, &gbr_metadata );
236  }
237 
238  holes_count++;
239  }
240 
241  plotter.EndPlot();
242 
243  return holes_count;
244 }
245 
246 
247 void convertOblong2Segment( wxSize aSize, double aOrient, wxPoint& aStart, wxPoint& aEnd )
248 {
249  wxSize size( aSize );
250  double orient = aOrient;
251 
252  /* The pad will be drawn as an oblong shape with size.y > size.x
253  * (Oval vertical orientation 0)
254  */
255  if( size.x > size.y )
256  {
257  std::swap( size.x, size.y );
258  orient = AddAngles( orient, 900 );
259  }
260 
261  int deltaxy = size.y - size.x; // distance between centers of the oval
262 
263  int cx = 0;
264  int cy = deltaxy / 2;
265  RotatePoint( &cx, &cy, orient );
266  aStart = wxPoint( cx, cy );
267  cx = 0; cy = -deltaxy / 2;
268  RotatePoint( &cx, &cy, orient );
269  aEnd = wxPoint( cx, cy );
270 }
271 
272 
273 void GERBER_WRITER::SetFormat( int aRightDigits )
274 {
275  /* Set conversion scale depending on drill file units */
276  m_conversionUnits = 1.0 / IU_PER_MM; // Gerber units = mm
277 
278  // Set precison (unit is mm).
279  m_precision.m_Lhs = 4;
280  m_precision.m_Rhs = aRightDigits == 6 ? 6 : 5;
281 }
282 
283 
284 const wxString GERBER_WRITER::getDrillFileName( DRILL_LAYER_PAIR aPair, bool aNPTH,
285  bool aMerge_PTH_NPTH ) const
286 {
287  // Gerber files extension is always .gbr.
288  // Therefore, to mark drill files, add "-drl" to the filename.
289  wxFileName fname( GENDRILL_WRITER_BASE::getDrillFileName( aPair, aNPTH, aMerge_PTH_NPTH ) );
290  fname.SetName( fname.GetName() + "-drl" );
291 
292  return fname.GetFullPath();
293 }
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
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
virtual void SetCreator(const wxString &aCreator)
Definition: plotter.h:172
void UseX2NetAttributes(bool aEnable)
aperture used for flashed cmp position in placement files.
Definition: gbr_metadata.h:143
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
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)
Plotting engines (PostScript, Gerber, HPGL and DXF)
static constexpr double IU_PER_MM
Mock up a conversion function.
virtual void ThickSegment(const wxPoint &start, const wxPoint &end, int width, OUTLINE_MODE tracemode, void *aData) override
virtual bool OpenFile(const wxString &aFullFilename)
Open or create the plot file aFullFilename.
Definition: plotter.cpp:76
static void convertOblong2Segment(wxSize aSize, double aOrient, wxPoint &aStart, wxPoint &aEnd)
void SetNetAttribType(int aNetAttribType)
Definition: gbr_metadata.h:219
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:279
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
Definition: sch_symbol.cpp:69
void UseX2format(bool aEnable)
aperture used for via holes in drill files.
Definition: gbr_metadata.h:139
void SetFormat(int aRightDigits=6)
Function SetFormat Initialize internal parameters to match the given format.
void AddLineToHeader(const wxString &aExtraString)
Add a line to the list of free lines to print at the beginning of the file.
Definition: plotter.h:180
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:228
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
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
Report a string with a given severity.
virtual bool StartPlot() override
Function StartPlot Write GERBER header to file initialize global variable g_Plot_PlotOutputFile.
a pad with a castellated through hole
Definition: pad_shapes.h:102
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:335
aperture used for pad holes in drill files.
Definition: gbr_metadata.h:140
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
const wxString & GetReference() const
Definition: footprint.h:423
std::pair< PCB_LAYER_ID, PCB_LAYER_ID > DRILL_LAYER_PAIR
Metadata which can be added in a gerber file as attribute in X2 format.
Definition: gbr_metadata.h:204
virtual void FlashPadOval(const wxPoint &aPadPos, const wxSize &size, double orient, OUTLINE_MODE trace_mode, void *aData) override
const wxString BuildFileFunctionAttributeString(DRILL_LAYER_PAIR aLayerPair, bool aIsNpth, bool aCompatNCdrill=false) const
PAD_PROP_T GetProperty() const
Definition: pad.h:366
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...
FOOTPRINT * GetParent() const
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
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:189
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:209
virtual void FlashPadCircle(const wxPoint &pos, int diametre, OUTLINE_MODE trace_mode, void *aData) override
Filled circular flashes are stored as apertures.
Classes used in drill files, map files and report files generation.
GERBER_WRITER(BOARD *aPcb)
Definition: pad.h:60
std::vector< HOLE_INFO > m_holeListBuffer
void SetCmpReference(const wxString &aComponentRef)
Definition: gbr_metadata.h:241
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 ...