KiCad PCB EDA Suite
gbr_metadata.cpp File Reference

helper functions to handle the gerber metadata in files, related to the netlist info and aperture attribute. More...

#include <fctsys.h>
#include <gbr_metadata.h>

Go to the source code of this file.

Macros

#define NO_NET_NAME   wxT( "N/C" )
 
#define NO_PAD_NAME   wxT( "" )
 

Functions

wxString FormatStringFromGerber (const wxString &aString)
 This helper function make the inverse conversion of formatStringToGerber() It converts a "normalized" gerber string and convert it to a 16 bits sequence unicode. More...
 
std::string formatStringToGerber (const wxString &aString)
 This helper function "normalize" aString and convert it to a Gerber std::string Normalisation means convert any code > 0x7F and unautorized code to a hexadecimal 16 bits sequence unicode unautorized codes are ',' '*' '' '\'. More...
 
bool FormatNetAttribute (std::string &aPrintedText, std::string &aLastNetAttributes, GBR_NETLIST_METADATA *aData, bool &aClearPreviousAttributes, bool aUseX1StructuredComment)
 Generates the string to print to a gerber file, to set a net attribute for a graphic object. More...
 

Detailed Description

helper functions to handle the gerber metadata in files, related to the netlist info and aperture attribute.

Definition in file gbr_metadata.cpp.

Macro Definition Documentation

#define NO_NET_NAME   wxT( "N/C" )

Definition at line 228 of file gbr_metadata.cpp.

Referenced by FormatNetAttribute().

#define NO_PAD_NAME   wxT( "" )

Definition at line 229 of file gbr_metadata.cpp.

Referenced by FormatNetAttribute().

Function Documentation

bool FormatNetAttribute ( std::string &  aPrintedText,
std::string &  aLastNetAttributes,
GBR_NETLIST_METADATA aData,
bool &  aClearPreviousAttributes,
bool  aUseX1StructuredComment 
)

Generates the string to print to a gerber file, to set a net attribute for a graphic object.

Parameters
aPrintedTextis the string to print
aLastNetAttributesis the current full set of attributes.
aDatais the GBR_NETLIST_METADATA associated to the graphic object (can be NULL if no associated metadata, and aClearPreviousAttributes will be set to false)
aClearPreviousAttributesreturns true if the full set of attributes must be deleted from file before adding new attribute (happens when a previous attribute does not exist no more).
aUseX1StructuredComment= false in X2 mode, and true in X1 mode to add the net attribut in compatible X1 structured comment (i.e. prefixed by "G04 #@! ")
Returns
false if nothing can be done (GBR_NETLIST_METADATA has GBR_APERTURE_ATTRIB_NONE, and true if OK if the new attribute(s) is the same as current attribute(s), aPrintedText will be empty

Definition at line 231 of file gbr_metadata.cpp.

References formatStringToGerber(), GBR_NETLIST_METADATA::GBR_NETINFO_CMP, GBR_NETLIST_METADATA::GBR_NETINFO_NET, GBR_NETLIST_METADATA::GBR_NETINFO_PAD, GBR_NETLIST_METADATA::GBR_NETINFO_UNSPECIFIED, GBR_NETLIST_METADATA::m_Cmpref, GBR_NETLIST_METADATA::m_NetAttribType, GBR_NETLIST_METADATA::m_Netname, GBR_NETLIST_METADATA::m_NotInNet, GBR_NETLIST_METADATA::m_Padname, NO_NET_NAME, and NO_PAD_NAME.

Referenced by GERBER_PLOTTER::formatNetAttribute().

234 {
235  aClearPreviousAttributes = false;
236  wxString prepend_string;
237  wxString eol_string;
238 
239  if( aUseX1StructuredComment )
240  {
241  prepend_string = "G04 #@! ";
242  eol_string = "*\n";
243  }
244  else
245  {
246  prepend_string = "%";
247  eol_string = "*%\n";
248  }
249 
250  // print a Gerber net attribute record.
251  // it is added to the object attributes dictionnary
252  // On file, only modified or new attributes are printed.
253  if( aData == NULL )
254  return false;
255 
256  std::string pad_attribute_string;
257  std::string net_attribute_string;
258  std::string cmp_attribute_string;
259 
261  return false; // idle command: do nothing
262 
264  {
265  // print info associated to a flashed pad (cmpref, pad name)
266  // example: %TO.P,R5,3*%
267  pad_attribute_string = prepend_string + "TO.P,";
268  pad_attribute_string += formatStringToGerber( aData->m_Cmpref ) + ",";
269 
270  if( aData->m_Padname.IsEmpty() )
271  // Happens for "mechanical" or never connected pads
272  pad_attribute_string += formatStringToGerber( NO_PAD_NAME );
273  else
274  pad_attribute_string += formatStringToGerber( aData->m_Padname );
275 
276  pad_attribute_string += eol_string;
277  }
278 
280  {
281  // print info associated to a net
282  // example: %TO.N,Clk3*%
283  net_attribute_string = prepend_string + "TO.N,";
284 
285  if( aData->m_Netname.IsEmpty() )
286  {
287  if( aData->m_NotInNet )
288  {
289  // Happens for not connectable pads: mechanical pads
290  // and pads with no padname/num
291  // In this case the net name must be left empty
292  }
293  else
294  {
295  // Happens for not connected pads: use a normalized
296  // dummy name
297  net_attribute_string += formatStringToGerber( NO_NET_NAME );
298  }
299  }
300  else
301  net_attribute_string += formatStringToGerber( aData->m_Netname );
302 
303  net_attribute_string += eol_string;
304  }
305 
308  {
309  // print info associated to a footprint
310  // example: %TO.C,R2*%
311  // Because GBR_NETINFO_PAD option already contains this info, it is not
312  // created here for a GBR_NETINFO_PAD attribute
313  cmp_attribute_string = prepend_string + "TO.C,";
314  cmp_attribute_string += formatStringToGerber( aData->m_Cmpref ) + eol_string;
315  }
316 
317  // the full list of requested attributes:
318  std::string full_attribute_string = pad_attribute_string + net_attribute_string
319  + cmp_attribute_string;
320  // the short list of requested attributes
321  // (only modified or new attributes are stored here):
322  std::string short_attribute_string;
323 
324  if( aLastNetAttributes != full_attribute_string )
325  {
326  // first, remove no more existing attributes.
327  // Because in Kicad the full attribute list is evaluated for each object,
328  // the entire dictionnary is cleared
329  bool clearDict = false;
330 
331  if( aLastNetAttributes.find( "TO.P," ) != std::string::npos )
332  {
333  if( pad_attribute_string.empty() ) // No more this attribute
334  clearDict = true;
335  else if( aLastNetAttributes.find( pad_attribute_string )
336  == std::string::npos ) // This attribute has changed
337  short_attribute_string += pad_attribute_string;
338  }
339  else // New attribute
340  short_attribute_string += pad_attribute_string;
341 
342  if( aLastNetAttributes.find( "TO.N," ) != std::string::npos )
343  {
344  if( net_attribute_string.empty() ) // No more this attribute
345  clearDict = true;
346  else if( aLastNetAttributes.find( net_attribute_string )
347  == std::string::npos ) // This attribute has changed
348  short_attribute_string += net_attribute_string;
349  }
350  else // New attribute
351  short_attribute_string += net_attribute_string;
352 
353  if( aLastNetAttributes.find( "TO.C," ) != std::string::npos )
354  {
355  if( cmp_attribute_string.empty() ) // No more this attribute
356  clearDict = true;
357  else if( aLastNetAttributes.find( cmp_attribute_string )
358  == std::string::npos ) // This attribute has changed
359  short_attribute_string += cmp_attribute_string;
360  }
361  else // New attribute
362  short_attribute_string += cmp_attribute_string;
363 
364  aClearPreviousAttributes = clearDict;
365 
366  aLastNetAttributes = full_attribute_string;
367 
368  if( clearDict )
369  aPrintedText = full_attribute_string;
370  else
371  aPrintedText = short_attribute_string;
372  }
373 
374  return true;
375 }
#define NO_NET_NAME
print info associated to a component (TO.C attribute)
wxString m_Cmpref
the component reference parent of the data
std::string formatStringToGerber(const wxString &aString)
This helper function "normalize" aString and convert it to a Gerber std::string Normalisation means c...
wxString m_Padname
for a flashed pad: the pad name ((TO.P attribute)
#define NO_PAD_NAME
wxString m_Netname
for items associated to a net: the netname
print info associated to a flashed pad (TO.P attribute)
bool m_NotInNet
true if a pad of a footprint cannot be connected (for instance a mechanical NPTH, ot a not named pad)...
print info associated to a net (TO.N attribute)
int m_NetAttribType
the type of net info (used to define the gerber string to create)
wxString FormatStringFromGerber ( const wxString &  aString)

This helper function make the inverse conversion of formatStringToGerber() It converts a "normalized" gerber string and convert it to a 16 bits sequence unicode.

Parameters
aString= the wxString compliant with a gerber string format
Returns
a wxString (unicode 16) from the gerber string

Definition at line 145 of file gbr_metadata.cpp.

Referenced by GERBER_JOBFILE_READER::ReadGerberJobFile().

146 {
147  // make the inverse conversion of formatStringToGerber()
148  // It converts a "normalized" gerber string and convert it to a 16 bits sequence unicode
149  // and return a wxString (unicode 16) from the gerber string
150  wxString txt;
151 
152  for( unsigned ii = 0; ii < aString.Length(); ++ii )
153  {
154  unsigned code = aString[ii];
155 
156  if( code == '\\' )
157  {
158  // Convert 4 hexadecimal digits to a 16 bit unicode
159  // (Gerber allows only 4 hexadecimal digits)
160  long value = 0;
161 
162  for( int jj = 0; jj < 4; jj++ )
163  {
164  value <<= 4;
165  code = aString[++ii];
166  // Very basic conversion, but it expects a valid gerber file
167  int hexa = (code <= '9' ? code - '0' : code - 'A' + 10) & 0xF;
168  value += hexa;
169  }
170 
171  txt.Append( wxChar( value ) );
172  }
173  else
174  txt.Append( aString[ii] );
175  }
176 
177  return txt;
178 }
std::string formatStringToGerber ( const wxString &  aString)

This helper function "normalize" aString and convert it to a Gerber std::string Normalisation means convert any code > 0x7F and unautorized code to a hexadecimal 16 bits sequence unicode unautorized codes are ',' '*' '' '\'.

Parameters
aString= the wxString to convert
Returns
a std::string (ASCII7 coded) compliant with a gerber string

Definition at line 181 of file gbr_metadata.cpp.

Referenced by GERBER_JOBFILE_WRITER::addJSONFilesAttributes(), GERBER_JOBFILE_WRITER::addJSONMaterialStackup(), and FormatNetAttribute().

182 {
183  /* format string means convert any code > 0x7F and unautorized code to a hexadecimal
184  * 16 bits sequence unicode
185  * unautorized codes are ',' '*' '%' '\'
186  */
187  std::string txt;
188 
189  txt.reserve( aString.Length() );
190 
191  for( unsigned ii = 0; ii < aString.Length(); ++ii )
192  {
193  unsigned code = aString[ii];
194  bool convert = false;
195 
196  switch( code )
197  {
198  case '\\':
199  case '%':
200  case '*':
201  case ',':
202  convert = true;
203  break;
204 
205  default:
206  break;
207  }
208 
209  if( convert || code > 0x7F )
210  {
211  txt += '\\';
212 
213  // Convert code to 4 hexadecimal digit
214  // (Gerber allows only 4 hexadecimal digit)
215  char hexa[32];
216  sprintf( hexa,"%4.4X", code & 0xFFFF);
217  txt += hexa;
218  }
219  else
220  txt += char( code );
221  }
222 
223  return txt;
224 }