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.

1311 lines
35 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004-2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
  6. * Copyright (C) 2004-2020 KiCad Developers, see AUTHORS.txt for contributors.
  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
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <fctsys.h>
  26. #include <macros.h>
  27. #include <kicad_string.h>
  28. #include <sch_draw_panel.h>
  29. #include <plotter.h>
  30. #include <gr_basic.h>
  31. #include <sch_screen.h>
  32. #include <richio.h>
  33. #include <trace_helpers.h>
  34. #include <general.h>
  35. #include <template_fieldnames.h>
  36. #include <transform.h>
  37. #include <class_library.h>
  38. #include <class_libentry.h>
  39. #include <lib_pin.h>
  40. #include <lib_arc.h>
  41. #include <settings/color_settings.h>
  42. // the separator char between the subpart id and the reference
  43. // 0 (no separator) or '.' or some other character
  44. int LIB_PART::m_subpartIdSeparator = 0;
  45. // the ascii char value to calculate the subpart symbol id from the part number:
  46. // 'A' or '1' usually. (to print U1.A or U1.1)
  47. // if this a a digit, a number is used as id symbol
  48. int LIB_PART::m_subpartFirstId = 'A';
  49. wxString LIB_PART::GetSearchText()
  50. {
  51. // Matches are scored by offset from front of string, so inclusion of this spacer
  52. // discounts matches found after it.
  53. static const wxString discount( wxT( " " ) );
  54. wxString text = GetKeyWords() + discount + GetDescription();
  55. wxString footprint = GetFootprintField().GetText();
  56. if( !footprint.IsEmpty() )
  57. {
  58. text += discount + footprint;
  59. }
  60. return text;
  61. }
  62. bool operator<( const LIB_PART& aItem1, const LIB_PART& aItem2 )
  63. {
  64. return aItem1.GetName() < aItem2.GetName();
  65. }
  66. /// http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/sp_techniques.html#weak_without_shared
  67. struct null_deleter
  68. {
  69. void operator()(void const *) const
  70. {
  71. }
  72. };
  73. LIB_PART::LIB_PART( const wxString& aName, LIB_PART* aParent, PART_LIB* aLibrary ) :
  74. EDA_ITEM( LIB_PART_T ),
  75. m_me( this, null_deleter() )
  76. {
  77. m_dateLastEdition = 0;
  78. m_unitCount = 1;
  79. m_pinNameOffset = Mils2iu( DEFAULT_PIN_NAME_OFFSET );
  80. m_options = ENTRY_NORMAL;
  81. m_unitsLocked = false;
  82. m_showPinNumbers = true;
  83. m_showPinNames = true;
  84. // Add the MANDATORY_FIELDS in RAM only. These are assumed to be present
  85. // when the field editors are invoked.
  86. m_drawings[LIB_FIELD_T].reserve( 4 );
  87. m_drawings[LIB_FIELD_T].push_back( new LIB_FIELD( this, VALUE ) );
  88. m_drawings[LIB_FIELD_T].push_back( new LIB_FIELD( this, REFERENCE ) );
  89. m_drawings[LIB_FIELD_T].push_back( new LIB_FIELD( this, FOOTPRINT ) );
  90. m_drawings[LIB_FIELD_T].push_back( new LIB_FIELD( this, DATASHEET ) );
  91. SetName( aName );
  92. if( aParent )
  93. SetParent( aParent );
  94. SetLib( aLibrary );
  95. }
  96. LIB_PART::LIB_PART( const LIB_PART& aPart, PART_LIB* aLibrary ) :
  97. EDA_ITEM( aPart ),
  98. m_me( this, null_deleter() )
  99. {
  100. LIB_ITEM* newItem;
  101. m_library = aLibrary;
  102. m_name = aPart.m_name;
  103. m_FootprintList = wxArrayString( aPart.m_FootprintList );
  104. m_unitCount = aPart.m_unitCount;
  105. m_unitsLocked = aPart.m_unitsLocked;
  106. m_pinNameOffset = aPart.m_pinNameOffset;
  107. m_showPinNumbers = aPart.m_showPinNumbers;
  108. m_showPinNames = aPart.m_showPinNames;
  109. m_dateLastEdition = aPart.m_dateLastEdition;
  110. m_options = aPart.m_options;
  111. m_libId = aPart.m_libId;
  112. m_description = aPart.m_description;
  113. m_keyWords = aPart.m_keyWords;
  114. ClearSelected();
  115. for( const LIB_ITEM& oldItem : aPart.m_drawings )
  116. {
  117. if( ( oldItem.GetFlags() & ( IS_NEW | STRUCT_DELETED ) ) != 0 )
  118. continue;
  119. try
  120. {
  121. newItem = (LIB_ITEM*) oldItem.Clone();
  122. newItem->ClearSelected();
  123. newItem->SetParent( this );
  124. m_drawings.push_back( newItem );
  125. }
  126. catch( ... )
  127. {
  128. wxFAIL_MSG( "Failed to clone LIB_ITEM." );
  129. }
  130. }
  131. PART_SPTR parent = aPart.m_parent.lock();
  132. if( parent )
  133. SetParent( parent.get() );
  134. }
  135. LIB_PART::~LIB_PART()
  136. {
  137. }
  138. const LIB_PART& LIB_PART::operator=( const LIB_PART& aPart )
  139. {
  140. if( &aPart == this )
  141. return aPart;
  142. LIB_ITEM* newItem;
  143. m_library = aPart.m_library;
  144. m_name = aPart.m_name;
  145. m_FootprintList = wxArrayString( aPart.m_FootprintList );
  146. m_unitCount = aPart.m_unitCount;
  147. m_unitsLocked = aPart.m_unitsLocked;
  148. m_pinNameOffset = aPart.m_pinNameOffset;
  149. m_showPinNumbers = aPart.m_showPinNumbers;
  150. m_showPinNames = aPart.m_showPinNames;
  151. m_dateLastEdition = aPart.m_dateLastEdition;
  152. m_options = aPart.m_options;
  153. m_libId = aPart.m_libId;
  154. m_description = aPart.m_description;
  155. m_keyWords = aPart.m_keyWords;
  156. m_drawings.clear();
  157. for( const LIB_ITEM& oldItem : aPart.m_drawings )
  158. {
  159. if( ( oldItem.GetFlags() & ( IS_NEW | STRUCT_DELETED ) ) != 0 )
  160. continue;
  161. newItem = (LIB_ITEM*) oldItem.Clone();
  162. newItem->SetParent( this );
  163. m_drawings.push_back( newItem );
  164. }
  165. PART_SPTR parent = aPart.m_parent.lock();
  166. if( parent )
  167. SetParent( parent.get() );
  168. return *this;
  169. }
  170. int LIB_PART::Compare( const LIB_PART& aRhs ) const
  171. {
  172. if( m_me == aRhs.m_me )
  173. return 0;
  174. int retv = m_name.Cmp( aRhs.m_name );
  175. if( retv )
  176. return retv;
  177. retv = m_libId.compare( aRhs.m_libId );
  178. if( retv )
  179. return retv;
  180. if( m_parent.lock() < aRhs.m_parent.lock() )
  181. return -1;
  182. if( m_parent.lock() > aRhs.m_parent.lock() )
  183. return 1;
  184. if( m_options != aRhs.m_options )
  185. return ( m_options == ENTRY_NORMAL ) ? -1 : 1;
  186. if( m_unitCount != aRhs.m_unitCount )
  187. return m_unitCount - aRhs.m_unitCount;
  188. if( m_drawings.size() != aRhs.m_drawings.size() )
  189. return m_drawings.size() - aRhs.m_drawings.size();
  190. LIB_ITEMS_CONTAINER::CONST_ITERATOR lhsItem = m_drawings.begin();
  191. LIB_ITEMS_CONTAINER::CONST_ITERATOR rhsItem = aRhs.m_drawings.begin();
  192. while( lhsItem != m_drawings.end() )
  193. {
  194. if( lhsItem->Type() != rhsItem->Type() )
  195. return lhsItem->Type() - rhsItem->Type();
  196. retv = lhsItem->compare( *rhsItem );
  197. if( retv )
  198. return retv;
  199. ++lhsItem;
  200. ++rhsItem;
  201. }
  202. if( m_FootprintList.GetCount() != aRhs.m_FootprintList.GetCount() )
  203. return m_FootprintList.GetCount() - aRhs.m_FootprintList.GetCount();
  204. for( size_t i = 0; i < m_FootprintList.GetCount(); i++ )
  205. {
  206. retv = m_FootprintList[i].Cmp( aRhs.m_FootprintList[i] );
  207. if( retv )
  208. return retv;
  209. }
  210. retv = m_description.Cmp( aRhs.m_description );
  211. if( retv )
  212. return retv;
  213. retv = m_keyWords.Cmp( aRhs.m_keyWords );
  214. if( retv )
  215. return retv;
  216. if( m_pinNameOffset != aRhs.m_pinNameOffset )
  217. return m_pinNameOffset - aRhs.m_pinNameOffset;
  218. if( m_unitsLocked != aRhs.m_unitsLocked )
  219. return ( m_unitsLocked ) ? 1 : -1;
  220. if( m_showPinNames != aRhs.m_showPinNames )
  221. return ( m_showPinNames ) ? 1 : -1;
  222. if( m_showPinNumbers != aRhs.m_showPinNumbers )
  223. return ( m_showPinNumbers ) ? 1 : -1;
  224. return 0;
  225. }
  226. wxString LIB_PART::GetUnitReference( int aUnit )
  227. {
  228. return LIB_PART::SubReference( aUnit, false );
  229. }
  230. void LIB_PART::SetName( const wxString& aName )
  231. {
  232. wxString validatedName = LIB_ID::FixIllegalChars( aName, LIB_ID::ID_SCH );
  233. m_name = validatedName;
  234. m_libId.SetLibItemName( validatedName, false );
  235. GetValueField().SetText( validatedName );
  236. }
  237. void LIB_PART::SetParent( LIB_PART* aParent )
  238. {
  239. if( aParent )
  240. m_parent = aParent->SharedPtr();
  241. else
  242. m_parent.reset();
  243. }
  244. std::unique_ptr< LIB_PART > LIB_PART::Flatten() const
  245. {
  246. std::unique_ptr< LIB_PART > retv;
  247. if( IsAlias() )
  248. {
  249. PART_SPTR parent = m_parent.lock();
  250. wxCHECK_MSG( parent, retv,
  251. wxString::Format( "Parent of derived symbol '%s' undefined", m_name ) );
  252. // Copy the parent.
  253. retv.reset( new LIB_PART( *parent.get() ) );
  254. retv->SetName( m_name );
  255. // Now add the inherited part mandatory field (this) information.
  256. for( int i = 0; i < MANDATORY_FIELDS; i++ )
  257. {
  258. wxString tmp = GetField( i )->GetText();
  259. // If the field isn't defined then inherit the parent field value.
  260. if( tmp.IsEmpty() )
  261. retv->GetField( i )->SetText( parent->GetField( i )->GetText() );
  262. else
  263. *retv->GetField( i ) = *GetField( i );
  264. }
  265. // Grab all the rest of derived symbol fields.
  266. for( const LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  267. {
  268. const LIB_FIELD* aliasField = dynamic_cast<const LIB_FIELD*>( &item );
  269. wxCHECK2( aliasField, continue );
  270. // Mandatory fields were already resolved.
  271. if( aliasField->IsMandatory() )
  272. continue;
  273. LIB_FIELD* newField = new LIB_FIELD( *aliasField );
  274. newField->SetParent( retv.get() );
  275. LIB_FIELD* parentField = retv->FindField( aliasField->GetName() );
  276. if( !parentField ) // Derived symbol field does not exist in parent symbol.
  277. {
  278. retv->AddDrawItem( newField );
  279. }
  280. else // Derived symbol field overrides the parent symbol field.
  281. {
  282. retv->RemoveDrawItem( parentField );
  283. retv->AddDrawItem( newField );
  284. }
  285. }
  286. retv->SetKeyWords( m_keyWords );
  287. retv->SetDescription( m_description );
  288. }
  289. else
  290. {
  291. retv.reset( new LIB_PART( *this ) );
  292. }
  293. return retv;
  294. }
  295. const wxString LIB_PART::GetLibraryName() const
  296. {
  297. if( m_library )
  298. return m_library->GetName();
  299. return m_libId.GetLibNickname();
  300. }
  301. wxString LIB_PART::SubReference( int aUnit, bool aAddSeparator )
  302. {
  303. wxString subRef;
  304. if( m_subpartIdSeparator != 0 && aAddSeparator )
  305. subRef << wxChar( m_subpartIdSeparator );
  306. if( m_subpartFirstId >= '0' && m_subpartFirstId <= '9' )
  307. subRef << aUnit;
  308. else
  309. {
  310. // use letters as notation. To allow more than 26 units, the sub ref
  311. // use one letter if letter = A .. Z or a ... z, and 2 letters otherwise
  312. // first letter is expected to be 'A' or 'a' (i.e. 26 letters are available)
  313. int u;
  314. aUnit -= 1; // Unit number starts to 1. now to 0.
  315. while( aUnit >= 26 ) // more than one letter are needed
  316. {
  317. u = aUnit / 26;
  318. subRef << wxChar( m_subpartFirstId + u -1 );
  319. aUnit %= 26;
  320. }
  321. u = m_subpartFirstId + aUnit;
  322. subRef << wxChar( u );
  323. }
  324. return subRef;
  325. }
  326. void LIB_PART::Print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, int aMulti,
  327. int aConvert, const PART_DRAW_OPTIONS& aOpts )
  328. {
  329. /* draw background for filled items using background option
  330. * Solid lines will be drawn after the background
  331. * Note also, background is not drawn when printing in black and white
  332. */
  333. if( !GetGRForceBlackPenState() )
  334. {
  335. for( LIB_ITEM& drawItem : m_drawings )
  336. {
  337. if( drawItem.m_Fill != FILLED_WITH_BG_BODYCOLOR )
  338. continue;
  339. // Do not draw items not attached to the current part
  340. if( aMulti && drawItem.m_Unit && ( drawItem.m_Unit != aMulti ) )
  341. continue;
  342. if( aConvert && drawItem.m_Convert && ( drawItem.m_Convert != aConvert ) )
  343. continue;
  344. if( drawItem.Type() == LIB_FIELD_T )
  345. continue;
  346. // Now, draw only the background for items with
  347. // m_Fill == FILLED_WITH_BG_BODYCOLOR:
  348. drawItem.Print( aSettings, aOffset, (void*) false, aOpts.transform );
  349. }
  350. }
  351. for( LIB_ITEM& drawItem : m_drawings )
  352. {
  353. // Do not draw items not attached to the current part
  354. if( aMulti && drawItem.m_Unit && ( drawItem.m_Unit != aMulti ) )
  355. continue;
  356. if( aConvert && drawItem.m_Convert && ( drawItem.m_Convert != aConvert ) )
  357. continue;
  358. if( drawItem.Type() == LIB_FIELD_T )
  359. {
  360. LIB_FIELD& field = static_cast<LIB_FIELD&>( drawItem );
  361. if( field.IsVisible() && !aOpts.draw_visible_fields )
  362. continue;
  363. if( !field.IsVisible() && !aOpts.draw_hidden_fields )
  364. continue;
  365. }
  366. if( drawItem.Type() == LIB_PIN_T )
  367. {
  368. drawItem.Print( aSettings, aOffset, (void*) &aOpts, aOpts.transform );
  369. }
  370. else if( drawItem.Type() == LIB_FIELD_T )
  371. {
  372. drawItem.Print( aSettings, aOffset, (void*) NULL, aOpts.transform );
  373. }
  374. else
  375. {
  376. bool forceNoFill = drawItem.m_Fill == FILLED_WITH_BG_BODYCOLOR;
  377. drawItem.Print( aSettings, aOffset, (void*) forceNoFill, aOpts.transform );
  378. }
  379. }
  380. }
  381. void LIB_PART::Plot( PLOTTER* aPlotter, int aUnit, int aConvert,
  382. const wxPoint& aOffset, const TRANSFORM& aTransform )
  383. {
  384. wxASSERT( aPlotter != NULL );
  385. aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE ) );
  386. bool fill = aPlotter->GetColorMode();
  387. // draw background for filled items using background option
  388. // Solid lines will be drawn after the background
  389. for( LIB_ITEM& item : m_drawings )
  390. {
  391. // Lib Fields are not plotted here, because this plot function
  392. // is used to plot schematic items, which have they own fields
  393. if( item.Type() == LIB_FIELD_T )
  394. continue;
  395. if( aUnit && item.m_Unit && ( item.m_Unit != aUnit ) )
  396. continue;
  397. if( aConvert && item.m_Convert && ( item.m_Convert != aConvert ) )
  398. continue;
  399. if( item.m_Fill == FILLED_WITH_BG_BODYCOLOR )
  400. item.Plot( aPlotter, aOffset, fill, aTransform );
  401. }
  402. // Not filled items and filled shapes are now plotted
  403. // Items that have BG fills only get re-stroked to ensure the edges are in the foreground
  404. for( LIB_ITEM& item : m_drawings )
  405. {
  406. if( item.Type() == LIB_FIELD_T )
  407. continue;
  408. if( aUnit && item.m_Unit && ( item.m_Unit != aUnit ) )
  409. continue;
  410. if( aConvert && item.m_Convert && ( item.m_Convert != aConvert ) )
  411. continue;
  412. item.Plot( aPlotter, aOffset, fill && ( item.m_Fill != FILLED_WITH_BG_BODYCOLOR ),
  413. aTransform );
  414. }
  415. }
  416. void LIB_PART::PlotLibFields( PLOTTER* aPlotter, int aUnit, int aConvert,
  417. const wxPoint& aOffset, const TRANSFORM& aTransform )
  418. {
  419. wxASSERT( aPlotter != NULL );
  420. aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_FIELDS ) );
  421. bool fill = aPlotter->GetColorMode();
  422. for( LIB_ITEM& item : m_drawings )
  423. {
  424. if( item.Type() != LIB_FIELD_T )
  425. continue;
  426. if( aUnit && item.m_Unit && ( item.m_Unit != aUnit ) )
  427. continue;
  428. if( aConvert && item.m_Convert && ( item.m_Convert != aConvert ) )
  429. continue;
  430. LIB_FIELD& field = (LIB_FIELD&) item;
  431. // The reference is a special case: we should change the basic text
  432. // to add '?' and the part id
  433. wxString tmp = field.GetShownText();
  434. if( field.GetId() == REFERENCE )
  435. {
  436. wxString text = field.GetFullText( aUnit );
  437. field.SetText( text );
  438. }
  439. item.Plot( aPlotter, aOffset, fill, aTransform );
  440. field.SetText( tmp );
  441. }
  442. }
  443. void LIB_PART::RemoveDrawItem( LIB_ITEM* aItem )
  444. {
  445. wxASSERT( aItem != NULL );
  446. // none of the MANDATORY_FIELDS may be removed in RAM, but they may be
  447. // omitted when saving to disk.
  448. if( aItem->Type() == LIB_FIELD_T )
  449. {
  450. if( static_cast<LIB_FIELD*>( aItem )->IsMandatory() )
  451. return;
  452. }
  453. LIB_ITEMS& items = m_drawings[ aItem->Type() ];
  454. for( LIB_ITEMS::iterator i = items.begin(); i != items.end(); i++ )
  455. {
  456. if( *i == aItem )
  457. {
  458. items.erase( i );
  459. SetModified();
  460. break;
  461. }
  462. }
  463. }
  464. void LIB_PART::AddDrawItem( LIB_ITEM* aItem )
  465. {
  466. if( !aItem )
  467. return;
  468. m_drawings.push_back( aItem );
  469. }
  470. LIB_ITEM* LIB_PART::GetNextDrawItem( LIB_ITEM* aItem, KICAD_T aType )
  471. {
  472. if( aItem == NULL )
  473. {
  474. LIB_ITEMS_CONTAINER::ITERATOR it1 = m_drawings.begin( aType );
  475. return (it1 != m_drawings.end( aType ) ) ? &( *( m_drawings.begin( aType ) ) ) : nullptr;
  476. }
  477. // Search for the last item, assume aItem is of type aType
  478. wxASSERT( ( aType == TYPE_NOT_INIT ) || ( aType == aItem->Type() ) );
  479. LIB_ITEMS_CONTAINER::ITERATOR it = m_drawings.begin( aType );
  480. while( ( it != m_drawings.end( aType ) ) && ( aItem != &( *it ) ) )
  481. ++it;
  482. // Search the next item
  483. if( it != m_drawings.end( aType ) )
  484. {
  485. ++it;
  486. if( it != m_drawings.end( aType ) )
  487. return &( *it );
  488. }
  489. return NULL;
  490. }
  491. void LIB_PART::GetPins( LIB_PINS& aList, int aUnit, int aConvert )
  492. {
  493. /* Notes:
  494. * when aUnit == 0: no unit filtering
  495. * when aConvert == 0: no convert (shape selection) filtering
  496. * when .m_Unit == 0, the body item is common to units
  497. * when .m_Convert == 0, the body item is common to shapes
  498. */
  499. for( LIB_ITEM& item : m_drawings[ LIB_PIN_T ] )
  500. {
  501. // Unit filtering:
  502. if( aUnit && item.m_Unit && ( item.m_Unit != aUnit ) )
  503. continue;
  504. // Shape filtering:
  505. if( aConvert && item.m_Convert && ( item.m_Convert != aConvert ) )
  506. continue;
  507. aList.push_back( (LIB_PIN*) &item );
  508. }
  509. }
  510. LIB_PIN* LIB_PART::GetPin( const wxString& aNumber, int aUnit, int aConvert )
  511. {
  512. LIB_PINS pinList;
  513. GetPins( pinList, aUnit, aConvert );
  514. for( size_t i = 0; i < pinList.size(); i++ )
  515. {
  516. wxASSERT( pinList[i]->Type() == LIB_PIN_T );
  517. if( aNumber == pinList[i]->GetNumber() )
  518. return pinList[i];
  519. }
  520. return NULL;
  521. }
  522. bool LIB_PART::PinsConflictWith( LIB_PART& aOtherPart, bool aTestNums, bool aTestNames,
  523. bool aTestType, bool aTestOrientation, bool aTestLength )
  524. {
  525. LIB_PINS thisPinList;
  526. GetPins( thisPinList, /* aUnit */ 0, /* aConvert */ 0 );
  527. for( LIB_PIN* eachThisPin : thisPinList )
  528. {
  529. wxASSERT( eachThisPin );
  530. LIB_PINS otherPinList;
  531. aOtherPart.GetPins( otherPinList, /* aUnit */ 0, /* aConvert */ 0 );
  532. bool foundMatch = false;
  533. for( LIB_PIN* eachOtherPin : otherPinList )
  534. {
  535. wxASSERT( eachOtherPin );
  536. // Same unit?
  537. if( eachThisPin->GetUnit() != eachOtherPin->GetUnit() )
  538. continue;
  539. // Same body stype?
  540. if( eachThisPin->GetConvert() != eachOtherPin->GetConvert() )
  541. continue;
  542. // Same position?
  543. if( eachThisPin->GetPosition() != eachOtherPin->GetPosition() )
  544. continue;
  545. // Same number?
  546. if( aTestNums && ( eachThisPin->GetNumber() != eachOtherPin->GetNumber() ) )
  547. continue;
  548. // Same name?
  549. if( aTestNames && ( eachThisPin->GetName() != eachOtherPin->GetName() ) )
  550. continue;
  551. // Same electrical type?
  552. if( aTestType && ( eachThisPin->GetType() != eachOtherPin->GetType() ) )
  553. continue;
  554. // Same orientation?
  555. if( aTestOrientation
  556. && ( eachThisPin->GetOrientation() != eachOtherPin->GetOrientation() ) )
  557. continue;
  558. // Same length?
  559. if( aTestLength && ( eachThisPin->GetLength() != eachOtherPin->GetLength() ) )
  560. continue;
  561. foundMatch = true;
  562. break; // Match found so seach is complete.
  563. }
  564. if( !foundMatch )
  565. {
  566. // This means there was not an identical (according to the arguments)
  567. // pin at the same position in the other component.
  568. return true;
  569. }
  570. }
  571. // The loop never gave up, so no conflicts were found.
  572. return false;
  573. }
  574. const EDA_RECT LIB_PART::GetUnitBoundingBox( int aUnit, int aConvert ) const
  575. {
  576. EDA_RECT bBox;
  577. bool initialized = false;
  578. for( const LIB_ITEM& item : m_drawings )
  579. {
  580. if( ( item.m_Unit > 0 ) && ( ( m_unitCount > 1 ) && ( aUnit > 0 )
  581. && ( aUnit != item.m_Unit ) ) )
  582. continue;
  583. if( item.m_Convert > 0 && ( ( aConvert > 0 ) && ( aConvert != item.m_Convert ) ) )
  584. continue;
  585. if ( ( item.Type() == LIB_FIELD_T ) && !( ( LIB_FIELD& ) item ).IsVisible() )
  586. continue;
  587. if( initialized )
  588. bBox.Merge( item.GetBoundingBox() );
  589. else
  590. {
  591. bBox = item.GetBoundingBox();
  592. initialized = true;
  593. }
  594. }
  595. return bBox;
  596. }
  597. void LIB_PART::ViewGetLayers( int aLayers[], int& aCount ) const
  598. {
  599. aCount = 6;
  600. aLayers[0] = LAYER_DEVICE;
  601. aLayers[1] = LAYER_DEVICE_BACKGROUND;
  602. aLayers[2] = LAYER_REFERENCEPART;
  603. aLayers[3] = LAYER_VALUEPART;
  604. aLayers[4] = LAYER_FIELDS;
  605. aLayers[5] = LAYER_SELECTION_SHADOWS;
  606. }
  607. const EDA_RECT LIB_PART::GetBodyBoundingBox( int aUnit, int aConvert ) const
  608. {
  609. EDA_RECT bbox;
  610. for( const LIB_ITEM& item : m_drawings )
  611. {
  612. if( ( item.m_Unit > 0 ) && ( ( m_unitCount > 1 ) && ( aUnit > 0 )
  613. && ( aUnit != item.m_Unit ) ) )
  614. continue;
  615. if( item.m_Convert > 0 && ( ( aConvert > 0 ) && ( aConvert != item.m_Convert ) ) )
  616. continue;
  617. if( item.Type() == LIB_FIELD_T )
  618. continue;
  619. bbox.Merge( item.GetBoundingBox() );
  620. }
  621. return bbox;
  622. }
  623. void LIB_PART::deleteAllFields()
  624. {
  625. m_drawings[ LIB_FIELD_T ].clear();
  626. }
  627. void LIB_PART::SetFields( const std::vector <LIB_FIELD>& aFields )
  628. {
  629. deleteAllFields();
  630. for( unsigned i=0; i<aFields.size(); ++i )
  631. {
  632. // drawings is a ptr_vector, new and copy an object on the heap.
  633. LIB_FIELD* field = new LIB_FIELD( aFields[i] );
  634. field->SetParent( this );
  635. m_drawings.push_back( field );
  636. }
  637. }
  638. void LIB_PART::GetFields( LIB_FIELDS& aList )
  639. {
  640. LIB_FIELD* field;
  641. // Grab the MANDATORY_FIELDS first, in expected order given by
  642. // enum NumFieldType
  643. for( int id=0; id<MANDATORY_FIELDS; ++id )
  644. {
  645. field = GetField( id );
  646. // the MANDATORY_FIELDS are exactly that in RAM.
  647. wxASSERT( field );
  648. aList.push_back( *field );
  649. }
  650. // Now grab all the rest of fields.
  651. for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  652. {
  653. field = ( LIB_FIELD* ) &item;
  654. if( field->IsMandatory() )
  655. continue; // was added above
  656. aList.push_back( *field );
  657. }
  658. }
  659. LIB_FIELD* LIB_PART::GetField( int aId ) const
  660. {
  661. for( const LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  662. {
  663. LIB_FIELD* field = ( LIB_FIELD* ) &item;
  664. if( field->GetId() == aId )
  665. return field;
  666. }
  667. return NULL;
  668. }
  669. LIB_FIELD* LIB_PART::FindField( const wxString& aFieldName )
  670. {
  671. for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  672. {
  673. if( static_cast<LIB_FIELD*>( &item )->GetCanonicalName() == aFieldName )
  674. return static_cast<LIB_FIELD*>( &item );
  675. }
  676. return NULL;
  677. }
  678. const LIB_FIELD* LIB_PART::FindField( const wxString& aFieldName ) const
  679. {
  680. for( const LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  681. {
  682. if( static_cast<const LIB_FIELD*>( &item )->GetCanonicalName() == aFieldName )
  683. return static_cast<const LIB_FIELD*>( &item );
  684. }
  685. return NULL;
  686. }
  687. LIB_FIELD& LIB_PART::GetValueField()
  688. {
  689. LIB_FIELD* field = GetField( VALUE );
  690. wxASSERT( field != NULL );
  691. return *field;
  692. }
  693. LIB_FIELD& LIB_PART::GetReferenceField()
  694. {
  695. LIB_FIELD* field = GetField( REFERENCE );
  696. wxASSERT( field != NULL );
  697. return *field;
  698. }
  699. LIB_FIELD& LIB_PART::GetFootprintField()
  700. {
  701. LIB_FIELD* field = GetField( FOOTPRINT );
  702. wxASSERT( field != NULL );
  703. return *field;
  704. }
  705. LIB_FIELD& LIB_PART::GetDatasheetField()
  706. {
  707. LIB_FIELD* field = GetField( DATASHEET );
  708. wxASSERT( field != NULL );
  709. return *field;
  710. }
  711. void LIB_PART::SetOffset( const wxPoint& aOffset )
  712. {
  713. for( LIB_ITEM& item : m_drawings )
  714. item.Offset( aOffset );
  715. }
  716. void LIB_PART::RemoveDuplicateDrawItems()
  717. {
  718. m_drawings.unique();
  719. }
  720. bool LIB_PART::HasConversion() const
  721. {
  722. for( const LIB_ITEM& item : m_drawings )
  723. {
  724. if( item.m_Convert > LIB_ITEM::LIB_CONVERT::BASE )
  725. return true;
  726. }
  727. if( PART_SPTR parent = m_parent.lock() )
  728. {
  729. for( const LIB_ITEM& item : parent->GetDrawItems() )
  730. {
  731. if( item.m_Convert > LIB_ITEM::LIB_CONVERT::BASE )
  732. return true;
  733. }
  734. }
  735. return false;
  736. }
  737. void LIB_PART::ClearTempFlags()
  738. {
  739. for( LIB_ITEM& item : m_drawings )
  740. item.ClearTempFlags();
  741. }
  742. void LIB_PART::ClearEditFlags()
  743. {
  744. for( LIB_ITEM& item : m_drawings )
  745. item.ClearEditFlags();
  746. }
  747. LIB_ITEM* LIB_PART::LocateDrawItem( int aUnit, int aConvert,
  748. KICAD_T aType, const wxPoint& aPoint )
  749. {
  750. for( LIB_ITEM& item : m_drawings )
  751. {
  752. if( ( aUnit && item.m_Unit && ( aUnit != item.m_Unit) )
  753. || ( aConvert && item.m_Convert && ( aConvert != item.m_Convert ) )
  754. || ( ( item.Type() != aType ) && ( aType != TYPE_NOT_INIT ) ) )
  755. continue;
  756. if( item.HitTest( aPoint ) )
  757. return &item;
  758. }
  759. return NULL;
  760. }
  761. LIB_ITEM* LIB_PART::LocateDrawItem( int aUnit, int aConvert, KICAD_T aType,
  762. const wxPoint& aPoint, const TRANSFORM& aTransform )
  763. {
  764. /* we use LocateDrawItem( int aUnit, int convert, KICAD_T type, const
  765. * wxPoint& pt ) to search items.
  766. * because this function uses DefaultTransform as orient/mirror matrix
  767. * we temporary copy aTransform in DefaultTransform
  768. */
  769. LIB_ITEM* item;
  770. TRANSFORM transform = DefaultTransform;
  771. DefaultTransform = aTransform;
  772. item = LocateDrawItem( aUnit, aConvert, aType, aPoint );
  773. // Restore matrix
  774. DefaultTransform = transform;
  775. return item;
  776. }
  777. SEARCH_RESULT LIB_PART::Visit( INSPECTOR aInspector, void* aTestData, const KICAD_T aFilterTypes[] )
  778. {
  779. // The part itself is never inspected, only its children
  780. for( LIB_ITEM& item : m_drawings )
  781. {
  782. if( item.IsType( aFilterTypes ) )
  783. {
  784. if( aInspector( &item, aTestData ) == SEARCH_RESULT::QUIT )
  785. return SEARCH_RESULT::QUIT;
  786. }
  787. }
  788. return SEARCH_RESULT::CONTINUE;
  789. }
  790. void LIB_PART::SetUnitCount( int aCount, bool aDuplicateDrawItems )
  791. {
  792. if( m_unitCount == aCount )
  793. return;
  794. if( aCount < m_unitCount )
  795. {
  796. LIB_ITEMS_CONTAINER::ITERATOR i = m_drawings.begin();
  797. while( i != m_drawings.end() )
  798. {
  799. if( i->m_Unit > aCount )
  800. i = m_drawings.erase( i );
  801. else
  802. ++i;
  803. }
  804. }
  805. else if( aDuplicateDrawItems )
  806. {
  807. int prevCount = m_unitCount;
  808. // Temporary storage for new items, as adding new items directly to
  809. // m_drawings may cause the buffer reallocation which invalidates the
  810. // iterators
  811. std::vector< LIB_ITEM* > tmp;
  812. for( LIB_ITEM& item : m_drawings )
  813. {
  814. if( item.m_Unit != 1 )
  815. continue;
  816. for( int j = prevCount + 1; j <= aCount; j++ )
  817. {
  818. LIB_ITEM* newItem = (LIB_ITEM*) item.Clone();
  819. newItem->m_Unit = j;
  820. tmp.push_back( newItem );
  821. }
  822. }
  823. for( auto item : tmp )
  824. m_drawings.push_back( item );
  825. }
  826. m_unitCount = aCount;
  827. }
  828. int LIB_PART::GetUnitCount() const
  829. {
  830. if( PART_SPTR parent = m_parent.lock() )
  831. return parent->GetUnitCount();
  832. return m_unitCount;
  833. }
  834. void LIB_PART::SetConversion( bool aSetConvert, bool aDuplicatePins )
  835. {
  836. if( aSetConvert == HasConversion() )
  837. return;
  838. // Duplicate items to create the converted shape
  839. if( aSetConvert )
  840. {
  841. if( aDuplicatePins )
  842. {
  843. std::vector< LIB_ITEM* > tmp; // Temporarily store the duplicated pins here.
  844. for( LIB_ITEM& item : m_drawings )
  845. {
  846. // Only pins are duplicated.
  847. if( item.Type() != LIB_PIN_T )
  848. continue;
  849. if( item.m_Convert == 1 )
  850. {
  851. LIB_ITEM* newItem = (LIB_ITEM*) item.Clone();
  852. newItem->m_Convert = 2;
  853. tmp.push_back( newItem );
  854. }
  855. }
  856. // Transfer the new pins to the LIB_PART.
  857. for( unsigned i = 0; i < tmp.size(); i++ )
  858. m_drawings.push_back( tmp[i] );
  859. }
  860. }
  861. else
  862. {
  863. // Delete converted shape items because the converted shape does
  864. // not exist
  865. LIB_ITEMS_CONTAINER::ITERATOR i = m_drawings.begin();
  866. while( i != m_drawings.end() )
  867. {
  868. if( i->m_Convert > 1 )
  869. i = m_drawings.erase( i );
  870. else
  871. ++i;
  872. }
  873. }
  874. }
  875. void LIB_PART::SetSubpartIdNotation( int aSep, int aFirstId )
  876. {
  877. m_subpartFirstId = 'A';
  878. m_subpartIdSeparator = 0;
  879. if( aSep == '.' || aSep == '-' || aSep == '_' )
  880. m_subpartIdSeparator = aSep;
  881. if( aFirstId == '1' && aSep != 0 )
  882. m_subpartFirstId = aFirstId;
  883. }
  884. std::vector<LIB_ITEM*> LIB_PART::GetUnitItems( int aUnit, int aConvert )
  885. {
  886. std::vector<LIB_ITEM*> unitItems;
  887. for( LIB_ITEM& item : m_drawings )
  888. {
  889. if( item.Type() == LIB_FIELD_T )
  890. continue;
  891. if( ( aConvert == -1 && item.GetUnit() == aUnit )
  892. || ( aUnit == -1 && item.GetConvert() == aConvert )
  893. || ( aUnit == item.GetUnit() && aConvert == item.GetConvert() ) )
  894. unitItems.push_back( &item );
  895. }
  896. return unitItems;
  897. }
  898. std::vector<struct PART_UNITS> LIB_PART::GetUnitDrawItems()
  899. {
  900. std::vector<struct PART_UNITS> units;
  901. for( LIB_ITEM& item : m_drawings )
  902. {
  903. if( item.Type() == LIB_FIELD_T )
  904. continue;
  905. int unit = item.GetUnit();
  906. int convert = item.GetConvert();
  907. auto it = std::find_if( units.begin(), units.end(),
  908. [unit, convert] ( const auto& a ) {
  909. return a.m_unit == unit && a.m_convert == convert;
  910. } );
  911. if( it == units.end() )
  912. {
  913. struct PART_UNITS newUnit;
  914. newUnit.m_unit = item.GetUnit();
  915. newUnit.m_convert = item.GetConvert();
  916. newUnit.m_items.push_back( &item );
  917. units.emplace_back( newUnit );
  918. }
  919. else
  920. {
  921. it->m_items.push_back( &item );
  922. }
  923. }
  924. return units;
  925. }
  926. std::vector<struct PART_UNITS> LIB_PART::GetUniqueUnits()
  927. {
  928. int unitNum;
  929. size_t i;
  930. struct PART_UNITS unit;
  931. std::vector<LIB_ITEM*> compareDrawItems;
  932. std::vector<LIB_ITEM*> currentDrawItems;
  933. std::vector<struct PART_UNITS> uniqueUnits;
  934. // The first unit is guarenteed to be unique so always include it.
  935. unit.m_unit = 1;
  936. unit.m_convert = 1;
  937. unit.m_items = GetUnitItems( 1, 1 );
  938. // There are no unique units if there are no draw items other than fields.
  939. if( unit.m_items.size() == 0 )
  940. return uniqueUnits;
  941. uniqueUnits.emplace_back( unit );
  942. if( ( GetUnitCount() == 1 || UnitsLocked() ) && !HasConversion() )
  943. return uniqueUnits;
  944. currentDrawItems = unit.m_items;
  945. for( unitNum = 2; unitNum <= GetUnitCount(); unitNum++ )
  946. {
  947. compareDrawItems = GetUnitItems( unitNum, 1 );
  948. wxCHECK2_MSG( compareDrawItems.size() != 0, continue,
  949. "Multiple unit symbol defined with empty units." );
  950. if( currentDrawItems.size() != compareDrawItems.size() )
  951. {
  952. unit.m_unit = unitNum;
  953. unit.m_convert = 1;
  954. unit.m_items = compareDrawItems;
  955. uniqueUnits.emplace_back( unit );
  956. }
  957. else
  958. {
  959. for( i = 0; i < currentDrawItems.size(); i++ )
  960. {
  961. if( currentDrawItems[i]->compare( *compareDrawItems[i],
  962. LIB_ITEM::COMPARE_FLAGS::UNIT ) != 0 )
  963. {
  964. unit.m_unit = unitNum;
  965. unit.m_convert = 1;
  966. unit.m_items = compareDrawItems;
  967. uniqueUnits.emplace_back( unit );
  968. }
  969. }
  970. }
  971. }
  972. if( HasConversion() )
  973. {
  974. currentDrawItems = GetUnitItems( 1, 2 );
  975. if( ( GetUnitCount() == 1 || UnitsLocked() ) )
  976. {
  977. unit.m_unit = 1;
  978. unit.m_convert = 2;
  979. unit.m_items = currentDrawItems;
  980. uniqueUnits.emplace_back( unit );
  981. return uniqueUnits;
  982. }
  983. for( unitNum = 2; unitNum <= GetUnitCount(); unitNum++ )
  984. {
  985. compareDrawItems = GetUnitItems( unitNum, 2 );
  986. wxCHECK2_MSG( compareDrawItems.size() != 0, continue,
  987. "Multiple unit symbol defined with empty units." );
  988. if( currentDrawItems.size() != compareDrawItems.size() )
  989. {
  990. unit.m_unit = unitNum;
  991. unit.m_convert = 2;
  992. unit.m_items = compareDrawItems;
  993. uniqueUnits.emplace_back( unit );
  994. }
  995. else
  996. {
  997. for( i = 0; i < currentDrawItems.size(); i++ )
  998. {
  999. if( currentDrawItems[i]->compare( *compareDrawItems[i],
  1000. LIB_ITEM::COMPARE_FLAGS::UNIT ) != 0 )
  1001. {
  1002. unit.m_unit = unitNum;
  1003. unit.m_convert = 2;
  1004. unit.m_items = compareDrawItems;
  1005. uniqueUnits.emplace_back( unit );
  1006. }
  1007. }
  1008. }
  1009. }
  1010. }
  1011. return uniqueUnits;
  1012. }