2007-10-29 10:05:07 +00:00
|
|
|
/***************************************/
|
|
|
|
/* files.cpp: read / write board files */
|
|
|
|
/***************************************/
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
#include "fctsys.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "pcbnew.h"
|
|
|
|
#include "protos.h"
|
|
|
|
#include "id.h"
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************/
|
2007-08-06 02:02:39 +00:00
|
|
|
void WinEDA_PcbFrame::Files_io( wxCommandEvent& event )
|
2007-06-05 12:10:51 +00:00
|
|
|
/****************************************************/
|
2007-08-06 02:02:39 +00:00
|
|
|
|
2007-10-29 10:05:07 +00:00
|
|
|
/* Handle the read/write file commands
|
2007-08-06 02:02:39 +00:00
|
|
|
*/
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2007-08-06 02:02:39 +00:00
|
|
|
int id = event.GetId();
|
|
|
|
wxString msg;
|
|
|
|
|
|
|
|
|
2007-10-29 10:05:07 +00:00
|
|
|
// If an edition is in progress, stop it
|
2007-08-06 02:02:39 +00:00
|
|
|
if( DrawPanel->ManageCurseur && DrawPanel->ForceCloseManageCurseur )
|
|
|
|
{
|
2008-03-31 08:00:15 +00:00
|
|
|
wxClientDC dc( DrawPanel );
|
|
|
|
DrawPanel->PrepareGraphicContext( &dc );
|
2007-08-06 02:02:39 +00:00
|
|
|
DrawPanel->ForceCloseManageCurseur( DrawPanel, &dc );
|
|
|
|
}
|
|
|
|
SetToolID( 0, wxCURSOR_ARROW, wxEmptyString );
|
|
|
|
|
|
|
|
switch( id )
|
|
|
|
{
|
|
|
|
case ID_MENU_LOAD_FILE:
|
|
|
|
case ID_LOAD_FILE:
|
2008-04-29 03:18:02 +00:00
|
|
|
LoadOnePcbFile( wxEmptyString, false );
|
2007-08-06 02:02:39 +00:00
|
|
|
ReCreateAuxiliaryToolbar();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ID_MENU_READ_LAST_SAVED_VERSION_BOARD:
|
|
|
|
case ID_MENU_RECOVER_BOARD:
|
|
|
|
{
|
2008-04-29 03:18:02 +00:00
|
|
|
wxString filename, oldfilename = GetScreen()->m_FileName;
|
|
|
|
if( id == ID_MENU_RECOVER_BOARD )
|
|
|
|
{
|
|
|
|
filename = g_SaveFileName + PcbExtBuffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filename = oldfilename;
|
|
|
|
ChangeFileNameExt( filename, wxT( ".000" ) );
|
|
|
|
}
|
|
|
|
if( !wxFileExists( filename ) )
|
|
|
|
{
|
|
|
|
msg = _( "Recovery file " ) + filename + _( " not found" );
|
|
|
|
DisplayInfo( this, msg );
|
2007-08-06 02:02:39 +00:00
|
|
|
break;
|
2008-04-29 03:18:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg = _( "Ok to load Recovery file " ) + filename;
|
|
|
|
if( !IsOK( this, msg ) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
LoadOnePcbFile( filename, false );
|
|
|
|
GetScreen()->m_FileName = oldfilename;
|
|
|
|
SetTitle( GetScreen()->m_FileName );
|
|
|
|
ReCreateAuxiliaryToolbar();
|
2007-08-06 02:02:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ID_MENU_APPEND_FILE:
|
|
|
|
case ID_APPEND_FILE:
|
2008-03-31 08:00:15 +00:00
|
|
|
LoadOnePcbFile( wxEmptyString, TRUE );
|
2007-08-06 02:02:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ID_MENU_NEW_BOARD:
|
|
|
|
case ID_NEW_BOARD:
|
2007-12-03 06:54:19 +00:00
|
|
|
Clear_Pcb( TRUE );
|
2007-08-06 02:02:39 +00:00
|
|
|
GetScreen()->m_FileName.Printf( wxT( "%s%cnoname%s" ),
|
|
|
|
wxGetCwd().GetData(), DIR_SEP, PcbExtBuffer.GetData() );
|
|
|
|
SetTitle( GetScreen()->m_FileName );
|
2008-04-29 15:43:28 +00:00
|
|
|
ReCreateLayerBox( NULL );
|
2007-08-06 02:02:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ID_LOAD_FILE_1:
|
|
|
|
case ID_LOAD_FILE_2:
|
|
|
|
case ID_LOAD_FILE_3:
|
|
|
|
case ID_LOAD_FILE_4:
|
|
|
|
case ID_LOAD_FILE_5:
|
|
|
|
case ID_LOAD_FILE_6:
|
|
|
|
case ID_LOAD_FILE_7:
|
|
|
|
case ID_LOAD_FILE_8:
|
|
|
|
case ID_LOAD_FILE_9:
|
|
|
|
case ID_LOAD_FILE_10:
|
|
|
|
wxSetWorkingDirectory( wxPathOnly( GetLastProject( id - ID_LOAD_FILE_1 ) ) );
|
|
|
|
LoadOnePcbFile( GetLastProject( id - ID_LOAD_FILE_1 ).GetData(),
|
2008-04-18 13:28:56 +00:00
|
|
|
false );
|
2007-08-06 02:02:39 +00:00
|
|
|
ReCreateAuxiliaryToolbar();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ID_SAVE_BOARD:
|
|
|
|
case ID_MENU_SAVE_BOARD:
|
|
|
|
SavePcbFile( GetScreen()->m_FileName );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ID_MENU_SAVE_BOARD_AS:
|
|
|
|
SavePcbFile( wxEmptyString );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ID_PCB_GEN_CMP_FILE:
|
|
|
|
RecreateCmpFileFromBoard();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DisplayError( this, wxT( "File_io Internal Error" ) ); break;
|
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-31 08:00:15 +00:00
|
|
|
/*******************************************************************************/
|
|
|
|
int WinEDA_PcbFrame::LoadOnePcbFile( const wxString& FullFileName, bool Append )
|
|
|
|
/*******************************************************************************/
|
2007-08-06 02:02:39 +00:00
|
|
|
|
2007-10-29 10:05:07 +00:00
|
|
|
/**
|
|
|
|
* Read a board file
|
|
|
|
* @param FullFileName = file name. If empty, a file name will be asked
|
|
|
|
* @return 0 if fails or abort, 1 if OK
|
2007-08-06 02:02:39 +00:00
|
|
|
*/
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2007-08-06 02:02:39 +00:00
|
|
|
int ii;
|
|
|
|
FILE* source;
|
|
|
|
wxString msg;
|
2008-07-31 15:30:57 +00:00
|
|
|
char cbuf[1024];
|
2007-08-06 02:02:39 +00:00
|
|
|
|
|
|
|
ActiveScreen = GetScreen();
|
|
|
|
|
|
|
|
if( GetScreen()->IsModify() && !Append )
|
|
|
|
{
|
|
|
|
if( !IsOK( this, _( "Board Modified: Continue ?" ) ) )
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_SelTrackWidthBox_Changed = TRUE;
|
|
|
|
m_SelViaSizeBox_Changed = TRUE;
|
|
|
|
|
|
|
|
if( Append )
|
|
|
|
{
|
|
|
|
GetScreen()->m_FileName = wxEmptyString;
|
|
|
|
GetScreen()->SetModify();
|
|
|
|
m_Pcb->m_Status_Pcb = 0;
|
|
|
|
}
|
|
|
|
|
2008-04-29 03:18:02 +00:00
|
|
|
wxString fileName;
|
|
|
|
|
2007-08-06 02:02:39 +00:00
|
|
|
if( FullFileName == wxEmptyString )
|
|
|
|
{
|
|
|
|
msg = wxT( "*" ) + PcbExtBuffer;
|
2008-04-29 03:18:02 +00:00
|
|
|
fileName =
|
2007-08-20 10:55:09 +00:00
|
|
|
EDA_FileSelector( _( "Load board files:" ),
|
2007-08-06 02:02:39 +00:00
|
|
|
wxEmptyString, /* Chemin par defaut */
|
|
|
|
GetScreen()->m_FileName, /* nom fichier par defaut */
|
|
|
|
PcbExtBuffer, /* extension par defaut */
|
|
|
|
msg, /* Masque d'affichage */
|
|
|
|
this,
|
|
|
|
wxFD_OPEN,
|
|
|
|
FALSE
|
|
|
|
);
|
2008-04-29 03:18:02 +00:00
|
|
|
|
|
|
|
if( fileName == wxEmptyString )
|
2007-08-06 02:02:39 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
2008-04-29 03:18:02 +00:00
|
|
|
fileName = FullFileName;
|
|
|
|
|
|
|
|
if( !Append )
|
|
|
|
Clear_Pcb( false ); // pass false since we prompted above for a modified board
|
|
|
|
|
|
|
|
GetScreen()->m_FileName = fileName;
|
2007-08-06 02:02:39 +00:00
|
|
|
|
2007-08-30 08:15:05 +00:00
|
|
|
/* Start read PCB file
|
2008-04-29 03:18:02 +00:00
|
|
|
*/
|
2007-08-06 02:02:39 +00:00
|
|
|
|
|
|
|
source = wxFopen( GetScreen()->m_FileName, wxT( "rt" ) );
|
|
|
|
if( source == NULL )
|
|
|
|
{
|
|
|
|
msg.Printf( _( "File <%s> not found" ), GetScreen()->m_FileName.GetData() );
|
|
|
|
DisplayError( this, msg );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-30 08:15:05 +00:00
|
|
|
/* Read header and TEST if it is a PCB file format */
|
2007-08-06 02:02:39 +00:00
|
|
|
GetLine( source, cbuf, &ii );
|
|
|
|
if( strncmp( cbuf, "PCBNEW-BOARD", 12 ) != 0 )
|
|
|
|
{
|
|
|
|
fclose( source );
|
|
|
|
DisplayError( this, wxT( "Unknown file type" ) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-06 16:05:23 +00:00
|
|
|
int ver;
|
|
|
|
sscanf(cbuf, "PCBNEW-BOARD Version %d date", &ver );
|
|
|
|
if ( ver > g_CurrentVersionPCB )
|
|
|
|
{
|
|
|
|
DisplayInfo( this, _( "This file was created by a more recent version of PCBnew and may not load correctly. Please consider updating!"));
|
|
|
|
}
|
|
|
|
else if ( ver < g_CurrentVersionPCB )
|
|
|
|
{
|
2008-04-29 03:18:02 +00:00
|
|
|
DisplayInfo( this, _( "This file was created by an older version of PCBnew. It will be stored in the new file format when you save this file again."));
|
2008-04-06 16:05:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-30 08:15:05 +00:00
|
|
|
// Reload the corresponding configuration file:
|
2007-08-06 02:02:39 +00:00
|
|
|
wxSetWorkingDirectory( wxPathOnly( GetScreen()->m_FileName ) );
|
|
|
|
if( Append )
|
2008-04-18 13:28:56 +00:00
|
|
|
ReadPcbFile( source, true );
|
2007-08-06 02:02:39 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Read_Config( GetScreen()->m_FileName );
|
|
|
|
|
2007-08-30 08:15:05 +00:00
|
|
|
// Update the option toolbar
|
2007-08-06 02:02:39 +00:00
|
|
|
m_DisplayPcbTrackFill = DisplayOpt.DisplayPcbTrackFill;
|
|
|
|
m_DisplayModText = DisplayOpt.DisplayModText;
|
|
|
|
m_DisplayModEdge = DisplayOpt.DisplayModEdge;
|
|
|
|
m_DisplayPadFill = DisplayOpt.DisplayPadFill;
|
|
|
|
|
2008-04-18 13:28:56 +00:00
|
|
|
ReadPcbFile( source, false );
|
2007-08-06 02:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose( source );
|
|
|
|
|
|
|
|
GetScreen()->ClrModify();
|
|
|
|
|
2008-07-22 05:29:16 +00:00
|
|
|
/* If append option: change the initial board name to <oldname>-append.brd */
|
2007-08-06 02:02:39 +00:00
|
|
|
if( Append )
|
|
|
|
{
|
2008-07-22 05:29:16 +00:00
|
|
|
wxString new_filename = GetScreen()->m_FileName.BeforeLast('.');
|
|
|
|
if ( ! new_filename.EndsWith(wxT("-append")) )
|
|
|
|
new_filename += wxT("-append");
|
|
|
|
|
|
|
|
new_filename += PcbExtBuffer;
|
|
|
|
|
2007-08-06 02:02:39 +00:00
|
|
|
GetScreen()->SetModify();
|
2008-07-22 05:29:16 +00:00
|
|
|
GetScreen()->m_FileName = new_filename;
|
2007-08-06 02:02:39 +00:00
|
|
|
}
|
|
|
|
|
2008-07-22 05:29:16 +00:00
|
|
|
GetScreen()->m_FileName.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
|
|
|
|
|
|
|
|
SetTitle( GetScreen()->m_FileName );
|
|
|
|
SetLastProject( GetScreen()->m_FileName );
|
|
|
|
|
2007-08-30 08:15:05 +00:00
|
|
|
/* Rebuild the new pad list (for drc and ratsnet control ...) */
|
2007-08-06 02:02:39 +00:00
|
|
|
build_liste_pads();
|
|
|
|
|
2007-08-20 19:33:15 +00:00
|
|
|
m_Pcb->Display_Infos( this );
|
2008-03-31 08:00:15 +00:00
|
|
|
DrawPanel->Refresh( true);
|
2007-08-06 02:02:39 +00:00
|
|
|
|
2008-04-29 03:18:02 +00:00
|
|
|
/* reset the auto save timer */
|
2007-08-06 02:02:39 +00:00
|
|
|
g_SaveTime = time( NULL );
|
|
|
|
|
2008-04-18 13:28:56 +00:00
|
|
|
|
|
|
|
#if 0 && defined(DEBUG)
|
|
|
|
// note this freezes up pcbnew when run under the kicad project
|
2007-10-31 06:40:15 +00:00
|
|
|
// 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
|
2007-10-31 06:40:15 +00:00
|
|
|
// 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:
|
2007-08-06 02:02:39 +00:00
|
|
|
m_Pcb->Show( 0, std::cout );
|
|
|
|
#endif
|
2008-04-18 13:28:56 +00:00
|
|
|
|
2007-08-06 02:02:39 +00:00
|
|
|
return 1;
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************/
|
2007-08-06 02:02:39 +00:00
|
|
|
bool WinEDA_PcbFrame::SavePcbFile( const wxString& FileName )
|
2007-06-05 12:10:51 +00:00
|
|
|
/************************************************************/
|
2007-08-06 02:02:39 +00:00
|
|
|
|
2007-10-29 10:05:07 +00:00
|
|
|
/* Write the board file
|
2007-08-06 02:02:39 +00:00
|
|
|
*/
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2007-10-29 10:05:07 +00:00
|
|
|
wxString BackupFileName;
|
2007-10-22 20:40:18 +00:00
|
|
|
wxString FullFileName;
|
|
|
|
wxString upperTxt;
|
|
|
|
wxString lowerTxt;
|
|
|
|
wxString msg;
|
2008-04-18 13:28:56 +00:00
|
|
|
|
2007-10-22 20:40:18 +00:00
|
|
|
bool saveok = TRUE;
|
|
|
|
FILE* dest;
|
2007-08-06 02:02:39 +00:00
|
|
|
|
|
|
|
if( FileName == wxEmptyString )
|
|
|
|
{
|
|
|
|
msg = wxT( "*" ) + PcbExtBuffer;
|
2008-04-18 13:28:56 +00:00
|
|
|
|
2007-08-20 10:55:09 +00:00
|
|
|
FullFileName = EDA_FileSelector( _( "Save board files:" ),
|
2007-08-06 02:02:39 +00:00
|
|
|
wxEmptyString, /* Chemin par defaut */
|
|
|
|
GetScreen()->m_FileName, /* nom fichier par defaut */
|
|
|
|
PcbExtBuffer, /* extension par defaut */
|
|
|
|
msg, /* Masque d'affichage */
|
|
|
|
this,
|
|
|
|
wxFD_SAVE,
|
|
|
|
FALSE
|
|
|
|
);
|
|
|
|
if( FullFileName == wxEmptyString )
|
|
|
|
return FALSE;
|
2008-04-18 13:28:56 +00:00
|
|
|
|
2007-08-06 02:02:39 +00:00
|
|
|
GetScreen()->m_FileName = FullFileName;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
GetScreen()->m_FileName = FileName;
|
|
|
|
|
2007-10-29 10:05:07 +00:00
|
|
|
/* If changes are made, update the board date */
|
2007-08-06 02:02:39 +00:00
|
|
|
if( GetScreen()->IsModify() )
|
|
|
|
{
|
|
|
|
GetScreen()->m_Date = GenDate();
|
|
|
|
}
|
|
|
|
|
2007-10-29 10:05:07 +00:00
|
|
|
/* Get the filename */
|
2007-08-06 02:02:39 +00:00
|
|
|
FullFileName = MakeFileName( wxEmptyString, GetScreen()->m_FileName, PcbExtBuffer );
|
|
|
|
|
2007-10-29 10:05:07 +00:00
|
|
|
/* Get the backup file name */
|
|
|
|
BackupFileName = FullFileName;
|
|
|
|
ChangeFileNameExt( BackupFileName, wxT( ".000" ) );
|
2007-08-06 02:02:39 +00:00
|
|
|
|
2007-10-29 10:05:07 +00:00
|
|
|
/* If an old backup file exists, delete it.
|
2008-04-29 03:18:02 +00:00
|
|
|
if an old board file existes, rename it to the backup file name
|
|
|
|
*/
|
2007-08-06 02:02:39 +00:00
|
|
|
if( wxFileExists( FullFileName ) )
|
|
|
|
{
|
2007-10-29 10:05:07 +00:00
|
|
|
/* rename the "old" file" from xxx.brd to xxx.000 */
|
|
|
|
wxRemoveFile( BackupFileName ); /* Remove the old file xxx.000 (if exists) */
|
|
|
|
if( !wxRenameFile( FullFileName, BackupFileName ) )
|
2007-08-06 02:02:39 +00:00
|
|
|
{
|
2007-10-29 10:05:07 +00:00
|
|
|
msg = _( "Warning: unable to create bakfile " ) + BackupFileName;
|
2007-08-06 02:02:39 +00:00
|
|
|
DisplayError( this, msg, 15 );
|
|
|
|
saveok = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-18 13:28:56 +00:00
|
|
|
BackupFileName = wxEmptyString;
|
2007-08-09 21:15:08 +00:00
|
|
|
saveok = FALSE;
|
2007-08-06 02:02:39 +00:00
|
|
|
}
|
|
|
|
|
2007-10-29 10:05:07 +00:00
|
|
|
/* Create the file */
|
2007-08-06 02:02:39 +00:00
|
|
|
dest = wxFopen( FullFileName, wxT( "wt" ) );
|
|
|
|
if( dest == 0 )
|
|
|
|
{
|
|
|
|
msg = _( "Unable to create " ) + FullFileName;
|
|
|
|
DisplayError( this, msg );
|
|
|
|
saveok = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( dest )
|
|
|
|
{
|
|
|
|
GetScreen()->m_FileName = FullFileName;
|
|
|
|
SetTitle( GetScreen()->m_FileName );
|
2008-04-18 13:28:56 +00:00
|
|
|
|
2007-08-06 02:02:39 +00:00
|
|
|
SavePcbFormatAscii( dest );
|
|
|
|
fclose( dest );
|
|
|
|
}
|
|
|
|
|
2007-10-29 10:05:07 +00:00
|
|
|
/* Display the file names: */
|
2007-08-06 02:02:39 +00:00
|
|
|
MsgPanel->EraseMsgBox();
|
|
|
|
|
|
|
|
if( saveok )
|
|
|
|
{
|
2007-10-29 10:05:07 +00:00
|
|
|
upperTxt = _( "Backup file: " ) + BackupFileName;
|
2007-08-06 02:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( dest )
|
2007-10-22 20:40:18 +00:00
|
|
|
lowerTxt = _( "Wrote board file: " );
|
2007-08-06 02:02:39 +00:00
|
|
|
else
|
2007-10-22 20:40:18 +00:00
|
|
|
lowerTxt = _( "Failed to create " );
|
|
|
|
lowerTxt += FullFileName;
|
2007-08-06 02:02:39 +00:00
|
|
|
|
2007-10-22 20:40:18 +00:00
|
|
|
Affiche_1_Parametre( this, 1, upperTxt, lowerTxt, CYAN );
|
2008-04-18 13:28:56 +00:00
|
|
|
|
2007-10-29 10:05:07 +00:00
|
|
|
g_SaveTime = time( NULL ); /* Reset timer for the automatic saving */
|
2008-04-18 13:28:56 +00:00
|
|
|
|
2007-08-06 02:02:39 +00:00
|
|
|
GetScreen()->ClrModify();
|
|
|
|
return TRUE;
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|