KiCad PCB EDA Suite
Loading...
Searching...
No Matches
link_imported_footprints.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 <set>
23
24#include <lib_id.h>
25#include <reporter.h>
26#include <schematic.h>
27#include <sch_screen.h>
28#include <sch_sheet_path.h>
29#include <sch_symbol.h>
30
31
32int LinkImportedFootprints( SCHEMATIC& aSchematic, const std::map<wxString, wxString>& aFootprintsByRef,
33 REPORTER& aReporter )
34{
35 int linked = 0;
36
37 // A reused sheet shares its symbols across instances, so one footprint is only safe when every instance agrees
38 std::map<SCH_SYMBOL*, std::set<wxString>> targets;
39
40 for( const SCH_SHEET_PATH& path : aSchematic.Hierarchy() )
41 {
42 SCH_SCREEN* screen = path.LastScreen();
43
44 if( !screen )
45 continue;
46
47 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
48 {
49 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
50
51 const wxString current = symbol->GetField( FIELD_T::FOOTPRINT )->GetText();
52 const wxString ref = symbol->GetRef( &path );
53
54 // an unannotated reference names no single part, and still counts against sharing instances
55 auto footprint = ref.EndsWith( wxS( "?" ) ) ? aFootprintsByRef.end() : aFootprintsByRef.find( ref );
56 wxString target = footprint == aFootprintsByRef.end() ? wxString() : footprint->second;
57
58 if( !current.IsEmpty() )
59 {
60 // A field in the same library as the board's footprint names an item of the import cache. The
61 // cache renames a footprint when two libraries supply different ones of a name, which only the
62 // board knows, so the board's name replaces the one the field was given
63 LIB_ID currentId;
64 LIB_ID targetId;
65
66 // an instance that keeps the field still counts against sharing instances
67 if( target.IsEmpty() || currentId.Parse( current ) >= 0 || targetId.Parse( target ) >= 0
68 || currentId.GetLibNickname() != targetId.GetLibNickname() )
69 {
70 targets[symbol].insert( wxString() );
71 continue;
72 }
73 }
74
75 targets[symbol].insert( target );
76 }
77 }
78
79 for( const auto& [symbol, footprints] : targets )
80 {
81 if( footprints.size() != 1 || footprints.begin()->IsEmpty() )
82 continue;
83
84 if( symbol->GetField( FIELD_T::FOOTPRINT )->GetText() == *footprints.begin() )
85 continue;
86
87 symbol->SetFootprintFieldText( *footprints.begin() );
88 linked++;
89 }
90
91 if( linked > 0 )
92 {
93 aReporter.Report( wxString::Format( _( "Linked %d symbol(s) to the footprint with the same reference.\n" ),
94 linked ),
96 }
97
98 return linked;
99}
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:248
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
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:83
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
Holds all the data relating to one schematic.
Definition schematic.h:149
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:139
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:122
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Schematic symbol object.
Definition sch_symbol.h:75
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
#define _(s)
@ RPT_SEVERITY_INFO
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
std::string path
@ SCH_SYMBOL_T
Definition typeinfo.h:168