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.

473 lines
13 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. PIN_ORIENTATION 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. aList.emplace_back( _( "Orientation" ), PinOrientationName( GetOrientation() ) );
  174. SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( aFrame );
  175. SCH_SHEET_PATH* currentSheet = schframe ? &schframe->GetCurrentSheet() : nullptr;
  176. SCH_SYMBOL* symbol = GetParentSymbol();
  177. // Don't use GetShownText(); we want to see the variable references here
  178. aList.emplace_back( symbol->GetRef( currentSheet ),
  179. UnescapeString( symbol->GetField( VALUE_FIELD )->GetText() ) );
  180. #if defined(DEBUG)
  181. if( !IsConnectivityDirty() && dynamic_cast<SCH_EDIT_FRAME*>( aFrame ) )
  182. {
  183. SCH_CONNECTION* conn = Connection();
  184. if( conn )
  185. conn->AppendInfoToMsgPanel( aList );
  186. }
  187. #endif
  188. }
  189. bool SCH_PIN::IsStacked( const SCH_PIN* aPin ) const
  190. {
  191. return m_parent == aPin->GetParent()
  192. && GetTransformedPosition() == aPin->GetTransformedPosition()
  193. && GetName() == aPin->GetName()
  194. && ( ( GetType() == aPin->GetType() ) || ( GetType() == ELECTRICAL_PINTYPE::PT_PASSIVE )
  195. || ( aPin->GetType() == ELECTRICAL_PINTYPE::PT_PASSIVE ) );
  196. }
  197. void SCH_PIN::ClearDefaultNetName( const SCH_SHEET_PATH* aPath )
  198. {
  199. std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
  200. if( aPath )
  201. m_net_name_map.erase( *aPath );
  202. else
  203. m_net_name_map.clear();
  204. }
  205. wxString SCH_PIN::GetDefaultNetName( const SCH_SHEET_PATH& aPath, bool aForceNoConnect )
  206. {
  207. // Need to check for parent as power symbol to make sure we aren't dealing
  208. // with legacy global power pins on non-power symbols
  209. if( IsGlobalPower() )
  210. {
  211. if( GetLibPin()->GetParent()->IsPower() )
  212. {
  213. return EscapeString( GetParentSymbol()->GetValueFieldText( true, &aPath, false ),
  214. CTX_NETNAME );
  215. }
  216. else
  217. {
  218. return EscapeString( m_libPin->GetName(), CTX_NETNAME );
  219. }
  220. }
  221. std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
  222. auto it = m_net_name_map.find( aPath );
  223. if( it != m_net_name_map.end() )
  224. {
  225. if( it->second.second == aForceNoConnect )
  226. return it->second.first;
  227. }
  228. wxString name = "Net-(";
  229. bool unconnected = false;
  230. if( aForceNoConnect || GetType() == ELECTRICAL_PINTYPE::PT_NC )
  231. {
  232. unconnected = true;
  233. name = ( "unconnected-(" );
  234. }
  235. bool annotated = true;
  236. std::vector<SCH_PIN*> pins = GetParentSymbol()->GetPins( &aPath );
  237. bool has_multiple = false;
  238. for( SCH_PIN* pin : pins )
  239. {
  240. if( pin->GetShownName() == GetShownName()
  241. && pin->GetShownNumber() != GetShownNumber()
  242. && unconnected == ( pin->GetType() == ELECTRICAL_PINTYPE::PT_NC ) )
  243. {
  244. has_multiple = true;
  245. break;
  246. }
  247. }
  248. // Use timestamp for unannotated symbols
  249. if( GetParentSymbol()->GetRef( &aPath, false ).Last() == '?' )
  250. {
  251. name << GetParentSymbol()->m_Uuid.AsString();
  252. name << "-Pad" << m_libPin->GetNumber() << ")";
  253. annotated = false;
  254. }
  255. else if( !m_libPin->GetShownName().IsEmpty()
  256. && m_libPin->GetShownName() != m_libPin->GetShownNumber() )
  257. {
  258. // Pin names might not be unique between different units so we must have the
  259. // unit token in the reference designator
  260. name << GetParentSymbol()->GetRef( &aPath, true );
  261. name << "-" << EscapeString( m_libPin->GetShownName(), CTX_NETNAME );
  262. if( unconnected || has_multiple )
  263. name << "-Pad" << EscapeString( m_libPin->GetShownNumber(), CTX_NETNAME );
  264. name << ")";
  265. }
  266. else
  267. {
  268. // Pin numbers are unique, so we skip the unit token
  269. name << GetParentSymbol()->GetRef( &aPath, false );
  270. name << "-Pad" << EscapeString( m_libPin->GetShownNumber(), CTX_NETNAME ) << ")";
  271. }
  272. if( annotated )
  273. m_net_name_map[ aPath ] = std::make_pair( name, aForceNoConnect );
  274. return name;
  275. }
  276. VECTOR2I SCH_PIN::GetTransformedPosition() const
  277. {
  278. TRANSFORM t = GetParentSymbol()->GetTransform();
  279. return t.TransformCoordinate( GetLocalPosition() ) + GetParentSymbol()->GetPosition();
  280. }
  281. const BOX2I SCH_PIN::GetBoundingBox( bool aIncludeInvisiblePins, bool aIncludeNameAndNumber,
  282. bool aIncludeElectricalType ) const
  283. {
  284. TRANSFORM t = GetParentSymbol()->GetTransform();
  285. BOX2I r = m_libPin->GetBoundingBox( aIncludeInvisiblePins, aIncludeNameAndNumber,
  286. aIncludeElectricalType );
  287. r.RevertYAxis();
  288. r = t.TransformCoordinate( r );
  289. r.Offset( GetParentSymbol()->GetPosition() );
  290. return r;
  291. }
  292. bool SCH_PIN::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  293. {
  294. // When looking for an "exact" hit aAccuracy will be 0 which works poorly if the pin has
  295. // no pin number or name. Give it a floor.
  296. if( Schematic() )
  297. aAccuracy = std::max( aAccuracy, Schematic()->Settings().m_PinSymbolSize / 4 );
  298. BOX2I rect = GetBoundingBox( false, true, m_flags & SHOW_ELEC_TYPE );
  299. return rect.Inflate( aAccuracy ).Contains( aPosition );
  300. }
  301. bool SCH_PIN::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  302. {
  303. BOX2I sel = aRect;
  304. if( aAccuracy )
  305. sel.Inflate( aAccuracy );
  306. if( aContained )
  307. return sel.Contains( GetBoundingBox( false, false, false ) );
  308. return sel.Intersects( GetBoundingBox( false, true, m_flags & SHOW_ELEC_TYPE ) );
  309. }
  310. EDA_ITEM* SCH_PIN::Clone() const
  311. {
  312. return new SCH_PIN( *this );
  313. }
  314. bool SCH_PIN::ConnectionPropagatesTo( const EDA_ITEM* aItem ) const
  315. {
  316. // Reciprocal checking is done in CONNECTION_GRAPH anyway
  317. return !( m_libPin->GetType() == ELECTRICAL_PINTYPE::PT_NC );
  318. }
  319. bool SCH_PIN::operator==( const SCH_ITEM& aOther ) const
  320. {
  321. if( aOther.Type() != SCH_PIN_T )
  322. return false;
  323. const SCH_PIN& other = static_cast<const SCH_PIN&>( aOther );
  324. if( m_number != other.m_number )
  325. return false;
  326. if( m_position != other.m_position )
  327. return false;
  328. return m_libPin == other.m_libPin;
  329. }
  330. double SCH_PIN::Similarity( const SCH_ITEM& aOther ) const
  331. {
  332. if( m_Uuid == aOther.m_Uuid )
  333. return 1.0;
  334. if( aOther.Type() != SCH_PIN_T )
  335. return 0.0;
  336. const SCH_PIN& other = static_cast<const SCH_PIN&>( aOther );
  337. if( m_number != other.m_number )
  338. return 0.0;
  339. if( m_position != other.m_position )
  340. return 0.0;
  341. return m_libPin->Similarity( *other.m_libPin );
  342. }
  343. static struct SCH_PIN_DESC
  344. {
  345. SCH_PIN_DESC()
  346. {
  347. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  348. REGISTER_TYPE( SCH_PIN );
  349. propMgr.InheritsAfter( TYPE_HASH( SCH_PIN ), TYPE_HASH( SCH_ITEM ) );
  350. propMgr.AddProperty( new PROPERTY<SCH_PIN, wxString>( _HKI( "Pin Name" ),
  351. NO_SETTER( SCH_PIN, wxString ), &SCH_PIN::GetName ) );
  352. propMgr.AddProperty( new PROPERTY<SCH_PIN, wxString>( _HKI( "Pin Number" ),
  353. NO_SETTER( SCH_PIN, wxString ), &SCH_PIN::GetNumber ) );
  354. propMgr.AddProperty( new PROPERTY<SCH_PIN, int>( _HKI( "Length" ),
  355. NO_SETTER( SCH_PIN, int ), &SCH_PIN::GetLength, PROPERTY_DISPLAY::PT_SIZE ) );
  356. }
  357. } _SCH_PIN_DESC;