KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue22620_group_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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 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
36
37#include <boost/test/unit_test.hpp>
38
39#include <sch_group.h>
40#include <sch_screen.h>
41#include <sch_symbol.h>
42#include <schematic.h>
44
46
47#include <wx/filename.h>
48#include <wx/stdpaths.h>
49
50
52{
55 {
56 wxString tempDir = wxStandardPaths::Get().GetTempDir();
57 wxString projectPath = tempDir + wxFileName::GetPathSeparator()
58 + wxT( "test_group_annotation.kicad_pro" );
59 m_tempFiles.push_back( projectPath );
60
61 m_settingsManager.LoadProject( projectPath.ToStdString() );
62 m_schematic = std::make_unique<SCHEMATIC>( nullptr );
64 m_schematic->SetProject( m_project );
65 }
66
68 {
69 for( const wxString& file : m_tempFiles )
70 {
71 if( wxFileExists( file ) )
72 wxRemoveFile( file );
73 }
74
75 m_schematic.reset();
76 }
77
79 std::unique_ptr<SCHEMATIC> m_schematic;
81 std::vector<wxString> m_tempFiles;
82};
83
84
85BOOST_FIXTURE_TEST_SUITE( Issue22620GroupAnnotation, GROUP_ANNOTATION_FIXTURE )
86
87
88
92BOOST_AUTO_TEST_CASE( TestGroupRunOnChildrenFindsSymbols )
93{
94 m_schematic->CreateDefaultScreens();
95
96 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
97 BOOST_REQUIRE( !topSheets.empty() );
98
99 SCH_SCREEN* screen = topSheets[0]->GetScreen();
100 BOOST_REQUIRE( screen != nullptr );
101
102 // Create two simple symbols
103 SCH_SYMBOL* symbol1 = new SCH_SYMBOL();
104 symbol1->SetPosition( VECTOR2I( 0, 0 ) );
105 screen->Append( symbol1 );
106
107 SCH_SYMBOL* symbol2 = new SCH_SYMBOL();
108 symbol2->SetPosition( VECTOR2I( 1000000, 0 ) );
109 screen->Append( symbol2 );
110
111 // Create a group containing both symbols
112 SCH_GROUP* group = new SCH_GROUP( screen );
113 group->SetName( wxT( "DesignBlock" ) );
114 group->AddItem( symbol1 );
115 group->AddItem( symbol2 );
116 screen->Append( group );
117
118 // Use RunOnChildren to find symbols in the group
119 std::unordered_set<SCH_SYMBOL*> foundSymbols;
120
121 group->RunOnChildren(
122 [&foundSymbols]( SCH_ITEM* aChild )
123 {
124 if( aChild->Type() == SCH_SYMBOL_T )
125 foundSymbols.insert( static_cast<SCH_SYMBOL*>( aChild ) );
126 },
128
129 // Verify both symbols were found
130 BOOST_CHECK_EQUAL( foundSymbols.size(), 2 );
131 BOOST_CHECK( foundSymbols.count( symbol1 ) == 1 );
132 BOOST_CHECK( foundSymbols.count( symbol2 ) == 1 );
133
134 BOOST_TEST_MESSAGE( "Test passed: RunOnChildren correctly finds symbols in groups" );
135}
136
137
141BOOST_AUTO_TEST_CASE( TestGroupRunOnChildrenWithNestedGroups )
142{
143 m_schematic->CreateDefaultScreens();
144
145 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
146 BOOST_REQUIRE( !topSheets.empty() );
147
148 SCH_SCREEN* screen = topSheets[0]->GetScreen();
149 BOOST_REQUIRE( screen != nullptr );
150
151 // Create symbols
152 SCH_SYMBOL* symbol1 = new SCH_SYMBOL();
153 symbol1->SetPosition( VECTOR2I( 0, 0 ) );
154 screen->Append( symbol1 );
155
156 SCH_SYMBOL* symbol2 = new SCH_SYMBOL();
157 symbol2->SetPosition( VECTOR2I( 1000000, 0 ) );
158 screen->Append( symbol2 );
159
160 SCH_SYMBOL* symbol3 = new SCH_SYMBOL();
161 symbol3->SetPosition( VECTOR2I( 2000000, 0 ) );
162 screen->Append( symbol3 );
163
164 // Create an inner group with symbol1
165 SCH_GROUP* innerGroup = new SCH_GROUP( screen );
166 innerGroup->SetName( wxT( "InnerGroup" ) );
167 innerGroup->AddItem( symbol1 );
168 screen->Append( innerGroup );
169
170 // Create an outer group containing the inner group and symbol2
171 SCH_GROUP* outerGroup = new SCH_GROUP( screen );
172 outerGroup->SetName( wxT( "OuterGroup" ) );
173 outerGroup->AddItem( innerGroup );
174 outerGroup->AddItem( symbol2 );
175 screen->Append( outerGroup );
176
177 // Use RunOnChildren with RECURSE to find all symbols in nested groups
178 std::unordered_set<SCH_SYMBOL*> foundSymbols;
179
180 outerGroup->RunOnChildren(
181 [&foundSymbols]( SCH_ITEM* aChild )
182 {
183 if( aChild->Type() == SCH_SYMBOL_T )
184 foundSymbols.insert( static_cast<SCH_SYMBOL*>( aChild ) );
185 },
187
188 // Verify both symbol1 (in nested group) and symbol2 were found
189 BOOST_CHECK_EQUAL( foundSymbols.size(), 2 );
190 BOOST_CHECK( foundSymbols.count( symbol1 ) == 1 );
191 BOOST_CHECK( foundSymbols.count( symbol2 ) == 1 );
192 BOOST_CHECK( foundSymbols.count( symbol3 ) == 0 ); // symbol3 is not in any group
193
194 BOOST_TEST_MESSAGE( "Test passed: RunOnChildren correctly handles nested groups" );
195}
196
197
void AddItem(EDA_ITEM *aItem)
Add item to group.
Definition eda_group.cpp:27
void SetName(const wxString &aName)
Definition eda_group.h:52
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
Container for project specific data.
Definition project.h:65
A set of SCH_ITEMs (i.e., without duplicates).
Definition sch_group.h:52
void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction, RECURSE_MODE aMode) override
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:167
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Schematic symbol object.
Definition sch_symbol.h:76
void SetPosition(const VECTOR2I &aPosition) override
Definition sch_symbol.h:855
@ RECURSE
Definition eda_item.h:51
Class to handle a set of SCH_ITEMs.
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(TestGroupRunOnChildrenFindsSymbols)
Test that SCH_GROUP::RunOnChildren correctly finds symbols inside a group.
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_SYMBOL_T
Definition typeinfo.h:176
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695