kicad/pcbnew/router/router_tool.cpp

415 lines
11 KiB
C++
Raw Normal View History

/*
* KiRouter - a push-and-(sometimes-)shove PCB router
*
* Copyright (C) 2013 CERN
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
2013-09-26 21:53:54 +00:00
*
* 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 3 of the License, or (at your
* option) any later version.
2013-09-26 21:53:54 +00:00
*
* 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.
2013-09-26 21:53:54 +00:00
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.or/licenses/>.
*/
#include <boost/foreach.hpp>
#include <boost/optional.hpp>
#include "class_drawpanel_gal.h"
#include "class_board_item.h"
#include "class_board.h"
#include <wxPcbStruct.h>
#include <view/view_controls.h>
#include <pcbcommon.h>
#include <pcb_painter.h>
#include <tool/context_menu.h>
#include <tool/tool_action.h>
#include "router_tool.h"
#include "pns_segment.h"
#include "pns_router.h"
#include "trace.h"
using namespace KIGFX;
using namespace std;
using boost::optional;
2013-09-26 21:53:54 +00:00
static TOOL_ACTION ACT_AutoEndRoute( "AutoEndRoute", AS_CONTEXT, 'F' );
static TOOL_ACTION ACT_PlaceVia( "PlaceVia", AS_CONTEXT, 'V' );
static TOOL_ACTION ACT_OpenRouteOptions( "OpenRouterOptions", AS_CONTEXT, 'E' );
static TOOL_ACTION ACT_SwitchPosture( "SwitchPosture", AS_CONTEXT, '/' );
static TOOL_ACTION ACT_EndTrack( "SwitchPosture", AS_CONTEXT, WXK_END );
ROUTER_TOOL::ROUTER_TOOL() :
2013-09-26 21:53:54 +00:00
TOOL_INTERACTIVE( "pcbnew.InteractiveRouter" )
{
2013-09-26 21:53:54 +00:00
m_router = NULL;
m_menu = new CONTEXT_MENU;
m_menu->SetTitle( wxT( "Interactive router" ) ); // fixme: not implemented yet. Sorry.
m_menu->Add( wxT( "Cancel" ), 1 );
m_menu->Add( wxT( "New track" ), 2 );
m_menu->Add( wxT( "End track" ), 3 );
m_menu->Add( wxT( "Auto-end track" ), 4 );
m_menu->Add( wxT( "Place via" ), 5 );
m_menu->Add( wxT( "Switch posture" ), 6 );
m_menu->Add( wxT( "Routing options..." ), 7 );
}
ROUTER_TOOL::~ROUTER_TOOL()
{
2013-09-26 21:53:54 +00:00
delete m_router;
}
void ROUTER_TOOL::Reset()
{
2013-09-26 21:53:54 +00:00
if( m_router )
delete m_router;
m_router = new PNS_ROUTER;
2013-09-26 21:53:54 +00:00
TRACEn( 0, "Reset" );
m_router->ClearWorld();
2013-09-26 21:53:54 +00:00
m_router->SetBoard( getModel<BOARD>( PCB_T ) );
m_router->SyncWorld();
2013-09-26 21:53:54 +00:00
if( getView() )
m_router->SetView( getView() );
2013-10-14 18:40:36 +00:00
Go( &ROUTER_TOOL::Main, TOOL_EVENT( TC_COMMAND, TA_ACTION, GetName() ) );
}
2013-09-26 21:53:54 +00:00
int ROUTER_TOOL::getDefaultWidth( int aNetCode )
{
int w, d1, d2;
2013-09-26 21:53:54 +00:00
getNetclassDimensions( aNetCode, w, d1, d2 );
return w;
}
2013-09-26 21:53:54 +00:00
void ROUTER_TOOL::getNetclassDimensions( int aNetCode, int& aWidth,
2013-09-26 21:53:54 +00:00
int& aViaDiameter, int& aViaDrill )
{
2013-09-26 21:53:54 +00:00
BOARD* board = getModel<BOARD>( PCB_T );
NETCLASS* netClass = NULL;
2013-09-26 21:53:54 +00:00
NETINFO_ITEM* ni = board->FindNet( aNetCode );
if( ni )
{
wxString netClassName = ni->GetClassName();
netClass = board->m_NetClasses.Find( netClassName );
}
if( !netClass )
netClass = board->m_NetClasses.GetDefault();
2013-09-26 21:53:54 +00:00
aWidth = netClass->GetTrackWidth();
aViaDiameter = netClass->GetViaDiameter();
aViaDrill = netClass->GetViaDrill();
}
2013-09-26 21:53:54 +00:00
PNS_ITEM* ROUTER_TOOL::pickSingleItem( const VECTOR2I& aWhere, int aNet, int aLayer )
{
int tl = getView()->GetTopLayer();
2013-09-26 21:53:54 +00:00
if( aLayer > 0 )
tl = aLayer;
2013-09-26 21:53:54 +00:00
PNS_ITEM* picked_seg = NULL;
PNS_ITEM* picked_via = NULL;
PNS_ITEMSET candidates = m_router->QueryHoverItems( aWhere );
2013-09-26 21:53:54 +00:00
BOOST_FOREACH( PNS_ITEM* item, candidates.Items() )
{
2013-09-26 21:53:54 +00:00
if( !IsCopperLayer( item->GetLayers().Start() ) )
continue;
if( item->GetParent() && !item->GetParent()->ViewIsVisible() )
continue;
if( aNet < 0 || item->GetNet() == aNet )
{
2013-09-26 21:53:54 +00:00
if( item->OfKind( PNS_ITEM::VIA | PNS_ITEM::SOLID ) )
{
if( item->GetLayers().Overlaps( tl ) || !picked_via )
picked_via = item;
}
else
{
2013-09-26 21:53:54 +00:00
if( item->GetLayers().Overlaps( tl ) || !picked_seg )
picked_seg = item;
}
}
}
if( DisplayOpt.ContrastModeDisplay )
{
2013-09-26 21:53:54 +00:00
if( picked_seg && !picked_seg->GetLayers().Overlaps( tl ) )
picked_seg = NULL;
}
2013-09-26 21:53:54 +00:00
PNS_ITEM* rv = picked_via ? picked_via : picked_seg;
if( rv && aLayer >= 0 && !rv->GetLayers().Overlaps( aLayer ) )
rv = NULL;
2013-09-26 21:53:54 +00:00
if( rv )
TRACE( 0, "%s, layer : %d, tl: %d", rv->GetKindStr().c_str() % rv->GetLayers().Start() %
tl );
return rv;
}
2013-09-26 21:53:54 +00:00
void ROUTER_TOOL::setMsgPanel( bool aEnabled, int aEntry,
const wxString& aUpperMessage, const wxString& aLowerMessage )
{
2013-09-26 21:53:54 +00:00
PCB_EDIT_FRAME* frame = getEditFrame<PCB_EDIT_FRAME> ();
if( m_panelItems.size() <= (unsigned int) aEntry )
m_panelItems.resize( aEntry + 1 );
m_panelItems[aEntry] = MSG_PANEL_ITEM( aUpperMessage, aLowerMessage, BLACK );
frame->SetMsgPanel( m_panelItems );
}
2013-09-26 21:53:54 +00:00
void ROUTER_TOOL::clearMsgPanel()
{
2013-09-26 21:53:54 +00:00
PCB_EDIT_FRAME* frame = getEditFrame<PCB_EDIT_FRAME> ();
frame->ClearMsgPanel();
}
2013-09-26 21:53:54 +00:00
void ROUTER_TOOL::highlightNet( bool aEnabled, int aNetcode )
{
2013-09-26 21:53:54 +00:00
RENDER_SETTINGS* rs = getView()->GetPainter()->GetSettings();
if( aNetcode >= 0 && aEnabled )
rs->SetHighlight( true, aNetcode );
else
rs->SetHighlight( false );
getView()->UpdateAllLayersColor();
}
2013-09-26 21:53:54 +00:00
void ROUTER_TOOL::updateStartItem( TOOL_EVENT& aEvent )
{
2013-09-26 21:53:54 +00:00
VIEW_CONTROLS* ctls = getViewControls();
int tl = getView()->GetTopLayer();
2013-09-26 21:53:54 +00:00
PNS_ITEM* startItem = NULL;
if( aEvent.IsMotion() || aEvent.IsClick() )
{
VECTOR2I p = aEvent.Position();
2013-09-26 21:53:54 +00:00
startItem = pickSingleItem( p );
2013-09-26 21:53:54 +00:00
if( startItem && startItem->GetNet() >= 0 )
{
bool dummy;
2013-09-26 21:53:54 +00:00
VECTOR2I cursorPos = m_router->SnapToItem( startItem, p, dummy );
ctls->ForceCursorPosition( true, cursorPos );
m_startSnapPoint = cursorPos;
2013-09-26 21:53:54 +00:00
if( startItem->GetLayers().IsMultilayer() )
m_startLayer = tl;
else
m_startLayer = startItem->GetLayers().Start();
2013-09-26 21:53:54 +00:00
m_startItem = startItem;
2013-09-26 21:53:54 +00:00
}
else
{
m_startItem = NULL;
m_startSnapPoint = p;
m_startLayer = tl;
2013-09-26 21:53:54 +00:00
ctls->ForceCursorPosition( false );
}
}
}
2013-09-26 21:53:54 +00:00
void ROUTER_TOOL::updateEndItem( TOOL_EVENT& aEvent )
{
2013-09-26 21:53:54 +00:00
VIEW_CONTROLS* ctls = getViewControls();
VECTOR2I p = aEvent.Position();
int layer;
2013-09-26 21:53:54 +00:00
if( m_router->GetCurrentNet() < 0 || !m_startItem )
{
m_endItem = NULL;
m_endSnapPoint = p;
return;
}
bool dummy;
2013-09-26 21:53:54 +00:00
if( m_router->IsPlacingVia() )
layer = -1;
2013-09-26 21:53:54 +00:00
else
layer = m_router->GetCurrentLayer();
2013-09-26 21:53:54 +00:00
PNS_ITEM* endItem = pickSingleItem( p, m_startItem->GetNet(), layer );
if( endItem )
{
2013-09-26 21:53:54 +00:00
VECTOR2I cursorPos = m_router->SnapToItem( endItem, p, dummy );
ctls->ForceCursorPosition( true, cursorPos );
m_endItem = endItem;
m_endSnapPoint = cursorPos;
2013-09-26 21:53:54 +00:00
}
else
{
m_endItem = NULL;
m_endSnapPoint = p;
2013-09-26 21:53:54 +00:00
ctls->ForceCursorPosition( false );
}
2013-09-26 21:53:54 +00:00
if( m_endItem )
TRACE( 0, "%s, layer : %d", m_endItem->GetKindStr().c_str() %
m_endItem->GetLayers().Start() );
}
2013-09-26 21:53:54 +00:00
void ROUTER_TOOL::startRouting()
{
2013-09-26 21:53:54 +00:00
VIEW_CONTROLS* ctls = getViewControls();
int width = getDefaultWidth( m_startItem ? m_startItem->GetNet() : -1 );
if( m_startItem && m_startItem->OfKind( PNS_ITEM::SEGMENT ) )
width = static_cast<PNS_SEGMENT*>( m_startItem )->GetWidth();
2013-09-26 21:53:54 +00:00
m_router->SetCurrentWidth( width );
m_router->SwitchLayer( m_startLayer );
2013-09-26 21:53:54 +00:00
getEditFrame<PCB_EDIT_FRAME>()->SetTopLayer( m_startLayer );
2013-09-26 21:53:54 +00:00
if( m_startItem && m_startItem->GetNet() >= 0 )
highlightNet( true, m_startItem->GetNet() );
ctls->ForceCursorPosition( false );
ctls->SetAutoPan( true );
m_router->StartRouting( m_startSnapPoint, m_startItem );
m_endItem = NULL;
m_endSnapPoint = m_startSnapPoint;
while( OPT_TOOL_EVENT evt = Wait() )
{
2013-09-26 21:53:54 +00:00
if( evt->IsCancel() )
break;
else if( evt->IsMotion() )
{
updateEndItem( *evt );
2013-09-26 21:53:54 +00:00
m_router->Move( m_endSnapPoint, m_endItem );
}
else if( evt->IsClick( BUT_LEFT ) )
2013-09-26 21:53:54 +00:00
{
updateEndItem( *evt );
2013-09-26 21:53:54 +00:00
if( m_router->FixRoute( m_endSnapPoint, m_endItem ) )
break;
2013-09-26 21:53:54 +00:00
m_router->Move( m_endSnapPoint, m_endItem );
}
else if( evt->IsKeyUp() )
{
switch( evt->KeyCode() )
{
2013-09-26 21:53:54 +00:00
case 'V':
{
int w, diameter, drill;
getNetclassDimensions( m_router->GetCurrentNet(), w, diameter, drill );
m_router->SetCurrentViaDiameter( diameter );
m_router->SetCurrentViaDrill( drill );
m_router->ToggleViaPlacement();
getEditFrame<PCB_EDIT_FRAME>()->SetTopLayer( m_router->GetCurrentLayer() );
m_router->Move( m_endSnapPoint, m_endItem );
break;
}
2013-09-26 21:53:54 +00:00
case '/':
m_router->FlipPosture();
break;
case '+':
case '=':
m_router->SwitchLayer( m_router->NextCopperLayer( true ) );
updateEndItem( *evt );
getEditFrame<PCB_EDIT_FRAME>()->SetTopLayer( m_router->GetCurrentLayer() );
m_router->Move( m_endSnapPoint, m_endItem );
break;
case '-':
m_router->SwitchLayer( m_router->NextCopperLayer( false ) );
getEditFrame<PCB_EDIT_FRAME>()->SetTopLayer( m_router->GetCurrentLayer() );
m_router->Move( m_endSnapPoint, m_endItem );
break;
}
}
}
2013-09-26 21:53:54 +00:00
if( m_router->RoutingInProgress() )
m_router->StopRouting();
2013-09-26 21:53:54 +00:00
ctls->SetAutoPan( false );
ctls->ForceCursorPosition( false );
highlightNet( false );
}
int ROUTER_TOOL::Main( TOOL_EVENT& aEvent )
{
2013-09-26 21:53:54 +00:00
VIEW_CONTROLS* ctls = getViewControls();
2013-09-26 21:53:54 +00:00
// SetContextMenu ( m_menu );
// setMsgPanel(true, 0, wxT("KiRouter"), wxT("Pick an item to start routing"));
2013-09-26 21:53:54 +00:00
ctls->SetSnapping( true );
ctls->ShowCursor( true );
// Main loop: keep receiving events
while( OPT_TOOL_EVENT evt = Wait() )
{
if( evt->IsCancel() )
2013-09-26 21:53:54 +00:00
break; // Finish
else if( evt->IsMotion() )
updateStartItem( *evt );
else if( evt->IsClick( BUT_LEFT ) )
{
updateStartItem( *evt );
2013-09-26 21:53:54 +00:00
startRouting();
}
}
2013-09-26 21:53:54 +00:00
// clearMsgPanel();
// Restore the default settings
ctls->SetAutoPan( false );
ctls->ShowCursor( false );
return 0;
}