KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_pcad_sch_import_fixture.h
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#ifndef TEST_PCAD_SCH_IMPORT_FIXTURE_H
21#define TEST_PCAD_SCH_IMPORT_FIXTURE_H
22
27
29
31
32#include <lib_symbol.h>
33#include <sch_bus_entry.h>
34#include <sch_junction.h>
35#include <sch_label.h>
36#include <sch_line.h>
37#include <sch_pin.h>
38#include <sch_screen.h>
39#include <sch_shape.h>
40#include <sch_sheet_path.h>
41#include <sch_symbol.h>
42#include <sch_text.h>
43#include <schematic.h>
45#include <title_block.h>
46
47#include <memory>
48#include <set>
49
50
52{
54 {
55 m_manager.LoadProject( "" );
56 m_schematic->SetProject( &m_manager.Prj() );
57 m_schematic->CurrentSheet().clear();
58 m_schematic->CurrentSheet().push_back( &m_schematic->Root() );
59 }
60
62 std::unique_ptr<SCHEMATIC> m_schematic;
65
66 std::string GetTestDataDir() { return KI_TEST::GetEeschemaTestDataDir() + "io/pcad/"; }
67
68 void LoadSchematic( const std::string& aFile )
69 {
70 m_loadedRoot = m_plugin.LoadSchematicFile( GetTestDataDir() + aFile, m_schematic.get() );
71 BOOST_REQUIRE( m_loadedRoot != nullptr );
72 }
73
75 {
76 std::set<SCH_SCREEN*> seenScreens;
77 int count = 0;
78
79 for( const SCH_SHEET_PATH& sheetPath : m_schematic->BuildUnorderedSheetList() )
80 {
81 SCH_SCREEN* screen = sheetPath.LastScreen();
82
83 if( !screen || !seenScreens.insert( screen ).second )
84 continue;
85
86 for( SCH_ITEM* item : screen->Items() )
87 {
88 if( item->Type() == aType )
89 count++;
90 }
91 }
92
93 return count;
94 }
95
97 {
98 std::set<SCH_SCREEN*> seenScreens;
99
100 for( const SCH_SHEET_PATH& sheetPath : m_schematic->BuildUnorderedSheetList() )
101 {
102 if( SCH_SCREEN* screen = sheetPath.LastScreen() )
103 seenScreens.insert( screen );
104 }
105
106 return static_cast<int>( seenScreens.size() );
107 }
108
110 {
111 int count = 0;
112
114 [&count]( SCH_ITEM* item, const SCH_SHEET_PATH& )
115 {
116 if( static_cast<SCH_LINE*>( item )->GetLayer() == LAYER_WIRE )
117 count++;
118 } );
119
120 return count;
121 }
122
124 {
125 int count = 0;
126
128 [&count]( SCH_ITEM* item, const SCH_SHEET_PATH& )
129 {
130 if( static_cast<SCH_LINE*>( item )->GetLayer() == LAYER_BUS )
131 count++;
132 } );
133
134 return count;
135 }
136
138 LIB_SYMBOL* LibSymbolForRefdes( const wxString& aRefdes )
139 {
140 LIB_SYMBOL* result = nullptr;
141
143 [&]( SCH_ITEM* item, const SCH_SHEET_PATH& sheetPath )
144 {
145 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
146
147 if( !result && symbol->GetRef( &sheetPath, false ) == aRefdes )
148 result = symbol->GetLibSymbolRef().get();
149 } );
150
151 return result;
152 }
153
154 SCH_SYMBOL* SymbolForRefdesUnit( const wxString& aRefdes, int aUnit )
155 {
156 SCH_SYMBOL* result = nullptr;
157
159 [&]( SCH_ITEM* item, const SCH_SHEET_PATH& sheetPath )
160 {
161 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
162
163 if( !result && symbol->GetRef( &sheetPath, false ) == aRefdes
164 && symbol->GetUnit() == aUnit )
165 {
166 result = symbol;
167 }
168 } );
169
170 return result;
171 }
172
174 SCH_PIN* LibPin( LIB_SYMBOL* aSymbol, const wxString& aNumber )
175 {
176 if( !aSymbol )
177 return nullptr;
178
179 for( SCH_PIN* pin : aSymbol->GetPins() )
180 {
181 if( pin->GetNumber() == aNumber )
182 return pin;
183 }
184
185 return nullptr;
186 }
187
188 bool HasLabel( const wxString& aText, KICAD_T aType = SCH_LABEL_T )
189 {
190 bool found = false;
191
192 forEachItemOfType( aType,
193 [&]( SCH_ITEM* item, const SCH_SHEET_PATH& )
194 {
195 if( static_cast<SCH_LABEL_BASE*>( item )->GetText() == aText )
196 found = true;
197 } );
198
199 return found;
200 }
201
202 SCH_TEXT* FindText( const wxString& aText )
203 {
204 SCH_TEXT* result = nullptr;
205
207 [&]( SCH_ITEM* item, const SCH_SHEET_PATH& )
208 {
209 if( !result && static_cast<SCH_TEXT*>( item )->GetText() == aText )
210 result = static_cast<SCH_TEXT*>( item );
211 } );
212
213 return result;
214 }
215
218 {
219 int count = 0;
220
222 [&]( SCH_ITEM* item, const SCH_SHEET_PATH& )
223 {
224 if( static_cast<SCH_SHAPE*>( item )->GetStroke().GetLineStyle() == aStyle )
225 count++;
226 } );
227
228 return count;
229 }
230
231 template <typename FUNC>
232 void forEachItemOfType( KICAD_T aType, FUNC aFunc )
233 {
234 std::set<SCH_SCREEN*> seenScreens;
235
236 for( const SCH_SHEET_PATH& sheetPath : m_schematic->BuildUnorderedSheetList() )
237 {
238 SCH_SCREEN* screen = sheetPath.LastScreen();
239
240 if( !screen || !seenScreens.insert( screen ).second )
241 continue;
242
243 for( SCH_ITEM* item : screen->Items().OfType( aType ) )
244 aFunc( item, sheetPath );
245 }
246 }
247};
248
249#endif // TEST_PCAD_SCH_IMPORT_FIXTURE_H
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:221
Define a library symbol object.
Definition lib_symbol.h:80
std::vector< SCH_PIN * > GetPins() const override
Holds all the data relating to one schematic.
Definition schematic.h:90
A SCH_IO derivation for loading P-CAD 2006 ASCII schematic files (.SCH).
Definition sch_io_pcad.h:49
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
int GetUnit() const
Definition sch_item.h:233
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:38
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:115
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
Schematic symbol object.
Definition sch_symbol.h:69
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:177
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
@ LAYER_WIRE
Definition layer_ids.h:450
@ LAYER_BUS
Definition layer_ids.h:451
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
LINE_STYLE
Dashed line types.
int CountShapesWithStyle(LINE_STYLE aStyle)
Count wire or graphic lines drawn with the given stroke style anywhere in the design.
void forEachItemOfType(KICAD_T aType, FUNC aFunc)
std::unique_ptr< SCHEMATIC > m_schematic
void LoadSchematic(const std::string &aFile)
SCH_TEXT * FindText(const wxString &aText)
SCH_PIN * LibPin(LIB_SYMBOL *aSymbol, const wxString &aNumber)
The library pin with the given number, searched across all units.
SCH_SYMBOL * SymbolForRefdesUnit(const wxString &aRefdes, int aUnit)
LIB_SYMBOL * LibSymbolForRefdes(const wxString &aRefdes)
The embedded library symbol of the first placed symbol matching the reference.
bool HasLabel(const wxString &aText, KICAD_T aType=SCH_LABEL_T)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
KIBIS_PIN * pin
wxString result
Test unit parsing edge cases and error handling.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:71
@ SCH_LINE_T
Definition typeinfo.h:160
@ SCH_SYMBOL_T
Definition typeinfo.h:169
@ SCH_LABEL_T
Definition typeinfo.h:164
@ SCH_SHAPE_T
Definition typeinfo.h:146
@ SCH_TEXT_T
Definition typeinfo.h:148