KiCad PCB EDA Suite
Loading...
Searching...
No Matches
fp_lib_differ.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 3
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/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 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 "fp_lib_differ.h"
25
26#include <footprint.h>
27#include <pad.h>
28#include <pcb_field.h>
29#include <zone.h>
30#include <io/io_utils.h>
31#include <io/io_base.h>
33#include <richio.h>
34#include <trace_helpers.h>
35
36#include <wx/log.h>
37
38
39namespace KICAD_DIFF
40{
41
42FP_LIB_DIFFER::FP_LIB_DIFFER( const FOOTPRINT_MAP& aBefore, const FOOTPRINT_MAP& aAfter, const wxString& aPath ) :
43 m_before( aBefore ),
44 m_after( aAfter ),
45 m_path( aPath )
46{
47}
48
49
51
52
53std::pair<std::vector<std::unique_ptr<FOOTPRINT>>, FP_LIB_DIFFER::FOOTPRINT_MAP>
54FP_LIB_DIFFER::LoadLibrary( const wxString& aPrettyPath )
55{
56 std::vector<std::unique_ptr<FOOTPRINT>> owners;
57 FOOTPRINT_MAP map;
58
60 wxArrayString names;
61
62 // Throws on corrupt/unreadable libraries; callers distinguish failures
63 // from a deliberately-empty .pretty directory.
64 io.FootprintEnumerate( names, aPrettyPath, false, nullptr );
65
66 // Per-footprint loads propagate too; previously swallowing them let a
67 // bilaterally-corrupt footprint vanish from the diff entirely.
68 for( const wxString& name : names )
69 {
70 std::unique_ptr<FOOTPRINT> owner = io.FootprintLoad( aPrettyPath, name, false, nullptr );
71
72 if( !owner )
73 {
74 wxLogTrace( traceDiffMerge, wxT( "FP_LIB_DIFFER: '%s' returned null on load" ), name );
75 continue;
76 }
77
78 map[name] = owner.get();
79 owners.push_back( std::move( owner ) );
80 }
81
82 return { std::move( owners ), std::move( map ) };
83}
84
85
87{
89 m_before, m_after, m_path, wxS( "pretty" ), wxS( "FOOTPRINT" ), m_options,
90 []( const FOOTPRINT* aFp )
91 {
92 return aFp->GetBoundingBox( false );
93 },
94 []( const FOOTPRINT* aA, const FOOTPRINT* aB )
95 {
96 return !( *aA == *aB );
97 } );
98
99 for( ITEM_CHANGE& change : result.changes )
100 {
101 if( change.kind != CHANGE_KIND::MODIFIED || !change.refdes )
102 continue;
103
104 auto beforeIt = m_before.find( *change.refdes );
105 auto afterIt = m_after.find( *change.refdes );
106
107 if( beforeIt != m_before.end() && afterIt != m_after.end() )
108 AppendElementChanges( change, DiffFootprintElements( beforeIt->second, afterIt->second ) );
109 }
110
111 return result;
112}
113
114
115std::vector<LIB_ELEMENT> DiffFootprintElements( const FOOTPRINT* aBefore, const FOOTPRINT* aAfter )
116{
117 auto fields = []( const FOOTPRINT* aFootprint )
118 {
119 std::map<wxString, const EDA_ITEM*> out;
120
121 if( aFootprint )
122 {
123 for( const PCB_FIELD* field : aFootprint->GetFields() )
124 out[field->GetName()] = field;
125 }
126
127 return out;
128 };
129
130 auto others = []( const FOOTPRINT* aFootprint )
131 {
132 std::vector<const EDA_ITEM*> out;
133
134 if( aFootprint )
135 {
136 out.insert( out.end(), aFootprint->Pads().begin(), aFootprint->Pads().end() );
137 out.insert( out.end(), aFootprint->GraphicalItems().begin(), aFootprint->GraphicalItems().end() );
138 out.insert( out.end(), aFootprint->Zones().begin(), aFootprint->Zones().end() );
139 }
140
141 return out;
142 };
143
144 std::vector<LIB_ELEMENT> out;
145
146 DiffKeyedElements( out, _( "Field" ), fields( aBefore ), fields( aAfter ) );
148 out, others( aBefore ), others( aAfter ),
149 []( const EDA_ITEM* aItem )
150 {
151 return aItem->GetFriendlyName();
152 },
153 []( const EDA_ITEM* aA, const EDA_ITEM* aB )
154 {
155 // Polygon points and pad primitives are no property. Pads are compared as pads, since
156 // their BOARD_ITEM comparison also wants a parent that two loads never share
157 if( aA->Type() == PCB_PAD_T )
158 return static_cast<const PAD&>( *aA ) == static_cast<const PAD&>( *aB );
159
160 return static_cast<const BOARD_ITEM&>( *aA ) == static_cast<const BOARD_ITEM&>( *aB );
161 } );
162
163 return out;
164}
165
166} // namespace KICAD_DIFF
const char * name
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
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
virtual wxString GetFriendlyName() const
Definition eda_item.cpp:575
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
static std::pair< std::vector< std::unique_ptr< FOOTPRINT > >, FOOTPRINT_MAP > LoadLibrary(const wxString &aPrettyPath)
Load a .pretty directory into a FOOTPRINT_MAP.
const FOOTPRINT_MAP & m_before
FP_LIB_DIFFER(const FOOTPRINT_MAP &aBefore, const FOOTPRINT_MAP &aAfter, const wxString &aPath=wxEmptyString)
DOCUMENT_DIFF Diff() override
Produce a DOCUMENT_DIFF of the inputs the concrete differ was constructed with.
const FOOTPRINT_MAP & m_after
std::map< wxString, const FOOTPRINT * > FOOTPRINT_MAP
Definition pad.h:61
A PCB_IO derivation for saving and loading Pcbnew s-expression formatted files.
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
std::unique_ptr< FOOTPRINT > FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
#define _(s)
const wxChar *const traceDiffMerge
Flag to enable diff/merge engine and renderer debugging output.
void DiffKeyedElements(std::vector< LIB_ELEMENT > &aOut, const wxString &aTypeName, const std::map< wxString, const EDA_ITEM * > &aBefore, const std::map< wxString, const EDA_ITEM * > &aAfter)
Elements that are matched by a key, such as pins by number or fields by name.
std::vector< LIB_ELEMENT > DiffFootprintElements(const FOOTPRINT *aBefore, const FOOTPRINT *aAfter)
Compare the elements of two library footprints.
DOCUMENT_DIFF DiffLibraryByName(const MAP &aBefore, const MAP &aAfter, const wxString &aPath, const wxString &aDocType, const wxString &aTypeName, const KICAD_DIFFER::OPTIONS &aOptions, BBoxFn aBBox, ChangedFn aChanged)
Shared name-keyed library diff used by FP_LIB_DIFFER and SYM_LIB_DIFFER.
void AppendElementChanges(ITEM_CHANGE &aChange, const std::vector< LIB_ELEMENT > &aElements)
Add each element to aChange as a child change, which is how a library item reports its elements.
void DiffUnkeyedElements(std::vector< LIB_ELEMENT > &aOut, const std::vector< const EDA_ITEM * > &aBefore, const std::vector< const EDA_ITEM * > &aAfter, const std::function< wxString(const EDA_ITEM *)> &aTypeName, const std::function< bool(const EDA_ITEM *, const EDA_ITEM *)> &aSameGeometry)
Elements with no key.
The full set of changes between two parsed documents of one type.
One change record on a single item.
std::optional< wxString > refdes
wxString result
Test unit parsing edge cases and error handling.
wxLogTrace helper definitions.
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:79