KiCad PCB EDA Suite
export_footprint_associations.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) 1992-2021 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 #include <confirm.h>
25 #include <kicad_string.h>
26 #include <macros.h>
27 #include <pcb_edit_frame.h>
28 #include <board.h>
29 #include <footprint.h>
30 #include <project.h>
32 #include <wx/filedlg.h>
33 
34 
35 
36 bool RecreateCmpFile( BOARD * aBrd, const wxString& aFullCmpFileName )
37 {
38  FILE* cmpFile = wxFopen( aFullCmpFileName, wxT( "wt" ) );
39 
40  if( cmpFile == nullptr )
41  return false;
42 
43  fprintf( cmpFile, "Cmp-Mod V01 Created by PcbNew date = %s\n", TO_UTF8( DateAndTime() ) );
44 
45  for( FOOTPRINT* fp : aBrd->Footprints() )
46  {
47  fprintf( cmpFile, "\nBeginCmp\n" );
48  fprintf( cmpFile, "TimeStamp = %s\n", TO_UTF8( fp->m_Uuid.AsString() ) );
49  fprintf( cmpFile, "Path = %s\n", TO_UTF8( fp->GetPath().AsString() ) );
50  fprintf( cmpFile, "Reference = %s;\n",
51  !fp->GetReference().IsEmpty() ? TO_UTF8( fp->GetReference() ) : "[NoRef]" );
52  fprintf( cmpFile, "ValeurCmp = %s;\n",
53  !fp->GetValue().IsEmpty() ? TO_UTF8( fp->GetValue() ) : "[NoVal]" );
54  fprintf( cmpFile, "IdModule = %s;\n", fp->GetFPID().Format().c_str() );
55  fprintf( cmpFile, "EndCmp\n" );
56  }
57 
58  fprintf( cmpFile, "\nEndListe\n" );
59  fclose( cmpFile );
60 
61  return true;
62 }
63 
64 
65 void PCB_EDIT_FRAME::RecreateCmpFileFromBoard( wxCommandEvent& aEvent )
66 {
67  // Build the .cmp file name from the board name
68  wxString projectDir = wxPathOnly( Prj().GetProjectFullName() );
69  wxFileName fn = GetBoard()->GetFileName();
70 
72 
73  wxFileDialog dlg( this, _( "Save Footprint Association File" ),
74  projectDir, fn.GetFullName(), FootprintAssignmentFileWildcard(),
75  wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
76 
77  if( dlg.ShowModal() == wxID_CANCEL )
78  return;
79 
80  wxString path = dlg.GetPath();
81 
82  if( !RecreateCmpFile( GetBoard(), path ) )
83  DisplayError( this, wxString::Format( _( "Failed to create file '%s'." ), path ) );
84 }
85 
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:252
bool RecreateCmpFile(BOARD *aBrd, const wxString &aFullCmpFileName)
void RecreateCmpFileFromBoard(wxCommandEvent &aEvent)
Recreates a .cmp file from the current loaded board.
This file is part of the common library.
const wxString & GetFileName() const
Definition: board.h:228
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
const std::string FootprintAssignmentFileExtension
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
wxString FootprintAssignmentFileWildcard()
FOOTPRINTS & Footprints()
Definition: board.h:233
Definition of file extensions used in Kicad.
#define _(s)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
BOARD * GetBoard() const
wxString DateAndTime()
Definition: string.cpp:509