KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_board_reannotate.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) 2020 Brian Piccioni [email protected]
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Brian Piccioni <[email protected]>
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, see <https://www.gnu.org/licenses/>.
20 */
21
23
24#include <algorithm>
25
26#include <wx/tokenzr.h>
27#include <wx/valtext.h>
28
29#include <base_units.h>
30#include <bitmaps.h>
31#include <board_commit.h>
32#include <confirm.h>
33#include <ctype.h>
34#include <footprint.h>
36#include <string_utils.h> // StrNumCmp
37#include <kiface_base.h>
38#include <pcbnew_settings.h>
40#include <mail_type.h>
41#include <ref_suffix_split.h>
42#include <refdes_utils.h>
43#include <richio.h>
44#include <tool/grid_menu.h>
46
47
51
52//
53// This converts the index into a sort code. Note that Back sort code will have left and
54// right swapped.
55//
57 SORTYFIRST + ASCENDINGFIRST + ASCENDINGSECOND, // "Top to bottom, left to right", // 100
58 SORTYFIRST + ASCENDINGFIRST + DESCENDINGSECOND, // "Top to bottom, right to left", // 101
59 SORTYFIRST + DESCENDINGFIRST + ASCENDINGSECOND, // "Back to Front, left to right", // 110
60 SORTYFIRST + DESCENDINGFIRST + DESCENDINGSECOND, // "Back to Front, right to left", // 111
61 SORTXFIRST + ASCENDINGFIRST + ASCENDINGSECOND, // "Left to right, Front to Back", // 000
62 SORTXFIRST + ASCENDINGFIRST + DESCENDINGSECOND, // "Left to right, Back to Front", // 001
63 SORTXFIRST + DESCENDINGFIRST + ASCENDINGSECOND, // "Right to left, Front to Back", // 010
64 SORTXFIRST + DESCENDINGFIRST + DESCENDINGSECOND // "Right to left, Back to Front", // 011
65};
66
67
68//
69// Back Left/Right is opposite because it is a mirror image (coordinates are from the top)
70//
72 SORTYFIRST + ASCENDINGFIRST + DESCENDINGSECOND, // "Top to bottom, left to right", // 101
73 SORTYFIRST + ASCENDINGFIRST + ASCENDINGSECOND, // "Top to bottom, right to left", // 100
74 SORTYFIRST + DESCENDINGFIRST + DESCENDINGSECOND, // "Bottom to top, left to right", // 111
75 SORTYFIRST + DESCENDINGFIRST + ASCENDINGSECOND, // "Bottom to top, right to left", // 110
76 SORTXFIRST + DESCENDINGFIRST + ASCENDINGSECOND, // "Left to right, top to bottom", // 010
77 SORTXFIRST + DESCENDINGFIRST + DESCENDINGSECOND, // "Left to right, bottom to top", // 011
78 SORTXFIRST + ASCENDINGFIRST + ASCENDINGSECOND, // "Right to left, top to bottom", // 000
79 SORTXFIRST + ASCENDINGFIRST + DESCENDINGSECOND // "Right to left, bottom to top", // 001
80};
81
82#define SetSortCodes( DirArray, Code ) \
83 { \
84 g_SortYFirst = ( ( DirArray[Code] & SORTYFIRST ) != 0 ); \
85 g_DescendingFirst = ( ( DirArray[Code] & DESCENDINGFIRST ) != 0 ); \
86 g_DescendingSecond = ( ( DirArray[Code] & DESCENDINGSECOND ) != 0 ); \
87 }
88
89
90wxString ActionMessage[] = {
91 "", // UPDATE_REFDES
92 _( "(not updated)" ), // EMPTY_REFDES
93 _( "(unannotated; not updated)" ), // INVALID_REFDES
94 _( "(excluded)" ), // EXCLUDE_REFDES
95 _( "(unknown suffix; not updated)" ) // UNKNOWN_SUFFIX_REFDES
96};
97
98
100static int refDesNumber( const wxString& aRefDes )
101{
102 size_t firstnum = aRefDes.find_first_of( wxS( "0123456789" ) );
103
104 if( firstnum == wxString::npos )
105 return -1;
106
107 long number = 0;
108 aRefDes.Mid( firstnum, aRefDes.find_first_not_of( wxS( "0123456789" ), firstnum ) - firstnum )
109 .ToLong( &number );
110 return static_cast<int>( number );
111}
112
113
115 DIALOG_BOARD_REANNOTATE_BASE( aParentFrame ),
116 m_frame( aParentFrame ),
117 m_footprints( aParentFrame->GetBoard()->Footprints() )
118{
119 // Init bitmaps associated to some wxRadioButton
128
129 m_FrontRefDesStart->SetValidator( wxTextValidator( wxFILTER_DIGITS ) );
130 m_BackRefDesStart->SetValidator( wxTextValidator( wxFILTER_DIGITS ) );
131
132 SetupStandardButtons( { { wxID_OK, _( "Reannotate PCB" ) },
133 { wxID_CANCEL, _( "Close" ) } } );
134
135 wxArrayString gridslist;
136 GRID_MENU::BuildChoiceList( &gridslist, m_frame->GetWindowSettings( m_frame->config() ), aParentFrame );
137
138 m_GridChoice->Set( gridslist );
139
140 int gridIndex = m_frame->config()->m_Window.grid.last_size_idx;
141
142 if( gridIndex >= 0 && gridIndex < (int) m_GridChoice->GetCount() )
143 m_GridChoice->SetSelection( gridIndex );
144 else
145 m_GridChoice->SetSelection( 0 );
146
147 m_ExcludeList->SetToolTip( m_ExcludeListText->GetToolTipText() );
148 m_GridChoice->SetToolTip( m_SortGridText->GetToolTipText() );
149
150 m_MessageWindow->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
151
153}
154
155
156void DIALOG_BOARD_REANNOTATE::OnCloseClick( wxCommandEvent& event )
157{
158 EndDialog( wxID_OK );
159}
160
161
163{
164 PCB_SELECTION selection = m_frame->GetToolManager()->GetTool<PCB_SELECTION_TOOL>()->GetSelection();
165
166 if( !selection.Empty() )
167 m_AnnotateSelection->SetValue( true );
168
169 // Ensure m_GridChoice selection validity
170 // If not, the choice 0 is arbitrary
171 if( m_GridChoice->GetSelection() < 0 || m_GridChoice->GetSelection() >= (int)m_GridChoice->GetCount() )
172 m_GridChoice->SetSelection( 0 );
173
174 return true;
175}
176
177
178void DIALOG_BOARD_REANNOTATE::FilterPrefix( wxTextCtrl* aPrefix )
179{
180 std::string tmps = VALIDPREFIX;
181
182 if( aPrefix->GetValue().empty() )
183 return; //Should never happen
184
185 char lastc = aPrefix->GetValue().Last();
186
187 if( isalnum( (int) lastc ) )
188 return;
189
190 if( tmps.find( lastc ) != std::string::npos )
191 return;
192
193 tmps = aPrefix->GetValue();
194 aPrefix->Clear();
195 tmps.pop_back();
196 aPrefix->AppendText( tmps );
197}
198
199
201 int aStartRefDes )
202{
203 for( size_t i = 0; i < m_refDesPrefixInfos.size(); i++ ) // See if it is in the info array
204 {
205 if( m_refDesPrefixInfos[i].RefDesPrefix == aRefDesPrefix ) // Found it!
206 return &m_refDesPrefixInfos[i];
207 }
208
209 // Wasn't in the info array so add it
210 REFDES_PREFIX_INFO newtype;
211 newtype.RefDesPrefix = aRefDesPrefix;
212 newtype.LastUsedRefDes = std::max( aStartRefDes - 1, 0 );
213 m_refDesPrefixInfos.push_back( newtype );
214
215 return &m_refDesPrefixInfos.back();
216}
217
218
220{
222}
223
224
225void DIALOG_BOARD_REANNOTATE::FilterBackPrefix( wxCommandEvent& event )
226{
228}
229
230
231void DIALOG_BOARD_REANNOTATE::OnApplyClick( wxCommandEvent& event )
232{
233 m_MessageWindow->SetLazyUpdate( true );
234
235 if( ReannotateBoard() )
236 {
237 ShowReport( _( "PCB successfully reannotated" ), RPT_SEVERITY_ACTION );
238 ShowReport( _( "PCB annotation changes should be synchronized with schematic using "
239 "\"Update Schematic from PCB\"." ), RPT_SEVERITY_WARNING );
240 }
241
242 m_MessageWindow->SetLazyUpdate( false );
243 m_MessageWindow->Flush( false );
244 m_frame->GetCanvas()->Refresh(); // Redraw
245 m_frame->OnModify(); // Need to save file on exit.
246}
247
248
249int DIALOG_BOARD_REANNOTATE::RoundToGrid( int aCoord, int aGrid )
250{
251 if( 0 == aGrid )
252 aGrid = MINGRID;
253
254 int rounder;
255 rounder = aCoord % aGrid;
256 aCoord -= rounder;
257
258 if( abs( rounder ) > ( aGrid / 2 ) )
259 aCoord += ( aCoord < 0 ? -aGrid : aGrid );
260
261 return ( aCoord );
262}
263
264
267static bool ChangeArrayCompare( const REFDES_CHANGE& aA, const REFDES_CHANGE& aB )
268{
269 return ( StrNumCmp( aA.OldRefDesString, aB.OldRefDesString ) < 0 );
270}
271
272
275static bool FootprintCompare( const REFDES_INFO& aA, const REFDES_INFO& aB )
276{
277 int X0 = aA.roundedx, X1 = aB.roundedx, Y0 = aA.roundedy, Y1 = aB.roundedy;
278
279 if( g_SortYFirst ) //If sorting by Y then X, swap X and Y
280 {
281 std::swap( X0, Y0 );
282 std::swap( X1, Y1 );
283 }
284
285 // If descending, same compare just swap directions
287 std::swap( X0, X1 );
288
290 std::swap( Y0, Y1 );
291
292 if( X0 < X1 )
293 return true; // yes, it's smaller
294 else if( X0 > X1 )
295 return false; // no, it's not
296 else if( Y0 < Y1 )
297 return true;
298 else
299 return false;
300}
301
302
303wxString empty_str()
304{
305 return wxT( "<i>" ) + _( "unannotated footprint" ) + wxT( "</i>" );
306}
307
308
309wxString unknown_str()
310{
311 return wxT( "<i>" ) + _( "unknown" ) + wxT( "</i>" );
312}
313
314
316{
317 return wxString::Format( wxT( "%s, %s" ),
318 m_frame->MessageTextFromValue( aX ),
319 m_frame->MessageTextFromValue( aY ) );
320}
321
322
323void DIALOG_BOARD_REANNOTATE::ShowReport( const wxString& aMessage, SEVERITY aSeverity )
324{
325 wxStringTokenizer msgs( aMessage, "\n" );
326
327 while( msgs.HasMoreTokens() )
328 m_MessageWindow->Report( msgs.GetNextToken(), aSeverity );
329}
330
331
333{
334 wxString message;
335
336 message = _( "Reference Designator Prefixes in Use" );
337 message += wxT( "<br/>-------------------------------------------------------------<br/>" );
338
339 int i = 1;
340
341 for( const REFDES_PREFIX_INFO& info : m_refDesPrefixInfos ) // Show all the types of refdes
342 message += info.RefDesPrefix + ( ( i++ % 16 ) == 0 ? wxT( "<br/>" ) : wxS( " " ) );
343
344 message += wxT( "<br/>" );
345
346 if( !m_excludeArray.empty() )
347 {
348 wxString excludes;
349
350 for( wxString& exclude : m_excludeArray ) // Show the refdes we are excluding
351 excludes += exclude + wxS( " " );
352
353 message += wxString::Format( _( "(Excluding %s from reannotation.)" ), excludes );
354 }
355
356 ShowReport( message + wxT( "<br/>" ), RPT_SEVERITY_INFO );
357
358 message = _( "Change Log" );
359 message += wxT( "<br/>-------------------------------------------------------------<br/>" );
360
361 for( const REFDES_CHANGE& change : m_changeArray )
362 {
363 if( change.Action != UPDATE_REFDES )
364 {
365 message += wxString::Format( wxT( "%s <i>%s</i><br/>" ),
366 change.OldRefDesString.IsEmpty() ? empty_str() : change.OldRefDesString,
367 ActionMessage[change.Action] );
368 }
369 else
370 {
371 message += wxString::Format( wxT( "%s -> %s<br/>" ),
372 change.OldRefDesString.IsEmpty() ? empty_str() : change.OldRefDesString,
373 change.NewRefDes.IsEmpty() ? empty_str() : change.NewRefDes );
374 }
375 }
376
378}
379
380
381void DIALOG_BOARD_REANNOTATE::LogFootprints( const std::vector<REFDES_INFO>& aFootprints )
382{
383 wxString message = aFootprints.front().Front ? _( "Front Footprints" ) : _( "Back Footprints" );
384 message += ' ';
385
386 if( m_locationChoice->GetSelection() == 0 )
387 message += _( "(sorted by footprint location)" );
388 else
389 message += _( "(sorted by reference designator location)" );
390
391 message += wxT( "<br/>-------------------------------------------------------------" );
392
393 int i = 1;
394
395 for( const REFDES_INFO& fp : aFootprints )
396 {
397 message += wxString::Format( _( "<br/>%d %s at %s (rounded to %s)" ),
398 i++,
399 fp.RefDesString.IsEmpty() ? empty_str() : fp.RefDesString,
400 CoordTowxString( fp.x, fp.y ),
401 CoordTowxString( fp.roundedx, fp.roundedy ) );
402 }
403
404 ShowReport( message + wxT( "<br/>" ), RPT_SEVERITY_INFO );
405}
406
407
409{
410 std::vector<REFDES_INFO> BadRefDes;
411 wxString message1, message2, badrefdes;
412 STRING_FORMATTER stringformatter;
413 REFDES_CHANGE* newref;
415
416 if( !BuildFootprintList( BadRefDes ) )
417 {
418 ShowReport( _( "Selected options resulted in errors! Change them and try again." ), RPT_SEVERITY_ERROR );
419 return false;
420 }
421
422 if( !BadRefDes.empty() )
423 {
424 message1.Printf( _( "PCB has %d empty or invalid reference designations." ), (int) BadRefDes.size() );
425 message2.Printf( _( "You may wish to run DRC with 'Test for parity between PCB and schematic' checked." ) );
426
427 for( const REFDES_INFO& mod : BadRefDes )
428 {
429 badrefdes += wxString::Format( _( "<br/> RefDes: %s; footprint: %s at %s on PCB." ),
430 mod.RefDesString.IsEmpty() ? empty_str() : mod.RefDesString,
431 mod.FPID.IsValid() ? wxString( mod.FPID.Format().c_str() ) : unknown_str(),
432 CoordTowxString( mod.x, mod.y ) );
433 }
434
435 ShowReport( message1 + wxT( "<br/>" ) + message2 + badrefdes + wxT( "<br/>" ), RPT_SEVERITY_WARNING );
436
437 if( !IsOK( m_frame, message1 + "\n" + message2 + "\n \n" + _( "Reannotate anyway?" ) ) )
438 return false;
439 }
440
441 int unknownSuffixes = (int) std::count_if( m_changeArray.begin(), m_changeArray.end(),
442 []( const REFDES_CHANGE& aChange )
443 {
444 return aChange.Action == UNKNOWN_SUFFIX_REFDES;
445 } );
446
447 if( unknownSuffixes > 0 )
448 {
449 ShowReport( wxString::Format( _( "%d footprints keep their references because their sheet suffixes "
450 "could not be determined. Open the schematic editor and update the PCB "
451 "from it if needed before renumbering them." ),
452 unknownSuffixes ),
454 }
455
456 BOARD_COMMIT commit( m_frame );
457
458 for( FOOTPRINT* footprint : m_footprints )
459 {
460 newref = GetNewRefDes( footprint );
461
462 if( nullptr == newref )
463 return false;
464
465 commit.Modify( footprint ); // Make a copy for undo
466 footprint->SetReference( newref->NewRefDes ); // Update the PCB reference
467 m_frame->GetCanvas()->GetView()->Update( footprint ); // Touch the footprint
468 }
469
470 commit.Push( _( "Annotation" ) );
471 return true;
472}
473
474
475bool DIALOG_BOARD_REANNOTATE::BuildFootprintList( std::vector<REFDES_INFO>& aBadRefDes )
476{
477 bool annotateSelected = m_AnnotateSelection->GetValue();
478 bool annotateFront = m_AnnotateFront->GetValue();
479 bool annotateBack = m_AnnotateBack->GetValue();
480 bool skipLocked = m_ExcludeLocked->GetValue();
481
482 GRID sortGridMils = m_frame->config()->m_Window.grid.grids[ m_GridChoice->GetSelection() ];
483 int sortGridx = (int) EDA_UNIT_UTILS::UI::ValueFromString( pcbIUScale, EDA_UNITS::MILS, sortGridMils.x );
484 int sortGridy = (int) EDA_UNIT_UTILS::UI::ValueFromString( pcbIUScale, EDA_UNITS::MILS, sortGridMils.y );
485
486 int errorcount = 0;
487 size_t firstnum = 0;
488
489 m_frontFootprints.clear();
490 m_backFootprints.clear();
491 m_excludeArray.clear();
492 m_footprints = m_frame->GetBoard()->Footprints();
493
494 wxStringTokenizer tokenizer( m_ExcludeList->GetValue(), ", \t\r\n", wxTOKEN_STRTOK );
495
496 while( tokenizer.HasMoreTokens() )
497 m_excludeArray.push_back( tokenizer.GetNextToken() );
498
499 std::optional<SHEET_REF_SUFFIXES> suffixes;
500
501 // A standalone board has no schematic to name the suffixes, so suffixed references stay as they are
502 if( m_frame->Kiway().Player( FRAME_SCH, false ) )
503 {
504 std::string payload;
505 m_frame->Kiway().ExpressMail( FRAME_SCH, MAIL_SCH_GET_REF_SUFFIXES, payload, this );
506 suffixes = ParseRefSuffixes( payload );
507 }
508
509 // Mirrors SCHEMATIC_SETTINGS::m_AnnotateReusedSheetSuffixes, which pcbnew cannot reach
510 const bool suffixMode =
511 m_frame->Prj().GetProjectFile().Get<bool>( "schematic.annotation.reused_sheet_suffixes" ).value_or( false );
512 std::map<KIID, FOOTPRINT_REF_SPLIT> splits =
513 SplitRefSuffixes( m_footprints, suffixes ? &*suffixes : nullptr, suffixMode );
514
515 REFDES_INFO fpData;
516 bool useFPLocation = m_locationChoice->GetSelection() == 0;
517
518 for( FOOTPRINT* footprint : m_footprints )
519 {
520 fpData.Uuid = footprint->m_Uuid;
521 fpData.RefDesString = footprint->GetReference();
522 fpData.FPID = footprint->GetFPID();
523 fpData.x = useFPLocation ? footprint->GetPosition().x
524 : footprint->Reference().GetPosition().x;
525 fpData.y = useFPLocation ? footprint->GetPosition().y
526 : footprint->Reference().GetPosition().y;
527 fpData.roundedx = RoundToGrid( fpData.x, sortGridx ); // Round to sort
528 fpData.roundedy = RoundToGrid( fpData.y, sortGridy );
529 fpData.Front = footprint->GetLayer() == F_Cu;
530 fpData.Action = UPDATE_REFDES; // Usually good
531 fpData.Suffix = splits.at( footprint->m_Uuid ).Suffix;
532 fpData.Representative = splits.at( footprint->m_Uuid ).Representative;
533 fpData.Shared = splits.at( footprint->m_Uuid ).Shared;
534 fpData.Held = footprint->IsLocked() && skipLocked;
535
536 if( fpData.RefDesString.IsEmpty() )
537 {
538 fpData.Action = EMPTY_REFDES;
539 }
540 else
541 {
542 firstnum = fpData.RefDesString.find_first_of( wxT( "0123456789" ) );
543
544 if( std::string::npos == firstnum )
545 fpData.Action = INVALID_REFDES;
546 }
547
548 // Get the type (R, C, etc)
549 fpData.RefDesPrefix = fpData.RefDesString.substr( 0, firstnum );
550
551 for( const wxString& excluded : m_excludeArray )
552 {
553 // If exclusion ends in *, apply it to entire refdes
554 if( excluded.EndsWith( '*' ) )
555 {
556 if( fpData.RefDesString.Matches( excluded ) )
557 {
558 fpData.Action = EXCLUDE_REFDES;
559 fpData.Held = true;
560 break;
561 }
562 }
563 else if( excluded == fpData.RefDesPrefix )
564 {
565 fpData.Action = EXCLUDE_REFDES;
566 fpData.Held = true;
567 break;
568 }
569 }
570
571 // The scope only narrows what the exclude list and the refdes checks leave to update
572 const bool outOfScope = ( footprint->IsLocked() && skipLocked )
573 || ( annotateSelected && !footprint->IsSelected() )
574 || ( annotateFront && !fpData.Front ) || ( annotateBack && fpData.Front );
575
576 if( fpData.Action == UPDATE_REFDES && outOfScope )
577 fpData.Action = EXCLUDE_REFDES;
578
579 if( fpData.Action == UPDATE_REFDES && splits.at( footprint->m_Uuid ).Unknown )
581
582 if( fpData.Front )
583 m_frontFootprints.push_back( fpData );
584 else
585 m_backFootprints.push_back( fpData );
586 }
587
588 // Copies on reused sheets share one base, so any copy in scope renumbers all of them and a locked
589 // or explicitly excluded copy holds the whole group
590 std::map<KIID, ACTION_CODE> groupActions;
591 std::set<KIID> scopedGroups;
592 std::set<KIID> heldGroups;
593 m_groupSuffixes.clear();
594
595 for( std::vector<REFDES_INFO>* side : { &m_frontFootprints, &m_backFootprints } )
596 {
597 for( const REFDES_INFO& info : *side )
598 {
599 if( !info.Shared )
600 continue;
601
602 if( info.Representative == info.Uuid )
603 groupActions[info.Uuid] = info.Action;
604
605 if( info.Action == UPDATE_REFDES )
606 scopedGroups.insert( info.Representative );
607
608 if( info.Held )
609 heldGroups.insert( info.Representative );
610 }
611 }
612
613 for( std::vector<REFDES_INFO>* side : { &m_frontFootprints, &m_backFootprints } )
614 {
615 for( REFDES_INFO& info : *side )
616 {
617 if( !info.Shared )
618 continue;
619
620 if( heldGroups.count( info.Representative ) )
621 info.Action = EXCLUDE_REFDES;
622 else if( scopedGroups.count( info.Representative ) )
623 info.Action = UPDATE_REFDES;
624 else
625 info.Action = groupActions.at( info.Representative );
626
627 m_groupSuffixes[info.Representative].push_back( info.Suffix );
628 }
629 }
630
631 int sortCode = 0; // Convert radio button to sort direction code
632
633 for( wxRadioButton* sortbuttons : m_sortButtons )
634 {
635 if( sortbuttons->GetValue() )
636 break;
637
638 sortCode++;
639 }
640
641 if( sortCode >= (int) m_sortButtons.size() )
642 sortCode = 0;
643
644 // Determine the sort order for the front.
646
647 // Sort the front footprints.
649
650 // Determine the sort order for the back.
652
653 // Sort the back footprints.
654 sort( m_backFootprints.begin(), m_backFootprints.end(), FootprintCompare );
655
656 m_refDesPrefixInfos.clear();
657 m_changeArray.clear();
658 m_newBases.clear();
659 m_takenRefs.clear();
660 m_suffixedRefs.clear();
661
663
664 if( !m_frontFootprints.empty() )
665 {
667 m_FrontPrefix->GetValue(), m_RemoveFrontPrefix->GetValue(), aBadRefDes );
668 }
669
670 if( !m_backFootprints.empty() )
671 {
673 m_BackPrefix->GetValue(), m_RemoveBackPrefix->GetValue(), aBadRefDes );
674 }
675
676 for( REFDES_CHANGE& change : m_changeArray )
677 {
678 if( change.Action == UPDATE_REFDES && change.Representative != change.Uuid )
679 change.NewRefDes = m_newBases.at( change.Representative ) + change.Suffix;
680 }
681
682 if( !m_changeArray.empty() )
683 sort( m_changeArray.begin(), m_changeArray.end(), ChangeArrayCompare );
684
686
687 size_t changearraysize = m_changeArray.size();
688
689 for( size_t i = 0; i < changearraysize; i++ ) // Scan through for duplicates if update or skip
690 {
691 if( m_changeArray[i].Action != EMPTY_REFDES && m_changeArray[i].Action != INVALID_REFDES )
692 {
693 for( size_t j = i + 1; j < changearraysize; j++ )
694 {
695 if( m_changeArray[i].NewRefDes == m_changeArray[j].NewRefDes )
696 {
697 ShowReport( wxString::Format( _( "Duplicate instances of %s" ), m_changeArray[j].NewRefDes ),
699
700 if( errorcount++ > MAXERROR )
701 {
702 ShowReport( _( "Aborted: too many errors" ), RPT_SEVERITY_ERROR );
703 break;
704 }
705 }
706 }
707 }
708
709 if( errorcount > MAXERROR )
710 break;
711 }
712
713 return ( errorcount == 0 );
714}
715
717{
718 for( std::vector<REFDES_INFO>* side : { &m_frontFootprints, &m_backFootprints } )
719 {
720 for( const REFDES_INFO& fpData : *side )
721 {
722 if( fpData.Action == UPDATE_REFDES )
723 continue;
724
725 m_takenRefs.insert( fpData.RefDesString );
726
727 if( !fpData.Suffix.IsEmpty() || fpData.Action == UNKNOWN_SUFFIX_REFDES )
728 m_suffixedRefs.insert( fpData.RefDesString );
729
730 if( fpData.Action == EXCLUDE_REFDES || fpData.Action == UNKNOWN_SUFFIX_REFDES )
731 {
732 REFDES_PREFIX_INFO* refDesInfo = GetOrBuildRefDesInfo( fpData.RefDesPrefix );
733 refDesInfo->UnavailableRefs.insert( refDesNumber( fpData.RefDesString ) );
734 }
735 }
736 }
737}
738
739
740void DIALOG_BOARD_REANNOTATE::BuildChangeArray( std::vector<REFDES_INFO>& aFootprints,
741 unsigned int aStartRefDes, const wxString& aPrefix,
742 bool aRemovePrefix, std::vector<REFDES_INFO>& aBadRefDes )
743{
744 size_t prefixsize = aPrefix.size();
745
746 bool haveprefix = ( 0 != prefixsize ); // Do I have a prefix?
747 bool addprefix = haveprefix & !aRemovePrefix; // Yes- and I'm not removing it
748 aRemovePrefix &= haveprefix; // Only remove if I have a prefix
749
750 bool prefixpresent; // Prefix found
751
752 LogFootprints( aFootprints );
753
754 if( aStartRefDes != 0 ) // Initialize the change array if present
755 {
756 for( REFDES_PREFIX_INFO& prefixInfo : m_refDesPrefixInfos )
757 prefixInfo.LastUsedRefDes = aStartRefDes - 1;
758 }
759
760 for( REFDES_INFO fpData : aFootprints )
761 {
762 REFDES_CHANGE change;
763
764 change.Uuid = fpData.Uuid;
765 change.Action = fpData.Action;
766 change.OldRefDesString = fpData.RefDesString;
767 change.NewRefDes = fpData.RefDesString;
768 change.Front = fpData.Front;
769 change.Suffix = fpData.Suffix;
770 change.Representative = fpData.Representative;
771
772 if( fpData.RefDesString.IsEmpty() )
773 fpData.Action = EMPTY_REFDES;
774
775 if( ( change.Action == EMPTY_REFDES ) || ( change.Action == INVALID_REFDES ) )
776 {
777 m_changeArray.push_back( change );
778 aBadRefDes.push_back( fpData );
779 continue;
780 }
781
782 // A copy on a reused sheet takes the base its representative is given
783 if( change.Action == UPDATE_REFDES && fpData.Representative != fpData.Uuid )
784 {
785 m_changeArray.push_back( change );
786 continue;
787 }
788
789 if( change.Action == UPDATE_REFDES )
790 {
791 prefixpresent = ( fpData.RefDesPrefix.find( aPrefix ) == 0 );
792
793 if( addprefix && !prefixpresent )
794 fpData.RefDesPrefix.insert( 0, aPrefix ); // Add prefix once only
795
796 if( aRemovePrefix && prefixpresent ) // If there is a prefix remove it
797 fpData.RefDesPrefix.erase( 0, prefixsize );
798
799 REFDES_PREFIX_INFO* refDesInfo = GetOrBuildRefDesInfo( fpData.RefDesPrefix, aStartRefDes );
800 unsigned int newRefDesNumber = refDesInfo->LastUsedRefDes + 1;
801
802 std::vector<wxString> suffixes = { fpData.Suffix };
803
804 if( auto it = m_groupSuffixes.find( fpData.Uuid ); it != m_groupSuffixes.end() )
805 suffixes = it->second;
806
807 // A digit suffix makes R1 with suffix 2 read as R12, so suffixed and bare references are compared whole
808 auto clashes =
809 [&]( const wxString& aBase )
810 {
811 for( const wxString& suffix : suffixes )
812 {
813 if( ( suffix.IsEmpty() ? m_suffixedRefs : m_takenRefs ).count( aBase + suffix ) )
814 return true;
815 }
816
817 return false;
818 };
819
820 while( refDesInfo->UnavailableRefs.count( newRefDesNumber )
821 || clashes( refDesInfo->RefDesPrefix + std::to_string( newRefDesNumber ) ) )
822 {
823 newRefDesNumber++;
824 }
825
826 wxString base = refDesInfo->RefDesPrefix + std::to_string( newRefDesNumber );
827
828 for( const wxString& suffix : suffixes )
829 {
830 m_takenRefs.insert( base + suffix );
831
832 if( !suffix.IsEmpty() )
833 m_suffixedRefs.insert( base + suffix );
834 }
835
836 m_newBases[fpData.Uuid] = base;
837 change.NewRefDes = base + fpData.Suffix;
838 refDesInfo->LastUsedRefDes = newRefDesNumber;
839 }
840
841 m_changeArray.push_back( change );
842 }
843}
844
845
847{
848 size_t i;
849
850 for( i = 0; i < m_changeArray.size(); i++ )
851 {
852 if( aFootprint->m_Uuid == m_changeArray[i].Uuid )
853 return ( &m_changeArray[i] );
854 }
855
856 ShowReport( _( "Footprint not found in changelist" ) + wxS( " " ) + aFootprint->GetReference(),
858
859 return nullptr; // Should never happen
860}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
@ reannotate_down_left
@ reannotate_up_right
@ reannotate_right_down
@ reannotate_up_left
@ reannotate_left_down
@ reannotate_down_right
@ reannotate_left_up
@ reannotate_right_up
void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
DIALOG_BOARD_REANNOTATE_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Geographical Reannotation"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
std::vector< REFDES_CHANGE > m_changeArray
std::vector< wxString > m_excludeArray
bool BuildFootprintList(std::vector< REFDES_INFO > &aBadRefDes)
Build the footprint lists, sort it, filter for excludes, then build the change list.
std::map< KIID, std::vector< wxString > > m_groupSuffixes
void FilterBackPrefix(wxCommandEvent &event) override
std::map< KIID, wxString > m_newBases
std::vector< REFDES_INFO > m_frontFootprints
wxString CoordTowxString(int aX, int aY)
Convert coordinates to wxString.
void FilterPrefix(wxTextCtrl *aPrefix)
Check to make sure the prefix (if there is one) is properly constructed.
void BuildChangeArray(std::vector< REFDES_INFO > &aFootprints, unsigned int aStartRefDes, const wxString &aPrefix, bool aRemovePrefix, std::vector< REFDES_INFO > &aBadRefDes)
Scan through the footprint arrays and create the from -> to array.
void LogFootprints(const std::vector< REFDES_INFO > &aFootprints)
Create a list of the footprints and their coordinates.
int RoundToGrid(int aCoord, int aGrid)
Round an int coordinate to a suitable grid.
REFDES_PREFIX_INFO * GetOrBuildRefDesInfo(const wxString &aRefDesPrefix, int aStartRefDes=1)
Get the structure representing the information currently held for aRefDesPrefix or create one if it d...
void BuildUnavailableRefsList()
Build list of unavailable references. E.g. unselected footprints or locked footprints.
void LogChangePlan(void)
Create an audit trail of the changes.
std::set< wxString > m_suffixedRefs
void OnCloseClick(wxCommandEvent &event) override
REFDES_CHANGE * GetNewRefDes(FOOTPRINT *aFootprint)
bool ReannotateBoard(void)
Actually reannotate the board.
void ShowReport(const wxString &aMessage, SEVERITY aSeverity)
Break report into strings separated by and sent to the reporter.
std::vector< REFDES_INFO > m_backFootprints
std::vector< REFDES_PREFIX_INFO > m_refDesPrefixInfos
DIALOG_BOARD_REANNOTATE(PCB_EDIT_FRAME *aParentFrame)
void OnApplyClick(wxCommandEvent &event) override
std::vector< wxRadioButton * > m_sortButtons
void FilterFrontPrefix(wxCommandEvent &event) override
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
const KIID m_Uuid
Definition eda_item.h:599
const wxString & GetReference() const
Definition footprint.h:793
static void BuildChoiceList(wxArrayString *aGridsList, WINDOW_SETTINGS *aCfg, EDA_DRAW_FRAME *aParent)
Definition grid_menu.cpp:79
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
The main frame for Pcbnew.
The selection tool: currently supports:
bool Empty() const
Checks if there is anything selected.
Definition selection.h:114
Implement an OUTPUTFORMATTER to a memory buffer.
Definition richio.h:430
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition confirm.cpp:274
This file is part of the common library.
int BackDirectionsArray[]
int FrontDirectionsArray[]
bool g_DescendingFirst
wxString unknown_str()
bool g_DescendingSecond
wxString empty_str()
#define SetSortCodes(DirArray, Code)
bool g_SortYFirst
static int refDesNumber(const wxString &aRefDes)
The number of a reference, which a sheet suffix may follow.
static bool ChangeArrayCompare(const REFDES_CHANGE &aA, const REFDES_CHANGE &aB)
Compare function used to compare ChangeArray element for sort.
static bool FootprintCompare(const REFDES_INFO &aA, const REFDES_INFO &aB)
Compare function to sort footprints.
wxString ActionMessage[]
#define MAXERROR
#define DESCENDINGFIRST
#define SORTXFIRST
@ UNKNOWN_SUFFIX_REFDES
#define DESCENDINGSECOND
#define ASCENDINGFIRST
#define SORTYFIRST
#define ASCENDINGSECOND
#define MINGRID
#define VALIDPREFIX
#define _(s)
@ FRAME_SCH
Definition frame_type.h:30
std::string message
@ F_Cu
Definition layer_ids.h:60
@ MAIL_SCH_GET_REF_SUFFIXES
Definition mail_type.h:68
KICOMMON_API long long int ValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Convert aTextValue in aUnits to internal units used by the application.
std::optional< SHEET_REF_SUFFIXES > ParseRefSuffixes(const std::string &aPayload)
std::map< KIID, FOOTPRINT_REF_SPLIT > SplitRefSuffixes(const FOOTPRINTS &aFootprints, const SHEET_REF_SUFFIXES *aSuffixes, bool aSuffixMode)
Split each footprint reference into its base and sheet suffix.
Collection of utility functions for component reference designators (refdes)
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
@ RPT_SEVERITY_ACTION
int StrNumCmp(const wxString &aString1, const wxString &aString2, CMP_T aCaseSensitivity)
Compare two strings with alphanumerical content.
Common grid settings, available to every frame.
wxString y
wxString x
std::set< unsigned int > UnavailableRefs
std::string netlist