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, unsigned /* aNetlistOptions */,
34 REPORTER& aReporter )
35{
36 m_f = nullptr;
37 bool success = true;
38
39 // Create the devices directory
40 m_exportPath = wxFileName( aOutFileName ).GetPath( static_cast<int>( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR ) )
41 + wxString( "devices" );
42
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 reportUnsafeRefs( aReporter );
74
75 // Start with package definitions, which we create from component groups.
76 success &= toAllegroPackages();
77
78 // Write out package properties. NOTE: Allegro doesn't recognize much...
80
81 // Write out nets
83
84 fmt::print( m_f, "$END\n" );
85 }
86 catch (...)
87 {
88 success = false;
89 }
90
91 // Done with the netlist
92 fclose( m_f );
93
94 m_f = nullptr;
95
96 return success;
97}
98
99
101 SCH_SHEET_PATH>& aItem1,
102 const std::pair<SCH_SYMBOL*,
103 SCH_SHEET_PATH>& aItem2 )
104{
105 wxString refText1 = aItem1.first->GetRef( &aItem1.second );
106 wxString refText2 = aItem2.first->GetRef( &aItem2.second );
107
108 if( refText1 == refText2 )
109 {
110 return aItem1.second.PathHumanReadable() < aItem2.second.PathHumanReadable();
111 }
112
113 return CompareSymbolRef( refText1, refText2 );
114}
115
116
117bool NETLIST_EXPORTER_ALLEGRO::CompareSymbolRef( const wxString& aRefText1,
118 const wxString& aRefText2 )
119{
120 // A sheet suffix follows the number, so R2_M1 still sorts before R10_M1
121 return StrNumCmp( aRefText1, aRefText2 ) < 0;
122}
123
124
126{
127 // return "lhs < rhs"
128 return StrNumCmp( aPin1->GetShownNumber(), aPin2->GetShownNumber(), CMP_T::IGNORE_CASE ) < 0;
129}
130
131
133{
135
136 const EXPORT_EXCLUSION excluded = boardExclusion();
137
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 ), CMP_T::IGNORE_CASE ) < 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
158 // An excluded unit must not stand for its package, or the included units go with it
159 if( excluded( symbol, sheet ) )
160 continue;
161
162 auto test = ordered_symbols.insert( symbol );
163
164 if( !test.second )
165 {
166 if( ( *( test.first ) )->m_Uuid > symbol->m_Uuid )
167 {
168 extra_units.insert( *( test.first ) );
169 ordered_symbols.erase( test.first );
170 ordered_symbols.insert( symbol );
171 }
172 else
173 {
174 extra_units.insert( symbol );
175 }
176 }
177 }
178
179 for( EDA_ITEM* item : ordered_symbols )
180 {
181 SCH_SYMBOL* symbol = findNextSymbol( item, sheet, excluded );
182
183 if( !symbol )
184 continue;
185
186 if( symbol->GetLibPins().empty() )
187 continue;
188
189 m_packageProperties.insert( std::pair<wxString, wxString>( formatRoom( sheet ),
190 symbol->GetRef( &sheet ) ) );
191 m_orderedSymbolsSheetpath.push_back( std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>( symbol, sheet ) );
192 }
193 }
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 EXPORT_NET& net : m_exportNets )
208 {
209 nets.emplace_back( new NET_RECORD( net.name ) );
210 NET_RECORD* net_record = nets.back();
211
212 for( const auto& [pin, sheet] : net.pins )
213 {
214 SYMBOL* symbol = pin->GetParentSymbol();
215
216 if( symbol && !symbol->GetExcludedFromBoard( &sheet, m_exportVariant ) )
217 net_record->m_Nodes.emplace_back( pin, sheet );
218 }
219 }
220
221 // Netlist ordering: Net name, then ref des, then pin name
222 std::sort( nets.begin(), nets.end(),
223 []( const NET_RECORD* a, const NET_RECORD*b )
224 {
225 return StrNumCmp( a->m_Name, b->m_Name ) < 0;
226 } );
227
228 for( NET_RECORD* net_record : nets )
229 {
230 // Netlist ordering: Net name, then ref des, then pin name
231 std::sort( net_record->m_Nodes.begin(), net_record->m_Nodes.end(),
232 []( const NET_NODE& a, const NET_NODE& b )
233 {
234 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
235 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
236
237 if( refA == refB )
238 return a.m_Pin->GetShownNumber() < b.m_Pin->GetShownNumber();
239
240 return refA < refB;
241 } );
242
243 // Some duplicates can exist, for example on multi-unit parts with duplicated pins across
244 // units. If the user connects the pins on each unit, they will appear on separate
245 // subgraphs. Remove those here:
246 alg::remove_duplicates( net_record->m_Nodes,
247 []( const NET_NODE& a, const NET_NODE& b )
248 {
249 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
250 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
251
252 return refA == refB && a.m_Pin->GetShownNumber() == b.m_Pin->GetShownNumber();
253 } );
254
255 for( const NET_NODE& netNode : net_record->m_Nodes )
256 {
257 wxString refText = netNode.m_Pin->GetParentSymbol()->GetRef( &netNode.m_Sheet );
258
259 // Skip power symbols and virtual symbols
260 if( refText[0] == wxChar( '#' ) )
261 continue;
262
263 m_netNameNodes.insert( std::pair<wxString, NET_NODE>( net_record->m_Name, netNode ) );
264 }
265 }
266
267 for( NET_RECORD* record : nets )
268 delete record;
269}
270
271
273{
274 int groupCount = 1;
275 wxString deviceFileCreatingError = wxString( "" );
276
277 //Group the components......
278 while( !m_orderedSymbolsSheetpath.empty() )
279 {
280 std::pair<SCH_SYMBOL*, SCH_SHEET_PATH> first_ele = m_orderedSymbolsSheetpath.front();
281 m_orderedSymbolsSheetpath.pop_front();
282 m_componentGroups.insert( std::pair<int, std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>>( groupCount, first_ele ) );
283
284 for( auto it = m_orderedSymbolsSheetpath.begin(); it != m_orderedSymbolsSheetpath.end(); )
285 {
286 if( it->first->GetValue( &it->second, RAW_VALUE, m_exportVariant )
287 != first_ele.first->GetValue( &first_ele.second, RAW_VALUE, m_exportVariant ) )
288 {
289 ++it;
290 continue;
291 }
292
293 if( it->first->GetFootprintFieldText( &it->second, RAW_VALUE, m_exportVariant )
294 != first_ele.first->GetFootprintFieldText( &first_ele.second, RAW_VALUE, m_exportVariant ) )
295 {
296 ++it;
297 continue;
298 }
299
300 wxString ref1 = it->first->GetRef( &it->second );
301 wxString ref2 = first_ele.first->GetRef( &first_ele.second );
302
303 if( refPrefix( ref1 ) == refPrefix( ref2 ) )
304 {
305 m_componentGroups.insert( std::pair<int, std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>>( groupCount, *it ) );
306 it = m_orderedSymbolsSheetpath.erase( it );
307
308 if( m_orderedSymbolsSheetpath.size() == 0 )
309 break;
310 else
311 continue; // Don't increment iterator; we want the item that replaced the erased one
312 }
313
314 ++it;
315 }
316
317 groupCount++;
318 }
319
320 struct COMP_PACKAGE_STRUCT
321 {
322 wxString m_value;
323 wxString m_tolerance;
324 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> m_symbolSheetpaths;
325 };
326
327 COMP_PACKAGE_STRUCT compPackageStruct;
328 std::map<wxString, COMP_PACKAGE_STRUCT> compPackageMap;
329
330 for( int groupIndex = 1; groupIndex < groupCount; groupIndex++ )
331 {
332 auto pairIter = m_componentGroups.equal_range( groupIndex );
333 auto beginIter = pairIter.first;
334 auto endIter = pairIter.second;
335
336 SCH_SYMBOL* sym = ( beginIter->second ).first;
337 SCH_SHEET_PATH sheetPath = ( beginIter->second ).second;
338
339 wxString valueText = sym->GetValue( &sheetPath, RAW_VALUE, m_exportVariant );
340 wxString footprintText = sym->GetFootprintFieldText( &sheetPath, RAW_VALUE, m_exportVariant );
341 wxString deviceType = valueText + wxT( "_" ) + footprintText;
342
343 while( deviceType.Length() > 0 && deviceType.GetChar( deviceType.Length() - 1 ) == '_' )
344 deviceType.RemoveLast();
345
346 deviceType = formatDevice( deviceType );
347
348 wxArrayString fieldArray;
349 fieldArray.Add( "Spice_Model" );
350 fieldArray.Add( "VALUE" );
351
352 wxString value = getGroupField( groupIndex, fieldArray );
353
354 fieldArray.clear();
355 fieldArray.Add( "TOLERANCE" );
356 fieldArray.Add( "TOL" );
357 wxString tol = getGroupField( groupIndex, fieldArray );
358
359 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths;
360
361 for( auto iter = beginIter; iter != endIter; iter++ )
362 {
363 symbolSheetpaths.push_back( std::pair<SCH_SYMBOL*, 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, wxString( "txt" ) ).GetFullPath();
378
379 if( ( d = wxFopen( deviceFileName, wxT( "wt" ) ) ) == nullptr )
380 {
381 wxString msg;
382 msg.Printf( _( "Failed to create file '%s'.\n" ), deviceFileName );
383 deviceFileCreatingError += msg;
384 continue;
385 }
386
387 footprintText = footprintText.AfterLast( ':' );
388
389 wxArrayString footprintAlt;
390 wxArrayString footprintArray = sym->GetLibSymbolRef()->GetFPFilters();
391
392 for( const wxString& fp : footprintArray )
393 {
394 if( ( fp.Find( '*' ) != wxNOT_FOUND ) || ( fp.Find( '?' ) != wxNOT_FOUND ) )
395 continue;
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 fmt::print( d, "{}", TO_UTF8( formatFunction( "main", pinList ) ) );
443
444 if( !value.IsEmpty() )
445 fmt::print( d, "PACKAGEPROP VALUE {}\n", TO_UTF8( value ) );
446
447 if( !tol.IsEmpty() )
448 fmt::print( d, "PACKAGEPROP TOL {}\n", TO_UTF8( tol ) );
449
450 if( !footprintAlt.IsEmpty() )
451 fmt::print( d, "PACKAGEPROP ALT_SYMBOLS '({})'\n", fmt::join( footprintAlt, "," ) );
452
453 wxArrayString propArray;
454 propArray.Add( "PART_NUMBER" );
455 propArray.Add( "mpn" );
456 propArray.Add( "mfr_pn" );
457 wxString data = getGroupField( groupIndex, propArray );
458
459 if(!data.IsEmpty())
460 fmt::print( d, "PACKAGEPROP {} {}\n", TO_UTF8( propArray[0] ), TO_UTF8( data ) );
461
462 propArray.clear();
463 propArray.Add( "HEIGHT" );
464 data = getGroupField( groupIndex, propArray );
465
466 if(!data.IsEmpty())
467 fmt::print( d, "PACKAGEPROP {} {}\n", TO_UTF8( propArray[0] ), TO_UTF8( data ) );
468
469 fmt::print( d, "END\n" );
470
471 fclose( d );
472 }
473
474 fmt::print( m_f, "$PACKAGES\n" );
475
476 for( auto iter = compPackageMap.begin(); iter != compPackageMap.end(); iter++ )
477 {
478 wxString deviceType = iter->first;
479 wxString value = iter->second.m_value;
480 wxString tolerance = iter->second.m_tolerance;
481
482 // Remove quotes in value (can be added by formatText), if any.
483 // (they are already in print format string)
484 if( value.StartsWith( "'" ) and value.EndsWith( "'" ) )
485 {
486 value.Remove( 0, 1 );
487 value.RemoveLast();
488 }
489
490 if( value.IsEmpty() && tolerance.IsEmpty() )
491 {
492 fmt::print( m_f, "! '{}' ; ", TO_UTF8( deviceType ) );
493 }
494 else if( tolerance.IsEmpty() )
495 {
496 fmt::print( m_f, "! '{}' ! '{}' ; ",
497 TO_UTF8( deviceType ),
498 TO_UTF8( value ) );
499 }
500 else
501 {
502 fmt::print( m_f, "! '{}' ! '{}' ! {} ; ",
503 TO_UTF8( deviceType ),
504 TO_UTF8( value ),
505 TO_UTF8( tolerance ) );
506 }
507
508 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths = iter->second.m_symbolSheetpaths;
509
510 std::vector<wxString> refTexts;
511
512 for( const auto& [ sym, sheetPath ] : symbolSheetpaths)
513 refTexts.push_back( sym->GetRef( &sheetPath ) );
514
515 fmt::print( m_f, "{}", fmt::join( refTexts, ",\n\t" ) );
516
517 fmt::print( m_f, "\n" );
518 }
519
520 if( !deviceFileCreatingError.IsEmpty() )
521 {
522 DisplayError( nullptr, deviceFileCreatingError );
523 return false;
524 }
525
526 return true;
527}
528
529
530wxString NETLIST_EXPORTER_ALLEGRO::formatText( wxString aString )
531{
532 if( aString.IsEmpty() )
533 return wxEmptyString;
534
535 // Replace 'µ' ("\u00b5") by 'u' to keep ASCII7 constraint
536 wxString mu( wxUniChar( 0x00B5 ) );
537 aString.Replace( mu, "u" );
538 mu = wxUniChar( 0x03BC );
539 aString.Replace( mu, "u" );
540
541 std::regex reg( "[!']|[^ -~]" );
542 wxString processedString = wxString( std::regex_replace( std::string( aString ), reg, "?" ) );
543
544 std::regex search_reg( "[^a-zA-Z0-9_/]" );
545
546 if( std::regex_search( std::string( processedString ), search_reg ) )
547 return wxString( "'" ) + processedString + wxString( "'" );
548
549 return processedString;
550}
551
552
554{
555 wxString pinName4Telesis = aPin.GetName() + wxString( "__" ) + aPin.GetNumber();
556 std::regex reg( "[^A-Za-z0-9_+?/-]" );
557 return wxString( std::regex_replace( std::string( pinName4Telesis ), reg, "?" ) );
558}
559
560
561wxString NETLIST_EXPORTER_ALLEGRO::formatFunction( wxString aName, std::vector<SCH_PIN*> aPinList )
562{
563 aName.MakeUpper();
564 std::list<wxString> pinNameList;
565
566 std::stable_sort( aPinList.begin(), aPinList.end(), NETLIST_EXPORTER_ALLEGRO::CompareLibPin );
567
568 for( auto pin : aPinList )
569 pinNameList.push_back( formatPin( *pin ) );
570
571 wxString out_str = "";
572 wxString str;
573
574 out_str.Printf( wxT( "PINORDER %s " ), TO_UTF8( aName ) );
575
576 for( const wxString& pinName : pinNameList )
577 {
578 str.Printf( ",\n\t%s", TO_UTF8( pinName ) );
579 out_str += str;
580 }
581
582 out_str += wxString( "\n" );
583
584 str.Printf( wxT( "FUNCTION %s %s " ), TO_UTF8( aName ), TO_UTF8( aName ) );
585 out_str += str;
586
587 for( const SCH_PIN* pin : aPinList )
588 {
589 str.Printf( ",\n\t%s", TO_UTF8( pin->GetNumber() ) );
590 out_str += str;
591 }
592
593 out_str += wxString( "\n" );
594
595 return out_str;
596}
597
598
599wxString NETLIST_EXPORTER_ALLEGRO::getGroupField( int aGroupIndex, const wxArrayString& aFieldArray, bool aSanitize )
600{
601 auto pairIter = m_componentGroups.equal_range( aGroupIndex );
602
603 for( auto iter = pairIter.first; iter != pairIter.second; ++iter )
604 {
605 SCH_SYMBOL* sym = ( iter->second ).first;
606 SCH_SHEET_PATH sheetPath = ( iter->second ).second;
607
608 for( const wxString& field : aFieldArray )
609 {
610 if( SCH_FIELD* fld = sym->FindFieldCaseInsensitive( field ) )
611 {
612 // TODO: FOR_CANVAS seems like an odd context here....
613 wxString fieldText = fld->GetShownText( &sheetPath, FOR_CANVAS, m_exportVariant );
614
615 if( !fieldText.IsEmpty() )
616 {
617 if( aSanitize )
618 return formatText( fieldText );
619 else
620 return fieldText;
621 }
622 }
623 }
624 }
625
626 for( auto iter = pairIter.first; iter != pairIter.second; ++iter )
627 {
628 SCH_SYMBOL* sym = ( iter->second ).first;
629
630 for( const wxString& field : aFieldArray )
631 {
632 if( SCH_FIELD* fld = sym->GetLibSymbolRef()->FindFieldCaseInsensitive( field ) )
633 {
634 wxString fieldText = fld->GetShownText( RESOLVED );
635
636 if( !fieldText.IsEmpty() )
637 {
638 if( aSanitize )
639 return formatText( fieldText );
640 else
641 return fieldText;
642 }
643 }
644 }
645 }
646
647 return wxEmptyString;
648}
649
650
652{
653 wxString path = aSheetPath.PathHumanReadable();
654
655 // Use root schematic file name for root
656 if( path == wxS( "/" ) )
657 path = aSheetPath.PathHumanReadable( false );
658
659 // The leading and trailing separators carry no information; the ones in between become
660 // dashes below.
661 while( path.StartsWith( wxS( "/" ) ) )
662 path = path.Mid( 1 );
663
664 while( path.EndsWith( wxS( "/" ) ) )
665 path.RemoveLast();
666
667 wxString roomName;
668
669 for( wxUniChar c : path )
670 {
671 if( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || ( c >= '0' && c <= '9' )
672 || c == '_' || c == '-' )
673 {
674 roomName << c;
675 }
676 else if( c == '/' )
677 {
678 // Use dash to represent our hierarchy breaks
679 roomName << wxS( "-" );
680 }
681 else
682 {
683 roomName << wxS( "_" );
684 }
685 }
686
687 return roomName;
688}
689
690
691wxString NETLIST_EXPORTER_ALLEGRO::formatDevice( wxString aString )
692{
693 aString.MakeLower();
694 std::regex reg( "[^a-z0-9_-]" );
695 return wxString( std::regex_replace( std::string( aString ), reg, "_" ) );
696}
697
698
700{
701 fmt::print( m_f, "$A_PROPERTIES\n" );
702
703 while( !m_packageProperties.empty() )
704 {
705 std::multimap<wxString, wxString>::iterator iter = m_packageProperties.begin();
706 wxString roomName = iter->first;
707
708 std::vector<wxString> refTexts;
709
710 auto pairIter = m_packageProperties.equal_range( roomName );
711
712 for( iter = pairIter.first; iter != pairIter.second; ++iter )
713 {
714 wxString refText = iter->second;
715 refTexts.push_back( refText );
716 }
717
718 m_packageProperties.erase( pairIter.first, pairIter.second );
719
720 // Nothing to name the room after (a schematic with no file name yet); leave these
721 // symbols without a ROOM rather than writing out an empty property.
722 if( roomName.IsEmpty() )
723 continue;
724
725 // formatRoom() already restricted this to characters that need no quoting or escaping.
726 fmt::print( m_f, "'ROOM' '{}' ; ", TO_UTF8( roomName ) );
727
728 std::stable_sort( refTexts.begin(), refTexts.end(), NETLIST_EXPORTER_ALLEGRO::CompareSymbolRef );
729
730 fmt::print( m_f, "{}", fmt::join( refTexts, ",\n\t" ) );
731
732 fmt::print( m_f, "\n" );
733 }
734}
735
736
738{
739 fmt::print( m_f, "$NETS\n" );
740
741 while( !m_netNameNodes.empty() )
742 {
743 std::multimap<wxString, NET_NODE>::iterator iter = m_netNameNodes.begin();
744 std::vector<NET_NODE> netNodes;
745
746 wxString netName = iter->first;
747
748 fmt::print( m_f, "{}; ", TO_UTF8( formatText( netName ).MakeUpper() ) );
749
750 auto pairIter = m_netNameNodes.equal_range( netName );
751
752 for( iter = pairIter.first; iter != pairIter.second; ++iter )
753 {
754 NET_NODE netNode = iter->second;
755 netNodes.push_back( netNode );
756 }
757
758 m_netNameNodes.erase( pairIter.first, pairIter.second );
759
760 std::stable_sort( netNodes.begin(), netNodes.end() );
761
762 std::vector<wxString> nets;
763
764 for( const NET_NODE& netNode : netNodes )
765 {
766 wxString refText = netNode.m_Pin->GetParentSymbol()->GetRef( &netNode.m_Sheet );
767
768 // Emit the resolved footprint pad number(s), not the raw symbol pin number, so a
769 // remapped pin's net lands on the right pad when the board reads this netlist
770 // (issue #2282). Shared with the PIN_INFO path via resolvePadNumbers.
771 for( const wxString& num : resolvePadNumbers( netNode.m_Pin, netNode.m_Sheet ) )
772 nets.push_back( refText + wxString( "." ) + num );
773 }
774
775 fmt::print( m_f, "{}", fmt::join( nets, ",\n\t" ) );
776
777 fmt::print( m_f, "\n" );
778 }
779}
780
781
782wxString NETLIST_EXPORTER_ALLEGRO::refPrefix( const wxString& aRef )
783{
784 return aRef.Left( aRef.find_first_of( wxS( "0123456789" ) ) );
785}
786
787
789{
790 // Allegro's default name length, and characters its netlist reader refuses
791 const size_t maxLength = 31;
792 const wxString unsafe = wxS( "()'!@" );
793
794 for( const auto& [symbol, sheet] : m_orderedSymbolsSheetpath )
795 {
796 const wxString ref = symbol->GetRef( &sheet );
797
798 if( ref.length() > maxLength )
799 {
800 aReporter.Report( wxString::Format( _( "Reference %s is longer than the %zu characters Allegro accepts "
801 "by default." ),
802 ref, maxLength ),
804 }
805
806 if( ref.find_first_of( unsafe ) != wxString::npos )
807 {
808 aReporter.Report( wxString::Format( _( "Reference %s contains a character Allegro rejects (%s)." ), ref,
809 unsafe ),
811 }
812 }
813}
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:254
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.
bool toAllegroPackages()
Write the $PACKAGES section.
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.
FILE * m_f
File pointer for netlist file writing operation.
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
void reportUnsafeRefs(REPORTER &aReporter) const
Warn about references Allegro may reject, which are too long or hold characters it refuses.
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 wxString refPrefix(const wxString &aRef)
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...
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.
std::set< LIB_SYMBOL *, LIB_SYMBOL_LESS_THAN > m_libParts
unique library symbols used. LIB_SYMBOL items are sorted by names
std::vector< wxString > resolvePadNumbers(const SCH_PIN *aPin, const SCH_SHEET_PATH &aSheetPath) const
Resolve aPin to its effective pad number(s) for every netlist path (issue #2282).
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
EXPORT_EXCLUSION boardExclusion() const
wxString m_exportVariant
The schematic we're generating a netlist for.
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, const SCH_SHEET_PATH &aSheetPath, const EXPORT_EXCLUSION &aExcluded=nullptr)
Check if the given symbol should be processed for netlisting.
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:530
const wxString & GetShownNumber() const
Definition sch_pin.cpp:711
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:76
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...
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:185
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:92
@ RAW_VALUE
Definition common.h:98
@ RESOLVED
Definition common.h:97
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
std::function< bool(const SCH_SYMBOL *, const SCH_SHEET_PATH &)> EXPORT_EXCLUSION
An abstract class used for the netlist exporters that Eeschema supports.
Collection of utility functions for component reference designators (refdes)
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
int StrNumCmp(const wxString &aString1, const wxString &aString2, CMP_T aCaseSensitivity)
Compare two strings with alphanumerical content.
wxString GetISO8601CurrentDateTime()
@ IGNORE_CASE
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
#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