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*, SCH_SHEET_PATH>>( groupCount, first_ele ) );
282
283 for( auto it = m_orderedSymbolsSheetpath.begin(); it != m_orderedSymbolsSheetpath.end(); ++it )
284 {
285 if( it->first->GetValue( &it->second, RAW_VALUE )
286 != first_ele.first->GetValue( &first_ele.second, RAW_VALUE ) )
287 {
288 continue;
289 }
290
291 if( it->first->GetFootprintFieldText( &it->second, RAW_VALUE )
292 != first_ele.first->GetFootprintFieldText( &first_ele.second, RAW_VALUE ) )
293 {
294 continue;
295 }
296
297 wxString ref1 = it->first->GetRef( &it->second );
298 wxString ref2 = first_ele.first->GetRef( &first_ele.second );
299
300 if( removeTailDigits( ref1 ) == removeTailDigits( ref2 ) )
301 {
302 m_componentGroups.insert( std::pair<int, std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>>( groupCount,
303 ( *it ) ) );
304 it = m_orderedSymbolsSheetpath.erase( it );
305
306 if( m_orderedSymbolsSheetpath.size() == 0 )
307 break;
308 else
309 it--; // we want to test the new it element, so compensate the next ++it
310 }
311 }
312
313 groupCount++;
314 }
315
316 struct COMP_PACKAGE_STRUCT
317 {
318 wxString m_value;
319 wxString m_tolerance;
320 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> m_symbolSheetpaths;
321 };
322
323 COMP_PACKAGE_STRUCT compPackageStruct;
324 std::map<wxString, COMP_PACKAGE_STRUCT> compPackageMap;
325
326 for( int groupIndex = 1; groupIndex < groupCount; groupIndex++ )
327 {
328 auto pairIter = m_componentGroups.equal_range( groupIndex );
329 auto beginIter = pairIter.first;
330 auto endIter = pairIter.second;
331
332 SCH_SYMBOL* sym = ( beginIter->second ).first;
333 SCH_SHEET_PATH sheetPath = ( beginIter->second ).second;
334
335 wxString valueText = sym->GetValue( &sheetPath, RAW_VALUE );
336 wxString footprintText = sym->GetFootprintFieldText( &sheetPath, RAW_VALUE );
337 wxString deviceType = valueText + wxString("_") + footprintText;
338
339 while( deviceType.GetChar(deviceType.Length()-1) == '_' )
340 {
341 deviceType.RemoveLast();
342 }
343
344 deviceType = formatDevice( deviceType );
345
346 wxArrayString fieldArray;
347 fieldArray.Add( "Spice_Model" );
348 fieldArray.Add( "VALUE" );
349
350 wxString value = getGroupField( groupIndex, fieldArray );
351
352 fieldArray.clear();
353 fieldArray.Add( "TOLERANCE" );
354 fieldArray.Add( "TOL" );
355 wxString tol = getGroupField( groupIndex, fieldArray );
356
357 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths;
358
359 for( auto iter = beginIter; iter != endIter; iter++ )
360 {
361 symbolSheetpaths.push_back( std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>( iter->second.first,
362 iter->second.second ) );
363 }
364
365 std::stable_sort( symbolSheetpaths.begin(), symbolSheetpaths.end(),
367
368 compPackageStruct.m_value = value;
369 compPackageStruct.m_tolerance = tol;
370 compPackageStruct.m_symbolSheetpaths = symbolSheetpaths;
371 compPackageMap.insert( std::pair( deviceType, compPackageStruct ) );
372
373 // Write out the corresponding device file
374 FILE* d = nullptr;
375 wxString deviceFileName = wxFileName( m_exportPath, deviceType, wxString( "txt" ) ).GetFullPath();
376
377 if( ( d = wxFopen( deviceFileName, wxT( "wt" ) ) ) == nullptr )
378 {
379 wxString msg;
380 msg.Printf( _( "Failed to create file '%s'.\n" ), deviceFileName );
381 deviceFileCreatingError += msg;
382 continue;
383 }
384
385 footprintText = footprintText.AfterLast( ':' );
386
387 wxArrayString footprintAlt;
388 wxArrayString footprintArray = sym->GetLibSymbolRef()->GetFPFilters();
389
390 for( const wxString& fp : footprintArray )
391 {
392 if( ( fp.Find( '*' ) != wxNOT_FOUND ) || ( fp.Find( '?' ) != wxNOT_FOUND ) )
393 {
394 continue;
395 }
396
397 footprintAlt.Add( fp.AfterLast( ':' ) );
398 }
399
400 if( footprintText.IsEmpty() )
401 {
402 if( !footprintAlt.IsEmpty() )
403 {
404 footprintText = footprintAlt[0];
405 footprintAlt.RemoveAt( 0 );
406 }
407 else
408 {
409 footprintText = deviceType;
410 }
411 }
412
413 fmt::print( d, "PACKAGE '{}'\n", TO_UTF8( formatDevice( footprintText ) ) );
414 fmt::print( d, "CLASS IC\n" );
415
416 std::vector<SCH_PIN*> pinList = sym->GetLibSymbolRef()->GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES );
417
418 /*
419 * We must erase redundant Pins references in pinList
420 * These redundant pins exist because some pins are found more than one time when a
421 * symbol has multiple parts per package or has 2 representations (DeMorgan conversion).
422 * For instance, a 74ls00 has DeMorgan conversion, with different pin shapes, and
423 * therefore each pin appears 2 times in the list. Common pins (VCC, GND) can also be
424 * found more than once.
425 */
426 sort( pinList.begin(), pinList.end(), NETLIST_EXPORTER_ALLEGRO::CompareLibPin );
427
428 for( int ii = 0; ii < (int) pinList.size() - 1; ii++ )
429 {
430 if( pinList[ii]->GetNumber() == pinList[ii + 1]->GetNumber() )
431 {
432 // 2 pins have the same number, remove the redundant pin at index i+1
433 pinList.erase( pinList.begin() + ii + 1 );
434 ii--;
435 }
436 }
437
438 unsigned int pinCount = pinList.size();
439 fmt::print( d, "PINCOUNT {}\n", pinCount );
440
441 if( pinCount > 0 )
442 {
443 fmt::print( d, "{}", TO_UTF8( formatFunction( "main", pinList ) ) );
444 }
445
446 if( !value.IsEmpty() )
447 {
448 fmt::print( d, "PACKAGEPROP VALUE {}\n", TO_UTF8( value ) );
449 }
450
451 if( !tol.IsEmpty() )
452 {
453 fmt::print( d, "PACKAGEPROP TOL {}\n", TO_UTF8( tol ) );
454 }
455
456 if( !footprintAlt.IsEmpty() )
457 {
458 fmt::print( d, "PACKAGEPROP ALT_SYMBOLS '({})'\n", fmt::join( footprintAlt, "," ) );
459 }
460
461 wxArrayString propArray;
462 propArray.Add( "PART_NUMBER" );
463 propArray.Add( "mpn" );
464 propArray.Add( "mfr_pn" );
465 wxString data = getGroupField( groupIndex, propArray );
466
467 if(!data.IsEmpty())
468 {
469 fmt::print( d, "PACKAGEPROP {} {}\n", TO_UTF8( propArray[0] ), TO_UTF8( data ) );
470 }
471
472 propArray.clear();
473 propArray.Add( "HEIGHT" );
474 data = getGroupField( groupIndex, propArray );
475
476 if(!data.IsEmpty())
477 {
478 fmt::print( d, "PACKAGEPROP {} {}\n", TO_UTF8( propArray[0] ), TO_UTF8( data ) );
479 }
480
481 fmt::print( d, "END\n" );
482
483 fclose( d );
484 }
485
486 fmt::print( m_f, "$PACKAGES\n" );
487
488 for( auto iter = compPackageMap.begin(); iter != compPackageMap.end(); iter++ )
489 {
490 wxString deviceType = iter->first;
491 wxString value = iter->second.m_value;
492 wxString tolerance = iter->second.m_tolerance;
493
494 // Remove quotes in value (can be added by formatText), if any.
495 // (they are already in print format string)
496 if( value.StartsWith( "'" ) and value.EndsWith( "'" ) )
497 {
498 value.Remove( 0, 1 );
499 value.RemoveLast();
500 }
501
502 if( value.IsEmpty() && tolerance.IsEmpty() )
503 {
504 fmt::print( m_f, "! '{}' ; ", TO_UTF8( deviceType ) );
505 }
506 else if( tolerance.IsEmpty() )
507 {
508 fmt::print( m_f, "! '{}' ! '{}' ; ", TO_UTF8( deviceType ), TO_UTF8( value ) );
509 }
510 else
511 {
512 fmt::print( m_f, "! '{}' ! '{}' ! {} ; ", TO_UTF8( deviceType ), TO_UTF8( value ),
513 TO_UTF8( tolerance ) );
514 }
515
516 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths =
517 iter->second.m_symbolSheetpaths;
518
519 std::vector<wxString> refTexts;
520 for( const auto& [ sym, sheetPath ] : symbolSheetpaths)
521 refTexts.push_back( sym->GetRef( &sheetPath ) );
522
523 fmt::print( m_f, "{}", fmt::join( refTexts, ",\n\t" ) );
524
525 fmt::print( m_f, "\n" );
526 }
527
528 if( !deviceFileCreatingError.IsEmpty() )
529 DisplayError( nullptr, deviceFileCreatingError );
530}
531
532
533wxString NETLIST_EXPORTER_ALLEGRO::formatText( wxString aString )
534{
535 if( aString.IsEmpty() )
536 return wxEmptyString;
537
538 // Replace 'µ' ("\u00b5") by 'u' to keep ASCII7 constraint
539 wxString mu( wxUniChar( 0x00B5 ) );
540 aString.Replace( mu, "u" );
541 mu = wxUniChar( 0x03BC );
542 aString.Replace( mu, "u" );
543
544 std::regex reg( "[!']|[^ -~]" );
545 wxString processedString = wxString( std::regex_replace( std::string( aString ), reg, "?" ) );
546
547 std::regex search_reg( "[^a-zA-Z0-9_/]" );
548
549 if( std::regex_search( std::string( processedString ), search_reg ) )
550 return wxString( "'" ) + processedString + wxString( "'" );
551
552 return processedString;
553}
554
555
557{
558 wxString pinName4Telesis = aPin.GetName() + wxString( "__" ) + aPin.GetNumber();
559 std::regex reg( "[^A-Za-z0-9_+?/-]" );
560 return wxString( std::regex_replace( std::string( pinName4Telesis ), reg, "?" ) );
561}
562
563
564wxString NETLIST_EXPORTER_ALLEGRO::formatFunction( wxString aName, std::vector<SCH_PIN*> aPinList )
565{
566 aName.MakeUpper();
567 std::list<wxString> pinNameList;
568
569 std::stable_sort( aPinList.begin(), aPinList.end(), NETLIST_EXPORTER_ALLEGRO::CompareLibPin );
570
571 for( auto pin : aPinList )
572 pinNameList.push_back( formatPin( *pin ) );
573
574 wxString out_str = "";
575 wxString str;
576
577 out_str.Printf( wxT( "PINORDER %s " ), TO_UTF8( aName ) );
578
579 for( const wxString& pinName : pinNameList )
580 {
581 str.Printf( ",\n\t%s", TO_UTF8( pinName ) );
582 out_str += str;
583 }
584 out_str += wxString( "\n" );
585
586 str.Printf( wxT( "FUNCTION %s %s " ), TO_UTF8( aName ), TO_UTF8( aName ) );
587 out_str += str;
588
589 for( auto pin : aPinList )
590 {
591 str.Printf( ",\n\t%s", TO_UTF8( pin->GetNumber() ) );
592 out_str += str;
593 }
594
595 out_str += wxString( "\n" );
596
597 return out_str;
598}
599
600
601wxString NETLIST_EXPORTER_ALLEGRO::getGroupField( int aGroupIndex, const wxArrayString& aFieldArray, bool aSanitize )
602{
603 auto pairIter = m_componentGroups.equal_range( aGroupIndex );
604
605 for( auto iter = pairIter.first; iter != pairIter.second; ++iter )
606 {
607 SCH_SYMBOL* sym = ( iter->second ).first;
608 SCH_SHEET_PATH sheetPath = ( iter->second ).second;
609
610 for( const wxString& field : aFieldArray )
611 {
612 if( SCH_FIELD* fld = sym->FindFieldCaseInsensitive( field ) )
613 {
614 // TODO: FOR_CANVAS seems like an odd context here....
615 wxString fieldText = fld->GetShownText( &sheetPath, FOR_CANVAS );
616
617 if( !fieldText.IsEmpty() )
618 {
619 if( aSanitize )
620 return formatText( fieldText );
621 else
622 return fieldText;
623 }
624 }
625 }
626 }
627
628 for( auto iter = pairIter.first; iter != pairIter.second; ++iter )
629 {
630 SCH_SYMBOL* sym = ( iter->second ).first;
631
632 for( const wxString& field : aFieldArray )
633 {
634 if( SCH_FIELD* fld = sym->GetLibSymbolRef()->FindFieldCaseInsensitive( field ) )
635 {
636 wxString fieldText = fld->GetShownText( RESOLVED );
637
638 if( !fieldText.IsEmpty() )
639 {
640 if( aSanitize )
641 return formatText( fieldText );
642 else
643 return fieldText;
644 }
645 }
646 }
647 }
648
649 return wxEmptyString;
650}
651
652
654{
655 wxString path = aSheetPath.PathHumanReadable();
656
657 // Use root schematic file name for root
658 if( path == wxS( "/" ) )
659 path = aSheetPath.PathHumanReadable( false );
660
661 // The leading and trailing separators carry no information; the ones in between become
662 // dashes below.
663 while( path.StartsWith( wxS( "/" ) ) )
664 path = path.Mid( 1 );
665
666 while( path.EndsWith( wxS( "/" ) ) )
667 path.RemoveLast();
668
669 wxString roomName;
670
671 for( wxUniChar c : path )
672 {
673 if( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || ( c >= '0' && c <= '9' )
674 || c == '_' || c == '-' )
675 {
676 roomName << c;
677 }
678 else if( c == '/' )
679 {
680 // Use dash to represent our hierarchy breaks
681 roomName << wxS( "-" );
682 }
683 else
684 {
685 roomName << wxS( "_" );
686 }
687 }
688
689 return roomName;
690}
691
692
693wxString NETLIST_EXPORTER_ALLEGRO::formatDevice( wxString aString )
694{
695 aString.MakeLower();
696 std::regex reg( "[^a-z0-9_-]" );
697 return wxString( std::regex_replace( std::string( aString ), reg, "_" ) );
698}
699
700
702{
703 fmt::print( m_f, "$A_PROPERTIES\n" );
704
705 while( !m_packageProperties.empty() )
706 {
707 std::multimap<wxString, wxString>::iterator iter = m_packageProperties.begin();
708 wxString roomName = iter->first;
709
710 std::vector<wxString> refTexts;
711
712 auto pairIter = m_packageProperties.equal_range( roomName );
713
714 for( iter = pairIter.first; iter != pairIter.second; ++iter )
715 {
716 wxString refText = iter->second;
717 refTexts.push_back( refText );
718 }
719
720 m_packageProperties.erase( pairIter.first, pairIter.second );
721
722 // Nothing to name the room after (a schematic with no file name yet); leave these
723 // symbols without a ROOM rather than writing out an empty property.
724 if( roomName.IsEmpty() )
725 continue;
726
727 // formatRoom() already restricted this to characters that need no quoting or escaping.
728 fmt::print( m_f, "'ROOM' '{}' ; ", TO_UTF8( roomName ) );
729
730 std::stable_sort( refTexts.begin(), refTexts.end(),
732
733 fmt::print( m_f, "{}", fmt::join( refTexts, ",\n\t" ) );
734
735 fmt::print( m_f, "\n" );
736 }
737}
738
739
741{
742 fmt::print( m_f, "$NETS\n" );
743
744 while( !m_netNameNodes.empty() )
745 {
746 std::multimap<wxString, NET_NODE>::iterator iter = m_netNameNodes.begin();
747 std::vector<NET_NODE> netNodes;
748
749 wxString netName = iter->first;
750
751 fmt::print( m_f, "{}; ", TO_UTF8( formatText( netName ).MakeUpper() ) );
752
753 auto pairIter = m_netNameNodes.equal_range( netName );
754
755 for( iter = pairIter.first; iter != pairIter.second; ++iter )
756 {
757 NET_NODE netNode = iter->second;
758 netNodes.push_back( netNode );
759 }
760
761 m_netNameNodes.erase( pairIter.first, pairIter.second );
762
763 std::stable_sort( netNodes.begin(), netNodes.end() );
764
765 std::vector<wxString> nets;
766 for( const NET_NODE& netNode : netNodes )
767 {
768 wxString refText = netNode.m_Pin->GetParentSymbol()->GetRef( &netNode.m_Sheet );
769 wxString pinText = netNode.m_Pin->GetShownNumber();
770 nets.push_back( refText + wxString( "." ) + pinText );
771 }
772
773 fmt::print( m_f, "{}", fmt::join( nets, ",\n\t" ) );
774
775 fmt::print( m_f, "\n" );
776 }
777}
778
779
781{
782 while( ( aString.GetChar( aString.Length() - 1 ) >= '0' )
783 && ( aString.GetChar( aString.Length() - 1 ) <= '9' ) )
784 {
785 aString.RemoveLast();
786 }
787
788 return aString;
789}
790
791
792unsigned int NETLIST_EXPORTER_ALLEGRO::extractTailNumber( wxString aString )
793{
794 wxString numString;
795
796 while( ( aString.GetChar( aString.Length() - 1 ) >= '0' )
797 && ( aString.GetChar( aString.Length() - 1 ) <= '9' ) )
798 {
799 numString.insert( 0, aString.GetChar( aString.Length() - 1 ) );
800 aString.RemoveLast();
801 }
802
803 unsigned long val;
804
805 //From wxWidgets 3.1.6, here we can use ToUInt instead of ToULong function.
806 numString.ToULong( &val );
807 return (unsigned int) val;
808}
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:599
SCH_FIELD * FindFieldCaseInsensitive(const wxString &aFieldName)
wxArrayString GetFPFilters() const
Definition lib_symbol.h:252
std::vector< SCH_PIN * > GetGraphicalPins(int aUnit=0, int aBodyStyle=0) const override
Graphical pins: Return schematic pin objects as drawn (unexpanded), filtered by unit/body.
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:170
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:350
const wxString & GetName() const
Definition sch_pin.cpp:527
const wxString & GetShownNumber() const
Definition sch_pin.cpp:708
const wxString & GetNumber() const
Definition sch_pin.h:144
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:73
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:182
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:60
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:216
@ FOR_CANVAS
Definition common.h:90
@ RAW_VALUE
Definition common.h:96
@ RESOLVED
Definition common.h:95
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)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition idf_helpers.h:37
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 ALL_UNITS
Definition symbol.h:150
#define ALL_BODY_STYLES
Definition symbol.h:151
std::string path
KIBIS_PIN * pin
@ SCH_SYMBOL_T
Definition typeinfo.h:168