class_netinfolist.cpp 4.87 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

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 47 48 49 50
{
    m_NetBuffer.push_back( aNewElement );
}


/* sort function, to sort pad list by netnames
51 52 53 54
 * 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
55 56
 */

57
static bool padlistSortByNetnames( const D_PAD* a, const D_PAD* b )
58 59 60 61 62 63 64 65
{
    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)
66
 *  Update the net buffer
67 68 69
 *  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
70 71 72 73
 * 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 Build_Pads_Full_List() taht build a sorted list of pads
74
 */
75
void NETINFO_LIST::buildListOfNets()
76
{
77 78 79
    D_PAD*          pad;
    int             nodes_count = 0;
    NETINFO_ITEM*   net_item;
80

81
    clear();        // Remove all nets info and free memory
82

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

88 89
    // Build the PAD list, sorted by net
    buildPadsFullList();
90

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

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

99 100 101 102 103 104 105 106 107
        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
         */
108
        if( last_pad == NULL || ( pad->GetNetname() != last_pad->GetNetname() ) )
109 110
        {
            netcode++;
111
            net_item = new NETINFO_ITEM( (BOARD_ITEM*)m_Parent );
112 113
            net_item->SetNet( netcode );
            net_item->SetNetname( pad->GetNetname() );
114
            AppendNet( net_item );
115 116 117
        }

        pad->SetNet( netcode );
118
        net_item->m_PadInNetList.push_back( pad );
119 120 121 122 123
        nodes_count ++;
        last_pad = pad;
    }

    m_Parent->m_NbNodes = nodes_count;
dickelbeck's avatar
dickelbeck committed
124 125

    m_Parent->SynchronizeNetsAndNetClasses( );
126 127 128 129

    m_Parent->m_Status_Pcb |= NET_CODES_OK;

    m_Parent->SetAreasNetCodesFromNetNames();
130 131 132 133 134

// For test and debug purposes only
#if 0
    for( unsigned icnt = 0; icnt < GetCount(); icnt++)
    {
135 136 137
        wxLogWarning( wxT( "icnt %d, netcode %d, netname <%s>\n" ),
                      icnt, m_NetBuffer[icnt]->GetNet(),
                      GetChars( m_NetBuffer[icnt]->GetNetname() ) );
138 139
    }
#endif
140 141 142
}


143
void NETINFO_LIST::buildPadsFullList()
144
{
145 146 147 148 149 150 151 152 153 154 155 156
    /*
     * 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
     */

157
    if( m_Parent->m_Status_Pcb & LISTE_PAD_OK )
158 159 160
        return;

    // empty the old list
161 162
    m_PadsFullList.clear();
    m_Parent->m_FullRatsnest.clear();
163

164
    // Clear variables used in ratsnest computation
165
    for( MODULE* module = m_Parent->m_Modules;  module;  module = module->Next() )
166 167 168
    {
        for( D_PAD* pad = module->m_Pads;  pad;  pad = pad->Next() )
        {
169
            m_PadsFullList.push_back( pad );
170 171 172 173 174 175 176

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

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

179
    m_Parent->m_Status_Pcb = LISTE_PAD_OK;
180
}