Exemple #1
0
bool PCB_EDIT_FRAME::doAutoSave()
{
    wxFileName tmpFileName;

    if( GetBoard()->GetFileName().IsEmpty() )
    {
        tmpFileName = wxFileName( wxStandardPaths::Get().GetDocumentsDir(), wxT( "noname" ),
                                  KiCadPcbFileExtension );
        GetBoard()->SetFileName( tmpFileName.GetFullPath() );
    }
    else
    {
        tmpFileName = Prj().AbsolutePath( GetBoard()->GetFileName() );
    }

    wxFileName autoSaveFileName = tmpFileName;

    // Auto save file name is the board file name prepended with autosaveFilePrefix string.
    autoSaveFileName.SetName( GetAutoSaveFilePrefix() + autoSaveFileName.GetName() );

    if( !autoSaveFileName.IsOk() )
        return false;

    // If the board file path is not writable, try writing to a platform specific temp file
    // path.  If that path isn't writabe, give up.
    if( !autoSaveFileName.IsDirWritable() )
    {
        autoSaveFileName.SetPath( wxFileName::GetTempDir() );

        if( !autoSaveFileName.IsOk() || !autoSaveFileName.IsDirWritable() )
            return false;
    }

    wxLogTrace( traceAutoSave, "Creating auto save file <" + autoSaveFileName.GetFullPath() + ">" );

    if( SavePcbFile( autoSaveFileName.GetFullPath(), NO_BACKUP_FILE ) )
    {
        GetScreen()->SetModify();
        GetBoard()->SetFileName( tmpFileName.GetFullPath() );
        UpdateTitle();
        m_autoSaveState = false;
        return true;
    }

    GetBoard()->SetFileName( tmpFileName.GetFullPath() );

    return false;
}
Exemple #2
0
bool PCB_EDIT_FRAME::Files_io_from_id( int id )
{
    wxString   msg;

    // If an edit is in progress, stop it.
    // For something else than save, get rid of current tool.
    if( id == ID_SAVE_BOARD )
        m_canvas->EndMouseCapture( -1, m_canvas->GetDefaultCursor() );
    else
        m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() );

    switch( id )
    {
    case ID_LOAD_FILE:
        {
            int         open_ctl = 0;
            wxString    fileName = Prj().AbsolutePath( GetBoard()->GetFileName() );

            return AskLoadBoardFileName( this, &open_ctl, &fileName, true )
                       && OpenProjectFiles( std::vector<wxString>( 1, fileName ), open_ctl );
        }

    case ID_IMPORT_NON_KICAD_BOARD:
        {
            int         open_ctl = 1;
            wxString    fileName; // = Prj().AbsolutePath( GetBoard()->GetFileName() );

            return AskLoadBoardFileName( this, &open_ctl, &fileName, false )
                       && OpenProjectFiles( std::vector<wxString>( 1, fileName ), open_ctl );
        }

    case ID_MENU_READ_BOARD_BACKUP_FILE:
    case ID_MENU_RECOVER_BOARD_AUTOSAVE:
        {
            wxFileName currfn = Prj().AbsolutePath( GetBoard()->GetFileName() );
            wxFileName fn = currfn;

            if( id == ID_MENU_RECOVER_BOARD_AUTOSAVE )
            {
                wxString rec_name = GetAutoSaveFilePrefix() + fn.GetName();
                fn.SetName( rec_name );
            }
            else
            {
                wxString backup_ext = fn.GetExt() + GetBackupSuffix();
                fn.SetExt( backup_ext );
            }

            if( !fn.FileExists() )
            {
                msg.Printf( _( "Recovery file \"%s\" not found." ), fn.GetFullPath() );
                DisplayInfoMessage( this, msg );
                return false;
            }

            msg.Printf( _( "OK to load recovery or backup file \"%s\"" ), fn.GetFullPath() );

            if( !IsOK( this, msg ) )
                return false;

            GetScreen()->ClrModify();    // do not prompt the user for changes

            if( OpenProjectFiles( std::vector<wxString>( 1, fn.GetFullPath() ) ) )
            {
                // Re-set the name since name or extension was changed
                GetBoard()->SetFileName( currfn.GetFullPath() );
                UpdateTitle();
                return true;
            }
            return false;
        }

    case ID_APPEND_FILE:
        wxFAIL_MSG( "OBSOLETE!  Should have gone though modern toolset." );

    case ID_NEW_BOARD:
    {
        if( !Clear_Pcb( true ) )
            return false;

        wxFileName fn( wxStandardPaths::Get().GetDocumentsDir(), wxT( "noname" ),
                       ProjectFileExtension );

        Prj().SetProjectFullName( fn.GetFullPath() );

        fn.SetExt( PcbFileExtension );

        GetBoard()->SetFileName( fn.GetFullPath() );

        onBoardLoaded();

        OnModify();
        return true;
    }

    case ID_SAVE_BOARD:
        if( !GetBoard()->GetFileName().IsEmpty() )
            return SavePcbFile( Prj().AbsolutePath( GetBoard()->GetFileName() ) );
        // Fall through

    case ID_COPY_BOARD_AS:
    case ID_SAVE_BOARD_AS:
        {
            wxString    pro_dir = wxPathOnly( Prj().GetProjectFullName() );
            wxFileName  fn( pro_dir, _( "noname" ), KiCadPcbFileExtension );
            wxString    filename = fn.GetFullPath();

            if( AskSaveBoardFileName( this, &filename ) )
            {
                if( id == ID_COPY_BOARD_AS )
                    return SavePcbCopy( filename );
                else
                    return SavePcbFile( filename, NO_BACKUP_FILE );
            }
            return false;
        }

    default:
        wxLogDebug( wxT( "File_io Internal Error" ) );
        return false;
    }
}
Exemple #3
0
bool PCB_EDIT_FRAME::SavePcbFile( const wxString& aFileName, bool aCreateBackupFile )
{
    // please, keep it simple.  prompting goes elsewhere.

    wxFileName  pcbFileName = aFileName;

    if( pcbFileName.GetExt() == LegacyPcbFileExtension )
        pcbFileName.SetExt( KiCadPcbFileExtension );

    if( !IsWritable( pcbFileName ) )
    {
        wxString msg = wxString::Format( _(
            "No access rights to write to file \"%s\"" ),
            GetChars( pcbFileName.GetFullPath() )
            );

        DisplayError( this, msg );
        return false;
    }

    wxString backupFileName;

    if( aCreateBackupFile )
    {
        backupFileName = createBackupFile( aFileName );
    }

    GetBoard()->SynchronizeNetsAndNetClasses();

    // Select default Netclass before writing file.
    // Useful to save default values in headers
    SetCurrentNetClass( NETCLASS::Default );

    ClearMsgPanel();

    wxString    upperTxt;
    wxString    lowerTxt;

    try
    {
        PLUGIN::RELEASER    pi( IO_MGR::PluginFind( IO_MGR::KICAD_SEXP ) );

        wxASSERT( pcbFileName.IsAbsolute() );

        pi->Save( pcbFileName.GetFullPath(), GetBoard(), NULL );
    }
    catch( const IO_ERROR& ioe )
    {
        wxString msg = wxString::Format( _(
                "Error saving board file \"%s\".\n%s" ),
                GetChars( pcbFileName.GetFullPath() ),
                GetChars( ioe.What() )
                );
        DisplayError( this, msg );

        lowerTxt.Printf( _( "Failed to create \"%s\"" ), GetChars( pcbFileName.GetFullPath() ) );

        AppendMsgPanel( upperTxt, lowerTxt, CYAN );

        return false;
    }

    GetBoard()->SetFileName( pcbFileName.GetFullPath() );
    UpdateTitle();

    // Put the saved file in File History, unless aCreateBackupFile
    // is false.
    // aCreateBackupFile == false is mainly used to write autosave files
    // and not need to have an autosave file in file history
    if( aCreateBackupFile )
        UpdateFileHistory( GetBoard()->GetFileName() );

    // Delete auto save file on successful save.
    wxFileName autoSaveFileName = pcbFileName;

    autoSaveFileName.SetName( GetAutoSaveFilePrefix() + pcbFileName.GetName() );

    if( autoSaveFileName.FileExists() )
        wxRemoveFile( autoSaveFileName.GetFullPath() );

    if( !!backupFileName )
        upperTxt.Printf( _( "Backup file: \"%s\"" ), GetChars( backupFileName ) );

    lowerTxt.Printf( _( "Wrote board file: \"%s\"" ), GetChars( pcbFileName.GetFullPath() ) );

    AppendMsgPanel( upperTxt, lowerTxt, CYAN );

    GetScreen()->ClrModify();
    GetScreen()->ClrSave();
    return true;
}
void PCB_EDIT_FRAME::OnExportSTEP( wxCommandEvent& event )
{

    wxFileName brdFile = GetBoard()->GetFileName();
    wxString brdName;

    if( GetScreen()->IsModify() || brdFile.GetFullPath().empty() )
    {
        if( !doAutoSave() )
        {
            wxMessageBox( _( "STEP export failed; please save the PCB and try again" ),
                          _( "STEP Export" ) );
            return;
        }

        brdFile = GetBoard()->GetFileName();
        brdName = GetAutoSaveFilePrefix();
        brdName.append( brdFile.GetName() );
        brdFile.SetName( brdName );
    }

    brdName = "\"";
    brdName.Append( brdFile.GetFullPath() );
    brdName.Append( "\"" );

    // Build default output file name
    brdFile = GetBoard()->GetFileName();
    wxString brdExt = brdFile.GetExt();
    brdFile.SetExt( "stp" );

    DIALOG_EXPORT_STEP dlg( this );
    dlg.FilePicker()->SetPath( brdFile.GetFullPath() );
    bool fileOverwrite = false;
    wxString outputFile;

    while( !fileOverwrite )
    {
        if ( dlg.ShowModal() != wxID_OK )
            return;

        brdFile = dlg.FilePicker()->GetPath();
        brdFile.SetExt( "stp" );
        outputFile = brdFile.GetFullPath();

        if( wxFile::Exists( outputFile ) )
        {
            wxString msg( _( "File: " ) );
            msg.append( outputFile );
            msg.append( "\n" );
            msg.append( _( "File exists, overwrite?" ) );
            int resp = wxMessageBox( msg, _( "STEP Export" ), wxYES_NO | wxCANCEL, this );

            switch( resp )
            {
                case wxCANCEL:
                    return;

                case wxYES:
                    fileOverwrite = true;
                    break;

                default:
                    break;
            }
        }
        else
        {
            fileOverwrite = true;
        }
    }

    outputFile.Prepend( "\"" );
    outputFile.Append( "\"" );
    bool   aUseDrillOrg = dlg.GetDrillOrgOption();
    bool   aUseAuxOrg   = dlg.GetAuxOrgOption();
    bool   aUseUserOrg  = dlg.GetUserOrgOption();
    bool   aNoVirtual = dlg.GetNoVirtOption();
    double aXOrg = 0.0;
    double aYOrg = 0.0;

    if( aUseUserOrg )
    {
        aXOrg = dlg.GetXOrg();
        aYOrg = dlg.GetYOrg();

        if( dlg.GetOrgUnitsChoice() == 1 )
        {
            // selected reference unit is in inches
            aXOrg *= 25.4;
            aYOrg *= 25.4;
        }
    }

    wxFileName appK2S( wxStandardPaths::Get().GetExecutablePath() );
    appK2S.SetName( "kicad2step" );

    wxString cmdK2S = "\"";
    cmdK2S.Append( appK2S.GetFullPath() );
    cmdK2S.Append( "\"" );

    if( aNoVirtual )
        cmdK2S.Append( " --no-virtual" );

    if( aUseDrillOrg )
        cmdK2S.Append( " --drill-origin" );

    if( aUseAuxOrg )
        cmdK2S.Append( " --grid-origin" );

    if( aUseUserOrg )
        cmdK2S.Append( wxString::Format( " --user-origin %.6fx%.6f", aXOrg, aYOrg ) );

    cmdK2S.Append( " -f -o " );
    cmdK2S.Append( outputFile );

    cmdK2S.Append( " " );
    cmdK2S.Append( brdName );

    int result = 0;

    do
    {
        wxBusyCursor dummy;
        result = wxExecute( cmdK2S, wxEXEC_SYNC | wxEXEC_HIDE_CONSOLE );
    } while( 0 );

    if( result )
    {
        wxMessageBox(
            _( "Unable to create STEP file; check that the board has a valid outline and models." ),
            _( "STEP Export" ), wxOK );
    }

    return;
}