Fp editor: fix crash when converting a rect to lines

Avoid adding menuitems in convert context menus having no meaning in fp editor

Fixes #6158
https://gitlab.com/kicad/code/kicad/issues/6158
This commit is contained in:
jean-pierre charras 2020-10-25 13:25:46 +01:00
parent 34179cd039
commit 13baea4900
1 changed files with 61 additions and 16 deletions

View File

@ -33,6 +33,7 @@
#include <confirm.h> #include <confirm.h>
#include <menus_helpers.h> #include <menus_helpers.h>
#include <pcb_edit_frame.h> #include <pcb_edit_frame.h>
#include <footprint_edit_frame.h>
#include <trigo.h> #include <trigo.h>
#include <tool/tool_manager.h> #include <tool/tool_manager.h>
#include <tools/edit_tool.h> #include <tools/edit_tool.h>
@ -91,9 +92,16 @@ bool CONVERT_TOOL::Init()
if( m_frame->IsType( FRAME_PCB_EDITOR ) ) if( m_frame->IsType( FRAME_PCB_EDITOR ) )
m_menu->AddItem( PCB_ACTIONS::convertToZone, anyLines ); m_menu->AddItem( PCB_ACTIONS::convertToZone, anyLines );
if( m_frame->IsType( FRAME_PCB_EDITOR ) )
m_menu->AddItem( PCB_ACTIONS::convertToKeepout, anyLines ); m_menu->AddItem( PCB_ACTIONS::convertToKeepout, anyLines );
m_menu->AddItem( PCB_ACTIONS::convertToLines, anyPolys ); m_menu->AddItem( PCB_ACTIONS::convertToLines, anyPolys );
// Currently the code exists, but tracks are not really existing in footprints
// only segments on copper layers
if( m_frame->IsType( FRAME_PCB_EDITOR ) )
m_menu->AddItem( PCB_ACTIONS::convertToTracks, anyPolys ); m_menu->AddItem( PCB_ACTIONS::convertToTracks, anyPolys );
m_menu->AddItem( PCB_ACTIONS::convertToArc, lineToArc ); m_menu->AddItem( PCB_ACTIONS::convertToArc, lineToArc );
for( std::shared_ptr<ACTION_MENU>& subMenu : m_selectionTool->GetToolMenu().GetSubMenus() ) for( std::shared_ptr<ACTION_MENU>& subMenu : m_selectionTool->GetToolMenu().GetSubMenus() )
@ -463,6 +471,12 @@ int CONVERT_TOOL::PolyToLines( const TOOL_EVENT& aEvent )
}; };
BOARD_COMMIT commit( m_frame ); BOARD_COMMIT commit( m_frame );
FOOTPRINT_EDIT_FRAME* fpEditor = dynamic_cast<FOOTPRINT_EDIT_FRAME*>( m_frame );
MODULE* footprint = nullptr;
if( fpEditor )
footprint = fpEditor->GetBoard()->GetFirstModule();
for( EDA_ITEM* item : selection ) for( EDA_ITEM* item : selection )
{ {
@ -473,6 +487,19 @@ int CONVERT_TOOL::PolyToLines( const TOOL_EVENT& aEvent )
if( aEvent.IsAction( &PCB_ACTIONS::convertToLines ) ) if( aEvent.IsAction( &PCB_ACTIONS::convertToLines ) )
{ {
for( SEG& seg : segs ) for( SEG& seg : segs )
{
if( fpEditor )
{
FP_SHAPE* graphic = new FP_SHAPE( footprint, S_SEGMENT );
graphic->SetLayer( layer );
graphic->SetStart( wxPoint( seg.A ) );
graphic->SetStart0( wxPoint( seg.A ) );
graphic->SetEnd( wxPoint( seg.B ) );
graphic->SetEnd0( wxPoint( seg.B ) );
commit.Add( graphic );
}
else
{ {
PCB_SHAPE* graphic = new PCB_SHAPE; PCB_SHAPE* graphic = new PCB_SHAPE;
@ -480,10 +507,10 @@ int CONVERT_TOOL::PolyToLines( const TOOL_EVENT& aEvent )
graphic->SetLayer( layer ); graphic->SetLayer( layer );
graphic->SetStart( wxPoint( seg.A ) ); graphic->SetStart( wxPoint( seg.A ) );
graphic->SetEnd( wxPoint( seg.B ) ); graphic->SetEnd( wxPoint( seg.B ) );
commit.Add( graphic ); commit.Add( graphic );
} }
} }
}
else else
{ {
PCB_BASE_EDIT_FRAME* frame = getEditFrame<PCB_BASE_EDIT_FRAME>(); PCB_BASE_EDIT_FRAME* frame = getEditFrame<PCB_BASE_EDIT_FRAME>();
@ -492,6 +519,24 @@ int CONVERT_TOOL::PolyToLines( const TOOL_EVENT& aEvent )
if( !IsCopperLayer( layer ) ) if( !IsCopperLayer( layer ) )
layer = frame->SelectLayer( F_Cu, LSET::AllNonCuMask() ); layer = frame->SelectLayer( F_Cu, LSET::AllNonCuMask() );
// I am really unsure converting a polygon to "tracks" (i.e. segments on
// copper layers) make sense for footprints, but anyway this code exists
if( fpEditor )
{
// Creating segments on copper layer
for( SEG& seg : segs )
{
FP_SHAPE* graphic = new FP_SHAPE( footprint, S_SEGMENT );
graphic->SetLayer( layer );
graphic->SetStart( wxPoint( seg.A ) );
graphic->SetStart0( wxPoint( seg.A ) );
graphic->SetEnd( wxPoint( seg.B ) );
graphic->SetEnd0( wxPoint( seg.B ) );
commit.Add( graphic );
}
}
else
{
// Creating tracks // Creating tracks
for( SEG& seg : segs ) for( SEG& seg : segs )
{ {
@ -500,11 +545,11 @@ int CONVERT_TOOL::PolyToLines( const TOOL_EVENT& aEvent )
track->SetLayer( layer ); track->SetLayer( layer );
track->SetStart( wxPoint( seg.A ) ); track->SetStart( wxPoint( seg.A ) );
track->SetEnd( wxPoint( seg.B ) ); track->SetEnd( wxPoint( seg.B ) );
commit.Add( track ); commit.Add( track );
} }
} }
} }
}
commit.Push( _( "Convert polygons to lines" ) ); commit.Push( _( "Convert polygons to lines" ) );