32#include <fmt/ranges.h>
42 m_exportPath = wxFileName( aOutFileName ).GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR )
43 + wxString(
"devices" );
48 wxString msg = wxString::Format(
_(
"Failed to create directory 'devices' ." ) );
55 if( (
m_f = wxFopen( aOutFileName, wxT(
"wt" ) ) ) ==
nullptr )
57 wxString msg = wxString::Format(
_(
"Failed to create file '%s'." ), aOutFileName );
64 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 )
125 return aRefText1 < aRefText2;
149 b->GetRef( &sheet,
false ),
true ) < 0 );
152 std::set<
SCH_SYMBOL*,
decltype( cmp )> ordered_symbols( cmp );
153 std::multiset<
SCH_SYMBOL*,
decltype( cmp )> extra_units( cmp );
158 auto test = ordered_symbols.insert( symbol );
162 if( ( *(
test.first ) )->m_Uuid > symbol->
m_Uuid )
164 extra_units.insert( *(
test.first ) );
165 ordered_symbols.erase(
test.first );
166 ordered_symbols.insert( symbol );
170 extra_units.insert( symbol );
175 for(
EDA_ITEM* item : ordered_symbols )
186 wxString>( sheet.PathHumanReadable(),
187 symbol->
GetRef( &sheet ) ) );
195 NET_RECORD(
const wxString& aName ) :
200 std::vector<NET_NODE> m_Nodes;
203 std::vector<NET_RECORD*> nets;
205 for(
const auto& it :
m_schematic->ConnectionGraph()->GetNetMap() )
207 wxString net_name = it.first.Name;
208 const std::vector<CONNECTION_SUBGRAPH*>& subgraphs = it.second;
209 NET_RECORD* net_record =
nullptr;
211 if( subgraphs.empty() )
214 nets.emplace_back(
new NET_RECORD( net_name ) );
215 net_record = nets.back();
219 bool nc = subgraph->GetNoConnect() &&
223 for(
SCH_ITEM* item : subgraph->GetItems() )
233 net_record->m_Nodes.emplace_back(
pin, sheet, nc );
240 std::sort( nets.begin(), nets.end(),
241 [](
const NET_RECORD* a,
const NET_RECORD*b )
243 return StrNumCmp( a->m_Name, b->m_Name ) < 0;
246 for( NET_RECORD* net_record : nets )
249 std::sort( net_record->m_Nodes.begin(), net_record->m_Nodes.end(),
252 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
253 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
256 return a.m_Pin->GetShownNumber() < b.m_Pin->GetShownNumber();
267 wxString refA = a.m_Pin->GetParentSymbol()->GetRef( &a.m_Sheet );
268 wxString refB = b.m_Pin->GetParentSymbol()->GetRef( &b.m_Sheet );
270 return refA == refB && a.m_Pin->GetShownNumber() == b.m_Pin->GetShownNumber();
273 for(
const NET_NODE& netNode : net_record->m_Nodes )
278 if( refText[0] == wxChar(
'#' ) )
283 m_netNameNodes.insert( std::pair<wxString, NET_NODE>( net_record->m_Name, netNode ) );
287 for( NET_RECORD* record : nets )
295 wxString deviceFileCreatingError = wxString(
"" );
308 if( it->first->GetValue(
false, &it->second,
false )
309 != first_ele.first->GetValue(
false, &first_ele.second,
false ) )
314 if( it->first->GetFootprintFieldText(
false, &it->second,
false )
315 != first_ele.first->GetFootprintFieldText(
false, &first_ele.second,
false ) )
320 wxString ref1 = it->first->GetRef( &it->second );
321 wxString ref2 = first_ele.first->GetRef( &first_ele.second );
338 struct COMP_PACKAGE_STRUCT
341 wxString m_tolerance;
342 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> m_symbolSheetpaths;
345 COMP_PACKAGE_STRUCT compPackageStruct;
346 std::map<wxString, COMP_PACKAGE_STRUCT> compPackageMap;
348 for(
int groupIndex = 1; groupIndex < groupCount; groupIndex++ )
351 auto beginIter = pairIter.first;
352 auto endIter = pairIter.second;
354 SCH_SYMBOL* sym = ( beginIter->second ).first;
357 wxString valueText = sym->
GetValue(
false, &sheetPath,
false );
359 wxString deviceType = valueText + wxString(
"_") + footprintText;
361 while( deviceType.GetChar(deviceType.Length()-1) ==
'_' )
363 deviceType.RemoveLast();
368 wxArrayString fieldArray;
369 fieldArray.Add(
"Spice_Model" );
370 fieldArray.Add(
"VALUE" );
375 fieldArray.Add(
"TOLERANCE" );
376 fieldArray.Add(
"TOL" );
379 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths;
381 for(
auto iter = beginIter; iter != endIter; iter++ )
383 symbolSheetpaths.push_back( std::pair<
SCH_SYMBOL*,
385 iter->second.second ) );
388 std::stable_sort( symbolSheetpaths.begin(), symbolSheetpaths.end(),
391 compPackageStruct.m_value = value;
392 compPackageStruct.m_tolerance = tol;
393 compPackageStruct.m_symbolSheetpaths = symbolSheetpaths;
394 compPackageMap.insert( std::pair( deviceType, compPackageStruct ) );
398 wxString deviceFileName = wxFileName(
m_exportPath, deviceType,
399 wxString(
"txt" ) ).GetFullPath();
401 if( ( d = wxFopen( deviceFileName, wxT(
"wt" ) ) ) ==
nullptr )
404 msg.Printf(
_(
"Failed to create file '%s'.\n" ), deviceFileName );
405 deviceFileCreatingError += msg;
409 footprintText = footprintText.AfterLast(
':' );
411 wxArrayString footprintAlt;
414 for(
const wxString& fp : footprintArray )
416 if( ( fp.Find(
'*' ) != wxNOT_FOUND ) || ( fp.Find(
'?' ) != wxNOT_FOUND ) )
421 footprintAlt.Add( fp.AfterLast(
':' ) );
424 if( footprintText.IsEmpty() )
426 if( !footprintAlt.IsEmpty() )
428 footprintText = footprintAlt[0];
429 footprintAlt.RemoveAt( 0 );
433 footprintText = deviceType;
438 fmt::print( d,
"CLASS IC\n" );
452 for(
int ii = 0; ii < (int) pinList.size() - 1; ii++ )
454 if( pinList[ii]->GetNumber() == pinList[ii + 1]->GetNumber() )
457 pinList.erase( pinList.begin() + ii + 1 );
462 unsigned int pinCount = pinList.size();
463 fmt::print( d,
"PINCOUNT {}\n", pinCount );
470 if( !value.IsEmpty() )
472 fmt::print( d,
"PACKAGEPROP VALUE {}\n",
TO_UTF8( value ) );
477 fmt::print( d,
"PACKAGEPROP TOL {}\n",
TO_UTF8( tol ) );
480 if( !footprintAlt.IsEmpty() )
482 fmt::print( d,
"PACKAGEPROP ALT_SYMBOLS '({})'\n", fmt::join( footprintAlt,
"," ) );
485 wxArrayString propArray;
486 propArray.Add(
"PART_NUMBER" );
487 propArray.Add(
"mpn" );
488 propArray.Add(
"mfr_pn" );
493 fmt::print( d,
"PACKAGEPROP {} {}\n",
TO_UTF8( propArray[0] ),
TO_UTF8( data ) );
497 propArray.Add(
"HEIGHT" );
502 fmt::print( d,
"PACKAGEPROP {} {}\n",
TO_UTF8( propArray[0] ),
TO_UTF8( data ) );
505 fmt::print( d,
"END\n" );
510 fmt::print(
m_f,
"$PACKAGES\n" );
512 for(
auto iter = compPackageMap.begin(); iter != compPackageMap.end(); iter++ )
514 wxString deviceType = iter->first;
515 wxString value = iter->second.m_value;
516 wxString tolerance = iter->second.m_tolerance;
520 if( value.StartsWith(
"'" ) and value.EndsWith(
"'" ) )
522 value.Remove( 0, 1 );
526 if( value.IsEmpty() && tolerance.IsEmpty() )
528 fmt::print(
m_f,
"! '{}' ; ",
TO_UTF8( deviceType ) );
530 else if( tolerance.IsEmpty() )
536 fmt::print(
m_f,
"! '{}' ! '{}' ! {} ; ",
TO_UTF8( deviceType ),
TO_UTF8( value ),
540 std::vector<std::pair<SCH_SYMBOL*, SCH_SHEET_PATH>> symbolSheetpaths =
541 iter->second.m_symbolSheetpaths;
543 std::vector<wxString> refTexts;
544 for(
const auto& [ sym, sheetPath ] : symbolSheetpaths)
545 refTexts.push_back( sym->GetRef( &sheetPath ) );
547 fmt::print(
m_f,
"{}", fmt::join( refTexts,
",\n\t" ) );
549 fmt::print(
m_f,
"\n" );
552 if( !deviceFileCreatingError.IsEmpty() )
559 if( aString.IsEmpty() )
560 return wxEmptyString;
564 aString.Replace( mu,
"u" );
566 aString.Replace( mu,
"u" );
568 std::regex reg(
"[!']|[^ -~]" );
569 wxString processedString = wxString( std::regex_replace( std::string( aString ), reg,
"?" ) );
571 std::regex search_reg(
"[^a-zA-Z0-9_/]" );
573 if( std::regex_search( std::string( processedString ), search_reg ) )
574 return wxString(
"'" ) + processedString + wxString(
"'" );
576 return processedString;
582 wxString pinName4Telesis = aPin.
GetName() + wxString(
"__" ) + aPin.
GetNumber();
583 std::regex reg(
"[^A-Za-z0-9_+?/-]" );
584 return wxString( std::regex_replace( std::string( pinName4Telesis ), reg,
"?" ) );
591 std::list<wxString> pinNameList;
595 for(
auto pin : aPinList )
598 wxString out_str =
"";
601 out_str.Printf( wxT(
"PINORDER %s " ),
TO_UTF8( aName ) );
603 for(
const wxString& pinName : pinNameList )
605 str.Printf(
",\n\t%s",
TO_UTF8( pinName ) );
608 out_str += wxString(
"\n" );
610 str.Printf( wxT(
"FUNCTION %s %s " ),
TO_UTF8( aName ),
TO_UTF8( aName ) );
613 for(
auto pin : aPinList )
615 str.Printf(
",\n\t%s",
TO_UTF8(
pin->GetNumber() ) );
619 out_str += wxString(
"\n" );
630 for(
auto iter = pairIter.first; iter != pairIter.second; ++iter )
635 for(
const wxString& field : aFieldArray )
639 wxString fieldText = fld->GetShownText( &sheetPath,
true );
641 if( !fieldText.IsEmpty() )
652 for(
auto iter = pairIter.first; iter != pairIter.second; ++iter )
656 for(
const wxString& field : aFieldArray )
660 wxString fieldText = fld->GetShownText(
false, 0 );
662 if( !fieldText.IsEmpty() )
673 return wxEmptyString;
680 std::regex reg(
"[^a-z0-9_-]" );
681 return wxString( std::regex_replace( std::string( aString ), reg,
"_" ) );
687 fmt::print(
m_f,
"$A_PROPERTIES\n" );
692 wxString sheetPathText = iter->first;
696 std::vector<wxString> refTexts;
700 for( iter = pairIter.first; iter != pairIter.second; ++iter )
702 wxString refText = iter->second;
703 refTexts.push_back( refText );
708 std::stable_sort( refTexts.begin(), refTexts.end(),
711 fmt::print(
m_f,
"{}", fmt::join( refTexts,
",\n\t" ) );
713 fmt::print(
m_f,
"\n" );
720 fmt::print(
m_f,
"$NETS\n" );
724 std::multimap<wxString, NET_NODE>::iterator iter =
m_netNameNodes.begin();
725 std::vector<NET_NODE> netNodes;
727 wxString netName = iter->first;
733 for( iter = pairIter.first; iter != pairIter.second; ++iter )
736 netNodes.push_back( netNode );
741 std::stable_sort( netNodes.begin(), netNodes.end() );
743 std::vector<wxString> nets;
744 for(
const NET_NODE& netNode : netNodes )
746 wxString refText = netNode.m_Pin->GetParentSymbol()->GetRef( &netNode.m_Sheet );
747 wxString pinText = netNode.m_Pin->GetShownNumber();
748 nets.push_back( refText + wxString(
"." ) + pinText );
751 fmt::print(
m_f,
"{}", fmt::join( nets,
",\n\t" ) );
753 fmt::print(
m_f,
"\n" );
760 while( ( aString.GetChar( aString.Length() - 1 ) >=
'0' )
761 && ( aString.GetChar( aString.Length() - 1 ) <=
'9' ) )
763 aString.RemoveLast();
774 while( ( aString.GetChar( aString.Length() - 1 ) >=
'0' )
775 && ( aString.GetChar( aString.Length() - 1 ) <=
'9' ) )
777 numString.insert( 0, aString.GetChar( aString.Length() - 1 ) );
778 aString.RemoveLast();
784 numString.ToULong( &val );
785 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
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...
const SCH_SHEET * GetSheet(unsigned aIndex) const
SCH_FIELD * FindFieldCaseInsensitive(const wxString &aFieldName)
Search for a SCH_FIELD with aFieldName.
const wxString GetValue(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText, const wxString &aVariantName=wxEmptyString) const override
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText, const wxString &aVariantName=wxEmptyString) 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...
bool GetExcludedFromBoard(const SCH_SHEET_PATH *aInstance=nullptr, 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.
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.