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.

284 lines
7.3 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 CERN
  5. * Copyright (C) 2019 KiCad Developers, see change_log.txt for contributors.
  6. * @author Jon Evans <jon@craftyjon.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <lib_pin.h>
  22. #include <sch_component.h>
  23. #include <sch_pin.h>
  24. #include <sch_sheet_path.h>
  25. #include <sch_edit_frame.h>
  26. SCH_PIN::SCH_PIN( LIB_PIN* aLibPin, SCH_COMPONENT* aParentComponent ) :
  27. SCH_ITEM( aParentComponent, SCH_PIN_T )
  28. {
  29. m_alt = wxEmptyString;
  30. m_number = aLibPin->GetNumber();
  31. m_libPin = aLibPin;
  32. SetPosition( aLibPin->GetPosition() );
  33. m_isDangling = true;
  34. }
  35. SCH_PIN::SCH_PIN( const SCH_PIN& aPin ) :
  36. SCH_ITEM( aPin )
  37. {
  38. m_alt = aPin.m_alt;
  39. m_number = aPin.m_number;
  40. m_libPin = aPin.m_libPin;
  41. m_position = aPin.m_position;
  42. m_isDangling = aPin.m_isDangling;
  43. }
  44. SCH_PIN& SCH_PIN::operator=( const SCH_PIN& aPin )
  45. {
  46. SCH_ITEM::operator=( aPin );
  47. m_alt = aPin.m_alt;
  48. m_number = aPin.m_number;
  49. m_libPin = aPin.m_libPin;
  50. m_position = aPin.m_position;
  51. m_isDangling = aPin.m_isDangling;
  52. return *this;
  53. }
  54. wxString SCH_PIN::GetName() const
  55. {
  56. if( !m_alt.IsEmpty() )
  57. return m_alt;
  58. return m_libPin->GetName();
  59. }
  60. ELECTRICAL_PINTYPE SCH_PIN::GetType() const
  61. {
  62. if( !m_alt.IsEmpty() )
  63. return m_libPin->GetAlt( m_alt ).m_Type;
  64. return m_libPin->GetType();
  65. }
  66. GRAPHIC_PINSHAPE SCH_PIN::GetShape() const
  67. {
  68. if( !m_alt.IsEmpty() )
  69. return m_libPin->GetAlt( m_alt ).m_Shape;
  70. return m_libPin->GetShape();
  71. }
  72. int SCH_PIN::GetOrientation() const
  73. {
  74. return m_libPin->GetOrientation();
  75. }
  76. int SCH_PIN::GetLength() const
  77. {
  78. return m_libPin->GetLength();
  79. }
  80. bool SCH_PIN::Matches( wxFindReplaceData& aSearchData, void* aAuxDat )
  81. {
  82. if( !( aSearchData.GetFlags() & FR_SEARCH_ALL_PINS ) )
  83. return false;
  84. return EDA_ITEM::Matches( GetName(), aSearchData )
  85. || EDA_ITEM::Matches( GetNumber(), aSearchData );
  86. }
  87. bool SCH_PIN::Replace( wxFindReplaceData& aSearchData, void* aAuxData )
  88. {
  89. bool isReplaced = false;
  90. /* TODO: waiting on a way to override pins in the schematic...
  91. isReplaced |= EDA_ITEM::Replace( aSearchData, m_name );
  92. isReplaced |= EDA_ITEM::Replace( aSearchData, m_number );
  93. */
  94. return isReplaced;
  95. }
  96. SCH_COMPONENT* SCH_PIN::GetParentComponent() const
  97. {
  98. return static_cast<SCH_COMPONENT*>( GetParent() );
  99. }
  100. wxString SCH_PIN::GetSelectMenuText( EDA_UNITS aUnits ) const
  101. {
  102. return wxString::Format( "%s %s",
  103. GetParentComponent()->GetSelectMenuText( aUnits ),
  104. m_libPin->GetSelectMenuText( aUnits ) );
  105. }
  106. void SCH_PIN::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, MSG_PANEL_ITEMS& aList )
  107. {
  108. wxString msg;
  109. aList.push_back( MSG_PANEL_ITEM( _( "Type" ), _( "Pin" ), CYAN ) );
  110. if( m_libPin->GetUnit() == 0 )
  111. msg = _( "All" );
  112. else
  113. msg.Printf( wxT( "%d" ), m_libPin->GetUnit() );
  114. aList.push_back( MSG_PANEL_ITEM( _( "Unit" ), msg, BROWN ) );
  115. if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::BASE )
  116. msg = _( "no" );
  117. else if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::DEMORGAN )
  118. msg = _( "yes" );
  119. else
  120. msg = wxT( "?" );
  121. aList.push_back( MSG_PANEL_ITEM( _( "Converted" ), msg, BROWN ) );
  122. aList.push_back( MSG_PANEL_ITEM( _( "Name" ), GetName(), DARKCYAN ) );
  123. aList.push_back( MSG_PANEL_ITEM( _( "Number" ), msg, DARKCYAN ) );
  124. aList.push_back( MSG_PANEL_ITEM( _( "Type" ), ElectricalPinTypeGetText( GetType() ), RED ) );
  125. msg = PinShapeGetText( GetShape() );
  126. aList.push_back( MSG_PANEL_ITEM( _( "Style" ), msg, BLUE ) );
  127. msg = IsVisible() ? _( "Yes" ) : _( "No" );
  128. aList.push_back( MSG_PANEL_ITEM( _( "Visible" ), msg, DARKGREEN ) );
  129. // Display pin length
  130. msg = StringFromValue( aFrame->GetUserUnits(), GetLength(), true );
  131. aList.push_back( MSG_PANEL_ITEM( _( "Length" ), msg, MAGENTA ) );
  132. msg = PinOrientationName( (unsigned) PinOrientationIndex( GetOrientation() ) );
  133. aList.push_back( MSG_PANEL_ITEM( _( "Orientation" ), msg, DARKMAGENTA ) );
  134. msg = MessageTextFromValue( aFrame->GetUserUnits(), m_position.x, true );
  135. aList.emplace_back( _( "Pos X" ), msg, DARKMAGENTA );
  136. msg = MessageTextFromValue( aFrame->GetUserUnits(), m_position.y, true );
  137. aList.emplace_back( _( "Pos Y" ), msg, DARKMAGENTA );
  138. SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( aFrame );
  139. SCH_SHEET_PATH* currentSheet = schframe ? &schframe->GetCurrentSheet() : nullptr;
  140. SCH_COMPONENT* comp = GetParentComponent();
  141. aList.emplace_back( comp->GetRef( currentSheet ), comp->GetValue( currentSheet ), DARKCYAN );
  142. #if defined(DEBUG)
  143. SCH_EDIT_FRAME* frame = dynamic_cast<SCH_EDIT_FRAME*>( aFrame );
  144. if( !frame )
  145. return;
  146. SCH_CONNECTION* conn = Connection( frame->GetCurrentSheet() );
  147. if( conn )
  148. conn->AppendInfoToMsgPanel( aList );
  149. #endif
  150. }
  151. void SCH_PIN::ClearDefaultNetName( const SCH_SHEET_PATH* aPath )
  152. {
  153. std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
  154. if( aPath )
  155. m_net_name_map.erase( *aPath );
  156. else
  157. m_net_name_map.clear();
  158. }
  159. wxString SCH_PIN::GetDefaultNetName( const SCH_SHEET_PATH& aPath )
  160. {
  161. if( m_libPin->IsPowerConnection() )
  162. return m_libPin->GetName();
  163. std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
  164. if( m_net_name_map.count( aPath ) > 0 )
  165. return m_net_name_map.at( aPath );
  166. wxString name = "Net-(";
  167. name << GetParentComponent()->GetRef( &aPath );
  168. bool annotated = true;
  169. // Add timestamp for uninitialized components
  170. if( name.Last() == '?' )
  171. {
  172. name << GetParentComponent()->m_Uuid.AsString();
  173. annotated = false;
  174. }
  175. name << "-Pad" << m_libPin->GetNumber() << ")";
  176. if( annotated )
  177. m_net_name_map[ aPath ] = name;
  178. return name;
  179. }
  180. wxPoint SCH_PIN::GetTransformedPosition() const
  181. {
  182. TRANSFORM t = GetParentComponent()->GetTransform();
  183. return ( t.TransformCoordinate( GetLocalPosition() ) + GetParentComponent()->GetPosition() );
  184. }
  185. const EDA_RECT SCH_PIN::GetBoundingBox() const
  186. {
  187. TRANSFORM t = GetParentComponent()->GetTransform();
  188. EDA_RECT r = m_libPin->GetBoundingBox();
  189. r.RevertYAxis();
  190. r = t.TransformCoordinate( r );
  191. r.Offset( GetParentComponent()->GetPosition() );
  192. return r;
  193. }
  194. bool SCH_PIN::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  195. {
  196. EDA_RECT rect = GetBoundingBox();
  197. return rect.Inflate( aAccuracy ).Contains( aPosition );
  198. }
  199. bool SCH_PIN::ConnectionPropagatesTo( const EDA_ITEM* aItem ) const
  200. {
  201. // Reciprocal checking is done in CONNECTION_GRAPH anyway
  202. return !( m_libPin->GetType() == ELECTRICAL_PINTYPE::PT_NC );
  203. }