KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sym_item_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
26
27#include <lib_symbol.h>
28#include <richio.h>
30#include <sch_field.h>
31#include <sch_item.h>
32#include <sch_pin.h>
33
34#include <algorithm>
35#include <map>
36#include <set>
37
38
39namespace KICAD_DIFF
40{
41
42namespace
43{
44 wxString graphicTypeName( const EDA_ITEM* aItem )
45 {
46 switch( aItem->Type() )
47 {
48 case SCH_SHAPE_T: return _( "Graphic" );
49 case SCH_TEXT_T: return _( "Text" );
50 case SCH_TEXTBOX_T: return _( "Text Box" );
51 default: return _( "Item" );
52 }
53 }
54
55
56 std::map<wxString, const EDA_ITEM*> pinsByNumber( const LIB_SYMBOL* aSym )
57 {
58 std::map<wxString, const EDA_ITEM*> out;
59
60 if( aSym )
61 {
62 for( SCH_PIN* pin : aSym->GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES ) )
63 {
64 if( pin )
65 out[pin->GetNumber()] = pin;
66 }
67 }
68
69 return out;
70 }
71
72
73 std::map<wxString, const EDA_ITEM*> fieldsByName( const LIB_SYMBOL* aSym )
74 {
75 std::map<wxString, const EDA_ITEM*> out;
76
77 if( aSym )
78 {
79 std::vector<SCH_FIELD*> fields;
80 aSym->GetFields( fields );
81
82 for( SCH_FIELD* field : fields )
83 {
84 if( field )
85 out[field->GetName()] = field;
86 }
87 }
88
89 return out;
90 }
91
92
93 std::vector<const EDA_ITEM*> graphics( const LIB_SYMBOL* aSym )
94 {
95 std::vector<const EDA_ITEM*> out;
96
97 if( aSym )
98 {
99 for( const SCH_ITEM& item : aSym->GetDrawItems() )
100 {
101 if( item.Type() != SCH_PIN_T && item.Type() != SCH_FIELD_T )
102 out.push_back( &item );
103 }
104 }
105
106 return out;
107 }
108} // namespace
109
110
111std::vector<LIB_ELEMENT> DiffSymbolElements( const LIB_SYMBOL* aBefore, const LIB_SYMBOL* aAfter )
112{
113 std::vector<LIB_ELEMENT> out;
114
115 DiffKeyedElements( out, _( "Pin" ), pinsByNumber( aBefore ), pinsByNumber( aAfter ) );
116 DiffKeyedElements( out, _( "Field" ), fieldsByName( aBefore ), fieldsByName( aAfter ) );
117 DiffUnkeyedElements( out, graphics( aBefore ), graphics( aAfter ), graphicTypeName );
118
119 return out;
120}
121
122
123std::unique_ptr<LIB_SYMBOL> CombineSymbolEdits( const LIB_SYMBOL& aAncestor, const LIB_SYMBOL& aOurs,
124 const LIB_SYMBOL& aTheirs, const ITEM_CHANGE& aOurChange,
125 const ITEM_CHANGE& aTheirChange )
126{
127 // Edits are compared on flattened symbols, so an inherited field cannot be copied back safely
128 if( aOurs.IsDerived() || aTheirs.IsDerived() )
129 return nullptr;
130
131 auto onlyFields = []( const ITEM_CHANGE& aChange )
132 {
133 return std::ranges::all_of( aChange.children,
134 []( const ITEM_CHANGE& aChild )
135 {
136 return aChild.typeName == _( "Field" ) && aChild.refdes
137 && aChild.kind != CHANGE_KIND::REMOVED;
138 } );
139 };
140
141 const bool theirsDonates = onlyFields( aTheirChange );
142
143 if( !theirsDonates && !onlyFields( aOurChange ) )
144 return nullptr;
145
146 const std::set<wxString> ourNames = EditedNames( aOurChange );
147 const std::set<wxString> theirNames = EditedNames( aTheirChange );
148
149 // A rename moves the symbol to another key, which a field copy cannot follow
150 if( ourNames.contains( wxS( "Name" ) ) || theirNames.contains( wxS( "Name" ) ) )
151 return nullptr;
152
153 if( std::ranges::any_of( ourNames, [&]( const wxString& aName ) { return theirNames.contains( aName ); } ) )
154 return nullptr;
155
156 const LIB_SYMBOL& donor = theirsDonates ? aTheirs : aOurs;
157 const ITEM_CHANGE& donation = theirsDonates ? aTheirChange : aOurChange;
158
159 auto donate = [&]( const LIB_SYMBOL& aBase ) -> std::unique_ptr<LIB_SYMBOL>
160 {
161 auto target = std::make_unique<LIB_SYMBOL>( aBase );
162
163 for( const ITEM_CHANGE& child : donation.children )
164 {
165 const SCH_FIELD* from = donor.GetField( *child.refdes );
166
167 if( !from )
168 return nullptr;
169
170 if( SCH_FIELD* to = target->GetField( *child.refdes ) )
171 *to = *from;
172 else
173 target->AddField( new SCH_FIELD( *from ) );
174 }
175
176 std::vector<PROPERTY_RESOLUTION> props;
177
178 for( const PROPERTY_DELTA& delta : donation.properties )
179 {
181 prop.name = delta.name;
182 prop.kind = PROP_RES::THEIRS;
183 props.push_back( std::move( prop ) );
184 }
185
186 if( ApplyPropertyResolutions( target.get(), props, nullptr, &donor, nullptr ).failed > 0 )
187 return nullptr;
188
189 return target;
190 };
191
192 // The saved form holds everything a symbol carries, including state no diff reports
193 auto saved = []( const LIB_SYMBOL& aSymbol )
194 {
195 LIB_SYMBOL copy( aSymbol );
196 STRING_FORMATTER formatter;
197
199 return formatter.GetString();
200 };
201
202 // Judge the merge as it will be written, since a symbol can hold state its file drops
203 auto reload = [&]( const LIB_SYMBOL& aSymbol ) -> std::unique_ptr<LIB_SYMBOL>
204 {
205 std::string text = saved( aSymbol );
206
207 try
208 {
209 std::vector<LIB_SYMBOL*> parsed = SCH_IO_KICAD_SEXPR::ParseLibSymbols( text, "combined symbol" );
210 std::vector<std::unique_ptr<LIB_SYMBOL>> owned( parsed.begin(), parsed.end() );
211
212 if( owned.size() == 1 )
213 return std::move( owned.front() );
214
215 return nullptr;
216 }
217 catch( const IO_ERROR& )
218 {
219 return nullptr;
220 }
221 };
222
223 std::unique_ptr<LIB_SYMBOL> replay = donate( aAncestor );
224
225 if( !replay || saved( *replay ) != saved( donor ) )
226 return nullptr;
227
228 std::unique_ptr<LIB_SYMBOL> merged = donate( theirsDonates ? aOurs : aTheirs );
229
230 if( merged )
231 merged = reload( *merged );
232
233 const wxString& name = aAncestor.GetName();
234
235 // A donated property can undo the other side's edit, as a lower unit count drops its pins
236 if( !merged
237 || !SYM_LIB_DIFFER::MergeKeepsBothSides( { { name, &aAncestor } }, { { name, &aOurs } },
238 { { name, &aTheirs } }, { { name, merged.get() } } ) )
239 {
240 return nullptr;
241 }
242
243 return merged;
244}
245
246} // namespace KICAD_DIFF
const char * name
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
static bool MergeKeepsBothSides(const SYMBOL_MAP &aAncestor, const SYMBOL_MAP &aOurs, const SYMBOL_MAP &aTheirs, const SYMBOL_MAP &aMerged)
True when aMerged differs from each side of a three-way merge exactly as the other side differs from ...
Define a library symbol object.
Definition lib_symbol.h:114
void GetFields(std::vector< SCH_FIELD * > &aList, bool aVisibleOnly=false) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
bool IsDerived() const
Definition lib_symbol.h:241
SCH_FIELD * GetField(const wxString &aFieldName)
Find a field within this symbol matching aFieldName; return nullptr if not found.
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
Definition lib_symbol.h:845
wxString GetName() const override
Definition lib_symbol.h:183
std::vector< SCH_PIN * > GetGraphicalPins(int aUnit=0, int aBodyStyle=0) const override
Graphical pins: Return schematic pin objects as drawn (unexpanded), filtered by unit/body.
static void FormatLibSymbol(LIB_SYMBOL *aPart, OUTPUTFORMATTER &aFormatter)
static std::vector< LIB_SYMBOL * > ParseLibSymbols(std::string &aSymbolText, std::string aSource, int aFileVersion=SEXPR_SCHEMATIC_FILE_VERSION)
Implement an OUTPUTFORMATTER to a memory buffer.
Definition richio.h:430
const std::string & GetString()
Definition richio.h:453
#define _(s)
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 > DiffSymbolElements(const LIB_SYMBOL *aBefore, const LIB_SYMBOL *aAfter)
Compares the elements of two library symbols.
PROPERTY_APPLY_COUNTS ApplyPropertyResolutions(INSPECTABLE *aTarget, const std::vector< PROPERTY_RESOLUTION > &aProps, const INSPECTABLE *aOurs, const INSPECTABLE *aTheirs, const INSPECTABLE *aAncestor)
Apply per-property merge resolutions to aTarget, sourcing OURS/THEIRS/ANCESTOR values from the matchi...
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.
std::unique_ptr< LIB_SYMBOL > CombineSymbolEdits(const LIB_SYMBOL &aAncestor, const LIB_SYMBOL &aOurs, const LIB_SYMBOL &aTheirs, const ITEM_CHANGE &aOurChange, const ITEM_CHANGE &aTheirChange)
Combine two edits of one library symbol when neither can lose anything.
std::set< wxString > EditedNames(const ITEM_CHANGE &aChange)
The properties and elements aChange edits, by the names a user would know them by.
One change record on a single item.
std::vector< PROPERTY_DELTA > properties
std::optional< wxString > refdes
std::vector< ITEM_CHANGE > children
Single (name, before, after) triple for one mutated property on an item.
#define ALL_UNITS
Definition symbol.h:150
#define ALL_BODY_STYLES
Definition symbol.h:151
KIBIS_PIN * pin
int delta
@ SCH_FIELD_T
Definition typeinfo.h:146
@ SCH_SHAPE_T
Definition typeinfo.h:145
@ SCH_TEXT_T
Definition typeinfo.h:147
@ SCH_TEXTBOX_T
Definition typeinfo.h:148
@ SCH_PIN_T
Definition typeinfo.h:149