2007-09-30 02:37:06 +00:00
|
|
|
/****************************/
|
2009-09-18 14:56:05 +00:00
|
|
|
/* EESCHEMA - files-io.cpp */
|
2007-09-30 02:37:06 +00:00
|
|
|
/****************************/
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
#include "fctsys.h"
|
|
|
|
#include "common.h"
|
2009-02-04 15:25:03 +00:00
|
|
|
#include "class_drawpanel.h"
|
|
|
|
#include "confirm.h"
|
|
|
|
#include "gestfich.h"
|
2010-11-10 15:30:12 +00:00
|
|
|
#include "wxEeschemaStruct.h"
|
2009-09-25 18:49:04 +00:00
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
#include "general.h"
|
|
|
|
#include "protos.h"
|
2009-09-22 12:27:57 +00:00
|
|
|
#include "eeschema_id.h"
|
2009-09-25 18:49:04 +00:00
|
|
|
#include "class_library.h"
|
2010-02-16 16:21:52 +00:00
|
|
|
#include "libeditframe.h"
|
2010-11-11 21:10:27 +00:00
|
|
|
#include "sch_sheet.h"
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
|
2007-09-30 02:37:06 +00:00
|
|
|
|
2010-10-26 20:25:48 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Routine to save an EESchema file. *
|
|
|
|
* FileSave controls how the file is to be saved - under what name. *
|
|
|
|
* Returns TRUE if the file has been saved. *
|
|
|
|
*****************************************************************************/
|
2010-12-08 20:12:46 +00:00
|
|
|
bool SCH_EDIT_FRAME::SaveEEFile( SCH_SCREEN* screen, int FileSave )
|
2010-10-26 20:25:48 +00:00
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
wxFileName schematicFileName, backupFileName;
|
|
|
|
FILE* f;
|
|
|
|
|
|
|
|
if( screen == NULL )
|
2010-12-10 19:47:44 +00:00
|
|
|
screen = GetScreen();
|
2010-10-26 20:25:48 +00:00
|
|
|
|
|
|
|
/* If no name exists in the window yet - save as new. */
|
2011-01-20 16:34:57 +00:00
|
|
|
if( screen->GetFileName().IsEmpty() )
|
2010-10-26 20:25:48 +00:00
|
|
|
FileSave = FILE_SAVE_NEW;
|
|
|
|
|
|
|
|
switch( FileSave )
|
|
|
|
{
|
|
|
|
case FILE_SAVE_AS:
|
2011-01-20 16:34:57 +00:00
|
|
|
schematicFileName = screen->GetFileName();
|
2010-10-26 20:25:48 +00:00
|
|
|
backupFileName = schematicFileName;
|
|
|
|
|
|
|
|
/* Rename the old file to a '.bak' one: */
|
|
|
|
if( schematicFileName.FileExists() )
|
|
|
|
{
|
|
|
|
backupFileName.SetExt( wxT( "bak" ) );
|
|
|
|
wxRemoveFile( backupFileName.GetFullPath() );
|
|
|
|
|
|
|
|
if( !wxRenameFile( schematicFileName.GetFullPath(), backupFileName.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
DisplayError( this, wxT( "Warning: unable to rename old file" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_SAVE_NEW:
|
|
|
|
{
|
|
|
|
wxFileDialog dlg( this, _( "Schematic Files" ), wxGetCwd(),
|
2011-01-20 16:34:57 +00:00
|
|
|
screen->GetFileName(), SchematicFileWildcard,
|
2010-10-26 20:25:48 +00:00
|
|
|
wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
|
|
|
|
|
|
|
|
if( dlg.ShowModal() == wxID_CANCEL )
|
|
|
|
return false;
|
|
|
|
|
2011-01-20 16:34:57 +00:00
|
|
|
screen->SetFileName( dlg.GetPath() );
|
2010-10-26 20:25:48 +00:00
|
|
|
schematicFileName = dlg.GetPath();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( f = wxFopen( schematicFileName.GetFullPath(), wxT( "wt" ) ) ) == NULL )
|
|
|
|
{
|
|
|
|
msg = _( "Failed to create file " ) + schematicFileName.GetFullPath();
|
|
|
|
DisplayError( this, msg );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( FileSave == FILE_SAVE_NEW )
|
2011-01-20 16:34:57 +00:00
|
|
|
screen->SetFileName( schematicFileName.GetFullPath() );
|
2010-10-26 20:25:48 +00:00
|
|
|
|
|
|
|
bool success = screen->Save( f );
|
|
|
|
|
|
|
|
if( !success )
|
|
|
|
DisplayError( this, _( "File write operation failed." ) );
|
|
|
|
else
|
|
|
|
screen->ClrModify();
|
|
|
|
|
|
|
|
|
|
|
|
fclose( f );
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-12 21:12:46 +00:00
|
|
|
/* Commands to save project or the current page.
|
2007-09-30 02:37:06 +00:00
|
|
|
*/
|
2010-12-08 20:12:46 +00:00
|
|
|
void SCH_EDIT_FRAME::Save_File( wxCommandEvent& event )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2007-09-30 02:37:06 +00:00
|
|
|
int id = event.GetId();
|
|
|
|
|
|
|
|
switch( id )
|
|
|
|
{
|
|
|
|
case ID_SAVE_PROJECT: /* Update Schematic File */
|
2008-07-18 07:04:43 +00:00
|
|
|
SaveProject();
|
2007-09-30 02:37:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ID_SAVE_ONE_SHEET: /* Update Schematic File */
|
|
|
|
SaveEEFile( NULL, FILE_SAVE_AS );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ID_SAVE_ONE_SHEET_AS: /* Save EED (new name) */
|
|
|
|
SaveEEFile( NULL, FILE_SAVE_NEW );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-12-08 20:12:46 +00:00
|
|
|
DisplayError( this, wxT( "SCH_EDIT_FRAME::Save_File Internal Error" ) );
|
2007-09-30 02:37:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-04 20:46:53 +00:00
|
|
|
/**
|
2009-09-18 14:56:05 +00:00
|
|
|
* Load an entire project
|
|
|
|
*
|
|
|
|
* Schematic root file and its subhierarchies, the configuration and the libs
|
|
|
|
* which are not already loaded)
|
|
|
|
*/
|
2010-12-08 20:12:46 +00:00
|
|
|
bool SCH_EDIT_FRAME::LoadOneEEProject( const wxString& FileName, bool IsNew )
|
2008-02-12 21:12:46 +00:00
|
|
|
{
|
2010-10-26 20:25:48 +00:00
|
|
|
SCH_SCREEN* screen;
|
|
|
|
wxString FullFileName, msg;
|
|
|
|
bool LibCacheExist = false;
|
|
|
|
SCH_SCREENS ScreenList;
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2010-04-06 14:09:52 +00:00
|
|
|
for( screen = ScreenList.GetFirst(); screen != NULL; screen = ScreenList.GetNext() )
|
2008-03-20 01:50:21 +00:00
|
|
|
{
|
|
|
|
if( screen->IsModify() )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( screen )
|
|
|
|
{
|
2011-01-20 16:34:57 +00:00
|
|
|
if( !IsOK( this, _( "Discard changes to the current schematic?" ) ) )
|
2010-05-17 20:35:46 +00:00
|
|
|
return false;
|
2011-01-20 16:34:57 +00:00
|
|
|
|
|
|
|
if( g_RootSheet->GetScreen()->GetFileName() != m_DefaultSchematicFileName )
|
|
|
|
SetLastProject( g_RootSheet->GetScreen()->GetFileName() );
|
2008-03-20 01:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FullFileName = FileName;
|
2011-01-20 16:34:57 +00:00
|
|
|
|
2008-03-20 01:50:21 +00:00
|
|
|
if( ( FullFileName.IsEmpty() ) && !IsNew )
|
|
|
|
{
|
2009-04-08 18:20:27 +00:00
|
|
|
wxFileDialog dlg( this, _( "Open Schematic" ), wxGetCwd(),
|
2009-04-06 18:54:57 +00:00
|
|
|
wxEmptyString, SchematicFileWildcard,
|
|
|
|
wxFD_OPEN | wxFD_FILE_MUST_EXIST );
|
|
|
|
|
|
|
|
if( dlg.ShowModal() == wxID_CANCEL )
|
2010-05-17 20:35:46 +00:00
|
|
|
return false;
|
2009-04-06 18:54:57 +00:00
|
|
|
|
|
|
|
FullFileName = dlg.GetPath();
|
2008-03-20 01:50:21 +00:00
|
|
|
}
|
2009-04-06 18:54:57 +00:00
|
|
|
|
2008-07-18 07:04:43 +00:00
|
|
|
if( g_RootSheet )
|
|
|
|
{
|
|
|
|
SAFE_DELETE( g_RootSheet );
|
2008-03-20 01:50:21 +00:00
|
|
|
}
|
2010-12-10 19:47:44 +00:00
|
|
|
|
2008-03-20 01:50:21 +00:00
|
|
|
CreateScreens();
|
2010-12-10 19:47:44 +00:00
|
|
|
screen = GetScreen();
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2010-03-18 20:35:29 +00:00
|
|
|
wxFileName fn = FullFileName;
|
2010-12-10 19:47:44 +00:00
|
|
|
|
2010-03-18 20:35:29 +00:00
|
|
|
if( fn.IsRelative() )
|
|
|
|
{
|
|
|
|
fn.MakeAbsolute();
|
|
|
|
FullFileName = fn.GetFullPath();
|
|
|
|
}
|
|
|
|
wxLogDebug( wxT( "Loading schematic " ) + FullFileName );
|
|
|
|
wxSetWorkingDirectory( fn.GetPath() );
|
|
|
|
|
2011-01-20 16:34:57 +00:00
|
|
|
screen->SetFileName( FullFileName );
|
2008-07-18 07:04:43 +00:00
|
|
|
g_RootSheet->SetFileName( FullFileName );
|
2010-03-16 18:22:59 +00:00
|
|
|
SetStatusText( wxEmptyString );
|
2009-10-14 19:43:31 +00:00
|
|
|
ClearMsgPanel();
|
2008-03-20 01:50:21 +00:00
|
|
|
|
|
|
|
memset( &g_EESchemaVar, 0, sizeof(g_EESchemaVar) );
|
|
|
|
|
2010-02-23 20:18:27 +00:00
|
|
|
screen->ClrModify();
|
2008-07-18 07:04:43 +00:00
|
|
|
|
2008-03-20 01:50:21 +00:00
|
|
|
if( IsNew )
|
|
|
|
{
|
|
|
|
screen->m_CurrentSheetDesc = &g_Sheet_A4;
|
|
|
|
screen->SetZoom( 32 );
|
2009-10-14 19:43:31 +00:00
|
|
|
screen->SetGrid( ID_POPUP_GRID_LEVEL_1000 + m_LastGridSizeId );
|
2010-02-24 15:33:03 +00:00
|
|
|
screen->m_Title = NAMELESS_PROJECT;
|
|
|
|
screen->m_Title += wxT( ".sch" );
|
2011-01-20 16:34:57 +00:00
|
|
|
GetScreen()->SetFileName( screen->m_Title );
|
2008-03-20 01:50:21 +00:00
|
|
|
screen->m_Company.Empty();
|
|
|
|
screen->m_Commentaire1.Empty();
|
|
|
|
screen->m_Commentaire2.Empty();
|
|
|
|
screen->m_Commentaire3.Empty();
|
|
|
|
screen->m_Commentaire4.Empty();
|
2009-04-29 17:09:00 +00:00
|
|
|
LoadProjectFile( wxEmptyString, TRUE );
|
2008-03-20 01:50:21 +00:00
|
|
|
Zoom_Automatique( TRUE );
|
2008-04-30 17:04:22 +00:00
|
|
|
SetSheetNumberAndCount();
|
2008-12-20 13:12:57 +00:00
|
|
|
DrawPanel->Refresh();
|
2010-05-17 20:35:46 +00:00
|
|
|
return true;
|
2008-03-20 01:50:21 +00:00
|
|
|
}
|
2008-02-12 21:12:46 +00:00
|
|
|
|
2009-11-04 20:46:53 +00:00
|
|
|
// Reloading configuration.
|
2008-03-20 01:50:21 +00:00
|
|
|
msg = _( "Ready\nWorking dir: \n" ) + wxGetCwd();
|
|
|
|
PrintMsg( msg );
|
2008-02-12 21:12:46 +00:00
|
|
|
|
2009-04-29 17:09:00 +00:00
|
|
|
LoadProjectFile( wxEmptyString, FALSE );
|
2008-02-12 21:12:46 +00:00
|
|
|
|
2009-12-22 20:08:56 +00:00
|
|
|
// Clear (if needed) the current active library in libedit because it could be
|
|
|
|
// removed from memory
|
2010-11-19 16:28:46 +00:00
|
|
|
LIB_EDIT_FRAME::EnsureActiveLibExists();
|
2009-12-22 20:08:56 +00:00
|
|
|
|
2008-02-12 21:12:46 +00:00
|
|
|
// Delete old caches.
|
2009-09-18 14:56:05 +00:00
|
|
|
CMP_LIBRARY::RemoveCacheLibrary();
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2009-04-11 15:50:31 +00:00
|
|
|
/* Loading the project library cache
|
|
|
|
* until apr 2009 the lib is named <root_name>.cache.lib
|
|
|
|
* and after (due to code change): <root_name>-cache.lib
|
2009-11-04 20:46:53 +00:00
|
|
|
* so if the <name>-cache.lib is not found, the old way will be tried
|
2009-04-11 15:50:31 +00:00
|
|
|
*/
|
2011-01-20 16:34:57 +00:00
|
|
|
fn = g_RootSheet->GetScreen()->GetFileName();
|
2010-04-06 14:09:52 +00:00
|
|
|
|
|
|
|
bool use_oldcachename = false;
|
2010-05-17 20:35:46 +00:00
|
|
|
wxString cachename = fn.GetName() + wxT( "-cache" );
|
2010-04-06 14:09:52 +00:00
|
|
|
|
2009-04-11 15:50:31 +00:00
|
|
|
fn.SetName( cachename );
|
|
|
|
fn.SetExt( CompLibFileExtension );
|
2011-01-20 16:34:57 +00:00
|
|
|
|
2009-04-11 15:50:31 +00:00
|
|
|
if( ! fn.FileExists() )
|
|
|
|
{
|
2011-01-20 16:34:57 +00:00
|
|
|
fn = g_RootSheet->GetScreen()->GetFileName();
|
2009-04-11 15:50:31 +00:00
|
|
|
fn.SetExt( wxT( "cache.lib" ) );
|
|
|
|
use_oldcachename = true;
|
|
|
|
}
|
2009-09-18 14:56:05 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
if( fn.FileExists() )
|
2008-03-20 01:50:21 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
wxString errMsg;
|
|
|
|
|
2010-02-15 14:24:52 +00:00
|
|
|
wxLogDebug( wxT( "LoadOneEEProject() load schematic cache library file <%s>" ),
|
2009-10-13 09:00:46 +00:00
|
|
|
GetChars( fn.GetFullPath() ) );
|
2009-04-05 20:49:15 +00:00
|
|
|
msg = wxT( "Load " ) + fn.GetFullPath();
|
2009-09-18 14:56:05 +00:00
|
|
|
|
|
|
|
CMP_LIBRARY* LibCache = CMP_LIBRARY::LoadLibrary( fn, errMsg );
|
|
|
|
|
2008-03-20 01:50:21 +00:00
|
|
|
if( LibCache )
|
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
LibCache->SetCache();
|
2008-03-20 01:50:21 +00:00
|
|
|
msg += wxT( " OK" );
|
2009-04-11 15:50:31 +00:00
|
|
|
if ( use_oldcachename ) // set the new name
|
|
|
|
{
|
2010-05-17 20:35:46 +00:00
|
|
|
fn.SetName( cachename );
|
2009-04-11 15:50:31 +00:00
|
|
|
fn.SetExt( CompLibFileExtension );
|
2009-09-18 14:56:05 +00:00
|
|
|
LibCache->SetFileName( fn );
|
2009-04-11 15:50:31 +00:00
|
|
|
}
|
2009-09-18 14:56:05 +00:00
|
|
|
|
|
|
|
LibCacheExist = true;
|
|
|
|
CMP_LIBRARY::GetLibraryList().push_back( LibCache );
|
2008-03-20 01:50:21 +00:00
|
|
|
}
|
|
|
|
else
|
2009-09-18 14:56:05 +00:00
|
|
|
{
|
|
|
|
wxString prompt;
|
|
|
|
|
2010-11-01 18:33:44 +00:00
|
|
|
prompt.Printf( _( "Component library <%s> failed to load.\nError: %s" ),
|
2009-10-16 17:18:23 +00:00
|
|
|
GetChars( fn.GetFullPath() ),
|
|
|
|
GetChars( errMsg ) );
|
2009-09-18 14:56:05 +00:00
|
|
|
DisplayError( this, prompt );
|
2010-06-19 10:58:50 +00:00
|
|
|
msg += _( " ->Error" );
|
2009-09-18 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2008-03-20 01:50:21 +00:00
|
|
|
PrintMsg( msg );
|
|
|
|
}
|
|
|
|
|
2011-01-20 16:34:57 +00:00
|
|
|
if( !wxFileExists( g_RootSheet->GetScreen()->GetFileName() ) && !LibCacheExist )
|
2008-03-20 01:50:21 +00:00
|
|
|
{
|
2008-07-18 07:04:43 +00:00
|
|
|
Zoom_Automatique( FALSE );
|
2008-10-01 17:34:55 +00:00
|
|
|
msg.Printf( _( "File <%s> not found." ),
|
2011-01-20 16:34:57 +00:00
|
|
|
GetChars( g_RootSheet->GetScreen()->GetFileName() ) );
|
2009-04-17 08:51:02 +00:00
|
|
|
DisplayInfoMessage( this, msg, 0 );
|
2010-05-17 20:35:46 +00:00
|
|
|
return false;
|
2008-03-20 01:50:21 +00:00
|
|
|
}
|
|
|
|
|
2009-11-04 20:46:53 +00:00
|
|
|
// load the project.
|
2011-01-20 16:34:57 +00:00
|
|
|
g_RootSheet->SetScreen( NULL );
|
2008-07-18 07:04:43 +00:00
|
|
|
bool diag = g_RootSheet->Load( this );
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2009-11-04 20:46:53 +00:00
|
|
|
/* Redraw base screen (ROOT) if necessary. */
|
2010-05-17 20:35:46 +00:00
|
|
|
GetScreen()->SetGrid( ID_POPUP_GRID_LEVEL_1000 + m_LastGridSizeId );
|
2008-03-20 01:50:21 +00:00
|
|
|
Zoom_Automatique( FALSE );
|
2008-04-30 17:04:22 +00:00
|
|
|
SetSheetNumberAndCount();
|
2008-07-18 07:04:43 +00:00
|
|
|
DrawPanel->Refresh( true );
|
2008-03-20 01:50:21 +00:00
|
|
|
return diag;
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
2008-07-18 07:04:43 +00:00
|
|
|
|
|
|
|
|
2009-11-04 20:46:53 +00:00
|
|
|
/**
|
|
|
|
* Save the entire project and create an archive for components.
|
|
|
|
*
|
2010-12-29 17:47:32 +00:00
|
|
|
* The library archive name is <root_name>-cache.lib
|
2007-09-30 02:37:06 +00:00
|
|
|
*/
|
2010-12-08 20:12:46 +00:00
|
|
|
void SCH_EDIT_FRAME::SaveProject()
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2010-10-26 20:25:48 +00:00
|
|
|
SCH_SCREEN* screen;
|
|
|
|
wxFileName fn;
|
|
|
|
SCH_SCREENS ScreenList;
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2010-12-08 20:12:46 +00:00
|
|
|
for( screen = ScreenList.GetFirst(); screen != NULL; screen = ScreenList.GetNext() )
|
2007-09-30 02:37:06 +00:00
|
|
|
{
|
2011-01-20 16:34:57 +00:00
|
|
|
D( printf( "SaveEEFile, %s\n", CONV_TO_UTF8( screen->GetFileName() ) ); )
|
2008-02-12 21:12:46 +00:00
|
|
|
SaveEEFile( screen, FILE_SAVE_AS );
|
2007-09-30 02:37:06 +00:00
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2009-11-04 20:46:53 +00:00
|
|
|
/* Archive components in current directory. */
|
2009-04-05 20:49:15 +00:00
|
|
|
fn = g_RootSheet->GetFileName();
|
2010-05-17 20:35:46 +00:00
|
|
|
wxString cachename = fn.GetName() + wxT( "-cache" );
|
2009-04-11 15:50:31 +00:00
|
|
|
fn.SetName( cachename );
|
|
|
|
fn.SetExt( CompLibFileExtension );
|
2009-04-05 20:49:15 +00:00
|
|
|
LibArchive( this, fn.GetFullPath() );
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|