KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_screen.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) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 2008 Wayne Stambaugh <[email protected]>
7 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <stack>
24#include <vector>
25#include <wx/filefn.h>
26#include <wx/log.h>
27
28#include <eda_item.h>
29#include <id.h>
30#include <string_utils.h>
31#include <kiway.h>
32#include <plotters/plotter.h>
33#include <sch_plotter.h>
34#include <project.h>
35#include <project_sch.h>
36#include <reporter.h>
37#include <trace_helpers.h>
38#include <sch_edit_frame.h>
39#include <sch_item.h>
40
42#include <connection_graph.h>
43#include <junction_helpers.h>
44#include <sch_commit.h>
45#include <sch_pin.h>
46#include <sch_symbol.h>
47#include <sch_group.h>
48#include <sch_junction.h>
49#include <sch_line.h>
50#include <sch_marker.h>
51#include <sch_sheet.h>
52#include <sch_sheet_pin.h>
53#include <sch_text.h>
54#include <schematic.h>
56#include <tool/common_tools.h>
57#include <sim/sim_model.h> // For V6 to V7 simulation model migration.
58#include <locale_io.h>
59
60#include <algorithm>
61#include <math/vector3.h>
62#include <memory>
63
64// TODO(JE) Debugging only
65#include <core/profile.h>
67
68#include "sch_bus_entry.h"
69#include "sch_shape.h"
70
76static const wxChar DanglingProfileMask[] = wxT( "DANGLING_PROFILE" );
77
78
80 BASE_SCREEN( aParent, SCH_SCREEN_T ),
83 m_isReadOnly( false ),
84 m_fileExists( false )
85{
87 m_refCount = 0;
88 m_zoomInitialized = false;
89 m_LastZoomLevel = 1.0;
90
91 // Suitable for schematic only. For symbol_editor and viewlib, must be set to true
92 m_Center = false;
93
94 InitDataPoints( m_paper.GetSizeIU( schIUScale.IU_PER_MILS ) );
95}
96
97
103
104
106{
107 wxCHECK_MSG( GetParent() && GetParent()->Type() == SCHEMATIC_T, nullptr,
108 wxT( "SCH_SCREEN must have a SCHEMATIC parent!" ) );
109
110 return static_cast<SCHEMATIC*>( GetParent() );
111}
112
113
115{
116 for( const std::pair<const wxString, LIB_SYMBOL*>& libSymbol : m_libSymbols )
117 delete libSymbol.second;
118
119 m_libSymbols.clear();
120}
121
122
123void SCH_SCREEN::SetFileName( const wxString& aFileName )
124{
125 // Don't assert here. We still call this after failing to load a file in order to show the
126 // user what we *tried* to load.
127 // wxASSERT( aFileName.IsEmpty() || wxIsAbsolutePath( aFileName ) );
128
129 m_fileName = aFileName;
130}
131
132
134{
135 m_refCount++;
136}
137
138
140{
141 wxCHECK_RET( m_refCount != 0, wxT( "Screen reference count already zero. Bad programmer!" ) );
142 m_refCount--;
143}
144
145
146bool SCH_SCREEN::HasItems( KICAD_T aItemType ) const
147{
148 EE_RTREE::EE_TYPE sheets = m_rtree.OfType( aItemType );
149
150 return sheets.begin() != sheets.end();
151}
152
153
154bool SCH_SCREEN::ClassOf( const EDA_ITEM* aItem )
155{
156 return aItem && SCH_SCREEN_T == aItem->Type();
157}
158
159
160void SCH_SCREEN::Append( SCH_ITEM* aItem, bool aUpdateLibSymbol )
161{
162 if( aItem->Type() != SCH_SHEET_PIN_T && aItem->Type() != SCH_FIELD_T )
163 {
164 // Ensure the item can reach the SCHEMATIC through this screen
165 aItem->SetParent( this );
166
167 if( aItem->Type() == SCH_SYMBOL_T && aUpdateLibSymbol )
168 {
169 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( aItem );
170
171 if( symbol->GetLibSymbolRef() )
172 {
173 symbol->GetLibSymbolRef()->GetDrawItems().sort();
174
175 auto it = m_libSymbols.find( symbol->GetSchSymbolLibraryName() );
176
177 if( it == m_libSymbols.end() || !it->second )
178 {
180 new LIB_SYMBOL( *symbol->GetLibSymbolRef() );
181 }
182 else
183 {
184 // The original library symbol may have changed since the last time
185 // it was added to the schematic. If it has changed, then a new name
186 // must be created for the library symbol list to prevent all of the
187 // other schematic symbols referencing that library symbol from changing.
188 LIB_SYMBOL* foundSymbol = it->second;
189
190 foundSymbol->GetDrawItems().sort();
191
192 if( *foundSymbol != *symbol->GetLibSymbolRef() )
193 {
194 wxString newName;
195 std::vector<wxString> matches;
196
197 getLibSymbolNameMatches( *symbol, matches );
198 foundSymbol = nullptr;
199
200 for( const wxString& libSymbolName : matches )
201 {
202 it = m_libSymbols.find( libSymbolName );
203
204 if( it == m_libSymbols.end() )
205 continue;
206
207 foundSymbol = it->second;
208
209 wxCHECK2( foundSymbol, continue );
210
211 wxString tmp = symbol->GetLibSymbolRef()->GetName();
212
213 // Temporarily update the new symbol library symbol name so it
214 // doesn't fail on the name comparison below.
215 symbol->GetLibSymbolRef()->SetName( foundSymbol->GetName() );
216
217 if( *foundSymbol == *symbol->GetLibSymbolRef() )
218 {
219 newName = libSymbolName;
220 symbol->GetLibSymbolRef()->SetName( tmp );
221 break;
222 }
223
224 symbol->GetLibSymbolRef()->SetName( tmp );
225 foundSymbol = nullptr;
226 }
227
228 if( !foundSymbol )
229 {
230 int cnt = 1;
231
232 newName.Printf( wxT( "%s_%d" ),
234 cnt );
235
236 while( m_libSymbols.find( newName ) != m_libSymbols.end() )
237 {
238 cnt += 1;
239 newName.Printf( wxT( "%s_%d" ),
241 cnt );
242 }
243 }
244
245 // Update the schematic symbol library link as this symbol only exists
246 // in the schematic.
247 symbol->SetSchSymbolLibraryName( newName );
248
249 if( !foundSymbol )
250 {
251 // Update the schematic symbol library link as this symbol does not
252 // exist in any symbol library.
253 LIB_ID newLibId( wxEmptyString, newName );
254 LIB_SYMBOL* newLibSymbol = new LIB_SYMBOL( *symbol->GetLibSymbolRef() );
255
256 newLibSymbol->SetLibId( newLibId );
257 newLibSymbol->SetName( newName );
258 symbol->SetLibSymbol( newLibSymbol->Flatten().release() );
259 m_libSymbols[newName] = newLibSymbol;
260 }
261 }
262 else
263 {
264 // LIB_SYMBOL::Compare ignores embedded files, so an embedded-file-only
265 // edit leaves the cached symbol equal but stale. Refresh the cache from
266 // the instance so the change survives serialization.
267 *foundSymbol->GetEmbeddedFiles() = *symbol->GetLibSymbolRef()->GetEmbeddedFiles();
268 }
269 }
270 }
271 }
272
273 m_rtree.insert( aItem );
275 }
276}
277
278
280{
281 wxCHECK_RET( aScreen, "Invalid screen object." );
282
283 // No need to descend the hierarchy. Once the top level screen is copied, all of its
284 // children are copied as well.
285 for( SCH_ITEM* aItem : aScreen->m_rtree )
286 Append( aItem );
287
288 aScreen->Clear( false );
289}
290
291
292void SCH_SCREEN::Clear( bool aFree )
293{
294 if( aFree )
295 {
296 FreeDrawList();
298 }
299 else
300 {
301 m_rtree.clear();
302 }
303
304 // Clear the project settings
306
307 m_titles.Clear();
308}
309
310
312{
313 // We don't know which order we will encounter dependent items (e.g. pins or fields), so
314 // we store the items to be deleted until we've fully cleared the tree before deleting
315 std::vector<SCH_ITEM*> delete_list;
316
317 std::copy_if( m_rtree.begin(), m_rtree.end(), std::back_inserter( delete_list ),
318 []( SCH_ITEM* aItem )
319 {
320 return ( aItem->Type() != SCH_SHEET_PIN_T && aItem->Type() != SCH_FIELD_T );
321 } );
322
323 m_rtree.clear();
324
325 for( SCH_ITEM* item : delete_list )
326 delete item;
327}
328
329
330void SCH_SCREEN::Update( SCH_ITEM* aItem, bool aUpdateLibSymbol )
331{
332 if( Remove( aItem, aUpdateLibSymbol ) )
333 Append( aItem, aUpdateLibSymbol );
334}
335
336
337bool SCH_SCREEN::Remove( SCH_ITEM* aItem, bool aUpdateLibSymbol )
338{
339 bool retv = m_rtree.remove( aItem );
340
341 // Check if the library symbol for the removed schematic symbol is still required.
342 if( retv && aItem->Type() == SCH_SYMBOL_T && aUpdateLibSymbol )
343 {
344 SCH_SYMBOL* removedSymbol = static_cast<SCH_SYMBOL*>( aItem );
345
346 bool removeUnusedLibSymbol = true;
347
348 for( SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
349 {
350 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
351
352 if( removedSymbol->GetSchSymbolLibraryName() == symbol->GetSchSymbolLibraryName() )
353 {
354 removeUnusedLibSymbol = false;
355 break;
356 }
357 }
358
359 if( removeUnusedLibSymbol )
360 {
361 auto it = m_libSymbols.find( removedSymbol->GetSchSymbolLibraryName() );
362
363 if( it != m_libSymbols.end() )
364 {
365 delete it->second;
366 m_libSymbols.erase( it );
367 }
368 }
369 }
370
371 return retv;
372}
373
374
376{
377 wxCHECK_RET( aItem, wxT( "Cannot delete invalid item from screen." ) );
378
379 // Markers are not saved in the file, no need to flag as modified.
380 // TODO: Maybe we should have a listing somewhere of items that aren't saved?
381 if( aItem->Type() != SCH_MARKER_T )
383
384 Remove( aItem );
385
386 if( aItem->Type() == SCH_SHEET_PIN_T )
387 {
388 // This structure is attached to a sheet, get the parent sheet object.
389 SCH_SHEET_PIN* sheetPin = (SCH_SHEET_PIN*) aItem;
390 SCH_SHEET* sheet = sheetPin->GetParent();
391 wxCHECK_RET( sheet, wxT( "Sheet pin parent not properly set, bad programmer!" ) );
392 sheet->RemovePin( sheetPin );
393 return;
394 }
395
396 delete aItem;
397}
398
399
400bool SCH_SCREEN::CheckIfOnDrawList( const SCH_ITEM* aItem ) const
401{
402 return m_rtree.contains( aItem, true );
403}
404
405
406SCH_ITEM* SCH_SCREEN::GetItem( const VECTOR2I& aPosition, int aAccuracy, KICAD_T aType ) const
407{
408 BOX2I bbox;
409 bbox.SetOrigin( aPosition );
410 bbox.Inflate( aAccuracy );
411
412 for( SCH_ITEM* item : Items().Overlapping( aType, bbox ) )
413 {
414 if( item->HitTest( aPosition, aAccuracy ) )
415 return item;
416 }
417
418 return nullptr;
419}
420
421
422std::set<SCH_ITEM*> SCH_SCREEN::MarkConnections( SCH_ITEM* aItem, bool aSecondPass )
423{
424#define PROCESSED CANDIDATE // Don't use SKIP_STRUCT; IsConnected() returns false if it's set.
425
426 std::set<SCH_ITEM*> retval;
427 std::stack<SCH_ITEM*> toSearch;
428
429 auto getItemEndpoints = []( SCH_ITEM* aCandidate ) -> std::vector<VECTOR2I>
430 {
431 if( !aCandidate )
432 return {};
433
434 if( aCandidate->Type() == SCH_LINE_T )
435 {
436 SCH_LINE* line = static_cast<SCH_LINE*>( aCandidate );
437 return { line->GetStartPoint(), line->GetEndPoint() };
438 }
439
440 if( aCandidate->Type() == SCH_SHAPE_T )
441 {
442 SCH_SHAPE* shape = static_cast<SCH_SHAPE*>( aCandidate );
443
444 if( shape->GetShape() == SHAPE_T::ARC || shape->GetShape() == SHAPE_T::BEZIER )
445 return { shape->GetStart(), shape->GetEnd() };
446 else if( shape->GetShape() == SHAPE_T::RECTANGLE )
447 return shape->GetRectCorners();
448 else if( shape->GetShape() == SHAPE_T::SEGMENT )
449 return { shape->GetStart(), shape->GetEnd() };
450 else if( shape->GetShape() == SHAPE_T::POLY )
451 return shape->GetPolyPoints();
452 }
453
454 return {};
455 };
456
457 if( !aItem || getItemEndpoints( aItem ).empty() )
458 return retval;
459
460 toSearch.push( aItem );
461
462 while( !toSearch.empty() )
463 {
464 SCH_ITEM* item = toSearch.top();
465 toSearch.pop();
466
467 if( item->HasFlag( PROCESSED ) )
468 continue;
469
470 item->SetFlags( PROCESSED );
471
472 const BOX2I bbox = item->GetBoundingBox();
473
474 for( KICAD_T type : { SCH_LINE_T, SCH_SHAPE_T } )
475 {
476 for( SCH_ITEM* candidate : Items().Overlapping( type, bbox ) )
477 {
478 if( candidate->HasFlag( PROCESSED ) )
479 continue;
480
481 std::vector<VECTOR2I> endpoints = getItemEndpoints( candidate );
482
483 if( endpoints.empty() )
484 continue;
485
486 // Skip connecting items on different layers (e.g. buses)
487 if( item->GetLayer() != candidate->GetLayer() )
488 continue;
489
490 bool sharesEndpoint = false;
491
492 for( const VECTOR2I& pt : endpoints )
493 {
494 if( item->IsEndPoint( pt ) )
495 {
496 sharesEndpoint = true;
497
498 if( aSecondPass && item->IsConnected( pt ) )
499 {
500 SCH_ITEM* junction = GetItem( pt, 0, SCH_JUNCTION_T );
501
502 if( junction )
503 retval.insert( junction );
504 }
505 }
506 }
507
508 if( !sharesEndpoint )
509 continue;
510
511 toSearch.push( candidate );
512 retval.insert( candidate );
513 }
514 }
515 }
516
517 for( SCH_ITEM* item : Items() )
518 item->ClearTempFlags();
519
520 return retval;
521}
522
523
524bool SCH_SCREEN::IsJunction( const VECTOR2I& aPosition ) const
525{
527 JUNCTION_HELPERS::AnalyzePoint( Items(), aPosition, false );
528 return info.isJunction;
529}
530
531
532bool SCH_SCREEN::IsExplicitJunction( const VECTOR2I& aPosition ) const
533{
535 JUNCTION_HELPERS::AnalyzePoint( Items(), aPosition, false );
536
537 return info.isJunction && ( !info.hasBusEntry || info.hasBusEntryToMultipleWires );
538}
539
540
541bool SCH_SCREEN::IsExplicitJunctionNeeded( const VECTOR2I& aPosition ) const
542{
544 JUNCTION_HELPERS::AnalyzePoint( Items(), aPosition, false );
545
546 return info.isJunction && ( !info.hasBusEntry || info.hasBusEntryToMultipleWires )
547 && !info.hasExplicitJunctionDot;
548}
549
550
552{
554 JUNCTION_HELPERS::AnalyzePoint( Items(), aPosition, true );
555
556 return info.isJunction && (!info.hasBusEntry || info.hasBusEntryToMultipleWires );
557}
558
559
561 SPIN_STYLE aDefaultOrientation,
562 const SCH_SHEET_PATH* aSheet ) const
563{
564 auto ret = aDefaultOrientation;
565
566 for( SCH_ITEM* item : Items().Overlapping( aPosition ) )
567 {
568 if( item->GetEditFlags() & STRUCT_DELETED )
569 continue;
570
571 switch( item->Type() )
572 {
574 {
575 auto busEntry = static_cast<const SCH_BUS_WIRE_ENTRY*>( item );
576 if( busEntry->m_connected_bus_item )
577 {
578 // bus connected, take the bus direction into consideration only if it is
579 // vertical or horizontal
580 auto bus = static_cast<const SCH_LINE*>( busEntry->m_connected_bus_item );
581 if( bus->Angle().AsDegrees() == 90.0 )
582 {
583 // bus is vertical -> label shall be horizontal and
584 // shall be placed to the side where the bus entry is
585 if( aPosition.x < bus->GetPosition().x )
586 ret = SPIN_STYLE::LEFT;
587 else if( aPosition.x > bus->GetPosition().x )
588 ret = SPIN_STYLE::RIGHT;
589 }
590 else if( bus->Angle().AsDegrees() == 0.0 )
591 {
592 // bus is horizontal -> label shall be vertical and
593 // shall be placed to the side where the bus entry is
594 if( aPosition.y < bus->GetPosition().y )
595 ret = SPIN_STYLE::UP;
596 else if( aPosition.y > bus->GetPosition().y )
597 ret = SPIN_STYLE::BOTTOM;
598 }
599 }
600 }
601 break;
602
603 case SCH_LINE_T:
604 {
605 auto line = static_cast<const SCH_LINE*>( item );
606 // line angles goes between -90 and 90 degrees, but normalize
607 auto angle = line->Angle().Normalize90().AsDegrees();
608
609 if( -45 < angle && angle <= 45 )
610 {
611 if( line->GetStartPoint().x <= line->GetEndPoint().x )
612 ret = line->GetEndPoint() == aPosition ? SPIN_STYLE::RIGHT : SPIN_STYLE::LEFT;
613 else
614 ret = line->GetEndPoint() == aPosition ? SPIN_STYLE::LEFT : SPIN_STYLE::RIGHT;
615 }
616 else
617 {
618 if( line->GetStartPoint().y <= line->GetEndPoint().y )
619 ret = line->GetEndPoint() == aPosition ? SPIN_STYLE::BOTTOM : SPIN_STYLE::UP;
620 else
621 ret = line->GetEndPoint() == aPosition ? SPIN_STYLE::UP : SPIN_STYLE::BOTTOM;
622 }
623 }
624 break;
625
626 case SCH_SYMBOL_T:
627 {
628 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
629
630 for( SCH_PIN* pin : symbol->GetPins( aSheet ) )
631 {
632 if( pin->GetPosition() == aPosition )
633 {
634 ret = GetPinSpinStyle( *pin, *symbol );
635 break;
636 }
637 }
638 }
639 break;
640
641 default: break;
642 }
643 }
644
645 return ret;
646}
647
648
649bool SCH_SCREEN::IsTerminalPoint( const VECTOR2I& aPosition, int aLayer ) const
650{
651 wxCHECK_MSG( aLayer == LAYER_NOTES || aLayer == LAYER_BUS || aLayer == LAYER_WIRE, false,
652 wxT( "Invalid layer type passed to SCH_SCREEN::IsTerminalPoint()." ) );
653
654 SCH_SHEET_PIN* sheetPin;
655 SCH_LABEL_BASE* label;
656
657 switch( aLayer )
658 {
659 case LAYER_BUS:
660 if( GetBus( aPosition ) )
661 return true;
662
663 sheetPin = GetSheetPin( aPosition );
664
665 if( sheetPin && sheetPin->IsConnected( aPosition ) )
666 return true;
667
668 label = GetLabel( aPosition );
669
670 if( label && !label->IsNew() && label->IsConnected( aPosition ) )
671 return true;
672
673 break;
674
675 case LAYER_NOTES:
676 if( GetLine( aPosition ) )
677 return true;
678
679 break;
680
681 case LAYER_WIRE:
682 if( GetItem( aPosition, 1, SCH_BUS_WIRE_ENTRY_T ) )
683 return true;
684
685 if( GetItem( aPosition, 1, SCH_JUNCTION_T ) )
686 return true;
687
688 if( GetPin( aPosition, nullptr, true ) )
689 return true;
690
691 if( GetWire( aPosition ) )
692 return true;
693
694 label = GetLabel( aPosition, 1 );
695
696 if( label && !label->IsNew() && label->IsConnected( aPosition ) )
697 return true;
698
699 sheetPin = GetSheetPin( aPosition );
700
701 if( sheetPin && sheetPin->IsConnected( aPosition ) )
702 return true;
703
704 break;
705
706 default:
707 break;
708 }
709
710 return false;
711}
712
713
715{
716 wxCHECK_RET( Schematic(), "Cannot call SCH_SCREEN::UpdateSymbolLinks with no SCHEMATIC" );
717
718 wxString msg;
719 std::vector<SCH_SYMBOL*> symbols;
721
722 // This will be a nullptr if an s-expression schematic is loaded.
723 LEGACY_SYMBOL_LIBS* legacyLibs = PROJECT_SCH::LegacySchLibs( &Schematic()->Project() );
724
725 for( SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
726 symbols.push_back( static_cast<SCH_SYMBOL*>( item ) );
727
728 // Remove them from the R tree. Their bounding box size may change.
729 for( SCH_SYMBOL* symbol : symbols )
730 Remove( symbol );
731
732 // Clear all existing symbol links.
734
735 for( SCH_SYMBOL* symbol : symbols )
736 {
737 LIB_SYMBOL* tmp = nullptr;
738
739 // If the symbol is already in the internal library, map the symbol to it.
740 auto it = m_libSymbols.find( symbol->GetSchSymbolLibraryName() );
741
742 if( ( it != m_libSymbols.end() ) )
743 {
744 if( aReporter )
745 {
746 msg.Printf( _( "Setting schematic symbol '%s %s' library identifier to '%s'." ),
747 symbol->GetField( FIELD_T::REFERENCE )->GetText(),
748 symbol->GetField( FIELD_T::VALUE )->GetText(),
749 UnescapeString( symbol->GetLibId().Format() ) );
750 aReporter->ReportTail( msg, RPT_SEVERITY_INFO );
751 }
752
753 // Internal library symbols are already flattened so just make a copy.
754 symbol->SetLibSymbol( new LIB_SYMBOL( *it->second ) );
755 continue;
756 }
757
758 if( !symbol->GetLibId().IsValid() )
759 {
760 if( aReporter )
761 {
762 msg.Printf( _( "Schematic symbol reference '%s' library identifier is not valid. "
763 "Unable to link library symbol." ),
764 UnescapeString( symbol->GetLibId().Format() ) );
765 aReporter->ReportTail( msg, RPT_SEVERITY_WARNING );
766 }
767
768 continue;
769 }
770
771 // LIB_TABLE_BASE::LoadSymbol() throws an IO_ERROR if the library nickname
772 // is not found in the table so check if the library still exists in the table
773 // before attempting to load the symbol.
774 std::optional<LIBRARY_TABLE_ROW*> libRow = libs->GetRow( symbol->GetLibId().GetLibNickname() );
775 bool hasLibraryRow = libRow.has_value();
776 bool hasLoadedLibrary = libs->HasLibrary( symbol->GetLibId().GetLibNickname() );
777
778 if( !hasLibraryRow && !legacyLibs )
779 {
780 if( aReporter )
781 {
782 msg.Printf( _( "Symbol library '%s' not found and no fallback cache library "
783 "available. Unable to link library symbol." ),
784 symbol->GetLibId().GetLibNickname().wx_str() );
785 aReporter->ReportTail( msg, RPT_SEVERITY_WARNING );
786 }
787
788 continue;
789 }
790
791 if( hasLibraryRow && !hasLoadedLibrary )
792 {
793 libs->LoadOne( symbol->GetLibId().GetLibNickname() );
794 hasLoadedLibrary = libs->HasLibrary( symbol->GetLibId().GetLibNickname() );
795 }
796
797 if( hasLoadedLibrary )
798 {
799 try
800 {
801 tmp = libs->LoadSymbol( symbol->GetLibId() );
802 }
803 catch( const IO_ERROR& ioe )
804 {
805 if( aReporter )
806 {
807 msg.Printf( _( "I/O error %s resolving library symbol %s" ), ioe.What(),
808 UnescapeString( symbol->GetLibId().Format() ) );
809 aReporter->ReportTail( msg, RPT_SEVERITY_ERROR );
810 }
811 }
812 }
813
814 if( !tmp && legacyLibs && legacyLibs->GetLibraryCount() )
815 {
816 LEGACY_SYMBOL_LIB& legacyCacheLib = legacyLibs->back();
817
818 // It better be the cache library.
819 wxCHECK2( legacyCacheLib.IsCache(), continue );
820
821 wxString id = symbol->GetLibId().Format();
822
823 id.Replace( ':', '_' );
824
825 if( aReporter )
826 {
827 msg.Printf( _( "Falling back to cache to set symbol '%s:%s' link '%s'." ),
828 symbol->GetField( FIELD_T::REFERENCE )->GetText(),
829 symbol->GetField( FIELD_T::VALUE )->GetText(),
830 UnescapeString( id ) );
831 aReporter->ReportTail( msg, RPT_SEVERITY_WARNING );
832 }
833
834 tmp = legacyCacheLib.FindSymbol( id );
835 }
836
837 if( tmp )
838 {
839 // We want a full symbol not just the top level child symbol.
840 std::unique_ptr<LIB_SYMBOL> libSymbol = tmp->Flatten();
841 libSymbol->SetParent();
842
843 m_libSymbols.insert( { symbol->GetSchSymbolLibraryName(),
844 new LIB_SYMBOL( *libSymbol ) } );
845
846 if( aReporter )
847 {
848 msg.Printf( _( "Setting schematic symbol '%s %s' library identifier to '%s'." ),
849 symbol->GetField( FIELD_T::REFERENCE )->GetText(),
850 symbol->GetField( FIELD_T::VALUE )->GetText(),
851 UnescapeString( symbol->GetLibId().Format() ) );
852 aReporter->ReportTail( msg, RPT_SEVERITY_INFO );
853 }
854
855 symbol->SetLibSymbol( libSymbol.release() );
856 }
857 else
858 {
859 if( aReporter )
860 {
861 msg.Printf( _( "No library symbol found for schematic symbol '%s %s'." ),
862 symbol->GetField( FIELD_T::REFERENCE )->GetText(),
863 symbol->GetField( FIELD_T::VALUE )->GetText() );
864 aReporter->ReportTail( msg, RPT_SEVERITY_ERROR );
865 }
866 }
867 }
868
869 // Changing the symbol may adjust the bbox of the symbol. This re-inserts the
870 // item with the new bbox
871 for( SCH_SYMBOL* symbol : symbols )
872 Append( symbol );
873}
874
875
877{
878 std::vector<SCH_SYMBOL*> symbols;
879
880 for( SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
881 symbols.push_back( static_cast<SCH_SYMBOL*>( item ) );
882
883 for( SCH_SYMBOL* symbol : symbols )
884 {
885 // Changing the symbol may adjust the bbox of the symbol; remove and reinsert it afterwards.
886 m_rtree.remove( symbol );
887
888 auto it = m_libSymbols.find( symbol->GetSchSymbolLibraryName() );
889
890 if( it != m_libSymbols.end() )
891 symbol->SetLibSymbol( new LIB_SYMBOL( *it->second ) );
892 else
893 symbol->SetLibSymbol( nullptr );
894
895 m_rtree.insert( symbol );
896 }
897}
898
899
901{
902 for( SCH_ITEM* item : Items() )
903 item->SetConnectivityDirty( true );
904}
905
906
907void SCH_SCREEN::Plot( PLOTTER* aPlotter, const SCH_PLOT_OPTS& aPlotOpts ) const
908{
909 std::vector<SCH_ITEM*> items;
910 items.reserve( Items().size() );
911
912 for( SCH_ITEM* item : Items() )
913 items.push_back( item );
914
915 Plot( aPlotter, aPlotOpts, items );
916}
917
918
919void SCH_SCREEN::Plot( PLOTTER* aPlotter, const SCH_PLOT_OPTS& aPlotOpts, const std::vector<SCH_ITEM*>& aItems ) const
920{
921 // Ensure links are up to date, even if a library was reloaded for some reason:
922 std::vector<SCH_ITEM*> junctions;
923 std::vector<SCH_ITEM*> bitmaps;
924 std::vector<SCH_SYMBOL*> symbols;
925 std::vector<SCH_ITEM*> other;
926 double hopOverScale = 0.0;
927 int defaultLineWidth = schIUScale.MilsToIU( DEFAULT_LINE_WIDTH_MILS );
928
929 if( !aItems.empty() && aItems[0]->Schematic() )
930 {
931 hopOverScale = aItems[0]->Schematic()->Settings().GetHopOverScale();
932 defaultLineWidth = aItems[0]->Schematic()->Settings().m_DefaultLineWidth;
933 }
934
935 for( SCH_ITEM* item : aItems )
936 {
937 if( item->IsMoving() )
938 continue;
939
940 if( item->Type() == SCH_JUNCTION_T )
941 junctions.push_back( item );
942 else if( item->Type() == SCH_BITMAP_T )
943 bitmaps.push_back( item );
944 else
945 other.push_back( item );
946
947 // Where the symbols overlap each other, we need to plot the text items a second
948 // time to get them on top of the overlapping element. This collection is in addition
949 // to the symbols already collected in `other`
950 if( item->Type() == SCH_SYMBOL_T )
951 {
952 for( SCH_ITEM* sym : m_rtree.Overlapping( SCH_SYMBOL_T, item->GetBoundingBox() ) )
953 {
954 if( sym != item )
955 {
956 symbols.push_back( static_cast<SCH_SYMBOL*>( item ) );
957 break;
958 }
959 }
960 }
961 }
962
964 std::sort( other.begin(), other.end(),
965 []( const SCH_ITEM* a, const SCH_ITEM* b )
966 {
967 if( a->Type() == b->Type() )
968 return a->GetLayer() > b->GetLayer();
969
970 return a->Type() > b->Type();
971 } );
972
973 auto* renderSettings = static_cast<SCH_RENDER_SETTINGS*>( aPlotter->RenderSettings() );
974 constexpr bool background = true;
975
976 // Bitmaps are drawn first to ensure they are in the background
977 // This is particularly important for the wxPostscriptDC (used in *nix printers) as
978 // the bitmap PS command clears the screen
979 for( SCH_ITEM* item : bitmaps )
980 {
981 aPlotter->SetCurrentLineWidth( item->GetEffectivePenWidth( renderSettings ) );
982 item->Plot( aPlotter, background, aPlotOpts, 0, 0, { 0, 0 }, false );
983 }
984
985 // Plot the background items
986 for( SCH_ITEM* item : other )
987 {
988 aPlotter->SetCurrentLineWidth( item->GetEffectivePenWidth( renderSettings ) );
989 item->Plot( aPlotter, background, aPlotOpts, 0, 0, { 0, 0 }, false );
990 }
991
992 // Plot the foreground items
993 for( SCH_ITEM* item : other )
994 {
995 double lineWidth = item->GetEffectivePenWidth( renderSettings );
996 aPlotter->SetCurrentLineWidth( lineWidth );
997
998 if( item->Type() != SCH_LINE_T )
999 {
1000 item->Plot( aPlotter, !background, aPlotOpts, 0, 0, { 0, 0 }, false );
1001 }
1002 else
1003 {
1004 SCH_LINE* aLine = static_cast<SCH_LINE*>( item );
1005
1006 if( ( !aLine->IsWire() && !aLine->IsBus() ) || !aPlotOpts.m_plotHopOver )
1007 {
1008 item->Plot( aPlotter, !background, aPlotOpts, 0, 0, { 0, 0 }, false );
1009 }
1010 else
1011 {
1012 double arcRadius = defaultLineWidth * hopOverScale;
1013 std::vector<VECTOR3I> curr_wire_shape = aLine->BuildWireWithHopShape( this, arcRadius );
1014
1015 // The hop pieces are standalone copies/shapes without the connection map, so
1016 // resolve the net-class color and style from the original wire and reuse them.
1017 COLOR4D lineColor = aLine->GetLineColor();
1018 LINE_STYLE lineStyle = aLine->GetEffectiveLineStyle();
1019
1020 for( size_t ii = 1; ii < curr_wire_shape.size(); ii++ )
1021 {
1022 VECTOR2I start( curr_wire_shape[ii-1].x, curr_wire_shape[ii-1].y );
1023
1024 if( curr_wire_shape[ii-1].z == 0 ) // This is the start point of a segment
1025 // there are always 2 points in list for a segment
1026 {
1027 VECTOR2I end( curr_wire_shape[ii].x, curr_wire_shape[ii].y );
1028
1029 SCH_LINE curr_line( *aLine );
1030 curr_line.SetStartPoint( start );
1031 curr_line.SetEndPoint( end );
1032 curr_line.SetLineColor( lineColor );
1033 curr_line.SetLineStyle( lineStyle );
1034 curr_line.Plot( aPlotter, !background, aPlotOpts, 0, 0, { 0, 0 }, false );
1035 }
1036 else // This is the start point of a arc. there are always 3 points in list for an arc
1037 {
1038 VECTOR2I arc_middle( curr_wire_shape[ii].x, curr_wire_shape[ii].y );
1039 ii++;
1040 VECTOR2I arc_end( curr_wire_shape[ii].x, curr_wire_shape[ii].y );
1041 ii++;
1042
1043 SCH_SHAPE arc( SHAPE_T::ARC, aLine->GetLayer(), lineWidth );
1044
1045 arc.SetArcGeometry( start, arc_middle, arc_end );
1046 // Hop are a small arc, so use a solid line style gives best results
1048 arc.SetLineColor( lineColor );
1049 arc.Plot( aPlotter, !background, aPlotOpts, 0, 0, { 0, 0 }, false );
1050 }
1051 }
1052 }
1053 }
1054 }
1055
1056 // After plotting the symbols as a group above (in `other`), we need to overplot the pins
1057 // and symbols to ensure that they are always visible
1058 TRANSFORM savedTransform = renderSettings->m_Transform;
1059
1060 wxString variant = Schematic()->GetCurrentVariant();
1061 SCH_SHEET_PATH* sheet = &Schematic()->CurrentSheet();
1062
1063 for( const SCH_SYMBOL* sym :symbols )
1064 {
1065 renderSettings->m_Transform = sym->GetTransform();
1066 aPlotter->SetCurrentLineWidth( sym->GetEffectivePenWidth( renderSettings ) );
1067
1068 bool dnp = sym->GetDNP( sheet, variant );
1069
1070 for( SCH_FIELD field : sym->GetFields() )
1071 {
1072 field.ClearRenderCache();
1073 field.Plot( aPlotter, false, aPlotOpts, sym->GetUnit(), sym->GetBodyStyle(), { 0, 0 }, dnp );
1074
1075 if( sym->IsSymbolLikePowerLocalLabel() && field.GetId() == FIELD_T::VALUE
1076 && ( field.IsVisible() || field.IsForceVisible() ) )
1077 {
1078 sym->PlotLocalPowerIconShape( aPlotter );
1079 }
1080 }
1081
1082 sym->PlotPins( aPlotter, dnp );
1083
1084 if( dnp )
1085 sym->PlotDNP( aPlotter );
1086 }
1087
1088 renderSettings->m_Transform = savedTransform;
1089
1090 for( SCH_ITEM* item : junctions )
1091 {
1092 aPlotter->SetCurrentLineWidth( item->GetEffectivePenWidth( renderSettings ) );
1093 item->Plot( aPlotter, !background, aPlotOpts, 0, 0, { 0, 0 }, false );
1094 }
1095}
1096
1097
1099{
1100 for( SCH_ITEM* item : Items() )
1101 item->ClearTempFlags();
1102}
1103
1104
1105SCH_PIN* SCH_SCREEN::GetPin( const VECTOR2I& aPosition, SCH_SYMBOL** aSymbol,
1106 bool aEndPointOnly ) const
1107{
1108 SCH_SYMBOL* candidate = nullptr;
1109 SCH_PIN* pin = nullptr;
1110
1111 for( SCH_ITEM* item : Items().Overlapping( SCH_SYMBOL_T, aPosition ) )
1112 {
1113 candidate = static_cast<SCH_SYMBOL*>( item );
1114
1115 if( aEndPointOnly )
1116 {
1117 pin = nullptr;
1118
1119 if( !candidate->GetLibSymbolRef() )
1120 continue;
1121
1122 for( SCH_PIN* test_pin : candidate->GetLibPins() )
1123 {
1124 if( candidate->GetPinPhysicalPosition( test_pin ) == aPosition )
1125 {
1126 pin = test_pin;
1127 break;
1128 }
1129 }
1130
1131 if( pin )
1132 break;
1133 }
1134 else
1135 {
1136 pin = static_cast<SCH_PIN*>( candidate->GetDrawItem( aPosition, SCH_PIN_T ) );
1137
1138 if( pin )
1139 break;
1140 }
1141 }
1142
1143 if( pin && aSymbol )
1144 *aSymbol = candidate;
1145
1146 return pin;
1147}
1148
1149
1151{
1152 SCH_SHEET_PIN* sheetPin = nullptr;
1153
1154 for( SCH_ITEM* item : Items().Overlapping( SCH_SHEET_T, aPosition ) )
1155 {
1156 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
1157
1158 sheetPin = sheet->GetPin( aPosition );
1159
1160 if( sheetPin )
1161 break;
1162 }
1163
1164 return sheetPin;
1165}
1166
1167
1168size_t SCH_SCREEN::CountConnectedItems( const VECTOR2I& aPos, bool aTestJunctions ) const
1169{
1170 size_t count = 0;
1171
1172 for( const SCH_ITEM* item : Items().Overlapping( aPos ) )
1173 {
1174 if( ( item->Type() != SCH_JUNCTION_T || aTestJunctions ) && item->IsConnected( aPos ) )
1175 count++;
1176 }
1177
1178 return count;
1179}
1180
1181
1182void SCH_SCREEN::ClearAnnotation( SCH_SHEET_PATH* aSheetPath, bool aResetPrefix )
1183{
1184
1185 for( SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
1186 {
1187 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1188
1189 symbol->ClearAnnotation( aSheetPath, aResetPrefix );
1190 }
1191}
1192
1193
1195{
1196 if( GetClientSheetPaths().size() <= 1 ) // No need for alternate reference
1197 return;
1198
1199 for( SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
1200 {
1201 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1202
1203 // Add (when not existing) all sheet path entries
1204 for( const SCH_SHEET_PATH& sheet : GetClientSheetPaths() )
1205 symbol->AddSheetPathReferenceEntryIfMissing( sheet.Path() );
1206 }
1207}
1208
1209
1210void SCH_SCREEN::GetHierarchicalItems( std::vector<SCH_ITEM*>* aItems ) const
1211{
1212 static const std::vector<KICAD_T> hierarchicalTypes = { SCH_SYMBOL_T,
1215
1216 for( SCH_ITEM* item : Items() )
1217 {
1218 if( item->IsType( hierarchicalTypes ) )
1219 aItems->push_back( item );
1220 }
1221}
1222
1223
1224void SCH_SCREEN::GetSheets( std::vector<SCH_ITEM*>* aItems ) const
1225{
1226 for( SCH_ITEM* item : Items().OfType( SCH_SHEET_T ) )
1227 aItems->push_back( item );
1228
1229 std::sort( aItems->begin(), aItems->end(),
1230 []( EDA_ITEM* a, EDA_ITEM* b ) -> bool
1231 {
1232 if( a->GetPosition().x == b->GetPosition().x )
1233 {
1234 // Ensure deterministic sort
1235 if( a->GetPosition().y == b->GetPosition().y )
1236 return a->m_Uuid < b->m_Uuid;
1237
1238 return a->GetPosition().y < b->GetPosition().y;
1239 }
1240 else
1241 {
1242 return a->GetPosition().x < b->GetPosition().x;
1243 }
1244 } );
1245}
1246
1247
1249 std::function<void( SCH_ITEM* )>* aChangedHandler ) const
1250{
1251 PROF_TIMER timer( __FUNCTION__ );
1252
1253 std::vector<DANGLING_END_ITEM> endPointsByPos;
1254 std::vector<DANGLING_END_ITEM> endPointsByType;
1255
1256 auto get_ends =
1257 [&]( SCH_ITEM* item )
1258 {
1259 if( item->IsConnectable() )
1260 item->GetEndPoints( endPointsByType );
1261 };
1262
1263 auto update_state =
1264 [&]( SCH_ITEM* item )
1265 {
1266 if( item->UpdateDanglingState( endPointsByType, endPointsByPos, aPath ) )
1267 {
1268 if( aChangedHandler )
1269 ( *aChangedHandler )( item );
1270 }
1271 };
1272
1273 for( SCH_ITEM* item : Items() )
1274 {
1275 get_ends( item );
1276 item->RunOnChildren( get_ends, RECURSE_MODE::NO_RECURSE );
1277 }
1278
1279 PROF_TIMER sortTimer( "SCH_SCREEN::TestDanglingEnds pre-sort" );
1280 endPointsByPos = endPointsByType;
1281 DANGLING_END_ITEM_HELPER::sort_dangling_end_items( endPointsByType, endPointsByPos );
1282 sortTimer.Stop();
1283
1284 if( wxLog::IsAllowedTraceMask( DanglingProfileMask ) )
1285 sortTimer.Show();
1286
1287 for( SCH_ITEM* item : Items() )
1288 {
1289 update_state( item );
1290 item->RunOnChildren( update_state, RECURSE_MODE::NO_RECURSE );
1291 }
1292
1293 if( wxLog::IsAllowedTraceMask( DanglingProfileMask ) )
1294 timer.Show();
1295}
1296
1297
1298SCH_LINE* SCH_SCREEN::GetLine( const VECTOR2I& aPosition, int aAccuracy, int aLayer,
1299 SCH_LINE_TEST_T aSearchType ) const
1300{
1301 // an accuracy of 0 had problems with rounding errors; use at least 1
1302 aAccuracy = std::max( aAccuracy, 1 );
1303
1304 for( SCH_ITEM* item : Items().Overlapping( aPosition, aAccuracy ) )
1305 {
1306 if( item->Type() != SCH_LINE_T )
1307 continue;
1308
1309 if( item->GetLayer() != aLayer )
1310 continue;
1311
1312 if( !item->HitTest( aPosition, aAccuracy ) )
1313 continue;
1314
1315 switch( aSearchType )
1316 {
1317 case ENTIRE_LENGTH_T:
1318 return (SCH_LINE*) item;
1319
1321 if( !( (SCH_LINE*) item )->IsEndPoint( aPosition ) )
1322 return (SCH_LINE*) item;
1323 break;
1324
1325 case END_POINTS_ONLY_T:
1326 if( ( (SCH_LINE*) item )->IsEndPoint( aPosition ) )
1327 return (SCH_LINE*) item;
1328 }
1329 }
1330
1331 return nullptr;
1332}
1333
1334
1335std::vector<SCH_LINE*> SCH_SCREEN::GetBusesAndWires( const VECTOR2I& aPosition,
1336 bool aIgnoreEndpoints ) const
1337{
1338 std::vector<SCH_LINE*> retVal;
1339
1340 for( SCH_ITEM* item : Items().Overlapping( SCH_LINE_T, aPosition ) )
1341 {
1342 if( item->IsType( { SCH_ITEM_LOCATE_WIRE_T, SCH_ITEM_LOCATE_BUS_T } ) )
1343 {
1344 SCH_LINE* wire = static_cast<SCH_LINE*>( item );
1345
1346 if( aIgnoreEndpoints && wire->IsEndPoint( aPosition ) )
1347 continue;
1348
1349 if( IsPointOnSegment( wire->GetStartPoint(), wire->GetEndPoint(), aPosition ) )
1350 retVal.push_back( wire );
1351 }
1352 }
1353
1354 return retVal;
1355}
1356
1357
1358std::vector<VECTOR2I> SCH_SCREEN::GetConnections() const
1359{
1360 std::vector<VECTOR2I> retval;
1361
1362 for( SCH_ITEM* item : Items() )
1363 {
1364 // Avoid items that are changing
1365 if( !( item->GetEditFlags() & ( IS_MOVING | IS_DELETED ) ) )
1366 {
1367 std::vector<VECTOR2I> pts = item->GetConnectionPoints();
1368 retval.insert( retval.end(), pts.begin(), pts.end() );
1369 }
1370 }
1371
1372 // We always have some overlapping connection points. Drop duplicates here
1373 std::sort( retval.begin(), retval.end(),
1374 []( const VECTOR2I& a, const VECTOR2I& b ) -> bool
1375 {
1376 return a.x < b.x || ( a.x == b.x && a.y < b.y );
1377 } );
1378
1379 retval.erase( std::unique( retval.begin(), retval.end() ), retval.end() );
1380
1381 return retval;
1382}
1383
1384
1385std::vector<VECTOR2I> SCH_SCREEN::GetNeededJunctions( const std::deque<EDA_ITEM*>& aItems ) const
1386{
1387 std::vector<VECTOR2I> pts;
1388 std::vector<VECTOR2I> connections = GetConnections();
1389
1390 for( const EDA_ITEM* edaItem : aItems )
1391 {
1392 const SCH_ITEM* item = dynamic_cast<const SCH_ITEM*>( edaItem );
1393
1394 if( !item || !item->IsConnectable() )
1395 continue;
1396
1397 std::vector<VECTOR2I> new_pts = item->GetConnectionPoints();
1398 pts.insert( pts.end(), new_pts.begin(), new_pts.end() );
1399
1400 // If the item is a line, we also add any connection points from the rest of the schematic
1401 // that terminate on the line after it is moved.
1402 if( item->Type() == SCH_LINE_T )
1403 {
1404 SCH_LINE* line = (SCH_LINE*) item;
1405
1406 for( const VECTOR2I& pt : connections )
1407 {
1408 if( IsPointOnSegment( line->GetStartPoint(), line->GetEndPoint(), pt ) )
1409 pts.push_back( pt );
1410 }
1411 }
1412 }
1413
1414 // We always have some overlapping connection points. Drop duplicates here
1415 std::sort( pts.begin(), pts.end(),
1416 []( const VECTOR2I& a, const VECTOR2I& b ) -> bool
1417 {
1418 return a.x < b.x || ( a.x == b.x && a.y < b.y );
1419 } );
1420
1421 pts.erase( unique( pts.begin(), pts.end() ), pts.end() );
1422
1423 // We only want the needed junction points, remove all the others
1424 pts.erase( std::remove_if( pts.begin(), pts.end(),
1425 [this]( const VECTOR2I& a ) -> bool
1426 {
1427 return !IsExplicitJunctionNeeded( a );
1428 } ),
1429 pts.end() );
1430
1431 return pts;
1432}
1433
1434
1435SCH_LABEL_BASE* SCH_SCREEN::GetLabel( const VECTOR2I& aPosition, int aAccuracy ) const
1436{
1437 for( SCH_ITEM* item : Items().Overlapping( aPosition, aAccuracy ) )
1438 {
1439 switch( item->Type() )
1440 {
1441 case SCH_LABEL_T:
1442 case SCH_GLOBAL_LABEL_T:
1443 case SCH_HIER_LABEL_T:
1445 if( item->HitTest( aPosition, aAccuracy ) )
1446 return static_cast<SCH_LABEL_BASE*>( item );
1447
1448 break;
1449
1450 default:
1451 ;
1452 }
1453 }
1454
1455 return nullptr;
1456}
1457
1458
1460{
1461 wxCHECK( aLibSymbol, /* void */ );
1462
1463 wxString libSymbolName = aLibSymbol->GetLibId().Format().wx_str();
1464
1465 auto it = m_libSymbols.find( libSymbolName );
1466
1467 if( it != m_libSymbols.end() )
1468 {
1469 delete it->second;
1470 m_libSymbols.erase( it );
1471 }
1472
1473 m_libSymbols[libSymbolName] = aLibSymbol;
1474}
1475
1476
1478{
1479 SCHEMATIC* schematic = Schematic();
1480
1481 const std::vector<wxString>* embeddedFonts = schematic->GetEmbeddedFiles()->UpdateFontFiles();
1482
1483 for( auto& [name, libSym] : m_libSymbols )
1484 {
1485 for( auto& [filename, embeddedFile] : libSym->EmbeddedFileMap() )
1486 {
1487 EMBEDDED_FILES::EMBEDDED_FILE* file = schematic->GetEmbeddedFile( filename );
1488
1489 if( file )
1490 {
1491 embeddedFile->compressedEncodedData = file->compressedEncodedData;
1492 embeddedFile->decompressedData = file->decompressedData;
1493 embeddedFile->data_hash = file->data_hash;
1494 embeddedFile->is_valid = file->is_valid;
1495 }
1496 }
1497
1498 libSym->RunOnChildren(
1499 [&]( SCH_ITEM* aChild )
1500 {
1501 if( EDA_TEXT* textItem = dynamic_cast<EDA_TEXT*>( aChild ) )
1502 textItem->ResolveFont( embeddedFonts );
1503 },
1505 }
1506
1507 std::vector<SCH_ITEM*> items_to_update;
1508
1509 for( SCH_ITEM* item : Items() )
1510 {
1511 bool update = false;
1512
1513 if( EDA_TEXT* textItem = dynamic_cast<EDA_TEXT*>( item ) )
1514 update |= textItem->ResolveFont( embeddedFonts );
1515
1516 item->RunOnChildren(
1517 [&]( SCH_ITEM* aChild )
1518 {
1519 if( EDA_TEXT* textItem = dynamic_cast<EDA_TEXT*>( aChild ) )
1520 update |= textItem->ResolveFont( embeddedFonts );
1521 },
1523
1524 if( update )
1525 items_to_update.push_back( item );
1526 }
1527
1528 for( SCH_ITEM* item : items_to_update )
1529 Update( item );
1530}
1531
1532
1533void SCH_SCREEN::AddBusAlias( std::shared_ptr<BUS_ALIAS> aAlias )
1534{
1535 if( SCHEMATIC* schematic = Schematic() )
1536 schematic->AddBusAlias( aAlias );
1537}
1538
1539
1541{
1542 for( SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
1543 {
1544 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1545
1546 // Add missing value and footprint instance data for legacy schematics.
1547 for( const SCH_SYMBOL_INSTANCE& instance : symbol->GetInstances() )
1548 {
1549 symbol->AddHierarchicalReference( instance.m_Path, instance.m_Reference,
1550 instance.m_Unit );
1551 }
1552 }
1553}
1554
1555
1557{
1558 for( SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
1559 {
1560 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1561
1562 // Fix pre-8.0 legacy power symbols with invisible pins
1563 // that have mismatched pin names and value fields
1564 if( symbol->GetLibSymbolRef()
1565 && symbol->GetLibSymbolRef()->IsGlobalPower()
1566 && symbol->GetAllLibPins().size() > 0
1567 && symbol->GetAllLibPins()[0]->IsGlobalPower()
1568 && !symbol->GetAllLibPins()[0]->IsVisible() )
1569 {
1570 symbol->SetValueFieldText( symbol->GetAllLibPins()[0]->GetName() );
1571 }
1572 }
1573}
1574
1575
1577 std::vector<wxString>& aMatches )
1578{
1579 wxString searchName = aSymbol.GetLibId().GetUniStringLibId();
1580
1581 if( m_libSymbols.find( searchName ) != m_libSymbols.end() )
1582 aMatches.emplace_back( searchName );
1583
1584 searchName = aSymbol.GetLibId().GetUniStringLibItemName() + wxS( "_" );
1585
1586 long tmp;
1587 wxString suffix;
1588
1589 for( auto& pair : m_libSymbols )
1590 {
1591 if( pair.first.StartsWith( searchName, &suffix ) && suffix.ToLong( &tmp ) )
1592 aMatches.emplace_back( pair.first );
1593 }
1594
1595 return aMatches.size();
1596}
1597
1598
1599void SCH_SCREEN::PruneOrphanedSymbolInstances( const wxString& aProjectName,
1600 const SCH_SHEET_LIST& aValidSheetPaths )
1601{
1602 // The project name cannot be empty. Projects older than 7.0 did not save project names
1603 // when saving instance data. Running this algorithm with an empty project name would
1604 // clobber all instance data for projects other than the current one when a schematic
1605 // file is shared across multiple projects. Because running the schematic editor in
1606 // stand alone mode can result in an empty project name, do not assert here.
1607 if( aProjectName.IsEmpty() )
1608 return;
1609
1610 for( SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
1611 {
1612 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1613
1614 wxCHECK2( symbol, continue );
1615
1616 std::set<KIID_PATH> pathsToPrune;
1617 const std::vector<SCH_SYMBOL_INSTANCE> instances = symbol->GetInstances();
1618
1619 for( const SCH_SYMBOL_INSTANCE& instance : instances )
1620 {
1621 // Ignore instance paths from other projects.
1622 if( aProjectName != instance.m_ProjectName )
1623 continue;
1624
1625 std::optional<SCH_SHEET_PATH> pathFound =
1626 aValidSheetPaths.GetSheetPathByKIIDPath( instance.m_Path );
1627
1628 // Check for paths that do not exist in the current project and paths that do
1629 // not contain the current symbol.
1630 if( !pathFound )
1631 pathsToPrune.emplace( instance.m_Path );
1632 else if( pathFound.value().LastScreen() != this )
1633 pathsToPrune.emplace( pathFound.value().Path() );
1634 }
1635
1636 for( const KIID_PATH& sheetPath : pathsToPrune )
1637 {
1638 wxLogTrace( traceSchSheetPaths, wxS( "Pruning project '%s' symbol instance %s." ),
1639 aProjectName, sheetPath.AsString() );
1640 symbol->RemoveInstance( sheetPath );
1641 }
1642 }
1643}
1644
1645
1646void SCH_SCREEN::PruneOrphanedSheetInstances( const wxString& aProjectName,
1647 const SCH_SHEET_LIST& aValidSheetPaths )
1648{
1649 // The project name cannot be empty. Projects older than 7.0 did not save project names
1650 // when saving instance data. Running this algorithm with an empty project name would
1651 // clobber all instance data for projects other than the current one when a schematic
1652 // file is shared across multiple projects. Because running the schematic editor in
1653 // stand alone mode can result in an empty project name, do not assert here.
1654 if( aProjectName.IsEmpty() )
1655 return;
1656
1657 for( SCH_ITEM* item : Items().OfType( SCH_SHEET_T ) )
1658 {
1659 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
1660
1661 wxCHECK2( sheet, continue );
1662
1663 std::set<KIID_PATH> pathsToPrune;
1664 const std::vector<SCH_SHEET_INSTANCE> instances = sheet->GetInstances();
1665
1666 for( const SCH_SHEET_INSTANCE& instance : instances )
1667 {
1668 // Ignore instance paths from other projects.
1669 if( aProjectName != instance.m_ProjectName )
1670 continue;
1671
1672 std::optional<SCH_SHEET_PATH> pathFound =
1673 aValidSheetPaths.GetSheetPathByKIIDPath( instance.m_Path );
1674
1675 // Check for paths that do not exist in the current project and paths that do
1676 // not contain the current symbol.
1677 if( !pathFound )
1678 pathsToPrune.emplace( instance.m_Path );
1679 else if( pathFound.value().LastScreen() != this )
1680 pathsToPrune.emplace( pathFound.value().Path() );
1681 }
1682
1683 for( const KIID_PATH& sheetPath : pathsToPrune )
1684 {
1685 wxLogTrace( traceSchSheetPaths, wxS( "Pruning project '%s' sheet instance %s." ),
1686 aProjectName, sheetPath.AsString() );
1687 sheet->RemoveInstance( sheetPath );
1688 }
1689 }
1690}
1691
1692
1694{
1695 wxString trimmedFieldName;
1696
1697 for( const SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
1698 {
1699 const SCH_SYMBOL* symbol = static_cast<const SCH_SYMBOL*>( item );
1700
1701 wxCHECK2( symbol, continue );
1702
1703 for( const SCH_FIELD& field : symbol->GetFields() )
1704 {
1705 trimmedFieldName = field.GetName();
1706 trimmedFieldName.Trim();
1707 trimmedFieldName.Trim( false );
1708
1709 if( field.GetName() != trimmedFieldName )
1710 return true;
1711 }
1712 }
1713
1714 return false;
1715}
1716
1717
1718std::set<wxString> SCH_SCREEN::GetSheetNames() const
1719{
1720 std::set<wxString> retv;
1721
1722 for( SCH_ITEM* item : Items().OfType( SCH_SHEET_T ) )
1723 {
1724 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
1725
1726 wxCHECK2( sheet, continue );
1727
1728 retv.emplace( sheet->GetName() );
1729 }
1730
1731 return retv;
1732}
1733
1734
1736{
1737 wxCHECK( Schematic(), false );
1738
1739 SCH_SHEET_LIST hierarchy = Schematic()->Hierarchy();
1740
1741 for( const SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
1742 {
1743 const SCH_SYMBOL* symbol = static_cast<const SCH_SYMBOL*>( item );
1744
1745 const std::vector<SCH_SYMBOL_INSTANCE> symbolInstances = symbol->GetInstances();
1746
1747 for( const SCH_SYMBOL_INSTANCE& instance : symbolInstances )
1748 {
1749 if( !hierarchy.HasPath( instance.m_Path ) )
1750 return true;
1751 }
1752 }
1753
1754 return false;
1755}
1756
1757
1758wxString SCH_SCREEN::GroupsSanityCheck( bool repair )
1759{
1760 if( repair )
1761 {
1762 while( GroupsSanityCheckInternal( repair ) != wxEmptyString )
1763 {
1764 };
1765
1766 return wxEmptyString;
1767 }
1768 return GroupsSanityCheckInternal( repair );
1769}
1770
1771
1773{
1774 // Cycle detection
1775 //
1776 // Each group has at most one parent group.
1777 // So we start at group 0 and traverse the parent chain, marking groups seen along the way.
1778 // If we ever see a group that we've already marked, that's a cycle.
1779 // If we reach the end of the chain, we know all groups in that chain are not part of any cycle.
1780 //
1781 // Algorithm below is linear in the # of groups because each group is visited only once.
1782 // There may be extra time taken due to the container access calls and iterators.
1783 //
1784 // Groups we know are cycle free
1785 std::unordered_set<EDA_GROUP*> knownCycleFreeGroups;
1786 // Groups in the current chain we're exploring.
1787 std::unordered_set<EDA_GROUP*> currentChainGroups;
1788 // Groups we haven't checked yet.
1789 std::unordered_set<EDA_GROUP*> toCheckGroups;
1790
1791 // Initialize set of groups and generators to check that could participate in a cycle.
1792 for( SCH_ITEM* item : Items().OfType( SCH_GROUP_T ) )
1793 toCheckGroups.insert( static_cast<SCH_GROUP*>( item ) );
1794
1795 while( !toCheckGroups.empty() )
1796 {
1797 currentChainGroups.clear();
1798 EDA_GROUP* group = *toCheckGroups.begin();
1799
1800 while( true )
1801 {
1802 if( currentChainGroups.find( group ) != currentChainGroups.end() )
1803 {
1804 if( repair )
1805 Remove( static_cast<SCH_ITEM*>( group->AsEdaItem() ) );
1806
1807 return "Cycle detected in group membership";
1808 }
1809 else if( knownCycleFreeGroups.find( group ) != knownCycleFreeGroups.end() )
1810 {
1811 // Parent is a group we know does not lead to a cycle
1812 break;
1813 }
1814
1815 currentChainGroups.insert( group );
1816 // We haven't visited currIdx yet, so it must be in toCheckGroups
1817 toCheckGroups.erase( group );
1818
1819 group = group->AsEdaItem()->GetParentGroup();
1820
1821 if( !group )
1822 {
1823 // end of chain and no cycles found in this chain
1824 break;
1825 }
1826 }
1827
1828 // No cycles found in chain, so add it to set of groups we know don't participate
1829 // in a cycle.
1830 knownCycleFreeGroups.insert( currentChainGroups.begin(), currentChainGroups.end() );
1831 }
1832
1833 // Success
1834 return "";
1835}
1836
1837
1839{
1840 wxCHECK( Schematic() && !m_fileName.IsEmpty(), false );
1841
1842 wxFileName thisScreenFn( m_fileName );
1843 wxFileName thisProjectFn( Schematic()->Project().GetProjectFullName() );
1844
1845 wxCHECK( thisProjectFn.IsAbsolute(), false );
1846
1847 if( thisScreenFn.GetDirCount() < thisProjectFn.GetDirCount() )
1848 return false;
1849
1850 while( thisProjectFn.GetDirCount() != thisScreenFn.GetDirCount() )
1851 thisScreenFn.RemoveLastDir();
1852
1853 return thisScreenFn.GetPath() == thisProjectFn.GetPath();
1854}
1855
1856
1857std::set<wxString> SCH_SCREEN::GetVariantNames() const
1858{
1859 std::set<wxString> variantNames;
1860
1861 for( const SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
1862 {
1863 const SCH_SYMBOL* symbol = static_cast<const SCH_SYMBOL*>( item );
1864
1865 wxCHECK2( symbol, continue );
1866
1867 const std::vector<SCH_SYMBOL_INSTANCE> symbolInstances = symbol->GetInstances();
1868
1869 for( const SCH_SYMBOL_INSTANCE& instance : symbolInstances )
1870 {
1871 for( const auto& [name, variant] : instance.m_Variants )
1872 variantNames.emplace( name );
1873 }
1874 }
1875
1876 for( const SCH_ITEM* item : Items().OfType( SCH_SHEET_T ) )
1877 {
1878 const SCH_SHEET* sheet = static_cast<const SCH_SHEET*>( item );
1879
1880 wxCHECK2( sheet, continue );
1881
1882 const std::vector<SCH_SHEET_INSTANCE> sheetInstances = sheet->GetInstances();
1883
1884 for( const SCH_SHEET_INSTANCE& instance : sheetInstances )
1885 {
1886 for( const auto& [name, variant] : instance.m_Variants )
1887 variantNames.emplace( name );
1888 }
1889 }
1890
1891 return variantNames;
1892}
1893
1894
1895void SCH_SCREEN::DeleteVariant( const wxString& aVariantName, SCH_COMMIT* aCommit )
1896{
1897 wxCHECK( !aVariantName.IsEmpty(), /* void */ );
1898
1899 for( SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
1900 {
1901 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1902
1903 wxCHECK2( symbol, continue );
1904
1905 std::vector<SCH_SYMBOL_INSTANCE> symbolInstances = symbol->GetInstances();
1906
1907 for( SCH_SYMBOL_INSTANCE& instance : symbolInstances )
1908 {
1909 if( instance.m_Variants.contains( aVariantName ) )
1910 {
1911 if( aCommit )
1912 aCommit->Modify( item, this );
1913
1914 symbol->DeleteVariant( instance.m_Path, aVariantName );
1915 }
1916 }
1917 }
1918
1919 for( SCH_ITEM* item : Items().OfType( SCH_SHEET_T ) )
1920 {
1921 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
1922
1923 wxCHECK2( sheet, continue );
1924
1925 std::vector<SCH_SHEET_INSTANCE> sheetInstances = sheet->GetInstances();
1926
1927 for( SCH_SHEET_INSTANCE& instance : sheetInstances )
1928 {
1929 if( instance.m_Variants.contains( aVariantName ) )
1930 {
1931 if( aCommit )
1932 aCommit->Modify( item, this );
1933
1934 sheet->DeleteVariant( instance.m_Path, aVariantName );
1935 }
1936 }
1937 }
1938}
1939
1940
1941void SCH_SCREEN::RenameVariant( const wxString& aOldName, const wxString& aNewName,
1942 SCH_COMMIT* aCommit )
1943{
1944 wxCHECK( !aOldName.IsEmpty() && !aNewName.IsEmpty(), /* void */ );
1945
1946 for( SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
1947 {
1948 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1949
1950 wxCHECK2( symbol, continue );
1951
1952 std::vector<SCH_SYMBOL_INSTANCE> symbolInstances = symbol->GetInstances();
1953
1954 for( SCH_SYMBOL_INSTANCE& instance : symbolInstances )
1955 {
1956 if( instance.m_Variants.contains( aOldName ) )
1957 {
1958 if( aCommit )
1959 aCommit->Modify( item, this );
1960
1961 symbol->RenameVariant( instance.m_Path, aOldName, aNewName );
1962 }
1963 }
1964 }
1965
1966 for( SCH_ITEM* item : Items().OfType( SCH_SHEET_T ) )
1967 {
1968 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
1969
1970 wxCHECK2( sheet, continue );
1971
1972 std::vector<SCH_SHEET_INSTANCE> sheetInstances = sheet->GetInstances();
1973
1974 for( SCH_SHEET_INSTANCE& instance : sheetInstances )
1975 {
1976 if( instance.m_Variants.contains( aOldName ) )
1977 {
1978 if( aCommit )
1979 aCommit->Modify( item, this );
1980
1981 sheet->RenameVariant( instance.m_Path, aOldName, aNewName );
1982 }
1983 }
1984 }
1985}
1986
1987
1988void SCH_SCREEN::CopyVariant( const wxString& aSourceVariant, const wxString& aNewVariant,
1989 SCH_COMMIT* aCommit )
1990{
1991 wxCHECK( !aSourceVariant.IsEmpty() && !aNewVariant.IsEmpty(), /* void */ );
1992
1993 for( SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
1994 {
1995 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1996
1997 wxCHECK2( symbol, continue );
1998
1999 std::vector<SCH_SYMBOL_INSTANCE> symbolInstances = symbol->GetInstances();
2000
2001 for( SCH_SYMBOL_INSTANCE& instance : symbolInstances )
2002 {
2003 if( instance.m_Variants.contains( aSourceVariant ) )
2004 {
2005 if( aCommit )
2006 aCommit->Modify( item, this );
2007
2008 symbol->CopyVariant( instance.m_Path, aSourceVariant, aNewVariant );
2009 }
2010 }
2011 }
2012
2013 for( SCH_ITEM* item : Items().OfType( SCH_SHEET_T ) )
2014 {
2015 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
2016
2017 wxCHECK2( sheet, continue );
2018
2019 std::vector<SCH_SHEET_INSTANCE> sheetInstances = sheet->GetInstances();
2020
2021 for( SCH_SHEET_INSTANCE& instance : sheetInstances )
2022 {
2023 if( instance.m_Variants.contains( aSourceVariant ) )
2024 {
2025 if( aCommit )
2026 aCommit->Modify( item, this );
2027
2028 sheet->CopyVariant( instance.m_Path, aSourceVariant, aNewVariant );
2029 }
2030 }
2031 }
2032}
2033
2034
2035#if defined(DEBUG)
2036void SCH_SCREEN::Show( int nestLevel, std::ostream& os ) const
2037{
2038 // for now, make it look like XML, expand on this later.
2039 NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() << ">\n";
2040
2041 for( const SCH_ITEM* item : Items() )
2042 item->Show( nestLevel + 1, os );
2043
2044 NestedSpace( nestLevel, os ) << "</" << GetClass().Lower().mb_str() << ">\n";
2045}
2046#endif
2047
2048
2050{
2051 m_index = 0;
2052 buildScreenList( aSheet );
2053}
2054
2055
2059
2060
2062{
2063 m_index = 0;
2064
2065 if( m_screens.size() > 0 )
2066 return m_screens[0];
2067
2068 return nullptr;
2069}
2070
2071
2073{
2074 if( m_index < m_screens.size() )
2075 m_index++;
2076
2077 return GetScreen( m_index );
2078}
2079
2080
2081SCH_SCREEN* SCH_SCREENS::GetScreen( unsigned int aIndex ) const
2082{
2083 if( aIndex < m_screens.size() )
2084 return m_screens[ aIndex ];
2085
2086 return nullptr;
2087}
2088
2089
2090SCH_SHEET* SCH_SCREENS::GetSheet( unsigned int aIndex ) const
2091{
2092 if( aIndex < m_sheets.size() )
2093 return m_sheets[ aIndex ];
2094
2095 return nullptr;
2096}
2097
2098
2100{
2101 if( aScreen == nullptr )
2102 return;
2103
2104 for( const SCH_SCREEN* screen : m_screens )
2105 {
2106 if( screen == aScreen )
2107 return;
2108 }
2109
2110 m_screens.push_back( aScreen );
2111 m_sheets.push_back( aSheet );
2112}
2113
2114
2116{
2117 if( aSheet && aSheet->Type() == SCH_SHEET_T )
2118 {
2119 SCH_SCREEN* screen = aSheet->GetScreen();
2120
2121 if( !screen )
2122 return;
2123
2124 addScreenToList( screen, aSheet );
2125
2126 for( SCH_ITEM* item : screen->Items().OfType( SCH_SHEET_T ) )
2127 buildScreenList( static_cast<SCH_SHEET*>( item ) );
2128 }
2129}
2130
2131
2133{
2134 SCH_SCREEN* first = GetFirst();
2135
2136 if( !first )
2137 return;
2138
2139 SCHEMATIC* sch = first->Schematic();
2140
2141 wxCHECK_RET( sch, "Null schematic in SCH_SCREENS::ClearAnnotationOfNewSheetPaths" );
2142
2143 // Clear the annotation for symbols inside new sheetpaths not already in aInitialSheetList
2144 SCH_SCREENS screensList( sch->Root() ); // The list of screens, shared by sheet paths
2145 screensList.BuildClientSheetPathList(); // build the shared by sheet paths, by screen
2146
2147 // Search for new sheet paths, not existing in aInitialSheetPathList
2148 // and existing in sheetpathList
2149 for( SCH_SHEET_PATH& sheetpath : sch->Hierarchy() )
2150 {
2151 bool path_exists = false;
2152
2153 for( const SCH_SHEET_PATH& existing_sheetpath: aInitialSheetPathList )
2154 {
2155 if( existing_sheetpath.Path() == sheetpath.Path() )
2156 {
2157 path_exists = true;
2158 break;
2159 }
2160 }
2161
2162 if( !path_exists )
2163 {
2164 // A new sheet path is found: clear the annotation corresponding to this new path:
2165 SCH_SCREEN* curr_screen = sheetpath.LastScreen();
2166
2167 // Clear annotation and create the AR for this path, if not exists,
2168 // when the screen is shared by sheet paths.
2169 // Otherwise ClearAnnotation do nothing, because the F1 field is used as
2170 // reference default value and takes the latest displayed value
2171 curr_screen->EnsureAlternateReferencesExist();
2172 curr_screen->ClearAnnotation( &sheetpath, false );
2173 }
2174 }
2175}
2176
2177
2179{
2180 std::vector<SCH_ITEM*> items;
2181 int count = 0;
2182
2183 auto timestamp_cmp = []( const EDA_ITEM* a, const EDA_ITEM* b ) -> bool
2184 {
2185 return a->m_Uuid < b->m_Uuid;
2186 };
2187
2188 std::set<EDA_ITEM*, decltype( timestamp_cmp )> unique_stamps( timestamp_cmp );
2189
2190 // Collect ALL items from all screens to detect duplicate UUIDs.
2191 // This is essential for design blocks where multiple instances of the same content
2192 // are placed on the same sheet - each instance needs unique UUIDs for items like
2193 // wires, junctions, and groups, not just symbols and sheets.
2194 for( SCH_SCREEN* screen : m_screens )
2195 {
2196 for( SCH_ITEM* item : screen->Items() )
2197 items.push_back( item );
2198 }
2199
2200 if( items.size() < 2 )
2201 return 0;
2202
2203 for( EDA_ITEM* item : items )
2204 {
2205 if( !unique_stamps.insert( item ).second )
2206 {
2207 // Reset to fully random UUID. This may lose reference, but better to be
2208 // deterministic about it rather than to have duplicate UUIDs with random
2209 // side-effects.
2210 const_cast<KIID&>( item->m_Uuid ) = KIID();
2211 count++;
2212
2213 // @todo If the item is a sheet, we need to descend the hierarchy from the sheet
2214 // and replace all instances of the changed UUID in sheet paths. Otherwise,
2215 // all instance paths with the sheet's UUID will get clobbered.
2216 }
2217 }
2218
2219 return count;
2220}
2221
2222
2224{
2225 for( SCH_SCREEN* screen = GetFirst(); screen; screen = GetNext() )
2226 {
2227 for( SCH_ITEM* item : screen->Items() )
2228 item->ClearEditFlags();
2229 }
2230}
2231
2232
2234{
2235 for( SCH_SCREEN* screen = GetFirst(); screen; screen = GetNext() )
2236 {
2237 for( SCH_ITEM* item : screen->Items().OfType( SCH_MARKER_T ) )
2238 {
2239 if( item == aMarker )
2240 {
2241 screen->DeleteItem( item );
2242 return;
2243 }
2244 }
2245 }
2246}
2247
2248
2249void SCH_SCREENS::DeleteMarkers( enum MARKER_BASE::MARKER_T aMarkerType, int aErrorCode,
2250 bool aIncludeExclusions )
2251{
2252 for( SCH_SCREEN* screen = GetFirst(); screen; screen = GetNext() )
2253 {
2254 std::vector<SCH_ITEM*> markers;
2255
2256 for( SCH_ITEM* item : screen->Items().OfType( SCH_MARKER_T ) )
2257 {
2258 SCH_MARKER* marker = static_cast<SCH_MARKER*>( item );
2259 std::shared_ptr<RC_ITEM>rcItem = marker->GetRCItem();
2260
2261 if( marker->GetMarkerType() == aMarkerType
2262 && ( aErrorCode == ERCE_UNSPECIFIED || rcItem->GetErrorCode() == aErrorCode )
2263 && ( !marker->IsExcluded() || aIncludeExclusions ) )
2264 {
2265 markers.push_back( item );
2266 }
2267 }
2268
2269 for( SCH_ITEM* marker : markers )
2270 screen->DeleteItem( marker );
2271 }
2272}
2273
2274
2276 bool aIncludeExclusions )
2277{
2278 DeleteMarkers( aMarkerType, ERCE_UNSPECIFIED, aIncludeExclusions );
2279}
2280
2281
2283{
2284 for( SCH_SCREEN* screen = GetFirst(); screen; screen = GetNext() )
2285 screen->UpdateSymbolLinks( aReporter );
2286
2287 SCH_SCREEN* first = GetFirst();
2288
2289 if( !first )
2290 return;
2291
2292 SCHEMATIC* sch = first->Schematic();
2293
2294 wxCHECK_RET( sch, "Null schematic in SCH_SCREENS::UpdateSymbolLinks" );
2295
2296 SCH_SHEET_LIST sheets = sch->Hierarchy();
2297
2298 // All of the library symbols have been replaced with copies so the connection graph
2299 // pointers are stale.
2300 if( sch->ConnectionGraph() )
2301 sch->ConnectionGraph()->Recalculate( sheets, true );
2302}
2303
2304
2306{
2307 bool has_symbols = false;
2308
2309 for( SCH_SCREEN* screen = GetFirst(); screen; screen = GetNext() )
2310 {
2311 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
2312 {
2313 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
2314 has_symbols = true;
2315
2316 if( !symbol->GetLibId().GetLibNickname().empty() )
2317 return false;
2318 }
2319 }
2320
2321 // return true (i.e. has no fully defined symbol) only if at least one symbol is found
2322 return has_symbols ? true : false;
2323}
2324
2325
2326size_t SCH_SCREENS::GetLibNicknames( wxArrayString& aLibNicknames )
2327{
2328 for( SCH_SCREEN* screen = GetFirst(); screen; screen = GetNext() )
2329 {
2330 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
2331 {
2332 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
2333 const UTF8& nickname = symbol->GetLibId().GetLibNickname();
2334
2335 if( !nickname.empty() && ( aLibNicknames.Index( nickname ) == wxNOT_FOUND ) )
2336 aLibNicknames.Add( nickname );
2337 }
2338 }
2339
2340 return aLibNicknames.GetCount();
2341}
2342
2343
2344int SCH_SCREENS::ChangeSymbolLibNickname( const wxString& aFrom, const wxString& aTo )
2345{
2346 SCH_SCREEN* screen;
2347 int cnt = 0;
2348
2349 for( screen = GetFirst(); screen; screen = GetNext() )
2350 {
2351 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
2352 {
2353 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
2354
2355 if( symbol->GetLibId().GetLibNickname().wx_str() != aFrom )
2356 continue;
2357
2358 LIB_ID id = symbol->GetLibId();
2359 id.SetLibNickname( aTo );
2360 symbol->SetLibId( id );
2361 cnt++;
2362 }
2363 }
2364
2365 return cnt;
2366}
2367
2368
2369bool SCH_SCREENS::HasSchematic( const wxString& aSchematicFileName )
2370{
2371 for( const SCH_SCREEN* screen = GetFirst(); screen; screen = GetNext() )
2372 {
2373 if( screen->GetFileName() == aSchematicFileName )
2374 return true;
2375 }
2376
2377 return false;
2378}
2379
2380
2382{
2383 SCH_SCREEN* first = GetFirst();
2384
2385 if( !first )
2386 return;
2387
2388 SCHEMATIC* sch = first->Schematic();
2389
2390 wxCHECK_RET( sch, "Null schematic in SCH_SCREENS::BuildClientSheetPathList" );
2391
2392 // Don't build until we have a hierarchy to work with. This can be called before the hierarchy is built.
2393 if( !sch->HasHierarchy() )
2394 return;
2395
2396 for( SCH_SCREEN* curr_screen = GetFirst(); curr_screen; curr_screen = GetNext() )
2397 curr_screen->GetClientSheetPaths().clear();
2398
2399 for( SCH_SHEET_PATH& sheetpath : sch->Hierarchy() )
2400 {
2401 SCH_SCREEN* used_screen = sheetpath.LastScreen();
2402
2403 // Search for the used_screen in list and add this unique sheet path:
2404 for( SCH_SCREEN* curr_screen = GetFirst(); curr_screen; curr_screen = GetNext() )
2405 {
2406 if( used_screen == curr_screen )
2407 {
2408 curr_screen->GetClientSheetPaths().push_back( sheetpath );
2409 break;
2410 }
2411 }
2412 }
2413}
2414
2415
2417{
2418 for( SCH_SCREEN* screen = GetFirst(); screen; screen = GetNext() )
2419 screen->SetLegacySymbolInstanceData();
2420}
2421
2422
2424{
2425 for( SCH_SCREEN* screen = GetFirst(); screen; screen = GetNext() )
2426 screen->FixLegacyPowerSymbolMismatches();
2427}
2428
2429
2431{
2432 LOCALE_IO toggle;
2433
2434 // V6 schematics may specify model names in Value fields, which we don't do in V7.
2435 // Migrate by adding an equivalent model for these symbols.
2436
2437 for( SCH_ITEM* item : Items().OfType( SCH_SYMBOL_T ) )
2438 {
2439 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
2440 SIM_MODEL::MigrateSimModel<SCH_SYMBOL>( *symbol, &Schematic()->Project() );
2441 }
2442}
2443
2444
2445void SCH_SCREENS::PruneOrphanedSymbolInstances( const wxString& aProjectName,
2446 const SCH_SHEET_LIST& aValidSheetPaths )
2447{
2448 if( aProjectName.IsEmpty() )
2449 return;
2450
2451 for( SCH_SCREEN* screen = GetFirst(); screen; screen = GetNext() )
2452 screen->PruneOrphanedSymbolInstances( aProjectName, aValidSheetPaths );
2453}
2454
2455
2456void SCH_SCREENS::PruneOrphanedSheetInstances( const wxString& aProjectName,
2457 const SCH_SHEET_LIST& aValidSheetPaths )
2458{
2459 if( aProjectName.IsEmpty() )
2460 return;
2461
2462 for( SCH_SCREEN* screen = GetFirst(); screen; screen = GetNext() )
2463 screen->PruneOrphanedSheetInstances( aProjectName, aValidSheetPaths );
2464}
2465
2466
2468{
2469 for( const SCH_SCREEN* screen : m_screens )
2470 {
2471 if( screen->HasSymbolFieldNamesWithWhiteSpace() )
2472 return true;
2473 }
2474
2475 return false;
2476}
2477
2478
2479std::set<wxString> SCH_SCREENS::GetVariantNames() const
2480{
2481 std::set<wxString> variantNames;
2482
2483 for( const SCH_SCREEN* screen : m_screens )
2484 {
2485 for( const wxString& variantName : screen->GetVariantNames() )
2486 variantNames.emplace( variantName );
2487 }
2488
2489 return variantNames;
2490}
2491
2492
2493void SCH_SCREENS::DeleteVariant( const wxString& aVariantName, SCH_COMMIT* aCommit )
2494{
2495 wxCHECK( !aVariantName.IsEmpty(), /* void */ );
2496
2497 for( SCH_SCREEN* screen : m_screens )
2498 screen->DeleteVariant( aVariantName, aCommit );
2499}
2500
2501
2502void SCH_SCREENS::RenameVariant( const wxString& aOldName, const wxString& aNewName,
2503 SCH_COMMIT* aCommit )
2504{
2505 wxCHECK( !aOldName.IsEmpty() && !aNewName.IsEmpty(), /* void */ );
2506
2507 for( SCH_SCREEN* screen : m_screens )
2508 screen->RenameVariant( aOldName, aNewName, aCommit );
2509}
2510
2511
2512void SCH_SCREENS::CopyVariant( const wxString& aSourceVariant, const wxString& aNewVariant,
2513 SCH_COMMIT* aCommit )
2514{
2515 wxCHECK( !aSourceVariant.IsEmpty() && !aNewVariant.IsEmpty(), /* void */ );
2516
2517 for( SCH_SCREEN* screen : m_screens )
2518 screen->CopyVariant( aSourceVariant, aNewVariant, aCommit );
2519}
const char * name
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
int m_virtualPageNumber
An integer based page number used for printing a range of pages.
bool m_Center
Center on screen.
Definition base_screen.h:92
int m_pageCount
The number of BASE_SCREEN objects in this design.
BASE_SCREEN(EDA_ITEM *aParent, KICAD_T aType=SCREEN_T)
void SetContentModified(bool aModified=true)
Definition base_screen.h:55
void InitDataPoints(const VECTOR2I &aPageSizeInternalUnits)
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:554
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:233
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
void Recalculate(const SCH_SHEET_LIST &aSheetList, bool aUnconditional=false, std::function< void(SCH_ITEM *)> *aChangedItemHandler=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Update the connection graph for the given list of sheets.
static void sort_dangling_end_items(std::vector< DANGLING_END_ITEM > &aItemListByType, std::vector< DANGLING_END_ITEM > &aItemListByPos)
Both contain the same information.
Definition sch_item.cpp:980
EDA_ANGLE Normalize90()
Definition eda_angle.h:257
double AsDegrees() const
Definition eda_angle.h:116
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:42
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:135
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:152
const KIID m_Uuid
Definition eda_item.h:531
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
EDA_ITEM * GetParent() const
Definition eda_item.h:110
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition eda_item.h:156
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:89
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:37
bool IsNew() const
Definition eda_item.h:129
std::vector< VECTOR2I > GetPolyPoints() const
Duplicate the polygon outlines into a flat list of VECTOR2I points.
void SetLineStyle(const LINE_STYLE aStyle)
SHAPE_T GetShape() const
Definition eda_shape.h:185
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:240
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:190
void SetLineColor(const COLOR4D &aColor)
Definition eda_shape.h:181
std::vector< VECTOR2I > GetRectCorners() const
void SetArcGeometry(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Set the three controlling points for an arc.
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:89
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:221
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Definition kiid.h:44
A collection of #SYMBOL_LIB objects.
Object used to load, save, search, and otherwise manipulate symbol library files.
LIB_SYMBOL * FindSymbol(const wxString &aName) const
Find LIB_SYMBOL by aName.
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library tables.
std::optional< LIBRARY_TABLE_ROW * > GetRow(const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH) const
Like LIBRARY_MANAGER::GetRow but filtered to the LIBRARY_TABLE_TYPE of this adapter.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
Definition lib_id.cpp:96
wxString GetUniStringLibId() const
Definition lib_id.h:144
UTF8 Format() const
Definition lib_id.cpp:115
const wxString GetUniStringLibItemName() const
Get strings for display messages in dialogs.
Definition lib_id.h:108
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:83
Define a library symbol object.
Definition lib_symbol.h:79
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:148
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
Definition lib_symbol.h:709
wxString GetName() const override
Definition lib_symbol.h:141
EMBEDDED_FILES * GetEmbeddedFiles() override
bool IsGlobalPower() const override
std::unique_ptr< LIB_SYMBOL > Flatten() const
Return a flattened symbol inheritance to the caller.
void SetLibId(const LIB_ID &aLibId)
virtual void SetName(const wxString &aName)
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
bool IsExcluded() const
Definition marker_base.h:89
std::shared_ptr< RC_ITEM > GetRCItem() const
enum MARKER_T GetMarkerType() const
Definition marker_base.h:87
Base plotter engine class.
Definition plotter.h:133
RENDER_SETTINGS * RenderSettings()
Definition plotter.h:164
virtual void SetCurrentLineWidth(int width, void *aData=nullptr)=0
Set the line width for the next drawing.
A small class to help profiling.
Definition profile.h:46
void Show(std::ostream &aStream=std::cerr)
Print the elapsed time (in a suitable unit) to a stream.
Definition profile.h:103
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition profile.h:86
static SYMBOL_LIBRARY_ADAPTER * SymbolLibAdapter(PROJECT *aProject)
Accessor for project symbol library manager adapter.
static LEGACY_SYMBOL_LIBS * LegacySchLibs(PROJECT *aProject)
Returns the list of symbol libraries from a legacy (pre-5.x) design This is only used from the remapp...
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:71
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the end of the list, for objects that support report ordering.
Definition reporter.h:110
Holds all the data relating to one schematic.
Definition schematic.h:90
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
bool HasHierarchy() const
Check if the hierarchy has been built.
Definition schematic.h:124
wxString GetCurrentVariant() const
Return the current variant being edited.
CONNECTION_GRAPH * ConnectionGraph() const
Definition schematic.h:201
SCH_SHEET & Root() const
Definition schematic.h:134
SCH_SHEET_PATH & CurrentSheet() const
Definition schematic.h:189
Class for a wire to bus entry.
A set of SCH_ITEMs (i.e., without duplicates).
Definition sch_group.h:48
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
virtual bool IsEndPoint(const VECTOR2I &aPt) const
Test if aPt is an end point of this schematic object.
Definition sch_item.h:515
virtual bool IsConnectable() const
Definition sch_item.h:524
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition sch_item.h:338
bool IsConnected(const VECTOR2I &aPoint) const
Test the item to see if it is connected to aPoint.
Definition sch_item.cpp:478
virtual std::vector< VECTOR2I > GetConnectionPoints() const
Add all the connection points for this item to aPoints.
Definition sch_item.h:539
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:38
void SetStartPoint(const VECTOR2I &aPosition)
Definition sch_line.h:136
std::vector< VECTOR3I > BuildWireWithHopShape(const SCH_SCREEN *aScreen, double aArcRadius) const
For wires only: build the list of points to draw the shape using segments and 180 deg arcs Points are...
bool IsWire() const
Return true if the line is a wire.
void SetLineColor(const COLOR4D &aColor)
Definition sch_line.cpp:319
LINE_STYLE GetEffectiveLineStyle() const
Definition sch_line.cpp:372
EDA_ANGLE Angle() const
Get the angle between the start and end lines.
Definition sch_line.h:100
void Plot(PLOTTER *aPlotter, bool aBackground, const SCH_PLOT_OPTS &aPlotOpts, int aUnit, int aBodyStyle, const VECTOR2I &aOffset, bool aDimmed) override
Plot the item to aPlotter.
Definition sch_line.cpp:918
VECTOR2I GetEndPoint() const
Definition sch_line.h:144
VECTOR2I GetStartPoint() const
Definition sch_line.h:135
bool IsBus() const
Return true if the line is a bus.
void SetLineStyle(const LINE_STYLE aStyle)
Definition sch_line.cpp:356
bool IsEndPoint(const VECTOR2I &aPoint) const override
Test if aPt is an end point of this schematic object.
Definition sch_line.h:87
COLOR4D GetLineColor() const
Return COLOR4D::UNSPECIFIED if a custom color hasn't been set for this line.
Definition sch_line.cpp:343
void SetEndPoint(const VECTOR2I &aPosition)
Definition sch_line.h:145
SCH_SCREEN * GetNext()
unsigned int m_index
Definition sch_screen.h:892
std::vector< SCH_SHEET * > m_sheets
Definition sch_screen.h:891
SCH_SCREEN * GetScreen(unsigned int aIndex) const
void UpdateSymbolLinks(REPORTER *aReporter=nullptr)
Initialize the LIB_SYMBOL reference for each SCH_SYMBOL found in the full schematic.
void DeleteMarker(SCH_MARKER *aMarker)
Delete a specific marker.
void DeleteMarkers(enum MARKER_BASE::MARKER_T aMarkerTyp, int aErrorCode, bool aIncludeExclusions=true)
Delete all markers of a particular type and error code.
void buildScreenList(SCH_SHEET *aSheet)
void FixLegacyPowerSymbolMismatches()
Fix legacy power symbols that have mismatched value text fields and invisible power pin names.
void CopyVariant(const wxString &aSourceVariant, const wxString &aNewVariant, SCH_COMMIT *aCommit=nullptr)
SCH_SCREEN * GetFirst()
void DeleteAllMarkers(enum MARKER_BASE::MARKER_T aMarkerType, bool aIncludeExclusions)
Delete all electronic rules check markers of aMarkerType from all the screens in the list.
void PruneOrphanedSheetInstances(const wxString &aProjectName, const SCH_SHEET_LIST &aValidSheetPaths)
int ChangeSymbolLibNickname(const wxString &aFrom, const wxString &aTo)
Change all of the symbol library nicknames.
void RenameVariant(const wxString &aOldName, const wxString &aNewName, SCH_COMMIT *aCommit=nullptr)
SCH_SCREENS(SCH_SHEET *aSheet)
void BuildClientSheetPathList()
Build the list of sheet paths sharing a screen for each screen in use.
bool HasSymbolFieldNamesWithWhiteSpace() const
void ClearAnnotationOfNewSheetPaths(SCH_SHEET_LIST &aInitialSheetPathList)
Clear the annotation for the symbols inside new sheetpaths when a complex hierarchy is modified and n...
void PruneOrphanedSymbolInstances(const wxString &aProjectName, const SCH_SHEET_LIST &aValidSheetPaths)
bool HasNoFullyDefinedLibIds()
Test all of the schematic symbols to see if all LIB_ID objects library nickname is not set.
void ClearEditFlags()
SCH_SHEET * GetSheet(unsigned int aIndex) const
int ReplaceDuplicateTimeStamps()
Test all sheet and symbol objects in the schematic for duplicate time stamps and replaces them as nec...
std::set< wxString > GetVariantNames() const
std::vector< SCH_SCREEN * > m_screens
Definition sch_screen.h:890
void DeleteVariant(const wxString &aVariantName, SCH_COMMIT *aCommit=nullptr)
bool HasSchematic(const wxString &aSchematicFileName)
Check if one of the schematics in the list of screens is aSchematicFileName.
size_t GetLibNicknames(wxArrayString &aLibNicknames)
Fetch all of the symbol library nicknames into aLibNicknames.
void SetLegacySymbolInstanceData()
Update the symbol value and footprint instance data for legacy designs.
void addScreenToList(SCH_SCREEN *aScreen, SCH_SHEET *aSheet)
std::set< wxString > GetVariantNames() const
void DeleteVariant(const wxString &aVariantName, SCH_COMMIT *aCommit=nullptr)
std::map< wxString, LIB_SYMBOL * > m_libSymbols
Library symbols required for this schematic.
Definition sch_screen.h:707
SCH_PIN * GetPin(const VECTOR2I &aPosition, SCH_SYMBOL **aSymbol=nullptr, bool aEndPointOnly=false) const
Test the screen for a symbol pin item at aPosition.
bool m_fileExists
Flag to indicate the file associated with this screen has been created.
Definition sch_screen.h:704
void ClearDrawingState()
Clear the state flags of all the items in the screen.
SCH_LINE * GetLine(const VECTOR2I &aPosition, int aAccuracy=0, int aLayer=LAYER_NOTES, SCH_LINE_TEST_T aSearchType=ENTIRE_LENGTH_T) const
Return a line item located at aPosition.
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
void AddLibSymbol(LIB_SYMBOL *aLibSymbol)
Add aLibSymbol to the library symbol map.
bool HasSymbolFieldNamesWithWhiteSpace() const
void AddBusAlias(std::shared_ptr< BUS_ALIAS > aAlias)
Add a bus alias definition.
void FixLegacyPowerSymbolMismatches()
Fix legacy power symbols that have mismatched value text fields and invisible power pin names.
bool HasItems(KICAD_T aItemType) const
void Clear(bool aFree=true)
Delete all draw items and clears the project settings.
bool HasInstanceDataFromOtherProjects() const
Check symbols for instance data from other projects.
void PruneOrphanedSymbolInstances(const wxString &aProjectName, const SCH_SHEET_LIST &aValidSheetPaths)
Remove all invalid symbol instance data in this screen object for the project defined by aProjectName...
std::vector< SCH_SHEET_PATH > & GetClientSheetPaths()
Return the number of times this screen is used.
Definition sch_screen.h:185
void UpdateSymbolLinks(REPORTER *aReporter=nullptr)
Initialize the LIB_SYMBOL reference for each SCH_SYMBOL found in this schematic from the project #SYM...
SCH_LINE * GetWire(const VECTOR2I &aPosition, int aAccuracy=0, SCH_LINE_TEST_T aSearchType=ENTIRE_LENGTH_T) const
Definition sch_screen.h:442
std::set< SCH_ITEM * > MarkConnections(SCH_ITEM *aItem, bool aSecondPass)
Return all wires and junctions connected to aItem which are not connected any symbol pin or all graph...
std::set< wxString > GetSheetNames() const
TITLE_BLOCK m_titles
Definition sch_screen.h:691
void TestDanglingEnds(const SCH_SHEET_PATH *aPath=nullptr, std::function< void(SCH_ITEM *)> *aChangedHandler=nullptr) const
Test all of the connectable objects in the schematic for unused connection points.
void EnsureAlternateReferencesExist()
For screens shared by many sheetpaths (complex hierarchies): to be able to clear or modify any refere...
void PruneOrphanedSheetInstances(const wxString &aProjectName, const SCH_SHEET_LIST &aValidSheetPaths)
Remove all invalid sheet instance data in this screen object for the project defined by aProjectName ...
std::vector< SCH_LINE * > GetBusesAndWires(const VECTOR2I &aPosition, bool aIgnoreEndpoints=false) const
Return buses and wires passing through aPosition.
wxString GroupsSanityCheckInternal(bool repair)
int m_modification_sync
Definition sch_screen.h:695
double m_LastZoomLevel
last value for the zoom level, useful in Eeschema when changing the current displayed sheet to reuse ...
Definition sch_screen.h:673
bool IsExplicitJunction(const VECTOR2I &aPosition) const
Indicate that a junction dot is necessary at the given location.
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:115
int m_fileFormatVersionAtLoad
Definition sch_screen.h:677
void DecRefCount()
SCH_ITEM * GetItem(const VECTOR2I &aPosition, int aAccuracy=0, KICAD_T aType=SCH_LOCATE_ANY_T) const
Check aPosition within a distance of aAccuracy for items of type aFilter.
bool IsExplicitJunctionAllowed(const VECTOR2I &aPosition) const
Indicate that a junction dot may be placed at the given location.
void clearLibSymbols()
wxString m_fileName
Definition sch_screen.h:676
bool IsTerminalPoint(const VECTOR2I &aPosition, int aLayer) const
Test if aPosition is a connection point on aLayer.
void UpdateLocalLibSymbolLinks()
Initialize the LIB_SYMBOL reference for each SCH_SYMBOL found in this schematic with the local projec...
void IncRefCount()
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
static bool ClassOf(const EDA_ITEM *aItem)
void SetLegacySymbolInstanceData()
Update the symbol value and footprint instance data for legacy designs.
SCH_LINE * GetBus(const VECTOR2I &aPosition, int aAccuracy=0, SCH_LINE_TEST_T aSearchType=ENTIRE_LENGTH_T) const
Definition sch_screen.h:448
bool Remove(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Remove aItem from the schematic associated with this screen.
SCH_SCREEN(EDA_ITEM *aParent=nullptr)
SCHEMATIC * Schematic() const
EE_RTREE m_rtree
Definition sch_screen.h:693
void FixupEmbeddedData()
After loading a file from disk, the library symbols do not yet contain the full data for their embedd...
void CopyVariant(const wxString &aSourceVariant, const wxString &aNewVariant, SCH_COMMIT *aCommit=nullptr)
void GetHierarchicalItems(std::vector< SCH_ITEM * > *aItems) const
Add all schematic sheet and symbol objects in the screen to aItems.
bool IsExplicitJunctionNeeded(const VECTOR2I &aPosition) const
Indicate that a junction dot is necessary at the given location, and does not yet exist.
SCH_SHEET_PIN * GetSheetPin(const VECTOR2I &aPosition) const
Test the screen if aPosition is a sheet label object.
wxString GroupsSanityCheck(bool repair=false)
Consistency check of internal m_groups structure.
bool InProjectPath() const
Check if the schematic file is in the current project path.
void RenameVariant(const wxString &aOldName, const wxString &aNewName, SCH_COMMIT *aCommit=nullptr)
void FreeDrawList()
Free all the items from the schematic associated with the screen.
void Plot(PLOTTER *aPlotter, const SCH_PLOT_OPTS &aPlotOpts) const
Plot all the schematic objects to aPlotter.
virtual wxString GetClass() const override
Return the class name.
Definition sch_screen.h:129
void Update(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Update aItem's bounding box in the tree.
void SetConnectivityDirty()
bool m_zoomInitialized
Definition sch_screen.h:698
std::vector< VECTOR2I > GetNeededJunctions(const std::deque< EDA_ITEM * > &aItems) const
Return the unique set of points belonging to aItems where a junction is needed.
PAGE_INFO m_paper
Definition sch_screen.h:690
bool IsJunction(const VECTOR2I &aPosition) const
Test if a junction is required for the items at aPosition on the screen.
bool m_isReadOnly
Read only status of the screen file.
Definition sch_screen.h:701
void GetSheets(std::vector< SCH_ITEM * > *aItems) const
Similar to Items().OfType( SCH_SHEET_T ), but return the sheets in a deterministic order (L-R,...
bool CheckIfOnDrawList(const SCH_ITEM *aItem) const
std::vector< VECTOR2I > GetConnections() const
Collect a unique list of all possible connection points in the schematic.
SPIN_STYLE GetLabelOrientationForPoint(const VECTOR2I &aPosition, SPIN_STYLE aDefaultOrientation, const SCH_SHEET_PATH *aSheet) const
void ClearAnnotation(SCH_SHEET_PATH *aSheetPath, bool aResetPrefix)
Clear the annotation for the symbols in aSheetPath on the screen.
size_t CountConnectedItems(const VECTOR2I &aPos, bool aTestJunctions) const
void MigrateSimModels()
Migrate any symbols having V6 simulation models to their V7 equivalents.
void DeleteItem(SCH_ITEM *aItem)
Remove aItem from the linked list and deletes the object.
size_t getLibSymbolNameMatches(const SCH_SYMBOL &aSymbol, std::vector< wxString > &aMatches)
Return a list of potential library symbol matches for aSymbol.
SCH_LABEL_BASE * GetLabel(const VECTOR2I &aPosition, int aAccuracy=0) const
Return a label item located at aPosition.
void Plot(PLOTTER *aPlotter, bool aBackground, const SCH_PLOT_OPTS &aPlotOpts, int aUnit, int aBodyStyle, const VECTOR2I &aOffset, bool aDimmed) override
Plot the item to aPlotter.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
std::optional< SCH_SHEET_PATH > GetSheetPathByKIIDPath(const KIID_PATH &aPath, bool aIncludeLastSheet=true) const
Finds a SCH_SHEET_PATH that matches the provided KIID_PATH.
bool HasPath(const KIID_PATH &aPath) const
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Define a sheet pin (label) used in sheets to create hierarchical schematics.
SCH_SHEET * GetParent() const
Get the parent sheet object of this sheet pin.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
void CopyVariant(const KIID_PATH &aPath, const wxString &aSourceVariant, const wxString &aNewVariant)
void RemoveInstance(const KIID_PATH &aInstancePath)
wxString GetName() const
Definition sch_sheet.h:136
SCH_SHEET_PIN * GetPin(const VECTOR2I &aPosition)
Return the sheet pin item found at aPosition in the sheet.
void RemovePin(const SCH_SHEET_PIN *aSheetPin)
Remove aSheetPin from the sheet.
void RenameVariant(const KIID_PATH &aPath, const wxString &aOldName, const wxString &aNewName)
void DeleteVariant(const KIID_PATH &aPath, const wxString &aVariantName)
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:139
const std::vector< SCH_SHEET_INSTANCE > & GetInstances() const
Definition sch_sheet.h:505
Schematic symbol object.
Definition sch_symbol.h:69
void SetLibId(const LIB_ID &aName)
SCH_ITEM * GetDrawItem(const VECTOR2I &aPosition, KICAD_T aType=TYPE_NOT_INIT)
Return the symbol library item at aPosition that is part of this symbol.
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition sch_symbol.h:128
void RemoveInstance(const SCH_SHEET_PATH &aInstancePath)
wxString GetSchSymbolLibraryName() const
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
bool AddSheetPathReferenceEntryIfMissing(const KIID_PATH &aSheetPath)
Add an instance to the alternate references list (m_instances), if this entry does not already exist.
void ClearAnnotation(const SCH_SHEET_PATH *aSheetPath, bool aResetPrefix)
Clear exiting symbol annotation.
void RenameVariant(const KIID_PATH &aPath, const wxString &aOldName, const wxString &aNewName)
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
std::vector< SCH_PIN * > GetAllLibPins() const
void AddHierarchicalReference(const KIID_PATH &aPath, const wxString &aRef, int aUnit)
Add a full hierarchical reference to this symbol.
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:158
void SetSchSymbolLibraryName(const wxString &aName)
The name of the symbol in the schematic library symbol list.
Definition sch_symbol.h:173
void SetValueFieldText(const wxString &aValue, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString)
std::vector< SCH_PIN * > GetLibPins() const
Populate a vector with all the pins from the library object that match the current unit and bodyStyle...
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:177
void DeleteVariant(const KIID_PATH &aPath, const wxString &aVariantName)
void SetLibSymbol(LIB_SYMBOL *aLibSymbol)
Set this schematic symbol library symbol reference to aLibSymbol.
VECTOR2I GetPinPhysicalPosition(const SCH_PIN *Pin) const
void CopyVariant(const KIID_PATH &aPath, const wxString &aSourceVariant, const wxString &aNewVariant)
static void MigrateSimModel(T &aSymbol, const PROJECT *aProject)
An interface to the global shared library manager that is schematic-specific and linked to one projec...
std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib) override
Loads or reloads the given library, if it exists.
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
for transforming drawing coordinates for a wxDC device context.
Definition transform.h:42
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:67
bool empty() const
Definition utf8.h:105
wxString wx_str() const
Definition utf8.cpp:41
#define DEFAULT_LINE_WIDTH_MILS
The default wire width in mils. (can be changed in preference menu)
static bool empty(const wxTextEntryBase *aCtrl)
#define _(s)
@ NO_RECURSE
Definition eda_item.h:50
#define IS_DELETED
#define STRUCT_DELETED
flag indication structures to be erased
#define IS_MOVING
Item being moved.
@ SEGMENT
Definition eda_shape.h:46
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:47
@ ERCE_UNSPECIFIED
static const wxChar DanglingProfileMask[]
Flag to enable connectivity profiling.
const wxChar *const traceSchSheetPaths
Flag to enable debug output of schematic symbol sheet path manipulation code.
@ LAYER_WIRE
Definition layer_ids.h:450
@ LAYER_NOTES
Definition layer_ids.h:465
@ LAYER_BUS
Definition layer_ids.h:451
POINT_INFO AnalyzePoint(const EE_RTREE &aItem, const VECTOR2I &aPosition, bool aBreakCrossings)
Check a tree of items for a confluence at a given point and work out what kind of junction it is,...
PAGE_SIZE_TYPE
Definition page_info.h:46
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
Class to handle a set of SCH_ITEMs.
#define PROCESSED
SCH_LINE_TEST_T
Definition sch_screen.h:69
@ ENTIRE_LENGTH_T
Definition sch_screen.h:70
@ EXCLUDE_END_POINTS_T
Definition sch_screen.h:72
@ END_POINTS_ONLY_T
Definition sch_screen.h:71
wxString UnescapeString(const wxString &aSource)
LINE_STYLE
Dashed line types.
The EE_TYPE struct provides a type-specific auto-range iterator to the RTree.
Definition sch_rtree.h:171
SearchIter begin()
Definition sch_rtree.h:210
SearchIter end()
Definition sch_rtree.h:211
std::vector< char > decompressedData
A selection of information about a point in the schematic that might be eligible for turning into a j...
A simple container for sheet instance information.
A simple container for schematic symbol instance information.
SPIN_STYLE GetPinSpinStyle(const SCH_PIN &aPin, const SCH_SYMBOL &aSymbol)
Get the spin style for a pin's label, taking into account the pin's orientation, as well as the given...
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
KIBIS_PIN * pin
VECTOR2I end
wxLogTrace helper definitions.
bool IsPointOnSegment(const VECTOR2I &aSegStart, const VECTOR2I &aSegEnd, const VECTOR2I &aTestPoint)
Test if aTestPoint is on line defined by aSegStart and aSegEnd.
Definition trigo.cpp:85
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:71
@ SCH_GROUP_T
Definition typeinfo.h:170
@ SCH_LINE_T
Definition typeinfo.h:160
@ SCH_SYMBOL_T
Definition typeinfo.h:169
@ SCH_FIELD_T
Definition typeinfo.h:147
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:168
@ SCH_LABEL_T
Definition typeinfo.h:164
@ SCH_SHEET_T
Definition typeinfo.h:172
@ SCH_MARKER_T
Definition typeinfo.h:155
@ SCH_SHAPE_T
Definition typeinfo.h:146
@ SCH_HIER_LABEL_T
Definition typeinfo.h:166
@ SCH_SCREEN_T
Definition typeinfo.h:199
@ SCH_LABEL_LOCATE_ANY_T
Definition typeinfo.h:188
@ SCHEMATIC_T
Definition typeinfo.h:201
@ SCH_SHEET_PIN_T
Definition typeinfo.h:171
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:158
@ SCH_BITMAP_T
Definition typeinfo.h:161
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:165
@ SCH_JUNCTION_T
Definition typeinfo.h:156
@ SCH_PIN_T
Definition typeinfo.h:150
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683