KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_zone.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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
26
27#include <board.h>
28#include <footprint.h>
30#include <zone.h>
31#include <zone_utils.h>
32
33
38
39
40static std::unique_ptr<ZONE> CreateSquareZone( BOARD_ITEM_CONTAINER& aParent, BOX2I aBox, PCB_LAYER_ID aLayer )
41{
42 auto zone = std::make_unique<ZONE>( &aParent );
43 zone->SetLayer( aLayer );
44
45 auto outline = std::make_unique<SHAPE_POLY_SET>();
46 outline->AddOutline( KIGEOM::BoxToLineChain( aBox ) );
47
48 zone->SetOutline( outline.release() );
49
50 return zone;
51}
52
53
57static std::unique_ptr<ZONE> CreateSimilarZone( BOARD_ITEM_CONTAINER& aParent, const ZONE& aOther, PCB_LAYER_ID aLayer )
58{
59 auto zone = std::make_unique<ZONE>( &aParent );
60 zone->SetLayer( aLayer );
61
62 std::unique_ptr<SHAPE_POLY_SET> outline = std::make_unique<SHAPE_POLY_SET>( *aOther.Outline() );
63 zone->SetOutline( outline.release() );
64
65 return zone;
66}
67
68
69BOOST_FIXTURE_TEST_SUITE( Zone, ZONE_TEST_FIXTURE )
70
72{
73 ZONE zone( &m_board );
74
75 zone.SetLayer( F_Cu );
76
77 BOOST_TEST( zone.GetLayer() == F_Cu );
78 BOOST_TEST( zone.GetLayer() == zone.GetFirstLayer() );
79
80 BOOST_TEST( zone.IsOnCopperLayer() == true );
81}
82
83BOOST_AUTO_TEST_CASE( MultipleLayers )
84{
85 ZONE zone( &m_board );
86
87 zone.SetLayerSet( { F_Cu, B_Cu } );
88
89 // There is no "the" layer in a multi-layer zone
91 // ... but there is a first layer
92 BOOST_TEST( zone.GetFirstLayer() == F_Cu );
93
94 BOOST_TEST( zone.IsOnCopperLayer() == true );
95}
96
103BOOST_AUTO_TEST_CASE( RescuedLayers )
104{
105 ZONE zone( &m_board );
106
107 zone.SetLayer( Rescue );
108
109 BOOST_TEST( zone.GetLayer() == Rescue );
110 BOOST_TEST( zone.GetLayer() == zone.GetFirstLayer() );
111
112 BOOST_TEST( zone.IsOnCopperLayer() == false );
113}
114
122BOOST_AUTO_TEST_CASE( RuleAreaInnerLayersExpandMode )
123{
124 FOOTPRINT footprint( &m_board );
126
127 ZONE* ruleArea = new ZONE( &footprint );
128 ruleArea->SetIsRuleArea( true );
129 ruleArea->SetLayerSet( LSET::InternalCuMask() );
130 footprint.Add( ruleArea );
131
132 // Collect all layers used by the footprint (mirrors GetAllUsedFootprintLayers
133 // from dialog_footprint_properties_fp_editor.cpp)
134 LSET usedLayers;
135
136 footprint.RunOnChildren(
137 [&]( BOARD_ITEM* aItem )
138 {
139 if( aItem->Type() == PCB_ZONE_T )
140 usedLayers |= static_cast<ZONE*>( aItem )->GetLayerSet();
141 else
142 usedLayers.set( aItem->GetLayer() );
143 },
145
146 // In EXPAND_INNER_LAYERS mode, F_Cu, B_Cu and all inner copper layers
147 // are valid, along with tech, user, and user-defined layers.
148 LSET allowedLayers = LSET{ F_Cu, B_Cu } | LSET::InternalCuMask();
149 allowedLayers |= LSET::UserDefinedLayersMask( 4 );
150
151 usedLayers &= ~allowedLayers;
152 usedLayers &= ~LSET::AllTechMask();
153 usedLayers &= ~LSET::UserMask();
154
155 BOOST_TEST( usedLayers.none() );
156}
157
164BOOST_AUTO_TEST_CASE( EmptyZoneGetPosition )
165{
166 ZONE zone( &m_board );
167 zone.SetLayer( F_Cu );
168
169 BOOST_TEST( zone.GetNumCorners() == 0 );
170 BOOST_CHECK_NO_THROW( zone.GetPosition() );
171 BOOST_TEST( zone.GetPosition() == VECTOR2I( 0, 0 ) );
172}
173
174
175BOOST_AUTO_TEST_CASE( ZoneMergeNull )
176{
177 std::vector<std::unique_ptr<ZONE>> zones;
178
179 zones.emplace_back( std::make_unique<ZONE>( &m_board ) );
180 zones.back()->SetLayer( F_Cu );
181
182 zones.emplace_back( std::make_unique<ZONE>( &m_board ) );
183 zones.back()->SetLayer( F_Cu );
184
185 std::vector<std::unique_ptr<ZONE>> merged = MergeZonesWithSameOutline( std::move( zones ) );
186
187 // They are the same, so they do merge
188 BOOST_TEST( merged.size() == 1 );
189}
190
191
192BOOST_AUTO_TEST_CASE( ZoneMergeNonNullNoMerge )
193{
194 std::vector<std::unique_ptr<ZONE>> zones;
195
196 zones.emplace_back( CreateSquareZone( m_board, BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ), F_Cu ) );
197 zones.emplace_back( CreateSquareZone( m_board, BOX2I( VECTOR2I( 200, 200 ), VECTOR2I( 300, 300 ) ), B_Cu ) );
198
199 std::vector<std::unique_ptr<ZONE>> merged = MergeZonesWithSameOutline( std::move( zones ) );
200
201 // They are different, so they don't merge
202 BOOST_TEST( merged.size() == 2 );
203}
204
205
206BOOST_AUTO_TEST_CASE( ZoneMergeNonNullMerge )
207{
208 std::vector<std::unique_ptr<ZONE>> zones;
209
210 zones.emplace_back( CreateSquareZone( m_board, BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ), F_Cu ) );
211 zones.emplace_back( CreateSimilarZone( m_board, *zones.back(), B_Cu ) );
212
213 std::vector<std::unique_ptr<ZONE>> merged = MergeZonesWithSameOutline( std::move( zones ) );
214
215 // They are the same, so they do merge
216 BOOST_REQUIRE( merged.size() == 1 );
217
218 BOOST_TEST( merged[0]->GetLayerSet() == ( LSET{ F_Cu, B_Cu } ) );
219 BOOST_TEST( merged[0]->GetNumCorners() == 4 );
220}
221
222
223BOOST_AUTO_TEST_CASE( ZoneMergeMergeSameGeomDifferentOrder )
224{
225 std::vector<std::unique_ptr<ZONE>> zones;
226
227 zones.emplace_back( CreateSquareZone( m_board, BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ), F_Cu ) );
228 zones.emplace_back( CreateSimilarZone( m_board, *zones.back(), B_Cu ) );
229
230 // Reverse the outline of one of them
231 // Don't go overboard here - detailed tests of CompareGeometry
232 // should be in the SHAPE_LINE_CHAIN tests.
233 auto newPolyB = std::make_unique<SHAPE_POLY_SET>( *zones.back()->Outline() );
234 newPolyB->Outline( 0 ).Reverse();
235 zones.back()->SetOutline( newPolyB.release() );
236
237 std::vector<std::unique_ptr<ZONE>> merged = MergeZonesWithSameOutline( std::move( zones ) );
238
239 // They are the same, so they do merge
240 BOOST_REQUIRE( merged.size() == 1 );
241
242 BOOST_TEST( merged[0]->GetLayerSet() == LSET( { F_Cu, B_Cu } ) );
243 BOOST_TEST( merged[0]->GetNumCorners() == 4 );
244}
245
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
BASE_SET & set(size_t pos)
Definition base_set.h:116
Abstract interface for BOARD_ITEMs capable of storing other items inside.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition board_item.h:237
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:111
void SetStackupMode(FOOTPRINT_STACKUP aMode)
Set the stackup mode for this footprint.
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction, RECURSE_MODE aMode) const override
Invoke a function on all children.
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static LSET UserDefinedLayersMask(int aUserDefinedLayerCount=MAX_USER_DEFINED_LAYERS)
Return a mask with the requested number of user defined layers.
Definition lset.cpp:704
static const LSET & InternalCuMask()
Return a complete set of internal copper layers which is all Cu layers except F_Cu and B_Cu.
Definition lset.cpp:577
Handle a list of polygons defining a copper zone.
Definition zone.h:73
virtual PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition zone.cpp:501
virtual void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
Definition zone.cpp:550
SHAPE_POLY_SET * Outline()
Definition zone.h:340
void SetIsRuleArea(bool aEnable)
Definition zone.h:720
void SetLayerSet(const LSET &aLayerSet) override
Definition zone.cpp:556
VECTOR2I GetPosition() const override
Definition zone.cpp:492
bool IsOnCopperLayer() const override
Definition zone.cpp:543
PCB_LAYER_ID GetFirstLayer() const
Definition zone.cpp:523
int GetNumCorners(void) const
Access to m_Poly parameters.
Definition zone.h:524
@ RECURSE
Definition eda_item.h:52
@ EXPAND_INNER_LAYERS
The 'normal' stackup handling, where there is a single inner layer (In1) and rule areas using it expa...
Definition footprint.h:99
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ B_Cu
Definition layer_ids.h:65
@ UNDEFINED_LAYER
Definition layer_ids.h:61
@ Rescue
Definition layer_ids.h:121
@ F_Cu
Definition layer_ids.h:64
SHAPE_LINE_CHAIN BoxToLineChain(const BOX2I &aBox)
Utility functions for working with shapes.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_TEST(contains==c.ExpectedContains)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
static std::unique_ptr< ZONE > CreateSquareZone(BOARD_ITEM_CONTAINER &aParent, BOX2I aBox, PCB_LAYER_ID aLayer)
Definition test_zone.cpp:40
static std::unique_ptr< ZONE > CreateSimilarZone(BOARD_ITEM_CONTAINER &aParent, const ZONE &aOther, PCB_LAYER_ID aLayer)
Create a similar zone (same outline) on a different layer.
Definition test_zone.cpp:57
BOOST_AUTO_TEST_CASE(SingleLayer)
Definition test_zone.cpp:71
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:108
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
std::vector< std::unique_ptr< ZONE > > MergeZonesWithSameOutline(std::vector< std::unique_ptr< ZONE > > &&aZones)
Merges zones with identical outlines and nets on different layers into single multi-layer zones.