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 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
|
/**
* @file component_references_lister.cpp
* @brief functions to create a component flat list and to annotate schematic.
*/
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 1992-2018 jean-pierre Charras <jp.charras at wanadoo.fr>
* Copyright (C) 1992-2011 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 1992-2018 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
*/
#include <wx/regex.h>
#include <algorithm>
#include <vector>
#include <unordered_set>
#include <fctsys.h>
#include <kicad_string.h>
#include <sch_edit_frame.h>
#include <sch_reference_list.h>
#include <sch_component.h>
#include <reporter.h>
//#define USE_OLD_ALGO
void SCH_REFERENCE_LIST::RemoveItem( unsigned int aIndex )
{
if( aIndex < componentFlatList.size() )
componentFlatList.erase( componentFlatList.begin() + aIndex );
}
bool SCH_REFERENCE_LIST::sortByXPosition( const SCH_REFERENCE& item1,
const SCH_REFERENCE& item2 )
{
int ii = item1.CompareRef( item2 );
if( ii == 0 )
ii = item1.m_SheetNum - item2.m_SheetNum;
if( ii == 0 )
ii = item1.m_CmpPos.x - item2.m_CmpPos.x;
if( ii == 0 )
ii = item1.m_CmpPos.y - item2.m_CmpPos.y;
if( ii == 0 )
ii = item1.m_TimeStamp - item2.m_TimeStamp;
return ii < 0;
}
bool SCH_REFERENCE_LIST::sortByYPosition( const SCH_REFERENCE& item1,
const SCH_REFERENCE& item2 )
{
int ii = item1.CompareRef( item2 );
if( ii == 0 )
ii = item1.m_SheetNum - item2.m_SheetNum;
if( ii == 0 )
ii = item1.m_CmpPos.y - item2.m_CmpPos.y;
if( ii == 0 )
ii = item1.m_CmpPos.x - item2.m_CmpPos.x;
if( ii == 0 )
ii = item1.m_TimeStamp - item2.m_TimeStamp;
return ii < 0;
}
bool SCH_REFERENCE_LIST::sortByRefAndValue( const SCH_REFERENCE& item1,
const SCH_REFERENCE& item2 )
{
int ii = item1.CompareRef( item2 );
if( ii == 0 )
ii = item1.CompareValue( item2 );
if( ii == 0 )
ii = item1.m_Unit - item2.m_Unit;
if( ii == 0 )
ii = item1.m_SheetNum - item2.m_SheetNum;
if( ii == 0 )
ii = item1.m_CmpPos.x - item2.m_CmpPos.x;
if( ii == 0 )
ii = item1.m_CmpPos.y - item2.m_CmpPos.y;
if( ii == 0 )
ii = item1.m_TimeStamp - item2.m_TimeStamp;
return ii < 0;
}
bool SCH_REFERENCE_LIST::sortByReferenceOnly( const SCH_REFERENCE& item1,
const SCH_REFERENCE& item2 )
{
int ii;
ii = RefDesStringCompare( item1.GetRef(), item2.GetRef() );
if( ii == 0 )
{
ii = item1.m_RootCmp->GetField( VALUE )->GetText().CmpNoCase( item2.m_RootCmp->GetField( VALUE )->GetText() );
}
if( ii == 0 )
{
ii = item1.m_Unit - item2.m_Unit;
}
return ii < 0;
}
bool SCH_REFERENCE_LIST::sortByTimeStamp( const SCH_REFERENCE& item1,
const SCH_REFERENCE& item2 )
{
int ii = item1.m_SheetPath.Cmp( item2.m_SheetPath );
if( ii == 0 )
ii = item1.m_TimeStamp - item2.m_TimeStamp;
return ii < 0;
}
int SCH_REFERENCE_LIST::FindUnit( size_t aIndex, int aUnit )
{
int NumRef;
NumRef = componentFlatList[aIndex].m_NumRef;
for( size_t ii = 0; ii < componentFlatList.size(); ii++ )
{
if( ( aIndex == ii )
|| ( componentFlatList[ii].m_IsNew )
|| ( componentFlatList[ii].m_NumRef != NumRef )
|| ( componentFlatList[aIndex].CompareRef( componentFlatList[ii] ) != 0 ) )
continue;
if( componentFlatList[ii].m_Unit == aUnit )
return (int) ii;
}
return -1;
}
void SCH_REFERENCE_LIST::RemoveSubComponentsFromList()
{
SCH_COMPONENT* libItem;
wxString oldName;
wxString currName;
// The component list **MUST** be sorted by reference and by unit number
// in order to find all parts of a component
SortByReferenceOnly();
for( unsigned ii = 0; ii < componentFlatList.size(); ii++ )
{
libItem = componentFlatList[ii].m_RootCmp;
if( libItem == NULL )
continue;
currName = componentFlatList[ii].GetRef();
if( !oldName.IsEmpty() )
{
if( oldName == currName ) // currName is a subpart of oldName: remove it
{
componentFlatList.erase( componentFlatList.begin() + ii );
ii--;
}
}
oldName = currName;
}
}
void SCH_REFERENCE_LIST::GetRefsInUse( int aIndex, std::vector< int >& aIdList, int aMinRefId )
{
aIdList.clear();
for( unsigned ii = 0; ii < componentFlatList.size(); ii++ )
{
if( ( componentFlatList[aIndex].CompareRef( componentFlatList[ii] ) == 0 )
&& ( componentFlatList[ii].m_NumRef >= aMinRefId ) )
aIdList.push_back( componentFlatList[ii].m_NumRef );
}
sort( aIdList.begin(), aIdList.end() );
// Ensure each reference number appears only once. If there are components with
// multiple parts per package the same number will be stored for each part.
std::vector< int >::iterator it = unique( aIdList.begin(), aIdList.end() );
// Using the C++ unique algorithm only moves the duplicate entries to the end of
// of the array. This removes the duplicate entries from the array.
aIdList.resize( it - aIdList.begin() );
}
int SCH_REFERENCE_LIST::GetLastReference( int aIndex, int aMinValue )
{
int lastNumber = aMinValue;
for( unsigned ii = 0; ii < componentFlatList.size(); ii++ )
{
// search only for the current reference prefix:
if( componentFlatList[aIndex].CompareRef( componentFlatList[ii] ) != 0 )
continue;
// update max value for the current reference prefix
if( lastNumber < componentFlatList[ii].m_NumRef )
lastNumber = componentFlatList[ii].m_NumRef;
}
return lastNumber;
}
int SCH_REFERENCE_LIST::CreateFirstFreeRefId( std::vector<int>& aIdList, int aFirstValue )
{
int expectedId = aFirstValue;
// We search for expected Id a value >= aFirstValue.
// Skip existing Id < aFirstValue
unsigned ii = 0;
for( ; ii < aIdList.size(); ii++ )
{
if( expectedId <= aIdList[ii] )
break;
}
// Ids are sorted by increasing value, from aFirstValue
// So we search from aFirstValue the first not used value, i.e. the first hole in list.
for( ; ii < aIdList.size(); ii++ )
{
if( expectedId != aIdList[ii] ) // This id is not yet used.
{
// Insert this free Id, in order to keep list sorted
aIdList.insert( aIdList.begin() + ii, expectedId );
return expectedId;
}
expectedId++;
}
// All existing Id are tested, and all values are found in use.
// So Create a new one.
aIdList.push_back( expectedId );
return expectedId;
}
// A helper function to build a full reference string of a SCH_REFERENCE item
wxString buildFullReference( const SCH_REFERENCE& aItem, int aUnitNumber = -1 )
{
wxString fullref;
fullref = aItem.GetRef() + aItem.GetRefNumber();
if( aUnitNumber < 0 )
fullref << ".." << aItem.GetUnit();
else
fullref << ".." << aUnitNumber;
return fullref;
}
void SCH_REFERENCE_LIST::Annotate( bool aUseSheetNum, int aSheetIntervalId, int aStartNumber,
SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap )
{
if ( componentFlatList.size() == 0 )
return;
int LastReferenceNumber = 0;
int NumberOfUnits, Unit;
/* calculate index of the first component with the same reference prefix
* than the current component. All components having the same reference
* prefix will receive a reference number with consecutive values:
* IC .. will be set to IC4, IC4, IC5 ...
*/
unsigned first = 0;
// calculate the last used number for this reference prefix:
#ifdef USE_OLD_ALGO
int minRefId = 0;
// when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
if( aUseSheetNum )
minRefId = componentFlatList[first].m_SheetNum * aSheetIntervalId;
LastReferenceNumber = GetLastReference( first, minRefId );
#else
int minRefId;
// when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
if( aUseSheetNum )
minRefId = componentFlatList[first].m_SheetNum * aSheetIntervalId + 1;
else
minRefId = aStartNumber + 1;
// For multi units components, when "keep order of multi unit" option is selected,
// store the list of already used full references.
// The algorithm try to allocate the new reference to components having the same
// old reference.
// This algo works fine as long as the previous annotation has no duplicates.
// But when a hierarchy is reannotated with this option, the previous anotation can
// have duplicate references, and obviously we must fix these duplicate.
// therefore do not try to allocate a full reference more than once when trying
// to keep this order of multi units.
// inUseRefs keep trace of previously allocated references
std::unordered_set<wxString> inUseRefs;
// This is the list of all Id already in use for a given reference prefix.
// Will be refilled for each new reference prefix.
std::vector<int>idList;
GetRefsInUse( first, idList, minRefId );
#endif
for( unsigned ii = 0; ii < componentFlatList.size(); ii++ )
{
if( componentFlatList[ii].m_Flag )
continue;
// Check whether this component is in aLockedUnitMap.
SCH_REFERENCE_LIST* lockedList = NULL;
for( SCH_MULTI_UNIT_REFERENCE_MAP::value_type& pair : aLockedUnitMap )
{
unsigned n_refs = pair.second.GetCount();
for( unsigned thisRefI = 0; thisRefI < n_refs; ++thisRefI )
{
SCH_REFERENCE &thisRef = pair.second[thisRefI];
if( thisRef.IsSameInstance( componentFlatList[ii] ) )
{
lockedList = &pair.second;
break;
}
}
if( lockedList != NULL ) break;
}
if( ( componentFlatList[first].CompareRef( componentFlatList[ii] ) != 0 )
|| ( aUseSheetNum && ( componentFlatList[first].m_SheetNum != componentFlatList[ii].m_SheetNum ) ) )
{
// New reference found: we need a new ref number for this reference
first = ii;
#ifdef USE_OLD_ALGO
minRefId = 0;
// when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
if( aUseSheetNum )
minRefId = componentFlatList[ii].m_SheetNum * aSheetIntervalId;
LastReferenceNumber = GetLastReference( ii, minRefId );
#else
// when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
if( aUseSheetNum )
minRefId = componentFlatList[ii].m_SheetNum * aSheetIntervalId + 1;
else
minRefId = aStartNumber + 1;
GetRefsInUse( first, idList, minRefId );
#endif
}
// Annotation of one part per package components (trivial case).
if( componentFlatList[ii].GetLibPart()->GetUnitCount() <= 1 )
{
if( componentFlatList[ii].m_IsNew )
{
#ifdef USE_OLD_ALGO
LastReferenceNumber++;
#else
LastReferenceNumber = CreateFirstFreeRefId( idList, minRefId );
#endif
componentFlatList[ii].m_NumRef = LastReferenceNumber;
}
componentFlatList[ii].m_Unit = 1;
componentFlatList[ii].m_Flag = 1;
componentFlatList[ii].m_IsNew = false;
continue;
}
// Annotation of multi-unit parts ( n units per part ) (complex case)
NumberOfUnits = componentFlatList[ii].GetLibPart()->GetUnitCount();
if( componentFlatList[ii].m_IsNew )
{
#ifdef USE_OLD_ALGO
LastReferenceNumber++;
#else
LastReferenceNumber = CreateFirstFreeRefId( idList, minRefId );
#endif
componentFlatList[ii].m_NumRef = LastReferenceNumber;
if( !componentFlatList[ii].IsUnitsLocked() )
componentFlatList[ii].m_Unit = 1;
componentFlatList[ii].m_Flag = 1;
}
// If this component is in aLockedUnitMap, copy the annotation to all
// components that are not it
if( lockedList != NULL )
{
unsigned n_refs = lockedList->GetCount();
for( unsigned thisRefI = 0; thisRefI < n_refs; ++thisRefI )
{
SCH_REFERENCE &thisRef = (*lockedList)[thisRefI];
if( thisRef.IsSameInstance( componentFlatList[ii] ) )
{
// This is the component we're currently annotating. Hold the unit!
componentFlatList[ii].m_Unit = thisRef.m_Unit;
// lock this new full reference
inUseRefs.insert( buildFullReference( componentFlatList[ii] ) );
}
if( thisRef.CompareValue( componentFlatList[ii] ) != 0 )
continue;
if( thisRef.CompareLibName( componentFlatList[ii] ) != 0 )
continue;
// Find the matching component
for( unsigned jj = ii + 1; jj < componentFlatList.size(); jj++ )
{
if( ! thisRef.IsSameInstance( componentFlatList[jj] ) )
continue;
wxString ref_candidate = buildFullReference( componentFlatList[ii], thisRef.m_Unit );
// propagate the new reference and unit selection to the "old" component,
// if this new full reference is not already used (can happens when initial
// multiunits components have duplicate references)
if( inUseRefs.find( ref_candidate ) == inUseRefs.end() )
{
componentFlatList[jj].m_NumRef = componentFlatList[ii].m_NumRef;
componentFlatList[jj].m_Unit = thisRef.m_Unit;
componentFlatList[jj].m_IsNew = false;
componentFlatList[jj].m_Flag = 1;
// lock this new full reference
inUseRefs.insert( ref_candidate );
break;
}
}
}
}
else
{
/* search for others units of this component.
* we search for others parts that have the same value and the same
* reference prefix (ref without ref number)
*/
for( Unit = 1; Unit <= NumberOfUnits; Unit++ )
{
if( componentFlatList[ii].m_Unit == Unit )
continue;
int found = FindUnit( ii, Unit );
if( found >= 0 )
continue; // this unit exists for this reference (unit already annotated)
// Search a component to annotate ( same prefix, same value, not annotated)
for( unsigned jj = ii + 1; jj < componentFlatList.size(); jj++ )
{
if( componentFlatList[jj].m_Flag ) // already tested
continue;
if( componentFlatList[ii].CompareRef( componentFlatList[jj] ) != 0 )
continue;
if( componentFlatList[jj].CompareValue( componentFlatList[ii] ) != 0 )
continue;
if( componentFlatList[jj].CompareLibName( componentFlatList[ii] ) != 0 )
continue;
if( !componentFlatList[jj].m_IsNew )
continue;
// Component without reference number found, annotate it if possible
if( !componentFlatList[jj].IsUnitsLocked()
|| ( componentFlatList[jj].m_Unit == Unit ) )
{
componentFlatList[jj].m_NumRef = componentFlatList[ii].m_NumRef;
componentFlatList[jj].m_Unit = Unit;
componentFlatList[jj].m_Flag = 1;
componentFlatList[jj].m_IsNew = false;
break;
}
}
}
}
}
}
int SCH_REFERENCE_LIST::CheckAnnotation( REPORTER& aReporter )
{
int error = 0;
wxString tmp;
wxString msg;
SortByRefAndValue();
// Spiit reference designators into name (prefix) and number: IC1 becomes IC, and 1.
SplitReferences();
// count not yet annotated items or annotation error.
for( unsigned ii = 0; ii < componentFlatList.size(); ii++ )
{
msg.Empty();
tmp.Empty();
if( componentFlatList[ii].m_IsNew ) // Not yet annotated
{
if( componentFlatList[ii].m_NumRef >= 0 )
tmp << componentFlatList[ii].m_NumRef;
else
tmp = wxT( "?" );
if( ( componentFlatList[ii].m_Unit > 0 )
&& ( componentFlatList[ii].m_Unit < 0x7FFFFFFF ) )
{
msg.Printf( _( "Item not annotated: %s%s (unit %d)\n" ),
GetChars( componentFlatList[ii].GetRef() ),
GetChars( tmp ),
componentFlatList[ii].m_Unit );
}
else
{
msg.Printf( _( "Item not annotated: %s%s\n" ),
GetChars( componentFlatList[ii].GetRef() ),
GetChars( tmp ) );
}
aReporter.Report( msg, REPORTER::RPT_WARNING );
error++;
break;
}
// Error if unit number selected does not exist ( greater than the number of
// parts in the component ). This can happen if a component has changed in a
// library after a previous annotation.
if( std::max( componentFlatList[ii].GetLibPart()->GetUnitCount(), 1 )
< componentFlatList[ii].m_Unit )
{
if( componentFlatList[ii].m_NumRef >= 0 )
tmp << componentFlatList[ii].m_NumRef;
else
tmp = wxT( "?" );
msg.Printf( _( "Error: symbol %s%s unit %d and symbol has only %d units defined\n" ),
GetChars( componentFlatList[ii].GetRef() ),
GetChars( tmp ),
componentFlatList[ii].m_Unit,
componentFlatList[ii].GetLibPart()->GetUnitCount() );
aReporter.Report( msg, REPORTER::RPT_ERROR );
error++;
break;
}
}
if( error )
return error;
// count the duplicated elements (if all are annotated)
int imax = componentFlatList.size() - 1;
for( int ii = 0; (ii < imax) && (error < 4); ii++ )
{
msg.Empty();
tmp.Empty();
if( ( componentFlatList[ii].CompareRef( componentFlatList[ii + 1] ) != 0 )
|| ( componentFlatList[ii].m_NumRef != componentFlatList[ii + 1].m_NumRef ) )
continue;
// Same reference found. If same unit, error!
if( componentFlatList[ii].m_Unit == componentFlatList[ii + 1].m_Unit )
{
if( componentFlatList[ii].m_NumRef >= 0 )
tmp << componentFlatList[ii].m_NumRef;
else
tmp = wxT( "?" );
if( ( componentFlatList[ii].m_Unit > 0 )
&& ( componentFlatList[ii].m_Unit < 0x7FFFFFFF ) )
{
msg.Printf( _( "Multiple item %s%s (unit %d)\n" ),
GetChars( componentFlatList[ii].GetRef() ),
GetChars( tmp ),
componentFlatList[ii].m_Unit );
}
else
{
msg.Printf( _( "Multiple item %s%s\n" ),
GetChars( componentFlatList[ii].GetRef() ),
GetChars( tmp ) );
}
aReporter.Report( msg, REPORTER::RPT_ERROR );
error++;
continue;
}
/* Test error if units are different but number of parts per package
* too high (ex U3 ( 1 part) and we find U3B this is an error) */
if( componentFlatList[ii].GetLibPart()->GetUnitCount()
!= componentFlatList[ii + 1].GetLibPart()->GetUnitCount() )
{
if( componentFlatList[ii].m_NumRef >= 0 )
tmp << componentFlatList[ii].m_NumRef;
else
tmp = wxT( "?" );
if( ( componentFlatList[ii].m_Unit > 0 )
&& ( componentFlatList[ii].m_Unit < 0x7FFFFFFF ) )
{
msg.Printf( _( "Multiple item %s%s (unit %d)\n" ),
GetChars( componentFlatList[ii].GetRef() ),
GetChars( tmp ),
componentFlatList[ii].m_Unit );
}
else
{
msg.Printf( _( "Multiple item %s%s\n" ),
GetChars( componentFlatList[ii].GetRef() ),
GetChars( tmp ) );
}
aReporter.Report( msg, REPORTER::RPT_ERROR );
error++;
}
// Error if values are different between units, for the same reference
int next = ii + 1;
if( componentFlatList[ii].CompareValue( componentFlatList[next] ) != 0 )
{
msg.Printf( _( "Different values for %s%d%s (%s) and %s%d%s (%s)" ),
GetChars( componentFlatList[ii].GetRef() ),
componentFlatList[ii].m_NumRef,
GetChars( LIB_PART::SubReference(
componentFlatList[ii].m_Unit ) ),
GetChars( componentFlatList[ii].m_Value->GetText() ),
GetChars( componentFlatList[next].GetRef() ),
componentFlatList[next].m_NumRef,
GetChars( LIB_PART::SubReference(
componentFlatList[next].m_Unit ) ),
GetChars( componentFlatList[next].m_Value->GetText() ) );
aReporter.Report( msg, REPORTER::RPT_ERROR );
error++;
}
}
// count the duplicated time stamps
SortByTimeStamp();
for( int ii = 0; ( ii < imax ) && ( error < 4 ); ii++ )
{
if( ( componentFlatList[ii].m_TimeStamp != componentFlatList[ii + 1].m_TimeStamp )
|| ( componentFlatList[ii].GetSheetPath() != componentFlatList[ii + 1].GetSheetPath() ) )
continue;
// Same time stamp found.
wxString full_path;
full_path.Printf( wxT( "%s%8.8X" ),
GetChars( componentFlatList[ii].GetSheetPath().Path() ),
componentFlatList[ii].m_TimeStamp );
msg.Printf( _( "Duplicate time stamp (%s) for %s%d and %s%d" ),
GetChars( full_path ),
GetChars( componentFlatList[ii].GetRef() ), componentFlatList[ii].m_NumRef,
GetChars( componentFlatList[ii + 1].GetRef() ),
componentFlatList[ii + 1].m_NumRef );
aReporter.Report( msg, REPORTER::RPT_WARNING );
error++;
}
return error;
}
SCH_REFERENCE::SCH_REFERENCE( SCH_COMPONENT* aComponent, LIB_PART* aLibPart,
SCH_SHEET_PATH& aSheetPath )
{
wxASSERT( aComponent != NULL && aLibPart != NULL );
m_RootCmp = aComponent;
m_Entry = aLibPart;
m_Unit = aComponent->GetUnitSelection( &aSheetPath );
m_SheetPath = aSheetPath;
m_IsNew = false;
m_Flag = 0;
m_TimeStamp = aComponent->GetTimeStamp();
m_CmpPos = aComponent->GetPosition();
m_SheetNum = 0;
if( aComponent->GetRef( &aSheetPath ).IsEmpty() )
aComponent->SetRef( &aSheetPath, wxT( "DefRef?" ) );
wxString ref = aComponent->GetRef( &aSheetPath );
SetRef( ref );
m_NumRef = -1;
if( aComponent->GetField( VALUE )->GetText().IsEmpty() )
aComponent->GetField( VALUE )->SetText( wxT( "~" ) );
m_Value = aComponent->GetField( VALUE );
}
void SCH_REFERENCE::Annotate()
{
if( m_NumRef < 0 )
m_Ref += '?';
else
{
m_Ref = TO_UTF8( GetRef() << GetRefNumber() );
}
m_RootCmp->SetRef( &m_SheetPath, FROM_UTF8( m_Ref.c_str() ) );
m_RootCmp->SetUnit( m_Unit );
m_RootCmp->SetUnitSelection( &m_SheetPath, m_Unit );
}
void SCH_REFERENCE::Split()
{
std::string refText = GetRefStr();
m_NumRef = -1;
int ll = refText.length() - 1;
if( refText[ll] == '?' )
{
m_IsNew = true;
if( !IsUnitsLocked() )
m_Unit = 0x7FFFFFFF;
refText.erase( ll ); // delete last char
SetRefStr( refText );
}
else if( isdigit( refText[ll] ) == 0 )
{
m_IsNew = true;
if( !IsUnitsLocked() )
m_Unit = 0x7FFFFFFF;
}
else
{
while( ll >= 0 )
{
if( (refText[ll] <= ' ' ) || isdigit( refText[ll] ) )
ll--;
else
{
if( isdigit( refText[ll + 1] ) )
{
// null terminated C string into cp
const char* cp = refText.c_str() + ll + 1;
m_NumRef = atoi( cp );
}
refText.erase( ll+1 ); // delete from ll+1 to end
break;
}
}
SetRefStr( refText );
}
}
wxString SCH_REFERENCE_LIST::Shorthand( std::vector<SCH_REFERENCE> aList )
{
wxString retVal;
std::sort( aList.begin(), aList.end(),
[]( const SCH_REFERENCE& lhs, const SCH_REFERENCE& rhs ) -> bool
{
wxString lhRef( lhs.GetRef() << lhs.GetRefNumber() );
wxString rhRef( rhs.GetRef() << rhs.GetRefNumber() );
return RefDesStringCompare( lhRef, rhRef ) < 0;
} );
size_t i = 0;
while( i < aList.size() )
{
wxString ref = aList[ i ].GetRef();
size_t j = i;
while( j + 1 < aList.size() && aList[ j + 1 ].GetRef() == ref )
j = j + 1;
if( !retVal.IsEmpty() )
retVal << wxT( ", " );
if( j == i )
{
retVal << ref << aList[ i ].GetRefNumber();
}
else if( j == i + 1 )
{
retVal << ref << aList[ i ].GetRefNumber();
retVal << wxT( ", " );
retVal << ref << aList[ j ].GetRefNumber();
}
else
{
retVal << ref << aList[ i ].GetRefNumber();
retVal << wxT( " - " );
retVal << ref << aList[ j ].GetRefNumber();
}
i = j + 1;
}
return retVal;
}
|