KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcbplot.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) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <plotters/plotter.h>
27#include <pcbplot.h>
28#include <common.h>
29#include <base_units.h>
30#include <lset.h>
31#include <locale_io.h>
32#include <reporter.h>
33#include <board.h>
35#include <pcb_plot_params.h>
36#include <wx/ffile.h>
37#include <dialog_plot.h>
38#include <build_version.h>
39#include <gbr_metadata.h>
40#include <render_settings.h>
41#include <pcb_plotter.h>
42
43const wxString GetGerberProtelExtension( int aLayer )
44{
45 if( IsCopperLayer( aLayer ) )
46 {
47 if( aLayer == F_Cu )
48 return wxT( "gtl" );
49 else if( aLayer == B_Cu )
50 return wxT( "gbl" );
51 else
52 return wxString( wxT( "g" ) ) << CopperLayerToOrdinal( ToLAYER_ID( aLayer ) );
53 }
54 else
55 {
56 switch( aLayer )
57 {
58 case B_Adhes: return wxT( "gba" );
59 case F_Adhes: return wxT( "gta" );
60
61 case B_Paste: return wxT( "gbp" );
62 case F_Paste: return wxT( "gtp" );
63
64 case B_SilkS: return wxT( "gbo" );
65 case F_SilkS: return wxT( "gto" );
66
67 case B_Mask: return wxT( "gbs" );
68 case F_Mask: return wxT( "gts" );
69
70 case Edge_Cuts: return wxT( "gm1" );
71
72 case Dwgs_User:
73 case Cmts_User:
74 case Eco1_User:
75 case Eco2_User:
76 default: return wxT( "gbr" );
77 }
78 }
79}
80
81
82const wxString GetGerberFileFunctionAttribute( const BOARD* aBoard, int aLayer )
83{
84 wxString attrib;
85
86
87 switch( aLayer )
88 {
89 case F_Adhes:
90 attrib = wxT( "Glue,Top" );
91 break;
92
93 case B_Adhes:
94 attrib = wxT( "Glue,Bot" );
95 break;
96
97 case F_SilkS:
98 attrib = wxT( "Legend,Top" );
99 break;
100
101 case B_SilkS:
102 attrib = wxT( "Legend,Bot" );
103 break;
104
105 case F_Mask:
106 attrib = wxT( "Soldermask,Top" );
107 break;
108
109 case B_Mask:
110 attrib = wxT( "Soldermask,Bot" );
111 break;
112
113 case F_Paste:
114 attrib = wxT( "Paste,Top" );
115 break;
116
117 case B_Paste:
118 attrib = wxT( "Paste,Bot" );
119 break;
120
121 case Edge_Cuts:
122 // Board outline.
123 // Can be "Profile,NP" (Not Plated: usual) or "Profile,P"
124 // This last is the exception (Plated)
125 attrib = wxT( "Profile,NP" );
126 break;
127
128 case Dwgs_User:
129 attrib = wxT( "OtherDrawing,Comment" );
130 break;
131
132 case Cmts_User:
133 attrib = wxT( "Other,Comment" );
134 break;
135
136 case Eco1_User:
137 attrib = wxT( "Other,ECO1" );
138 break;
139
140 case Eco2_User:
141 attrib = wxT( "Other,ECO2" );
142 break;
143
144 case B_Fab:
145 // This is actually a assembly layer
146 attrib = wxT( "AssemblyDrawing,Bot" );
147 break;
148
149 case F_Fab:
150 // This is actually a assembly layer
151 attrib = wxT( "AssemblyDrawing,Top" );
152 break;
153
154 case B_Cu:
155 attrib.Printf( wxT( "Copper,L%d,Bot" ), aBoard->GetCopperLayerCount() );
156 break;
157
158 case F_Cu:
159 attrib = wxT( "Copper,L1,Top" );
160 break;
161
162 default:
163 if( IsCopperLayer( aLayer ) )
164 {
165 // aLayer use even values, and the first internal layer
166 // is B_Cu + 2. And in gerber file, layer id is 2 (1 is F_Cu)
167 int ly_id = ( ( aLayer - B_Cu ) / 2 ) + 1;
168 attrib.Printf( wxT( "Copper,L%d,Inr" ), ly_id );
169 }
170 else
171 attrib.Printf( wxT( "Other,User" ), aLayer+1 );
172 break;
173 }
174
175 // This code adds a optional parameter: the type of copper layers.
176 // Because it is not used by Pcbnew (it can be used only by external autorouters)
177 // user do not really set this parameter.
178 // Therefore do not add it.
179 // However, this code is left here, for perhaps a future usage.
180#if 0
181 // Add the signal type of the layer, if relevant
182 if( IsCopperLayer( aLayer ) )
183 {
184 LAYER_T type = aBoard->GetLayerType( ToLAYER_ID( aLayer ) );
185
186 switch( type )
187 {
188 case LT_SIGNAL:
189 attrib += wxT( ",Signal" );
190 break;
191 case LT_POWER:
192 attrib += wxT( ",Plane" );
193 break;
194 case LT_MIXED:
195 attrib += wxT( ",Mixed" );
196 break;
197 default:
198 break; // do nothing (but avoid a warning for unhandled LAYER_T values from GCC)
199 }
200 }
201#endif
202
203 wxString fileFct;
204 fileFct.Printf( wxT( "%%TF.FileFunction,%s*%%" ), attrib );
205
206 return fileFct;
207}
208
209
210static const wxString GetGerberFilePolarityAttribute( int aLayer )
211{
212 /* build the string %TF.FilePolarity,Positive*%
213 * or %TF.FilePolarity,Negative*%
214 * an emply string for layers which do not use a polarity
215 *
216 * The value of the .FilePolarity specifies whether the image represents the
217 * presence or absence of material.
218 * This attribute can only be used when the file represents a pattern in a material layer,
219 * e.g. copper, solder mask, legend.
220 * Together with.FileFunction it defines the role of that image in
221 * the layer structure of the PCB.
222 * Note that the .FilePolarity attribute does not change the image -
223 * no attribute does.
224 * It changes the interpretation of the image.
225 * For example, in a copper layer in positive polarity a round flash generates a copper pad.
226 * In a copper layer in negative polarity it generates a clearance.
227 * Solder mask images usually represent solder mask openings and are then negative.
228 * This may be counter-intuitive.
229 */
230 int polarity = 0;
231
232 switch( aLayer )
233 {
234 case F_Adhes:
235 case B_Adhes:
236 case F_SilkS:
237 case B_SilkS:
238 case F_Paste:
239 case B_Paste:
240 polarity = 1;
241 break;
242
243 case F_Mask:
244 case B_Mask:
245 polarity = -1;
246 break;
247
248 default:
249 if( IsCopperLayer( aLayer ) )
250 polarity = 1;
251 break;
252 }
253
254 wxString filePolarity;
255
256 if( polarity == 1 )
257 filePolarity = wxT( "%TF.FilePolarity,Positive*%" );
258 if( polarity == -1 )
259 filePolarity = wxT( "%TF.FilePolarity,Negative*%" );
260
261 return filePolarity;
262}
263
264
265/* Add some X2 attributes to the file header, as defined in the
266 * Gerber file format specification J4 and "Revision 2015.06"
267 */
268
269
270// A helper function to convert a X2 attribute string to a X1 structured comment:
271static wxString& makeStringCompatX1( wxString& aText, bool aUseX1CompatibilityMode )
272{
273 if( aUseX1CompatibilityMode )
274 {
275 aText.Replace( wxT( "%" ), wxEmptyString );
276 aText.Prepend( wxT( "G04 #@! " ) );
277 }
278
279 return aText;
280}
281
282
283// A helper function to replace reserved chars (separators in gerber fields)
284// in a gerber string field.
285// reserved chars are replaced by _ (for ,) or an escaped sequence (for * and %)
286static void replaceReservedCharsField( wxString& aMsg )
287{
288 aMsg.Replace( wxT( "," ), wxT( "_" ) ); // can be replaced by \\u002C
289 aMsg.Replace( wxT( "*" ), wxT( "\\u002A" ) );
290 aMsg.Replace( wxT( "%" ), wxT( "\\u0025" ) );
291}
292
293
294void AddGerberX2Header( PLOTTER* aPlotter, const BOARD* aBoard, bool aUseX1CompatibilityMode )
295{
296 wxString text;
297
298 // Creates the TF,.GenerationSoftware. Format is:
299 // %TF,.GenerationSoftware,<vendor>,<application name>[,<application version>]*%
300 text.Printf( wxT( "%%TF.GenerationSoftware,KiCad,Pcbnew,%s*%%" ), GetBuildVersion() );
301 aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
302
303 // creates the TF.CreationDate attribute:
306 aPlotter->AddLineToHeader( text );
307
308 // Creates the TF,.ProjectId. Format is (from Gerber file format doc):
309 // %TF.ProjectId,<project id>,<project GUID>,<revision id>*%
310 // <project id> is the name of the project, restricted to basic ASCII symbols only,
311 // Rem: <project id> accepts only ASCII 7 code (only basic ASCII codes are allowed in
312 // gerber files) and comma not accepted.
313 // All illegal chars will be replaced by underscore.
314 //
315 // <project GUID> is a string which is an unique id of a project.
316 // However Kicad does not handle such a project GUID, so it is built from the board name
317 wxFileName fn = aBoard->GetFileName();
318 wxString msg = fn.GetFullName();
319
320 // Build a <project GUID>, from the board name
321 wxString guid = GbrMakeProjectGUIDfromString( msg );
322
323 // build the <project id> string: this is the board short filename (without ext)
324 // and all non ASCII chars and reserved chars (, * % ) are replaced by '_'
325 msg = fn.GetName();
327
328 // build the <revision id> string. All non ASCII chars and reserved chars are replaced by '_'
329 wxString rev = ExpandTextVars( aBoard->GetTitleBlock().GetRevision(), aBoard->GetProject() );
331
332 if( rev.IsEmpty() )
333 rev = wxT( "rev?" );
334
335 text.Printf( wxT( "%%TF.ProjectId,%s,%s,%s*%%" ), msg.ToAscii(), guid, rev.ToAscii() );
336 aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
337
338 // Add the TF.SameCoordinates to specify that all gerber files uses the same origin and
339 // orientation, and the registration between files is OK.
340 // The parameter of TF.SameCoordinates is a string that is common to all files using the
341 // same registration. The string value has no meaning; it is just a key.
342 // Because there is no mirroring/rotation in Kicad, only the plot offset origin can create
343 // incorrect registration, so we create a key from plot offset options.
344 //
345 // Currently the key is "Original" when using absolute Pcbnew coordinates, and the PY and PY
346 // position of the auxiliary axis when using it.
347 // If we ever add user-settable absolute Pcbnew coordinates, we'll need to change the way
348 // the key is built to ensure file only using the *same* axis have the same key.
349 wxString registration_id = wxT( "Original" );
350 VECTOR2I auxOrigin = aBoard->GetDesignSettings().GetAuxOrigin();
351
352 if( aBoard->GetPlotOptions().GetUseAuxOrigin() && auxOrigin.x && auxOrigin.y )
353 registration_id.Printf( wxT( "PX%xPY%x" ), auxOrigin.x, auxOrigin.y );
354
355 text.Printf( wxT( "%%TF.SameCoordinates,%s*%%" ), registration_id.GetData() );
356 aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
357}
358
359
360void AddGerberX2Attribute( PLOTTER* aPlotter, const BOARD* aBoard, int aLayer,
361 bool aUseX1CompatibilityMode )
362{
363 AddGerberX2Header( aPlotter, aBoard, aUseX1CompatibilityMode );
364
365 wxString text;
366
367 // Add the TF.FileFunction
368 text = GetGerberFileFunctionAttribute( aBoard, aLayer );
369 aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
370
371 // Add the TF.FilePolarity (for layers which support that)
373
374 if( !text.IsEmpty() )
375 aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
376}
LAYER_T
The allowed types of layers, same as Specctra DSN spec.
Definition board.h:186
@ LT_POWER
Definition board.h:189
@ LT_MIXED
Definition board.h:190
@ LT_SIGNAL
Definition board.h:188
wxString GetBuildVersion()
Get the full KiCad version string.
const VECTOR2I & GetAuxOrigin() const
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
LAYER_T GetLayerType(PCB_LAYER_ID aLayer) const
Return the type of the copper layer given by aLayer.
Definition board.cpp:792
TITLE_BLOCK & GetTitleBlock()
Definition board.h:813
int GetCopperLayerCount() const
Definition board.cpp:928
const wxString & GetFileName() const
Definition board.h:360
const PCB_PLOT_PARAMS & GetPlotOptions() const
Definition board.h:810
PROJECT * GetProject() const
Definition board.h:587
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1091
bool GetUseAuxOrigin() const
Base plotter engine class.
Definition plotter.h:136
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:198
const wxString & GetRevision() const
Definition title_block.h:86
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, int aFlags)
Definition common.cpp:62
The common library.
wxString GbrMakeProjectGUIDfromString(const wxString &aText)
Build a project GUID using format RFC4122 Version 1 or 4 from the project name, because a KiCad proje...
wxString GbrMakeCreationDateAttributeString(GBR_NC_STRING_FORMAT aFormat)
Handle special data (items attributes) during plot.
@ GBR_NC_STRING_FORMAT_X1
@ GBR_NC_STRING_FORMAT_X2
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition layer_ids.h:679
size_t CopperLayerToOrdinal(PCB_LAYER_ID aLayer)
Converts KiCad copper layer enum to an ordinal between the front and back layers.
Definition layer_ids.h:915
@ B_Adhes
Definition layer_ids.h:103
@ Edge_Cuts
Definition layer_ids.h:112
@ Dwgs_User
Definition layer_ids.h:107
@ F_Paste
Definition layer_ids.h:104
@ Cmts_User
Definition layer_ids.h:108
@ F_Adhes
Definition layer_ids.h:102
@ B_Mask
Definition layer_ids.h:98
@ B_Cu
Definition layer_ids.h:65
@ Eco1_User
Definition layer_ids.h:109
@ F_Mask
Definition layer_ids.h:97
@ B_Paste
Definition layer_ids.h:105
@ F_Fab
Definition layer_ids.h:119
@ F_SilkS
Definition layer_ids.h:100
@ Eco2_User
Definition layer_ids.h:110
@ B_SilkS
Definition layer_ids.h:101
@ F_Cu
Definition layer_ids.h:64
@ B_Fab
Definition layer_ids.h:118
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition lset.cpp:754
const wxString GetGerberProtelExtension(int aLayer)
Definition pcbplot.cpp:43
static wxString & makeStringCompatX1(wxString &aText, bool aUseX1CompatibilityMode)
Definition pcbplot.cpp:271
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:294
const wxString GetGerberFileFunctionAttribute(const BOARD *aBoard, int aLayer)
Return the "file function" attribute for aLayer, as defined in the Gerber file format specification J...
Definition pcbplot.cpp:82
static void replaceReservedCharsField(wxString &aMsg)
Definition pcbplot.cpp:286
void AddGerberX2Attribute(PLOTTER *aPlotter, const BOARD *aBoard, int aLayer, bool aUseX1CompatibilityMode)
Calculate some X2 attributes as defined in the Gerber file format specification and add them to the g...
Definition pcbplot.cpp:360
static const wxString GetGerberFilePolarityAttribute(int aLayer)
Definition pcbplot.cpp:210
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687