kicad/eeschema/netform.cpp

2022 lines
67 KiB
C++
Raw Normal View History

2010-07-31 23:57:36 +00:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
2010-07-31 23:57:36 +00:00
*
* Copyright (C) 1992-2012 jp.charras at wanadoo.fr
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 1992-2012 KiCad Developers, see change_log.txt for contributors.
2010-07-31 23:57:36 +00:00
*
* 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
*/
/**
* @file eeschema/netform.cpp
* @brief Net list generation code.
*/
2010-07-31 23:57:36 +00:00
#include <fctsys.h>
#include <confirm.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <appl_wxstruct.h>
#include <wxEeschemaStruct.h>
#include <general.h>
#include <netlist.h>
#include <dialog_netlist.h>
#include <class_library.h>
#include <lib_pin.h>
#include <sch_component.h>
#include <sch_text.h>
#include <sch_sheet.h>
#include <wx/tokenzr.h>
#include <xnode.h> // also nests: <wx/xml/xml.h>
2010-08-07 15:25:18 +00:00
#include <build_version.h>
#define INTERMEDIATE_NETLIST_EXT wxT("xml")
#include <set>
/**
* Class UNIQUE_STRINGS
* tracks unique wxStrings and is useful in telling if a string
* has been seen before.
*/
class UNIQUE_STRINGS
{
std::set<wxString> m_set; ///< set of wxStrings already found
2010-08-04 19:14:51 +00:00
typedef std::set<wxString>::iterator us_iterator;
public:
/**
* Function Clear
* erases the record.
*/
void Clear() { m_set.clear(); }
/**
* Function Lookup
* returns true if \a aString already exists in the set, otherwise returns
* false and adds \a aString to the set for next time.
*/
bool Lookup( const wxString& aString );
};
bool UNIQUE_STRINGS::Lookup( const wxString& aString )
{
2010-08-04 19:14:51 +00:00
std::pair<us_iterator, bool> pair = m_set.insert( aString );
2010-08-04 19:14:51 +00:00
return !pair.second;
}
/**
* Class NETLIST_EXPORT_TOOL
* is a private implementation class used in this source file to keep track
* of and recycle datastructures used in the generation of various exported netlist
* files. Since it is private it is not in a header file.
*/
class NETLIST_EXPORT_TOOL
{
/// Used to temporary store and filter the list of pins of a schematic component
/// when generating schematic component data in netlist (comp section)
NETLIST_OBJECT_LIST m_SortedComponentPinList;
/// Used for "multi parts per package" components,
/// avoids processing a lib component more than once.
UNIQUE_STRINGS m_ReferencesAlreadyFound;
// share a code generated std::set<void*> to reduce code volume
2010-08-04 19:14:51 +00:00
std::set<void*> m_LibParts; ///< unique library parts used
std::set<void*> m_Libraries; ///< unique libraries used
2010-08-04 19:14:51 +00:00
/**
* Function sprintPinNetName
* formats the net name for \a aPin using \a aNetNameFormat into \a aResult.
* <p>
* Net name is:
* <ul>
* <li> "?" if pin not connected
* <li> "netname" for global net (like gnd, vcc ..
* <li> "/path/netname" for the usual nets
* </ul>
*/
static void sprintPinNetName( wxString* aResult, const wxString& aNetNameFormat, NETLIST_OBJECT* aPin );
/**
* Function findNextComponentAndCreatePinList
* finds a component from the DrawList and builds
* its pin list in m_SortedComponentPinList. This list is sorted by pin num.
* the component is the next actual component after aItem
* (power symbols and virtual components that have their reference starting by '#'are skipped).
*/
SCH_COMPONENT* findNextComponentAndCreatePinList( EDA_ITEM* aItem, SCH_SHEET_PATH* aSheetPath );
2010-08-04 19:14:51 +00:00
SCH_COMPONENT* findNextComponent( EDA_ITEM* aItem, SCH_SHEET_PATH* aSheetPath );
2010-08-04 19:14:51 +00:00
/**
* Function eraseDuplicatePins
* erase duplicate Pins from m_SortedComponentPinList (i.e. set pointer in this list to NULL).
2010-08-04 19:14:51 +00:00
* (This is a list of pins found in the whole schematic, for a single
* component.) These duplicate pins were put in list because some pins (powers... )
* are found more than one time when we have a multiple parts per package
* component. For instance, a 74ls00 has 4 parts, and therefore the VCC pin
* and GND pin appears 4 times in the list.
* Note: this list *MUST* be sorted by pin number (.m_PinNum member value)
* Also set the m_Flag member of "removed" NETLIST_OBJECT pin item to 1
2010-08-04 19:14:51 +00:00
*/
void eraseDuplicatePins( );
2010-08-04 19:14:51 +00:00
/**
* Function addPinToComponentPinList
2010-08-04 19:14:51 +00:00
* adds a new pin description to the pin list m_SortedComponentPinList.
* A pin description is a pointer to the corresponding structure
2010-08-04 19:14:51 +00:00
* created by BuildNetList() in the table g_NetObjectslist.
*/
bool addPinToComponentPinList( SCH_COMPONENT* Component,
SCH_SHEET_PATH* sheet,
LIB_PIN* PinEntry );
2010-08-04 19:14:51 +00:00
/**
* Function findAllInstancesOfComponent
* is used for "multiple parts per package" components.
* <p>
* Search the entire design for all instances of \a aComponent based on
* matching reference designator, and for each part, add all its pins
* to the temporary sorted pin list.
*/
void findAllInstancesOfComponent( SCH_COMPONENT* aComponent,
LIB_COMPONENT* aEntry,
SCH_SHEET_PATH* aSheetPath );
2010-08-04 19:14:51 +00:00
/**
* Function writeGENERICListOfNets
* writes out nets (ranked by Netcode), and elements that are
* connected as part of that net.
*/
bool writeGENERICListOfNets( FILE* f, NETLIST_OBJECT_LIST& aObjectsList );
/**
* Function writeListOfNetsCADSTAR
* writes a net list (ranked by Netcode), and pins connected to it.
* <p>
2010-08-04 19:14:51 +00:00
* Format:
* - ADD_TER RR2 6 \"$42\"
* - B U1 100
* - 6 CA
* </p>
2010-08-04 19:14:51 +00:00
*/
bool writeListOfNetsCADSTAR( FILE* f, NETLIST_OBJECT_LIST& aObjectsList );
2010-08-04 19:14:51 +00:00
2010-08-07 15:25:18 +00:00
/**
* Function makeGenericRoot
* builds the entire document tree for the generic export. This is factored
* out here so we can write the tree in either S-expression file format
* or in XML if we put the tree built here into a wxXmlDocument.
*/
2010-08-08 00:31:07 +00:00
XNODE* makeGenericRoot();
2010-08-07 15:25:18 +00:00
/**
* Function makeGenericComponents
* returns a sub-tree holding all the schematic components.
*/
2010-08-08 00:31:07 +00:00
XNODE* makeGenericComponents();
2010-08-07 15:25:18 +00:00
2010-08-04 19:14:51 +00:00
/**
* Function makeGenericDesignHeader
* fills out a project "design" header into an XML node.
2010-08-08 00:31:07 +00:00
* @return XNODE* - the design header
2010-08-04 19:14:51 +00:00
*/
2010-08-08 00:31:07 +00:00
XNODE* makeGenericDesignHeader();
2010-08-04 19:14:51 +00:00
/**
* Function makeGenericLibParts
* fills out an XML node with the unique library parts and returns it.
*/
2010-08-08 00:31:07 +00:00
XNODE* makeGenericLibParts();
2010-08-04 19:14:51 +00:00
/**
* Function makeGenericListOfNets
* fills out an XML node with a list of nets and returns it.
*/
2010-08-08 00:31:07 +00:00
XNODE* makeGenericListOfNets();
2010-08-04 19:14:51 +00:00
/**
* Function makeGenericLibraries
* fills out an XML node with a list of used libraries and returns it.
* Must have called makeGenericLibParts() before this function.
*/
2010-08-08 00:31:07 +00:00
XNODE* makeGenericLibraries();
2010-08-04 19:14:51 +00:00
public:
2010-08-04 19:14:51 +00:00
/**
* Function WriteKiCadNetList
* creates a netlist, using the S expressions.
* the netlist creates the same data as the generic XML netlist,
* but using SWEET (or S expression), more easy to read and faster to parse
* @param aOutFileName = the full filename of the file to create
* @return bool - true if there were no errors, else false.
*/
bool WriteKiCadNetList( const wxString& aOutFileName );
2010-08-04 19:14:51 +00:00
/**
2010-08-07 15:25:18 +00:00
* Function WriteGENERICNetList
2010-08-04 19:14:51 +00:00
* creates a generic netlist, now in XML.
2010-12-29 17:47:32 +00:00
* @param aOutFileName = the full filename of the file to create
2010-08-04 19:14:51 +00:00
* @return bool - true if there were no errors, else false.
*/
bool WriteGENERICNetList( const wxString& aOutFileName );
/**
* Function WriteNetListPCBNEW
* generates a net list file (Format 2 improves ORCAD PCB)
2010-08-04 19:14:51 +00:00
*
2010-12-29 17:47:32 +00:00
* @param f = the file to write to
* @param with_pcbnew if true, then use Pcbnew format (OrcadPcb2 + a list of net),<p>
* else use ORCADPCB2 basic format.
*/
bool WriteNetListPCBNEW( FILE* f, bool with_pcbnew );
2010-08-04 19:14:51 +00:00
/**
* Function WriteNetListCADSTAR
* generates a netlist file in CADSTAR Format.
* Header:
* HEA ..
* TIM .. 2004 07 29 16 22 17
* APA .. "Cadstar RINF Output - Version 6.0.2.3"
* INCH UNI .. 1000.0 in
* FULL TYP ..
*
* List of components:
* .. ADD_COM X1 "CNT D41612 (48pts CONTOUR TM)"
* .. ADD_COM U2 "74HCT245D" "74HCT245D"
*
* Connections:
* .. ADD_TER RR2 * 6 "$ 42"
* .. B U1 100
* 6 CA
*
* ADD_TER .. U2 * 6 "$ 59"
* .. B * U7 39
* U6 17
* U1 * 122
*
* .. ADD_TER P2 * 1 "$ 9"
* .. B * T3 1
*U1 * 14
*/
bool WriteNetListCADSTAR( FILE* f );
2010-08-04 19:14:51 +00:00
/**
* Function WriteNetListPspice
* generates a netlist file in PSPICE format.
* <p>
* All graphics text starting by [.-+] PSpice or [.-+] gnucap
* are seen as spice directives and put in netlist
* .-PSpice or .-gnucap put at beginning of the netlist
* .+PSpice or .-genucap are put at end of the netList
2010-12-29 17:47:32 +00:00
* @param f = the file to write to
* @param use_netnames = true, to use netnames in netlist,
* false to use net number.
* @param aUsePrefix = true, adds an 'X' prefix to any reference designator starting with "U" or "IC",
* false to leave reference designator unchanged.
2010-08-04 19:14:51 +00:00
*/
bool WriteNetListPspice( FILE* f, bool use_netnames, bool aUsePrefix );
/**
* Function MakeCommandLine
* builds up a string that describes a command line for
* executing a child process. The input and output file names
* along with any options to the executable are all possibly
* in the returned string.
*
* @param aFormatString holds:
* <ul>
* <li>the name of the external program
* <li>any options needed by that program
* <li>formatting sequences, see below.
* </ul>
*
* @param aTempfile is the name of an input file to the
* external program.
* @param aFinalFile is the name of an output file that
* the user expects.
*
* <p> Supported formatting sequences and their meaning:
* <ul>
* <li> %B => base filename of selected output file, minus
* path and extension.
* <li> %I => complete filename and path of the temporary
* input file.
* <li> %O => complete filename and path of the user chosen
* output file.
* </ul>
*/
static wxString MakeCommandLine( const wxString& aFormatString,
const wxString& aTempfile, const wxString& aFinalFile );
};
wxString NETLIST_EXPORT_TOOL::MakeCommandLine( const wxString& aFormatString,
const wxString& aTempfile, const wxString& aFinalFile )
{
wxString ret = aFormatString;
wxFileName in = aTempfile;
wxFileName out = aFinalFile;
ret.Replace( wxT("%B"), out.GetName().GetData(), true );
ret.Replace( wxT("%I"), in.GetFullPath().GetData(), true );
ret.Replace( wxT("%O"), out.GetFullPath().GetData(), true );
return ret;
}
/* Function WriteNetListFile
* creates the netlist file. Netlist info must be existing
* param aFormat = netlist format (NET_TYPE_PCBNEW ...)
* param aFullFileName = full netlist file name
* param aNetlistOptions = netlist options using OR'ed bits.
* For SPICE netlist only:
* if NET_USE_NETNAMES is set, use net names from labels in schematic
* else use net numbers (net codes)
* if NET_USE_X_PREFIX is set : change "U" and "IC" refernce prefix to "X"
* return true if success.
2007-09-21 13:23:51 +00:00
*/
bool SCH_EDIT_FRAME::WriteNetListFile( int aFormat, const wxString& aFullFileName,
unsigned aNetlistOptions )
{
2010-08-04 19:14:51 +00:00
bool ret = true;
FILE* f = NULL;
NETLIST_EXPORT_TOOL helper;
bool open_file = aFormat < NET_TYPE_CUSTOM1;
if( (aFormat == NET_TYPE_PCBNEW) && (aNetlistOptions & NET_PCBNEW_USE_NEW_FORMAT ) )
open_file = false;
2007-09-21 13:23:51 +00:00
if( open_file )
2007-09-21 13:23:51 +00:00
{
if( ( f = wxFopen( aFullFileName, wxT( "wt" ) ) ) == NULL )
2007-09-21 13:23:51 +00:00
{
wxString msg;
msg.Printf( _( "Failed to create file <%s>" ),
GetChars( aFullFileName ) );
DisplayError( this, msg );
return false;
2007-09-21 13:23:51 +00:00
}
}
wxBusyCursor Busy;
switch( aFormat )
2007-09-21 13:23:51 +00:00
{
case NET_TYPE_PCBNEW:
if( (aNetlistOptions & NET_PCBNEW_USE_NEW_FORMAT ) )
ret = helper.WriteKiCadNetList( aFullFileName );
else
{
ret = helper.WriteNetListPCBNEW( f, true );
fclose( f );
}
2007-09-21 13:23:51 +00:00
break;
case NET_TYPE_ORCADPCB2:
ret = helper.WriteNetListPCBNEW( f, false );
2007-09-21 13:23:51 +00:00
fclose( f );
break;
case NET_TYPE_CADSTAR:
ret = helper.WriteNetListCADSTAR( f );
2007-09-21 13:23:51 +00:00
fclose( f );
break;
case NET_TYPE_SPICE:
ret = helper.WriteNetListPspice( f,
aNetlistOptions & NET_USE_NETNAMES,
aNetlistOptions & NET_USE_X_PREFIX );
2007-09-21 13:23:51 +00:00
fclose( f );
break;
default:
{
wxFileName tmpFile = aFullFileName;
tmpFile.SetExt( INTERMEDIATE_NETLIST_EXT );
D(printf("tmpFile:'%s'\n", TO_UTF8( tmpFile.GetFullPath() ) );)
ret = helper.WriteGENERICNetList( tmpFile.GetFullPath() );
if( !ret )
break;
// If user provided no plugin command line, return now.
if( m_netListerCommand.IsEmpty() )
break;
// build full command line from user's format string, e.g.:
// "xsltproc -o %O /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl %I"
// becomes, after the user selects /tmp/s1.net as the output file from the file dialog:
// "xsltproc -o /tmp/s1.net /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl /tmp/s1.xml"
wxString commandLine = NETLIST_EXPORT_TOOL::MakeCommandLine( m_netListerCommand,
tmpFile.GetFullPath(),
aFullFileName );
D(printf("commandLine:'%s'\n", TO_UTF8( commandLine ) );)
ProcessExecute( commandLine, wxEXEC_SYNC );
// ::wxRemoveFile( tmpFile.GetFullPath() );
}
2007-09-21 13:23:51 +00:00
break;
}
return ret;
}
2010-08-04 19:14:51 +00:00
/// Comparison routine for sorting by pin numbers.
static bool sortPinsByNum( NETLIST_OBJECT* aPin1, NETLIST_OBJECT* aPin2 )
{
// return "lhs < rhs"
2010-08-04 19:14:51 +00:00
return RefDesStringCompare( aPin1->GetPinNumText(), aPin2->GetPinNumText() ) < 0;
}
2010-08-08 00:31:07 +00:00
static bool sortPinsByNumber( LIB_PIN* aPin1, LIB_PIN* aPin2 )
{
// return "lhs < rhs"
return RefDesStringCompare( aPin1->GetNumberString(), aPin2->GetNumberString() ) < 0;
2010-08-08 00:31:07 +00:00
}
void NETLIST_EXPORT_TOOL::sprintPinNetName( wxString* aResult,
const wxString& aNetNameFormat, NETLIST_OBJECT* aPin )
{
int netcode = aPin->GetNet();
// Not wxString::Clear(), which would free memory. We want the worst
// case wxString memory to grow to avoid reallocation from within the
// caller's loop.
aResult->Empty();
if( netcode != 0 && aPin->m_FlagOfConnection == PAD_CONNECT )
{
NETLIST_OBJECT* netref = aPin->m_NetNameCandidate;
if( netref )
*aResult = netref->m_Label;
if( !aResult->IsEmpty() )
{
// prefix non global label names with the sheet path, to avoid name collisions
if( netref->m_Type != NET_PINLABEL && netref->m_Type != NET_GLOBLABEL )
{
wxString lnet = *aResult;
*aResult = netref->m_SheetList.PathHumanReadable();
// If sheet path is too long, use the time stamp name instead
if( aResult->Length() > 32 )
*aResult = netref->m_SheetList.Path();
*aResult += lnet;
}
}
else
{
aResult->Printf( aNetNameFormat.GetData(), netcode );
}
}
}
SCH_COMPONENT* NETLIST_EXPORT_TOOL::findNextComponent( EDA_ITEM* aItem, SCH_SHEET_PATH* aSheetPath )
2010-08-04 19:14:51 +00:00
{
wxString ref;
// continue searching from the middle of a linked list (the draw list)
for( ; aItem; aItem = aItem->Next() )
{
if( aItem->Type() != SCH_COMPONENT_T )
2010-08-04 19:14:51 +00:00
continue;
// found next component
SCH_COMPONENT* comp = (SCH_COMPONENT*) aItem;
// Power symbols and other components which have the reference starting
// with "#" are not included in netlist (pseudo or virtual components)
ref = comp->GetRef( aSheetPath );
if( ref[0] == wxChar( '#' ) )
continue;
// if( Component->m_FlagControlMulti == 1 )
// continue; /* yes */
// removed because with multiple instances of one schematic
// (several sheets pointing to 1 screen), this will be erroneously be
// toggled.
LIB_COMPONENT* entry = CMP_LIBRARY::FindLibraryComponent( comp->GetLibName() );
2010-08-04 19:14:51 +00:00
if( !entry )
continue;
// If component is a "multi parts per package" type
if( entry->GetPartCount() > 1 )
{
// test if this reference has already been processed, and if so skip
if( m_ReferencesAlreadyFound.Lookup( ref ) )
continue;
}
// record the usage of this library component entry.
m_LibParts.insert( entry ); // rejects non-unique pointers
return comp;
}
return NULL;
}
SCH_COMPONENT* NETLIST_EXPORT_TOOL::findNextComponentAndCreatePinList( EDA_ITEM* aItem,
SCH_SHEET_PATH* aSheetPath )
{
wxString ref;
m_SortedComponentPinList.clear();
// continue searching from the middle of a linked list (the draw list)
for( ; aItem; aItem = aItem->Next() )
2007-09-21 13:23:51 +00:00
{
if( aItem->Type() != SCH_COMPONENT_T )
2007-09-21 13:23:51 +00:00
continue;
// found next component
SCH_COMPONENT* comp = (SCH_COMPONENT*) aItem;
// Power symbols and other components which have the reference starting
// with "#" are not included in netlist (pseudo or virtual components)
ref = comp->GetRef( aSheetPath );
if( ref[0] == wxChar( '#' ) )
continue;
// if( Component->m_FlagControlMulti == 1 )
// continue; /* yes */
2008-03-20 01:50:21 +00:00
// removed because with multiple instances of one schematic
// (several sheets pointing to 1 screen), this will be erroneously be
// toggled.
2007-09-21 13:23:51 +00:00
LIB_COMPONENT* entry = CMP_LIBRARY::FindLibraryComponent( comp->GetLibName() );
if( !entry )
2007-09-21 13:23:51 +00:00
continue;
// If component is a "multi parts per package" type
if( entry->GetPartCount() > 1 )
{
2010-08-04 19:14:51 +00:00
// test if this reference has already been processed, and if so skip
if( m_ReferencesAlreadyFound.Lookup( ref ) )
continue;
2010-07-31 23:57:36 +00:00
2010-08-04 19:14:51 +00:00
// Collect all pins for this reference designator by searching
// the entire design for other parts with the same reference designator.
// This is only done once, it would be too expensive otherwise.
2010-08-04 19:14:51 +00:00
findAllInstancesOfComponent( comp, entry, aSheetPath );
}
2010-07-31 23:57:36 +00:00
else // entry->GetPartCount() <= 1 means one part per package
2007-09-21 13:23:51 +00:00
{
LIB_PINS pins; // constructed once here
entry->GetPins( pins, comp->GetUnitSelection( aSheetPath ), comp->GetConvert() );
for( size_t i = 0; i < pins.size(); i++ )
2007-09-21 13:23:51 +00:00
{
LIB_PIN* pin = pins[i];
wxASSERT( pin->Type() == LIB_PIN_T );
2010-08-04 19:14:51 +00:00
addPinToComponentPinList( comp, aSheetPath, pin );
2007-09-21 13:23:51 +00:00
}
}
// Sort pins in m_SortedComponentPinList by pin number
sort( m_SortedComponentPinList.begin(),
m_SortedComponentPinList.end(), sortPinsByNum );
2007-09-21 13:23:51 +00:00
// Remove duplicate Pins in m_SortedComponentPinList
eraseDuplicatePins( );
2007-09-21 13:23:51 +00:00
// record the usage of this library component entry.
m_LibParts.insert( entry ); // rejects non-unique pointers
return comp;
2007-09-21 13:23:51 +00:00
}
return NULL;
}
2007-09-21 13:23:51 +00:00
2010-07-31 23:57:36 +00:00
/**
* Function node
2010-08-07 15:31:32 +00:00
* is a convenience function that creates a new XNODE with an optional textual child.
* It also provides some insulation from a possible change in XML library.
*
2010-07-31 23:57:36 +00:00
* @param aName is the name to associate with a new node of type wxXML_ELEMENT_NODE.
* @param aTextualContent is optional, and if given is the text to include in a child
2010-07-31 23:57:36 +00:00
* of the returned node, and has type wxXML_TEXT_NODE.
*/
2010-08-07 15:31:32 +00:00
static XNODE* node( const wxString& aName, const wxString& aTextualContent = wxEmptyString )
{
2010-08-07 15:31:32 +00:00
XNODE* n = new XNODE( wxXML_ELEMENT_NODE, aName );
2010-08-04 19:14:51 +00:00
if( aTextualContent.Len() > 0 ) // excludes wxEmptyString, the parameter's default value
2010-08-07 15:31:32 +00:00
n->AddChild( new XNODE( wxXML_TEXT_NODE, wxEmptyString, aTextualContent ) );
return n;
}
XNODE* NETLIST_EXPORT_TOOL::makeGenericDesignHeader()
2010-08-04 19:14:51 +00:00
{
2010-08-08 00:31:07 +00:00
XNODE* xdesign = node( wxT("design") );
2010-08-04 19:14:51 +00:00
// the root sheet is a special sheet, call it source
xdesign->AddChild( node( wxT( "source" ), g_RootSheet->GetScreen()->GetFileName() ) );
2010-08-04 19:14:51 +00:00
xdesign->AddChild( node( wxT( "date" ), DateAndTime() ) );
2010-08-04 19:14:51 +00:00
// which Eeschema tool
xdesign->AddChild( node( wxT( "tool" ), wxGetApp().GetAppName() + wxChar(' ') +
GetBuildVersion() ) );
2010-08-04 19:14:51 +00:00
/* @todo might do a list of schematic pages
<page name="">
<title/>
<revision/>
<company/>
<comments>
<comment>blah</comment> <!-- comment1 -->
<comment>blah</comment> <!-- comment2 -->
</comments>
<pagesize/>
</page>
:
2010-08-04 19:14:51 +00:00
and a sheet hierarchy report here
<sheets>
<sheet name="sheetname1" page="pagenameA">
<sheet name="sheetname2" page="pagenameB"/> use recursion to output?
2010-08-04 19:14:51 +00:00
</sheet>
</sheets>
*/
return xdesign;
}
XNODE* NETLIST_EXPORT_TOOL::makeGenericLibraries()
2010-08-04 19:14:51 +00:00
{
XNODE* xlibs = node( wxT( "libraries" ) ); // auto_ptr
2010-08-04 19:14:51 +00:00
for( std::set<void*>::iterator it = m_Libraries.begin(); it!=m_Libraries.end(); ++it )
2010-08-04 19:14:51 +00:00
{
CMP_LIBRARY* lib = (CMP_LIBRARY*) *it;
2010-08-08 00:31:07 +00:00
XNODE* xlibrary;
2010-08-04 19:14:51 +00:00
xlibs->AddChild( xlibrary = node( wxT( "library" ) ) );
2010-08-08 00:31:07 +00:00
xlibrary->AddAttribute( wxT( "logical" ), lib->GetLogicalName() );
xlibrary->AddChild( node( wxT( "uri" ), lib->GetFullFileName() ) );
2010-08-04 19:14:51 +00:00
// @todo: add more fun stuff here
}
return xlibs;
}
XNODE* NETLIST_EXPORT_TOOL::makeGenericLibParts()
2010-08-04 19:14:51 +00:00
{
XNODE* xlibparts = node( wxT( "libparts" ) ); // auto_ptr
2010-08-04 19:14:51 +00:00
wxString sLibpart = wxT( "libpart" );
wxString sLib = wxT( "lib" );
wxString sPart = wxT( "part" );
wxString sPins = wxT( "pins" ); // key for library component pins list
wxString sPin = wxT( "pin" ); // key for one library component pin descr
wxString sPinNum = wxT( "num" ); // key for one library component pin num
wxString sPinName = wxT( "name" ); // key for one library component pin name
wxString sPinType = wxT( "type" ); // key for one library component pin electrical type
2010-08-04 19:14:51 +00:00
wxString sName = wxT( "name" );
wxString sField = wxT( "field" );
wxString sFields = wxT( "fields" );
wxString sDescr = wxT( "description" );
wxString sDocs = wxT( "docs" );
wxString sFprints = wxT( "footprints" );
wxString sFp = wxT( "fp" );
2010-08-04 19:14:51 +00:00
LIB_PINS pinList;
LIB_FIELDS fieldList;
2010-08-04 19:14:51 +00:00
m_Libraries.clear();
for( std::set<void*>::iterator it = m_LibParts.begin(); it!=m_LibParts.end(); ++it )
2010-08-04 19:14:51 +00:00
{
LIB_COMPONENT* lcomp = (LIB_COMPONENT*) *it;
CMP_LIBRARY* library = lcomp->GetLibrary();
2010-08-04 19:14:51 +00:00
m_Libraries.insert( library ); // inserts component's library if unique
2010-08-04 19:14:51 +00:00
2010-08-07 15:31:32 +00:00
XNODE* xlibpart;
xlibparts->AddChild( xlibpart = node( sLibpart ) );
2010-08-08 00:31:07 +00:00
xlibpart->AddAttribute( sLib, library->GetLogicalName() );
xlibpart->AddAttribute( sPart, lcomp->GetName() );
2010-08-04 19:14:51 +00:00
//----- show the important properties -------------------------
if( !lcomp->GetAlias( 0 )->GetDescription().IsEmpty() )
xlibpart->AddChild( node( sDescr, lcomp->GetAlias( 0 )->GetDescription() ) );
2010-08-04 19:14:51 +00:00
if( !lcomp->GetAlias( 0 )->GetDocFileName().IsEmpty() )
xlibpart->AddChild( node( sDocs, lcomp->GetAlias( 0 )->GetDocFileName() ) );
2010-08-04 19:14:51 +00:00
// Write the footprint list
if( lcomp->GetFootPrints().GetCount() )
{
XNODE* xfootprints;
xlibpart->AddChild( xfootprints = node( sFprints ) );
2010-08-04 19:14:51 +00:00
for( unsigned i=0; i<lcomp->GetFootPrints().GetCount(); ++i )
{
xfootprints->AddChild( node( sFp, lcomp->GetFootPrints()[i] ) );
}
}
2010-08-04 19:14:51 +00:00
//----- show the fields here ----------------------------------
fieldList.clear();
lcomp->GetFields( fieldList );
2010-08-04 19:14:51 +00:00
2010-08-07 15:31:32 +00:00
XNODE* xfields;
xlibpart->AddChild( xfields = node( sFields ) );
2010-08-04 19:14:51 +00:00
for( unsigned i=0; i<fieldList.size(); ++i )
{
if( !fieldList[i].GetText().IsEmpty() )
2010-08-04 19:14:51 +00:00
{
2010-08-07 15:31:32 +00:00
XNODE* xfield;
xfields->AddChild( xfield = node( sField, fieldList[i].GetText() ) );
xfield->AddAttribute( sName, fieldList[i].GetName(false) );
2010-08-04 19:14:51 +00:00
}
}
//----- show the pins here ------------------------------------
pinList.clear();
lcomp->GetPins( pinList, 0, 0 );
2010-08-04 19:14:51 +00:00
/* we must erase redundant Pins references in pinList
* These redundant pins exist because some pins
* are found more than one time when a component has
* multiple parts per package or has 2 representations (DeMorgan conversion)
* For instance, a 74ls00 has DeMorgan conversion, with different pin shapes,
* and therefore each pin appears 2 times in the list.
* Common pins (VCC, GND) can also be found more than once.
*/
2010-08-08 00:31:07 +00:00
sort( pinList.begin(), pinList.end(), sortPinsByNumber );
for( int ii = 0; ii < (int)pinList.size()-1; ii++ )
{
if( pinList[ii]->GetNumber() == pinList[ii+1]->GetNumber() )
{ // 2 pins have the same number, remove the redundant pin at index i+1
pinList.erase(pinList.begin() + ii + 1);
ii--;
}
}
2010-08-04 19:14:51 +00:00
if( pinList.size() )
{
2010-08-07 15:31:32 +00:00
XNODE* pins;
2010-08-04 19:14:51 +00:00
xlibpart->AddChild( pins = node( sPins ) );
2010-08-04 19:14:51 +00:00
for( unsigned i=0; i<pinList.size(); ++i )
{
2010-08-07 15:31:32 +00:00
XNODE* pin;
2010-08-04 19:14:51 +00:00
pins->AddChild( pin = node( sPin ) );
pin->AddAttribute( sPinNum, pinList[i]->GetNumberString() );
pin->AddAttribute( sPinName, pinList[i]->GetName() );
pin->AddAttribute( sPinType, pinList[i]->GetTypeString() );
2010-08-04 19:14:51 +00:00
// caution: construction work site here, drive slowly
}
}
}
return xlibparts;
}
XNODE* NETLIST_EXPORT_TOOL::makeGenericListOfNets()
2010-08-04 19:14:51 +00:00
{
XNODE* xnets = node( wxT( "nets" ) ); // auto_ptr if exceptions ever get used.
2010-08-04 19:14:51 +00:00
wxString netCodeTxt;
wxString netName;
wxString ref;
wxString sNet = wxT( "net" );
wxString sName = wxT( "name" );
wxString sCode = wxT( "code" );
wxString sRef = wxT( "ref" );
wxString sPin = wxT( "pin" );
wxString sNode = wxT( "node" );
wxString sFmtd = wxT( "%d" );
2010-08-07 15:31:32 +00:00
XNODE* xnet = 0;
2010-08-04 19:14:51 +00:00
int netCode;
int lastNetCode = -1;
int sameNetcodeCount = 0;
/* output:
<net code="123" name="/cfcard.sch/WAIT#">
<node ref="R23" pin="1"/>
<node ref="U18" pin="12"/>
</net>
*/
m_LibParts.clear(); // must call this function before using m_LibParts.
for( unsigned ii = 0; ii < g_NetObjectslist.size(); ii++ )
{
NETLIST_OBJECT* nitem = g_NetObjectslist[ii];
SCH_COMPONENT* comp;
// New net found, write net id;
if( ( netCode = nitem->GetNet() ) != lastNetCode )
{
sameNetcodeCount = 0; // item count for this net
netName.Empty();
// Find a label for this net, if it exists.
NETLIST_OBJECT* netref = nitem->m_NetNameCandidate;
if( netref )
{
if( netref->m_Type != NET_PINLABEL && netref->m_Type != NET_GLOBLABEL )
{
// usual net name, prefix it by the sheet path
netName = netref->m_SheetList.PathHumanReadable();
}
netName += netref->m_Label;
}
lastNetCode = netCode;
}
if( nitem->m_Type != NET_PIN )
continue;
if( nitem->m_Flag != 0 ) // Redundant pin, skip it
continue;
2010-08-04 19:14:51 +00:00
comp = (SCH_COMPONENT*) nitem->m_Link;
// Get the reference for the net name and the main parent component
ref = comp->GetRef( &nitem->m_SheetList );
if( ref[0] == wxChar( '#' ) )
continue;
if( ++sameNetcodeCount == 1 )
{
xnets->AddChild( xnet = node( sNet ) );
2010-08-04 19:14:51 +00:00
netCodeTxt.Printf( sFmtd, netCode );
2010-08-08 00:31:07 +00:00
xnet->AddAttribute( sCode, netCodeTxt );
xnet->AddAttribute( sName, netName );
2010-08-04 19:14:51 +00:00
}
2010-08-07 15:31:32 +00:00
XNODE* xnode;
xnet->AddChild( xnode = node( sNode ) );
2010-08-08 00:31:07 +00:00
xnode->AddAttribute( sRef, ref );
xnode->AddAttribute( sPin, nitem->GetPinNumText() );
2010-08-04 19:14:51 +00:00
}
return xnets;
}
XNODE* NETLIST_EXPORT_TOOL::makeGenericRoot()
{
2010-08-07 15:31:32 +00:00
XNODE* xroot = node( wxT( "export" ) );
2010-08-07 15:25:18 +00:00
2010-08-08 00:31:07 +00:00
xroot->AddAttribute( wxT( "version" ), wxT( "D" ) );
2010-08-04 19:14:51 +00:00
2010-08-07 15:25:18 +00:00
// add the "design" header
xroot->AddChild( makeGenericDesignHeader() );
xroot->AddChild( makeGenericComponents() );
xroot->AddChild( makeGenericLibParts() );
// must follow makeGenericLibParts()
xroot->AddChild( makeGenericLibraries() );
xroot->AddChild( makeGenericListOfNets() );
return xroot;
}
XNODE* NETLIST_EXPORT_TOOL::makeGenericComponents()
2010-08-07 15:25:18 +00:00
{
2010-08-08 00:31:07 +00:00
XNODE* xcomps = node( wxT( "components" ) );
2010-08-07 15:25:18 +00:00
2010-08-08 00:31:07 +00:00
wxString timeStamp;
// some strings we need many times, but don't want to construct more
// than once for performance. These are used within loops so the
2010-07-31 23:57:36 +00:00
// enclosing wxString constructor would fire on each loop iteration if
// they were in a nested scope.
// these are actually constructor invocations, not assignments as it appears:
2010-08-08 00:31:07 +00:00
wxString sFields = wxT( "fields" );
wxString sField = wxT( "field" );
wxString sComponent = wxT( "comp" ); // use "part" ?
wxString sName = wxT( "name" );
wxString sRef = wxT( "ref" );
wxString sPins = wxT( "pins" );
wxString sPin = wxT( "pin" );
wxString sValue = wxT( "value" );
wxString sSheetPath = wxT( "sheetpath" );
wxString sFootprint = wxT( "footprint" );
wxString sDatasheet = wxT( "datasheet" );
wxString sTStamp = wxT( "tstamp" );
wxString sTStamps = wxT( "tstamps" );
wxString sTSFmt = wxT( "%8.8lX" ); // comp->m_TimeStamp
wxString sLibSource = wxT( "libsource" );
wxString sLibPart = wxT( "libpart" );
wxString sLib = wxT( "lib" );
wxString sPart = wxT( "part" );
wxString sNames = wxT( "names" );
2010-08-04 19:14:51 +00:00
m_ReferencesAlreadyFound.Clear();
SCH_SHEET_LIST sheetList;
2010-08-04 19:14:51 +00:00
// Output is xml, so there is no reason to remove spaces from the field values.
// And XML element names need not be translated to various languages.
for( SCH_SHEET_PATH* path = sheetList.GetFirst(); path; path = sheetList.GetNext() )
{
for( EDA_ITEM* schItem = path->LastDrawList(); schItem; schItem = schItem->Next() )
{
SCH_COMPONENT* comp = findNextComponentAndCreatePinList( schItem, path );
if( !comp )
break; // No component left
schItem = comp;
2010-08-08 00:31:07 +00:00
XNODE* xcomp; // current component being constructed
// Output the component's elements in order of expected access frequency.
2010-07-31 23:57:36 +00:00
// This may not always look best, but it will allow faster execution
// under XSL processing systems which do sequential searching within
// an element.
xcomps->AddChild( xcomp = node( sComponent ) );
2010-08-08 00:31:07 +00:00
xcomp->AddAttribute( sRef, comp->GetRef( path ) );
xcomp->AddChild( node( sValue, comp->GetField( VALUE )->GetText() ) );
if( !comp->GetField( FOOTPRINT )->IsVoid() )
xcomp->AddChild( node( sFootprint, comp->GetField( FOOTPRINT )->GetText() ) );
if( !comp->GetField( DATASHEET )->IsVoid() )
xcomp->AddChild( node( sDatasheet, comp->GetField( DATASHEET )->GetText() ) );
2010-07-31 23:57:36 +00:00
// Export all user defined fields within the component,
// which start at field index MANDATORY_FIELDS. Only output the <fields>
// container element if there are any <field>s.
if( comp->GetFieldCount() > MANDATORY_FIELDS )
{
2010-08-08 00:31:07 +00:00
XNODE* xfields;
xcomp->AddChild( xfields = node( sFields ) );
2010-07-31 23:57:36 +00:00
for( int fldNdx = MANDATORY_FIELDS; fldNdx < comp->GetFieldCount(); ++fldNdx )
{
SCH_FIELD* f = comp->GetField( fldNdx );
// only output a field if non empty and not just "~"
if( !f->IsVoid() )
2010-07-31 23:57:36 +00:00
{
2010-08-08 00:31:07 +00:00
XNODE* xfield;
xfields->AddChild( xfield = node( sField, f->GetText() ) );
2011-12-07 18:47:59 +00:00
xfield->AddAttribute( sName, f->GetName() );
2010-07-31 23:57:36 +00:00
}
}
}
2010-08-08 00:31:07 +00:00
XNODE* xlibsource;
xcomp->AddChild( xlibsource = node( sLibSource ) );
2010-07-31 23:57:36 +00:00
// "logical" library name, which is in anticipation of a better search
// algorithm for parts based on "logical_lib.part" and where logical_lib
// is merely the library name minus path and extension.
LIB_COMPONENT* entry = CMP_LIBRARY::FindLibraryComponent( comp->GetLibName() );
2010-07-31 23:57:36 +00:00
if( entry )
2010-08-08 00:31:07 +00:00
xlibsource->AddAttribute( sLib, entry->GetLibrary()->GetLogicalName() );
xlibsource->AddAttribute( sPart, comp->GetLibName() );
2010-07-31 23:57:36 +00:00
2010-08-08 00:31:07 +00:00
XNODE* xsheetpath;
xcomp->AddChild( xsheetpath = node( sSheetPath ) );
2010-08-08 00:31:07 +00:00
xsheetpath->AddAttribute( sNames, path->PathHumanReadable() );
xsheetpath->AddAttribute( sTStamps, path->Path() );
2010-07-31 23:57:36 +00:00
2011-12-12 08:37:05 +00:00
timeStamp.Printf( sTSFmt, comp->GetTimeStamp() );
xcomp->AddChild( node( sTStamp, timeStamp ) );
}
}
2010-08-07 15:25:18 +00:00
return xcomps;
}
2007-09-21 13:23:51 +00:00
2010-08-04 19:14:51 +00:00
bool NETLIST_EXPORT_TOOL::WriteKiCadNetList( const wxString& aOutFileName )
2010-08-07 15:25:18 +00:00
{
// Prepare list of nets generation
for( unsigned ii = 0; ii < g_NetObjectslist.size(); ii++ )
g_NetObjectslist[ii]->m_Flag = 0;
std::auto_ptr<XNODE> xroot( makeGenericRoot() );
2010-08-08 00:31:07 +00:00
try
2010-08-08 00:31:07 +00:00
{
FILE_OUTPUTFORMATTER formatter( aOutFileName );
xroot->Format( &formatter, 0 );
2010-08-08 00:31:07 +00:00
}
catch( IO_ERROR ioe )
2010-08-08 00:31:07 +00:00
{
DisplayError( NULL, ioe.errorText );
return false;
2010-08-08 00:31:07 +00:00
}
return true;
}
bool NETLIST_EXPORT_TOOL::WriteGENERICNetList( const wxString& aOutFileName )
{
// Prepare list of nets generation
for( unsigned ii = 0; ii < g_NetObjectslist.size(); ii++ )
g_NetObjectslist[ii]->m_Flag = 0;
2010-08-08 00:31:07 +00:00
#if 1
2010-08-07 15:25:18 +00:00
// output the XML format netlist.
wxXmlDocument xdoc;
xdoc.SetRoot( makeGenericRoot() );
2007-09-21 13:23:51 +00:00
return xdoc.Save( aOutFileName, 2 /* indent bug, today was ignored by wxXml lib */ );
#else // output the well established/old generic net list format which was not XML.
wxString field;
wxString footprint;
wxString netname;
FILE* out;
int ret = 0; // OR on each call, test sign bit at very end.
if( ( out = wxFopen( aOutFileName, wxT( "wt" ) ) ) == NULL )
2007-09-21 13:23:51 +00:00
{
wxString msg;
msg.Printf( _( "Failed to create file <%s>" ), GetChars( aOutFileName ) );
DisplayError( NULL, msg );
return false;
2007-09-21 13:23:51 +00:00
}
m_ReferencesAlreadyFound.Clear();
2007-09-21 13:23:51 +00:00
ret |= fprintf( out, "$BeginNetlist\n" );
// Create netlist module section
ret |= fprintf( out, "$BeginComponentList\n" );
SCH_SHEET_LIST sheetList;
for( SCH_SHEET_PATH* path = sheetList.GetFirst(); path; path = sheetList.GetNext() )
2008-03-20 01:50:21 +00:00
{
for( EDA_ITEM* schItem = path->LastDrawList(); schItem; schItem = schItem->Next() )
2008-03-20 01:50:21 +00:00
{
SCH_COMPONENT* comp = findNextComponentAndCreatePinList( schItem, path );
if( !comp )
break; // No component left
2007-09-21 13:23:51 +00:00
schItem = comp;
footprint.Empty();
if( !comp->GetField( FOOTPRINT )->IsVoid() )
2007-09-21 13:23:51 +00:00
{
footprint = comp->GetField( FOOTPRINT )->m_Text;
footprint.Replace( wxT( " " ), wxT( "_" ) );
2007-09-21 13:23:51 +00:00
}
ret |= fprintf( out, "\n$BeginComponent\n" );
ret |= fprintf( out, "TimeStamp=%8.8lX\n", comp->m_TimeStamp );
ret |= fprintf( out, "Footprint=%s\n", TO_UTF8( footprint ) );
2007-09-21 13:23:51 +00:00
field = wxT( "Reference=" ) + comp->GetRef( path ) + wxT( "\n" );
field.Replace( wxT( " " ), wxT( "_" ) );
ret |= fputs( TO_UTF8( field ), out );
2007-09-21 13:23:51 +00:00
field = comp->GetField( VALUE )->m_Text;
field.Replace( wxT( " " ), wxT( "_" ) );
ret |= fprintf( out, "Value=%s\n", TO_UTF8( field ) );
field = comp->GetLibName();
field.Replace( wxT( " " ), wxT( "_" ) );
ret |= fprintf( out, "Libref=%s\n", TO_UTF8( field ) );
2007-09-21 13:23:51 +00:00
// Write pin list:
ret |= fprintf( out, "$BeginPinList\n" );
for( unsigned ii = 0; ii < m_SortedComponentPinList.size(); ii++ )
2007-09-21 13:23:51 +00:00
{
NETLIST_OBJECT* Pin = m_SortedComponentPinList[ii];
2007-09-21 13:23:51 +00:00
if( !Pin )
continue;
2010-08-04 19:14:51 +00:00
sprintPinNetName( &netname, wxT( "$-%.6d" ), Pin );
2007-09-21 13:23:51 +00:00
if( netname.IsEmpty() )
netname = wxT( "?" );
ret |= fprintf( out, "%.4s=%s\n", (char*) &Pin->m_PinNum, TO_UTF8( netname ) );
2007-09-21 13:23:51 +00:00
}
ret |= fprintf( out, "$EndPinList\n" );
ret |= fprintf( out, "$EndComponent\n" );
2007-09-21 13:23:51 +00:00
}
}
ret |= fprintf( out, "$EndComponentList\n" );
2007-09-21 13:23:51 +00:00
ret |= fprintf( out, "\n$BeginNets\n" );
2007-09-21 13:23:51 +00:00
2010-08-04 19:14:51 +00:00
if( !writeGENERICListOfNets( out, g_NetObjectslist ) )
ret = -1;
2007-09-21 13:23:51 +00:00
ret |= fprintf( out, "$EndNets\n" );
2007-09-21 13:23:51 +00:00
ret |= fprintf( out, "\n$EndNetlist\n" );
ret |= fclose( out );
2007-09-21 13:23:51 +00:00
return ret >= 0;
#endif
}
2007-09-21 13:23:51 +00:00
bool NETLIST_EXPORT_TOOL::WriteNetListPspice( FILE* f, bool use_netnames, bool aUsePrefix )
{
int ret = 0;
int nbitems;
wxString text;
wxArrayString spiceCommandAtBeginFile;
wxArrayString spiceCommandAtEndFile;
wxString msg;
wxString netName;
#define BUFYPOS_LEN 4
wxChar bufnum[BUFYPOS_LEN + 1];
std::vector<int> pinSequence; // numeric indices into m_SortedComponentPinList
wxArrayString stdPinNameArray; // Array containing Standard Pin Names
wxString delimeters = wxT( "{:,; }" );
wxString disableStr = wxT( "N" );
ret |= fprintf( f, "* %s (Spice format) creation date: %s\n\n",
NETLIST_HEAD_STRING, TO_UTF8( DateAndTime() ) );
2010-08-04 19:14:51 +00:00
// Prepare list of nets generation (not used here, but...
for( unsigned ii = 0; ii < g_NetObjectslist.size(); ii++ )
g_NetObjectslist[ii]->m_Flag = 0;
ret |= fprintf( f, "* To exclude a component from the Spice Netlist add [Spice_Netlist_Enabled] user FIELD set to: N\n" );
ret |= fprintf( f, "* To reorder the component spice node sequence add [Spice_Node_Sequence] user FIELD and define sequence: 2,1,0\n" );
2010-08-04 19:14:51 +00:00
// Create text list starting by [.-]pspice , or [.-]gnucap (simulator
// commands) and create text list starting by [+]pspice , or [+]gnucap
// (simulator commands)
2007-09-21 13:23:51 +00:00
bufnum[BUFYPOS_LEN] = 0;
2010-08-04 19:14:51 +00:00
SCH_SHEET_LIST sheetList;
2007-09-21 13:23:51 +00:00
2010-08-04 19:14:51 +00:00
for( SCH_SHEET_PATH* sheet = sheetList.GetFirst(); sheet; sheet = sheetList.GetNext() )
2007-09-21 13:23:51 +00:00
{
for( EDA_ITEM* item = sheet->LastDrawList(); item; item = item->Next() )
2007-09-21 13:23:51 +00:00
{
wxChar ident;
if( item->Type() != SCH_TEXT_T )
2007-09-21 13:23:51 +00:00
continue;
2010-08-04 19:14:51 +00:00
SCH_TEXT* drawText = (SCH_TEXT*) item;
text = drawText->GetText();
2010-08-04 19:14:51 +00:00
if( text.IsEmpty() )
2007-09-21 13:23:51 +00:00
continue;
2007-09-21 13:23:51 +00:00
ident = text.GetChar( 0 );
2007-09-21 13:23:51 +00:00
if( ident != '.' && ident != '-' && ident != '+' )
continue;
text.Remove( 0, 1 ); // Remove the first char.
text.Remove( 6 ); // text contains 6 char.
2008-06-18 09:18:51 +00:00
text.MakeLower();
2007-09-21 13:23:51 +00:00
if( ( text == wxT( "pspice" ) ) || ( text == wxT( "gnucap" ) ) )
{
2010-08-04 19:14:51 +00:00
// Put the Y position as an ascii string, for sort by vertical
// position, using usual sort string by alphabetic value
int ypos = drawText->GetPosition().y;
for( int ii = 0; ii < BUFYPOS_LEN; ii++ )
2007-09-21 13:23:51 +00:00
{
bufnum[BUFYPOS_LEN - 1 - ii] = (ypos & 63) + ' ';
ypos >>= 6;
2007-09-21 13:23:51 +00:00
}
text = drawText->GetText().AfterFirst( ' ' );
// First BUFYPOS_LEN char are the Y position.
msg.Printf( wxT( "%s %s" ), bufnum, text.GetData() );
2007-09-21 13:23:51 +00:00
if( ident == '+' )
2010-08-04 19:14:51 +00:00
spiceCommandAtEndFile.Add( msg );
2007-09-21 13:23:51 +00:00
else
2010-08-04 19:14:51 +00:00
spiceCommandAtBeginFile.Add( msg );
2007-09-21 13:23:51 +00:00
}
}
}
2010-08-04 19:14:51 +00:00
// Print texts starting by [.-]pspice , ou [.-]gnucap (of course, without
// the Y position string)
nbitems = spiceCommandAtBeginFile.GetCount();
2007-09-21 13:23:51 +00:00
if( nbitems )
{
2010-08-04 19:14:51 +00:00
spiceCommandAtBeginFile.Sort();
for( int ii = 0; ii < nbitems; ii++ )
2007-09-21 13:23:51 +00:00
{
2010-08-04 19:14:51 +00:00
spiceCommandAtBeginFile[ii].Remove( 0, BUFYPOS_LEN );
spiceCommandAtBeginFile[ii].Trim( true );
spiceCommandAtBeginFile[ii].Trim( false );
ret |= fprintf( f, "%s\n", TO_UTF8( spiceCommandAtBeginFile[ii] ) );
2007-09-21 13:23:51 +00:00
}
}
2010-08-04 19:14:51 +00:00
ret |= fprintf( f, "\n" );
2007-09-21 13:23:51 +00:00
// Create component list
m_ReferencesAlreadyFound.Clear();
2007-09-21 13:23:51 +00:00
2010-08-04 19:14:51 +00:00
for( SCH_SHEET_PATH* sheet = sheetList.GetFirst(); sheet; sheet = sheetList.GetNext() )
2007-09-21 13:23:51 +00:00
{
ret |= fprintf( f, "*Sheet Name:%s\n", TO_UTF8( sheet->PathHumanReadable() ) );
for( EDA_ITEM* item = sheet->LastDrawList(); item; item = item->Next() )
2007-09-21 13:23:51 +00:00
{
SCH_COMPONENT* comp = findNextComponentAndCreatePinList( item, sheet );
2010-08-04 19:14:51 +00:00
if( !comp )
2007-09-21 13:23:51 +00:00
break;
2010-08-04 19:14:51 +00:00
item = comp;
// Reset NodeSeqIndex Count:
pinSequence.clear();
// Check to see if component should be removed from Spice Netlist:
SCH_FIELD* netlistEnabledField = comp->FindField( wxT( "Spice_Netlist_Enabled" ) );
if( netlistEnabledField )
{
wxString netlistEnabled = netlistEnabledField->GetText();
if( netlistEnabled.IsEmpty() )
break;
if( netlistEnabled.CmpNoCase( disableStr ) == 0 )
continue;
}
// Check if Alternative Pin Sequence is Available:
SCH_FIELD* spiceSeqField = comp->FindField( wxT( "Spice_Node_Sequence" ) );
if( spiceSeqField )
{
// Get String containing the Sequence of Nodes:
wxString nodeSeqIndexLineStr = spiceSeqField->GetText();
// Verify Field Exists and is not empty:
if( nodeSeqIndexLineStr.IsEmpty() )
break;
// Create an Array of Standard Pin Names from part definition:
stdPinNameArray.Clear();
for( unsigned ii = 0; ii < m_SortedComponentPinList.size(); ii++ )
{
NETLIST_OBJECT* pin = m_SortedComponentPinList[ii];
if( !pin )
continue;
stdPinNameArray.Add( pin->GetPinNumText() );
}
// Get Alt Pin Name Array From User:
wxStringTokenizer tkz( nodeSeqIndexLineStr, delimeters );
while( tkz.HasMoreTokens() )
{
wxString pinIndex = tkz.GetNextToken();
int seq;
// Find PinName In Standard List assign Standard List Index to Name:
seq = stdPinNameArray.Index(pinIndex);
if( seq != wxNOT_FOUND )
{
pinSequence.push_back( seq );
}
}
}
//Get Standard Reference Designator:
wxString RefName = comp->GetRef( sheet );
//Conditionally add Prefix only for devices that begin with U or IC:
if( aUsePrefix )
{
if( RefName.StartsWith( wxT( "U" ) ) || RefName.StartsWith( wxT( "IC" ) ) )
RefName = wxT( "X" ) + RefName;
}
ret |= fprintf( f, "%s ", TO_UTF8( RefName) );
2007-09-21 13:23:51 +00:00
// Write pin list:
int activePinIndex = 0;
for( unsigned ii = 0; ii < m_SortedComponentPinList.size(); ii++ )
2007-09-21 13:23:51 +00:00
{
// Case of Alt Sequence definition with Unused/Invalid Node index:
// Valid used Node Indexes are in the set {0,1,2,...m_SortedComponentPinList.size()-1}
if( pinSequence.size() )
{
// All Vector values must be less <= max package size
// And Total Vector size should be <= package size
if( ( (unsigned) pinSequence[ii] < m_SortedComponentPinList.size() ) && ( ii < pinSequence.size() ) )
{
// Case of Alt Pin Sequence in control good Index:
activePinIndex = pinSequence[ii];
}
else
{
// Case of Alt Pin Sequence in control Bad Index or not using all pins for simulation:
continue;
}
}
// Case of Standard Pin Sequence in control:
else
{
activePinIndex = ii;
}
NETLIST_OBJECT* pin = m_SortedComponentPinList[activePinIndex];
2010-08-04 19:14:51 +00:00
if( !pin )
2007-09-21 13:23:51 +00:00
continue;
2010-08-04 19:14:51 +00:00
sprintPinNetName( &netName , wxT( "N-%.6d" ), pin );
if( netName.IsEmpty() )
netName = wxT( "?" );
2007-09-21 13:23:51 +00:00
if( use_netnames )
ret |= fprintf( f, " %s", TO_UTF8( netName ) );
2010-08-04 19:14:51 +00:00
else // Use number for net names (net number = 0 for "GND")
2007-09-21 13:23:51 +00:00
{
// NetName = "0" is "GND" net for Spice
if( netName == wxT( "0" ) || netName == wxT( "GND" ) )
2010-08-04 19:14:51 +00:00
ret |= fprintf( f, " 0" );
2007-09-21 13:23:51 +00:00
else
2010-08-04 19:14:51 +00:00
ret |= fprintf( f, " %d", pin->GetNet() );
2007-09-21 13:23:51 +00:00
}
}
// Get Component Value Name:
wxString CompValue = comp->GetField( VALUE )->GetText();
// Check if Override Model Name is Provided:
SCH_FIELD* spiceModelField = comp->FindField( wxT( "spice_model" ) );
if( spiceModelField )
{
// Get Model Name String:
wxString ModelNameStr = spiceModelField->GetText();
// Verify Field Exists and is not empty:
if( !ModelNameStr.IsEmpty() )
CompValue = ModelNameStr;
}
// Print Component Value:
ret |= fprintf( f, " %s\t\t",TO_UTF8( CompValue ) );
// Show Seq Spec on same line as component using line-comment ";":
for( unsigned i = 0; i < pinSequence.size(); ++i )
{
if( i==0 )
ret |= fprintf( f, ";Node Sequence Spec.<" );
ret |= fprintf( f, "%s", TO_UTF8( stdPinNameArray.Item( pinSequence[i] ) ) );
if( i < pinSequence.size()-1 )
ret |= fprintf( f, "," );
else
ret |= fprintf( f, ">" );
}
// Next Netlist line record:
ret |= fprintf( f, "\n" );
2007-09-21 13:23:51 +00:00
}
}
m_SortedComponentPinList.clear();
2007-09-21 13:23:51 +00:00
2010-08-04 19:14:51 +00:00
// Print texts starting with [+]pspice or [+]gnucap
nbitems = spiceCommandAtEndFile.GetCount();
2007-09-21 13:23:51 +00:00
if( nbitems )
{
2010-08-04 19:14:51 +00:00
ret |= fprintf( f, "\n" );
spiceCommandAtEndFile.Sort();
for( int ii = 0; ii < nbitems; ii++ )
2007-09-21 13:23:51 +00:00
{
2010-08-04 19:14:51 +00:00
spiceCommandAtEndFile[ii].Remove( 0, +BUFYPOS_LEN );
spiceCommandAtEndFile[ii].Trim( true );
spiceCommandAtEndFile[ii].Trim( false );
ret |= fprintf( f, "%s\n", TO_UTF8( spiceCommandAtEndFile[ii] ) );
2007-09-21 13:23:51 +00:00
}
}
2010-08-04 19:14:51 +00:00
ret |= fprintf( f, "\n.end\n" );
return ret >= 0;
}
2007-09-21 13:23:51 +00:00
bool NETLIST_EXPORT_TOOL::WriteNetListPCBNEW( FILE* f, bool with_pcbnew )
{
2010-07-31 23:57:36 +00:00
wxString field;
wxString footprint;
int ret = 0; // zero now, OR in the sign bit on error
wxString netName;
std::vector< SCH_REFERENCE > cmpList;
2007-09-21 13:23:51 +00:00
if( with_pcbnew )
ret |= fprintf( f, "# %s created %s\n(\n",
NETLIST_HEAD_STRING, TO_UTF8( DateAndTime() ) );
2007-09-21 13:23:51 +00:00
else
ret |= fprintf( f, "( { %s created %s }\n",
NETLIST_HEAD_STRING, TO_UTF8( DateAndTime() ) );
2007-09-21 13:23:51 +00:00
// Prepare list of nets generation
for( unsigned ii = 0; ii < g_NetObjectslist.size(); ii++ )
g_NetObjectslist[ii]->m_Flag = 0;
2007-09-21 13:23:51 +00:00
// Create netlist module section
m_ReferencesAlreadyFound.Clear();
2007-09-21 13:23:51 +00:00
SCH_SHEET_LIST sheetList;
2007-09-21 13:23:51 +00:00
for( SCH_SHEET_PATH* path = sheetList.GetFirst(); path; path = sheetList.GetNext() )
2007-09-21 13:23:51 +00:00
{
for( EDA_ITEM* item = path->LastDrawList(); item; item = item->Next() )
2007-09-21 13:23:51 +00:00
{
SCH_COMPONENT* comp = findNextComponentAndCreatePinList( item, path );
if( !comp )
2007-09-21 13:23:51 +00:00
break;
item = comp;
// Get the Component FootprintFilter and put the component in
// cmpList if filter is present
LIB_COMPONENT* entry = CMP_LIBRARY::FindLibraryComponent( comp->GetLibName() );
if( entry )
2007-09-21 13:23:51 +00:00
{
if( entry->GetFootPrints().GetCount() != 0 ) // Put in list
2007-09-21 13:23:51 +00:00
{
cmpList.push_back( SCH_REFERENCE( comp, entry, *path ) );
2007-09-21 13:23:51 +00:00
}
}
if( !comp->GetField( FOOTPRINT )->IsVoid() )
2007-09-21 13:23:51 +00:00
{
footprint = comp->GetField( FOOTPRINT )->GetText();
footprint.Replace( wxT( " " ), wxT( "_" ) );
2007-09-21 13:23:51 +00:00
}
else
footprint = wxT( "$noname" );
2007-09-21 13:23:51 +00:00
2010-07-31 23:57:36 +00:00
field = comp->GetRef( path );
2007-09-21 13:23:51 +00:00
ret |= fprintf( f, " ( %s %s",
TO_UTF8( comp->GetPath( path ) ),
TO_UTF8( footprint ) );
ret |= fprintf( f, " %s", TO_UTF8( field ) );
field = comp->GetField( VALUE )->GetText();
2010-07-31 23:57:36 +00:00
field.Replace( wxT( " " ), wxT( "_" ) );
ret |= fprintf( f, " %s", TO_UTF8( field ) );
2007-09-21 13:23:51 +00:00
if( with_pcbnew ) // Add the lib name for this component
{
field = comp->GetLibName();
2010-07-31 23:57:36 +00:00
field.Replace( wxT( " " ), wxT( "_" ) );
ret |= fprintf( f, " {Lib=%s}", TO_UTF8( field ) );
2007-09-21 13:23:51 +00:00
}
ret |= fprintf( f, "\n" );
2007-09-21 13:23:51 +00:00
// Write pin list:
for( unsigned ii = 0; ii < m_SortedComponentPinList.size(); ii++ )
2007-09-21 13:23:51 +00:00
{
NETLIST_OBJECT* pin = m_SortedComponentPinList[ii];
if( !pin )
2007-09-21 13:23:51 +00:00
continue;
sprintPinNetName( &netName, wxT( "N-%.6d" ), pin );
if( netName.IsEmpty() )
netName = wxT( "?" );
netName.Replace( wxT( " " ), wxT( "_" ) );
ret |= fprintf( f, " ( %4.4s %s )\n", (char*) &pin->m_PinNum,
TO_UTF8( netName ) );
2007-09-21 13:23:51 +00:00
}
ret |= fprintf( f, " )\n" );
2007-09-21 13:23:51 +00:00
}
}
ret |= fprintf( f, ")\n*\n" );
2007-09-21 13:23:51 +00:00
m_SortedComponentPinList.clear();
2007-09-21 13:23:51 +00:00
// Write the allowed footprint list for each component
if( with_pcbnew && cmpList.size() )
2007-09-21 13:23:51 +00:00
{
wxString ref;
ret |= fprintf( f, "{ Allowed footprints by component:\n" );
for( unsigned ii = 0; ii < cmpList.size(); ii++ )
2007-09-21 13:23:51 +00:00
{
LIB_COMPONENT* entry = cmpList[ii].GetLibComponent();
ref = cmpList[ii].GetRef();
ref.Replace( wxT( " " ), wxT( "_" ) );
ret |= fprintf( f, "$component %s\n", TO_UTF8( ref ) );
// Write the footprint list
for( unsigned jj = 0; jj < entry->GetFootPrints().GetCount(); jj++ )
2007-09-21 13:23:51 +00:00
{
ret |= fprintf( f, " %s\n", TO_UTF8( entry->GetFootPrints()[jj] ) );
2007-09-21 13:23:51 +00:00
}
ret |= fprintf( f, "$endlist\n" );
2007-09-21 13:23:51 +00:00
}
ret |= fprintf( f, "$endfootprintlist\n}\n" );
2007-09-21 13:23:51 +00:00
}
if( with_pcbnew )
{
ret |= fprintf( f, "{ Pin List by Nets\n" );
2010-08-04 19:14:51 +00:00
if( !writeGENERICListOfNets( f, g_NetObjectslist ) )
ret = -1;
ret |= fprintf( f, "}\n" );
ret |= fprintf( f, "#End\n" );
2007-09-21 13:23:51 +00:00
}
return ret >= 0;
}
bool NETLIST_EXPORT_TOOL::addPinToComponentPinList( SCH_COMPONENT* aComponent,
SCH_SHEET_PATH* aSheetPath, LIB_PIN* aPin )
{
// Search the PIN description for Pin in g_NetObjectslist
for( unsigned ii = 0; ii < g_NetObjectslist.size(); ii++ )
2007-09-21 13:23:51 +00:00
{
NETLIST_OBJECT* pin = g_NetObjectslist[ii];
if( pin->m_Type != NET_PIN )
2007-09-21 13:23:51 +00:00
continue;
if( pin->m_Link != aComponent )
2007-09-21 13:23:51 +00:00
continue;
if( pin->m_PinNum != aPin->GetNumber() )
2008-03-20 01:50:21 +00:00
continue;
// most expensive test at the end.
if( pin->m_SheetList != *aSheetPath )
2007-09-21 13:23:51 +00:00
continue;
m_SortedComponentPinList.push_back( pin );
if( m_SortedComponentPinList.size() >= MAXPIN )
2007-09-21 13:23:51 +00:00
{
// Log message for Internal error
2010-08-04 19:14:51 +00:00
DisplayError( NULL, wxT( "addPinToComponentPinList err: MAXPIN reached" ) );
2007-09-21 13:23:51 +00:00
}
return true; // we're done, we appended.
2007-09-21 13:23:51 +00:00
}
return false;
}
/*
* remove duplicate pins from aPinList (list of pins relative to a given component)
* (i.e. set pointer to duplicate pins to NULL in this list).
* also set .m_Flag member of "removed" NETLIST_OBJECT pins to 1
* When pins are duplicated, not connected duplicate is removed
* (for instance when a multiple part per package component has its power pins connected
* only on a part).
*/
void NETLIST_EXPORT_TOOL::eraseDuplicatePins( )
{
for( unsigned ii = 0; ii < m_SortedComponentPinList.size(); ii++ )
2007-09-21 13:23:51 +00:00
{
if( m_SortedComponentPinList[ii] == NULL ) /* already deleted */
2007-09-21 13:23:51 +00:00
continue;
/* Search for duplicated pins
* If found, remove duplicates. The priority is to keep connected pins
* and remove unconnected
* - So this allows (for instance when using multi op amps per package
* - to connect only one op amp to power
* Because the pin list is sorted by m_PinNum value, duplicated pins
* are necessary successive in list
*/
int idxref = ii;
for( unsigned jj = ii + 1; jj < m_SortedComponentPinList.size(); jj++ )
2007-09-21 13:23:51 +00:00
{
if( m_SortedComponentPinList[jj] == NULL ) // Already removed
continue;
// if other pin num, stop search,
// because all pins having the same number are consecutive in list.
if( m_SortedComponentPinList[idxref]->m_PinNum != m_SortedComponentPinList[jj]->m_PinNum )
2007-09-21 13:23:51 +00:00
break;
if( m_SortedComponentPinList[idxref]->m_FlagOfConnection == PAD_CONNECT )
{
m_SortedComponentPinList[jj]->m_Flag = 1;
m_SortedComponentPinList[jj] = NULL;
}
else /* the reference pin is not connected: remove this pin if the
* other pin is connected */
{
if( m_SortedComponentPinList[jj]->m_FlagOfConnection == PAD_CONNECT )
{
m_SortedComponentPinList[idxref]->m_Flag = 1;
m_SortedComponentPinList[idxref] = NULL;
idxref = jj;
}
else // the 2 pins are not connected: remove the tested pin,
{ // and continue ...
m_SortedComponentPinList[jj]->m_Flag = 1;
m_SortedComponentPinList[jj] = NULL;
}
}
2007-09-21 13:23:51 +00:00
}
}
}
void NETLIST_EXPORT_TOOL::findAllInstancesOfComponent( SCH_COMPONENT* aComponent,
LIB_COMPONENT* aEntry,
SCH_SHEET_PATH* aSheetPath )
{
wxString ref = aComponent->GetRef( aSheetPath );
wxString ref2;
2007-09-21 13:23:51 +00:00
SCH_SHEET_LIST sheetList;
for( SCH_SHEET_PATH* sheet = sheetList.GetFirst(); sheet; sheet = sheetList.GetNext() )
2007-09-21 13:23:51 +00:00
{
for( EDA_ITEM* item = sheet->LastDrawList(); item; item = item->Next() )
2007-09-21 13:23:51 +00:00
{
if( item->Type() != SCH_COMPONENT_T )
continue;
2007-09-21 13:23:51 +00:00
2010-08-04 19:14:51 +00:00
SCH_COMPONENT* comp2 = (SCH_COMPONENT*) item;
2008-03-20 01:50:21 +00:00
ref2 = comp2->GetRef( sheet );
if( ref2.CmpNoCase( ref ) != 0 )
continue;
2007-09-21 13:23:51 +00:00
int unit2 = comp2->GetUnitSelection( sheet ); // slow
for( LIB_PIN* pin = aEntry->GetNextPin(); pin; pin = aEntry->GetNextPin( pin ) )
{
wxASSERT( pin->Type() == LIB_PIN_T );
if( pin->GetUnit() && pin->GetUnit() != unit2 )
continue;
if( pin->GetConvert() && pin->GetConvert() != comp2->GetConvert() )
continue;
// A suitable pin is found: add it to the current list
2010-08-04 19:14:51 +00:00
addPinToComponentPinList( comp2, sheet, pin );
2007-09-21 13:23:51 +00:00
}
}
}
}
bool NETLIST_EXPORT_TOOL::writeGENERICListOfNets( FILE* f, NETLIST_OBJECT_LIST& aObjectsList )
{
int ret = 0;
int netCode;
int lastNetCode = -1;
int sameNetcodeCount = 0;
wxString netName;
wxString ref;
wxString netcodeName;
char firstItemInNet[256];
2007-09-21 13:23:51 +00:00
for( unsigned ii = 0; ii < aObjectsList.size(); ii++ )
2007-09-21 13:23:51 +00:00
{
SCH_COMPONENT* comp;
// New net found, write net id;
if( ( netCode = aObjectsList[ii]->GetNet() ) != lastNetCode )
2007-09-21 13:23:51 +00:00
{
sameNetcodeCount = 0; // Items count for this net
netName.Empty();
// Find a label (if exists) for this net.
NETLIST_OBJECT* netref;
netref = aObjectsList[ii]->m_NetNameCandidate;
if( netref )
netName = netref->m_Label;
2007-09-21 13:23:51 +00:00
netcodeName.Printf( wxT( "Net %d " ), netCode );
netcodeName += wxT( "\"" );
if( !netName.IsEmpty() )
2007-09-21 13:23:51 +00:00
{
if( ( netref->m_Type != NET_PINLABEL )
&& ( netref->m_Type != NET_GLOBLABEL ) )
{
// usual net name, prefix it by the sheet path
netcodeName += netref->m_SheetList.PathHumanReadable();
2008-03-20 01:50:21 +00:00
}
netcodeName += netName;
2007-09-21 13:23:51 +00:00
}
netcodeName += wxT( "\"" );
// Add the netname without prefix, in cases we need only the
// "short" netname
netcodeName += wxT( " \"" ) + netName + wxT( "\"" );
lastNetCode = netCode;
2007-09-21 13:23:51 +00:00
}
if( aObjectsList[ii]->m_Type != NET_PIN )
2007-09-21 13:23:51 +00:00
continue;
if( aObjectsList[ii]->m_Flag != 0 ) // Redundant pin, skip it
continue;
comp = (SCH_COMPONENT*) aObjectsList[ii]->m_Link;
// Get the reference for the net name and the main parent component
ref = comp->GetRef( &aObjectsList[ii]->m_SheetList );
if( ref[0] == wxChar( '#' ) )
continue; // Pseudo component (Like Power symbol)
2007-09-21 13:23:51 +00:00
// Print the pin list for this net, use special handling if
// 2 or more items are connected:
// if first item for this net found, defer printing this connection
// until a second item will is found
if( ++sameNetcodeCount == 1 )
2007-09-21 13:23:51 +00:00
{
snprintf( firstItemInNet, sizeof(firstItemInNet), " %s %.4s\n",
TO_UTF8( ref ),
(const char*) &aObjectsList[ii]->m_PinNum );
2007-09-21 13:23:51 +00:00
}
// Second item for this net found, print the Net name, and the
// first item
if( sameNetcodeCount == 2 )
2007-09-21 13:23:51 +00:00
{
ret |= fprintf( f, "%s\n", TO_UTF8( netcodeName ) );
ret |= fputs( firstItemInNet, f );
2007-09-21 13:23:51 +00:00
}
if( sameNetcodeCount >= 2 )
ret |= fprintf( f, " %s %.4s\n", TO_UTF8( ref ),
(const char*) &aObjectsList[ii]->m_PinNum );
2007-09-21 13:23:51 +00:00
}
return ret >= 0;
}
/* Generate CADSTAR net list. */
static wxString StartLine( wxT( "." ) );
bool NETLIST_EXPORT_TOOL::WriteNetListCADSTAR( FILE* f )
{
int ret = 0;
2007-09-21 13:23:51 +00:00
wxString StartCmpDesc = StartLine + wxT( "ADD_COM" );
wxString msg;
wxString footprint;
SCH_SHEET_PATH* sheet;
EDA_ITEM* DrawList;
2008-03-20 01:50:21 +00:00
SCH_COMPONENT* Component;
wxString Title = wxGetApp().GetAppName() + wxT( " " ) + GetBuildVersion();
2007-09-21 13:23:51 +00:00
ret |= fprintf( f, "%sHEA\n", TO_UTF8( StartLine ) );
ret |= fprintf( f, "%sTIM %s\n", TO_UTF8( StartLine ), TO_UTF8( DateAndTime() ) );
ret |= fprintf( f, "%sAPP ", TO_UTF8( StartLine ) );
ret |= fprintf( f, "\"%s\"\n", TO_UTF8( Title ) );
ret |= fprintf( f, "\n" );
2007-09-21 13:23:51 +00:00
// Prepare list of nets generation
for( unsigned ii = 0; ii < g_NetObjectslist.size(); ii++ )
g_NetObjectslist[ii]->m_Flag = 0;
// Create netlist module section
m_ReferencesAlreadyFound.Clear();
SCH_SHEET_LIST SheetList;
2007-09-21 13:23:51 +00:00
for( sheet = SheetList.GetFirst(); sheet != NULL; sheet = SheetList.GetNext() )
2007-09-21 13:23:51 +00:00
{
for( DrawList = sheet->LastDrawList(); DrawList != NULL; DrawList = DrawList->Next() )
2007-09-21 13:23:51 +00:00
{
DrawList = Component = findNextComponentAndCreatePinList( DrawList, sheet );
2007-09-21 13:23:51 +00:00
if( Component == NULL )
break;
/*
doing nothing with footprint
if( !Component->GetField( FOOTPRINT )->IsVoid() )
2007-09-21 13:23:51 +00:00
{
footprint = Component->GetField( FOOTPRINT )->m_Text;
footprint.Replace( wxT( " " ), wxT( "_" ) );
2007-09-21 13:23:51 +00:00
}
else
footprint = wxT( "$noname" );
*/
2007-09-21 13:23:51 +00:00
msg = Component->GetRef( sheet );
ret |= fprintf( f, "%s ", TO_UTF8( StartCmpDesc ) );
ret |= fprintf( f, "%s", TO_UTF8( msg ) );
2007-09-21 13:23:51 +00:00
msg = Component->GetField( VALUE )->GetText();
2007-09-21 13:23:51 +00:00
msg.Replace( wxT( " " ), wxT( "_" ) );
ret |= fprintf( f, " \"%s\"", TO_UTF8( msg ) );
ret |= fprintf( f, "\n" );
2007-09-21 13:23:51 +00:00
}
}
ret |= fprintf( f, "\n" );
2007-09-21 13:23:51 +00:00
m_SortedComponentPinList.clear();
2007-09-21 13:23:51 +00:00
if( ! writeListOfNetsCADSTAR( f, g_NetObjectslist ) )
ret = -1; // set error
2007-09-21 13:23:51 +00:00
ret |= fprintf( f, "\n%sEND\n", TO_UTF8( StartLine ) );
return ret >= 0;
}
2007-09-21 13:23:51 +00:00
bool NETLIST_EXPORT_TOOL::writeListOfNetsCADSTAR( FILE* f, NETLIST_OBJECT_LIST& aObjectsList )
{
int ret = 0;
2007-09-21 13:23:51 +00:00
wxString InitNetDesc = StartLine + wxT( "ADD_TER" );
wxString StartNetDesc = StartLine + wxT( "TER" );
wxString netcodeName, InitNetDescLine;
unsigned ii;
int print_ter = 0;
int NetCode, lastNetCode = -1;
2008-03-20 01:50:21 +00:00
SCH_COMPONENT* Cmp;
2007-09-21 13:23:51 +00:00
wxString NetName;
for( ii = 0; ii < g_NetObjectslist.size(); ii++ )
2007-09-21 13:23:51 +00:00
{
// Get the NetName of the current net :
if( ( NetCode = aObjectsList[ii]->GetNet() ) != lastNetCode )
2007-09-21 13:23:51 +00:00
{
NetName.Empty();
NETLIST_OBJECT* netref;
netref = aObjectsList[ii]->m_NetNameCandidate;
if( netref )
NetName = netref->m_Label;
2007-09-21 13:23:51 +00:00
netcodeName = wxT( "\"" );
2007-09-21 13:23:51 +00:00
if( !NetName.IsEmpty() )
{
if( ( netref->m_Type != NET_PINLABEL )
&& ( netref->m_Type != NET_GLOBLABEL ) )
{
// usual net name, prefix it by the sheet path
netcodeName +=
netref->m_SheetList.PathHumanReadable();
2008-03-20 01:50:21 +00:00
}
netcodeName += NetName;
2007-09-21 13:23:51 +00:00
}
else // this net has no name: create a default name $<net number>
netcodeName << wxT( "$" ) << NetCode;
netcodeName += wxT( "\"" );
lastNetCode = NetCode;
2007-09-21 13:23:51 +00:00
print_ter = 0;
}
if( aObjectsList[ii]->m_Type != NET_PIN )
2007-09-21 13:23:51 +00:00
continue;
if( aObjectsList[ii]->m_Flag != 0 )
2007-09-21 13:23:51 +00:00
continue;
Cmp = (SCH_COMPONENT*) aObjectsList[ii]->m_Link;
wxString refstr = Cmp->GetRef( &(aObjectsList[ii]->m_SheetList) );
if( refstr[0] == '#' )
continue; // Power supply symbols.
2007-09-21 13:23:51 +00:00
switch( print_ter )
{
case 0:
{
char buf[5];
wxString str_pinnum;
strncpy( buf, (char*) &aObjectsList[ii]->m_PinNum, 4 );
buf[4] = 0;
str_pinnum = FROM_UTF8( buf );
2007-09-21 13:23:51 +00:00
InitNetDescLine.Printf( wxT( "\n%s %s %.4s %s" ),
GetChars( InitNetDesc ),
GetChars( refstr ),
GetChars( str_pinnum ),
GetChars( netcodeName ) );
2007-09-21 13:23:51 +00:00
}
print_ter++;
break;
case 1:
ret |= fprintf( f, "%s\n", TO_UTF8( InitNetDescLine ) );
ret |= fprintf( f, "%s %s %.4s\n",
TO_UTF8( StartNetDesc ),
TO_UTF8( refstr ),
(char*) &aObjectsList[ii]->m_PinNum );
2007-09-21 13:23:51 +00:00
print_ter++;
break;
default:
ret |= fprintf( f, " %s %.4s\n",
TO_UTF8( refstr ),
(char*) &aObjectsList[ii]->m_PinNum );
2007-09-21 13:23:51 +00:00
break;
}
aObjectsList[ii]->m_Flag = 1;
2007-09-21 13:23:51 +00:00
}
return ret >= 0;
}