KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_link_imported_footprints.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 2
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
20#include <memory>
21
24
25#include <lib_symbol.h>
26#include <pgm_base.h>
27#include <project.h>
28#include <reporter.h>
29#include <sch_screen.h>
30#include <sch_sheet_path.h>
31#include <sch_symbol.h>
32#include <schematic.h>
34
36
37
38BOOST_AUTO_TEST_SUITE( LinkImportedFootprints_ )
39
40
41// Two unannotated symbols share R?, so only the annotated one may take the board footprint
42BOOST_AUTO_TEST_CASE( UnannotatedReferencesAreNotLinked )
43{
44 Pgm().GetSettingsManager().LoadProject( wxS( "" ) );
46
47 std::unique_ptr<SCHEMATIC> schematic = std::make_unique<SCHEMATIC>( &project );
48 schematic->Reset();
49
50 BOOST_REQUIRE_EQUAL( schematic->Hierarchy().size(), 1U );
51
52 SCH_SHEET_PATH path = schematic->Hierarchy().front();
53 SCH_SCREEN* screen = path.LastScreen();
54 LIB_SYMBOL lib( wxS( "R" ), nullptr );
55
56 std::vector<SCH_SYMBOL*> symbols;
57
58 for( const wxString& ref : { wxS( "R?" ), wxS( "R?" ), wxS( "R1" ) } )
59 {
60 SCH_SYMBOL* symbol = new SCH_SYMBOL( lib, lib.GetLibId(), &path, 1 );
61 symbol->SetRef( &path, ref );
62 screen->Append( symbol );
63 symbols.push_back( symbol );
64 }
65
66 std::map<wxString, wxString> footprints = { { wxS( "R?" ), wxS( "lib:R_0603" ) },
67 { wxS( "R1" ), wxS( "lib:R_0805" ) } };
68
70
71 BOOST_CHECK( symbols[0]->GetField( FIELD_T::FOOTPRINT )->GetText().IsEmpty() );
72 BOOST_CHECK( symbols[1]->GetField( FIELD_T::FOOTPRINT )->GetText().IsEmpty() );
73 BOOST_CHECK_EQUAL( symbols[2]->GetField( FIELD_T::FOOTPRINT )->GetText(), wxS( "lib:R_0805" ) );
74}
75
76
77// The import cache renames a footprint two libraries supply differently, which only the board knows, but a field in
78// another library is the user's choice
79BOOST_AUTO_TEST_CASE( CacheRenamesReachTheFields )
80{
81 Pgm().GetSettingsManager().LoadProject( wxS( "" ) );
83
84 std::unique_ptr<SCHEMATIC> schematic = std::make_unique<SCHEMATIC>( &project );
85 schematic->Reset();
86
87 SCH_SHEET_PATH path = schematic->Hierarchy().front();
88 SCH_SCREEN* screen = path.LastScreen();
89 LIB_SYMBOL lib( wxS( "R" ), nullptr );
90
91 std::vector<SCH_SYMBOL*> symbols;
92
93 for( const wxString& ref : { wxS( "R1" ), wxS( "R2" ) } )
94 {
95 SCH_SYMBOL* symbol = new SCH_SYMBOL( lib, lib.GetLibId(), &path, 1 );
96 symbol->SetRef( &path, ref );
97 screen->Append( symbol );
98 symbols.push_back( symbol );
99 }
100
101 symbols[0]->SetFootprintFieldText( wxS( "cache:SOIC8" ) );
102 symbols[1]->SetFootprintFieldText( wxS( "mine:SOIC8" ) );
103
104 std::map<wxString, wxString> footprints = { { wxS( "R1" ), wxS( "cache:SOIC8_1" ) },
105 { wxS( "R2" ), wxS( "cache:SOIC8_1" ) } };
106
108
109 BOOST_CHECK_EQUAL( symbols[0]->GetField( FIELD_T::FOOTPRINT )->GetText(), wxS( "cache:SOIC8_1" ) );
110 BOOST_CHECK_EQUAL( symbols[1]->GetField( FIELD_T::FOOTPRINT )->GetText(), wxS( "mine:SOIC8" ) );
111}
112
113
114// A sheet used twice shares its symbols, so a rename that reaches only one of the two parts must leave the field
115BOOST_AUTO_TEST_CASE( SharedSheetInstancesMustAgree )
116{
117 SETTINGS_MANAGER settings;
118 std::unique_ptr<SCHEMATIC> schematic;
119 KI_TEST::LoadSchematic( settings, "issue23840/BusAndVectors", schematic );
120
121 std::vector<SCH_SHEET_PATH> paths;
122
123 for( const SCH_SHEET_PATH& path : schematic->Hierarchy() )
124 {
125 if( path.LastScreen()->GetFileName().EndsWith( "LEDs.kicad_sch" ) )
126 paths.push_back( path );
127 }
128
129 BOOST_REQUIRE_EQUAL( paths.size(), 2 );
130
131 SCH_SYMBOL* shared = nullptr;
132
133 for( SCH_ITEM* item : paths[0].LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
134 {
135 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
136
137 if( symbol->GetRef( &paths[0] ) != symbol->GetRef( &paths[1] ) )
138 {
139 shared = symbol;
140 break;
141 }
142 }
143
144 BOOST_REQUIRE( shared );
145
146 const wxString first = shared->GetRef( &paths[0] );
147 const wxString second = shared->GetRef( &paths[1] );
148
149 shared->SetFootprintFieldText( wxS( "cache:SOIC8" ) );
150
151 std::map<wxString, wxString> split = { { first, wxS( "cache:SOIC8_1" ) }, { second, wxS( "mine:SOIC8" ) } };
152
154 BOOST_CHECK_EQUAL( shared->GetField( FIELD_T::FOOTPRINT )->GetText(), wxS( "cache:SOIC8" ) );
155
156 std::map<wxString, wxString> agreed = { { first, wxS( "cache:SOIC8_1" ) }, { second, wxS( "cache:SOIC8_1" ) } };
157
158 BOOST_CHECK_GT( LinkImportedFootprints( *schematic, agreed, NULL_REPORTER::GetInstance() ), 0 );
159 BOOST_CHECK_EQUAL( shared->GetField( FIELD_T::FOOTPRINT )->GetText(), wxS( "cache:SOIC8_1" ) );
160}
161
162
Define a library symbol object.
Definition lib_symbol.h:114
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:190
static REPORTER & GetInstance()
Definition reporter.cpp:207
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:123
Container for project specific data.
Definition project.h:63
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:139
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
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:75
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
void SetFootprintFieldText(const wxString &aFootprint)
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
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.
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
static std::vector< std::string > split(const std::string &aStr, const std::string &aDelim)
Split the input string into a vector of output strings.
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
std::string path
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_SYMBOL_T
Definition typeinfo.h:168