class_track.h 16.8 KB
Newer Older
1 2 3 4
/**
 * @file class_track.h
 * @brief Definitions for tracks, vias and zones.
 */
5 6 7 8

#ifndef CLASS_TRACK_H
#define CLASS_TRACK_H

9 10

#include "class_board_item.h"
11
#include "PolyLine.h"
12 13


14 15 16
class TRACK;


17
// Via attributes (m_Shape parameter)
18 19
#define VIA_THROUGH             3           /* Always a through hole via */
#define VIA_BLIND_BURIED        2           /* this via can be on internal layers */
20 21
#define VIA_MICROVIA            1           /* this via which connect from an external layer
                                             * to the near neighbor internal layer */
22
#define VIA_NOT_DEFINED         0           /* not yet used */
23

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

/**
 * Function GetTrace
 * is a helper function to locate a trace segment having an end point at \a aPosition
 * on \a aLayerMask starting at \a aStartTrace and end at \a aEndTrace.
 * <p>
 * The segments of track that are flagged as deleted or busy are ignored.  Layer
 * visibility is also ignored.
 * </p>
 * @param aStartTrace A pointer to the TRACK object to begin searching.
 * @param aEndTrace A pointer to the TRACK object to stop the search.  A NULL value
 *                  searches to the end of the list.
 * @param aPosition A wxPoint object containing the position to test.
 * @param aLayerMask A layer or layers to mask the hit test.  Use -1 to ignore
 *                   layer mask.
 * @return A TRACK object pointer if found otherwise NULL.
 */
extern TRACK* GetTrace( TRACK* aStartTrace, TRACK* aEndTrace, const wxPoint& aPosition,
                        int aLayerMask );

44

45
class TRACK : public BOARD_CONNECTED_ITEM
46
{
47 48 49
    // make SetNext() and SetBack() private so that they may not be called from anywhere.
    // list management is done on TRACKs using DLIST<TRACK> only.
private:
50 51
    void SetNext( EDA_ITEM* aNext )       { Pnext = aNext; }
    void SetBack( EDA_ITEM* aBack )       { Pback = aBack; }
52 53


54
public:
55
    int         m_Width;            // Thickness of track, or via diameter
56 57 58 59
    wxPoint     m_Start;            // Line start point
    wxPoint     m_End;              // Line end point
    int         m_Shape;            // vias: shape and type, Track = shape..

60
protected:
dickelbeck's avatar
dickelbeck committed
61
    int         m_Drill;            // for vias: via drill (- 1 for default value)
62 63

public:
64 65 66 67
    BOARD_ITEM* start;              // pointers to a connected item (pad or track)
    BOARD_ITEM* end;

    int         m_Param;            // Auxiliary variable ( used in some computations )
68

69
protected:
70
    TRACK( const TRACK& track );    // protected so Copy() is used instead.
71

dickelbeck's avatar
dickelbeck committed
72
public:
73
    TRACK( BOARD_ITEM* aParent, KICAD_T idtype = PCB_TRACE_T );
74 75 76 77 78

    /**
     * Function Copy
     * will copy this object whether it is a TRACK or a SEGVIA returning
     * the corresponding type.
79 80 81 82 83 84
     * Because of the way SEGVIA and SEGZONE are derived from TRACK and because there are
     * virtual functions being used, we can no longer simply copy a TRACK and
     * expect it to be a via or zone.  We must construct a true SEGVIA or SEGZONE so its
     * constructor can initialize the virtual function table properly.  This factory type
     * of function called Copy() can duplicate either a TRACK, SEGVIA, or SEGZONE.
     *
85
     * @return - TRACK*, SEGVIA*, or SEGZONE*, declared as the least common
86
     *           denominator: TRACK
87
     */
88
    TRACK* Copy() const;
dickelbeck's avatar
dickelbeck committed
89

90 91
    TRACK* Next() const { return (TRACK*) Pnext; }
    TRACK* Back() const { return (TRACK*) Pback; }
dickelbeck's avatar
dickelbeck committed
92

93 94 95
    /**
     * Function Move
     * move this object.
96
     * @param aMoveVector - the move vector for this object.
97
     */
98
    virtual void Move( const wxPoint& aMoveVector )
99 100 101 102 103 104 105 106
    {
        m_Start += aMoveVector;
        m_End += aMoveVector;
    }

    /**
     * Function Rotate
     * Rotate this object.
107
     * @param aRotCentre - the rotation point.
108 109
     * @param aAngle - the rotation angle in 0.1 degree.
     */
110
    virtual void Rotate( const wxPoint& aRotCentre, int aAngle );
111 112 113 114

    /**
     * Function Flip
     * Flip this object, i.e. change the board side for this object
115
     * @param aCentre - the rotation point.
116
     */
117
    virtual void Flip( const wxPoint& aCentre );
118

119 120 121 122 123 124 125 126 127
    /**
     * Function GetPosition
     * returns the position of this object.
     * @return const wxPoint& - The position of this object.
     */
    wxPoint& GetPosition()
    {
        return m_Start;  // it had to be start or end.
    }
128

129
    EDA_RECT GetBoundingBox() const;
dickelbeck's avatar
dickelbeck committed
130

dickelbeck's avatar
dickelbeck committed
131 132
    /**
     * Function Save
dickelbeck's avatar
dickelbeck committed
133
     * writes the data structures for this object out to a FILE in "*.brd" format.
dickelbeck's avatar
dickelbeck committed
134 135
     * @param aFile The FILE to write to.
     * @return bool - true if success writing else false.
136
     */
137
    bool Save( FILE* aFile ) const;
138

dickelbeck's avatar
dickelbeck committed
139 140 141
    /**
     * Function GetBestInsertPoint
     * searches the "best" insertion point within the track linked list.
dickelbeck's avatar
dickelbeck committed
142 143
     * The best point is the begging of the corresponding net code section.
     * (The BOARD::m_Track and BOARD::m_Zone lists are sorted by netcode.)
144
     * @param aPcb The BOARD to search for the insertion point.
dickelbeck's avatar
dickelbeck committed
145 146
     * @return TRACK* - the item found in the linked list (or NULL if no track)
     */
147
    TRACK* GetBestInsertPoint( BOARD* aPcb );
dickelbeck's avatar
dickelbeck committed
148

CHARRAS's avatar
CHARRAS committed
149 150
    /* Search (within the track linked list) the first segment matching the netcode
     * ( the linked list is always sorted by net codes )
151
     */
152
    TRACK* GetStartNetCode( int NetCode );
dickelbeck's avatar
dickelbeck committed
153

CHARRAS's avatar
CHARRAS committed
154 155 156
    /* Search (within the track linked list) the last segment matching the netcode
     * ( the linked list is always sorted by net codes )
     */
157
    TRACK* GetEndNetCode( int NetCode );
dickelbeck's avatar
dickelbeck committed
158

159 160 161 162 163
    /**
     * Function GetLength
     * returns the length of the track using the hypotenuse calculation.
     * @return double - the length of the track
     */
164
    double GetLength() const
165
    {
166 167 168
        int dx = m_Start.x - m_End.x;
        int dy = m_Start.y - m_End.y;

169 170
        return hypot( dx, dy );
    }
171 172


dickelbeck's avatar
dickelbeck committed
173
    /* Display on screen: */
174 175
    void Draw( EDA_DRAW_PANEL* panel, wxDC* DC, int aDrawMode,
               const wxPoint& aOffset = ZeroOffset );
dickelbeck's avatar
dickelbeck committed
176 177

    /* divers */
dickelbeck's avatar
dickelbeck committed
178
    int Shape() const { return m_Shape & 0xFF; }
dickelbeck's avatar
dickelbeck committed
179

180 181
    /**
     * Function TransformShapeWithClearanceToPolygon
182 183 184 185 186 187 188
     * Convert the track shape to a closed polygon
     * Used in filling zones calculations
     * Circles (vias) and arcs (ends of tracks) are approximated by segments
     * @param aCornerBuffer = a buffer to store the polygon
     * @param aClearanceValue = the clearance around the pad
     * @param aCircleToSegmentsCount = the number of segments to approximate a circle
     * @param aCorrectionFactor = the correction to apply to circles radius to keep
189
     * clearance when the circle is approximated by segment bigger or equal
190 191
     * to the real clearance value (usually near from 1.0)
     */
192
    void TransformShapeWithClearanceToPolygon( std::vector <CPolyPt>& aCornerBuffer,
193 194 195
                                               int                    aClearanceValue,
                                               int                    aCircleToSegmentsCount,
                                               double                 aCorrectionFactor );
dickelbeck's avatar
dickelbeck committed
196
    /**
197
     * Function SetDrillValue
dickelbeck's avatar
dickelbeck committed
198 199 200
     * Set the drill value for vias
     * @param drill_value = new drill value
    */
201
    void SetDrillValue( int drill_value ) { m_Drill = drill_value; }
202

dickelbeck's avatar
dickelbeck committed
203
    /**
204
     * Function SetDrillDefault
dickelbeck's avatar
dickelbeck committed
205 206
     * Set the drill value for vias at default value (-1)
    */
207
    void SetDrillDefault( void ) { m_Drill = -1; }
dickelbeck's avatar
dickelbeck committed
208 209

    /**
210
     * Function IsDrillDefault
dickelbeck's avatar
dickelbeck committed
211 212
     * @return true if the drill value is default value (-1)
    */
213
    bool IsDrillDefault( void ) { return m_Drill <= 0; }
214

dickelbeck's avatar
dickelbeck committed
215
    /**
216
     * Function GetDrillValue
dickelbeck's avatar
dickelbeck committed
217 218 219 220
     * calculate the drill value for vias (m-Drill if > 0, or default drill value for the board
     * @return real drill_value
    */
    int GetDrillValue() const;
dickelbeck's avatar
dickelbeck committed
221

222 223
    /**
     * Function ReturnMaskLayer
224
     * returns a "layer mask", which is a bitmap of all layers on which the
225
     * TRACK segment or SEGVIA physically resides.
226
     * @return int - a layer mask, see pcbstruct.h's LAYER_BACK, etc.
227
     */
228
    int ReturnMaskLayer();
229

230 231 232 233 234 235 236 237
    /**
     * Function IsPointOnEnds
     * returns STARTPOINT if point if near (dist = min_dist) start point, ENDPOINT if
     * point if near (dist = min_dist) end point,STARTPOINT|ENDPOINT if point if near
     * (dist = min_dist) both ends, or 0 if none of the above.
     * if min_dist < 0: min_dist = track_width/2
     */
    int IsPointOnEnds( const wxPoint& point, int min_dist = 0 );
238

239 240 241 242
    /**
     * Function IsNull
     * returns true if segment length is zero.
     */
243
    bool IsNull();
dickelbeck's avatar
dickelbeck committed
244

245
    /**
246
     * Function DisplayInfo
247 248
     * has knowledge about the frame and how and where to put status information
     * about this object into the frame's message panel.
249
     * Is virtual from EDA_ITEM.
250
     * Display info about the track segment and the full track length
251
     * @param frame A EDA_DRAW_FRAME in which to print status information.
252
     */
253
    void DisplayInfo( EDA_DRAW_FRAME* frame );
254

255 256 257 258 259
    /**
     * Function DisplayInfoBase
     * has knowledge about the frame and how and where to put status information
     * about this object into the frame's message panel.
     * Display info about the track segment only, and does not calculate the full track length
260
     * @param frame A EDA_DRAW_FRAME in which to print status information.
261
     */
262
    void DisplayInfoBase( EDA_DRAW_FRAME* frame );
263

264 265 266 267
    /**
     * Function ShowWidth
     * returns the width of the track in displayable user units.
     */
268
    wxString ShowWidth() const;
269

dickelbeck's avatar
dickelbeck committed
270 271 272 273
    /**
     * Function Visit
     * is re-implemented here because TRACKs and SEGVIAs are in the same list
     * within BOARD.  If that were not true, then we could inherit the
274
     * version from EDA_ITEM.  This one does not iterate through scanTypes
275
     * but only looks at the first item in the list.
dickelbeck's avatar
dickelbeck committed
276 277
     * @param inspector An INSPECTOR instance to use in the inspection.
     * @param testData Arbitrary data used by the inspector.
278
     * @param scanTypes Which KICAD_T types are of interest and the order
dickelbeck's avatar
dickelbeck committed
279 280 281 282
     *  is significant too, terminated by EOT.
     * @return SEARCH_RESULT - SEARCH_QUIT if the Iterator is to stop the scan,
     *  else SCAN_CONTINUE, and determined by the inspector.
     */
283 284
    SEARCH_RESULT Visit( INSPECTOR* inspector, const void* testData,
                         const KICAD_T scanTypes[] );
285 286


dickelbeck's avatar
dickelbeck committed
287 288 289 290 291 292
    /**
     * Function HitTest
     * tests if the given wxPoint is within the bounds of this object.
     * @param refPos A wxPoint to test
     * @return bool - true if a hit, else false
     */
293
    bool HitTest( const wxPoint& refPos );
dickelbeck's avatar
dickelbeck committed
294 295

    /**
296
     * Function HitTest (overlaid)
297
     * tests if the given wxRect intersect this object.
298
     * For now, an ending point must be inside this rect.
299
     * @param refArea an EDA_RECT to test
300 301
     * @return bool - true if a hit, else false
     */
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    bool HitTest( EDA_RECT& refArea );

    /**
     * Function GetVia
     * finds the first SEGVIA object at \a aPosition on \a aLayer starting at the trace.
     *
     * @param aPosition The wxPoint to HitTest() against.
     * @param aLayerMask The layer to match, pass -1 for a don't care.
     * @return A pointer to a SEGVIA object if found, else NULL.
     */
    TRACK* GetVia( const wxPoint& aPosition, int aLayerMask = -1 );

    /**
     * Function GetVia
     * finds the first SEGVIA object at \a aPosition on \a aLayer starting at the trace
     * and ending at \a aEndTrace.
     *
     * @param aEndTrace Pointer to the last TRACK object to end search.
     * @param aPosition The wxPoint to HitTest() against.
     * @param aLayerMask The layers to match, pass -1 for a don't care.
     * @return A pointer to a SEGVIA object if found, else NULL.
     */
    TRACK* GetVia( TRACK* aEndTrace, const wxPoint& aPosition, int aLayerMask );
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
    /**
     * Function GetTrace
     * return the trace segment connected to the segment at \a aEndPoint from \a
     * aStartTrace to \a aEndTrace.
     *
     * @param aStartTrace A pointer to the TRACK object to begin searching.
     * @param aEndTrace A pointer to the TRACK object to stop the search.  A NULL value
     *                  searches to the end of the list.
     * @param aEndPoint The start or end point of the segment to test against.
     * @return A TRACK object pointer if found otherwise NULL.
     */
    TRACK* GetTrace( TRACK* aStartTrace, TRACK* aEndTrace, int aEndPoint );

    /**
     * Function GetEndSegments
     * get the segments connected to the end point of the track.
     *  return 1 if OK, 0 when a track is a closed loop
     *  and the beginning and the end of the track in *StartTrack and *EndTrack
     *  Modify *StartTrack en *EndTrack  :
     *  (*StartTrack)->m_Start coordinate is the beginning of the track
     *  (*EndTrack)->m_End coordinate is the end of the track
     *  Segments connected must be consecutive in list
     */
    int GetEndSegments( int NbSegm, TRACK** StartTrack, TRACK** EndTrack );

351
    /**
dickelbeck's avatar
dickelbeck committed
352 353 354 355 356 357
     * Function GetClass
     * returns the class name.
     * @return wxString
     */
    wxString GetClass() const
    {
358
        return wxT( "TRACK" );
dickelbeck's avatar
dickelbeck committed
359
    }
360

361 362 363 364 365 366 367 368 369 370 371
     /**
     * Function GetClearance
     * returns the clearance in internal units.  If \a aItem is not NULL then the
     * returned clearance is the greater of this object's clearance and
     * aItem's clearance.  If \a aItem is NULL, then this objects clearance
     * is returned.
     * @param aItem is another BOARD_CONNECTED_ITEM or NULL
     * @return int - the clearance in internal units.
     */
    virtual int GetClearance( BOARD_CONNECTED_ITEM* aItem = NULL ) const;

372
    virtual wxString GetSelectMenuText() const;
373

374
    virtual BITMAP_DEF GetMenuImage() const { return  showtrack_xpm; }
375

376 377
#if defined (DEBUG)

dickelbeck's avatar
dickelbeck committed
378 379 380
    /**
     * Function Show
     * is used to output the object tree, currently for debugging only.
381
     * @param nestLevel An aid to prettier tree indenting, and is the level
dickelbeck's avatar
dickelbeck committed
382 383 384 385
     *          of nesting of this object within the overall tree.
     * @param os The ostream& to output to.
     */
    void Show( int nestLevel, std::ostream& os );
386

387 388 389 390 391 392 393 394

    /**
     * Function ShowState
     * converts a set of state bits to a wxString
     * @param stateBits Is an OR-ed together set of bits like BUSY, EDIT, etc.
     */
    static wxString ShowState( int stateBits );

395
#endif
396 397
};

398

dickelbeck's avatar
dickelbeck committed
399
class SEGZONE : public TRACK
400 401
{
public:
402
    SEGZONE( BOARD_ITEM* aParent );
403

dickelbeck's avatar
dickelbeck committed
404 405 406 407 408 409 410
    /**
     * Function GetClass
     * returns the class name.
     * @return wxString
     */
    wxString GetClass() const
    {
411
        return wxT( "ZONE" );
dickelbeck's avatar
dickelbeck committed
412
    }
413

414 415

    SEGZONE* Next() const { return (SEGZONE*) Pnext; }
416 417 418

    virtual wxString GetSelectMenuText() const;

419
    virtual BITMAP_DEF GetMenuImage() const { return  add_zone_xpm; }
420 421
};

422

dickelbeck's avatar
dickelbeck committed
423
class SEGVIA : public TRACK
424 425
{
public:
426
    SEGVIA( BOARD_ITEM* aParent );
427 428 429 430 431

    SEGVIA( const SEGVIA& source ) :
        TRACK( source )
    {
    }
432 433


434 435
    void Draw( EDA_DRAW_PANEL* panel, wxDC* DC, int aDrawMode,
               const wxPoint& aOffset = ZeroOffset );
436

dickelbeck's avatar
dickelbeck committed
437 438
    /**
     * Function IsOnLayer
439
     * tests to see if this object is on the given layer.  Is virtual
440 441
     * from BOARD_ITEM.  Tests the starting and ending range of layers for the via.
     * @param aLayer the layer to test for.
dickelbeck's avatar
dickelbeck committed
442 443
     * @return bool - true if on given layer, else false.
     */
444
    bool IsOnLayer( int aLayer ) const;
445

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
    /**
     * Function SetLayerPair
     * set the .m_Layer member param:
     *  For a via m_Layer contains the 2 layers :
     * top layer and bottom layer used by the via.
     * The via connect all layers from top layer to bottom layer
     * 4 bits for the first layer and 4 next bits for the secaon layer
     * @param top_layer = first layer connected by the via
     * @param bottom_layer = last layer connected by the via
     */
    void SetLayerPair( int top_layer, int bottom_layer );

    /**
     * Function ReturnLayerPair
     * Return the 2 layers used by  the via (the via actually uses
     * all layers between these 2 layers)
     *  @param top_layer = pointer to the first layer (can be null)
     *  @param bottom_layer = pointer to the last layer (can be null)
     */
    void ReturnLayerPair( int* top_layer, int* bottom_layer ) const;
466

467 468 469 470 471
    /**
     * Function GetPosition
     * returns the position of this object.
     * @return const wxPoint& - The position of this object.
     */
472 473 474
    wxPoint& GetPosition()
    {
        return m_Start;
475
    }
476 477


478
    void SetPosition( const wxPoint& aPoint ) { m_Start = aPoint;  m_End = aPoint; }
479

dickelbeck's avatar
dickelbeck committed
480 481 482 483 484 485 486
    /**
     * Function GetClass
     * returns the class name.
     * @return wxString
     */
    wxString GetClass() const
    {
487
        return wxT( "VIA" );
dickelbeck's avatar
dickelbeck committed
488
    }
489

490

491 492
    virtual wxString GetSelectMenuText() const;

493
    virtual BITMAP_DEF GetMenuImage() const { return  via_sketch_xpm; }
494

495 496
#if defined (DEBUG)

497 498 499
    /**
     * Function Show
     * is used to output the object tree, currently for debugging only.
500
     * @param nestLevel An aid to prettier tree indenting, and is the level
501 502 503 504
     *          of nesting of this object within the overall tree.
     * @param os The ostream& to output to.
     */
    void Show( int nestLevel, std::ostream& os );
505 506

#endif
507 508 509 510
};


#endif /* CLASS_TRACK_H */