sch_sheet_pin.cpp 11.1 KB
Newer Older
1 2 3 4
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2006 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
5
 * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * 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
 */

/**
 * @file sch_sheet_pin.cpp
 * @brief Implementation of the SCH_SHEET_PIN class.
 */
charras's avatar
charras committed
29

30 31 32 33 34 35 36 37
#include <fctsys.h>
#include <gr_basic.h>
#include <class_drawpanel.h>
#include <drawtxt.h>
#include <plot_common.h>
#include <trigo.h>
#include <richio.h>
#include <wxEeschemaStruct.h>
38

39 40 41 42
#include <general.h>
#include <protos.h>
#include <sch_sheet.h>
#include <kicad_string.h>
43

charras's avatar
charras committed
44

45
SCH_SHEET_PIN::SCH_SHEET_PIN( SCH_SHEET* parent, const wxPoint& pos, const wxString& text ) :
46
    SCH_HIERLABEL( pos, text, SCH_SHEET_PIN_T )
charras's avatar
charras committed
47
{
48
    SetParent( parent );
49
    wxASSERT( parent );
50 51
    m_Layer = LAYER_SHEETLABEL;
    m_Pos   = pos;
52

53 54 55 56
    if( parent->IsVerticalOrientation() )
        SetEdge( 2 );
    else
        SetEdge( 0 );
57

58 59 60
    m_shape = NET_INPUT;
    m_isDangling = true;
    m_number     = 2;
charras's avatar
charras committed
61 62 63
}


64
EDA_ITEM* SCH_SHEET_PIN::Clone() const
65 66
{
    return new SCH_SHEET_PIN( *this );
charras's avatar
charras committed
67 68
}

69

70 71 72
void SCH_SHEET_PIN::Draw( EDA_DRAW_PANEL* aPanel,
                          wxDC*           aDC,
                          const wxPoint&  aOffset,
73
                          GR_DRAWMODE     aDraw_mode,
74
                          EDA_COLOR_T     aColor )
75 76 77
{
    // The icon selection is handle by the virtual method CreateGraphicShape
    // called by ::Draw
78
    SCH_HIERLABEL::Draw( aPanel, aDC, aOffset, aDraw_mode, aColor );
79 80
}

charras's avatar
charras committed
81

82
void SCH_SHEET_PIN::SwapData( SCH_ITEM* aItem )
83
{
84 85 86 87 88 89
    wxCHECK_RET( aItem->Type() == SCH_SHEET_PIN_T,
                 wxString::Format( wxT( "SCH_SHEET_PIN object cannot swap data with %s object." ),
                                   GetChars( aItem->GetClass() ) ) );

    SCH_SHEET_PIN* pin = ( SCH_SHEET_PIN* ) aItem;
    SCH_TEXT::SwapData( (SCH_TEXT*) pin );
90
    int tmp;
91 92
    tmp = pin->GetNumber();
    pin->SetNumber( GetNumber() );
93
    SetNumber( tmp );
94 95
    tmp = pin->GetEdge();
    pin->SetEdge( GetEdge() );
96 97 98 99 100
    SetEdge( tmp );
}


bool SCH_SHEET_PIN::operator==( const SCH_SHEET_PIN* aPin ) const
101 102 103 104 105
{
    return aPin == this;
}


106
int SCH_SHEET_PIN::GetPenSize() const
107
{
108
    return GetDefaultLineThickness();
109 110 111
}


112 113 114 115
void SCH_SHEET_PIN::SetNumber( int aNumber )
{
    wxASSERT( aNumber >= 2 );

116
    m_number = aNumber;
117 118 119
}


120
void SCH_SHEET_PIN::SetEdge( int aEdge )
charras's avatar
charras committed
121
{
122
    SCH_SHEET* Sheet = (SCH_SHEET*) GetParent();
123

124 125
    /* use -1 to adjust text orientation without changing edge*/
    if( aEdge > -1 )
126
        m_edge = aEdge;
127

128
    switch( m_edge )
129 130
    {
    case 0:        /* pin on left side*/
131
        m_Pos.x = Sheet->m_pos.x;
132
        SetOrientation( 2 ); /* Orientation horiz inverse */
133
        break;
charras's avatar
charras committed
134

135
    case 1:        /* pin on right side*/
136
        m_Pos.x = Sheet->m_pos.x + Sheet->m_size.x;
137
        SetOrientation( 0 ); /* Orientation horiz normal */
138
        break;
charras's avatar
charras committed
139

140
    case 2:        /* pin on top side*/
141
        m_Pos.y = Sheet->m_pos.y;
142
        SetOrientation( 3 ); /* Orientation vert BOTTOM  */
143
        break;
144

145
    case 3:        /* pin on bottom side*/
146
        m_Pos.y = Sheet->m_pos.y + Sheet->m_size.y;
147
        SetOrientation( 1 ); /* Orientation vert UP  */
148
        break;
charras's avatar
charras committed
149
    }
charras's avatar
charras committed
150 151 152
}


153
int SCH_SHEET_PIN::GetEdge() const
154
{
155
    return m_edge;
156 157 158
}


159
void SCH_SHEET_PIN::ConstrainOnEdge( wxPoint Pos )
charras's avatar
charras committed
160
{
161
    SCH_SHEET* Sheet = (SCH_SHEET*) GetParent();
charras's avatar
charras committed
162

163 164
    if( Sheet == NULL )
        return;
charras's avatar
charras committed
165

166
    if( m_edge<2 ) /*horizontal sheetpin*/
charras's avatar
charras committed
167
    {
168
        if( Pos.x > ( Sheet->m_pos.x + ( Sheet->m_size.x / 2 ) ) )
169 170 171 172 173 174 175
        {
            SetEdge( 1 );
        }
        else
        {
            SetEdge( 0 );
        }
charras's avatar
charras committed
176

177
        m_Pos.y = Pos.y;
178

179 180
        if( m_Pos.y < Sheet->m_pos.y )
            m_Pos.y = Sheet->m_pos.y;
181

182 183
        if( m_Pos.y > (Sheet->m_pos.y + Sheet->m_size.y) )
            m_Pos.y = Sheet->m_pos.y + Sheet->m_size.y;
184 185 186
    }
    else /* vertical sheetpin*/
    {
187
        if( Pos.y > ( Sheet->m_pos.y + ( Sheet->m_size.y / 2 ) ) )
188 189 190 191 192 193 194
        {
            SetEdge( 3 ); //bottom
        }
        else
        {
            SetEdge( 2 ); //top
        }
charras's avatar
charras committed
195

196
        m_Pos.x = Pos.x;
197

198 199
        if( m_Pos.x < Sheet->m_pos.x )
            m_Pos.x = Sheet->m_pos.x;
200

201 202
        if( m_Pos.x > (Sheet->m_pos.x + Sheet->m_size.x) )
            m_Pos.x = Sheet->m_pos.x + Sheet->m_size.x;
charras's avatar
charras committed
203 204 205 206
    }
}


207
bool SCH_SHEET_PIN::Save( FILE* aFile ) const
charras's avatar
charras committed
208 209 210 211 212
{
    int type = 'U', side = 'L';

    if( m_Text.IsEmpty() )
        return true;
213

214
    switch( m_edge )
215 216 217 218 219 220
    {
    case 0:     //pin on left side
        side = 'L';
        break;

    case 1:     //pin on right side
charras's avatar
charras committed
221
        side = 'R';
222 223 224 225 226 227 228 229 230 231
        break;

    case 2:      //pin on top side
        side = 'T';
        break;

    case 3:     //pin on bottom side
        side = 'B';
        break;
    }
charras's avatar
charras committed
232

233
    switch( m_shape )
charras's avatar
charras committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    {
    case NET_INPUT:
        type = 'I'; break;

    case NET_OUTPUT:
        type = 'O'; break;

    case NET_BIDI:
        type = 'B'; break;

    case NET_TRISTATE:
        type = 'T'; break;

    case NET_UNSPECIFIED:
        type = 'U'; break;
    }

251
    if( fprintf( aFile, "F%d %s %c %c %-3d %-3d %-3d\n", m_number,
Dick Hollenbeck's avatar
Dick Hollenbeck committed
252 253
                 EscapedUTF8( m_Text ).c_str(),     // supplies wrapping quotes
                 type, side, m_Pos.x, m_Pos.y,
254
                 m_Size.x ) == EOF )
charras's avatar
charras committed
255 256 257 258 259 260 261
    {
        return false;
    }

    return true;
}

262

263 264
bool SCH_SHEET_PIN::Load( LINE_READER& aLine, wxString& aErrorMsg )
{
Dick Hollenbeck's avatar
Dick Hollenbeck committed
265 266 267 268 269 270 271 272 273 274 275
    int     size;
    char    number[256];
    char    name[256];
    char    connectType[256];
    char    sheetSide[256];
    char*   line = aLine.Line();
    char*   cp;

    static const char delims[] = " \t";

    // Read coordinates.
276
    // D( printf( "line: \"%s\"\n", line );)
Dick Hollenbeck's avatar
Dick Hollenbeck committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

    cp = strtok( line, delims );

    strncpy( number, cp, sizeof(number) );
    number[sizeof(number)-1] = 0;

    cp += strlen( number ) + 1;

    cp += ReadDelimitedText( name, cp, sizeof(name) );

    cp = strtok( cp, delims );
    strncpy( connectType, cp, sizeof(connectType) );
    connectType[sizeof(connectType)-1] = 0;

    cp = strtok( NULL, delims );
    strncpy( sheetSide, cp, sizeof(sheetSide) );
    sheetSide[sizeof(sheetSide)-1] = 0;

    cp += strlen( sheetSide ) + 1;

    int r = sscanf( cp, "%d %d %d", &m_Pos.x, &m_Pos.y, &size );
    if( r != 3 )
299
    {
300
        aErrorMsg.Printf( wxT( "Eeschema file sheet hierarchical label error at line %d.\n" ),
301
                          aLine.LineNumber() );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
302 303

        aErrorMsg << FROM_UTF8( line );
304 305 306
        return false;
    }

307
    m_Text = FROM_UTF8( name );
308 309

    if( size == 0 )
310
        size = GetDefaultTextSize();
311 312 313 314 315 316

    m_Size.x = m_Size.y = size;

    switch( connectType[0] )
    {
    case 'I':
317
        m_shape = NET_INPUT;
318
        break;
319

320
    case 'O':
321
        m_shape = NET_OUTPUT;
322
        break;
323

324
    case 'B':
325
        m_shape = NET_BIDI;
326
        break;
327

328
    case 'T':
329
        m_shape = NET_TRISTATE;
330
        break;
331

332
    case 'U':
333
        m_shape = NET_UNSPECIFIED;
334 335 336 337 338 339 340 341
        break;
    }

    switch( sheetSide[0] )
    {
    case 'R' : /* pin on right side */
        SetEdge( 1 );
        break;
342

343 344 345
    case 'T' : /* pin on top side */
        SetEdge( 2 );
        break;
346

347 348 349
    case 'B' : /* pin on bottom side */
        SetEdge( 3 );
        break;
350

351 352 353 354 355 356 357 358 359 360
    case 'L' : /* pin on left side */
    default  :
        SetEdge( 0 );
        break;
    }

    return true;
}


361
bool SCH_SHEET_PIN::Matches( wxFindReplaceData& aSearchData,
362
                             void* aAuxData, wxPoint* aFindLocation )
363
{
364 365 366
    wxCHECK_MSG( GetParent() != NULL, false,
                 wxT( "Sheet pin " ) + m_Text + wxT( " does not have a parent sheet!" ) );

367
    wxLogTrace( traceFindItem, wxT( "    child item " ) + GetSelectMenuText() );
368

369 370 371
    if( SCH_ITEM::Matches( m_Text, aSearchData ) )
    {
        if( aFindLocation )
372
            *aFindLocation = GetBoundingBox().Centre();
373

374 375 376 377
        return true;
    }

    return false;
378 379 380
}


381
void SCH_SHEET_PIN::MirrorX( int aXaxis_position )
382 383 384 385
{
    int p = m_Pos.y - aXaxis_position;

    m_Pos.y = aXaxis_position - p;
386

387
    switch( m_edge )
388 389 390 391 392 393 394 395 396 397 398 399
    {
    case 2:
        SetEdge( 3 );
        break;

    case 3:
        SetEdge( 2 );
        break;
    }
}


400
void SCH_SHEET_PIN::MirrorY( int aYaxis_position )
401 402 403 404
{
    int p = m_Pos.x - aYaxis_position;

    m_Pos.x = aYaxis_position - p;
405

406
    switch( m_edge )
407 408 409 410 411 412 413 414 415 416 417 418
    {
    case 0:
        SetEdge( 1 );
        break;

    case 1:
        SetEdge( 0 );
        break;
    }
}


419
void SCH_SHEET_PIN::Rotate( wxPoint aPosition )
420
{
421
    RotatePoint( &m_Pos, aPosition, 900 );
422

423
    switch( m_edge )
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    {
    case 0:     //pin on left side
        SetEdge( 3 );
        break;

    case 1:     //pin on right side
        SetEdge( 2 );
        break;

    case 2:      //pin on top side
        SetEdge( 0 );
        break;

    case 3:     //pin on bottom side
        SetEdge( 1 );
        break;
    }
}


444
void SCH_SHEET_PIN::CreateGraphicShape( std::vector <wxPoint>& aPoints, const wxPoint& aPos )
445 446 447 448 449 450
{
     /* This is the same icon shapes as SCH_HIERLABEL
     * but the graphic icon is slightly different in 2 cases:
     * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
     * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
     */
451
    int tmp = m_shape;
452

453
    switch( m_shape )
454 455
    {
    case NET_INPUT:
456
        m_shape = NET_OUTPUT;
457 458 459
        break;

    case NET_OUTPUT:
460
        m_shape = NET_INPUT;
461 462 463 464 465
        break;

    default:
        break;
    }
466

467
    SCH_HIERLABEL::CreateGraphicShape( aPoints, aPos );
468
    m_shape = tmp;
469 470 471 472 473
}


void SCH_SHEET_PIN::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
{
474
    DANGLING_END_ITEM item( SHEET_LABEL_END, this, m_Pos );
475 476 477 478
    aItemList.push_back( item );
}


479 480
wxString SCH_SHEET_PIN::GetSelectMenuText() const
{
481
    wxString tmp;
482
    tmp.Printf( _( "Hierarchical Sheet Pin %s" ), GetChars( ShortenedShownText() ) );
483
    return tmp;
484 485 486
}


487
bool SCH_SHEET_PIN::HitTest( const wxPoint& aPoint, int aAccuracy ) const
488
{
489
    EDA_RECT rect = GetBoundingBox();
490 491 492 493 494 495 496

    rect.Inflate( aAccuracy );

    return rect.Contains( aPoint );
}


497
#if defined(DEBUG)
498

499
void SCH_SHEET_PIN::Show( int nestLevel, std::ostream& os ) const
500 501 502 503 504
{
    // XML output:
    wxString s = GetClass();

    NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << ">"
505
                                 << " pin_name=\"" << TO_UTF8( m_Text )
506
                                 << '"' << "/>\n" << std::flush;
507 508 509 510 511

//    NestedSpace( nestLevel, os ) << "</" << s.Lower().mb_str() << ">\n";
}

#endif