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( LIB_PIN* aPin1, LIB_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 SCH_SHEET_PATH* aSheet )
97{
98 wxString value;
99 wxString footprint;
100 wxString datasheet;
101 wxString description;
102 wxString candidate;
103 nlohmann::ordered_map<wxString, wxString> fields;
104
105 if( aSymbol->GetUnitCount() > 1 )
106 {
107 // Sadly, each unit of a symbol can have its own unique fields. This
108 // block finds the unit with the lowest number having a non blank field
109 // value and records it. Therefore user is best off setting fields
110 // into only the first unit. But this scavenger algorithm will find
111 // any non blank fields in all units and use the first non-blank field
112 // for each unique field name.
113
114 wxString ref = aSymbol->GetRef( aSheet );
115
116 SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
117 int minUnit = aSymbol->GetUnitSelection( aSheet );
118
119 for( unsigned i = 0; i < sheetList.size(); i++ )
120 {
121 for( SCH_ITEM* item : sheetList[i].LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
122 {
123 SCH_SYMBOL* symbol2 = (SCH_SYMBOL*) item;
124
125 wxString ref2 = symbol2->GetRef( &sheetList[i] );
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->GetValueFieldText( m_resolveTextVars, &sheetList[i], false );
136
137 if( !candidate.IsEmpty() && ( unit < minUnit || value.IsEmpty() ) )
138 value = candidate;
139
140 // Footprint
141 candidate = symbol2->GetFootprintFieldText( m_resolveTextVars, &sheetList[i], 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( &sheetList[i], 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( &sheetList[i], 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->GetValueFieldText( 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_LIST sheetList = m_schematic->GetSheets();
250 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( unsigned ii = 0; ii < sheetList.size(); ii++ )
256 {
257 SCH_SHEET_PATH sheet = sheetList[ii];
258
259 // Change schematic CurrentSheet in each iteration to allow hierarchical
260 // resolution of text variables in sheet fields.
262
263 auto cmp = [sheet]( SCH_SYMBOL* a, SCH_SYMBOL* b )
264 {
265 return ( StrNumCmp( a->GetRef( &sheet, false ),
266 b->GetRef( &sheet, false ), true ) < 0 );
267 };
268
269 std::set<SCH_SYMBOL*, decltype( cmp )> ordered_symbols( cmp );
270 std::multiset<SCH_SYMBOL*, decltype( cmp )> extra_units( cmp );
271
272 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
273 {
274 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
275 auto test = ordered_symbols.insert( symbol );
276
277 if( !test.second )
278 {
279 if( ( *( test.first ) )->m_Uuid > symbol->m_Uuid )
280 {
281 extra_units.insert( *( test.first ) );
282 ordered_symbols.erase( test.first );
283 ordered_symbols.insert( symbol );
284 }
285 else
286 {
287 extra_units.insert( symbol );
288 }
289 }
290 }
291
292 for( EDA_ITEM* item : ordered_symbols )
293 {
294 SCH_SYMBOL* symbol = findNextSymbol( item, &sheet );
295
296 if( !symbol
297 || ( ( aCtl & GNL_OPT_BOM ) && symbol->GetExcludedFromBOM() )
298 || ( ( aCtl & GNL_OPT_KICAD ) && symbol->GetExcludedFromBoard() ) )
299 {
300 continue;
301 }
302
303 // Output the symbol's elements in order of expected access frequency. This may
304 // not always look best, but it will allow faster execution under XSL processing
305 // systems which do sequential searching within an element.
306
307 XNODE* xcomp; // current symbol being constructed
308 xcomps->AddChild( xcomp = node( wxT( "comp" ) ) );
309
310 xcomp->AddAttribute( wxT( "ref" ), symbol->GetRef( &sheet ) );
311 addSymbolFields( xcomp, symbol, &sheet );
312
313 XNODE* xlibsource;
314 xcomp->AddChild( xlibsource = node( wxT( "libsource" ) ) );
315
316 // "logical" library name, which is in anticipation of a better search algorithm
317 // for parts based on "logical_lib.part" and where logical_lib is merely the library
318 // name minus path and extension.
319 wxString libName;
320 wxString partName;
321
322 if( symbol->UseLibIdLookup() )
323 {
324 libName = symbol->GetLibId().GetUniStringLibNickname();
325 partName = symbol->GetLibId().GetUniStringLibItemName();
326 }
327 else
328 {
329 partName = symbol->GetSchSymbolLibraryName();
330 }
331
332 xlibsource->AddAttribute( wxT( "lib" ), libName );
333
334 // We only want the symbol name, not the full LIB_ID.
335 xlibsource->AddAttribute( wxT( "part" ), partName );
336
337 xlibsource->AddAttribute( wxT( "description" ), symbol->GetDescription() );
338
339 /* Add the symbol properties. */
340 XNODE* xproperty;
341
342 std::vector<SCH_FIELD>& fields = symbol->GetFields();
343
344 for( size_t jj = MANDATORY_FIELDS; jj < fields.size(); ++jj )
345 {
346 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
347 xproperty->AddAttribute( wxT( "name" ), fields[jj].GetCanonicalName() );
348
350 xproperty->AddAttribute( wxT( "value" ), fields[jj].GetShownText( &sheet, false ) );
351 else
352 xproperty->AddAttribute( wxT( "value" ), fields[jj].GetText() );
353 }
354
355 for( const SCH_FIELD& sheetField : sheet.Last()->GetFields() )
356 {
357 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
358 xproperty->AddAttribute( wxT( "name" ), sheetField.GetCanonicalName() );
359
361 // do not allow GetShownText() to add any prefix useful only when displaying
362 // the field on screen
363 xproperty->AddAttribute( wxT( "value" ), sheetField.GetShownText( &sheet, false ) );
364 else
365 xproperty->AddAttribute( wxT( "value" ), sheetField.GetText() );
366 }
367
368 if( symbol->GetExcludedFromBOM() )
369 {
370 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
371 xproperty->AddAttribute( wxT( "name" ), wxT( "exclude_from_bom" ) );
372 }
373
374 if( symbol->GetExcludedFromBoard() )
375 {
376 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
377 xproperty->AddAttribute( wxT( "name" ), wxT( "exclude_from_board" ) );
378 }
379
380 if( symbol->GetDNP() )
381 {
382 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
383 xproperty->AddAttribute( wxT( "name" ), wxT( "dnp" ) );
384 }
385
386 if( const std::unique_ptr<LIB_SYMBOL>& part = symbol->GetLibSymbolRef() )
387 {
388 if( part->GetKeyWords().size() )
389 {
390 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
391 xproperty->AddAttribute( wxT( "name" ), wxT( "ki_keywords" ) );
392 xproperty->AddAttribute( wxT( "value" ), part->GetKeyWords() );
393 }
394
395 if( !part->GetFPFilters().IsEmpty() )
396 {
397 wxString filters;
398
399 for( const wxString& filter : part->GetFPFilters() )
400 filters += ' ' + filter;
401
402 xcomp->AddChild( xproperty = node( wxT( "property" ) ) );
403 xproperty->AddAttribute( wxT( "name" ), wxT( "ki_fp_filters" ) );
404 xproperty->AddAttribute( wxT( "value" ), filters.Trim( false ) );
405 }
406 }
407
408 XNODE* xsheetpath;
409 xcomp->AddChild( xsheetpath = node( wxT( "sheetpath" ) ) );
410
411 xsheetpath->AddAttribute( wxT( "names" ), sheet.PathHumanReadable() );
412 xsheetpath->AddAttribute( wxT( "tstamps" ), sheet.PathAsString() );
413
414 XNODE* xunits; // Node for extra units
415 xcomp->AddChild( xunits = node( wxT( "tstamps" ) ) );
416
417 auto range = extra_units.equal_range( symbol );
418 wxString uuid;
419
420 // Output a series of children with all UUIDs associated with the REFDES
421 for( auto it = range.first; it != range.second; ++it )
422 {
423 uuid = ( *it )->m_Uuid.AsString();
424
425 // Add a space between UUIDs, if not in KICAD mode (i.e.
426 // using wxXmlDocument::Save()). KICAD MODE has its own XNODE::Format function.
427 if( !( aCtl & GNL_OPT_KICAD ) ) // i.e. for .xml format
428 uuid += ' ';
429
430 xunits->AddChild( new XNODE( wxXML_TEXT_NODE, wxEmptyString, uuid ) );
431 }
432
433 // Output the primary UUID
434 uuid = symbol->m_Uuid.AsString();
435 xunits->AddChild( new XNODE( wxXML_TEXT_NODE, wxEmptyString, uuid ) );
436 }
437 }
438
439 m_schematic->SetCurrentSheet( currentSheet );
440
441 return xcomps;
442}
443
444
446{
447 SCH_SCREEN* screen;
448 XNODE* xdesign = node( wxT( "design" ) );
449 XNODE* xtitleBlock;
450 XNODE* xsheet;
451 XNODE* xcomment;
452 XNODE* xtextvar;
453 wxString sheetTxt;
454 wxFileName sourceFileName;
455
456 // the root sheet is a special sheet, call it source
457 xdesign->AddChild( node( wxT( "source" ), m_schematic->GetFileName() ) );
458
459 xdesign->AddChild( node( wxT( "date" ), GetISO8601CurrentDateTime() ) );
460
461 // which Eeschema tool
462 xdesign->AddChild( node( wxT( "tool" ), wxT( "Eeschema " ) + GetBuildVersion() ) );
463
464 const std::map<wxString, wxString>& properties = m_schematic->Prj().GetTextVars();
465
466 for( const std::pair<const wxString, wxString>& prop : properties )
467 {
468 xdesign->AddChild( xtextvar = node( wxT( "textvar" ), prop.second ) );
469 xtextvar->AddAttribute( wxT( "name" ), prop.first );
470 }
471
472 /*
473 * Export the sheets information
474 */
475 SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
476
477 for( unsigned i = 0; i < sheetList.size(); i++ )
478 {
479 screen = sheetList[i].LastScreen();
480
481 xdesign->AddChild( xsheet = node( wxT( "sheet" ) ) );
482
483 // get the string representation of the sheet index number.
484 // Note that sheet->GetIndex() is zero index base and we need to increment the
485 // number by one to make it human readable
486 sheetTxt.Printf( wxT( "%u" ), i + 1 );
487 xsheet->AddAttribute( wxT( "number" ), sheetTxt );
488 xsheet->AddAttribute( wxT( "name" ), sheetList[i].PathHumanReadable() );
489 xsheet->AddAttribute( wxT( "tstamps" ), sheetList[i].PathAsString() );
490
491 TITLE_BLOCK tb = screen->GetTitleBlock();
492 PROJECT* prj = &m_schematic->Prj();
493
494 xsheet->AddChild( xtitleBlock = node( wxT( "title_block" ) ) );
495
496 xtitleBlock->AddChild( node( wxT( "title" ), ExpandTextVars( tb.GetTitle(), prj ) ) );
497 xtitleBlock->AddChild( node( wxT( "company" ), ExpandTextVars( tb.GetCompany(), prj ) ) );
498 xtitleBlock->AddChild( node( wxT( "rev" ), ExpandTextVars( tb.GetRevision(), prj ) ) );
499 xtitleBlock->AddChild( node( wxT( "date" ), ExpandTextVars( tb.GetDate(), prj ) ) );
500
501 // We are going to remove the fileName directories.
502 sourceFileName = wxFileName( screen->GetFileName() );
503 xtitleBlock->AddChild( node( wxT( "source" ), sourceFileName.GetFullName() ) );
504
505 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
506 xcomment->AddAttribute( wxT( "number" ), wxT( "1" ) );
507 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 0 ), prj ) );
508
509 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
510 xcomment->AddAttribute( wxT( "number" ), wxT( "2" ) );
511 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 1 ), prj ) );
512
513 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
514 xcomment->AddAttribute( wxT( "number" ), wxT( "3" ) );
515 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 2 ), prj ) );
516
517 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
518 xcomment->AddAttribute( wxT( "number" ), wxT( "4" ) );
519 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 3 ), prj ) );
520
521 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
522 xcomment->AddAttribute( wxT( "number" ), wxT( "5" ) );
523 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 4 ), prj ) );
524
525 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
526 xcomment->AddAttribute( wxT( "number" ), wxT( "6" ) );
527 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 5 ), prj ) );
528
529 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
530 xcomment->AddAttribute( wxT( "number" ), wxT( "7" ) );
531 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 6 ), prj ) );
532
533 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
534 xcomment->AddAttribute( wxT( "number" ), wxT( "8" ) );
535 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 7 ), prj ) );
536
537 xtitleBlock->AddChild( xcomment = node( wxT( "comment" ) ) );
538 xcomment->AddAttribute( wxT( "number" ), wxT( "9" ) );
539 xcomment->AddAttribute( wxT( "value" ), ExpandTextVars( tb.GetComment( 8 ), prj ) );
540 }
541
542 return xdesign;
543}
544
545
547{
548 XNODE* xlibs = node( wxT( "libraries" ) ); // auto_ptr
550
551 for( std::set<wxString>::iterator it = m_libraries.begin(); it!=m_libraries.end(); ++it )
552 {
553 wxString libNickname = *it;
554 XNODE* xlibrary;
555
556 if( symbolLibTable->HasLibrary( libNickname ) )
557 {
558 xlibs->AddChild( xlibrary = node( wxT( "library" ) ) );
559 xlibrary->AddAttribute( wxT( "logical" ), libNickname );
560 xlibrary->AddChild( node( wxT( "uri" ), symbolLibTable->GetFullURI( libNickname ) ) );
561 }
562
563 // @todo: add more fun stuff here
564 }
565
566 return xlibs;
567}
568
569
571{
572 XNODE* xlibparts = node( wxT( "libparts" ) ); // auto_ptr
573
574 LIB_PINS pinList;
575 std::vector<LIB_FIELD*> fieldList;
576
577 m_libraries.clear();
578
579 for( LIB_SYMBOL* lcomp : m_libParts )
580 {
581 wxString libNickname = lcomp->GetLibId().GetLibNickname();;
582
583 // The library nickname will be empty if the cache library is used.
584 if( !libNickname.IsEmpty() )
585 m_libraries.insert( libNickname ); // inserts symbol's library if unique
586
587 XNODE* xlibpart;
588 xlibparts->AddChild( xlibpart = node( wxT( "libpart" ) ) );
589 xlibpart->AddAttribute( wxT( "lib" ), libNickname );
590 xlibpart->AddAttribute( wxT( "part" ), lcomp->GetName() );
591
592 //----- show the important properties -------------------------
593 if( !lcomp->GetDescription().IsEmpty() )
594 xlibpart->AddChild( node( wxT( "description" ), lcomp->GetDescription() ) );
595
596 if( !lcomp->GetDatasheetField().GetText().IsEmpty() )
597 xlibpart->AddChild( node( wxT( "docs" ), lcomp->GetDatasheetField().GetText() ) );
598
599 // Write the footprint list
600 if( lcomp->GetFPFilters().GetCount() )
601 {
602 XNODE* xfootprints;
603 xlibpart->AddChild( xfootprints = node( wxT( "footprints" ) ) );
604
605 for( unsigned i = 0; i < lcomp->GetFPFilters().GetCount(); ++i )
606 xfootprints->AddChild( node( wxT( "fp" ), lcomp->GetFPFilters()[i] ) );
607 }
608
609 //----- show the fields here ----------------------------------
610 fieldList.clear();
611 lcomp->GetFields( fieldList );
612
613 XNODE* xfields;
614 xlibpart->AddChild( xfields = node( "fields" ) );
615
616 for( const LIB_FIELD* field : fieldList )
617 {
618 XNODE* xfield;
619 xfields->AddChild( xfield = node( wxT( "field" ), field->GetText() ) );
620 xfield->AddAttribute( wxT( "name" ), field->GetCanonicalName() );
621 }
622
623 //----- show the pins here ------------------------------------
624 pinList.clear();
625 lcomp->GetPins( pinList, 0, 0 );
626
627 /*
628 * We must erase redundant Pins references in pinList
629 * These redundant pins exist because some pins are found more than one time when a
630 * symbol has multiple parts per package or has 2 representations (DeMorgan conversion).
631 * For instance, a 74ls00 has DeMorgan conversion, with different pin shapes, and
632 * therefore each pin appears 2 times in the list. Common pins (VCC, GND) can also be
633 * found more than once.
634 */
635 sort( pinList.begin(), pinList.end(), sortPinsByNumber );
636 for( int ii = 0; ii < (int)pinList.size()-1; ii++ )
637 {
638 if( pinList[ii]->GetNumber() == pinList[ii+1]->GetNumber() )
639 { // 2 pins have the same number, remove the redundant pin at index i+1
640 pinList.erase(pinList.begin() + ii + 1);
641 ii--;
642 }
643 }
644
645 if( pinList.size() )
646 {
647 XNODE* pins;
648
649 xlibpart->AddChild( pins = node( wxT( "pins" ) ) );
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 ) : m_Pin( aPin ), m_Sheet( aSheet )
687 {}
688
689 SCH_PIN* m_Pin;
690 SCH_SHEET_PATH m_Sheet;
691 };
692
693 struct NET_RECORD
694 {
695 NET_RECORD( const wxString& aName ) : m_Name( aName ), m_HasNoConnect( false ){};
696
697 wxString m_Name;
698 bool m_HasNoConnect;
699 std::vector<NET_NODE> m_Nodes;
700 };
701
702 std::vector<NET_RECORD*> nets;
703
704 for( const auto& [ key, subgraphs ] : m_schematic->ConnectionGraph()->GetNetMap() )
705 {
706 wxString net_name = key.Name;
707 NET_RECORD* net_record = nullptr;
708
709 if( subgraphs.empty() )
710 continue;
711
712 nets.emplace_back( new NET_RECORD( net_name ) );
713 net_record = nets.back();
714
715 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
716 {
717 bool nc = subgraph->GetNoConnect() && subgraph->GetNoConnect()->Type() == SCH_NO_CONNECT_T;
718 const SCH_SHEET_PATH& sheet = subgraph->GetSheet();
719
720 if( nc )
721 net_record->m_HasNoConnect = true;
722
723 for( SCH_ITEM* item : subgraph->GetItems() )
724 {
725 if( item->Type() == SCH_PIN_T )
726 {
727 SCH_PIN* pin = static_cast<SCH_PIN*>( item );
728 SCH_SYMBOL* symbol = pin->GetParentSymbol();
729
730 if( !symbol
731 || ( ( aCtl & GNL_OPT_BOM ) && symbol->GetExcludedFromBOM() )
732 || ( ( aCtl & GNL_OPT_KICAD ) && symbol->GetExcludedFromBoard() ) )
733 {
734 continue;
735 }
736
737 net_record->m_Nodes.emplace_back( pin, sheet );
738 }
739 }
740 }
741 }
742
743 // Netlist ordering: Net name, then ref des, then pin name
744 std::sort( nets.begin(), nets.end(),
745 []( const NET_RECORD* a, const NET_RECORD*b )
746 {
747 return StrNumCmp( a->m_Name, b->m_Name ) < 0;
748 } );
749
750 for( int i = 0; i < (int) nets.size(); ++i )
751 {
752 NET_RECORD* net_record = nets[i];
753 bool added = false;
754 XNODE* xnode;
755
756 // Netlist ordering: Net name, then ref des, then pin name
757 std::sort( net_record->m_Nodes.begin(), net_record->m_Nodes.end(),
758 []( const NET_NODE& a, const NET_NODE& b )
759 {
760 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
761 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
762
763 if( refA == refB )
764 return a.m_Pin->GetShownNumber() < b.m_Pin->GetShownNumber();
765
766 return refA < refB;
767 } );
768
769 // Some duplicates can exist, for example on multi-unit parts with duplicated pins across
770 // units. If the user connects the pins on each unit, they will appear on separate
771 // subgraphs. Remove those here:
772 alg::remove_duplicates( net_record->m_Nodes,
773 []( const NET_NODE& a, const NET_NODE& b )
774 {
775 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
776 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
777
778 return refA == refB && a.m_Pin->GetShownNumber() == b.m_Pin->GetShownNumber();
779 } );
780
781 // Determine if all pins in the net are stacked (nets with only one pin are implicitly
782 // taken to be stacked)
783 bool allNetPinsStacked = true;
784
785 if( net_record->m_Nodes.size() > 1 )
786 {
787 SCH_PIN* firstPin = net_record->m_Nodes.begin()->m_Pin;
788 allNetPinsStacked =
789 std::all_of( net_record->m_Nodes.begin() + 1, net_record->m_Nodes.end(),
790 [=]( auto& node )
791 {
792 return firstPin->GetParent() == node.m_Pin->GetParent()
793 && firstPin->GetPosition() == node.m_Pin->GetPosition()
794 && firstPin->GetName() == node.m_Pin->GetName();
795 } );
796 }
797
798 for( const NET_NODE& netNode : net_record->m_Nodes )
799 {
800 wxString refText = netNode.m_Pin->GetParentSymbol()->GetRef( &netNode.m_Sheet );
801 wxString pinText = netNode.m_Pin->GetShownNumber();
802
803 // Skip power symbols and virtual symbols
804 if( refText[0] == wxChar( '#' ) )
805 continue;
806
807 if( !added )
808 {
809 netCodeTxt.Printf( wxT( "%d" ), i + 1 );
810
811 xnets->AddChild( xnet = node( wxT( "net" ) ) );
812 xnet->AddAttribute( wxT( "code" ), netCodeTxt );
813 xnet->AddAttribute( wxT( "name" ), net_record->m_Name );
814
815 added = true;
816 }
817
818 xnet->AddChild( xnode = node( wxT( "node" ) ) );
819 xnode->AddAttribute( wxT( "ref" ), refText );
820 xnode->AddAttribute( wxT( "pin" ), pinText );
821
822 wxString pinName = netNode.m_Pin->GetShownName();
823 wxString pinType = netNode.m_Pin->GetCanonicalElectricalTypeName();
824
825 if( !pinName.IsEmpty() )
826 xnode->AddAttribute( wxT( "pinfunction" ), pinName );
827
828 if( net_record->m_HasNoConnect
829 && ( net_record->m_Nodes.size() == 1 || allNetPinsStacked ) )
830 pinType += wxT( "+no_connect" );
831
832 xnode->AddAttribute( wxT( "pintype" ), pinType );
833 }
834 }
835
836 for( NET_RECORD* record : nets )
837 delete record;
838
839 return xnets;
840}
841
842
843XNODE* NETLIST_EXPORTER_XML::node( const wxString& aName,
844 const wxString& aTextualContent /* = wxEmptyString*/ )
845{
846 XNODE* n = new XNODE( wxXML_ELEMENT_NODE, aName );
847
848 if( aTextualContent.Len() > 0 ) // excludes wxEmptyString, the parameter's default value
849 n->AddChild( new XNODE( wxXML_TEXT_NODE, wxEmptyString, aTextualContent ) );
850
851 return n;
852}
853
854
855static bool sortPinsByNumber( LIB_PIN* aPin1, LIB_PIN* aPin2 )
856{
857 // return "lhs < rhs"
858 return StrNumCmp( aPin1->GetShownNumber(), aPin2->GetShownNumber(), true ) < 0;
859}
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:482
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:95
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:62
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
wxString GetShownNumber() const
Definition: lib_pin.h:118
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.
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_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:52
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: sch_field.cpp:1000
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition: sch_field.cpp:188
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:151
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:109
int GetUnitCount() const
Return the number of units per package of the symbol.
Definition: sch_symbol.cpp:483
int GetFieldCount() const
Return the number of fields in this symbol.
Definition: sch_symbol.h:588
bool UseLibIdLookup() const
Definition: sch_symbol.h:220
wxString GetSchSymbolLibraryName() const
Definition: sch_symbol.cpp:293
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:748
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:950
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
Definition: sch_symbol.cpp:934
wxString GetDescription() const
Definition: sch_symbol.cpp:311
bool GetExcludedFromBOM() const
Definition: sch_symbol.h:857
const wxString GetValueFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
Definition: sch_symbol.cpp:918
int GetUnitSelection(const SCH_SHEET_PATH *aSheet) const
Return the instance-specific unit selection for the given sheet path.
Definition: sch_symbol.cpp:876
const LIB_ID & GetLibId() const
Definition: sch_symbol.h:203
bool GetExcludedFromBoard() const
Definition: sch_symbol.h:860
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with SCH_FIELDs.
Definition: sch_symbol.cpp:998
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:222
bool GetDNP() const
Definition: sch_symbol.h:863
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:61
void remove_duplicates(_Container &__c)
Deletes all duplicate values from __c.
Definition: kicad_algo.h:183
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 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:143
@ SCH_SYMBOL_T
Definition: typeinfo.h:156
@ SCH_PIN_T
Definition: typeinfo.h:159