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.

232 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 <kicad_string.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_timeStamp );
  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. aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( m_timeStamp ).c_str() );
  71. }
  72. if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
  73. {
  74. aOut->Print( nl+1, "(fp_filters" );
  75. for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
  76. aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
  77. aOut->Print( 0, ")\n" );
  78. }
  79. if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
  80. {
  81. int llen = aOut->Print( nl+1, "(nets " );
  82. for( unsigned i = 0; i < m_nets.size(); ++i )
  83. {
  84. if( llen > 80 )
  85. {
  86. aOut->Print( 0, "\n" );
  87. llen = aOut->Print( nl+1, " " );
  88. }
  89. llen += m_nets[i].Format( aOut, 0, aCtl );
  90. }
  91. aOut->Print( 0, ")\n" );
  92. }
  93. aOut->Print( nl, ")\n" ); // </ref>
  94. }
  95. void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
  96. {
  97. int nl = aNestLevel;
  98. aOut->Print( nl, "(%s\n", aDocName );
  99. for( unsigned i = 0; i < m_components.size(); i++ )
  100. {
  101. m_components[i].Format( aOut, nl+1, aCtl );
  102. }
  103. aOut->Print( nl, ")\n" );
  104. }
  105. void NETLIST::AddComponent( COMPONENT* aComponent )
  106. {
  107. m_components.push_back( aComponent );
  108. }
  109. COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
  110. {
  111. COMPONENT* component = NULL;
  112. for( unsigned i = 0; i < m_components.size(); i++ )
  113. {
  114. if( m_components[i].GetReference() == aReference )
  115. {
  116. component = &m_components[i];
  117. break;
  118. }
  119. }
  120. return component;
  121. }
  122. COMPONENT* NETLIST::GetComponentByTimeStamp( const wxString& aTimeStamp )
  123. {
  124. COMPONENT* component = NULL;
  125. for( unsigned i = 0; i < m_components.size(); i++ )
  126. {
  127. if( m_components[i].GetTimeStamp() == aTimeStamp )
  128. {
  129. component = &m_components[i];
  130. break;
  131. }
  132. }
  133. return component;
  134. }
  135. /**
  136. * Function ByFPID
  137. * is a helper function used to sort the component list used by loadNewModules.
  138. */
  139. static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
  140. {
  141. return ref.GetFPID() > cmp.GetFPID();
  142. }
  143. void NETLIST::SortByFPID()
  144. {
  145. m_components.sort( ByFPID );
  146. }
  147. /**
  148. * Operator <
  149. * compares two #COMPONENT objects by reference designator.
  150. */
  151. bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
  152. {
  153. return RefDesStringCompare(item1.GetReference(), item2.GetReference() ) < 0;
  154. }
  155. void NETLIST::SortByReference()
  156. {
  157. m_components.sort();
  158. }
  159. bool NETLIST::AnyFootprintsLinked() const
  160. {
  161. for( unsigned i = 0; i < m_components.size(); i++ )
  162. {
  163. if( !m_components[i].GetFPID().empty() )
  164. return true;
  165. }
  166. return false;
  167. }
  168. bool NETLIST::AllFootprintsLinked() const
  169. {
  170. for( unsigned i = 0; i < m_components.size(); i++ )
  171. {
  172. if( m_components[i].GetFPID().empty() )
  173. return false;
  174. }
  175. return true;
  176. }