From 9f10c142c4cecddc517ea157f14a2f7d929e3c91 Mon Sep 17 00:00:00 2001 From: qu1ck Date: Sat, 11 Feb 2023 01:03:19 -0800 Subject: [PATCH] Sync pcbnew selection with selected items after plugin is run --- pcbnew/board.cpp | 18 +++++++ pcbnew/board.h | 2 + .../scripting/pcbnew_action_plugins.cpp | 51 ++++++++++++------- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/pcbnew/board.cpp b/pcbnew/board.cpp index d365ccfb82..edd5b5e987 100644 --- a/pcbnew/board.cpp +++ b/pcbnew/board.cpp @@ -2344,3 +2344,21 @@ void BOARD::ConvertBrdLayerToPolygonalContours( PCB_LAYER_ID aLayer, } +const std::set BOARD::GetItemSet() +{ + std::set items; + +#define INSERT_ITEMS( collection ) \ + for( BOARD_ITEM * item : collection ) \ + items.insert( item ); + + INSERT_ITEMS( m_tracks ) + INSERT_ITEMS( m_footprints ) + INSERT_ITEMS( m_drawings ) + INSERT_ITEMS( m_zones ) + INSERT_ITEMS( m_markers ) + +#undef INSERT_ITEMS + + return items; +} diff --git a/pcbnew/board.h b/pcbnew/board.h index 67ed9eb01f..e84a5b6ea7 100644 --- a/pcbnew/board.h +++ b/pcbnew/board.h @@ -316,6 +316,8 @@ public: MARKERS& Markers() { return m_markers; } const MARKERS& Markers() const { return m_markers; } + const std::set GetItemSet(); + /** * The groups must maintain the following invariants. These are checked by * GroupsSanityCheck(): diff --git a/pcbnew/python/scripting/pcbnew_action_plugins.cpp b/pcbnew/python/scripting/pcbnew_action_plugins.cpp index a6fcc5abe4..6a9cf36644 100644 --- a/pcbnew/python/scripting/pcbnew_action_plugins.cpp +++ b/pcbnew/python/scripting/pcbnew_action_plugins.cpp @@ -37,6 +37,8 @@ #include #include #include +#include +#include #include #include #include "../../scripting/python_scripting.h" @@ -281,23 +283,7 @@ void PCB_EDIT_FRAME::RunActionPlugin( ACTION_PLUGIN* aActionPlugin ) PICKED_ITEMS_LIST deletedItemsList; // The list of existing items after running the action script - std::set currItemList; - - // Append tracks: - for( PCB_TRACK* item : currentPcb->Tracks() ) - currItemList.insert( item ); - - // Append footprints: - for( FOOTPRINT* item : currentPcb->Footprints() ) - currItemList.insert( item ); - - // Append drawings - for( BOARD_ITEM* item : currentPcb->Drawings() ) - currItemList.insert( item ); - - // Append zones outlines - for( ZONE* zone : currentPcb->Zones() ) - currItemList.insert( zone ); + const std::set currItemList = currentPcb->GetItemSet(); // Found deleted items for( unsigned int i = 0; i < oldBuffer->GetCount(); i++ ) @@ -362,7 +348,6 @@ void PCB_EDIT_FRAME::RunActionPlugin( ACTION_PLUGIN* aActionPlugin ) } } - if( oldBuffer->GetCount() ) { OnModify(); @@ -382,6 +367,36 @@ void PCB_EDIT_FRAME::RunActionPlugin( ACTION_PLUGIN* aActionPlugin ) void PCB_EDIT_FRAME::RebuildAndRefresh() { + // The list of existing items after running the action script + const std::set items = GetBoard()->GetItemSet(); + + // Sync selection with items selection state + SELECTION& selection = GetCurrentSelection(); + PCB_SELECTION_TOOL* selTool = m_toolManager->GetTool(); + EDA_ITEMS to_add; + EDA_ITEMS to_remove; + + for( BOARD_ITEM* item : items ) + { + if( item->IsSelected() && !selection.Contains( item ) ) + { + item->ClearSelected(); // temporarily + to_add.push_back( item ); + } + } + + for( EDA_ITEM* item : selection.GetItems() ) + { + if( !item->IsSelected() ) + to_remove.push_back( static_cast( item ) ); + } + + if( !to_add.empty() ) + selTool->AddItemsToSel( &to_add ); + + if( !to_remove.empty() ) + selTool->RemoveItemsFromSel( &to_remove ); + m_pcb->BuildConnectivity(); PCB_DRAW_PANEL_GAL* canvas = GetCanvas();