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 (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 <pcb_edit_frame.h>
31#include <pcbnew_settings.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
53{
54public:
57 m_parent( aParent )
58 {
59 m_messagesPanel->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
61 initDialog();
62
63 SetupStandardButtons( { { wxID_OK, _( "Generate Position File" ) },
64 { wxID_CANCEL, _( "Close" ) } } );
65
66 GetSizer()->SetSizeHints(this);
67 Centre();
68 }
69
70private:
71 void initDialog();
72 void OnOutputDirectoryBrowseClicked( wxCommandEvent& event ) override;
73 void OnGenerate( wxCommandEvent& event ) override;
74
75 void onUpdateUIUnits( wxUpdateUIEvent& event ) override
76 {
77 m_radioBoxUnits->Enable( m_rbFormat->GetSelection() != 2 );
78 }
79
80 void onUpdateUIFileOpt( wxUpdateUIEvent& event ) override
81 {
82 m_radioBoxFilesCount->Enable( m_rbFormat->GetSelection() != 2 );
83 }
84
85 void onUpdateUIOnlySMD( wxUpdateUIEvent& event ) override
86 {
87 if( m_rbFormat->GetSelection() == 2 )
88 {
89 m_onlySMD->SetValue( false );
90 m_onlySMD->Enable( false );
91 }
92 else
93 {
94 m_onlySMD->Enable( true );
95 }
96 }
97
98 void onUpdateUInegXcoord( wxUpdateUIEvent& event ) override
99 {
100 if( m_rbFormat->GetSelection() == 2 )
101 {
102 m_negateXcb->SetValue( false );
103 m_negateXcb->Enable( false );
104 }
105 else
106 {
107 m_negateXcb->Enable( true );
108 }
109 }
110
111 void onUpdateUIExcludeTH( wxUpdateUIEvent& event ) override
112 {
113 if( m_rbFormat->GetSelection() == 2 )
114 {
115 m_excludeTH->SetValue( false );
116 m_excludeTH->Enable( false );
117 }
118 else
119 {
120 m_excludeTH->Enable( true );
121 }
122 }
123
124 void onUpdateUIincludeBoardEdge( wxUpdateUIEvent& event ) override
125 {
126 m_cbIncludeBoardEdge->Enable( m_rbFormat->GetSelection() == 2 );
127 }
128
132 bool CreateAsciiFiles();
133
137 bool CreateGerberFiles();
138
139 // accessors to options:
140 bool UnitsMM()
141 {
142 return m_radioBoxUnits->GetSelection() == 1;
143 }
144
146 {
147 return m_radioBoxFilesCount->GetSelection() == 1;
148 }
149
150 bool OnlySMD()
151 {
152 return m_onlySMD->GetValue();
153 }
154
156 {
157 return m_excludeTH->GetValue();
158 }
159
161 {
162 return m_excludeDNP->GetValue();
163 }
164
165private:
168};
169
170
171
173{
174 m_browseButton->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
175
176 PROJECT_FILE& projectFile = m_parent->Prj().GetProjectFile();
178
179 m_units = cfg->m_PlaceFile.units == 0 ? EDA_UNITS::INCHES : EDA_UNITS::MILLIMETRES;
180
181 // Output directory
182 m_outputDirectoryName->SetValue( projectFile.m_PcbLastPath[ LAST_PATH_POS_FILES ] );
183
184 // Update Options
185 m_radioBoxUnits->SetSelection( cfg->m_PlaceFile.units );
186 m_radioBoxFilesCount->SetSelection( cfg->m_PlaceFile.file_options );
187 m_rbFormat->SetSelection( cfg->m_PlaceFile.file_format );
190 m_onlySMD->SetValue( cfg->m_PlaceFile.only_SMD );
191 m_negateXcb->SetValue( cfg->m_PlaceFile.negate_xcoord );
192 m_excludeTH->SetValue( cfg->m_PlaceFile.exclude_TH );
193
194 // Update sizes and sizers:
195 m_messagesPanel->MsgPanelSetMinSize( wxSize( -1, 160 ) );
196 GetSizer()->SetSizeHints( this );
197}
198
199
201{
202 // Build the absolute path of current output directory to preselect it in the file browser.
203 wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
204 path = Prj().AbsolutePath( path );
205
206 wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
207
208 if( dirDialog.ShowModal() == wxID_CANCEL )
209 return;
210
211 wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
212
213 wxMessageDialog dialog( this, _( "Use a relative path?"), _( "Plot Output Directory" ),
214 wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
215
216 if( dialog.ShowModal() == wxID_YES )
217 {
218 wxString boardFilePath = ( (wxFileName) m_parent->GetBoard()->GetFileName() ).GetPath();
219
220 if( !dirName.MakeRelativeTo( boardFilePath ) )
221 {
222 wxMessageBox( _( "Cannot make path relative (target volume different from board "
223 "file volume)!" ),
224 _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
225 }
226 }
227
228 m_outputDirectoryName->SetValue( dirName.GetFullPath() );
229}
230
231
232void DIALOG_GEN_FOOTPRINT_POSITION::OnGenerate( wxCommandEvent& event )
233{
234 m_units = m_radioBoxUnits->GetSelection() == 0 ? EDA_UNITS::INCHES : EDA_UNITS::MILLIMETRES;
235
237
238 wxString dirStr = m_outputDirectoryName->GetValue();
239 // Keep unix directory format convention in cfg files
240 dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
241
243 cfg->m_PlaceFile.output_directory = dirStr;
244 cfg->m_PlaceFile.units = m_units == EDA_UNITS::INCHES ? 0 : 1;
245 cfg->m_PlaceFile.file_options = m_radioBoxFilesCount->GetSelection();
246 cfg->m_PlaceFile.file_format = m_rbFormat->GetSelection();
248 cfg->m_PlaceFile.exclude_TH = m_excludeTH->GetValue();
249 cfg->m_PlaceFile.only_SMD = m_onlySMD->GetValue();
251 cfg->m_PlaceFile.negate_xcoord = m_negateXcb->GetValue();
252
253 if( m_rbFormat->GetSelection() == 2 )
255 else
257}
258
259
261{
262 BOARD* brd = m_parent->GetBoard();
263 wxString msg;
264 int fullcount = 0;
265
266 // Create output directory if it does not exist (also transform it in absolute form).
267 // Bail if it fails.
268
269 std::function<bool( wxString* )> textResolver =
270 [&]( wxString* token ) -> bool
271 {
272 // Handles board->GetTitleBlock() *and* board->GetProject()
273 return m_parent->GetBoard()->ResolveTextVar( token, 0 );
274 };
275
277 path = ExpandTextVars( path, &textResolver );
278 path = ExpandEnvVarSubstitutions( path, nullptr );
279
280 wxFileName outputDir = wxFileName::DirName( path );
281 wxString boardFilename = m_parent->GetBoard()->GetFileName();
282
284
285 if( !EnsureFileDirectoryExists( &outputDir, boardFilename, m_reporter ) )
286 {
287 msg.Printf( _( "Could not write plot files to folder '%s'." ), outputDir.GetPath() );
288 DisplayError( this, msg );
289 return false;
290 }
291
292 wxFileName fn = m_parent->GetBoard()->GetFileName();
293 fn.SetPath( outputDir.GetPath() );
294
295 // Create the Front and Top side placement files. Gerber P&P files are always separated.
296 // Not also they include all footprints
297 PLACEFILE_GERBER_WRITER exporter( brd );
298 wxString filename = exporter.GetPlaceFileName( fn.GetFullPath(), F_Cu );
299
300 int fpcount = exporter.CreatePlaceFile( filename, F_Cu, m_cbIncludeBoardEdge->GetValue() );
301
302 if( fpcount < 0 )
303 {
304 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
305 wxMessageBox( msg );
307 return false;
308 }
309
310 msg.Printf( _( "Front (top side) placement file: '%s'." ), filename );
312
313 msg.Printf( _( "Component count: %d." ), fpcount );
315
316 // Create the Back or Bottom side placement file
317 fullcount = fpcount;
318
319 filename = exporter.GetPlaceFileName( fn.GetFullPath(), B_Cu );
320
321 fpcount = exporter.CreatePlaceFile( filename, B_Cu, m_cbIncludeBoardEdge->GetValue() );
322
323 if( fpcount < 0 )
324 {
325 msg.Printf( _( "Failed to create file '%s'." ), filename );
327 wxMessageBox( msg );
328 return false;
329 }
330
331 // Display results
332 msg.Printf( _( "Back (bottom side) placement file: '%s'." ), filename );
334
335 msg.Printf( _( "Component count: %d." ), fpcount );
337
338 fullcount += fpcount;
339 msg.Printf( _( "Full component count: %d." ), fullcount );
341
342 m_reporter->Report( _( "File generation successful." ), RPT_SEVERITY_INFO );
343
344 return true;
345}
346
347
349{
350 BOARD * brd = m_parent->GetBoard();
351 wxString msg;
352 bool singleFile = OneFileOnly();
353 bool useCSVfmt = m_rbFormat->GetSelection() == 1;
354 bool useAuxOrigin = m_useDrillPlaceOrigin->GetValue();
355 int fullcount = 0;
356 int topSide = true;
357 int bottomSide = true;
358 bool negateBottomX = m_negateXcb->GetValue();
359
360 // Test for any footprint candidate in list.
361 {
363 topSide, bottomSide, useCSVfmt, useAuxOrigin, negateBottomX );
364 exporter.GenPositionData();
365
366 if( exporter.GetFootprintCount() == 0 )
367 {
368 wxMessageBox( _( "No footprint for automated placement." ) );
369 return false;
370 }
371 }
372
373 // Create output directory if it does not exist (also transform it in absolute form).
374 // Bail if it fails.
375
376 std::function<bool( wxString* )> textResolver =
377 [&]( wxString* token ) -> bool
378 {
379 // Handles board->GetTitleBlock() *and* board->GetProject()
380 return m_parent->GetBoard()->ResolveTextVar( token, 0 );
381 };
382
384 path = ExpandTextVars( path, &textResolver );
385 path = ExpandEnvVarSubstitutions( path, nullptr );
386
387 wxFileName outputDir = wxFileName::DirName( path );
388 wxString boardFilename = m_parent->GetBoard()->GetFileName();
389
391
392 if( !EnsureFileDirectoryExists( &outputDir, boardFilename, m_reporter ) )
393 {
394 msg.Printf( _( "Could not write plot files to folder '%s'." ), outputDir.GetPath() );
395 DisplayError( this, msg );
396 return false;
397 }
398
399 wxFileName fn = m_parent->GetBoard()->GetFileName();
400 fn.SetPath( outputDir.GetPath() );
401
402 // Create the Front or Top side placement file, or a single file
403 topSide = true;
404 bottomSide = false;
405
406 if( singleFile )
407 {
408 bottomSide = true;
409 fn.SetName( fn.GetName() + wxT( "-" ) + wxT( "all" ) );
410 }
411 else
412 {
413 fn.SetName( fn.GetName() + wxT( "-" ) + PLACE_FILE_EXPORTER::GetFrontSideName().c_str() );
414 }
415
416
417 if( useCSVfmt )
418 {
419 fn.SetName( fn.GetName() + wxT( "-" ) + FILEEXT::FootprintPlaceFileExtension );
420 fn.SetExt( wxT( "csv" ) );
421 }
422 else
423 {
425 }
426
427 int fpcount = m_parent->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(), OnlySMD(),
428 ExcludeAllTH(), ExcludeDNP(), topSide,
429 bottomSide, useCSVfmt, useAuxOrigin,
430 negateBottomX );
431 if( fpcount < 0 )
432 {
433 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
434 wxMessageBox( msg );
436 return false;
437 }
438
439 if( singleFile )
440 msg.Printf( _( "Placement file: '%s'." ), fn.GetFullPath() );
441 else
442 msg.Printf( _( "Front (top side) placement file: '%s'." ), fn.GetFullPath() );
443
445
446 msg.Printf( _( "Component count: %d." ), fpcount );
448
449 if( singleFile )
450 {
451 m_reporter->Report( _( "File generation successful." ), RPT_SEVERITY_INFO );
452 return true;
453 }
454
455 // Create the Back or Bottom side placement file
456 fullcount = fpcount;
457 topSide = false;
458 bottomSide = true;
459 fn = brd->GetFileName();
460 fn.SetPath( outputDir.GetPath() );
461 fn.SetName( fn.GetName() + wxT( "-" ) + PLACE_FILE_EXPORTER::GetBackSideName().c_str() );
462
463 if( useCSVfmt )
464 {
465 fn.SetName( fn.GetName() + wxT( "-" ) + FILEEXT::FootprintPlaceFileExtension );
466 fn.SetExt( wxT( "csv" ) );
467 }
468 else
469 {
471 }
472
473 fpcount = m_parent->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(), OnlySMD(),
474 ExcludeAllTH(), ExcludeDNP(), topSide,
475 bottomSide, useCSVfmt,
476 useAuxOrigin, negateBottomX );
477
478 if( fpcount < 0 )
479 {
480 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
482 wxMessageBox( msg );
483 return false;
484 }
485
486 // Display results
487 if( !singleFile )
488 {
489 msg.Printf( _( "Back (bottom side) placement file: '%s'." ), fn.GetFullPath() );
491
492 msg.Printf( _( "Component count: %d." ), fpcount );
494 }
495
496 if( !singleFile )
497 {
498 fullcount += fpcount;
499 msg.Printf( _( "Full component count: %d." ), fullcount );
501 }
502
503 m_reporter->Report( _( "File generation successful." ), RPT_SEVERITY_INFO );
504 return true;
505}
506
507
509{
510 DIALOG_GEN_FOOTPRINT_POSITION dlg( getEditFrame<PCB_EDIT_FRAME>() );
511 dlg.ShowModal();
512 return 0;
513}
514
515
516int PCB_EDIT_FRAME::DoGenFootprintsPositionFile( const wxString& aFullFileName, bool aUnitsMM,
517 bool aOnlySMD, bool aNoTHItems, bool aExcludeDNP,
518 bool aTopSide, bool aBottomSide, bool aFormatCSV,
519 bool aUseAuxOrigin, bool aNegateBottomX )
520{
521 FILE * file = nullptr;
522
523 if( !aFullFileName.IsEmpty() )
524 {
525 file = wxFopen( aFullFileName, wxT( "wt" ) );
526
527 if( file == nullptr )
528 return -1;
529 }
530
531 std::string data;
532 PLACE_FILE_EXPORTER exporter( GetBoard(), aUnitsMM, aOnlySMD, aNoTHItems, aExcludeDNP, aTopSide,
533 aBottomSide, aFormatCSV, aUseAuxOrigin, aNegateBottomX );
534 data = exporter.GenPositionData();
535
536 // if aFullFileName is empty, the file is not created, only the
537 // count of footprints to place is returned
538 if( file )
539 {
540 // Creates a footprint position file
541 // aSide = 0 -> Back (bottom) side)
542 // aSide = 1 -> Front (top) side)
543 // aSide = 2 -> both sides
544 fputs( data.c_str(), file );
545 fclose( file );
546 }
547
548 return exporter.GetFootprintCount();
549}
550
551
552void PCB_EDIT_FRAME::GenFootprintsReport( wxCommandEvent& event )
553{
554 wxFileName fn;
555
556 wxString boardFilePath = ( (wxFileName) GetBoard()->GetFileName() ).GetPath();
557 wxDirDialog dirDialog( this, _( "Select Output Directory" ), boardFilePath );
558
559 if( dirDialog.ShowModal() == wxID_CANCEL )
560 return;
561
562 fn = GetBoard()->GetFileName();
563 fn.SetPath( dirDialog.GetPath() );
564 fn.SetExt( wxT( "rpt" ) );
565
566 bool unitMM = GetUserUnits() == EDA_UNITS::MILLIMETRES;
567 bool success = DoGenFootprintsReport( fn.GetFullPath(), unitMM );
568
569 wxString msg;
570
571 if( success )
572 {
573 msg.Printf( _( "Footprint report file created:\n'%s'." ), fn.GetFullPath() );
574 wxMessageBox( msg, _( "Footprint Report" ), wxICON_INFORMATION );
575 }
576 else
577 {
578 msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
579 DisplayError( this, msg );
580 }
581}
582
583
584bool PCB_EDIT_FRAME::DoGenFootprintsReport( const wxString& aFullFilename, bool aUnitsMM )
585{
586 FILE* rptfile = wxFopen( aFullFilename, wxT( "wt" ) );
587
588 if( rptfile == nullptr )
589 return false;
590
591 std::string data;
592 PLACE_FILE_EXPORTER exporter( GetBoard(), aUnitsMM,
593 false, false, // SMD aOnlySMD, aNoTHItems
594 false, // aExcludeDNP
595 true, true, // aTopSide, aBottomSide
596 false, true, false // aFormatCSV, aUseAuxOrigin, aNegateBottomX
597 );
598 data = exporter.GenReportData();
599
600 fputs( data.c_str(), rptfile );
601 fclose( rptfile );
602
603 return true;
604}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
int GeneratePosFile(const TOOL_EVENT &aEvent)
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:282
bool ResolveTextVar(wxString *token, int aDepth) const
Definition: board.cpp:421
const wxString & GetFileName() const
Definition: board.h:319
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:209
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 aExcludeDNP, 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,...
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:70
wxString m_PcbLastPath[LAST_PATH_SIZE]
MRU path storage.
Definition: project_file.h:157
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:166
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:320
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 wxBitmapBundle &aBmp)
Generic, UI-independent tool event.
Definition: tool_event.h:167
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:334
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:362
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:161
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:95
@ F_Cu
Definition: layer_ids.h:64
int GetUserUnits()
Return the currently selected user unit value for the interface.
@ LAST_PATH_POS_FILES
Definition: project_file.h:55
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
Definition of file extensions used in Kicad.