pns_logger.cpp 5.01 KB
Newer Older
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
/*
 * KiRouter - a push-and-(sometimes-)shove PCB router
 *
 * Copyright (C) 2013-2014 CERN
 * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
 *
 * 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.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include "pns_logger.h"
#include "pns_item.h"
#include "pns_via.h"
#include "pns_line.h"
#include "pns_segment.h"
#include "pns_solid.h"

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

PNS_LOGGER::PNS_LOGGER( )
{
Maciej Suminski's avatar
Maciej Suminski committed
35
    m_groupOpened = false;
36 37 38 39 40 41 42
}


PNS_LOGGER::~PNS_LOGGER()
{
}

43

44 45
void PNS_LOGGER::Clear()
{
Maciej Suminski's avatar
Maciej Suminski committed
46 47
    m_theLog.str( std::string() );
    m_groupOpened = false;
48 49
}

50 51

void PNS_LOGGER::NewGroup( const std::string& aName, int aIter )
52
{
Maciej Suminski's avatar
Maciej Suminski committed
53 54
    if( m_groupOpened )
        m_theLog << "endgroup" << std::endl;
55

Maciej Suminski's avatar
Maciej Suminski committed
56 57
    m_theLog << "group " << aName << " " << aIter << std::endl;
    m_groupOpened = true;
58 59
}

60

61 62
void PNS_LOGGER::EndGroup()
{
Maciej Suminski's avatar
Maciej Suminski committed
63 64
    if( !m_groupOpened )
        return;
65

Maciej Suminski's avatar
Maciej Suminski committed
66 67
    m_groupOpened = false;
    m_theLog << "endgroup" << std::endl;
68 69
}

70 71

void PNS_LOGGER::Log ( const PNS_ITEM* aItem, int aKind, const std::string aName )
72
{
73
    m_theLog << "item " << aKind << " " << aName << " ";
Maciej Suminski's avatar
Maciej Suminski committed
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
    m_theLog << aItem->Net() << " " << aItem->Layers().Start() << " " <<
                aItem->Layers().End() << " " << aItem->Marker() << " " << aItem->Rank();

    switch( aItem->Kind() )
    {
        case PNS_ITEM::LINE:
        {
            PNS_LINE* l = (PNS_LINE*) aItem;
            m_theLog << " line ";
            m_theLog << l->Width() << " " << ( l->EndsWithVia() ? 1 : 0 ) << " ";
            dumpShape ( l->Shape() );
            m_theLog << std::endl;
            break;
        }

        case PNS_ITEM::VIA:
        {
            m_theLog << " via 0 0 ";
            dumpShape ( aItem->Shape() );
            m_theLog << std::endl;
            break;
        }

        case PNS_ITEM::SEGMENT:
        {
            PNS_SEGMENT* s =(PNS_SEGMENT*) aItem;
            m_theLog << " line ";
            m_theLog << s->Width() << " 0 linechain 2 0 " << s->Seg().A.x << " " <<
                        s->Seg().A.y << " " << s->Seg().B.x << " " <<s->Seg().B.y << std::endl;
            break;
        }

        case PNS_ITEM::SOLID:
        {
            PNS_SOLID* s = (PNS_SOLID*) aItem;
            m_theLog << " solid 0 0 ";
            dumpShape( s->Shape() );
            m_theLog << std::endl;
            break;
        }

        default:
            break;
    }
118 119
}

120 121

void PNS_LOGGER::Log( const SHAPE_LINE_CHAIN *aL, int aKind, const std::string aName )
122
{
Maciej Suminski's avatar
Maciej Suminski committed
123 124 125 126 127 128
    m_theLog << "item " << aKind << " " << aName << " ";
    m_theLog << 0 << " " << 0 << " " << 0 << " " << 0 << " " << 0;
    m_theLog << " line ";
    m_theLog << 0 << " " << 0 << " ";
    dumpShape( aL );
    m_theLog << std::endl;
129 130 131
}


132 133 134
void PNS_LOGGER::Log( const VECTOR2I& aStart, const VECTOR2I& aEnd,
                      int aKind, const std::string aName)
{
135 136
}

137 138

void PNS_LOGGER::dumpShape( const SHAPE* aSh )
139
{
Maciej Suminski's avatar
Maciej Suminski committed
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
    switch( aSh->Type() )
    {
        case SH_LINE_CHAIN:
        {
            const SHAPE_LINE_CHAIN* lc = (const SHAPE_LINE_CHAIN*) aSh;
            m_theLog << "linechain " << lc->PointCount() << " " << ( lc->IsClosed() ? 1 : 0 ) << " ";

            for( int i = 0; i < lc->PointCount(); i++ )
                m_theLog << lc->CPoint( i ).x << " " << lc->CPoint( i ).y << " ";

            break;
        }

        case SH_CIRCLE:
        {
            const SHAPE_CIRCLE *c = (const SHAPE_CIRCLE*) aSh;
            m_theLog << "circle " << c->GetCenter().x << " " << c->GetCenter().y << " " << c->GetRadius();
            break;
        }

        case SH_RECT:
        {
            const SHAPE_RECT* r = (const SHAPE_RECT*) aSh;
            m_theLog << "rect " << r->GetPosition().x << " " << r->GetPosition().y << " " <<
                        r->GetSize().x << " " <<r->GetSize().y;
            break;
        }

        case SH_SEGMENT:
        {
            const SHAPE_SEGMENT* s = (const SHAPE_SEGMENT*) aSh;
            m_theLog << "linechain 2 0 " << s->GetSeg().A.x << " " << s->GetSeg().A.y << " " <<
                        s->GetSeg().B.x << " " << s->GetSeg().B.y;
            break;
        }

        default:
            break;
    }
179 180
}

Maciej Suminski's avatar
Maciej Suminski committed
181

182
void PNS_LOGGER::Save( const std::string& aFilename )
183
{
Maciej Suminski's avatar
Maciej Suminski committed
184
    EndGroup();
185

Maciej Suminski's avatar
Maciej Suminski committed
186 187 188 189 190
    FILE* f = fopen( aFilename.c_str(), "wb" );
    printf( "Saving to '%s' [%p]\n", aFilename.c_str(), f );
    const std::string s = m_theLog.str();
    fwrite( s.c_str(), 1, s.length(), f );
    fclose( f );
191
}