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
56 case SCH_FIELD_T:
57 {
58 // Goes via EDA_TEXT
59 const SCH_FIELD& field = static_cast<const SCH_FIELD&>( aItem );
60 return field.GetShownText( true );
61 }
62
63 case SCH_TEXTBOX_T:
64 case SCH_TABLECELL_T:
65 {
66 // Also EDA_TEXT
67 const SCH_TEXTBOX& textbox = static_cast<const SCH_TEXTBOX&>( aItem );
68
69 // Call the correct GetShownText overload with nullptr for settings/path and aDepth=0
70 // This ensures proper variable expansion and escape marker conversion
71 return textbox.GetShownText( nullptr, nullptr, true, 0 );
72 }
73
74 case SCH_PIN_T:
75 {
76 // This is a choice - probably the name makes more sense than the number
77 // (or should it be name/number?)
78 const SCH_PIN& pin = static_cast<const SCH_PIN&>( aItem );
79 return pin.GetShownName();
80 }
81
82 case SCH_TABLE_T:
83 {
84 // A simple tabbed list of the cells seems like a place to start here
85 const SCH_TABLE& table = static_cast<const SCH_TABLE&>( aItem );
86 wxString s;
87
88 for( int row = 0; row < table.GetRowCount(); ++row )
89 {
90 for( int col = 0; col < table.GetColCount(); ++col )
91 {
92 const SCH_TABLECELL* cell = table.GetCell( row, col );
93 s << cell->GetShownText( true );
94
95 if( col < table.GetColCount() - 1 )
96 {
97 s << '\t';
98 }
99 }
100
101 if( row < table.GetRowCount() - 1 )
102 {
103 s << '\n';
104 }
105 }
106 return s;
107 }
108
109 default:
110 break;
111 }
112
113 return wxEmptyString;
114};
115
116
117wxString GetSelectedItemsAsText( const SELECTION& aSel )
118{
119 wxArrayString itemTexts;
120
121 for( EDA_ITEM* item : aSel )
122 {
123 if( item->IsSCH_ITEM() )
124 {
125 const SCH_ITEM& schItem = static_cast<const SCH_ITEM&>( *item );
126 wxString itemText = GetSchItemAsText( schItem );
127
128 itemText.Trim( false ).Trim( true );
129
130 if( !itemText.IsEmpty() )
131 {
132 itemTexts.Add( std::move( itemText ) );
133 }
134 }
135 }
136
137 return wxJoin( itemTexts, '\n', '\0' );
138}
139
140
141std::set<int> GetUnplacedUnitsForSymbol( const SCH_SYMBOL& aSym )
142{
143 SCHEMATIC const* schematic = aSym.Schematic();
144
145 if( !schematic )
146 return {};
147
148 const wxString symRefDes = aSym.GetRef( &schematic->CurrentSheet(), false );
149
150 // Get a list of all references in the schematic
151 SCH_SHEET_LIST hierarchy = schematic->Hierarchy();
152 SCH_REFERENCE_LIST existingRefs;
153 hierarchy.GetSymbols( existingRefs );
154
155 std::set<int> missingUnits;
156
157 for( int unit = 1; unit <= aSym.GetUnitCount(); ++unit )
158 missingUnits.insert( unit );
159
160 for( const SCH_REFERENCE& ref : existingRefs )
161 {
162 if( symRefDes == ref.GetRef() )
163 missingUnits.erase( ref.GetUnit() );
164 }
165
166 return missingUnits;
167}
168
169
170std::optional<SCH_REFERENCE> FindSymbolByRefAndUnit( const SCHEMATIC& aSchematic,
171 const wxString& aRef, int aUnit )
172{
174 aSchematic.Hierarchy().GetSymbols( refs );
175
176 for( const SCH_REFERENCE& ref : refs )
177 {
178 if( ref.GetRef() == aRef && ref.GetUnit() == aUnit )
179 {
180 return ref;
181 }
182 }
183
184 return std::nullopt;
185}
186
187
188std::vector<SCH_SYMBOL*> GetSameSymbolMultiUnitSelection( const SELECTION& aSel )
189{
190 std::vector<SCH_SYMBOL*> result;
191
192 if( aSel.GetSize() < 2 || !aSel.OnlyContains( { SCH_SYMBOL_T } ) )
193 return result;
194
195 wxString rootRef;
196 LIB_ID rootLibId;
197 bool haveRootLibId = false;
198 size_t rootPinCount = 0;
199 bool haveRootPinCount = false;
200
201 // Preserve selection order for cyclical swaps A->B->C
202 std::vector<EDA_ITEM*> itemsInOrder = aSel.GetItemsSortedBySelectionOrder();
203
204 for( EDA_ITEM* it : itemsInOrder )
205 {
206 SCH_SYMBOL* sym = dynamic_cast<SCH_SYMBOL*>( it );
207
208 if( !sym || !sym->GetLibSymbolRef() || sym->GetLibSymbolRef()->GetUnitCount() < 2 )
209 return {};
210
211 const SCH_SHEET_PATH& sheet = sym->Schematic()->CurrentSheet();
212
213 // Get unit-less reference
214 wxString ref = sym->GetRef( &sheet, false );
215
216 if( rootRef.IsEmpty() )
217 rootRef = ref;
218
219 if( ref != rootRef )
220 return {};
221
222 // Make sure the user isn't selecting units that are misreferenced such that
223 // they have U1A and U1B that are actually from different library symbols.
224 const LIB_ID& libId = sym->GetLibId();
225
226 if( !haveRootLibId )
227 {
228 rootLibId = libId;
229 haveRootLibId = true;
230 }
231
232 if( libId != rootLibId )
233 return {};
234
235 // Ensure same pin count across selected units
236 size_t pinCount = sym->GetPins( &sheet ).size();
237
238 if( !haveRootPinCount )
239 {
240 rootPinCount = pinCount;
241 haveRootPinCount = true;
242 }
243
244 if( pinCount != rootPinCount )
245 return {};
246
247 result.push_back( sym );
248 }
249
250 if( result.size() < 2 )
251 return {};
252
253 return result;
254}
255
256
257bool SwapPinGeometry( SCH_PIN* aFirst, SCH_PIN* aSecond )
258{
259 wxCHECK_MSG( aFirst && aSecond, false, "Invalid pins supplied to SwapPinGeometry" );
260
261 // If the schematic pin is still backed by a library definition, swap that library pin; once the
262 // caller sees the true return value it can decide to clone the updated library data into the
263 // schematic cache. Otherwise the schematic already owns a local copy, so swap in place.
264 SCH_PIN* firstPin = aFirst->GetLibPin() ? aFirst->GetLibPin() : aFirst;
265 SCH_PIN* secondPin = aSecond->GetLibPin() ? aSecond->GetLibPin() : aSecond;
266
267 VECTOR2I firstLocal = firstPin->GetLocalPosition();
268 VECTOR2I secondLocal = secondPin->GetLocalPosition();
269 firstPin->SetPosition( secondLocal );
270 secondPin->SetPosition( firstLocal );
271
272 PIN_ORIENTATION firstOrientation = firstPin->GetOrientation();
273 PIN_ORIENTATION secondOrientation = secondPin->GetOrientation();
274 firstPin->SetOrientation( secondOrientation );
275 secondPin->SetOrientation( firstOrientation );
276
277 int firstLength = firstPin->GetLength();
278 int secondLength = secondPin->GetLength();
279 firstPin->SetLength( secondLength );
280 secondPin->SetLength( firstLength );
281
282 const wxString& firstOp = firstPin->GetOperatingPoint();
283 const wxString& secondOp = secondPin->GetOperatingPoint();
284 firstPin->SetOperatingPoint( secondOp );
285 secondPin->SetOperatingPoint( firstOp );
286
287 // Return true if we touched the library-backed copy so callers can refresh the schematic symbol
288 // cache once the swap completes.
289 return ( firstPin != aFirst ) || ( secondPin != aSecond );
290}
291
292
293bool SymbolHasSheetInstances( const SCH_SYMBOL& aSymbol, const wxString& aCurrentProject,
294 std::set<wxString>* aSheetPaths, std::set<wxString>* aProjectNames )
295{
296 std::set<KIID_PATH> uniquePaths;
297 std::set<wxString> sheetPaths;
298 std::set<wxString> otherProjects;
299
300 for( const SCH_SYMBOL_INSTANCE& instance : aSymbol.GetInstances() )
301 {
302 uniquePaths.insert( instance.m_Path );
303
304 if( !instance.m_Path.empty() )
305 sheetPaths.insert( instance.m_Path.AsString() );
306
307 if( !instance.m_ProjectName.IsEmpty() )
308 {
309 if( aCurrentProject.IsEmpty() || !instance.m_ProjectName.IsSameAs( aCurrentProject ) )
310 otherProjects.insert( instance.m_ProjectName );
311 }
312 }
313
314 bool sharedWithinProject = uniquePaths.size() > 1;
315 bool sharedWithOtherProjects = !otherProjects.empty();
316
317 if( aSheetPaths )
318 {
319 if( sharedWithinProject )
320 *aSheetPaths = sheetPaths;
321 else
322 aSheetPaths->clear();
323 }
324
325 if( aProjectNames )
326 {
327 if( sharedWithOtherProjects )
328 *aProjectNames = otherProjects;
329 else
330 aProjectNames->clear();
331 }
332
333 return sharedWithinProject || sharedWithOtherProjects;
334}
335
336
337std::set<wxString> GetSheetNamesFromPaths( const std::set<wxString>& aSheetPaths, const SCHEMATIC& aSchematic )
338{
339 std::set<wxString> friendlyNames;
340
341 if( aSheetPaths.empty() )
342 return friendlyNames;
343
344 SCH_SHEET_LIST hierarchy = aSchematic.Hierarchy();
345
346 for( const wxString& pathStr : aSheetPaths )
347 {
348 wxString display = pathStr;
349
350 try
351 {
352 KIID_PATH kiidPath( pathStr );
353
354 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
355 {
356 if( sheetPath.Path() == kiidPath )
357 {
358 wxString sheetNames;
359
360 for( size_t ii = 0; ii < sheetPath.size(); ++ii )
361 {
362 SCH_SHEET* sheet = sheetPath.at( ii );
363
364 if( !sheet )
365 continue;
366
367 const SCH_FIELD* nameField = sheet->GetField( FIELD_T::SHEET_NAME );
368
369 if( !nameField )
370 continue;
371
372 wxString name = nameField->GetShownText( false );
373
374 if( name.IsEmpty() )
375 continue;
376
377 if( !sheetNames.IsEmpty() )
378 sheetNames << wxS( "/" );
379
380 sheetNames << name;
381 }
382
383 if( sheetNames.IsEmpty() )
384 display = sheetPath.PathHumanReadable( false, true );
385 else
386 display = sheetNames;
387
388 break;
389 }
390 }
391 }
392 catch( ... )
393 {
394 // If the path cannot be parsed, fall back to the raw string.
395 }
396
397 friendlyNames.insert( display );
398 }
399
400 return friendlyNames;
401}
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:247
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