1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2012 Wayne Stambaugh <stambaughw@gmail.com>
* Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.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
*/
/**
* @file dialog_erc.cpp
* @brief Electrical Rules Check dialog implementation.
*/
#include <fctsys.h>
#include <class_drawpanel.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <pgm_base.h>
#include <sch_screen.h>
#include <sch_edit_frame.h>
#include <invoke_sch_dialog.h>
#include <project.h>
#include <kiface_i.h>
#include <bitmaps.h>
#include <reporter.h>
#include <wildcards_and_files_ext.h>
#include <netlist.h>
#include <netlist_object.h>
#include <sch_marker.h>
#include <sch_sheet.h>
#include <lib_pin.h>
#include <sch_component.h>
#include <dialog_erc.h>
#include <erc.h>
#include <id.h>
extern int DiagErc[PINTYPE_COUNT][PINTYPE_COUNT];
extern int DefaultDiagErc[PINTYPE_COUNT][PINTYPE_COUNT];
bool DIALOG_ERC::m_writeErcFile = false; // saved only for the current session
bool DIALOG_ERC::m_TestSimilarLabels = true; // Save in project config
bool DIALOG_ERC::m_diagErcTableInit = false; // saved only for the current session
bool DIALOG_ERC::m_tstUniqueGlobalLabels = true; // saved only for the current session
// Control identifiers for events
#define ID_MATRIX_0 1800
BEGIN_EVENT_TABLE( DIALOG_ERC, DIALOG_ERC_BASE )
EVT_COMMAND_RANGE( ID_MATRIX_0, ID_MATRIX_0 + ( PINTYPE_COUNT * PINTYPE_COUNT ) - 1,
wxEVT_COMMAND_BUTTON_CLICKED, DIALOG_ERC::ChangeErrorLevel )
END_EVENT_TABLE()
DIALOG_ERC::DIALOG_ERC( SCH_EDIT_FRAME* parent ) :
DIALOG_ERC_BASE( parent, ID_DIALOG_ERC // parent looks for this ID explicitly
)
{
m_parent = parent;
m_lastMarkerFound = NULL;
Init();
// Now all widgets have the size fixed, call FinishDialogSettings
FinishDialogSettings();
}
DIALOG_ERC::~DIALOG_ERC()
{
m_TestSimilarLabels = m_cbTestSimilarLabels->GetValue();
m_tstUniqueGlobalLabels = m_cbTestUniqueGlbLabels->GetValue();
}
void DIALOG_ERC::Init()
{
m_initialized = false;
for( int ii = 0; ii < PINTYPE_COUNT; ii++ )
{
for( int jj = 0; jj < PINTYPE_COUNT; jj++ )
m_buttonList[ii][jj] = NULL;
}
m_WriteResultOpt->SetValue( m_writeErcFile );
m_cbTestSimilarLabels->SetValue( m_TestSimilarLabels );
m_cbTestUniqueGlbLabels->SetValue( m_tstUniqueGlobalLabels );
SCH_SCREENS screens;
updateMarkerCounts( &screens );
DisplayERC_MarkersList();
// Init Panel Matrix
ReBuildMatrixPanel();
// Set the run ERC button as the default button.
m_buttonERC->SetDefault();
}
void DIALOG_ERC::updateMarkerCounts( SCH_SCREENS *screens )
{
int markers = screens->GetMarkerCount( MARKER_BASE::MARKER_ERC,
MARKER_BASE::MARKER_SEVERITY_UNSPEC );
int warnings = screens->GetMarkerCount( MARKER_BASE::MARKER_ERC,
MARKER_BASE::MARKER_SEVERITY_WARNING );
int errors = screens->GetMarkerCount( MARKER_BASE::MARKER_ERC,
MARKER_BASE::MARKER_SEVERITY_ERROR );
wxString num;
num.Printf( wxT( "%d" ), markers );
m_TotalErrCount->SetValue( num );
num.Printf( wxT( "%d" ), errors );
m_LastErrCount->SetValue( num );
num.Printf( wxT( "%d" ), warnings );
m_LastWarningCount->SetValue( num );
}
/* Delete the old ERC markers, over the whole hierarchy
*/
void DIALOG_ERC::OnEraseDrcMarkersClick( wxCommandEvent& event )
{
SCH_SCREENS ScreenList;
ScreenList.DeleteAllMarkers( MARKER_BASE::MARKER_ERC );
updateMarkerCounts( &ScreenList );
m_MarkersList->ClearList();
m_parent->GetCanvas()->Refresh();
}
/* event handler for Close button
*/
void DIALOG_ERC::OnButtonCloseClick( wxCommandEvent& event )
{
Close();
}
void DIALOG_ERC::OnCloseErcDialog( wxCloseEvent& event )
{
Destroy();
}
void DIALOG_ERC::OnResetMatrixClick( wxCommandEvent& event )
{
ResetDefaultERCDiag( event );
}
void DIALOG_ERC::OnErcCmpClick( wxCommandEvent& event )
{
wxBusyCursor();
m_MarkersList->ClearList();
m_MessagesList->Clear();
wxSafeYield(); // m_MarkersList must be redraw
WX_TEXT_CTRL_REPORTER reporter( m_MessagesList );
TestErc( reporter );
}
void DIALOG_ERC::OnLeftClickMarkersList( wxHtmlLinkEvent& event )
{
wxString link = event.GetLinkInfo().GetHref();
m_lastMarkerFound = NULL;
long index;
if( !link.ToLong( &index ) )
return;
const SCH_MARKER* marker = m_MarkersList->GetItem( index );
if( marker == NULL )
return;
// Search for the selected marker
unsigned i;
SCH_SHEET_LIST sheetList( g_RootSheet );
bool notFound = true;
for( i = 0; i < sheetList.size(); i++ )
{
SCH_ITEM* item = (SCH_ITEM*) sheetList[i].LastDrawList();
for( ; item; item = item->Next() )
{
if( item == marker )
{
notFound = false;
break;
}
}
if( notFound == false )
break;
}
if( notFound ) // Error
{
wxMessageBox( _( "Marker not found" ) );
// The marker was deleted, so rebuild marker list
DisplayERC_MarkersList();
return;
}
if( sheetList[i] != m_parent->GetCurrentSheet() )
{
sheetList[i].LastScreen()->SetZoom( m_parent->GetScreen()->GetZoom() );
m_parent->SetCurrentSheet( sheetList[i] );
m_parent->GetCurrentSheet().UpdateAllScreenReferences();
}
m_lastMarkerFound = marker;
m_parent->SetCrossHairPosition( marker->m_Pos );
m_parent->RedrawScreen( marker->m_Pos, false);
}
void DIALOG_ERC::OnLeftDblClickMarkersList( wxMouseEvent& event )
{
// Remember: OnLeftClickMarkersList was called just before
// and therefore m_lastMarkerFound was initialized.
// (NULL if not found)
if( m_lastMarkerFound )
{
m_parent->SetCrossHairPosition( m_lastMarkerFound->m_Pos );
m_parent->RedrawScreen( m_lastMarkerFound->m_Pos, true );
// prevent a mouse left button release event in
// coming from the ERC dialog double click
// ( the button is released after closing this dialog and will generate
// an unwanted event in parent frame)
m_parent->SkipNextLeftButtonReleaseEvent();
}
Close();
}
void DIALOG_ERC::ReBuildMatrixPanel()
{
// Try to know the size of bitmap button used in drc matrix
wxBitmapButton * dummy = new wxBitmapButton( m_matrixPanel, wxID_ANY, KiBitmap( ercerr_xpm ) );
wxSize bitmap_size = dummy->GetSize();
delete dummy;
if( !m_diagErcTableInit )
{
memcpy( DiagErc, DefaultDiagErc, sizeof(DefaultDiagErc) );
m_diagErcTableInit = true;
}
wxPoint pos;
// Get the current text size:use a dummy text.
wxStaticText* text = new wxStaticText( m_matrixPanel, -1, wxT( "W" ), pos );
int text_height = text->GetRect().GetHeight();
bitmap_size.y = std::max( bitmap_size.y, text_height );
delete text;
// compute the Y pos interval:
pos.y = text_height;
if( m_initialized == false )
{
std::vector<wxStaticText*> labels;
// Print row labels
for( int ii = 0; ii < PINTYPE_COUNT; ii++ )
{
int y = pos.y + (ii * bitmap_size.y);
text = new wxStaticText( m_matrixPanel, -1, CommentERC_H[ii],
wxPoint( 5, y + ( bitmap_size.y / 2) - (text_height / 2) ) );
labels.push_back( text );
int x = text->GetRect().GetRight();
pos.x = std::max( pos.x, x );
}
// Right-align
for( int ii = 0; ii < PINTYPE_COUNT; ii++ )
{
wxPoint labelPos = labels[ ii ]->GetPosition();
labelPos.x = pos.x - labels[ ii ]->GetRect().GetWidth();
labels[ ii ]->SetPosition( labelPos );
}
pos.x += 5;
}
else
pos = m_buttonList[0][0]->GetPosition();
for( int ii = 0; ii < PINTYPE_COUNT; ii++ )
{
int y = pos.y + (ii * bitmap_size.y);
for( int jj = 0; jj <= ii; jj++ )
{
// Add column labels (only once)
int diag = DiagErc[ii][jj];
int x = pos.x + (jj * bitmap_size.x);
if( (ii == jj) && !m_initialized )
{
wxPoint txtpos;
txtpos.x = x + (bitmap_size.x / 2);
txtpos.y = y - text_height;
text = new wxStaticText( m_matrixPanel, -1, CommentERC_V[ii], txtpos );
}
int event_id = ID_MATRIX_0 + ii + ( jj * PINTYPE_COUNT );
BITMAP_DEF bitmap_butt = erc_green_xpm;
delete m_buttonList[ii][jj];
m_buttonList[ii][jj] = new wxBitmapButton( m_matrixPanel,
event_id,
KiBitmap( bitmap_butt ),
wxPoint( x, y ) );
setDRCMatrixButtonState( m_buttonList[ii][jj], diag );
}
}
m_initialized = true;
}
void DIALOG_ERC::setDRCMatrixButtonState( wxBitmapButton *aButton, int aState )
{
BITMAP_DEF bitmap_butt = NULL;
wxString tooltip;
switch( aState )
{
case OK:
bitmap_butt = erc_green_xpm;
tooltip = _( "No error or warning" );
break;
case WAR:
bitmap_butt = ercwarn_xpm;
tooltip = _( "Generate warning" );
break;
case ERR:
bitmap_butt = ercerr_xpm;
tooltip = _( "Generate error" );
break;
}
if( bitmap_butt )
{
aButton->SetBitmap( KiBitmap( bitmap_butt ) );
aButton->SetToolTip( tooltip );
}
}
void DIALOG_ERC::DisplayERC_MarkersList()
{
SCH_SHEET_LIST sheetList( g_RootSheet);
m_MarkersList->ClearList();
for( unsigned i = 0; i < sheetList.size(); i++ )
{
SCH_ITEM* item = sheetList[i].LastDrawList();
for( ; item != NULL; item = item->Next() )
{
if( item->Type() != SCH_MARKER_T )
continue;
SCH_MARKER* marker = (SCH_MARKER*) item;
if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
continue;
m_MarkersList->AppendToList( marker );
}
}
m_MarkersList->DisplayList();
}
void DIALOG_ERC::ResetDefaultERCDiag( wxCommandEvent& event )
{
memcpy( DiagErc, DefaultDiagErc, sizeof( DiagErc ) );
ReBuildMatrixPanel();
m_TestSimilarLabels = true;
m_cbTestSimilarLabels->SetValue( m_TestSimilarLabels );
m_tstUniqueGlobalLabels = true;
m_cbTestUniqueGlbLabels->SetValue( m_tstUniqueGlobalLabels );
}
void DIALOG_ERC::ChangeErrorLevel( wxCommandEvent& event )
{
int id, level, ii, x, y;
wxPoint pos;
id = event.GetId();
ii = id - ID_MATRIX_0;
wxBitmapButton* butt = (wxBitmapButton*) event.GetEventObject();
pos = butt->GetPosition();
x = ii / PINTYPE_COUNT; y = ii % PINTYPE_COUNT;
level = DiagErc[y][x];
//change to the next error level
switch( level )
{
case OK:
level = WAR;
break;
case WAR:
level = ERR;
break;
case ERR:
level = OK;
break;
}
setDRCMatrixButtonState( butt, level );
DiagErc[y][x] = DiagErc[x][y] = level;
}
void DIALOG_ERC::TestErc( REPORTER& aReporter )
{
wxFileName fn;
m_writeErcFile = m_WriteResultOpt->GetValue();
m_TestSimilarLabels = m_cbTestSimilarLabels->GetValue();
m_tstUniqueGlobalLabels = m_cbTestUniqueGlbLabels->GetValue();
// Build the whole sheet list in hierarchy (sheet, not screen)
SCH_SHEET_LIST sheets( g_RootSheet );
sheets.AnnotatePowerSymbols();
if( m_parent->CheckAnnotate( aReporter, false ) )
{
if( aReporter.HasMessage() )
aReporter.ReportTail( _( "Annotation required!" ), REPORTER::RPT_ERROR );
return;
}
SCH_SCREENS screens;
// Erase all previous DRC markers.
screens.DeleteAllMarkers( MARKER_BASE::MARKER_ERC );
/* Test duplicate sheet names inside a given sheet, one cannot have sheets with
* duplicate names (file names can be duplicated).
*/
TestDuplicateSheetNames( true );
/* Test is all units of each multiunit component have the same footprint assigned.
*/
TestMultiunitFootprints( sheets );
std::unique_ptr<NETLIST_OBJECT_LIST> objectsConnectedList( m_parent->BuildNetListBase() );
// Reset the connection type indicator
objectsConnectedList->ResetConnectionsType();
unsigned lastItemIdx;
unsigned nextItemIdx = lastItemIdx = 0;
int MinConn = NOC;
/* Check that a pin appears in only one net. This check is necessary
* because multi-unit components that have shared pins can be wired to
* different nets.
*/
std::unordered_map<wxString, wxString> pin_to_net_map;
/* The netlist generated by SCH_EDIT_FRAME::BuildNetListBase is sorted
* by net number, which means we can group netlist items into ranges
* that live in the same net. The range from nextItem to the current
* item (exclusive) needs to be checked against the current item. The
* lastItem variable is used as a helper to pass the last item's number
* from one loop iteration to the next, which simplifies the initial
* pass.
*/
for( unsigned itemIdx = 0; itemIdx < objectsConnectedList->size(); itemIdx++ )
{
auto item = objectsConnectedList->GetItem( itemIdx );
auto lastItem = objectsConnectedList->GetItem( lastItemIdx );
auto lastNet = lastItem->GetNet();
auto net = item->GetNet();
wxASSERT_MSG( lastNet <= net, wxT( "Netlist not correctly ordered" ) );
if( lastNet != net )
{
// New net found:
MinConn = NOC;
nextItemIdx = itemIdx;
}
switch( item->m_Type )
{
// These items do not create erc problems
case NET_ITEM_UNSPECIFIED:
case NET_SEGMENT:
case NET_BUS:
case NET_JUNCTION:
case NET_LABEL:
case NET_BUSLABELMEMBER:
case NET_PINLABEL:
case NET_GLOBBUSLABELMEMBER:
break;
case NET_HIERLABEL:
case NET_HIERBUSLABELMEMBER:
case NET_SHEETLABEL:
case NET_SHEETBUSLABELMEMBER:
// ERC problems when pin sheets do not match hierarchical labels.
// Each pin sheet must match a hierarchical label
// Each hierarchical label must match a pin sheet
objectsConnectedList->TestforNonOrphanLabel( itemIdx, nextItemIdx );
break;
case NET_GLOBLABEL:
if( m_tstUniqueGlobalLabels )
objectsConnectedList->TestforNonOrphanLabel( itemIdx, nextItemIdx );
break;
case NET_NOCONNECT:
// ERC problems when a noconnect symbol is connected to more than one pin.
MinConn = NET_NC;
if( objectsConnectedList->CountPinsInNet( nextItemIdx ) > 1 )
Diagnose( item, NULL, MinConn, UNC );
break;
case NET_PIN:
{
// Check if this pin has appeared before on a different net
if( item->m_Link )
{
auto ref = item->GetComponentParent()->GetRef( &item->m_SheetPath );
wxString pin_name = ref + "_" + item->m_PinNum;
if( pin_to_net_map.count( pin_name ) == 0 )
{
pin_to_net_map[pin_name] = item->GetNetName();
}
else if( pin_to_net_map[pin_name] != item->GetNetName() )
{
SCH_MARKER* marker = new SCH_MARKER();
marker->SetTimeStamp( GetNewTimeStamp() );
marker->SetData( ERCE_DIFFERENT_UNIT_NET, item->m_Start,
wxString::Format( _( "Pin %s on %s is connected to both %s and %s" ),
item->m_PinNum, ref, pin_to_net_map[pin_name], item->GetNetName() ),
item->m_Start );
marker->SetMarkerType( MARKER_BASE::MARKER_ERC );
marker->SetErrorLevel( MARKER_BASE::MARKER_SEVERITY_ERROR );
item->m_SheetPath.LastScreen()->Append( marker );
}
}
// Look for ERC problems between pins:
TestOthersItems( objectsConnectedList.get(), itemIdx, nextItemIdx, &MinConn );
break;
}
}
lastItemIdx = itemIdx;
}
// Test similar labels (i;e. labels which are identical when
// using case insensitive comparisons)
if( m_TestSimilarLabels )
objectsConnectedList->TestforSimilarLabels();
// Displays global results:
updateMarkerCounts( &screens );
// Display diags:
DisplayERC_MarkersList();
// Display new markers:
m_parent->GetCanvas()->Refresh();
// Display message
aReporter.ReportTail( _( "Finished" ), REPORTER::RPT_INFO );
if( m_writeErcFile )
{
fn = g_RootSheet->GetScreen()->GetFileName();
fn.SetExt( wxT( "erc" ) );
wxFileDialog dlg( this, _( "ERC File" ), fn.GetPath(), fn.GetFullName(),
ErcFileWildcard(), wxFD_SAVE );
if( dlg.ShowModal() == wxID_CANCEL )
return;
if( WriteDiagnosticERC( dlg.GetPath() ) )
ExecuteFile( this, Pgm().GetEditorName(), QuoteFullPath( fn ) );
}
}
wxDialog* InvokeDialogERC( SCH_EDIT_FRAME* aCaller )
{
// This is a modeless dialog, so new it rather than instantiating on stack.
DIALOG_ERC* dlg = new DIALOG_ERC( aCaller );
dlg->Show( true );
return dlg; // wxDialog is information hiding about DIALOG_ERC.
}
|