KiCad PCB EDA Suite
Loading...
Searching...
No Matches
place_file_exporter.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24/*
25 * 1 - create ascii/csv files for automatic placement of smd components
26 * 2 - create a footprint report (pos and footprint descr) (ascii file)
27 */
28
29#include <string_utils.h>
30#include <macros.h>
31#include <locale_io.h>
33#include <build_version.h>
35#include <pad.h>
36
37#include <fmt/format.h>
38
39#include <wx/dirdlg.h>
40
41class LIST_MOD // An helper class used to build a list of useful footprints.
42{
43public:
44 FOOTPRINT* m_Footprint; // Link to the actual footprint
45 wxString m_Reference; // Its schematic reference
46 wxString m_Value; // Its schematic value
47 int m_Layer; // its side (B_Cu, or F_Cu)
48};
49
50
51// Defined values to write coordinates using inches or mm:
52static const double conv_unit_inch = 0.001 / pcbIUScale.IU_PER_MILS ; // units = in
53static const char unit_text_inch[] = "## Unit = inches, Angle = deg.\n";
54
55static const double conv_unit_mm = 1.0 / pcbIUScale.IU_PER_MM; // units = mm
56static const char unit_text_mm[] = "## Unit = mm, Angle = deg.\n";
57
58// Sort function use by GenerefootprintsPosition()
59// sort is made by side (layer) top layer first
60// then by reference increasing order
61static bool sortFPlist( const LIST_MOD& ref, const LIST_MOD& tst )
62{
63 if( ref.m_Layer == tst.m_Layer )
64 return StrNumCmp( ref.m_Reference, tst.m_Reference ) < 0;
65
66 return ref.m_Layer > tst.m_Layer;
67}
68
69
77
78PLACE_FILE_EXPORTER::PLACE_FILE_EXPORTER( BOARD* aBoard, bool aUnitsMM, bool aOnlySMD,
79 bool aExcludeAllTH, bool aExcludeDNP, bool aExcludeBOM,
80 bool aTopSide, bool aBottomSide, bool aFormatCSV,
81 bool aUseAuxOrigin, bool aNegateBottomX )
82{
83 m_board = aBoard;
84 m_unitsMM = aUnitsMM;
85 m_onlySMD = aOnlySMD;
86 m_excludeAllTH = aExcludeAllTH;
87 m_excludeDNP = aExcludeDNP;
88 m_excludeBOM = aExcludeBOM;
89 m_fpCount = 0;
90 m_negateBottomX = aNegateBottomX;
91
92 if( aTopSide && aBottomSide )
94 else if( aTopSide )
96 else if( aBottomSide )
98 else
100
101 m_formatCSV = aFormatCSV;
102
103 if( aUseAuxOrigin )
104 m_place_Offset = m_board->GetDesignSettings().GetAuxOrigin();
105 else
106 m_place_Offset = VECTOR2I( 0, 0 );
107}
108
109
111{
112 std::string buffer;
113 char line[1024]; // A line to print intermediate data
114
115 // Minimal text lengths:
116 m_fpCount = 0;
117 int lenRefText = 8;
118 int lenValText = 8;
119 int lenPkgText = 16;
120
121 // Calculating the number of useful footprints (CMS attribute, not VIRTUAL)
122 m_fpCount = 0;
123
124 // Select units:
125 double conv_unit = m_unitsMM ? conv_unit_mm : conv_unit_inch;
126 const char *unit_text = m_unitsMM ? unit_text_mm : unit_text_inch;
127
128 // Build and sort the list of footprints alphabetically
129 std::vector<LIST_MOD> list;
130
131 for( FOOTPRINT* footprint : m_board->Footprints() )
132 {
133 if( m_side != PCB_BOTH_SIDES )
134 {
135 if( footprint->GetLayer() == B_Cu && m_side != PCB_BACK_SIDE )
136 continue;
137 if( footprint->GetLayer() == F_Cu && m_side != PCB_FRONT_SIDE )
138 continue;
139 }
140
141 if( footprint->GetAttributes() & FP_EXCLUDE_FROM_POS_FILES )
142 continue;
143
144 if( m_onlySMD && !( footprint->GetAttributes() & FP_SMD ) )
145 continue;
146
147 if( m_excludeAllTH && footprint->HasThroughHolePads() )
148 continue;
149
150 if( m_excludeDNP && ( footprint->GetAttributes() & FP_DNP ) )
151 continue;
152
153 if( m_excludeBOM && ( footprint->GetAttributes() & FP_EXCLUDE_FROM_BOM ) )
154 continue;
155
156 m_fpCount++;
157
158 LIST_MOD item;
159 item.m_Footprint = footprint;
160 item.m_Reference = footprint->Reference().GetShownText( false );
161 item.m_Value = footprint->Value().GetShownText( false );
162 item.m_Layer = footprint->GetLayer();
163 list.push_back( item );
164
165 lenRefText = std::max( lenRefText, (int) item.m_Reference.length() );
166 lenValText = std::max( lenValText, (int) item.m_Value.length() );
167 lenPkgText = std::max( lenPkgText, (int) item.m_Footprint->GetFPID().GetLibItemName().length() );
168 }
169
170 if( list.size() > 1 )
171 sort( list.begin(), list.end(), sortFPlist );
172
173 // Switch the locale to standard C (needed to print floating point numbers)
174 LOCALE_IO toggle;
175
176 if( m_formatCSV )
177 {
178 wxChar csv_sep = ',';
179
180 // Set first line:;
181 snprintf( line, sizeof(line), "Ref%cVal%cPackage%cPosX%cPosY%cRot%cSide\n",
182 csv_sep, csv_sep, csv_sep, csv_sep, csv_sep, csv_sep );
183
184 buffer += line;
185
186 for( int ii = 0; ii < m_fpCount; ii++ )
187 {
188 VECTOR2I footprint_pos;
189 footprint_pos = list[ii].m_Footprint->GetPosition();
190 footprint_pos -= m_place_Offset;
191
192 int layer = list[ii].m_Footprint->GetLayer();
193 wxASSERT( IsExternalCopperLayer( layer ) );
194
195 if( layer == B_Cu && m_negateBottomX )
196 footprint_pos.x = - footprint_pos.x;
197
198 wxString tmp = wxT( "\"" ) + list[ii].m_Reference;
199 tmp << wxT( "\"" ) << csv_sep;
200 tmp << wxT( "\"" ) << list[ii].m_Value;
201 tmp << wxT( "\"" ) << csv_sep;
202 tmp << wxT( "\"" ) << list[ii].m_Footprint->GetFPID().GetLibItemName().wx_str();
203 tmp << wxT( "\"" ) << csv_sep;
204
205 tmp << wxString::Format( wxT( "%f%c%f%c%f" ),
206 footprint_pos.x * conv_unit,
207 csv_sep,
208 // Keep the Y axis oriented from bottom to top,
209 // ( change y coordinate sign )
210 -footprint_pos.y * conv_unit,
211 csv_sep,
212 list[ii].m_Footprint->GetOrientation().AsDegrees() );
213 tmp << csv_sep;
214
215 tmp << ( (layer == F_Cu ) ? PLACE_FILE_EXPORTER::GetFrontSideName()
217 tmp << '\n';
218
219 buffer += TO_UTF8( tmp );
220 }
221 }
222 else
223 {
224 // Write file header
225 snprintf( line, sizeof(line), "### Footprint positions - created on %s ###\n",
227
228 buffer += line;
229
230 wxString Title = GetBuildVersion();
231 snprintf( line, sizeof(line), "### Printed by KiCad version %s\n", TO_UTF8( Title ) );
232 buffer += line;
233
234 buffer += unit_text;
235 buffer += "## Side : ";
236
237 if( m_side == PCB_BACK_SIDE )
238 buffer += GetBackSideName().c_str();
239 else if( m_side == PCB_FRONT_SIDE )
240 buffer += GetFrontSideName().c_str();
241 else if( m_side == PCB_BOTH_SIDES )
242 buffer += "All";
243 else
244 buffer += "---";
245
246 buffer += "\n";
247
248 snprintf( line, sizeof(line), "%-*s %-*s %-*s %9.9s %9.9s %8.8s %s\n",
249 int(lenRefText), "# Ref",
250 int(lenValText), "Val",
251 int(lenPkgText), "Package",
252 "PosX", "PosY", "Rot", "Side" );
253 buffer += line;
254
255 for( int ii = 0; ii < m_fpCount; ii++ )
256 {
257 VECTOR2I footprint_pos;
258 footprint_pos = list[ii].m_Footprint->GetPosition();
259 footprint_pos -= m_place_Offset;
260
261 int layer = list[ii].m_Footprint->GetLayer();
262 wxASSERT( IsExternalCopperLayer( layer ) );
263
264 if( layer == B_Cu && m_negateBottomX )
265 footprint_pos.x = - footprint_pos.x;
266
267 wxString ref = list[ii].m_Reference;
268 wxString val = list[ii].m_Value;
269 wxString pkg = list[ii].m_Footprint->GetFPID().GetLibItemName();
270 ref.Replace( wxT( " " ), wxT( "_" ) );
271 val.Replace( wxT( " " ), wxT( "_" ) );
272 pkg.Replace( wxT( " " ), wxT( "_" ) );
273 snprintf( line, sizeof(line), "%-*s %-*s %-*s %9.4f %9.4f %8.4f %s\n",
274 lenRefText, TO_UTF8( ref ),
275 lenValText, TO_UTF8( val ),
276 lenPkgText, TO_UTF8( pkg ),
277 footprint_pos.x * conv_unit,
278 // Keep the coordinates in the first quadrant,
279 // (i.e. change y sign
280 -footprint_pos.y * conv_unit,
281 list[ii].m_Footprint->GetOrientation().AsDegrees(),
282 (layer == F_Cu ) ? GetFrontSideName().c_str() : GetBackSideName().c_str() );
283 buffer += line;
284 }
285
286 // Write EOF
287 buffer += "## End\n";
288 }
289
290 return buffer;
291}
292
293
295{
296 std::string buffer;
297
298 m_place_Offset = VECTOR2I( 0, 0 );
299
300 // Select units:
301 double conv_unit = m_unitsMM ? conv_unit_mm : conv_unit_inch;
302 const char *unit_text = m_unitsMM ? unit_text_mm : unit_text_inch;
303
304 LOCALE_IO toggle;
305
306 // Generate header file comments.)
307
308 buffer += fmt::format( "## Footprint report - date {}\n", TO_UTF8( GetISO8601CurrentDateTime() ) );
309
310 wxString Title = GetBuildVersion();
311 buffer += fmt::format( "## Printed by KiCad version {}\n", TO_UTF8( Title ) );
312
313 buffer += unit_text;
314
315 buffer += "\n$BeginDESCRIPTION\n";
316
317 BOX2I bbbox = m_board->ComputeBoundingBox( false );
318
319 buffer += "\n$BOARD\n";
320
321 buffer += fmt::format( "upper_left_corner {:9.6f} {:9.6f}\n",
322 bbbox.GetX() * conv_unit,
323 bbbox.GetY() * conv_unit );
324
325 buffer += "$EndBOARD\n\n";
326
327 std::vector<FOOTPRINT*> sortedFootprints;
328
329 for( FOOTPRINT* footprint : m_board->Footprints() )
330 sortedFootprints.push_back( footprint );
331
332 std::sort( sortedFootprints.begin(), sortedFootprints.end(),
333 []( FOOTPRINT* a, FOOTPRINT* b ) -> bool
334 {
335 return StrNumCmp( a->GetReference(), b->GetReference(), true ) < 0;
336 });
337
338 for( FOOTPRINT* footprint : sortedFootprints )
339 {
340 wxString ref = footprint->Reference().GetShownText( false );
341 wxString value = footprint->Value().GetShownText( false );
342
343 buffer += fmt::format( "$MODULE {}\n", TO_UTF8( ref ) );
344
345 buffer += fmt::format( "reference {}\n", TO_UTF8( ref ) );
346 buffer += fmt::format( "value {}\n", TO_UTF8( value ) );
347 buffer += fmt::format( "footprint {}\n", footprint->GetFPID().Format().c_str() );
348
349 buffer += "attribut";
350
351 if(( footprint->GetAttributes() & ( FP_THROUGH_HOLE | FP_SMD ) ) == 0 )
352 buffer += " virtual";
353
354 if( footprint->GetAttributes() & FP_SMD )
355 buffer += " smd";
356
357 if( footprint->GetAttributes() & FP_THROUGH_HOLE )
358 buffer += " none";
359
360 buffer += "\n";
361
362 VECTOR2I footprint_pos = footprint->GetPosition();
363 footprint_pos -= m_place_Offset;
364
365 buffer += fmt::format( "position {:9.6f} {:9.6f} orientation {:.2f}\n",
366 footprint_pos.x * conv_unit,
367 footprint_pos.y * conv_unit,
368 footprint->GetOrientation().AsDegrees() );
369
370 if( footprint->GetLayer() == F_Cu )
371 buffer += "layer front\n";
372 else if( footprint->GetLayer() == B_Cu )
373 buffer += "layer back\n";
374 else
375 buffer += "layer other\n";
376
377 std::vector<PAD*> sortedPads;
378
379 for( PAD* pad : footprint->Pads() )
380 sortedPads.push_back( pad );
381
382 std::sort( sortedPads.begin(), sortedPads.end(),
383 []( PAD* a, PAD* b ) -> bool
384 {
385 return StrNumCmp( a->GetNumber(), b->GetNumber(), true ) < 0;
386 });
387
388 for( PAD* pad : sortedPads )
389 {
390 buffer += fmt::format( "$PAD \"{}\"\n", TO_UTF8( pad->GetNumber() ) );
391
392 int layer = 0;
393
394 if( pad->GetLayerSet()[B_Cu] )
395 layer = 1;
396
397 if( pad->GetLayerSet()[F_Cu] )
398 layer |= 2;
399
400 // TODO(JE) padstacks
401 static const char* layer_name[4] = { "nocopper", "back", "front", "both" };
402 buffer += fmt::format( "Shape {} Layer {}\n",
403 TO_UTF8( pad->ShowPadShape( PADSTACK::ALL_LAYERS ) ),
404 layer_name[layer] );
405
406 VECTOR2I padPos = pad->GetFPRelativePosition();
407
408 buffer += fmt::format( "position {:9.6f} {:9.6f} size {:9.6f} {:9.6f} orientation {:.2f}\n",
409 padPos.x * conv_unit,
410 padPos.y * conv_unit,
411 pad->GetSize( PADSTACK::ALL_LAYERS ).x * conv_unit,
412 pad->GetSize( PADSTACK::ALL_LAYERS ).y * conv_unit,
413 pad->GetOrientation().AsDegrees() );
414
415 buffer += fmt::format( "drill {:9.6f}\n", pad->GetDrillSize().x * conv_unit );
416
417 buffer += fmt::format( "shape_offset {:9.6f} {:9.6f}\n",
418 pad->GetOffset( PADSTACK::ALL_LAYERS ).x * conv_unit,
419 pad->GetOffset( PADSTACK::ALL_LAYERS ).y * conv_unit );
420
421 buffer += "$EndPAD\n";
422 }
423
424 buffer += fmt::format( "$EndMODULE {}\n\n", TO_UTF8( ref ) );
425 }
426
427 // Generate EOF.
428 buffer += "$EndDESCRIPTION\n";
429
430 return buffer;
431}
432
433
434wxString PLACE_FILE_EXPORTER::DecorateFilename( const wxString& aBaseName, bool aFront, bool aBack )
435{
436 if( aFront && aBack )
437 return aBaseName + wxT( "-" ) + wxT( "all" );
438 else if( aFront )
439 return aBaseName + wxT( "-" ) + GetFrontSideName();
440 else if( aBack )
441 return aBaseName + wxT( "-" ) + GetBackSideName();
442 else
443 return aBaseName;
444}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
wxString GetBuildVersion()
Get the full KiCad version string.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
constexpr coord_type GetY() const
Definition box2.h:208
constexpr coord_type GetX() const
Definition box2.h:207
const LIB_ID & GetFPID() const
Definition footprint.h:269
const UTF8 & GetLibItemName() const
Definition lib_id.h:102
FOOTPRINT * m_Footprint
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:41
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition padstack.h:145
Definition pad.h:54
PLACE_FILE_EXPORTER(BOARD *aBoard, bool aUnitsMM, bool aOnlySMD, bool aExcludeAllTH, bool aExcludeDNP, bool aExcludeBOM, bool aTopSide, bool aBottomSide, bool aFormatCSV, bool aUseAuxOrigin, bool aNegateBottomX)
Create a PLACE_FILE_EXPORTER.
static std::string GetFrontSideName()
static wxString DecorateFilename(const wxString &aBaseName, bool aFront, bool aBack)
std::string GenPositionData()
build a string filled with the position data
static std::string GetBackSideName()
std::string GenReportData()
build a string filled with the pad report data This report does not used options aForceSmdItems,...
std::string::size_type length() const
Definition utf8.h:116
@ FP_SMD
Definition footprint.h:82
@ FP_DNP
Definition footprint.h:87
@ FP_EXCLUDE_FROM_POS_FILES
Definition footprint.h:83
@ FP_EXCLUDE_FROM_BOM
Definition footprint.h:84
@ FP_THROUGH_HOLE
Definition footprint.h:81
bool IsExternalCopperLayer(int aLayerId)
Test whether a layer is an external (F_Cu or B_Cu) copper layer.
Definition layer_ids.h:687
@ B_Cu
Definition layer_ids.h:65
@ F_Cu
Definition layer_ids.h:64
This file contains miscellaneous commonly used macros and functions.
static const double conv_unit_mm
static bool sortFPlist(const LIST_MOD &ref, const LIST_MOD &tst)
static const double conv_unit_inch
static const char unit_text_mm[]
@ PCB_BACK_SIDE
@ PCB_BOTH_SIDES
@ PCB_FRONT_SIDE
static const char unit_text_inch[]
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
wxString GetISO8601CurrentDateTime()
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695