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
31#include <wx/dirdlg.h>
32#include <wx/msgdlg.h>
33
34#include <confirm.h>
35#include <pcb_edit_frame.h>
37#include <bitmaps.h>
38#include <reporter.h>
41#include <kiface_base.h>
42#include <string_utils.h>
48
49
52 m_editFrame( aEditFrame ),
53 m_job( nullptr )
54{
55 m_messagesPanel->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
56 m_messagesPanel->MsgPanelSetMinSize( wxSize( -1, 160 ) );
57
59
60 SetupStandardButtons( { { wxID_OK, _( "Generate Position File" ) },
61 { wxID_CANCEL, _( "Close" ) } } );
62
63 // DIALOG_SHIM needs a unique hash_key because classname will be the same for both job and
64 // non-job versions.
65 m_hash_key = TO_UTF8( GetTitle() );
66
67 GetSizer()->SetSizeHints( this );
68 Centre();
69}
70
71
73 PCB_EDIT_FRAME* aEditFrame,
74 wxWindow* aParent ) :
76 m_editFrame( aEditFrame ),
77 m_job( aJob )
78{
79 SetTitle( m_job->GetSettingsDialogTitle() );
80
81 m_browseButton->Hide();
83 m_staticTextDir->SetLabel( _( "Output file:" ) );
84
85 m_messagesPanel->Hide();
86
88
89 // DIALOG_SHIM needs a unique hash_key because classname will be the same for both job and
90 // non-job versions.
91 m_hash_key = TO_UTF8( GetTitle() );
92
93 GetSizer()->SetSizeHints( this );
94 Centre();
95}
96
97
99{
100 if( m_job )
101 {
102 m_outputDirectoryName->SetValue( m_job->GetConfiguredOutputPath() );
103
104 m_unitsCtrl->SetSelection( static_cast<int>( m_job->m_units ) );
105 m_singleFile->SetValue( m_job->m_singleFile );
106 m_formatCtrl->SetSelection( static_cast<int>( m_job->m_format ) );
107 m_cbIncludeBoardEdge->SetValue( m_job->m_gerberBoardEdge );
108 m_useDrillPlaceOrigin->SetValue( m_job->m_useDrillPlaceFileOrigin );
109 m_onlySMD->SetValue( m_job->m_smdOnly );
110 m_negateXcb->SetValue( m_job->m_negateBottomX );
111 m_excludeTH->SetValue( m_job->m_excludeFootprintsWithTh );
112 m_excludeDNP->SetValue( m_job->m_excludeDNP );
113 }
114
115 return true;
116}
117
118
120{
121 m_unitsLabel->Enable( m_formatCtrl->GetSelection() != 2 );
122 m_unitsCtrl->Enable( m_formatCtrl->GetSelection() != 2 );
123}
124
125
127{
128 m_singleFile->Enable( m_formatCtrl->GetSelection() != 2 );
129}
130
131
133{
134 if( m_formatCtrl->GetSelection() == 2 )
135 {
136 m_onlySMD->SetValue( false );
137 m_onlySMD->Enable( false );
138 }
139 else
140 {
141 m_onlySMD->Enable( true );
142 }
143}
144
145
147{
148 if( m_formatCtrl->GetSelection() == 2 )
149 {
150 m_negateXcb->SetValue( false );
151 m_negateXcb->Enable( false );
152 }
153 else
154 {
155 m_negateXcb->Enable( true );
156 }
157}
158
160{
161 if( m_formatCtrl->GetSelection() == 2 )
162 {
163 m_excludeTH->SetValue( false );
164 m_excludeTH->Enable( false );
165 }
166 else
167 {
168 m_excludeTH->Enable( true );
169 }
170}
171
172
174{
175 m_cbIncludeBoardEdge->Enable( m_formatCtrl->GetSelection() == 2 );
176}
177
178
180{
181 // Build the absolute path of current output directory to preselect it in the file browser.
182 wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
183 path = Prj().AbsolutePath( path );
184
185 wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
186
187 if( dirDialog.ShowModal() == wxID_CANCEL )
188 return;
189
190 wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
191
192 if( IsOK( this, _( "Use a relative path?" ) ) )
193 {
194 wxString boardFilePath = ( (wxFileName) m_editFrame->GetBoard()->GetFileName() ).GetPath();
195
196 if( !dirName.MakeRelativeTo( boardFilePath ) )
197 {
198 DisplayErrorMessage( this, _( "Cannot make path relative (target volume different from board "
199 "file volume)!" ) );
200 }
201 }
202
203 m_outputDirectoryName->SetValue( dirName.GetFullPath() );
204}
205
206
207void DIALOG_GEN_FOOTPRINT_POSITION::onGenerate( wxCommandEvent& event )
208{
209 if( !m_job )
210 {
211 m_units = m_unitsCtrl->GetSelection() == 0 ? EDA_UNITS::INCH : EDA_UNITS::MM;
212
214 // Keep unix directory format convention in cfg files
215 m_outputDirectory.Replace( wxT( "\\" ), wxT( "/" ) );
216
217 if( m_formatCtrl->GetSelection() == 2 )
219 else
221 }
222 else
223 {
224 m_job->SetConfiguredOutputPath( m_outputDirectoryName->GetValue() );
225 m_job->m_units = m_unitsCtrl->GetSelection() == 0 ? JOB_EXPORT_PCB_POS::UNITS::INCH
227 m_job->m_format = static_cast<JOB_EXPORT_PCB_POS::FORMAT>( m_formatCtrl->GetSelection() );
229 m_job->m_singleFile = m_singleFile->GetValue();
230 m_job->m_gerberBoardEdge = m_cbIncludeBoardEdge->GetValue();
231 m_job->m_excludeFootprintsWithTh = m_excludeTH->GetValue();
232 m_job->m_smdOnly = m_onlySMD->GetValue();
233 m_job->m_useDrillPlaceFileOrigin = m_useDrillPlaceOrigin->GetValue();
234 m_job->m_negateBottomX = m_negateXcb->GetValue();
235 m_job->m_excludeDNP = m_excludeDNP->GetValue();
236
237 event.Skip(); // Allow normal close action
238 }
239}
240
241
243{
244 BOARD* brd = m_editFrame->GetBoard();
245 wxString msg;
246 int fullcount = 0;
247
248 // Create output directory if it does not exist (also transform it in absolute form).
249 // Bail if it fails.
250
251 std::function<bool( wxString* )> textResolver =
252 [&]( wxString* token ) -> bool
253 {
254 // Handles board->GetTitleBlock() *and* board->GetProject()
255 return m_editFrame->GetBoard()->ResolveTextVar( token, 0 );
256 };
257
258 wxString path = m_outputDirectory;
259 path = ExpandTextVars( path, &textResolver );
260 path = ExpandEnvVarSubstitutions( path, nullptr );
261
262 wxFileName outputDir = wxFileName::DirName( path );
263 wxString boardFilename = m_editFrame->GetBoard()->GetFileName();
264 REPORTER* reporter = &m_messagesPanel->Reporter();
265
266 if( !EnsureFileDirectoryExists( &outputDir, boardFilename, reporter ) )
267 {
268 msg.Printf( _( "Could not write plot files to folder '%s'." ), outputDir.GetPath() );
269 DisplayError( this, msg );
270 return false;
271 }
272
273 wxFileName fn = m_editFrame->GetBoard()->GetFileName();
274 fn.SetPath( outputDir.GetPath() );
275
276 // Create the Front and Top side placement files. Gerber P&P files are always separated.
277 // Not also they include all footprints
278 PLACEFILE_GERBER_WRITER exporter( brd );
279 wxString filename = exporter.GetPlaceFileName( fn.GetFullPath(), F_Cu );
280
281 int fpcount = exporter.CreatePlaceFile( filename, F_Cu, m_cbIncludeBoardEdge->GetValue(),
282 m_excludeDNP->GetValue() );
283
284 if( fpcount < 0 )
285 {
286 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
287 wxMessageBox( msg );
288 reporter->Report( msg, RPT_SEVERITY_ERROR );
289 return false;
290 }
291
292 msg.Printf( _( "Front (top side) placement file: '%s'." ), filename );
293 reporter->Report( msg, RPT_SEVERITY_ACTION );
294
295 msg.Printf( _( "Component count: %d." ), fpcount );
296 reporter->Report( msg, RPT_SEVERITY_INFO );
297
298 // Create the Back or Bottom side placement file
299 fullcount = fpcount;
300
301 filename = exporter.GetPlaceFileName( fn.GetFullPath(), B_Cu );
302
303 fpcount = exporter.CreatePlaceFile( filename, B_Cu, m_cbIncludeBoardEdge->GetValue(),
304 m_excludeDNP->GetValue() );
305
306 if( fpcount < 0 )
307 {
308 msg.Printf( _( "Failed to create file '%s'." ), filename );
309 reporter->Report( msg, RPT_SEVERITY_ERROR );
310 wxMessageBox( msg );
311 return false;
312 }
313
314 // Display results
315 msg.Printf( _( "Back (bottom side) placement file: '%s'." ), filename );
316 reporter->Report( msg, RPT_SEVERITY_ACTION );
317
318 msg.Printf( _( "Component count: %d." ), fpcount );
319 reporter->Report( msg, RPT_SEVERITY_INFO );
320
321 fullcount += fpcount;
322 msg.Printf( _( "Full component count: %d." ), fullcount );
323 reporter->Report( msg, RPT_SEVERITY_INFO );
324
325 reporter->Report( _( "Done." ), RPT_SEVERITY_INFO );
326
327 return true;
328}
329
330
332{
333 BOARD* brd = m_editFrame->GetBoard();
334 wxString msg;
335 bool singleFile = OneFileOnly();
336 bool useCSVfmt = m_formatCtrl->GetSelection() == 1;
337 bool useAuxOrigin = m_useDrillPlaceOrigin->GetValue();
338 int fullcount = 0;
339 int topSide = true;
340 int bottomSide = true;
341 bool negateBottomX = m_negateXcb->GetValue();
342
343 // Test for any footprint candidate in list.
344 {
346 topSide, bottomSide, useCSVfmt, useAuxOrigin, negateBottomX );
347 exporter.GenPositionData();
348
349 if( exporter.GetFootprintCount() == 0 )
350 {
351 wxMessageBox( _( "No footprint for automated placement." ) );
352 return false;
353 }
354 }
355
356 // Create output directory if it does not exist (also transform it in absolute form).
357 // Bail if it fails.
358
359 std::function<bool( wxString* )> textResolver =
360 [&]( wxString* token ) -> bool
361 {
362 // Handles board->GetTitleBlock() *and* board->GetProject()
363 return m_editFrame->GetBoard()->ResolveTextVar( token, 0 );
364 };
365
366 wxString path = m_outputDirectory;
367 path = ExpandTextVars( path, &textResolver );
368 path = ExpandEnvVarSubstitutions( path, nullptr );
369
370 wxFileName outputDir = wxFileName::DirName( path );
371 wxString boardFilename = m_editFrame->GetBoard()->GetFileName();
372 REPORTER* reporter = &m_messagesPanel->Reporter();
373
374 if( !EnsureFileDirectoryExists( &outputDir, boardFilename, reporter ) )
375 {
376 msg.Printf( _( "Could not write plot files to folder '%s'." ), outputDir.GetPath() );
377 DisplayError( this, msg );
378 return false;
379 }
380
381 wxFileName fn = m_editFrame->GetBoard()->GetFileName();
382 fn.SetPath( outputDir.GetPath() );
383
384 // Create the Front or Top side placement file, or a single file
385 topSide = true;
386 bottomSide = singleFile;
387
388 fn.SetName( PLACE_FILE_EXPORTER::DecorateFilename( fn.GetName(), topSide, bottomSide ) );
390
391 if( useCSVfmt )
392 {
393 fn.SetName( fn.GetName() + wxT( "-" ) + FILEEXT::FootprintPlaceFileExtension );
394 fn.SetExt( wxT( "csv" ) );
395 }
396
397 int fpcount = m_editFrame->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(), OnlySMD(),
398 ExcludeAllTH(), ExcludeDNP(), topSide, bottomSide,
399 useCSVfmt, useAuxOrigin, negateBottomX );
400 if( fpcount < 0 )
401 {
402 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
403 wxMessageBox( msg );
404 reporter->Report( msg, RPT_SEVERITY_ERROR );
405 return false;
406 }
407
408 if( singleFile )
409 msg.Printf( _( "Placement file: '%s'." ), fn.GetFullPath() );
410 else
411 msg.Printf( _( "Front (top side) placement file: '%s'." ), fn.GetFullPath() );
412
413 reporter->Report( msg, RPT_SEVERITY_ACTION );
414
415 msg.Printf( _( "Component count: %d." ), fpcount );
416 reporter->Report( msg, RPT_SEVERITY_INFO );
417
418 if( singleFile )
419 {
420 reporter->Report( _( "Done." ), RPT_SEVERITY_INFO );
421 return true;
422 }
423
424 // Create the Back or Bottom side placement file
425 fullcount = fpcount;
426 topSide = false;
427 bottomSide = true;
428 fn = brd->GetFileName();
429 fn.SetPath( outputDir.GetPath() );
430 fn.SetName( PLACE_FILE_EXPORTER::DecorateFilename( fn.GetName(), topSide, bottomSide ) );
432
433 if( useCSVfmt )
434 {
435 fn.SetName( fn.GetName() + wxT( "-" ) + FILEEXT::FootprintPlaceFileExtension );
436 fn.SetExt( wxT( "csv" ) );
437 }
438
439 fpcount = m_editFrame->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(), OnlySMD(),
440 ExcludeAllTH(), ExcludeDNP(), topSide, bottomSide,
441 useCSVfmt, useAuxOrigin, negateBottomX );
442
443 if( fpcount < 0 )
444 {
445 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
446 reporter->Report( msg, RPT_SEVERITY_ERROR );
447 wxMessageBox( msg );
448 return false;
449 }
450
451 // Display results
452 if( !singleFile )
453 {
454 msg.Printf( _( "Back (bottom side) placement file: '%s'." ), fn.GetFullPath() );
455 reporter->Report( msg, RPT_SEVERITY_ACTION );
456
457 msg.Printf( _( "Component count: %d." ), fpcount );
458 reporter->Report( msg, RPT_SEVERITY_INFO );
459 }
460
461 if( !singleFile )
462 {
463 fullcount += fpcount;
464 msg.Printf( _( "Full component count: %d." ), fullcount );
465 reporter->Report( msg, RPT_SEVERITY_INFO );
466 }
467
468 reporter->Report( _( "Done." ), RPT_SEVERITY_INFO );
469 return true;
470}
471
472
479
480
481int PCB_EDIT_FRAME::DoGenFootprintsPositionFile( const wxString& aFullFileName, bool aUnitsMM,
482 bool aOnlySMD, bool aNoTHItems, bool aExcludeDNP,
483 bool aTopSide, bool aBottomSide, bool aFormatCSV,
484 bool aUseAuxOrigin, bool aNegateBottomX )
485{
486 FILE * file = nullptr;
487
488 if( !aFullFileName.IsEmpty() )
489 {
490 file = wxFopen( aFullFileName, wxT( "wt" ) );
491
492 if( file == nullptr )
493 return -1;
494 }
495
496 std::string data;
497 PLACE_FILE_EXPORTER exporter( GetBoard(), aUnitsMM, aOnlySMD, aNoTHItems, aExcludeDNP, aTopSide,
498 aBottomSide, aFormatCSV, aUseAuxOrigin, aNegateBottomX );
499 data = exporter.GenPositionData();
500
501 // if aFullFileName is empty, the file is not created, only the
502 // count of footprints to place is returned
503 if( file )
504 {
505 // Creates a footprint position file
506 // aSide = 0 -> Back (bottom) side)
507 // aSide = 1 -> Front (top) side)
508 // aSide = 2 -> both sides
509 fputs( data.c_str(), file );
510 fclose( file );
511 }
512
513 return exporter.GetFootprintCount();
514}
515
516
518{
519 BOARD* board = m_frame->GetBoard();
520 wxFileName fn;
521
522 wxString boardFilePath = ( (wxFileName) board->GetFileName() ).GetPath();
523 wxDirDialog dirDialog( m_frame, _( "Select Output Directory" ), boardFilePath );
524
525 if( dirDialog.ShowModal() == wxID_CANCEL )
526 return 0;
527
528 fn = board->GetFileName();
529 fn.SetPath( dirDialog.GetPath() );
530 fn.SetExt( wxT( "rpt" ) );
531
532 FILE* rptfile = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
533
534 if( rptfile == nullptr )
535 {
536 wxMessageBox( wxString::Format( _( "Footprint report file created:\n'%s'." ), fn.GetFullPath() ),
537 _( "Footprint Report" ), wxICON_INFORMATION );
538
539 return 0;
540 }
541
542 std::string data;
543 PLACE_FILE_EXPORTER exporter( board, m_frame->GetUserUnits() == EDA_UNITS::MM,
544 false, // aOnlySMD
545 false, // aNoTHItems
546 false, // aExcludeDNP
547 true, true, // aTopSide, aBottomSide
548 false, // aFormatCSV
549 true, // aUseAuxOrigin
550 false ); // aNegateBottomX
551 data = exporter.GenReportData();
552
553 fputs( data.c_str(), rptfile );
554 fclose( rptfile );
555
556 wxMessageBox( wxString::Format( _( "Footprint report file created:\n'%s'." ), fn.GetFullPath() ),
557 _( "Footprint Report" ), wxICON_INFORMATION );
558
559 return 0;
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
const wxString & GetFileName() const
Definition board.h:354
DIALOG_GEN_FOOTPRINT_POSITION_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Generate Placement Files"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
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
void SetupStandardButtons(std::map< int, wxString > aLabels={})
std::string m_hash_key
int ShowModal() override
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:386
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
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition tool_base.h:186
Generic, UI-independent tool event.
Definition tool_event.h:171
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 of file extensions used in Kicad.