2008-04-15 19:38:19 +00:00
|
|
|
/*********************************************/
|
|
|
|
/* eesave.cpp Module to Save EESchema files */
|
|
|
|
/*********************************************/
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
#include "fctsys.h"
|
|
|
|
#include "gr_basic.h"
|
|
|
|
#include "common.h"
|
2009-02-04 15:25:03 +00:00
|
|
|
#include "confirm.h"
|
|
|
|
#include "kicad_string.h"
|
|
|
|
#include "gestfich.h"
|
2007-06-05 12:10:51 +00:00
|
|
|
#include "program.h"
|
|
|
|
#include "libcmp.h"
|
|
|
|
#include "general.h"
|
|
|
|
#include "macros.h"
|
|
|
|
|
|
|
|
#include "protos.h"
|
|
|
|
|
|
|
|
/* Fonctions Locales */
|
2008-04-15 19:38:19 +00:00
|
|
|
static void SaveLayers( FILE* f );
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
/* Variables locales */
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* 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. *
|
|
|
|
*****************************************************************************/
|
2008-04-15 19:38:19 +00:00
|
|
|
bool WinEDA_SchematicFrame::SaveEEFile( SCH_SCREEN* screen, int FileSave )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2009-04-05 20:49:15 +00:00
|
|
|
wxString msg, tmp;
|
|
|
|
wxFileName schematicFileName, backupFileName;
|
2008-04-15 19:38:19 +00:00
|
|
|
FILE* f;
|
2008-03-20 01:50:21 +00:00
|
|
|
wxString dirbuf;
|
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
if( screen == NULL )
|
|
|
|
screen = (SCH_SCREEN*) GetScreen();
|
2008-03-20 01:50:21 +00:00
|
|
|
|
|
|
|
/* If no name exists in the window yet - save as new. */
|
2008-04-15 19:38:19 +00:00
|
|
|
if( screen->m_FileName.IsEmpty() )
|
|
|
|
FileSave = FILE_SAVE_NEW;
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
switch( FileSave )
|
2008-03-20 01:50:21 +00:00
|
|
|
{
|
2008-04-15 19:38:19 +00:00
|
|
|
case FILE_SAVE_AS:
|
2009-04-05 20:49:15 +00:00
|
|
|
schematicFileName = screen->m_FileName;
|
|
|
|
backupFileName = schematicFileName;
|
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
/* Rename the old file to a '.bak' one: */
|
2009-04-05 20:49:15 +00:00
|
|
|
if( schematicFileName.FileExists() )
|
2008-04-15 19:38:19 +00:00
|
|
|
{
|
2009-04-05 20:49:15 +00:00
|
|
|
backupFileName.SetExt( wxT( "bak" ) );
|
|
|
|
wxRemoveFile( backupFileName.GetFullPath() );
|
|
|
|
|
|
|
|
if( !wxRenameFile( schematicFileName.GetFullPath(),
|
|
|
|
backupFileName.GetFullPath() ) )
|
2008-03-20 01:50:21 +00:00
|
|
|
{
|
2009-04-05 20:49:15 +00:00
|
|
|
DisplayError( this,
|
|
|
|
wxT( "Warning: unable to rename old file" ), 10 );
|
2008-03-20 01:50:21 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-15 19:38:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_SAVE_NEW:
|
|
|
|
{
|
2009-04-08 18:20:27 +00:00
|
|
|
wxFileDialog dlg( this, _( "Schematic Files" ), wxGetCwd(),
|
2009-04-05 20:49:15 +00:00
|
|
|
screen->m_FileName, SchematicFileWildcard,
|
|
|
|
wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
|
|
|
|
|
|
|
|
if( dlg.ShowModal() == wxID_CANCEL )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
screen->m_FileName = dlg.GetPath();
|
|
|
|
schematicFileName = dlg.GetPath();
|
2008-04-15 19:38:19 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
default:
|
|
|
|
break;
|
2008-03-20 01:50:21 +00:00
|
|
|
}
|
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
if( ( f = wxFopen( schematicFileName.GetFullPath(), wxT( "wt" ) ) ) == NULL )
|
2008-03-20 01:50:21 +00:00
|
|
|
{
|
2009-04-05 20:49:15 +00:00
|
|
|
msg = _( "Failed to create file " ) + schematicFileName.GetFullPath();
|
2008-04-15 19:38:19 +00:00
|
|
|
DisplayError( this, msg );
|
2009-04-05 20:49:15 +00:00
|
|
|
return false;
|
2008-03-20 01:50:21 +00:00
|
|
|
}
|
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
if( FileSave == FILE_SAVE_NEW )
|
2009-04-05 20:49:15 +00:00
|
|
|
screen->m_FileName = schematicFileName.GetFullPath();
|
2008-04-15 19:38:19 +00:00
|
|
|
|
|
|
|
bool success = screen->Save( f );
|
2009-04-05 20:49:15 +00:00
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
if( !success )
|
|
|
|
DisplayError( this, _( "File write operation failed." ) );
|
|
|
|
else
|
|
|
|
screen->ClrModify();
|
|
|
|
|
|
|
|
|
|
|
|
fclose( f );
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************/
|
|
|
|
bool SCH_SCREEN::Save( FILE* aFile ) const
|
|
|
|
/*****************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Save
|
|
|
|
* writes the data structures for this object out to a FILE in "*.brd" format.
|
|
|
|
* @param aFile The FILE to write to.
|
|
|
|
* @return bool - true if success writing else false.
|
|
|
|
*/
|
|
|
|
{
|
2008-04-30 17:04:22 +00:00
|
|
|
wxString Name, msg;
|
|
|
|
Ki_PageDescr* PlotSheet;
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2008-12-07 17:55:29 +00:00
|
|
|
wxString datetime = DateAndTime( );
|
2009-06-04 10:42:59 +00:00
|
|
|
bool first = true;
|
|
|
|
for( LibraryStruct* Lib = g_LibraryList; Lib != NULL; Lib = Lib->m_Pnext )
|
2008-03-20 01:50:21 +00:00
|
|
|
{
|
2009-06-23 15:45:53 +00:00
|
|
|
if( ! first )
|
2008-04-15 19:38:19 +00:00
|
|
|
Name += wxT( "," );
|
2009-06-04 10:42:59 +00:00
|
|
|
Name += Lib->m_Name;
|
|
|
|
first = false;
|
2008-03-20 01:50:21 +00:00
|
|
|
}
|
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
// Creates header
|
2008-12-07 17:55:29 +00:00
|
|
|
if( fprintf( aFile, "%s %s %d", EESCHEMA_FILE_STAMP,
|
2009-04-05 20:49:15 +00:00
|
|
|
SCHEMATIC_HEAD_STRING, EESCHEMA_VERSION ) == EOF )
|
2008-12-07 17:55:29 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if( fprintf( aFile, " date %s\n", CONV_TO_UTF8(datetime) ) == EOF )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if( fprintf( aFile, "LIBS:%s\n", CONV_TO_UTF8( Name ) ) == EOF )
|
2008-03-20 01:50:21 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
SaveLayers( aFile );
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
/* Write page info */
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
PlotSheet = m_CurrentSheetDesc;
|
|
|
|
fprintf( aFile, "$Descr %s %d %d\n", CONV_TO_UTF8( PlotSheet->m_Name ),
|
2009-04-05 20:49:15 +00:00
|
|
|
PlotSheet->m_Size.x, PlotSheet->m_Size.y );
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2008-04-30 17:04:22 +00:00
|
|
|
/* Write ScreenNumber and NumberOfScreen; not very meaningfull for SheetNumber and Sheet Count
|
|
|
|
* in a complex hierarchy, but usefull in simple hierarchy and flat hierarchy
|
|
|
|
* Used also to serach the root sheet ( ScreenNumber = 1 ) withing the files
|
|
|
|
*/
|
2008-04-15 19:38:19 +00:00
|
|
|
fprintf( aFile, "Sheet %d %d\n", m_ScreenNumber, m_NumberOfScreen );
|
|
|
|
fprintf( aFile, "Title \"%s\"\n", CONV_TO_UTF8( m_Title ) );
|
|
|
|
fprintf( aFile, "Date \"%s\"\n", CONV_TO_UTF8( m_Date ) );
|
|
|
|
fprintf( aFile, "Rev \"%s\"\n", CONV_TO_UTF8( m_Revision ) );
|
|
|
|
fprintf( aFile, "Comp \"%s\"\n", CONV_TO_UTF8( m_Company ) );
|
|
|
|
fprintf( aFile, "Comment1 \"%s\"\n", CONV_TO_UTF8( m_Commentaire1 ) );
|
|
|
|
fprintf( aFile, "Comment2 \"%s\"\n", CONV_TO_UTF8( m_Commentaire2 ) );
|
|
|
|
fprintf( aFile, "Comment3 \"%s\"\n", CONV_TO_UTF8( m_Commentaire3 ) );
|
|
|
|
fprintf( aFile, "Comment4 \"%s\"\n", CONV_TO_UTF8( m_Commentaire4 ) );
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
fprintf( aFile, "$EndDescr\n" );
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
/* Saving schematic items */
|
2009-04-05 20:49:15 +00:00
|
|
|
bool failed = false;
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
for( SCH_ITEM* item = EEDrawList; item && !failed; item = item->Next() )
|
|
|
|
{
|
|
|
|
if( !item->Save( aFile ) )
|
|
|
|
failed = true;
|
2008-04-15 19:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( fprintf( aFile, "$EndSCHEMATC\n" ) == EOF )
|
2009-04-05 20:49:15 +00:00
|
|
|
failed = true;
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2008-04-30 12:34:40 +00:00
|
|
|
return !failed;
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************/
|
2008-04-15 19:38:19 +00:00
|
|
|
static void SaveLayers( FILE* f )
|
2007-06-05 12:10:51 +00:00
|
|
|
/****************************/
|
2008-04-15 19:38:19 +00:00
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
/* Save a Layer Structure to a file
|
2008-04-15 19:38:19 +00:00
|
|
|
* theses infos are not used in eeschema
|
|
|
|
*/
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2008-04-15 19:38:19 +00:00
|
|
|
fprintf( f, "EELAYER %2d %2d\n", g_LayerDescr.NumberOfLayers, g_LayerDescr.CurrentLayer );
|
|
|
|
fprintf( f, "EELAYER END\n" );
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|