KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_element_diff.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 */
20
23
24#include <eda_item.h>
25
26#include <set>
27
28
29namespace KICAD_DIFF
30{
31
32void DiffKeyedElements( std::vector<LIB_ELEMENT>& aOut, const wxString& aTypeName,
33 const std::map<wxString, const EDA_ITEM*>& aBefore,
34 const std::map<wxString, const EDA_ITEM*>& aAfter )
35{
36 std::set<wxString> keys;
37
38 for( const auto& [key, item] : aBefore )
39 keys.insert( key );
40
41 for( const auto& [key, item] : aAfter )
42 keys.insert( key );
43
44 for( const wxString& key : keys )
45 {
46 auto beforeIt = aBefore.find( key );
47 auto afterIt = aAfter.find( key );
48 const EDA_ITEM* before = beforeIt != aBefore.end() ? beforeIt->second : nullptr;
49 const EDA_ITEM* after = afterIt != aAfter.end() ? afterIt->second : nullptr;
50
51 if( before && after )
52 {
53 std::vector<PROPERTY_DELTA> deltas = DiffItemProperties( before, after );
54
55 if( !deltas.empty() )
56 aOut.push_back( { after, aTypeName, key, CHANGE_KIND::MODIFIED, std::move( deltas ) } );
57 }
58 else if( before )
59 {
60 aOut.push_back( { before, aTypeName, key, CHANGE_KIND::REMOVED, ItemProperties( before, false ) } );
61 }
62 else
63 {
64 aOut.push_back( { after, aTypeName, key, CHANGE_KIND::ADDED, ItemProperties( after, true ) } );
65 }
66 }
67}
68
69
70void DiffUnkeyedElements( std::vector<LIB_ELEMENT>& aOut, const std::vector<const EDA_ITEM*>& aBefore,
71 const std::vector<const EDA_ITEM*>& aAfter,
72 const std::function<wxString( const EDA_ITEM* )>& aTypeName,
73 const std::function<bool( const EDA_ITEM*, const EDA_ITEM* )>& aSameGeometry )
74{
75 auto sameGeometry = [&]( const EDA_ITEM* aBeforeItem, const EDA_ITEM* aAfterItem )
76 {
77 return !aSameGeometry || aSameGeometry( aBeforeItem, aAfterItem );
78 };
79
80 std::set<const EDA_ITEM*> matchedBefore;
81 std::set<const EDA_ITEM*> matchedAfter;
82
83 // Consume identical elements first so unchanged geometry never shows up
84 for( const EDA_ITEM* after : aAfter )
85 {
86 for( const EDA_ITEM* before : aBefore )
87 {
88 if( matchedBefore.count( before ) || before->Type() != after->Type() )
89 continue;
90
91 if( DiffItemProperties( before, after ).empty() && sameGeometry( before, after ) )
92 {
93 matchedBefore.insert( before );
94 matchedAfter.insert( after );
95 break;
96 }
97 }
98 }
99
100 // Pair each leftover with the nearest leftover of its type, else it is new
101 for( const EDA_ITEM* after : aAfter )
102 {
103 if( matchedAfter.count( after ) )
104 continue;
105
106 const EDA_ITEM* best = nullptr;
107 double bestDist = 0.0;
108
109 for( const EDA_ITEM* before : aBefore )
110 {
111 if( matchedBefore.count( before ) || before->Type() != after->Type() )
112 continue;
113
114 VECTOR2I delta = before->GetPosition() - after->GetPosition();
115 double dist = (double) delta.x * delta.x + (double) delta.y * delta.y;
116
117 if( !best || dist < bestDist )
118 {
119 best = before;
120 bestDist = dist;
121 }
122 }
123
124 if( best )
125 {
126 matchedBefore.insert( best );
127
128 std::vector<PROPERTY_DELTA> deltas = DiffItemProperties( best, after );
129
130 if( !deltas.empty() || !sameGeometry( best, after ) )
131 {
132 aOut.push_back(
133 { after, aTypeName( after ), wxEmptyString, CHANGE_KIND::MODIFIED, std::move( deltas ) } );
134 }
135 }
136 else
137 {
138 aOut.push_back(
139 { after, aTypeName( after ), wxEmptyString, CHANGE_KIND::ADDED, ItemProperties( after, true ) } );
140 }
141 }
142
143 for( const EDA_ITEM* before : aBefore )
144 {
145 if( !matchedBefore.count( before ) )
146 {
147 aOut.push_back( { before, aTypeName( before ), wxEmptyString, CHANGE_KIND::REMOVED,
148 ItemProperties( before, false ) } );
149 }
150 }
151}
152
153void AppendElementChanges( ITEM_CHANGE& aChange, const std::vector<LIB_ELEMENT>& aElements )
154{
155 for( const LIB_ELEMENT& element : aElements )
156 {
157 ITEM_CHANGE child;
158 child.typeName = element.typeName;
159 child.kind = element.kind;
160 child.properties = element.deltas;
161
162 if( !element.key.IsEmpty() )
163 child.refdes = element.key;
164
165 aChange.children.push_back( std::move( child ) );
166 }
167}
168
169} // namespace KICAD_DIFF
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
static bool empty(const wxTextEntryBase *aCtrl)
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< PROPERTY_DELTA > DiffItemProperties(const INSPECTABLE *aBefore, const INSPECTABLE *aAfter)
Enumerate the property deltas between two items of the same dynamic type.
std::vector< PROPERTY_DELTA > ItemProperties(const INSPECTABLE *aItem, bool aAsAfter)
List one item's properties as one-sided deltas for an added or removed item.
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.
One change record on a single item.
std::vector< PROPERTY_DELTA > properties
std::optional< wxString > refdes
std::vector< ITEM_CHANGE > children
One changed element of a library item, such as a pin, pad, field or graphic.
int delta
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708