dialog_netlist.cpp 13.8 KB
Newer Older
1

2
/**
Wayne Stambaugh's avatar
Wayne Stambaugh committed
3
 * @file pcbnew/dialogs/dialog_netlist.cpp
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 */

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 1992-2012 KiCad Developers, see change_log.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
plyatov's avatar
plyatov committed
28

29 30 31
#include <fctsys.h>
#include <appl_wxstruct.h>
#include <confirm.h>
32 33 34
#include <dialog_helpers.h>
#include <html_messagebox.h>
#include <base_units.h>
35 36
#include <wxPcbStruct.h>
#include <pcbcommon.h>
37 38
#include <netlist_reader.h>
#include <reporter.h>
39

40 41
#include <pcbnew_config.h>
#include <class_board_design_settings.h>
42
#include <class_board.h>
43
#include <class_module.h>
44
#include <wildcards_and_files_ext.h>
plyatov's avatar
plyatov committed
45

46
#include <dialog_netlist.h>
plyatov's avatar
plyatov committed
47

48

49
void PCB_EDIT_FRAME::InstallNetlistFrame( wxDC* DC )
plyatov's avatar
plyatov committed
50
{
51 52
    /* Setup the netlist file name to the last netlist file read,
     * or the board file name if the last filename is empty or last file not existing.
53 54
     */
    wxFileName fn = GetLastNetListRead();
55
    wxString lastNetlistName = GetLastNetListRead();
plyatov's avatar
plyatov committed
56

57 58
    if( !fn.FileExists() )
    {
59
        fn = GetBoard()->GetFileName();
60
        fn.SetExt( NetlistFileExtension );
61
        lastNetlistName = fn.GetFullPath();
62
    }
63

64
    DIALOG_NETLIST dlg( this, DC, lastNetlistName );
plyatov's avatar
plyatov committed
65

66
    dlg.ShowModal();
67 68 69

    // Save project settings if needed.
    // Project settings are saved in the corresponding <board name>.pro file
70
    bool configChanged = lastNetlistName != GetLastNetListRead();
71

72 73 74 75 76 77
    if( dlg.UseCmpFileForFpNames() != GetUseCmpFileForFpNames() )
    {
        SetUseCmpFileForFpNames( dlg.UseCmpFileForFpNames() );
        configChanged = true;
    }

78 79 80
    if( configChanged
      && !GetBoard()->GetFileName().IsEmpty()
      && IsOK( NULL, _( "The project configuration has changed.  Do you want to save it?" ) ) )
81
    {
82
        wxFileName fn = GetBoard()->GetFileName();
83
        fn.SetExt( ProjectFileExtension );
84
        wxGetApp().WriteProjectConfig( fn.GetFullPath(), GROUP, GetProjectFileParameters() );
85
    }
plyatov's avatar
plyatov committed
86 87
}

88

89
DIALOG_NETLIST::DIALOG_NETLIST( PCB_EDIT_FRAME* aParent, wxDC * aDC,
90
                                const wxString & aNetlistFullFilename )
91
    : DIALOG_NETLIST_FBP( aParent )
92
{
93 94 95
    m_parent = aParent;
    m_dc = aDC;
    m_NetlistFilenameCtrl->SetValue( aNetlistFullFilename );
96
    m_cmpNameSourceOpt->SetSelection( m_parent->GetUseCmpFileForFpNames() ? 1 : 0 );
97

98
    GetSizer()->SetSizeHints( this );
plyatov's avatar
plyatov committed
99 100
}

101

102
void DIALOG_NETLIST::OnOpenNetlistClick( wxCommandEvent& event )
plyatov's avatar
plyatov committed
103
{
104
    wxString lastPath = wxFileName::GetCwd();
105
    wxString lastNetlistRead = m_parent->GetLastNetListRead();
106 107 108 109 110 111 112 113 114

    if( !lastNetlistRead.IsEmpty() && !wxFileName::FileExists( lastNetlistRead ) )
    {
        lastNetlistRead = wxEmptyString;
    }
    else
    {
        wxFileName fn = lastNetlistRead;
        lastPath = fn.GetPath();
115
        lastNetlistRead = fn.GetFullName();
116 117 118 119 120 121 122
    }

    wxLogDebug( wxT( "Last net list read path <%s>, file name <%s>." ),
                GetChars( lastPath ), GetChars( lastNetlistRead ) );

    wxFileDialog FilesDialog( this, _( "Select Netlist" ), lastPath, lastNetlistRead,
                              NetlistFileWildcard, wxFD_DEFAULT_STYLE | wxFD_FILE_MUST_EXIST );
123

124 125
    if( FilesDialog.ShowModal() != wxID_OK )
        return;
126

127
    m_NetlistFilenameCtrl->SetValue( FilesDialog.GetPath() );
128 129
}

130

131
void DIALOG_NETLIST::OnReadNetlistFileClick( wxCommandEvent& event )
132
{
133 134 135 136
    wxString msg;
    wxString netlistFileName = m_NetlistFilenameCtrl->GetValue();
    wxString cmpFileName;

137 138 139 140
    if( UseCmpFileForFpNames() )
    {
        wxFileName fn = m_NetlistFilenameCtrl->GetValue();
        fn.SetExt( ComponentFileExtension );
141 142 143 144
        cmpFileName = fn.GetFullPath();
    }

    // Give the user a chance to bail out when making changes from a netlist.
145 146
    if( !m_checkDryRun->GetValue()
      && !m_parent->GetBoard()->IsEmpty()
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
      && !IsOK( NULL, _( "The changes made by reading the netlist cannot be undone.  Are you "
                         "sure you want to read the netlist?" ) ) )
        return;

    wxBusyCursor busy();
    m_MessageWindow->Clear();

    msg.Printf( _( "Reading netlist file \"%s\".\n" ), GetChars( netlistFileName ) );
    m_MessageWindow->AppendText( msg );

    if( !cmpFileName.IsEmpty() )
    {
        msg.Printf( _( "Using component footprint link file \"%s\".\n" ), GetChars( cmpFileName ) );
        m_MessageWindow->AppendText( msg );
    }

    if( m_Select_By_Timestamp->GetSelection() == 1 )
    {
        msg.Printf( _( "Using time stamps to select footprints in file \"%s\".\n" ),
                    GetChars( cmpFileName ) );
        m_MessageWindow->AppendText( msg );
168
    }
169

170 171 172 173 174 175 176 177
    WX_TEXT_CTRL_REPORTER reporter( m_MessageWindow );

    m_parent->ReadPcbNetlist( netlistFileName, cmpFileName, &reporter,
                              m_ChangeExistingFootprintCtrl->GetSelection() == 1,
                              m_DeleteBadTracks->GetSelection() == 1,
                              m_RemoveExtraFootprintsCtrl->GetSelection() == 1,
                              m_Select_By_Timestamp->GetSelection() == 1,
                              m_checkDryRun->GetValue() );
178 179
}

180

181
void DIALOG_NETLIST::OnTestFootprintsClick( wxCommandEvent& event )
182
{
183 184 185 186 187 188 189 190 191 192 193
    if( m_parent->GetBoard()->m_Modules == NULL )
    {
        DisplayInfoMessage( this, _( "No modules" ) );
        return;
    }

    // Lists of duplicates, missing references and not in netlist footprints:
    std::vector <MODULE*> duplicate;
    wxArrayString missing;
    std::vector <MODULE*> notInNetlist;
    wxString netlistFilename = m_NetlistFilenameCtrl->GetValue();
194
    wxString cmpFilename;
195

196
    if( UseCmpFileForFpNames() )
197
    {
198 199 200
        wxFileName fn = m_NetlistFilenameCtrl->GetValue();
        fn.SetExt( ComponentFileExtension );
        cmpFilename = fn.GetFullPath();
201 202
    }

203 204 205
    if( !verifyFootprints( netlistFilename, cmpFilename, duplicate, missing, notInNetlist ) )
        return;

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    #define ERR_CNT_MAX 100 // Max number of errors to output in dialog
                            // to avoid a too long message list

    wxString list;          // The messages to display

    m_parent->SetLastNetListRead( netlistFilename );

    int err_cnt = 0;

    // Search for duplicate footprints.
    if( duplicate.size() == 0 )
        list << wxT("<p><b>") << _( "No duplicate." ) << wxT("</b></p>");
    else
    {
        list << wxT("<p><b>") << _( "Duplicates:" ) << wxT("</b></p>");

        for( unsigned ii = 0; ii < duplicate.size(); ii++ )
        {
            MODULE* module = duplicate[ii];

226
            if( module->GetReference().IsEmpty() )
227 228
                list << wxT("<br>") << wxT("[noref)");
            else
229
                list << wxT("<br>") << module->GetReference();
230

231
            list << wxT("  (<i>") << module->GetValue() << wxT("</i>)");
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
            list << wxT(" @ ");
            list << CoordinateToString( module->GetPosition().x ),
            list << wxT(", ") << CoordinateToString( module->GetPosition().y ),
            err_cnt++;

            if( ERR_CNT_MAX < err_cnt )
                break;
        }
    }

    // Search for missing modules on board.
    if( missing.size() == 0 )
        list << wxT("<p><b>") <<  _( "No missing modules." ) << wxT("</b></p>");
    else
    {
        list << wxT("<p><b>") << _( "Missing:" ) << wxT("</b></p>");

        for( unsigned ii = 0; ii < missing.size(); ii += 2 )
        {
            list << wxT("<br>") << missing[ii];
            list << wxT("  (<i>") << missing[ii+1] << wxT("</i>)");
            err_cnt++;

            if( ERR_CNT_MAX < err_cnt )
                break;
        }
    }


    // Search for modules found on board but not in net list.
    if( notInNetlist.size() == 0 )
263
        list << wxT( "<p><b>" ) << _( "No extra modules." ) << wxT( "</b></p>" );
264 265
    else
    {
266
        list << wxT( "<p><b>" ) << _( "Not in Netlist:" ) << wxT( "</b></p>" );
267 268 269 270 271

        for( unsigned ii = 0; ii < notInNetlist.size(); ii++ )
        {
            MODULE* module = notInNetlist[ii];

272
            if( module->GetReference().IsEmpty() )
273
                list << wxT( "<br>" ) << wxT( "[noref)" );
274
            else
275
                list << wxT( "<br>" ) << module->GetReference() ;
276

277 278
            list << wxT( " (<i>" ) << module->GetValue() << wxT( "</i>)" );
            list << wxT( " @ " );
279
            list << CoordinateToString( module->GetPosition().x ),
280
            list << wxT( ", " ) << CoordinateToString( module->GetPosition().y ),
281 282 283 284 285 286 287 288 289
            err_cnt++;

            if( ERR_CNT_MAX < err_cnt )
                break;
        }
    }

    if( ERR_CNT_MAX < err_cnt )
    {
290
        list << wxT( "<p><b>" )
291
             << _( "Too many errors: some are skipped" )
292
             << wxT( "</b></p>" );
293 294 295
    }

    HTML_MESSAGE_BOX dlg( this, _( "Check Modules" ) );
296
    dlg.AddHTML_Text( list );
297
    dlg.ShowModal();
298 299
}

300

301 302 303 304
/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_COMPILE_RATSNEST
 */

305
void DIALOG_NETLIST::OnCompileRatsnestClick( wxCommandEvent& event )
306
{
307
    m_parent->Compile_Ratsnest( m_dc, true );
308 309
}

310

311
/*!
g_harland's avatar
g_harland committed
312
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL
313 314
 */

315
void DIALOG_NETLIST::OnCancelClick( wxCommandEvent& event )
316
{
317
    EndModal( wxID_CANCEL );
318
}
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463


void DIALOG_NETLIST::OnSaveMessagesToFile( wxCommandEvent& aEvent )
{
    wxFileName fn;

    if( !m_parent->GetLastNetListRead().IsEmpty() )
    {
        fn = m_parent->GetLastNetListRead();
        fn.SetExt( wxT( "txt" ) );
    }
    else
    {
        fn.SetPath( wxFileName::GetCwd() );
    }

    wxFileDialog dlg( this, _( "Save contents of message window" ), fn.GetPath(), fn.GetName(),
                      TextWildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT );

    if( dlg.ShowModal() != wxID_OK )
        return;

    fn = dlg.GetPath();

    if( fn.GetExt().IsEmpty() )
        fn.SetExt( wxT( "txt" ) );

    wxFile f( fn.GetFullPath(), wxFile::write );

    if( !f.IsOpened() )
    {
        wxString msg;

        msg.Printf( _( "Cannot write message contents to file \"%s\"." ),
                    GetChars( fn.GetFullPath() ) );
        wxMessageBox( msg, _( "File Write Error" ), wxOK | wxICON_ERROR, this );
        return;
    }

    f.Write( m_MessageWindow->GetValue() );
}


void DIALOG_NETLIST::OnUpdateUISaveMessagesToFile( wxUpdateUIEvent& aEvent )
{
    aEvent.Enable( !m_MessageWindow->IsEmpty() );
}


void DIALOG_NETLIST::OnUpdateUIValidNetlistFile( wxUpdateUIEvent& aEvent )
{
    aEvent.Enable( !m_NetlistFilenameCtrl->GetValue().IsEmpty() );
}


bool DIALOG_NETLIST::verifyFootprints( const wxString&         aNetlistFilename,
                                       const wxString &        aCmpFilename,
                                       std::vector< MODULE* >& aDuplicates,
                                       wxArrayString&          aMissing,
                                       std::vector< MODULE* >& aNotInNetlist )
{
    wxString        msg;
    MODULE*         module;
    MODULE*         nextModule;
    NETLIST         netlist;
    wxBusyCursor    dummy;           // Shows an hourglass while calculating.
    NETLIST_READER* netlistReader;
    COMPONENT*      component;

    try
    {
        netlistReader = NETLIST_READER::GetNetlistReader( &netlist, aNetlistFilename,
                                                          aCmpFilename );

        if( netlistReader == NULL )
        {
            msg.Printf( _( "Cannot open netlist file \"%s\"." ), GetChars( aNetlistFilename ) );
            wxMessageBox( msg, _( "Netlist Load Error." ), wxOK | wxICON_ERROR );
            return false;
        }

        std::auto_ptr< NETLIST_READER > nlr( netlistReader );
        netlistReader->LoadNetlist();
    }
    catch( IO_ERROR& ioe )
    {
        msg.Printf( _( "Error loading netlist file:\n%s" ), ioe.errorText.GetData() );
        wxMessageBox( msg, _( "Netlist Load Error" ), wxOK | wxICON_ERROR );
        return false;
    }


#if defined( DEBUG )
    m_MessageWindow->Clear();
    WX_TEXT_CTRL_REPORTER rpt( m_MessageWindow );
    netlist.Show( 0, rpt );
#endif

    BOARD* pcb = m_parent->GetBoard();

    // Search for duplicate footprints.
    module = pcb->m_Modules;

    for( ; module != NULL; module = module->Next() )
    {
        nextModule = module->Next();

        for( ; nextModule != NULL; nextModule = nextModule->Next() )
        {
            if( module->GetReference().CmpNoCase( nextModule->GetReference() ) == 0 )
            {
                aDuplicates.push_back( module );
                break;
            }
        }
    }

    // Search for component footprints in the netlist but not on the board.
    for( unsigned ii = 0; ii < netlist.GetCount(); ii++ )
    {
        component = netlist.GetComponent( ii );

        module = pcb->FindModuleByReference( component->GetReference() );

        if( module == NULL )
        {
            aMissing.Add( component->GetReference() );
            aMissing.Add( component->GetValue() );
        }
    }

    // Search for component footprints found on board but not in netlist.
    module = pcb->m_Modules;

    for( ; module != NULL; module = module->Next() )
    {

        component = netlist.GetComponentByReference( module->GetReference() );

        if( component == NULL )
            aNotInNetlist.push_back( module );
    }

    return true;
}