KiCad PCB EDA Suite
Loading...
Searching...
No Matches
ee_tool_utils.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) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include "ee_tool_utils.h"
25
26#include <sch_text.h>
27#include <sch_field.h>
28#include <sch_pin.h>
29#include <sch_reference_list.h>
30#include <sch_symbol.h>
31#include <sch_table.h>
32#include <sch_tablecell.h>
33#include <sch_textbox.h>
34
35#include <wx/arrstr.h>
36
37wxString GetSchItemAsText( const SCH_ITEM& aItem )
38{
39 switch( aItem.Type() )
40 {
41 case SCH_TEXT_T:
42 case SCH_LABEL_T:
46 case SCH_SHEET_PIN_T:
47 {
48 const SCH_TEXT& text = static_cast<const SCH_TEXT&>( aItem );
49 return text.GetShownText( true );
50 }
51 case SCH_FIELD_T:
52 {
53 // Goes via EDA_TEXT
54 const SCH_FIELD& field = static_cast<const SCH_FIELD&>( aItem );
55 return field.GetShownText( true );
56 }
57 case SCH_TEXTBOX_T:
58 case SCH_TABLECELL_T:
59 {
60 // Also EDA_TEXT
61 const SCH_TEXTBOX& textbox = static_cast<const SCH_TEXTBOX&>( aItem );
62 return textbox.GetShownText( true );
63 }
64 case SCH_PIN_T:
65 {
66 // This is a choice - probably the name makes more sense than the number
67 // (or should it be name/number?)
68 const SCH_PIN& pin = static_cast<const SCH_PIN&>( aItem );
69 return pin.GetShownName();
70 }
71 case SCH_TABLE_T:
72 {
73 // A simple tabbed list of the cells seems like a place to start here
74 const SCH_TABLE& table = static_cast<const SCH_TABLE&>( aItem );
75 wxString s;
76
77 for( int row = 0; row < table.GetRowCount(); ++row )
78 {
79 for( int col = 0; col < table.GetColCount(); ++col )
80 {
81 const SCH_TABLECELL* cell = table.GetCell( row, col );
82 s << cell->GetShownText( true );
83
84 if( col < table.GetColCount() - 1 )
85 {
86 s << '\t';
87 }
88 }
89
90 if( row < table.GetRowCount() - 1 )
91 {
92 s << '\n';
93 }
94 }
95 return s;
96 }
97 default:
98 {
99 break;
100 }
101 }
102
103 return wxEmptyString;
104};
105
106
107wxString GetSelectedItemsAsText( const SELECTION& aSel )
108{
109 wxArrayString itemTexts;
110
111 for( EDA_ITEM* item : aSel )
112 {
113 if( item->IsSCH_ITEM() )
114 {
115 const SCH_ITEM& schItem = static_cast<const SCH_ITEM&>( *item );
116 wxString itemText = GetSchItemAsText( schItem );
117
118 itemText.Trim( false ).Trim( true );
119
120 if( !itemText.IsEmpty() )
121 {
122 itemTexts.Add( std::move( itemText ) );
123 }
124 }
125 }
126
127 return wxJoin( itemTexts, '\n', '\0' );
128}
129
130
131std::set<int> GetUnplacedUnitsForSymbol( const SCH_SYMBOL& aSym )
132{
133 SCHEMATIC const* schematic = aSym.Schematic();
134 const wxString symRefDes = aSym.GetRef( &schematic->CurrentSheet(), false );
135
136 if( !schematic )
137 return {};
138
139 SCH_SHEET_LIST hierarchy = schematic->Hierarchy();
140
141 // Get a list of all references in the schematic
142 SCH_REFERENCE_LIST existingRefs;
143 hierarchy.GetSymbols( existingRefs );
144
145 std::set<int> missingUnits;
146 for( int unit = 1; unit <= aSym.GetUnitCount(); ++unit )
147 {
148 missingUnits.insert( unit );
149 }
150
151 for( const SCH_REFERENCE& ref : existingRefs )
152 {
153 if( symRefDes == ref.GetRef() )
154 {
155 missingUnits.erase( ref.GetUnit() );
156 }
157 }
158
159 return missingUnits;
160}
161
162
163std::optional<SCH_REFERENCE> FindSymbolByRefAndUnit( const SCHEMATIC& aSchematic,
164 const wxString& aRef, int aUnit )
165{
167 aSchematic.Hierarchy().GetSymbols( refs );
168
169 for( const SCH_REFERENCE& ref : refs )
170 {
171 if( ref.GetRef() == aRef && ref.GetUnit() == aUnit )
172 {
173 return ref;
174 }
175 }
176
177 return std::nullopt;
178}
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
Holds all the data relating to one schematic.
Definition: schematic.h:77
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:152
SCH_SHEET_LIST Hierarchy() const override
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:214
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition: sch_field.cpp:209
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:150
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
A helper to define a symbol's reference designator in a schematic.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void GetSymbols(SCH_REFERENCE_LIST &aReferences, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanSymbols=false) const
Add a SCH_REFERENCE object to aReferences for each symbol in the list of sheets.
Schematic symbol object.
Definition: sch_symbol.h:104
int GetUnitCount() const override
Return the number of units per package of the symbol.
Definition: sch_symbol.cpp:468
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:737
int GetColCount() const
Definition: sch_table.h:104
SCH_TABLECELL * GetCell(int aRow, int aCol) const
Definition: sch_table.h:131
int GetRowCount() const
Definition: sch_table.h:106
virtual wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
std::set< int > GetUnplacedUnitsForSymbol(const SCH_SYMBOL &aSym)
Get a list of unplaced (i.e.
wxString GetSelectedItemsAsText(const SELECTION &aSel)
std::optional< SCH_REFERENCE > FindSymbolByRefAndUnit(const SCHEMATIC &aSchematic, const wxString &aRef, int aUnit)
Find a symbol by reference and unit.
wxString GetSchItemAsText(const SCH_ITEM &aItem)
@ SCH_TABLE_T
Definition: typeinfo.h:165
@ SCH_TABLECELL_T
Definition: typeinfo.h:166
@ SCH_FIELD_T
Definition: typeinfo.h:150
@ SCH_DIRECTIVE_LABEL_T
Definition: typeinfo.h:171
@ SCH_LABEL_T
Definition: typeinfo.h:167
@ SCH_HIER_LABEL_T
Definition: typeinfo.h:169
@ SCH_SHEET_PIN_T
Definition: typeinfo.h:173
@ SCH_TEXT_T
Definition: typeinfo.h:151
@ SCH_TEXTBOX_T
Definition: typeinfo.h:152
@ SCH_GLOBAL_LABEL_T
Definition: typeinfo.h:168
@ SCH_PIN_T
Definition: typeinfo.h:153