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-2024 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 (route holes).
43// WARNING: currently ( gerber-layer-format-specification-revision-2023-08 ),
44// oblong holes **must be routed* in a drill file and not flashed,
45// so set FLASH_OVAL_HOLE to 0
46#define FLASH_OVAL_HOLE 0
47
48
50 : GENDRILL_WRITER_BASE( aPcb )
51{
54 m_unitsMetric = true;
55 m_drillFileExtension = wxT( "gbr" );
56 m_merge_PTH_NPTH = false;
57}
58
59
60bool GERBER_WRITER::CreateDrillandMapFilesSet( const wxString& aPlotDirectory, bool aGenDrill,
61 bool aGenMap, REPORTER* aReporter )
62{
63 bool success = true;
64 // Note: In Gerber drill files, NPTH and PTH are always separate files
65 m_merge_PTH_NPTH = false;
66
67 wxFileName fn;
68 wxString msg;
69
70 std::vector<DRILL_LAYER_PAIR> hole_sets = getUniqueLayerPairs();
71
72 // append a pair representing the NPTH set of holes, for separate drill files.
73 // (Gerber drill files are separate files for PTH and NPTH)
74 hole_sets.emplace_back( F_Cu, B_Cu );
75
76 for( std::vector<DRILL_LAYER_PAIR>::const_iterator it = hole_sets.begin();
77 it != hole_sets.end(); ++it )
78 {
79 DRILL_LAYER_PAIR pair = *it;
80 // For separate drill files, the last layer pair is the NPTH drill file.
81 bool doing_npth = ( it == hole_sets.end() - 1 );
82
83 buildHolesList( pair, doing_npth );
84
85 // The file is created if it has holes, or if it is the non plated drill file
86 // to be sure the NPTH file is up to date in separate files mode.
87 // Also a PTH drill/map file is always created, to be sure at least one plated hole drill
88 // file is created (do not create any PTH drill file can be seen as not working drill
89 // generator).
90 if( getHolesCount() > 0 || doing_npth || pair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
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( _( "Failed to create file '%s'." ), fullFilename );
106 aReporter->Report( msg, RPT_SEVERITY_ERROR );
107 success = false;
108 }
109
110 break;
111 }
112 else
113 {
114 if( aReporter )
115 {
116 msg.Printf( _( "Created file '%s'." ), fullFilename );
117 aReporter->Report( msg, RPT_SEVERITY_ACTION );
118 }
119 }
120
121 }
122 }
123 }
124
125 if( aGenMap )
126 success &= CreateMapFilesSet( aPlotDirectory, aReporter );
127
128 if( aReporter )
129 aReporter->ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
130
131 return success;
132}
133
134
135#if !FLASH_OVAL_HOLE
136// A helper class to transform an oblong hole to a segment
137static void convertOblong2Segment( const VECTOR2I& aSize, const EDA_ANGLE& aOrient, VECTOR2I& aStart, VECTOR2I& aEnd );
138#endif
139
140
141int GERBER_WRITER::createDrillFile( wxString& aFullFilename, bool aIsNpth,
142 DRILL_LAYER_PAIR aLayerPair )
143{
144 int holes_count;
145
146 LOCALE_IO dummy; // Use the standard notation for double numbers
147
148 GERBER_PLOTTER plotter;
149
150 // Gerber drill file imply X2 format:
151 plotter.UseX2format( true );
152 plotter.UseX2NetAttributes( true );
153 plotter.DisableApertMacros( false );
154
155 // Add the standard X2 header, without FileFunction
156 AddGerberX2Header( &plotter, m_pcb );
157 plotter.SetViewport( m_offset, pcbIUScale.IU_PER_MILS/10, /* scale */ 1.0, /* mirror */false );
158
159 // has meaning only for gerber plotter. Must be called only after SetViewport
160 plotter.SetGerberCoordinatesFormat( 6 );
161 plotter.SetCreator( wxT( "PCBNEW" ) );
162
163 // Add the standard X2 FileFunction for drill files
164 // %TF.FileFunction,Plated[NonPlated],layer1num,layer2num,PTH[NPTH][Blind][Buried],Drill[Rout][Mixed]*%
165 wxString text = BuildFileFunctionAttributeString( aLayerPair,
166 aIsNpth ? TYPE_FILE::NPTH_FILE
168 plotter.AddLineToHeader( text );
169
170 // Add file polarity (positive)
171 text = wxT( "%TF.FilePolarity,Positive*%" );
172 plotter.AddLineToHeader( text );
173
174 if( !plotter.OpenFile( aFullFilename ) )
175 return -1;
176
177 plotter.StartPlot( wxT( "1" ) );
178
179 holes_count = 0;
180
181 VECTOR2I hole_pos;
182 bool last_item_is_via = true; // a flag to clear object attributes when a via hole is created.
183
184 for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
185 {
186 HOLE_INFO& hole_descr = m_holeListBuffer[ii];
187 hole_pos = hole_descr.m_Hole_Pos;
188
189 // Manage the aperture attributes: in drill files 3 attributes can be used:
190 // "ViaDrill", only for vias, not pads
191 // "ComponentDrill", only for Through Holes pads
192 // "Slot" for oblong holes;
193 GBR_METADATA gbr_metadata;
194
195 if( dyn_cast<const PCB_VIA*>( hole_descr.m_ItemParent ) )
196 {
198
199 if( !last_item_is_via )
200 {
201 // be sure the current object attribute is cleared for vias
202 plotter.EndBlock( nullptr );
203 }
204
205 last_item_is_via = true;
206 }
207 else if( dyn_cast<const PAD*>( hole_descr.m_ItemParent ) )
208 {
209 last_item_is_via = false;
210 const PAD* pad = dyn_cast<const PAD*>( hole_descr.m_ItemParent );
211
212 if( pad->GetProperty() == PAD_PROP::CASTELLATED )
213 {
214 gbr_metadata.SetApertureAttrib(
216 }
217 else
218 {
219 // Good practice of oblong pad holes (slots) is to use a specific aperture for
220 // routing, not used in drill commands.
221 if( hole_descr.m_Hole_Shape )
222 gbr_metadata.SetApertureAttrib(
224 else
225 gbr_metadata.SetApertureAttrib(
227 }
228
229 // Add object attribute: component reference to pads (mainly useful for users)
230 wxString ref = pad->GetParentFootprint()->GetReference();
231
232 gbr_metadata.SetCmpReference( ref );
234 }
235
236 if( hole_descr.m_Hole_Shape )
237 {
238#if FLASH_OVAL_HOLE // set to 1 to use flashed oblong holes,
239 // 0 to draw them as a line.
240 plotter.FlashPadOval( hole_pos, hole_descr.m_Hole_Size, hole_descr.m_Hole_Orient,
241 FILLED, &gbr_metadata );
242#else
243 // Use routing for oblong hole (Slots)
244 VECTOR2I start, end;
245 convertOblong2Segment( hole_descr.m_Hole_Size, hole_descr.m_Hole_Orient, start, end );
246 int width = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
247
248 if ( width == 0 )
249 continue;
250
251 plotter.ThickSegment( start+hole_pos, end+hole_pos, width, FILLED, &gbr_metadata );
252#endif
253 }
254 else
255 {
256 int diam = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
257 plotter.FlashPadCircle( hole_pos, diam, FILLED, &gbr_metadata );
258 }
259
260 holes_count++;
261 }
262
263 plotter.EndPlot();
264
265 return holes_count;
266}
267
268
269#if !FLASH_OVAL_HOLE
270void convertOblong2Segment( const VECTOR2I& aSize, const EDA_ANGLE& aOrient, VECTOR2I& aStart,
271 VECTOR2I& aEnd )
272{
273 VECTOR2I size( aSize );
274 EDA_ANGLE orient( aOrient );
275
276 /* The pad will be drawn as an oblong shape with size.y > size.x
277 * (Oval vertical orientation 0)
278 */
279 if( size.x > size.y )
280 {
281 std::swap( size.x, size.y );
282 orient += ANGLE_90;
283 }
284
285 int deltaxy = size.y - size.x; // distance between centers of the oval
286 aStart = VECTOR2I( 0, deltaxy / 2 );
287 RotatePoint( aStart, orient );
288
289 aEnd = VECTOR2I( 0, -deltaxy / 2 );
290 RotatePoint( aEnd, orient );
291}
292#endif
293
294
295void GERBER_WRITER::SetFormat( int aRightDigits )
296{
297 /* Set conversion scale depending on drill file units */
298 m_conversionUnits = 1.0 / pcbIUScale.IU_PER_MM; // Gerber units = mm
299
300 // Set precision (unit is mm).
301 m_precision.m_Lhs = 4;
302 m_precision.m_Rhs = aRightDigits == 6 ? 6 : 5;
303}
304
305
306const wxString GERBER_WRITER::getDrillFileName( DRILL_LAYER_PAIR aPair, bool aNPTH,
307 bool aMerge_PTH_NPTH ) const
308{
309 // Gerber files extension is always .gbr.
310 // Therefore, to mark drill files, add "-drl" to the filename.
311 wxFileName fname( GENDRILL_WRITER_BASE::getDrillFileName( aPair, aNPTH, aMerge_PTH_NPTH ) );
312 fname.SetName( fname.GetName() + wxT( "-drl" ) );
313
314 return fname.GetFullPath();
315}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:282
@ 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:49
Definition: pad.h:53
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:154
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:164
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:437
Handle special data (items attributes) during plot.
std::pair< PCB_LAYER_ID, PCB_LAYER_ID > DRILL_LAYER_PAIR
static void convertOblong2Segment(const VECTOR2I &aSize, const EDA_ANGLE &aOrient, VECTOR2I &aStart, VECTOR2I &aEnd)
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
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
@ RPT_SEVERITY_ACTION
std::vector< FAB_LAYER_COLOR > dummy
const double IU_PER_MM
Definition: base_units.h:76
const double IU_PER_MILS
Definition: base_units.h:77
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:228
VECTOR2< int > VECTOR2I
Definition: vector2d.h:602