KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_ee_item.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 <properties/property.h>
22#include <eda_item_test_utils.h>
23#include <core/typeinfo.h>
24
25#include <eda_item.h>
26#include <sch_item.h>
27
28#include <sch_marker.h>
29#include <sch_junction.h>
30#include <sch_no_connect.h>
31#include <sch_bus_entry.h>
32#include <sch_line.h>
33#include <sch_rule_area.h>
34#include <sch_shape.h>
35#include <sch_bitmap.h>
36#include <sch_text.h>
37#include <sch_label.h>
38#include <sch_textbox.h>
39#include <sch_table.h>
40#include <sch_tablecell.h>
41#include <sch_field.h>
42#include <sch_group.h>
43#include <sch_symbol.h>
44#include <sch_sheet_pin.h>
45#include <sch_sheet.h>
46#include <sch_pin.h>
47
48#include <erc/erc_settings.h>
50
52{
53public:
58 std::shared_ptr<ERC_ITEM> m_ercItem;
59
61 m_sheet(),
62 m_symbol( "test symbol" ),
63 m_pin( &m_symbol ),
65 {
66 m_sheet.SetPosition( VECTOR2I( schIUScale.mmToIU( 5 ), schIUScale.mmToIU( 10 ) ) );
67 m_sheet.SetSize( VECTOR2I( schIUScale.mmToIU( 50 ), schIUScale.mmToIU( 100 ) ) );
68 }
69
71 {
72 m_text.SetParentGroup( nullptr );
73 }
74
76 {
77 if( !IsEeschemaType( aType ) )
78 return nullptr;
79
80 if( !IsInstantiableType( aType ) )
81 return nullptr;
82
83 switch( aType )
84 {
85 case SCH_MARKER_T: return new SCH_MARKER( m_ercItem, VECTOR2I( 0, 0 ) );
86 case SCH_JUNCTION_T: return new SCH_JUNCTION();
87 case SCH_NO_CONNECT_T: return new SCH_NO_CONNECT();
89 case SCH_BUS_BUS_ENTRY_T: return new SCH_BUS_BUS_ENTRY();
90 case SCH_LINE_T: return new SCH_LINE();
91
92 case SCH_RULE_AREA_T:
93 {
94 SHAPE_POLY_SET ruleShape;
95
96 ruleShape.NewOutline();
97 auto& outline = ruleShape.Outline( 0 );
98 outline.Append( VECTOR2I( 20000, 20000) );
99 outline.Append( VECTOR2I( 22000, 20000) );
100 outline.Append( VECTOR2I( 22000, 22000) );
101 outline.Append( VECTOR2I( 20000, 22000) );
102 outline.SetClosed( true );
103 outline.Simplify( true );
104
105 SCH_RULE_AREA* ruleArea = new SCH_RULE_AREA();
106 ruleArea->SetPolyShape( ruleShape );
107
108 return ruleArea;
109 }
110
111 case SCH_SHAPE_T: return new SCH_SHAPE( SHAPE_T::ARC, LAYER_NOTES );
112 case SCH_BITMAP_T: return new SCH_BITMAP();
113 case SCH_TEXT_T: return new SCH_TEXT( VECTOR2I( 0, 0 ), "test text" );
114 case SCH_TEXTBOX_T: return new SCH_TEXTBOX( LAYER_NOTES, 0, FILL_T::NO_FILL, "test textbox" );
115 case SCH_TABLECELL_T: return new SCH_TABLECELL();
116
117 case SCH_TABLE_T:
118 {
119 SCH_TABLE* table = new SCH_TABLE( schIUScale.mmToIU( 0.1 ) );
120
121 table->SetColCount( 2 );
122
123 for( int ii = 0; ii < 4; ++ii )
124 table->InsertCell( ii, new SCH_TABLECELL() );
125
126 return table;
127 }
128
129 case SCH_LABEL_T: return new SCH_LABEL( VECTOR2I( 0, 0 ), "test label" );
130 case SCH_DIRECTIVE_LABEL_T: return new SCH_DIRECTIVE_LABEL( VECTOR2I( 0, 0 ) );
131 case SCH_GLOBAL_LABEL_T: return new SCH_GLOBALLABEL();
132 case SCH_HIER_LABEL_T: return new SCH_HIERLABEL();
133 case SCH_FIELD_T: return new SCH_FIELD( nullptr, FIELD_T::USER );
134 case SCH_SYMBOL_T: return new SCH_SYMBOL();
135
136 case SCH_SHEET_PIN_T:
137 // XXX: m_sheet pins currently have to have their initial positions calculated manually.
138 return new SCH_SHEET_PIN( &m_sheet,
139 VECTOR2I( m_sheet.GetPosition().x,
140 m_sheet.GetPosition().y + schIUScale.mmToIU( 40 ) ),
141 "test aPin" );
142
143 case SCH_SHEET_T: return new SCH_SHEET();
144 case SCH_PIN_T: return new SCH_PIN( &m_symbol );
145
146 case SCH_GROUP_T:
147 {
148 SCH_GROUP* group = new SCH_GROUP();
149
150 // Group position only makes sense if there's at least one item in the group.
151 group->AddItem( &m_text );
152
153 return group;
154 }
155
156 case SCHEMATIC_T:
157 case LIB_SYMBOL_T: return nullptr;
158
159 default:
160 BOOST_FAIL( wxString::Format(
161 "Unhandled type: %d "
162 "(if you created a new type you need to handle it in this switch statement)",
163 aType ) );
164 return nullptr;
165 }
166 }
167
168 static void CompareItems( EDA_ITEM* aItem, EDA_ITEM* aOriginalItem )
169 {
170 BOOST_CHECK_EQUAL( aItem->GetPosition(), aOriginalItem->GetPosition() );
172 aOriginalItem->GetBoundingBox().GetTop() );
174 aOriginalItem->GetBoundingBox().GetLeft() );
176 aOriginalItem->GetBoundingBox().GetBottom() );
178 aOriginalItem->GetBoundingBox().GetRight() );
179 }
180};
181
182
183BOOST_FIXTURE_TEST_SUITE( EeItem, TEST_EE_ITEM_FIXTURE )
184
185
187{
188 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
189 {
190 KICAD_T type = static_cast<KICAD_T>( i );
191
192 auto item = std::unique_ptr<EDA_ITEM>( Instantiate( type ) );
193
194 if( item == nullptr )
195 continue;
196
197 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
198 {
200 item.get(),
201 []( EDA_ITEM* aOriginalItem, VECTOR2I aRef )
202 {
203 auto item = std::unique_ptr<EDA_ITEM>( aOriginalItem->Clone() );
204 VECTOR2I originalPos = item->GetPosition();
205
206 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item.get() );
207
208 // Move to a point, then go back.
209 // This has to be an identity transformation.
210
211 if( schItem != nullptr )
212 {
213 schItem->Move( aRef );
214 BOOST_CHECK_EQUAL( schItem->GetPosition(), originalPos + aRef );
215
216 schItem->Move( -aRef );
217 }
218
219 CompareItems( item.get(), aOriginalItem );
220 } );
221 }
222 }
223}
224
225
227{
228 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
229 {
230 KICAD_T type = static_cast<KICAD_T>( i );
231
232 auto item = std::unique_ptr<EDA_ITEM>( Instantiate( type ) );
233
234 if( item == nullptr )
235 continue;
236
237 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
238 {
239 // Four equivalent 90 degree rotations are an identity.
240 // (warning: only for items having no autoplaced fields).
241
242 if( item->GetClass() == "SCH_SHEET_PIN" )
243 {
244 auto newItem = std::unique_ptr<EDA_ITEM>( item->Clone() );
245
246 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( newItem.get() );
247
248 if( schItem != nullptr )
249 {
251 // Only rotating pins around the center of parent sheet works.
252 schItem->Rotate( m_sheet.GetBodyBoundingBox().GetCenter(), false );
253 schItem->Rotate( m_sheet.GetBodyBoundingBox().GetCenter(), false );
254 schItem->Rotate( m_sheet.GetBodyBoundingBox().GetCenter(), false );
255 schItem->Rotate( m_sheet.GetBodyBoundingBox().GetCenter(), false );
256 }
257
258 CompareItems( newItem.get(), item.get() );
259 }
260 else
261 {
263 item.get(),
264 []( EDA_ITEM* aOriginalItem, VECTOR2I aRef )
265 {
266 auto item = std::unique_ptr<EDA_ITEM>( aOriginalItem->Clone() );
267
268 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item.get() );
269
270 if( schItem != nullptr )
271 {
272 schItem->SetFieldsAutoplaced( AUTOPLACE_NONE );
273 schItem->Rotate( aRef, false );
274 schItem->Rotate( aRef, false );
275 schItem->Rotate( aRef, false );
276 schItem->Rotate( aRef, false );
277 }
278
279 CompareItems( item.get(), aOriginalItem );
280 } );
281 }
282 }
283 }
284}
285
286
287BOOST_AUTO_TEST_CASE( MirrorHorizontally )
288{
289 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
290 {
291 KICAD_T type = static_cast<KICAD_T>( i );
292
293 auto item = std::unique_ptr<EDA_ITEM>( Instantiate( type ) );
294
295 if( item == nullptr )
296 continue;
297
298 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
299 {
301 item.get(),
302 []( EDA_ITEM* aOriginalItem, VECTOR2I aRef )
303 {
304 auto item = std::unique_ptr<EDA_ITEM>( aOriginalItem->Clone() );
305
306 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item.get() );
307
308 // Two mirrorings are an identity
309 // (warning: only for text items having no autoplaced fields).
310 if( schItem != nullptr )
311 {
312 schItem->SetFieldsAutoplaced( AUTOPLACE_NONE );
313 schItem->MirrorHorizontally( aRef.x );
314 schItem->MirrorHorizontally( aRef.x );
315 }
316
317 CompareItems( item.get(), aOriginalItem );
318 } );
319 }
320 }
321}
322
323
324BOOST_AUTO_TEST_CASE( MirrorVertically )
325{
326 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
327 {
328 KICAD_T type = static_cast<KICAD_T>( i );
329
330 auto item = std::unique_ptr<EDA_ITEM>( Instantiate( type ) );
331
332 if( item == nullptr )
333 continue;
334
335 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
336 {
338 item.get(),
339 []( EDA_ITEM* aOriginalItem, VECTOR2I aRef )
340 {
341 auto item = std::unique_ptr<EDA_ITEM>( aOriginalItem->Clone() );
342
343 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item.get() );
344
345 // Two mirrorings are an identity
346 // (warning only for text items having no autoplaced fields).
347
348 if( schItem != nullptr )
349 {
350 schItem->SetFieldsAutoplaced( AUTOPLACE_NONE );
351 schItem->MirrorVertically( aRef.y );
352 schItem->MirrorVertically( aRef.y );
353 }
354
355 CompareItems( item.get(), aOriginalItem );
356 } );
357 }
358 }
359}
360
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
constexpr coord_type GetLeft() const
Definition box2.h:224
constexpr coord_type GetRight() const
Definition box2.h:213
constexpr coord_type GetTop() const
Definition box2.h:225
constexpr coord_type GetBottom() const
Definition box2.h:218
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
virtual VECTOR2I GetPosition() const
Definition eda_item.h:282
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:135
virtual void SetPolyShape(const SHAPE_POLY_SET &aShape)
Definition eda_shape.h:427
Define a library symbol object.
Definition lib_symbol.h:79
Object to handle a bitmap image that can be inserted in a schematic.
Definition sch_bitmap.h:36
Class for a bus to bus entry.
Class for a wire to bus entry.
A set of SCH_ITEMs (i.e., without duplicates).
Definition sch_group.h:48
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
void SetFieldsAutoplaced(AUTOPLACE_ALGO aAlgo)
Definition sch_item.h:624
virtual void Rotate(const VECTOR2I &aCenter, bool aRotateCCW)
Rotate the item around aCenter 90 degrees in the clockwise direction.
Definition sch_item.h:420
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:38
Define a sheet pin (label) used in sheets to create hierarchical schematics.
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
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
Represent a set of closed polygons.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int NewOutline()
Creates a new empty polygon in the set and returns its index.
EDA_ITEM * Instantiate(KICAD_T aType)
static void CompareItems(EDA_ITEM *aItem, EDA_ITEM *aOriginalItem)
std::shared_ptr< ERC_ITEM > m_ercItem
static void IterateOverPositionsAndReferences(T *aItem, void(*aCallback)(T *, VECTOR2I))
@ NO_FILL
Definition eda_shape.h:60
@ ERCE_DRIVER_CONFLICT
Conflicting drivers (labels, etc) on a subgraph.
@ LAYER_NOTES
Definition layer_ids.h:465
Class to handle a set of SCH_ITEMs.
@ AUTOPLACE_NONE
Definition sch_item.h:66
@ USER
The field ID hasn't been set yet; field is invalid.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(Move)
BOOST_AUTO_TEST_SUITE_END()
std::vector< std::vector< std::string > > table
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_CHECK_EQUAL(result, "25.4")
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:71
@ SCH_GROUP_T
Definition typeinfo.h:170
@ SCH_TABLE_T
Definition typeinfo.h:162
@ SCH_LINE_T
Definition typeinfo.h:160
@ LIB_SYMBOL_T
Definition typeinfo.h:145
@ SCH_NO_CONNECT_T
Definition typeinfo.h:157
@ MAX_STRUCT_TYPE_ID
Definition typeinfo.h:240
@ SCH_SYMBOL_T
Definition typeinfo.h:169
@ SCH_TABLECELL_T
Definition typeinfo.h:163
@ SCH_FIELD_T
Definition typeinfo.h:147
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:168
@ SCH_LABEL_T
Definition typeinfo.h:164
@ SCH_SHEET_T
Definition typeinfo.h:172
@ SCH_MARKER_T
Definition typeinfo.h:155
@ SCH_SHAPE_T
Definition typeinfo.h:146
@ SCH_RULE_AREA_T
Definition typeinfo.h:167
@ SCH_HIER_LABEL_T
Definition typeinfo.h:166
@ SCH_BUS_BUS_ENTRY_T
Definition typeinfo.h:159
@ SCHEMATIC_T
Definition typeinfo.h:201
@ SCH_SHEET_PIN_T
Definition typeinfo.h:171
@ SCH_TEXT_T
Definition typeinfo.h:148
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:158
@ SCH_BITMAP_T
Definition typeinfo.h:161
@ SCH_TEXTBOX_T
Definition typeinfo.h:149
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:165
@ SCH_JUNCTION_T
Definition typeinfo.h:156
@ SCH_PIN_T
Definition typeinfo.h:150
constexpr bool IsInstantiableType(const KICAD_T aType)
Definition typeinfo.h:316
constexpr bool IsEeschemaType(const KICAD_T aType)
Definition typeinfo.h:378
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683