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
116 case SCH_TABLE_T:
117 {
118 SCH_TABLE* table = new SCH_TABLE( schIUScale.mmToIU( 0.1 ) );
119
120 table->SetColCount( 2 );
121
122 for( int ii = 0; ii < 4; ++ii )
123 table->InsertCell( ii, new SCH_TABLECELL() );
124
125 return table;
126 }
127
128 case SCH_LABEL_T: return new SCH_LABEL( VECTOR2I( 0, 0 ), "test label" );
129 case SCH_DIRECTIVE_LABEL_T: return new SCH_DIRECTIVE_LABEL( VECTOR2I( 0, 0 ) );
130 case SCH_GLOBAL_LABEL_T: return new SCH_GLOBALLABEL();
131 case SCH_HIER_LABEL_T: return new SCH_HIERLABEL();
132 case SCH_FIELD_T: return new SCH_FIELD( nullptr, FIELD_T::USER );
133 case SCH_SYMBOL_T: return new SCH_SYMBOL();
134
135 case SCH_SHEET_PIN_T:
136 // XXX: m_sheet pins currently have to have their initial positions calculated manually.
137 return new SCH_SHEET_PIN( &m_sheet,
138 VECTOR2I( m_sheet.GetPosition().x,
139 m_sheet.GetPosition().y + schIUScale.mmToIU( 40 ) ),
140 "test aPin" );
141
142 case SCH_SHEET_T: return new SCH_SHEET();
143 case SCH_PIN_T: return new SCH_PIN( &m_symbol );
144
145 case SCH_GROUP_T:
146 {
147 SCH_GROUP* group = new SCH_GROUP();
148
149 // Group position only makes sense if there's at least one item in the group.
150 group->AddItem( &m_text );
151
152 return group;
153 }
154
155 case SCHEMATIC_T:
156 case LIB_SYMBOL_T: return nullptr;
157
158 default:
159 BOOST_FAIL( wxString::Format(
160 "Unhandled type: %d "
161 "(if you created a new type you need to handle it in this switch statement)",
162 aType ) );
163 return nullptr;
164 }
165 }
166
167 static void CompareItems( EDA_ITEM* aItem, EDA_ITEM* aOriginalItem )
168 {
169 BOOST_CHECK_EQUAL( aItem->GetPosition(), aOriginalItem->GetPosition() );
171 aOriginalItem->GetBoundingBox().GetTop() );
173 aOriginalItem->GetBoundingBox().GetLeft() );
175 aOriginalItem->GetBoundingBox().GetBottom() );
177 aOriginalItem->GetBoundingBox().GetRight() );
178 }
179};
180
181
182BOOST_FIXTURE_TEST_SUITE( EeItem, TEST_EE_ITEM_FIXTURE )
183
184
186{
187 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
188 {
189 KICAD_T type = static_cast<KICAD_T>( i );
190
191 auto item = std::unique_ptr<EDA_ITEM>( Instantiate( type ) );
192
193 if( item == nullptr )
194 continue;
195
196 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
197 {
199 item.get(),
200 []( EDA_ITEM* aOriginalItem, VECTOR2I aRef )
201 {
202 auto item = std::unique_ptr<EDA_ITEM>( aOriginalItem->Clone() );
203 VECTOR2I originalPos = item->GetPosition();
204
205 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item.get() );
206
207 // Move to a point, then go back.
208 // This has to be an identity transformation.
209
210 if( schItem != nullptr )
211 {
212 schItem->Move( aRef );
213 BOOST_CHECK_EQUAL( schItem->GetPosition(), originalPos + aRef );
214
215 schItem->Move( -aRef );
216 }
217
218 CompareItems( item.get(), aOriginalItem );
219 } );
220 }
221 }
222}
223
224
226{
227 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
228 {
229 KICAD_T type = static_cast<KICAD_T>( i );
230
231 auto item = std::unique_ptr<EDA_ITEM>( Instantiate( type ) );
232
233 if( item == nullptr )
234 continue;
235
236 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
237 {
238 // Four equivalent 90 degree rotations are an identity.
239 // (warning: only for items having no autoplaced fields).
240
241 if( item->GetClass() == "SCH_SHEET_PIN" )
242 {
243 auto newItem = std::unique_ptr<EDA_ITEM>( item->Clone() );
244
245 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( newItem.get() );
246
247 if( schItem != nullptr )
248 {
250 // Only rotating pins around the center of parent sheet works.
251 schItem->Rotate( m_sheet.GetBodyBoundingBox().GetCenter(), false );
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 }
256
257 CompareItems( newItem.get(), item.get() );
258 }
259 else
260 {
262 item.get(),
263 []( EDA_ITEM* aOriginalItem, VECTOR2I aRef )
264 {
265 auto item = std::unique_ptr<EDA_ITEM>( aOriginalItem->Clone() );
266
267 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item.get() );
268
269 if( schItem != nullptr )
270 {
271 schItem->SetFieldsAutoplaced( AUTOPLACE_NONE );
272 schItem->Rotate( aRef, false );
273 schItem->Rotate( aRef, false );
274 schItem->Rotate( aRef, false );
275 schItem->Rotate( aRef, false );
276 }
277
278 CompareItems( item.get(), aOriginalItem );
279 } );
280 }
281 }
282 }
283}
284
285
286BOOST_AUTO_TEST_CASE( RotateTextRoundTrip )
287{
288 const VECTOR2I textPos( 1000, 2000 );
289 const VECTOR2I ref( 5000, 7000 );
290
292 {
293 BOOST_TEST_CONTEXT( "H justify: " << static_cast<int>( hJustify ) )
294 {
295 SCH_TEXT text( textPos, "test text" );
296 text.SetTextAngle( ANGLE_HORIZONTAL );
297 text.SetHorizJustify( hJustify );
298
299 // A clockwise rotation followed by a counter-clockwise one is an identity
300 text.Rotate( ref, false );
301 text.Rotate( ref, true );
302
303 BOOST_CHECK( text.GetTextAngle() == ANGLE_HORIZONTAL );
304 BOOST_CHECK_EQUAL( static_cast<int>( text.GetHorizJustify() ), static_cast<int>( hJustify ) );
305 BOOST_CHECK_EQUAL( text.GetTextPos(), textPos );
306 }
307 }
308}
309
310
311BOOST_AUTO_TEST_CASE( MirrorHorizontally )
312{
313 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
314 {
315 KICAD_T type = static_cast<KICAD_T>( i );
316
317 auto item = std::unique_ptr<EDA_ITEM>( Instantiate( type ) );
318
319 if( item == nullptr )
320 continue;
321
322 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
323 {
325 item.get(),
326 []( EDA_ITEM* aOriginalItem, VECTOR2I aRef )
327 {
328 auto item = std::unique_ptr<EDA_ITEM>( aOriginalItem->Clone() );
329
330 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item.get() );
331
332 // Two mirrorings are an identity
333 // (warning: only for text items having no autoplaced fields).
334 if( schItem != nullptr )
335 {
336 schItem->SetFieldsAutoplaced( AUTOPLACE_NONE );
337 schItem->MirrorHorizontally( aRef.x );
338 schItem->MirrorHorizontally( aRef.x );
339 }
340
341 CompareItems( item.get(), aOriginalItem );
342 } );
343 }
344 }
345}
346
347
348BOOST_AUTO_TEST_CASE( MirrorVertically )
349{
350 for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ )
351 {
352 KICAD_T type = static_cast<KICAD_T>( i );
353
354 auto item = std::unique_ptr<EDA_ITEM>( Instantiate( type ) );
355
356 if( item == nullptr )
357 continue;
358
359 BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
360 {
362 item.get(),
363 []( EDA_ITEM* aOriginalItem, VECTOR2I aRef )
364 {
365 auto item = std::unique_ptr<EDA_ITEM>( aOriginalItem->Clone() );
366
367 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item.get() );
368
369 // Two mirrorings are an identity
370 // (warning only for text items having no autoplaced fields).
371
372 if( schItem != nullptr )
373 {
374 schItem->SetFieldsAutoplaced( AUTOPLACE_NONE );
375 schItem->MirrorVertically( aRef.y );
376 schItem->MirrorVertically( aRef.y );
377 }
378
379 CompareItems( item.get(), aOriginalItem );
380 } );
381 }
382 }
383}
384
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:130
constexpr coord_type GetLeft() const
Definition box2.h:231
constexpr coord_type GetRight() const
Definition box2.h:220
constexpr coord_type GetTop() const
Definition box2.h:232
constexpr coord_type GetBottom() const
Definition box2.h:225
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
virtual VECTOR2I GetPosition() const
Definition eda_item.h:348
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:280
virtual void SetPolyShape(const SHAPE_POLY_SET &aShape)
Definition eda_shape.h:529
Define a library symbol object.
Definition lib_symbol.h:114
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:170
void SetFieldsAutoplaced(AUTOPLACE_ALGO aAlgo)
Definition sch_item.h:679
virtual void Rotate(const VECTOR2I &aCenter, bool aRotateCCW)
Rotate the item around aCenter 90 degrees in the clockwise direction.
Definition sch_item.h:446
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:39
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:50
Schematic symbol object.
Definition sch_symbol.h:76
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 constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition eda_angle.h:468
@ NO_FILL
Definition eda_fill.h:30
static void IterateOverPositionsAndReferences(T *aItem, void(*aCallback)(T *, VECTOR2I))
@ ERCE_DRIVER_CONFLICT
Conflicting drivers (labels, etc) on a subgraph.
@ LAYER_NOTES
Definition layer_ids.h:498
Class to handle a set of SCH_ITEMs.
@ AUTOPLACE_NONE
Definition sch_item.h:74
@ USER
The field ID hasn't been set yet; field is invalid.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
const std::string table
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_AUTO_TEST_CASE(Move)
BOOST_AUTO_TEST_SUITE_END()
BOOST_TEST_CONTEXT("Test Clearance")
GR_TEXT_H_ALIGN_T
This is API surface mapped to common.types.HorizontalAlignment.
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:70
@ SCH_GROUP_T
Definition typeinfo.h:169
@ SCH_TABLE_T
Definition typeinfo.h:161
@ SCH_LINE_T
Definition typeinfo.h:159
@ LIB_SYMBOL_T
Definition typeinfo.h:144
@ SCH_NO_CONNECT_T
Definition typeinfo.h:156
@ MAX_STRUCT_TYPE_ID
Definition typeinfo.h:245
@ SCH_SYMBOL_T
Definition typeinfo.h:168
@ SCH_FIELD_T
Definition typeinfo.h:146
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:167
@ SCH_LABEL_T
Definition typeinfo.h:163
@ SCH_SHEET_T
Definition typeinfo.h:171
@ SCH_MARKER_T
Definition typeinfo.h:154
@ SCH_SHAPE_T
Definition typeinfo.h:145
@ SCH_RULE_AREA_T
Definition typeinfo.h:166
@ SCH_HIER_LABEL_T
Definition typeinfo.h:165
@ SCH_BUS_BUS_ENTRY_T
Definition typeinfo.h:158
@ SCHEMATIC_T
Definition typeinfo.h:200
@ SCH_SHEET_PIN_T
Definition typeinfo.h:170
@ SCH_TEXT_T
Definition typeinfo.h:147
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:157
@ SCH_BITMAP_T
Definition typeinfo.h:160
@ SCH_TEXTBOX_T
Definition typeinfo.h:148
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:164
@ SCH_JUNCTION_T
Definition typeinfo.h:155
@ SCH_PIN_T
Definition typeinfo.h:149
constexpr bool IsInstantiableType(const KICAD_T aType)
Definition typeinfo.h:333
constexpr bool IsEeschemaType(const KICAD_T aType)
Definition typeinfo.h:403
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708