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 <plot_auxiliary_data.h>

Go to the source code of this file.

Macros

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

Functions

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)
 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 158 of file gbr_metadata.cpp.

Referenced by FormatNetAttribute().

#define NO_PAD_NAME   wxT( "" )

Definition at line 159 of file gbr_metadata.cpp.

Referenced by FormatNetAttribute().

Function Documentation

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

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.
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).
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 161 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().

163 {
164  aClearPreviousAttributes = false;
165 
166  // print a Gerber net attribute record.
167  // it is added to the object attributes dictionnary
168  // On file, only modified or new attributes are printed.
169  if( aData == NULL )
170  return false;
171 
172  std::string pad_attribute_string;
173  std::string net_attribute_string;
174  std::string cmp_attribute_string;
175 
177  return false; // idle command: do nothing
178 
180  {
181  // print info associated to a flashed pad (cmpref, pad name)
182  // example: %TO.P,R5,3*%
183  pad_attribute_string = "%TO.P,";
184  pad_attribute_string += formatStringToGerber( aData->m_Cmpref ) + ",";
185 
186  if( aData->m_Padname.IsEmpty() )
187  // Happens for "mechanical" or never connected pads
188  pad_attribute_string += formatStringToGerber( NO_PAD_NAME );
189  else
190  pad_attribute_string += formatStringToGerber( aData->m_Padname );
191 
192  pad_attribute_string += "*%\n";
193  }
194 
196  {
197  // print info associated to a net
198  // example: %TO.N,Clk3*%
199  net_attribute_string = "%TO.N,";
200 
201  if( aData->m_Netname.IsEmpty() )
202  {
203  if( aData->m_NotInNet )
204  {
205  // Happens for not connectable pads: mechanical pads
206  // and pads with no padname/num
207  // In this case the net name must be left empty
208  }
209  else
210  {
211  // Happens for not connected pads: use a normalized
212  // dummy name
213  net_attribute_string += formatStringToGerber( NO_NET_NAME );
214  }
215  }
216  else
217  net_attribute_string += formatStringToGerber( aData->m_Netname );
218 
219  net_attribute_string += "*%\n";
220  }
221 
224  {
225  // print info associated to a footprint
226  // example: %TO.C,R2*%
227  // Because GBR_NETINFO_PAD option already contains this info, it is not
228  // created here for a GBR_NETINFO_PAD attribute
229  cmp_attribute_string = "%TO.C,";
230  cmp_attribute_string += formatStringToGerber( aData->m_Cmpref ) + "*%\n";
231  }
232 
233  // the full list of requested attributes:
234  std::string full_attribute_string = pad_attribute_string + net_attribute_string
235  + cmp_attribute_string;
236  // the short list of requested attributes
237  // (only modified or new attributes are stored here):
238  std::string short_attribute_string;
239 
240  if( aLastNetAttributes != full_attribute_string )
241  {
242  // first, remove no more existing attributes.
243  // Because in Kicad the full attribute list is evaluated for each object,
244  // the entire dictionnary is cleared
245  bool clearDict = false;
246 
247  if( aLastNetAttributes.find( "%TO.P," ) != std::string::npos )
248  {
249  if( pad_attribute_string.empty() ) // No more this attribute
250  clearDict = true;
251  else if( aLastNetAttributes.find( pad_attribute_string )
252  == std::string::npos ) // This attribute has changed
253  short_attribute_string += pad_attribute_string;
254  }
255  else // New attribute
256  short_attribute_string += pad_attribute_string;
257 
258  if( aLastNetAttributes.find( "%TO.N," ) != std::string::npos )
259  {
260  if( net_attribute_string.empty() ) // No more this attribute
261  clearDict = true;
262  else if( aLastNetAttributes.find( net_attribute_string )
263  == std::string::npos ) // This attribute has changed
264  short_attribute_string += net_attribute_string;
265  }
266  else // New attribute
267  short_attribute_string += net_attribute_string;
268 
269  if( aLastNetAttributes.find( "%TO.C," ) != std::string::npos )
270  {
271  if( cmp_attribute_string.empty() ) // No more this attribute
272  clearDict = true;
273  else if( aLastNetAttributes.find( cmp_attribute_string )
274  == std::string::npos ) // This attribute has changed
275  short_attribute_string += cmp_attribute_string;
276  }
277  else // New attribute
278  short_attribute_string += cmp_attribute_string;
279 
280  aClearPreviousAttributes = clearDict;
281 
282  aLastNetAttributes = full_attribute_string;
283 
284  if( clearDict )
285  aPrintedText = full_attribute_string;
286  else
287  aPrintedText = short_attribute_string;
288  }
289 
290  return true;
291 }
#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)
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 111 of file gbr_metadata.cpp.

Referenced by FormatNetAttribute().

112 {
113  /* format string means convert any code > 0x7F and unautorized code to a hexadecimal
114  * 16 bits sequence unicode
115  * unautorized codes are ',' '*' '%' '\'
116  */
117  std::string txt;
118 
119  txt.reserve( aString.Length() );
120 
121  for( unsigned ii = 0; ii < aString.Length(); ++ii )
122  {
123  unsigned code = aString[ii];
124  bool convert = false;
125 
126  switch( code )
127  {
128  case '\\':
129  case '%':
130  case '*':
131  case ',':
132  convert = true;
133  break;
134 
135  default:
136  break;
137  }
138 
139  if( convert || code > 0x7F )
140  {
141  txt += '\\';
142 
143  // Convert code to 4 hexadecimal digit
144  // (Gerber allows only 4 hexadecimal digit)
145  char hexa[32];
146  sprintf( hexa,"%4.4X", code & 0xFFFF);
147  txt += hexa;
148  }
149  else
150  txt += char( code );
151  }
152 
153  return txt;
154 }