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"
|
|
|
|
#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
|
|
|
{
|
2008-03-20 01:50:21 +00:00
|
|
|
wxString msg;
|
|
|
|
wxString Name, BakName;
|
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:
|
|
|
|
dirbuf = wxGetCwd() + STRING_DIR_SEP;
|
|
|
|
Name = MakeFileName( dirbuf, screen->m_FileName, g_SchExtBuffer );
|
|
|
|
/* Rename the old file to a '.bak' one: */
|
|
|
|
BakName = Name;
|
|
|
|
if( wxFileExists( Name ) )
|
|
|
|
{
|
|
|
|
ChangeFileNameExt( BakName, wxT( ".bak" ) );
|
|
|
|
wxRemoveFile( BakName ); /* delete Old .bak file */
|
|
|
|
if( !wxRenameFile( Name, BakName ) )
|
2008-03-20 01:50:21 +00:00
|
|
|
{
|
2008-04-15 19:38:19 +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:
|
|
|
|
{
|
|
|
|
wxString mask = wxT( "*" ) + g_SchExtBuffer;
|
|
|
|
Name = EDA_FileSelector( _( "Schematic files:" ),
|
|
|
|
wxEmptyString, /* Chemin par defaut */
|
|
|
|
screen->m_FileName, /* nom fichier par defaut, et resultat */
|
|
|
|
g_SchExtBuffer, /* extension par defaut */
|
|
|
|
mask, /* Masque d'affichage */
|
|
|
|
this,
|
|
|
|
wxFD_SAVE,
|
|
|
|
FALSE
|
2008-04-30 17:04:22 +00:00
|
|
|
);
|
2008-04-15 19:38:19 +00:00
|
|
|
if( Name.IsEmpty() )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
screen->m_FileName = Name;
|
|
|
|
dirbuf = wxGetCwd() + STRING_DIR_SEP;
|
|
|
|
Name = MakeFileName( dirbuf, Name, g_SchExtBuffer );
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
if( ( f = wxFopen( Name, wxT( "wt" ) ) ) == NULL )
|
2008-03-20 01:50:21 +00:00
|
|
|
{
|
2008-04-15 19:38:19 +00:00
|
|
|
msg = _( "Failed to create file " ) + Name;
|
|
|
|
DisplayError( this, msg );
|
2008-03-20 01:50:21 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
if( FileSave == FILE_SAVE_NEW )
|
|
|
|
screen->m_FileName = Name;
|
|
|
|
|
|
|
|
bool success = screen->Save( f );
|
|
|
|
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
|
|
|
const wxChar** LibNames;
|
|
|
|
wxString Name, msg;
|
|
|
|
Ki_PageDescr* PlotSheet;
|
2008-03-20 01:50:21 +00:00
|
|
|
|
|
|
|
LibNames = GetLibNames();
|
2008-04-30 12:34:40 +00:00
|
|
|
for( int ii = 0; LibNames[ii] != NULL; ii++ )
|
2008-03-20 01:50:21 +00:00
|
|
|
{
|
2008-04-15 19:38:19 +00:00
|
|
|
if( ii > 0 )
|
|
|
|
Name += wxT( "," );
|
|
|
|
Name += LibNames[ii];
|
2008-03-20 01:50:21 +00:00
|
|
|
}
|
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
MyFree( LibNames );
|
|
|
|
|
|
|
|
// Creates header
|
|
|
|
if( fprintf( aFile, "%s %s %d\n", EESCHEMA_FILE_STAMP,
|
|
|
|
SCHEMATIC_HEAD_STRING, EESCHEMA_VERSION ) == EOF
|
|
|
|
|| 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 ),
|
|
|
|
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 );
|
2008-04-30 17:04:22 +00:00
|
|
|
|
2008-04-15 19:38:19 +00:00
|
|
|
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 */
|
2008-04-30 12:34:40 +00:00
|
|
|
bool failed = FALSE;
|
2008-04-30 17:04:22 +00:00
|
|
|
for( SCH_ITEM* item = EEDrawList; item; item = item->Next() )
|
2008-04-15 19:38:19 +00:00
|
|
|
{
|
2008-04-30 12:34:40 +00:00
|
|
|
switch( item->Type() )
|
2008-03-20 01:50:21 +00:00
|
|
|
{
|
2008-04-30 12:34:40 +00:00
|
|
|
case TYPE_SCH_COMPONENT: /* Its a library item. */
|
|
|
|
case DRAW_SHEET_STRUCT_TYPE: /* Its a Sheet item. */
|
|
|
|
case DRAW_SEGMENT_STRUCT_TYPE: /* Its a Segment item. */
|
|
|
|
case DRAW_BUSENTRY_STRUCT_TYPE: /* Its a Raccord item. */
|
|
|
|
case DRAW_POLYLINE_STRUCT_TYPE: /* Its a polyline item. */
|
|
|
|
case DRAW_JUNCTION_STRUCT_TYPE: /* Its a connection item. */
|
2008-04-15 19:38:19 +00:00
|
|
|
case DRAW_NOCONNECT_STRUCT_TYPE: /* Its a NoConnection item. */
|
2008-04-30 12:34:40 +00:00
|
|
|
case TYPE_SCH_TEXT: /* Its a text item. */
|
|
|
|
case TYPE_SCH_LABEL: /* Its a label item. */
|
|
|
|
case TYPE_SCH_GLOBALLABEL: /* Its a Global label item. */
|
|
|
|
case TYPE_SCH_HIERLABEL: /* Its a Hierarchical label item. */
|
|
|
|
case DRAW_MARKER_STRUCT_TYPE: /* Its a marker item. */
|
|
|
|
if( !item->Save( aFile ) )
|
|
|
|
failed = TRUE;
|
2008-04-15 19:38:19 +00:00
|
|
|
break;
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2008-04-30 17:04:22 +00:00
|
|
|
/*
|
|
|
|
* case DRAW_HIERARCHICAL_PIN_SHEET_STRUCT_TYPE:
|
|
|
|
* case DRAW_PICK_ITEM_STRUCT_TYPE:
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
2008-04-30 12:34:40 +00:00
|
|
|
if( failed )
|
2008-04-15 19:38:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fprintf( aFile, "$EndSCHEMATC\n" ) == EOF )
|
2008-04-30 12:34:40 +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
|
|
|
}
|