38#include <fmt/ranges.h>
48 m_exportPath = wxFileName( aOutFileName ).GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR )
49 + wxString(
"devices" );
54 wxString msg = wxString::Format(
_(
"Failed to create directory 'devices' ." ) );
61 if( (
m_f = wxFopen( aOutFileName, wxT(
"wt" ) ) ) ==
nullptr )
63 wxString msg = wxString::Format(
_(
"Failed to create file '%s'." ), aOutFileName );
70 fmt::print(
m_f,
"(NETLIST)\n" );
90 fmt::print(
m_f,
"$END\n" );
111 wxString refText1 = aItem1.first->GetRef( &aItem1.second );
112 wxString refText2 = aItem2.first->GetRef( &aItem2.second );
114 if( refText1 == refText2 )
116 return aItem1.second.PathHumanReadable() < aItem2.second.PathHumanReadable();
124 const wxString& aRefText2 )
131 return aRefText1 < aRefText2;
155 b->GetRef( &sheet,
false ),
true ) < 0 );
158 std::set<
SCH_SYMBOL*,
decltype( cmp )> ordered_symbols( cmp );
159 std::multiset<
SCH_SYMBOL*,
decltype( cmp )> extra_units( cmp );
164 auto test = ordered_symbols.insert( symbol );
168 if( ( *(
test.first ) )->m_Uuid > symbol->
m_Uuid )
170 extra_units.insert( *(
test.first ) );
171 ordered_symbols.erase(
test.first );
172 ordered_symbols.insert( symbol );
176 extra_units.insert( symbol );
181 for(
EDA_ITEM* item : ordered_symbols )
192 wxString>( sheet.PathHumanReadable(),
193 symbol->
GetRef( &sheet ) ) );
201 NET_RECORD(
const wxString& aName ) :
206 std::vector<NET_NODE> m_Nodes;
209 std::vector<NET_RECORD*> nets;
211 for(
const auto& it :
m_schematic->ConnectionGraph()->GetNetMap() )
213 wxString net_name = it.first.Name;
214 const std::vector<CONNECTION_SUBGRAPH*>& subgraphs = it.second;
215 NET_RECORD* net_record =
nullptr;
217 if( subgraphs.empty() )
220 nets.emplace_back(
new NET_RECORD( net_name ) );
221 net_record = nets.back();
225 bool nc = subgraph->GetNoConnect() &&
229 for(
SCH_ITEM* item : subgraph->GetItems() )
239 net_record->m_Nodes.emplace_back(
pin, sheet, nc );
246 std::sort( nets.begin(), nets.end(),
247 [](
const NET_RECORD* a,
const NET_RECORD*b )
249 return StrNumCmp( a->m_Name, b->m_Name ) < 0;
252 for( NET_RECORD* net_record : nets )
255 std::sort( net_record->m_Nodes.begin(), net_record->m_Nodes.end(),
258 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
259 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
262 return a.m_Pin->GetShownNumber() < b.m_Pin->GetShownNumber();
273 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
274 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
276 return refA == refB && a.m_Pin->GetShownNumber() == b.m_Pin->GetShownNumber();
279 for(
const NET_NODE& netNode : net_record->m_Nodes )
284 if( refText[0] == wxChar(
'#' ) )
289 m_netNameNodes.insert( std::pair<wxString, NET_NODE>( net_record->m_Name, netNode ) );
293 for( NET_RECORD* record : nets )
301 wxString deviceFileCreatingError = wxString(
"" );
314 if( it->first->GetValue(
false, &it->second,
false )
315 != first_ele.first->GetValue(
false, &first_ele.second,
false ) )
320 if( it->first->GetFootprintFieldText(
false, &it->second,
false )
321 != first_ele.first->GetFootprintFieldText(
false, &first_ele.second,
false ) )
326 wxString ref1 = it->first->GetRef( &it->second );
327 wxString ref2 = first_ele.first->GetRef( &first_ele.second );
344 struct COMP_PACKAGE_STRUCT
347 wxString m_tolerance;
348 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> m_symbolSheetpaths;
351 COMP_PACKAGE_STRUCT compPackageStruct;
352 std::map<wxString, COMP_PACKAGE_STRUCT> compPackageMap;
354 for(
int groupIndex = 1; groupIndex < groupCount; groupIndex++ )
357 auto beginIter = pairIter.first;
358 auto endIter = pairIter.second;
360 SCH_SYMBOL* sym = ( beginIter->second ).first;
363 wxString valueText = sym->
GetValue(
false, &sheetPath,
false );
365 wxString deviceType = valueText + wxString(
"_") + footprintText;
367 while( deviceType.GetChar(deviceType.Length()-1) ==
'_' )
369 deviceType.RemoveLast();
374 wxArrayString fieldArray;
375 fieldArray.Add(
"Spice_Model" );
376 fieldArray.Add(
"VALUE" );
381 fieldArray.Add(
"TOLERANCE" );
382 fieldArray.Add(
"TOL" );
385 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths;
387 for(
auto iter = beginIter; iter != endIter; iter++ )
389 symbolSheetpaths.push_back( std::pair<
SCH_SYMBOL*,
391 iter->second.second ) );
394 std::stable_sort( symbolSheetpaths.begin(), symbolSheetpaths.end(),
397 compPackageStruct.m_value = value;
398 compPackageStruct.m_tolerance = tol;
399 compPackageStruct.m_symbolSheetpaths = symbolSheetpaths;
400 compPackageMap.insert( std::pair( deviceType, compPackageStruct ) );
404 wxString deviceFileName = wxFileName(
m_exportPath, deviceType,
405 wxString(
"txt" ) ).GetFullPath();
407 if( ( d = wxFopen( deviceFileName, wxT(
"wt" ) ) ) ==
nullptr )
410 msg.Printf(
_(
"Failed to create file '%s'.\n" ), deviceFileName );
411 deviceFileCreatingError += msg;
415 footprintText = footprintText.AfterLast(
':' );
417 wxArrayString footprintAlt;
420 for(
const wxString& fp : footprintArray )
422 if( ( fp.Find(
'*' ) != wxNOT_FOUND ) || ( fp.Find(
'?' ) != wxNOT_FOUND ) )
427 footprintAlt.Add( fp.AfterLast(
':' ) );
430 if( footprintText.IsEmpty() )
432 if( !footprintAlt.IsEmpty() )
434 footprintText = footprintAlt[0];
435 footprintAlt.RemoveAt( 0 );
439 footprintText = deviceType;
444 fmt::print( d,
"CLASS IC\n" );
458 for(
int ii = 0; ii < (int) pinList.size() - 1; ii++ )
460 if( pinList[ii]->GetNumber() == pinList[ii + 1]->GetNumber() )
463 pinList.erase( pinList.begin() + ii + 1 );
468 unsigned int pinCount = pinList.size();
469 fmt::print( d,
"PINCOUNT {}\n", pinCount );
476 if( !value.IsEmpty() )
478 fmt::print( d,
"PACKAGEPROP VALUE {}\n",
TO_UTF8( value ) );
483 fmt::print( d,
"PACKAGEPROP TOL {}\n",
TO_UTF8( tol ) );
486 if( !footprintAlt.IsEmpty() )
488 fmt::print( d,
"PACKAGEPROP ALT_SYMBOLS '({})'\n", fmt::join( footprintAlt,
"," ) );
491 wxArrayString propArray;
492 propArray.Add(
"PART_NUMBER" );
493 propArray.Add(
"mpn" );
494 propArray.Add(
"mfr_pn" );
499 fmt::print( d,
"PACKAGEPROP {} {}\n",
TO_UTF8( propArray[0] ),
TO_UTF8( data ) );
503 propArray.Add(
"HEIGHT" );
508 fmt::print( d,
"PACKAGEPROP {} {}\n",
TO_UTF8( propArray[0] ),
TO_UTF8( data ) );
511 fmt::print( d,
"END\n" );
516 fmt::print(
m_f,
"$PACKAGES\n" );
518 for(
auto iter = compPackageMap.begin(); iter != compPackageMap.end(); iter++ )
520 wxString deviceType = iter->first;
521 wxString value = iter->second.m_value;
522 wxString tolerance = iter->second.m_tolerance;
524 if( value.IsEmpty() && tolerance.IsEmpty() )
526 fmt::print(
m_f,
"! '{}' ; ",
TO_UTF8( deviceType ) );
528 else if( tolerance.IsEmpty() )
534 fmt::print(
m_f,
"! '{}' ! '{}' ! {} ; ",
TO_UTF8( deviceType ),
TO_UTF8( value ),
538 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths =
539 iter->second.m_symbolSheetpaths;
541 std::vector<wxString> refTexts;
542 for(
const auto& [ sym, sheetPath ] : symbolSheetpaths)
543 refTexts.push_back( sym->GetRef( &sheetPath ) );
545 fmt::print(
m_f,
"{}", fmt::join( refTexts,
",\n\t" ) );
547 fmt::print(
m_f,
"\n" );
550 if( !deviceFileCreatingError.IsEmpty() )
557 if( aString.IsEmpty() )
558 return wxEmptyString;
560 aString.Replace(
"\u03BC",
"u" );
562 std::regex reg(
"[!']|[^ -~]" );
563 wxString processedString = wxString( std::regex_replace( std::string( aString ), reg,
"?" ) );
565 std::regex search_reg(
"[^a-zA-Z0-9_/]" );
567 if( std::regex_search( std::string( processedString ), search_reg ) )
568 return wxString(
"'" ) + processedString + wxString(
"'" );
570 return processedString;
576 wxString pinName4Telesis = aPin.
GetName() + wxString(
"__" ) + aPin.
GetNumber();
577 std::regex reg(
"[^A-Za-z0-9_+?/-]" );
578 return wxString( std::regex_replace( std::string( pinName4Telesis ), reg,
"?" ) );
585 std::list<wxString> pinNameList;
589 for(
auto pin : aPinList )
592 wxString out_str =
"";
595 out_str.Printf( wxT(
"PINORDER %s " ),
TO_UTF8( aName ) );
597 for(
const wxString& pinName : pinNameList )
599 str.Printf(
",\n\t%s",
TO_UTF8( pinName ) );
602 out_str += wxString(
"\n" );
604 str.Printf( wxT(
"FUNCTION %s %s " ),
TO_UTF8( aName ),
TO_UTF8( aName ) );
607 for(
auto pin : aPinList )
609 str.Printf(
",\n\t%s",
TO_UTF8(
pin->GetNumber() ) );
613 out_str += wxString(
"\n" );
624 for(
auto iter = pairIter.first; iter != pairIter.second; ++iter )
629 for(
const wxString& field : aFieldArray )
633 wxString fieldText = fld->GetShownText( &sheetPath,
true );
635 if( !fieldText.IsEmpty() )
646 for(
auto iter = pairIter.first; iter != pairIter.second; ++iter )
650 for(
const wxString& field : aFieldArray )
654 wxString fieldText = fld->GetShownText(
false, 0 );
656 if( !fieldText.IsEmpty() )
667 return wxEmptyString;
674 std::regex reg(
"[^a-z0-9_-]" );
675 return wxString( std::regex_replace( std::string( aString ), reg,
"_" ) );
681 fmt::print(
m_f,
"$A_PROPERTIES\n" );
686 wxString sheetPathText = iter->first;
690 std::vector<wxString> refTexts;
694 for( iter = pairIter.first; iter != pairIter.second; ++iter )
696 wxString refText = iter->second;
697 refTexts.push_back( refText );
702 std::stable_sort( refTexts.begin(), refTexts.end(),
705 fmt::print(
m_f,
"{}", fmt::join( refTexts,
",\n\t" ) );
707 fmt::print(
m_f,
"\n" );
714 fmt::print(
m_f,
"$NETS\n" );
718 std::multimap<wxString, NET_NODE>::iterator iter =
m_netNameNodes.begin();
719 std::vector<NET_NODE> netNodes;
721 wxString netName = iter->first;
727 for( iter = pairIter.first; iter != pairIter.second; ++iter )
730 netNodes.push_back( netNode );
735 std::stable_sort( netNodes.begin(), netNodes.end() );
737 std::vector<wxString> nets;
738 for(
const NET_NODE& netNode : netNodes )
740 wxString refText = netNode.m_Pin->GetParentSymbol()->GetRef( &netNode.m_Sheet );
741 wxString pinText = netNode.m_Pin->GetShownNumber();
742 nets.push_back( refText + wxString(
"." ) + pinText );
745 fmt::print(
m_f,
"{}", fmt::join( nets,
",\n\t" ) );
747 fmt::print(
m_f,
"\n" );
754 while( ( aString.GetChar( aString.Length() - 1 ) >=
'0' )
755 && ( aString.GetChar( aString.Length() - 1 ) <=
'9' ) )
757 aString.RemoveLast();
768 while( ( aString.GetChar( aString.Length() - 1 ) >=
'0' )
769 && ( aString.GetChar( aString.Length() - 1 ) <=
'9' ) )
771 numString.insert( 0, aString.GetChar( aString.Length() - 1 ) );
772 aString.RemoveLast();
778 numString.ToULong( &val );
779 return (
unsigned int) val;
A subgraph is a set of items that are electrically connected on a single sheet.
A base class for most all the KiCad significant classes used in schematics and boards.
SCH_FIELD * FindFieldCaseInsensitive(const wxString &aFieldName)
wxArrayString GetFPFilters() const
std::vector< SCH_PIN * > GetPins() const override
wxString formatDevice(wxString aString)
Convert a string into one safe for a Telesis device name.
wxString m_exportPath
Directory to store device files.
static unsigned int extractTailNumber(wxString aString)
Extract the str's tailing number.
FILE * m_f
File pointer for netlist file writing operation.
static wxString removeTailDigits(wxString aString)
Remove the str's tailing digits.
void toAllegroPackageProperties()
Write $A_PROPERTIES section.
bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions, REPORTER &aReporter) override
Write netlist to aOutFileName.
std::list< std::pair< SCH_SYMBOL *, SCH_SHEET_PATH > > m_orderedSymbolsSheetpath
Store the ordered symbols with sheetpath.
static bool CompareSymbolSheetpath(const std::pair< SCH_SYMBOL *, SCH_SHEET_PATH > &aItem1, const std::pair< SCH_SYMBOL *, SCH_SHEET_PATH > &aItem2)
Compare two std::pair<SCH_SYMBOL*, SCH_SHEET_PATH> variables.
wxString formatText(wxString aString)
Convert a string into Telesis-safe format.
std::multimap< wxString, wxString > m_packageProperties
wxString formatFunction(wxString aName, std::vector< SCH_PIN * > aPinList)
Generate the definition of a function in Telesis format, which consists of multiple declarations (PIN...
static bool CompareLibPin(const SCH_PIN *aPin1, const SCH_PIN *aPin2)
Compare two SCH_PIN* variables.
wxString formatPin(const SCH_PIN &aPin)
Generate a Telesis-compatible pin name from a pin node.
static bool CompareSymbolRef(const wxString &aRefText1, const wxString &aRefText2)
Compare two wxString variables.
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...
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.
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
wxString GetShownNumber() const
const wxString & GetName() 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...
const SCH_SHEET * GetSheet(unsigned aIndex) const
const wxString GetValue(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const override
SCH_FIELD * FindFieldCaseInsensitive(const wxString &aFieldName)
Search for a SCH_FIELD with aFieldName.
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
std::vector< SCH_PIN * > GetLibPins() const
Populate a vector with all the pins from the library object that match the current unit and bodyStyle...
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
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 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.
Collection of utility functions for component reference designators (refdes)
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
wxString GetISO8601CurrentDateTime()
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition for symbol library class.