KiCad PCB EDA Suite
Loading...
Searching...
No Matches
netlist_exporter_allegro.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) 2023, 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <confirm.h>
25#include <refdes_utils.h>
26#include <sch_edit_frame.h>
27#include <sch_reference_list.h>
28#include <string_utils.h>
29#include <connection_graph.h>
30#include <core/kicad_algo.h>
31#include <symbol_library.h>
32#include <symbol_lib_table.h>
33#include <netlist.h>
36#include <regex>
37
38
39bool NETLIST_EXPORTER_ALLEGRO::WriteNetlist( const wxString& aOutFileName,
40 unsigned /* aNetlistOptions */,
41 REPORTER& aReporter )
42{
43 m_f = nullptr;
44 wxString field;
45 wxString footprint;
46 int ret = 0; // zero now, OR in the sign bit on error
47 wxString netName;
48
49 // Create the devices directory
50 m_exportPath = wxFileName( aOutFileName ).GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR )
51 + wxString( "devices" );
52 if( !wxDirExists( m_exportPath ) )
53 {
54 if( !wxMkdir( m_exportPath, wxS_DIR_DEFAULT ) )
55 {
56 wxString msg = wxString::Format( _( "Failed to create directory 'devices' ." ) );
57 aReporter.Report( msg, RPT_SEVERITY_ERROR );
58 return false;
59 }
60 }
61
62 // Write the netlist file
63 if( ( m_f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
64 {
65 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), aOutFileName );
66 aReporter.Report( msg, RPT_SEVERITY_ERROR );
67 return false;
68 }
69
70 ret |= fprintf( m_f, "(Source: %s)\n", TO_UTF8( m_schematic->GetFileName() ) );
71 ret |= fprintf( m_f, "(Date: %s)\n", TO_UTF8( GetISO8601CurrentDateTime() ) );
72
73 m_packageProperties.clear();
74 m_componentGroups.clear();
76 m_netNameNodes.clear();
77
79
80 // Start with package definitions, which we create from component groups.
82
83 // Write out nets
85
86 // Write out package properties. NOTE: Allegro doesn't recognize much...
88
89 // Done with the netlist
90 fclose( m_f );
91
92 m_f = nullptr;
93
94 return ret >= 0;
95}
96
97
99 SCH_SHEET_PATH>& aItem1,
100 const std::pair<SCH_SYMBOL*,
101 SCH_SHEET_PATH>& aItem2 )
102{
103 wxString refText1 = aItem1.first->GetRef( &aItem1.second );
104 wxString refText2 = aItem2.first->GetRef( &aItem2.second );
105
106 if( refText1 == refText2 )
107 {
108 return aItem1.second.PathHumanReadable() < aItem2.second.PathHumanReadable();
109 }
110
111 return CompareSymbolRef( refText1, refText2 );
112}
113
114
115bool NETLIST_EXPORTER_ALLEGRO::CompareSymbolRef( const wxString& aRefText1,
116 const wxString& aRefText2 )
117{
118 if( removeTailDigits( aRefText1 ) == removeTailDigits( aRefText2 ) )
119 {
120 return extractTailNumber( aRefText1 ) < extractTailNumber( aRefText2 );
121 }
122
123 return aRefText1 < aRefText2;
124}
125
126
128{
129 // return "lhs < rhs"
130 return StrNumCmp( aPin1->GetShownNumber(), aPin2->GetShownNumber(), true ) < 0;
131}
132
133
135{
137 m_libParts.clear();
138
139 for( const SCH_SHEET_PATH& sheet : m_schematic->Hierarchy() )
140 {
142
143 auto cmp =
144 [sheet]( SCH_SYMBOL* a, SCH_SYMBOL* b )
145 {
146 return ( StrNumCmp( a->GetRef( &sheet, false ),
147 b->GetRef( &sheet, false ), true ) < 0 );
148 };
149
150 std::set<SCH_SYMBOL*, decltype( cmp )> ordered_symbols( cmp );
151 std::multiset<SCH_SYMBOL*, decltype( cmp )> extra_units( cmp );
152
153 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
154 {
155 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
156 auto test = ordered_symbols.insert( symbol );
157
158 if( !test.second )
159 {
160 if( ( *( test.first ) )->m_Uuid > symbol->m_Uuid )
161 {
162 extra_units.insert( *( test.first ) );
163 ordered_symbols.erase( test.first );
164 ordered_symbols.insert( symbol );
165 }
166 else
167 {
168 extra_units.insert( symbol );
169 }
170 }
171 }
172
173 for( EDA_ITEM* item : ordered_symbols )
174 {
175 SCH_SYMBOL* symbol = findNextSymbol( item, sheet );
176
177 if( !symbol || symbol->GetExcludedFromBoard() )
178 continue;
179
180 if( symbol->GetLibPins().empty() )
181 continue;
182
183 m_packageProperties.insert( std::pair<wxString,
184 wxString>( sheet.PathHumanReadable(),
185 symbol->GetRef( &sheet ) ) );
186 m_orderedSymbolsSheetpath.push_back( std::pair<SCH_SYMBOL*,
187 SCH_SHEET_PATH>( symbol, sheet ) );
188 }
189 }
190
191 wxString netCodeTxt;
192 wxString netName;
193 wxString ref;
194
195 struct NET_RECORD
196 {
197 NET_RECORD( const wxString& aName ) :
198 m_Name( aName )
199 {};
200
201 wxString m_Name;
202 std::vector<NET_NODE> m_Nodes;
203 };
204
205 std::vector<NET_RECORD*> nets;
206
207 for( const auto& it : m_schematic->ConnectionGraph()->GetNetMap() )
208 {
209 wxString net_name = it.first.Name;
210 const std::vector<CONNECTION_SUBGRAPH*>& subgraphs = it.second;
211 NET_RECORD* net_record = nullptr;
212
213 if( subgraphs.empty() )
214 continue;
215
216 nets.emplace_back( new NET_RECORD( net_name ) );
217 net_record = nets.back();
218
219 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
220 {
221 bool nc = subgraph->GetNoConnect() &&
222 subgraph->GetNoConnect()->Type() == SCH_NO_CONNECT_T;
223 const SCH_SHEET_PATH& sheet = subgraph->GetSheet();
224
225 for( SCH_ITEM* item : subgraph->GetItems() )
226 {
227 if( item->Type() == SCH_PIN_T )
228 {
229 SCH_PIN* pin = static_cast<SCH_PIN*>( item );
230 SYMBOL* symbol = pin->GetParentSymbol();
231
232 if( !symbol || symbol->GetExcludedFromBoard() )
233 continue;
234
235 net_record->m_Nodes.emplace_back( pin, sheet, nc );
236 }
237 }
238 }
239 }
240
241 // Netlist ordering: Net name, then ref des, then pin name
242 std::sort( nets.begin(), nets.end(),
243 []( const NET_RECORD* a, const NET_RECORD*b )
244 {
245 return StrNumCmp( a->m_Name, b->m_Name ) < 0;
246 } );
247
248 for( NET_RECORD* net_record : nets )
249 {
250 // Netlist ordering: Net name, then ref des, then pin name
251 std::sort( net_record->m_Nodes.begin(), net_record->m_Nodes.end(),
252 []( const NET_NODE& a, const NET_NODE& b )
253 {
254 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
255 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
256
257 if( refA == refB )
258 return a.m_Pin->GetShownNumber() < b.m_Pin->GetShownNumber();
259
260 return refA < refB;
261 } );
262
263 // Some duplicates can exist, for example on multi-unit parts with duplicated pins across
264 // units. If the user connects the pins on each unit, they will appear on separate
265 // subgraphs. Remove those here:
266 alg::remove_duplicates( net_record->m_Nodes,
267 []( const NET_NODE& a, const NET_NODE& b )
268 {
269 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
270 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
271
272 return refA == refB && a.m_Pin->GetShownNumber() == b.m_Pin->GetShownNumber();
273 } );
274
275 for( const NET_NODE& netNode : net_record->m_Nodes )
276 {
277 wxString refText = netNode.m_Pin->GetParentSymbol()->GetRef( &netNode.m_Sheet );
278 wxString pinText = netNode.m_Pin->GetShownNumber();
279
280 // Skip power symbols and virtual symbols
281 if( refText[0] == wxChar( '#' ) )
282 {
283 continue;
284 }
285
286 m_netNameNodes.insert( std::pair<wxString, NET_NODE>( net_record->m_Name, netNode ) );
287 }
288 }
289
290 for( NET_RECORD* record : nets )
291 delete record;
292}
293
294
296{
297 int groupCount = 1;
298 wxString deviceFileCreatingError = wxString( "" );
299
300 //Group the components......
301 while(!m_orderedSymbolsSheetpath.empty())
302 {
303 std::pair<SCH_SYMBOL*, SCH_SHEET_PATH> first_ele = m_orderedSymbolsSheetpath.front();
304 m_orderedSymbolsSheetpath.pop_front();
305 m_componentGroups.insert( std::pair<int, std::pair<SCH_SYMBOL*,
306 SCH_SHEET_PATH>>( groupCount, first_ele ) );
307
308 for( auto it = m_orderedSymbolsSheetpath.begin(); it != m_orderedSymbolsSheetpath.end();
309 ++it )
310 {
311 if( it->first->GetValue( false, &it->second, false )
312 != first_ele.first->GetValue( false, &first_ele.second, false ) )
313 {
314 continue;
315 }
316
317 if( it->first->GetFootprintFieldText( false, &it->second, false )
318 != first_ele.first->GetFootprintFieldText( false, &first_ele.second, false ) )
319 {
320 continue;
321 }
322
323 wxString ref1 = it->first->GetRef( &it->second );
324 wxString ref2 = first_ele.first->GetRef( &first_ele.second );
325
326 if( removeTailDigits( ref1 ) == removeTailDigits( ref2 ) )
327 {
328 m_componentGroups.insert( std::pair<int, std::pair<SCH_SYMBOL*,
329 SCH_SHEET_PATH>>( groupCount, ( *it ) ) );
330 it = m_orderedSymbolsSheetpath.erase( it );
331
332 if( std::distance( it, m_orderedSymbolsSheetpath.begin() ) > 0 )
333 it--;
334 else if( it == m_orderedSymbolsSheetpath.end() )
335 break;
336 }
337 }
338 groupCount++;
339 }
340
341 struct COMP_PACKAGE_STRUCT
342 {
343 wxString m_value;
344 wxString m_tolerance;
345 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> m_symbolSheetpaths;
346 };
347
348 COMP_PACKAGE_STRUCT compPackageStruct;
349 std::map<wxString, COMP_PACKAGE_STRUCT> compPackageMap;
350
351 for( int groupIndex = 1; groupIndex < groupCount; groupIndex++ )
352 {
353 auto pairIter = m_componentGroups.equal_range( groupIndex );
354 auto beginIter = pairIter.first;
355 auto endIter = pairIter.second;
356
357 SCH_SYMBOL* sym = ( beginIter->second ).first;
358 SCH_SHEET_PATH sheetPath = ( beginIter->second ).second;
359
360 wxString valueText = sym->GetValue( false, &sheetPath, false );
361 wxString footprintText = sym->GetFootprintFieldText( false, &sheetPath, false);
362 wxString deviceType = valueText + wxString("_") + footprintText;
363
364 while( deviceType.GetChar(deviceType.Length()-1) == '_' )
365 {
366 deviceType.RemoveLast();
367 }
368
369 deviceType = formatDevice( deviceType );
370
371 wxArrayString fieldArray;
372 fieldArray.Add( "Spice_Model" );
373 fieldArray.Add( "VALUE" );
374
375 wxString value = getGroupField( groupIndex, fieldArray );
376
377 fieldArray.clear();
378 fieldArray.Add( "TOLERANCE" );
379 fieldArray.Add( "TOL" );
380 wxString tol = getGroupField( groupIndex, fieldArray );
381
382 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths;
383
384 for( auto iter = beginIter; iter != endIter; iter++ )
385 {
386 symbolSheetpaths.push_back( std::pair<SCH_SYMBOL*,
387 SCH_SHEET_PATH>( iter->second.first,
388 iter->second.second ) );
389 }
390
391 std::stable_sort( symbolSheetpaths.begin(), symbolSheetpaths.end(),
393
394 compPackageStruct.m_value = value;
395 compPackageStruct.m_tolerance = tol;
396 compPackageStruct.m_symbolSheetpaths = symbolSheetpaths;
397 compPackageMap.insert( std::pair( deviceType, compPackageStruct ) );
398
399 // Write out the corresponding device file
400 FILE* d = nullptr;
401 wxString deviceFileName = wxFileName( m_exportPath, deviceType,
402 wxString( "txt" ) ).GetFullPath();
403
404 if( ( d = wxFopen( deviceFileName, wxT( "wt" ) ) ) == nullptr )
405 {
406 wxString msg;
407 msg.Printf( _( "Failed to create file '%s'.\n" ), deviceFileName );
408 deviceFileCreatingError += msg;
409 continue;
410 }
411
412 footprintText = footprintText.AfterLast( ':' );
413
414 wxArrayString footprintAlt;
415 wxArrayString footprintArray = sym->GetLibSymbolRef()->GetFPFilters();
416
417 for( const wxString& fp : footprintArray )
418 {
419 if( ( fp.Find( '*' ) != wxNOT_FOUND ) || ( fp.Find( '?' ) != wxNOT_FOUND ) )
420 {
421 continue;
422 }
423
424 footprintAlt.Add( fp.AfterLast( ':' ) );
425 }
426
427 if( footprintText.IsEmpty() )
428 {
429 if( !footprintAlt.IsEmpty() )
430 {
431 footprintText = footprintAlt[0];
432 footprintAlt.RemoveAt( 0 );
433 }
434 else
435 {
436 footprintText = deviceType;
437 }
438 }
439
440 fprintf( d, "PACKAGE '%s'\n", TO_UTF8( formatDevice( footprintText ) ) );
441 fprintf( d, "CLASS IC\n" );
442
443 std::vector<SCH_PIN*> pinList = sym->GetLibSymbolRef()->GetAllLibPins();
444
445 /*
446 * We must erase redundant Pins references in pinList
447 * These redundant pins exist because some pins are found more than one time when a
448 * symbol has multiple parts per package or has 2 representations (DeMorgan conversion).
449 * For instance, a 74ls00 has DeMorgan conversion, with different pin shapes, and
450 * therefore each pin appears 2 times in the list. Common pins (VCC, GND) can also be
451 * found more than once.
452 */
453 sort( pinList.begin(), pinList.end(), NETLIST_EXPORTER_ALLEGRO::CompareLibPin );
454
455 for( int ii = 0; ii < (int) pinList.size() - 1; ii++ )
456 {
457 if( pinList[ii]->GetNumber() == pinList[ii + 1]->GetNumber() )
458 {
459 // 2 pins have the same number, remove the redundant pin at index i+1
460 pinList.erase( pinList.begin() + ii + 1 );
461 ii--;
462 }
463 }
464
465 unsigned int pinCount = pinList.size();
466 fprintf( d, "PINCOUNT %u\n", pinCount );
467
468 if( pinCount > 0 )
469 {
470 fprintf( d, "%s", TO_UTF8( formatFunction( "main", pinList ) ) );
471 }
472
473 if( !value.IsEmpty() )
474 {
475 fprintf( d, "PACKAGEPROP VALUE %s\n", TO_UTF8( value ) );
476 }
477
478 if( !tol.IsEmpty() )
479 {
480 fprintf( d, "PACKAGEPROP TOL %s\n", TO_UTF8( tol ) );
481 }
482
483 if( !footprintAlt.IsEmpty() )
484 {
485 fprintf( d, "PACKAGEPROP ALT_SYMBOLS '(" );
486
487 wxString footprintAltSymbolsText;
488
489 for( const wxString& fp : footprintAlt )
490 {
491 footprintAltSymbolsText += fp + wxString( "," );
492 }
493
494 footprintAltSymbolsText.Truncate( footprintAltSymbolsText.Length() - 1 );
495 fprintf( d, "%s)'\n", TO_UTF8( footprintAltSymbolsText ) );
496 }
497
498 wxArrayString propArray;
499 propArray.Add( "PART_NUMBER" );
500 propArray.Add( "mpn" );
501 propArray.Add( "mfr_pn" );
502 wxString data = getGroupField( groupIndex, propArray );
503
504 if(!data.IsEmpty())
505 {
506 fprintf( d, "PACKAGEPROP %s %s\n", TO_UTF8( propArray[0] ), TO_UTF8( data ) );
507 }
508
509 propArray.clear();
510 propArray.Add( "HEIGHT" );
511 data = getGroupField( groupIndex, propArray );
512
513 if(!data.IsEmpty())
514 {
515 fprintf( d, "PACKAGEPROP %s %s\n", TO_UTF8( propArray[0] ), TO_UTF8( data ) );
516 }
517
518 fprintf( d, "END\n" );
519
520 fclose( d );
521 }
522
523 for( auto iter = compPackageMap.begin(); iter != compPackageMap.end(); iter++ )
524 {
525 wxString deviceType = iter->first;
526 wxString value = iter->second.m_value;
527 wxString tolerance = iter->second.m_tolerance;
528
529 if( value.IsEmpty() && tolerance.IsEmpty() )
530 {
531 fprintf( m_f, "!%s;", TO_UTF8( deviceType ) );
532 }
533 else if( tolerance.IsEmpty() )
534 {
535 fprintf( m_f, "!%s!%s;", TO_UTF8( deviceType ), TO_UTF8( value ) );
536 }
537 else
538 {
539 fprintf( m_f, "!%s!%s!%s;", TO_UTF8( deviceType ), TO_UTF8( value ),
540 TO_UTF8( tolerance ) );
541 }
542
543 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths =
544 iter->second.m_symbolSheetpaths;
545
546 for( const auto& [ sym, sheetPath ] : symbolSheetpaths)
547 fprintf( m_f, ",\n\t%s", TO_UTF8( sym->GetRef( &sheetPath ) ) );
548
549 fprintf( m_f, "\n" );
550 }
551
552 if( !deviceFileCreatingError.IsEmpty() )
553 DisplayError( nullptr, deviceFileCreatingError );
554}
555
556
557wxString NETLIST_EXPORTER_ALLEGRO::formatText( wxString aString )
558{
559 if( aString.IsEmpty() )
560 return wxEmptyString;
561
562 aString.Replace( "\u03BC", "u" );
563
564 std::regex reg( "[!']|[^ -~]" );
565 wxString processedString = wxString( std::regex_replace( std::string( aString ), reg, "?" ) );
566
567 std::regex search_reg( "[^a-zA-Z0-9_/]" );
568
569 if( std::regex_search( std::string( processedString ), search_reg ) )
570 return wxString( "'" ) + processedString + wxString( "'" );
571
572 return processedString;
573}
574
575
577{
578 wxString pinName4Telesis = aPin.GetName() + wxString( "__" ) + aPin.GetNumber();
579 std::regex reg( "[^A-Za-z0-9_+?/-]" );
580 return wxString( std::regex_replace( std::string( pinName4Telesis ), reg, "?" ) );
581}
582
583
584wxString NETLIST_EXPORTER_ALLEGRO::formatFunction( wxString aName, std::vector<SCH_PIN*> aPinList )
585{
586 aName.MakeUpper();
587 std::list<wxString> pinNameList;
588
589 std::stable_sort( aPinList.begin(), aPinList.end(), NETLIST_EXPORTER_ALLEGRO::CompareLibPin );
590
591 for( auto pin : aPinList )
592 pinNameList.push_back( formatPin( *pin ) );
593
594 wxString out_str = "";
595 wxString str;
596
597 out_str.Printf( wxT( "PINORDER %s " ), TO_UTF8( aName ) );
598
599 for( const wxString& pinName : pinNameList )
600 {
601 str.Printf( ",\n\t%s", TO_UTF8( pinName ) );
602 out_str += str;
603 }
604 out_str += wxString( "\n" );
605
606 str.Printf( wxT( "FUNCTION %s %s " ), TO_UTF8( aName ), TO_UTF8( aName ) );
607 out_str += str;
608
609 for( auto pin : aPinList )
610 {
611 str.Printf( ",\n\t%s", TO_UTF8( pin->GetNumber() ) );
612 out_str += str;
613 }
614
615 out_str += wxString( "\n" );
616
617 return out_str;
618}
619
620
621wxString NETLIST_EXPORTER_ALLEGRO::getGroupField( int aGroupIndex, const wxArrayString& aFieldArray,
622 bool aSanitize )
623{
624 auto pairIter = m_componentGroups.equal_range( aGroupIndex );
625
626 for( auto iter = pairIter.first; iter != pairIter.second; ++iter )
627 {
628 SCH_SYMBOL* sym = ( iter->second ).first;
629 SCH_SHEET_PATH sheetPath = ( iter->second ).second;
630
631 for( const wxString& field : aFieldArray )
632 {
633 if( SCH_FIELD* fld = sym->FindField( field, true, true ) )
634 {
635 wxString fieldText = fld->GetShownText( &sheetPath, true );
636
637 if( !fieldText.IsEmpty() )
638 {
639 if( aSanitize )
640 return formatText( fieldText );
641 else
642 return fieldText;
643 }
644 }
645 }
646 }
647
648 for( auto iter = pairIter.first; iter != pairIter.second; ++iter )
649 {
650 SCH_SYMBOL* sym = ( iter->second ).first;
651
652 for( const wxString& field : aFieldArray )
653 {
654 if( SCH_FIELD* fld = sym->GetLibSymbolRef()->FindField( field, true ) )
655 {
656 wxString fieldText = fld->GetShownText( false, 0 );
657
658 if( !fieldText.IsEmpty() )
659 {
660 if( aSanitize )
661 return formatText( fieldText );
662 else
663 return fieldText;
664 }
665 }
666 }
667 }
668
669 return wxEmptyString;
670}
671
672
673wxString NETLIST_EXPORTER_ALLEGRO::formatDevice( wxString aString )
674{
675 aString.MakeLower();
676 std::regex reg( "[^a-z0-9_-]" );
677 return wxString( std::regex_replace( std::string( aString ), reg, "_" ) );
678}
679
680
682{
683 fprintf( m_f, "%s\n", "$PACKAGES" );
684 fprintf( m_f, "%s\n", "$A_PROPERTIES" );
685
686 while( !m_packageProperties.empty() )
687 {
688 std::multimap<wxString, wxString>::iterator iter = m_packageProperties.begin();
689 wxString sheetPathText = iter->first;
690
691 fprintf( m_f, "ROOM %s;", TO_UTF8( formatText( sheetPathText ) ) );
692
693 std::vector<wxString> refTexts;
694
695 auto pairIter = m_packageProperties.equal_range( sheetPathText );
696
697 for( iter = pairIter.first; iter != pairIter.second; ++iter )
698 {
699 wxString refText = iter->second;
700 refTexts.push_back( refText );
701 }
702
703 m_packageProperties.erase( pairIter.first, pairIter.second );
704
705 std::stable_sort( refTexts.begin(), refTexts.end(),
707
708 for( const wxString& ref : refTexts )
709 fprintf( m_f, ",\n\t%s", TO_UTF8( ref ) );
710
711 fprintf( m_f, "\n" );
712 }
713}
714
715
717{
718 fprintf( m_f, "%s\n", "$NETS" );
719
720 while( !m_netNameNodes.empty() )
721 {
722 std::multimap<wxString, NET_NODE>::iterator iter = m_netNameNodes.begin();
723 std::vector<NET_NODE> netNodes;
724
725 wxString netName = iter->first;
726
727 fprintf( m_f, "%s;", TO_UTF8( formatText( netName ).MakeUpper() ) );
728
729 auto pairIter = m_netNameNodes.equal_range( netName );
730
731 for( iter = pairIter.first; iter != pairIter.second; ++iter )
732 {
733 NET_NODE netNode = iter->second;
734 netNodes.push_back( netNode );
735 }
736
737 m_netNameNodes.erase( pairIter.first, pairIter.second );
738
739 std::stable_sort( netNodes.begin(), netNodes.end() );
740
741 for( const NET_NODE& netNode : netNodes )
742 {
743 wxString refText = netNode.m_Pin->GetParentSymbol()->GetRef( &netNode.m_Sheet );
744 wxString pinText = netNode.m_Pin->GetShownNumber();
745 fprintf( m_f, ",\n\t%s.%s", TO_UTF8( refText ), TO_UTF8( pinText ) );
746 }
747
748 fprintf( m_f, "\n" );
749 }
750}
751
752
754{
755 while( ( aString.GetChar( aString.Length() - 1 ) >= '0' )
756 && ( aString.GetChar( aString.Length() - 1 ) <= '9' ) )
757 {
758 aString.RemoveLast();
759 }
760
761 return aString;
762}
763
764
765unsigned int NETLIST_EXPORTER_ALLEGRO::extractTailNumber( wxString aString )
766{
767 wxString numString;
768
769 while( ( aString.GetChar( aString.Length() - 1 ) >= '0' )
770 && ( aString.GetChar( aString.Length() - 1 ) <= '9' ) )
771 {
772 numString.insert( 0, aString.GetChar( aString.Length() - 1 ) );
773 aString.RemoveLast();
774 }
775
776 unsigned long val;
777
778 //From wxWidgets 3.1.6, here we can use ToUInt instead of ToULong function.
779 numString.ToULong( &val );
780 return (unsigned int) val;
781}
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:89
const KIID m_Uuid
Definition: eda_item.h:489
wxString formatDevice(wxString aString)
Convert a string into one safe for a Telesis device name.
wxString m_exportPath
Directory to store device files.
static unsigned int extractTailNumber(wxString aString)
Extract the str's tailing number.
FILE * m_f
File pointer for netlist file writing operation.
static wxString removeTailDigits(wxString aString)
Remove the str's tailing digits.
void toAllegroPackageProperties()
Write $A_PROPERTIES section.
bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions, REPORTER &aReporter) override
Write netlist to aOutFileName.
std::list< std::pair< SCH_SYMBOL *, SCH_SHEET_PATH > > m_orderedSymbolsSheetpath
Store the ordered symbols with sheetpath.
static bool CompareSymbolSheetpath(const std::pair< SCH_SYMBOL *, SCH_SHEET_PATH > &aItem1, const std::pair< SCH_SYMBOL *, SCH_SHEET_PATH > &aItem2)
Compare two std::pair<SCH_SYMBOL*, SCH_SHEET_PATH> variables.
wxString formatText(wxString aString)
Convert a string into Telesis-safe format.
std::multimap< wxString, wxString > m_packageProperties
wxString formatFunction(wxString aName, std::vector< SCH_PIN * > aPinList)
Generate the definition of a function in Telesis format, which consists of multiple declarations (PIN...
static bool CompareLibPin(const SCH_PIN *aPin1, const SCH_PIN *aPin2)
Compare two SCH_PIN* variables.
wxString formatPin(const SCH_PIN &aPin)
Generate a Telesis-compatible pin name from a pin node.
static bool CompareSymbolRef(const wxString &aRefText1, const wxString &aRefText2)
Compare two wxString variables.
wxString getGroupField(int aGroupIndex, const wxArrayString &aFieldArray, bool aSanitize=true)
Look up a field for a component group, which may have mismatched case, or the component group may not...
void toAllegroPackages()
Write the $PACKAGES section.
std::multimap< wxString, NET_NODE > m_netNameNodes
Store the NET_NODE with the net name.
std::multimap< int, std::pair< SCH_SYMBOL *, SCH_SHEET_PATH > > m_componentGroups
Store the component group.
void toAllegroNets()
Write the $NETS section.
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.
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
virtual void SetCurrentSheet(const SCH_SHEET_PATH &aPath)=0
virtual wxString GetFileName() const =0
virtual CONNECTION_GRAPH * ConnectionGraph() const =0
virtual SCH_SHEET_LIST Hierarchy() const =0
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
const SYMBOL * GetParentSymbol() const
Definition: sch_item.cpp:166
wxString GetShownNumber() const
Definition: sch_pin.cpp:505
const wxString & GetName() const
Definition: sch_pin.cpp:375
const wxString & GetNumber() const
Definition: sch_pin.h:117
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
Schematic symbol object.
Definition: sch_symbol.h:104
SCH_FIELD * FindField(const wxString &aFieldName, bool aIncludeDefaultFields=true, bool aCaseInsensitive=false)
Search for a SCH_FIELD with aFieldName.
const wxString GetValue(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const override
Definition: sch_symbol.cpp:907
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
Definition: sch_symbol.cpp:923
std::vector< SCH_PIN * > GetLibPins() const
Populate a vector with all the pins from the library object that match the current unit and bodyStyle...
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:212
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:737
A base class for LIB_SYMBOL and SCH_SYMBOL.
Definition: symbol.h:34
bool GetExcludedFromBoard() const
Definition: symbol.h:148
virtual const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const =0
void Clear()
Erase the record.
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:170
This file is part of the common library.
#define _(s)
void remove_duplicates(_Container &__c)
Deletes all duplicate values from __c.
Definition: kicad_algo.h:183
Collection of utility functions for component reference designators (refdes)
@ RPT_SEVERITY_ERROR
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
wxString GetISO8601CurrentDateTime()
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:398
Definition for symbol library class.
@ SCH_NO_CONNECT_T
Definition: typeinfo.h:160
@ SCH_SYMBOL_T
Definition: typeinfo.h:172
@ SCH_PIN_T
Definition: typeinfo.h:153