kicad/pcbnew/router/router_tool.cpp

474 lines
13 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_draw_panel_gal.h"
#include "class_board_item.h"
#include "class_board.h"
#include <wxPcbStruct.h>
#include <id.h>
#include <pcbnew_id.h>
#include <view/view_controls.h>
#include <pcbcommon.h>
#include <pcb_painter.h>
#include <tool/context_menu.h>
#include <tools/common_actions.h>
#include <ratsnest_data.h>
#include "router_tool.h"
#include "pns_segment.h"
#include "pns_router.h"
#include "trace.h"
using namespace KIGFX;
using boost::optional;
//static TOOL_ACTION ACT_AutoEndRoute( "pcbnew.InteractiveRouter.AutoEndRoute", AS_CONTEXT, 'G' );
//static TOOL_ACTION ACT_PlaceVia( "pcbnew.InteractiveRouter.PlaceVia", AS_CONTEXT, 'V' );
//static TOOL_ACTION ACT_OpenRouteOptions( "pcbnew.InteractiveRouter.OpenRouterOptions", AS_CONTEXT, 'T' );
//static TOOL_ACTION ACT_SwitchPosture( "pcbnew.InteractiveRouter.SwitchPosture", AS_CONTEXT, '/' );
//static TOOL_ACTION ACT_EndTrack( "pcbnew.InteractiveRouter.EndTrack", 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( RESET_REASON aReason )
{
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();
2014-01-31 15:08:20 +00:00
m_needsSync = false;
2013-09-26 21:53:54 +00:00
if( getView() )
m_router->SetView( getView() );
Go( &ROUTER_TOOL::Main, COMMON_ACTIONS::routerActivate.MakeEvent() );
}
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;
2014-02-03 17:09:17 +00:00
if( item->GetParent() && !item->GetParent()->ViewIsVisible() &&
!item->GetParent()->IsSelected() )
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;
2014-02-13 19:23:28 +00:00
m_endSnapPoint = ctls->GetCursorPosition();
2013-09-26 21:53:54 +00:00
ctls->ForceCursorPosition( false );
}
2013-09-26 21:53:54 +00:00
// Draw ratsnest for the currently routed track
RN_DATA* ratsnest = getModel<BOARD>( PCB_T )->GetRatsnest();
ratsnest->ClearSimple();
if( ( m_endItem == NULL || m_endItem == m_startItem ) && m_startItem->GetNet() > 0 )
{
// The ending node has to be first, so the line for the track is drawn first
ratsnest->AddSimple( m_endSnapPoint, m_startItem->GetNet() );
// Those nodes are added just to force ratsnest not to drawn
// lines to already routed parts of the track
const PICKED_ITEMS_LIST& changes = m_router->GetLastChanges();
for( unsigned int i = 0; i < changes.GetCount(); ++i )
{
// Block the new tracks, do not handle tracks that were moved
// (moved tracks are saved in the undo buffer with UR_DELETED status instead)
if( changes.GetPickedItemStatus( i ) == UR_NEW )
ratsnest->AddBlocked( static_cast<BOARD_CONNECTED_ITEM*>( changes.GetPickedItem( i ) ) );
}
// Also the origin of the new track should be skipped in the ratsnest shown for the routed track
ratsnest->AddBlocked( static_cast<BOARD_ITEM*>( m_startItem->GetParent() ) );
}
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()
{
bool saveUndoBuffer = true;
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->Action() == TA_UNDO_REDO )
{
saveUndoBuffer = false;
break;
}
2013-09-26 21:53:54 +00:00
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->IsKeyPressed() )
2013-09-26 21:53:54 +00:00
{
switch( std::toupper( 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;
}
}
}
m_router->StopRouting();
2013-09-26 21:53:54 +00:00
if( saveUndoBuffer )
{
// Save the recent changes in the undo buffer
2014-01-31 15:08:20 +00:00
getEditFrame<PCB_EDIT_FRAME>()->SaveCopyInUndoList( m_router->GetLastChanges(),
UR_UNSPECIFIED );
m_router->ClearLastChanges();
getEditFrame<PCB_EDIT_FRAME>()->OnModify();
}
else
{
// It was interrupted by TA_UNDO_REDO event, so we have to sync the world now
2014-01-31 15:08:20 +00:00
m_needsSync = true;
}
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"));
getEditFrame<PCB_EDIT_FRAME>()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Add tracks" ) );
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() )
{
2014-01-31 15:08:20 +00:00
if( m_needsSync )
{
m_router->SyncWorld();
m_needsSync = false;
}
if( evt->IsCancel() )
2013-09-26 21:53:54 +00:00
break; // Finish
else if( evt->Action() == TA_UNDO_REDO )
2014-01-31 15:08:20 +00:00
m_needsSync = true;
2013-09-26 21:53:54 +00:00
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 );
ctls->ForceCursorPosition( false );
getEditFrame<PCB_EDIT_FRAME>()->SetToolID( ID_NO_TOOL_SELECTED, wxCURSOR_DEFAULT, wxEmptyString );
return 0;
}