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 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2011 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 sch_sheet_path.cpp
* @brief SCH_SHEET_PATH class implementation.
*/
#include <fctsys.h>
#include <dlist.h>
#include <sch_screen.h>
#include <sch_item_struct.h>
#include <sch_reference_list.h>
#include <class_library.h>
#include <sch_sheet_path.h>
#include <sch_component.h>
#include <template_fieldnames.h>
#include <dialogs/dialog_schematic_find.h>
#include <wx/filename.h>
SCH_SHEET_PATH::SCH_SHEET_PATH()
{
m_pageNumber = 0;
}
int SCH_SHEET_PATH::Cmp( const SCH_SHEET_PATH& aSheetPathToTest ) const
{
if( size() > aSheetPathToTest.size() )
return 1;
if( size() < aSheetPathToTest.size() )
return -1;
//otherwise, same number of sheets.
for( unsigned i = 0; i < size(); i++ )
{
if( at( i )->GetTimeStamp() > aSheetPathToTest.at( i )->GetTimeStamp() )
return 1;
if( at( i )->GetTimeStamp() < aSheetPathToTest.at( i )->GetTimeStamp() )
return -1;
}
return 0;
}
SCH_SHEET* SCH_SHEET_PATH::Last() const
{
if( !empty() )
return at( size() - 1 );
return NULL;
}
SCH_SCREEN* SCH_SHEET_PATH::LastScreen() const
{
SCH_SHEET* lastSheet = Last();
if( lastSheet )
return lastSheet->GetScreen();
return NULL;
}
SCH_ITEM* SCH_SHEET_PATH::LastDrawList() const
{
SCH_SHEET* lastSheet = Last();
if( lastSheet && lastSheet->GetScreen() )
return lastSheet->GetScreen()->GetDrawItems();
return NULL;
}
SCH_ITEM* SCH_SHEET_PATH::FirstDrawList() const
{
SCH_ITEM* item = NULL;
if( !empty() && at( 0 )->GetScreen() )
item = at( 0 )->GetScreen()->GetDrawItems();
/* @fixme - These lists really should be one of the boost pointer containers. This
* is a brain dead hack to allow reverse iteration of EDA_ITEM linked
* list.
*/
SCH_ITEM* lastItem = NULL;
while( item )
{
lastItem = item;
item = item->Next();
}
return lastItem;
}
wxString SCH_SHEET_PATH::Path() const
{
wxString s, t;
s = wxT( "/" ); // This is the root path
// start at 1 to avoid the root sheet,
// which does not need to be added to the path
// it's timestamp changes anyway.
for( unsigned i = 1; i < size(); i++ )
{
t.Printf( _( "%8.8lX/" ), (long unsigned) at( i )->GetTimeStamp() );
s = s + t;
}
return s;
}
wxString SCH_SHEET_PATH::PathHumanReadable() const
{
wxString s;
s = wxT( "/" );
// start at 1 to avoid the root sheet, as above.
for( unsigned i = 1; i < size(); i++ )
{
s = s + at( i )->GetName() + wxT( "/" );
}
return s;
}
void SCH_SHEET_PATH::UpdateAllScreenReferences()
{
EDA_ITEM* t = LastDrawList();
while( t )
{
if( t->Type() == SCH_COMPONENT_T )
{
SCH_COMPONENT* component = (SCH_COMPONENT*) t;
component->GetField( REFERENCE )->SetText( component->GetRef( this ) );
component->UpdateUnit( component->GetUnitSelection( this ) );
}
t = t->Next();
}
}
void SCH_SHEET_PATH::GetComponents( SCH_REFERENCE_LIST& aReferences, bool aIncludePowerSymbols,
bool aForceIncludeOrphanComponents )
{
for( SCH_ITEM* item = LastDrawList(); item; item = item->Next() )
{
if( item->Type() == SCH_COMPONENT_T )
{
SCH_COMPONENT* component = (SCH_COMPONENT*) item;
// Skip pseudo components, which have a reference starting with #. This mainly
// affects power symbols.
if( !aIncludePowerSymbols && component->GetRef( this )[0] == wxT( '#' ) )
continue;
LIB_PART* part = component->GetPartRef().lock().get();
if( part || aForceIncludeOrphanComponents )
{
SCH_REFERENCE schReference( component, part, *this );
schReference.SetSheetNumber( m_pageNumber );
aReferences.AddItem( schReference );
}
}
}
}
void SCH_SHEET_PATH::GetMultiUnitComponents( SCH_MULTI_UNIT_REFERENCE_MAP& aRefList,
bool aIncludePowerSymbols )
{
for( SCH_ITEM* item = LastDrawList(); item; item = item->Next() )
{
if( item->Type() != SCH_COMPONENT_T )
continue;
SCH_COMPONENT* component = (SCH_COMPONENT*) item;
// Skip pseudo components, which have a reference starting with #. This mainly
// affects power symbols.
if( !aIncludePowerSymbols && component->GetRef( this )[0] == wxT( '#' ) )
continue;
LIB_PART* part = component->GetPartRef().lock().get();
if( part && part->GetUnitCount() > 1 )
{
SCH_REFERENCE schReference = SCH_REFERENCE( component, part, *this );
schReference.SetSheetNumber( m_pageNumber );
wxString reference_str = schReference.GetRef();
// Never lock unassigned references
if( reference_str[reference_str.Len() - 1] == '?' )
continue;
aRefList[reference_str].AddItem( schReference );
}
}
}
SCH_ITEM* SCH_SHEET_PATH::FindNextItem( KICAD_T aType, SCH_ITEM* aLastItem, bool aWrap ) const
{
bool hasWrapped = false;
bool firstItemFound = false;
SCH_ITEM* drawItem = LastDrawList();
while( drawItem )
{
if( drawItem->Type() == aType )
{
if( !aLastItem || firstItemFound )
{
return drawItem;
}
else if( !firstItemFound && drawItem == aLastItem )
{
firstItemFound = true;
}
}
drawItem = drawItem->Next();
if( !drawItem && aLastItem && aWrap && !hasWrapped )
{
hasWrapped = true;
drawItem = LastDrawList();
}
}
return NULL;
}
SCH_ITEM* SCH_SHEET_PATH::FindPreviousItem( KICAD_T aType, SCH_ITEM* aLastItem, bool aWrap ) const
{
bool hasWrapped = false;
bool firstItemFound = false;
SCH_ITEM* drawItem = FirstDrawList();
while( drawItem )
{
if( drawItem->Type() == aType )
{
if( aLastItem == NULL || firstItemFound )
{
return drawItem;
}
else if( !firstItemFound && drawItem == aLastItem )
{
firstItemFound = true;
}
}
drawItem = drawItem->Back();
if( drawItem == NULL && aLastItem && aWrap && !hasWrapped )
{
hasWrapped = true;
drawItem = FirstDrawList();
}
}
return NULL;
}
bool SCH_SHEET_PATH::SetComponentFootprint( const wxString& aReference, const wxString& aFootPrint,
bool aSetVisible )
{
SCH_SCREEN* screen = LastScreen();
if( screen == NULL )
return false;
return screen->SetComponentFootprint( this, aReference, aFootPrint, aSetVisible );
}
bool SCH_SHEET_PATH::operator==( const SCH_SHEET_PATH& d1 ) const
{
if( size() != d1.size() )
return false;
for( unsigned i = 0; i < size(); i++ )
{
if( at( i ) != d1[i] )
return false;
}
return true;
}
bool SCH_SHEET_PATH::TestForRecursion( const wxString& aSrcFileName,
const wxString& aDestFileName ) const
{
wxFileName rootFn = g_RootSheet->GetFileName();
wxFileName srcFn = aSrcFileName;
wxFileName destFn = aDestFileName;
if( srcFn.IsRelative() )
srcFn.MakeAbsolute( rootFn.GetPath() );
if( destFn.IsRelative() )
destFn.MakeAbsolute( rootFn.GetPath() );
// The source and destination sheet file names cannot be the same.
if( srcFn == destFn )
return true;
/// @todo Store sheet file names with full path, either relative to project path
/// or absolute path. The current design always assumes subsheet files are
/// located in the project folder which may or may not be desirable.
unsigned i = 0;
while( i < size() )
{
wxFileName cmpFn = at( i )->GetFileName();
if( cmpFn.IsRelative() )
cmpFn.MakeAbsolute( rootFn.GetPath() );
// Test if the file name of the destination sheet is in anywhere in this sheet path.
if( cmpFn == destFn )
break;
i++;
}
// The destination sheet file name was not found in the sheet path or the destination
// sheet file name is the root sheet so no recursion is possible.
if( i >= size() || i == 0 )
return false;
// Walk back up to the root sheet to see if the source file name is already a parent in
// the sheet path. If so, recursion will occur.
do
{
i -= 1;
wxFileName cmpFn = at( i )->GetFileName();
if( cmpFn.IsRelative() )
cmpFn.MakeAbsolute( rootFn.GetPath() );
if( cmpFn == srcFn )
return true;
} while( i != 0 );
// The source sheet file name is not a parent of the destination sheet file name.
return false;
}
int SCH_SHEET_PATH::FindSheet( const wxString& aFileName ) const
{
for( unsigned i = 0; i < size(); i++ )
{
if( at( i )->GetFileName().CmpNoCase( aFileName ) == 0 )
return (int)i;
}
return SHEET_NOT_FOUND;
}
SCH_SHEET* SCH_SHEET_PATH::FindSheetByName( const wxString& aSheetName )
{
for( unsigned i = 0; i < size(); i++ )
{
if( at( i )->GetName().CmpNoCase( aSheetName ) == 0 )
return at( i );
}
return NULL;
}
/********************************************************************/
/* Class SCH_SHEET_LIST to handle the list of Sheets in a hierarchy */
/********************************************************************/
SCH_SHEET_LIST::SCH_SHEET_LIST( SCH_SHEET* aSheet )
{
m_isRootSheet = false;
if( aSheet != NULL )
BuildSheetList( aSheet );
}
SCH_SHEET_PATH* SCH_SHEET_LIST::GetSheetByPath( const wxString& aPath, bool aHumanReadable )
{
wxString sheetPath;
for( unsigned i = 0; i < size(); i++ )
{
sheetPath = ( aHumanReadable ) ? at( i ).PathHumanReadable() : at( i ).Path();
if( sheetPath == aPath )
return &at( i );
}
return NULL;
}
void SCH_SHEET_LIST::BuildSheetList( SCH_SHEET* aSheet )
{
wxCHECK_RET( aSheet != NULL, wxT( "Cannot build sheet list from undefined sheet." ) );
if( aSheet == g_RootSheet )
m_isRootSheet = true;
m_currentSheetPath.push_back( aSheet );
/**
* @todo: Schematic page number is currently a left over relic and is generated as
* SCH_SHEET_PATH object is pushed to the list. This only has meaning when
* entire hierarchy is created from the root sheet down.
*/
m_currentSheetPath.SetPageNumber( size() + 1 );
push_back( m_currentSheetPath );
if( aSheet->GetScreen() )
{
EDA_ITEM* item = m_currentSheetPath.LastDrawList();
while( item )
{
if( item->Type() == SCH_SHEET_T )
{
SCH_SHEET* sheet = (SCH_SHEET*) item;
BuildSheetList( sheet );
}
item = item->Next();
}
}
m_currentSheetPath.pop_back();
}
bool SCH_SHEET_LIST::IsModified()
{
for( SCH_SHEET_PATHS_ITER it = begin(); it != end(); ++it )
{
if( (*it).LastScreen() && (*it).LastScreen()->IsModify() )
return true;
}
return false;
}
void SCH_SHEET_LIST::ClearModifyStatus()
{
for( SCH_SHEET_PATHS_ITER it = begin(); it != end(); ++it )
{
if( (*it).LastScreen() )
(*it).LastScreen()->ClrModify();
}
}
void SCH_SHEET_LIST::AnnotatePowerSymbols()
{
// List of reference for power symbols
SCH_REFERENCE_LIST references;
// Map of locked components (not used, but needed by Annotate()
SCH_MULTI_UNIT_REFERENCE_MAP lockedComponents;
// Build the list of power components:
for( SCH_SHEET_PATHS_ITER it = begin(); it != end(); ++it )
{
SCH_SHEET_PATH& spath = *it;
for( EDA_ITEM* item = spath.LastDrawList(); item; item = item->Next() )
{
if( item->Type() != SCH_COMPONENT_T )
continue;
SCH_COMPONENT* component = (SCH_COMPONENT*) item;
LIB_PART* part = component->GetPartRef().lock().get();
if( !part || !part->IsPower() )
continue;
if( part )
{
SCH_REFERENCE schReference( component, part, spath );
references.AddItem( schReference );
}
}
}
// Find duplicate, and silently clear annotation of duplicate
std::map<wxString, int> ref_list; // stores the existing references
for( unsigned ii = 0; ii< references.GetCount(); ++ii )
{
wxString curr_ref = references[ii].GetRef();
if( ref_list.find( curr_ref ) == ref_list.end() )
{
ref_list[curr_ref] = ii;
continue;
}
// Possible duplicate, if the ref ends by a number:
if( curr_ref.Last() < '0' && curr_ref.Last() > '9' )
continue; // not annotated
// Duplicate: clear annotation by removing the number ending the ref
while( curr_ref.Last() >= '0' && curr_ref.Last() <= '9' )
curr_ref.RemoveLast();
references[ii].SetRef( curr_ref );
}
// Break full components reference in name (prefix) and number:
// example: IC1 become IC, and 1
references.SplitReferences();
// Ensure all power symbols have the reference starting by '#'
// (No sure this is really useful)
for( unsigned ii = 0; ii< references.GetCount(); ++ii )
{
if( references[ii].GetRef()[0] != '#' )
{
wxString new_ref = "#" + references[ii].GetRef();
references[ii].SetRef( new_ref );
}
}
// Recalculate and update reference numbers in schematic
references.Annotate( false, 0, 100, lockedComponents );
references.UpdateAnnotation();
}
void SCH_SHEET_LIST::GetComponents( SCH_REFERENCE_LIST& aReferences, bool aIncludePowerSymbols,
bool aForceIncludeOrphanComponents )
{
for( SCH_SHEET_PATHS_ITER it = begin(); it != end(); ++it )
(*it).GetComponents( aReferences, aIncludePowerSymbols, aForceIncludeOrphanComponents );
}
void SCH_SHEET_LIST::GetMultiUnitComponents( SCH_MULTI_UNIT_REFERENCE_MAP &aRefList,
bool aIncludePowerSymbols )
{
for( SCH_SHEET_PATHS_ITER it = begin(); it != end(); ++it )
{
SCH_MULTI_UNIT_REFERENCE_MAP tempMap;
(*it).GetMultiUnitComponents( tempMap );
for( SCH_MULTI_UNIT_REFERENCE_MAP::value_type& pair : tempMap )
{
// Merge this list into the main one
unsigned n_refs = pair.second.GetCount();
for( unsigned thisRef = 0; thisRef < n_refs; ++thisRef )
{
aRefList[pair.first].AddItem( pair.second[thisRef] );
}
}
}
}
SCH_ITEM* SCH_SHEET_LIST::FindNextItem( KICAD_T aType, SCH_SHEET_PATH** aSheetFoundIn,
SCH_ITEM* aLastItem, bool aWrap )
{
bool hasWrapped = false;
bool firstItemFound = false;
SCH_ITEM* drawItem = NULL;
SCH_SHEET_PATHS_ITER it = begin();
while( it != end() )
{
drawItem = (*it).LastDrawList();
while( drawItem )
{
if( drawItem->Type() == aType )
{
if( aLastItem == NULL || firstItemFound )
{
if( aSheetFoundIn )
*aSheetFoundIn = &(*it);
return drawItem;
}
else if( !firstItemFound && drawItem == aLastItem )
{
firstItemFound = true;
}
}
drawItem = drawItem->Next();
}
++it;
if( it == end() && aLastItem && aWrap && !hasWrapped )
{
hasWrapped = true;
it = begin();
}
}
return NULL;
}
SCH_ITEM* SCH_SHEET_LIST::FindPreviousItem( KICAD_T aType, SCH_SHEET_PATH** aSheetFoundIn,
SCH_ITEM* aLastItem, bool aWrap )
{
bool hasWrapped = false;
bool firstItemFound = false;
SCH_ITEM* drawItem = NULL;
SCH_SHEET_PATHS_RITER it = rbegin();
while( it != rend() )
{
drawItem = (*it).FirstDrawList();
while( drawItem )
{
if( drawItem->Type() == aType )
{
if( aLastItem == NULL || firstItemFound )
{
if( aSheetFoundIn )
*aSheetFoundIn = &(*it);
return drawItem;
}
else if( !firstItemFound && drawItem == aLastItem )
{
firstItemFound = true;
}
}
drawItem = drawItem->Back();
}
++it;
if( it == rend() && aLastItem && aWrap && !hasWrapped )
{
hasWrapped = true;
it = rbegin();
}
}
return NULL;
}
bool SCH_SHEET_LIST::SetComponentFootprint( const wxString& aReference,
const wxString& aFootPrint, bool aSetVisible )
{
bool found = false;
for( SCH_SHEET_PATHS_ITER it = begin(); it != end(); ++it )
found = (*it).SetComponentFootprint( aReference, aFootPrint, aSetVisible );
return found;
}
bool SCH_SHEET_LIST::IsComplexHierarchy() const
{
wxString fileName;
for( unsigned i = 0; i < size(); i++ )
{
fileName = at( i ).Last()->GetFileName();
for( unsigned j = 0; j < size(); j++ )
{
if( i == j )
continue;
if( fileName == at( j ).Last()->GetFileName() )
return true;
}
}
return false;
}
bool SCH_SHEET_LIST::TestForRecursion( const SCH_SHEET_LIST& aSrcSheetHierarchy,
const wxString& aDestFileName ) const
{
wxFileName rootFn = g_RootSheet->GetFileName();
wxFileName destFn = aDestFileName;
if( destFn.IsRelative() )
destFn.MakeAbsolute( rootFn.GetPath() );
// Test each SCH_SHEET_PATH in this SCH_SHEET_LIST for potential recursion.
for( unsigned i = 0; i < size(); i++ )
{
// Test each SCH_SHEET_PATH in the source sheet.
for( unsigned j = 0; j < aSrcSheetHierarchy.size(); j++ )
{
const SCH_SHEET_PATH* sheetPath = &aSrcSheetHierarchy[j];
for( unsigned k = 0; k < sheetPath->size(); k++ )
{
if( at( i ).TestForRecursion( sheetPath->GetSheet( k )->GetFileName(),
aDestFileName ) )
return true;
}
}
}
// The source sheet file can safely be added to the destination sheet file.
return false;
}
SCH_SHEET* SCH_SHEET_LIST::FindSheetByName( const wxString& aSheetName )
{
for( unsigned i = 0; i < size(); i++ )
{
SCH_SHEET* sheet = at( i ).FindSheetByName( aSheetName );
if( sheet )
return sheet;
}
return NULL;
}
|