class_zone.cpp 29.6 KB
Newer Older
1 2 3
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
4 5 6
 * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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 2
 * of the License, or (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

26 27 28 29
/**
 * @file class_zone.cpp
 * @brief Implementation of class to handle copper zones.
 */
30

31 32 33 34 35 36 37 38 39 40 41
#include <fctsys.h>
#include <wxstruct.h>
#include <trigo.h>
#include <class_pcb_screen.h>
#include <class_drawpanel.h>
#include <kicad_string.h>
#include <pcbcommon.h>
#include <colors_selection.h>
#include <richio.h>
#include <macros.h>
#include <wxBasePcbFrame.h>
42
#include <msgpanel.h>
43

44 45 46
#include <protos.h>
#include <class_board.h>
#include <class_zone.h>
47

48 49
#include <pcbnew.h>
#include <zones.h>
50
#include <math_for_graphics.h>
51
#include <polygon_test_point_inside.h>
52

53

Dick Hollenbeck's avatar
Dick Hollenbeck committed
54 55
ZONE_CONTAINER::ZONE_CONTAINER( BOARD* aBoard ) :
    BOARD_CONNECTED_ITEM( aBoard, PCB_ZONE_AREA_T )
56
{
57
    SetNet( -1 );                               // Net number for fast comparisons
58
    m_CornerSelection = -1;
59 60
    m_IsFilled = false;                         // fill status : true when the zone is filled
    m_FillMode = 0;                             // How to fill areas: 0 = use filled polygons, != 0 fill with segments
61
    m_priority = 0;
62 63
    m_smoothedPoly = NULL;
    m_cornerSmoothingType = ZONE_SETTINGS::SMOOTHING_NONE;
64 65 66 67
    SetIsKeepout( false );
    SetDoNotAllowCopperPour( false );           // has meaning only if m_isKeepout == true
    SetDoNotAllowVias( true );                  // has meaning only if m_isKeepout == true
    SetDoNotAllowTracks( true );                // has meaning only if m_isKeepout == true
68
    m_cornerRadius = 0;
69
    SetLocalFlags( 0 );                         // flags tempoarry used in zone calculations
70
    m_Poly     = new CPolyLine();               // Outlines
Dick Hollenbeck's avatar
Dick Hollenbeck committed
71
    aBoard->GetZoneSettings().ExportSetting( *this );
72 73 74
}


75 76 77 78 79 80 81 82 83
ZONE_CONTAINER::ZONE_CONTAINER( const ZONE_CONTAINER& aZone ) :
    BOARD_CONNECTED_ITEM( aZone )
{
    // Should the copy be on the same net?
    SetNet( aZone.GetNet() );
    m_Poly = new CPolyLine( *aZone.m_Poly );

    // For corner moving, corner index to drag, or -1 if no selection
    m_CornerSelection = -1;
84
    m_IsFilled = aZone.m_IsFilled;
85 86 87
    m_ZoneClearance = aZone.m_ZoneClearance;     // clearance value
    m_ZoneMinThickness = aZone.m_ZoneMinThickness;
    m_FillMode = aZone.m_FillMode;               // Filling mode (segments/polygons)
88
    m_priority = aZone.m_priority;
89
    m_ArcToSegmentsCount = aZone.m_ArcToSegmentsCount;
90
    m_PadConnection = aZone.m_PadConnection;
91 92
    m_ThermalReliefGap = aZone.m_ThermalReliefGap;
    m_ThermalReliefCopperBridge = aZone.m_ThermalReliefCopperBridge;
93 94
    m_FilledPolysList.Append( aZone.m_FilledPolysList );
    m_FillSegmList = aZone.m_FillSegmList;      // vector <> copy
95

96
    m_isKeepout = aZone.m_isKeepout;
97
    m_doNotAllowCopperPour = aZone.m_doNotAllowCopperPour;
98 99 100 101 102 103
    m_doNotAllowVias = aZone.m_doNotAllowVias;
    m_doNotAllowTracks = aZone.m_doNotAllowTracks;

    m_cornerSmoothingType = aZone.m_cornerSmoothingType;
    m_cornerRadius = aZone.m_cornerRadius;

104
    SetLocalFlags( aZone.GetLocalFlags() );
105 106 107
}


108
ZONE_CONTAINER::~ZONE_CONTAINER()
109
{
110 111
    delete m_Poly;
    m_Poly = NULL;
112 113
}

114

115
EDA_ITEM* ZONE_CONTAINER::Clone() const
116 117 118 119 120
{
    return new ZONE_CONTAINER( *this );
}


121 122
bool ZONE_CONTAINER::UnFill()
{
123 124
    bool change = ( m_FilledPolysList.GetCornersCount() > 0 ) ||
                  ( m_FillSegmList.size() > 0 );
125

126
    m_FilledPolysList.RemoveAllContours();
127 128 129 130 131
    m_FillSegmList.clear();
    m_IsFilled = false;

    return change;
}
132

133

Dick Hollenbeck's avatar
Dick Hollenbeck committed
134
const wxPoint& ZONE_CONTAINER::GetPosition() const
135
{
Dick Hollenbeck's avatar
Dick Hollenbeck committed
136
    static const wxPoint dummy;
137

Dick Hollenbeck's avatar
Dick Hollenbeck committed
138
    return m_Poly ? GetCornerPosition( 0 ) : dummy;
139
}
140

141

142
void ZONE_CONTAINER::SetNet( int aNetCode )
143
{
144
    BOARD_CONNECTED_ITEM::SetNet( aNetCode );
145

146
    if( aNetCode < 0 )
147 148
        return;

149
    BOARD* board = GetBoard();
150

151
    if( board )
152
    {
153
        NETINFO_ITEM* net = board->FindNet( aNetCode );
154

155
        if( net )
156
            m_Netname = net->GetNetname();
157 158 159 160
        else
            m_Netname.Empty();
    }
    else
161
    {
162
        m_Netname.Empty();
163
    }
164
}
165

166

167 168
void ZONE_CONTAINER::Draw( EDA_DRAW_PANEL* panel, wxDC* DC, GR_DRAWMODE aDrawMode,
                           const wxPoint& offset )
169
{
170 171 172
    if( DC == NULL )
        return;

dickelbeck's avatar
dickelbeck committed
173
    wxPoint seg_start, seg_end;
174
    LAYER_NUM curr_layer = ( (PCB_SCREEN*) panel->GetScreen() )->m_Active_Layer;
175

176
    BOARD*  brd   = GetBoard();
177
    EDA_COLOR_T color = brd->GetLayerColor( m_Layer );
178

179
    if( brd->IsLayerVisible( m_Layer ) == false && ( color & HIGHLIGHT_FLAG ) != HIGHLIGHT_FLAG )
180 181
        return;

182
    GRSetDrawMode( DC, aDrawMode );
183 184 185 186

    if( DisplayOpt.ContrastModeDisplay )
    {
        if( !IsOnLayer( curr_layer ) )
187
            ColorTurnToDarkDarkGray( &color );
188 189
    }

190
    if( aDrawMode & GR_HIGHLIGHT )
191
        ColorChangeHighlightFlag( &color, !(aDrawMode & GR_AND) );
192

193
    ColorApplyHighlightFlag( &color );
194

195 196
    SetAlpha( &color, 150 );

197 198
    // draw the lines
    int i_start_contour = 0;
199
    std::vector<wxPoint> lines;
200
    lines.reserve( (GetNumCorners() * 2) + 2 );
201

202
    for( int ic = 0; ic < GetNumCorners(); ic++ )
203
    {
204
        seg_start = GetCornerPosition( ic ) + offset;
205

206
        if( !m_Poly->m_CornersList.IsEndContour( ic ) && ic < GetNumCorners() - 1 )
207
        {
208
            seg_end = GetCornerPosition( ic + 1 ) + offset;
209 210 211
        }
        else
        {
212
            seg_end = GetCornerPosition( i_start_contour ) + offset;
213 214
            i_start_contour = ic + 1;
        }
215

216 217
        lines.push_back( seg_start );
        lines.push_back( seg_end );
218
    }
219

220
    GRLineArray( panel->GetClipBox(), DC, lines, 0, color );
221 222

    // draw hatches
223
    lines.clear();
224
    lines.reserve( (m_Poly->m_HatchLines.size() * 2) + 2 );
225

226
    for( unsigned ic = 0; ic < m_Poly->m_HatchLines.size(); ic++ )
227
    {
228 229
        seg_start = m_Poly->m_HatchLines[ic].m_Start + offset;
        seg_end   = m_Poly->m_HatchLines[ic].m_End + offset;
230 231
        lines.push_back( seg_start );
        lines.push_back( seg_end );
232
    }
233

234
    GRLineArray( panel->GetClipBox(), DC, lines, 0, color );
235 236 237
}


238
void ZONE_CONTAINER::DrawFilledArea( EDA_DRAW_PANEL* panel,
239
                                     wxDC* DC, GR_DRAWMODE aDrawMode, const wxPoint& offset )
240
{
jean-pierre charras's avatar
jean-pierre charras committed
241 242
    static std::vector <char>    CornersTypeBuffer;
    static std::vector <wxPoint> CornersBuffer;
243

244 245
    // outline_mode is false to show filled polys,
    // and true to show polygons outlines only (test and debug purposes)
246
    bool outline_mode = DisplayOpt.DisplayZonesMode == 2 ? true : false;
247 248 249 250

    if( DC == NULL )
        return;

251
    if( DisplayOpt.DisplayZonesMode == 1 )     // Do not show filled areas
252 253
        return;

254
    if( m_FilledPolysList.GetCornersCount() == 0 )  // Nothing to draw
255 256
        return;

257
    BOARD* brd = GetBoard();
258
    LAYER_NUM curr_layer = ( (PCB_SCREEN*) panel->GetScreen() )->m_Active_Layer;
259
    EDA_COLOR_T color = brd->GetLayerColor( m_Layer );
260

261
    if( brd->IsLayerVisible( m_Layer ) == false && ( color & HIGHLIGHT_FLAG ) != HIGHLIGHT_FLAG )
262 263 264 265 266 267 268
        return;

    GRSetDrawMode( DC, aDrawMode );

    if( DisplayOpt.ContrastModeDisplay )
    {
        if( !IsOnLayer( curr_layer ) )
269
            ColorTurnToDarkDarkGray( &color );
270 271
    }

272
    if( aDrawMode & GR_HIGHLIGHT )
273
        ColorChangeHighlightFlag( &color, !(aDrawMode & GR_AND) );
274

275
    ColorApplyHighlightFlag( &color );
276

277
    SetAlpha( &color, 150 );
278

279 280
    CornersTypeBuffer.clear();
    CornersBuffer.clear();
281

282
    // Draw all filled areas
283
    int imax = m_FilledPolysList.GetCornersCount() - 1;
284

285
    for( int ic = 0; ic <= imax; ic++ )
286
    {
287 288
        const CPolyPt& corner = m_FilledPolysList.GetCorner( ic );
        wxPoint  coord( corner.x + offset.x, corner.y + offset.y );
289
        CornersBuffer.push_back( coord );
290
        CornersTypeBuffer.push_back( (char) corner.m_utility );
291

292
        // the last corner of a filled area is found: draw it
293
        if( (corner.end_contour) || (ic == imax) )
294
        {
295 296 297 298 299
            /* Draw the current filled area: draw segments outline first
             * Curiously, draw segments outline first and after draw filled polygons
             * with outlines thickness = 0 is a faster than
             * just draw filled polygons but with outlines thickness = m_ZoneMinThickness
             * So DO NOT use draw filled polygons with outlines having a thickness  > 0
300 301 302 303
             * Note: Extra segments ( added to joint holes with external outline) flagged by
             * m_utility != 0 are not drawn
             * Note not all polygon libraries provide a flag for these extra-segments, therefore
             * the m_utility member can be always 0
304
             */
305 306
            {
                // Draw outlines:
307
                if( (m_ZoneMinThickness > 1) || outline_mode )
308
                {
309
                    int ilim = CornersBuffer.size() - 1;
310

311
                    for(  int is = 0, ie = ilim; is <= ilim; ie = is, is++ )
312
                    {
313 314 315 316
                        int x0 = CornersBuffer[is].x;
                        int y0 = CornersBuffer[is].y;
                        int x1 = CornersBuffer[ie].x;
                        int y1 = CornersBuffer[ie].y;
317

318 319
                        // Draw only basic outlines, not extra segments.
                        if( CornersTypeBuffer[ie] == 0 )
320
                        {
321
                            if( !DisplayOpt.DisplayPcbTrackFill || GetState( FORCE_SKETCH ) )
322
                                GRCSegm( panel->GetClipBox(), DC,
323 324
                                         x0, y0, x1, y1,
                                         m_ZoneMinThickness, color );
325
                            else
326
                                GRFillCSegm( panel->GetClipBox(), DC,
327 328
                                             x0, y0, x1, y1,
                                             m_ZoneMinThickness, color );
329
                        }
330 331
                    }
                }
332

333
                // Draw areas:
334
                if( m_FillMode==0  && !outline_mode )
335
                    GRPoly( panel->GetClipBox(), DC, CornersBuffer.size(), &CornersBuffer[0],
336
                            true, 0, color, color );
337
            }
338

339 340
            CornersTypeBuffer.clear();
            CornersBuffer.clear();
341 342
        }
    }
charras's avatar
charras committed
343 344 345 346 347

    if( m_FillMode == 1  && !outline_mode )     // filled with segments
    {
        for( unsigned ic = 0; ic < m_FillSegmList.size(); ic++ )
        {
348 349
            wxPoint start = m_FillSegmList[ic].m_Start + offset;
            wxPoint end   = m_FillSegmList[ic].m_End + offset;
350

charras's avatar
charras committed
351
            if( !DisplayOpt.DisplayPcbTrackFill || GetState( FORCE_SKETCH ) )
352
                GRCSegm( panel->GetClipBox(), DC, start.x, start.y, end.x, end.y,
353
                         m_ZoneMinThickness, color );
charras's avatar
charras committed
354
            else
355
                GRFillCSegm( panel->GetClipBox(), DC, start.x, start.y, end.x, end.y,
356
                             m_ZoneMinThickness, color );
charras's avatar
charras committed
357 358
        }
    }
359 360 361
}


362
EDA_RECT ZONE_CONTAINER::GetBoundingBox() const
363
{
364
    const int PRELOAD = 0x7FFFFFFF;     // Biggest integer (32 bits)
365

366 367 368 369
    int       ymax = -PRELOAD;
    int       ymin = PRELOAD;
    int       xmin = PRELOAD;
    int       xmax = -PRELOAD;
370

371
    int       count = GetNumCorners();
372

373
    for( int i = 0; i<count; ++i )
374
    {
375
        wxPoint corner = GetCornerPosition( i );
376

377 378 379 380
        ymax = std::max( ymax, corner.y );
        xmax = std::max( xmax, corner.x );
        ymin = std::min( ymin, corner.y );
        xmin = std::min( xmin, corner.x );
381 382
    }

383
    EDA_RECT ret( wxPoint( xmin, ymin ), wxSize( xmax - xmin + 1, ymax - ymin + 1 ) );
384 385 386 387 388

    return ret;
}


389 390
void ZONE_CONTAINER::DrawWhileCreateOutline( EDA_DRAW_PANEL* panel, wxDC* DC,
                                             GR_DRAWMODE draw_mode )
391
{
392
    GR_DRAWMODE current_gr_mode  = draw_mode;
393
    bool    is_close_segment = false;
dickelbeck's avatar
dickelbeck committed
394
    wxPoint seg_start, seg_end;
395 396 397

    if( DC == NULL )
        return;
398

399
    LAYER_NUM curr_layer = ( (PCB_SCREEN*) panel->GetScreen() )->m_Active_Layer;
400
    BOARD* brd   = GetBoard();
401
    EDA_COLOR_T color = brd->GetLayerColor( m_Layer );
402 403 404 405

    if( DisplayOpt.ContrastModeDisplay )
    {
        if( !IsOnLayer( curr_layer ) )
406
            ColorTurnToDarkDarkGray( &color );
407
    }
408

409
    // draw the lines
410
    wxPoint start_contour_pos = GetCornerPosition( 0 );
411
    int     icmax = GetNumCorners() - 1;
412

413
    for( int ic = 0; ic <= icmax; ic++ )
414
    {
415 416
        int xi = GetCornerPosition( ic ).x;
        int yi = GetCornerPosition( ic ).y;
417
        int xf, yf;
418

419
        if( !m_Poly->m_CornersList.IsEndContour( ic ) && ic < icmax )
420
        {
dickelbeck's avatar
dickelbeck committed
421
            is_close_segment = false;
422 423
            xf = GetCornerPosition( ic + 1 ).x;
            yf = GetCornerPosition( ic + 1 ).y;
424

425
            if( m_Poly->m_CornersList.IsEndContour( ic + 1 ) || (ic == icmax - 1) )
dickelbeck's avatar
dickelbeck committed
426 427 428
                current_gr_mode = GR_XOR;
            else
                current_gr_mode = draw_mode;
429
        }
430
        else    // Draw the line from last corner to the first corner of the current contour
431
        {
dickelbeck's avatar
dickelbeck committed
432
            is_close_segment = true;
433
            current_gr_mode  = GR_XOR;
434 435
            xf = start_contour_pos.x;
            yf = start_contour_pos.y;
436

437
            // Prepare the next contour for drawing, if exists
438
            if( ic < icmax )
439
                start_contour_pos = GetCornerPosition( ic + 1 );
440
        }
441

dickelbeck's avatar
dickelbeck committed
442
        GRSetDrawMode( DC, current_gr_mode );
443

444
        if( is_close_segment )
445
            GRLine( panel->GetClipBox(), DC, xi, yi, xf, yf, 0, WHITE );
dickelbeck's avatar
dickelbeck committed
446
        else
447
            GRLine( panel->GetClipBox(), DC, xi, yi, xf, yf, 0, color );
448
    }
449 450
}

451

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
int ZONE_CONTAINER::GetThermalReliefGap( D_PAD* aPad ) const
{
    if( aPad == NULL || aPad->GetThermalGap() == 0 )
        return m_ThermalReliefGap;
    else
        return aPad->GetThermalGap();
}


int ZONE_CONTAINER::GetThermalReliefCopperBridge( D_PAD* aPad ) const
{
    if( aPad == NULL || aPad->GetThermalWidth() == 0 )
        return m_ThermalReliefCopperBridge;
    else
        return aPad->GetThermalWidth();
}


470
bool ZONE_CONTAINER::HitTest( const wxPoint& aPosition )
471
{
472
    if( HitTestForCorner( aPosition ) )
473
        return true;
474

475
    if( HitTestForEdge( aPosition ) )
476 477 478 479 480
        return true;

    return false;
}

481 482 483 484
// Zones outlines have no thickness, so it Hit Test functions
// we must have a default distance between the test point
// and a corner or a zone edge:
#define MIN_DIST_IN_MILS 10
485

486
bool ZONE_CONTAINER::HitTestForCorner( const wxPoint& refPos )
487
{
488 489
    m_CornerSelection = -1;         // Set to not found

490
    // distance (in internal units) to detect a corner in a zone outline.
491
    int min_dist = MIN_DIST_IN_MILS*IU_PER_MILS;
492

493
    wxPoint delta;
494
    unsigned lim = m_Poly->m_CornersList.GetCornersCount();
495

496
    for( unsigned item_pos = 0; item_pos < lim; item_pos++ )
497
    {
498 499
        delta.x = refPos.x - m_Poly->m_CornersList.GetX( item_pos );
        delta.y = refPos.y - m_Poly->m_CornersList.GetY( item_pos );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
500

501
        // Calculate a distance:
502
        int dist = std::max( abs( delta.x ), abs( delta.y ) );
503

504
        if( dist < min_dist )  // this corner is a candidate:
505 506
        {
            m_CornerSelection = item_pos;
507
            min_dist = dist;
508 509 510
        }
    }

511
    return m_CornerSelection >= 0;
512 513
}

514

515
bool ZONE_CONTAINER::HitTestForEdge( const wxPoint& refPos )
516
{
517
    unsigned lim = m_Poly->m_CornersList.GetCornersCount();
518

519 520
    m_CornerSelection = -1;     // Set to not found

521 522
    // distance (in internal units) to detect a zone outline
    int min_dist = MIN_DIST_IN_MILS*IU_PER_MILS;
Dick Hollenbeck's avatar
Dick Hollenbeck committed
523

524
    unsigned first_corner_pos = 0;
525

526
    for( unsigned item_pos = 0; item_pos < lim; item_pos++ )
527
    {
528
        unsigned end_segm = item_pos + 1;
529 530 531 532 533

        /* the last corner of the current outline is tested
         * the last segment of the current outline starts at current corner, and ends
         * at the first corner of the outline
         */
534
        if( m_Poly->m_CornersList.IsEndContour ( item_pos ) || end_segm >= lim )
535 536 537 538 539 540
        {
            unsigned tmp = first_corner_pos;
            first_corner_pos = end_segm;    // first_corner_pos is now the beginning of the next outline
            end_segm = tmp;                 // end_segm is the beginning of the current outline
        }

541
        // test the dist between segment and ref point
542 543 544 545 546 547
        int dist = KiROUND( GetPointToLineSegmentDistance(
                    refPos.x, refPos.y,
                    m_Poly->m_CornersList.GetX( item_pos ),
                    m_Poly->m_CornersList.GetY( item_pos ),
                    m_Poly->m_CornersList.GetX( end_segm ),
                    m_Poly->m_CornersList.GetY( end_segm ) ) );
548

549
        if( dist < min_dist )
550 551
        {
            m_CornerSelection = item_pos;
552
            min_dist = dist;
553 554 555
        }
    }

556
    return m_CornerSelection >= 0;
557 558
}

559

560
bool ZONE_CONTAINER::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
561
{
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
    EDA_RECT arect = aRect;
    arect.Inflate( aAccuracy );
    CRect rect = m_Poly->GetBoundingBox();
    EDA_RECT bbox;

    bbox.SetOrigin( rect.left,  rect.bottom );
    bbox.SetEnd(    rect.right, rect.top    );

    if( aContained )
         return arect.Contains( bbox );
    else    // Test for intersection between aRect and the polygon
            // For a polygon, using its bounding box has no sense here
    {
        // Fast test: if aRect is outside the polygon bounding box,
        // rectangles cannot intersect
        if( ! bbox.Intersects( arect ) )
            return false;

        // aRect is inside the polygon bounding box,
        // and can intersect the polygon: use a fine test.
        // aRect intersects the polygon if at least one aRect corner
        // is inside the polygon
        wxPoint corner = arect.GetOrigin();

        if( HitTestInsideZone( corner ) )
            return true;

        corner.x = arect.GetEnd().x;
590

591 592
        if( HitTestInsideZone( corner ) )
            return true;
593

594
        corner = arect.GetEnd();
595

596 597
        if( HitTestInsideZone( corner ) )
            return true;
598

599
        corner.x = arect.GetOrigin().x;
600

601 602
        if( HitTestInsideZone( corner ) )
            return true;
603

604 605 606 607 608 609 610 611 612 613 614
        // No corner inside arect, but outlines can intersect arect
        // if one of outline corners is inside arect
        int count = m_Poly->GetCornersCount();
        for( int ii =0; ii < count; ii++ )
        {
            if( arect.Contains( m_Poly->GetPos( ii ) ) )
                return true;
        }

        return false;
    }
615
}
616

617

618 619 620 621
int ZONE_CONTAINER::GetClearance( BOARD_CONNECTED_ITEM* aItem ) const
{
    int         myClearance = m_ZoneClearance;

622 623 624 625 626 627
#if 0   // Maybe the netclass clearance should not come into play for a zone?
        // At least the policy decision can be controlled by the zone
        // itself, i.e. here.  On reasons of insufficient documentation,
        // the user will be less bewildered if we simply respect the
        // "zone clearance" setting in the zone properties dialog.  (At least
        // until there is a UI boolean for this.)
628

629 630 631 632
    NETCLASS*   myClass  = GetNetClass();

    if( myClass )
        myClearance = std::max( myClearance, myClass->GetClearance() );
633 634 635 636 637
#endif

    if( aItem )
    {
        int hisClearance = aItem->GetClearance( NULL );
jean-pierre charras's avatar
jean-pierre charras committed
638
        myClearance = std::max( hisClearance, myClearance );
639 640 641 642 643 644
    }

    return myClearance;
}


645
bool ZONE_CONTAINER::HitTestFilledArea( const wxPoint& aRefPos ) const
646 647
{
    unsigned indexstart = 0, indexend;
648 649
    bool     inside     = false;

650
    for( indexend = 0; indexend < m_FilledPolysList.GetCornersCount(); indexend++ )
651
    {
652
        if( m_FilledPolysList.IsEndContour( indexend ) )       // end of a filled sub-area found
653
        {
654 655
            if( TestPointInsidePolygon( m_FilledPolysList, indexstart, indexend,
                                        aRefPos.x, aRefPos.y ) )
656 657 658 659
            {
                inside = true;
                break;
            }
660

661
            // Prepare test of next area which starts after the current index end (if exists)
662
            indexstart = indexend + 1;
663 664
        }
    }
665

666 667 668 669
    return inside;
}


670
void ZONE_CONTAINER::GetMsgPanelInfo( std::vector< MSG_PANEL_ITEM >& aList )
671 672
{
    wxString msg;
673

dickelbeck's avatar
dickelbeck committed
674 675 676 677
    BOARD*   board = (BOARD*) m_Parent;

    wxASSERT( board );

678
    msg = _( "Zone Outline" );
679

680 681
    // Display Cutout instead of Outline for holes inside a zone
    // i.e. when num contour !=0
682
    int ncont = m_Poly->GetContour( m_CornerSelection );
683

684 685
    if( ncont )
        msg << wxT( " " ) << _( "(Cutout)" );
686

687
    aList.push_back( MSG_PANEL_ITEM( _( "Type" ), msg, DARKCYAN ) );
688

689 690 691
    if( GetIsKeepout() )
    {
        msg.Empty();
692

693
        if( GetDoNotAllowVias() )
694
            AccumulateDescription( msg, _("No via") );
695

696
        if( GetDoNotAllowTracks() )
697
            AccumulateDescription( msg, _("No track") );
698

699
        if( GetDoNotAllowCopperPour() )
700
            AccumulateDescription( msg, _("No copper pour") );
701

702
        aList.push_back( MSG_PANEL_ITEM( _( "Keepout" ), msg, RED ) );
703 704
    }
    else if( IsOnCopperLayer() )
705
    {
706 707
        if( GetNet() >= 0 )
        {
708
            NETINFO_ITEM* equipot = board->FindNet( GetNet() );
709

710
            if( equipot )
711
                msg = equipot->GetNetname();
712 713 714 715 716 717 718 719 720
            else
                msg = wxT( "<noname>" );
        }
        else // a netcode < 0 is an error
        {
            msg = wxT( " [" );
            msg << m_Netname + wxT( "]" );
            msg << wxT( " <" ) << _( "Not Found" ) << wxT( ">" );
        }
721

722 723
        aList.push_back( MSG_PANEL_ITEM( _( "NetName" ), msg, RED ) );

724 725 726
#if 1
        // Display net code : (useful in test or debug)
        msg.Printf( wxT( "%d" ), GetNet() );
727
        aList.push_back( MSG_PANEL_ITEM( _( "NetCode" ), msg, RED ) );
728 729 730 731
#endif

        // Display priority level
        msg.Printf( wxT( "%d" ), GetPriority() );
732
        aList.push_back( MSG_PANEL_ITEM( _( "Priority" ), msg, BLUE ) );
733 734
    }
    else
735
    {
736
        aList.push_back( MSG_PANEL_ITEM( _( "Non Copper Zone" ), wxEmptyString, RED ) );
737
    }
738

739
    aList.push_back( MSG_PANEL_ITEM( _( "Layer" ), GetLayerName(), BROWN ) );
740

741
    msg.Printf( wxT( "%d" ), (int) m_Poly->m_CornersList.GetCornersCount() );
742
    aList.push_back( MSG_PANEL_ITEM( _( "Corners" ), msg, BLUE ) );
743

744
    if( m_FillMode )
745
        msg = _( "Segments" );
746
    else
747
        msg = _( "Polygons" );
748

749
    aList.push_back( MSG_PANEL_ITEM( _( "Fill mode" ), msg, BROWN ) );
750

751
    // Useful for statistics :
752
    msg.Printf( wxT( "%d" ), (int) m_Poly->m_HatchLines.size() );
753
    aList.push_back( MSG_PANEL_ITEM( _( "Hatch lines" ), msg, BLUE ) );
754

755
    if( m_FilledPolysList.GetCornersCount() )
756
    {
757
        msg.Printf( wxT( "%d" ), (int) m_FilledPolysList.GetCornersCount() );
758
        aList.push_back( MSG_PANEL_ITEM( _( "Corners in DrawList" ), msg, BLUE ) );
759
    }
760
}
761 762


763
/* Geometric transforms: */
764 765

void ZONE_CONTAINER::Move( const wxPoint& offset )
766
{
767
    /* move outlines */
768
    for( unsigned ii = 0; ii < m_Poly->m_CornersList.GetCornersCount(); ii++ )
769
    {
770
        SetCornerPosition( ii, GetCornerPosition( ii ) + offset );
771 772 773
    }

    m_Poly->Hatch();
774 775

    /* move filled areas: */
776
    for( unsigned ic = 0; ic < m_FilledPolysList.GetCornersCount(); ic++ )
777
    {
778
        m_FilledPolysList.SetX( ic, m_FilledPolysList.GetX( ic ) + offset.x );
779
        m_FilledPolysList.SetY( ic, m_FilledPolysList.GetY( ic ) + offset.y );
780
    }
781

charras's avatar
charras committed
782 783 784
    for( unsigned ic = 0; ic < m_FillSegmList.size(); ic++ )
    {
        m_FillSegmList[ic].m_Start += offset;
785
        m_FillSegmList[ic].m_End   += offset;
charras's avatar
charras committed
786
    }
787 788
}

789

790 791
void ZONE_CONTAINER::MoveEdge( const wxPoint& offset )
{
dickelbeck's avatar
dickelbeck committed
792
    int ii = m_CornerSelection;
793

dickelbeck's avatar
dickelbeck committed
794
    // Move the start point of the selected edge:
795
    SetCornerPosition( ii, GetCornerPosition( ii ) + offset );
796

dickelbeck's avatar
dickelbeck committed
797
    // Move the end point of the selected edge:
798
    if( m_Poly->m_CornersList.IsEndContour( ii ) || ii == GetNumCorners() - 1 )
dickelbeck's avatar
dickelbeck committed
799 800 801 802 803
    {
        int icont = m_Poly->GetContour( ii );
        ii = m_Poly->GetContourStart( icont );
    }
    else
804
    {
dickelbeck's avatar
dickelbeck committed
805
        ii++;
806 807
    }

808
    SetCornerPosition( ii, GetCornerPosition( ii ) + offset );
809 810 811 812

    m_Poly->Hatch();
}

813

Dick Hollenbeck's avatar
Dick Hollenbeck committed
814
void ZONE_CONTAINER::Rotate( const wxPoint& centre, double angle )
815
{
816
    wxPoint pos;
817

818
    for( unsigned ic = 0; ic < m_Poly->m_CornersList.GetCornersCount(); ic++ )
819
    {
820
        pos = m_Poly->m_CornersList.GetPos( ic );
821
        RotatePoint( &pos, centre, angle );
822 823
        m_Poly->SetX( ic, pos.x );
        m_Poly->SetY( ic, pos.y );
824 825 826
    }

    m_Poly->Hatch();
827 828

    /* rotate filled areas: */
829
    for( unsigned ic = 0; ic < m_FilledPolysList.GetCornersCount(); ic++ )
830
    {
831
        pos = m_FilledPolysList.GetPos( ic );
832
        RotatePoint( &pos, centre, angle );
833 834
        m_FilledPolysList.SetX( ic, pos.x );
        m_FilledPolysList.SetY( ic, pos.y );
835
    }
836

charras's avatar
charras committed
837 838 839 840 841
    for( unsigned ic = 0; ic < m_FillSegmList.size(); ic++ )
    {
        RotatePoint( &m_FillSegmList[ic].m_Start, centre, angle );
        RotatePoint( &m_FillSegmList[ic].m_End, centre, angle );
    }
842 843
}

844 845

void ZONE_CONTAINER::Flip( const wxPoint& aCentre )
846 847
{
    Mirror( aCentre );
848
    SetLayer( FlipLayer( GetLayer() ) );
849
}
850

851

852
void ZONE_CONTAINER::Mirror( const wxPoint& mirror_ref )
853
{
854
    for( unsigned ic = 0; ic < m_Poly->m_CornersList.GetCornersCount(); ic++ )
855
    {
856 857 858
        int py = m_Poly->m_CornersList.GetY( ic ) - mirror_ref.y;
        NEGATE( py );
        m_Poly->m_CornersList.SetY( ic, py + mirror_ref.y );
859 860 861
    }

    m_Poly->Hatch();
862 863

    /* mirror filled areas: */
864
    for( unsigned ic = 0; ic < m_FilledPolysList.GetCornersCount(); ic++ )
865
    {
866 867 868
        int py = m_FilledPolysList.GetY( ic ) - mirror_ref.y;
        NEGATE( py );
        m_FilledPolysList.SetY( ic, py + mirror_ref.y );
869
    }
870

charras's avatar
charras committed
871 872 873
    for( unsigned ic = 0; ic < m_FillSegmList.size(); ic++ )
    {
        m_FillSegmList[ic].m_Start.y -= mirror_ref.y;
874
        NEGATE( m_FillSegmList[ic].m_Start.y );
charras's avatar
charras committed
875
        m_FillSegmList[ic].m_Start.y += mirror_ref.y;
876 877
        m_FillSegmList[ic].m_End.y   -= mirror_ref.y;
        NEGATE( m_FillSegmList[ic].m_End.y );
charras's avatar
charras committed
878 879
        m_FillSegmList[ic].m_End.y += mirror_ref.y;
    }
880 881 882
}


883
void ZONE_CONTAINER::Copy( ZONE_CONTAINER* src )
884
{
885 886 887
    m_Parent = src->m_Parent;
    m_Layer  = src->m_Layer;
    SetNet( src->GetNet() );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
888
    SetTimeStamp( src->m_TimeStamp );
889
    m_Poly->RemoveAllContours();
890
    m_Poly->Copy( src->m_Poly );                // copy outlines
891 892 893 894 895
    m_CornerSelection  = -1;                    // For corner moving, corner index to drag, or -1 if no selection
    m_ZoneClearance    = src->m_ZoneClearance;  // clearance value
    m_ZoneMinThickness = src->m_ZoneMinThickness;
    m_FillMode = src->m_FillMode;               // Filling mode (segments/polygons)
    m_ArcToSegmentsCount = src->m_ArcToSegmentsCount;
896
    m_PadConnection = src->m_PadConnection;
897 898
    m_ThermalReliefGap = src->m_ThermalReliefGap;
    m_ThermalReliefCopperBridge = src->m_ThermalReliefCopperBridge;
899 900
    m_Poly->SetHatchStyle( src->m_Poly->GetHatchStyle() );
    m_Poly->SetHatchPitch( src->m_Poly->GetHatchPitch() );
901
    m_Poly->m_HatchLines = src->m_Poly->m_HatchLines;   // Copy vector <CSegment>
902 903
    m_FilledPolysList.RemoveAllContours();
    m_FilledPolysList.Append( src->m_FilledPolysList );
904
    m_FillSegmList.clear();
charras's avatar
charras committed
905
    m_FillSegmList = src->m_FillSegmList;
906
}
907

908

909 910
bool ZONE_CONTAINER::SetNetNameFromNetCode( void )
{
911
    NETINFO_ITEM* net;
912 913

    if( m_Parent && ( net = ( (BOARD*) m_Parent )->FindNet( GetNet() ) ) )
914
    {
915
        m_Netname = net->GetNetname();
916 917 918 919 920
        return true;
    }

    return false;
}
921 922


923 924 925 926 927 928 929 930 931
ZoneConnection ZONE_CONTAINER::GetPadConnection( D_PAD* aPad ) const
{
    if( aPad == NULL || aPad->GetZoneConnection() == UNDEFINED_CONNECTION )
        return m_PadConnection;
    else
        return aPad->GetZoneConnection();
}


932 933 934 935 936 937 938 939 940 941 942 943 944
void ZONE_CONTAINER::AddPolygon( std::vector< wxPoint >& aPolygon )
{
    if( aPolygon.empty() )
        return;

    for( unsigned i = 0;  i < aPolygon.size();  i++ )
    {
        if( i == 0 )
            m_Poly->Start( GetLayer(), aPolygon[i].x, aPolygon[i].y, GetHatchStyle() );
        else
            AppendCorner( aPolygon[i] );
    }

945
    m_Poly->CloseLastContour();
946 947 948 949
}



950 951 952 953 954 955 956 957 958 959 960
wxString ZONE_CONTAINER::GetSelectMenuText() const
{
    wxString text;
    NETINFO_ITEM* net;
    BOARD* board = GetBoard();

    int ncont = m_Poly->GetContour( m_CornerSelection );

    if( ncont )
        text << wxT( " " ) << _( "(Cutout)" );

961 962 963
    if( GetIsKeepout() )
        text << wxT( " " ) << _( "(Keepout)" );

964
    text << wxString::Format( wxT( " (%08lX)" ), m_TimeStamp );
965

966 967
    // Display net name for copper zones
    if( !GetIsKeepout() )
968
    {
969
        if( GetNet() >= 0 )
970
        {
971 972 973
            if( board )
            {
                net = board->FindNet( GetNet() );
974

975 976 977 978 979 980
                if( net )
                {
                    text << wxT( " [" ) << net->GetNetname() << wxT( "]" );
                }
            }
            else
981
            {
982
                text << _( "** NO BOARD DEFINED **" );
983 984 985
            }
        }
        else
986 987 988 989
        {   // A netcode < 0 is an error:
            // Netname not found or area not initialised
            text << wxT( " [" ) << m_Netname << wxT( "]" );
            text << wxT( " <" ) << _( "Not Found" ) << wxT( ">" );
990 991 992
        }
    }

993 994
    wxString msg;
    msg.Printf( _( "Zone Outline %s on %s" ), GetChars( text ),
995
                 GetChars( GetLayerName() ) );
996

997
    return msg;
998
}