class_netinfolist.cpp 4.81 KB
Newer Older
1 2 3
/**
 * @file class_netinfolist.cpp
 */
4

5 6 7 8
#include <fctsys.h>
#include <gr_basic.h>
#include <common.h>
#include <class_drawpanel.h>
9
#include <macros.h>
10
#include <pcbnew.h>
11

12 13 14
#include <class_board.h>
#include <class_module.h>
#include <class_netinfo.h>
15

16 17 18 19 20 21 22 23 24 25

// Constructor and destructor
NETINFO_LIST::NETINFO_LIST( BOARD* aParent )
{
    m_Parent = aParent;
}


NETINFO_LIST::~NETINFO_LIST()
{
26
    clear();
27 28 29
}


30
void NETINFO_LIST::clear()
31
{
32
    for( unsigned ii = 0; ii < GetNetCount(); ii++ )
33 34 35
        delete m_NetBuffer[ii];

    m_NetBuffer.clear();
36
    m_PadsFullList.clear();
37 38 39 40 41 42 43
}


/**
 * Function Append
 * adds \a aNewElement to the end of the list.
 */
44
void NETINFO_LIST::AppendNet( NETINFO_ITEM* aNewElement )
45 46
{
    m_NetBuffer.push_back( aNewElement );
47 48

    // D(Show();)
49 50 51 52
}


/* sort function, to sort pad list by netnames
53 54 55 56
 * this is a case sensitive sort.
 * DO NOT change it because NETINFO_ITEM* BOARD::FindNet( const wxString& aNetname )
 * when search a net by its net name does a binary search
 * and expects to have a nets list sorted by an alphabetic case sensitive sort
57 58
 */

59
static bool padlistSortByNetnames( const D_PAD* a, const D_PAD* b )
60 61 62 63 64 65 66 67
{
    return ( a->GetNetname().Cmp( b->GetNetname() ) ) < 0;
}


/**
 *  Compute and update the net_codes for PADS et and equipots (.m_NetCode member)
 *  net_codes are >= 1 (net_code = 0 means not connected)
68
 *  Update the net buffer
69 70 71
 *  Must be called after editing pads (netname, or deleting) or after read a netlist
 *  set to 1 flag NET_CODE_OK  of m_Pcb->m_Status_Pcb;
 *  m_Pcb->m_NbNodes and m_Pcb->m_NbNets are updated
72 73 74
 * Be aware NETINFO_ITEM* BOARD::FindNet( const wxString& aNetname )
 * when search a net by its net name does a binary search
 * and expects to have a nets list sorted by an alphabetic case sensitive sort
75
 * So do not change Build_Pads_Full_List() which build a sorted list of pads
76
 */
77
void NETINFO_LIST::buildListOfNets()
78
{
79 80 81
    D_PAD*          pad;
    int             nodes_count = 0;
    NETINFO_ITEM*   net_item;
82

83
    clear();        // Remove all nets info and free memory
84

charras's avatar
charras committed
85 86
    // Create and add the "unconnected net", always existing,
    // used to handle pads and tracks that are not member of a "real" net
87
    net_item = new NETINFO_ITEM( m_Parent );
88
    AppendNet( net_item );
89

90 91
    // Build the PAD list, sorted by net
    buildPadsFullList();
92

93
    // Build netnames list, and create a netcode for each netname
94
    D_PAD* last_pad = NULL;
95
    int    netcode = 0;
96

97
    for( unsigned ii = 0; ii < m_PadsFullList.size(); ii++ )
98
    {
99
        pad = m_PadsFullList[ii];
100

101 102 103 104 105 106 107 108 109
        if( pad->GetNetname().IsEmpty() ) // pad not connected
        {
            pad->SetNet( 0 );
            continue;
        }

        /* if the current netname was already found: add pad to the current net_item ,
         *  else create a new net_code and a new net_item
         */
110
        if( last_pad == NULL || ( pad->GetNetname() != last_pad->GetNetname() ) )
111 112
        {
            netcode++;
113
            net_item = new NETINFO_ITEM( m_Parent, pad->GetNetname(), netcode );
114
            AppendNet( net_item );
115 116 117
        }

        pad->SetNet( netcode );
118
        net_item->m_PadInNetList.push_back( pad );
119 120 121

        nodes_count++;

122 123 124
        last_pad = pad;
    }

125
    m_Parent->SetNodeCount( nodes_count );
dickelbeck's avatar
dickelbeck committed
126 127

    m_Parent->SynchronizeNetsAndNetClasses( );
128 129 130 131

    m_Parent->m_Status_Pcb |= NET_CODES_OK;

    m_Parent->SetAreasNetCodesFromNetNames();
132

133 134 135 136 137 138 139
    // D( Show(); )
}

#if defined(DEBUG)
void NETINFO_LIST::Show() const
{
    for( unsigned i=0; i < m_NetBuffer.size();  ++i )
140
    {
141 142 143
        printf( "[%d]: netcode:%d  netname:<%s>\n",
            i, m_NetBuffer[i]->GetNet(),
            TO_UTF8( m_NetBuffer[i]->GetNetname() ) );
144
    }
145
}
146
#endif
147

148
void NETINFO_LIST::buildPadsFullList()
149
{
150 151 152 153 154 155 156 157 158 159 160 161
    /*
     * initialize:
     *   m_Pads (list of pads)
     * set m_Status_Pcb = LISTE_PAD_OK;
     * also clear m_Pcb->m_FullRatsnest that could have bad data
     *   (m_Pcb->m_FullRatsnest uses pointer to pads)
     * Be aware NETINFO_ITEM* BOARD::FindNet( const wxString& aNetname )
     * when search a net by its net name does a binary search
     * and expects to have a nets list sorted by an alphabetic case sensitive sort
     * So do not change the sort function used here
     */

162
    if( m_Parent->m_Status_Pcb & LISTE_PAD_OK )
163 164 165
        return;

    // empty the old list
166 167
    m_PadsFullList.clear();
    m_Parent->m_FullRatsnest.clear();
168

169
    // Clear variables used in ratsnest computation
170
    for( MODULE* module = m_Parent->m_Modules;  module;  module = module->Next() )
171
    {
172
        for( D_PAD* pad = module->Pads();  pad;  pad = pad->Next() )
173
        {
174
            m_PadsFullList.push_back( pad );
175 176 177 178 179 180 181

            pad->SetSubRatsnest( 0 );
            pad->SetParent( module );
        }
    }

    // Sort pad list per net
182
    sort( m_PadsFullList.begin(), m_PadsFullList.end(), padlistSortByNetnames );
183

184
    m_Parent->m_Status_Pcb = LISTE_PAD_OK;
185
}