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 (C) 2015-2022 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", TO_UTF8( GetISO8601CurrentDateTime() ) );
220
221 buffer += line;
222
223 wxString Title = GetBuildVersion();
224 snprintf( line, sizeof(line), "### Printed by KiCad version %s\n", TO_UTF8( Title ) );
225 buffer += line;
226
227 buffer += unit_text;
228 buffer += "## Side : ";
229
230 if( m_side == PCB_BACK_SIDE )
231 buffer += GetBackSideName().c_str();
232 else if( m_side == PCB_FRONT_SIDE )
233 buffer += GetFrontSideName().c_str();
234 else if( m_side == PCB_BOTH_SIDES )
235 buffer += "All";
236 else
237 buffer += "---";
238
239 buffer += "\n";
240
241 snprintf( line, sizeof(line), "%-*s %-*s %-*s %9.9s %9.9s %8.8s %s\n",
242 int(lenRefText), "# Ref",
243 int(lenValText), "Val",
244 int(lenPkgText), "Package",
245 "PosX", "PosY", "Rot", "Side" );
246 buffer += line;
247
248 for( int ii = 0; ii < m_fpCount; ii++ )
249 {
250 VECTOR2I footprint_pos;
251 footprint_pos = list[ii].m_Footprint->GetPosition();
252 footprint_pos -= m_place_Offset;
253
254 int layer = list[ii].m_Footprint->GetLayer();
255 wxASSERT( layer == F_Cu || layer == B_Cu );
256
257 if( layer == B_Cu && m_negateBottomX )
258 footprint_pos.x = - footprint_pos.x;
259
260 wxString ref = list[ii].m_Reference;
261 wxString val = list[ii].m_Value;
262 wxString pkg = list[ii].m_Footprint->GetFPID().GetLibItemName();
263 ref.Replace( wxT( " " ), wxT( "_" ) );
264 val.Replace( wxT( " " ), wxT( "_" ) );
265 pkg.Replace( wxT( " " ), wxT( "_" ) );
266 snprintf( line, sizeof(line), "%-*s %-*s %-*s %9.4f %9.4f %8.4f %s\n",
267 lenRefText, TO_UTF8( ref ),
268 lenValText, TO_UTF8( val ),
269 lenPkgText, TO_UTF8( pkg ),
270 footprint_pos.x * conv_unit,
271 // Keep the coordinates in the first quadrant,
272 // (i.e. change y sign
273 -footprint_pos.y * conv_unit,
274 list[ii].m_Footprint->GetOrientation().AsDegrees(),
275 (layer == F_Cu ) ? GetFrontSideName().c_str() : GetBackSideName().c_str() );
276 buffer += line;
277 }
278
279 // Write EOF
280 buffer += "## End\n";
281 }
282
283 return buffer;
284}
285
286
288{
289 std::string buffer;
290
291 m_place_Offset = VECTOR2I( 0, 0 );
292
293 // Select units:
294 double conv_unit = m_unitsMM ? conv_unit_mm : conv_unit_inch;
295 const char *unit_text = m_unitsMM ? unit_text_mm : unit_text_inch;
296
297 LOCALE_IO toggle;
298
299 // Generate header file comments.)
300 char line[1024];
301 snprintf( line, sizeof(line), "## Footprint report - date %s\n", TO_UTF8( GetISO8601CurrentDateTime() ) );
302 buffer += line;
303
304 wxString Title = GetBuildVersion();
305 snprintf( line, sizeof(line), "## Created by KiCad version %s\n", TO_UTF8( Title ) );
306 buffer += line;
307
308 buffer += unit_text;
309
310 buffer += "\n$BeginDESCRIPTION\n";
311
312 BOX2I bbbox = m_board->ComputeBoundingBox( false );
313
314 buffer += "\n$BOARD\n";
315
316 snprintf( line, sizeof(line), "upper_left_corner %9.6f %9.6f\n",
317 bbbox.GetX() * conv_unit, bbbox.GetY() * conv_unit );
318 buffer += line;
319
320 snprintf( line, sizeof(line), "lower_right_corner %9.6f %9.6f\n",
321 bbbox.GetRight() * conv_unit, bbbox.GetBottom() * conv_unit );
322 buffer += line;
323
324 buffer += "$EndBOARD\n\n";
325
326 std::vector<FOOTPRINT*> sortedFootprints;
327
328 for( FOOTPRINT* footprint : m_board->Footprints() )
329 sortedFootprints.push_back( footprint );
330
331 std::sort( sortedFootprints.begin(), sortedFootprints.end(),
332 []( FOOTPRINT* a, FOOTPRINT* b ) -> bool
333 {
334 return StrNumCmp( a->GetReference(), b->GetReference(), true ) < 0;
335 });
336
337 for( FOOTPRINT* footprint : sortedFootprints )
338 {
339 wxString ref = footprint->Reference().GetShownText( false );
340 wxString value = footprint->Value().GetShownText( false );
341
342 snprintf( line, sizeof(line), "$MODULE %s\n", TO_UTF8( ref ) );
343 buffer += line;
344
345 snprintf( line, sizeof(line), "reference %s\n", TO_UTF8( ref ) );
346 snprintf( line, sizeof(line), "value %s\n", TO_UTF8( value ) );
347 snprintf( line, sizeof(line), "footprint %s\n", footprint->GetFPID().Format().c_str() );
348 buffer += line;
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 snprintf( line, sizeof(line), "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 buffer += line;
371
372 if( footprint->GetLayer() == F_Cu )
373 buffer += "layer front\n";
374 else if( footprint->GetLayer() == B_Cu )
375 buffer += "layer back\n";
376 else
377 buffer += "layer other\n";
378
379 std::vector<PAD*> sortedPads;
380
381 for( PAD* pad : footprint->Pads() )
382 sortedPads.push_back( pad );
383
384 std::sort( sortedPads.begin(), sortedPads.end(),
385 []( PAD* a, PAD* b ) -> bool
386 {
387 return StrNumCmp( a->GetNumber(), b->GetNumber(), true ) < 0;
388 });
389
390 for( PAD* pad : sortedPads )
391 {
392 snprintf( line, sizeof(line), "$PAD \"%s\"\n", TO_UTF8( pad->GetNumber() ) );
393 buffer += line;
394
395 int layer = 0;
396
397 if( pad->GetLayerSet()[B_Cu] )
398 layer = 1;
399
400 if( pad->GetLayerSet()[F_Cu] )
401 layer |= 2;
402
403 // TODO(JE) padstacks
404 static const char* layer_name[4] = { "nocopper", "back", "front", "both" };
405 snprintf( line, sizeof(line), "Shape %s Layer %s\n",
406 TO_UTF8( pad->ShowPadShape( PADSTACK::ALL_LAYERS ) ),
407 layer_name[layer] );
408 buffer += line;
409
410 VECTOR2I padPos = pad->GetFPRelativePosition();
411
412 snprintf( line, sizeof(line), "position %9.6f %9.6f size %9.6f %9.6f orientation %.2f\n",
413 padPos.x * conv_unit,
414 padPos.y * conv_unit,
415 pad->GetSize( PADSTACK::ALL_LAYERS ).x * conv_unit,
416 pad->GetSize( PADSTACK::ALL_LAYERS ).y * conv_unit,
417 ( pad->GetOrientation() - footprint->GetOrientation() ).AsDegrees() );
418 buffer += line;
419
420 snprintf( line, sizeof(line), "drill %9.6f\n", pad->GetDrillSize().x * conv_unit );
421 buffer += line;
422
423 snprintf( line, sizeof(line), "shape_offset %9.6f %9.6f\n",
424 pad->GetOffset( PADSTACK::ALL_LAYERS ).x * conv_unit,
425 pad->GetOffset( PADSTACK::ALL_LAYERS ).y * conv_unit );
426 buffer += line;
427
428 buffer += "$EndPAD\n";
429 }
430
431 snprintf( line, sizeof(line), "$EndMODULE %s\n\n", TO_UTF8( ref ) );
432 buffer += line;
433 }
434
435 // Generate EOF.
436 buffer += "$EndDESCRIPTION\n";
437
438 return buffer;
439}
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:290
BOX2I ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Calculate the bounding box containing all board items (or board edge segments).
Definition: board.cpp:1674
const FOOTPRINTS & Footprints() const
Definition: board.h:331
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:895
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:248
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()
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:76
@ FP_DNP
Definition: footprint.h:83
@ FP_EXCLUDE_FROM_POS_FILES
Definition: footprint.h:77
@ FP_THROUGH_HOLE
Definition: footprint.h:75
@ 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:691