KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_legacy_power_symbols.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, see <https://www.gnu.org/licenses/>.
18 */
19
21#include <qa_utils/file_utils.h>
23#include <wx/filefn.h>
24
25#include <connection_graph.h>
26#include <advanced_config.h>
27#include <eeschema_helpers.h>
28#include <pgm_base.h>
31#include <scoped_set_reset.h>
32#include <schematic.h>
33#include <sch_screen.h>
34#include <sch_line.h>
37#include <locale_io.h>
38
40{
43
45 {
46 size_t checked = 0;
47
48 for( SCH_ITEM* item : m_schematic->RootScreen()->Items().OfType( SCH_SYMBOL_T ) )
49 {
50 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
51
52 // Fix pre-8.0 legacy power symbols with invisible pins
53 // that have mismatched pin names and value fields
54 if( symbol->GetLibSymbolRef()
55 && symbol->GetLibSymbolRef()->IsGlobalPower()
56 && symbol->GetAllLibPins().size() > 0
57 && symbol->GetAllLibPins()[0]->IsGlobalPower()
58 && !symbol->GetAllLibPins()[0]->IsVisible() )
59 {
60 ++checked;
62 symbol->GetAllLibPins()[0]->GetName() );
63 }
64 }
65
66 BOOST_CHECK_GT( checked, 0 );
67 }
68
70 std::unique_ptr<SCHEMATIC> m_schematic;
71};
72
73
75{
76 KI_TEST::LoadSchematic( m_settingsManager, "netlists/legacy_power/legacy_power", m_schematic );
77
78 CheckSymbols();
79}
80
81
83{
84 const wxString path = KI_TEST::GetEeschemaTestDataDir() + "netlists/legacy_power4/legacy_power4.sch";
85 m_schematic.reset( EESCHEMA_HELPERS::LoadSchematic( path, false, true ) );
86 BOOST_REQUIRE( m_schematic );
87 size_t symbols = 0;
88
89 for( SCH_ITEM* item : m_schematic->RootScreen()->Items().OfType( SCH_SYMBOL_T ) )
90 {
91 const auto* symbol = static_cast<SCH_SYMBOL*>( item );
92 BOOST_REQUIRE( symbol->GetLibSymbolRef() );
93 ++symbols;
94
95 if( symbol->GetField( FIELD_T::REFERENCE )->GetText().StartsWith( "R" ) )
96 BOOST_CHECK_EQUAL( symbol->GetAllLibPins().size(), 2 );
97 }
98
99 BOOST_CHECK_EQUAL( symbols, 6 );
100 CheckSymbols();
101
102 m_schematic.reset();
103 KI_TEST::SCOPED_TEMP_DIR directory( "legacy-headless" );
104 const wxString withoutCache = directory.PathStr() + "/legacy_power4.sch";
105 BOOST_REQUIRE( wxCopyFile( path, withoutCache ) );
106 m_schematic.reset( EESCHEMA_HELPERS::LoadSchematic( withoutCache, false, true ) );
107 BOOST_CHECK( !m_schematic );
108}
109
110
111BOOST_AUTO_TEST_CASE( LegacyHeadlessLoadUsesItsProjectLibraries )
112{
113 KI_TEST::SCOPED_TEMP_DIR directory( "legacy-project-libraries" );
114 const wxString source = KI_TEST::GetEeschemaTestDataDir() + "netlists/complex_hierarchy";
115 std::filesystem::copy( std::filesystem::path( source.ToStdString() ), directory.Path(),
116 std::filesystem::copy_options::recursive );
117 SETTINGS_MANAGER settings;
118 BOOST_REQUIRE( settings.LoadProject( directory.PathStr() + "/complex_hierarchy.kicad_pro" ) );
119 const auto defaults = settings.Prj().GetProjectFile().NetSettings()->GetDefaultNetclass();
120 BOOST_REQUIRE( defaults );
121 BOOST_CHECK( defaults->HasWireWidth() );
122 BOOST_CHECK_GT( defaults->GetWireWidth(), 0 );
123 BOOST_CHECK( defaults->HasBusWidth() );
124 BOOST_CHECK_GT( defaults->GetBusWidth(), 0 );
125 PROJECT* active = &Pgm().GetSettingsManager().Prj();
126 const wxString activePath = active->GetProjectFullName();
127 BOOST_REQUIRE( active != &settings.Prj() );
128 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
129
130 for( bool engine : { false, true } )
131 {
132 SCOPED_SET_RESET backend( enabled, engine );
133 std::unique_ptr<SCHEMATIC> schematic( EESCHEMA_HELPERS::LoadSchematic(
134 directory.PathStr() + "/complex_hierarchy.sch", false, false, &settings.Prj() ) );
135 BOOST_REQUIRE( schematic );
136 BOOST_CHECK( &Pgm().GetSettingsManager().Prj() == active );
137 BOOST_CHECK_EQUAL( active->GetProjectFullName(), activePath );
138 BOOST_CHECK( &schematic->Project() == &settings.Prj() );
139 size_t aliases = 0;
140 SCH_SCREENS screens( schematic->Root() );
141
142 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
143 {
144 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
145 {
146 auto* symbol = static_cast<SCH_SYMBOL*>( item );
147 BOOST_REQUIRE( symbol->GetLibSymbolRef() );
148
149 if( symbol->GetLibId().GetLibItemName() == UTF8( "LM358N" ) )
150 {
151 ++aliases;
152 BOOST_CHECK_EQUAL( symbol->GetLibSymbolRef()->GetUnitCount(), 2 );
153 BOOST_CHECK_EQUAL( symbol->GetAllLibPins().size(), 8 );
154 }
155 }
156 }
157
158 BOOST_CHECK_EQUAL( aliases, 2 );
159
160 bool checkedOutput = false;
161
162 for( const SCH_SHEET_PATH& path : schematic->Hierarchy() )
163 {
164 for( SCH_ITEM* item : path.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
165 {
166 auto* symbol = static_cast<SCH_SYMBOL*>( item );
167
168 if( symbol->GetRef( &path ) == wxS( "U2" ) )
169 {
170 SCH_PIN* pin = symbol->GetPin( "1" );
172 const VECTOR2I pos = pin->GetPosition();
173 const auto wires = path.LastScreen()->GetBusesAndWires( pos, false );
174 BOOST_REQUIRE_EQUAL( wires.size(), 1 );
175 BOOST_CHECK_GT( wires.front()->GetPenWidth(), 0 );
176
177 const auto name = pin->GetConnectionName( &path );
179 BOOST_CHECK_EQUAL( *name, wxString( "VCC" ) );
180 checkedOutput = true;
181 }
182 }
183 }
184
185 BOOST_CHECK( checkedOutput );
186 }
187}
188
189
191{
192 const wxString path = KI_TEST::GetEeschemaTestDataDir() + "netlists/legacy_power4/legacy_power4";
193 m_settingsManager.LoadProject( "" );
194 m_schematic = std::make_unique<SCHEMATIC>( &m_settingsManager.Prj() );
195 m_schematic->Reset();
197 SCH_SHEET* root = io.LoadSchematicFile( path + ".sch", m_schematic.get() );
198 BOOST_REQUIRE( root );
199 BOOST_REQUIRE_MESSAGE( io.GetError().IsEmpty(), io.GetError() );
200 m_schematic->SetTopLevelSheets( { root } );
201 BOOST_REQUIRE_LT( m_schematic->RootScreen()->GetFileFormatVersionAtLoad(), 20230221 );
202
203 size_t mismatches = 0;
204
205 for( SCH_ITEM* item : m_schematic->RootScreen()->Items().OfType( SCH_SYMBOL_T ) )
206 {
207 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
208 LIB_SYMBOL* lib = io.LoadSymbol( path + "-cache.lib", symbol->GetLibId().GetLibItemName() );
209 BOOST_REQUIRE( lib );
210 symbol->SetLibSymbol( lib->Flatten().release() );
211
212 if( lib->IsGlobalPower() && symbol->GetField( FIELD_T::VALUE )->GetText() == "+3.3V" )
213 {
214 BOOST_REQUIRE_EQUAL( symbol->GetAllLibPins().size(), 1 );
215 BOOST_REQUIRE( symbol->GetAllLibPins()[0]->IsGlobalPower() );
216 BOOST_REQUIRE( !symbol->GetAllLibPins()[0]->IsVisible() );
217 BOOST_CHECK_EQUAL( symbol->GetAllLibPins()[0]->GetName(), wxString( "VCC" ) );
218 ++mismatches;
219 }
220 }
221
222 BOOST_REQUIRE_GT( mismatches, 0 );
223 m_schematic->RootScreen()->FixLegacyPowerSymbolMismatches();
224 CheckSymbols();
225}
const char * name
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
static SCHEMATIC * LoadSchematic(const wxString &aFileName, bool aSetActive, bool aForceDefaultProject, PROJECT *aProject=nullptr, bool aCalculateConnectivity=true, REPORTER *aRootReporter=nullptr)
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
Define a library symbol object.
Definition lib_symbol.h:114
bool IsGlobalPower() const override
std::unique_ptr< LIB_SYMBOL > Flatten() const
Return a flattened symbol inheritance to the caller.
std::shared_ptr< NETCLASS > GetDefaultNetclass() const
Gets the default netclass for the project.
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:123
std::shared_ptr< NET_SETTINGS > & NetSettings()
Container for project specific data.
Definition project.h:63
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition project.cpp:177
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:201
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:139
A SCH_IO derivation for loading schematic files created before the new s-expression file format.
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
const wxString & GetError() const override
Return an error string to the caller.
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:170
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:818
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
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:48
Schematic symbol object.
Definition sch_symbol.h:74
std::vector< SCH_PIN * > GetAllLibPins() const
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:163
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:182
void SetLibSymbol(LIB_SYMBOL *aLibSymbol)
Set this schematic symbol library symbol reference to aLibSymbol.
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
RAII class that sets an value at construction and resets it to the original value at destruction.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:67
PROJECT & Prj()
Definition kicad.cpp:727
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
std::string path
KIBIS_PIN * pin
BOOST_AUTO_TEST_CASE(LegacyHeadlessLoadUsesItsProjectLibraries)
BOOST_FIXTURE_TEST_CASE(LegacyPowerFixup, LEGACY_POWER_SYMBOLS_TEST_FIXTURE)
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_SYMBOL_T
Definition typeinfo.h:168
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683