kicad/pcbnew/files.cpp

393 lines
11 KiB
C++
Raw Normal View History

2007-10-29 10:05:07 +00:00
/***************************************/
/* files.cpp: read / write board files */
/***************************************/
#include "fctsys.h"
#include "common.h"
#include "class_drawpanel.h"
#include "confirm.h"
#include "kicad_string.h"
#include "gestfich.h"
#include "pcbnew.h"
2009-07-30 11:04:07 +00:00
#include "wxPcbStruct.h"
#include "protos.h"
#include "pcbnew_id.h"
void WinEDA_PcbFrame::OnFileHistory( wxCommandEvent& event )
{
wxString fn;
wxClientDC dc( DrawPanel );
fn = GetFileFromHistory( event.GetId(), _( "Printed circuit board" ) );
if( fn != wxEmptyString )
{
if( DrawPanel->ManageCurseur && DrawPanel->ForceCloseManageCurseur )
{
DrawPanel->PrepareGraphicContext( &dc );
DrawPanel->ForceCloseManageCurseur( DrawPanel, &dc );
}
SetToolID( 0, wxCURSOR_ARROW, wxEmptyString );
::wxSetWorkingDirectory( ::wxPathOnly( fn ) );
LoadOnePcbFile( fn, false );
ReCreateAuxiliaryToolbar();
DrawPanel->MouseToCursorSchema();
DrawPanel->CursorOn( &dc );
}
}
/****************************************************/
void WinEDA_PcbFrame::Files_io( wxCommandEvent& event )
/****************************************************/
2007-10-29 10:05:07 +00:00
/* Handle the read/write file commands
*/
{
int id = event.GetId();
wxString msg;
2007-10-29 10:05:07 +00:00
// If an edition is in progress, stop it
if( DrawPanel->ManageCurseur && DrawPanel->ForceCloseManageCurseur )
{
wxClientDC dc( DrawPanel );
DrawPanel->PrepareGraphicContext( &dc );
DrawPanel->ForceCloseManageCurseur( DrawPanel, &dc );
}
SetToolID( 0, wxCURSOR_ARROW, wxEmptyString );
switch( id )
{
case ID_LOAD_FILE:
2008-04-29 03:18:02 +00:00
LoadOnePcbFile( wxEmptyString, false );
ReCreateAuxiliaryToolbar();
break;
case ID_MENU_READ_LAST_SAVED_VERSION_BOARD:
case ID_MENU_RECOVER_BOARD:
{
wxFileName fn;
if( id == ID_MENU_RECOVER_BOARD )
{
fn = wxFileName( wxEmptyString, g_SaveFileName, PcbExtBuffer );
}
else
{
fn = GetScreen()->m_FileName;
fn.SetExt( wxT( "000" ) );
}
if( !fn.FileExists() )
{
msg = _( "Recovery file " ) + fn.GetFullPath() + _( " not found" );
DisplayInfoMessage( this, msg );
break;
}
else
{
msg = _( "Ok to load Recovery file " ) + fn.GetFullPath();
if( !IsOK( this, msg ) )
break;
}
LoadOnePcbFile( fn.GetFullPath(), false );
fn.SetExt( PcbExtBuffer );
GetScreen()->m_FileName = fn.GetFullPath();
SetTitle( GetScreen()->m_FileName );
ReCreateAuxiliaryToolbar();
break;
}
case ID_APPEND_FILE:
LoadOnePcbFile( wxEmptyString, true );
break;
case ID_NEW_BOARD:
Clear_Pcb( true );
GetScreen()->m_FileName.Printf( wxT( "%s%cnoname%s" ),
GetChars( wxGetCwd() ), DIR_SEP,
GetChars( PcbExtBuffer ) );
SetTitle( GetScreen()->m_FileName );
2008-04-29 15:43:28 +00:00
ReCreateLayerBox( NULL );
break;
case ID_SAVE_BOARD:
SavePcbFile( GetScreen()->m_FileName );
break;
case ID_SAVE_BOARD_AS:
SavePcbFile( wxEmptyString );
break;
default:
DisplayError( this, wxT( "File_io Internal Error" ) ); break;
}
}
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
*/
bool WinEDA_PcbFrame::LoadOnePcbFile( const wxString& FullFileName, bool Append )
{
int ii;
FILE* source;
wxString msg;
char cbuf[1024];
ActiveScreen = GetScreen();
if( GetScreen()->IsModify() && !Append )
{
if( !IsOK( this, _( "Board Modified: Continue ?" ) ) )
return false;
}
m_TrackAndViasSizesList_Changed = true;
if( Append )
{
GetScreen()->m_FileName = wxEmptyString;
GetScreen()->SetModify();
GetBoard()->m_Status_Pcb = 0;
}
2008-04-29 03:18:02 +00:00
wxString fileName;
if( FullFileName == wxEmptyString )
{
wxFileName fn = GetScreen()->m_FileName;
wxFileDialog dlg( this, _( "Open Board File" ), wxEmptyString,
fn.GetFullName(), PcbFileWildcard,
wxFD_OPEN | wxFD_FILE_MUST_EXIST );
if( dlg.ShowModal() == wxID_CANCEL )
return false;
fileName = dlg.GetPath();
}
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;
/* Start read PCB file
2008-04-29 03:18:02 +00:00
*/
source = wxFopen( GetScreen()->m_FileName, wxT( "rt" ) );
if( source == NULL )
{
msg.Printf( _( "File <%s> not found" ),
GetChars( GetScreen()->m_FileName ) );
DisplayError( this, msg );
return false;
}
/* Read header and TEST if it is a PCB file format */
GetLine( source, cbuf, &ii );
if( strncmp( cbuf, "PCBNEW-BOARD", 12 ) != 0 )
{
fclose( source );
DisplayError( this, wxT( "Unknown file type" ) );
return false;
}
int ver;
sscanf(cbuf, "PCBNEW-BOARD Version %d date", &ver );
if ( ver > g_CurrentVersionPCB )
{
DisplayInfoMessage( 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 )
{
DisplayInfoMessage( 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."));
}
// Reload the corresponding configuration file:
wxSetWorkingDirectory( wxPathOnly( GetScreen()->m_FileName ) );
if( Append )
2008-04-18 13:28:56 +00:00
ReadPcbFile( source, true );
else
{
Read_Config( GetScreen()->m_FileName );
// Update the option toolbar
m_DisplayPcbTrackFill = DisplayOpt.DisplayPcbTrackFill;
m_DisplayModText = DisplayOpt.DisplayModText;
m_DisplayModEdge = DisplayOpt.DisplayModEdge;
m_DisplayPadFill = DisplayOpt.DisplayPadFill;
2009-09-29 04:44:35 +00:00
m_DisplayViaFill = DisplayOpt.DisplayViaFill;
2008-04-18 13:28:56 +00:00
ReadPcbFile( source, false );
}
fclose( source );
GetScreen()->ClrModify();
/* If append option: change the initial board name to <oldname>-append.brd */
if( Append )
{
wxString new_filename = GetScreen()->m_FileName.BeforeLast('.');
if ( ! new_filename.EndsWith(wxT("-append")) )
new_filename += wxT("-append");
new_filename += PcbExtBuffer;
GetScreen()->SetModify();
GetScreen()->m_FileName = new_filename;
}
GetScreen()->m_FileName.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
SetTitle( GetScreen()->m_FileName );
SetLastProject( GetScreen()->m_FileName );
/* Rebuild the new pad list (for drc and ratsnet control ...) */
GetBoard()->m_Status_Pcb = 0;
2009-06-20 19:09:43 +00:00
// Display the loaded board:
DrawPanel->Refresh( true);
2009-06-20 19:09:43 +00:00
wxSafeYield(); // Needed if we want to see the board now.
2009-07-30 11:04:07 +00:00
2009-06-20 19:09:43 +00:00
// Compile rastnest and displays net info
Compile_Ratsnest( NULL, true );
GetBoard()->DisplayInfo( this );
2008-04-29 03:18:02 +00:00
/* reset the auto save timer */
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:
GetBoard()->Show( 0, std::cout );
#endif
2008-04-18 13:28:56 +00:00
return true;
}
/***********************************************************/
bool WinEDA_PcbFrame::SavePcbFile( const wxString& FileName )
/************************************************************/
2007-10-29 10:05:07 +00:00
/* Write the board file
*/
{
wxFileName backupFileName;
wxFileName pcbFileName;
2007-10-22 20:40:18 +00:00
wxString upperTxt;
wxString lowerTxt;
wxString msg;
2008-04-18 13:28:56 +00:00
bool saveok = true;
2007-10-22 20:40:18 +00:00
FILE* dest;
if( FileName == wxEmptyString )
{
wxFileDialog dlg( this, _( "Save Board File" ), wxEmptyString,
GetScreen()->m_FileName, BoardFileWildcard,
wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
2008-04-18 13:28:56 +00:00
if( dlg.ShowModal() == wxID_CANCEL )
return false;
2008-04-18 13:28:56 +00:00
GetScreen()->m_FileName = dlg.GetPath();
}
else
GetScreen()->m_FileName = FileName;
2007-10-29 10:05:07 +00:00
/* If changes are made, update the board date */
if( GetScreen()->IsModify() )
{
GetScreen()->m_Date = GenDate();
}
pcbFileName = GetScreen()->m_FileName;
2007-10-29 10:05:07 +00:00
/* Get the backup file name */
backupFileName = pcbFileName;
backupFileName.SetExt( wxT( "000" ) );
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
*/
if( backupFileName.FileExists() )
{
2007-10-29 10:05:07 +00:00
/* rename the "old" file" from xxx.brd to xxx.000 */
wxRemoveFile( backupFileName.GetFullPath() ); /* Remove the old file xxx.000 (if exists) */
if( !wxRenameFile( pcbFileName.GetFullPath(),
backupFileName.GetFullPath() ) )
{
msg = _( "Warning: unable to create backup file " ) +
backupFileName.GetFullPath();
DisplayError( this, msg, 15 );
saveok = false;
}
}
else
{
backupFileName.Clear();
saveok = false;
}
2007-10-29 10:05:07 +00:00
/* Create the file */
dest = wxFopen( pcbFileName.GetFullPath(), wxT( "wt" ) );
if( dest == 0 )
{
msg = _( "Unable to create " ) + pcbFileName.GetFullPath();
DisplayError( this, msg );
saveok = false;
}
if( dest )
{
GetScreen()->m_FileName = pcbFileName.GetFullPath();
SetTitle( GetScreen()->m_FileName );
2008-04-18 13:28:56 +00:00
SavePcbFormatAscii( dest );
fclose( dest );
}
2007-10-29 10:05:07 +00:00
/* Display the file names: */
MsgPanel->EraseMsgBox();
if( saveok )
{
upperTxt = _( "Backup file: " ) + backupFileName.GetFullPath();
}
if( dest )
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();
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
GetScreen()->ClrModify();
return true;
}