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.

417 lines
11 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-2023 KiCad Developers, see AUTHORS.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 <base_units.h>
  22. #include <lib_pin.h>
  23. #include <sch_symbol.h>
  24. #include <sch_pin.h>
  25. #include <schematic.h>
  26. #include <schematic_settings.h>
  27. #include <sch_sheet_path.h>
  28. #include <sch_edit_frame.h>
  29. #include "string_utils.h"
  30. SCH_PIN::SCH_PIN( LIB_PIN* aLibPin, SCH_SYMBOL* aParentSymbol ) :
  31. SCH_ITEM( aParentSymbol, SCH_PIN_T )
  32. {
  33. m_layer = LAYER_PIN;
  34. m_alt = wxEmptyString;
  35. m_number = aLibPin->GetNumber();
  36. m_libPin = aLibPin;
  37. SetPosition( aLibPin->GetPosition() );
  38. m_isDangling = true;
  39. }
  40. /**
  41. * Create a proxy pin from an alternate pin designation.
  42. * The LIB_PIN data will be filled in when the pin is resolved (see SCH_SYMBOL::UpdatePins).
  43. */
  44. SCH_PIN::SCH_PIN( SCH_SYMBOL* aParentSymbol, const wxString& aNumber, const wxString& aAlt ) :
  45. SCH_ITEM( aParentSymbol, SCH_PIN_T )
  46. {
  47. m_layer = LAYER_PIN;
  48. m_alt = aAlt;
  49. m_number = aNumber;
  50. m_libPin = nullptr;
  51. m_isDangling = true;
  52. }
  53. SCH_PIN::SCH_PIN( const SCH_PIN& aPin ) :
  54. SCH_ITEM( aPin )
  55. {
  56. m_layer = aPin.m_layer;
  57. m_alt = aPin.m_alt;
  58. m_number = aPin.m_number;
  59. m_libPin = aPin.m_libPin;
  60. m_position = aPin.m_position;
  61. m_isDangling = aPin.m_isDangling;
  62. }
  63. SCH_PIN& SCH_PIN::operator=( const SCH_PIN& aPin )
  64. {
  65. SCH_ITEM::operator=( aPin );
  66. m_alt = aPin.m_alt;
  67. m_number = aPin.m_number;
  68. m_libPin = aPin.m_libPin;
  69. m_position = aPin.m_position;
  70. m_isDangling = aPin.m_isDangling;
  71. return *this;
  72. }
  73. wxString SCH_PIN::GetName() const
  74. {
  75. if( !m_alt.IsEmpty() )
  76. return m_alt;
  77. return m_libPin->GetName();
  78. }
  79. wxString SCH_PIN::GetShownName() const
  80. {
  81. wxString name = m_libPin->GetName();
  82. if( !m_alt.IsEmpty() )
  83. name = m_alt;
  84. if( name == wxS( "~" ) )
  85. return wxEmptyString;
  86. else
  87. return name;
  88. }
  89. wxString SCH_PIN::GetShownNumber() const
  90. {
  91. if( m_number == wxS( "~" ) )
  92. return wxEmptyString;
  93. else
  94. return m_number;
  95. }
  96. ELECTRICAL_PINTYPE SCH_PIN::GetType() const
  97. {
  98. if( !m_alt.IsEmpty() )
  99. return m_libPin->GetAlt( m_alt ).m_Type;
  100. return m_libPin->GetType();
  101. }
  102. GRAPHIC_PINSHAPE SCH_PIN::GetShape() const
  103. {
  104. if( !m_alt.IsEmpty() )
  105. return m_libPin->GetAlt( m_alt ).m_Shape;
  106. return m_libPin->GetShape();
  107. }
  108. int SCH_PIN::GetOrientation() const
  109. {
  110. return m_libPin->GetOrientation();
  111. }
  112. int SCH_PIN::GetLength() const
  113. {
  114. return m_libPin->GetLength();
  115. }
  116. const BOX2I SCH_PIN::ViewBBox() const
  117. {
  118. return GetBoundingBox( false, true, true );
  119. }
  120. void SCH_PIN::ViewGetLayers( int aLayers[], int& aCount ) const
  121. {
  122. aCount = 4;
  123. aLayers[0] = LAYER_DANGLING;
  124. aLayers[1] = LAYER_DEVICE;
  125. aLayers[2] = LAYER_SELECTION_SHADOWS;
  126. aLayers[3] = LAYER_OP_CURRENTS;
  127. }
  128. bool SCH_PIN::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxDat ) const
  129. {
  130. const SCH_SEARCH_DATA& schSearchData =
  131. dynamic_cast<const SCH_SEARCH_DATA&>( aSearchData );
  132. if( !schSearchData.searchAllPins )
  133. return false;
  134. return EDA_ITEM::Matches( GetName(), aSearchData )
  135. || EDA_ITEM::Matches( GetNumber(), aSearchData );
  136. }
  137. bool SCH_PIN::Replace( const EDA_SEARCH_DATA& aSearchData, void* aAuxData )
  138. {
  139. bool isReplaced = false;
  140. /* TODO: waiting on a way to override pins in the schematic...
  141. isReplaced |= EDA_ITEM::Replace( aSearchData, m_name );
  142. isReplaced |= EDA_ITEM::Replace( aSearchData, m_number );
  143. */
  144. return isReplaced;
  145. }
  146. SCH_SYMBOL* SCH_PIN::GetParentSymbol() const
  147. {
  148. return static_cast<SCH_SYMBOL*>( GetParent() );
  149. }
  150. wxString SCH_PIN::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
  151. {
  152. return wxString::Format( "Symbol %s %s",
  153. UnescapeString( GetParentSymbol()->GetField( REFERENCE_FIELD )->GetText() ),
  154. m_libPin->GetItemDescription( aUnitsProvider ) );
  155. }
  156. void SCH_PIN::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  157. {
  158. wxString msg;
  159. aList.emplace_back( _( "Type" ), _( "Pin" ) );
  160. if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::BASE )
  161. msg = _( "no" );
  162. else if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::DEMORGAN )
  163. msg = _( "yes" );
  164. else
  165. msg = wxT( "?" );
  166. aList.emplace_back( _( "Converted" ), msg );
  167. aList.emplace_back( _( "Name" ), GetShownName() );
  168. aList.emplace_back( _( "Number" ), GetShownNumber() );
  169. aList.emplace_back( _( "Type" ), ElectricalPinTypeGetText( GetType() ) );
  170. aList.emplace_back( _( "Style" ), PinShapeGetText( GetShape() ) );
  171. aList.emplace_back( _( "Visible" ), IsVisible() ? _( "Yes" ) : _( "No" ) );
  172. aList.emplace_back( _( "Length" ), aFrame->MessageTextFromValue( GetLength() ), true );
  173. int i = PinOrientationIndex( GetOrientation() );
  174. aList.emplace_back( _( "Orientation" ), PinOrientationName( (unsigned) i ) );
  175. SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( aFrame );
  176. SCH_SHEET_PATH* currentSheet = schframe ? &schframe->GetCurrentSheet() : nullptr;
  177. SCH_SYMBOL* symbol = GetParentSymbol();
  178. // Don't use GetShownText(); we want to see the variable references here
  179. aList.emplace_back( symbol->GetRef( currentSheet ),
  180. UnescapeString( symbol->GetField( VALUE_FIELD )->GetText() ) );
  181. #if defined(DEBUG)
  182. if( !IsConnectivityDirty() && dynamic_cast<SCH_EDIT_FRAME*>( aFrame ) )
  183. {
  184. SCH_CONNECTION* conn = Connection();
  185. if( conn )
  186. conn->AppendInfoToMsgPanel( aList );
  187. }
  188. #endif
  189. }
  190. bool SCH_PIN::IsStacked( const SCH_PIN* aPin ) const
  191. {
  192. return m_parent == aPin->GetParent()
  193. && GetTransformedPosition() == aPin->GetTransformedPosition()
  194. && GetName() == aPin->GetName()
  195. && ( ( GetType() == aPin->GetType() ) || ( GetType() == ELECTRICAL_PINTYPE::PT_PASSIVE )
  196. || ( aPin->GetType() == ELECTRICAL_PINTYPE::PT_PASSIVE ) );
  197. }
  198. void SCH_PIN::ClearDefaultNetName( const SCH_SHEET_PATH* aPath )
  199. {
  200. std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
  201. if( aPath )
  202. m_net_name_map.erase( *aPath );
  203. else
  204. m_net_name_map.clear();
  205. }
  206. wxString SCH_PIN::GetDefaultNetName( const SCH_SHEET_PATH& aPath, bool aForceNoConnect )
  207. {
  208. // Need to check for parent as power symbol to make sure we aren't dealing
  209. // with legacy global power pins on non-power symbols
  210. if( IsGlobalPower() )
  211. {
  212. if( GetLibPin()->GetParent()->IsPower() )
  213. {
  214. return EscapeString( GetParentSymbol()->GetValueFieldText( true, &aPath, false ),
  215. CTX_NETNAME );
  216. }
  217. else
  218. {
  219. return EscapeString( m_libPin->GetName(), CTX_NETNAME );
  220. }
  221. }
  222. std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
  223. auto it = m_net_name_map.find( aPath );
  224. if( it != m_net_name_map.end() )
  225. {
  226. if( it->second.second == aForceNoConnect )
  227. return it->second.first;
  228. }
  229. wxString name = "Net-(";
  230. bool unconnected = false;
  231. if( aForceNoConnect || GetType() == ELECTRICAL_PINTYPE::PT_NC )
  232. {
  233. unconnected = true;
  234. name = ( "unconnected-(" );
  235. }
  236. bool annotated = true;
  237. std::vector<SCH_PIN*> pins = GetParentSymbol()->GetPins( &aPath );
  238. bool has_multiple = false;
  239. for( SCH_PIN* pin : pins )
  240. {
  241. if( pin->GetShownName() == GetShownName()
  242. && pin->GetShownNumber() != GetShownNumber()
  243. && unconnected == ( pin->GetType() == ELECTRICAL_PINTYPE::PT_NC ) )
  244. {
  245. has_multiple = true;
  246. break;
  247. }
  248. }
  249. // Use timestamp for unannotated symbols
  250. if( GetParentSymbol()->GetRef( &aPath, false ).Last() == '?' )
  251. {
  252. name << GetParentSymbol()->m_Uuid.AsString();
  253. name << "-Pad" << m_libPin->GetNumber() << ")";
  254. annotated = false;
  255. }
  256. else if( !m_libPin->GetShownName().IsEmpty()
  257. && m_libPin->GetShownName() != m_libPin->GetShownNumber() )
  258. {
  259. // Pin names might not be unique between different units so we must have the
  260. // unit token in the reference designator
  261. name << GetParentSymbol()->GetRef( &aPath, true );
  262. name << "-" << EscapeString( m_libPin->GetShownName(), CTX_NETNAME );
  263. if( unconnected || has_multiple )
  264. name << "-Pad" << EscapeString( m_libPin->GetShownNumber(), CTX_NETNAME );
  265. name << ")";
  266. }
  267. else
  268. {
  269. // Pin numbers are unique, so we skip the unit token
  270. name << GetParentSymbol()->GetRef( &aPath, false );
  271. name << "-Pad" << EscapeString( m_libPin->GetShownNumber(), CTX_NETNAME ) << ")";
  272. }
  273. if( annotated )
  274. m_net_name_map[ aPath ] = std::make_pair( name, aForceNoConnect );
  275. return name;
  276. }
  277. VECTOR2I SCH_PIN::GetTransformedPosition() const
  278. {
  279. TRANSFORM t = GetParentSymbol()->GetTransform();
  280. return t.TransformCoordinate( GetLocalPosition() ) + GetParentSymbol()->GetPosition();
  281. }
  282. const BOX2I SCH_PIN::GetBoundingBox( bool aIncludeInvisiblePins, bool aIncludeNameAndNumber,
  283. bool aIncludeElectricalType ) const
  284. {
  285. TRANSFORM t = GetParentSymbol()->GetTransform();
  286. BOX2I r = m_libPin->GetBoundingBox( aIncludeInvisiblePins, aIncludeNameAndNumber,
  287. aIncludeElectricalType );
  288. r.RevertYAxis();
  289. r = t.TransformCoordinate( r );
  290. r.Offset( GetParentSymbol()->GetPosition() );
  291. return r;
  292. }
  293. bool SCH_PIN::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  294. {
  295. // When looking for an "exact" hit aAccuracy will be 0 which works poorly if the pin has
  296. // no pin number or name. Give it a floor.
  297. if( Schematic() )
  298. aAccuracy = std::max( aAccuracy, Schematic()->Settings().m_PinSymbolSize / 4 );
  299. BOX2I rect = GetBoundingBox( false, true, m_flags & SHOW_ELEC_TYPE );
  300. return rect.Inflate( aAccuracy ).Contains( aPosition );
  301. }
  302. bool SCH_PIN::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  303. {
  304. BOX2I sel = aRect;
  305. if( aAccuracy )
  306. sel.Inflate( aAccuracy );
  307. if( aContained )
  308. return sel.Contains( GetBoundingBox( false, false, false ) );
  309. return sel.Intersects( GetBoundingBox( false, true, m_flags & SHOW_ELEC_TYPE ) );
  310. }
  311. EDA_ITEM* SCH_PIN::Clone() const
  312. {
  313. return new SCH_PIN( *this );
  314. }
  315. bool SCH_PIN::ConnectionPropagatesTo( const EDA_ITEM* aItem ) const
  316. {
  317. // Reciprocal checking is done in CONNECTION_GRAPH anyway
  318. return !( m_libPin->GetType() == ELECTRICAL_PINTYPE::PT_NC );
  319. }