KiCad PCB EDA Suite
PLACE_FILE_EXPORTER Class Reference

The ASCII format of the kicad place file is: More...

#include <export_footprints_placefile.h>

Public Member Functions

 PLACE_FILE_EXPORTER (BOARD *aBoard, bool aUnitsMM, bool aForceSmdItems, bool aTopSide, bool aBottomSide, bool aFormatCSV)
 Create a PLACE_FILE_EXPORTER. More...
 
std::string GenPositionData ()
 build a string filled with the position data More...
 
std::string GenReportData ()
 build a string filled with the pad report data This report does not used options aForceSmdItems,aTopSide, aBottomSide and aFormatCSV. More...
 
int GetFootprintCount ()
 

Static Public Member Functions

static std::string GetFrontSideName ()
 
static std::string GetBackSideName ()
 

Private Attributes

BOARDm_board
 
bool m_unitsMM
 
bool m_excludeAllTH
 
int m_side
 
bool m_formatCSV
 
int m_fpCount
 
wxPoint m_place_Offset
 

Detailed Description

The ASCII format of the kicad place file is:

Module positions - created on 04/12/2012 15:24:24

Printed by Pcbnew version pcbnew (2012-11-30 BZR 3828)-testing

Unit = inches, Angle = deg.

or

Unit = mm, Angle = deg.

Side : top

or

Side : bottom

or

Side : all

Ref Val Package PosX PosY Rot Side

C123 0,1uF/50V SM0603 1.6024 -2.6280 180.0 Front C124 0,1uF/50V SM0603 1.6063 -2.7579 180.0 Front C125 0,1uF/50V SM0603 1.6010 -2.8310 180.0 Front

End

Definition at line 51 of file export_footprints_placefile.h.

Constructor & Destructor Documentation

◆ PLACE_FILE_EXPORTER()

PLACE_FILE_EXPORTER::PLACE_FILE_EXPORTER ( BOARD aBoard,
bool  aUnitsMM,
bool  aForceSmdItems,
bool  aTopSide,
bool  aBottomSide,
bool  aFormatCSV 
)

Create a PLACE_FILE_EXPORTER.

Parameters
aBoardis the board
aUnitsMMis the unit option: true foo mm, false for inches
aForceSmdItemstrue to force not virtual and not flagged smd footprints but having only smd pads to be in list
aTopSidetrue to generate top side info
aBottomSidetrue to generate bottom side info
aFormatCSVtrue to generate a csv format info, false to generate a ascii info

Definition at line 73 of file export_footprints_placefile.cpp.

75 {
76  m_board = aBoard;
77  m_unitsMM = aUnitsMM;
78  m_excludeAllTH = aExcludeAllTH;
79  m_fpCount = 0;
80 
81  if( aTopSide && aBottomSide )
83  else if( aTopSide )
85  else if( aBottomSide )
87  else
89 
90  m_formatCSV = aFormatCSV;
91 }

References m_board, m_excludeAllTH, m_formatCSV, m_fpCount, m_side, m_unitsMM, PCB_BACK_SIDE, PCB_BOTH_SIDES, PCB_FRONT_SIDE, and PCB_NO_SIDE.

Member Function Documentation

◆ GenPositionData()

std::string PLACE_FILE_EXPORTER::GenPositionData ( )

build a string filled with the position data

Definition at line 94 of file export_footprints_placefile.cpp.

95 {
96  std::string buffer;
97  char line[1024]; // A line to print intermediate data
98 
99  // Minimal text lengths:
100  m_fpCount = 0;
101  int lenRefText = 8;
102  int lenValText = 8;
103  int lenPkgText = 16;
104 
106 
107  // Calculating the number of useful footprints (CMS attribute, not VIRTUAL)
108  m_fpCount = 0;
109 
110  // Select units:
111  double conv_unit = m_unitsMM ? conv_unit_mm : conv_unit_inch;
112  const char *unit_text = m_unitsMM ? unit_text_mm : unit_text_inch;
113 
114  // Build and sort the list of footprints alphabetically
115  std::vector<LIST_MOD> list;
116 
117  for( FOOTPRINT* footprint : m_board->Footprints() )
118  {
119  if( m_side != PCB_BOTH_SIDES )
120  {
121  if( footprint->GetLayer() == B_Cu && m_side != PCB_BACK_SIDE )
122  continue;
123  if( footprint->GetLayer() == F_Cu && m_side != PCB_FRONT_SIDE )
124  continue;
125  }
126 
127  if( footprint->GetAttributes() & FP_EXCLUDE_FROM_POS_FILES )
128  continue;
129 
130  if( m_excludeAllTH && footprint->HasThroughHolePads() )
131  continue;
132 
133  m_fpCount++;
134 
135  LIST_MOD item;
136  item.m_Footprint = footprint;
137  item.m_Reference = footprint->Reference().GetShownText();
138  item.m_Value = footprint->Value().GetShownText();
139  item.m_Layer = footprint->GetLayer();
140  list.push_back( item );
141 
142  lenRefText = std::max( lenRefText, (int) item.m_Reference.length() );
143  lenValText = std::max( lenValText, (int) item.m_Value.length() );
144  lenPkgText = std::max( lenPkgText, (int) item.m_Footprint->GetFPID().GetLibItemName().length() );
145  }
146 
147  if( list.size() > 1 )
148  sort( list.begin(), list.end(), sortFPlist );
149 
150  // Switch the locale to standard C (needed to print floating point numbers)
151  LOCALE_IO toggle;
152 
153  if( m_formatCSV )
154  {
155  wxChar csv_sep = ',';
156 
157  // Set first line:;
158  sprintf( line, "Ref%cVal%cPackage%cPosX%cPosY%cRot%cSide\n",
159  csv_sep, csv_sep, csv_sep, csv_sep, csv_sep, csv_sep );
160 
161  buffer += line;
162 
163  for( int ii = 0; ii < m_fpCount; ii++ )
164  {
165  wxPoint footprint_pos;
166  footprint_pos = list[ii].m_Footprint->GetPosition();
167  footprint_pos -= m_place_Offset;
168 
169  LAYER_NUM layer = list[ii].m_Footprint->GetLayer();
170  wxASSERT( layer == F_Cu || layer == B_Cu );
171 
172  if( layer == B_Cu )
173  footprint_pos.x = - footprint_pos.x;
174 
175  wxString tmp = "\"" + list[ii].m_Reference;
176  tmp << "\"" << csv_sep;
177  tmp << "\"" << list[ii].m_Value;
178  tmp << "\"" << csv_sep;
179  tmp << "\"" << list[ii].m_Footprint->GetFPID().GetLibItemName().wx_str();
180  tmp << "\"" << csv_sep;
181 
182  tmp << wxString::Format( "%f%c%f%c%f",
183  footprint_pos.x * conv_unit, csv_sep,
184  // Keep the Y axis oriented from bottom to top,
185  // ( change y coordinate sign )
186  -footprint_pos.y * conv_unit, csv_sep,
187  list[ii].m_Footprint->GetOrientation() / 10.0 );
188  tmp << csv_sep;
189 
190  tmp << ( (layer == F_Cu ) ? PLACE_FILE_EXPORTER::GetFrontSideName()
192  tmp << '\n';
193 
194  buffer += TO_UTF8( tmp );
195  }
196  }
197  else
198  {
199  // Write file header
200  sprintf( line, "### Module positions - created on %s ###\n", TO_UTF8( DateAndTime() ) );
201 
202  buffer += line;
203 
204  wxString Title = GetBuildVersion();
205  sprintf( line, "### Printed by Pcbnew version %s\n", TO_UTF8( Title ) );
206  buffer += line;
207 
208  buffer += unit_text;
209  buffer += "## Side : ";
210 
211  if( m_side == PCB_BACK_SIDE )
212  buffer += GetBackSideName().c_str();
213  else if( m_side == PCB_FRONT_SIDE )
214  buffer += GetFrontSideName().c_str();
215  else if( m_side == PCB_BOTH_SIDES )
216  buffer += "All";
217  else
218  buffer += "---";
219 
220  buffer += "\n";
221 
222  sprintf(line, "%-*s %-*s %-*s %9.9s %9.9s %8.8s %s\n",
223  int(lenRefText), "# Ref",
224  int(lenValText), "Val",
225  int(lenPkgText), "Package",
226  "PosX", "PosY", "Rot", "Side" );
227  buffer += line;
228 
229  for( int ii = 0; ii < m_fpCount; ii++ )
230  {
231  wxPoint footprint_pos;
232  footprint_pos = list[ii].m_Footprint->GetPosition();
233  footprint_pos -= m_place_Offset;
234 
235  LAYER_NUM layer = list[ii].m_Footprint->GetLayer();
236  wxASSERT( layer == F_Cu || layer == B_Cu );
237 
238  if( layer == B_Cu )
239  footprint_pos.x = - footprint_pos.x;
240 
241  wxString ref = list[ii].m_Reference;
242  wxString val = list[ii].m_Value;
243  wxString pkg = list[ii].m_Footprint->GetFPID().GetLibItemName();
244  ref.Replace( wxT( " " ), wxT( "_" ) );
245  val.Replace( wxT( " " ), wxT( "_" ) );
246  pkg.Replace( wxT( " " ), wxT( "_" ) );
247  sprintf(line, "%-*s %-*s %-*s %9.4f %9.4f %8.4f %s\n",
248  lenRefText, TO_UTF8( ref ),
249  lenValText, TO_UTF8( val ),
250  lenPkgText, TO_UTF8( pkg ),
251  footprint_pos.x * conv_unit,
252  // Keep the coordinates in the first quadrant,
253  // (i.e. change y sign
254  -footprint_pos.y * conv_unit,
255  list[ii].m_Footprint->GetOrientation() / 10.0,
256  (layer == F_Cu ) ? GetFrontSideName().c_str() : GetBackSideName().c_str() );
257  buffer += line;
258  }
259 
260  // Write EOF
261  buffer += "## End\n";
262  }
263 
264  return buffer;
265 }
const UTF8 & GetLibItemName() const
Definition: lib_id.h:106
static const double conv_unit_mm
static const char unit_text_mm[]
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.h:593
static const double conv_unit_inch
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
wxString GetBuildVersion()
Get the full KiCad version string.
static const char unit_text_inch[]
std::string::size_type length() const
Definition: utf8.h:109
FOOTPRINTS & Footprints()
Definition: board.h:305
const LIB_ID & GetFPID() const
Definition: footprint.h:190
The ASCII format of the kicad place file is:
int LAYER_NUM
This can be replaced with int and removed.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
static bool sortFPlist(const LIST_MOD &ref, const LIST_MOD &tst)
static std::string GetBackSideName()
static std::string GetFrontSideName()
wxPoint m_AuxOrigin
origin for plot exports
wxString DateAndTime()
Definition: string.cpp:431

References B_Cu, conv_unit_inch, conv_unit_mm, DateAndTime(), F_Cu, BOARD::Footprints(), Format(), FP_EXCLUDE_FROM_POS_FILES, GetBackSideName(), GetBuildVersion(), BOARD::GetDesignSettings(), FOOTPRINT::GetFPID(), GetFrontSideName(), LIB_ID::GetLibItemName(), UTF8::length(), BOARD_DESIGN_SETTINGS::m_AuxOrigin, m_board, m_excludeAllTH, LIST_MOD::m_Footprint, m_formatCSV, m_fpCount, LIST_MOD::m_Layer, m_place_Offset, LIST_MOD::m_Reference, m_side, m_unitsMM, LIST_MOD::m_Value, PCB_BACK_SIDE, PCB_BOTH_SIDES, PCB_FRONT_SIDE, sortFPlist(), TO_UTF8, unit_text_inch, and unit_text_mm.

Referenced by DIALOG_GEN_FOOTPRINT_POSITION::CreateAsciiFiles(), and PCB_EDIT_FRAME::DoGenFootprintsPositionFile().

◆ GenReportData()

std::string PLACE_FILE_EXPORTER::GenReportData ( )

build a string filled with the pad report data This report does not used options aForceSmdItems,aTopSide, aBottomSide and aFormatCSV.

All footprints and their pads on board are reported.

Definition at line 268 of file export_footprints_placefile.cpp.

269 {
270  std::string buffer;
271 
272  m_place_Offset = wxPoint( 0, 0 );
273 
274  // Select units:
275  double conv_unit = m_unitsMM ? conv_unit_mm : conv_unit_inch;
276  const char *unit_text = m_unitsMM ? unit_text_mm : unit_text_inch;
277 
278  LOCALE_IO toggle;
279 
280  // Generate header file comments.)
281  char line[1024];
282  sprintf( line, "## Footprint report - date %s\n", TO_UTF8( DateAndTime() ) );
283  buffer += line;
284 
285  wxString Title = GetBuildVersion();
286  sprintf( line, "## Created by Pcbnew version %s\n", TO_UTF8( Title ) );
287  buffer += line;
288 
289  buffer += unit_text;
290 
291  buffer += "\n$BeginDESCRIPTION\n";
292 
294 
295  buffer += "\n$BOARD\n";
296 
297  sprintf( line, "upper_left_corner %9.6f %9.6f\n",
298  bbbox.GetX() * conv_unit, bbbox.GetY() * conv_unit );
299  buffer += line;
300 
301  sprintf( line, "lower_right_corner %9.6f %9.6f\n",
302  bbbox.GetRight() * conv_unit, bbbox.GetBottom() * conv_unit );
303  buffer += line;
304 
305  buffer += "$EndBOARD\n\n";
306 
307  std::vector<FOOTPRINT*> sortedFootprints;
308 
309  for( FOOTPRINT* footprint : m_board->Footprints() )
310  sortedFootprints.push_back( footprint );
311 
312  std::sort( sortedFootprints.begin(), sortedFootprints.end(),
313  []( FOOTPRINT* a, FOOTPRINT* b ) -> bool
314  {
315  return StrNumCmp( a->GetReference(), b->GetReference(), true ) < 0;
316  });
317 
318  for( FOOTPRINT* footprint : sortedFootprints )
319  {
320  wxString ref = footprint->Reference().GetShownText();
321 
322  sprintf( line, "$MODULE %s\n", TO_UTF8( ref ) );
323  buffer += line;
324 
325  sprintf( line, "reference %s\n", TO_UTF8( ref ) );
326  sprintf( line, "value %s\n", EscapedUTF8( footprint->Value().GetShownText() ).c_str() );
327  sprintf( line, "footprint %s\n", footprint->GetFPID().Format().c_str() );
328  buffer += line;
329 
330  buffer += "attribut";
331 
332  if(( footprint->GetAttributes() & ( FP_THROUGH_HOLE | FP_SMD ) ) == 0 )
333  buffer += " virtual";
334 
335  if( footprint->GetAttributes() & FP_SMD )
336  buffer += " smd";
337 
338  if( footprint->GetAttributes() & FP_THROUGH_HOLE )
339  buffer += " none";
340 
341  buffer += "\n";
342 
343  wxPoint footprint_pos = footprint->GetPosition();
344  footprint_pos -= m_place_Offset;
345 
346  sprintf( line, "position %9.6f %9.6f orientation %.2f\n",
347  footprint_pos.x * conv_unit,
348  footprint_pos.y * conv_unit,
349  footprint->GetOrientation() / 10.0 );
350  buffer += line;
351 
352  if( footprint->GetLayer() == F_Cu )
353  buffer += "layer front\n";
354  else if( footprint->GetLayer() == B_Cu )
355  buffer += "layer back\n";
356  else
357  buffer += "layer other\n";
358 
359  std::vector<PAD*> sortedPads;
360 
361  for( PAD* pad : footprint->Pads() )
362  sortedPads.push_back( pad );
363 
364  std::sort( sortedPads.begin(), sortedPads.end(),
365  []( PAD* a, PAD* b ) -> bool
366  {
367  return StrNumCmp( a->GetName(), b->GetName(), true ) < 0;
368  });
369 
370  for( PAD* pad : sortedPads )
371  {
372  sprintf( line, "$PAD \"%s\"\n", TO_UTF8( pad->GetName() ) );
373  buffer += line;
374 
375  int layer = 0;
376 
377  if( pad->GetLayerSet()[B_Cu] )
378  layer = 1;
379 
380  if( pad->GetLayerSet()[F_Cu] )
381  layer |= 2;
382 
383  static const char* layer_name[4] = { "nocopper", "back", "front", "both" };
384  sprintf( line, "Shape %s Layer %s\n",
385  TO_UTF8( pad->ShowPadShape() ),
386  layer_name[layer] );
387  buffer += line;
388 
389  sprintf( line, "position %9.6f %9.6f size %9.6f %9.6f orientation %.2f\n",
390  pad->GetPos0().x * conv_unit,
391  pad->GetPos0().y * conv_unit,
392  pad->GetSize().x * conv_unit,
393  pad->GetSize().y * conv_unit,
394  ( pad->GetOrientation() - footprint->GetOrientation()) / 10.0 );
395  buffer += line;
396 
397  sprintf( line, "drill %9.6f\n", pad->GetDrillSize().x * conv_unit );
398  buffer += line;
399 
400  sprintf( line, "shape_offset %9.6f %9.6f\n",
401  pad->GetOffset().x * conv_unit,
402  pad->GetOffset().y * conv_unit );
403  buffer += line;
404 
405  buffer += "$EndPAD\n";
406  }
407 
408  sprintf( line, "$EndMODULE %s\n\n", TO_UTF8( ref ) );
409  buffer += line;
410  }
411 
412  // Generate EOF.
413  buffer += "$EndDESCRIPTION\n";
414 
415  return buffer;
416 }
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
Definition: string.cpp:440
static const double conv_unit_mm
static const char unit_text_mm[]
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
int GetX() const
Definition: eda_rect.h:103
int GetBottom() const
Definition: eda_rect.h:119
static const double conv_unit_inch
std::string EscapedUTF8(wxString aString)
Return an 8 bit UTF8 string given aString in Unicode form.
Definition: string.cpp:329
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
wxString GetBuildVersion()
Get the full KiCad version string.
static const char unit_text_inch[]
FOOTPRINTS & Footprints()
Definition: board.h:305
int GetRight() const
Definition: eda_rect.h:116
const wxString & GetName() const
Definition: pad.h:133
const wxString & GetReference() const
Definition: footprint.h:426
Handle the component boundary box.
Definition: eda_rect.h:42
int GetY() const
Definition: eda_rect.h:104
EDA_RECT ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Calculate the bounding box containing all board items (or board edge segments).
Definition: board.cpp:1050
Definition: pad.h:60
wxString DateAndTime()
Definition: string.cpp:431

References B_Cu, BOARD::ComputeBoundingBox(), conv_unit_inch, conv_unit_mm, DateAndTime(), EscapedUTF8(), F_Cu, BOARD::Footprints(), FP_SMD, FP_THROUGH_HOLE, EDA_RECT::GetBottom(), GetBuildVersion(), PAD::GetName(), FOOTPRINT::GetReference(), EDA_RECT::GetRight(), EDA_RECT::GetX(), EDA_RECT::GetY(), m_board, m_place_Offset, m_unitsMM, pad, StrNumCmp(), TO_UTF8, unit_text_inch, and unit_text_mm.

Referenced by PCB_EDIT_FRAME::DoGenFootprintsReport().

◆ GetBackSideName()

static std::string PLACE_FILE_EXPORTER::GetBackSideName ( )
inlinestatic

Definition at line 88 of file export_footprints_placefile.h.

88 { return std::string( "bottom" ); }

Referenced by DIALOG_GEN_FOOTPRINT_POSITION::CreateAsciiFiles(), and GenPositionData().

◆ GetFootprintCount()

int PLACE_FILE_EXPORTER::GetFootprintCount ( )
inline
Returns
the footprint count found on board by GenPositionData() must be called only after GenPositionData() is run

Definition at line 83 of file export_footprints_placefile.h.

References m_fpCount.

Referenced by DIALOG_GEN_FOOTPRINT_POSITION::CreateAsciiFiles(), and PCB_EDIT_FRAME::DoGenFootprintsPositionFile().

◆ GetFrontSideName()

static std::string PLACE_FILE_EXPORTER::GetFrontSideName ( )
inlinestatic

Definition at line 87 of file export_footprints_placefile.h.

87 { return std::string( "top" ); }

Referenced by DIALOG_GEN_FOOTPRINT_POSITION::CreateAsciiFiles(), and GenPositionData().

Member Data Documentation

◆ m_board

BOARD* PLACE_FILE_EXPORTER::m_board
private

◆ m_excludeAllTH

bool PLACE_FILE_EXPORTER::m_excludeAllTH
private

Definition at line 93 of file export_footprints_placefile.h.

Referenced by GenPositionData(), and PLACE_FILE_EXPORTER().

◆ m_formatCSV

bool PLACE_FILE_EXPORTER::m_formatCSV
private

Definition at line 95 of file export_footprints_placefile.h.

Referenced by GenPositionData(), and PLACE_FILE_EXPORTER().

◆ m_fpCount

int PLACE_FILE_EXPORTER::m_fpCount
private

◆ m_place_Offset

wxPoint PLACE_FILE_EXPORTER::m_place_Offset
private

Definition at line 97 of file export_footprints_placefile.h.

Referenced by GenPositionData(), and GenReportData().

◆ m_side

int PLACE_FILE_EXPORTER::m_side
private

Definition at line 94 of file export_footprints_placefile.h.

Referenced by GenPositionData(), and PLACE_FILE_EXPORTER().

◆ m_unitsMM

bool PLACE_FILE_EXPORTER::m_unitsMM
private

The documentation for this class was generated from the following files: