KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_ee_grid_helper.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 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#define BOOST_TEST_NO_MAIN
19#include <boost/test/unit_test.hpp>
20
22#include <lib_symbol.h>
23#include <sch_text.h>
24#include <sch_line.h>
25#include <sch_pin.h>
26#include <sch_shape.h>
27#include <sch_junction.h>
28#include <sch_symbol.h>
29#include <layer_ids.h>
30
32{
33public:
34 static BOX2I LayoutBounds( const SCH_ITEM& aItem ) { return EE_GRID_HELPER::layoutBounds( aItem ); }
35
36 static std::vector<std::pair<const SCH_PIN*, VECTOR2I>> LayoutPins( SCH_ITEM& aItem )
37 {
38 return EE_GRID_HELPER::layoutPins( aItem );
39 }
40};
41
42BOOST_AUTO_TEST_SUITE( EEGridHelperTest )
43
44BOOST_AUTO_TEST_CASE( ItemGridClassification )
45{
46 EE_GRID_HELPER helper;
47
50
51 SCH_LINE wire( VECTOR2I( 0, 0 ), LAYER_WIRE );
52 BOOST_CHECK_EQUAL( helper.GetItemGrid( &wire ), GRID_WIRES );
53
54 SCH_LINE graphic( VECTOR2I( 0, 0 ), LAYER_NOTES );
55 BOOST_CHECK_EQUAL( helper.GetItemGrid( &graphic ), GRID_GRAPHICS );
56
57 SCH_JUNCTION junc;
58 BOOST_CHECK_EQUAL( helper.GetItemGrid( &junc ), GRID_WIRES );
59}
60
61
62namespace
63{
64std::unique_ptr<LIB_SYMBOL> MakeTwoPinSymbol()
65{
66 auto libSymbol = std::make_unique<LIB_SYMBOL>( "R", nullptr );
67
68 for( const auto& [number, position] : { std::pair( "1", VECTOR2I( -508000, 0 ) ),
69 std::pair( "2", VECTOR2I( 508000, 0 ) ) } )
70 {
71 SCH_PIN* pin = new SCH_PIN( libSymbol.get() );
72 pin->SetNumber( number );
74 pin->SetPosition( position );
75 libSymbol->AddDrawItem( pin );
76 }
77
78 return libSymbol;
79}
80} // namespace
81
82
83BOOST_AUTO_TEST_CASE( SymbolLayoutBoundsIgnoreFields )
84{
85 std::unique_ptr<LIB_SYMBOL> libSymbol = MakeTwoPinSymbol();
86 SCH_SYMBOL symbol( *libSymbol, libSymbol->GetLibId(), nullptr, 0, 0, VECTOR2I( 0, 0 ) );
87
88 symbol.GetField( FIELD_T::REFERENCE )->SetPosition( VECTOR2I( 0, 50000000 ) );
89
90 BOOST_CHECK( EEGridHelperTestFixture::LayoutBounds( symbol ) == symbol.GetBodyAndPinsBoundingBox() );
91 BOOST_CHECK( EEGridHelperTestFixture::LayoutBounds( symbol ) != symbol.GetBoundingBox() );
92}
93
94
95BOOST_AUTO_TEST_CASE( SymbolLayoutPinsReportEveryPin )
96{
97 std::unique_ptr<LIB_SYMBOL> libSymbol = MakeTwoPinSymbol();
98 SCH_SYMBOL symbol( *libSymbol, libSymbol->GetLibId(), nullptr, 0, 0, VECTOR2I( 2540000, 1270000 ) );
99 symbol.UpdatePins();
100
101 std::vector<std::pair<const SCH_PIN*, VECTOR2I>> pins = EEGridHelperTestFixture::LayoutPins( symbol );
102 BOOST_REQUIRE_EQUAL( pins.size(), 2 );
103
104 for( const auto& [pin, position] : pins )
105 BOOST_CHECK_EQUAL( position, pin->GetPosition() );
106
107 // A bare pin is its own alignment anchor in the symbol editor.
108 SCH_PIN standalone( libSymbol.get() );
109 standalone.SetPosition( VECTOR2I( 1000, 2000 ) );
110 BOOST_REQUIRE_EQUAL( EEGridHelperTestFixture::LayoutPins( standalone ).size(), 1 );
111 BOOST_CHECK_EQUAL( EEGridHelperTestFixture::LayoutPins( standalone ).front().second, VECTOR2I( 1000, 2000 ) );
112}
113
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
static std::vector< std::pair< const SCH_PIN *, VECTOR2I > > LayoutPins(SCH_ITEM &aItem)
static BOX2I LayoutBounds(const SCH_ITEM &aItem)
static std::vector< std::pair< const SCH_PIN *, VECTOR2I > > layoutPins(SCH_ITEM &aItem)
Pin connection points the item offers as alignment anchors.
GRID_HELPER_GRIDS GetItemGrid(const EDA_ITEM *aItem) const override
Get the coarsest grid that applies to an item.
static BOX2I layoutBounds(const SCH_ITEM &aItem)
Bounds a layout relation should align to.
void SetPosition(const VECTOR2I &aPosition) override
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:38
void SetPosition(const VECTOR2I &aPos) override
Definition sch_pin.h:300
Schematic symbol object.
Definition sch_symbol.h:69
BOX2I GetBodyAndPinsBoundingBox() const override
Return a bounding box for the symbol body and pins but not the fields.
void UpdatePins()
Updates the cache of SCH_PIN objects for each pin.
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
@ GRID_TEXT
Definition grid_helper.h:62
@ GRID_GRAPHICS
Definition grid_helper.h:63
@ GRID_WIRES
Definition grid_helper.h:60
@ LAYER_WIRE
Definition layer_ids.h:458
@ LAYER_NOTES
Definition layer_ids.h:473
@ PT_PASSIVE
pin for passive symbols: must be connected, and can be connected to any pin.
Definition pin_type.h:39
@ REFERENCE
Field Reference of part, i.e. "IC21".
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(ItemGridClassification)
BOOST_AUTO_TEST_SUITE_END()
KIBIS_PIN * pin
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683