sch_part.cpp 17.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 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 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2011 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 2010 KiCad Developers, see change_log.txt for contributors.
 *
 * 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
 */

#include <wx/wx.h>          // _()

#include <sch_part.h>
#include <sch_sweet_parser.h>
#include <sch_lpid.h>
#include <sch_lib_table.h>
#include <macros.h>



/**
 * Function formatAt
 * outputs a formatted "(at X Y [ANGLE])" s-expression
 */
 static void formatAt( OUTPUTFORMATTER* out, const SCH::POINT& aPos, ANGLE aAngle, int indent=0 )
    throw( IO_ERROR )
{
    // if( aPos.x || aPos.y || aAngle )
    {
        out->Print( indent, aAngle!=0.0 ? "(at %.6g %.6g %.6g)" : "(at %.6g %.6g)",
            InternalToLogical( aPos.x ), InternalToLogical( aPos.y ),
            double( aAngle ) );
    }
}

static void formatStroke( OUTPUTFORMATTER* out, STROKE aStroke, int indent=0 )
    throw( IO_ERROR )
{
    if( aStroke == STROKE_DEFAULT )
        out->Print( indent, "(stroke %.6g)", InternalToWidth( aStroke ) );
}


using namespace SCH;


PART::PART( LIB* aOwner, const STRING& aPartNameAndRev ) :
    owner( aOwner ),
    contains( 0 ),
    partNameAndRev( aPartNameAndRev ),
    extends( 0 ),
    base( 0 )
{
    // Our goal is to have class LIB only instantiate what is needed, so print here
    // what it is doing. It is the only class where PART can be instantiated.
    D(printf("PART::PART(%s)\n", aPartNameAndRev.c_str() );)

    for( int i=REFERENCE;  i<END;  ++i )
        mandatory[i] = 0;
}


void PART::clear()
{
    if( extends )
    {
        delete extends;
        extends = 0;
    }

    // clear the mandatory fields
    for( int ndx = REFERENCE;  ndx < END;  ++ndx )
    {
        delete mandatory[ndx];
        mandatory[ndx] = 0;
    }

    // delete properties I own, since their container will not destroy them:
    for( PROPERTIES::iterator it = properties.begin();  it != properties.end();  ++it )
        delete *it;
    properties.clear();

    // delete graphics I own, since their container will not destroy them:
    for( GRAPHICS::iterator it = graphics.begin();  it != graphics.end();  ++it )
        delete *it;
    graphics.clear();

    // delete PINs I own, since their container will not destroy them.
    for( PINS::iterator it = pins.begin();  it != pins.end();  ++it )
        delete *it;
    pins.clear();

    alternates.clear();

    keywords.clear();

    pin_merges.clear();

    contains = 0;
}


PROPERTY* PART::FieldLookup( PROP_ID aPropertyId )
{
    wxASSERT( unsigned(aPropertyId) < unsigned(END) );

    PROPERTY* p = mandatory[aPropertyId];

    if( !p )
    {
        switch( aPropertyId )
        {
        case REFERENCE:
            p = new PROPERTY( this, wxT( "reference" ) );
            p->text = wxT( "U?" );
            break;

        case VALUE:
            p = new PROPERTY( this, wxT( "value" ) );
            break;

        case FOOTPRINT:
            p = new PROPERTY(  this, wxT( "footprint" ) );
            break;

        case DATASHEET:
            p = new PROPERTY( this, wxT( "datasheet" ) );
            break;

        case MODEL:
            p = new PROPERTY( this, wxT( "model" ) );
            break;

        default:
            ;
        }

        mandatory[aPropertyId] = p;
    }

    return p;
}


PROPERTY& PROPERTY::operator = ( const PROPERTY& r )
{
    *(BASE_GRAPHIC*) this = (BASE_GRAPHIC&) r;

    name  = r.name;
    text  = r.text;

    delete effects;

    if( r.effects )
        effects = new TEXT_EFFECTS( *r.effects );
    else
        effects = 0;

    return *this;
}


PINS::iterator PART::pinFindByPad( const wxString& aPad )
{
    PINS::iterator it;

    for( it = pins.begin();  it != pins.end();  ++it )
    {
        if( (*it)->pad.text == aPad )
            break;
    }

    return it;
}


void PART::PinsFindBySignal( PIN_LIST* aResults, const wxString& aSignal )
{
    for( PINS::const_iterator it = pins.begin();  it != pins.end();  ++it )
    {
        if( (*it)->signal.text == aSignal )
        {
            aResults->push_back( *it );
        }
    }
}


bool PART::PinDelete( const wxString& aPad )
{
    PINS::iterator it = pinFindByPad( aPad );
    if( it != pins.end() )
    {
        delete *it;
        pins.erase( it );
        return true;
    }

    // there is only one reason this can fail: not found:
    return false;
}


PART::~PART()
{
    clear();
}


void PART::setExtends( LPID* aLPID )
{
    delete extends;
    extends = aLPID;
}


void PART::inherit( const PART& r )
{
    // Inherit can be called at any time, even from an interactive text
    // editor, so cannot assume 'this' object is new.  Clear it.
    clear();

    // copy anything inherited, such as drawables, properties, pins, etc. here
    contains = r.contains;

    base     = &r;

    anchor   = r.anchor;

    for( int i=REFERENCE;  i<END;  ++i )
    {
        if( r.mandatory[i] )
            mandatory[i] = (PROPERTY*) r.mandatory[i]->Clone( this );
    }

    for( PROPERTIES::const_iterator it = r.properties.begin();  it != r.properties.end();  ++it )
        properties.push_back( (PROPERTY*) (*it)->Clone( this ) );

    for( GRAPHICS::const_iterator it = r.graphics.begin();  it != r.graphics.end();  ++it )
        graphics.push_back( (*it)->Clone( this ) );

    for( PINS::const_iterator it = r.pins.begin();  it != r.pins.end();  ++it )
        pins.push_back( (PIN*) (*it)->Clone( this ) );

    /* not sure about this concept yet:
    for( PART_REFS::const_iterator it = r.alternates.begin();  it != r.alternates.end();  ++it )
        alternates.push_back( *it );
    */

    for( KEYWORDS::const_iterator it = r.keywords.begin();  it != r.keywords.end();  ++it )
        keywords.insert( *it );

    for( MERGE_SETS::const_iterator it = r.pin_merges.begin();  it != r.pin_merges.end();  ++it )
    {
        pin_merges[ *it->first ] = * new MERGE_SET( *it->second );
    }
}


PART& PART::operator=( const PART& r )
{
    // maintain in concert with inherit(), which is a partial assignment operator.
    inherit( r );

    owner          = r.owner;
    partNameAndRev = r.partNameAndRev;
    body           = r.body;
    base           = r.base;

    setExtends( r.extends ? new LPID( *r.extends ) : 0 );

    return *this;
}


void PART::Parse( SWEET_PARSER* aParser , LIB_TABLE* aTable ) throw( IO_ERROR, PARSE_ERROR )
{
    aParser->Parse( this, aTable );
}


bool PART::PropDelete( const wxString& aPropertyName )
{
    PROPERTIES::iterator it = propertyFind( aPropertyName );
    if( it != properties.end() )
    {
        delete *it;
        properties.erase( it );
        return true;
    }

    return false;
}


PROPERTIES::iterator PART::propertyFind( const wxString& aPropertyName )
{
    PROPERTIES::iterator it;
    for( it = properties.begin();  it!=properties.end();  ++it )
        if( (*it)->name == aPropertyName )
            break;
    return it;
}


void PART::Format( OUTPUTFORMATTER* out, int indent, int ctl ) const
    throw( IO_ERROR )
{
    out->Print( indent, "(part %s", partNameAndRev.c_str() );

    if( extends )
        out->Print( 0, " inherits %s", extends->Format().c_str() );

    out->Print( 0, "\n" );

    for( int i = REFERENCE;  i < END;  ++i )
    {
        PROPERTY* prop = Field( PROP_ID( i ) );
        if( prop )
            prop->Format( out, indent+1, ctl );
    }

    for( PROPERTIES::const_iterator it = properties.begin();  it != properties.end();  ++it )
    {
        (*it)->Format( out, indent+1, ctl );
    }

    if( anchor.x || anchor.y )
    {
        out->Print( indent+1, "(anchor (at %.6g %.6g))\n",
            InternalToLogical( anchor.x ),
            InternalToLogical( anchor.y ) );
    }

    if( keywords.size() )
    {
        out->Print( indent+1, "(keywords" );
        for( KEYWORDS::iterator it = keywords.begin();  it != keywords.end();  ++it )
            out->Print( 0, " %s", out->Quotew( *it ).c_str() );
        out->Print( 0, ")\n" );
    }

    for( GRAPHICS::const_iterator it = graphics.begin();  it != graphics.end();  ++it )
    {
        (*it)->Format( out, indent+1, ctl );
    }

    for( PINS::const_iterator it = pins.begin();  it != pins.end();  ++it )
    {
        (*it)->Format( out, indent+1, ctl );
    }

    if( alternates.size() )
    {
        out->Print( indent+1, "(alternates" );
        for( PART_REFS::const_iterator it = alternates.begin();  it!=alternates.end();  ++it )
            out->Print( 0, " %s", out->Quotes( it->Format() ).c_str() );
        out->Print( 0, ")\n" );
    }

    for( MERGE_SETS::const_iterator mit = pin_merges.begin();  mit != pin_merges.end();  ++mit )
    {
        out->Print( indent+1, "(pin_merge %s (pads", out->Quotew( mit->first ).c_str() );

        const MERGE_SET& mset = *mit->second;
        for( MERGE_SET::const_iterator pit = mset.begin();  pit != mset.end();  ++pit )
        {
            out->Print( 0, " %s", out->Quotew( *pit ).c_str() );
        }
        out->Print( 0, "))\n" );
    }

    out->Print( indent, ")\n" );
}


//-----< PART objects  >------------------------------------------------------


void PROPERTY::Format( OUTPUTFORMATTER* out, int indent, int ctl ) const
    throw( IO_ERROR )
{
    wxASSERT( owner );      // all PROPERTYs should have an owner.

    int i;
    for( i = PART::REFERENCE;  i < PART::END;  ++i )
    {
        if( owner->Field( PART::PROP_ID(i) ) == this )
            break;
    }

    if( i < PART::END )   // is a field not a property
        out->Print( indent, "(%s", TO_UTF8( name ) );
    else
        out->Print( indent, "(property %s", out->Quotew( name ).c_str() );

    if( effects )
    {
        out->Print( 0, " %s\n", out->Quotew( text ).c_str() );
        effects->Format( out, indent+1, ctl | CTL_OMIT_NL );
        out->Print( 0, ")\n" );
    }
    else
    {
        out->Print( 0, " %s)\n", out->Quotew( text ).c_str() );
    }
}


TEXT_EFFECTS* PROPERTY::EffectsLookup()
{
    if( !effects )
    {
        effects = new TEXT_EFFECTS();
    }

    return effects;
}


void TEXT_EFFECTS::Format( OUTPUTFORMATTER* out, int indent, int ctl ) const
    throw( IO_ERROR )
{
    if( propName.IsEmpty() )
        out->Print( indent, "(effects " );
    else
        out->Print( indent, "(effects %s ",  out->Quotew( propName ).c_str() );

    formatAt( out, pos, angle );

    font.Format( out, 0, ctl | CTL_OMIT_NL );

    out->Print( 0, "(visible %s))%s",
            isVisible ? "yes" : "no",
            ctl & CTL_OMIT_NL ? "" : "\n" );
}


void FONT::Format( OUTPUTFORMATTER* out, int indent, int ctl ) const
    throw( IO_ERROR )
{
    if( italic || bold || !name.IsEmpty() || size.height != FONTZ_DEFAULT || size.width != FONTZ_DEFAULT )
    {
        if( name.IsEmpty() )
            out->Print( indent, "(font " );
        else
            out->Print( indent, "(font %s ", out->Quotew( name ).c_str() );

        out->Print( 0, "(size %.6g %.6g)",
                InternalToFontz( size.height ),
                InternalToFontz( size.width ) );

        if( italic )
            out->Print( 0, " italic" );

        if( bold )
            out->Print( 0, " bold" );

        out->Print( 0, ")%s", (ctl & CTL_OMIT_NL) ? "" : "\n" );
    }
}


void PIN::Format( OUTPUTFORMATTER* out, int indent, int ctl ) const
    throw( IO_ERROR )
{
    bool    hasSignal = !signal.text.IsEmpty();
    bool    hasPad    = !pad.text.IsEmpty();

    out->Print( indent, "(pin" );

    if( connectionType != PIN_CONN_DEFAULT )
        out->Print( 0, " %s", ShowType() );

    if( shape != PIN_SHAPE_DEFAULT )
        out->Print( 0, " %s", ShowShape() );

    out->Print( 0, " " );

    if( pos.x || pos.y || angle )
        formatAt( out, pos, angle );

    if( length != PIN_LEN_DEFAULT )
        out->Print( 0, "(length %.6g)", InternalToLogical( length ) );

    if( !isVisible )
        out->Print( 0, "(visible %s)", isVisible ? "yes" : "no" );

    if( hasSignal )
        signal.Format(  out, "signal",  0, CTL_OMIT_NL );

    if( hasPad )
        pad.Format( out, "pad", 0, CTL_OMIT_NL );

    out->Print( 0, ")\n" );
}


PIN::~PIN()
{
}


void PINTEXT::Format( OUTPUTFORMATTER* out, const char* aElement, int indent, int ctl ) const
    throw( IO_ERROR )
{
    out->Print( indent, "(%s %s", aElement, out->Quotew( text ).c_str() );

    font.Format( out, 0, CTL_OMIT_NL );

    if( !isVisible )
        out->Print( 0, " (visible %s)", isVisible ? "yes" : "no" );

    out->Print( 0, ")%s", ctl & CTL_OMIT_NL ? "" : "\n" );
}


void POLY_LINE::Format( OUTPUTFORMATTER* out, int indent, int ctl ) const
    throw( IO_ERROR )
{
    out->Print( indent, "(%s ",  pts.size() == 2 ? "line" : "polyline" );
    formatContents( out, indent, ctl );
}


void POLY_LINE::formatContents(  OUTPUTFORMATTER* out, int indent, int ctl ) const
    throw( IO_ERROR )
{
    formatStroke( out, stroke );

    if( fillType != PR::T_none )
        out->Print( 0, "(fill %s)", ShowFill( fillType ) );

    out->Print( 0, "\n" );

    if( pts.size() )
    {
        const int   maxLength = 75;
        int         len = 10;

        len += out->Print( indent+1, "(pts " );

        for( POINTS::const_iterator it = pts.begin();  it != pts.end();  ++it )
        {
            if( len > maxLength )
            {
                len = 10;
                out->Print( 0, "\n" );
                out->Print( indent+2, "(xy %.6g %.6g)",
                    InternalToLogical( it->x ), InternalToLogical( it->y ) );
            }
            else
                out->Print( 0, "(xy %.6g %.6g)",
                    InternalToLogical( it->x ), InternalToLogical( it->y ) );
        }

        out->Print( 0, ")" );
    }

    out->Print( 0, ")\n" );
}


void BEZIER::Format( OUTPUTFORMATTER* out, int indent, int ctl ) const
    throw( IO_ERROR )
{
    out->Print( indent, "(bezier " );
    formatContents( out, indent, ctl );     // inherited from POLY_LINE
}


void RECTANGLE::Format( OUTPUTFORMATTER* out, int indent, int ctl ) const
    throw( IO_ERROR )
{
    // (rectangle (start X Y) (end X Y) [(stroke WIDTH)] (fill FILL_TYPE))

    out->Print( indent, "(rectangle (start %.6g %.6g)(end %.6g %.6g)",
        InternalToLogical( start.x ), InternalToLogical( start.y ),
        InternalToLogical( end.x ), InternalToLogical( end.y )
        );

    formatStroke( out, stroke );

    if( fillType != PR::T_none )
        out->Print( 0, "(fill %s)", ShowFill( fillType ) );

    out->Print( 0, ")\n" );
}


void CIRCLE::Format( OUTPUTFORMATTER* out, int indent, int ctl ) const
    throw( IO_ERROR )
{
    /*
        (circle (center X Y)(radius LENGTH) [(stroke WIDTH)] (fill FILL_TYPE))
    */

    out->Print( indent, "(circle (center %.6g %.6g)(radius %.6g)",
        InternalToLogical( center.x ), InternalToLogical( center.y ),
        InternalToLogical( radius) );

    formatStroke( out, stroke );

    if( fillType != PR::T_none )
        out->Print( 0, "(fill %s)", ShowFill( fillType ) );

    out->Print( 0, ")\n" );
}


void ARC::Format( OUTPUTFORMATTER* out, int indent, int ctl ) const
    throw( IO_ERROR )
{
    /*
        (arc (pos X Y)(radius RADIUS)(start X Y)(end X Y) [(stroke WIDTH)] (fill FILL_TYPE))
    */

    out->Print( indent, "(arc (pos %.6g %.6g)(radius %.6g)(start %.6g %.6g)(end %.6g %.6g)",
        InternalToLogical( pos.x ), InternalToLogical( pos.y ),
        InternalToLogical( radius),
        InternalToLogical( start.x ), InternalToLogical( start.y ),
        InternalToLogical( end.x ),   InternalToLogical( end.y )
        );

    formatStroke( out, stroke );

    if( fillType != PR::T_none )
        out->Print( 0, "(fill %s)", ShowFill( fillType ) );

    out->Print( 0, ")\n" );
}


void GR_TEXT::Format( OUTPUTFORMATTER* out, int indent, int ctl ) const
    throw( IO_ERROR )
{
    /*
        (text "This is the text that gets drawn."
            (at X Y [ANGLE])(justify HORIZONTAL_JUSTIFY VERTICAL_JUSTIFY)(visible YES)(fill FILL_TYPE)
            (font [FONT] (size HEIGHT WIDTH) [italic] [bold])
        )
    */

    out->Print( indent, "(text %s\n", out->Quotew( text ).c_str() );

    formatAt( out, pos, angle, indent+1 );

    if( hjustify != PR::T_left || vjustify != PR::T_bottom )
        out->Print( 0, "(justify %s %s)",
            ShowJustify( hjustify ), ShowJustify( vjustify ) );

    if( !isVisible )
        out->Print( 0, "(visible %s)", isVisible ? "yes" : "no" );

    if( fillType != PR::T_filled )
        out->Print( 0, "(fill %s)", ShowFill( fillType ) );

    font.Format( out, 0, CTL_OMIT_NL );
    out->Print( 0, ")\n" );
}