KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue22651_sheet_annotation.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
33
34#include <boost/test/unit_test.hpp>
35
36#include <sch_screen.h>
37#include <sch_sheet.h>
38#include <sch_sheet_path.h>
39#include <sch_symbol.h>
40#include <schematic.h>
41#include <sch_reference_list.h>
43
45
46#include <wx/filename.h>
47#include <wx/stdpaths.h>
48
49
51{
54 {
55 wxString tempDir = wxStandardPaths::Get().GetTempDir();
56 wxString projectPath = tempDir + wxFileName::GetPathSeparator()
57 + wxT( "test_sheet_annotation.kicad_pro" );
58 m_tempFiles.push_back( projectPath );
59
60 m_settingsManager.LoadProject( projectPath.ToStdString() );
61 m_schematic = std::make_unique<SCHEMATIC>( nullptr );
63 m_schematic->SetProject( m_project );
64 }
65
67 {
68 for( const wxString& file : m_tempFiles )
69 {
70 if( wxFileExists( file ) )
71 wxRemoveFile( file );
72 }
73
74 m_schematic.reset();
75 }
76
78 std::unique_ptr<SCHEMATIC> m_schematic;
80 std::vector<wxString> m_tempFiles;
81};
82
83
84BOOST_FIXTURE_TEST_SUITE( Issue22651SheetAnnotation, SHEET_ANNOTATION_FIXTURE )
85
86
87
91BOOST_AUTO_TEST_CASE( TestSheetFoundInHierarchyAfterRefresh )
92{
93 m_schematic->CreateDefaultScreens();
94
95 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
96 BOOST_REQUIRE( !topSheets.empty() );
97
98 SCH_SHEET* rootSheet = topSheets[0];
99 SCH_SCREEN* rootScreen = rootSheet->GetScreen();
100 BOOST_REQUIRE( rootScreen != nullptr );
101
102 // Create a new sub-sheet with its own screen
103 SCH_SHEET* newSheet = new SCH_SHEET( rootSheet, VECTOR2I( 0, 0 ) );
104 SCH_SCREEN* newScreen = new SCH_SCREEN( m_schematic.get() );
105 newSheet->SetScreen( newScreen );
106 newSheet->GetField( FIELD_T::SHEET_NAME )->SetText( wxT( "TestSubSheet" ) );
107 newSheet->GetField( FIELD_T::SHEET_FILENAME )->SetText( wxT( "test_subsheet.kicad_sch" ) );
108 newScreen->SetFileName( wxT( "/tmp/test_subsheet.kicad_sch" ) );
109
110 // Add a symbol to the sub-sheet's screen
111 SCH_SYMBOL* symbol = new SCH_SYMBOL();
112 symbol->SetPosition( VECTOR2I( 1000000, 0 ) );
113 newScreen->Append( symbol );
114
115 // Get hierarchy BEFORE adding the sheet (simulating stale cache)
116 SCH_SHEET_LIST hierarchyBefore = m_schematic->Hierarchy();
117 size_t countBefore = hierarchyBefore.size();
118
119 // Add the new sheet to the root screen
120 rootScreen->Append( newSheet );
121
122 // The cached hierarchy should still be stale at this point
123 SCH_SHEET_LIST hierarchyStale = m_schematic->Hierarchy();
124 BOOST_CHECK_EQUAL( hierarchyStale.size(), countBefore );
125
126 // Refresh the hierarchy
127 m_schematic->RefreshHierarchy();
128
129 // Now the hierarchy should include the new sheet
130 SCH_SHEET_LIST hierarchyAfter = m_schematic->Hierarchy();
131 BOOST_CHECK_EQUAL( hierarchyAfter.size(), countBefore + 1 );
132
133 BOOST_TEST_MESSAGE( "Test passed: Sheet found in hierarchy after refresh" );
134}
135
136
141BOOST_AUTO_TEST_CASE( TestSymbolsFoundInNewSheetAfterRefresh )
142{
143 m_schematic->CreateDefaultScreens();
144
145 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
146 BOOST_REQUIRE( !topSheets.empty() );
147
148 SCH_SHEET* rootSheet = topSheets[0];
149 SCH_SCREEN* rootScreen = rootSheet->GetScreen();
150 BOOST_REQUIRE( rootScreen != nullptr );
151
152 // Create a new sub-sheet with its own screen
153 SCH_SHEET* newSheet = new SCH_SHEET( rootSheet, VECTOR2I( 0, 0 ) );
154 SCH_SCREEN* newScreen = new SCH_SCREEN( m_schematic.get() );
155 newSheet->SetScreen( newScreen );
156 newSheet->GetField( FIELD_T::SHEET_NAME )->SetText( wxT( "DesignBlockSheet" ) );
157 newSheet->GetField( FIELD_T::SHEET_FILENAME )->SetText( wxT( "design_block.kicad_sch" ) );
158 newScreen->SetFileName( wxT( "/tmp/design_block.kicad_sch" ) );
159
160 // Add two symbols to the sub-sheet's screen (simulating design block content)
161 SCH_SYMBOL* symbol1 = new SCH_SYMBOL();
162 symbol1->SetPosition( VECTOR2I( 0, 0 ) );
163 newScreen->Append( symbol1 );
164
165 SCH_SYMBOL* symbol2 = new SCH_SYMBOL();
166 symbol2->SetPosition( VECTOR2I( 1000000, 0 ) );
167 newScreen->Append( symbol2 );
168
169 // Add the new sheet to the root screen
170 rootScreen->Append( newSheet );
171
172 // Refresh the hierarchy (this is the fix for issue 22651)
173 m_schematic->RefreshHierarchy();
174
175 // Build the sheet path for the new sheet
176 SCH_SHEET_PATH currentPath;
177 currentPath.push_back( rootSheet );
178
179 SCH_SHEET_PATH newSheetPath = currentPath;
180 newSheetPath.push_back( newSheet );
181
182 // Use GetSheetsWithinPath to find the new sheet (as annotation does)
183 SCH_SHEET_LIST hierarchy = m_schematic->Hierarchy();
184 std::vector<SCH_SHEET_PATH> foundSheets;
185 hierarchy.GetSheetsWithinPath( foundSheets, newSheetPath );
186
187 BOOST_CHECK_EQUAL( foundSheets.size(), 1 );
188
189 // Use GetSymbolsWithinPath to find symbols (as annotation does)
190 // Pass true for aForceIncludeOrphanSymbols since test symbols don't have library refs
191 SCH_REFERENCE_LIST references;
192 hierarchy.GetSymbolsWithinPath( references, newSheetPath, SYMBOL_FILTER_NON_POWER, true );
193
194 BOOST_CHECK_EQUAL( references.GetCount(), 2 );
195
196 BOOST_TEST_MESSAGE( "Test passed: Symbols found in new sheet after hierarchy refresh" );
197}
198
199
204BOOST_AUTO_TEST_CASE( TestSymbolsNotFoundWithoutRefresh )
205{
206 m_schematic->CreateDefaultScreens();
207
208 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
209 BOOST_REQUIRE( !topSheets.empty() );
210
211 SCH_SHEET* rootSheet = topSheets[0];
212 SCH_SCREEN* rootScreen = rootSheet->GetScreen();
213 BOOST_REQUIRE( rootScreen != nullptr );
214
215 // Get hierarchy BEFORE adding any sheets
216 SCH_SHEET_LIST hierarchyBefore = m_schematic->Hierarchy();
217
218 // Create a new sub-sheet with its own screen
219 SCH_SHEET* newSheet = new SCH_SHEET( rootSheet, VECTOR2I( 0, 0 ) );
220 SCH_SCREEN* newScreen = new SCH_SCREEN( m_schematic.get() );
221 newSheet->SetScreen( newScreen );
222 newSheet->GetField( FIELD_T::SHEET_FILENAME )->SetText( wxT( "stale_test.kicad_sch" ) );
223 newScreen->SetFileName( wxT( "/tmp/stale_test.kicad_sch" ) );
224
225 // Add a symbol to the sub-sheet's screen
226 SCH_SYMBOL* symbol = new SCH_SYMBOL();
227 symbol->SetPosition( VECTOR2I( 0, 0 ) );
228 newScreen->Append( symbol );
229
230 // Add the new sheet to the root screen
231 rootScreen->Append( newSheet );
232
233 // Build the sheet path for the new sheet
234 SCH_SHEET_PATH currentPath;
235 currentPath.push_back( rootSheet );
236
237 SCH_SHEET_PATH newSheetPath = currentPath;
238 newSheetPath.push_back( newSheet );
239
240 // WITHOUT refreshing the hierarchy, try to find symbols
241 // This demonstrates the bug behavior before the fix
242 std::vector<SCH_SHEET_PATH> foundSheets;
243 hierarchyBefore.GetSheetsWithinPath( foundSheets, newSheetPath );
244
245 // The stale hierarchy should NOT find the new sheet
246 BOOST_CHECK_EQUAL( foundSheets.size(), 0 );
247
248 SCH_REFERENCE_LIST references;
249 hierarchyBefore.GetSymbolsWithinPath( references, newSheetPath, SYMBOL_FILTER_NON_POWER, true );
250
251 // The stale hierarchy should NOT find the symbols
252 BOOST_CHECK_EQUAL( references.GetCount(), 0 );
253
254 BOOST_TEST_MESSAGE( "Test passed: Stale hierarchy does not find symbols (confirms root cause)" );
255}
256
257
Container for project specific data.
Definition project.h:62
void SetText(const wxString &aText) override
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void GetSymbolsWithinPath(SCH_REFERENCE_LIST &aReferences, const SCH_SHEET_PATH &aSheetPath, SYMBOL_FILTER aSymbolFilter, bool aForceIncludeOrphanSymbols=false) const
Add a SCH_REFERENCE object to aReferences for each symbol in the list of sheets that are contained wi...
void GetSheetsWithinPath(std::vector< SCH_SHEET_PATH > &aSheets, const SCH_SHEET_PATH &aSheetPath) const
Add a SCH_SHEET_PATH object to aSheets for each sheet in the list that are contained within aSheetPat...
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this sheet.
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:139
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Schematic symbol object.
Definition sch_symbol.h:69
void SetPosition(const VECTOR2I &aPosition) override
Definition sch_symbol.h:886
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ SYMBOL_FILTER_NON_POWER
std::unique_ptr< SCHEMATIC > m_schematic
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(TestSheetFoundInHierarchyAfterRefresh)
Test that after adding a sheet and refreshing the hierarchy, the sheet path can be found via GetSheet...
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683