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#include <nlohmann/json.hpp>
38#include <project_sch.h>
39
40#include <symbol_lib_table.h>
41
42#include <set>
43
44static bool sortPinsByNumber( SCH_PIN* aPin1, SCH_PIN* aPin2 );
45
46bool NETLIST_EXPORTER_XML::WriteNetlist( const wxString& aOutFileName, unsigned aNetlistOptions,
47 REPORTER& aReporter )
48{
49 // output the XML format netlist.
50
51 // declare the stream ourselves to use the buffered FILE api
52 // instead of letting wx use the syscall variant
53 wxFFileOutputStream stream( aOutFileName );
54
55 if( !stream.IsOk() )
56 return false;
57
58 wxXmlDocument xdoc;
59 xdoc.SetRoot( makeRoot( GNL_ALL | aNetlistOptions ) );
60
61 return xdoc.Save( stream, 2 /* indent bug, today was ignored by wxXml lib */ );
62}
63
64
66{
67 XNODE* xroot = node( wxT( "export" ) );
68
69 xroot->AddAttribute( wxT( "version" ), wxT( "E" ) );
70
71 if( aCtl & GNL_HEADER )
72 // add the "design" header
73 xroot->AddChild( makeDesignHeader() );
74
75 if( aCtl & GNL_SYMBOLS )
76 xroot->AddChild( makeSymbols( aCtl ) );
77
78 if( aCtl & GNL_PARTS )
79 xroot->AddChild( makeLibParts() );
80
81 if( aCtl & GNL_LIBRARIES )
82 // must follow makeGenericLibParts()
83 xroot->AddChild( makeLibraries() );
84
85 if( aCtl & GNL_NETS )
86 xroot->AddChild( makeListOfNets( aCtl ) );
87
88 return xroot;
89}
90
91
93
94
96 const SCH_SHEET_PATH& aSheet,
97 const SCH_SHEET_LIST& aSheetList )
98{
99 wxString value;
100 wxString footprint;
101 wxString datasheet;
102 wxString description;
103 wxString candidate;
104 nlohmann::ordered_map<wxString, wxString> fields;
105
106 if( aSymbol->GetUnitCount() > 1 )
107 {
108 // Sadly, each unit of a symbol can have its own unique fields. This
109 // block finds the unit with the lowest number having a non blank field
110 // value and records it. Therefore user is best off setting fields
111 // into only the first unit. But this scavenger algorithm will find
112 // any non blank fields in all units and use the first non-blank field
113 // for each unique field name.
114
115 wxString ref = aSymbol->GetRef( &aSheet );
116
117 int minUnit = aSymbol->GetUnitSelection( &aSheet );
118
119 for( const SCH_SHEET_PATH& sheet : aSheetList )
120 {
121 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
122 {
123 SCH_SYMBOL* symbol2 = static_cast<SCH_SYMBOL*>( item );
124
125 wxString ref2 = symbol2->GetRef( &sheet );
126
127 if( ref2.CmpNoCase( ref ) != 0 )
128 continue;
129
130 int unit = symbol2->GetUnitSelection( &aSheet );
131
132 // The lowest unit number wins. User should only set fields in any one unit.
133
134 // Value
135 candidate = symbol2->GetValue( m_resolveTextVars, &sheet, false );
136
137 if( !candidate.IsEmpty() && ( unit < minUnit || value.IsEmpty() ) )
138 value = candidate;
139
140 // Footprint
141 candidate = symbol2->GetFootprintFieldText( m_resolveTextVars, &sheet, false );
142
143 if( !candidate.IsEmpty() && ( unit < minUnit || footprint.IsEmpty() ) )
144 footprint = candidate;
145
146 // Datasheet
147 candidate = m_resolveTextVars
148 ? symbol2->GetField( DATASHEET_FIELD )->GetShownText( &sheet, false )
149 : symbol2->GetField( DATASHEET_FIELD )->GetText();
150
151 if( !candidate.IsEmpty() && ( unit < minUnit || datasheet.IsEmpty() ) )
152 datasheet = candidate;
153
154 // Description
155 candidate = m_resolveTextVars
156 ? symbol2->GetField( DESCRIPTION_FIELD )->GetShownText( &sheet, false )
157 : symbol2->GetField( DESCRIPTION_FIELD )->GetText();
158
159 if( !candidate.IsEmpty() && ( unit < minUnit || description.IsEmpty() ) )
160 description = candidate;
161
162 // All non-mandatory fields
163 for( int ii = MANDATORY_FIELDS; ii < symbol2->GetFieldCount(); ++ii )
164 {
165 const SCH_FIELD& f = symbol2->GetFields()[ ii ];
166
167 if( unit < minUnit || fields.count( f.GetName() ) == 0 )
168 {
170 fields[f.GetName()] = f.GetShownText( &aSheet, false );
171 else
172 fields[f.GetName()] = f.GetText();
173 }
174 }
175
176 minUnit = std::min( unit, minUnit );
177 }
178 }
179 }
180 else
181 {
182 value = aSymbol->GetValue( m_resolveTextVars, &aSheet, false );
183 footprint = aSymbol->GetFootprintFieldText( m_resolveTextVars, &aSheet, false );
184
185 SCH_FIELD* datasheetField = aSymbol->GetField( DATASHEET_FIELD );
186 SCH_FIELD* descriptionField = aSymbol->GetField( DESCRIPTION_FIELD );
187
188 // Datasheet
190 datasheet = datasheetField->GetShownText( &aSheet, false );
191 else
192 datasheet = datasheetField->GetText();
193
194 // Description
196 description = descriptionField->GetShownText( &aSheet, false );
197 else
198 description = descriptionField->GetText();
199
200 for( int ii = MANDATORY_FIELDS; ii < aSymbol->GetFieldCount(); ++ii )
201 {
202 const SCH_FIELD& f = aSymbol->GetFields()[ ii ];
203
205 fields[f.GetName()] = f.GetShownText( &aSheet, false );
206 else
207 fields[f.GetName()] = f.GetText();
208 }
209 }
210
211 fields[GetCanonicalFieldName( FOOTPRINT_FIELD )] = footprint;
213 fields[GetCanonicalFieldName( DESCRIPTION_FIELD )] = description;
214
215 // Do not output field values blank in netlist:
216 if( value.size() )
217 aNode->AddChild( node( wxT( "value" ), UnescapeString( value ) ) );
218 else // value field always written in netlist
219 aNode->AddChild( node( wxT( "value" ), wxT( "~" ) ) );
220
221 if( footprint.size() )
222 aNode->AddChild( node( wxT( "footprint" ), UnescapeString( footprint ) ) );
223
224 if( datasheet.size() )
225 aNode->AddChild( node( wxT( "datasheet" ), UnescapeString( datasheet ) ) );
226
227 if( description.size() )
228 aNode->AddChild( node( wxT( "description" ), UnescapeString( description ) ) );
229
230 XNODE* xfields;
231 aNode->AddChild( xfields = node( wxT( "fields" ) ) );
232
233 for( const auto& [ fieldName, fieldValue ] : fields )
234 {
235 XNODE* xfield = node( wxT( "field" ), UnescapeString( fieldValue ) );
236 xfield->AddAttribute( wxT( "name" ), UnescapeString( fieldName ) );
237 xfields->AddChild( xfield );
238 }
239}
240
241
243{
244 XNODE* xcomps = node( wxT( "components" ) );
245
247 m_libParts.clear();
248
249 SCH_SHEET_PATH currentSheet = m_schematic->CurrentSheet();
251
252 // Output is xml, so there is no reason to remove spaces from the field values.
253 // And XML element names need not be translated to various languages.
254
255 for( const SCH_SHEET_PATH& sheet : sheetList )
256 {
257 // Change schematic CurrentSheet in each iteration to allow hierarchical
258 // resolution of text variables in sheet fields.
260
261 auto cmp =
262 [sheet]( SCH_SYMBOL* a, SCH_SYMBOL* b )
263 {
264 return ( StrNumCmp( a->GetRef( &sheet, false ),
265 b->GetRef( &sheet, false ), true ) < 0 );
266 };
267
268 std::set<SCH_SYMBOL*, decltype( cmp )> ordered_symbols( cmp );
269 std::multiset<SCH_SYMBOL*, decltype( cmp )> extra_units( cmp );
270
271 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
272 {
273 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
274 auto test = ordered_symbols.insert( symbol );
275
276 if( !test.second )
277 {
278 if( ( *( test.first ) )->m_Uuid > symbol->m_Uuid )
279 {
280 extra_units.insert( *( test.first ) );
281 ordered_symbols.erase( test.first );
282 ordered_symbols.insert( symbol );
283 }
284 else
285 {
286 extra_units.insert( symbol );
287 }
288 }
289 }
290
291 for( EDA_ITEM* item : ordered_symbols )
292 {
293 SCH_SYMBOL* symbol = findNextSymbol( item, sheet );
294 bool forBOM = aCtl & GNL_OPT_BOM;
295 bool forBoard = aCtl & GNL_OPT_KICAD;
296
297 if( !symbol )
298 continue;
299
300 if( forBOM && ( sheet.GetExcludedFromBOM() || symbol->GetExcludedFromBOM() ) )
301 continue;
302
303 if( forBoard && ( sheet.GetExcludedFromBoard() || symbol->GetExcludedFromBoard() ) )
304 continue;
305
306 // Output the symbol's elements in order of expected access frequency. This may
307 // not always look best, but it will allow faster execution under XSL processing
308 // systems which do sequential searching within an element.
309
310 XNODE* xcomp; // current symbol being constructed
311 xcomps->AddChild( xcomp = node( wxT( "comp" ) ) );
312
313 xcomp->AddAttribute( wxT( "ref" ), symbol->GetRef( &sheet ) );
314 addSymbolFields( xcomp, symbol, sheet, sheetList );
315
316 XNODE* xlibsource;
317 xcomp->AddChild( xlibsource = node( wxT( "libsource" ) ) );
318
319 // "logical" library name, which is in anticipation of a better search algorithm
320 // for parts based on "logical_lib.part" and where logical_lib is merely the library
321 // name minus path and extension.
322 wxString libName;
323 wxString partName;
324
325 if( symbol->UseLibIdLookup() )
326 {
327 libName = symbol->GetLibId().GetUniStringLibNickname();
328 partName = symbol->GetLibId().GetUniStringLibItemName();
329 }
330 else
331 {
332 partName = symbol->GetSchSymbolLibraryName();
333 }
334
335 xlibsource->AddAttribute( wxT( "lib" ), libName );
336
337 // We only want the symbol name, not the full LIB_ID.
338 xlibsource->AddAttribute( wxT( "part" ), partName );
339
340 xlibsource->AddAttribute( wxT( "description" ), symbol->GetDescription() );
341
342 /* Add the symbol properties. */
343 XNODE* xproperty;
344
345 std::vector<SCH_FIELD>& fields = symbol->GetFields();
346
347 for( size_t jj = MANDATORY_FIELDS; jj < fields.size(); ++jj )
348 {
349 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
350 xproperty->AddAttribute( wxT( "name" ), fields[jj].GetCanonicalName() );
351
353 xproperty->AddAttribute( wxT( "value" ), fields[jj].GetShownText( &sheet, false ) );
354 else
355 xproperty->AddAttribute( wxT( "value" ), fields[jj].GetText() );
356 }
357
358 for( const SCH_FIELD& sheetField : sheet.Last()->GetFields() )
359 {
360 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
361 xproperty->AddAttribute( wxT( "name" ), sheetField.GetCanonicalName() );
362
364 // do not allow GetShownText() to add any prefix useful only when displaying
365 // the field on screen
366 xproperty->AddAttribute( wxT( "value" ), sheetField.GetShownText( &sheet, false ) );
367 else
368 xproperty->AddAttribute( wxT( "value" ), sheetField.GetText() );
369 }
370
371 if( symbol->GetExcludedFromBOM() )
372 {
373 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
374 xproperty->AddAttribute( wxT( "name" ), wxT( "exclude_from_bom" ) );
375 }
376
377 if( symbol->GetExcludedFromBoard() )
378 {
379 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
380 xproperty->AddAttribute( wxT( "name" ), wxT( "exclude_from_board" ) );
381 }
382
383 if( symbol->GetDNP() )
384 {
385 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
386 xproperty->AddAttribute( wxT( "name" ), wxT( "dnp" ) );
387 }
388
389 if( const std::unique_ptr<LIB_SYMBOL>& part = symbol->GetLibSymbolRef() )
390 {
391 if( part->GetKeyWords().size() )
392 {
393 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
394 xproperty->AddAttribute( wxT( "name" ), wxT( "ki_keywords" ) );
395 xproperty->AddAttribute( wxT( "value" ), part->GetKeyWords() );
396 }
397
398 if( !part->GetFPFilters().IsEmpty() )
399 {
400 wxString filters;
401
402 for( const wxString& filter : part->GetFPFilters() )
403 filters += ' ' + filter;
404
405 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
406 xproperty->AddAttribute( wxT( "name" ), wxT( "ki_fp_filters" ) );
407 xproperty->AddAttribute( wxT( "value" ), filters.Trim( false ) );
408 }
409 }
410
411 XNODE* xsheetpath;
412 xcomp->AddChild( xsheetpath = node( wxT( "sheetpath" ) ) );
413
414 xsheetpath->AddAttribute( wxT( "names" ), sheet.PathHumanReadable() );
415 xsheetpath->AddAttribute( wxT( "tstamps" ), sheet.PathAsString() );
416
417 XNODE* xunits; // Node for extra units
418 xcomp->AddChild( xunits = node( wxT( "tstamps" ) ) );
419
420 auto range = extra_units.equal_range( symbol );
421 wxString uuid;
422
423 // Output a series of children with all UUIDs associated with the REFDES
424 for( auto it = range.first; it != range.second; ++it )
425 {
426 uuid = ( *it )->m_Uuid.AsString();
427
428 // Add a space between UUIDs, if not in KICAD mode (i.e.
429 // using wxXmlDocument::Save()). KICAD MODE has its own XNODE::Format function.
430 if( !( aCtl & GNL_OPT_KICAD ) ) // i.e. for .xml format
431 uuid += ' ';
432
433 xunits->AddChild( new XNODE( wxXML_TEXT_NODE, wxEmptyString, uuid ) );
434 }
435
436 // Output the primary UUID
437 uuid = symbol->m_Uuid.AsString();
438 xunits->AddChild( new XNODE( wxXML_TEXT_NODE, wxEmptyString, uuid ) );
439 }
440 }
441
442 m_schematic->SetCurrentSheet( currentSheet );
443
444 return xcomps;
445}
446
447
449{
450 SCH_SCREEN* screen;
451 XNODE* xdesign = node( wxT( "design" ) );
452 XNODE* xtitleBlock;
453 XNODE* xsheet;
454 XNODE* xcomment;
455 XNODE* xtextvar;
456 wxString sheetTxt;
457 wxFileName sourceFileName;
458
459 // the root sheet is a special sheet, call it source
460 xdesign->AddChild( node( wxT( "source" ), m_schematic->GetFileName() ) );
461
462 xdesign->AddChild( node( wxT( "date" ), GetISO8601CurrentDateTime() ) );
463
464 // which Eeschema tool
465 xdesign->AddChild( node( wxT( "tool" ), wxT( "Eeschema " ) + GetBuildVersion() ) );
466
467 const std::map<wxString, wxString>& properties = m_schematic->Prj().GetTextVars();
468
469 for( const std::pair<const wxString, wxString>& prop : properties )
470 {
471 xdesign->AddChild( xtextvar = node( wxT( "textvar" ), prop.second ) );
472 xtextvar->AddAttribute( wxT( "name" ), prop.first );
473 }
474
475 /*
476 * Export the sheets information
477 */
478 unsigned sheetIndex = 1; // Human readable index
479
481 {
482 screen = sheet.LastScreen();
483
484 xdesign->AddChild( xsheet = node( wxT( "sheet" ) ) );
485
486 // get the string representation of the sheet index number.
487 sheetTxt.Printf( wxT( "%u" ), sheetIndex++ );
488 xsheet->AddAttribute( wxT( "number" ), sheetTxt );
489 xsheet->AddAttribute( wxT( "name" ), sheet.PathHumanReadable() );
490 xsheet->AddAttribute( wxT( "tstamps" ), sheet.PathAsString() );
491
492 TITLE_BLOCK tb = screen->GetTitleBlock();
493 PROJECT* prj = &m_schematic->Prj();
494
495 xsheet->AddChild( xtitleBlock = node( wxT( "title_block" ) ) );
496
497 xtitleBlock->AddChild( node( wxT( "title" ), ExpandTextVars( tb.GetTitle(), prj ) ) );
498 xtitleBlock->AddChild( node( wxT( "company" ), ExpandTextVars( tb.GetCompany(), prj ) ) );
499 xtitleBlock->AddChild( node( wxT( "rev" ), ExpandTextVars( tb.GetRevision(), prj ) ) );
500 xtitleBlock->AddChild( node( wxT( "date" ), ExpandTextVars( tb.GetDate(), prj ) ) );
501
502 // We are going to remove the fileName directories.
503 sourceFileName = wxFileName( screen->GetFileName() );
504 xtitleBlock->AddChild( node( wxT( "source" ), sourceFileName.GetFullName() ) );
505
506 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
507 xcomment->AddAttribute( wxT( "number" ), wxT( "1" ) );
508 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 0 ), prj ) );
509
510 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
511 xcomment->AddAttribute( wxT( "number" ), wxT( "2" ) );
512 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 1 ), prj ) );
513
514 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
515 xcomment->AddAttribute( wxT( "number" ), wxT( "3" ) );
516 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 2 ), prj ) );
517
518 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
519 xcomment->AddAttribute( wxT( "number" ), wxT( "4" ) );
520 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 3 ), prj ) );
521
522 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
523 xcomment->AddAttribute( wxT( "number" ), wxT( "5" ) );
524 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 4 ), prj ) );
525
526 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
527 xcomment->AddAttribute( wxT( "number" ), wxT( "6" ) );
528 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 5 ), prj ) );
529
530 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
531 xcomment->AddAttribute( wxT( "number" ), wxT( "7" ) );
532 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 6 ), prj ) );
533
534 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
535 xcomment->AddAttribute( wxT( "number" ), wxT( "8" ) );
536 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 7 ), prj ) );
537
538 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
539 xcomment->AddAttribute( wxT( "number" ), wxT( "9" ) );
540 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 8 ), prj ) );
541 }
542
543 return xdesign;
544}
545
546
548{
549 XNODE* xlibs = node( wxT( "libraries" ) ); // auto_ptr
551
552 for( std::set<wxString>::iterator it = m_libraries.begin(); it!=m_libraries.end(); ++it )
553 {
554 wxString libNickname = *it;
555 XNODE* xlibrary;
556
557 if( symbolLibTable->HasLibrary( libNickname ) )
558 {
559 xlibs->AddChild( xlibrary = node( wxT( "library" ) ) );
560 xlibrary->AddAttribute( wxT( "logical" ), libNickname );
561 xlibrary->AddChild( node( wxT( "uri" ), symbolLibTable->GetFullURI( libNickname ) ) );
562 }
563
564 // @todo: add more fun stuff here
565 }
566
567 return xlibs;
568}
569
570
572{
573 XNODE* xlibparts = node( wxT( "libparts" ) ); // auto_ptr
574 std::vector<SCH_FIELD*> fieldList;
575
576 m_libraries.clear();
577
578 for( LIB_SYMBOL* lcomp : m_libParts )
579 {
580 wxString libNickname = lcomp->GetLibId().GetLibNickname();;
581
582 // The library nickname will be empty if the cache library is used.
583 if( !libNickname.IsEmpty() )
584 m_libraries.insert( libNickname ); // inserts symbol's library if unique
585
586 XNODE* xlibpart;
587 xlibparts->AddChild( xlibpart = node( wxT( "libpart" ) ) );
588 xlibpart->AddAttribute( wxT( "lib" ), libNickname );
589 xlibpart->AddAttribute( wxT( "part" ), lcomp->GetName() );
590
591 //----- show the important properties -------------------------
592 if( !lcomp->GetDescription().IsEmpty() )
593 xlibpart->AddChild( node( wxT( "description" ), lcomp->GetDescription() ) );
594
595 if( !lcomp->GetDatasheetField().GetText().IsEmpty() )
596 xlibpart->AddChild( node( wxT( "docs" ), lcomp->GetDatasheetField().GetText() ) );
597
598 // Write the footprint list
599 if( lcomp->GetFPFilters().GetCount() )
600 {
601 XNODE* xfootprints;
602 xlibpart->AddChild( xfootprints = node( wxT( "footprints" ) ) );
603
604 for( unsigned i = 0; i < lcomp->GetFPFilters().GetCount(); ++i )
605 xfootprints->AddChild( node( wxT( "fp" ), lcomp->GetFPFilters()[i] ) );
606 }
607
608 //----- show the fields here ----------------------------------
609 fieldList.clear();
610 lcomp->GetFields( fieldList );
611
612 XNODE* xfields;
613 xlibpart->AddChild( xfields = node( "fields" ) );
614
615 for( const SCH_FIELD* field : fieldList )
616 {
617 XNODE* xfield;
618 xfields->AddChild( xfield = node( wxT( "field" ), field->GetText() ) );
619 xfield->AddAttribute( wxT( "name" ), field->GetCanonicalName() );
620 }
621
622 //----- show the pins here ------------------------------------
623 std::vector<SCH_PIN*> pinList = lcomp->GetPins( 0, 0 );
624
625 /*
626 * We must erase redundant Pins references in pinList
627 * These redundant pins exist because some pins are found more than one time when a
628 * symbol has multiple parts per package or has 2 representations (DeMorgan conversion).
629 * For instance, a 74ls00 has DeMorgan conversion, with different pin shapes, and
630 * therefore each pin appears 2 times in the list. Common pins (VCC, GND) can also be
631 * found more than once.
632 */
633 sort( pinList.begin(), pinList.end(), sortPinsByNumber );
634
635 for( int ii = 0; ii < (int)pinList.size()-1; ii++ )
636 {
637 if( pinList[ii]->GetNumber() == pinList[ii+1]->GetNumber() )
638 { // 2 pins have the same number, remove the redundant pin at index i+1
639 pinList.erase(pinList.begin() + ii + 1);
640 ii--;
641 }
642 }
643
644 if( pinList.size() )
645 {
646 XNODE* pins;
647
648 xlibpart->AddChild( pins = node( wxT( "pins" ) ) );
649
650 for( unsigned i=0; i<pinList.size(); ++i )
651 {
652 XNODE* pin;
653
654 pins->AddChild( pin = node( wxT( "pin" ) ) );
655 pin->AddAttribute( wxT( "num" ), pinList[i]->GetShownNumber() );
656 pin->AddAttribute( wxT( "name" ), pinList[i]->GetShownName() );
657 pin->AddAttribute( wxT( "type" ), pinList[i]->GetCanonicalElectricalTypeName() );
658
659 // caution: construction work site here, drive slowly
660 }
661 }
662 }
663
664 return xlibparts;
665}
666
667
669{
670 XNODE* xnets = node( wxT( "nets" ) ); // auto_ptr if exceptions ever get used.
671 wxString netCodeTxt;
672 wxString netName;
673 wxString ref;
674
675 XNODE* xnet = nullptr;
676
677 /* output:
678 <net code="123" name="/cfcard.sch/WAIT#">
679 <node ref="R23" pin="1"/>
680 <node ref="U18" pin="12"/>
681 </net>
682 */
683
684 struct NET_NODE
685 {
686 NET_NODE( SCH_PIN* aPin, const SCH_SHEET_PATH& aSheet ) :
687 m_Pin( aPin ),
688 m_Sheet( aSheet )
689 {}
690
691 SCH_PIN* m_Pin;
692 SCH_SHEET_PATH m_Sheet;
693 };
694
695 struct NET_RECORD
696 {
697 NET_RECORD( const wxString& aName ) :
698 m_Name( aName ),
699 m_HasNoConnect( false )
700 {};
701
702 wxString m_Name;
703 bool m_HasNoConnect;
704 std::vector<NET_NODE> m_Nodes;
705 };
706
707 std::vector<NET_RECORD*> nets;
708
709 for( const auto& [ key, subgraphs ] : m_schematic->ConnectionGraph()->GetNetMap() )
710 {
711 wxString net_name = key.Name;
712 NET_RECORD* net_record = nullptr;
713
714 if( subgraphs.empty() )
715 continue;
716
717 nets.emplace_back( new NET_RECORD( net_name ) );
718 net_record = nets.back();
719
720 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
721 {
722 bool nc = subgraph->GetNoConnect() && subgraph->GetNoConnect()->Type() == SCH_NO_CONNECT_T;
723 const SCH_SHEET_PATH& sheet = subgraph->GetSheet();
724
725 if( nc )
726 net_record->m_HasNoConnect = true;
727
728 for( SCH_ITEM* item : subgraph->GetItems() )
729 {
730 if( item->Type() == SCH_PIN_T )
731 {
732 SCH_PIN* pin = static_cast<SCH_PIN*>( item );
733 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( pin->GetParentSymbol() );
734 bool forBOM = aCtl & GNL_OPT_BOM;
735 bool forBoard = aCtl & GNL_OPT_KICAD;
736
737 if( !symbol )
738 continue;
739
740 if( forBOM && ( sheet.GetExcludedFromBOM() || symbol->GetExcludedFromBOM() ) )
741 continue;
742
743 if( forBoard && ( sheet.GetExcludedFromBoard() || symbol->GetExcludedFromBoard() ) )
744 continue;
745
746 net_record->m_Nodes.emplace_back( pin, sheet );
747 }
748 }
749 }
750 }
751
752 // Netlist ordering: Net name, then ref des, then pin name
753 std::sort( nets.begin(), nets.end(),
754 []( const NET_RECORD* a, const NET_RECORD*b )
755 {
756 return StrNumCmp( a->m_Name, b->m_Name ) < 0;
757 } );
758
759 for( int i = 0; i < (int) nets.size(); ++i )
760 {
761 NET_RECORD* net_record = nets[i];
762 bool added = false;
763 XNODE* xnode;
764
765 // Netlist ordering: Net name, then ref des, then pin name
766 std::sort( net_record->m_Nodes.begin(), net_record->m_Nodes.end(),
767 []( const NET_NODE& a, const NET_NODE& b )
768 {
769 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
770 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
771
772 if( refA == refB )
773 return a.m_Pin->GetShownNumber() < b.m_Pin->GetShownNumber();
774
775 return refA < refB;
776 } );
777
778 // Some duplicates can exist, for example on multi-unit parts with duplicated pins across
779 // units. If the user connects the pins on each unit, they will appear on separate
780 // subgraphs. Remove those here:
781 alg::remove_duplicates( net_record->m_Nodes,
782 []( const NET_NODE& a, const NET_NODE& b )
783 {
784 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
785 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
786
787 return refA == refB && a.m_Pin->GetShownNumber() == b.m_Pin->GetShownNumber();
788 } );
789
790 // Determine if all pins in the net are stacked (nets with only one pin are implicitly
791 // taken to be stacked)
792 bool allNetPinsStacked = true;
793
794 if( net_record->m_Nodes.size() > 1 )
795 {
796 SCH_PIN* firstPin = net_record->m_Nodes.begin()->m_Pin;
797 allNetPinsStacked =
798 std::all_of( net_record->m_Nodes.begin() + 1, net_record->m_Nodes.end(),
799 [=]( auto& node )
800 {
801 return firstPin->GetParent() == node.m_Pin->GetParent()
802 && firstPin->GetPosition() == node.m_Pin->GetPosition()
803 && firstPin->GetName() == node.m_Pin->GetName();
804 } );
805 }
806
807 for( const NET_NODE& netNode : net_record->m_Nodes )
808 {
809 wxString refText = netNode.m_Pin->GetParentSymbol()->GetRef( &netNode.m_Sheet );
810 wxString pinText = netNode.m_Pin->GetShownNumber();
811
812 // Skip power symbols and virtual symbols
813 if( refText[0] == wxChar( '#' ) )
814 continue;
815
816 if( !added )
817 {
818 netCodeTxt.Printf( wxT( "%d" ), i + 1 );
819
820 xnets->AddChild( xnet = node( wxT( "net" ) ) );
821 xnet->AddAttribute( wxT( "code" ), netCodeTxt );
822 xnet->AddAttribute( wxT( "name" ), net_record->m_Name );
823
824 added = true;
825 }
826
827 xnet->AddChild( xnode = node( wxT( "node" ) ) );
828 xnode->AddAttribute( wxT( "ref" ), refText );
829 xnode->AddAttribute( wxT( "pin" ), pinText );
830
831 wxString pinName = netNode.m_Pin->GetShownName();
832 wxString pinType = netNode.m_Pin->GetCanonicalElectricalTypeName();
833
834 if( !pinName.IsEmpty() )
835 xnode->AddAttribute( wxT( "pinfunction" ), pinName );
836
837 if( net_record->m_HasNoConnect
838 && ( net_record->m_Nodes.size() == 1 || allNetPinsStacked ) )
839 pinType += wxT( "+no_connect" );
840
841 xnode->AddAttribute( wxT( "pintype" ), pinType );
842 }
843 }
844
845 for( NET_RECORD* record : nets )
846 delete record;
847
848 return xnets;
849}
850
851
852XNODE* NETLIST_EXPORTER_XML::node( const wxString& aName,
853 const wxString& aTextualContent /* = wxEmptyString*/ )
854{
855 XNODE* n = new XNODE( wxXML_ELEMENT_NODE, aName );
856
857 if( aTextualContent.Len() > 0 ) // excludes wxEmptyString, the parameter's default value
858 n->AddChild( new XNODE( wxXML_TEXT_NODE, wxEmptyString, aTextualContent ) );
859
860 return n;
861}
862
863
864static bool sortPinsByNumber( SCH_PIN* aPin1, SCH_PIN* aPin2 )
865{
866 // return "lhs < rhs"
867 return StrNumCmp( aPin1->GetShownNumber(), aPin2->GetShownNumber(), true ) < 0;
868}
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:88
const KIID m_Uuid
Definition: eda_item.h:485
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:98
wxString AsString() const
Definition: kiid.cpp:246
const wxString GetUniStringLibItemName() const
Get strings for display messages in dialogs.
Definition: lib_id.h:112
const wxString GetUniStringLibNickname() const
Definition: lib_id.h:88
Define a library symbol object.
Definition: lib_symbol.h:77
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.
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, const SCH_SHEET_PATH &aSheetPath)
Check if the given symbol should be processed for netlisting.
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.
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.
void addSymbolFields(XNODE *aNode, SCH_SYMBOL *aSymbol, const SCH_SHEET_PATH &aSheet, const SCH_SHEET_LIST &aSheetList)
Holder for multi-unit symbol fields.
XNODE * makeSymbols(unsigned aCtl)
XNODE * makeRoot(unsigned aCtl=GNL_ALL)
Build the entire document tree for the generic export.
std::set< wxString > m_libraries
XNODE * makeLibParts()
Fill out an XML node with the unique library parts and returns it.
static SYMBOL_LIB_TABLE * SchSymbolLibTable(PROJECT *aProject)
Accessor for project symbol library table.
Container for project specific data.
Definition: project.h:62
virtual std::map< wxString, wxString > & GetTextVars() const
Definition: project.cpp:84
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_PATH & CurrentSheet() const =0
virtual SCH_SHEET_LIST BuildSheetListSortedByPageNumbers() 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:1165
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition: sch_field.cpp:197
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
wxString GetShownNumber() const
Definition: sch_pin.cpp:458
const wxString & GetFileName() const
Definition: sch_screen.h:143
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:154
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...
bool GetExcludedFromBOM() const
const SCH_SHEET * GetSheet(unsigned aIndex) const
bool GetExcludedFromBoard() const
Schematic symbol object.
Definition: sch_symbol.h:105
wxString GetDescription() const override
Definition: sch_symbol.cpp:284
int GetFieldCount() const
Return the number of fields in this symbol.
Definition: sch_symbol.h:591
bool UseLibIdLookup() const
Definition: sch_symbol.h:211
wxString GetSchSymbolLibraryName() const
Definition: sch_symbol.cpp:266
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:919
const wxString GetValue(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const override
Definition: sch_symbol.cpp:887
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
Definition: sch_symbol.cpp:903
const LIB_ID & GetLibId() const override
Definition: sch_symbol.h:194
int GetUnitSelection(const SCH_SHEET_PATH *aSheet) const
Return the instance-specific unit selection for the given sheet path.
Definition: sch_symbol.cpp:845
int GetUnitCount() const override
Return the number of units per package of the symbol.
Definition: sch_symbol.cpp:451
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with SCH_FIELDs.
Definition: sch_symbol.cpp:967
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:213
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:720
bool GetExcludedFromBoard() const
Definition: symbol.h:148
bool GetExcludedFromBOM() const
Definition: symbol.h:142
bool GetDNP() const
Set or clear the 'Do Not Populate' flaga.
Definition: symbol.h:153
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.
void remove_duplicates(_Container &__c)
Deletes all duplicate values from __c.
Definition: kicad_algo.h:183
static bool sortPinsByNumber(SCH_PIN *aPin1, SCH_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 UnescapeString(const wxString &aSource)
wxString GetISO8601CurrentDateTime()
Definition for symbol library class.
wxString GetCanonicalFieldName(int idx)
@ DATASHEET_FIELD
name of datasheet
@ FOOTPRINT_FIELD
Field Name Module PCB, i.e. "16DIP300".
@ MANDATORY_FIELDS
The first 5 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors.
@ DESCRIPTION_FIELD
Field Description of part, i.e. "1/4W 1% Metal Film Resistor".
@ SCH_NO_CONNECT_T
Definition: typeinfo.h:160
@ SCH_SYMBOL_T
Definition: typeinfo.h:172
@ SCH_PIN_T
Definition: typeinfo.h:153