KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_pin_map_instance_override.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21
22#include <memory>
23
24#include <pin_map.h>
25#include <lib_symbol.h>
26#include <sch_pin.h>
27#include <sch_symbol.h>
28#include <sch_screen.h>
29#include <sch_sheet.h>
30#include <sch_sheet_path.h>
31#include <schematic.h>
34#include <locale_io.h>
35
36#include <wx/file.h>
37#include <wx/filename.h>
38#include <wx/stdpaths.h>
39
40
42{
44 {
45 wxString tempDir = wxStandardPaths::Get().GetTempDir();
46 wxString projectPath = tempDir + wxFileName::GetPathSeparator()
47 + wxT( "test_pinmap_ovr.kicad_pro" );
48 m_tempFiles.push_back( projectPath );
49
50 m_settingsManager.LoadProject( projectPath.ToStdString() );
51 m_schematic = std::make_unique<SCHEMATIC>( nullptr );
52 m_schematic->SetProject( &m_settingsManager.Prj() );
53 }
54
56 {
57 for( const wxString& file : m_tempFiles )
58 {
59 if( wxFileExists( file ) )
60 wxRemoveFile( file );
61 }
62
63 m_schematic.reset();
64 }
65
66 wxString tempFile()
67 {
68 wxString dir = wxStandardPaths::Get().GetTempDir();
69 wxString name = wxFileName::CreateTempFileName( dir + wxFileName::GetPathSeparator()
70 + "pin_map_override" );
71 name += ".kicad_sch";
72 m_tempFiles.push_back( name );
73 return name;
74 }
75
78 {
79 m_schematic->CreateDefaultScreens();
80
81 SCH_SHEET* sheet = m_schematic->GetTopLevelSheets()[0];
82 SCH_SCREEN* screen = sheet->GetScreen();
83 screen->SetFileName( "pin_map_override.kicad_sch" );
84
86 path.push_back( sheet );
87
88 m_libSym = std::make_unique<LIB_SYMBOL>( "LM358", nullptr );
89
90 SCH_PIN* pin1 = new SCH_PIN( m_libSym.get() );
91 pin1->SetNumber( "1" );
93 pin1->SetPosition( VECTOR2I( -508000, 0 ) );
94 m_libSym->AddDrawItem( pin1 );
95
96 SCH_SYMBOL* sym = new SCH_SYMBOL( *m_libSym, m_libSym->GetLibId(), &path, 0, 0,
97 VECTOR2I( 15621000, 6223000 ) );
98 sym->UpdatePins();
99
100 if( !aOverride.IsDefault() )
101 sym->SetPinMapOverride( aOverride );
102
103 screen->Append( sym );
104
105 return sheet;
106 }
107
108 SCH_SYMBOL* reload( const wxString& aFileName )
109 {
111
112 m_schematic->Reset();
113 SCH_SHEET* defaultSheet = m_schematic->GetTopLevelSheet( 0 );
114 SCH_SHEET* loaded = io.LoadSchematicFile( aFileName, m_schematic.get() );
115 m_schematic->AddTopLevelSheet( loaded );
116 m_schematic->RemoveTopLevelSheet( defaultSheet );
117 delete defaultSheet;
118
119 SCH_SYMBOL* found = nullptr;
120
121 for( SCH_ITEM* item : loaded->GetScreen()->Items().OfType( SCH_SYMBOL_T ) )
122 found = static_cast<SCH_SYMBOL*>( item );
123
124 return found;
125 }
126
128 std::unique_ptr<SCHEMATIC> m_schematic;
129 std::unique_ptr<LIB_SYMBOL> m_libSym;
130 std::vector<wxString> m_tempFiles;
131};
132
133
134BOOST_FIXTURE_TEST_SUITE( PinMapInstanceOverride, PIN_MAP_OVERRIDE_FIXTURE )
135
136
137BOOST_AUTO_TEST_CASE( BaseOverrideSetGet )
138{
139 SCH_SYMBOL sym;
140
141 BOOST_CHECK( sym.GetPinMapOverride().IsDefault() );
142
145 ov.m_ActiveMapName = wxS( "DFN-8-EP" );
146 ov.m_Edits.push_back( { wxS( "1" ), wxS( "8" ) } );
147 ov.m_Edits.push_back( { wxS( "8" ), wxS( "1" ) } );
148
149 sym.SetPinMapOverride( ov );
150
151 BOOST_CHECK( sym.GetPinMapOverride() == ov );
152 BOOST_CHECK( !sym.GetPinMapOverride().IsDefault() );
153}
154
155
156BOOST_AUTO_TEST_CASE( VariantSnapshotsBaseOverride )
157{
158 // A variant created for an unrelated reason must snapshot the base override so it does not
159 // mask it when read back through that variant.
160 SCH_SYMBOL sym;
161
164 sym.SetPinMapOverride( ov );
165
166 SCH_SYMBOL_VARIANT variant( wxS( "Assembly" ) );
167 variant.InitializeAttributes( sym );
168
169 BOOST_CHECK( variant.m_PinMapOverride == ov );
170}
171
172
173BOOST_AUTO_TEST_CASE( BaseOverrideRoundTripThroughFile )
174{
176
179 ov.m_ActiveMapName = wxS( "DFN-8-EP" );
180 ov.m_Edits.push_back( { wxS( "1" ), wxS( "8" ) } );
181
182 SCH_SHEET* sheet = placeSymbol( ov );
183 wxString fileName = tempFile();
184
186 io.SaveSchematicFile( fileName, sheet, m_schematic.get() );
187
188 {
189 wxFile f( fileName );
190 wxString contents;
191 f.ReadAll( &contents );
192 BOOST_CHECK( contents.Contains( wxS( "(pin_map_override" ) ) );
193 BOOST_CHECK( contents.Contains( wxS( "named_map" ) ) );
194 }
195
196 m_libSym.reset();
197
198 SCH_SYMBOL* reloaded = reload( fileName );
199 BOOST_REQUIRE( reloaded );
200
202 BOOST_CHECK( result.m_Mode == PIN_MAP_OVERRIDE_MODE::USE_NAMED_MAP );
203 BOOST_CHECK_EQUAL( result.m_ActiveMapName, wxS( "DFN-8-EP" ) );
204 BOOST_REQUIRE_EQUAL( result.m_Edits.size(), 1u );
205 BOOST_CHECK_EQUAL( result.m_Edits[0].m_PinNumber, wxS( "1" ) );
206 BOOST_CHECK_EQUAL( result.m_Edits[0].m_PadNumber, wxS( "8" ) );
207}
208
209
210BOOST_AUTO_TEST_CASE( DefaultOverrideEmitsNothing )
211{
213
214 SCH_SHEET* sheet = placeSymbol( PIN_MAP_INSTANCE_OVERRIDE() );
215 wxString fileName = tempFile();
216
218 io.SaveSchematicFile( fileName, sheet, m_schematic.get() );
219
220 wxFile f( fileName );
221 wxString contents;
222 f.ReadAll( &contents );
223 BOOST_CHECK( !contents.Contains( wxS( "(pin_map_override" ) ) );
224}
225
226
const char * name
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:221
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
A SCH_IO derivation for loading schematic files using the new s-expression file format.
void SaveSchematicFile(const wxString &aFileName, SCH_SHEET *aSheet, SCHEMATIC *aSchematic, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aSchematic to a storage file in a format that this SCH_IO implementation knows about,...
SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load information from some input file format that this SCH_IO implementation knows about,...
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:115
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:139
Variant information for a schematic symbol.
PIN_MAP_INSTANCE_OVERRIDE m_PinMapOverride
Per-instance pin-to-pad map override for this variant (issue #2282).
void InitializeAttributes(const SCH_SYMBOL &aSymbol)
Schematic symbol object.
Definition sch_symbol.h:69
PIN_MAP_INSTANCE_OVERRIDE GetPinMapOverride(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const
void SetPinMapOverride(const PIN_MAP_INSTANCE_OVERRIDE &aOverride, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString)
Set the per-instance pin-to-pad map override (issue #2282).
void UpdatePins()
Updates the cache of SCH_PIN objects for each pin.
@ PT_PASSIVE
pin for passive symbols: must be connected, and can be connected to any pin.
Definition pin_type.h:39
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
std::vector< FAB_LAYER_COLOR > dummy
Per-instance override of the active pin map and a sparse delta on top.
Definition pin_map.h:199
std::vector< PIN_MAP_ENTRY > m_Edits
Definition pin_map.h:202
bool IsDefault() const
Definition pin_map.h:209
PIN_MAP_OVERRIDE_MODE m_Mode
Definition pin_map.h:200
SCH_SHEET * placeSymbol(const PIN_MAP_INSTANCE_OVERRIDE &aOverride)
Place a one-pin symbol carrying aOverride on a fresh top sheet and return the sheet.
std::unique_ptr< SCHEMATIC > m_schematic
SCH_SYMBOL * reload(const wxString &aFileName)
std::unique_ptr< LIB_SYMBOL > m_libSym
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
std::string path
KIBIS_PIN * pin1
BOOST_AUTO_TEST_CASE(BaseOverrideSetGet)
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_SYMBOL_T
Definition typeinfo.h:169
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683