KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_footprint_field_reconciler.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, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <lib_id.h>
23#include <reporter.h>
24#include <schematic.h>
25#include <sch_screen.h>
26#include <sch_symbol.h>
27#include <string_utils.h>
28
29
31 const wxString& aCacheNickname, const std::vector<wxString>& aSourceLibNicknames,
32 REPORTER* aReporter ) :
33 m_cacheNickname( aCacheNickname ),
34 m_sourceLibs( aSourceLibNicknames.begin(), aSourceLibNicknames.end() ),
35 m_reporter( aReporter )
36{
37}
38
39
41{
43
44 // no cache nickname = no project lib to point fields at
45 if( m_cacheNickname.IsEmpty() )
46 return result;
47
48 SCH_SCREENS screens( aSchematic.Root() );
49
50 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
51 {
52 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
53 {
54 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
55 const wxString& fpText = symbol->GetField( FIELD_T::FOOTPRINT )->GetText();
56
57 if( fpText.IsEmpty() )
58 continue;
59
60 LIB_ID fpid;
61
62 if( fpid.Parse( fpText ) >= 0 )
63 continue; // malformed FPID, leave untouched
64
65 if( fpid.GetUniStringLibItemName().IsEmpty() )
66 continue;
67
68 wxString nick = fpid.GetUniStringLibNickname();
69
70 // field already on a registered source lib resolves there
71 if( !nick.IsEmpty() && m_sourceLibs.count( nick ) )
72 {
73 result.m_keptSource++;
74 continue;
75 }
76
77 // the cache names items by their legal file name
78 wxString itemName = fpid.GetUniStringLibItemName();
79 ReplaceIllegalFileNameChars( itemName, '_' );
80
82 fpid.SetLibItemName( itemName );
83 symbol->SetFootprintFieldText( fpid.Format().wx_str() );
84 result.m_relinkedToCache++;
85 }
86 }
87
88 if( m_reporter && result.m_relinkedToCache > 0 )
89 {
90 m_reporter->Report(
91 wxString::Format( _( "Re-linked %d imported footprint assignment(s) to library "
92 "'%s'." ),
93 result.m_relinkedToCache, m_cacheNickname ),
95 }
96
97 return result;
98}
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition lib_id.cpp:124
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
Definition lib_id.cpp:113
UTF8 Format() const
Definition lib_id.cpp:132
const wxString GetUniStringLibItemName() const
Get strings for display messages in dialogs.
Definition lib_id.h:108
const wxString GetUniStringLibNickname() const
Definition lib_id.h:84
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
Holds all the data relating to one schematic.
Definition schematic.h:149
SCH_SHEET & Root() const
Definition schematic.h:208
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:139
SCH_FOOTPRINT_FIELD_RECONCILER(const wxString &aCacheNickname, const std::vector< wxString > &aSourceLibNicknames, REPORTER *aReporter=nullptr)
SCH_FP_FIELD_RECONCILE_RESULT Reconcile(SCHEMATIC &aSchematic)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:826
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
Schematic symbol object.
Definition sch_symbol.h:75
void SetFootprintFieldText(const wxString &aFootprint)
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
wxString wx_str() const
Definition utf8.cpp:41
#define _(s)
@ RPT_SEVERITY_INFO
bool ReplaceIllegalFileNameChars(std::string &aName, int aReplaceChar)
Checks aName for illegal file name characters.
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
VECTOR2I end
wxString result
Test unit parsing edge cases and error handling.
@ SCH_SYMBOL_T
Definition typeinfo.h:168