kicad/pcbnew/files.cpp

625 lines
20 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2004-2010 Jean-Pierre Charras, jean-pierre.charras@gpisa-lab.inpg.fr
* Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 2010 KiCad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file pcbnew/files.cpp
* @brief Read and write board files.
*/
#include <fctsys.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <wxPcbStruct.h>
#include <macros.h>
#include <pcbcommon.h>
#include <3d_viewer.h>
#include <richio.h>
#include <filter_reader.h>
#include <appl_wxstruct.h>
#include <msgpanel.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> // LEGACY_BOARD_FILE_VERSION
static const wxString backupFileExtensionSuffix( wxT( "-bak" ) );
static const wxString autosaveFilePrefix( wxT( "_autosave-" ) );
void PCB_EDIT_FRAME::OnFileHistory( wxCommandEvent& event )
{
wxString fn;
fn = GetFileFromHistory( event.GetId(), _( "Printed circuit board" ) );
if( fn != wxEmptyString )
{
m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() );
::wxSetWorkingDirectory( ::wxPathOnly( fn ) );
LoadOnePcbFile( fn );
}
}
void PCB_EDIT_FRAME::Files_io( wxCommandEvent& event )
{
int id = event.GetId();
wxString msg;
// If an edition is in progress, stop it.
// For something else than save, get rid of current tool.
if( id == ID_SAVE_BOARD )
m_canvas->EndMouseCapture( -1, m_canvas->GetDefaultCursor() );
else
m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() );
switch( id )
{
case ID_LOAD_FILE:
2012-08-29 16:59:50 +00:00
LoadOnePcbFile( GetBoard()->GetFileName(), false, true );
break;
2012-12-21 07:58:51 +00:00
case ID_MENU_READ_BOARD_BACKUP_FILE:
case ID_MENU_RECOVER_BOARD_AUTOSAVE:
{
2012-12-21 07:58:51 +00:00
wxFileName currfn = GetBoard()->GetFileName();
wxFileName fn = currfn;
if( id == ID_MENU_RECOVER_BOARD_AUTOSAVE )
{
wxString rec_name = autosaveFilePrefix + fn.GetName();
2012-12-21 07:58:51 +00:00
fn.SetName( rec_name );
}
else
{
wxString backup_ext = fn.GetExt()+ backupFileExtensionSuffix;
fn.SetExt( backup_ext );
}
if( !fn.FileExists() )
{
msg.Printf( _( "Recovery file <%s> not found." ),
GetChars( fn.GetFullPath() ) );
DisplayInfoMessage( this, msg );
break;
}
2012-12-21 07:58:51 +00:00
msg.Printf( _( "OK to load recovery or backup file <%s>" ),
GetChars(fn.GetFullPath() ) );
if( !IsOK( this, msg ) )
break;
2012-12-21 07:58:51 +00:00
GetScreen()->ClrModify(); // do not prompt the user for changes
LoadOnePcbFile( fn.GetFullPath(), false );
2012-08-29 16:59:50 +00:00
2012-12-21 07:58:51 +00:00
// Re-set the name since name or extension was changed
GetBoard()->SetFileName( currfn.GetFullPath() );
UpdateTitle();
}
break;
case ID_APPEND_FILE:
LoadOnePcbFile( wxEmptyString, true );
break;
case ID_NEW_BOARD:
2012-08-29 16:59:50 +00:00
{
Clear_Pcb( true );
wxFileName fn( wxT( "noname" ) );
fn.AssignCwd();
fn.SetExt( PcbFileExtension );
GetBoard()->SetFileName( fn.GetFullPath() );
2012-08-29 16:59:50 +00:00
UpdateTitle();
ReCreateLayerBox( NULL );
}
break;
case ID_SAVE_BOARD:
2012-08-29 16:59:50 +00:00
SavePcbFile( GetBoard()->GetFileName() );
break;
case ID_SAVE_BOARD_AS:
SavePcbFile( wxEmptyString );
break;
default:
DisplayError( this, wxT( "File_io Internal Error" ) ); break;
}
}
bool PCB_EDIT_FRAME::LoadOnePcbFile( const wxString& aFileName, bool aAppend,
bool aForceFileDialog )
{
2010-12-29 17:47:32 +00:00
if( GetScreen()->IsModify() && !aAppend )
{
if( !IsOK( this, _( "The current board has been modified. Do you wish to discard \
the changes?" ) ) )
return false;
}
2010-12-29 17:47:32 +00:00
if( aAppend )
{
2012-08-29 16:59:50 +00:00
GetBoard()->SetFileName( wxEmptyString );
OnModify();
GetBoard()->m_Status_Pcb = 0;
}
wxFileName fileName = aFileName;
IO_MGR::PCB_FILE_T pluginType = IO_MGR::LEGACY;
// This is a subset of all PLUGINs which are trusted to be able to
// load a BOARD. Order is subject to change as KICAD plugin matures.
// User may occasionally use the wrong plugin to load a *.brd file,
// (since both legacy and eagle use *.brd extension),
// but eventually *.kicad_pcb will be more common than legacy *.brd files.
static const struct
{
const wxString& filter;
IO_MGR::PCB_FILE_T pluginType;
} loaders[] =
{
{ PcbFileWildcard, IO_MGR::KICAD },
{ LegacyPcbFileWildcard, IO_MGR::LEGACY },
{ EaglePcbFileWildcard, IO_MGR::EAGLE },
{ PCadPcbFileWildcard, IO_MGR::PCAD },
};
2008-04-29 03:18:02 +00:00
if( !fileName.IsOk() || !fileName.FileExists() || aForceFileDialog )
{
wxString name;
wxString path = wxGetCwd();
wxString fileFilters;
for( unsigned i=0; i<DIM( loaders ); ++i )
{
if( i > 0 )
fileFilters += wxChar( '|' );
fileFilters += wxGetTranslation( loaders[i].filter );
}
if( aForceFileDialog && fileName.FileExists() )
{
path = fileName.GetPath();
name = fileName.GetFullName();
}
wxFileDialog dlg( this, _( "Open Board File" ), path, name, fileFilters,
wxFD_OPEN | wxFD_FILE_MUST_EXIST );
if( dlg.ShowModal() == wxID_CANCEL )
return false;
fileName = dlg.GetPath();
int chosenFilter = dlg.GetFilterIndex();
pluginType = loaders[chosenFilter].pluginType;
}
else // if a filename is given, force IO_MGR::KICAD if the file ext is kicad_pcb
// for instance if the filename comes from file history
// or it is a backup file with ext = kicad_pcb-bak
{
wxString backup_ext = IO_MGR::GetFileExtension( IO_MGR::KICAD ) +
backupFileExtensionSuffix;
if( fileName.GetExt() == IO_MGR::GetFileExtension( IO_MGR::KICAD ) ||
fileName.GetExt() == backup_ext )
pluginType = IO_MGR::KICAD;
}
2008-04-29 03:18:02 +00:00
PLUGIN::RELEASER pi( IO_MGR::PluginFind( pluginType ) );
if( !fileName.HasExt() )
fileName.SetExt( pi->GetFileExtension() );
2010-12-29 17:47:32 +00:00
if( !aAppend )
{
if( !wxGetApp().LockFile( fileName.GetFullPath() ) )
{
DisplayError( this, _( "This file is already open." ) );
return false;
}
2008-04-29 03:18:02 +00:00
Clear_Pcb( false ); // pass false since we prompted above for a modified board
}
2008-04-29 03:18:02 +00:00
CheckForAutoSaveFile( fileName, fileName.GetExt() );
2012-08-29 16:59:50 +00:00
GetBoard()->SetFileName( fileName.GetFullPath() );
if( !aAppend )
{
// Update the option toolbar
m_DisplayPcbTrackFill = DisplayOpt.DisplayPcbTrackFill;
m_DisplayModText = DisplayOpt.DisplayModText;
m_DisplayModEdge = DisplayOpt.DisplayModEdge;
m_DisplayPadFill = DisplayOpt.DisplayPadFill;
m_DisplayViaFill = DisplayOpt.DisplayViaFill;
// load project settings before BOARD, in case BOARD file has overrides.
2012-08-29 16:59:50 +00:00
LoadProjectSettings( GetBoard()->GetFileName() );
}
2011-12-15 02:24:52 +00:00
else
{
GetBoard()->m_NetClasses.Clear();
}
BOARD* loadedBoard = 0; // it will be set to non-NULL if loaded OK
try
{
PROPERTIES props;
// EAGLE_PLUGIN can use this info to center the BOARD, but it does not yet.
props["page_width"] = wxString::Format( wxT( "%d" ), GetPageSizeIU().x );
props["page_height"] = wxString::Format( wxT( "%d" ), GetPageSizeIU().y );
// load or append either:
2012-08-29 16:59:50 +00:00
loadedBoard = pi->Load( GetBoard()->GetFileName(), aAppend ? GetBoard() : NULL, &props );
// the Load plugin method makes a 'fresh' board, so we need to
// set its own name
GetBoard()->SetFileName( fileName.GetFullPath() );
if( !aAppend )
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
2011-12-05 06:15:33 +00:00
{
if( pluginType == IO_MGR::LEGACY &&
loadedBoard->GetFileFormatVersionAtLoad() < LEGACY_BOARD_FILE_VERSION )
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
2011-12-05 06:15:33 +00:00
{
DisplayInfoMessage( this,
_( "This file was created by an older version of Pcbnew.\
\nIt will be stored in the new file format when you save this file again." ) );
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
2011-12-05 06:15:33 +00:00
}
2011-12-15 02:24:52 +00:00
SetBoard( loadedBoard );
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
2011-12-05 06:15:33 +00:00
}
}
catch( IO_ERROR ioe )
2011-12-28 14:15:00 +00:00
{
wxString msg = wxString::Format( _( "Error loading board.\n%s" ),
ioe.errorText.GetData() );
wxMessageBox( msg, _( "Open Board File" ), wxOK | wxICON_ERROR );
}
2011-12-15 02:24:52 +00:00
if( loadedBoard )
{
// we should not ask PLUGINs to do these items:
loadedBoard->BuildListOfNets();
loadedBoard->SynchronizeNetsAndNetClasses();
SetStatusText( wxEmptyString );
BestZoom();
}
GetScreen()->ClrModify();
2012-02-06 05:44:19 +00:00
// If append option: change the initial board name to <oldname>-append.brd
2010-12-29 17:47:32 +00:00
if( aAppend )
{
2012-08-29 16:59:50 +00:00
wxString new_filename = GetBoard()->GetFileName().BeforeLast( '.' );
if ( ! new_filename.EndsWith( wxT( "-append" ) ) )
new_filename += wxT( "-append" );
new_filename += wxT( "." ) + PcbFileExtension;
OnModify();
2012-08-29 16:59:50 +00:00
GetBoard()->SetFileName( new_filename );
}
// Fix the directory separator on Windows and
// force the new file format for not Kicad boards,
// to ensure the right format when saving the board
bool converted = pluginType != IO_MGR::LEGACY && pluginType != IO_MGR::KICAD;
wxString fn;
if( converted )
fn = GetBoard()->GetFileName().BeforeLast( '.' );
else
fn = GetBoard()->GetFileName();
2012-08-29 16:59:50 +00:00
fn.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
if( converted )
fn += wxT( "." ) + PcbFileExtension;
2012-08-29 16:59:50 +00:00
GetBoard()->SetFileName( fn );
UpdateTitle();
if( !converted )
UpdateFileHistory( GetBoard()->GetFileName() );
2012-02-06 05:44:19 +00:00
// Rebuild the new pad list (for drc and ratsnet control ...)
GetBoard()->m_Status_Pcb = 0;
2012-02-06 05:44:19 +00:00
// Dick 5-Feb-2012: I do not agree with this. The layer widget will show what
2012-02-06 07:14:51 +00:00
// is visible or not, and it would be nice for the board to look like it
// did when I saved it, immediately after loading.
2012-02-06 05:44:19 +00:00
#if 0
/* Reset the items visibility flag when loading a new config
2012-02-06 05:44:19 +00:00
* Because it could creates SERIOUS mistakes for the user,
* if board items are not visible after loading a board...
* Grid and ratsnest can be left to their previous state
*/
bool showGrid = IsElementVisible( GRID_VISIBLE );
bool showRats = IsElementVisible( RATSNEST_VISIBLE );
2012-02-06 05:44:19 +00:00
SetVisibleAlls();
2012-02-06 05:44:19 +00:00
SetElementVisibility( GRID_VISIBLE, showGrid );
SetElementVisibility( RATSNEST_VISIBLE, showRats );
2012-02-06 05:44:19 +00:00
#endif
2009-12-03 09:33:48 +00:00
// Update info shown by the horizontal toolbars
GetBoard()->SetCurrentNetClass( NETCLASS::Default );
2010-01-21 07:41:30 +00:00
ReFillLayerWidget();
2009-12-03 09:33:48 +00:00
ReCreateLayerBox( NULL );
2012-02-06 05:44:19 +00:00
// upate the layer widget to match board visibility states, both layers and render columns.
syncLayerVisibilities();
syncLayerWidgetLayer();
2012-02-06 05:44:19 +00:00
syncRenderStates();
2009-12-03 09:33:48 +00:00
// Update the RATSNEST items, which were not loaded at the time
// BOARD::SetVisibleElements() was called from within any PLUGIN.
// See case RATSNEST_VISIBLE: in BOARD::SetElementVisibility()
GetBoard()->SetVisibleElements( GetBoard()->GetVisibleElements() );
updateTraceWidthSelectBox();
updateViaSizeSelectBox();
2009-06-20 19:09:43 +00:00
// Display the loaded board:
2009-12-19 19:24:49 +00:00
Zoom_Automatique( false );
2009-07-30 11:04:07 +00:00
// Compile ratsnest and displays net info
2011-04-09 10:52:14 +00:00
wxBusyCursor dummy; // Displays an Hourglass while building connectivity
Compile_Ratsnest( NULL, true );
SetMsgPanel( GetBoard() );
// Refresh the 3D view, if any
if( m_Draw3DFrame )
m_Draw3DFrame->NewDisplay();
2008-04-18 13:28:56 +00:00
#if 0 && defined(DEBUG)
// note this freezes up Pcbnew when run under the KiCad project
// manager. runs fine from command prompt. This is because the KiCad
// project manager redirects stdout of the child Pcbnew process to itself,
2008-04-18 13:28:56 +00:00
// but never reads from that pipe, and that in turn eventually blocks
// the Pcbnew program when the pipe it is writing to gets full.
2008-04-18 13:28:56 +00:00
2007-10-31 06:40:15 +00:00
// Output the board object tree to stdout, but please run from command prompt:
GetBoard()->Show( 0, std::cout );
#endif
2008-04-18 13:28:56 +00:00
return true;
}
bool PCB_EDIT_FRAME::SavePcbFile( const wxString& aFileName, bool aCreateBackupFile )
{
wxFileName backupFileName;
wxFileName pcbFileName;
2007-10-22 20:40:18 +00:00
wxString upperTxt;
wxString lowerTxt;
wxString msg;
bool saveok = true;
IO_MGR::PCB_FILE_T pluginType;
if( aFileName == wxEmptyString )
{
wxString wildcard;
wildcard << wxGetTranslation( PcbFileWildcard ) << wxChar( '|' ) <<
wxGetTranslation( LegacyPcbFileWildcard );
pcbFileName = GetBoard()->GetFileName();
// Match the default wildcard filter choice, with the inital file extension shown.
// That'll be the extension unless user changes filter dropdown listbox.
pcbFileName.SetExt( KiCadPcbFileExtension );
wxFileDialog dlg( this, _( "Save Board File As" ), wxEmptyString,
pcbFileName.GetFullPath(),
wildcard, wxFD_SAVE
/* wxFileDialog is not equipped to handle multiple wildcards and
wxFD_OVERWRITE_PROMPT both together.
| wxFD_OVERWRITE_PROMPT
*/
);
2008-04-18 13:28:56 +00:00
if( dlg.ShowModal() != wxID_OK )
return false;
2008-04-18 13:28:56 +00:00
int filterNdx = dlg.GetFilterIndex();
pluginType = ( filterNdx == 1 ) ? IO_MGR::LEGACY : IO_MGR::KICAD;
// Note: on Linux wxFileDialog is not reliable for noticing a changed filename.
// We probably need to file a bug report or implement our own derivation.
pcbFileName = dlg.GetPath();
// enforce file extension, must match plugin's policy.
pcbFileName.SetExt( IO_MGR::GetFileExtension( pluginType ) );
}
else
{
pcbFileName = aFileName;
if( pcbFileName.GetExt() == LegacyPcbFileExtension )
pluginType = IO_MGR::LEGACY;
else
{
pluginType = IO_MGR::KICAD;
pcbFileName.SetExt( KiCadPcbFileExtension );
}
}
// If changes are made, update the board date
if( GetScreen()->IsModify() )
{
TITLE_BLOCK tb = GetTitleBlock();
tb.SetDate();
SetTitleBlock( tb );
}
if( !IsWritable( pcbFileName ) )
return false;
if( aCreateBackupFile )
{
2012-02-06 05:44:19 +00:00
// Get the backup file name
backupFileName = pcbFileName;
backupFileName.SetExt( pcbFileName.GetExt() + backupFileExtensionSuffix );
// If an old backup file exists, delete it. If an old board file exists, rename
// it to the backup file name.
if( pcbFileName.FileExists() )
{
// Remove the old file xxx.000 if it exists.
if( backupFileName.FileExists() )
wxRemoveFile( backupFileName.GetFullPath() );
// Rename the "old" file" from xxx.kicad_pcb to xxx.000
if( !wxRenameFile( pcbFileName.GetFullPath(), backupFileName.GetFullPath() ) )
{
msg = _( "Warning: unable to create backup file " ) + backupFileName.GetFullPath();
DisplayError( this, msg );
saveok = false;
}
}
else
{
backupFileName.Clear();
}
}
2011-12-15 02:24:52 +00:00
GetBoard()->m_Status_Pcb &= ~CONNEXION_OK;
GetBoard()->SynchronizeNetsAndNetClasses();
// Select default Netclass before writing file.
// Useful to save default values in headers
GetBoard()->SetCurrentNetClass( GetBoard()->m_NetClasses.GetDefault()->GetName() );
try
{
PLUGIN::RELEASER pi( IO_MGR::PluginFind( pluginType ) );
/*
if( (PLUGIN*)pi == NULL )
THROW_IO_ERROR( wxString::Format( _( "cannot find file plug in for file format '%s'" ),
GetChars( pcbFileName.GetExt() ) ) );
*/
2011-12-15 02:24:52 +00:00
pi->Save( pcbFileName.GetFullPath(), GetBoard(), NULL );
}
catch( IO_ERROR ioe )
{
wxString msg = wxString::Format( _( "Error saving board.\n%s" ),
ioe.errorText.GetData() );
wxMessageBox( msg, _( "Save Board File" ), wxICON_ERROR | wxOK );
saveok = false;
}
2011-12-15 02:24:52 +00:00
if( saveok )
{
2012-08-29 16:59:50 +00:00
GetBoard()->SetFileName( pcbFileName.GetFullPath() );
2011-12-15 02:24:52 +00:00
UpdateTitle();
// Put the saved file in File History, unless aCreateBackupFile
// is false.
// aCreateBackupFile == false is mainly used to write autosave files
// and not need to have an autosave file in file history
if( aCreateBackupFile )
UpdateFileHistory( GetBoard()->GetFileName() );
2011-12-15 02:24:52 +00:00
}
2012-02-06 05:44:19 +00:00
// Display the file names:
m_messagePanel->EraseMsgBox();
if( saveok )
{
// Delete auto save file on successful save.
wxFileName autoSaveFileName = pcbFileName;
autoSaveFileName.SetName( autosaveFilePrefix + pcbFileName.GetName() );
if( autoSaveFileName.FileExists() )
wxRemoveFile( autoSaveFileName.GetFullPath() );
upperTxt = _( "Backup file: " ) + backupFileName.GetFullPath();
}
2011-12-14 07:03:55 +00:00
if( saveok )
2007-10-22 20:40:18 +00:00
lowerTxt = _( "Wrote board file: " );
else
2007-10-22 20:40:18 +00:00
lowerTxt = _( "Failed to create " );
lowerTxt += pcbFileName.GetFullPath();
ClearMsgPanel();
AppendMsgPanel( upperTxt, lowerTxt, CYAN );
2008-04-18 13:28:56 +00:00
GetScreen()->ClrSave();
GetScreen()->ClrModify();
return true;
}
bool PCB_EDIT_FRAME::doAutoSave()
{
2012-08-29 16:59:50 +00:00
wxFileName tmpFileName = GetBoard()->GetFileName();
wxFileName fn = tmpFileName;
// Auto save file name is the normal file name prepended with
// autosaveFilePrefix string.
fn.SetName( autosaveFilePrefix + fn.GetName() );
wxLogTrace( traceAutoSave,
wxT( "Creating auto save file <" + fn.GetFullPath() ) + wxT( ">" ) );
if( SavePcbFile( fn.GetFullPath(), NO_BACKUP_FILE ) )
{
GetScreen()->SetModify();
2012-08-29 16:59:50 +00:00
GetBoard()->SetFileName( tmpFileName.GetFullPath() );
UpdateTitle();
m_autoSaveState = false;
return true;
}
2012-08-29 16:59:50 +00:00
GetBoard()->SetFileName( tmpFileName.GetFullPath() );
return false;
}