KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 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 files for automatic placement of smd components
26 * 2 - create a footprint report (pos and footprint descr) (ascii file)
27 */
28
30#include <confirm.h>
31#include <pcb_edit_frame.h>
33#include <bitmaps.h>
34#include <reporter.h>
37#include <kiface_base.h>
43
44#include <wx/dirdlg.h>
45#include <wx/msgdlg.h>
46
47
50 m_editFrame( aEditFrame ),
51 m_job( nullptr )
52{
53 m_messagesPanel->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
54 m_messagesPanel->MsgPanelSetMinSize( wxSize( -1, 160 ) );
55
56 m_browseButton->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
57
58 SetupStandardButtons( { { wxID_OK, _( "Generate Position File" ) },
59 { wxID_CANCEL, _( "Close" ) } } );
60
61 // DIALOG_SHIM needs a unique hash_key because classname will be the same for both job and
62 // non-job versions.
63 m_hash_key = TO_UTF8( GetTitle() );
64
65 GetSizer()->SetSizeHints( this );
66 Centre();
67}
68
69
71 PCB_EDIT_FRAME* aEditFrame,
72 wxWindow* aParent ) :
74 m_editFrame( aEditFrame ),
75 m_job( aJob )
76{
77 SetTitle( m_job->GetSettingsDialogTitle() );
78
79 m_browseButton->Hide();
80 m_units = m_job->m_units == JOB_EXPORT_PCB_POS::UNITS::INCH ? EDA_UNITS::INCH : EDA_UNITS::MM;
81 m_staticTextDir->SetLabel( _( "Output file:" ) );
82
83 m_messagesPanel->Hide();
84
86
87 // DIALOG_SHIM needs a unique hash_key because classname will be the same for both job and
88 // non-job versions.
89 m_hash_key = TO_UTF8( GetTitle() );
90
91 GetSizer()->SetSizeHints( this );
92 Centre();
93}
94
95
97{
98 if( m_job )
99 {
101
102 m_unitsCtrl->SetSelection( static_cast<int>( m_job->m_units ) );
103 m_singleFile->SetValue( m_job->m_singleFile );
104 m_formatCtrl->SetSelection( static_cast<int>( m_job->m_format ) );
107 m_onlySMD->SetValue( m_job->m_smdOnly );
108 m_negateXcb->SetValue( m_job->m_negateBottomX );
110 m_excludeDNP->SetValue( m_job->m_excludeDNP );
111 }
112
113 return true;
114}
115
116
118{
119 m_unitsLabel->Enable( m_formatCtrl->GetSelection() != 2 );
120 m_unitsCtrl->Enable( m_formatCtrl->GetSelection() != 2 );
121}
122
123
125{
126 m_singleFile->Enable( m_formatCtrl->GetSelection() != 2 );
127}
128
129
131{
132 if( m_formatCtrl->GetSelection() == 2 )
133 {
134 m_onlySMD->SetValue( false );
135 m_onlySMD->Enable( false );
136 }
137 else
138 {
139 m_onlySMD->Enable( true );
140 }
141}
142
143
145{
146 if( m_formatCtrl->GetSelection() == 2 )
147 {
148 m_negateXcb->SetValue( false );
149 m_negateXcb->Enable( false );
150 }
151 else
152 {
153 m_negateXcb->Enable( true );
154 }
155}
156
158{
159 if( m_formatCtrl->GetSelection() == 2 )
160 {
161 m_excludeTH->SetValue( false );
162 m_excludeTH->Enable( false );
163 }
164 else
165 {
166 m_excludeTH->Enable( true );
167 }
168}
169
170
172{
173 m_cbIncludeBoardEdge->Enable( m_formatCtrl->GetSelection() == 2 );
174}
175
176
178{
179 // Build the absolute path of current output directory to preselect it in the file browser.
180 wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
181 path = Prj().AbsolutePath( path );
182
183 wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
184
185 if( dirDialog.ShowModal() == wxID_CANCEL )
186 return;
187
188 wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
189
190 if( IsOK( this, _( "Use a relative path?" ) ) )
191 {
192 wxString boardFilePath = ( (wxFileName) m_editFrame->GetBoard()->GetFileName() ).GetPath();
193
194 if( !dirName.MakeRelativeTo( boardFilePath ) )
195 {
196 DisplayErrorMessage( this, _( "Cannot make path relative (target volume different from board "
197 "file volume)!" ) );
198 }
199 }
200
201 m_outputDirectoryName->SetValue( dirName.GetFullPath() );
202}
203
204
205void DIALOG_GEN_FOOTPRINT_POSITION::onGenerate( wxCommandEvent& event )
206{
207 if( !m_job )
208 {
209 m_units = m_unitsCtrl->GetSelection() == 0 ? EDA_UNITS::INCH : EDA_UNITS::MM;
210
212 // Keep unix directory format convention in cfg files
213 m_outputDirectory.Replace( wxT( "\\" ), wxT( "/" ) );
214
215 if( m_formatCtrl->GetSelection() == 2 )
217 else
219 }
220 else
221 {
225 m_job->m_format = static_cast<JOB_EXPORT_PCB_POS::FORMAT>( m_formatCtrl->GetSelection() );
227 m_job->m_singleFile = m_singleFile->GetValue();
230 m_job->m_smdOnly = m_onlySMD->GetValue();
232 m_job->m_negateBottomX = m_negateXcb->GetValue();
233 m_job->m_excludeDNP = m_excludeDNP->GetValue();
234
235 event.Skip(); // Allow normal close action
236 }
237}
238
239
241{
242 BOARD* brd = m_editFrame->GetBoard();
243 wxString msg;
244 int fullcount = 0;
245
246 // Create output directory if it does not exist (also transform it in absolute form).
247 // Bail if it fails.
248
249 std::function<bool( wxString* )> textResolver =
250 [&]( wxString* token ) -> bool
251 {
252 // Handles board->GetTitleBlock() *and* board->GetProject()
253 return m_editFrame->GetBoard()->ResolveTextVar( token, 0 );
254 };
255
256 wxString path = m_outputDirectory;
257 path = ExpandTextVars( path, &textResolver );
258 path = ExpandEnvVarSubstitutions( path, nullptr );
259
260 wxFileName outputDir = wxFileName::DirName( path );
261 wxString boardFilename = m_editFrame->GetBoard()->GetFileName();
262 REPORTER* reporter = &m_messagesPanel->Reporter();
263
264 if( !EnsureFileDirectoryExists( &outputDir, boardFilename, reporter ) )
265 {
266 msg.Printf( _( "Could not write plot files to folder '%s'." ), outputDir.GetPath() );
267 DisplayError( this, msg );
268 return false;
269 }
270
271 wxFileName fn = m_editFrame->GetBoard()->GetFileName();
272 fn.SetPath( outputDir.GetPath() );
273
274 // Create the Front and Top side placement files. Gerber P&P files are always separated.
275 // Not also they include all footprints
276 PLACEFILE_GERBER_WRITER exporter( brd );
277 wxString filename = exporter.GetPlaceFileName( fn.GetFullPath(), F_Cu );
278
279 int fpcount = exporter.CreatePlaceFile( filename, F_Cu, m_cbIncludeBoardEdge->GetValue(),
280 m_excludeDNP->GetValue() );
281
282 if( fpcount < 0 )
283 {
284 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
285 wxMessageBox( msg );
286 reporter->Report( msg, RPT_SEVERITY_ERROR );
287 return false;
288 }
289
290 msg.Printf( _( "Front (top side) placement file: '%s'." ), filename );
291 reporter->Report( msg, RPT_SEVERITY_ACTION );
292
293 msg.Printf( _( "Component count: %d." ), fpcount );
294 reporter->Report( msg, RPT_SEVERITY_INFO );
295
296 // Create the Back or Bottom side placement file
297 fullcount = fpcount;
298
299 filename = exporter.GetPlaceFileName( fn.GetFullPath(), B_Cu );
300
301 fpcount = exporter.CreatePlaceFile( filename, B_Cu, m_cbIncludeBoardEdge->GetValue(),
302 m_excludeDNP->GetValue() );
303
304 if( fpcount < 0 )
305 {
306 msg.Printf( _( "Failed to create file '%s'." ), filename );
307 reporter->Report( msg, RPT_SEVERITY_ERROR );
308 wxMessageBox( msg );
309 return false;
310 }
311
312 // Display results
313 msg.Printf( _( "Back (bottom side) placement file: '%s'." ), filename );
314 reporter->Report( msg, RPT_SEVERITY_ACTION );
315
316 msg.Printf( _( "Component count: %d." ), fpcount );
317 reporter->Report( msg, RPT_SEVERITY_INFO );
318
319 fullcount += fpcount;
320 msg.Printf( _( "Full component count: %d." ), fullcount );
321 reporter->Report( msg, RPT_SEVERITY_INFO );
322
323 reporter->Report( _( "Done." ), RPT_SEVERITY_INFO );
324
325 return true;
326}
327
328
330{
331 BOARD* brd = m_editFrame->GetBoard();
332 wxString msg;
333 bool singleFile = OneFileOnly();
334 bool useCSVfmt = m_formatCtrl->GetSelection() == 1;
335 bool useAuxOrigin = m_useDrillPlaceOrigin->GetValue();
336 int fullcount = 0;
337 int topSide = true;
338 int bottomSide = true;
339 bool negateBottomX = m_negateXcb->GetValue();
340
341 // Test for any footprint candidate in list.
342 {
344 topSide, bottomSide, useCSVfmt, useAuxOrigin, negateBottomX );
345 exporter.GenPositionData();
346
347 if( exporter.GetFootprintCount() == 0 )
348 {
349 wxMessageBox( _( "No footprint for automated placement." ) );
350 return false;
351 }
352 }
353
354 // Create output directory if it does not exist (also transform it in absolute form).
355 // Bail if it fails.
356
357 std::function<bool( wxString* )> textResolver =
358 [&]( wxString* token ) -> bool
359 {
360 // Handles board->GetTitleBlock() *and* board->GetProject()
361 return m_editFrame->GetBoard()->ResolveTextVar( token, 0 );
362 };
363
364 wxString path = m_outputDirectory;
365 path = ExpandTextVars( path, &textResolver );
366 path = ExpandEnvVarSubstitutions( path, nullptr );
367
368 wxFileName outputDir = wxFileName::DirName( path );
369 wxString boardFilename = m_editFrame->GetBoard()->GetFileName();
370 REPORTER* reporter = &m_messagesPanel->Reporter();
371
372 if( !EnsureFileDirectoryExists( &outputDir, boardFilename, reporter ) )
373 {
374 msg.Printf( _( "Could not write plot files to folder '%s'." ), outputDir.GetPath() );
375 DisplayError( this, msg );
376 return false;
377 }
378
379 wxFileName fn = m_editFrame->GetBoard()->GetFileName();
380 fn.SetPath( outputDir.GetPath() );
381
382 // Create the Front or Top side placement file, or a single file
383 topSide = true;
384 bottomSide = singleFile;
385
386 fn.SetName( PLACE_FILE_EXPORTER::DecorateFilename( fn.GetName(), topSide, bottomSide ) );
388
389 if( useCSVfmt )
390 {
391 fn.SetName( fn.GetName() + wxT( "-" ) + FILEEXT::FootprintPlaceFileExtension );
392 fn.SetExt( wxT( "csv" ) );
393 }
394
395 int fpcount = m_editFrame->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(), OnlySMD(),
396 ExcludeAllTH(), ExcludeDNP(), topSide, bottomSide,
397 useCSVfmt, useAuxOrigin, negateBottomX );
398 if( fpcount < 0 )
399 {
400 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
401 wxMessageBox( msg );
402 reporter->Report( msg, RPT_SEVERITY_ERROR );
403 return false;
404 }
405
406 if( singleFile )
407 msg.Printf( _( "Placement file: '%s'." ), fn.GetFullPath() );
408 else
409 msg.Printf( _( "Front (top side) placement file: '%s'." ), fn.GetFullPath() );
410
411 reporter->Report( msg, RPT_SEVERITY_ACTION );
412
413 msg.Printf( _( "Component count: %d." ), fpcount );
414 reporter->Report( msg, RPT_SEVERITY_INFO );
415
416 if( singleFile )
417 {
418 reporter->Report( _( "Done." ), RPT_SEVERITY_INFO );
419 return true;
420 }
421
422 // Create the Back or Bottom side placement file
423 fullcount = fpcount;
424 topSide = false;
425 bottomSide = true;
426 fn = brd->GetFileName();
427 fn.SetPath( outputDir.GetPath() );
428 fn.SetName( PLACE_FILE_EXPORTER::DecorateFilename( fn.GetName(), topSide, bottomSide ) );
430
431 if( useCSVfmt )
432 {
433 fn.SetName( fn.GetName() + wxT( "-" ) + FILEEXT::FootprintPlaceFileExtension );
434 fn.SetExt( wxT( "csv" ) );
435 }
436
437 fpcount = m_editFrame->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(), OnlySMD(),
438 ExcludeAllTH(), ExcludeDNP(), topSide, bottomSide,
439 useCSVfmt, useAuxOrigin, negateBottomX );
440
441 if( fpcount < 0 )
442 {
443 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
444 reporter->Report( msg, RPT_SEVERITY_ERROR );
445 wxMessageBox( msg );
446 return false;
447 }
448
449 // Display results
450 if( !singleFile )
451 {
452 msg.Printf( _( "Back (bottom side) placement file: '%s'." ), fn.GetFullPath() );
453 reporter->Report( msg, RPT_SEVERITY_ACTION );
454
455 msg.Printf( _( "Component count: %d." ), fpcount );
456 reporter->Report( msg, RPT_SEVERITY_INFO );
457 }
458
459 if( !singleFile )
460 {
461 fullcount += fpcount;
462 msg.Printf( _( "Full component count: %d." ), fullcount );
463 reporter->Report( msg, RPT_SEVERITY_INFO );
464 }
465
466 reporter->Report( _( "Done." ), RPT_SEVERITY_INFO );
467 return true;
468}
469
470
472{
473 DIALOG_GEN_FOOTPRINT_POSITION dlg( getEditFrame<PCB_EDIT_FRAME>() );
474 dlg.ShowModal();
475 return 0;
476}
477
478
479int PCB_EDIT_FRAME::DoGenFootprintsPositionFile( const wxString& aFullFileName, bool aUnitsMM,
480 bool aOnlySMD, bool aNoTHItems, bool aExcludeDNP,
481 bool aTopSide, bool aBottomSide, bool aFormatCSV,
482 bool aUseAuxOrigin, bool aNegateBottomX )
483{
484 FILE * file = nullptr;
485
486 if( !aFullFileName.IsEmpty() )
487 {
488 file = wxFopen( aFullFileName, wxT( "wt" ) );
489
490 if( file == nullptr )
491 return -1;
492 }
493
494 std::string data;
495 PLACE_FILE_EXPORTER exporter( GetBoard(), aUnitsMM, aOnlySMD, aNoTHItems, aExcludeDNP, aTopSide,
496 aBottomSide, aFormatCSV, aUseAuxOrigin, aNegateBottomX );
497 data = exporter.GenPositionData();
498
499 // if aFullFileName is empty, the file is not created, only the
500 // count of footprints to place is returned
501 if( file )
502 {
503 // Creates a footprint position file
504 // aSide = 0 -> Back (bottom) side)
505 // aSide = 1 -> Front (top) side)
506 // aSide = 2 -> both sides
507 fputs( data.c_str(), file );
508 fclose( file );
509 }
510
511 return exporter.GetFootprintCount();
512}
513
514
516{
518 wxFileName fn;
519
520 wxString boardFilePath = ( (wxFileName) board->GetFileName() ).GetPath();
521 wxDirDialog dirDialog( m_frame, _( "Select Output Directory" ), boardFilePath );
522
523 if( dirDialog.ShowModal() == wxID_CANCEL )
524 return 0;
525
526 fn = board->GetFileName();
527 fn.SetPath( dirDialog.GetPath() );
528 fn.SetExt( wxT( "rpt" ) );
529
530 FILE* rptfile = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
531
532 if( rptfile == nullptr )
533 {
534 wxMessageBox( wxString::Format( _( "Footprint report file created:\n'%s'." ), fn.GetFullPath() ),
535 _( "Footprint Report" ), wxICON_INFORMATION );
536
537 return 0;
538 }
539
540 std::string data;
541 PLACE_FILE_EXPORTER exporter( board, m_frame->GetUserUnits() == EDA_UNITS::MM,
542 false, // aOnlySMD
543 false, // aNoTHItems
544 false, // aExcludeDNP
545 true, true, // aTopSide, aBottomSide
546 false, // aFormatCSV
547 true, // aUseAuxOrigin
548 false ); // aNegateBottomX
549 data = exporter.GenReportData();
550
551 fputs( data.c_str(), rptfile );
552 fclose( rptfile );
553
554 wxMessageBox( wxString::Format( _( "Footprint report file created:\n'%s'." ), fn.GetFullPath() ),
555 _( "Footprint Report" ), wxICON_INFORMATION );
556
557 return 0;
558}
559
560
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
int GenFootprintsReport(const TOOL_EVENT &aEvent)
int GeneratePosFile(const TOOL_EVENT &aEvent)
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:317
bool ResolveTextVar(wxString *token, int aDepth) const
Definition: board.cpp:500
const wxString & GetFileName() const
Definition: board.h:354
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
bool CreateGerberFiles()
Creates placement files in gerber format.
void onGenerate(wxCommandEvent &event) override
void onUpdateUIFileOpt(wxUpdateUIEvent &event) override
void onUpdateUIOnlySMD(wxUpdateUIEvent &event) override
DIALOG_GEN_FOOTPRINT_POSITION(PCB_EDIT_FRAME *aEditFrame)
bool CreateAsciiFiles()
Creates files in text or csv format.
void onUpdateUInegXcoord(wxUpdateUIEvent &event) override
void onOutputDirectoryBrowseClicked(wxCommandEvent &event) override
void onUpdateUIUnits(wxUpdateUIEvent &event) override
EDA_UNITS m_units
Definition: dialog_shim.h:235
void SetupStandardButtons(std::map< int, wxString > aLabels={})
std::string m_hash_key
Definition: dialog_shim.h:236
int ShowModal() override
wxString GetSettingsDialogTitle() const override
void SetConfiguredOutputPath(const wxString &aPath)
Sets the configured output path for the job, this path is always saved to file.
Definition: job.cpp:153
wxString GetConfiguredOutputPath() const
Returns the configured output path for the job.
Definition: job.h:232
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
BOARD * GetBoard() const
The main frame for Pcbnew.
int DoGenFootprintsPositionFile(const wxString &aFullFileName, bool aUnitsMM, bool aOnlySMD, bool aNoTHItems, bool aExcludeDNP, bool aTopSide, bool aBottomSide, bool aFormatCSV, bool aUseAuxOrigin, bool aNegateBottomX)
Create an ASCII footprint position file.
BOARD * board() const
Used to create Gerber drill files.
const wxString GetPlaceFileName(const wxString &aFullBaseFilename, PCB_LAYER_ID aLayer) const
int CreatePlaceFile(const wxString &aFullFilename, PCB_LAYER_ID aLayer, bool aIncludeBrdEdges, bool aExcludeDNP)
Create an pnp gerber file.
The ASCII format of the kicad place file is:
static wxString DecorateFilename(const wxString &aBaseName, bool aFront, bool aBack)
std::string GenPositionData()
build a string filled with the position data
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:373
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition: reporter.h:102
void SetBitmap(const wxBitmapBundle &aBmp)
Generic, UI-independent tool event.
Definition: tool_event.h:168
EDA_UNITS GetUserUnits() const
void MsgPanelSetMinSize(const wxSize &aMinSize)
Set the min size of the area which displays html messages.
void SetFileName(const wxString &aReportFileName)
Set the report full file name to the string.
REPORTER & Reporter()
Return the reporter object that reports to this panel.
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:355
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, int aFlags)
Definition: common.cpp:59
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:376
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:251
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:194
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:169
This file is part of the common library.
#define _(s)
Classes used in place file generation.
static const std::string FootprintPlaceFileExtension
@ B_Cu
Definition: layer_ids.h:65
@ F_Cu
Definition: layer_ids.h:64
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
@ RPT_SEVERITY_ACTION
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:429
Definition of file extensions used in Kicad.