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 wxString wxLine; // wxString used for UTF-8 line
115
116 // Minimal text lengths:
117 m_fpCount = 0;
118 int lenRefText = 8;
119 int lenValText = 8;
120 int lenPkgText = 16;
121
122 // Calculating the number of useful footprints (CMS attribute, not VIRTUAL)
123 m_fpCount = 0;
124
125 // Select units:
126 double conv_unit = m_unitsMM ? conv_unit_mm : conv_unit_inch;
127 const char *unit_text = m_unitsMM ? unit_text_mm : unit_text_inch;
128
129 // Build and sort the list of footprints alphabetically
130 std::vector<LIST_MOD> list;
131
132 for( FOOTPRINT* footprint : m_board->Footprints() )
133 {
134 if( m_side != PCB_BOTH_SIDES )
135 {
136 if( footprint->GetLayer() == B_Cu && m_side != PCB_BACK_SIDE )
137 continue;
138 if( footprint->GetLayer() == F_Cu && m_side != PCB_FRONT_SIDE )
139 continue;
140 }
141
142 if( footprint->GetAttributes() & FP_EXCLUDE_FROM_POS_FILES )
143 continue;
144
145 if( m_onlySMD && !( footprint->GetAttributes() & FP_SMD ) )
146 continue;
147
148 if( m_excludeAllTH && footprint->HasThroughHolePads() )
149 continue;
150
151 if( m_excludeDNP && ( footprint->GetAttributes() & FP_DNP ) )
152 continue;
153
154 if( m_excludeBOM && ( footprint->GetAttributes() & FP_EXCLUDE_FROM_BOM ) )
155 continue;
156
157 m_fpCount++;
158
159 LIST_MOD item;
160 item.m_Footprint = footprint;
161 item.m_Reference = footprint->Reference().GetShownText( false );
162 item.m_Value = footprint->Value().GetShownText( false );
163 item.m_Layer = footprint->GetLayer();
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 list.push_back( std::move( item ) );
170 }
171
172 if( list.size() > 1 )
173 sort( list.begin(), list.end(), sortFPlist );
174
175 // Switch the locale to standard C (needed to print floating point numbers)
176 LOCALE_IO toggle;
177
178 if( m_formatCSV )
179 {
180 wxChar csv_sep = ',';
181
182 // Set first line:;
183 snprintf( line, sizeof(line), "Ref%cVal%cPackage%cPosX%cPosY%cRot%cSide\n",
184 csv_sep, csv_sep, csv_sep, csv_sep, csv_sep, csv_sep );
185
186 buffer += line;
187
188 for( int ii = 0; ii < m_fpCount; ii++ )
189 {
190 VECTOR2I footprint_pos;
191 footprint_pos = list[ii].m_Footprint->GetPosition();
192 footprint_pos -= m_place_Offset;
193
194 int layer = list[ii].m_Footprint->GetLayer();
195 wxASSERT( IsExternalCopperLayer( layer ) );
196
197 if( layer == B_Cu && m_negateBottomX )
198 footprint_pos.x = - footprint_pos.x;
199
200 wxLine = wxT( "\"" ) + list[ii].m_Reference;
201 wxLine << wxT( "\"" ) << csv_sep;
202 wxLine << wxT( "\"" ) << list[ii].m_Value;
203 wxLine << wxT( "\"" ) << csv_sep;
204 wxLine << wxT( "\"" ) << list[ii].m_Footprint->GetFPID().GetLibItemName().wx_str();
205 wxLine << wxT( "\"" ) << csv_sep;
206
207 wxLine << wxString::Format( wxT( "%f%c%f%c%f" ),
208 footprint_pos.x * conv_unit,
209 csv_sep,
210 // Keep the Y axis oriented from bottom to top,
211 // ( change y coordinate sign )
212 -footprint_pos.y * conv_unit,
213 csv_sep,
214 list[ii].m_Footprint->GetOrientation().AsDegrees() );
215 wxLine << csv_sep;
216
217 wxLine << ( (layer == F_Cu ) ? PLACE_FILE_EXPORTER::GetFrontSideName()
219 wxLine << '\n';
220
221 buffer += TO_UTF8( wxLine );
222 }
223 }
224 else
225 {
226 // Write file header
227 snprintf( line, sizeof(line), "### Footprint positions - created on %s ###\n",
229
230 buffer += line;
231
232 wxString Title = GetBuildVersion();
233 snprintf( line, sizeof(line), "### Printed by KiCad version %s\n", TO_UTF8( Title ) );
234 buffer += line;
235
236 buffer += unit_text;
237 buffer += "## Side : ";
238
239 if( m_side == PCB_BACK_SIDE )
240 buffer += GetBackSideName();
241 else if( m_side == PCB_FRONT_SIDE )
242 buffer += GetFrontSideName();
243 else if( m_side == PCB_BOTH_SIDES )
244 buffer += "All";
245 else
246 buffer += "---";
247
248 buffer += "\n";
249
250 snprintf( line, sizeof(line), "%-*s %-*s %-*s %9.9s %9.9s %8.8s %s\n",
251 lenRefText, "# Ref",
252 lenValText, "Val",
253 lenPkgText, "Package",
254 "PosX", "PosY", "Rot", "Side" );
255 buffer += line;
256
257 for( int ii = 0; ii < m_fpCount; ii++ )
258 {
259 VECTOR2I footprint_pos;
260 footprint_pos = list[ii].m_Footprint->GetPosition();
261 footprint_pos -= m_place_Offset;
262
263 int layer = list[ii].m_Footprint->GetLayer();
264 wxASSERT( IsExternalCopperLayer( layer ) );
265
266 if( layer == B_Cu && m_negateBottomX )
267 footprint_pos.x = - footprint_pos.x;
268
269 wxString ref = list[ii].m_Reference;
270 wxString val = list[ii].m_Value;
271 wxString pkg = list[ii].m_Footprint->GetFPID().GetLibItemName();
272 ref.Replace( wxT( " " ), wxT( "_" ) );
273 val.Replace( wxT( " " ), wxT( "_" ) );
274 pkg.Replace( wxT( " " ), wxT( "_" ) );
275 wxLine.Printf( wxT( "%-*s %-*s %-*s %9.4f %9.4f %8.4f %s\n" ),
276 lenRefText, std::move( ref ),
277 lenValText, std::move( val ),
278 lenPkgText, std::move( pkg ),
279 footprint_pos.x * conv_unit,
280 // Keep the coordinates in the first quadrant, (i.e. change y sign)
281 -footprint_pos.y * conv_unit,
282 list[ii].m_Footprint->GetOrientation().AsDegrees(),
283 ( layer == F_Cu ) ? GetFrontSideName() : GetBackSideName() );
284 buffer += TO_UTF8( wxLine );
285 }
286
287 // Write EOF
288 buffer += "## End\n";
289 }
290
291 return buffer;
292}
293
294
296{
297 std::string buffer;
298
299 m_place_Offset = VECTOR2I( 0, 0 );
300
301 // Select units:
302 double conv_unit = m_unitsMM ? conv_unit_mm : conv_unit_inch;
303 const char *unit_text = m_unitsMM ? unit_text_mm : unit_text_inch;
304
305 LOCALE_IO toggle;
306
307 // Generate header file comments.)
308
309 buffer += fmt::format( "## Footprint report - date {}\n", TO_UTF8( GetISO8601CurrentDateTime() ) );
310
311 wxString Title = GetBuildVersion();
312 buffer += fmt::format( "## Printed by KiCad version {}\n", TO_UTF8( Title ) );
313
314 buffer += unit_text;
315
316 buffer += "\n$BeginDESCRIPTION\n";
317
318 BOX2I bbbox = m_board->ComputeBoundingBox( false );
319
320 buffer += "\n$BOARD\n";
321
322 buffer += fmt::format( "upper_left_corner {:9.6f} {:9.6f}\n",
323 bbbox.GetX() * conv_unit,
324 bbbox.GetY() * conv_unit );
325
326 buffer += "$EndBOARD\n\n";
327
328 std::vector<FOOTPRINT*> sortedFootprints;
329
330 for( FOOTPRINT* footprint : m_board->Footprints() )
331 sortedFootprints.push_back( footprint );
332
333 std::sort( sortedFootprints.begin(), sortedFootprints.end(),
334 []( FOOTPRINT* a, FOOTPRINT* b ) -> bool
335 {
336 return StrNumCmp( a->GetReference(), b->GetReference(), true ) < 0;
337 });
338
339 for( FOOTPRINT* footprint : sortedFootprints )
340 {
341 wxString ref = footprint->Reference().GetShownText( false );
342 wxString value = footprint->Value().GetShownText( false );
343
344 buffer += fmt::format( "$MODULE {}\n", TO_UTF8( ref ) );
345
346 buffer += fmt::format( "reference {}\n", TO_UTF8( ref ) );
347 buffer += fmt::format( "value {}\n", TO_UTF8( value ) );
348 buffer += fmt::format( "footprint {}\n", footprint->GetFPID().Format().c_str() );
349
350 buffer += "attribut";
351
352 if(( footprint->GetAttributes() & ( FP_THROUGH_HOLE | FP_SMD ) ) == 0 )
353 buffer += " virtual";
354
355 if( footprint->GetAttributes() & FP_SMD )
356 buffer += " smd";
357
358 if( footprint->GetAttributes() & FP_THROUGH_HOLE )
359 buffer += " none";
360
361 buffer += "\n";
362
363 VECTOR2I footprint_pos = footprint->GetPosition();
364 footprint_pos -= m_place_Offset;
365
366 buffer += fmt::format( "position {:9.6f} {:9.6f} orientation {:.2f}\n",
367 footprint_pos.x * conv_unit,
368 footprint_pos.y * conv_unit,
369 footprint->GetOrientation().AsDegrees() );
370
371 if( footprint->GetLayer() == F_Cu )
372 buffer += "layer front\n";
373 else if( footprint->GetLayer() == B_Cu )
374 buffer += "layer back\n";
375 else
376 buffer += "layer other\n";
377
378 std::vector<PAD*> sortedPads;
379
380 for( PAD* pad : footprint->Pads() )
381 sortedPads.push_back( pad );
382
383 std::sort( sortedPads.begin(), sortedPads.end(),
384 []( PAD* a, PAD* b ) -> bool
385 {
386 return StrNumCmp( a->GetNumber(), b->GetNumber(), true ) < 0;
387 });
388
389 for( PAD* pad : sortedPads )
390 {
391 buffer += fmt::format( "$PAD \"{}\"\n", TO_UTF8( pad->GetNumber() ) );
392
393 int layer = 0;
394
395 if( pad->GetLayerSet()[B_Cu] )
396 layer = 1;
397
398 if( pad->GetLayerSet()[F_Cu] )
399 layer |= 2;
400
401 // TODO(JE) padstacks
402 static const char* layer_name[4] = { "nocopper", "back", "front", "both" };
403 buffer += fmt::format( "Shape {} Layer {}\n",
404 TO_UTF8( pad->ShowPadShape( PADSTACK::ALL_LAYERS ) ),
405 layer_name[layer] );
406
407 VECTOR2I padPos = pad->GetFPRelativePosition();
408
409 buffer += fmt::format( "position {:9.6f} {:9.6f} size {:9.6f} {:9.6f} orientation {:.2f}\n",
410 padPos.x * conv_unit,
411 padPos.y * conv_unit,
412 pad->GetSize( PADSTACK::ALL_LAYERS ).x * conv_unit,
413 pad->GetSize( PADSTACK::ALL_LAYERS ).y * conv_unit,
414 pad->GetOrientation().AsDegrees() );
415
416 buffer += fmt::format( "drill {:9.6f}\n", pad->GetDrillSize().x * conv_unit );
417
418 buffer += fmt::format( "shape_offset {:9.6f} {:9.6f}\n",
419 pad->GetOffset( PADSTACK::ALL_LAYERS ).x * conv_unit,
420 pad->GetOffset( PADSTACK::ALL_LAYERS ).y * conv_unit );
421
422 buffer += "$EndPAD\n";
423 }
424
425 buffer += fmt::format( "$EndMODULE {}\n\n", TO_UTF8( ref ) );
426 }
427
428 // Generate EOF.
429 buffer += "$EndDESCRIPTION\n";
430
431 return buffer;
432}
433
434
435wxString PLACE_FILE_EXPORTER::DecorateFilename( const wxString& aBaseName, bool aFront, bool aBack )
436{
437 if( aFront && aBack )
438 return aBaseName + wxT( "-" ) + wxT( "all" );
439 else if( aFront )
440 return aBaseName + wxT( "-" ) + GetFrontSideName();
441 else if( aBack )
442 return aBaseName + wxT( "-" ) + GetBackSideName();
443 else
444 return aBaseName;
445}
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:688
@ 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