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 <wxPcbStruct.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 <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 <module_editor_frame.h>
#include <modview_frame.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 60 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 85 of file pcbnew/files.cpp.

References DIM, IO_MGR::EAGLE, EaglePcbFileWildcard, fileFilters, IO_MGR::KICAD, KICTL_EAGLE_BRD, IO_MGR::LEGACY, LegacyPcbFileWildcard, IO_MGR::PCAD, PCadPcbFileWildcard, and PcbFileWildcard.

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

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

References KiCadPcbFileExtension, and PcbFileWildcard.

Referenced by PCB_EDIT_FRAME::Files_io_from_id().

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

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

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

Referenced by PCB_EDIT_FRAME::SavePcbFile().

642 {
643  wxFileName fn = aFileName;
644  wxFileName backupFileName = aFileName;
645 
646  backupFileName.SetExt( fn.GetExt() + backupSuffix );
647 
648  // If an old backup file exists, delete it. If an old board file exists,
649  // rename it to the backup file name.
650  if( fn.FileExists() )
651  {
652  // Remove the old file xxx.000 if it exists.
653  if( backupFileName.FileExists() )
654  wxRemoveFile( backupFileName.GetFullPath() );
655 
656  // Rename the current file from <xxx>.kicad_pcb to <xxx>.kicad_pcb-bak
657  if( !wxRenameFile( fn.GetFullPath(), backupFileName.GetFullPath() ) )
658  {
659  wxString msg = wxString::Format( _(
660  "Warning: unable to create backup file '%s'" ),
661  GetChars( backupFileName.GetFullPath() )
662  );
663  DisplayError( NULL, msg );
664  }
665  }
666  else
667  {
668  backupFileName.Clear();
669  }
670 
671  return backupFileName.GetFullPath();
672 }
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:69
IO_MGR::PCB_FILE_T plugin_type ( const wxString &  aFileName,
int  aCtl 
)

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

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

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

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

Variable Documentation

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