KiCad PCB EDA Suite
pcbplot.cpp File Reference
#include <fctsys.h>
#include <plot_common.h>
#include <confirm.h>
#include <wxPcbStruct.h>
#include <pcbplot.h>
#include <pcbstruct.h>
#include <base_units.h>
#include <reporter.h>
#include <class_board.h>
#include <pcbnew.h>
#include <plotcontroller.h>
#include <pcb_plot_params.h>
#include <wx/ffile.h>
#include <dialog_plot.h>
#include <macros.h>
#include <build_version.h>

Go to the source code of this file.

Functions

const wxString GetGerberProtelExtension (LAYER_NUM aLayer)
 Function GetGerberProtelExtension. More...
 
const wxString GetGerberFileFunctionAttribute (const BOARD *aBoard, LAYER_NUM aLayer)
 Function GetGerberFileFunctionAttribute Returns the "file function" attribute for aLayer, as defined in the Gerber file format specification J1 (chapter 5). More...
 
static const wxString GetGerberFilePolarityAttribute (LAYER_NUM aLayer)
 
static wxString & makeStringCompatX1 (wxString &aText, bool aUseX1CompatibilityMode)
 
void AddGerberX2Attribute (PLOTTER *aPlotter, const BOARD *aBoard, LAYER_NUM aLayer, bool aUseX1CompatibilityMode)
 Function AddGerberX2Attribute Calculates some X2 attributes, as defined in the Gerber file format specification J4 (chapter 5) and add them the to the gerber file header. More...
 
void BuildPlotFileName (wxFileName *aFilename, const wxString &aOutputDir, const wxString &aSuffix, const wxString &aExtension)
 Function BuildPlotFileName (helper function) Complete a plot filename: forces the output directory, add a suffix to the name and sets the specified extension the suffix is usually the layer name replaces not allowed chars in suffix by '_'. More...
 

Function Documentation

void AddGerberX2Attribute ( PLOTTER aPlotter,
const BOARD aBoard,
LAYER_NUM  aLayer,
bool  aUseX1CompatibilityMode 
)

Function AddGerberX2Attribute Calculates some X2 attributes, as defined in the Gerber file format specification J4 (chapter 5) and add them the to the gerber file header.

Parameters
aPlotter,thecurrent plotter.
aBoard= the board, needed to extract some info
aLayer= the layer number to create the attribute for
aUseX1CompatibilityMode= false to generate X2 attributes, true to use X1 compatibility (X2 attributes added as structured comments, starting by "G04 #@! " followed by the X2 attribute

Definition at line 272 of file pcbplot.cpp.

References PLOTTER::AddLineToHeader(), Format(), GetBuildVersion(), GetChars(), BOARD::GetFileName(), GetGerberFileFunctionAttribute(), GetGerberFilePolarityAttribute(), and makeStringCompatX1().

Referenced by StartPlotBoard().

274 {
275  wxString text;
276 
277  // Creates the TF,.GenerationSoftware. Format is:
278  // %TF,.GenerationSoftware,<vendor>,<application name>[,<application version>]*%
279  text.Printf( wxT( "%%TF.GenerationSoftware,KiCad,Pcbnew,%s*%%" ), GetBuildVersion() );
280  aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
281 
282  // creates the TF.CreationDate ext:
283  // The attribute value must conform to the full version of the ISO 8601
284  // date and time format, including time and time zone. Note that this is
285  // the date the Gerber file was effectively created,
286  // not the time the project of PCB was started
287  wxDateTime date( wxDateTime::GetTimeNow() );
288  // Date format: see http://www.cplusplus.com/reference/ctime/strftime
289  wxString msg = date.Format( wxT( "%z" ) ); // Extract the time zone offset
290  // The time zone offset format is + (or -) mm or hhmm (mm = number of minutes, hh = number of hours)
291  // we want +(or -) hh:mm
292  if( msg.Len() > 3 )
293  msg.insert( 3, ":", 1 ),
294  text.Printf( wxT( "%%TF.CreationDate,%s%s*%%" ), GetChars( date.FormatISOCombined() ), GetChars( msg ) );
295  aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
296 
297  // Creates the TF,.ProjectId. Format is (from Gerber file format doc):
298  // %TF.ProjectId,<project id>,<project GUID>,<revision id>*%
299  // <project id> is the name of the project, restricted to basic ASCII symbols only,
300  // and comma not accepted
301  // All illegal chars will be replaced by underscore
302  // <project GUID> is a 32 hexadecimal digits string which is an unique id of a project.
303  // This is a random 128-bit number expressed in 32 hexadecimal digits.
304  // See en.wikipedia.org/wiki/GUID for more information
305  // However Kicad does not handle such a project GUID, so it is built from the board name
306  // Rem: <project id> accepts only ASCII 7 code (only basic ASCII codes are allowed in gerber files).
307  wxFileName fn = aBoard->GetFileName();
308  msg = fn.GetFullName();
309  wxString guid;
310 
311  // Build a 32 digits GUID from the board name:
312  for( unsigned ii = 0; ii < msg.Len(); ii++ )
313  {
314  int cc1 = int( msg[ii] ) & 0x0F;
315  int cc2 = ( int( msg[ii] ) >> 4) & 0x0F;
316  guid << wxString::Format( wxT( "%X%X" ), cc2, cc1 );
317 
318  if( guid.Len() >= 32 )
319  break;
320  }
321 
322  // guid has 32 digits, so add missing digits
323  int cnt = 32 - guid.Len();
324 
325  if( cnt > 0 )
326  guid.Append( '0', cnt );
327 
328  // build the <project id> string: this is the board short filename (without ext)
329  // and all non ASCII chars and comma are replaced by '_'
330  msg = fn.GetName();
331  msg.Replace( wxT( "," ), wxT( "_" ) );
332 
333  // build the <rec> string. All non ASCII chars and comma are replaced by '_'
334  wxString rev = ((BOARD*)aBoard)->GetTitleBlock().GetRevision();
335  rev.Replace( wxT( "," ), wxT( "_" ) );
336 
337  if( rev.IsEmpty() )
338  rev = wxT( "rev?" );
339 
340  text.Printf( wxT( "%%TF.ProjectId,%s,%s,%s*%%" ), msg.ToAscii(), GetChars( guid ), rev.ToAscii() );
341  aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
342 
343  // Add the TF.FileFunction
344  text = GetGerberFileFunctionAttribute( aBoard, aLayer );
345  aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
346 
347  // Add the TF.FilePolarity (for layers which support that)
348  text = GetGerberFilePolarityAttribute( aLayer );
349 
350  if( !text.IsEmpty() )
351  aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
352 }
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: plot_common.h:166
wxString GetBuildVersion()
Function GetBuildVersion Return the build version string.
const wxString & GetFileName() const
Definition: class_board.h:237
static const wxString GetGerberFilePolarityAttribute(LAYER_NUM aLayer)
Definition: pcbplot.cpp:202
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
static wxString & makeStringCompatX1(wxString &aText, bool aUseX1CompatibilityMode)
Definition: pcbplot.cpp:261
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:166
const wxString GetGerberFileFunctionAttribute(const BOARD *aBoard, LAYER_NUM aLayer)
Function GetGerberFileFunctionAttribute Returns the "file function" attribute for aLayer...
Definition: pcbplot.cpp:89
void BuildPlotFileName ( wxFileName *  aFilename,
const wxString &  aOutputDir,
const wxString &  aSuffix,
const wxString &  aExtension 
)

Function BuildPlotFileName (helper function) Complete a plot filename: forces the output directory, add a suffix to the name and sets the specified extension the suffix is usually the layer name replaces not allowed chars in suffix by '_'.

Parameters
aFilename= the wxFileName to initialize Contains the base filename
aOutputDir= the path
aSuffix= the suffix to add to the base filename
aExtension= the file extension

Definition at line 355 of file pcbplot.cpp.

Referenced by DIALOG_SVG_PRINT::ExportSVGFile(), PLOT_CONTROLLER::OpenPlotfile(), and DIALOG_PLOT::Plot().

357 {
358  // aFilename contains the base filename only (without path and extension)
359  // when calling this function.
360  // It is expected to be a valid filename (this is usually the board filename)
361  aFilename->SetPath( aOutputDir );
362 
363  // Set the file extension
364  aFilename->SetExt( aExtension );
365 
366  // remove leading and trailing spaces if any from the suffix, if
367  // something survives add it to the name;
368  // also the suffix can contain some not allowed chars in filename (/ \ . :),
369  // so change them to underscore
370  // Remember it can be called from a python script, so the illegal chars
371  // have to be filtered here.
372  wxString suffix = aSuffix;
373  suffix.Trim( true );
374  suffix.Trim( false );
375 
376  wxString badchars = wxFileName::GetForbiddenChars(wxPATH_DOS);
377  badchars.Append( '%' );
378 
379  for( unsigned ii = 0; ii < badchars.Len(); ii++ )
380  suffix.Replace( badchars[ii], wxT("_") );
381 
382  if( !suffix.IsEmpty() )
383  aFilename->SetName( aFilename->GetName() + wxT( "-" ) + suffix );
384 }
const wxString GetGerberFileFunctionAttribute ( const BOARD aBoard,
LAYER_NUM  aLayer 
)

Function GetGerberFileFunctionAttribute Returns the "file function" attribute for aLayer, as defined in the Gerber file format specification J1 (chapter 5).

The returned string includes the "%TF.FileFunction" attribute prefix and the "*%" suffix.

Parameters
aBoard= the board, needed to get the total count of copper layers
aLayer= the layer number to create the attribute for
Returns
The attribute, as a text string

Definition at line 89 of file pcbplot.cpp.

References B_Adhes, B_Cu, B_Fab, B_Mask, B_Paste, B_SilkS, Cmts_User, Dwgs_User, Eco1_User, Eco2_User, Edge_Cuts, F_Adhes, F_Cu, F_Fab, F_Mask, F_Paste, F_SilkS, GetChars(), BOARD::GetCopperLayerCount(), BOARD::GetLayerType(), IsCopperLayer(), LT_MIXED, LT_POWER, LT_SIGNAL, and ToLAYER_ID().

Referenced by AddGerberX2Attribute().

90 {
91  wxString attrib;
92 
93  switch( aLayer )
94  {
95  case F_Adhes:
96  attrib = "Glue,Top";
97  break;
98 
99  case B_Adhes:
100  attrib = "Glue,Bot";
101  break;
102 
103  case F_SilkS:
104  attrib = "Legend,Top";
105  break;
106 
107  case B_SilkS:
108  attrib = "Legend,Bot";
109  break;
110 
111  case F_Mask:
112  attrib = "Soldermask,Top";
113  break;
114 
115  case B_Mask:
116  attrib = "Soldermask,Bot";
117  break;
118 
119  case F_Paste:
120  attrib = "Paste,Top";
121  break;
122 
123  case B_Paste:
124  attrib = "Paste,Bot";
125  break;
126 
127  case Edge_Cuts:
128  // Board outline.
129  // Can be "Profile,NP" (Not Plated: usual) or "Profile,P"
130  // This last is the exception (Plated)
131  attrib = "Profile,NP";
132  break;
133 
134  case Dwgs_User:
135  attrib = "Drawing";
136  break;
137 
138  case Cmts_User:
139  attrib = "Other,Comment";
140  break;
141 
142  case Eco1_User:
143  attrib = "Other,ECO1";
144  break;
145 
146  case Eco2_User:
147  attrib = "Other,ECO2";
148  break;
149 
150  case B_Fab:
151  attrib = "Other,Fab,Bot";
152  break;
153 
154  case F_Fab:
155  attrib = "Other,Fab,Top";
156  break;
157 
158  case B_Cu:
159  attrib.Printf( wxT( "Copper,L%d,Bot" ), aBoard->GetCopperLayerCount() );
160  break;
161 
162  case F_Cu:
163  attrib = "Copper,L1,Top";
164  break;
165 
166  default:
167  if( IsCopperLayer( aLayer ) )
168  attrib.Printf( wxT( "Copper,L%d,Inr" ), aLayer+1 );
169  else
170  attrib.Printf( wxT( "Other,User" ), aLayer+1 );
171  break;
172  }
173 
174  // Add the signal type of the layer, if relevant
175  if( IsCopperLayer( aLayer ) )
176  {
177  LAYER_T type = aBoard->GetLayerType( ToLAYER_ID( aLayer ) );
178 
179  switch( type )
180  {
181  case LT_SIGNAL:
182  attrib += ",Signal";
183  break;
184  case LT_POWER:
185  attrib += ",Plane";
186  break;
187  case LT_MIXED:
188  attrib += ",Mixed";
189  break;
190  default:
191  break; // do nothing (but avoid a warning for unhandled LAYER_T values from GCC)
192  }
193  }
194 
195  wxString fileFct;
196  fileFct.Printf( "%%TF.FileFunction,%s*%%", GetChars( attrib ) );
197 
198  return fileFct;
199 }
int GetCopperLayerCount() const
Function GetCopperLayerCount.
LAYER_T
Enum LAYER_T gives the allowed types of layers, same as Specctra DSN spec.
Definition: class_board.h:68
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
bool IsCopperLayer(LAYER_NUM aLayerId)
Function IsCopperLayer tests whether a layer is a copper layer.
LAYER_T GetLayerType(PCB_LAYER_ID aLayer) const
Function GetLayerType returns the type of the copper layer given by aLayer.
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:767
static const wxString GetGerberFilePolarityAttribute ( LAYER_NUM  aLayer)
static

Definition at line 202 of file pcbplot.cpp.

References B_Adhes, B_Mask, B_Paste, B_SilkS, F_Adhes, F_Mask, F_Paste, F_SilkS, and IsCopperLayer().

Referenced by AddGerberX2Attribute().

203 {
204  /* build the string %TF.FilePolarity,Positive*%
205  * or %TF.FilePolarity,Negative*%
206  * an emply string for layers which do not use a polarity
207  *
208  * The value of the .FilePolarity specifies whether the image represents the
209  * presence or absence of material.
210  * This attribute can only be used when the file represents a pattern in a material layer,
211  * e.g. copper, solder mask, legend.
212  * Together with.FileFunction it defines the role of that image in
213  * the layer structure of the PCB.
214  * Note that the .FilePolarity attribute does not change the image -
215  * no attribute does.
216  * It changes the interpretation of the image.
217  * For example, in a copper layer in positive polarity a round flash generates a copper pad.
218  * In a copper layer in negative polarity it generates a clearance.
219  * Solder mask images usually represent solder mask openings and are then negative.
220  * This may be counter-intuitive.
221  */
222  int polarity = 0;
223 
224  switch( aLayer )
225  {
226  case F_Adhes:
227  case B_Adhes:
228  case F_SilkS:
229  case B_SilkS:
230  case F_Paste:
231  case B_Paste:
232  polarity = 1;
233  break;
234 
235  case F_Mask:
236  case B_Mask:
237  polarity = -1;
238  break;
239 
240  default:
241  if( IsCopperLayer( aLayer ) )
242  polarity = 1;
243  break;
244  }
245 
246  wxString filePolarity;
247 
248  if( polarity == 1 )
249  filePolarity = "%TF.FilePolarity,Positive*%";
250  if( polarity == -1 )
251  filePolarity = "%TF.FilePolarity,Negative*%";
252 
253  return filePolarity;
254 }
bool IsCopperLayer(LAYER_NUM aLayerId)
Function IsCopperLayer tests whether a layer is a copper layer.
const wxString GetGerberProtelExtension ( LAYER_NUM  aLayer)

Function GetGerberProtelExtension.

Returns
the appropriate Gerber file extension for aLayer used by Protel, and still sometimes in use (although the official Gerber Ext is now .gbr)

Definition at line 48 of file pcbplot.cpp.

References B_Adhes, B_Cu, B_Mask, B_Paste, B_SilkS, Cmts_User, Dwgs_User, Eco1_User, Eco2_User, Edge_Cuts, F_Adhes, F_Cu, F_Mask, F_Paste, F_SilkS, Format(), and IsCopperLayer().

Referenced by PLOT_CONTROLLER::OpenPlotfile(), and DIALOG_PLOT::Plot().

49 {
50  if( IsCopperLayer( aLayer ) )
51  {
52  if( aLayer == F_Cu )
53  return wxT( "gtl" );
54  else if( aLayer == B_Cu )
55  return wxT( "gbl" );
56  else
57  {
58  return wxString::Format( wxT( "g%d" ), aLayer+1 );
59  }
60  }
61  else
62  {
63  switch( aLayer )
64  {
65  case B_Adhes: return wxT( "gba" );
66  case F_Adhes: return wxT( "gta" );
67 
68  case B_Paste: return wxT( "gbp" );
69  case F_Paste: return wxT( "gtp" );
70 
71  case B_SilkS: return wxT( "gbo" );
72  case F_SilkS: return wxT( "gto" );
73 
74  case B_Mask: return wxT( "gbs" );
75  case F_Mask: return wxT( "gts" );
76 
77  case Edge_Cuts: return wxT( "gm1" );
78 
79  case Dwgs_User:
80  case Cmts_User:
81  case Eco1_User:
82  case Eco2_User:
83  default: return wxT( "gbr" );
84  }
85  }
86 }
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
bool IsCopperLayer(LAYER_NUM aLayerId)
Function IsCopperLayer tests whether a layer is a copper layer.
static wxString& makeStringCompatX1 ( wxString &  aText,
bool  aUseX1CompatibilityMode 
)
static

Definition at line 261 of file pcbplot.cpp.

Referenced by AddGerberX2Attribute().

262 {
263  if( aUseX1CompatibilityMode )
264  {
265  aText.Replace( "%", "" );
266  aText.Prepend( "G04 #@! " );
267  }
268 
269  return aText;
270 }