31#include <fmt/ranges.h>
40 m_exportPath = wxFileName( aOutFileName ).GetPath(
static_cast<int>( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR ) )
41 + wxString(
"devices" );
47 wxString msg = wxString::Format(
_(
"Failed to create directory 'devices' ." ) );
54 if( (
m_f = wxFopen( aOutFileName, wxT(
"wt" ) ) ) ==
nullptr )
56 wxString msg = wxString::Format(
_(
"Failed to create file '%s'." ), aOutFileName );
63 fmt::print(
m_f,
"(NETLIST)\n" );
84 fmt::print(
m_f,
"$END\n" );
105 wxString refText1 = aItem1.first->GetRef( &aItem1.second );
106 wxString refText2 = aItem2.first->GetRef( &aItem2.second );
108 if( refText1 == refText2 )
110 return aItem1.second.PathHumanReadable() < aItem2.second.PathHumanReadable();
118 const wxString& aRefText2 )
121 return StrNumCmp( aRefText1, aRefText2 ) < 0;
151 std::set<
SCH_SYMBOL*,
decltype( cmp )> ordered_symbols( cmp );
152 std::multiset<
SCH_SYMBOL*,
decltype( cmp )> extra_units( cmp );
159 if( excluded( symbol, sheet ) )
162 auto test = ordered_symbols.insert( symbol );
166 if( ( *(
test.first ) )->m_Uuid > symbol->
m_Uuid )
168 extra_units.insert( *(
test.first ) );
169 ordered_symbols.erase(
test.first );
170 ordered_symbols.insert( symbol );
174 extra_units.insert( symbol );
179 for(
EDA_ITEM* item : ordered_symbols )
190 symbol->
GetRef( &sheet ) ) );
197 NET_RECORD(
const wxString& aName ) :
202 std::vector<NET_NODE> m_Nodes;
205 std::vector<NET_RECORD*> nets;
209 nets.emplace_back(
new NET_RECORD( net.name ) );
210 NET_RECORD* net_record = nets.back();
212 for(
const auto& [
pin, sheet] : net.pins )
217 net_record->m_Nodes.emplace_back(
pin, sheet );
222 std::sort( nets.begin(), nets.end(),
223 [](
const NET_RECORD* a,
const NET_RECORD*b )
225 return StrNumCmp( a->m_Name, b->m_Name ) < 0;
228 for( NET_RECORD* net_record : nets )
231 std::sort( net_record->m_Nodes.begin(), net_record->m_Nodes.end(),
234 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
235 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
238 return a.m_Pin->GetShownNumber() < b.m_Pin->GetShownNumber();
249 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
250 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
252 return refA == refB && a.m_Pin->GetShownNumber() == b.m_Pin->GetShownNumber();
255 for(
const NET_NODE& netNode : net_record->m_Nodes )
260 if( refText[0] == wxChar(
'#' ) )
263 m_netNameNodes.insert( std::pair<wxString, NET_NODE>( net_record->m_Name, netNode ) );
267 for( NET_RECORD* record : nets )
275 wxString deviceFileCreatingError = wxString(
"" );
282 m_componentGroups.insert( std::pair<
int, std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>>( groupCount, first_ele ) );
300 wxString ref1 = it->first->GetRef( &it->second );
301 wxString ref2 = first_ele.first->GetRef( &first_ele.second );
305 m_componentGroups.insert( std::pair<
int, std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>>( groupCount, *it ) );
320 struct COMP_PACKAGE_STRUCT
323 wxString m_tolerance;
324 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> m_symbolSheetpaths;
327 COMP_PACKAGE_STRUCT compPackageStruct;
328 std::map<wxString, COMP_PACKAGE_STRUCT> compPackageMap;
330 for(
int groupIndex = 1; groupIndex < groupCount; groupIndex++ )
333 auto beginIter = pairIter.first;
334 auto endIter = pairIter.second;
336 SCH_SYMBOL* sym = ( beginIter->second ).first;
341 wxString deviceType = valueText + wxT(
"_" ) + footprintText;
343 while( deviceType.Length() > 0 && deviceType.GetChar( deviceType.Length() - 1 ) ==
'_' )
344 deviceType.RemoveLast();
348 wxArrayString fieldArray;
349 fieldArray.Add(
"Spice_Model" );
350 fieldArray.Add(
"VALUE" );
355 fieldArray.Add(
"TOLERANCE" );
356 fieldArray.Add(
"TOL" );
359 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths;
361 for(
auto iter = beginIter; iter != endIter; iter++ )
363 symbolSheetpaths.push_back( std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>( iter->second.first,
364 iter->second.second ) );
367 std::stable_sort( symbolSheetpaths.begin(), symbolSheetpaths.end(),
370 compPackageStruct.m_value = value;
371 compPackageStruct.m_tolerance = tol;
372 compPackageStruct.m_symbolSheetpaths = symbolSheetpaths;
373 compPackageMap.insert( std::pair( deviceType, compPackageStruct ) );
377 wxString deviceFileName = wxFileName(
m_exportPath, deviceType, wxString(
"txt" ) ).GetFullPath();
379 if( ( d = wxFopen( deviceFileName, wxT(
"wt" ) ) ) ==
nullptr )
382 msg.Printf(
_(
"Failed to create file '%s'.\n" ), deviceFileName );
383 deviceFileCreatingError += msg;
387 footprintText = footprintText.AfterLast(
':' );
389 wxArrayString footprintAlt;
392 for(
const wxString& fp : footprintArray )
394 if( ( fp.Find(
'*' ) != wxNOT_FOUND ) || ( fp.Find(
'?' ) != wxNOT_FOUND ) )
397 footprintAlt.Add( fp.AfterLast(
':' ) );
400 if( footprintText.IsEmpty() )
402 if( !footprintAlt.IsEmpty() )
404 footprintText = footprintAlt[0];
405 footprintAlt.RemoveAt( 0 );
409 footprintText = deviceType;
414 fmt::print( d,
"CLASS IC\n" );
428 for(
int ii = 0; ii < (int) pinList.size() - 1; ii++ )
430 if( pinList[ii]->GetNumber() == pinList[ii + 1]->GetNumber() )
433 pinList.erase( pinList.begin() + ii + 1 );
438 unsigned int pinCount = pinList.size();
439 fmt::print( d,
"PINCOUNT {}\n", pinCount );
444 if( !value.IsEmpty() )
445 fmt::print( d,
"PACKAGEPROP VALUE {}\n",
TO_UTF8( value ) );
448 fmt::print( d,
"PACKAGEPROP TOL {}\n",
TO_UTF8( tol ) );
450 if( !footprintAlt.IsEmpty() )
451 fmt::print( d,
"PACKAGEPROP ALT_SYMBOLS '({})'\n", fmt::join( footprintAlt,
"," ) );
453 wxArrayString propArray;
454 propArray.Add(
"PART_NUMBER" );
455 propArray.Add(
"mpn" );
456 propArray.Add(
"mfr_pn" );
460 fmt::print( d,
"PACKAGEPROP {} {}\n",
TO_UTF8( propArray[0] ),
TO_UTF8( data ) );
463 propArray.Add(
"HEIGHT" );
467 fmt::print( d,
"PACKAGEPROP {} {}\n",
TO_UTF8( propArray[0] ),
TO_UTF8( data ) );
469 fmt::print( d,
"END\n" );
474 fmt::print(
m_f,
"$PACKAGES\n" );
476 for(
auto iter = compPackageMap.begin(); iter != compPackageMap.end(); iter++ )
478 wxString deviceType = iter->first;
479 wxString value = iter->second.m_value;
480 wxString tolerance = iter->second.m_tolerance;
484 if( value.StartsWith(
"'" ) and value.EndsWith(
"'" ) )
486 value.Remove( 0, 1 );
490 if( value.IsEmpty() && tolerance.IsEmpty() )
492 fmt::print(
m_f,
"! '{}' ; ",
TO_UTF8( deviceType ) );
494 else if( tolerance.IsEmpty() )
496 fmt::print(
m_f,
"! '{}' ! '{}' ; ",
502 fmt::print(
m_f,
"! '{}' ! '{}' ! {} ; ",
508 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths = iter->second.m_symbolSheetpaths;
510 std::vector<wxString> refTexts;
512 for(
const auto& [ sym, sheetPath ] : symbolSheetpaths)
513 refTexts.push_back( sym->GetRef( &sheetPath ) );
515 fmt::print(
m_f,
"{}", fmt::join( refTexts,
",\n\t" ) );
517 fmt::print(
m_f,
"\n" );
520 if( !deviceFileCreatingError.IsEmpty() )
532 if( aString.IsEmpty() )
533 return wxEmptyString;
536 wxString mu( wxUniChar( 0x00B5 ) );
537 aString.Replace( mu,
"u" );
538 mu = wxUniChar( 0x03BC );
539 aString.Replace( mu,
"u" );
541 std::regex reg(
"[!']|[^ -~]" );
542 wxString processedString = wxString( std::regex_replace( std::string( aString ), reg,
"?" ) );
544 std::regex search_reg(
"[^a-zA-Z0-9_/]" );
546 if( std::regex_search( std::string( processedString ), search_reg ) )
547 return wxString(
"'" ) + processedString + wxString(
"'" );
549 return processedString;
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,
"?" ) );
564 std::list<wxString> pinNameList;
568 for(
auto pin : aPinList )
571 wxString out_str =
"";
574 out_str.Printf( wxT(
"PINORDER %s " ),
TO_UTF8( aName ) );
576 for(
const wxString& pinName : pinNameList )
578 str.Printf(
",\n\t%s",
TO_UTF8( pinName ) );
582 out_str += wxString(
"\n" );
584 str.Printf( wxT(
"FUNCTION %s %s " ),
TO_UTF8( aName ),
TO_UTF8( aName ) );
589 str.Printf(
",\n\t%s",
TO_UTF8(
pin->GetNumber() ) );
593 out_str += wxString(
"\n" );
603 for(
auto iter = pairIter.first; iter != pairIter.second; ++iter )
608 for(
const wxString& field : aFieldArray )
615 if( !fieldText.IsEmpty() )
626 for(
auto iter = pairIter.first; iter != pairIter.second; ++iter )
630 for(
const wxString& field : aFieldArray )
634 wxString fieldText = fld->GetShownText(
RESOLVED );
636 if( !fieldText.IsEmpty() )
647 return wxEmptyString;
656 if(
path == wxS(
"/" ) )
661 while(
path.StartsWith( wxS(
"/" ) ) )
664 while(
path.EndsWith( wxS(
"/" ) ) )
669 for( wxUniChar c :
path )
671 if( ( c >=
'a' && c <=
'z' ) || ( c >=
'A' && c <=
'Z' ) || ( c >=
'0' && c <=
'9' )
672 || c ==
'_' || c ==
'-' )
679 roomName << wxS(
"-" );
683 roomName << wxS(
"_" );
694 std::regex reg(
"[^a-z0-9_-]" );
695 return wxString( std::regex_replace( std::string( aString ), reg,
"_" ) );
701 fmt::print(
m_f,
"$A_PROPERTIES\n" );
706 wxString roomName = iter->first;
708 std::vector<wxString> refTexts;
712 for( iter = pairIter.first; iter != pairIter.second; ++iter )
714 wxString refText = iter->second;
715 refTexts.push_back( refText );
722 if( roomName.IsEmpty() )
726 fmt::print(
m_f,
"'ROOM' '{}' ; ",
TO_UTF8( roomName ) );
730 fmt::print(
m_f,
"{}", fmt::join( refTexts,
",\n\t" ) );
732 fmt::print(
m_f,
"\n" );
739 fmt::print(
m_f,
"$NETS\n" );
743 std::multimap<wxString, NET_NODE>::iterator iter =
m_netNameNodes.begin();
744 std::vector<NET_NODE> netNodes;
746 wxString netName = iter->first;
752 for( iter = pairIter.first; iter != pairIter.second; ++iter )
755 netNodes.push_back( netNode );
760 std::stable_sort( netNodes.begin(), netNodes.end() );
762 std::vector<wxString> nets;
764 for(
const NET_NODE& netNode : netNodes )
766 wxString refText = netNode.m_Pin->GetParentSymbol()->GetRef( &netNode.m_Sheet );
772 nets.push_back( refText + wxString(
"." ) + num );
775 fmt::print(
m_f,
"{}", fmt::join( nets,
",\n\t" ) );
777 fmt::print(
m_f,
"\n" );
784 return aRef.Left( aRef.find_first_of( wxS(
"0123456789" ) ) );
791 const size_t maxLength = 31;
792 const wxString unsafe = wxS(
"()'!@" );
796 const wxString ref = symbol->GetRef( &sheet );
798 if( ref.length() > maxLength )
800 aReporter.
Report( wxString::Format(
_(
"Reference %s is longer than the %zu characters Allegro accepts "
806 if( ref.find_first_of( unsafe ) != wxString::npos )
808 aReporter.
Report( wxString::Format(
_(
"Reference %s contains a character Allegro rejects (%s)." ), ref,
A base class for most all the KiCad significant classes used in schematics and boards.
SCH_FIELD * FindFieldCaseInsensitive(const wxString &aFieldName)
wxArrayString GetFPFilters() const
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.
void extractComponentsInfo()
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.
SCH_SHEET_LIST m_exportSheets
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.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Base class for any item which can be embedded within the SCHEMATIC container class,...
const SYMBOL * GetParentSymbol() const
const wxString & GetName() const
const wxString & GetShownNumber() const
const wxString & GetNumber() const
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.
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()
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
A base class for LIB_SYMBOL and SCH_SYMBOL.
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
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
This file is part of the common library.
void remove_duplicates(_Container &__c)
Deletes all duplicate values from __c.
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)
int StrNumCmp(const wxString &aString1, const wxString &aString2, CMP_T aCaseSensitivity)
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.