KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_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 The 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 "sch_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#include <schematic.h>
35#include <sch_sheet_path.h>
36#include <sch_sheet.h>
37#include <kiid.h>
38
39#include <wx/arrstr.h>
40
41wxString GetSchItemAsText( const SCH_ITEM& aItem )
42{
43 switch( aItem.Type() )
44 {
45 case SCH_TEXT_T:
46 case SCH_LABEL_T:
50 case SCH_SHEET_PIN_T:
51 {
52 const SCH_TEXT& text = static_cast<const SCH_TEXT&>( aItem );
53 return text.GetShownText( true );
54 }
55 case SCH_FIELD_T:
56 {
57 // Goes via EDA_TEXT
58 const SCH_FIELD& field = static_cast<const SCH_FIELD&>( aItem );
59 return field.GetShownText( true );
60 }
61 case SCH_TEXTBOX_T:
62 case SCH_TABLECELL_T:
63 {
64 // Also EDA_TEXT
65 const SCH_TEXTBOX& textbox = static_cast<const SCH_TEXTBOX&>( aItem );
66
67 // Call the correct GetShownText overload with nullptr for settings/path and aDepth=0
68 // This ensures proper variable expansion and escape marker conversion
69 return textbox.GetShownText( nullptr, nullptr, true, 0 );
70 }
71 case SCH_PIN_T:
72 {
73 // This is a choice - probably the name makes more sense than the number
74 // (or should it be name/number?)
75 const SCH_PIN& pin = static_cast<const SCH_PIN&>( aItem );
76 return pin.GetShownName();
77 }
78 case SCH_TABLE_T:
79 {
80 // A simple tabbed list of the cells seems like a place to start here
81 const SCH_TABLE& table = static_cast<const SCH_TABLE&>( aItem );
82 wxString s;
83
84 for( int row = 0; row < table.GetRowCount(); ++row )
85 {
86 for( int col = 0; col < table.GetColCount(); ++col )
87 {
88 const SCH_TABLECELL* cell = table.GetCell( row, col );
89 s << cell->GetShownText( true );
90
91 if( col < table.GetColCount() - 1 )
92 {
93 s << '\t';
94 }
95 }
96
97 if( row < table.GetRowCount() - 1 )
98 {
99 s << '\n';
100 }
101 }
102 return s;
103 }
104 default:
105 {
106 break;
107 }
108 }
109
110 return wxEmptyString;
111};
112
113
114wxString GetSelectedItemsAsText( const SELECTION& aSel )
115{
116 wxArrayString itemTexts;
117
118 for( EDA_ITEM* item : aSel )
119 {
120 if( item->IsSCH_ITEM() )
121 {
122 const SCH_ITEM& schItem = static_cast<const SCH_ITEM&>( *item );
123 wxString itemText = GetSchItemAsText( schItem );
124
125 itemText.Trim( false ).Trim( true );
126
127 if( !itemText.IsEmpty() )
128 {
129 itemTexts.Add( std::move( itemText ) );
130 }
131 }
132 }
133
134 return wxJoin( itemTexts, '\n', '\0' );
135}
136
137
138std::set<int> GetUnplacedUnitsForSymbol( const SCH_SYMBOL& aSym )
139{
140 SCHEMATIC const* schematic = aSym.Schematic();
141
142 if( !schematic )
143 return {};
144
145 const wxString symRefDes = aSym.GetRef( &schematic->CurrentSheet(), false );
146
147 // Get a list of all references in the schematic
148 SCH_SHEET_LIST hierarchy = schematic->Hierarchy();
149 SCH_REFERENCE_LIST existingRefs;
150 hierarchy.GetSymbols( existingRefs );
151
152 std::set<int> missingUnits;
153
154 for( int unit = 1; unit <= aSym.GetUnitCount(); ++unit )
155 missingUnits.insert( unit );
156
157 for( const SCH_REFERENCE& ref : existingRefs )
158 {
159 if( symRefDes == ref.GetRef() )
160 missingUnits.erase( ref.GetUnit() );
161 }
162
163 return missingUnits;
164}
165
166
167std::optional<SCH_REFERENCE> FindSymbolByRefAndUnit( const SCHEMATIC& aSchematic,
168 const wxString& aRef, int aUnit )
169{
171 aSchematic.Hierarchy().GetSymbols( refs );
172
173 for( const SCH_REFERENCE& ref : refs )
174 {
175 if( ref.GetRef() == aRef && ref.GetUnit() == aUnit )
176 {
177 return ref;
178 }
179 }
180
181 return std::nullopt;
182}
183
184
185std::vector<SCH_SYMBOL*> GetSameSymbolMultiUnitSelection( const SELECTION& aSel )
186{
187 std::vector<SCH_SYMBOL*> result;
188
189 if( aSel.GetSize() < 2 || !aSel.OnlyContains( { SCH_SYMBOL_T } ) )
190 return result;
191
192 wxString rootRef;
193 LIB_ID rootLibId;
194 bool haveRootLibId = false;
195 size_t rootPinCount = 0;
196 bool haveRootPinCount = false;
197
198 // Preserve selection order for cyclical swaps A->B->C
199 std::vector<EDA_ITEM*> itemsInOrder = aSel.GetItemsSortedBySelectionOrder();
200
201 for( EDA_ITEM* it : itemsInOrder )
202 {
203 SCH_SYMBOL* sym = dynamic_cast<SCH_SYMBOL*>( it );
204
205 if( !sym || !sym->GetLibSymbolRef() || sym->GetLibSymbolRef()->GetUnitCount() < 2 )
206 return {};
207
208 const SCH_SHEET_PATH& sheet = sym->Schematic()->CurrentSheet();
209
210 // Get unit-less reference
211 wxString ref = sym->GetRef( &sheet, false );
212
213 if( rootRef.IsEmpty() )
214 rootRef = ref;
215
216 if( ref != rootRef )
217 return {};
218
219 // Make sure the user isn't selecting units that are misreferenced such that
220 // they have U1A and U1B that are actually from different library symbols.
221 const LIB_ID& libId = sym->GetLibId();
222
223 if( !haveRootLibId )
224 {
225 rootLibId = libId;
226 haveRootLibId = true;
227 }
228
229 if( libId != rootLibId )
230 return {};
231
232 // Ensure same pin count across selected units
233 size_t pinCount = sym->GetPins( &sheet ).size();
234
235 if( !haveRootPinCount )
236 {
237 rootPinCount = pinCount;
238 haveRootPinCount = true;
239 }
240
241 if( pinCount != rootPinCount )
242 return {};
243
244 result.push_back( sym );
245 }
246
247 if( result.size() < 2 )
248 return {};
249
250 return result;
251}
252
253
254bool SwapPinGeometry( SCH_PIN* aFirst, SCH_PIN* aSecond )
255{
256 wxCHECK_MSG( aFirst && aSecond, false, "Invalid pins supplied to SwapPinGeometry" );
257
258 // If the schematic pin is still backed by a library definition, swap that library pin; once the
259 // caller sees the true return value it can decide to clone the updated library data into the
260 // schematic cache. Otherwise the schematic already owns a local copy, so swap in place.
261 SCH_PIN* firstPin = aFirst->GetLibPin() ? aFirst->GetLibPin() : aFirst;
262 SCH_PIN* secondPin = aSecond->GetLibPin() ? aSecond->GetLibPin() : aSecond;
263
264 VECTOR2I firstLocal = firstPin->GetLocalPosition();
265 VECTOR2I secondLocal = secondPin->GetLocalPosition();
266 firstPin->SetPosition( secondLocal );
267 secondPin->SetPosition( firstLocal );
268
269 PIN_ORIENTATION firstOrientation = firstPin->GetOrientation();
270 PIN_ORIENTATION secondOrientation = secondPin->GetOrientation();
271 firstPin->SetOrientation( secondOrientation );
272 secondPin->SetOrientation( firstOrientation );
273
274 int firstLength = firstPin->GetLength();
275 int secondLength = secondPin->GetLength();
276 firstPin->SetLength( secondLength );
277 secondPin->SetLength( firstLength );
278
279 const wxString& firstOp = firstPin->GetOperatingPoint();
280 const wxString& secondOp = secondPin->GetOperatingPoint();
281 firstPin->SetOperatingPoint( secondOp );
282 secondPin->SetOperatingPoint( firstOp );
283
284 // Return true if we touched the library-backed copy so callers can refresh the schematic symbol
285 // cache once the swap completes.
286 return ( firstPin != aFirst ) || ( secondPin != aSecond );
287}
288
289
290bool SymbolHasSheetInstances( const SCH_SYMBOL& aSymbol, const wxString& aCurrentProject,
291 std::set<wxString>* aSheetPaths, std::set<wxString>* aProjectNames )
292{
293 std::set<KIID_PATH> uniquePaths;
294 std::set<wxString> sheetPaths;
295 std::set<wxString> otherProjects;
296
297 for( const SCH_SYMBOL_INSTANCE& instance : aSymbol.GetInstances() )
298 {
299 uniquePaths.insert( instance.m_Path );
300
301 if( !instance.m_Path.empty() )
302 sheetPaths.insert( instance.m_Path.AsString() );
303
304 if( !instance.m_ProjectName.IsEmpty() )
305 {
306 if( aCurrentProject.IsEmpty() || !instance.m_ProjectName.IsSameAs( aCurrentProject ) )
307 otherProjects.insert( instance.m_ProjectName );
308 }
309 }
310
311 bool sharedWithinProject = uniquePaths.size() > 1;
312 bool sharedWithOtherProjects = !otherProjects.empty();
313
314 if( aSheetPaths )
315 {
316 if( sharedWithinProject )
317 *aSheetPaths = sheetPaths;
318 else
319 aSheetPaths->clear();
320 }
321
322 if( aProjectNames )
323 {
324 if( sharedWithOtherProjects )
325 *aProjectNames = otherProjects;
326 else
327 aProjectNames->clear();
328 }
329
330 return sharedWithinProject || sharedWithOtherProjects;
331}
332
333
334std::set<wxString> GetSheetNamesFromPaths( const std::set<wxString>& aSheetPaths, const SCHEMATIC& aSchematic )
335{
336 std::set<wxString> friendlyNames;
337
338 if( aSheetPaths.empty() )
339 return friendlyNames;
340
341 SCH_SHEET_LIST hierarchy = aSchematic.Hierarchy();
342
343 for( const wxString& pathStr : aSheetPaths )
344 {
345 wxString display = pathStr;
346
347 try
348 {
349 KIID_PATH kiidPath( pathStr );
350
351 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
352 {
353 if( sheetPath.Path() == kiidPath )
354 {
355 wxString sheetNames;
356
357 for( size_t ii = 0; ii < sheetPath.size(); ++ii )
358 {
359 SCH_SHEET* sheet = sheetPath.at( ii );
360
361 if( !sheet )
362 continue;
363
364 const SCH_FIELD* nameField = sheet->GetField( FIELD_T::SHEET_NAME );
365
366 if( !nameField )
367 continue;
368
369 wxString name = nameField->GetShownText( false );
370
371 if( name.IsEmpty() )
372 continue;
373
374 if( !sheetNames.IsEmpty() )
375 sheetNames << wxS( "/" );
376
377 sheetNames << name;
378 }
379
380 if( sheetNames.IsEmpty() )
381 display = sheetPath.PathHumanReadable( false, true );
382 else
383 display = sheetNames;
384
385 break;
386 }
387 }
388 }
389 catch( ... )
390 {
391 // If the path cannot be parsed, fall back to the raw string.
392 }
393
394 friendlyNames.insert( display );
395 }
396
397 return friendlyNames;
398}
const char * name
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
int GetUnitCount() const override
Holds all the data relating to one schematic.
Definition schematic.h:88
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
SCH_SHEET_PATH & CurrentSheet() const
Definition schematic.h:186
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:167
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:244
VECTOR2I GetLocalPosition() const
Definition sch_pin.h:250
int GetLength() const
Definition sch_pin.cpp:299
const wxString & GetOperatingPoint() const
Definition sch_pin.h:337
void SetOrientation(PIN_ORIENTATION aOrientation)
Definition sch_pin.h:93
SCH_PIN * GetLibPin() const
Definition sch_pin.h:89
void SetPosition(const VECTOR2I &aPos) override
Definition sch_pin.h:251
void SetLength(int aLength)
Definition sch_pin.h:99
PIN_ORIENTATION GetOrientation() const
Definition sch_pin.cpp:264
void SetOperatingPoint(const wxString &aText)
Definition sch_pin.h:338
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.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:47
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this sheet.
Schematic symbol object.
Definition sch_symbol.h:76
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition sch_symbol.h:135
std::vector< SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:165
int GetUnitCount() const override
Return the number of units per package of the symbol.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:184
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
wxString GetShownText(const RENDER_SETTINGS *aSettings, const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const override
virtual wxString GetShownText(const RENDER_SETTINGS *aSettings, const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
virtual unsigned int GetSize() const override
Return the number of stored items.
Definition selection.h:105
std::vector< EDA_ITEM * > GetItemsSortedBySelectionOrder() const
bool OnlyContains(std::vector< KICAD_T > aList) const
Checks if all items in the selection have a type in aList.
PIN_ORIENTATION
The symbol library pin object orientations.
Definition pin_type.h:105
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
std::set< wxString > GetSheetNamesFromPaths(const std::set< wxString > &aSheetPaths, const SCHEMATIC &aSchematic)
Get human-readable sheet names from a set of sheet paths, e.g.
bool SwapPinGeometry(SCH_PIN *aFirst, SCH_PIN *aSecond)
Swap the positions/lengths/etc.
std::set< int > GetUnplacedUnitsForSymbol(const SCH_SYMBOL &aSym)
Get a list of unplaced (i.e.
wxString GetSelectedItemsAsText(const SELECTION &aSel)
bool SymbolHasSheetInstances(const SCH_SYMBOL &aSymbol, const wxString &aCurrentProject, std::set< wxString > *aSheetPaths, std::set< wxString > *aProjectNames)
Returns true when the given symbol has instances, e.g.
std::optional< SCH_REFERENCE > FindSymbolByRefAndUnit(const SCHEMATIC &aSchematic, const wxString &aRef, int aUnit)
Find a symbol by reference and unit.
std::vector< SCH_SYMBOL * > GetSameSymbolMultiUnitSelection(const SELECTION &aSel)
Validates and gathers a selection containing multiple symbol units that all belong to the same refere...
wxString GetSchItemAsText(const SCH_ITEM &aItem)
A simple container for schematic symbol instance information.
KIBIS_PIN * pin
wxString result
Test unit parsing edge cases and error handling.
@ SCH_TABLE_T
Definition typeinfo.h:169
@ SCH_TABLECELL_T
Definition typeinfo.h:170
@ SCH_FIELD_T
Definition typeinfo.h:154
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:175
@ SCH_LABEL_T
Definition typeinfo.h:171
@ SCH_HIER_LABEL_T
Definition typeinfo.h:173
@ SCH_SHEET_PIN_T
Definition typeinfo.h:178
@ SCH_TEXT_T
Definition typeinfo.h:155
@ SCH_TEXTBOX_T
Definition typeinfo.h:156
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:172
@ SCH_PIN_T
Definition typeinfo.h:157
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695