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-2023 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 BOARD_ITEM( aParent, idtype, aLayer )
45{
46}
47
48
50{
51 switch ( aType )
52 {
53 case PCB_FOOTPRINT_T:
54 case PCB_PAD_T:
55 case PCB_SHAPE_T:
57 case PCB_FIELD_T:
58 case PCB_TEXT_T:
59 case PCB_TEXTBOX_T:
60 case PCB_TABLE_T:
61 case PCB_GROUP_T:
62 case PCB_GENERATOR_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
183void PCB_GROUP::SetLocked( bool aLockState )
184{
185 BOARD_ITEM::SetLocked( aLockState );
186
188 [&]( BOARD_ITEM* child )
189 {
190 child->SetLocked( aLockState );
191 } );
192}
193
194
196{
197 // Use copy constructor to get the same uuid and other fields
198 PCB_GROUP* newGroup = new PCB_GROUP( *this );
199 return newGroup;
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 newGroup->m_items.clear();
208
209 for( BOARD_ITEM* member : m_items )
210 {
211 if( member->Type() == PCB_GROUP_T )
212 newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepClone() );
213 else
214 newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Clone() ) );
215 }
216
217 return newGroup;
218}
219
220
222{
223 PCB_GROUP* newGroup = static_cast<PCB_GROUP*>( Duplicate() );
224 newGroup->m_items.clear();
225
226 for( BOARD_ITEM* member : m_items )
227 {
228 if( member->Type() == PCB_GROUP_T )
229 newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepDuplicate() );
230 else
231 newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Duplicate() ) );
232 }
233
234 return newGroup;
235}
236
237
239{
240 assert( aImage->Type() == PCB_GROUP_T );
241
242 std::swap( *( (PCB_GROUP*) this ), *( (PCB_GROUP*) aImage ) );
243}
244
245
246bool PCB_GROUP::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
247{
248 // Groups are selected by promoting a selection of one of their children
249 return false;
250}
251
252
253bool PCB_GROUP::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
254{
255 // Groups are selected by promoting a selection of one of their children
256 return false;
257}
258
259
261{
262 BOX2I bbox;
263
264 for( BOARD_ITEM* item : m_items )
265 {
266 if( item->Type() == PCB_FOOTPRINT_T )
267 bbox.Merge( static_cast<FOOTPRINT*>( item )->GetBoundingBox( true, false ) );
268 else
269 bbox.Merge( item->GetBoundingBox() );
270 }
271
272 bbox.Inflate( pcbIUScale.mmToIU( 0.25 ) ); // Give a min size to the bbox
273
274 return bbox;
275}
276
277
278std::shared_ptr<SHAPE> PCB_GROUP::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
279{
280 std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
281
282 for( BOARD_ITEM* item : m_items )
283 shape->AddShape( item->GetEffectiveShape( aLayer, aFlash )->Clone() );
284
285 return shape;
286}
287
288
289INSPECT_RESULT PCB_GROUP::Visit( INSPECTOR aInspector, void* aTestData,
290 const std::vector<KICAD_T>& aScanTypes )
291{
292 for( KICAD_T scanType : aScanTypes )
293 {
294 if( scanType == Type() )
295 {
296 if( INSPECT_RESULT::QUIT == aInspector( this, aTestData ) )
297 return INSPECT_RESULT::QUIT;
298 }
299 }
300
301 return INSPECT_RESULT::CONTINUE;
302}
303
304
306{
307 LSET aSet;
308
309 for( BOARD_ITEM* item : m_items )
310 aSet |= item->GetLayerSet();
311
312 return aSet;
313}
314
315
317{
318 // A group is on a layer if any item is on the layer
319 for( BOARD_ITEM* item : m_items )
320 {
321 if( item->IsOnLayer( aLayer ) )
322 return true;
323 }
324
325 return false;
326}
327
328
329void PCB_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
330{
331 aCount = 1;
332 aLayers[0] = LAYER_ANCHOR;
333}
334
335
336double PCB_GROUP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
337{
338 if( aView->IsLayerVisible( LAYER_ANCHOR ) )
339 return 0.0;
340
341 return std::numeric_limits<double>::max();
342}
343
344
345void PCB_GROUP::Move( const VECTOR2I& aMoveVector )
346{
347 for( BOARD_ITEM* member : m_items )
348 member->Move( aMoveVector );
349}
350
351
352void PCB_GROUP::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
353{
354 for( BOARD_ITEM* item : m_items )
355 item->Rotate( aRotCentre, aAngle );
356}
357
358
359void PCB_GROUP::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
360{
361 for( BOARD_ITEM* item : m_items )
362 item->Flip( aCentre, aFlipLeftRight );
363}
364
365
366wxString PCB_GROUP::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
367{
368 if( m_name.empty() )
369 return wxString::Format( _( "Unnamed Group, %zu members" ), m_items.size() );
370 else
371 return wxString::Format( _( "Group '%s', %zu members" ), m_name, m_items.size() );
372}
373
374
376{
377 return BITMAPS::module;
378}
379
380
381void PCB_GROUP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
382{
383 aList.emplace_back( _( "Group" ), m_name.empty() ? _( "<unnamed>" ) : m_name );
384 aList.emplace_back( _( "Members" ), wxString::Format( wxT( "%zu" ), m_items.size() ) );
385
386 if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
387 aList.emplace_back( _( "Status" ), _( "Locked" ) );
388}
389
390
391void PCB_GROUP::RunOnChildren( const std::function<void( BOARD_ITEM* )>& aFunction ) const
392{
393 try
394 {
395 for( BOARD_ITEM* item : m_items )
396 aFunction( item );
397 }
398 catch( std::bad_function_call& )
399 {
400 wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnChildren" ) );
401 }
402}
403
404
405void PCB_GROUP::RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction,
406 int aDepth ) const
407{
408 // Avoid freezes with infinite recursion
409 if( aDepth > 20 )
410 return;
411
412 try
413 {
414 for( BOARD_ITEM* item : m_items )
415 {
416 aFunction( item );
417
418 if( item->Type() == PCB_GROUP_T )
419 item->RunOnDescendants( aFunction, aDepth + 1 );
420 }
421 }
422 catch( std::bad_function_call& )
423 {
424 wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnDescendants" ) );
425 }
426}
427
428
429bool PCB_GROUP::operator==( const BOARD_ITEM& aOther ) const
430{
431 if( aOther.Type() != Type() )
432 return false;
433
434 const PCB_GROUP& other = static_cast<const PCB_GROUP&>( aOther );
435
436 if( m_items.size() != other.m_items.size() )
437 return false;
438
439 // The items in groups are in unordered sets hashed by the pointer value, so we need to
440 // order them by UUID (EDA_ITEM_SET) to compare
441 EDA_ITEM_SET itemSet( m_items.begin(), m_items.end() );
442 EDA_ITEM_SET otherItemSet( other.m_items.begin(), other.m_items.end() );
443
444 for( auto it1 = itemSet.begin(), it2 = otherItemSet.begin(); it1 != itemSet.end(); ++it1, ++it2 )
445 {
446 // Compare UUID instead of the items themselves because we only care if the contents
447 // of the group has changed, not which elements in the group have changed
448 if( ( *it1 )->m_Uuid != ( *it2 )->m_Uuid )
449 return false;
450 }
451
452 return true;
453}
454
455
456double PCB_GROUP::Similarity( const BOARD_ITEM& aOther ) const
457{
458 if( aOther.Type() != Type() )
459 return 0.0;
460
461 const PCB_GROUP& other = static_cast<const PCB_GROUP&>( aOther );
462
463 double similarity = 0.0;
464
465 for( BOARD_ITEM* item : m_items )
466 {
467 for( BOARD_ITEM* otherItem : other.m_items )
468 {
469 similarity += item->Similarity( *otherItem );
470 }
471 }
472
473 return similarity / m_items.size();
474}
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:300
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:193
virtual bool IsLocked() const
Definition: board_item.cpp:74
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:204
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:68
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:412
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:573
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:260
std::unordered_set< BOARD_ITEM * > m_items
Definition: pcb_group.h:216
static bool WithinScope(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool isFootprintEditor)
Definition: pcb_group.cpp:156
PCB_GROUP * DeepClone() const
Definition: pcb_group.cpp:203
static PCB_GROUP * TopLevelGroup(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool isFootprintEditor)
Definition: pcb_group.cpp:150
void RunOnDescendants(const std::function< void(BOARD_ITEM *)> &aFunction, int aDepth=0) const override
Invoke a function on all descendants.
Definition: pcb_group.cpp:405
static bool IsGroupableType(KICAD_T aType)
Check if the proposed type can be added to a group.
Definition: pcb_group.cpp:49
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate this object.
Definition: pcb_group.cpp:352
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Definition: pcb_group.cpp:336
bool operator==(const BOARD_ITEM &aOther) const override
Definition: pcb_group.cpp:429
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider) const override
Return a user-visible description string of this item.
Definition: pcb_group.cpp:366
void RemoveAll()
Definition: pcb_group.cpp:108
bool IsOnLayer(PCB_LAYER_ID aLayer) const override
Test to see if this object is on the given layer.
Definition: pcb_group.cpp:316
PCB_GROUP * DeepDuplicate() const
Definition: pcb_group.cpp:221
LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: pcb_group.cpp:305
void SetPosition(const VECTOR2I &aNewpos) override
Definition: pcb_group.cpp:175
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition: pcb_group.cpp:345
double Similarity(const BOARD_ITEM &aOther) const override
Return a measure of how likely the other object is to represent the same object.
Definition: pcb_group.cpp:456
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:289
virtual 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
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition: pcb_group.cpp:278
void Flip(const VECTOR2I &aCentre, bool aFlipLeftRight) override
Flip this object, i.e.
Definition: pcb_group.cpp:359
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:381
VECTOR2I GetPosition() const override
Definition: pcb_group.cpp:169
virtual 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:329
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction) const override
Invoke a function on all descendants.
Definition: pcb_group.cpp:391
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:246
wxString m_name
Definition: pcb_group.h:217
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_group.cpp:195
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:375
void swapData(BOARD_ITEM *aImage) override
Definition: pcb_group.cpp:238
void SetLocked(bool aLocked) override
Definition: pcb_group.cpp:183
This file is part of the common library.
#define _(s)
#define PCB_EDIT_FRAME_NAME
INSPECT_RESULT
Definition: eda_item.h:42
std::set< EDA_ITEM *, CompareByUuid > EDA_ITEM_SET
Definition: eda_item.h:531
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:204
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:131
PCB_GROUP * getClosestGroup(BOARD_ITEM *aItem, bool isFootprintEditor)
Definition: pcb_group.cpp:121
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:105
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:102
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition: typeinfo.h:91
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:103
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:110
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:93
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition: typeinfo.h:107
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition: typeinfo.h:89
@ 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:101
@ 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:98
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition: typeinfo.h:100
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition: typeinfo.h:94
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:104