KiCad PCB EDA Suite
pcbnew/files.cpp File Reference

Read and write board files. More...

#include <fctsys.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <pcb_edit_frame.h>
#include <macros.h>
#include <3d_viewer/eda_3d_viewer.h>
#include <richio.h>
#include <filter_reader.h>
#include <pgm_base.h>
#include <msgpanel.h>
#include <fp_lib_table.h>
#include <ratsnest_data.h>
#include <kiway.h>
#include <kiway_player.h>
#include <trace_helpers.h>
#include <pcbnew.h>
#include <pcbnew_id.h>
#include <io_mgr.h>
#include <wildcards_and_files_ext.h>
#include <class_board.h>
#include <build_version.h>
#include <wx/stdpaths.h>

Go to the source code of this file.

Macros

#define USE_INSTRUMENTATION   0
 

Functions

bool AskLoadBoardFileName (wxWindow *aParent, int *aCtl, wxString *aFileName, bool aKicadFilesOnly)
 Function AskLoadBoardFileName puts up a wxFileDialog asking for a BOARD filename to open. More...
 
bool AskSaveBoardFileName (wxWindow *aParent, wxString *aFileName)
 Function AskSaveBoardFileName puts up a wxFileDialog asking for a BOARD filename to save. More...
 
IO_MGR::PCB_FILE_T plugin_type (const wxString &aFileName, int aCtl)
 
static wxString create_backup_file (const wxString &aFileName)
 

Variables

static const wxChar backupSuffix [] = wxT( "-bak" )
 
static const wxChar autosavePrefix [] = wxT( "_autosave-" )
 

Detailed Description

Read and write board files.

Definition in file pcbnew/files.cpp.

Macro Definition Documentation

#define USE_INSTRUMENTATION   0

Definition at line 61 of file pcbnew/files.cpp.

Function Documentation

bool AskLoadBoardFileName ( wxWindow *  aParent,
int *  aCtl,
wxString *  aFileName,
bool  aKicadFilesOnly 
)

Function AskLoadBoardFileName puts up a wxFileDialog asking for a BOARD filename to open.

Parameters
aParentis a wxFrame passed to wxFileDialog.
aCtlis where to put the OpenProjectFiles() control bits.
aFileNameon entry is a probable choice, on return is the chosen filename.
aKicadFilesOnlytrue to list kiacad pcb files plugins only, false to list import plugins.
Returns
bool - true if chosen, else false if user aborted.

Definition at line 86 of file pcbnew/files.cpp.

References DIM, IO_MGR::EAGLE, EaglePcbFileWildcard(), fileFilters, IO_MGR::KICAD_SEXP, KICTL_EAGLE_BRD, IO_MGR::LEGACY, LegacyPcbFileWildcard(), name, IO_MGR::PCAD, PCadPcbFileWildcard(), and PcbFileWildcard().

Referenced by PCBNEW_CONTROL::AppendBoardFromFile(), PCB_EDIT_FRAME::Files_io_from_id(), and PCB_EDIT_FRAME::OnFileHistory().

87 {
88  // This is a subset of all PLUGINs which are trusted to be able to
89  // load a BOARD. User may occasionally use the wrong plugin to load a
90  // *.brd file (since both legacy and eagle use *.brd extension),
91  // but eventually *.kicad_pcb will be more common than legacy *.brd files.
92  static const struct
93  {
94  const wxString& filter;
95  IO_MGR::PCB_FILE_T pluginType;
96  } loaders[] =
97  {
98  { PcbFileWildcard(), IO_MGR::KICAD_SEXP }, // Current Kicad board files
99  { LegacyPcbFileWildcard(), IO_MGR::LEGACY }, // Old Kicad board files
100  { EaglePcbFileWildcard(), IO_MGR::EAGLE }, // Import board files
101  { PCadPcbFileWildcard(), IO_MGR::PCAD }, // Import board files
102  };
103 
104  wxFileName fileName( *aFileName );
105  wxString fileFilters;
106 
107  if( aKicadFilesOnly )
108  {
109  for( unsigned ii = 0; ii < 2; ++ii )
110  {
111  if( !fileFilters.IsEmpty() )
112  fileFilters += wxChar( '|' );
113 
114  fileFilters += wxGetTranslation( loaders[ii].filter );
115  }
116  }
117  else
118  {
119  for( unsigned ii = 2; ii < DIM( loaders ); ++ii )
120  {
121  if( !fileFilters.IsEmpty() )
122  fileFilters += wxChar( '|' );
123 
124  fileFilters += wxGetTranslation( loaders[ii].filter );
125  }
126  }
127 
128  wxString path;
129  wxString name;
130 
131  if( fileName.FileExists() )
132  {
133  path = fileName.GetPath();
134  name = fileName.GetFullName();
135  }
136  else
137  {
138  path = wxStandardPaths::Get().GetDocumentsDir();
139  // leave name empty
140  }
141 
142  wxFileDialog dlg( aParent,
143  aKicadFilesOnly ? _( "Open Board File" ) : _( "Import Non KiCad Board File" ),
144  path, name, fileFilters,
145  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
146 
147  if( dlg.ShowModal() == wxID_OK )
148  {
149  // For import option, if Eagle (*.brd files), tell OpenProjectFiles() to use Eagle plugin.
150  // It's the only special case because of the duplicate use of the *.brd file extension.
151  // Other cases are clear because of unique file extensions.
152  *aCtl = aKicadFilesOnly ? 0 : KICTL_EAGLE_BRD;
153  *aFileName = dlg.GetPath();
154  return true;
155  }
156  else
157  return false;
158 }
#define DIM(x)
of elements in an array
Definition: macros.h:98
wxString EaglePcbFileWildcard()
#define KICTL_EAGLE_BRD
chosen *.brd file is Eagle according to user.
Definition: kiway_player.h:130
static const struct @13 fileFilters[FILTER_COUNT]
wxString LegacyPcbFileWildcard()
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:54
const char * name
Definition: DXF_plotter.cpp:61
wxString PcbFileWildcard()
PCB_FILE_T
Enum PCB_FILE_T is a set of file types that the IO_MGR knows about, and for which there has been a pl...
Definition: io_mgr.h:52
wxString PCadPcbFileWildcard()
S-expression Pcbnew file format.
Definition: io_mgr.h:55
bool AskSaveBoardFileName ( wxWindow *  aParent,
wxString *  aFileName 
)

Function AskSaveBoardFileName puts up a wxFileDialog asking for a BOARD filename to save.

Parameters
aParentis a wxFrame passed to wxFileDialog.
aFileNameon entry is a probable choice, on return is the chosen full filename (includes path).
Returns
bool - true if chosen, else false if user aborted.

Definition at line 171 of file pcbnew/files.cpp.

References KiCadPcbFileExtension, and PcbFileWildcard().

Referenced by PCB_EDIT_FRAME::Files_io_from_id().

172 {
173  wxString wildcard = PcbFileWildcard();
174  wxFileName fn = *aFileName;
175 
176  fn.SetExt( KiCadPcbFileExtension );
177 
178  wxFileDialog dlg( aParent,
179  _( "Save Board File As" ),
180  fn.GetPath(),
181  fn.GetFullName(),
182  wildcard,
183  wxFD_SAVE | wxFD_OVERWRITE_PROMPT
184  );
185 
186  if( dlg.ShowModal() != wxID_OK )
187  return false;
188 
189  fn = dlg.GetPath();
190 
191  // always enforce filename extension, user may not have entered it.
192  fn.SetExt( KiCadPcbFileExtension );
193 
194  *aFileName = fn.GetFullPath();
195 
196  return true;
197 }
wxString PcbFileWildcard()
const wxString KiCadPcbFileExtension
static wxString create_backup_file ( const wxString &  aFileName)
static

Definition at line 610 of file pcbnew/files.cpp.

References backupSuffix, DisplayError(), Format(), and GetChars().

Referenced by PCB_EDIT_FRAME::SavePcbFile().

611 {
612  wxFileName fn = aFileName;
613  wxFileName backupFileName = aFileName;
614 
615  backupFileName.SetExt( fn.GetExt() + backupSuffix );
616 
617  // If an old backup file exists, delete it. If an old board file exists,
618  // rename it to the backup file name.
619  if( fn.FileExists() )
620  {
621  // Remove the old file xxx.000 if it exists.
622  if( backupFileName.FileExists() )
623  wxRemoveFile( backupFileName.GetFullPath() );
624 
625  // Rename the current file from <xxx>.kicad_pcb to <xxx>.kicad_pcb-bak
626  if( !wxRenameFile( fn.GetFullPath(), backupFileName.GetFullPath() ) )
627  {
628  wxString msg = wxString::Format( _(
629  "Warning: unable to create backup file \"%s\"" ),
630  GetChars( backupFileName.GetFullPath() )
631  );
632  DisplayError( NULL, msg );
633  }
634  }
635  else
636  {
637  backupFileName.Clear();
638  }
639 
640  return backupFileName.GetFullPath();
641 }
static const wxChar backupSuffix[]
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:185
IO_MGR::PCB_FILE_T plugin_type ( const wxString &  aFileName,
int  aCtl 
)

Definition at line 374 of file pcbnew/files.cpp.

References backupSuffix, IO_MGR::EAGLE, IO_MGR::GetFileExtension(), IO_MGR::KICAD_SEXP, KICTL_EAGLE_BRD, IO_MGR::LEGACY, and IO_MGR::PCAD.

Referenced by PCB_EDIT_FRAME::AppendBoardFile(), PCBNEW_CONTROL::AppendBoardFromFile(), and PCB_EDIT_FRAME::OpenProjectFiles().

375 {
376  IO_MGR::PCB_FILE_T pluginType;
377 
378  wxFileName fn = aFileName;
379 
380  // Note: file extensions are expected to be in ower case.
381  // This is not always true, especially when importing files, so the string
382  // comparisons are case insensitive to try to find the suitable plugin.
383 
384  if( fn.GetExt().CmpNoCase( IO_MGR::GetFileExtension( IO_MGR::LEGACY ) ) == 0 )
385  {
386  // both legacy and eagle share a common file extension.
387  pluginType = ( aCtl & KICTL_EAGLE_BRD ) ? IO_MGR::EAGLE : IO_MGR::LEGACY;
388  }
389  else if( fn.GetExt().CmpNoCase( IO_MGR::GetFileExtension( IO_MGR::LEGACY ) + backupSuffix ) == 0 )
390  {
391  pluginType = IO_MGR::LEGACY;
392  }
393  else if( fn.GetExt().CmpNoCase( IO_MGR::GetFileExtension( IO_MGR::PCAD ) ) == 0 )
394  {
395  pluginType = IO_MGR::PCAD;
396  }
397  else
398  {
399  pluginType = IO_MGR::KICAD_SEXP;
400  }
401 
402  return pluginType;
403 }
#define KICTL_EAGLE_BRD
chosen *.brd file is Eagle according to user.
Definition: kiway_player.h:130
static const wxChar backupSuffix[]
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:54
PCB_FILE_T
Enum PCB_FILE_T is a set of file types that the IO_MGR knows about, and for which there has been a pl...
Definition: io_mgr.h:52
S-expression Pcbnew file format.
Definition: io_mgr.h:55
static const wxString GetFileExtension(PCB_FILE_T aFileType)
Function GetFileExtension returns the file extension for aFileType.
Definition: io_mgr.cpp:109

Variable Documentation

const wxChar autosavePrefix[] = wxT( "_autosave-" )
static
const wxChar backupSuffix[] = wxT( "-bak" )
static