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 <wx/dirdlg.h>
38
39class LIST_MOD // An helper class used to build a list of useful footprints.
40{
41public:
42 FOOTPRINT* m_Footprint; // Link to the actual footprint
43 wxString m_Reference; // Its schematic reference
44 wxString m_Value; // Its schematic value
45 int m_Layer; // its side (B_Cu, or F_Cu)
46};
47
48
49// Defined values to write coordinates using inches or mm:
50static const double conv_unit_inch = 0.001 / pcbIUScale.IU_PER_MILS ; // units = INCHES
51static const char unit_text_inch[] = "## Unit = inches, Angle = deg.\n";
52
53static const double conv_unit_mm = 1.0 / pcbIUScale.IU_PER_MM; // units = mm
54static const char unit_text_mm[] = "## Unit = mm, Angle = deg.\n";
55
56// Sort function use by GenerefootprintsPosition()
57// sort is made by side (layer) top layer first
58// then by reference increasing order
59static bool sortFPlist( const LIST_MOD& ref, const LIST_MOD& tst )
60{
61 if( ref.m_Layer == tst.m_Layer )
62 return StrNumCmp( ref.m_Reference, tst.m_Reference ) < 0;
63
64 return ref.m_Layer > tst.m_Layer;
65}
66
67
69{
74};
75
76PLACE_FILE_EXPORTER::PLACE_FILE_EXPORTER( BOARD* aBoard, bool aUnitsMM, bool aOnlySMD,
77 bool aExcludeAllTH, bool aExcludeDNP, bool aTopSide,
78 bool aBottomSide, bool aFormatCSV, bool aUseAuxOrigin,
79 bool aNegateBottomX )
80{
81 m_board = aBoard;
82 m_unitsMM = aUnitsMM;
83 m_onlySMD = aOnlySMD;
84 m_excludeAllTH = aExcludeAllTH;
85 m_excludeDNP = aExcludeDNP;
86 m_fpCount = 0;
87 m_negateBottomX = aNegateBottomX;
88
89 if( aTopSide && aBottomSide )
91 else if( aTopSide )
93 else if( aBottomSide )
95 else
97
98 m_formatCSV = aFormatCSV;
99
100 if( aUseAuxOrigin )
102 else
103 m_place_Offset = VECTOR2I( 0, 0 );
104}
105
106
108{
109 std::string buffer;
110 char line[1024]; // A line to print intermediate data
111
112 // Minimal text lengths:
113 m_fpCount = 0;
114 int lenRefText = 8;
115 int lenValText = 8;
116 int lenPkgText = 16;
117
118 // Calculating the number of useful footprints (CMS attribute, not VIRTUAL)
119 m_fpCount = 0;
120
121 // Select units:
122 double conv_unit = m_unitsMM ? conv_unit_mm : conv_unit_inch;
123 const char *unit_text = m_unitsMM ? unit_text_mm : unit_text_inch;
124
125 // Build and sort the list of footprints alphabetically
126 std::vector<LIST_MOD> list;
127
128 for( FOOTPRINT* footprint : m_board->Footprints() )
129 {
130 if( m_side != PCB_BOTH_SIDES )
131 {
132 if( footprint->GetLayer() == B_Cu && m_side != PCB_BACK_SIDE )
133 continue;
134 if( footprint->GetLayer() == F_Cu && m_side != PCB_FRONT_SIDE )
135 continue;
136 }
137
138 if( footprint->GetAttributes() & FP_EXCLUDE_FROM_POS_FILES )
139 continue;
140
141 if( m_onlySMD && !( footprint->GetAttributes() & FP_SMD ) )
142 continue;
143
144 if( m_excludeAllTH && footprint->HasThroughHolePads() )
145 continue;
146
147 if( m_excludeDNP && ( footprint->GetAttributes() & FP_DNP ) )
148 continue;
149
150 m_fpCount++;
151
152 LIST_MOD item;
153 item.m_Footprint = footprint;
154 item.m_Reference = footprint->Reference().GetShownText( false );
155 item.m_Value = footprint->Value().GetShownText( false );
156 item.m_Layer = footprint->GetLayer();
157 list.push_back( item );
158
159 lenRefText = std::max( lenRefText, (int) item.m_Reference.length() );
160 lenValText = std::max( lenValText, (int) item.m_Value.length() );
161 lenPkgText = std::max( lenPkgText, (int) item.m_Footprint->GetFPID().GetLibItemName().length() );
162 }
163
164 if( list.size() > 1 )
165 sort( list.begin(), list.end(), sortFPlist );
166
167 // Switch the locale to standard C (needed to print floating point numbers)
168 LOCALE_IO toggle;
169
170 if( m_formatCSV )
171 {
172 wxChar csv_sep = ',';
173
174 // Set first line:;
175 snprintf( line, sizeof(line), "Ref%cVal%cPackage%cPosX%cPosY%cRot%cSide\n",
176 csv_sep, csv_sep, csv_sep, csv_sep, csv_sep, csv_sep );
177
178 buffer += line;
179
180 for( int ii = 0; ii < m_fpCount; ii++ )
181 {
182 VECTOR2I footprint_pos;
183 footprint_pos = list[ii].m_Footprint->GetPosition();
184 footprint_pos -= m_place_Offset;
185
186 int layer = list[ii].m_Footprint->GetLayer();
187 wxASSERT( layer == F_Cu || layer == B_Cu );
188
189 if( layer == B_Cu && m_negateBottomX )
190 footprint_pos.x = - footprint_pos.x;
191
192 wxString tmp = wxT( "\"" ) + list[ii].m_Reference;
193 tmp << wxT( "\"" ) << csv_sep;
194 tmp << wxT( "\"" ) << list[ii].m_Value;
195 tmp << wxT( "\"" ) << csv_sep;
196 tmp << wxT( "\"" ) << list[ii].m_Footprint->GetFPID().GetLibItemName().wx_str();
197 tmp << wxT( "\"" ) << csv_sep;
198
199 tmp << wxString::Format( wxT( "%f%c%f%c%f" ),
200 footprint_pos.x * conv_unit,
201 csv_sep,
202 // Keep the Y axis oriented from bottom to top,
203 // ( change y coordinate sign )
204 -footprint_pos.y * conv_unit,
205 csv_sep,
206 list[ii].m_Footprint->GetOrientation().AsDegrees() );
207 tmp << csv_sep;
208
209 tmp << ( (layer == F_Cu ) ? PLACE_FILE_EXPORTER::GetFrontSideName()
211 tmp << '\n';
212
213 buffer += TO_UTF8( tmp );
214 }
215 }
216 else
217 {
218 // Write file header
219 snprintf( line, sizeof(line), "### Footprint positions - created on %s ###\n",
221
222 buffer += line;
223
224 wxString Title = GetBuildVersion();
225 snprintf( line, sizeof(line), "### Printed by KiCad version %s\n", TO_UTF8( Title ) );
226 buffer += line;
227
228 buffer += unit_text;
229 buffer += "## Side : ";
230
231 if( m_side == PCB_BACK_SIDE )
232 buffer += GetBackSideName().c_str();
233 else if( m_side == PCB_FRONT_SIDE )
234 buffer += GetFrontSideName().c_str();
235 else if( m_side == PCB_BOTH_SIDES )
236 buffer += "All";
237 else
238 buffer += "---";
239
240 buffer += "\n";
241
242 snprintf( line, sizeof(line), "%-*s %-*s %-*s %9.9s %9.9s %8.8s %s\n",
243 int(lenRefText), "# Ref",
244 int(lenValText), "Val",
245 int(lenPkgText), "Package",
246 "PosX", "PosY", "Rot", "Side" );
247 buffer += line;
248
249 for( int ii = 0; ii < m_fpCount; ii++ )
250 {
251 VECTOR2I footprint_pos;
252 footprint_pos = list[ii].m_Footprint->GetPosition();
253 footprint_pos -= m_place_Offset;
254
255 int layer = list[ii].m_Footprint->GetLayer();
256 wxASSERT( layer == F_Cu || layer == B_Cu );
257
258 if( layer == B_Cu && m_negateBottomX )
259 footprint_pos.x = - footprint_pos.x;
260
261 wxString ref = list[ii].m_Reference;
262 wxString val = list[ii].m_Value;
263 wxString pkg = list[ii].m_Footprint->GetFPID().GetLibItemName();
264 ref.Replace( wxT( " " ), wxT( "_" ) );
265 val.Replace( wxT( " " ), wxT( "_" ) );
266 pkg.Replace( wxT( " " ), wxT( "_" ) );
267 snprintf( line, sizeof(line), "%-*s %-*s %-*s %9.4f %9.4f %8.4f %s\n",
268 lenRefText, TO_UTF8( ref ),
269 lenValText, TO_UTF8( val ),
270 lenPkgText, TO_UTF8( pkg ),
271 footprint_pos.x * conv_unit,
272 // Keep the coordinates in the first quadrant,
273 // (i.e. change y sign
274 -footprint_pos.y * conv_unit,
275 list[ii].m_Footprint->GetOrientation().AsDegrees(),
276 (layer == F_Cu ) ? GetFrontSideName().c_str() : GetBackSideName().c_str() );
277 buffer += line;
278 }
279
280 // Write EOF
281 buffer += "## End\n";
282 }
283
284 return buffer;
285}
286
287
289{
290 std::string buffer;
291
292 m_place_Offset = VECTOR2I( 0, 0 );
293
294 // Select units:
295 double conv_unit = m_unitsMM ? conv_unit_mm : conv_unit_inch;
296 const char *unit_text = m_unitsMM ? unit_text_mm : unit_text_inch;
297
298 LOCALE_IO toggle;
299
300 // Generate header file comments.)
301 char line[1024];
302 snprintf( line, sizeof(line), "## Footprint report - date %s\n",
304 buffer += line;
305
306 wxString Title = GetBuildVersion();
307 snprintf( line, sizeof(line), "## Created by KiCad version %s\n",
308 TO_UTF8( Title ) );
309 buffer += line;
310
311 buffer += unit_text;
312
313 buffer += "\n$BeginDESCRIPTION\n";
314
315 BOX2I bbbox = m_board->ComputeBoundingBox( false );
316
317 buffer += "\n$BOARD\n";
318
319 snprintf( line, sizeof(line), "upper_left_corner %9.6f %9.6f\n",
320 bbbox.GetX() * conv_unit,
321 bbbox.GetY() * conv_unit );
322 buffer += line;
323
324 snprintf( line, sizeof(line), "lower_right_corner %9.6f %9.6f\n",
325 bbbox.GetRight() * conv_unit,
326 bbbox.GetBottom() * conv_unit );
327 buffer += line;
328
329 buffer += "$EndBOARD\n\n";
330
331 std::vector<FOOTPRINT*> sortedFootprints;
332
333 for( FOOTPRINT* footprint : m_board->Footprints() )
334 sortedFootprints.push_back( footprint );
335
336 std::sort( sortedFootprints.begin(), sortedFootprints.end(),
337 []( FOOTPRINT* a, FOOTPRINT* b ) -> bool
338 {
339 return StrNumCmp( a->GetReference(), b->GetReference(), true ) < 0;
340 });
341
342 for( FOOTPRINT* footprint : sortedFootprints )
343 {
344 wxString ref = footprint->Reference().GetShownText( false );
345 wxString value = footprint->Value().GetShownText( false );
346
347 snprintf( line, sizeof(line), "$MODULE %s\n", TO_UTF8( ref ) );
348 buffer += line;
349
350 snprintf( line, sizeof(line), "reference %s\n", TO_UTF8( ref ) );
351 snprintf( line, sizeof(line), "value %s\n", TO_UTF8( value ) );
352 snprintf( line, sizeof(line), "footprint %s\n", footprint->GetFPID().Format().c_str() );
353 buffer += line;
354
355 buffer += "attribut";
356
357 if(( footprint->GetAttributes() & ( FP_THROUGH_HOLE | FP_SMD ) ) == 0 )
358 buffer += " virtual";
359
360 if( footprint->GetAttributes() & FP_SMD )
361 buffer += " smd";
362
363 if( footprint->GetAttributes() & FP_THROUGH_HOLE )
364 buffer += " none";
365
366 buffer += "\n";
367
368 VECTOR2I footprint_pos = footprint->GetPosition();
369 footprint_pos -= m_place_Offset;
370
371 snprintf( line, sizeof(line), "position %9.6f %9.6f orientation %.2f\n",
372 footprint_pos.x * conv_unit,
373 footprint_pos.y * conv_unit,
374 footprint->GetOrientation().AsDegrees() );
375 buffer += line;
376
377 if( footprint->GetLayer() == F_Cu )
378 buffer += "layer front\n";
379 else if( footprint->GetLayer() == B_Cu )
380 buffer += "layer back\n";
381 else
382 buffer += "layer other\n";
383
384 std::vector<PAD*> sortedPads;
385
386 for( PAD* pad : footprint->Pads() )
387 sortedPads.push_back( pad );
388
389 std::sort( sortedPads.begin(), sortedPads.end(),
390 []( PAD* a, PAD* b ) -> bool
391 {
392 return StrNumCmp( a->GetNumber(), b->GetNumber(), true ) < 0;
393 });
394
395 for( PAD* pad : sortedPads )
396 {
397 snprintf( line, sizeof(line), "$PAD \"%s\"\n", TO_UTF8( pad->GetNumber() ) );
398 buffer += line;
399
400 int layer = 0;
401
402 if( pad->GetLayerSet()[B_Cu] )
403 layer = 1;
404
405 if( pad->GetLayerSet()[F_Cu] )
406 layer |= 2;
407
408 // TODO(JE) padstacks
409 static const char* layer_name[4] = { "nocopper", "back", "front", "both" };
410 snprintf( line, sizeof(line), "Shape %s Layer %s\n",
411 TO_UTF8( pad->ShowPadShape( PADSTACK::ALL_LAYERS ) ),
412 layer_name[layer] );
413 buffer += line;
414
415 VECTOR2I padPos = pad->GetFPRelativePosition();
416
417 snprintf( line, sizeof(line), "position %9.6f %9.6f size %9.6f %9.6f orientation %.2f\n",
418 padPos.x * conv_unit,
419 padPos.y * conv_unit,
420 pad->GetSize( PADSTACK::ALL_LAYERS ).x * conv_unit,
421 pad->GetSize( PADSTACK::ALL_LAYERS ).y * conv_unit,
422 ( pad->GetOrientation() - footprint->GetOrientation() ).AsDegrees() );
423 buffer += line;
424
425 snprintf( line, sizeof(line), "drill %9.6f\n", pad->GetDrillSize().x * conv_unit );
426 buffer += line;
427
428 snprintf( line, sizeof(line), "shape_offset %9.6f %9.6f\n",
429 pad->GetOffset( PADSTACK::ALL_LAYERS ).x * conv_unit,
430 pad->GetOffset( PADSTACK::ALL_LAYERS ).y * conv_unit );
431 buffer += line;
432
433 buffer += "$EndPAD\n";
434 }
435
436 snprintf( line, sizeof(line), "$EndMODULE %s\n\n", TO_UTF8( ref ) );
437 buffer += line;
438 }
439
440 // Generate EOF.
441 buffer += "$EndDESCRIPTION\n";
442
443 return buffer;
444}
445
446
447wxString PLACE_FILE_EXPORTER::DecorateFilename( const wxString& aBaseName, bool aFront, bool aBack )
448{
449 if( aFront && aBack )
450 return aBaseName + wxT( "-" ) + wxT( "all" );
451 else if( aFront )
452 return aBaseName + wxT( "-" ) + GetFrontSideName();
453 else if( aBack )
454 return aBaseName + wxT( "-" ) + GetBackSideName();
455 else
456 return aBaseName;
457}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
wxString GetBuildVersion()
Get the full KiCad version string.
const VECTOR2I & GetAuxOrigin()
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:295
BOX2I ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Calculate the bounding box containing all board items (or board edge segments).
Definition: board.cpp:1713
const FOOTPRINTS & Footprints() const
Definition: board.h:336
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:934
constexpr coord_type GetY() const
Definition: box2.h:208
constexpr coord_type GetX() const
Definition: box2.h:207
constexpr coord_type GetRight() const
Definition: box2.h:217
constexpr coord_type GetBottom() const
Definition: box2.h:222
const LIB_ID & GetFPID() const
Definition: footprint.h:246
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
wxString m_Reference
FOOTPRINT * m_Footprint
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition: padstack.h:144
Definition: pad.h:54
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
PLACE_FILE_EXPORTER(BOARD *aBoard, bool aUnitsMM, bool aOnlySMD, bool aExcludeAllTH, bool aExcludeDNP, bool aTopSide, bool aBottomSide, bool aFormatCSV, bool aUseAuxOrigin, bool aNegateBottomX)
Create a PLACE_FILE_EXPORTER.
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:110
@ FP_SMD
Definition: footprint.h:80
@ FP_DNP
Definition: footprint.h:87
@ FP_EXCLUDE_FROM_POS_FILES
Definition: footprint.h:81
@ FP_THROUGH_HOLE
Definition: footprint.h:79
@ 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_NO_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.
Definition: string_utils.h:398
const double IU_PER_MM
Definition: base_units.h:76
const double IU_PER_MILS
Definition: base_units.h:77
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695