KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_footprint_net_tie.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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
21
22#include <vector>
23
24#include <base_units.h>
25#include <board.h>
26#include <footprint.h>
27#include <footprint_utils.h>
28#include <layer_ids.h>
29#include <pad.h>
30#include <padstack.h>
31#include <pcb_shape.h>
32
33
34BOOST_AUTO_TEST_SUITE( FootprintNetTieCheck )
35
36namespace
37{
45struct FP_LEVEL_NET_TIE_FIXTURE
46{
47 BOARD m_board;
48 FOOTPRINT m_footprint;
49
50 FP_LEVEL_NET_TIE_FIXTURE() :
51 m_footprint( &m_board )
52 {
53 m_board.SetBoardUse( BOARD_USE::FPHOLDER );
54
55 // Start with the default stackup mode, which expands inner layers automatically.
56 m_footprint.SetStackupMode( FOOTPRINT_STACKUP::EXPAND_INNER_LAYERS );
57 }
58
59
60 void setCustomStackup( int numCuLayers )
61 {
62 m_footprint.SetStackupMode( FOOTPRINT_STACKUP::CUSTOM_LAYERS );
63 m_footprint.SetStackupLayers( LSET::AllCuMask( numCuLayers ) );
64 }
65
71 void addNonComponentPad( const wxString& aNumber, const VECTOR2I& aPos, PCB_LAYER_ID aLayer )
72 {
73 const VECTOR2I padSize( pcbIUScale.mmToIU( 1.0 ), pcbIUScale.mmToIU( 1.0 ) );
74
75 PAD* pad = new PAD( &m_footprint );
76
77 pad->SetNumber( aNumber );
78 pad->SetAttribute( PAD_ATTRIB::NON_COMPONENT );
80 pad->SetSize( PADSTACK::ALL_LAYERS, padSize );
81 pad->SetLayerSet( LSET( { aLayer } ) );
82 pad->SetPosition( aPos );
83
84 m_footprint.Add( pad, ADD_MODE::APPEND );
85 }
86
87 void addCopperLine( const VECTOR2I& aStart, const VECTOR2I& aEnd, PCB_LAYER_ID aLayer )
88 {
89 PCB_SHAPE* line = new PCB_SHAPE( &m_footprint, SHAPE_T::SEGMENT );
90
91 line->SetStart( aStart );
92 line->SetEnd( aEnd );
93 line->SetWidth( pcbIUScale.mmToIU( 0.25 ) );
94 line->SetLayer( aLayer );
95
96 m_footprint.Add( line, ADD_MODE::APPEND );
97 }
98
103 void buildTwoPadTie( PCB_LAYER_ID aLayer )
104 {
105 const VECTOR2I padAPos( pcbIUScale.mmToIU( 90.0 ), pcbIUScale.mmToIU( 0.0 ) );
106 const VECTOR2I padBPos( pcbIUScale.mmToIU( 110.0 ), pcbIUScale.mmToIU( 0.0 ) );
107
108 addNonComponentPad( wxS( "1" ), padAPos, aLayer );
109 addNonComponentPad( wxS( "2" ), padBPos, aLayer );
110 addCopperLine( padAPos, padBPos, aLayer );
111 }
112
113 struct SHORT_INFO
114 {
115 const BOARD_ITEM* padA;
116 const BOARD_ITEM* padB;
117 const BOARD_ITEM* copper;
118 VECTOR2I pos;
119 };
120
124 std::vector<SHORT_INFO> getShortedItems()
125 {
126 std::vector<SHORT_INFO> shorts;
127
128 const auto onShort =
129 [&]( const BOARD_ITEM* padA, const BOARD_ITEM* padB, const BOARD_ITEM* copper, const VECTOR2I& pos )
130 {
131 shorts.emplace_back( SHORT_INFO{ padA, padB, copper, pos } );
132 };
133
134 FOOTPRINT_UTILS::CheckNetTies( m_footprint, onShort );
135
136 return shorts;
137 }
138
139 size_t countShortedItems()
140 {
141 return getShortedItems().size();
142 }
143
144 const PAD& expectPadWithNumber( const wxString& aNumber )
145 {
146 for( const PAD* pad : m_footprint.Pads() )
147 {
148 if( pad->GetNumber() == aNumber )
149 return *pad;
150 }
151 throw std::runtime_error( "Pad with number " + std::string( aNumber.mb_str() ) + " not found" );
152 }
153
154 const PCB_SHAPE& expectCopperLine()
155 {
156 for( const BOARD_ITEM* shape : m_footprint.GraphicalItems() )
157 {
158 if( shape->Type() == PCB_SHAPE_T )
159 return *static_cast<const PCB_SHAPE*>( shape );
160 }
161 throw std::runtime_error( "Copper line not found" );
162 }
163};
164
165} // namespace
166
167
171BOOST_FIXTURE_TEST_CASE( PadsInDifferentGroupsOnOuterLayerExpandingStackup, FP_LEVEL_NET_TIE_FIXTURE )
172{
173 buildTwoPadTie( F_Cu );
174
175 const std::vector<FP_LEVEL_NET_TIE_FIXTURE::SHORT_INFO> shorts = getShortedItems();
176
177 BOOST_REQUIRE_EQUAL( shorts.size(), 1 );
178
179 // Check that the short involves two distinct pads and a copper line.
180 BOOST_CHECK_NE( shorts[0].padA, nullptr );
181 BOOST_CHECK_NE( shorts[0].padB, nullptr );
182 BOOST_CHECK_NE( shorts[0].padA, shorts[0].padB );
183
184 BOOST_CHECK_NE( shorts[0].copper, &expectCopperLine() );
185
186 BOOST_CHECK( m_footprint.GetBoundingBox().Contains( shorts[0].pos ) );
187}
188
189
194BOOST_FIXTURE_TEST_CASE( PadsInDifferentGroupsOnInnerLayerExpandingStackup, FP_LEVEL_NET_TIE_FIXTURE )
195{
196 // Expanding stackups can handle In1 (only)
197 buildTwoPadTie( In1_Cu );
198
199 const std::vector<FP_LEVEL_NET_TIE_FIXTURE::SHORT_INFO> shorts = getShortedItems();
200
201 BOOST_REQUIRE_EQUAL( shorts.size(), 1 );
202
203 // Check that the short involves two distinct pads and a copper line.
204 BOOST_CHECK_NE( shorts[0].padA, nullptr );
205 BOOST_CHECK_NE( shorts[0].padB, nullptr );
206 BOOST_CHECK_NE( shorts[0].padA, shorts[0].padB );
207
208 BOOST_CHECK_NE( shorts[0].copper, &expectCopperLine() );
209
210 BOOST_CHECK( m_footprint.GetBoundingBox().Contains( shorts[0].pos ) );
211}
212
213
218BOOST_FIXTURE_TEST_CASE( PadsInDifferentGroupsOnInnerLayerCustomStackup, FP_LEVEL_NET_TIE_FIXTURE )
219{
220 setCustomStackup( 4 );
221 buildTwoPadTie( In2_Cu );
222
223 const std::vector<FP_LEVEL_NET_TIE_FIXTURE::SHORT_INFO> shorts = getShortedItems();
224
225 BOOST_REQUIRE_EQUAL( shorts.size(), 1 );
226
227 // Check that the short involves two distinct pads and a copper line.
228 BOOST_CHECK_NE( shorts[0].padA, nullptr );
229 BOOST_CHECK_NE( shorts[0].padB, nullptr );
230 BOOST_CHECK_NE( shorts[0].padA, shorts[0].padB );
231
232 BOOST_CHECK_NE( shorts[0].copper, &expectCopperLine() );
233
234 BOOST_CHECK( m_footprint.GetBoundingBox().Contains( shorts[0].pos ) );
235}
236
237
244BOOST_FIXTURE_TEST_CASE( PadsInSameGroupAreAllowed, FP_LEVEL_NET_TIE_FIXTURE )
245{
246 setCustomStackup( 4 );
247 buildTwoPadTie( In2_Cu );
248
249 m_footprint.AddNetTiePadGroup( wxS( "1, 2" ) );
250
251 BOOST_CHECK_EQUAL( countShortedItems(), 0 );
252}
253
254
261BOOST_FIXTURE_TEST_CASE( PadOnOtherLayerIsNotPartOfConnection, FP_LEVEL_NET_TIE_FIXTURE )
262{
263 setCustomStackup( 4 );
264
265 const VECTOR2I padAPos( pcbIUScale.mmToIU( 90.0 ), pcbIUScale.mmToIU( 0.0 ) );
266 const VECTOR2I padBPos( pcbIUScale.mmToIU( 100.0 ), pcbIUScale.mmToIU( 0.0 ) );
267
268 // Both pads sit at the ends of the line, but only pad 1 carries copper on In2.Cu.
269 addNonComponentPad( wxS( "1" ), padAPos, In2_Cu );
270 addNonComponentPad( wxS( "2" ), padBPos, In1_Cu );
271 addCopperLine( padAPos, padBPos, In2_Cu );
272
273 BOOST_CHECK_EQUAL( countShortedItems(), 0 );
274}
275
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
@ FPHOLDER
Definition board.h:403
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:171
void AddNetTiePadGroup(const wxString &aGroup)
Definition footprint.h:574
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
static constexpr PCB_LAYER_ID ALL_LAYERS
! The layer identifier to use for the single defintion on normal padstacks
Definition padstack.h:184
void SetWidth(int aWidth) override
void SetEnd(const VECTOR2I &aEnd) override
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
void SetStart(const VECTOR2I &aStart) override
@ SEGMENT
Definition eda_shape.h:57
@ EXPAND_INNER_LAYERS
The 'normal' stackup handling, where there is a single inner layer (In1) and rule areas using it expa...
Definition footprint.h:111
@ CUSTOM_LAYERS
Stackup handling where the footprint can have any number of copper layers, and objects on those layer...
Definition footprint.h:116
Collection of reusable/testable functions for footprint manipulation and validation.
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ In2_Cu
Definition layer_ids.h:63
@ In1_Cu
Definition layer_ids.h:62
@ F_Cu
Definition layer_ids.h:60
void CheckNetTies(const FOOTPRINT &aFootprint, const std::function< void(const BOARD_ITEM *, const BOARD_ITEM *, const BOARD_ITEM *, const VECTOR2I &)> &aErrorHandler)
Check for un-allowed shorting of pads in net-tie footprints.
@ NON_COMPONENT
Pad with no hole that does not connect to a physical component, but rather provides a connection poin...
Definition padstack.h:106
@ RECTANGLE
Definition padstack.h:55
constexpr int mmToIU(double mm) const
Definition base_units.h:92
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_FIXTURE_TEST_CASE(PadsInDifferentGroupsOnOuterLayerExpandingStackup, FP_LEVEL_NET_TIE_FIXTURE)
Two pads in different groups, joined by copper on an outer layer.
BOOST_AUTO_TEST_SUITE_END()
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708