2020-08-11 19:37:07 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 Joshua Redstone redstone at gmail.com
|
|
|
|
* Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
#include <bitmaps.h>
|
2020-09-02 01:09:34 +00:00
|
|
|
#include <class_board.h>
|
|
|
|
#include <class_board_item.h>
|
2020-08-12 11:23:30 +00:00
|
|
|
#include <class_pcb_group.h>
|
2020-08-11 19:37:07 +00:00
|
|
|
#include <confirm.h>
|
2020-10-25 04:49:02 +00:00
|
|
|
#include <widgets/msgpanel.h>
|
2020-08-11 19:37:07 +00:00
|
|
|
#include <view/view.h>
|
|
|
|
|
2020-10-03 11:16:29 +00:00
|
|
|
PCB_GROUP::PCB_GROUP( BOARD_ITEM* aParent ) :
|
2020-09-25 21:48:07 +00:00
|
|
|
BOARD_ITEM( aParent, PCB_GROUP_T )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-02 01:09:34 +00:00
|
|
|
bool PCB_GROUP::AddItem( BOARD_ITEM* aItem )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
2020-09-02 01:09:34 +00:00
|
|
|
// Items can only be in one group at a time
|
2020-09-25 17:37:03 +00:00
|
|
|
if( aItem->GetParentGroup() )
|
|
|
|
aItem->GetParentGroup()->RemoveItem( aItem );
|
2020-09-02 01:09:34 +00:00
|
|
|
|
|
|
|
m_items.insert( aItem );
|
2020-09-25 17:37:03 +00:00
|
|
|
aItem->SetParentGroup( this );
|
2020-09-02 01:09:34 +00:00
|
|
|
return true;
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-09-02 01:09:34 +00:00
|
|
|
bool PCB_GROUP::RemoveItem( BOARD_ITEM* aItem )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
2020-09-02 01:09:34 +00:00
|
|
|
// Only clear the item's group field if it was inside this group
|
|
|
|
if( m_items.erase( aItem ) == 1 )
|
|
|
|
{
|
2020-09-25 17:37:03 +00:00
|
|
|
aItem->SetParentGroup( nullptr );
|
2020-09-02 01:09:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-09-24 01:05:46 +00:00
|
|
|
void PCB_GROUP::RemoveAll()
|
|
|
|
{
|
|
|
|
for( BOARD_ITEM* item : m_items )
|
2020-09-25 17:37:03 +00:00
|
|
|
item->SetParentGroup( nullptr );
|
2020-09-24 01:05:46 +00:00
|
|
|
|
|
|
|
m_items.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-26 16:06:32 +00:00
|
|
|
PCB_GROUP* PCB_GROUP::TopLevelGroup( BOARD_ITEM* item, PCB_GROUP* scope )
|
|
|
|
{
|
|
|
|
PCB_GROUP* candidate = item->GetParentGroup();
|
|
|
|
|
2020-10-03 11:16:29 +00:00
|
|
|
if( !candidate && item->GetParent() && item->GetParent()->Type() == PCB_MODULE_T )
|
|
|
|
candidate = item->GetParent()->GetParentGroup();
|
|
|
|
|
2020-09-26 16:06:32 +00:00
|
|
|
while( candidate && candidate->GetParentGroup() && candidate->GetParentGroup() != scope )
|
|
|
|
candidate = candidate->GetParentGroup();
|
|
|
|
|
|
|
|
return candidate == scope ? nullptr : candidate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PCB_GROUP::WithinScope( BOARD_ITEM* item, PCB_GROUP* scope )
|
|
|
|
{
|
|
|
|
if( item->GetParent() && item->GetParent()->Type() == PCB_MODULE_T )
|
|
|
|
item = item->GetParent();
|
|
|
|
|
|
|
|
for( PCB_GROUP* parent = item->GetParentGroup(); parent; parent = parent->GetParentGroup() )
|
|
|
|
{
|
|
|
|
if( parent == scope )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
wxPoint PCB_GROUP::GetPosition() const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
return GetBoundingBox().Centre();
|
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-09-02 01:09:34 +00:00
|
|
|
void PCB_GROUP::SetPosition( const wxPoint& aNewpos )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
2020-09-02 01:09:34 +00:00
|
|
|
wxPoint delta = aNewpos - GetPosition();
|
2020-08-11 19:37:07 +00:00
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
Move( delta );
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
EDA_ITEM* PCB_GROUP::Clone() const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
// Use copy constructor to get the same uuid and other fields
|
2020-08-12 11:23:30 +00:00
|
|
|
PCB_GROUP* newGroup = new PCB_GROUP( *this );
|
2020-08-11 19:37:07 +00:00
|
|
|
return newGroup;
|
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
PCB_GROUP* PCB_GROUP::DeepClone() const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
// Use copy constructor to get the same uuid and other fields
|
2020-08-12 11:23:30 +00:00
|
|
|
PCB_GROUP* newGroup = new PCB_GROUP( *this );
|
2020-08-11 19:37:07 +00:00
|
|
|
newGroup->m_items.clear();
|
|
|
|
|
2020-09-02 01:09:34 +00:00
|
|
|
for( BOARD_ITEM* member : m_items )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
if( member->Type() == PCB_GROUP_T )
|
2020-08-12 11:23:30 +00:00
|
|
|
newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepClone() );
|
2020-08-11 19:37:07 +00:00
|
|
|
else
|
|
|
|
newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Clone() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return newGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
PCB_GROUP* PCB_GROUP::DeepDuplicate() const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
2020-08-12 11:23:30 +00:00
|
|
|
PCB_GROUP* newGroup = static_cast<PCB_GROUP*>( this->Duplicate() );
|
2020-08-11 19:37:07 +00:00
|
|
|
newGroup->m_items.clear();
|
|
|
|
|
2020-09-02 01:09:34 +00:00
|
|
|
for( BOARD_ITEM* member : m_items )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
if( member->Type() == PCB_GROUP_T )
|
2020-08-12 11:23:30 +00:00
|
|
|
newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepDuplicate() );
|
2020-08-11 19:37:07 +00:00
|
|
|
else
|
|
|
|
newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Duplicate() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return newGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
void PCB_GROUP::SwapData( BOARD_ITEM* aImage )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
assert( aImage->Type() == PCB_GROUP_T );
|
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
std::swap( *( (PCB_GROUP*) this ), *( (PCB_GROUP*) aImage ) );
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
bool PCB_GROUP::HitTest( const wxPoint& aPosition, int aAccuracy ) const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
2020-10-02 11:32:48 +00:00
|
|
|
// Groups are selected by promoting a selection of one of their children
|
|
|
|
return false;
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
bool PCB_GROUP::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
2020-10-02 11:32:48 +00:00
|
|
|
// Groups are selected by promoting a selection of one of their children
|
2020-09-02 01:09:34 +00:00
|
|
|
return false;
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
const EDA_RECT PCB_GROUP::GetBoundingBox() const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
EDA_RECT area;
|
|
|
|
|
|
|
|
for( BOARD_ITEM* item : m_items )
|
2020-09-02 01:09:34 +00:00
|
|
|
area.Merge( item->GetBoundingBox() );
|
|
|
|
|
2020-08-11 19:37:07 +00:00
|
|
|
area.Inflate( Millimeter2iu( 0.25 ) ); // Give a min size to the area
|
|
|
|
|
|
|
|
return area;
|
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-09-02 01:09:34 +00:00
|
|
|
SEARCH_RESULT PCB_GROUP::Visit( INSPECTOR aInspector, void* aTestData, const KICAD_T aScanTypes[] )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
2020-09-02 01:09:34 +00:00
|
|
|
for( const KICAD_T* stype = aScanTypes; *stype != EOT; ++stype )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
// If caller wants to inspect my type
|
|
|
|
if( *stype == Type() )
|
|
|
|
{
|
2020-09-02 01:09:34 +00:00
|
|
|
if( SEARCH_RESULT::QUIT == aInspector( this, aTestData ) )
|
2020-08-11 19:37:07 +00:00
|
|
|
return SEARCH_RESULT::QUIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SEARCH_RESULT::CONTINUE;
|
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
LSET PCB_GROUP::GetLayerSet() const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
LSET aSet;
|
|
|
|
|
|
|
|
for( BOARD_ITEM* item : m_items )
|
|
|
|
aSet |= item->GetLayerSet();
|
2020-09-02 01:09:34 +00:00
|
|
|
|
2020-08-11 19:37:07 +00:00
|
|
|
return aSet;
|
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-09-02 01:09:34 +00:00
|
|
|
bool PCB_GROUP::IsOnLayer( PCB_LAYER_ID aLayer ) const
|
|
|
|
{
|
|
|
|
// A group is on a layer if any item is on the layer
|
|
|
|
for( BOARD_ITEM* item : m_items )
|
|
|
|
{
|
|
|
|
if( item->IsOnLayer( aLayer ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
void PCB_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
2020-08-12 11:23:30 +00:00
|
|
|
// What layer to put bounding box on? change in class_pcb_group.cpp
|
2020-08-11 19:37:07 +00:00
|
|
|
std::unordered_set<int> layers = { LAYER_ANCHOR }; // for bounding box
|
|
|
|
|
|
|
|
for( BOARD_ITEM* item : m_items )
|
|
|
|
{
|
|
|
|
int member_layers[KIGFX::VIEW::VIEW_MAX_LAYERS], member_layers_count;
|
|
|
|
item->ViewGetLayers( member_layers, member_layers_count );
|
|
|
|
|
|
|
|
for( int i = 0; i < member_layers_count; i++ )
|
|
|
|
layers.insert( member_layers[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
aCount = layers.size();
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for( int layer : layers )
|
|
|
|
aLayers[i++] = layer;
|
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-09-21 15:03:08 +00:00
|
|
|
double PCB_GROUP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
if( aView->IsLayerVisible( LAYER_ANCHOR ) )
|
2020-09-21 15:03:08 +00:00
|
|
|
return 0.0;
|
2020-08-11 19:37:07 +00:00
|
|
|
|
2020-09-21 15:03:08 +00:00
|
|
|
return std::numeric_limits<double>::max();
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
void PCB_GROUP::Move( const wxPoint& aMoveVector )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
2020-09-02 01:09:34 +00:00
|
|
|
for( BOARD_ITEM* member : m_items )
|
2020-08-15 02:45:56 +00:00
|
|
|
member->Move( aMoveVector );
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
void PCB_GROUP::Rotate( const wxPoint& aRotCentre, double aAngle )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
for( BOARD_ITEM* item : m_items )
|
|
|
|
item->Rotate( aRotCentre, aAngle );
|
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
void PCB_GROUP::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
for( BOARD_ITEM* item : m_items )
|
|
|
|
item->Flip( aCentre, aFlipLeftRight );
|
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
wxString PCB_GROUP::GetSelectMenuText( EDA_UNITS aUnits ) const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
if( m_name.empty() )
|
|
|
|
{
|
2020-09-19 16:12:00 +00:00
|
|
|
return wxString::Format( _( "Anonymous Group, %zu members" ),
|
|
|
|
m_items.size() );
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
2020-09-02 01:09:34 +00:00
|
|
|
|
2020-09-19 16:12:00 +00:00
|
|
|
return wxString::Format( _( "Group \"%s\", %zu members" ),
|
|
|
|
m_name,
|
|
|
|
m_items.size() );
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
BITMAP_DEF PCB_GROUP::GetMenuImage() const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
return module_xpm;
|
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-08-12 11:23:30 +00:00
|
|
|
void PCB_GROUP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
aList.emplace_back( _( "Group" ), m_name.empty() ? _( "Anonymous" ) :
|
2020-08-15 15:46:52 +00:00
|
|
|
wxString::Format( "\"%s\"", m_name ), DARKCYAN );
|
2020-09-02 01:09:34 +00:00
|
|
|
aList.emplace_back( _( "Members" ), wxString::Format( "%zu", m_items.size() ), BROWN );
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-10-27 11:03:35 +00:00
|
|
|
void PCB_GROUP::RunOnChildren( const std::function<void( BOARD_ITEM* )>& aFunction ) const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
for( BOARD_ITEM* item : m_items )
|
|
|
|
aFunction( item );
|
|
|
|
}
|
|
|
|
catch( std::bad_function_call& )
|
|
|
|
{
|
2020-09-02 01:09:34 +00:00
|
|
|
wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnChildren" ) );
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 02:45:56 +00:00
|
|
|
|
2020-10-27 11:03:35 +00:00
|
|
|
void PCB_GROUP::RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction ) const
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
for( BOARD_ITEM* item : m_items )
|
|
|
|
{
|
|
|
|
aFunction( item );
|
2020-09-02 01:09:34 +00:00
|
|
|
|
2020-08-11 19:37:07 +00:00
|
|
|
if( item->Type() == PCB_GROUP_T )
|
2020-08-12 11:23:30 +00:00
|
|
|
static_cast<PCB_GROUP*>( item )->RunOnDescendants( aFunction );
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( std::bad_function_call& )
|
|
|
|
{
|
2020-09-02 01:09:34 +00:00
|
|
|
wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnDescendants" ) );
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
}
|