KiCad PCB EDA Suite
component_references_lister.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 1992-2018 jean-pierre Charras <jp.charras at wanadoo.fr>
5 * Copyright (C) 1992-2011 Wayne Stambaugh <[email protected]>
6 * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
31#include <sch_reference_list.h>
32#include <core/kicad_algo.h>
33
34#include <wx/regex.h>
35#include <algorithm>
36#include <vector>
37#include <unordered_set>
38
39#include <string_utils.h>
40#include <erc_settings.h>
41#include <sch_symbol.h>
42#include <sch_edit_frame.h>
43
44
45void SCH_REFERENCE_LIST::RemoveItem( unsigned int aIndex )
46{
47 if( aIndex < m_flatList.size() )
48 m_flatList.erase( m_flatList.begin() + aIndex );
49}
50
51
53{
54 for( unsigned ii = 0; ii < GetCount(); ii++ )
55 {
56 if( m_flatList[ii].IsSameInstance( aItem ) )
57 return true;
58 }
59
60 return false;
61}
62
63
65{
66 int ii = item1.CompareRef( item2 );
67
68 if( ii == 0 )
69 ii = item1.m_sheetNum - item2.m_sheetNum;
70
71 if( ii == 0 )
72 ii = item1.m_symbolPos.x - item2.m_symbolPos.x;
73
74 if( ii == 0 )
75 ii = item1.m_symbolPos.y - item2.m_symbolPos.y;
76
77 if( ii == 0 )
78 return item1.m_symbolUuid < item2.m_symbolUuid; // ensure a deterministic sort
79 else
80 return ii < 0;
81}
82
83
85{
86 int ii = item1.CompareRef( item2 );
87
88 if( ii == 0 )
89 ii = item1.m_sheetNum - item2.m_sheetNum;
90
91 if( ii == 0 )
92 ii = item1.m_symbolPos.y - item2.m_symbolPos.y;
93
94 if( ii == 0 )
95 ii = item1.m_symbolPos.x - item2.m_symbolPos.x;
96
97 if( ii == 0 )
98 return item1.m_symbolUuid < item2.m_symbolUuid; // ensure a deterministic sort
99 else
100 return ii < 0;
101}
102
103
105 const SCH_REFERENCE& item2 )
106{
107 int ii = item1.CompareRef( item2 );
108
109 if( ii == 0 )
110 ii = item1.CompareValue( item2 );
111
112 if( ii == 0 )
113 ii = item1.m_unit - item2.m_unit;
114
115 if( ii == 0 )
116 ii = item1.m_sheetNum - item2.m_sheetNum;
117
118 if( ii == 0 )
119 ii = item1.m_symbolPos.x - item2.m_symbolPos.x;
120
121 if( ii == 0 )
122 ii = item1.m_symbolPos.y - item2.m_symbolPos.y;
123
124 if( ii == 0 )
125 return item1.m_symbolUuid < item2.m_symbolUuid; // ensure a deterministic sort
126 else
127 return ii < 0;
128}
129
130
132 const SCH_REFERENCE& item2 )
133{
134 int ii = StrNumCmp( item1.GetRef(), item2.GetRef(), false );
135
136 if( ii == 0 )
137 ii = item1.m_unit - item2.m_unit;
138
139 if( ii == 0 )
140 return item1.m_symbolUuid < item2.m_symbolUuid; // ensure a deterministic sort
141 else
142 return ii < 0;
143}
144
145
147 const SCH_REFERENCE& item2 )
148{
149 int ii = item1.m_sheetPath.Cmp( item2.m_sheetPath );
150
151 if( ii == 0 )
152 return item1.m_symbolUuid < item2.m_symbolUuid; // ensure a deterministic sort
153 else
154 return ii < 0;
155}
156
157
158int SCH_REFERENCE_LIST::FindUnit( size_t aIndex, int aUnit, bool aIncludeNew ) const
159{
160 int NumRef = m_flatList[aIndex].m_numRef;
161
162 for( size_t ii = 0; ii < m_flatList.size(); ii++ )
163 {
164 if( ( aIndex == ii )
165 || ( m_flatList[ii].m_isNew && !aIncludeNew )
166 || ( m_flatList[ii].m_numRef != NumRef )
167 || ( m_flatList[aIndex].CompareRef( m_flatList[ii] ) != 0 ) )
168 continue;
169
170 if( m_flatList[ii].m_unit == aUnit )
171 return (int) ii;
172 }
173
174 return -1;
175}
176
177
178int SCH_REFERENCE_LIST::FindRefByFullPath( const wxString& aFullPath ) const
179{
180 for( size_t i = 0; i < m_flatList.size(); ++i )
181 {
182 if( m_flatList[i].GetFullPath() == aFullPath )
183 return i;
184 }
185
186 return -1;
187}
188
189
190int SCH_REFERENCE_LIST::FindRef( const wxString& aRef ) const
191{
192 for( size_t i = 0; i < m_flatList.size(); ++i )
193 {
194 if( m_flatList[i].GetRef() == aRef )
195 return i;
196 }
197
198 return -1;
199}
200
201
202void SCH_REFERENCE_LIST::GetRefsInUse( int aIndex, std::vector< int >& aIdList,
203 int aMinRefId ) const
204{
205 aIdList.clear();
206
207 for( const SCH_REFERENCE& ref : m_flatList )
208 {
209 // Don't add new references to the list as we will reannotate those
210 if( m_flatList[aIndex].CompareRef( ref ) == 0 && ref.m_numRef >= aMinRefId && !ref.m_isNew )
211 aIdList.push_back( ref.m_numRef );
212 }
213
214 std::sort( aIdList.begin(), aIdList.end() );
215
216 // Ensure each reference number appears only once. If there are symbols with
217 // multiple parts per package the same number will be stored for each part.
218 alg::remove_duplicates( aIdList );
219}
220
221
222std::vector<int> SCH_REFERENCE_LIST::GetUnitsMatchingRef( const SCH_REFERENCE& aRef ) const
223{
224 std::vector<int> unitsList;
225
226 // Always add this reference to the list
227 unitsList.push_back( aRef.m_unit );
228
229 for( SCH_REFERENCE ref : m_flatList )
230 {
231 if( ref.CompareValue( aRef ) != 0 )
232 continue;
233
234 if( ref.CompareLibName( aRef ) != 0 )
235 continue;
236
237 // Split if needed before comparing ref and number
238 if( ref.IsSplitNeeded() )
239 ref.Split();
240
241 if( ref.CompareRef( aRef ) != 0 )
242 continue;
243
244 if( ref.m_numRef != aRef.m_numRef )
245 continue;
246
247 unitsList.push_back( ref.m_unit );
248 }
249
250 std::sort( unitsList.begin(), unitsList.end() );
251
252 // Ensure each reference number appears only once. If there are symbols with
253 // multiple parts per package the same number will be stored for each part.
254 alg::remove_duplicates( unitsList );
255
256 return unitsList;
257}
258
259
261 const std::vector<int>& aRequiredUnits ) const
262{
263 // Create a map of references indexed by reference number, only including those with the same
264 // reference prefix as aRef
265 std::map<int, std::vector<SCH_REFERENCE>> refNumberMap;
266
267 for( const SCH_REFERENCE& ref : m_flatList )
268 {
269 // search only for the current reference prefix:
270 if( ref.CompareRef( aRef ) != 0 )
271 continue;
272
273 if( ref.m_isNew )
274 continue; // It will be reannotated
275
276 refNumberMap[ref.m_numRef].push_back( ref );
277 }
278
279 // Start at the given minimum value
280 int minFreeNumber = aMinValue;
281
282 for( ; refNumberMap[minFreeNumber].size() > 0; ++minFreeNumber )
283 {
284 auto isNumberInUse = [&]() -> bool
285 {
286 for( const int& unit : aRequiredUnits )
287 {
288 for( const SCH_REFERENCE& ref : refNumberMap[minFreeNumber] )
289 {
290 if( ref.CompareLibName( aRef ) || ref.CompareValue( aRef )
291 || ref.GetUnit() == unit )
292 {
293 return true;
294 }
295 }
296 }
297
298 return false;
299 };
300
301 if( !isNumberInUse() )
302 return minFreeNumber;
303 }
304
305 return minFreeNumber;
306}
307
308
309std::vector<SYMBOL_INSTANCE_REFERENCE> SCH_REFERENCE_LIST::GetSymbolInstances() const
310{
311 std::vector<SYMBOL_INSTANCE_REFERENCE> retval;
312
313 for( const SCH_REFERENCE& ref : m_flatList )
314 {
316 instance.m_Path = ref.GetSheetPath().Path();
317 instance.m_Reference = ref.GetRef();
318 instance.m_Unit = ref.GetUnit();
319
320 retval.push_back( instance );
321 }
322
323 return retval;
324}
325
326
327int SCH_REFERENCE_LIST::createFirstFreeRefId( std::vector<int>& aIdList, int aFirstValue )
328{
329 int expectedId = aFirstValue;
330
331 // We search for expected Id a value >= aFirstValue.
332 // Skip existing Id < aFirstValue
333 unsigned ii = 0;
334
335 for( ; ii < aIdList.size(); ii++ )
336 {
337 if( expectedId <= aIdList[ii] )
338 break;
339 }
340
341 // Ids are sorted by increasing value, from aFirstValue
342 // So we search from aFirstValue the first not used value, i.e. the first hole in list.
343 for( ; ii < aIdList.size(); ii++ )
344 {
345 if( expectedId != aIdList[ii] ) // This id is not yet used.
346 {
347 // Insert this free Id, in order to keep list sorted
348 aIdList.insert( aIdList.begin() + ii, expectedId );
349 return expectedId;
350 }
351
352 expectedId++;
353 }
354
355 // All existing Id are tested, and all values are found in use.
356 // So Create a new one.
357 aIdList.push_back( expectedId );
358 return expectedId;
359}
360
361
362// A helper function to build a full reference string of a SCH_REFERENCE item
363wxString buildFullReference( const SCH_REFERENCE& aItem, int aUnitNumber = -1 )
364{
365 wxString fullref;
366 fullref = aItem.GetRef() + aItem.GetRefNumber();
367
368 if( aUnitNumber < 0 )
369 fullref << ".." << aItem.GetUnit();
370 else
371 fullref << ".." << aUnitNumber;
372
373 return fullref;
374}
375
376
378 ANNOTATE_ALGO_T aAlgoOption,
379 int aStartNumber,
380 const SCH_REFERENCE_LIST& aAdditionalRefs,
381 bool aStartAtCurrent,
382 SCH_SHEET_LIST* aHierarchy )
383{
385
386 // All multi-unit symbols always locked to ensure consistent re-annotation
387 SCH_MULTI_UNIT_REFERENCE_MAP lockedSymbols;
388
389 for( size_t i = 0; i < GetCount(); i++ )
390 {
391 SCH_REFERENCE& ref = m_flatList[i];
392 wxString refstr = ref.GetSymbol()->GetRef( &ref.GetSheetPath() );
393
394 // Update sheet numbers based on the reference's sheet's position within the full
395 // hierarchy; we do this now before we annotate so annotation by sheet number * X
396 // works correctly.
397 if( aHierarchy )
398 {
399 SCH_SHEET_PATH* path = aHierarchy->FindSheetForPath( &ref.GetSheetPath() );
400 wxASSERT_MSG( path, wxT( "Attempting to annotate item on sheet not part of the hierarchy?" ) );
401
402 ref.SetSheetNumber( path->GetVirtualPageNumber() );
403 }
404
405 // Never lock unassigned references
406 if( refstr[refstr.Len() - 1] == '?' )
407 continue;
408
409 ref.m_isNew = true; // We want to reannotate all references
410
411 lockedSymbols[refstr].AddItem( ref );
412 }
413
414 AnnotateByOptions( aSortOption, aAlgoOption, aStartNumber, lockedSymbols, aAdditionalRefs,
415 aStartAtCurrent );
416}
417
418
420{
421 ReannotateByOptions( UNSORTED, INCREMENTAL_BY_REF, 0, aAdditionalReferences, true, nullptr );
422}
423
424
426 ANNOTATE_ALGO_T aAlgoOption,
427 int aStartNumber,
428 SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap,
429 const SCH_REFERENCE_LIST& aAdditionalRefs,
430 bool aStartAtCurrent )
431{
432 switch( aSortOption )
433 {
434 default:
437 }
438
439 bool useSheetNum;
440 int idStep;
441
442 switch( aAlgoOption )
443 {
444 default:
446 useSheetNum = false;
447 idStep = 1;
448 break;
449
451 useSheetNum = true;
452 idStep = 100;
453 aStartAtCurrent = false; // Not implemented for sheet # * 100
454 break;
455
457 useSheetNum = true;
458 idStep = 1000;
459 aStartAtCurrent = false; // Not implemented for sheet # * 1000
460 break;
461 }
462
463 Annotate( useSheetNum, idStep, aStartNumber, aLockedUnitMap, aAdditionalRefs, aStartAtCurrent );
464}
465
466
467void SCH_REFERENCE_LIST::Annotate( bool aUseSheetNum, int aSheetIntervalId, int aStartNumber,
468 SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap,
469 const SCH_REFERENCE_LIST& aAdditionalRefs, bool aStartAtCurrent )
470{
471 if ( m_flatList.size() == 0 )
472 return;
473
474 size_t originalSize = GetCount();
475
476 // For multi units symbols, store the list of already used full references.
477 // The algorithm tries to allocate the new reference to symbols having the same
478 // old reference.
479 // This algo works fine as long as the previous annotation has no duplicates.
480 // But when a hierarchy is reannotated with this option, the previous annotation can
481 // have duplicate references, and obviously we must fix these duplicate.
482 // therefore do not try to allocate a full reference more than once when trying
483 // to keep this order of multi units.
484 // inUseRefs keep trace of previously allocated references
485 std::unordered_set<wxString> inUseRefs;
486
487 for( size_t i = 0; i < aAdditionalRefs.GetCount(); i++ )
488 {
489 SCH_REFERENCE additionalRef = aAdditionalRefs[i];
490 additionalRef.Split();
491
492 // Add the additional reference to the multi-unit set if annotated
493 if( !additionalRef.m_isNew )
494 inUseRefs.insert( buildFullReference( additionalRef ) );
495
496 // We don't want to reannotate the additional references even if not annotated
497 // so we change the m_isNew flag to be false after splitting
498 additionalRef.m_isNew = false;
499 AddItem( additionalRef ); //add to this container
500 }
501
502 int LastReferenceNumber = 0;
503
504 /* calculate index of the first symbol with the same reference prefix
505 * than the current symbol. All symbols having the same reference
506 * prefix will receive a reference number with consecutive values:
507 * IC .. will be set to IC4, IC4, IC5 ...
508 */
509 unsigned first = 0;
510
511 // calculate the last used number for this reference prefix:
512 int minRefId;
513
514 // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
515 if( aUseSheetNum )
516 minRefId = m_flatList[first].m_sheetNum * aSheetIntervalId + 1;
517 else
518 minRefId = aStartNumber + 1;
519
520
521 for( unsigned ii = 0; ii < m_flatList.size(); ii++ )
522 {
523 auto& ref_unit = m_flatList[ii];
524
525 if( ref_unit.m_flag )
526 continue;
527
528 // Check whether this symbol is in aLockedUnitMap.
529 SCH_REFERENCE_LIST* lockedList = nullptr;
530
531 for( SCH_MULTI_UNIT_REFERENCE_MAP::value_type& pair : aLockedUnitMap )
532 {
533 unsigned n_refs = pair.second.GetCount();
534
535 for( unsigned thisRefI = 0; thisRefI < n_refs; ++thisRefI )
536 {
537 SCH_REFERENCE &thisRef = pair.second[thisRefI];
538
539 if( thisRef.IsSameInstance( ref_unit ) )
540 {
541 lockedList = &pair.second;
542 break;
543 }
544 }
545
546 if( lockedList != nullptr )
547 break;
548 }
549
550 if( ( m_flatList[first].CompareRef( ref_unit ) != 0 )
551 || ( aUseSheetNum && ( m_flatList[first].m_sheetNum != ref_unit.m_sheetNum ) ) )
552 {
553 // New reference found: we need a new ref number for this reference
554 first = ii;
555
556 // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
557 if( aUseSheetNum )
558 minRefId = ref_unit.m_sheetNum * aSheetIntervalId + 1;
559 else
560 minRefId = aStartNumber + 1;
561 }
562
563 // Find references greater than current reference (unless not annotated)
564 if( aStartAtCurrent && ref_unit.m_numRef > 0 )
565 minRefId = ref_unit.m_numRef;
566
567 // Annotation of one part per package symbols (trivial case).
568 if( ref_unit.GetLibPart()->GetUnitCount() <= 1 )
569 {
570 if( ref_unit.m_isNew )
571 {
572 std::vector<int> idList;
573 GetRefsInUse( first, idList, minRefId );
574 LastReferenceNumber = createFirstFreeRefId( idList, minRefId );
575 ref_unit.m_numRef = LastReferenceNumber;
576 }
577
578 ref_unit.m_flag = 1;
579 ref_unit.m_isNew = false;
580 continue;
581 }
582
583 // If this symbol is in aLockedUnitMap, copy the annotation to all
584 // symbols that are not it
585 if( lockedList != nullptr )
586 {
587 unsigned n_refs = lockedList->GetCount();
588 std::vector<int> units = lockedList->GetUnitsMatchingRef( ref_unit );
589
590 if( ref_unit.m_isNew )
591 {
592 LastReferenceNumber = FindFirstUnusedReference( ref_unit, minRefId, units );
593 ref_unit.m_numRef = LastReferenceNumber;
594 ref_unit.m_isNew = false;
595 ref_unit.m_flag = 1;
596 }
597
598 for( unsigned lockedRefI = 0; lockedRefI < n_refs; ++lockedRefI )
599 {
600 SCH_REFERENCE& lockedRef = ( *lockedList )[lockedRefI];
601
602 if( lockedRef.IsSameInstance( ref_unit ) )
603 {
604 // This is the symbol we're currently annotating. Hold the unit!
605 ref_unit.m_unit = lockedRef.m_unit;
606 // lock this new full reference
607 inUseRefs.insert( buildFullReference( ref_unit ) );
608 }
609
610 if( lockedRef.CompareValue( ref_unit ) != 0 )
611 continue;
612
613 if( lockedRef.CompareLibName( ref_unit ) != 0 )
614 continue;
615
616 // Find the matching symbol
617 for( unsigned jj = ii + 1; jj < m_flatList.size(); jj++ )
618 {
619 if( !lockedRef.IsSameInstance( m_flatList[jj] ) )
620 continue;
621
622 wxString ref_candidate = buildFullReference( ref_unit, lockedRef.m_unit );
623
624 // propagate the new reference and unit selection to the "old" symbol,
625 // if this new full reference is not already used (can happens when initial
626 // multiunits symbols have duplicate references)
627 if( inUseRefs.find( ref_candidate ) == inUseRefs.end() )
628 {
629 m_flatList[jj].m_numRef = ref_unit.m_numRef;
630 m_flatList[jj].m_isNew = false;
631 m_flatList[jj].m_flag = 1;
632 // lock this new full reference
633 inUseRefs.insert( ref_candidate );
634 break;
635 }
636 }
637 }
638 }
639 else if( ref_unit.m_isNew )
640 {
641 // Reference belonging to multi-unit symbol that has not yet been annotated. We don't
642 // know what group this might belong to, so just find the first unused reference for
643 // this specific unit. The other units will be annotated in the following passes.
644 std::vector<int> units = { ref_unit.GetUnit() };
645 LastReferenceNumber = FindFirstUnusedReference( ref_unit, minRefId, units );
646 ref_unit.m_numRef = LastReferenceNumber;
647 ref_unit.m_isNew = false;
648 ref_unit.m_flag = 1;
649 }
650 }
651
652 // Remove aAdditionalRefs references
653 for( size_t i = originalSize; i < ( aAdditionalRefs.GetCount() + originalSize ); i++ )
654 RemoveItem( originalSize );
655
656 wxASSERT( originalSize == GetCount() ); // Make sure we didn't make a mistake
657}
658
659
661{
662 int error = 0;
663 wxString tmp;
664 wxString tmp2;
665 wxString msg;
666
668
669 // Split reference designators into name (prefix) and number: IC1 becomes IC, and 1.
671
672 // count not yet annotated items or annotation error.
673 for( unsigned ii = 0; ii < m_flatList.size(); ii++ )
674 {
675 msg.Empty();
676 tmp.Empty();
677
678 if( m_flatList[ii].m_isNew ) // Not yet annotated
679 {
680 if( m_flatList[ii].m_numRef >= 0 )
681 tmp << m_flatList[ii].m_numRef;
682 else
683 tmp = wxT( "?" );
684
685 if( ( m_flatList[ii].m_unit > 0 ) && ( m_flatList[ii].m_unit < 0x7FFFFFFF ) )
686 {
687 msg.Printf( _( "Item not annotated: %s%s (unit %d)" ),
688 m_flatList[ii].GetRef(),
689 tmp,
690 m_flatList[ii].m_unit );
691 }
692 else
693 {
694 msg.Printf( _( "Item not annotated: %s%s" ), m_flatList[ii].GetRef(), tmp );
695 }
696
697 aHandler( ERCE_UNANNOTATED, msg, &m_flatList[ii], nullptr );
698 error++;
699 break;
700 }
701
702 // Error if unit number selected does not exist (greater than the number of units in
703 // the symbol). This can happen if a symbol has changed in a library after a
704 // previous annotation.
705 if( std::max( m_flatList[ii].GetLibPart()->GetUnitCount(), 1 ) < m_flatList[ii].m_unit )
706 {
707 if( m_flatList[ii].m_numRef >= 0 )
708 tmp << m_flatList[ii].m_numRef;
709 else
710 tmp = wxT( "?" );
711
712 msg.Printf( _( "Error: symbol %s%s%s (unit %d) exceeds units defined (%d)" ),
713 m_flatList[ii].GetRef(),
714 tmp,
716 m_flatList[ii].m_unit,
717 m_flatList[ii].GetLibPart()->GetUnitCount() );
718
719 aHandler( ERCE_EXTRA_UNITS, msg, &m_flatList[ii], nullptr );
720 error++;
721 break;
722 }
723 }
724
725 // count the duplicated elements (if all are annotated)
726 int imax = m_flatList.size() - 1;
727
728 for( int ii = 0; ii < imax; ii++ )
729 {
730 msg.Empty();
731 tmp.Empty();
732 tmp2.Empty();
733
734 SCH_REFERENCE& first = m_flatList[ii];
735 SCH_REFERENCE& second = m_flatList[ii + 1];
736
737 if( ( first.CompareRef( second ) != 0 )
738 || ( first.m_numRef != second.m_numRef ) )
739 {
740 continue;
741 }
742
743 // Same reference found. If same unit, error!
744 if( first.m_unit == second.m_unit )
745 {
746 if( first.m_numRef >= 0 )
747 tmp << first.m_numRef;
748 else
749 tmp = wxT( "?" );
750
751 msg.Printf( _( "Duplicate items %s%s%s\n" ),
752 first.GetRef(),
753 tmp,
755
756 aHandler( ERCE_DUPLICATE_REFERENCE, msg, &first, &m_flatList[ii+1] );
757 error++;
758 continue;
759 }
760
761 /* Test error if units are different but number of parts per package
762 * too high (ex U3 ( 1 part) and we find U3B this is an error) */
763 if( first.GetLibPart()->GetUnitCount() != second.GetLibPart()->GetUnitCount() )
764 {
765 if( first.m_numRef >= 0 )
766 tmp << first.m_numRef;
767 else
768 tmp = wxT( "?" );
769
770 if( second.m_numRef >= 0 )
771 tmp2 << second.m_numRef;
772 else
773 tmp2 = wxT( "?" );
774
775 msg.Printf( _( "Differing unit counts for item %s%s%s and %s%s%s\n" ),
776 first.GetRef(),
777 tmp,
779 second.GetRef(),
780 tmp2,
782
783 aHandler( ERCE_DUPLICATE_REFERENCE, msg, &first, &second );
784 error++;
785 continue;
786 }
787
788 // Error if values are different between units, for the same reference
789 if( first.CompareValue( second ) != 0 )
790 {
791 msg.Printf( _( "Different values for %s%d%s (%s) and %s%d%s (%s)" ),
792 first.GetRef(),
793 first.m_numRef,
795 first.m_value,
796 second.GetRef(),
797 second.m_numRef,
799 second.m_value );
800
801 aHandler( ERCE_DIFFERENT_UNIT_VALUE, msg, &first, &second );
802 error++;
803 }
804 }
805
806 return error;
807}
808
809
811 const SCH_SHEET_PATH& aSheetPath )
812{
813 wxASSERT( aSymbol != nullptr );
814
815 m_rootSymbol = aSymbol;
816 m_libPart = aLibSymbol; // Warning: can be nullptr for orphan symbols
817 // (i.e. with a symbol library not found)
818 m_unit = aSymbol->GetUnitSelection( &aSheetPath );
819 m_footprint = aSymbol->GetFootprintFieldText( true );
820 m_sheetPath = aSheetPath;
821 m_isNew = false;
822 m_flag = 0;
823 m_symbolUuid = aSymbol->m_Uuid;
824 m_symbolPos = aSymbol->GetPosition();
825 m_sheetNum = 0;
826
827 if( aSymbol->GetRef( &aSheetPath ).IsEmpty() )
828 aSymbol->SetRef( &aSheetPath, wxT( "DefRef?" ) );
829
830 wxString ref = aSymbol->GetRef( &aSheetPath );
831 SetRef( ref );
832
833 m_numRef = -1;
834
835 if( aSymbol->GetValueFieldText( false ).IsEmpty() )
836 aSymbol->SetValueFieldText( wxT( "~" ) );
837
838 m_value = aSymbol->GetValueFieldText( false );
839}
840
841
843{
844 if( m_numRef < 0 )
845 m_ref += '?';
846 else
847 m_ref = TO_UTF8( GetRef() << GetRefNumber() );
848
852}
853
854
856{
857 std::string refText = GetRefStr();
858
859 m_numRef = -1;
860
861 int ll = refText.length() - 1;
862
863 if( refText[ll] == '?' )
864 {
865 m_isNew = true;
866
867 refText.erase( ll ); // delete last char
868
869 SetRefStr( refText );
870 }
871 else if( isdigit( refText[ll] ) == 0 )
872 {
873 m_isNew = true;
874 }
875 else
876 {
877 while( ll >= 0 )
878 {
879 if( (refText[ll] <= ' ' ) || isdigit( refText[ll] ) )
880 ll--;
881 else
882 {
883 if( isdigit( refText[ll + 1] ) )
884 {
885 // null terminated C string into cp
886 const char* cp = refText.c_str() + ll + 1;
887
888 m_numRef = atoi( cp );
889 }
890
891 refText.erase( ll+1 ); // delete from ll+1 to end
892 break;
893 }
894 }
895
896 SetRefStr( refText );
897 }
898}
899
900
902{
903 std::string refText = GetRefStr();
904
905 int ll = refText.length() - 1;
906
907 return ( refText[ll] == '?' ) || isdigit( refText[ll] );
908}
909
910
911wxString SCH_REFERENCE_LIST::Shorthand( std::vector<SCH_REFERENCE> aList )
912{
913 wxString retVal;
914 size_t i = 0;
915
916 while( i < aList.size() )
917 {
918 wxString ref = aList[ i ].GetRef();
919 int numRef = aList[ i ].m_numRef;
920
921 size_t range = 1;
922
923 while( i + range < aList.size()
924 && aList[ i + range ].GetRef() == ref
925 && aList[ i + range ].m_numRef == int( numRef + range ) )
926 {
927 range++;
928 }
929
930 if( !retVal.IsEmpty() )
931 retVal << wxT( ", " );
932
933 if( range == 1 )
934 {
935 retVal << ref << aList[ i ].GetRefNumber();
936 }
937 else if( range == 2 )
938 {
939 retVal << ref << aList[ i ].GetRefNumber();
940 retVal << wxT( ", " );
941 retVal << ref << aList[ i + 1 ].GetRefNumber();
942 }
943 else
944 {
945 retVal << ref << aList[ i ].GetRefNumber();
946 retVal << wxT( "-" );
947 retVal << ref << aList[ i + ( range - 1 ) ].GetRefNumber();
948 }
949
950 i+= range;
951 }
952
953 return retVal;
954}
const KIID m_Uuid
Definition: eda_item.h:494
Define a library symbol object.
Definition: lib_symbol.h:98
static wxString SubReference(int aUnit, bool aAddSeparator=true)
Definition: lib_symbol.cpp:588
int GetUnitCount() const override
For items with units, return the number of units.
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
std::vector< SYMBOL_INSTANCE_REFERENCE > GetSymbolInstances() const
void ReannotateByOptions(ANNOTATE_ORDER_T aSortOption, ANNOTATE_ALGO_T aAlgoOption, int aStartNumber, const SCH_REFERENCE_LIST &aAdditionalRefs, bool aStartAtCurrent, SCH_SHEET_LIST *aHierarchy)
Forces reannotation of the provided references.
static int createFirstFreeRefId(std::vector< int > &aIdList, int aFirstValue)
Search for the first free reference number in aListId of reference numbers in use.
static wxString Shorthand(std::vector< SCH_REFERENCE > aList)
Return a shorthand string representing all the references in the list.
static bool sortByRefAndValue(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
bool Contains(const SCH_REFERENCE &aItem) const
Return true if aItem exists in this list.
void SortByYCoordinate()
Sort the list of references by Y position.
void AnnotateByOptions(enum ANNOTATE_ORDER_T aSortOption, enum ANNOTATE_ALGO_T aAlgoOption, int aStartNumber, SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap, const SCH_REFERENCE_LIST &aAdditionalRefs, bool aStartAtCurrent)
Annotate the references by the provided options.
static bool sortByTimeStamp(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
int FindRef(const wxString &aPath) const
Search the list for a symbol with a given reference.
void Annotate(bool aUseSheetNum, int aSheetIntervalId, int aStartNumber, SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap, const SCH_REFERENCE_LIST &aAdditionalRefs, bool aStartAtCurrent=false)
Set the reference designators in the list that have not been annotated.
void SortByXCoordinate()
Sort the list of references by X position.
int FindUnit(size_t aIndex, int aUnit, bool aIncludeNew=false) const
Search the sorted list of symbols for a another symbol with the same reference and a given part unit.
static bool sortByXPosition(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
size_t GetCount() const
int FindFirstUnusedReference(const SCH_REFERENCE &aRef, int aMinValue, const std::vector< int > &aRequiredUnits) const
Return the first unused reference number from the properties given in aRef, ensuring all of the units...
void SplitReferences()
Attempt to split all reference designators into a name (U) and number (1).
void SortByRefAndValue()
Sort the list of references by value.
std::vector< int > GetUnitsMatchingRef(const SCH_REFERENCE &aRef) const
Return all the unit numbers for a given reference, comparing library reference, value,...
std::vector< SCH_REFERENCE > m_flatList
static bool sortByReferenceOnly(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
void GetRefsInUse(int aIndex, std::vector< int > &aIdList, int aMinRefId) const
Add all the reference designator numbers greater than aMinRefId to aIdList skipping the reference at ...
void ReannotateDuplicates(const SCH_REFERENCE_LIST &aAdditionalReferences)
Convenience function for the Paste Unique functionality.
int FindRefByFullPath(const wxString &aFullPath) const
Search the list for a symbol with the given KIID path (as string).
void AddItem(const SCH_REFERENCE &aItem)
void RemoveItem(unsigned int aIndex)
Remove an item from the list of references.
static bool sortByYPosition(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
int CheckAnnotation(ANNOTATION_ERROR_HANDLER aErrorHandler)
Check for annotations errors.
A helper to define a symbol's reference designator in a schematic.
const SCH_SHEET_PATH & GetSheetPath() const
SCH_SYMBOL * m_rootSymbol
The symbol associated the reference object.
int m_unit
The unit number for symbol with multiple parts per package.
SCH_SHEET_PATH m_sheetPath
The sheet path for this reference.
wxString m_footprint
The footprint assigned.
int CompareLibName(const SCH_REFERENCE &item) const
int CompareRef(const SCH_REFERENCE &item) const
void SetRef(const wxString &aReference)
bool m_isNew
True if not yet annotated.
void Split()
Attempt to split the reference designator into a name (U) and number (1).
bool IsSplitNeeded()
Determine if this reference needs to be split or if it likely already has been.
SCH_SYMBOL * GetSymbol() const
wxString m_ref
Symbol reference prefix, without number (for IC1, this is IC) )
wxString GetRef() const
int m_sheetNum
The sheet number for the reference.
void SetRefStr(const std::string &aReference)
int m_numRef
The numeric part of the reference designator.
LIB_SYMBOL * GetLibPart() const
void SetSheetNumber(int aSheetNumber)
const char * GetRefStr() const
Return reference name with unit altogether.
bool IsSameInstance(const SCH_REFERENCE &other) const
Return whether this reference refers to the same symbol instance (symbol and sheet) as another.
KIID m_symbolUuid
UUID of the symbol.
int GetUnit() const
LIB_SYMBOL * m_libPart
The source symbol from a library.
wxString m_value
The symbol value.
VECTOR2I m_symbolPos
The physical position of the symbol in schematic used to annotate by X or Y position.
wxString GetRefNumber() const
int CompareValue(const SCH_REFERENCE &item) const
void Annotate()
Update the annotation of the symbol according the current object state.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
SCH_SHEET_PATH * FindSheetForPath(const SCH_SHEET_PATH *aPath)
Return a pointer to the first SCH_SHEET_PATH object (not necessarily the only one) matching the provi...
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
int Cmp(const SCH_SHEET_PATH &aSheetPathToTest) const
Compare if this is the same sheet path as aSheetPathToTest.
Schematic symbol object.
Definition: sch_symbol.h:80
void SetValueFieldText(const wxString &aValue)
Definition: sch_symbol.cpp:792
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:624
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
Definition: sch_symbol.cpp:666
const wxString GetFootprintFieldText(bool aResolve) const
Definition: sch_symbol.cpp:798
VECTOR2I GetPosition() const override
Definition: sch_symbol.h:702
const wxString GetValueFieldText(bool aResolve) const
Definition: sch_symbol.cpp:783
int GetUnitSelection(const SCH_SHEET_PATH *aSheet) const
Return the instance-specific unit selection for the given sheet path.
Definition: sch_symbol.cpp:741
void SetUnit(int aUnit)
Change the unit number to aUnit.
Definition: sch_symbol.cpp:380
void SetUnitSelection(const SCH_SHEET_PATH *aSheet, int aUnitSelection)
Set the selected unit of this symbol on one sheet.
Definition: sch_symbol.cpp:757
wxString buildFullReference(const SCH_REFERENCE &aItem, int aUnitNumber=-1)
#define _(s)
@ ERCE_UNANNOTATED
Symbol has not been annotated.
Definition: erc_settings.h:75
@ ERCE_DUPLICATE_REFERENCE
More than one symbol with the same reference.
Definition: erc_settings.h:78
@ ERCE_EXTRA_UNITS
Symbol has more units than are defined.
Definition: erc_settings.h:76
@ ERCE_DIFFERENT_UNIT_VALUE
Units of same symbol have different values.
Definition: erc_settings.h:77
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
void remove_duplicates(_Container &__c)
Deletes all duplicate values from __c.
Definition: kicad_algo.h:182
std::function< void(ERCE_T aType, const wxString &aMsg, SCH_REFERENCE *aItemA, SCH_REFERENCE *aItemB)> ANNOTATION_ERROR_HANDLER
Define a standard error handler for annotation errors.
ANNOTATE_ORDER_T
Schematic annotation order options.
@ UNSORTED
Annotate by position of symbol in the schematic sheet object list.
@ SORT_BY_X_POSITION
Annotate by X position from left to right.
@ SORT_BY_Y_POSITION
Annotate by Y position from top to bottom.
ANNOTATE_ALGO_T
Schematic annotation type options.
@ SHEET_NUMBER_X_1000
Annotate using the first free reference number starting at the sheet number * 1000.
@ INCREMENTAL_BY_REF
Annotate incrementally using the first free reference number.
@ SHEET_NUMBER_X_100
Annotate using the first free reference number starting at the sheet number * 100.
std::map< wxString, SCH_REFERENCE_LIST > SCH_MULTI_UNIT_REFERENCE_MAP
Container to map reference designators for multi-unit parts.
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
A simple container for schematic symbol instance information.