KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_group.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 (C) 2020 Joshua Redstone redstone at gmail.com
5 * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24#include <bitmaps.h>
25#include <eda_draw_frame.h>
27#include <board.h>
28#include <board_item.h>
29#include <footprint.h>
30#include <pcb_group.h>
31#include <confirm.h>
32#include <widgets/msgpanel.h>
33#include <view/view.h>
34
35#include <wx/debug.h>
36
38 BOARD_ITEM( aParent, PCB_GROUP_T )
39{
40}
41
42
44{
45 switch ( aType )
46 {
47 case PCB_FOOTPRINT_T:
48 case PCB_PAD_T:
49 case PCB_SHAPE_T:
50 case PCB_BITMAP_T:
51 case PCB_FIELD_T:
52 case PCB_TEXT_T:
53 case PCB_TEXTBOX_T:
54 case PCB_GROUP_T:
55 case PCB_TRACE_T:
56 case PCB_VIA_T:
57 case PCB_ARC_T:
58 case PCB_DIMENSION_T:
64 case PCB_ZONE_T:
65 return true;
66 default:
67 return false;
68 }
69}
70
71
73{
74 wxCHECK_MSG( IsGroupableType( aItem->Type() ), false,
75 wxT( "Invalid item type added to group: " ) + aItem->GetTypeDesc() );
76
77 // Items can only be in one group at a time
78 if( aItem->GetParentGroup() )
79 aItem->GetParentGroup()->RemoveItem( aItem );
80
81 m_items.insert( aItem );
82 aItem->SetParentGroup( this );
83 return true;
84}
85
86
88{
89 // Only clear the item's group field if it was inside this group
90 if( m_items.erase( aItem ) == 1 )
91 {
92 aItem->SetParentGroup( nullptr );
93 return true;
94 }
95
96 return false;
97}
98
99
101{
102 for( BOARD_ITEM* item : m_items )
103 item->SetParentGroup( nullptr );
104
105 m_items.clear();
106}
107
108
109/*
110 * @return if not in the footprint editor and aItem is in a footprint, returns the
111 * footprint's parent group. Otherwise, returns the aItem's parent group.
112 */
113PCB_GROUP* getClosestGroup( BOARD_ITEM* aItem, bool isFootprintEditor )
114{
115 if( !isFootprintEditor && aItem->GetParent() && aItem->GetParent()->Type() == PCB_FOOTPRINT_T )
116 return aItem->GetParent()->GetParentGroup();
117 else
118 return aItem->GetParentGroup();
119}
120
121
123PCB_GROUP* getNestedGroup( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor )
124{
125 PCB_GROUP* group = getClosestGroup( aItem, isFootprintEditor );
126
127 if( group == aScope )
128 return nullptr;
129
130 while( group && group->GetParentGroup() && group->GetParentGroup() != aScope )
131 {
132 if( group->GetParent()->Type() == PCB_FOOTPRINT_T && isFootprintEditor )
133 break;
134
135 group = group->GetParentGroup();
136 }
137
138 return group;
139}
140
141
142PCB_GROUP* PCB_GROUP::TopLevelGroup( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor )
143{
144 return getNestedGroup( aItem, aScope, isFootprintEditor );
145}
146
147
148bool PCB_GROUP::WithinScope( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor )
149{
150 PCB_GROUP* group = getClosestGroup( aItem, isFootprintEditor );
151
152 if( group && group == aScope )
153 return true;
154
155 PCB_GROUP* nested = getNestedGroup( aItem, aScope, isFootprintEditor );
156
157 return nested && nested->GetParentGroup() && nested->GetParentGroup() == aScope;
158}
159
160
162{
163 return GetBoundingBox().Centre();
164}
165
166
167void PCB_GROUP::SetPosition( const VECTOR2I& aNewpos )
168{
169 VECTOR2I delta = aNewpos - GetPosition();
170
171 Move( delta );
172}
173
174
176{
177 for( BOARD_ITEM* item : m_items )
178 {
179 if( ( item->Type() == PCB_GROUP_T ) && ( aDepth > 0 ) )
180 {
181 static_cast<PCB_GROUP*>( item )->SetLayerRecursive( aLayer, aDepth - 1 );
182 }
183 else
184 {
185 item->SetLayer( aLayer );
186 }
187 }
188}
189
190
191void PCB_GROUP::SetLocked( bool aLockState )
192{
193 BOARD_ITEM::SetLocked( aLockState );
194
196 [&]( BOARD_ITEM* child )
197 {
198 child->SetLocked( aLockState );
199 } );
200}
201
202
204{
205 // Use copy constructor to get the same uuid and other fields
206 PCB_GROUP* newGroup = new PCB_GROUP( *this );
207 return newGroup;
208}
209
210
212{
213 // Use copy constructor to get the same uuid and other fields
214 PCB_GROUP* newGroup = new PCB_GROUP( *this );
215 newGroup->m_items.clear();
216
217 for( BOARD_ITEM* member : m_items )
218 {
219 if( member->Type() == PCB_GROUP_T )
220 newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepClone() );
221 else
222 newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Clone() ) );
223 }
224
225 return newGroup;
226}
227
228
230{
231 PCB_GROUP* newGroup = static_cast<PCB_GROUP*>( Duplicate() );
232 newGroup->m_items.clear();
233
234 for( BOARD_ITEM* member : m_items )
235 {
236 if( member->Type() == PCB_GROUP_T )
237 newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepDuplicate() );
238 else
239 newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Duplicate() ) );
240 }
241
242 return newGroup;
243}
244
245
247{
248 assert( aImage->Type() == PCB_GROUP_T );
249
250 std::swap( *( (PCB_GROUP*) this ), *( (PCB_GROUP*) aImage ) );
251}
252
253
254bool PCB_GROUP::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
255{
256 // Groups are selected by promoting a selection of one of their children
257 return false;
258}
259
260
261bool PCB_GROUP::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
262{
263 // Groups are selected by promoting a selection of one of their children
264 return false;
265}
266
267
269{
270 BOX2I bbox;
271
272 for( BOARD_ITEM* item : m_items )
273 {
274 if( item->Type() == PCB_FOOTPRINT_T )
275 bbox.Merge( static_cast<FOOTPRINT*>( item )->GetBoundingBox( true, false ) );
276 else
277 bbox.Merge( item->GetBoundingBox() );
278 }
279
280 bbox.Inflate( pcbIUScale.mmToIU( 0.25 ) ); // Give a min size to the bbox
281
282 return bbox;
283}
284
285
286std::shared_ptr<SHAPE> PCB_GROUP::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
287{
288 std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
289
290 for( BOARD_ITEM* item : m_items )
291 shape->AddShape( item->GetEffectiveShape( aLayer, aFlash )->Clone() );
292
293 return shape;
294}
295
296
297INSPECT_RESULT PCB_GROUP::Visit( INSPECTOR aInspector, void* aTestData,
298 const std::vector<KICAD_T>& aScanTypes )
299{
300 for( KICAD_T scanType : aScanTypes )
301 {
302 if( scanType == Type() )
303 {
304 if( INSPECT_RESULT::QUIT == aInspector( this, aTestData ) )
305 return INSPECT_RESULT::QUIT;
306 }
307 }
308
309 return INSPECT_RESULT::CONTINUE;
310}
311
312
314{
315 LSET aSet;
316
317 for( BOARD_ITEM* item : m_items )
318 aSet |= item->GetLayerSet();
319
320 return aSet;
321}
322
323
325{
326 // A group is on a layer if any item is on the layer
327 for( BOARD_ITEM* item : m_items )
328 {
329 if( item->IsOnLayer( aLayer ) )
330 return true;
331 }
332
333 return false;
334}
335
336
337void PCB_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
338{
339 aCount = 1;
340 aLayers[0] = LAYER_ANCHOR;
341}
342
343
344double PCB_GROUP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
345{
346 if( aView->IsLayerVisible( LAYER_ANCHOR ) )
347 return 0.0;
348
349 return std::numeric_limits<double>::max();
350}
351
352
353void PCB_GROUP::Move( const VECTOR2I& aMoveVector )
354{
355 for( BOARD_ITEM* member : m_items )
356 member->Move( aMoveVector );
357}
358
359
360void PCB_GROUP::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
361{
362 for( BOARD_ITEM* item : m_items )
363 item->Rotate( aRotCentre, aAngle );
364}
365
366
367void PCB_GROUP::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
368{
369 for( BOARD_ITEM* item : m_items )
370 item->Flip( aCentre, aFlipLeftRight );
371}
372
373
374wxString PCB_GROUP::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
375{
376 if( m_name.empty() )
377 return wxString::Format( _( "Unnamed Group, %zu members" ), m_items.size() );
378 else
379 return wxString::Format( _( "Group '%s', %zu members" ), m_name, m_items.size() );
380}
381
382
384{
385 return BITMAPS::module;
386}
387
388
389void PCB_GROUP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
390{
391 aList.emplace_back( _( "Group" ), m_name.empty() ? _( "<unnamed>" ) : m_name );
392 aList.emplace_back( _( "Members" ), wxString::Format( wxT( "%zu" ), m_items.size() ) );
393
394 if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
395 aList.emplace_back( _( "Status" ), _( "Locked" ) );
396}
397
398
399void PCB_GROUP::RunOnChildren( const std::function<void( BOARD_ITEM* )>& aFunction ) const
400{
401 try
402 {
403 for( BOARD_ITEM* item : m_items )
404 aFunction( item );
405 }
406 catch( std::bad_function_call& )
407 {
408 wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnChildren" ) );
409 }
410}
411
412
413void PCB_GROUP::RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction ) const
414{
415 try
416 {
417 for( BOARD_ITEM* item : m_items )
418 {
419 aFunction( item );
420
421 if( item->Type() == PCB_GROUP_T )
422 static_cast<PCB_GROUP*>( item )->RunOnDescendants( aFunction );
423 }
424 }
425 catch( std::bad_function_call& )
426 {
427 wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnDescendants" ) );
428 }
429}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
void SetParentGroup(PCB_GROUP *aGroup)
Definition: board_item.h:90
virtual void SetLocked(bool aLocked)
Definition: board_item.h:278
PCB_GROUP * GetParentGroup() const
Definition: board_item.h:91
virtual BOARD_ITEM * Duplicate() const
Create a copy of this BOARD_ITEM.
Definition: board_item.cpp:192
virtual bool IsLocked() const
Definition: board_item.cpp:73
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:182
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:507
Vec Centre() const
Definition: box2.h:71
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:589
The base class for create windows for drawing purpose.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
wxString GetTypeDesc() const
Return a translated description of the type for this EDA_ITEM for display in user facing messages.
Definition: eda_item.cpp:320
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:408
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:552
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:51
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: pcb_group.cpp:268
std::unordered_set< BOARD_ITEM * > m_items
Definition: pcb_group.h:243
static bool WithinScope(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool isFootprintEditor)
Definition: pcb_group.cpp:148
PCB_GROUP * DeepClone() const
Definition: pcb_group.cpp:211
static PCB_GROUP * TopLevelGroup(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool isFootprintEditor)
Definition: pcb_group.cpp:142
static bool IsGroupableType(KICAD_T aType)
Check if the proposed type can be added to a group.
Definition: pcb_group.cpp:43
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate this object.
Definition: pcb_group.cpp:360
void SetLayerRecursive(PCB_LAYER_ID aLayer, int aDepth)
Set layer for all items within the group.
Definition: pcb_group.cpp:175
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction) const
Invoke a function on all members of the group.
Definition: pcb_group.cpp:399
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Definition: pcb_group.cpp:344
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider) const override
Return a user-visible description string of this item.
Definition: pcb_group.cpp:374
void RemoveAll()
Definition: pcb_group.cpp:100
bool IsOnLayer(PCB_LAYER_ID aLayer) const override
Test to see if this object is on the given layer.
Definition: pcb_group.cpp:324
PCB_GROUP * DeepDuplicate() const
Definition: pcb_group.cpp:229
LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: pcb_group.cpp:313
void SetPosition(const VECTOR2I &aNewpos) override
Definition: pcb_group.cpp:167
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition: pcb_group.cpp:353
INSPECT_RESULT Visit(INSPECTOR aInspector, void *aTestData, const std::vector< KICAD_T > &aScanTypes) override
May be re-implemented for each derived class in order to handle all the types given by its member dat...
Definition: pcb_group.cpp:297
bool RemoveItem(BOARD_ITEM *aItem)
Remove item from group.
Definition: pcb_group.cpp:87
std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition: pcb_group.cpp:286
void Flip(const VECTOR2I &aCentre, bool aFlipLeftRight) override
Flip this object, i.e.
Definition: pcb_group.cpp:367
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Populate aList of MSG_PANEL_ITEM objects with it's internal state for display purposes.
Definition: pcb_group.cpp:389
VECTOR2I GetPosition() const override
Definition: pcb_group.cpp:161
bool AddItem(BOARD_ITEM *aItem)
Add item to group.
Definition: pcb_group.cpp:72
void ViewGetLayers(int aLayers[], int &aCount) const override
Definition: pcb_group.cpp:337
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
Definition: pcb_group.cpp:254
void RunOnDescendants(const std::function< void(BOARD_ITEM *)> &aFunction) const
Invoke a function on all descendants of the group.
Definition: pcb_group.cpp:413
wxString m_name
Definition: pcb_group.h:244
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_group.cpp:203
PCB_GROUP(BOARD_ITEM *aParent)
Definition: pcb_group.cpp:37
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: pcb_group.cpp:383
void swapData(BOARD_ITEM *aImage) override
Definition: pcb_group.cpp:246
void SetLocked(bool aLocked) override
Definition: pcb_group.cpp:191
This file is part of the common library.
#define _(s)
#define PCB_EDIT_FRAME_NAME
INSPECT_RESULT
Definition: eda_item.h:42
const INSPECTOR_FUNC & INSPECTOR
Definition: eda_item.h:78
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition: layer_ids.h:148
@ LAYER_ANCHOR
anchor of items having an anchor point (texts, footprints)
Definition: layer_ids.h:202
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
Message panel definition file.
PCB_GROUP * getNestedGroup(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool isFootprintEditor)
Returns the top level group inside the aScope group, or nullptr.
Definition: pcb_group.cpp:123
PCB_GROUP * getClosestGroup(BOARD_ITEM *aItem, bool isFootprintEditor)
Definition: pcb_group.cpp:113
Class to handle a set of BOARD_ITEMs.
constexpr int mmToIU(double mm) const
Definition: base_units.h:89
constexpr int delta
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:102
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:99
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:94
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:100
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:107
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:92
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition: typeinfo.h:104
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:91
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:98
@ PCB_BITMAP_T
class PCB_BITMAP, bitmap on a layer
Definition: typeinfo.h:89
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:95
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition: typeinfo.h:97
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:93
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:101