You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
5.6 KiB

  1. /**
  2. * @file pcb_netlist.cpp
  3. */
  4. /*
  5. * This program source code file is part of KiCad, a free EDA CAD application.
  6. *
  7. * Copyright (C) 1992-2011 Jean-Pierre Charras.
  8. * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>.
  9. * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, you may find one here:
  23. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  24. * or you may search the http://www.gnu.org website for the version 2 license,
  25. * or you may write to the Free Software Foundation, Inc.,
  26. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  27. */
  28. #include <macros.h>
  29. #include <refdes_utils.h>
  30. #include <reporter.h>
  31. #include "pcb_netlist.h"
  32. #include <class_module.h>
  33. #include <eda_pattern_match.h>
  34. int COMPONENT_NET::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
  35. {
  36. return aOut->Print( aNestLevel, "(pin_net %s %s)",
  37. aOut->Quotew( m_pinName ).c_str(),
  38. aOut->Quotew( m_netName ).c_str() );
  39. }
  40. void COMPONENT::SetModule( MODULE* aModule )
  41. {
  42. m_footprint.reset( aModule );
  43. if( aModule == NULL )
  44. return;
  45. aModule->SetReference( m_reference );
  46. aModule->SetValue( m_value );
  47. aModule->SetFPID( m_fpid );
  48. aModule->SetPath( m_path );
  49. }
  50. COMPONENT_NET COMPONENT::m_emptyNet;
  51. const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName )
  52. {
  53. for( unsigned i = 0; i < m_nets.size(); i++ )
  54. {
  55. if( m_nets[i].GetPinName() == aPinName )
  56. return m_nets[i];
  57. }
  58. return m_emptyNet;
  59. }
  60. void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
  61. {
  62. int nl = aNestLevel;
  63. aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
  64. aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
  65. if( ! ( aCtl & CTL_OMIT_EXTRA ) )
  66. {
  67. aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
  68. aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
  69. aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
  70. wxString path;
  71. for( const KIID& pathStep : m_path )
  72. path += '/' + pathStep.AsString();
  73. aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
  74. }
  75. if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
  76. {
  77. aOut->Print( nl+1, "(fp_filters" );
  78. for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
  79. aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
  80. aOut->Print( 0, ")\n" );
  81. }
  82. if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
  83. {
  84. int llen = aOut->Print( nl+1, "(nets " );
  85. for( unsigned i = 0; i < m_nets.size(); ++i )
  86. {
  87. if( llen > 80 )
  88. {
  89. aOut->Print( 0, "\n" );
  90. llen = aOut->Print( nl+1, " " );
  91. }
  92. llen += m_nets[i].Format( aOut, 0, aCtl );
  93. }
  94. aOut->Print( 0, ")\n" );
  95. }
  96. aOut->Print( nl, ")\n" ); // </ref>
  97. }
  98. void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
  99. {
  100. int nl = aNestLevel;
  101. aOut->Print( nl, "(%s\n", aDocName );
  102. for( unsigned i = 0; i < m_components.size(); i++ )
  103. {
  104. m_components[i].Format( aOut, nl+1, aCtl );
  105. }
  106. aOut->Print( nl, ")\n" );
  107. }
  108. void NETLIST::AddComponent( COMPONENT* aComponent )
  109. {
  110. m_components.push_back( aComponent );
  111. }
  112. COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
  113. {
  114. COMPONENT* component = NULL;
  115. for( unsigned i = 0; i < m_components.size(); i++ )
  116. {
  117. if( m_components[i].GetReference() == aReference )
  118. {
  119. component = &m_components[i];
  120. break;
  121. }
  122. }
  123. return component;
  124. }
  125. COMPONENT* NETLIST::GetComponentByPath( const KIID_PATH& aUuidPath )
  126. {
  127. for( COMPONENT& component : m_components )
  128. {
  129. if( component.GetPath() == aUuidPath )
  130. return &component;
  131. }
  132. return nullptr;
  133. }
  134. /**
  135. * Function ByFPID
  136. * is a helper function used to sort the component list used by loadNewModules.
  137. */
  138. static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
  139. {
  140. return ref.GetFPID() > cmp.GetFPID();
  141. }
  142. void NETLIST::SortByFPID()
  143. {
  144. m_components.sort( ByFPID );
  145. }
  146. /**
  147. * Operator <
  148. * compares two #COMPONENT objects by reference designator.
  149. */
  150. bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
  151. {
  152. return UTIL::RefDesStringCompare( item1.GetReference(), item2.GetReference() ) < 0;
  153. }
  154. void NETLIST::SortByReference()
  155. {
  156. m_components.sort();
  157. }
  158. bool NETLIST::AnyFootprintsLinked() const
  159. {
  160. for( unsigned i = 0; i < m_components.size(); i++ )
  161. {
  162. if( !m_components[i].GetFPID().empty() )
  163. return true;
  164. }
  165. return false;
  166. }
  167. bool NETLIST::AllFootprintsLinked() const
  168. {
  169. for( unsigned i = 0; i < m_components.size(); i++ )
  170. {
  171. if( m_components[i].GetFPID().empty() )
  172. return false;
  173. }
  174. return true;
  175. }