pns_router.cpp 21.3 KB
Newer Older
1 2 3
/*
 * KiRouter - a push-and-(sometimes-)shove PCB router
 *
4
 * Copyright (C) 2013-2014 CERN
5
 * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6
 *
7 8 9 10
 * 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.
11
 *
12 13 14 15
 * 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.
16
 *
17
 * You should have received a copy of the GNU General Public License along
18
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <cstdio>
#include <vector>

#include <boost/foreach.hpp>

#include <view/view.h>
#include <view/view_item.h>
#include <view/view_group.h>
#include <gal/graphics_abstraction_layer.h>

#include <pcb_painter.h>

#include <geometry/shape.h>
#include <geometry/shape_line_chain.h>
#include <geometry/shape_rect.h>
#include <geometry/shape_circle.h>
37

38 39 40 41 42 43 44
#include "trace.h"
#include "pns_node.h"
#include "pns_line_placer.h"
#include "pns_line.h"
#include "pns_solid.h"
#include "pns_utils.h"
#include "pns_router.h"
45 46
#include "pns_shove.h"
#include "pns_dragger.h"
47 48 49 50

#include <router/router_preview_item.h>

#include <class_board.h>
51
#include <class_board_connected_item.h>
52
#include <class_module.h>
53
#include <class_track.h>
54
#include <ratsnest_data.h>
55
#include <layers_id_colors_and_visibility.h>
56

57
// an ugly singleton for drawing debug items within the router context.
58 59 60 61 62 63 64 65 66 67 68 69 70
// To be fixed sometime in the future.
static PNS_ROUTER* theRouter;

class PCBNEW_CLEARANCE_FUNC : public PNS_CLEARANCE_FUNC
{
public:
    PCBNEW_CLEARANCE_FUNC( BOARD* aBoard )
    {
        m_clearanceCache.resize( aBoard->GetNetCount() );

        for( unsigned int i = 0; i < aBoard->GetNetCount(); i++ )
        {
            NETINFO_ITEM* ni = aBoard->FindNet( i );
71 72 73
            if( ni == NULL )
                continue;

74
            wxString netClassName = ni->GetClassName();
75
            NETCLASSPTR nc = aBoard->GetDesignSettings().m_NetClasses.Find( netClassName );
76 77 78 79 80 81 82 83 84
            int clearance = nc->GetClearance();
            m_clearanceCache[i] = clearance;
            TRACE( 1, "Add net %d netclass %s clearance %d", i % netClassName.mb_str() %
                    clearance );
        }

        m_defaultClearance = 254000;    // aBoard->m_NetClasses.Find ("Default clearance")->GetClearance();
    }

85
    int localPadClearance( const PNS_ITEM* aItem ) const
86
    {
87
        if( !aItem->Parent() || aItem->Parent()->Type() != PCB_PAD_T )
88 89
            return 0;

90
        const D_PAD* pad = static_cast<D_PAD*>( aItem->Parent() );
91 92 93 94

        return pad->GetLocalClearance();
    }

95
    int operator()( const PNS_ITEM* aA, const PNS_ITEM* aB )
96
    {
97 98 99 100
        int net_a = aA->Net();
        int cl_a = ( net_a >= 0 ? m_clearanceCache[net_a] : m_defaultClearance );
        int net_b = aB->Net();
        int cl_b = ( net_b >= 0 ? m_clearanceCache[net_b] : m_defaultClearance );
101

102 103
        int pad_a = localPadClearance( aA );
        int pad_b = localPadClearance( aB );
104

105 106
        cl_a = std::max( cl_a, pad_a );
        cl_b = std::max( cl_b, pad_b );
107 108 109 110 111

        return std::max( cl_a, cl_b );
    }

private:
112
    std::vector<int> m_clearanceCache;
113
    int m_defaultClearance;
114 115
};

116

117 118
PNS_ITEM* PNS_ROUTER::syncPad( D_PAD* aPad )
{
119
    PNS_LAYERSET layers( 0, MAX_CU_LAYERS - 1 );
120 121 122 123

    switch( aPad->GetAttribute() )
    {
    case PAD_STANDARD:
124
        layers = PNS_LAYERSET( 0, MAX_CU_LAYERS - 1 ); // TODO necessary? it is already initialized
125 126 127 128
        break;

    case PAD_SMD:
    case PAD_CONN:
129
        {
Dick Hollenbeck's avatar
Dick Hollenbeck committed
130 131 132
            LSET lmsk = aPad->GetLayerSet();

            for( int i = 0; i < MAX_CU_LAYERS; i++ )
133
            {
Dick Hollenbeck's avatar
Dick Hollenbeck committed
134 135 136 137 138
                if( lmsk[i] )
                {
                    layers = PNS_LAYERSET( i );
                    break;
                }
139
            }
140
        }
141
        break;
142 143 144 145 146 147 148 149 150

    default:
        TRACE( 0, "unsupported pad type 0x%x", aPad->GetAttribute() );
        return NULL;
    }

    PNS_SOLID* solid = new PNS_SOLID;

    solid->SetLayers( layers );
151
    solid->SetNet( aPad->GetNetCode() );
152 153
    solid->SetParent( aPad );

154 155 156 157 158 159
    wxPoint wx_c = aPad->GetPosition();
    wxSize  wx_sz = aPad->GetSize();

    VECTOR2I c( wx_c.x, wx_c.y );
    VECTOR2I sz( wx_sz.x, wx_sz.y );

160
    solid->SetPos( c );
161 162

    double orient = aPad->GetOrientation() / 10.0;
163
    bool nonOrtho = false;
164 165 166 167 168

    if( orient == 90.0 || orient == 270.0 )
        sz = VECTOR2I( sz.y, sz.x );
    else if( orient != 0.0 && orient != 180.0 )
    {
169
        // rotated pads are replaced by for the moment by circles due to my laziness ;)
170
        solid->SetShape( new SHAPE_CIRCLE( c, std::min( sz.x, sz.y ) / 2 ) );
171
        nonOrtho = true;
172 173
    }

174
    if( !nonOrtho )
175
    {
176 177 178
        switch( aPad->GetShape() )
        {
        case PAD_CIRCLE:
179
            solid->SetShape( new SHAPE_CIRCLE( c, sz.x / 2 ) );
180
            break;
181

182 183 184
        case PAD_OVAL:
            if( sz.x == sz.y )
                solid->SetShape( new SHAPE_CIRCLE( c, sz.x / 2 ) );
185 186
            else
            {
187
                VECTOR2I delta;
188

189 190
                if( sz.x > sz.y )
                    delta = VECTOR2I( ( sz.x - sz.y ) / 2, 0 );
191
                else
192
                    delta = VECTOR2I( 0, ( sz.y - sz.x ) / 2 );
193

194
                SHAPE_SEGMENT* shape = new SHAPE_SEGMENT( c - delta, c + delta,
195
                                                          std::min( sz.x, sz.y ) );
196 197 198 199 200
                solid->SetShape( shape );
            }
            break;

        case PAD_RECT:
201
            solid->SetShape( new SHAPE_RECT( c - sz / 2, sz.x, sz.y ) );
202
            break;
203

204 205 206
        default:
            TRACEn( 0, "unsupported pad shape" );
            delete solid;
207

208 209
            return NULL;
        }
210 211 212
    }

    return solid;
213
}
214 215 216


PNS_ITEM* PNS_ROUTER::syncTrack( TRACK* aTrack )
217
{
218
    PNS_SEGMENT* s =
219
        new PNS_SEGMENT( SEG( aTrack->GetStart(), aTrack->GetEnd() ), aTrack->GetNetCode() );
220

221 222 223 224
    s->SetWidth( aTrack->GetWidth() );
    s->SetLayers( PNS_LAYERSET( aTrack->GetLayer() ) );
    s->SetParent( aTrack );
    return s;
225 226 227
}


228
PNS_ITEM* PNS_ROUTER::syncVia( VIA* aVia )
229 230 231
{
    PNS_VIA* v = new PNS_VIA(
            aVia->GetPosition(),
232
            PNS_LAYERSET( 0, MAX_CU_LAYERS - 1 ),
233
            aVia->GetWidth(),
234
            aVia->GetDrillValue(),
235
            aVia->GetNetCode() );
236 237

    v->SetParent( aVia );
238

239
    return v;
240 241
}

242 243

void PNS_ROUTER::SetBoard( BOARD* aBoard )
244
{
245 246
    m_board = aBoard;
    TRACE( 1, "m_board = %p\n", m_board );
247
}
248 249


250 251
int PNS_ROUTER::NextCopperLayer( bool aUp )
{
Dick Hollenbeck's avatar
Dick Hollenbeck committed
252 253
    LSET        mask = m_board->GetEnabledLayers() & m_board->GetVisibleLayers();
    LAYER_NUM   l = m_currentLayer;
254

255 256
    do
    {
257 258
        l += ( aUp ? 1 : -1 );

Dick Hollenbeck's avatar
Dick Hollenbeck committed
259 260
        if( l >= MAX_CU_LAYERS )
            l = 0;
261

Dick Hollenbeck's avatar
Dick Hollenbeck committed
262
        if( l < 0 )
263
            l = MAX_CU_LAYERS - 1;
264

Dick Hollenbeck's avatar
Dick Hollenbeck committed
265
        if( mask[l] )
266
            return l;
267 268
    }
    while( l != m_currentLayer );
269

270
    return l;
271 272
}

273

274 275
void PNS_ROUTER::SyncWorld()
{
276 277 278 279 280 281 282 283
    if( !m_board )
    {
        TRACEn( 0, "No board attached, aborting sync." );
        return;
    }

    ClearWorld();

284
    int worstClearance = m_board->GetDesignSettings().GetBiggestClearanceValue();
285

286 287 288
    m_clearanceFunc = new PCBNEW_CLEARANCE_FUNC( m_board );
    m_world = new PNS_NODE();
    m_world->SetClearanceFunctor( m_clearanceFunc );
289
    m_world->SetMaxClearance( 4 * worstClearance );
290

291 292 293 294 295
    for( MODULE* module = m_board->m_Modules; module; module = module->Next() )
    {
        for( D_PAD* pad = module->Pads(); pad; pad = pad->Next() )
        {
            PNS_ITEM* solid = syncPad( pad );
296

297 298 299
            if( solid )
                m_world->Add( solid );
        }
300 301 302 303 304 305 306 307 308 309
    }

    for( TRACK* t = m_board->m_Track; t; t = t->Next() )
    {
        KICAD_T type = t->Type();
        PNS_ITEM* item = NULL;

        if( type == PCB_TRACE_T )
            item = syncTrack( t );
        else if( type == PCB_VIA_T )
310
            item = syncVia( static_cast<VIA*>( t ) );
311 312 313 314

        if( item )
            m_world->Add( item );
    }
315 316
}

317

318 319
PNS_ROUTER::PNS_ROUTER()
{
320
    theRouter = this;
321

322
    m_clearanceFunc = NULL;
323

324 325 326
    m_currentLayer = 1;
    m_placingVia = false;
    m_currentNet = -1;
327 328 329 330 331
    m_state = IDLE;
    m_world = NULL;
    m_placer = NULL;
    m_previewItems = NULL;
    m_board = NULL;
332
    m_dragger = NULL;
333 334 335
}


336
void PNS_ROUTER::SetView( KIGFX::VIEW* aView )
337
{
338 339 340 341 342 343 344
    if( m_previewItems )
    {
        m_previewItems->FreeItems();
        delete m_previewItems;
    }

    m_view = aView;
345
    m_previewItems = new KIGFX::VIEW_GROUP( m_view );
346 347 348
    m_previewItems->SetLayer( ITEM_GAL_LAYER( GP_OVERLAY ) );
    m_view->Add( m_previewItems );
    m_previewItems->ViewSetVisible( true );
349 350
}

351 352

PNS_ROUTER* PNS_ROUTER::GetInstance()
353
{
354
    return theRouter;
355 356
}

357

358 359
PNS_ROUTER::~PNS_ROUTER()
{
360 361
    ClearWorld();
    theRouter = NULL;
362 363 364

    if( m_previewItems )
        delete m_previewItems;
365 366
}

367

368 369
void PNS_ROUTER::ClearWorld()
{
370 371 372 373 374 375 376 377
    if( m_world )
        delete m_world;

    if( m_clearanceFunc )
        delete m_clearanceFunc;

    if( m_placer )
        delete m_placer;
378

Maciej Suminski's avatar
Maciej Suminski committed
379 380 381
    if( m_previewItems )
        delete m_previewItems;

382 383 384
    m_clearanceFunc = NULL;
    m_world = NULL;
    m_placer = NULL;
Maciej Suminski's avatar
Maciej Suminski committed
385
    m_previewItems = NULL;
386 387
}

388

389 390
bool PNS_ROUTER::RoutingInProgress() const
{
391
    return m_state != IDLE;
392 393 394
}


395
const PNS_ITEMSET PNS_ROUTER::QueryHoverItems( const VECTOR2I& aP )
396
{
397 398 399
    if( m_state == IDLE )
        return m_world->HitTest( aP );
    else
400 401 402 403 404
    {
        //assert ( m_placer->GetCurrentNode()->checkExists() );
        //TRACE(0,"query-hover [%p]", m_placer->GetCurrentNode());
        return m_placer->CurrentNode()->HitTest( aP );
    }
405 406 407
}


408
const VECTOR2I PNS_ROUTER::SnapToItem( PNS_ITEM* aItem, VECTOR2I aP, bool& aSplitsSegment )
409 410 411
{
    VECTOR2I anchor;

412
    if( !aItem )
413 414 415 416 417
    {
        aSplitsSegment = false;
        return aP;
    }

418
    switch( aItem->Kind() )
419 420
    {
    case PNS_ITEM::SOLID:
421
        anchor = static_cast<PNS_SOLID*>( aItem )->Pos();
422 423 424 425
        aSplitsSegment = false;
        break;

    case PNS_ITEM::VIA:
426
        anchor = static_cast<PNS_VIA*>( aItem )->Pos();
427 428 429 430
        aSplitsSegment = false;
        break;

    case PNS_ITEM::SEGMENT:
431 432 433 434
    {
        PNS_SEGMENT* seg = static_cast<PNS_SEGMENT*>( aItem );
        const SEG& s = seg->Seg();
        int w = seg->Width();
435

436
        aSplitsSegment = false;
437

438 439 440 441 442 443 444 445
        if( ( aP - s.A ).EuclideanNorm() < w / 2 )
            anchor = s.A;
        else if( ( aP - s.B ).EuclideanNorm() < w / 2 )
            anchor = s.B;
        else
        {
            anchor = s.NearestPoint( aP );
            aSplitsSegment = true;
446 447
        }

448 449 450
        break;
    }

451 452 453 454 455
    default:
        break;
    }

    return anchor;
456 457 458
}


459
bool PNS_ROUTER::StartDragging( const VECTOR2I& aP, PNS_ITEM* aStartItem )
460
{
461
    if( !aStartItem || aStartItem->OfKind( PNS_ITEM::SOLID ) )
462
        return false;
463

Maciej Suminski's avatar
Maciej Suminski committed
464
    m_dragger = new PNS_DRAGGER( this );
465
    m_dragger->SetWorld( m_world );
466

467 468
    if( m_dragger->Start ( aP, aStartItem ) )
        m_state = DRAG_SEGMENT;
469
    else
470
    {
471 472 473 474
        delete m_dragger;
        m_state = IDLE;
        return false;
    }
475

476 477
    return true;
}
478 479


480 481
bool PNS_ROUTER::StartRouting( const VECTOR2I& aP, PNS_ITEM* aStartItem )
{
482
    m_state = ROUTE_TRACK;
483

484 485
    m_placer = new PNS_LINE_PLACER( this );
    m_placer->SetLayer( m_currentLayer );
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

    const BOARD_DESIGN_SETTINGS& dsnSettings = m_board->GetDesignSettings();

    if( dsnSettings.UseNetClassTrack() && aStartItem != NULL ) // netclass value
    {
        m_settings.SetTrackWidth( aStartItem->Parent()->GetNetClass()->GetTrackWidth() );
    }
    else
    {
        m_settings.SetTrackWidth( dsnSettings.GetCurrentTrackWidth() );
    }

    if( dsnSettings.UseNetClassVia() && aStartItem != NULL )   // netclass value
    {
        m_settings.SetViaDiameter( aStartItem->Parent()->GetNetClass()->GetViaDiameter() );
        m_settings.SetViaDrill( aStartItem->Parent()->GetNetClass()->GetViaDrill() );
    }
    else
    {
        m_settings.SetViaDiameter( dsnSettings.GetCurrentViaSize() );
        m_settings.SetViaDrill( dsnSettings.GetCurrentViaDrill() );
    }

    m_placer->UpdateSizes( m_settings );
510 511 512
    m_placer->Start( aP, aStartItem );
    m_currentEnd = aP;
    m_currentEndItem = NULL;
513

514
    return true;
515 516
}

517

518
const VECTOR2I PNS_ROUTER::CurrentEnd() const
519
{
520
    return m_currentEnd;
521 522
}

523

524
void PNS_ROUTER::eraseView()
525
{
526
    BOOST_FOREACH( BOARD_ITEM* item, m_hiddenItems )
Dick Hollenbeck's avatar
Dick Hollenbeck committed
527
    {
528 529 530
        item->ViewSetVisible( true );
    }

531 532
    m_hiddenItems.clear();

533
    if( m_previewItems )
534
    {
535
        m_previewItems->FreeItems();
536 537
        m_previewItems->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
    }
538 539
}

540

541
void PNS_ROUTER::DisplayItem( const PNS_ITEM* aItem, int aColor, int aClearance )
542
{
543
    ROUTER_PREVIEW_ITEM* pitem = new ROUTER_PREVIEW_ITEM( aItem, m_previewItems );
544

545 546
    if( aColor >= 0 )
        pitem->SetColor( KIGFX::COLOR4D ( aColor ) );
547

548 549
    if( aClearance >= 0 )
        pitem->SetClearance( aClearance );
550 551

    m_previewItems->Add( pitem );
552 553

    pitem->ViewSetVisible( true );
554
    m_previewItems->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY | KIGFX::VIEW_ITEM::APPEARANCE );
555 556
}

557

558 559
void PNS_ROUTER::DisplayItems( const PNS_ITEMSET& aItems )
{
560 561
    BOOST_FOREACH( const PNS_ITEM *item, aItems.CItems() )
        DisplayItem( item );
562
}
563

564

565
void PNS_ROUTER::DisplayDebugLine( const SHAPE_LINE_CHAIN& aLine, int aType, int aWidth )
566
{
567 568
    ROUTER_PREVIEW_ITEM* pitem = new ROUTER_PREVIEW_ITEM( NULL, m_previewItems );

569
    pitem->Line( aLine, aWidth, aType );
570 571
    m_previewItems->Add( pitem );
    pitem->ViewSetVisible( true );
572
    m_previewItems->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY | KIGFX::VIEW_ITEM::APPEARANCE );
573 574 575
}


576
void PNS_ROUTER::DisplayDebugPoint( const VECTOR2I aPos, int aType )
577
{
578
    ROUTER_PREVIEW_ITEM* pitem = new ROUTER_PREVIEW_ITEM( NULL, m_previewItems );
579

580
    pitem->Point( aPos, aType );
581 582 583
    m_previewItems->Add( pitem );
    pitem->ViewSetVisible( true );
    m_previewItems->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY | KIGFX::VIEW_ITEM::APPEARANCE );
584 585
}

586 587

void PNS_ROUTER::Move( const VECTOR2I& aP, PNS_ITEM* endItem )
588
{
589 590
    m_currentEnd = aP;
    m_currentEndItem = endItem;
591

592
    switch( m_state )
593
    {
594 595 596 597 598
        case ROUTE_TRACK:
            movePlacing( aP, endItem );
            break;

        case DRAG_SEGMENT:
Maciej Suminski's avatar
Maciej Suminski committed
599
            moveDragging( aP, endItem );
600
            break;
601

Dick Hollenbeck's avatar
Dick Hollenbeck committed
602 603
        default:
            break;
604
    }
605
}
606 607


608
void PNS_ROUTER::moveDragging( const VECTOR2I& aP, PNS_ITEM* aEndItem )
609 610
{
    eraseView();
611

612 613
    m_dragger->Drag( aP );
    PNS_ITEMSET dragged = m_dragger->Traces();
614

615
    updateView ( m_dragger->CurrentNode(), dragged );
616
}
617

618 619 620

void PNS_ROUTER::markViolations( PNS_NODE* aNode, PNS_ITEMSET& aCurrent,
                                 PNS_NODE::ITEM_VECTOR& aRemoved )
621
{
Maciej Suminski's avatar
Maciej Suminski committed
622
    BOOST_FOREACH( PNS_ITEM* item, aCurrent.Items() )
623
    {
624
        PNS_NODE::OBSTACLES obstacles;
625

626
        aNode->QueryColliding( item, obstacles, PNS_ITEM::ANY );
627

628
        if( item->OfKind( PNS_ITEM::LINE ) )
629
        {
Maciej Suminski's avatar
Maciej Suminski committed
630
            PNS_LINE* l = static_cast<PNS_LINE*>( item );
631 632

            if( l->EndsWithVia() )
633
            {
Maciej Suminski's avatar
Maciej Suminski committed
634
                PNS_VIA v( l->Via() );
635
                aNode->QueryColliding( &v, obstacles, PNS_ITEM::ANY );
636 637
            }
        }
638

639
        BOOST_FOREACH( PNS_OBSTACLE& obs, obstacles )
640
        {
641
            int clearance = aNode->GetClearance( item, obs.m_item );
642 643
            std::auto_ptr<PNS_ITEM> tmp( obs.m_item->Clone() );
            tmp->Mark( MK_VIOLATION );
644
            DisplayItem( tmp.get(), -1, clearance );
645
            aRemoved.push_back( obs.m_item );
646 647 648
        }
    }
}
649 650


651
void PNS_ROUTER::updateView( PNS_NODE* aNode, PNS_ITEMSET& aCurrent )
652
{
653 654
    PNS_NODE::ITEM_VECTOR removed, added;
    PNS_NODE::OBSTACLES obstacles;
655

656
    if( !aNode )
657
        return;
658

659 660
    if( Settings().Mode() == RM_MarkObstacles )
        markViolations(aNode, aCurrent, removed);
661

662
    aNode->GetUpdatedItems( removed, added );
663

664
    BOOST_FOREACH( PNS_ITEM* item, added )
665
    {
666 667
        DisplayItem( item );
    }
668

669 670
    BOOST_FOREACH( PNS_ITEM* item, removed )
    {
671
        BOARD_CONNECTED_ITEM* parent = item->Parent();
672

673 674 675 676
        if( parent )
        {
            if( parent->ViewIsVisible() )
                m_hiddenItems.insert( parent );
677

678
            parent->ViewSetVisible( false );
679
            parent->ViewUpdate( KIGFX::VIEW_ITEM::APPEARANCE );
680 681
        }
    }
682 683
}

684

685
void PNS_ROUTER::ApplySettings()
686
{
687 688
    // Change track/via size settings
    if( m_state == ROUTE_TRACK)
689
    {
690 691 692 693 694
        m_placer->UpdateSizes( m_settings );
        m_placer->Move( m_currentEnd, m_currentEndItem );
        movePlacing( m_currentEnd, m_currentEndItem );
    }
}
695 696


697
void PNS_ROUTER::movePlacing( const VECTOR2I& aP, PNS_ITEM* aEndItem )
698 699
{
    eraseView();
700

701
    m_placer->Move( aP, aEndItem );
702
    PNS_LINE current = m_placer->Trace();
703

704
    DisplayItem( &current );
705

706 707
    if( current.EndsWithVia() )
        DisplayItem( &current.Via() );
708

Maciej Suminski's avatar
Maciej Suminski committed
709 710
    PNS_ITEMSET tmp( &current );
    updateView( m_placer->CurrentNode( true ), tmp );
711 712
}

713

714
void PNS_ROUTER::CommitRouting( PNS_NODE* aNode )
715
{
716
    PNS_NODE::ITEM_VECTOR removed, added;
717 718 719 720 721

    aNode->GetUpdatedItems( removed, added );

    for( unsigned int i = 0; i < removed.size(); i++ )
    {
722
        BOARD_CONNECTED_ITEM* parent = removed[i]->Parent();
723 724 725

        if( parent )
        {
726
            m_view->Remove( parent );
727 728
            m_board->Remove( parent );
            m_undoBuffer.PushItem( ITEM_PICKER( parent, UR_DELETED ) );
729 730 731 732 733
        }
    }

    BOOST_FOREACH( PNS_ITEM* item, added )
    {
734
        BOARD_CONNECTED_ITEM* newBI = NULL;
735

736
        switch( item->Kind() )
737 738
        {
        case PNS_ITEM::SEGMENT:
739 740 741 742 743 744 745 746
        {
            PNS_SEGMENT* seg = static_cast<PNS_SEGMENT*>( item );
            TRACK* track = new TRACK( m_board );
            const SEG& s = seg->Seg();

            track->SetStart( wxPoint( s.A.x, s.A.y ) );
            track->SetEnd( wxPoint( s.B.x, s.B.y ) );
            track->SetWidth( seg->Width() );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
747
            track->SetLayer( ToLAYER_ID( seg->Layers().Start() ) );
748 749 750 751
            track->SetNetCode( seg->Net() );
            newBI = track;
            break;
        }
752 753

        case PNS_ITEM::VIA:
754 755 756 757 758 759 760 761 762 763
        {
            VIA* via_board = new VIA( m_board );
            PNS_VIA* via = static_cast<PNS_VIA*>( item );
            via_board->SetPosition( wxPoint( via->Pos().x, via->Pos().y ) );
            via_board->SetWidth( via->Diameter() );
            via_board->SetDrill( via->Drill() );
            via_board->SetNetCode( via->Net() );
            newBI = via_board;
            break;
        }
764 765 766 767 768 769 770 771 772 773 774

        default:
            break;
        }

        if( newBI )
        {
            item->SetParent( newBI );
            newBI->ClearFlags();
            m_view->Add( newBI );
            m_board->Add( newBI );
775
            m_undoBuffer.PushItem( ITEM_PICKER( newBI, UR_NEW ) );
776
            newBI->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
777 778 779 780
        }
    }

    m_world->Commit( aNode );
781 782
}

783 784

PNS_VIA* PNS_ROUTER::checkLoneVia( PNS_JOINT* aJoint ) const
785
{
786 787 788
    PNS_VIA* theVia = NULL;
    PNS_LAYERSET l;

789
    BOOST_FOREACH( PNS_ITEM* item, aJoint->LinkList() )
790
    {
791
        if( item->Kind() == PNS_ITEM::VIA )
792
            theVia = static_cast<PNS_VIA*>( item );
793

794
        l.Merge( item->Layers() );
795
    }
796

797 798
    if( l.Start() == l.End() )
        return theVia;
799

800
    return NULL;
801 802
}

803 804 805

bool PNS_ROUTER::FixRoute( const VECTOR2I& aP, PNS_ITEM* aEndItem )
{
806
    bool rv = false;
807

808
    switch( m_state )
809
    {
810
        case ROUTE_TRACK:
811
            rv = m_placer->FixRoute( aP, aEndItem );
812 813
            m_placingVia = false;
            break;
814

815
        case DRAG_SEGMENT:
816
            rv = m_dragger->FixRoute();
817
            break;
818

819
        default:
820 821
            break;
    }
822

823
    if( rv )
824
       StopRouting();
825

826
    return rv;
827 828
}

829

830 831
void PNS_ROUTER::StopRouting()
{
832 833 834
    // Update the ratsnest with new changes
    m_board->GetRatsnest()->Recalculate( m_currentNet );

835 836 837
    if( !RoutingInProgress() )
        return;

838
    if( m_placer )
839 840
        delete m_placer;

841
    if( m_dragger )
842 843 844 845 846 847
        delete m_dragger;

    m_placer = NULL;
    m_dragger = NULL;

    eraseView();
848

849 850
    m_state = IDLE;
    m_world->KillChildren();
851
    m_world->ClearRanks();
852 853
}

854

855 856
void PNS_ROUTER::FlipPosture()
{
857
    if( m_state == ROUTE_TRACK )
858 859
    {
        m_placer->FlipPosture();
Maciej Suminski's avatar
Maciej Suminski committed
860
        m_placer->Move( m_currentEnd, m_currentEndItem );
861
    }
862 863
}

864

865
void PNS_ROUTER::SwitchLayer( int aLayer )
866 867 868 869
{
    switch( m_state )
    {
    case IDLE:
870
        m_currentLayer = aLayer;
871 872 873
        break;

    case ROUTE_TRACK:
874 875 876 877 878 879 880
        if( m_startsOnVia )
        {
            m_currentLayer = aLayer;
            //m_placer->StartPlacement( m_currentStart, m_currentNet, m_currentWidth,
            //        m_currentLayer );
        }
        break;
881 882 883 884

    default:
        break;
    }
885 886
}

887 888

void PNS_ROUTER::ToggleViaPlacement()
889
{
890 891 892
    if( m_state == ROUTE_TRACK )
    {
        m_placingVia = !m_placingVia;
893
        m_placer->AddVia( m_placingVia, m_settings.GetViaDiameter(), m_settings.GetViaDrill() );
894 895
    }
}
896

897

898 899
int PNS_ROUTER::GetCurrentNet() const
{
900
    switch( m_state )
901 902 903
    {
        case ROUTE_TRACK:
            return m_placer->CurrentNet();
904

905 906 907 908 909
        default:
            return m_currentNet;
    }
}

910

911 912
int PNS_ROUTER::GetCurrentLayer() const
{
913
    switch( m_state )
914 915 916
    {
        case ROUTE_TRACK:
            return m_placer->CurrentLayer();
917

918 919
        default:
            return m_currentLayer;
920 921
    }
}
922 923


924 925
void PNS_ROUTER::DumpLog()
{
926 927 928
    PNS_LOGGER* logger = NULL;

    switch( m_state )
929 930 931 932
    {
        case DRAG_SEGMENT:
            logger = m_dragger->Logger();
            break;
933

934 935
        default:
            break;
936
    }
937

938
    if( logger )
Maciej Suminski's avatar
Maciej Suminski committed
939
        logger->Save( "/tmp/shove.log" );
940
}