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