KiCad PCB EDA Suite
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_TEXT_T:
52 case PCB_TEXTBOX_T:
53 case PCB_FP_TEXT_T:
55 case PCB_FP_SHAPE_T:
61 case PCB_FP_ZONE_T:
62 case PCB_GROUP_T:
63 case PCB_TRACE_T:
64 case PCB_VIA_T:
65 case PCB_ARC_T:
66 case PCB_DIMENSION_T:
72 case PCB_ZONE_T:
73 return true;
74 default:
75 return false;
76 }
77}
78
79
81{
82 wxCHECK_MSG( IsGroupableType( aItem->Type() ), false,
83 wxT( "Invalid item type added to group: " ) + aItem->GetTypeDesc() );
84
85 // Items can only be in one group at a time
86 if( aItem->GetParentGroup() )
87 aItem->GetParentGroup()->RemoveItem( aItem );
88
89 m_items.insert( aItem );
90 aItem->SetParentGroup( this );
91 return true;
92}
93
94
96{
97 // Only clear the item's group field if it was inside this group
98 if( m_items.erase( aItem ) == 1 )
99 {
100 aItem->SetParentGroup( nullptr );
101 return true;
102 }
103
104 return false;
105}
106
107
109{
110 for( BOARD_ITEM* item : m_items )
111 item->SetParentGroup( nullptr );
112
113 m_items.clear();
114}
115
116
117/*
118 * @return if not in the footprint editor and aItem is in a footprint, returns the
119 * footprint's parent group. Otherwise, returns the aItem's parent group.
120 */
121PCB_GROUP* getClosestGroup( BOARD_ITEM* aItem, bool isFootprintEditor )
122{
123 if( !isFootprintEditor && aItem->GetParent() && aItem->GetParent()->Type() == PCB_FOOTPRINT_T )
124 return aItem->GetParent()->GetParentGroup();
125 else
126 return aItem->GetParentGroup();
127}
128
129
131PCB_GROUP* getNestedGroup( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor )
132{
133 PCB_GROUP* group = getClosestGroup( aItem, isFootprintEditor );
134
135 if( group == aScope )
136 return nullptr;
137
138 while( group && group->GetParentGroup() && group->GetParentGroup() != aScope )
139 {
140 if( group->GetParent()->Type() == PCB_FOOTPRINT_T && isFootprintEditor )
141 break;
142
143 group = group->GetParentGroup();
144 }
145
146 return group;
147}
148
149
150PCB_GROUP* PCB_GROUP::TopLevelGroup( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor )
151{
152 return getNestedGroup( aItem, aScope, isFootprintEditor );
153}
154
155
156bool PCB_GROUP::WithinScope( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor )
157{
158 PCB_GROUP* group = getClosestGroup( aItem, isFootprintEditor );
159
160 if( group && group == aScope )
161 return true;
162
163 PCB_GROUP* nested = getNestedGroup( aItem, aScope, isFootprintEditor );
164
165 return nested && nested->GetParentGroup() && nested->GetParentGroup() == aScope;
166}
167
168
170{
171 return GetBoundingBox().Centre();
172}
173
174
175void PCB_GROUP::SetPosition( const VECTOR2I& aNewpos )
176{
177 VECTOR2I delta = aNewpos - GetPosition();
178
179 Move( delta );
180}
181
182
184{
185 for( BOARD_ITEM* item : m_items )
186 {
187 if( ( item->Type() == PCB_GROUP_T ) && ( aDepth > 0 ) )
188 {
189 static_cast<PCB_GROUP*>( item )->SetLayerRecursive( aLayer, aDepth - 1 );
190 }
191 else
192 {
193 item->SetLayer( aLayer );
194 }
195 }
196}
197
198
199void PCB_GROUP::SetLocked( bool aLockState )
200{
201 BOARD_ITEM::SetLocked( aLockState );
202
204 [&]( BOARD_ITEM* child )
205 {
206 child->SetLocked( aLockState );
207 } );
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 return newGroup;
216}
217
218
220{
221 // Use copy constructor to get the same uuid and other fields
222 PCB_GROUP* newGroup = new PCB_GROUP( *this );
223 newGroup->m_items.clear();
224
225 for( BOARD_ITEM* member : m_items )
226 {
227 if( member->Type() == PCB_GROUP_T )
228 newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepClone() );
229 else
230 newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Clone() ) );
231 }
232
233 return newGroup;
234}
235
236
238{
239 PCB_GROUP* newGroup = static_cast<PCB_GROUP*>( Duplicate() );
240 newGroup->m_items.clear();
241
242 for( BOARD_ITEM* member : m_items )
243 {
244 if( member->Type() == PCB_GROUP_T )
245 newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepDuplicate() );
246 else
247 newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Duplicate() ) );
248 }
249
250 return newGroup;
251}
252
253
255{
256 assert( aImage->Type() == PCB_GROUP_T );
257
258 std::swap( *( (PCB_GROUP*) this ), *( (PCB_GROUP*) aImage ) );
259}
260
261
262bool PCB_GROUP::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
263{
264 // Groups are selected by promoting a selection of one of their children
265 return false;
266}
267
268
269bool PCB_GROUP::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
270{
271 // Groups are selected by promoting a selection of one of their children
272 return false;
273}
274
275
277{
278 BOX2I bbox;
279
280 for( BOARD_ITEM* item : m_items )
281 {
282 if( item->Type() == PCB_FOOTPRINT_T )
283 bbox.Merge( static_cast<FOOTPRINT*>( item )->GetBoundingBox( true, false ) );
284 else
285 bbox.Merge( item->GetBoundingBox() );
286 }
287
288 bbox.Inflate( pcbIUScale.mmToIU( 0.25 ) ); // Give a min size to the bbox
289
290 return bbox;
291}
292
293
294std::shared_ptr<SHAPE> PCB_GROUP::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
295{
296 std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
297
298 for( BOARD_ITEM* item : m_items )
299 shape->AddShape( item->GetEffectiveShape( aLayer, aFlash )->Clone() );
300
301 return shape;
302}
303
304
305INSPECT_RESULT PCB_GROUP::Visit( INSPECTOR aInspector, void* aTestData,
306 const std::vector<KICAD_T>& aScanTypes )
307{
308 for( KICAD_T scanType : aScanTypes )
309 {
310 if( scanType == Type() )
311 {
312 if( INSPECT_RESULT::QUIT == aInspector( this, aTestData ) )
314 }
315 }
316
318}
319
320
322{
323 LSET aSet;
324
325 for( BOARD_ITEM* item : m_items )
326 aSet |= item->GetLayerSet();
327
328 return aSet;
329}
330
331
332bool PCB_GROUP::IsOnLayer( PCB_LAYER_ID aLayer, bool aIncludeCourtyards ) const
333{
334 // A group is on a layer if any item is on the layer
335 for( BOARD_ITEM* item : m_items )
336 {
337 if( item->IsOnLayer( aLayer, aIncludeCourtyards ) )
338 return true;
339 }
340
341 return false;
342}
343
344
345void PCB_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
346{
347 aCount = 1;
348 aLayers[0] = LAYER_ANCHOR;
349}
350
351
352double PCB_GROUP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
353{
354 if( aView->IsLayerVisible( LAYER_ANCHOR ) )
355 return 0.0;
356
357 return std::numeric_limits<double>::max();
358}
359
360
361void PCB_GROUP::Move( const VECTOR2I& aMoveVector )
362{
363 for( BOARD_ITEM* member : m_items )
364 member->Move( aMoveVector );
365}
366
367
368void PCB_GROUP::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
369{
370 for( BOARD_ITEM* item : m_items )
371 item->Rotate( aRotCentre, aAngle );
372}
373
374
375void PCB_GROUP::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
376{
377 for( BOARD_ITEM* item : m_items )
378 item->Flip( aCentre, aFlipLeftRight );
379}
380
381
382wxString PCB_GROUP::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
383{
384 if( m_name.empty() )
385 return wxString::Format( _( "Unnamed Group, %zu members" ), m_items.size() );
386 else
387 return wxString::Format( _( "Group '%s', %zu members" ), m_name, m_items.size() );
388}
389
390
392{
393 return BITMAPS::module;
394}
395
396
397void PCB_GROUP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
398{
399 aList.emplace_back( _( "Group" ), m_name.empty() ? _( "<unnamed>" ) : m_name );
400 aList.emplace_back( _( "Members" ), wxString::Format( wxT( "%zu" ), m_items.size() ) );
401
402 if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
403 aList.emplace_back( _( "Status" ), _( "Locked" ) );
404}
405
406
407void PCB_GROUP::RunOnChildren( const std::function<void( BOARD_ITEM* )>& aFunction ) const
408{
409 try
410 {
411 for( BOARD_ITEM* item : m_items )
412 aFunction( item );
413 }
414 catch( std::bad_function_call& )
415 {
416 wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnChildren" ) );
417 }
418}
419
420
421void PCB_GROUP::RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction ) const
422{
423 try
424 {
425 for( BOARD_ITEM* item : m_items )
426 {
427 aFunction( item );
428
429 if( item->Type() == PCB_GROUP_T )
430 static_cast<PCB_GROUP*>( item )->RunOnDescendants( aFunction );
431 }
432 }
433 catch( std::bad_function_call& )
434 {
435 wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnDescendants" ) );
436 }
437}
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:70
void SetParentGroup(PCB_GROUP *aGroup)
Definition: board_item.h:83
virtual void SetLocked(bool aLocked)
Definition: board_item.h:266
PCB_GROUP * GetParentGroup() const
Definition: board_item.h:84
virtual BOARD_ITEM * Duplicate() const
Create a copy of this BOARD_ITEM.
Definition: board_item.cpp:184
virtual bool IsLocked() const
Definition: board_item.cpp:71
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:175
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:506
Vec Centre() const
Definition: box2.h:70
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:588
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:301
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:69
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:410
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:532
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:276
std::unordered_set< BOARD_ITEM * > m_items
Definition: pcb_group.h:237
static bool WithinScope(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool isFootprintEditor)
Definition: pcb_group.cpp:156
PCB_GROUP * DeepClone() const
Definition: pcb_group.cpp:219
static PCB_GROUP * TopLevelGroup(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool isFootprintEditor)
Definition: pcb_group.cpp:150
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
Flip this object, i.e.
Definition: pcb_group.cpp:368
void SetLayerRecursive(PCB_LAYER_ID aLayer, int aDepth)
Set layer for all items within the group.
Definition: pcb_group.cpp:183
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction) const
Invoke a function on all members of the group.
Definition: pcb_group.cpp:407
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Move this object.
Definition: pcb_group.cpp:352
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider) const override
Return a pointer to an image to be used in menus.
Definition: pcb_group.cpp:382
void RemoveAll()
Definition: pcb_group.cpp:108
PCB_GROUP * DeepDuplicate() const
Test to see if this object is on the given layer.
Definition: pcb_group.cpp:237
LSET GetLayerSet() const override
Set the layer this item is on.
Definition: pcb_group.cpp:321
void SetPosition(const VECTOR2I &aNewpos) override
Return a std::bitset of all layers on which the item physically resides.
Definition: pcb_group.cpp:175
void Move(const VECTOR2I &aMoveVector) override
Rotate this object.
Definition: pcb_group.cpp:361
INSPECT_RESULT Visit(INSPECTOR aInspector, void *aTestData, const std::vector< KICAD_T > &aScanTypes) override
Definition: pcb_group.cpp:305
bool RemoveItem(BOARD_ITEM *aItem)
Remove item from group.
Definition: pcb_group.cpp:95
std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT) const 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:294
void Flip(const VECTOR2I &aCentre, bool aFlipLeftRight) override
Return a user-visible description string of this item.
Definition: pcb_group.cpp:375
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:397
VECTOR2I GetPosition() const override
Definition: pcb_group.cpp:169
bool AddItem(BOARD_ITEM *aItem)
Add item to group.
Definition: pcb_group.cpp:80
void ViewGetLayers(int aLayers[], int &aCount) const override
Definition: pcb_group.cpp:345
bool IsOnLayer(PCB_LAYER_ID aLayer, bool aIncludeCourtyards=false) const override
Test if aPosition is inside or on the boundary of this item.
Definition: pcb_group.cpp:332
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:262
void RunOnDescendants(const std::function< void(BOARD_ITEM *)> &aFunction) const
Invoke a function on all descendants of the group.
Definition: pcb_group.cpp:421
wxString m_name
Definition: pcb_group.h:238
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_group.cpp:211
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:391
void swapData(BOARD_ITEM *aImage) override
<
Definition: pcb_group.cpp:254
void SetLocked(bool aLocked) override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_group.cpp:199
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:147
@ LAYER_ANCHOR
anchor of items having an anchor point (texts, footprints)
Definition: layer_ids.h:201
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
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:131
PCB_GROUP * getClosestGroup(BOARD_ITEM *aItem, bool isFootprintEditor)
Definition: pcb_group.cpp:121
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
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_FP_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:95
@ 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:110
@ PCB_FP_SHAPE_T
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:94
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:107
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:102
@ PCB_FP_TEXTBOX_T
class FP_TEXTBOX, wrapped text in a footprint
Definition: typeinfo.h:93
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:108
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:115
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:91
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition: typeinfo.h:112
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:90
@ PCB_FP_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:97
@ PCB_FP_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:99
@ PCB_FP_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:96
@ 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:106
@ PCB_FP_ZONE_T
class ZONE, managed by a footprint
Definition: typeinfo.h:100
@ PCB_BITMAP_T
class PCB_BITMAP, bitmap on a layer
Definition: typeinfo.h:89
@ PCB_FP_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:98
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_FP_TEXT_T
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:103
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition: typeinfo.h:105
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:101
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:109