1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 1992-2013 jp.charras at wanadoo.fr
* Copyright (C) 2013-2017 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 1992-2017 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
* 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
*/
#include <build_version.h>
#include <sch_base_frame.h>
#include <class_library.h>
#include <sch_edit_frame.h>
#include <symbol_lib_table.h>
#include "netlist_exporter_generic.h"
static bool sortPinsByNumber( LIB_PIN* aPin1, LIB_PIN* aPin2 );
bool NETLIST_EXPORTER_GENERIC::WriteNetlist( const wxString& aOutFileName, unsigned aNetlistOptions )
{
// Prepare list of nets generation
for( unsigned ii = 0; ii < m_masterList->size(); ii++ )
m_masterList->GetItem( ii )->m_Flag = 0;
// output the XML format netlist.
wxXmlDocument xdoc;
xdoc.SetRoot( makeRoot( GNL_ALL ) );
return xdoc.Save( aOutFileName, 2 /* indent bug, today was ignored by wxXml lib */ );
}
XNODE* NETLIST_EXPORTER_GENERIC::makeRoot( int aCtl )
{
XNODE* xroot = node( "export" );
xroot->AddAttribute( "version", "D" );
if( aCtl & GNL_HEADER )
// add the "design" header
xroot->AddChild( makeDesignHeader() );
if( aCtl & GNL_COMPONENTS )
xroot->AddChild( makeComponents() );
if( aCtl & GNL_PARTS )
xroot->AddChild( makeLibParts() );
if( aCtl & GNL_LIBRARIES )
// must follow makeGenericLibParts()
xroot->AddChild( makeLibraries() );
if( aCtl & GNL_NETS )
xroot->AddChild( makeListOfNets() );
return xroot;
}
/// Holder for multi-unit component fields
struct COMP_FIELDS
{
wxString value;
wxString datasheet;
wxString footprint;
std::map< wxString, wxString > f;
};
void NETLIST_EXPORTER_GENERIC::addComponentFields( XNODE* xcomp, SCH_COMPONENT* comp, SCH_SHEET_PATH* aSheet )
{
COMP_FIELDS fields;
if( comp->GetUnitCount() > 1 )
{
// Sadly, each unit of a component can have its own unique fields. This
// block finds the unit with the lowest number having a non blank field
// value and records it. Therefore user is best off setting fields
// into only the first unit. But this scavenger algorithm will find
// any non blank fields in all units and use the first non-blank field
// for each unique field name.
wxString ref = comp->GetRef( aSheet );
SCH_SHEET_LIST sheetList( g_RootSheet );
int minUnit = comp->GetUnit();
for( unsigned i = 0; i < sheetList.size(); i++ )
{
for( EDA_ITEM* item = sheetList[i].LastDrawList(); item; item = item->Next() )
{
if( item->Type() != SCH_COMPONENT_T )
continue;
SCH_COMPONENT* comp2 = (SCH_COMPONENT*) item;
wxString ref2 = comp2->GetRef( &sheetList[i] );
if( ref2.CmpNoCase( ref ) != 0 )
continue;
int unit = comp2->GetUnit();
// The lowest unit number wins. User should only set fields in any one unit.
// remark: IsVoid() returns true for empty strings or the "~" string (empty field value)
if( !comp2->GetField( VALUE )->IsVoid()
&& ( unit < minUnit || fields.value.IsEmpty() ) )
fields.value = comp2->GetField( VALUE )->GetText();
if( !comp2->GetField( FOOTPRINT )->IsVoid()
&& ( unit < minUnit || fields.footprint.IsEmpty() ) )
fields.footprint = comp2->GetField( FOOTPRINT )->GetText();
if( !comp2->GetField( DATASHEET )->IsVoid()
&& ( unit < minUnit || fields.datasheet.IsEmpty() ) )
fields.datasheet = comp2->GetField( DATASHEET )->GetText();
for( int fldNdx = MANDATORY_FIELDS; fldNdx < comp2->GetFieldCount(); ++fldNdx )
{
SCH_FIELD* f = comp2->GetField( fldNdx );
if( f->GetText().size()
&& ( unit < minUnit || fields.f.count( f->GetName() ) == 0 ) )
{
fields.f[ f->GetName() ] = f->GetText();
}
}
minUnit = std::min( unit, minUnit );
}
}
}
else
{
fields.value = comp->GetField( VALUE )->GetText();
fields.footprint = comp->GetField( FOOTPRINT )->GetText();
fields.datasheet = comp->GetField( DATASHEET )->GetText();
for( int fldNdx = MANDATORY_FIELDS; fldNdx < comp->GetFieldCount(); ++fldNdx )
{
SCH_FIELD* f = comp->GetField( fldNdx );
if( f->GetText().size() )
fields.f[ f->GetName() ] = f->GetText();
}
}
// Do not output field values blank in netlist:
if( fields.value.size() )
xcomp->AddChild( node( "value", fields.value ) );
else // value field always written in netlist
xcomp->AddChild( node( "value", "~" ) );
if( fields.footprint.size() )
xcomp->AddChild( node( "footprint", fields.footprint ) );
if( fields.datasheet.size() )
xcomp->AddChild( node( "datasheet", fields.datasheet ) );
if( fields.f.size() )
{
XNODE* xfields;
xcomp->AddChild( xfields = node( "fields" ) );
// non MANDATORY fields are output alphabetically
for( std::map< wxString, wxString >::const_iterator it = fields.f.begin();
it != fields.f.end(); ++it )
{
XNODE* xfield;
xfields->AddChild( xfield = node( "field", it->second ) );
xfield->AddAttribute( "name", it->first );
}
}
}
XNODE* NETLIST_EXPORTER_GENERIC::makeComponents()
{
XNODE* xcomps = node( "components" );
wxString timeStamp;
m_ReferencesAlreadyFound.Clear();
SCH_SHEET_LIST sheetList( g_RootSheet );
// 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( unsigned i = 0; i < sheetList.size(); i++ )
{
for( EDA_ITEM* schItem = sheetList[i].LastDrawList(); schItem; schItem = schItem->Next() )
{
SCH_COMPONENT* comp = findNextComponent( schItem, &sheetList[i] );
if( !comp )
break; // No component left
schItem = comp;
XNODE* xcomp; // current component being constructed
// Output the component's elements in order of expected access frequency.
// 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( "comp" ) );
xcomp->AddAttribute( "ref", comp->GetRef( &sheetList[i] ) );
addComponentFields( xcomp, comp, &sheetList[i] );
XNODE* xlibsource;
xcomp->AddChild( xlibsource = node( "libsource" ) );
// "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.
PART_SPTR part = comp->GetPartRef().lock();
if( part )
xlibsource->AddAttribute( "lib", part->GetLibId().GetLibNickname() );
// We only want the symbol name, not the full LIB_ID.
xlibsource->AddAttribute( "part", comp->GetLibId().GetLibItemName() );
xlibsource->AddAttribute( "description", comp->GetAliasDescription() );
XNODE* xsheetpath;
xcomp->AddChild( xsheetpath = node( "sheetpath" ) );
xsheetpath->AddAttribute( "names", sheetList[i].PathHumanReadable() );
xsheetpath->AddAttribute( "tstamps", sheetList[i].Path() );
timeStamp.Printf( "%8.8lX", (unsigned long)comp->GetTimeStamp() );
xcomp->AddChild( node( "tstamp", timeStamp ) );
}
}
return xcomps;
}
XNODE* NETLIST_EXPORTER_GENERIC::makeDesignHeader()
{
SCH_SCREEN* screen;
XNODE* xdesign = node( "design" );
XNODE* xtitleBlock;
XNODE* xsheet;
XNODE* xcomment;
wxString sheetTxt;
wxFileName sourceFileName;
// the root sheet is a special sheet, call it source
xdesign->AddChild( node( "source", g_RootSheet->GetScreen()->GetFileName() ) );
xdesign->AddChild( node( "date", DateAndTime() ) );
// which Eeschema tool
xdesign->AddChild( node( "tool", wxString( "Eeschema " ) + GetBuildVersion() ) );
/*
Export the sheets information
*/
SCH_SHEET_LIST sheetList( g_RootSheet );
for( unsigned i = 0; i < sheetList.size(); i++ )
{
screen = sheetList[i].LastScreen();
xdesign->AddChild( xsheet = node( "sheet" ) );
// get the string representation of the sheet index number.
// Note that sheet->GetIndex() is zero index base and we need to increment the
// number by one to make it human readable
sheetTxt.Printf( "%u", i + 1 );
xsheet->AddAttribute( "number", sheetTxt );
xsheet->AddAttribute( "name", sheetList[i].PathHumanReadable() );
xsheet->AddAttribute( "tstamps", sheetList[i].Path() );
TITLE_BLOCK tb = screen->GetTitleBlock();
xsheet->AddChild( xtitleBlock = node( "title_block" ) );
xtitleBlock->AddChild( node( "title", tb.GetTitle() ) );
xtitleBlock->AddChild( node( "company", tb.GetCompany() ) );
xtitleBlock->AddChild( node( "rev", tb.GetRevision() ) );
xtitleBlock->AddChild( node( "date", tb.GetDate() ) );
// We are going to remove the fileName directories.
sourceFileName = wxFileName( screen->GetFileName() );
xtitleBlock->AddChild( node( "source", sourceFileName.GetFullName() ) );
xtitleBlock->AddChild( xcomment = node( "comment" ) );
xcomment->AddAttribute( "number", "1" );
xcomment->AddAttribute( "value", tb.GetComment1() );
xtitleBlock->AddChild( xcomment = node( "comment" ) );
xcomment->AddAttribute( "number", "2" );
xcomment->AddAttribute( "value", tb.GetComment2() );
xtitleBlock->AddChild( xcomment = node( "comment" ) );
xcomment->AddAttribute( "number", "3" );
xcomment->AddAttribute( "value", tb.GetComment3() );
xtitleBlock->AddChild( xcomment = node( "comment" ) );
xcomment->AddAttribute( "number", "4" );
xcomment->AddAttribute( "value", tb.GetComment4() );
}
return xdesign;
}
XNODE* NETLIST_EXPORTER_GENERIC::makeLibraries()
{
XNODE* xlibs = node( "libraries" ); // auto_ptr
for( std::set<wxString>::iterator it = m_libraries.begin(); it!=m_libraries.end(); ++it )
{
wxString libNickname = *it;
XNODE* xlibrary;
if( m_libTable->HasLibrary( libNickname ) )
{
xlibs->AddChild( xlibrary = node( "library" ) );
xlibrary->AddAttribute( "logical", libNickname );
xlibrary->AddChild( node( "uri", m_libTable->GetFullURI( libNickname ) ) );
}
// @todo: add more fun stuff here
}
return xlibs;
}
XNODE* NETLIST_EXPORTER_GENERIC::makeLibParts()
{
XNODE* xlibparts = node( "libparts" ); // auto_ptr
LIB_PINS pinList;
LIB_FIELDS fieldList;
m_libraries.clear();
for( std::set<LIB_PART*>::iterator it = m_LibParts.begin(); it!=m_LibParts.end(); ++it )
{
LIB_PART* lcomp = *it;
wxString libNickname = lcomp->GetLibId().GetLibNickname();;
// The library nickname will be empty if the cache library is used.
if( !libNickname.IsEmpty() )
m_libraries.insert( libNickname ); // inserts component's library if unique
XNODE* xlibpart;
xlibparts->AddChild( xlibpart = node( "libpart" ) );
xlibpart->AddAttribute( "lib", libNickname );
xlibpart->AddAttribute( "part", lcomp->GetName() );
if( lcomp->GetAliasCount() )
{
wxArrayString aliases = lcomp->GetAliasNames( false );
if( aliases.GetCount() )
{
XNODE* xaliases = node( "aliases" );
xlibpart->AddChild( xaliases );
for( unsigned i=0; i<aliases.GetCount(); ++i )
{
xaliases->AddChild( node( "alias", aliases[i] ) );
}
}
}
//----- show the important properties -------------------------
if( !lcomp->GetAlias( 0 )->GetDescription().IsEmpty() )
xlibpart->AddChild( node( "description", lcomp->GetAlias( 0 )->GetDescription() ) );
if( !lcomp->GetAlias( 0 )->GetDocFileName().IsEmpty() )
xlibpart->AddChild( node( "docs", lcomp->GetAlias( 0 )->GetDocFileName() ) );
// Write the footprint list
if( lcomp->GetFootprints().GetCount() )
{
XNODE* xfootprints;
xlibpart->AddChild( xfootprints = node( "footprints" ) );
for( unsigned i=0; i<lcomp->GetFootprints().GetCount(); ++i )
{
xfootprints->AddChild( node( "fp", lcomp->GetFootprints()[i] ) );
}
}
//----- show the fields here ----------------------------------
fieldList.clear();
lcomp->GetFields( fieldList );
XNODE* xfields;
xlibpart->AddChild( xfields = node( "fields" ) );
for( unsigned i=0; i<fieldList.size(); ++i )
{
if( !fieldList[i].GetText().IsEmpty() )
{
XNODE* xfield;
xfields->AddChild( xfield = node( "field", fieldList[i].GetText() ) );
xfield->AddAttribute( "name", fieldList[i].GetName(false) );
}
}
//----- show the pins here ------------------------------------
pinList.clear();
lcomp->GetPins( pinList, 0, 0 );
/* 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.
*/
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--;
}
}
if( pinList.size() )
{
XNODE* pins;
xlibpart->AddChild( pins = node( "pins" ) );
for( unsigned i=0; i<pinList.size(); ++i )
{
XNODE* pin;
pins->AddChild( pin = node( "pin" ) );
pin->AddAttribute( "num", pinList[i]->GetNumber() );
pin->AddAttribute( "name", pinList[i]->GetName() );
pin->AddAttribute( "type", pinList[i]->GetCanonicalElectricalTypeName() );
// caution: construction work site here, drive slowly
}
}
}
return xlibparts;
}
XNODE* NETLIST_EXPORTER_GENERIC::makeListOfNets()
{
XNODE* xnets = node( "nets" ); // auto_ptr if exceptions ever get used.
wxString netCodeTxt;
wxString netName;
wxString ref;
XNODE* xnet = 0;
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 < m_masterList->size(); ii++ )
{
NETLIST_OBJECT* nitem = m_masterList->GetItem( ii );
SCH_COMPONENT* comp;
// New net found, write net id;
if( ( netCode = nitem->GetNet() ) != lastNetCode )
{
sameNetcodeCount = 0; // item count for this net
netName = nitem->GetNetName();
lastNetCode = netCode;
}
if( nitem->m_Type != NET_PIN )
continue;
if( nitem->m_Flag != 0 ) // Redundant pin, skip it
continue;
comp = nitem->GetComponentParent();
// Get the reference for the net name and the main parent component
ref = comp->GetRef( &nitem->m_SheetPath );
if( ref[0] == wxChar( '#' ) )
continue;
if( ++sameNetcodeCount == 1 )
{
xnets->AddChild( xnet = node( "net" ) );
netCodeTxt.Printf( "%d", netCode );
xnet->AddAttribute( "code", netCodeTxt );
xnet->AddAttribute( "name", netName );
}
XNODE* xnode;
xnet->AddChild( xnode = node( "node" ) );
xnode->AddAttribute( "ref", ref );
xnode->AddAttribute( "pin", nitem->GetPinNumText() );
}
return xnets;
}
XNODE* NETLIST_EXPORTER_GENERIC::node( const wxString& aName, const wxString& aTextualContent /* = wxEmptyString*/ )
{
XNODE* n = new XNODE( wxXML_ELEMENT_NODE, aName );
if( aTextualContent.Len() > 0 ) // excludes wxEmptyString, the parameter's default value
n->AddChild( new XNODE( wxXML_TEXT_NODE, wxEmptyString, aTextualContent ) );
return n;
}
static bool sortPinsByNumber( LIB_PIN* aPin1, LIB_PIN* aPin2 )
{
// return "lhs < rhs"
return RefDesStringCompare( aPin1->GetNumber(), aPin2->GetNumber() ) < 0;
}
|