Commit a0fbe422 authored by jean-pierre charras's avatar jean-pierre charras
Browse files

Eeschema: Rework on netlist generator: code cleaning and enhancement.

Mainly, fix an annoying issue about not named nets:
now, these nets are named from the component references and pin names which are connected.
therefore, unless the net or the footprint references are modified, the net name is not modified between 2 netlist calculations.

Netlist generation code still needs some code enhancement (mainly removing the global g_NetObjectslist variable).
parents f72394cc daeeee56
Loading
Loading
Loading
Loading
+92 −6
Original line number Diff line number Diff line
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2009 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
 * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
 * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2013 KiCad Developers, see AUTHORS.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
@@ -32,8 +32,8 @@
#include <macros.h>
#include <wxEeschemaStruct.h>

#include <general.h>
#include <sch_component.h>
#include <class_netlist_object.h>

#include <wx/regex.h>

@@ -175,7 +175,7 @@ NETLIST_OBJECT::NETLIST_OBJECT()
    m_Flag = 0;                     /* flag used in calculations */
    m_ElectricalType = 0;           /* Has meaning only for Pins and hierarchical pins: electrical
                                     * type */
    m_NetCode    = 0;               /* net code for all items except BUS labels because a BUS
    m_netCode    = 0;               /* net code for all items except BUS labels because a BUS
                                     * label has as many net codes as bus members
                                     */
    m_BusNetCode = 0;               /* Used for BUS connections */
@@ -184,7 +184,7 @@ NETLIST_OBJECT::NETLIST_OBJECT()
                                     */
    m_FlagOfConnection = UNCONNECTED;
    m_PinNum = 0;                   /* pin number ( 1 long = 4 bytes -> 4 ascii codes) */
    m_NetNameCandidate = NULL;      /* a pointer to a NETLIST_OBJECT type label connected to this
    m_netNameCandidate = NULL;      /* a pointer to a NETLIST_OBJECT type label connected to this
                                     * object used to give a name to the net
                                     */
}
@@ -201,6 +201,15 @@ NETLIST_OBJECT::~NETLIST_OBJECT()
{
}

// return true if the object is a label of any type
bool NETLIST_OBJECT::IsLabelType() const
{
    return m_Type == NET_LABEL
        || m_Type == NET_GLOBLABEL || m_Type == NET_HIERLABEL
        || m_Type == NET_BUSLABELMEMBER || m_Type == NET_GLOBBUSLABELMEMBER
        || m_Type == NET_HIERBUSLABELMEMBER
        || m_Type == NET_PINLABEL;
}

bool NETLIST_OBJECT::IsLabelConnected( NETLIST_OBJECT* aNetItem )
{
@@ -299,3 +308,80 @@ void NETLIST_OBJECT::ConvertBusToNetListItems( NETLIST_OBJECT_LIST& aNetListItem
        aNetListItems.push_back( item );
    }
}

/*
 * return the net name of the item
 */
wxString NETLIST_OBJECT::GetNetName() const
{
    if( m_netNameCandidate == NULL )
        return wxEmptyString;

    wxString netName;

    if( m_netNameCandidate->m_Type == NET_PIN )
        return GetShortNetName();

    if( !m_netNameCandidate->IsLabelGlobal() )
    {
        // usual net name, prefix it by the sheet path
        netName = m_netNameCandidate->m_SheetList.PathHumanReadable();
    }

    netName += m_netNameCandidate->m_Label;

    return netName;
}

/**
 * return the short net name of the item i.e. the net name
 * from the "best" label without any prefix.
 * 2 different nets can have the same short name
 */
wxString NETLIST_OBJECT::GetShortNetName() const
{
    if( m_netNameCandidate == NULL )
        return wxEmptyString;

    wxString netName;

    if( m_netNameCandidate->m_Type == NET_PIN )
    {
        if( m_Link )
        {
            netName = wxT("Net-<");
            netName << ( (SCH_COMPONENT*) m_Link )->GetRef( &m_SheetList );
            netName << wxT("-Pad") << LIB_PIN::ReturnPinStringNum( m_PinNum );
            netName << wxT(">");
        }
    }
    else
        netName = m_netNameCandidate->m_Label;

    return netName;
}

/**
 * Set m_netNameCandidate to a connected item which will
 * be used to calcule the net name of the item
 * Obviously the candidate can be only a label
 * when there is no label on the net a pad which will
 * used to build a net name (something like Cmp<REF>_Pad<PAD_NAME>
 * @param aCandidate = the connected item candidate
 */
void NETLIST_OBJECT::SetNetNameCandidate( NETLIST_OBJECT* aCandidate )
{
    switch( aCandidate->m_Type )
    {
        case NET_HIERLABEL:
        case NET_LABEL:
        case NET_PINLABEL:
        case NET_GLOBLABEL:
        case NET_PIN:
            m_netNameCandidate = aCandidate;
            break;

        default:
            break;
    }
}
+220 −38
Original line number Diff line number Diff line
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2009 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
 * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
 * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2013 KiCad Developers, see AUTHORS.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
@@ -33,15 +33,9 @@


#include <sch_sheet_path.h>

#include <lib_pin.h>      // LIB_PIN::ReturnPinStringNum( m_PinNum )


class NETLIST_OBJECT;


// Buffer to build the list of items used in netlist and erc calculations
typedef std::vector <NETLIST_OBJECT*> NETLIST_OBJECT_LIST;
class NETLIST_OBJECT_LIST;


/* Type of Net objects (wires, labels, pins...) */
@@ -89,53 +83,46 @@ enum NET_CONNECTION_T {
};


/**
 * Function IsBusLabel
 * test if \a aLabel has a bus notation.
 *
 * @param aLabel A wxString object containing the label to test.
 * @return true if text is a bus notation format otherwise false is returned.
 */
extern bool IsBusLabel( const wxString& aLabel );


class NETLIST_OBJECT
{
public:
    NETLIST_ITEM_T  m_Type;             /* Type of item (see NETLIST_ITEM_T enum) */
    EDA_ITEM*       m_Comp;             /* Pointer on the library item that
    EDA_ITEM*       m_Comp;             /* Pointer to the library item that
                                         * created this net object (the parent)
                                         */
    SCH_ITEM*       m_Link;             /* For SCH_SHEET_PIN:
                                         * Pointer to the hierarchy sheet that
                                         * contains this SCH_SHEET_PIN
                                         * For Pins: pointer to the component
                                         * For Pins: pointer to the schematic component
                                         * that contains this pin
                                         */
    int            m_Flag;              /* flag used in calculations */
    SCH_SHEET_PATH m_SheetList;
    int            m_ElectricalType;    /* Has meaning only for Pins and
                                         * hierarchical pins: electrical type */
private:
    int            m_NetCode;           /* net code for all items except BUS
                                         * labels because a BUS label has
                                         * as many net codes as bus members
                                         */
public:
    int m_BusNetCode;                   /* Used for BUS connections */
    int m_Member;                       /* for labels type NET_BUSLABELMEMBER ( bus member
                                         * created from the BUS label ) member number.
                                         */
    NET_CONNECTION_T m_FlagOfConnection;
    SCH_SHEET_PATH  m_SheetListInclude; /* sheet that the hierarchical label connects to.*/
    long            m_PinNum;           /* pin number ( 1 long = 4 bytes -> 4 ascii codes) */
    wxString        m_Label;            /* Label text. */
    SCH_SHEET_PATH  m_SheetListInclude; // sheet path which contains the hierarchical label
    long            m_PinNum;           // pin number ( 1 long = 4 bytes -> 4 ascii codes)
    wxString        m_Label;            // Label text (for labels) or Pin name (for pins)
    wxPoint         m_Start;            // Position of object or for segments: starting point
    wxPoint         m_End;              // For segments (wire and buses): ending point
    NETLIST_OBJECT* m_NetNameCandidate; /* a pointer to a label connected to the net,

private:
    int            m_netCode;           /* net code for all items except BUS
                                         * labels because a BUS label has
                                         * as many net codes as bus members
                                         */
    NETLIST_OBJECT* m_netNameCandidate; /* a pointer to a label connected to the net,
                                         * that can be used to give a name to the net
                                         * NULL if no usable label
                                         * or a pin if there is no label in net
                                         * When no label, the pin is used to build
                                         * default net name.
                                         */
public:

#if defined(DEBUG)
    void Show( std::ostream& out, int ndx ) const;      // override
@@ -146,8 +133,19 @@ public:

    ~NETLIST_OBJECT();

    void SetNet( int aNetCode ) { m_NetCode = aNetCode; }
    int GetNet() const { return m_NetCode; }
    // Accessors:
    void SetNet( int aNetCode ) { m_netCode = aNetCode; }
    int GetNet() const { return m_netCode; }

    /**
     * Set m_netNameCandidate to a connected item which will
     * be used to calcule the net name of the item
     * Obviously the candidate can be only a label
     * when there is no label on the net a pad which will
     * used to build a net name (something like Cmp<REF>_Pad<PAD_NAME>
     * @param aCandidate = the connected item candidate
     */
    void SetNetNameCandidate( NETLIST_OBJECT* aCandidate );

    /**
     * Function GetPinNum
@@ -171,6 +169,38 @@ public:
     */
    bool IsLabelConnected( NETLIST_OBJECT* aNetItem );

    /**
     * Function IsLabelGlobal
     * @return true if the object is a global label
     * (i.e. an real global label or a pin label coming
     * from a power pin invisible
     */
    bool IsLabelGlobal() const
    {
        return  ( m_Type == NET_PINLABEL ) || ( m_Type == NET_GLOBLABEL );
    }

    /**
     * Function IsLabelType
     * @return true if the object is a label of any type
     */
    bool IsLabelType() const;

    /**
     * Function GetNetName
     * @return the full net name of the item, i.e. the net name
     * from the "best" label, prefixed by the sheet path
     */
    wxString GetNetName() const;

    /**
     * Function GetShortNetName
     * @return the short net name of the item i.e. the net name
     * from the "best" label without any prefix.
     * 2 different nets can have the same short name
     */
    wxString GetShortNetName() const;

    /**
     * Function ConvertBusToNetListItems
     * breaks the text of a bus label type net list object into as many members as
@@ -184,4 +214,156 @@ public:
};



/**
 * NETLIST_OBJECT_LIST is a class to handle the list of connected items
 * in a full shematic hierarchy for netlist and erc calculations
 */
class NETLIST_OBJECT_LIST: public std::vector <NETLIST_OBJECT*>
{
    bool m_isOwner;     // = true if the objects in list are owned my me, and therefore
                        // the memory should be freed by the destructor and the list cleared
public:
    /**
     * Constructor.
     * NETLIST_OBJECT_LIST handle a list of connected items.
     * the instance can be owner of items or not.
     * If it is the owner, the items are freeed by the destructor
     * @param aIsOwner true if the instance is the owner of item list
     * (default = false)
     */
    NETLIST_OBJECT_LIST( bool aIsOwner = false ) { m_isOwner = aIsOwner; }
    ~NETLIST_OBJECT_LIST();

    void SetOwner( bool aIsOwner ) { m_isOwner = aIsOwner; }

    /**
     * Function BuildNetListInfo
     * the master function of tgis class.
     * Build the list of connected objects (pins, labels ...) and
     * all info to generate netlists or run ERC diags
     * @param aSheets = the flattened sheet list
     * @return true if OK, false is not item found
     */
    bool BuildNetListInfo( SCH_SHEET_LIST& aSheets );

    /*
     * Acces to an item in list
     */
    NETLIST_OBJECT* GetItem( unsigned aIdx )
    {
        return  *( this->begin() + aIdx );
    }

    /*
     * Delete all objects in list and clear list
     * (free memory used to store info about NETLIST_OBJECT items)
     */
    void ClearList();

    /*
     * Sorts the list of connected items by net code
     */
    void SortListbyNetcode();

    /*
     * Sorts the list of connected items by sheet.
     * This sorting is used when searching "physical" connection between items
     * because obviously only items inside the same sheet can be connected
     */
    void SortListbySheet();

    /*
     * Propagate net codes from a parent sheet to an include sheet,
     * from a pin sheet connection
     */
    void SheetLabelConnect( NETLIST_OBJECT* aSheetLabel );

    void PointToPointConnect( NETLIST_OBJECT* aRef, bool aIsBus, int start );

    /*
     * Search connections betweena junction and segments
     * Propagate the junction net code to objects connected by this junction.
     * The junction must have a valid net code
     * The list of objects is expected sorted by sheets.
     * Search is done from index aIdxStart to the last element of list
     */
    void SegmentToPointConnect( NETLIST_OBJECT* aJonction, bool aIsBus, int aIdxStart );

    void ConnectBusLabels();

    /*
     * Set the m_FlagOfConnection member of items in list
     * depending on the connection type:
     * UNCONNECTED, PAD_CONNECT or NOCONNECT_SYMBOL_PRESENT
     * The list is expected sorted by order of net code,
     * i.e. items having the same net code are grouped
     */
    void SetUnconnectedFlag();

    /**
     * Function FindBestNetNameForEachNet
     * fill the .m_NetNameCandidate member of each item of aNetItemBuffer
     * with a reference to the "best" NETLIST_OBJECT usable to give a name to the net
     * If no suitable object found, .m_NetNameCandidate is filled with 0.
     * The "best" NETLIST_OBJECT is a NETLIST_OBJECT that have the type label
     * and by priority order:
     * the label is global or local
     * the label is in the first sheet in a hierarchy (the root sheet has the most priority)
     * alphabetic order.
     */
    void FindBestNetNameForEachNet();

    #if defined(DEBUG)
    void DumpNetTable()
    {
        for( unsigned idx = 0; idx < size(); ++idx )
        {
            GetItem( idx )->Show( std::cout, idx );
        }
    }
    #endif
private:
    /*
     * Propagate aNewNetCode to items having an internal netcode aOldNetCode
     * used to interconnect group of items already physically connected,
     * when a new connection is found between aOldNetCode and aNewNetCode
     */
    void propageNetCode( int aOldNetCode, int aNewNetCode, bool aIsBus );

    /*
     * This function merges the net codes of groups of objects already connected
     * to labels (wires, bus, pins ... ) when 2 labels are equivalents
     * (i.e. group objects connected by labels)
     */
    void labelConnect( NETLIST_OBJECT* aLabelRef );

    /* Comparison function to sort by increasing Netcode the list of connected items
     */
    static bool sortItemsbyNetcode( const NETLIST_OBJECT* Objet1, const NETLIST_OBJECT* Objet2 )
    {
        return Objet1->GetNet() < Objet2->GetNet();
    }


    /* Comparison routine to sort items by Sheet Number
     */
    static bool sortItemsBySheet( const NETLIST_OBJECT* Objet1, const NETLIST_OBJECT* Objet2 )
    {
        return Objet1->m_SheetList.Cmp( Objet2->m_SheetList ) < 0;
    }
};


extern NETLIST_OBJECT_LIST g_NetObjectslist;

/**
 * Function IsBusLabel
 * test if \a aLabel has a bus notation.
 *
 * @param aLabel A wxString object containing the label to test.
 * @return true if text is a bus notation format otherwise false is returned.
 */
extern bool IsBusLabel( const wxString& aLabel );

#endif  // _CLASS_NETLIST_OBJECT_H_
+1 −1
Original line number Diff line number Diff line
@@ -37,8 +37,8 @@
#include <wxEeschemaStruct.h>
#include <invoke_sch_dialog.h>

#include <general.h>
#include <netlist.h>
#include <class_netlist_object.h>
#include <sch_marker.h>
#include <sch_sheet.h>
#include <lib_pin.h>
+1 −1
Original line number Diff line number Diff line
@@ -33,8 +33,8 @@
#include <kicad_string.h>
#include <wxEeschemaStruct.h>

#include <general.h>
#include <netlist.h>
#include <class_netlist_object.h>
#include <lib_pin.h>
#include <protos.h>
#include <erc.h>
+1 −2
Original line number Diff line number Diff line
@@ -35,12 +35,11 @@
#include <wxEeschemaStruct.h>
#include <appl_wxstruct.h>

#include <general.h>
#include <protos.h>
#include <eeschema_id.h>
#include <class_library.h>
#include <libeditframe.h>
#include <sch_sheet.h>
#include <sch_sheet_path.h>
#include <sch_component.h>
#include <wildcards_and_files_ext.h>

Loading