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
141bool IsUnannotatedUnitOccupied( const SCH_REFERENCE_LIST& aRefs, const wxString& aRef,
142 const LIB_ID& aLibId, int aUnit )
143{
144 for( size_t i = 0; i < aRefs.GetCount(); ++i )
145 {
146 const SCH_REFERENCE& ref = aRefs[i];
147
148 if( ref.GetUnit() != aUnit )
149 continue;
150
151 if( ref.GetRef() != aRef )
152 continue;
153
154 SCH_SYMBOL* refSym = ref.GetSymbol();
155
156 if( refSym && refSym->GetLibId() == aLibId )
157 return true;
158 }
159
160 return false;
161}
162
163
164std::set<int> GetUnplacedUnitsForSymbol( const SCH_SYMBOL& aSym )
165{
166 SCHEMATIC const* schematic = aSym.Schematic();
167
168 if( !schematic )
169 return {};
170
171 const wxString symRefDes = aSym.GetRef( &schematic->CurrentSheet(), false );
172
173 // Pre-annotation references all share the same "U?" form regardless of which library symbol
174 // they came from, so an unannotated AD8620 and an unannotated OPA1664 would otherwise be
175 // collapsed into one logical part. Match library identity as a tie-breaker when the
176 // reference is still unannotated.
177 const bool refIsUnannotated = !symRefDes.IsEmpty() && symRefDes.Last() == '?';
178 const LIB_ID& symLibId = aSym.GetLibId();
179
180 // Get a list of all references in the schematic
181 SCH_SHEET_LIST hierarchy = schematic->Hierarchy();
182 SCH_REFERENCE_LIST existingRefs;
183 hierarchy.GetSymbols( existingRefs, SYMBOL_FILTER_ALL );
184
185 std::set<int> missingUnits;
186
187 for( int unit = 1; unit <= aSym.GetUnitCount(); ++unit )
188 missingUnits.insert( unit );
189
190 for( const SCH_REFERENCE& ref : existingRefs )
191 {
192 if( symRefDes != ref.GetRef() )
193 continue;
194
195 if( refIsUnannotated && ref.GetSymbol() && ref.GetSymbol()->GetLibId() != symLibId )
196 continue;
197
198 missingUnits.erase( ref.GetUnit() );
199 }
200
201 return missingUnits;
202}
203
204
205std::optional<SCH_REFERENCE> FindSymbolByRefAndUnit( const SCHEMATIC& aSchematic,
206 const wxString& aRef, int aUnit )
207{
209 aSchematic.Hierarchy().GetSymbols( refs, SYMBOL_FILTER_ALL );
210
211 for( const SCH_REFERENCE& ref : refs )
212 {
213 if( ref.GetRef() == aRef && ref.GetUnit() == aUnit )
214 {
215 return ref;
216 }
217 }
218
219 return std::nullopt;
220}
221
222
223std::vector<SCH_SYMBOL*> GetSameSymbolMultiUnitSelection( const SELECTION& aSel )
224{
225 std::vector<SCH_SYMBOL*> result;
226
227 if( aSel.GetSize() < 2 || !aSel.OnlyContains( { SCH_SYMBOL_T } ) )
228 return result;
229
230 wxString rootRef;
231 LIB_ID rootLibId;
232 bool haveRootLibId = false;
233 size_t rootPinCount = 0;
234 bool haveRootPinCount = false;
235
236 // Preserve selection order for cyclical swaps A->B->C
237 std::vector<EDA_ITEM*> itemsInOrder = aSel.GetItemsSortedBySelectionOrder();
238
239 for( EDA_ITEM* it : itemsInOrder )
240 {
241 SCH_SYMBOL* sym = dynamic_cast<SCH_SYMBOL*>( it );
242
243 if( !sym || !sym->GetLibSymbolRef() || sym->GetLibSymbolRef()->GetUnitCount() < 2 )
244 return {};
245
246 const SCH_SHEET_PATH& sheet = sym->Schematic()->CurrentSheet();
247
248 // Get unit-less reference
249 wxString ref = sym->GetRef( &sheet, false );
250
251 if( rootRef.IsEmpty() )
252 rootRef = ref;
253
254 if( ref != rootRef )
255 return {};
256
257 // Make sure the user isn't selecting units that are misreferenced such that
258 // they have U1A and U1B that are actually from different library symbols.
259 const LIB_ID& libId = sym->GetLibId();
260
261 if( !haveRootLibId )
262 {
263 rootLibId = libId;
264 haveRootLibId = true;
265 }
266
267 if( libId != rootLibId )
268 return {};
269
270 // Ensure same pin count across selected units
271 size_t pinCount = sym->GetPins( &sheet ).size();
272
273 if( !haveRootPinCount )
274 {
275 rootPinCount = pinCount;
276 haveRootPinCount = true;
277 }
278
279 if( pinCount != rootPinCount )
280 return {};
281
282 result.push_back( sym );
283 }
284
285 if( result.size() < 2 )
286 return {};
287
288 return result;
289}
290
291
292bool SwapPinGeometry( SCH_PIN* aFirst, SCH_PIN* aSecond )
293{
294 wxCHECK_MSG( aFirst && aSecond, false, "Invalid pins supplied to SwapPinGeometry" );
295
296 // If the schematic pin is still backed by a library definition, swap that library pin; once the
297 // caller sees the true return value it can decide to clone the updated library data into the
298 // schematic cache. Otherwise the schematic already owns a local copy, so swap in place.
299 SCH_PIN* firstPin = aFirst->GetLibPin() ? aFirst->GetLibPin() : aFirst;
300 SCH_PIN* secondPin = aSecond->GetLibPin() ? aSecond->GetLibPin() : aSecond;
301
302 VECTOR2I firstLocal = firstPin->GetLocalPosition();
303 VECTOR2I secondLocal = secondPin->GetLocalPosition();
304 firstPin->SetPosition( secondLocal );
305 secondPin->SetPosition( firstLocal );
306
307 PIN_ORIENTATION firstOrientation = firstPin->GetOrientation();
308 PIN_ORIENTATION secondOrientation = secondPin->GetOrientation();
309 firstPin->SetOrientation( secondOrientation );
310 secondPin->SetOrientation( firstOrientation );
311
312 int firstLength = firstPin->GetLength();
313 int secondLength = secondPin->GetLength();
314 firstPin->SetLength( secondLength );
315 secondPin->SetLength( firstLength );
316
317 const wxString& firstOp = firstPin->GetOperatingPoint();
318 const wxString& secondOp = secondPin->GetOperatingPoint();
319 firstPin->SetOperatingPoint( secondOp );
320 secondPin->SetOperatingPoint( firstOp );
321
322 // Return true if we touched the library-backed copy so callers can refresh the schematic symbol
323 // cache once the swap completes.
324 return ( firstPin != aFirst ) || ( secondPin != aSecond );
325}
326
327
328bool SymbolHasSheetInstances( const SCH_SYMBOL& aSymbol, const wxString& aCurrentProject,
329 std::set<wxString>* aSheetPaths, std::set<wxString>* aProjectNames )
330{
331 std::set<KIID_PATH> uniquePaths;
332 std::set<wxString> sheetPaths;
333 std::set<wxString> otherProjects;
334
335 for( const SCH_SYMBOL_INSTANCE& instance : aSymbol.GetInstances() )
336 {
337 uniquePaths.insert( instance.m_Path );
338
339 if( !instance.m_Path.empty() )
340 sheetPaths.insert( instance.m_Path.AsString() );
341
342 if( !instance.m_ProjectName.IsEmpty() )
343 {
344 if( aCurrentProject.IsEmpty() || !instance.m_ProjectName.IsSameAs( aCurrentProject ) )
345 otherProjects.insert( instance.m_ProjectName );
346 }
347 }
348
349 bool sharedWithinProject = uniquePaths.size() > 1;
350 bool sharedWithOtherProjects = !otherProjects.empty();
351
352 if( aSheetPaths )
353 {
354 if( sharedWithinProject )
355 *aSheetPaths = sheetPaths;
356 else
357 aSheetPaths->clear();
358 }
359
360 if( aProjectNames )
361 {
362 if( sharedWithOtherProjects )
363 *aProjectNames = otherProjects;
364 else
365 aProjectNames->clear();
366 }
367
368 return sharedWithinProject || sharedWithOtherProjects;
369}
370
371
372std::set<wxString> GetSheetNamesFromPaths( const std::set<wxString>& aSheetPaths, const SCHEMATIC& aSchematic )
373{
374 std::set<wxString> friendlyNames;
375
376 if( aSheetPaths.empty() )
377 return friendlyNames;
378
379 SCH_SHEET_LIST hierarchy = aSchematic.Hierarchy();
380
381 for( const wxString& pathStr : aSheetPaths )
382 {
383 wxString display = pathStr;
384
385 try
386 {
387 KIID_PATH kiidPath( pathStr );
388
389 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
390 {
391 if( sheetPath.Path() == kiidPath )
392 {
393 wxString sheetNames;
394
395 for( size_t ii = 0; ii < sheetPath.size(); ++ii )
396 {
397 SCH_SHEET* sheet = sheetPath.at( ii );
398
399 if( !sheet )
400 continue;
401
402 const SCH_FIELD* nameField = sheet->GetField( FIELD_T::SHEET_NAME );
403
404 if( !nameField )
405 continue;
406
407 wxString name = nameField->GetShownText( false );
408
409 if( name.IsEmpty() )
410 continue;
411
412 if( !sheetNames.IsEmpty() )
413 sheetNames << wxS( "/" );
414
415 sheetNames << name;
416 }
417
418 if( sheetNames.IsEmpty() )
419 display = sheetPath.PathHumanReadable( false, true );
420 else
421 display = sheetNames;
422
423 break;
424 }
425 }
426 }
427 catch( ... )
428 {
429 // If the path cannot be parsed, fall back to the raw string.
430 }
431
432 friendlyNames.insert( display );
433 }
434
435 return friendlyNames;
436}
const char * name
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:100
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:112
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:89
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
SCH_SHEET_PATH & CurrentSheet() const
Definition schematic.h:188
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0, const wxString &aVariantName=wxEmptyString) const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:168
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:272
VECTOR2I GetLocalPosition() const
Definition sch_pin.h:253
int GetLength() const
Definition sch_pin.cpp:379
const wxString & GetOperatingPoint() const
Definition sch_pin.h:342
void SetOrientation(PIN_ORIENTATION aOrientation)
Definition sch_pin.h:96
SCH_PIN * GetLibPin() const
Definition sch_pin.h:92
void SetPosition(const VECTOR2I &aPos) override
Definition sch_pin.h:254
void SetLength(int aLength)
Definition sch_pin.h:102
PIN_ORIENTATION GetOrientation() const
Definition sch_pin.cpp:344
void SetOperatingPoint(const wxString &aText)
Definition sch_pin.h:343
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.
SCH_SYMBOL * GetSymbol() const
wxString GetRef() const
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void GetSymbols(SCH_REFERENCE_LIST &aReferences, SYMBOL_FILTER aSymbolFilter, 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:48
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< const 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.
@ SYMBOL_FILTER_ALL
bool IsUnannotatedUnitOccupied(const SCH_REFERENCE_LIST &aRefs, const wxString &aRef, const LIB_ID &aLibId, int aUnit)
Decide whether aUnit of an unannotated multi-unit symbol is already placed.
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
std::vector< std::vector< std::string > > table
wxString result
Test unit parsing edge cases and error handling.
@ SCH_TABLE_T
Definition typeinfo.h:166
@ SCH_TABLECELL_T
Definition typeinfo.h:167
@ SCH_FIELD_T
Definition typeinfo.h:151
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:172
@ SCH_LABEL_T
Definition typeinfo.h:168
@ SCH_HIER_LABEL_T
Definition typeinfo.h:170
@ SCH_SHEET_PIN_T
Definition typeinfo.h:175
@ SCH_TEXT_T
Definition typeinfo.h:152
@ SCH_TEXTBOX_T
Definition typeinfo.h:153
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:169
@ SCH_PIN_T
Definition typeinfo.h:154
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687