diff --git a/pcbnew/dialogs/dialog_swap_layers.cpp b/pcbnew/dialogs/dialog_swap_layers.cpp index 0c04ff5097..d02415a105 100644 --- a/pcbnew/dialogs/dialog_swap_layers.cpp +++ b/pcbnew/dialogs/dialog_swap_layers.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include "dialog_swap_layers.h" @@ -67,7 +68,8 @@ public: }; -DIALOG_SWAP_LAYERS::DIALOG_SWAP_LAYERS( PCB_BASE_EDIT_FRAME* aParent, PCB_LAYER_ID* aArray ) : +DIALOG_SWAP_LAYERS::DIALOG_SWAP_LAYERS( PCB_BASE_EDIT_FRAME* aParent, + std::map& aArray ) : DIALOG_SWAP_LAYERS_BASE( aParent ), m_parent( aParent ), m_layerDestinations( aArray ) @@ -133,10 +135,20 @@ bool DIALOG_SWAP_LAYERS::TransferDataFromWindow() for( size_t layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer ) { + std::optional src = magic_enum::enum_cast( layer ); + wxCHECK2( src.has_value(), continue ); + if( enabledCopperLayers.test( layer ) ) - m_layerDestinations[ layer ] = (PCB_LAYER_ID) table->GetValueAsLong( row++, 1 ); + { + std::optional dest = + magic_enum::enum_cast( table->GetValueAsLong( row++, 1 ) ); + wxCHECK2( dest.has_value(), m_layerDestinations[ *src ] = *dest ); + m_layerDestinations[ *src ] = *dest; + } else - m_layerDestinations[ layer ] = (PCB_LAYER_ID) layer; + { + m_layerDestinations[ *src ] = *src; + } } return true; diff --git a/pcbnew/dialogs/dialog_swap_layers.h b/pcbnew/dialogs/dialog_swap_layers.h index f549b11ad6..7767d6180e 100644 --- a/pcbnew/dialogs/dialog_swap_layers.h +++ b/pcbnew/dialogs/dialog_swap_layers.h @@ -33,7 +33,8 @@ class LAYER_GRID_TABLE; class DIALOG_SWAP_LAYERS : public DIALOG_SWAP_LAYERS_BASE { public: - DIALOG_SWAP_LAYERS( PCB_BASE_EDIT_FRAME* aParent, PCB_LAYER_ID* aArray ); + DIALOG_SWAP_LAYERS( PCB_BASE_EDIT_FRAME* aParent, + std::map& aArray ); ~DIALOG_SWAP_LAYERS() override; private: @@ -45,7 +46,7 @@ private: void adjustGridColumns(); PCB_BASE_EDIT_FRAME* m_parent; - PCB_LAYER_ID* m_layerDestinations; + std::map& m_layerDestinations; LAYER_GRID_TABLE* m_gridTable; }; diff --git a/pcbnew/tools/global_edit_tool.cpp b/pcbnew/tools/global_edit_tool.cpp index ecf4031234..a05a6bfd2b 100644 --- a/pcbnew/tools/global_edit_tool.cpp +++ b/pcbnew/tools/global_edit_tool.cpp @@ -111,12 +111,15 @@ int GLOBAL_EDIT_TOOL::ExchangeFootprints( const TOOL_EVENT& aEvent ) } -bool GLOBAL_EDIT_TOOL::swapBoardItem( BOARD_ITEM* aItem, PCB_LAYER_ID* aLayerMap ) +bool GLOBAL_EDIT_TOOL::swapBoardItem( BOARD_ITEM* aItem, + std::map& aLayerMap ) { - if( aLayerMap[ aItem->GetLayer() ] != aItem->GetLayer() ) + PCB_LAYER_ID original = aItem->GetLayer(); + + if( aLayerMap.count( original ) && aLayerMap.at( original ) != original ) { m_commit->Modify( aItem ); - aItem->SetLayer( aLayerMap[ aItem->GetLayer() ] ); + aItem->SetLayer( aLayerMap.at( original ) ); frame()->GetCanvas()->GetView()->Update( aItem, KIGFX::GEOMETRY ); return true; } @@ -127,7 +130,7 @@ bool GLOBAL_EDIT_TOOL::swapBoardItem( BOARD_ITEM* aItem, PCB_LAYER_ID* aLayerMap int GLOBAL_EDIT_TOOL::SwapLayers( const TOOL_EVENT& aEvent ) { - PCB_LAYER_ID layerMap[PCB_LAYER_ID_COUNT]; + std::map layerMap; DIALOG_SWAP_LAYERS dlg( frame(), layerMap ); diff --git a/pcbnew/tools/global_edit_tool.h b/pcbnew/tools/global_edit_tool.h index 88563dfa0f..c014cdc2a5 100644 --- a/pcbnew/tools/global_edit_tool.h +++ b/pcbnew/tools/global_edit_tool.h @@ -66,7 +66,7 @@ public: int ZonesManager( const TOOL_EVENT& aEvent ); private: - bool swapBoardItem( BOARD_ITEM* aItem, PCB_LAYER_ID* aLayerMap ); + bool swapBoardItem( BOARD_ITEM* aItem, std::map& aLayerMap ); ///< Set up handlers for various events. void setTransitions() override;