KiCad PCB EDA Suite
dialog_gen_footprint_position.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-2023 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 files for automatic placement of smd components
26 * 2 - create a footprint report (pos and footprint descr) (ascii file)
27 */
28
29#include <confirm.h>
30#include <string_utils.h>
31#include <gestfich.h>
32#include <pcb_edit_frame.h>
33#include <pcbnew_settings.h>
34#include <bitmaps.h>
35#include <reporter.h>
37#include <board.h>
39#include <kiface_base.h>
45
46#include <wx/dirdlg.h>
47
48
54{
55public:
58 m_parent( aParent )
59 {
60 m_messagesPanel->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
62 initDialog();
63
64 SetupStandardButtons( { { wxID_OK, _( "Generate Position File" ) },
65 { wxID_CANCEL, _( "Close" ) } } );
66
67 GetSizer()->SetSizeHints(this);
68 Centre();
69 }
70
71private:
72 void initDialog();
73 void OnOutputDirectoryBrowseClicked( wxCommandEvent& event ) override;
74 void OnGenerate( wxCommandEvent& event ) override;
75
76 void onUpdateUIUnits( wxUpdateUIEvent& event ) override
77 {
78 m_radioBoxUnits->Enable( m_rbFormat->GetSelection() != 2 );
79 }
80
81 void onUpdateUIFileOpt( wxUpdateUIEvent& event ) override
82 {
83 m_radioBoxFilesCount->Enable( m_rbFormat->GetSelection() != 2 );
84 }
85
86 void onUpdateUIOnlySMD( wxUpdateUIEvent& event ) override
87 {
88 if( m_rbFormat->GetSelection() == 2 )
89 {
90 m_onlySMD->SetValue( false );
91 m_onlySMD->Enable( false );
92 }
93 else
94 {
95 m_onlySMD->Enable( true );
96 }
97 }
98
99 void onUpdateUInegXcoord( wxUpdateUIEvent& event ) override
100 {
101 if( m_rbFormat->GetSelection() == 2 )
102 {
103 m_negateXcb->SetValue( false );
104 m_negateXcb->Enable( false );
105 }
106 else
107 {
108 m_negateXcb->Enable( true );
109 }
110 }
111
112 void onUpdateUIExcludeTH( wxUpdateUIEvent& event ) override
113 {
114 if( m_rbFormat->GetSelection() == 2 )
115 {
116 m_excludeTH->SetValue( false );
117 m_excludeTH->Enable( false );
118 }
119 else
120 {
121 m_excludeTH->Enable( true );
122 }
123 }
124
125 void onUpdateUIincludeBoardEdge( wxUpdateUIEvent& event ) override
126 {
127 m_cbIncludeBoardEdge->Enable( m_rbFormat->GetSelection() == 2 );
128 }
129
133 bool CreateAsciiFiles();
134
138 bool CreateGerberFiles();
139
140 // accessors to options:
141 bool UnitsMM()
142 {
143 return m_radioBoxUnits->GetSelection() == 1;
144 }
145
147 {
148 return m_radioBoxFilesCount->GetSelection() == 1;
149 }
150
151 bool OnlySMD()
152 {
153 return m_onlySMD->GetValue();
154 }
155
157 {
158 return m_excludeTH->GetValue();
159 }
160
161private:
164};
165
166
167
169{
171
173
175
176 // Output directory
178
179 // Update Options
180 m_radioBoxUnits->SetSelection( cfg->m_PlaceFile.units );
181 m_radioBoxFilesCount->SetSelection( cfg->m_PlaceFile.file_options );
182 m_rbFormat->SetSelection( cfg->m_PlaceFile.file_format );
185 m_onlySMD->SetValue( cfg->m_PlaceFile.only_SMD );
186 m_negateXcb->SetValue( cfg->m_PlaceFile.negate_xcoord );
187 m_excludeTH->SetValue( cfg->m_PlaceFile.exclude_TH );
188
189 // Update sizes and sizers:
190 m_messagesPanel->MsgPanelSetMinSize( wxSize( -1, 160 ) );
191 GetSizer()->SetSizeHints( this );
192}
193
195{
196 // Build the absolute path of current output directory to preselect it in the file browser.
197 wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
198 path = Prj().AbsolutePath( path );
199
200 wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
201
202 if( dirDialog.ShowModal() == wxID_CANCEL )
203 return;
204
205 wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
206
207 wxMessageDialog dialog( this, _( "Use a relative path?"), _( "Plot Output Directory" ),
208 wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
209
210 if( dialog.ShowModal() == wxID_YES )
211 {
212 wxString boardFilePath = ( (wxFileName) m_parent->GetBoard()->GetFileName() ).GetPath();
213
214 if( !dirName.MakeRelativeTo( boardFilePath ) )
215 wxMessageBox( _( "Cannot make path relative (target volume different from board "
216 "file volume)!" ),
217 _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
218 }
219
220 m_outputDirectoryName->SetValue( dirName.GetFullPath() );
221}
222
223
224void DIALOG_GEN_FOOTPRINT_POSITION::OnGenerate( wxCommandEvent& event )
225{
227
229
230 wxString dirStr = m_outputDirectoryName->GetValue();
231 // Keep unix directory format convention in cfg files
232 dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
233
234 cfg->m_PlaceFile.output_directory = dirStr;
235
236 cfg->m_PlaceFile.units = m_units == EDA_UNITS::INCHES ? 0 : 1;
237 cfg->m_PlaceFile.file_options = m_radioBoxFilesCount->GetSelection();
238 cfg->m_PlaceFile.file_format = m_rbFormat->GetSelection();
240 cfg->m_PlaceFile.exclude_TH = m_excludeTH->GetValue();
241 cfg->m_PlaceFile.only_SMD = m_onlySMD->GetValue();
243 cfg->m_PlaceFile.negate_xcoord = m_negateXcb->GetValue();
244
245 if( m_rbFormat->GetSelection() == 2 )
247 else
249}
250
251
253{
254 BOARD* brd = m_parent->GetBoard();
255 wxFileName fn;
256 wxString msg;
257 int fullcount = 0;
258
259 // Create output directory if it does not exist (also transform it in absolute form).
260 // Bail if it fails.
261
262 std::function<bool( wxString* )> textResolver =
263 [&]( wxString* token ) -> bool
264 {
265 // Handles board->GetTitleBlock() *and* board->GetProject()
266 return m_parent->GetBoard()->ResolveTextVar( token, 0 );
267 };
268
270 path = ExpandTextVars( path, &textResolver );
271 path = ExpandEnvVarSubstitutions( path, nullptr );
272
273 wxFileName outputDir = wxFileName::DirName( path );
274 wxString boardFilename = m_parent->GetBoard()->GetFileName();
275
277
278 if( !EnsureFileDirectoryExists( &outputDir, boardFilename, m_reporter ) )
279 {
280 msg.Printf( _( "Could not write plot files to folder '%s'." ), outputDir.GetPath() );
281 DisplayError( this, msg );
282 return false;
283 }
284
285 fn = m_parent->GetBoard()->GetFileName();
286 fn.SetPath( outputDir.GetPath() );
287
288 // Create the Front and Top side placement files. Gerber P&P files are always separated.
289 // Not also they include all footprints
290 PLACEFILE_GERBER_WRITER exporter( brd );
291 wxString filename = exporter.GetPlaceFileName( fn.GetFullPath(), F_Cu );
292
293 int fpcount = exporter.CreatePlaceFile( filename, F_Cu, m_cbIncludeBoardEdge->GetValue() );
294
295 if( fpcount < 0 )
296 {
297 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
298 wxMessageBox( msg );
300 return false;
301 }
302
303 msg.Printf( _( "Front (top side) placement file: '%s'." ), filename );
305
306 msg.Printf( _( "Component count: %d." ), fpcount );
308
309 // Create the Back or Bottom side placement file
310 fullcount = fpcount;
311
312 filename = exporter.GetPlaceFileName( fn.GetFullPath(), B_Cu );
313
314 fpcount = exporter.CreatePlaceFile( filename, B_Cu, m_cbIncludeBoardEdge->GetValue() );
315
316 if( fpcount < 0 )
317 {
318 msg.Printf( _( "Failed to create file '%s'." ), filename );
320 wxMessageBox( msg );
321 return false;
322 }
323
324 // Display results
325 msg.Printf( _( "Back (bottom side) placement file: '%s'." ), filename );
327
328 msg.Printf( _( "Component count: %d." ), fpcount );
330
331 fullcount += fpcount;
332 msg.Printf( _( "Full component count: %d." ), fullcount );
334
335 m_reporter->Report( _( "File generation successful." ), RPT_SEVERITY_INFO );
336
337 return true;
338}
339
340
342{
343 BOARD * brd = m_parent->GetBoard();
344 wxFileName fn;
345 wxString msg;
346 bool singleFile = OneFileOnly();
347 bool useCSVfmt = m_rbFormat->GetSelection() == 1;
348 bool useAuxOrigin = m_useDrillPlaceOrigin->GetValue();
349 int fullcount = 0;
350 int topSide = true;
351 int bottomSide = true;
352 bool negateBottomX = m_negateXcb->GetValue();
353
354 // Test for any footprint candidate in list.
355 {
356 PLACE_FILE_EXPORTER exporter( brd, UnitsMM(), OnlySMD(), ExcludeAllTH(), topSide,
357 bottomSide, useCSVfmt, useAuxOrigin, negateBottomX );
358 exporter.GenPositionData();
359
360 if( exporter.GetFootprintCount() == 0 )
361 {
362 wxMessageBox( _( "No footprint for automated placement." ) );
363 return false;
364 }
365 }
366
367 // Create output directory if it does not exist (also transform it in absolute form).
368 // Bail if it fails.
369
370 std::function<bool( wxString* )> textResolver =
371 [&]( wxString* token ) -> bool
372 {
373 // Handles board->GetTitleBlock() *and* board->GetProject()
374 return m_parent->GetBoard()->ResolveTextVar( token, 0 );
375 };
376
378 path = ExpandTextVars( path, &textResolver );
379 path = ExpandEnvVarSubstitutions( path, nullptr );
380
381 wxFileName outputDir = wxFileName::DirName( path );
382 wxString boardFilename = m_parent->GetBoard()->GetFileName();
383
385
386 if( !EnsureFileDirectoryExists( &outputDir, boardFilename, m_reporter ) )
387 {
388 msg.Printf( _( "Could not write plot files to folder '%s'." ), outputDir.GetPath() );
389 DisplayError( this, msg );
390 return false;
391 }
392
393 fn = m_parent->GetBoard()->GetFileName();
394 fn.SetPath( outputDir.GetPath() );
395
396 // Create the Front or Top side placement file, or a single file
397 topSide = true;
398 bottomSide = false;
399
400 if( singleFile )
401 {
402 bottomSide = true;
403 fn.SetName( fn.GetName() + wxT( "-" ) + wxT( "all" ) );
404 }
405 else
406 {
407 fn.SetName( fn.GetName() + wxT( "-" ) + PLACE_FILE_EXPORTER::GetFrontSideName().c_str() );
408 }
409
410
411 if( useCSVfmt )
412 {
413 fn.SetName( fn.GetName() + wxT( "-" ) + FootprintPlaceFileExtension );
414 fn.SetExt( wxT( "csv" ) );
415 }
416 else
417 {
418 fn.SetExt( FootprintPlaceFileExtension );
419 }
420
421 int fpcount = m_parent->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(), OnlySMD(),
422 ExcludeAllTH(), topSide, bottomSide,
423 useCSVfmt, useAuxOrigin, negateBottomX );
424 if( fpcount < 0 )
425 {
426 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
427 wxMessageBox( msg );
429 return false;
430 }
431
432 if( singleFile )
433 msg.Printf( _( "Placement file: '%s'." ), fn.GetFullPath() );
434 else
435 msg.Printf( _( "Front (top side) placement file: '%s'." ), fn.GetFullPath() );
436
438
439 msg.Printf( _( "Component count: %d." ), fpcount );
441
442 if( singleFile )
443 {
444 m_reporter->Report( _( "File generation successful." ), RPT_SEVERITY_INFO );
445 return true;
446 }
447
448 // Create the Back or Bottom side placement file
449 fullcount = fpcount;
450 topSide = false;
451 bottomSide = true;
452 fn = brd->GetFileName();
453 fn.SetPath( outputDir.GetPath() );
454 fn.SetName( fn.GetName() + wxT( "-" ) + PLACE_FILE_EXPORTER::GetBackSideName().c_str() );
455
456 if( useCSVfmt )
457 {
458 fn.SetName( fn.GetName() + wxT( "-" ) + FootprintPlaceFileExtension );
459 fn.SetExt( wxT( "csv" ) );
460 }
461 else
462 {
463 fn.SetExt( FootprintPlaceFileExtension );
464 }
465
466 fpcount = m_parent->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(), OnlySMD(),
467 ExcludeAllTH(), topSide, bottomSide, useCSVfmt,
468 useAuxOrigin, negateBottomX );
469
470 if( fpcount < 0 )
471 {
472 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
474 wxMessageBox( msg );
475 return false;
476 }
477
478 // Display results
479 if( !singleFile )
480 {
481 msg.Printf( _( "Back (bottom side) placement file: '%s'." ), fn.GetFullPath() );
483
484 msg.Printf( _( "Component count: %d." ), fpcount );
486 }
487
488 if( !singleFile )
489 {
490 fullcount += fpcount;
491 msg.Printf( _( "Full component count: %d." ), fullcount );
493 }
494
495 m_reporter->Report( _( "File generation successful." ), RPT_SEVERITY_INFO );
496
497 return true;
498}
499
500
502{
503 PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
504 DIALOG_GEN_FOOTPRINT_POSITION dlg( editFrame );
505
506 dlg.ShowModal();
507 return 0;
508}
509
510
511int PCB_EDIT_FRAME::DoGenFootprintsPositionFile( const wxString& aFullFileName, bool aUnitsMM,
512 bool aOnlySMD, bool aNoTHItems, bool aTopSide,
513 bool aBottomSide, bool aFormatCSV,
514 bool aUseAuxOrigin, bool aNegateBottomX )
515{
516 FILE * file = nullptr;
517
518 if( !aFullFileName.IsEmpty() )
519 {
520 file = wxFopen( aFullFileName, wxT( "wt" ) );
521
522 if( file == nullptr )
523 return -1;
524 }
525
526 std::string data;
527 PLACE_FILE_EXPORTER exporter( GetBoard(), aUnitsMM, aOnlySMD, aNoTHItems, aTopSide, aBottomSide,
528 aFormatCSV, aUseAuxOrigin, aNegateBottomX );
529 data = exporter.GenPositionData();
530
531 // if aFullFileName is empty, the file is not created, only the
532 // count of footprints to place is returned
533 if( file )
534 {
535 // Creates a footprint position file
536 // aSide = 0 -> Back (bottom) side)
537 // aSide = 1 -> Front (top) side)
538 // aSide = 2 -> both sides
539 fputs( data.c_str(), file );
540 fclose( file );
541 }
542
543 return exporter.GetFootprintCount();
544}
545
546
547void PCB_EDIT_FRAME::GenFootprintsReport( wxCommandEvent& event )
548{
549 wxFileName fn;
550
551 wxString boardFilePath = ( (wxFileName) GetBoard()->GetFileName() ).GetPath();
552 wxDirDialog dirDialog( this, _( "Select Output Directory" ), boardFilePath );
553
554 if( dirDialog.ShowModal() == wxID_CANCEL )
555 return;
556
557 fn = GetBoard()->GetFileName();
558 fn.SetPath( dirDialog.GetPath() );
559 fn.SetExt( wxT( "rpt" ) );
560
561 bool unitMM = GetUserUnits() == EDA_UNITS::MILLIMETRES;
562 bool success = DoGenFootprintsReport( fn.GetFullPath(), unitMM );
563
564 wxString msg;
565
566 if( success )
567 {
568 msg.Printf( _( "Footprint report file created:\n'%s'." ), fn.GetFullPath() );
569 wxMessageBox( msg, _( "Footprint Report" ), wxICON_INFORMATION );
570 }
571
572 else
573 {
574 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
575 DisplayError( this, msg );
576 }
577}
578
579
580bool PCB_EDIT_FRAME::DoGenFootprintsReport( const wxString& aFullFilename, bool aUnitsMM )
581{
582 FILE* rptfile = wxFopen( aFullFilename, wxT( "wt" ) );
583
584 if( rptfile == nullptr )
585 return false;
586
587 std::string data;
588 PLACE_FILE_EXPORTER exporter( GetBoard(), aUnitsMM,
589 false, false, // SMD aOnlySMD, aNoTHItems
590 true, true, // aTopSide, aBottomSide
591 false, true, false // aFormatCSV, aUseAuxOrigin, aNegateBottomX
592 );
593 data = exporter.GenReportData();
594
595 fputs( data.c_str(), rptfile );
596 fclose( rptfile );
597
598 return true;
599}
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:105
@ small_folder
int GeneratePosFile(const TOOL_EVENT &aEvent)
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:265
bool ResolveTextVar(wxString *token, int aDepth) const
Definition: board.cpp:332
const wxString & GetFileName() const
Definition: board.h:302
Class DIALOG_GEN_FOOTPRINT_POSITION_BASE.
The dialog to create footprint position files and choose options (one or 2 files, units and force all...
void onUpdateUIincludeBoardEdge(wxUpdateUIEvent &event) override
void onUpdateUIExcludeTH(wxUpdateUIEvent &event) override
DIALOG_GEN_FOOTPRINT_POSITION(PCB_EDIT_FRAME *aParent)
bool CreateGerberFiles()
Creates placement files in gerber format.
void onUpdateUIFileOpt(wxUpdateUIEvent &event) override
void onUpdateUIOnlySMD(wxUpdateUIEvent &event) override
bool CreateAsciiFiles()
Creates files in text or csv format.
void onUpdateUInegXcoord(wxUpdateUIEvent &event) override
void OnGenerate(wxCommandEvent &event) override
void OnOutputDirectoryBrowseClicked(wxCommandEvent &event) override
void onUpdateUIUnits(wxUpdateUIEvent &event) override
EDA_UNITS m_units
Definition: dialog_shim.h:202
void SetupStandardButtons(std::map< int, wxString > aLabels={})
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
DIALOG_PLACE_FILE m_PlaceFile
PCBNEW_SETTINGS * GetPcbNewSettings() const
BOARD * GetBoard() const
The main frame for Pcbnew.
int DoGenFootprintsPositionFile(const wxString &aFullFileName, bool aUnitsMM, bool aOnlySMD, bool aNoTHItems, bool aTopSide, bool aBottomSide, bool aFormatCSV, bool aUseAuxOrigin, bool aNegateBottomX)
Create an ASCII footprint position file.
void GenFootprintsReport(wxCommandEvent &event)
Call DoGenFootprintsReport to create a footprint report file.
bool DoGenFootprintsReport(const wxString &aFullFilename, bool aUnitsMM)
Create an ASCII footprint report file giving some infos on footprints and board outlines.
Used to create Gerber drill files.
const wxString GetPlaceFileName(const wxString &aFullBaseFilename, PCB_LAYER_ID aLayer) const
int CreatePlaceFile(wxString &aFullFilename, PCB_LAYER_ID aLayer, bool aIncludeBrdEdges)
Create an pnp gerber file.
The ASCII format of the kicad place file is:
static std::string GetFrontSideName()
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,...
virtual const wxString AbsolutePath(const wxString &aFileName) const
Fix up aFileName if it is relative to the project's directory to be an absolute path and filename.
Definition: project.cpp:305
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
void SetBitmap(const wxBitmap &aBmp)
Generic, UI-independent tool event.
Definition: tool_event.h:156
EDA_UNITS GetUserUnits() const
void MsgPanelSetMinSize(const wxSize &aMinSize)
returns the reporter object that reports to this panel
void SetFileName(const wxString &aReportFileName)
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:299
bool EnsureFileDirectoryExists(wxFileName *aTargetFullFileName, const wxString &aBaseFilename, REPORTER *aReporter)
Make aTargetFullFileName absolute and create the path of this file if it doesn't yet exist.
Definition: common.cpp:327
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject)
Definition: common.cpp:58
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:300
This file is part of the common library.
#define _(s)
Classes used in place file generation.
const std::string FootprintPlaceFileExtension
@ B_Cu
Definition: layer_ids.h:95
@ F_Cu
Definition: layer_ids.h:64
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
Definition of file extensions used in Kicad.