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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <confirm.h>
21#include <refdes_utils.h>
22#include <sch_edit_frame.h>
23#include <sch_reference_list.h>
24#include <string_utils.h>
25#include <core/kicad_algo.h>
26#include <netlist.h>
29#include <regex>
30#include <fmt.h>
31#include <fmt/ranges.h>
32
33bool NETLIST_EXPORTER_ALLEGRO::writeNetlist( const wxString& aOutFileName,
34 unsigned /* aNetlistOptions */,
35 REPORTER& aReporter )
36{
37 m_f = nullptr;
38 bool success = true;
39
40 // Create the devices directory
41 m_exportPath = wxFileName( aOutFileName ).GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR )
42 + wxString( "devices" );
43 if( !wxDirExists( m_exportPath ) )
44 {
45 if( !wxMkdir( m_exportPath, wxS_DIR_DEFAULT ) )
46 {
47 wxString msg = wxString::Format( _( "Failed to create directory 'devices' ." ) );
48 aReporter.Report( msg, RPT_SEVERITY_ERROR );
49 return false;
50 }
51 }
52
53 // Write the netlist file
54 if( ( m_f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
55 {
56 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), aOutFileName );
57 aReporter.Report( msg, RPT_SEVERITY_ERROR );
58 return false;
59 }
60
61 try
62 {
63 fmt::print( m_f, "(NETLIST)\n" );
64 fmt::print( m_f, "(Source: {})\n", TO_UTF8( m_schematic->GetFileName() ) );
65 fmt::print( m_f, "(Date: {})\n", TO_UTF8( GetISO8601CurrentDateTime() ) );
66
67 m_packageProperties.clear();
68 m_componentGroups.clear();
70 m_netNameNodes.clear();
71
73
74 // Start with package definitions, which we create from component groups.
76
77 // Write out package properties. NOTE: Allegro doesn't recognize much...
79
80 // Write out nets
82
83 fmt::print( m_f, "$END\n" );
84 }
85 catch (...)
86 {
87 success = false;
88 }
89
90 // Done with the netlist
91 fclose( m_f );
92
93 m_f = nullptr;
94
95 return success;
96}
97
98
100 SCH_SHEET_PATH>& aItem1,
101 const std::pair<SCH_SYMBOL*,
102 SCH_SHEET_PATH>& aItem2 )
103{
104 wxString refText1 = aItem1.first->GetRef( &aItem1.second );
105 wxString refText2 = aItem2.first->GetRef( &aItem2.second );
106
107 if( refText1 == refText2 )
108 {
109 return aItem1.second.PathHumanReadable() < aItem2.second.PathHumanReadable();
110 }
111
112 return CompareSymbolRef( refText1, refText2 );
113}
114
115
116bool NETLIST_EXPORTER_ALLEGRO::CompareSymbolRef( const wxString& aRefText1,
117 const wxString& aRefText2 )
118{
119 if( removeTailDigits( aRefText1 ) == removeTailDigits( aRefText2 ) )
120 {
121 return extractTailNumber( aRefText1 ) < extractTailNumber( aRefText2 );
122 }
123
124 return aRefText1 < aRefText2;
125}
126
127
129{
130 // return "lhs < rhs"
131 return StrNumCmp( aPin1->GetShownNumber(), aPin2->GetShownNumber(), true ) < 0;
132}
133
134
136{
138 m_libParts.clear();
139
140 for( const SCH_SHEET_PATH& sheet : m_exportSheets )
141 {
142 m_schematic->SetCurrentSheet( sheet );
143
144 auto cmp =
145 [&sheet]( SCH_SYMBOL* a, SCH_SYMBOL* b )
146 {
147 return ( StrNumCmp( a->GetRef( &sheet, false ),
148 b->GetRef( &sheet, false ), true ) < 0 );
149 };
150
151 std::set<SCH_SYMBOL*, decltype( cmp )> ordered_symbols( cmp );
152 std::multiset<SCH_SYMBOL*, decltype( cmp )> extra_units( cmp );
153
154 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
155 {
156 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
157 auto test = ordered_symbols.insert( symbol );
158
159 if( !test.second )
160 {
161 if( ( *( test.first ) )->m_Uuid > symbol->m_Uuid )
162 {
163 extra_units.insert( *( test.first ) );
164 ordered_symbols.erase( test.first );
165 ordered_symbols.insert( symbol );
166 }
167 else
168 {
169 extra_units.insert( symbol );
170 }
171 }
172 }
173
174 for( EDA_ITEM* item : ordered_symbols )
175 {
176 SCH_SYMBOL* symbol = findNextSymbol( item, sheet );
177
178 if( !symbol || symbol->GetExcludedFromBoard() )
179 continue;
180
181 if( symbol->GetLibPins().empty() )
182 continue;
183
184 m_packageProperties.insert( std::pair<wxString,
185 wxString>( formatRoom( sheet ),
186 symbol->GetRef( &sheet ) ) );
187 m_orderedSymbolsSheetpath.push_back( std::pair<SCH_SYMBOL*,
188 SCH_SHEET_PATH>( symbol, sheet ) );
189 }
190 }
191
192 struct NET_RECORD
193 {
194 NET_RECORD( const wxString& aName ) :
195 m_Name( aName )
196 {};
197
198 wxString m_Name;
199 std::vector<NET_NODE> m_Nodes;
200 };
201
202 std::vector<NET_RECORD*> nets;
203
204 for( const EXPORT_NET& net : m_exportNets )
205 {
206 nets.emplace_back( new NET_RECORD( net.name ) );
207 NET_RECORD* net_record = nets.back();
208
209 for( const auto& [pin, sheet] : net.pins )
210 {
211 SYMBOL* symbol = pin->GetParentSymbol();
212
213 if( symbol && !symbol->GetExcludedFromBoard() )
214 net_record->m_Nodes.emplace_back( pin, sheet );
215 }
216 }
217
218 // Netlist ordering: Net name, then ref des, then pin name
219 std::sort( nets.begin(), nets.end(),
220 []( const NET_RECORD* a, const NET_RECORD*b )
221 {
222 return StrNumCmp( a->m_Name, b->m_Name ) < 0;
223 } );
224
225 for( NET_RECORD* net_record : nets )
226 {
227 // Netlist ordering: Net name, then ref des, then pin name
228 std::sort( net_record->m_Nodes.begin(), net_record->m_Nodes.end(),
229 []( const NET_NODE& a, const NET_NODE& b )
230 {
231 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
232 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
233
234 if( refA == refB )
235 return a.m_Pin->GetShownNumber() < b.m_Pin->GetShownNumber();
236
237 return refA < refB;
238 } );
239
240 // Some duplicates can exist, for example on multi-unit parts with duplicated pins across
241 // units. If the user connects the pins on each unit, they will appear on separate
242 // subgraphs. Remove those here:
243 alg::remove_duplicates( net_record->m_Nodes,
244 []( const NET_NODE& a, const NET_NODE& b )
245 {
246 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
247 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
248
249 return refA == refB && a.m_Pin->GetShownNumber() == b.m_Pin->GetShownNumber();
250 } );
251
252 for( const NET_NODE& netNode : net_record->m_Nodes )
253 {
254 wxString refText = netNode.m_Pin->GetParentSymbol()->GetRef( &netNode.m_Sheet );
255
256 // Skip power symbols and virtual symbols
257 if( refText[0] == wxChar( '#' ) )
258 {
259 continue;
260 }
261
262 m_netNameNodes.insert( std::pair<wxString, NET_NODE>( net_record->m_Name, netNode ) );
263 }
264 }
265
266 for( NET_RECORD* record : nets )
267 delete record;
268}
269
270
272{
273 int groupCount = 1;
274 wxString deviceFileCreatingError = wxString( "" );
275
276 //Group the components......
277 while(!m_orderedSymbolsSheetpath.empty())
278 {
279 std::pair<SCH_SYMBOL*, SCH_SHEET_PATH> first_ele = m_orderedSymbolsSheetpath.front();
280 m_orderedSymbolsSheetpath.pop_front();
281 m_componentGroups.insert( std::pair<int, std::pair<SCH_SYMBOL*,
282 SCH_SHEET_PATH>>( groupCount, first_ele ) );
283
284 for( auto it = m_orderedSymbolsSheetpath.begin(); it != m_orderedSymbolsSheetpath.end();
285 ++it )
286 {
287 if( it->first->GetValue( &it->second, RAW_VALUE )
288 != first_ele.first->GetValue( &first_ele.second, RAW_VALUE ) )
289 {
290 continue;
291 }
292
293 if( it->first->GetFootprintFieldText( &it->second, RAW_VALUE )
294 != first_ele.first->GetFootprintFieldText( &first_ele.second, RAW_VALUE ) )
295 {
296 continue;
297 }
298
299 wxString ref1 = it->first->GetRef( &it->second );
300 wxString ref2 = first_ele.first->GetRef( &first_ele.second );
301
302 if( removeTailDigits( ref1 ) == removeTailDigits( ref2 ) )
303 {
304 m_componentGroups.insert( std::pair<int, std::pair<SCH_SYMBOL*,
305 SCH_SHEET_PATH>>( groupCount, ( *it ) ) );
306 it = m_orderedSymbolsSheetpath.erase( it );
307
308 if( m_orderedSymbolsSheetpath.size() == 0 )
309 break;
310 else
311 it--; // we want to test the new it element, so compensate the next ++it
312 }
313 }
314 groupCount++;
315 }
316
317 struct COMP_PACKAGE_STRUCT
318 {
319 wxString m_value;
320 wxString m_tolerance;
321 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> m_symbolSheetpaths;
322 };
323
324 COMP_PACKAGE_STRUCT compPackageStruct;
325 std::map<wxString, COMP_PACKAGE_STRUCT> compPackageMap;
326
327 for( int groupIndex = 1; groupIndex < groupCount; groupIndex++ )
328 {
329 auto pairIter = m_componentGroups.equal_range( groupIndex );
330 auto beginIter = pairIter.first;
331 auto endIter = pairIter.second;
332
333 SCH_SYMBOL* sym = ( beginIter->second ).first;
334 SCH_SHEET_PATH sheetPath = ( beginIter->second ).second;
335
336 wxString valueText = sym->GetValue( &sheetPath, RAW_VALUE );
337 wxString footprintText = sym->GetFootprintFieldText( &sheetPath, RAW_VALUE );
338 wxString deviceType = valueText + wxString("_") + footprintText;
339
340 while( deviceType.GetChar(deviceType.Length()-1) == '_' )
341 {
342 deviceType.RemoveLast();
343 }
344
345 deviceType = formatDevice( deviceType );
346
347 wxArrayString fieldArray;
348 fieldArray.Add( "Spice_Model" );
349 fieldArray.Add( "VALUE" );
350
351 wxString value = getGroupField( groupIndex, fieldArray );
352
353 fieldArray.clear();
354 fieldArray.Add( "TOLERANCE" );
355 fieldArray.Add( "TOL" );
356 wxString tol = getGroupField( groupIndex, fieldArray );
357
358 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths;
359
360 for( auto iter = beginIter; iter != endIter; iter++ )
361 {
362 symbolSheetpaths.push_back( std::pair<SCH_SYMBOL*,
363 SCH_SHEET_PATH>( iter->second.first,
364 iter->second.second ) );
365 }
366
367 std::stable_sort( symbolSheetpaths.begin(), symbolSheetpaths.end(),
369
370 compPackageStruct.m_value = value;
371 compPackageStruct.m_tolerance = tol;
372 compPackageStruct.m_symbolSheetpaths = symbolSheetpaths;
373 compPackageMap.insert( std::pair( deviceType, compPackageStruct ) );
374
375 // Write out the corresponding device file
376 FILE* d = nullptr;
377 wxString deviceFileName = wxFileName( m_exportPath, deviceType,
378 wxString( "txt" ) ).GetFullPath();
379
380 if( ( d = wxFopen( deviceFileName, wxT( "wt" ) ) ) == nullptr )
381 {
382 wxString msg;
383 msg.Printf( _( "Failed to create file '%s'.\n" ), deviceFileName );
384 deviceFileCreatingError += msg;
385 continue;
386 }
387
388 footprintText = footprintText.AfterLast( ':' );
389
390 wxArrayString footprintAlt;
391 wxArrayString footprintArray = sym->GetLibSymbolRef()->GetFPFilters();
392
393 for( const wxString& fp : footprintArray )
394 {
395 if( ( fp.Find( '*' ) != wxNOT_FOUND ) || ( fp.Find( '?' ) != wxNOT_FOUND ) )
396 {
397 continue;
398 }
399
400 footprintAlt.Add( fp.AfterLast( ':' ) );
401 }
402
403 if( footprintText.IsEmpty() )
404 {
405 if( !footprintAlt.IsEmpty() )
406 {
407 footprintText = footprintAlt[0];
408 footprintAlt.RemoveAt( 0 );
409 }
410 else
411 {
412 footprintText = deviceType;
413 }
414 }
415
416 fmt::print( d, "PACKAGE '{}'\n", TO_UTF8( formatDevice( footprintText ) ) );
417 fmt::print( d, "CLASS IC\n" );
418
419 std::vector<SCH_PIN*> pinList = sym->GetLibSymbolRef()->GetPins();
420
421 /*
422 * We must erase redundant Pins references in pinList
423 * These redundant pins exist because some pins are found more than one time when a
424 * symbol has multiple parts per package or has 2 representations (DeMorgan conversion).
425 * For instance, a 74ls00 has DeMorgan conversion, with different pin shapes, and
426 * therefore each pin appears 2 times in the list. Common pins (VCC, GND) can also be
427 * found more than once.
428 */
429 sort( pinList.begin(), pinList.end(), NETLIST_EXPORTER_ALLEGRO::CompareLibPin );
430
431 for( int ii = 0; ii < (int) pinList.size() - 1; ii++ )
432 {
433 if( pinList[ii]->GetNumber() == pinList[ii + 1]->GetNumber() )
434 {
435 // 2 pins have the same number, remove the redundant pin at index i+1
436 pinList.erase( pinList.begin() + ii + 1 );
437 ii--;
438 }
439 }
440
441 unsigned int pinCount = pinList.size();
442 fmt::print( d, "PINCOUNT {}\n", pinCount );
443
444 if( pinCount > 0 )
445 {
446 fmt::print( d, "{}", TO_UTF8( formatFunction( "main", pinList ) ) );
447 }
448
449 if( !value.IsEmpty() )
450 {
451 fmt::print( d, "PACKAGEPROP VALUE {}\n", TO_UTF8( value ) );
452 }
453
454 if( !tol.IsEmpty() )
455 {
456 fmt::print( d, "PACKAGEPROP TOL {}\n", TO_UTF8( tol ) );
457 }
458
459 if( !footprintAlt.IsEmpty() )
460 {
461 fmt::print( d, "PACKAGEPROP ALT_SYMBOLS '({})'\n", fmt::join( footprintAlt, "," ) );
462 }
463
464 wxArrayString propArray;
465 propArray.Add( "PART_NUMBER" );
466 propArray.Add( "mpn" );
467 propArray.Add( "mfr_pn" );
468 wxString data = getGroupField( groupIndex, propArray );
469
470 if(!data.IsEmpty())
471 {
472 fmt::print( d, "PACKAGEPROP {} {}\n", TO_UTF8( propArray[0] ), TO_UTF8( data ) );
473 }
474
475 propArray.clear();
476 propArray.Add( "HEIGHT" );
477 data = getGroupField( groupIndex, propArray );
478
479 if(!data.IsEmpty())
480 {
481 fmt::print( d, "PACKAGEPROP {} {}\n", TO_UTF8( propArray[0] ), TO_UTF8( data ) );
482 }
483
484 fmt::print( d, "END\n" );
485
486 fclose( d );
487 }
488
489 fmt::print( m_f, "$PACKAGES\n" );
490
491 for( auto iter = compPackageMap.begin(); iter != compPackageMap.end(); iter++ )
492 {
493 wxString deviceType = iter->first;
494 wxString value = iter->second.m_value;
495 wxString tolerance = iter->second.m_tolerance;
496
497 // Remove quotes in value (can be added by formatText), if any.
498 // (they are already in print format string)
499 if( value.StartsWith( "'" ) and value.EndsWith( "'" ) )
500 {
501 value.Remove( 0, 1 );
502 value.RemoveLast();
503 }
504
505 if( value.IsEmpty() && tolerance.IsEmpty() )
506 {
507 fmt::print( m_f, "! '{}' ; ", TO_UTF8( deviceType ) );
508 }
509 else if( tolerance.IsEmpty() )
510 {
511 fmt::print( m_f, "! '{}' ! '{}' ; ", TO_UTF8( deviceType ), TO_UTF8( value ) );
512 }
513 else
514 {
515 fmt::print( m_f, "! '{}' ! '{}' ! {} ; ", TO_UTF8( deviceType ), TO_UTF8( value ),
516 TO_UTF8( tolerance ) );
517 }
518
519 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths =
520 iter->second.m_symbolSheetpaths;
521
522 std::vector<wxString> refTexts;
523 for( const auto& [ sym, sheetPath ] : symbolSheetpaths)
524 refTexts.push_back( sym->GetRef( &sheetPath ) );
525
526 fmt::print( m_f, "{}", fmt::join( refTexts, ",\n\t" ) );
527
528 fmt::print( m_f, "\n" );
529 }
530
531 if( !deviceFileCreatingError.IsEmpty() )
532 DisplayError( nullptr, deviceFileCreatingError );
533}
534
535
536wxString NETLIST_EXPORTER_ALLEGRO::formatText( wxString aString )
537{
538 if( aString.IsEmpty() )
539 return wxEmptyString;
540
541 // Replace 'µ' ("\u00b5") by 'u' to keep ASCII7 constraint
542 wxString mu = "µ"; // also could be "\u03BC" (grec symbol mu);
543 aString.Replace( mu, "u" );
544 mu = "\u03BC"; // grec mu
545 aString.Replace( mu, "u" );
546
547 std::regex reg( "[!']|[^ -~]" );
548 wxString processedString = wxString( std::regex_replace( std::string( aString ), reg, "?" ) );
549
550 std::regex search_reg( "[^a-zA-Z0-9_/]" );
551
552 if( std::regex_search( std::string( processedString ), search_reg ) )
553 return wxString( "'" ) + processedString + wxString( "'" );
554
555 return processedString;
556}
557
558
560{
561 wxString pinName4Telesis = aPin.GetName() + wxString( "__" ) + aPin.GetNumber();
562 std::regex reg( "[^A-Za-z0-9_+?/-]" );
563 return wxString( std::regex_replace( std::string( pinName4Telesis ), reg, "?" ) );
564}
565
566
567wxString NETLIST_EXPORTER_ALLEGRO::formatFunction( wxString aName, std::vector<SCH_PIN*> aPinList )
568{
569 aName.MakeUpper();
570 std::list<wxString> pinNameList;
571
572 std::stable_sort( aPinList.begin(), aPinList.end(), NETLIST_EXPORTER_ALLEGRO::CompareLibPin );
573
574 for( auto pin : aPinList )
575 pinNameList.push_back( formatPin( *pin ) );
576
577 wxString out_str = "";
578 wxString str;
579
580 out_str.Printf( wxT( "PINORDER %s " ), TO_UTF8( aName ) );
581
582 for( const wxString& pinName : pinNameList )
583 {
584 str.Printf( ",\n\t%s", TO_UTF8( pinName ) );
585 out_str += str;
586 }
587 out_str += wxString( "\n" );
588
589 str.Printf( wxT( "FUNCTION %s %s " ), TO_UTF8( aName ), TO_UTF8( aName ) );
590 out_str += str;
591
592 for( auto pin : aPinList )
593 {
594 str.Printf( ",\n\t%s", TO_UTF8( pin->GetNumber() ) );
595 out_str += str;
596 }
597
598 out_str += wxString( "\n" );
599
600 return out_str;
601}
602
603
604wxString NETLIST_EXPORTER_ALLEGRO::getGroupField( int aGroupIndex, const wxArrayString& aFieldArray, bool aSanitize )
605{
606 auto pairIter = m_componentGroups.equal_range( aGroupIndex );
607
608 for( auto iter = pairIter.first; iter != pairIter.second; ++iter )
609 {
610 SCH_SYMBOL* sym = ( iter->second ).first;
611 SCH_SHEET_PATH sheetPath = ( iter->second ).second;
612
613 for( const wxString& field : aFieldArray )
614 {
615 if( SCH_FIELD* fld = sym->FindFieldCaseInsensitive( field ) )
616 {
617 // TODO: FOR_CANVAS seems like an odd context here....
618 wxString fieldText = fld->GetShownText( &sheetPath, FOR_CANVAS );
619
620 if( !fieldText.IsEmpty() )
621 {
622 if( aSanitize )
623 return formatText( fieldText );
624 else
625 return fieldText;
626 }
627 }
628 }
629 }
630
631 for( auto iter = pairIter.first; iter != pairIter.second; ++iter )
632 {
633 SCH_SYMBOL* sym = ( iter->second ).first;
634
635 for( const wxString& field : aFieldArray )
636 {
637 if( SCH_FIELD* fld = sym->GetLibSymbolRef()->FindFieldCaseInsensitive( field ) )
638 {
639 wxString fieldText = fld->GetShownText( RESOLVED );
640
641 if( !fieldText.IsEmpty() )
642 {
643 if( aSanitize )
644 return formatText( fieldText );
645 else
646 return fieldText;
647 }
648 }
649 }
650 }
651
652 return wxEmptyString;
653}
654
655
657{
658 wxString path = aSheetPath.PathHumanReadable();
659
660 // Use root schematic file name for root
661 if( path == wxS( "/" ) )
662 path = aSheetPath.PathHumanReadable( false );
663
664 // The leading and trailing separators carry no information; the ones in between become
665 // dashes below.
666 while( path.StartsWith( wxS( "/" ) ) )
667 path = path.Mid( 1 );
668
669 while( path.EndsWith( wxS( "/" ) ) )
670 path.RemoveLast();
671
672 wxString roomName;
673
674 for( wxUniChar c : path )
675 {
676 if( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || ( c >= '0' && c <= '9' )
677 || c == '_' || c == '-' )
678 {
679 roomName << c;
680 }
681 else if( c == '/' )
682 {
683 // Use dash to represent our hierarchy breaks
684 roomName << wxS( "-" );
685 }
686 else
687 {
688 roomName << wxS( "_" );
689 }
690 }
691
692 return roomName;
693}
694
695
696wxString NETLIST_EXPORTER_ALLEGRO::formatDevice( wxString aString )
697{
698 aString.MakeLower();
699 std::regex reg( "[^a-z0-9_-]" );
700 return wxString( std::regex_replace( std::string( aString ), reg, "_" ) );
701}
702
703
705{
706 fmt::print( m_f, "$A_PROPERTIES\n" );
707
708 while( !m_packageProperties.empty() )
709 {
710 std::multimap<wxString, wxString>::iterator iter = m_packageProperties.begin();
711 wxString roomName = iter->first;
712
713 std::vector<wxString> refTexts;
714
715 auto pairIter = m_packageProperties.equal_range( roomName );
716
717 for( iter = pairIter.first; iter != pairIter.second; ++iter )
718 {
719 wxString refText = iter->second;
720 refTexts.push_back( refText );
721 }
722
723 m_packageProperties.erase( pairIter.first, pairIter.second );
724
725 // Nothing to name the room after (a schematic with no file name yet); leave these
726 // symbols without a ROOM rather than writing out an empty property.
727 if( roomName.IsEmpty() )
728 continue;
729
730 // formatRoom() already restricted this to characters that need no quoting or escaping.
731 fmt::print( m_f, "'ROOM' '{}' ; ", TO_UTF8( roomName ) );
732
733 std::stable_sort( refTexts.begin(), refTexts.end(),
735
736 fmt::print( m_f, "{}", fmt::join( refTexts, ",\n\t" ) );
737
738 fmt::print( m_f, "\n" );
739 }
740}
741
742
744{
745 fmt::print( m_f, "$NETS\n" );
746
747 while( !m_netNameNodes.empty() )
748 {
749 std::multimap<wxString, NET_NODE>::iterator iter = m_netNameNodes.begin();
750 std::vector<NET_NODE> netNodes;
751
752 wxString netName = iter->first;
753
754 fmt::print( m_f, "{}; ", TO_UTF8( formatText( netName ).MakeUpper() ) );
755
756 auto pairIter = m_netNameNodes.equal_range( netName );
757
758 for( iter = pairIter.first; iter != pairIter.second; ++iter )
759 {
760 NET_NODE netNode = iter->second;
761 netNodes.push_back( netNode );
762 }
763
764 m_netNameNodes.erase( pairIter.first, pairIter.second );
765
766 std::stable_sort( netNodes.begin(), netNodes.end() );
767
768 std::vector<wxString> nets;
769 for( const NET_NODE& netNode : netNodes )
770 {
771 wxString refText = netNode.m_Pin->GetParentSymbol()->GetRef( &netNode.m_Sheet );
772 wxString pinText = netNode.m_Pin->GetShownNumber();
773 nets.push_back( refText + wxString( "." ) + pinText );
774 }
775
776 fmt::print( m_f, "{}", fmt::join( nets, ",\n\t" ) );
777
778 fmt::print( m_f, "\n" );
779 }
780}
781
782
784{
785 while( ( aString.GetChar( aString.Length() - 1 ) >= '0' )
786 && ( aString.GetChar( aString.Length() - 1 ) <= '9' ) )
787 {
788 aString.RemoveLast();
789 }
790
791 return aString;
792}
793
794
795unsigned int NETLIST_EXPORTER_ALLEGRO::extractTailNumber( wxString aString )
796{
797 wxString numString;
798
799 while( ( aString.GetChar( aString.Length() - 1 ) >= '0' )
800 && ( aString.GetChar( aString.Length() - 1 ) <= '9' ) )
801 {
802 numString.insert( 0, aString.GetChar( aString.Length() - 1 ) );
803 aString.RemoveLast();
804 }
805
806 unsigned long val;
807
808 //From wxWidgets 3.1.6, here we can use ToUInt instead of ToULong function.
809 numString.ToULong( &val );
810 return (unsigned int) val;
811}
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
const KIID m_Uuid
Definition eda_item.h:597
SCH_FIELD * FindFieldCaseInsensitive(const wxString &aFieldName)
wxArrayString GetFPFilters() const
Definition lib_symbol.h:247
std::vector< SCH_PIN * > GetPins() const override
static wxString formatRoom(const SCH_SHEET_PATH &aSheetPath)
Generate an Allegro room name from a sheet path.
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.
std::list< std::pair< SCH_SYMBOL *, SCH_SHEET_PATH > > m_orderedSymbolsSheetpath
Store the ordered symbols with sheetpath.
bool writeNetlist(const wxString &aOutFileName, unsigned aNetlistOptions, REPORTER &aReporter) override
Write netlist to aOutFileName.
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.
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.
std::vector< EXPORT_NET > m_exportNets
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:165
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:287
const wxString & GetName() const
Definition sch_pin.cpp:503
const wxString & GetShownNumber() const
Definition sch_pin.cpp:691
const wxString & GetNumber() const
Definition sch_pin.h:142
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
wxString PathHumanReadable(bool aUseShortRootName=true, bool aStripTrailingSeparator=false, bool aEscapeSheetNames=false) const
Return the sheet path in a human readable form made from the sheet names.
Schematic symbol object.
Definition sch_symbol.h:75
const wxString GetFootprintFieldText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString) const
SCH_FIELD * FindFieldCaseInsensitive(const wxString &aFieldName)
Search for a SCH_FIELD with aFieldName.
std::vector< SCH_PIN * > GetLibPins() const
Populate a vector with all the pins from the library object that match the current unit and bodyStyle...
bool GetExcludedFromBoard(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
const wxString GetValue(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString) const override
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:183
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
A base class for LIB_SYMBOL and SCH_SYMBOL.
Definition symbol.h:59
virtual const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const =0
bool GetExcludedFromBoard(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
Definition symbol.h:212
@ FOR_CANVAS
Definition common.h:88
@ RAW_VALUE
Definition common.h:94
@ RESOLVED
Definition common.h:93
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
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:157
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.
std::string path
KIBIS_PIN * pin
@ SCH_SYMBOL_T
Definition typeinfo.h:168