KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue21401_paste_pin_numbers.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 * https://www.gnu.org/licenses/gpl-3.0.en.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
37
39
40#include <memory>
41#include <set>
42
43#include <lib_symbol.h>
44#include <sch_pin.h>
45#include <sch_symbol.h>
46#include <sch_screen.h>
48
49
50namespace
51{
52
53LIB_SYMBOL* MakeTestLibSymbol( const wxString& aName, const wxString& aPin1Num,
54 const wxString& aPin2Num )
55{
56 LIB_SYMBOL* sym = new LIB_SYMBOL( aName, nullptr );
57 sym->SetLibId( LIB_ID( "testlib", aName ) );
58
59 SCH_PIN* pin1 = new SCH_PIN( sym );
60 pin1->SetNumber( aPin1Num );
61 pin1->SetName( "pin_a" );
63 pin1->SetPosition( VECTOR2I( 0, 0 ) );
64 sym->AddDrawItem( pin1 );
65
66 SCH_PIN* pin2 = new SCH_PIN( sym );
67 pin2->SetNumber( aPin2Num );
68 pin2->SetName( "pin_b" );
70 pin2->SetPosition( VECTOR2I( 0, schIUScale.MilsToIU( 100 ) ) );
71 sym->AddDrawItem( pin2 );
72
73 return sym;
74}
75
76
77std::set<wxString> PinNumbers( SCH_SYMBOL& aSymbol, const SCH_SHEET_PATH& aPath )
78{
79 std::set<wxString> nums;
80
81 for( SCH_PIN* pin : aSymbol.GetPins( &aPath ) )
82 nums.insert( pin->GetNumber() );
83
84 return nums;
85}
86
87} // anonymous namespace
88
89
96BOOST_AUTO_TEST_CASE( Issue21401_WrongLibSymbolRevertsPinNumbers )
97{
98 std::unique_ptr<LIB_SYMBOL> modifiedLib( MakeTestLibSymbol( "TestPart", "A1", "A2" ) );
99
101 SCH_SYMBOL symbol( *modifiedLib, modifiedLib->GetLibId(), &path, 0, 0, VECTOR2I( 0, 0 ) );
102 symbol.UpdatePins();
103
104 BOOST_REQUIRE_EQUAL( symbol.GetPins( &path ).size(), 2 );
105 BOOST_CHECK( PinNumbers( symbol, path ) == ( std::set<wxString>{ "A1", "A2" } ) );
106
107 // Applying the original (unmodified) definition reverts the numbering.
108 symbol.SetLibSymbol( MakeTestLibSymbol( "TestPart", "1", "2" ) );
109
110 BOOST_CHECK( PinNumbers( symbol, path ) == ( std::set<wxString>{ "1", "2" } ) );
111}
112
113
118BOOST_AUTO_TEST_CASE( Issue21401_PastePrefersClipboardLibSymbol )
119{
120 SCH_SCREEN destScreen;
121 destScreen.AddLibSymbol( MakeTestLibSymbol( "TestPart", "1", "2" ) );
122
123 SCH_SCREEN clipScreen;
124 clipScreen.AddLibSymbol( MakeTestLibSymbol( "TestPart", "A1", "A2" ) );
125
126 std::unique_ptr<LIB_SYMBOL> clipLib( MakeTestLibSymbol( "TestPart", "A1", "A2" ) );
127
129 SCH_SYMBOL symbol( *clipLib, clipLib->GetLibId(), &path, 0, 0, VECTOR2I( 0, 0 ) );
130 symbol.UpdatePins();
131
133 &clipScreen, &destScreen, symbol.GetSchSymbolLibraryName() );
134
135 BOOST_REQUIRE( source );
136
137 symbol.SetLibSymbol( new LIB_SYMBOL( *source ) );
138
139 BOOST_CHECK_MESSAGE( PinNumbers( symbol, path ) == ( std::set<wxString>{ "A1", "A2" } ),
140 "Paste must keep the clipboard's modified pin numbers" );
141}
142
143
148BOOST_AUTO_TEST_CASE( Issue22162_PastePrefersClipboardPowerType )
149{
150 LIB_SYMBOL* destPower = MakeTestLibSymbol( "PWR", "1", "1" );
151 destPower->SetGlobalPower();
152
153 SCH_SCREEN destScreen;
154 destScreen.AddLibSymbol( destPower );
155
156 LIB_SYMBOL* clipPower = MakeTestLibSymbol( "PWR", "1", "1" );
157 clipPower->SetLocalPower();
158
159 SCH_SCREEN clipScreen;
160 clipScreen.AddLibSymbol( clipPower );
161
162 const wxString lookupName = clipPower->GetLibId().Format().wx_str();
163 const LIB_SYMBOL* source = SCH_EDITOR_CONTROL::ChoosePasteLibSymbol( &clipScreen, &destScreen,
164 lookupName );
165
166 BOOST_REQUIRE( source );
167 BOOST_CHECK_MESSAGE( source->IsLocalPower(),
168 "Paste must keep the clipboard symbol's local power type" );
169}
170
171
176BOOST_AUTO_TEST_CASE( Issue21401_FallsBackToDestinationWhenClipboardEmpty )
177{
178 SCH_SCREEN destScreen;
179 LIB_SYMBOL* destLib = MakeTestLibSymbol( "TestPart", "1", "2" );
180 destScreen.AddLibSymbol( destLib );
181
182 SCH_SCREEN clipScreen; // no cached symbol
183
184 const wxString lookupName = destLib->GetLibId().Format().wx_str();
185 const LIB_SYMBOL* source = SCH_EDITOR_CONTROL::ChoosePasteLibSymbol( &clipScreen, &destScreen,
186 lookupName );
187
188 BOOST_CHECK_EQUAL( source, destLib );
189}
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
UTF8 Format() const
Definition lib_id.cpp:132
Define a library symbol object.
Definition lib_symbol.h:114
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:183
void SetGlobalPower()
bool IsLocalPower() const override
void SetLocalPower()
void SetLibId(const LIB_ID &aLibId)
void AddDrawItem(SCH_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
static const LIB_SYMBOL * ChoosePasteLibSymbol(const SCH_SCREEN *aClipboardScreen, const SCH_SCREEN *aDestScreen, const wxString &aLibSymbolName)
Choose which cached library symbol a pasted instance should adopt.
void SetNumber(const wxString &aNumber)
Definition sch_pin.cpp:810
void SetName(const wxString &aName)
Definition sch_pin.cpp:517
void SetPosition(const VECTOR2I &aPos) override
Definition sch_pin.h:300
void SetType(ELECTRICAL_PINTYPE aType)
Definition sch_pin.cpp:427
void AddLibSymbol(LIB_SYMBOL *aLibSymbol)
Add aLibSymbol to the library symbol map.
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:69
wxString GetSchSymbolLibraryName() const
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
void UpdatePins()
Updates the cache of SCH_PIN objects for each pin.
void SetLibSymbol(LIB_SYMBOL *aLibSymbol)
Set this schematic symbol library symbol reference to aLibSymbol.
wxString wx_str() const
Definition utf8.cpp:41
@ PT_INPUT
usual pin input: must be connected
Definition pin_type.h:33
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_CASE(Issue21401_WrongLibSymbolRevertsPinNumbers)
The core bug mechanism: SetLibSymbol()/UpdatePins() drives pin numbers from the chosen lib symbol.
std::string path
KIBIS_PIN * pin1
KIBIS_PIN * pin
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683