KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2021 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
31#include <string_utils.h>
32#include <locale_io.h>
33#include <board.h>
34#include <footprint.h>
35#include <pcb_track.h>
36#include <pad.h>
37#include <pcbplot.h>
39#include <reporter.h>
40#include <gbr_metadata.h>
41
42// set to 1 to use flashed oblong holes, 0 to draw them by a line.
43#define FLASH_OVAL_HOLE 1
44
45
47 : GENDRILL_WRITER_BASE( aPcb )
48{
51 m_unitsMetric = true;
52 m_drillFileExtension = wxT( "gbr" );
53 m_merge_PTH_NPTH = false;
54}
55
56
57bool GERBER_WRITER::CreateDrillandMapFilesSet( const wxString& aPlotDirectory, bool aGenDrill,
58 bool aGenMap, REPORTER* aReporter )
59{
60 bool success = true;
61 // Note: In Gerber drill files, NPTH and PTH are always separate files
62 m_merge_PTH_NPTH = false;
63
64 wxFileName fn;
65 wxString msg;
66
67 std::vector<DRILL_LAYER_PAIR> hole_sets = getUniqueLayerPairs();
68
69 // append a pair representing the NPTH set of holes, for separate drill files.
70 // (Gerber drill files are separate files for PTH and NPTH)
71 hole_sets.emplace_back( F_Cu, B_Cu );
72
73 for( std::vector<DRILL_LAYER_PAIR>::const_iterator it = hole_sets.begin();
74 it != hole_sets.end(); ++it )
75 {
76 DRILL_LAYER_PAIR pair = *it;
77 // For separate drill files, the last layer pair is the NPTH drill file.
78 bool doing_npth = ( it == hole_sets.end() - 1 );
79
80 buildHolesList( pair, doing_npth );
81
82 // The file is created if it has holes, or if it is the non plated drill file
83 // to be sure the NPTH file is up to date in separate files mode.
84 // Also a PTH drill/map file is always created, to be sure at least one plated hole drill
85 // file is created (do not create any PTH drill file can be seen as not working drill
86 // generator).
87 if( getHolesCount() > 0 || doing_npth || pair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
88 {
89 fn = getDrillFileName( pair, doing_npth, false );
90 fn.SetPath( aPlotDirectory );
91
92 if( aGenDrill )
93 {
94 wxString fullFilename = fn.GetFullPath();
95
96 int result = createDrillFile( fullFilename, doing_npth, pair );
97
98 if( result < 0 )
99 {
100 if( aReporter )
101 {
102 msg.Printf( _( "Failed to create file '%s'." ), fullFilename );
103 aReporter->Report( msg, RPT_SEVERITY_ERROR );
104 success = false;
105 }
106
107 break;
108 }
109 else
110 {
111 if( aReporter )
112 {
113 msg.Printf( _( "Created file '%s'." ), fullFilename );
114 aReporter->Report( msg, RPT_SEVERITY_ACTION );
115 }
116 }
117
118 }
119 }
120 }
121
122 if( aGenMap )
123 success &= CreateMapFilesSet( aPlotDirectory, aReporter );
124
125 if( aReporter )
126 aReporter->ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
127
128 return success;
129}
130
131
132#if !FLASH_OVAL_HOLE
133// A helper class to transform an oblong hole to a segment
134static void convertOblong2Segment( wxSize aSize, double aOrient, VECTOR2I& aStart, VECTOR2I& aEnd );
135#endif
136
137
138int GERBER_WRITER::createDrillFile( wxString& aFullFilename, bool aIsNpth,
139 DRILL_LAYER_PAIR aLayerPair )
140{
141 int holes_count;
142
143 LOCALE_IO dummy; // Use the standard notation for double numbers
144
145 GERBER_PLOTTER plotter;
146
147 // Gerber drill file imply X2 format:
148 plotter.UseX2format( true );
149 plotter.UseX2NetAttributes( true );
150 plotter.DisableApertMacros( false );
151
152 // Add the standard X2 header, without FileFunction
153 AddGerberX2Header( &plotter, m_pcb );
154 plotter.SetViewport( m_offset, pcbIUScale.IU_PER_MILS/10, /* scale */ 1.0, /* mirror */false );
155
156 // has meaning only for gerber plotter. Must be called only after SetViewport
157 plotter.SetGerberCoordinatesFormat( 6 );
158 plotter.SetCreator( wxT( "PCBNEW" ) );
159
160 // Add the standard X2 FileFunction for drill files
161 // %TF.FileFunction,Plated[NonPlated],layer1num,layer2num,PTH[NPTH][Blind][Buried],Drill[Route][Mixed]*%
162 wxString text = BuildFileFunctionAttributeString( aLayerPair,
163 aIsNpth ? TYPE_FILE::NPTH_FILE
165 plotter.AddLineToHeader( text );
166
167 // Add file polarity (positive)
168 text = wxT( "%TF.FilePolarity,Positive*%" );
169 plotter.AddLineToHeader( text );
170
171 if( !plotter.OpenFile( aFullFilename ) )
172 return -1;
173
174 plotter.StartPlot( wxT( "1" ) );
175
176 holes_count = 0;
177
178 VECTOR2I hole_pos;
179 bool last_item_is_via = true; // a flag to clear object attributes when a via hole is created.
180
181 for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
182 {
183 HOLE_INFO& hole_descr = m_holeListBuffer[ii];
184 hole_pos = hole_descr.m_Hole_Pos;
185
186 // Manage the aperture attributes: in drill files 3 attributes can be used:
187 // "ViaDrill", only for vias, not pads
188 // "ComponentDrill", only for Through Holes pads
189 // "Slot" for oblong holes;
190 GBR_METADATA gbr_metadata;
191
192 if( dyn_cast<const PCB_VIA*>( hole_descr.m_ItemParent ) )
193 {
195
196 if( !last_item_is_via )
197 {
198 // be sure the current object attribute is cleared for vias
199 plotter.EndBlock( nullptr );
200 }
201
202 last_item_is_via = true;
203 }
204 else if( dyn_cast<const PAD*>( hole_descr.m_ItemParent ) )
205 {
206 last_item_is_via = false;
207 const PAD* pad = dyn_cast<const PAD*>( hole_descr.m_ItemParent );
208
209 if( pad->GetProperty() == PAD_PROP::CASTELLATED )
210 {
211 gbr_metadata.SetApertureAttrib(
213 }
214 else
215 {
216 // Good practice of oblong pad holes (slots) is to use a specific aperture for
217 // routing, not used in drill commands.
218 if( hole_descr.m_Hole_Shape )
219 gbr_metadata.SetApertureAttrib(
221 else
222 gbr_metadata.SetApertureAttrib(
224 }
225
226 // Add object attribute: component reference to pads (mainly useful for users)
227 wxString ref = pad->GetParent()->GetReference();
228
229 gbr_metadata.SetCmpReference( ref );
231 }
232
233 if( hole_descr.m_Hole_Shape )
234 {
235#if FLASH_OVAL_HOLE // set to 1 to use flashed oblong holes,
236 // 0 to draw them as a line.
237 plotter.FlashPadOval( hole_pos, hole_descr.m_Hole_Size, hole_descr.m_Hole_Orient,
238 FILLED, &gbr_metadata );
239#else
240 // Use routing for oblong hole (Slots)
241 VECTOR2I start, end;
242 convertOblong2Segment( hole_descr.m_Hole_Size, hole_descr.m_Hole_Orient, start, end );
243 int width = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
244
245 if ( width == 0 )
246 continue;
247
248 plotter.ThickSegment( start+hole_pos, end+hole_pos, width, FILLED, &gbr_metadata );
249#endif
250 }
251 else
252 {
253 int diam = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
254 plotter.FlashPadCircle( hole_pos, diam, FILLED, &gbr_metadata );
255 }
256
257 holes_count++;
258 }
259
260 plotter.EndPlot();
261
262 return holes_count;
263}
264
265
266#if !FLASH_OVAL_HOLE
267void convertOblong2Segment( wxSize aSize, const EDA_ANGLE& aOrient, VECTOR2I& aStart,
268 VECTOR2I& aEnd )
269{
270 wxSize size( aSize );
271 EDA_ANGLE orient( aOrient );
272
273 /* The pad will be drawn as an oblong shape with size.y > size.x
274 * (Oval vertical orientation 0)
275 */
276 if( size.x > size.y )
277 {
278 std::swap( size.x, size.y );
279 orient += ANGLE_90;
280 }
281
282 int deltaxy = size.y - size.x; // distance between centers of the oval
283 aStart = VECTOR2I( 0, deltaxy / 2 );
284 RotatePoint( aStart, orient );
285
286 aEnd = VECTOR2I( 0, -deltaxy / 2 );
287 RotatePoint( aEnd, orient );
288}
289#endif
290
291
292void GERBER_WRITER::SetFormat( int aRightDigits )
293{
294 /* Set conversion scale depending on drill file units */
295 m_conversionUnits = 1.0 / pcbIUScale.IU_PER_MM; // Gerber units = mm
296
297 // Set precision (unit is mm).
298 m_precision.m_Lhs = 4;
299 m_precision.m_Rhs = aRightDigits == 6 ? 6 : 5;
300}
301
302
303const wxString GERBER_WRITER::getDrillFileName( DRILL_LAYER_PAIR aPair, bool aNPTH,
304 bool aMerge_PTH_NPTH ) const
305{
306 // Gerber files extension is always .gbr.
307 // Therefore, to mark drill files, add "-drl" to the filename.
308 wxFileName fname( GENDRILL_WRITER_BASE::getDrillFileName( aPair, aNPTH, aMerge_PTH_NPTH ) );
309 fname.SetName( fname.GetName() + wxT( "-drl" ) );
310
311 return fname.GetFullPath();
312}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:270
@ GBR_APERTURE_ATTRIB_CMP_DRILL
aperture used for pad holes in drill files.
Definition: gbr_metadata.h:140
@ GBR_APERTURE_ATTRIB_CMP_OBLONG_DRILL
aperture used for flashed cmp position in placement files.
Definition: gbr_metadata.h:143
@ GBR_APERTURE_ATTRIB_VIADRILL
aperture used for via holes in drill files.
Definition: gbr_metadata.h:139
Metadata which can be added in a gerber file as attribute in X2 format.
Definition: gbr_metadata.h:205
void SetCmpReference(const wxString &aComponentRef)
Definition: gbr_metadata.h:241
void SetApertureAttrib(GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB aApertAttribute)
Definition: gbr_metadata.h:209
void SetNetAttribType(int aNetAttribType)
Definition: gbr_metadata.h:219
@ GBR_NETINFO_CMP
print info associated to a component (TO.C attribute)
Create drill maps and drill reports and drill files.
std::vector< DRILL_LAYER_PAIR > getUniqueLayerPairs() const
Get unique layer pairs by examining the micro and blind_buried vias.
virtual const wxString getDrillFileName(DRILL_LAYER_PAIR aPair, bool aNPTH, bool aMerge_PTH_NPTH) const
void buildHolesList(DRILL_LAYER_PAIR aLayerPair, bool aGenerateNPTH_list)
Create the list of holes and tools for a given board.
const wxString BuildFileFunctionAttributeString(DRILL_LAYER_PAIR aLayerPair, TYPE_FILE aHoleType, bool aCompatNCdrill=false) const
std::vector< HOLE_INFO > m_holeListBuffer
bool CreateMapFilesSet(const wxString &aPlotDirectory, REPORTER *aReporter=nullptr)
Create the full set of map files for the board, in PS, PDF ... format (use SetMapFileFormat() to sele...
virtual void SetGerberCoordinatesFormat(int aResolution, bool aUseInches=false) override
Selection of Gerber units and resolution (number of digits in mantissa).
virtual void FlashPadCircle(const VECTOR2I &pos, int diametre, OUTLINE_MODE trace_mode, void *aData) override
Filled circular flashes are stored as apertures.
virtual void SetViewport(const VECTOR2I &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
virtual bool EndPlot() override
void UseX2format(bool aEnable)
virtual void FlashPadOval(const VECTOR2I &aPadPos, const VECTOR2I &aSize, const EDA_ANGLE &aOrient, OUTLINE_MODE aTraceMode, void *aData) override
void UseX2NetAttributes(bool aEnable)
virtual void EndBlock(void *aData) override
Define the end of a group of drawing items the group is started by StartBlock().
virtual void ThickSegment(const VECTOR2I &start, const VECTOR2I &end, int width, OUTLINE_MODE tracemode, void *aData) override
virtual bool StartPlot(const wxString &pageNumber) override
Write GERBER header to file initialize global variable g_Plot_PlotOutputFile.
void DisableApertMacros(bool aDisable)
Disable Aperture Macro (AM) command, only for broken Gerber Readers.
virtual const wxString getDrillFileName(DRILL_LAYER_PAIR aPair, bool aNPTH, bool aMerge_PTH_NPTH) const override
int createDrillFile(wxString &aFullFilename, bool aIsNpth, DRILL_LAYER_PAIR aLayerPair)
Create an Excellon drill file.
bool CreateDrillandMapFilesSet(const wxString &aPlotDirectory, bool aGenDrill, bool aGenMap, REPORTER *aReporter=nullptr)
Create the full set of Excellon drill file for the board filenames are computed from the board name,...
void SetFormat(int aRightDigits=6)
Initialize internal parameters to match the given format.
GERBER_WRITER(BOARD *aPcb)
Handle hole which must be drilled (diameter, position and layers).
BOARD_ITEM * m_ItemParent
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:41
Definition: pad.h:59
virtual bool OpenFile(const wxString &aFullFilename)
Open or create the plot file aFullFilename.
Definition: plotter.cpp:74
virtual void SetCreator(const wxString &aCreator)
Definition: plotter.h:159
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:168
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the end of the list, for objects that support report ordering.
Definition: reporter.h:99
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
#define _(s)
static constexpr EDA_ANGLE & ANGLE_90
Definition: eda_angle.h:431
Handle special data (items attributes) during plot.
std::pair< PCB_LAYER_ID, PCB_LAYER_ID > DRILL_LAYER_PAIR
Classes used in drill files, map files and report files generation.
static const bool FILLED
Definition: gr_basic.cpp:30
@ B_Cu
Definition: layer_ids.h:95
@ F_Cu
Definition: layer_ids.h:64
void AddGerberX2Header(PLOTTER *aPlotter, const BOARD *aBoard, bool aUseX1CompatibilityMode)
Calculate some X2 attributes as defined in the Gerber file format specification J4 (chapter 5) and ad...
Definition: pcbplot.cpp:276
Plotting engine (Gerber)
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
@ RPT_SEVERITY_ACTION
std::vector< FAB_LAYER_COLOR > dummy
const double IU_PER_MM
Definition: base_units.h:77
const double IU_PER_MILS
Definition: base_units.h:78
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588