KiCad PCB EDA Suite
Loading...
Searching...
No Matches
netlist_exporter_xml.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 1992-2013 jp.charras at wanadoo.fr
5 * Copyright (C) 2013-2017 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
27
28#include <build_version.h>
29#include <common.h> // for ExpandTextVars
30#include <sch_base_frame.h>
31#include <symbol_library.h>
32#include <string_utils.h>
33#include <connection_graph.h>
34#include <core/kicad_algo.h>
35#include <wx/wfstream.h>
36#include <xnode.h> // also nests: <wx/xml/xml.h>
37
38#include <symbol_lib_table.h>
39
40#include <set>
41
42static bool sortPinsByNumber( LIB_PIN* aPin1, LIB_PIN* aPin2 );
43
44bool NETLIST_EXPORTER_XML::WriteNetlist( const wxString& aOutFileName, unsigned aNetlistOptions,
45 REPORTER& aReporter )
46{
47 // output the XML format netlist.
48
49 // declare the stream ourselves to use the buffered FILE api
50 // instead of letting wx use the syscall variant
51 wxFFileOutputStream stream( aOutFileName );
52
53 if( !stream.IsOk() )
54 return false;
55
56 wxXmlDocument xdoc;
57 xdoc.SetRoot( makeRoot( GNL_ALL | aNetlistOptions ) );
58
59 return xdoc.Save( stream, 2 /* indent bug, today was ignored by wxXml lib */ );
60}
61
62
64{
65 XNODE* xroot = node( wxT( "export" ) );
66
67 xroot->AddAttribute( wxT( "version" ), wxT( "E" ) );
68
69 if( aCtl & GNL_HEADER )
70 // add the "design" header
71 xroot->AddChild( makeDesignHeader() );
72
73 if( aCtl & GNL_SYMBOLS )
74 xroot->AddChild( makeSymbols( aCtl ) );
75
76 if( aCtl & GNL_PARTS )
77 xroot->AddChild( makeLibParts() );
78
79 if( aCtl & GNL_LIBRARIES )
80 // must follow makeGenericLibParts()
81 xroot->AddChild( makeLibraries() );
82
83 if( aCtl & GNL_NETS )
84 xroot->AddChild( makeListOfNets( aCtl ) );
85
86 return xroot;
87}
88
89
91
92
94 SCH_SHEET_PATH* aSheet )
95{
96 wxString value;
97 wxString datasheet;
98 wxString footprint;
99 wxString candidate;
100 std::map<wxString, wxString> userFields;
101
102 if( aSymbol->GetUnitCount() > 1 )
103 {
104 // Sadly, each unit of a symbol can have its own unique fields. This
105 // block finds the unit with the lowest number having a non blank field
106 // value and records it. Therefore user is best off setting fields
107 // into only the first unit. But this scavenger algorithm will find
108 // any non blank fields in all units and use the first non-blank field
109 // for each unique field name.
110
111 wxString ref = aSymbol->GetRef( aSheet );
112
113 SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
114 int minUnit = aSymbol->GetUnitSelection( aSheet );
115
116 for( unsigned i = 0; i < sheetList.size(); i++ )
117 {
118 for( SCH_ITEM* item : sheetList[i].LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
119 {
120 SCH_SYMBOL* symbol2 = (SCH_SYMBOL*) item;
121
122 wxString ref2 = symbol2->GetRef( &sheetList[i] );
123
124 if( ref2.CmpNoCase( ref ) != 0 )
125 continue;
126
127 int unit = symbol2->GetUnitSelection( aSheet );
128
129 // The lowest unit number wins. User should only set fields in any one unit.
130 candidate = symbol2->GetValueFieldText( m_resolveTextVars, &sheetList[i], false );
131
132 if( !candidate.IsEmpty() && ( unit < minUnit || value.IsEmpty() ) )
133 value = candidate;
134
135 candidate = symbol2->GetFootprintFieldText( m_resolveTextVars, &sheetList[i], false );
136
137 if( !candidate.IsEmpty() && ( unit < minUnit || footprint.IsEmpty() ) )
138 footprint = candidate;
139
140 candidate = m_resolveTextVars
141 ? symbol2->GetField( DATASHEET_FIELD )->GetShownText( &sheetList[i], false )
142 : symbol2->GetField( DATASHEET_FIELD )->GetText();
143
144 if( !candidate.IsEmpty() && ( unit < minUnit || datasheet.IsEmpty() ) )
145 datasheet = candidate;
146
147 for( int ii = MANDATORY_FIELDS; ii < symbol2->GetFieldCount(); ++ii )
148 {
149 const SCH_FIELD& f = symbol2->GetFields()[ ii ];
150
151 if( f.GetText().size()
152 && ( unit < minUnit || userFields.count( f.GetName() ) == 0 ) )
153 {
155 userFields[ f.GetName() ] = f.GetShownText( aSheet, false );
156 else
157 userFields[ f.GetName() ] = f.GetText();
158 }
159 }
160
161 minUnit = std::min( unit, minUnit );
162 }
163 }
164 }
165 else
166 {
167 value = aSymbol->GetValueFieldText( m_resolveTextVars, aSheet, false );
168 footprint = aSymbol->GetFootprintFieldText( m_resolveTextVars, aSheet, false );
169
171 datasheet = aSymbol->GetField( DATASHEET_FIELD )->GetShownText( aSheet, false );
172 else
173 datasheet = aSymbol->GetField( DATASHEET_FIELD )->GetText();
174
175 for( int ii = MANDATORY_FIELDS; ii < aSymbol->GetFieldCount(); ++ii )
176 {
177 const SCH_FIELD& f = aSymbol->GetFields()[ ii ];
178
179 if( f.GetText().size() )
180 {
182 userFields[ f.GetName() ] = f.GetShownText( aSheet, false );
183 else
184 userFields[ f.GetName() ] = f.GetText();
185 }
186 }
187 }
188
189 // Do not output field values blank in netlist:
190 if( value.size() )
191 aNode->AddChild( node( wxT( "value" ), UnescapeString( value ) ) );
192 else // value field always written in netlist
193 aNode->AddChild( node( wxT( "value" ), wxT( "~" ) ) );
194
195 if( footprint.size() )
196 aNode->AddChild( node( wxT( "footprint" ), UnescapeString( footprint ) ) );
197
198 if( datasheet.size() )
199 aNode->AddChild( node( wxT( "datasheet" ), UnescapeString( datasheet ) ) );
200
201 if( userFields.size() )
202 {
203 XNODE* xfields;
204 aNode->AddChild( xfields = node( wxT( "fields" ) ) );
205
206 // non MANDATORY fields are output alphabetically
207 for( const std::pair<const wxString, wxString>& f : userFields )
208 {
209 XNODE* xfield = node( wxT( "field" ), UnescapeString( f.second ) );
210 xfield->AddAttribute( wxT( "name" ), UnescapeString( f.first ) );
211 xfields->AddChild( xfield );
212 }
213 }
214}
215
216
218{
219 XNODE* xcomps = node( wxT( "components" ) );
220
222 m_libParts.clear();
223
224 SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
225 SCH_SHEET_PATH currentSheet = m_schematic->CurrentSheet();
226
227 // Output is xml, so there is no reason to remove spaces from the field values.
228 // And XML element names need not be translated to various languages.
229
230 for( unsigned ii = 0; ii < sheetList.size(); ii++ )
231 {
232 SCH_SHEET_PATH sheet = sheetList[ii];
233
234 // Change schematic CurrentSheet in each iteration to allow hierarchical
235 // resolution of text variables in sheet fields.
237
238 auto cmp = [sheet]( SCH_SYMBOL* a, SCH_SYMBOL* b )
239 {
240 return ( StrNumCmp( a->GetRef( &sheet, false ),
241 b->GetRef( &sheet, false ), true ) < 0 );
242 };
243
244 std::set<SCH_SYMBOL*, decltype( cmp )> ordered_symbols( cmp );
245 std::multiset<SCH_SYMBOL*, decltype( cmp )> extra_units( cmp );
246
247 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
248 {
249 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
250 auto test = ordered_symbols.insert( symbol );
251
252 if( !test.second )
253 {
254 if( ( *( test.first ) )->m_Uuid > symbol->m_Uuid )
255 {
256 extra_units.insert( *( test.first ) );
257 ordered_symbols.erase( test.first );
258 ordered_symbols.insert( symbol );
259 }
260 else
261 {
262 extra_units.insert( symbol );
263 }
264 }
265 }
266
267 for( EDA_ITEM* item : ordered_symbols )
268 {
269 SCH_SYMBOL* symbol = findNextSymbol( item, &sheet );
270
271 if( !symbol
272 || ( ( aCtl & GNL_OPT_BOM ) && !symbol->GetIncludeInBom() )
273 || ( ( aCtl & GNL_OPT_KICAD ) && !symbol->GetIncludeOnBoard() ) )
274 {
275 continue;
276 }
277
278 // Output the symbol's elements in order of expected access frequency. This may
279 // not always look best, but it will allow faster execution under XSL processing
280 // systems which do sequential searching within an element.
281
282 XNODE* xcomp; // current symbol being constructed
283 xcomps->AddChild( xcomp = node( wxT( "comp" ) ) );
284
285 xcomp->AddAttribute( wxT( "ref" ), symbol->GetRef( &sheet ) );
286 addSymbolFields( xcomp, symbol, &sheet );
287
288 XNODE* xlibsource;
289 xcomp->AddChild( xlibsource = node( wxT( "libsource" ) ) );
290
291 // "logical" library name, which is in anticipation of a better search algorithm
292 // for parts based on "logical_lib.part" and where logical_lib is merely the library
293 // name minus path and extension.
294 wxString libName;
295 wxString partName;
296
297 if( symbol->UseLibIdLookup() )
298 {
299 libName = symbol->GetLibId().GetLibNickname();
300 partName = symbol->GetLibId().GetLibItemName();
301 }
302 else
303 {
304 partName = symbol->GetSchSymbolLibraryName();
305 }
306
307 xlibsource->AddAttribute( wxT( "lib" ), libName );
308
309 // We only want the symbol name, not the full LIB_ID.
310 xlibsource->AddAttribute( wxT( "part" ), partName );
311
312 xlibsource->AddAttribute( wxT( "description" ), symbol->GetDescription() );
313
314 XNODE* xproperty;
315
316 std::vector<SCH_FIELD>& fields = symbol->GetFields();
317
318 for( size_t jj = MANDATORY_FIELDS; jj < fields.size(); ++jj )
319 {
320 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
321 xproperty->AddAttribute( wxT( "name" ), fields[jj].GetCanonicalName() );
322
324 xproperty->AddAttribute( wxT( "value" ), fields[jj].GetShownText( &sheet, false ) );
325 else
326 xproperty->AddAttribute( wxT( "value" ), fields[jj].GetText() );
327 }
328
329 for( const SCH_FIELD& sheetField : sheet.Last()->GetFields() )
330 {
331 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
332 xproperty->AddAttribute( wxT( "name" ), sheetField.GetCanonicalName() );
333
335 // do not allow GetShownText() to add any prefix useful only when displaying
336 // the field on screen
337 xproperty->AddAttribute( wxT( "value" ), sheetField.GetShownText( &sheet, false ) );
338 else
339 xproperty->AddAttribute( wxT( "value" ), sheetField.GetText() );
340 }
341
342 if( !symbol->GetIncludeInBom() )
343 {
344 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
345 xproperty->AddAttribute( wxT( "name" ), wxT( "exclude_from_bom" ) );
346 }
347
348 if( !symbol->GetIncludeOnBoard() )
349 {
350 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
351 xproperty->AddAttribute( wxT( "name" ), wxT( "exclude_from_board" ) );
352 }
353
354 if( symbol->GetDNP() )
355 {
356 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
357 xproperty->AddAttribute( wxT( "name" ), wxT( "dnp" ) );
358 }
359
360 if( const std::unique_ptr<LIB_SYMBOL>& part = symbol->GetLibSymbolRef() )
361 {
362 if( part->GetDescription().size() )
363 {
364 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
365 xproperty->AddAttribute( wxT( "name" ), wxT( "ki_description" ) );
366 xproperty->AddAttribute( wxT( "value" ), part->GetDescription() );
367 }
368
369 if( part->GetKeyWords().size() )
370 {
371 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
372 xproperty->AddAttribute( wxT( "name" ), wxT( "ki_keywords" ) );
373 xproperty->AddAttribute( wxT( "value" ), part->GetKeyWords() );
374 }
375 }
376
377 XNODE* xsheetpath;
378 xcomp->AddChild( xsheetpath = node( wxT( "sheetpath" ) ) );
379
380 xsheetpath->AddAttribute( wxT( "names" ), sheet.PathHumanReadable() );
381 xsheetpath->AddAttribute( wxT( "tstamps" ), sheet.PathAsString() );
382
383 XNODE* xunits; // Node for extra units
384 xcomp->AddChild( xunits = node( wxT( "tstamps" ) ) );
385
386 auto range = extra_units.equal_range( symbol );
387 wxString uuid;
388
389 // Output a series of children with all UUIDs associated with the REFDES
390 for( auto it = range.first; it != range.second; ++it )
391 {
392 uuid = ( *it )->m_Uuid.AsString();
393
394 // Add a space between UUIDs, if not in KICAD mode (i.e.
395 // using wxXmlDocument::Save()). KICAD MODE has its own XNODE::Format function.
396 if( !( aCtl & GNL_OPT_KICAD ) ) // i.e. for .xml format
397 uuid += ' ';
398
399 xunits->AddChild( new XNODE( wxXML_TEXT_NODE, wxEmptyString, uuid ) );
400 }
401
402 // Output the primary UUID
403 uuid = symbol->m_Uuid.AsString();
404 xunits->AddChild( new XNODE( wxXML_TEXT_NODE, wxEmptyString, uuid ) );
405 }
406 }
407
408 m_schematic->SetCurrentSheet( currentSheet );
409
410 return xcomps;
411}
412
413
415{
416 SCH_SCREEN* screen;
417 XNODE* xdesign = node( wxT( "design" ) );
418 XNODE* xtitleBlock;
419 XNODE* xsheet;
420 XNODE* xcomment;
421 XNODE* xtextvar;
422 wxString sheetTxt;
423 wxFileName sourceFileName;
424
425 // the root sheet is a special sheet, call it source
426 xdesign->AddChild( node( wxT( "source" ), m_schematic->GetFileName() ) );
427
428 xdesign->AddChild( node( wxT( "date" ), DateAndTime() ) );
429
430 // which Eeschema tool
431 xdesign->AddChild( node( wxT( "tool" ), wxT( "Eeschema " ) + GetBuildVersion() ) );
432
433 const std::map<wxString, wxString>& properties = m_schematic->Prj().GetTextVars();
434
435 for( const std::pair<const wxString, wxString>& prop : properties )
436 {
437 xdesign->AddChild( xtextvar = node( wxT( "textvar" ), prop.second ) );
438 xtextvar->AddAttribute( wxT( "name" ), prop.first );
439 }
440
441 /*
442 * Export the sheets information
443 */
444 SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
445
446 for( unsigned i = 0; i < sheetList.size(); i++ )
447 {
448 screen = sheetList[i].LastScreen();
449
450 xdesign->AddChild( xsheet = node( wxT( "sheet" ) ) );
451
452 // get the string representation of the sheet index number.
453 // Note that sheet->GetIndex() is zero index base and we need to increment the
454 // number by one to make it human readable
455 sheetTxt.Printf( wxT( "%u" ), i + 1 );
456 xsheet->AddAttribute( wxT( "number" ), sheetTxt );
457 xsheet->AddAttribute( wxT( "name" ), sheetList[i].PathHumanReadable() );
458 xsheet->AddAttribute( wxT( "tstamps" ), sheetList[i].PathAsString() );
459
460 TITLE_BLOCK tb = screen->GetTitleBlock();
461 PROJECT* prj = &m_schematic->Prj();
462
463 xsheet->AddChild( xtitleBlock = node( wxT( "title_block" ) ) );
464
465 xtitleBlock->AddChild( node( wxT( "title" ), ExpandTextVars( tb.GetTitle(), prj ) ) );
466 xtitleBlock->AddChild( node( wxT( "company" ), ExpandTextVars( tb.GetCompany(), prj ) ) );
467 xtitleBlock->AddChild( node( wxT( "rev" ), ExpandTextVars( tb.GetRevision(), prj ) ) );
468 xtitleBlock->AddChild( node( wxT( "date" ), ExpandTextVars( tb.GetDate(), prj ) ) );
469
470 // We are going to remove the fileName directories.
471 sourceFileName = wxFileName( screen->GetFileName() );
472 xtitleBlock->AddChild( node( wxT( "source" ), sourceFileName.GetFullName() ) );
473
474 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
475 xcomment->AddAttribute( wxT( "number" ), wxT( "1" ) );
476 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 0 ), prj ) );
477
478 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
479 xcomment->AddAttribute( wxT( "number" ), wxT( "2" ) );
480 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 1 ), prj ) );
481
482 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
483 xcomment->AddAttribute( wxT( "number" ), wxT( "3" ) );
484 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 2 ), prj ) );
485
486 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
487 xcomment->AddAttribute( wxT( "number" ), wxT( "4" ) );
488 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 3 ), prj ) );
489
490 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
491 xcomment->AddAttribute( wxT( "number" ), wxT( "5" ) );
492 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 4 ), prj ) );
493
494 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
495 xcomment->AddAttribute( wxT( "number" ), wxT( "6" ) );
496 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 5 ), prj ) );
497
498 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
499 xcomment->AddAttribute( wxT( "number" ), wxT( "7" ) );
500 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 6 ), prj ) );
501
502 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
503 xcomment->AddAttribute( wxT( "number" ), wxT( "8" ) );
504 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 7 ), prj ) );
505
506 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
507 xcomment->AddAttribute( wxT( "number" ), wxT( "9" ) );
508 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 8 ), prj ) );
509 }
510
511 return xdesign;
512}
513
514
516{
517 XNODE* xlibs = node( wxT( "libraries" ) ); // auto_ptr
518 SYMBOL_LIB_TABLE* symbolLibTable = m_schematic->Prj().SchSymbolLibTable();
519
520 for( std::set<wxString>::iterator it = m_libraries.begin(); it!=m_libraries.end(); ++it )
521 {
522 wxString libNickname = *it;
523 XNODE* xlibrary;
524
525 if( symbolLibTable->HasLibrary( libNickname ) )
526 {
527 xlibs->AddChild( xlibrary = node( wxT( "library" ) ) );
528 xlibrary->AddAttribute( wxT( "logical" ), libNickname );
529 xlibrary->AddChild( node( wxT( "uri" ), symbolLibTable->GetFullURI( libNickname ) ) );
530 }
531
532 // @todo: add more fun stuff here
533 }
534
535 return xlibs;
536}
537
538
540{
541 XNODE* xlibparts = node( wxT( "libparts" ) ); // auto_ptr
542
543 LIB_PINS pinList;
544 std::vector<LIB_FIELD*> fieldList;
545
546 m_libraries.clear();
547
548 for( LIB_SYMBOL* lcomp : m_libParts )
549 {
550 wxString libNickname = lcomp->GetLibId().GetLibNickname();;
551
552 // The library nickname will be empty if the cache library is used.
553 if( !libNickname.IsEmpty() )
554 m_libraries.insert( libNickname ); // inserts symbol's library if unique
555
556 XNODE* xlibpart;
557 xlibparts->AddChild( xlibpart = node( wxT( "libpart" ) ) );
558 xlibpart->AddAttribute( wxT( "lib" ), libNickname );
559 xlibpart->AddAttribute( wxT( "part" ), lcomp->GetName() );
560
561 //----- show the important properties -------------------------
562 if( !lcomp->GetDescription().IsEmpty() )
563 xlibpart->AddChild( node( wxT( "description" ), lcomp->GetDescription() ) );
564
565 if( !lcomp->GetDatasheetField().GetText().IsEmpty() )
566 xlibpart->AddChild( node( wxT( "docs" ), lcomp->GetDatasheetField().GetText() ) );
567
568 // Write the footprint list
569 if( lcomp->GetFPFilters().GetCount() )
570 {
571 XNODE* xfootprints;
572 xlibpart->AddChild( xfootprints = node( wxT( "footprints" ) ) );
573
574 for( unsigned i = 0; i < lcomp->GetFPFilters().GetCount(); ++i )
575 xfootprints->AddChild( node( wxT( "fp" ), lcomp->GetFPFilters()[i] ) );
576 }
577
578 //----- show the fields here ----------------------------------
579 fieldList.clear();
580 lcomp->GetFields( fieldList );
581
582 XNODE* xfields;
583 xlibpart->AddChild( xfields = node( "fields" ) );
584
585 for( const LIB_FIELD* field : fieldList )
586 {
587 if( !field->GetText().IsEmpty() )
588 {
589 XNODE* xfield;
590 xfields->AddChild( xfield = node( wxT( "field" ), field->GetText() ) );
591 xfield->AddAttribute( wxT( "name" ), field->GetCanonicalName() );
592 }
593 }
594
595 //----- show the pins here ------------------------------------
596 pinList.clear();
597 lcomp->GetPins( pinList, 0, 0 );
598
599 /*
600 * We must erase redundant Pins references in pinList
601 * These redundant pins exist because some pins are found more than one time when a
602 * symbol has multiple parts per package or has 2 representations (DeMorgan conversion).
603 * For instance, a 74ls00 has DeMorgan conversion, with different pin shapes, and
604 * therefore each pin appears 2 times in the list. Common pins (VCC, GND) can also be
605 * found more than once.
606 */
607 sort( pinList.begin(), pinList.end(), sortPinsByNumber );
608 for( int ii = 0; ii < (int)pinList.size()-1; ii++ )
609 {
610 if( pinList[ii]->GetNumber() == pinList[ii+1]->GetNumber() )
611 { // 2 pins have the same number, remove the redundant pin at index i+1
612 pinList.erase(pinList.begin() + ii + 1);
613 ii--;
614 }
615 }
616
617 if( pinList.size() )
618 {
619 XNODE* pins;
620
621 xlibpart->AddChild( pins = node( wxT( "pins" ) ) );
622 for( unsigned i=0; i<pinList.size(); ++i )
623 {
624 XNODE* pin;
625
626 pins->AddChild( pin = node( wxT( "pin" ) ) );
627 pin->AddAttribute( wxT( "num" ), pinList[i]->GetShownNumber() );
628 pin->AddAttribute( wxT( "name" ), pinList[i]->GetShownName() );
629 pin->AddAttribute( wxT( "type" ), pinList[i]->GetCanonicalElectricalTypeName() );
630
631 // caution: construction work site here, drive slowly
632 }
633 }
634 }
635
636 return xlibparts;
637}
638
639
641{
642 XNODE* xnets = node( wxT( "nets" ) ); // auto_ptr if exceptions ever get used.
643 wxString netCodeTxt;
644 wxString netName;
645 wxString ref;
646
647 XNODE* xnet = nullptr;
648
649 /* output:
650 <net code="123" name="/cfcard.sch/WAIT#">
651 <node ref="R23" pin="1"/>
652 <node ref="U18" pin="12"/>
653 </net>
654 */
655
656 struct NET_NODE
657 {
658 NET_NODE( SCH_PIN* aPin, const SCH_SHEET_PATH& aSheet ) : m_Pin( aPin ), m_Sheet( aSheet )
659 {}
660
661 SCH_PIN* m_Pin;
662 SCH_SHEET_PATH m_Sheet;
663 };
664
665 struct NET_RECORD
666 {
667 NET_RECORD( const wxString& aName ) : m_Name( aName ), m_HasNoConnect( false ){};
668
669 wxString m_Name;
670 bool m_HasNoConnect;
671 std::vector<NET_NODE> m_Nodes;
672 };
673
674 std::vector<NET_RECORD*> nets;
675
676 for( const auto& [ key, subgraphs ] : m_schematic->ConnectionGraph()->GetNetMap() )
677 {
678 wxString net_name = key.Name;
679 NET_RECORD* net_record = nullptr;
680
681 if( subgraphs.empty() )
682 continue;
683
684 nets.emplace_back( new NET_RECORD( net_name ) );
685 net_record = nets.back();
686
687 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
688 {
689 bool nc = subgraph->GetNoConnect() && subgraph->GetNoConnect()->Type() == SCH_NO_CONNECT_T;
690 const SCH_SHEET_PATH& sheet = subgraph->GetSheet();
691
692 if( nc )
693 net_record->m_HasNoConnect = true;
694
695 for( SCH_ITEM* item : subgraph->GetItems() )
696 {
697 if( item->Type() == SCH_PIN_T )
698 {
699 SCH_PIN* pin = static_cast<SCH_PIN*>( item );
700 SCH_SYMBOL* symbol = pin->GetParentSymbol();
701
702 if( !symbol
703 || ( ( aCtl & GNL_OPT_BOM ) && !symbol->GetIncludeInBom() )
704 || ( ( aCtl & GNL_OPT_KICAD ) && !symbol->GetIncludeOnBoard() ) )
705 {
706 continue;
707 }
708
709 net_record->m_Nodes.emplace_back( pin, sheet );
710 }
711 }
712 }
713 }
714
715 // Netlist ordering: Net name, then ref des, then pin name
716 std::sort( nets.begin(), nets.end(),
717 []( const NET_RECORD* a, const NET_RECORD*b )
718 {
719 return StrNumCmp( a->m_Name, b->m_Name ) < 0;
720 } );
721
722 for( int i = 0; i < (int) nets.size(); ++i )
723 {
724 NET_RECORD* net_record = nets[i];
725 bool added = false;
726 XNODE* xnode;
727
728 // Netlist ordering: Net name, then ref des, then pin name
729 std::sort( net_record->m_Nodes.begin(), net_record->m_Nodes.end(),
730 []( const NET_NODE& a, const NET_NODE& b )
731 {
732 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
733 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
734
735 if( refA == refB )
736 return a.m_Pin->GetShownNumber() < b.m_Pin->GetShownNumber();
737
738 return refA < refB;
739 } );
740
741 // Some duplicates can exist, for example on multi-unit parts with duplicated pins across
742 // units. If the user connects the pins on each unit, they will appear on separate
743 // subgraphs. Remove those here:
744 alg::remove_duplicates( net_record->m_Nodes,
745 []( const NET_NODE& a, const NET_NODE& b )
746 {
747 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
748 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
749
750 return refA == refB && a.m_Pin->GetShownNumber() == b.m_Pin->GetShownNumber();
751 } );
752
753 // Determine if all pins in the net are stacked (nets with only one pin are implicitly
754 // taken to be stacked)
755 bool allNetPinsStacked = true;
756
757 if( net_record->m_Nodes.size() > 1 )
758 {
759 SCH_PIN* firstPin = net_record->m_Nodes.begin()->m_Pin;
760 allNetPinsStacked =
761 std::all_of( net_record->m_Nodes.begin() + 1, net_record->m_Nodes.end(),
762 [=]( auto& node )
763 {
764 return firstPin->GetParent() == node.m_Pin->GetParent()
765 && firstPin->GetPosition() == node.m_Pin->GetPosition()
766 && firstPin->GetName() == node.m_Pin->GetName();
767 } );
768 }
769
770 for( const NET_NODE& netNode : net_record->m_Nodes )
771 {
772 wxString refText = netNode.m_Pin->GetParentSymbol()->GetRef( &netNode.m_Sheet );
773 wxString pinText = netNode.m_Pin->GetShownNumber();
774
775 // Skip power symbols and virtual symbols
776 if( refText[0] == wxChar( '#' ) )
777 continue;
778
779 if( !added )
780 {
781 netCodeTxt.Printf( wxT( "%d" ), i + 1 );
782
783 xnets->AddChild( xnet = node( wxT( "net" ) ) );
784 xnet->AddAttribute( wxT( "code" ), netCodeTxt );
785 xnet->AddAttribute( wxT( "name" ), net_record->m_Name );
786
787 added = true;
788 }
789
790 xnet->AddChild( xnode = node( wxT( "node" ) ) );
791 xnode->AddAttribute( wxT( "ref" ), refText );
792 xnode->AddAttribute( wxT( "pin" ), pinText );
793
794 wxString pinName = netNode.m_Pin->GetShownName();
795 wxString pinType = netNode.m_Pin->GetCanonicalElectricalTypeName();
796
797 if( !pinName.IsEmpty() )
798 xnode->AddAttribute( wxT( "pinfunction" ), pinName );
799
800 if( net_record->m_HasNoConnect
801 && ( net_record->m_Nodes.size() == 1 || allNetPinsStacked ) )
802 pinType += wxT( "+no_connect" );
803
804 xnode->AddAttribute( wxT( "pintype" ), pinType );
805 }
806 }
807
808 for( NET_RECORD* record : nets )
809 delete record;
810
811 return xnets;
812}
813
814
815XNODE* NETLIST_EXPORTER_XML::node( const wxString& aName,
816 const wxString& aTextualContent /* = wxEmptyString*/ )
817{
818 XNODE* n = new XNODE( wxXML_ELEMENT_NODE, aName );
819
820 if( aTextualContent.Len() > 0 ) // excludes wxEmptyString, the parameter's default value
821 n->AddChild( new XNODE( wxXML_TEXT_NODE, wxEmptyString, aTextualContent ) );
822
823 return n;
824}
825
826
827static bool sortPinsByNumber( LIB_PIN* aPin1, LIB_PIN* aPin2 )
828{
829 // return "lhs < rhs"
830 return StrNumCmp( aPin1->GetShownNumber(), aPin2->GetShownNumber(), true ) < 0;
831}
wxString GetBuildVersion()
Get the full KiCad version string.
const NET_MAP & GetNetMap() const
A subgraph is a set of items that are electrically connected on a single sheet.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
const KIID m_Uuid
Definition: eda_item.h:475
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:87
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:238
wxString AsString() const
Definition: kiid.cpp:257
Field object used in symbol libraries.
Definition: lib_field.h:61
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
wxString GetShownNumber() const
Definition: lib_pin.h:129
Define a library symbol object.
Definition: lib_symbol.h:99
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
wxString GetFullURI(const wxString &aLibNickname, bool aExpandEnvVars=true) const
Return the full URI of the library mapped to aLibNickname.
std::set< LIB_SYMBOL *, LIB_SYMBOL_LESS_THAN > m_libParts
unique library symbols used. LIB_SYMBOL items are sorted by names
UNIQUE_STRINGS m_referencesAlreadyFound
Used for "multiple symbols per package" symbols to avoid processing a lib symbol more than once.
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
Check if the given symbol should be processed for netlisting.
SCHEMATIC_IFACE * m_schematic
The schematic we're generating a netlist for.
XNODE * makeDesignHeader()
Fill out a project "design" header into an XML node.
XNODE * makeLibraries()
Fill out an XML node with a list of used libraries and returns it.
bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions, REPORTER &aReporter) override
Write generic netlist to aOutFileName.
XNODE * node(const wxString &aName, const wxString &aTextualContent=wxEmptyString)
A convenience function that creates a new XNODE with an optional textual child.
XNODE * makeListOfNets(unsigned aCtl)
Fill out an XML node with a list of nets and returns it.
XNODE * makeSymbols(unsigned aCtl)
XNODE * makeRoot(unsigned aCtl=GNL_ALL)
Build the entire document tree for the generic export.
void addSymbolFields(XNODE *aNode, SCH_SYMBOL *aSymbol, SCH_SHEET_PATH *aSheet)
Holder for multi-unit symbol fields.
std::set< wxString > m_libraries
XNODE * makeLibParts()
Fill out an XML node with the unique library parts and returns it.
Container for project specific data.
Definition: project.h:64
virtual std::map< wxString, wxString > & GetTextVars() const
Definition: project.cpp:82
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
virtual void SetCurrentSheet(const SCH_SHEET_PATH &aPath)=0
virtual wxString GetFileName() const =0
virtual CONNECTION_GRAPH * ConnectionGraph() const =0
virtual SCH_SHEET_LIST GetSheets() const =0
virtual SCH_SHEET_PATH & CurrentSheet() const =0
virtual PROJECT & Prj() const =0
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: sch_field.cpp:815
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition: sch_field.cpp:175
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:147
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:109
const wxString & GetFileName() const
Definition: sch_screen.h:144
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:155
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
const SCH_SHEET * GetSheet(unsigned aIndex) const
wxString PathHumanReadable(bool aUseShortRootName=true, bool aStripTrailingSeparator=false) const
Return the sheet path in a human readable form made from the sheet names.
SCH_SCREEN * LastScreen()
wxString PathAsString() const
Return the path of time stamps which do not changes even when editing sheet parameters.
SCH_SHEET * Last() const
Return a pointer to the last SCH_SHEET of the list.
std::vector< SCH_FIELD > & GetFields()
Definition: sch_sheet.h:93
Schematic symbol object.
Definition: sch_symbol.h:81
int GetUnitCount() const
Return the number of units per package of the symbol.
Definition: sch_symbol.cpp:427
int GetFieldCount() const
Return the number of fields in this symbol.
Definition: sch_symbol.h:500
bool UseLibIdLookup() const
Definition: sch_symbol.h:193
wxString GetSchSymbolLibraryName() const
Definition: sch_symbol.cpp:294
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:698
bool GetIncludeOnBoard() const
Definition: sch_symbol.h:764
bool GetIncludeInBom() const
Definition: sch_symbol.h:761
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:891
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
Definition: sch_symbol.cpp:875
wxString GetDescription() const
Definition: sch_symbol.cpp:312
const wxString GetValueFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
Definition: sch_symbol.cpp:859
int GetUnitSelection(const SCH_SHEET_PATH *aSheet) const
Return the instance-specific unit selection for the given sheet path.
Definition: sch_symbol.cpp:817
const LIB_ID & GetLibId() const
Definition: sch_symbol.h:178
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with SCH_FIELDs.
Definition: sch_symbol.cpp:939
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:195
bool GetDNP() const
Definition: sch_symbol.h:767
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition: title_block.h:41
const wxString & GetCompany() const
Definition: title_block.h:96
const wxString & GetRevision() const
Definition: title_block.h:86
const wxString & GetDate() const
Definition: title_block.h:76
const wxString & GetComment(int aIdx) const
Definition: title_block.h:107
const wxString & GetTitle() const
Definition: title_block.h:63
void Clear()
Erase the record.
Hold an XML or S-expression element.
Definition: xnode.h:44
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject)
Definition: common.cpp:58
The common library.
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:54
void remove_duplicates(_Container &__c)
Deletes all duplicate values from __c.
Definition: kicad_algo.h:182
static bool sortPinsByNumber(LIB_PIN *aPin1, LIB_PIN *aPin2)
#define GNL_ALL
@ GNL_PARTS
@ GNL_LIBRARIES
@ GNL_OPT_KICAD
@ GNL_SYMBOLS
@ GNL_HEADER
@ GNL_OPT_BOM
@ GNL_NETS
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
wxString DateAndTime()
wxString UnescapeString(const wxString &aSource)
Definition for symbol library class.
@ DATASHEET_FIELD
name of datasheet
@ MANDATORY_FIELDS
The first 4 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors.
@ SCH_NO_CONNECT_T
Definition: typeinfo.h:133
@ SCH_SYMBOL_T
Definition: typeinfo.h:146
@ SCH_PIN_T
Definition: typeinfo.h:149