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