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