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.

2011 lines
53 KiB

3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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) 2022 CERN
  7. * Copyright (C) 2004-2023 KiCad Developers, see AUTHORS.txt for contributors.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #include <sch_draw_panel.h>
  27. #include <plotters/plotter.h>
  28. #include <sch_screen.h>
  29. #include <richio.h>
  30. #include <template_fieldnames.h>
  31. #include <transform.h>
  32. #include <symbol_library.h>
  33. #include <lib_pin.h>
  34. #include <settings/color_settings.h>
  35. #include <lib_shape.h>
  36. #include <memory>
  37. std::vector<SEARCH_TERM> LIB_SYMBOL::GetSearchTerms()
  38. {
  39. std::vector<SEARCH_TERM> terms;
  40. terms.emplace_back( SEARCH_TERM( GetName(), 8 ) );
  41. wxStringTokenizer keywordTokenizer( GetKeyWords(), wxS( " " ), wxTOKEN_STRTOK );
  42. while( keywordTokenizer.HasMoreTokens() )
  43. terms.emplace_back( SEARCH_TERM( keywordTokenizer.GetNextToken(), 4 ) );
  44. // TODO(JE) rework this later so we can highlight matches in their column
  45. std::map<wxString, wxString> fields;
  46. GetChooserFields( fields );
  47. for( const auto& [ name, text ] : fields )
  48. terms.emplace_back( SEARCH_TERM( text, 4 ) );
  49. // Also include keywords as one long string, just in case
  50. terms.emplace_back( SEARCH_TERM( GetKeyWords(), 1 ) );
  51. terms.emplace_back( SEARCH_TERM( GetDescription(), 1 ) );
  52. wxString footprint = GetFootprintField().GetText();
  53. if( !footprint.IsEmpty() )
  54. terms.emplace_back( SEARCH_TERM( GetFootprintField().GetText(), 1 ) );
  55. return terms;
  56. }
  57. void LIB_SYMBOL::GetChooserFields( std::map<wxString , wxString>& aColumnMap )
  58. {
  59. for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  60. {
  61. LIB_FIELD* field = static_cast<LIB_FIELD*>( &item );
  62. if( field->ShowInChooser() )
  63. aColumnMap[field->GetName()] = field->EDA_TEXT::GetShownText( false );
  64. }
  65. }
  66. bool operator<( const LIB_SYMBOL& aItem1, const LIB_SYMBOL& aItem2 )
  67. {
  68. return aItem1.GetName() < aItem2.GetName();
  69. }
  70. /// http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/sp_techniques.html#weak_without_shared
  71. struct null_deleter
  72. {
  73. void operator()(void const *) const
  74. {
  75. }
  76. };
  77. LIB_SYMBOL::LIB_SYMBOL( const wxString& aName, LIB_SYMBOL* aParent, SYMBOL_LIB* aLibrary ) :
  78. EDA_ITEM( LIB_SYMBOL_T ),
  79. m_me( this, null_deleter() ),
  80. m_excludedFromSim( false ),
  81. m_excludedFromBOM( false ),
  82. m_excludedFromBoard( false )
  83. {
  84. m_lastModDate = 0;
  85. m_unitCount = 1;
  86. m_pinNameOffset = schIUScale.MilsToIU( DEFAULT_PIN_NAME_OFFSET );
  87. m_options = ENTRY_NORMAL;
  88. m_unitsLocked = false;
  89. m_showPinNumbers = true;
  90. m_showPinNames = true;
  91. // Add the MANDATORY_FIELDS in RAM only. These are assumed to be present
  92. // when the field editors are invoked.
  93. m_drawings[LIB_FIELD_T].reserve( MANDATORY_FIELDS );
  94. for( int i = 0; i < MANDATORY_FIELDS; i++ )
  95. m_drawings[LIB_FIELD_T].push_back( new LIB_FIELD( this, i ) );
  96. SetName( aName );
  97. if( aParent )
  98. SetParent( aParent );
  99. SetLib( aLibrary );
  100. }
  101. LIB_SYMBOL::LIB_SYMBOL( const LIB_SYMBOL& aSymbol, SYMBOL_LIB* aLibrary ) :
  102. EDA_ITEM( aSymbol ),
  103. m_me( this, null_deleter() )
  104. {
  105. LIB_ITEM* newItem;
  106. m_library = aLibrary;
  107. m_name = aSymbol.m_name;
  108. m_fpFilters = wxArrayString( aSymbol.m_fpFilters );
  109. m_unitCount = aSymbol.m_unitCount;
  110. m_unitsLocked = aSymbol.m_unitsLocked;
  111. m_pinNameOffset = aSymbol.m_pinNameOffset;
  112. m_showPinNumbers = aSymbol.m_showPinNumbers;
  113. m_excludedFromSim = aSymbol.m_excludedFromSim;
  114. m_excludedFromBOM = aSymbol.m_excludedFromBOM;
  115. m_excludedFromBoard = aSymbol.m_excludedFromBoard;
  116. m_showPinNames = aSymbol.m_showPinNames;
  117. m_lastModDate = aSymbol.m_lastModDate;
  118. m_options = aSymbol.m_options;
  119. m_libId = aSymbol.m_libId;
  120. m_keyWords = aSymbol.m_keyWords;
  121. aSymbol.CopyUnitDisplayNames( m_unitDisplayNames );
  122. ClearSelected();
  123. for( const LIB_ITEM& oldItem : aSymbol.m_drawings )
  124. {
  125. if( ( oldItem.GetFlags() & ( IS_NEW | STRUCT_DELETED ) ) != 0 )
  126. continue;
  127. try
  128. {
  129. newItem = (LIB_ITEM*) oldItem.Clone();
  130. newItem->ClearSelected();
  131. newItem->SetParent( this );
  132. m_drawings.push_back( newItem );
  133. }
  134. catch( ... )
  135. {
  136. wxFAIL_MSG( "Failed to clone LIB_ITEM." );
  137. return;
  138. }
  139. }
  140. LIB_SYMBOL_SPTR parent = aSymbol.m_parent.lock();
  141. if( parent )
  142. SetParent( parent.get() );
  143. }
  144. const LIB_SYMBOL& LIB_SYMBOL::operator=( const LIB_SYMBOL& aSymbol )
  145. {
  146. if( &aSymbol == this )
  147. return aSymbol;
  148. LIB_ITEM* newItem;
  149. m_library = aSymbol.m_library;
  150. m_name = aSymbol.m_name;
  151. m_fpFilters = wxArrayString( aSymbol.m_fpFilters );
  152. m_unitCount = aSymbol.m_unitCount;
  153. m_unitsLocked = aSymbol.m_unitsLocked;
  154. m_pinNameOffset = aSymbol.m_pinNameOffset;
  155. m_showPinNumbers = aSymbol.m_showPinNumbers;
  156. m_showPinNames = aSymbol.m_showPinNames;
  157. m_excludedFromSim = aSymbol.m_excludedFromSim;
  158. m_excludedFromBOM = aSymbol.m_excludedFromBOM;
  159. m_excludedFromBoard = aSymbol.m_excludedFromBoard;
  160. m_lastModDate = aSymbol.m_lastModDate;
  161. m_options = aSymbol.m_options;
  162. m_libId = aSymbol.m_libId;
  163. m_keyWords = aSymbol.m_keyWords;
  164. m_unitDisplayNames.clear();
  165. aSymbol.CopyUnitDisplayNames( m_unitDisplayNames );
  166. m_drawings.clear();
  167. for( const LIB_ITEM& oldItem : aSymbol.m_drawings )
  168. {
  169. if( ( oldItem.GetFlags() & ( IS_NEW | STRUCT_DELETED ) ) != 0 )
  170. continue;
  171. newItem = (LIB_ITEM*) oldItem.Clone();
  172. newItem->SetParent( this );
  173. m_drawings.push_back( newItem );
  174. }
  175. m_drawings.sort();
  176. LIB_SYMBOL_SPTR parent = aSymbol.m_parent.lock();
  177. if( parent )
  178. SetParent( parent.get() );
  179. return *this;
  180. }
  181. unsigned LIB_SYMBOL::GetInheritanceDepth() const
  182. {
  183. unsigned depth = 0;
  184. LIB_SYMBOL_SPTR parent = GetParent().lock();
  185. while( parent )
  186. {
  187. depth += 1;
  188. parent = parent->GetParent().lock();
  189. }
  190. return depth;
  191. }
  192. #define REPORT( msg ) { if( aReporter ) aReporter->Report( msg ); }
  193. #define ITEM_DESC( item ) ( item )->GetItemDescription( &unitsProvider )
  194. int LIB_SYMBOL::Compare( const LIB_SYMBOL& aRhs, int aCompareFlags, REPORTER* aReporter ) const
  195. {
  196. UNITS_PROVIDER unitsProvider( schIUScale, EDA_UNITS::MILLIMETRES );
  197. if( m_me == aRhs.m_me )
  198. return 0;
  199. if( !aReporter && ( aCompareFlags & LIB_ITEM::COMPARE_FLAGS::ERC ) == 0 )
  200. {
  201. if( int tmp = m_name.Cmp( aRhs.m_name ) )
  202. return tmp;
  203. if( int tmp = m_libId.compare( aRhs.m_libId ) )
  204. return tmp;
  205. if( m_parent.lock() < aRhs.m_parent.lock() )
  206. return -1;
  207. if( m_parent.lock() > aRhs.m_parent.lock() )
  208. return 1;
  209. }
  210. int retv = 0;
  211. if( m_options != aRhs.m_options )
  212. {
  213. retv = ( m_options == ENTRY_NORMAL ) ? -1 : 1;
  214. REPORT( _( "Power flag differs." ) );
  215. if( !aReporter )
  216. return retv;
  217. }
  218. if( int tmp = m_unitCount - aRhs.m_unitCount )
  219. {
  220. retv = tmp;
  221. REPORT( _( "Unit count differs." ) );
  222. if( !aReporter )
  223. return retv;
  224. }
  225. // Make sure shapes and pins are sorted. No need with fields as those are
  226. // matched by id/name.
  227. std::set<const LIB_ITEM*, LIB_ITEM::cmp_items> aShapes;
  228. std::set<const LIB_ITEM*> aFields;
  229. std::set<const LIB_ITEM*, LIB_ITEM::cmp_items> aPins;
  230. for( auto it = m_drawings.begin(); it != m_drawings.end(); ++it )
  231. {
  232. if( it->Type() == LIB_SHAPE_T )
  233. aShapes.insert( &(*it) );
  234. else if( it->Type() == LIB_FIELD_T )
  235. aFields.insert( &(*it) );
  236. else if( it->Type() == LIB_PIN_T )
  237. aPins.insert( &(*it) );
  238. }
  239. std::set<const LIB_ITEM*, LIB_ITEM::cmp_items> bShapes;
  240. std::set<const LIB_ITEM*> bFields;
  241. std::set<const LIB_ITEM*, LIB_ITEM::cmp_items> bPins;
  242. for( auto it = aRhs.m_drawings.begin(); it != aRhs.m_drawings.end(); ++it )
  243. {
  244. if( it->Type() == LIB_SHAPE_T )
  245. bShapes.insert( &(*it) );
  246. else if( it->Type() == LIB_FIELD_T )
  247. bFields.insert( &(*it) );
  248. else if( it->Type() == LIB_PIN_T )
  249. bPins.insert( &(*it) );
  250. }
  251. if( int tmp = static_cast<int>( aShapes.size() - bShapes.size() ) )
  252. {
  253. retv = tmp;
  254. REPORT( _( "Graphic item count differs." ) );
  255. if( !aReporter )
  256. return retv;
  257. }
  258. else
  259. {
  260. for( auto aIt = aShapes.begin(), bIt = bShapes.begin(); aIt != aShapes.end(); aIt++, bIt++ )
  261. {
  262. if( int tmp2 = (*aIt)->compare( *(*bIt), aCompareFlags ) )
  263. {
  264. retv = tmp2;
  265. REPORT( wxString::Format( _( "%s differs." ), ITEM_DESC( *aIt ) ) );
  266. if( !aReporter )
  267. return retv;
  268. }
  269. }
  270. }
  271. if( int tmp = static_cast<int>( aPins.size() - bPins.size() ) )
  272. {
  273. retv = tmp;
  274. REPORT( _( "Pin count differs." ) );
  275. if( !aReporter )
  276. return retv;
  277. }
  278. else
  279. {
  280. for( const LIB_ITEM* aPinItem : aPins )
  281. {
  282. const LIB_PIN* aPin = static_cast<const LIB_PIN*>( aPinItem );
  283. const LIB_PIN* bPin = aRhs.GetPin( aPin->GetNumber(), aPin->GetUnit(),
  284. aPin->GetBodyStyle() );
  285. if( !bPin )
  286. {
  287. retv = 1;
  288. REPORT( wxString::Format( _( "Pin %s not found." ), aPin->GetNumber() ) );
  289. if( !aReporter )
  290. return retv;
  291. }
  292. else if( int tmp2 = aPinItem->compare( *bPin, aCompareFlags ) )
  293. {
  294. retv = tmp2;
  295. REPORT( wxString::Format( _( "Pin %s differs." ), aPin->GetNumber() ) );
  296. if( !aReporter )
  297. return retv;
  298. }
  299. }
  300. }
  301. for( const LIB_ITEM* aFieldItem : aFields )
  302. {
  303. const LIB_FIELD* aField = static_cast<const LIB_FIELD*>( aFieldItem );
  304. const LIB_FIELD* bField = nullptr;
  305. int tmp = 0;
  306. if( aField->GetId() >= 0 && aField->GetId() < MANDATORY_FIELDS )
  307. bField = aRhs.GetFieldById( aField->GetId() );
  308. else
  309. bField = aRhs.FindField( aField->GetName() );
  310. if( !bField )
  311. {
  312. tmp = 1;
  313. }
  314. else
  315. {
  316. tmp = aFieldItem->compare( *bField, aCompareFlags );
  317. }
  318. if( tmp )
  319. {
  320. retv = tmp;
  321. REPORT( wxString::Format( _( "%s field differs." ), aField->GetName( false ) ) );
  322. if( !aReporter )
  323. return retv;
  324. }
  325. }
  326. if( int tmp = static_cast<int>( aFields.size() - bFields.size() ) )
  327. {
  328. retv = tmp;
  329. REPORT( _( "Field count differs." ) );
  330. if( !aReporter )
  331. return retv;
  332. }
  333. if( int tmp = static_cast<int>( m_fpFilters.GetCount() - aRhs.m_fpFilters.GetCount() ) )
  334. {
  335. retv = tmp;
  336. REPORT( _( "Footprint filters differs." ) );
  337. if( !aReporter )
  338. return retv;
  339. }
  340. else
  341. {
  342. for( size_t i = 0; i < m_fpFilters.GetCount(); i++ )
  343. {
  344. if( int tmp2 = m_fpFilters[i].Cmp( aRhs.m_fpFilters[i] ) )
  345. {
  346. retv = tmp2;
  347. REPORT( _( "Footprint filters differ." ) );
  348. if( !aReporter )
  349. return retv;
  350. }
  351. }
  352. }
  353. if( int tmp = m_keyWords.Cmp( aRhs.m_keyWords ) )
  354. {
  355. retv = tmp;
  356. REPORT( _( "Symbol keywords differ." ) );
  357. if( !aReporter )
  358. return retv;
  359. }
  360. if( int tmp = m_pinNameOffset - aRhs.m_pinNameOffset )
  361. {
  362. retv = tmp;
  363. REPORT( _( "Symbol pin name offsets differ." ) );
  364. if( !aReporter )
  365. return retv;
  366. }
  367. if( ( aCompareFlags & LIB_ITEM::COMPARE_FLAGS::ERC ) == 0 )
  368. {
  369. if( m_showPinNames != aRhs.m_showPinNames )
  370. {
  371. retv = ( m_showPinNames ) ? 1 : -1;
  372. REPORT( _( "Show pin names settings differ." ) );
  373. if( !aReporter )
  374. return retv;
  375. }
  376. if( m_showPinNumbers != aRhs.m_showPinNumbers )
  377. {
  378. retv = ( m_showPinNumbers ) ? 1 : -1;
  379. REPORT( _( "Show pin numbers settings differ." ) );
  380. if( !aReporter )
  381. return retv;
  382. }
  383. if( m_excludedFromSim != aRhs.m_excludedFromSim )
  384. {
  385. retv = ( m_excludedFromSim ) ? -1 : 1;
  386. REPORT( _( "Exclude from simulation settings differ." ) );
  387. if( !aReporter )
  388. return retv;
  389. }
  390. if( m_excludedFromBOM != aRhs.m_excludedFromBOM )
  391. {
  392. retv = ( m_excludedFromBOM ) ? -1 : 1;
  393. REPORT( _( "Exclude from bill of materials settings differ." ) );
  394. if( !aReporter )
  395. return retv;
  396. }
  397. if( m_excludedFromBoard != aRhs.m_excludedFromBoard )
  398. {
  399. retv = ( m_excludedFromBoard ) ? -1 : 1;
  400. REPORT( _( "Exclude from board settings differ." ) );
  401. if( !aReporter )
  402. return retv;
  403. }
  404. }
  405. if( !aReporter )
  406. {
  407. if( m_unitsLocked != aRhs.m_unitsLocked )
  408. return ( m_unitsLocked ) ? 1 : -1;
  409. // Compare unit display names
  410. if( m_unitDisplayNames < aRhs.m_unitDisplayNames )
  411. {
  412. return -1;
  413. }
  414. else if( m_unitDisplayNames > aRhs.m_unitDisplayNames )
  415. {
  416. return 1;
  417. }
  418. }
  419. return retv;
  420. }
  421. LIB_SYMBOL_SPTR LIB_SYMBOL::GetRootSymbol() const
  422. {
  423. const LIB_SYMBOL_SPTR sp = m_parent.lock();
  424. // Recurse until the parent symbol is empty.
  425. if( sp )
  426. return sp->GetRootSymbol();
  427. return m_me;
  428. }
  429. wxString LIB_SYMBOL::GetUnitReference( int aUnit )
  430. {
  431. return LIB_SYMBOL::LetterSubReference( aUnit, 'A' );
  432. }
  433. bool LIB_SYMBOL::HasUnitDisplayName( int aUnit )
  434. {
  435. return ( m_unitDisplayNames.count( aUnit ) == 1 );
  436. }
  437. wxString LIB_SYMBOL::GetUnitDisplayName( int aUnit )
  438. {
  439. if( HasUnitDisplayName( aUnit ) )
  440. {
  441. return m_unitDisplayNames[aUnit];
  442. }
  443. else
  444. {
  445. return wxString::Format( _( "Unit %s" ), GetUnitReference( aUnit ) );
  446. }
  447. }
  448. void LIB_SYMBOL::CopyUnitDisplayNames( std::map<int, wxString>& aTarget ) const
  449. {
  450. for( const auto& it : m_unitDisplayNames )
  451. {
  452. aTarget[it.first] = it.second;
  453. }
  454. }
  455. void LIB_SYMBOL::SetUnitDisplayName( int aUnit, const wxString& aName )
  456. {
  457. if( aUnit <= GetUnitCount() )
  458. {
  459. if( aName.Length() > 0 )
  460. {
  461. m_unitDisplayNames[aUnit] = aName;
  462. }
  463. else
  464. {
  465. m_unitDisplayNames.erase( aUnit );
  466. }
  467. }
  468. }
  469. void LIB_SYMBOL::SetName( const wxString& aName )
  470. {
  471. m_name = aName;
  472. m_libId.SetLibItemName( aName );
  473. }
  474. void LIB_SYMBOL::SetParent( LIB_SYMBOL* aParent )
  475. {
  476. if( aParent )
  477. m_parent = aParent->SharedPtr();
  478. else
  479. m_parent.reset();
  480. }
  481. std::unique_ptr< LIB_SYMBOL > LIB_SYMBOL::Flatten() const
  482. {
  483. std::unique_ptr< LIB_SYMBOL > retv;
  484. if( IsAlias() )
  485. {
  486. LIB_SYMBOL_SPTR parent = m_parent.lock();
  487. wxCHECK_MSG( parent, retv,
  488. wxString::Format( "Parent of derived symbol '%s' undefined", m_name ) );
  489. // Copy the parent.
  490. if( parent->IsAlias() )
  491. retv = parent->Flatten();
  492. else
  493. retv = std::make_unique<LIB_SYMBOL>( *parent.get() );
  494. retv->m_name = m_name;
  495. retv->SetLibId( m_libId );
  496. // Now add the inherited part mandatory field (this) information.
  497. for( int i = 0; i < MANDATORY_FIELDS; i++ )
  498. {
  499. wxString tmp = GetFieldById( i )->GetText();
  500. // If the field isn't defined then inherit the parent field value.
  501. if( tmp.IsEmpty() )
  502. retv->GetFieldById( i )->SetText( retv->GetFieldById( i )->GetText() );
  503. else
  504. *retv->GetFieldById( i ) = *GetFieldById( i );
  505. }
  506. // Grab all the rest of derived symbol fields.
  507. for( const LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  508. {
  509. const LIB_FIELD* aliasField = dynamic_cast<const LIB_FIELD*>( &item );
  510. wxCHECK2( aliasField, continue );
  511. // Mandatory fields were already resolved.
  512. if( aliasField->IsMandatory() )
  513. continue;
  514. LIB_FIELD* newField = new LIB_FIELD( *aliasField );
  515. newField->SetParent( retv.get() );
  516. LIB_FIELD* parentField = retv->FindField( aliasField->GetName() );
  517. if( !parentField ) // Derived symbol field does not exist in parent symbol.
  518. {
  519. retv->AddDrawItem( newField );
  520. }
  521. else // Derived symbol field overrides the parent symbol field.
  522. {
  523. retv->RemoveDrawItem( parentField );
  524. retv->AddDrawItem( newField );
  525. }
  526. }
  527. retv->SetKeyWords( m_keyWords.IsEmpty() ? parent->GetKeyWords() : m_keyWords );
  528. retv->SetFPFilters( m_fpFilters.IsEmpty() ? parent->GetFPFilters() : m_fpFilters );
  529. retv->SetExcludedFromSim( parent->GetExcludedFromSim() );
  530. retv->SetExcludedFromBOM( parent->GetExcludedFromBOM() );
  531. retv->SetExcludedFromBoard( parent->GetExcludedFromBoard() );
  532. retv->UpdateFieldOrdinals();
  533. }
  534. else
  535. {
  536. retv = std::make_unique<LIB_SYMBOL>( *this );
  537. }
  538. return retv;
  539. }
  540. void LIB_SYMBOL::ClearCaches()
  541. {
  542. for( LIB_ITEM& item : m_drawings )
  543. {
  544. if( EDA_TEXT* eda_text = dynamic_cast<EDA_TEXT*>( &item ) )
  545. {
  546. eda_text->ClearBoundingBoxCache();
  547. eda_text->ClearRenderCache();
  548. }
  549. }
  550. }
  551. const wxString LIB_SYMBOL::GetLibraryName() const
  552. {
  553. if( m_library )
  554. return m_library->GetName();
  555. return m_libId.GetLibNickname();
  556. }
  557. bool LIB_SYMBOL::IsPower() const
  558. {
  559. if( LIB_SYMBOL_SPTR parent = m_parent.lock() )
  560. {
  561. if( parent->IsRoot() )
  562. return parent->m_options == ENTRY_POWER;
  563. else
  564. return parent->IsPower();
  565. }
  566. return m_options == ENTRY_POWER;
  567. }
  568. void LIB_SYMBOL::SetPower()
  569. {
  570. if( LIB_SYMBOL_SPTR parent = m_parent.lock() )
  571. {
  572. if( parent->IsRoot() )
  573. parent->m_options = ENTRY_POWER;
  574. else
  575. parent->SetPower();
  576. }
  577. m_options = ENTRY_POWER;
  578. }
  579. bool LIB_SYMBOL::IsNormal() const
  580. {
  581. if( LIB_SYMBOL_SPTR parent = m_parent.lock() )
  582. {
  583. if( parent->IsRoot() )
  584. return parent->m_options == ENTRY_NORMAL;
  585. else
  586. return parent->IsNormal();
  587. }
  588. return m_options == ENTRY_NORMAL;
  589. }
  590. void LIB_SYMBOL::SetNormal()
  591. {
  592. if( LIB_SYMBOL_SPTR parent = m_parent.lock() )
  593. {
  594. if( parent->IsRoot() )
  595. parent->m_options = ENTRY_NORMAL;
  596. else
  597. parent->SetNormal();
  598. }
  599. m_options = ENTRY_NORMAL;
  600. }
  601. wxString LIB_SYMBOL::LetterSubReference( int aUnit, int aFirstId )
  602. {
  603. // use letters as notation. To allow more than 26 units, the sub ref
  604. // use one letter if letter = A .. Z or a ... z, and 2 letters otherwise
  605. // first letter is expected to be 'A' or 'a' (i.e. 26 letters are available)
  606. int u;
  607. wxString suffix;
  608. do
  609. {
  610. u = ( aUnit - 1 ) % 26;
  611. suffix = wxChar( aFirstId + u ) + suffix;
  612. aUnit = ( aUnit - u ) / 26;
  613. } while( aUnit > 0 );
  614. return suffix;
  615. }
  616. void LIB_SYMBOL::PrintBackground( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
  617. int aUnit, int aBodyStyle, const LIB_SYMBOL_OPTIONS& aOpts,
  618. bool aDimmed )
  619. {
  620. /* draw background for filled items using background option
  621. * Solid lines will be drawn after the background
  622. * Note also, background is not drawn when printing in black and white
  623. */
  624. if( !GetGRForceBlackPenState() )
  625. {
  626. for( LIB_ITEM& item : m_drawings )
  627. {
  628. // Do not print private items
  629. if( item.IsPrivate() )
  630. continue;
  631. if( item.Type() == LIB_SHAPE_T )
  632. {
  633. LIB_SHAPE& shape = static_cast<LIB_SHAPE&>( item );
  634. // Do not draw items not attached to the current part
  635. if( aUnit && shape.m_unit && ( shape.m_unit != aUnit ) )
  636. continue;
  637. if( aBodyStyle && shape.m_bodyStyle && ( shape.m_bodyStyle != aBodyStyle ) )
  638. continue;
  639. if( shape.GetFillMode() == FILL_T::FILLED_WITH_BG_BODYCOLOR )
  640. shape.Print( aSettings, aOffset, (void*) false, aOpts.transform, aDimmed );
  641. }
  642. }
  643. }
  644. }
  645. void LIB_SYMBOL::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, int aUnit,
  646. int aBodyStyle, const LIB_SYMBOL_OPTIONS& aOpts, bool aDimmed )
  647. {
  648. for( LIB_ITEM& item : m_drawings )
  649. {
  650. // Do not print private items
  651. if( item.IsPrivate() )
  652. continue;
  653. // Do not draw items not attached to the current part
  654. if( aUnit && item.m_unit && ( item.m_unit != aUnit ) )
  655. continue;
  656. if( aBodyStyle && item.m_bodyStyle && ( item.m_bodyStyle != aBodyStyle ) )
  657. continue;
  658. if( item.Type() == LIB_FIELD_T )
  659. {
  660. LIB_FIELD& field = static_cast<LIB_FIELD&>( item );
  661. if( field.IsVisible() && !aOpts.draw_visible_fields )
  662. continue;
  663. if( !field.IsVisible() && !aOpts.draw_hidden_fields )
  664. continue;
  665. }
  666. if( item.Type() == LIB_PIN_T )
  667. {
  668. item.Print( aSettings, aOffset, (void*) &aOpts, aOpts.transform, aDimmed );
  669. }
  670. else if( item.Type() == LIB_FIELD_T )
  671. {
  672. item.Print( aSettings, aOffset, nullptr, aOpts.transform, aDimmed );
  673. }
  674. else if( item.Type() == LIB_SHAPE_T )
  675. {
  676. LIB_SHAPE& shape = static_cast<LIB_SHAPE&>( item );
  677. bool forceNoFill = shape.GetFillMode() == FILL_T::FILLED_WITH_BG_BODYCOLOR;
  678. shape.Print( aSettings, aOffset, (void*) forceNoFill, aOpts.transform, aDimmed );
  679. }
  680. else
  681. {
  682. item.Print( aSettings, aOffset, (void*) false, aOpts.transform, aDimmed );
  683. }
  684. }
  685. }
  686. void LIB_SYMBOL::Plot( PLOTTER *aPlotter, int aUnit, int aBodyStyle, bool aBackground,
  687. const VECTOR2I &aOffset, const TRANSFORM &aTransform, bool aDimmed ) const
  688. {
  689. wxASSERT( aPlotter != nullptr );
  690. COLOR4D color = aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE );
  691. COLOR4D bg = aPlotter->RenderSettings()->GetBackgroundColor();
  692. if( bg == COLOR4D::UNSPECIFIED || !aPlotter->GetColorMode() )
  693. bg = COLOR4D::WHITE;
  694. if( aDimmed )
  695. {
  696. color.Desaturate( );
  697. color = color.Mix( bg, 0.5f );
  698. }
  699. aPlotter->SetColor( color );
  700. for( const LIB_ITEM& item : m_drawings )
  701. {
  702. // Do not plot private items
  703. if( item.IsPrivate() )
  704. continue;
  705. // LIB_FIELDs are not plotted here, because this plot function is used to plot schematic
  706. // items which have their own SCH_FIELDs
  707. if( item.Type() == LIB_FIELD_T )
  708. continue;
  709. if( aUnit && item.m_unit && ( item.m_unit != aUnit ) )
  710. continue;
  711. if( aBodyStyle && item.m_bodyStyle && ( item.m_bodyStyle != aBodyStyle ) )
  712. continue;
  713. item.Plot( aPlotter, aBackground, aOffset, aTransform, aDimmed );
  714. }
  715. }
  716. void LIB_SYMBOL::PlotLibFields( PLOTTER* aPlotter, int aUnit, int aBodyStyle, bool aBackground,
  717. const VECTOR2I& aOffset, const TRANSFORM& aTransform, bool aDimmed,
  718. bool aPlotHidden )
  719. {
  720. wxASSERT( aPlotter != nullptr );
  721. COLOR4D color = aPlotter->RenderSettings()->GetLayerColor( LAYER_FIELDS );
  722. COLOR4D bg = aPlotter->RenderSettings()->GetBackgroundColor();
  723. if( bg == COLOR4D::UNSPECIFIED || !aPlotter->GetColorMode() )
  724. bg = COLOR4D::WHITE;
  725. if( aDimmed )
  726. {
  727. color.Desaturate( );
  728. color = color.Mix( bg, 0.5f );
  729. }
  730. aPlotter->SetColor( color );
  731. for( LIB_ITEM& item : m_drawings )
  732. {
  733. if( item.Type() != LIB_FIELD_T )
  734. continue;
  735. if( !aPlotHidden && !( (LIB_FIELD&) item ).IsVisible() )
  736. continue;
  737. if( aUnit && item.m_unit && ( item.m_unit != aUnit ) )
  738. continue;
  739. if( aBodyStyle && item.m_bodyStyle && ( item.m_bodyStyle != aBodyStyle ) )
  740. continue;
  741. LIB_FIELD& field = (LIB_FIELD&) item;
  742. // The reference is a special case: we should change the basic text
  743. // to add '?' and the part id
  744. wxString tmp = field.GetShownText( true );
  745. if( field.GetId() == REFERENCE_FIELD )
  746. {
  747. wxString text = field.GetFullText( aUnit );
  748. field.SetText( text );
  749. }
  750. item.Plot( aPlotter, aBackground, aOffset, aTransform, aDimmed );
  751. field.SetText( tmp );
  752. }
  753. }
  754. void LIB_SYMBOL::FixupDrawItems()
  755. {
  756. std::vector<LIB_SHAPE*> potential_top_items;
  757. std::vector<LIB_ITEM*> bottom_items;
  758. for( LIB_ITEM& item : m_drawings )
  759. {
  760. if( item.Type() == LIB_SHAPE_T )
  761. {
  762. LIB_SHAPE& shape = static_cast<LIB_SHAPE&>( item );
  763. if( shape.GetFillMode() == FILL_T::FILLED_WITH_COLOR )
  764. potential_top_items.push_back( &shape );
  765. else
  766. bottom_items.push_back( &item );
  767. }
  768. else
  769. {
  770. bottom_items.push_back( &item );
  771. }
  772. }
  773. std::sort( potential_top_items.begin(), potential_top_items.end(),
  774. []( LIB_ITEM* a, LIB_ITEM* b )
  775. {
  776. return a->GetBoundingBox().GetArea() > b->GetBoundingBox().GetArea();
  777. } );
  778. for( LIB_SHAPE* item : potential_top_items )
  779. {
  780. for( LIB_ITEM* bottom_item : bottom_items )
  781. {
  782. if( item->GetBoundingBox().Contains( bottom_item->GetBoundingBox() ) )
  783. {
  784. item->SetFillMode( FILL_T::FILLED_WITH_BG_BODYCOLOR );
  785. break;
  786. }
  787. }
  788. }
  789. }
  790. void LIB_SYMBOL::RemoveDrawItem( LIB_ITEM* aItem )
  791. {
  792. wxASSERT( aItem != nullptr );
  793. // none of the MANDATORY_FIELDS may be removed in RAM, but they may be
  794. // omitted when saving to disk.
  795. if( aItem->Type() == LIB_FIELD_T )
  796. {
  797. if( static_cast<LIB_FIELD*>( aItem )->IsMandatory() )
  798. return;
  799. }
  800. LIB_ITEMS& items = m_drawings[ aItem->Type() ];
  801. for( LIB_ITEMS::iterator i = items.begin(); i != items.end(); i++ )
  802. {
  803. if( &*i == aItem )
  804. {
  805. items.erase( i );
  806. break;
  807. }
  808. }
  809. }
  810. void LIB_SYMBOL::AddDrawItem( LIB_ITEM* aItem, bool aSort )
  811. {
  812. wxCHECK( aItem, /* void */ );
  813. m_drawings.push_back( aItem );
  814. if( aSort )
  815. m_drawings.sort();
  816. }
  817. void LIB_SYMBOL::GetPins( LIB_PINS& aList, int aUnit, int aBodyStyle ) const
  818. {
  819. /* Notes:
  820. * when aUnit == 0: no unit filtering
  821. * when aBodyStyle == 0: no body style filtering
  822. * when m_unit == 0, the item is common to all units
  823. * when m_bodyStyle == 0, the item is common to all body styles
  824. */
  825. LIB_SYMBOL_SPTR parent = m_parent.lock();
  826. const LIB_ITEMS_CONTAINER& drawItems = parent ? parent->m_drawings : m_drawings;
  827. for( const LIB_ITEM& item : drawItems[LIB_PIN_T] )
  828. {
  829. // Unit filtering:
  830. if( aUnit && item.m_unit && ( item.m_unit != aUnit ) )
  831. continue;
  832. // De Morgan variant filtering:
  833. if( aBodyStyle && item.m_bodyStyle && ( item.m_bodyStyle != aBodyStyle ) )
  834. continue;
  835. aList.push_back( (LIB_PIN*) &item );
  836. }
  837. }
  838. std::vector<LIB_PIN*> LIB_SYMBOL::GetAllLibPins() const
  839. {
  840. std::vector<LIB_PIN*> pinList;
  841. GetPins( pinList, 0, 0 );
  842. return pinList;
  843. }
  844. int LIB_SYMBOL::GetPinCount()
  845. {
  846. std::vector<LIB_PIN*> pinList;
  847. GetPins( pinList, 0, 1 ); // All units, but a single convert
  848. return pinList.size();
  849. }
  850. LIB_PIN* LIB_SYMBOL::GetPin( const wxString& aNumber, int aUnit, int aBodyStyle ) const
  851. {
  852. LIB_PINS pinList;
  853. GetPins( pinList, aUnit, aBodyStyle );
  854. for( LIB_PIN* pin : pinList )
  855. {
  856. wxASSERT( pin->Type() == LIB_PIN_T );
  857. if( aNumber == pin->GetNumber() )
  858. return pin;
  859. }
  860. return nullptr;
  861. }
  862. bool LIB_SYMBOL::PinsConflictWith( const LIB_SYMBOL& aOtherPart, bool aTestNums, bool aTestNames,
  863. bool aTestType, bool aTestOrientation, bool aTestLength ) const
  864. {
  865. LIB_PINS thisPinList;
  866. GetPins( thisPinList, /* aUnit */ 0, /* aBodyStyle */ 0 );
  867. for( const LIB_PIN* eachThisPin : thisPinList )
  868. {
  869. wxASSERT( eachThisPin );
  870. LIB_PINS otherPinList;
  871. aOtherPart.GetPins( otherPinList, /* aUnit */ 0, /* aBodyStyle */ 0 );
  872. bool foundMatch = false;
  873. for( const LIB_PIN* eachOtherPin : otherPinList )
  874. {
  875. wxASSERT( eachOtherPin );
  876. // Same unit?
  877. if( eachThisPin->GetUnit() != eachOtherPin->GetUnit() )
  878. continue;
  879. // Same body stype?
  880. if( eachThisPin->GetBodyStyle() != eachOtherPin->GetBodyStyle() )
  881. continue;
  882. // Same position?
  883. if( eachThisPin->GetPosition() != eachOtherPin->GetPosition() )
  884. continue;
  885. // Same number?
  886. if( aTestNums && ( eachThisPin->GetNumber() != eachOtherPin->GetNumber() ) )
  887. continue;
  888. // Same name?
  889. if( aTestNames && ( eachThisPin->GetName() != eachOtherPin->GetName() ) )
  890. continue;
  891. // Same electrical type?
  892. if( aTestType && ( eachThisPin->GetType() != eachOtherPin->GetType() ) )
  893. continue;
  894. // Same orientation?
  895. if( aTestOrientation
  896. && ( eachThisPin->GetOrientation() != eachOtherPin->GetOrientation() ) )
  897. continue;
  898. // Same length?
  899. if( aTestLength && ( eachThisPin->GetLength() != eachOtherPin->GetLength() ) )
  900. continue;
  901. foundMatch = true;
  902. break; // Match found so search is complete.
  903. }
  904. if( !foundMatch )
  905. {
  906. // This means there was not an identical (according to the arguments)
  907. // pin at the same position in the other symbol.
  908. return true;
  909. }
  910. }
  911. // The loop never gave up, so no conflicts were found.
  912. return false;
  913. }
  914. const BOX2I LIB_SYMBOL::GetUnitBoundingBox( int aUnit, int aBodyStyle,
  915. bool aIgnoreHiddenFields ) const
  916. {
  917. BOX2I bBox; // Start with a fresh BOX2I so the Merge algorithm works
  918. for( const LIB_ITEM& item : m_drawings )
  919. {
  920. if( item.m_unit > 0
  921. && m_unitCount > 1
  922. && aUnit > 0
  923. && aUnit != item.m_unit )
  924. {
  925. continue;
  926. }
  927. if( item.m_bodyStyle > 0 && aBodyStyle > 0 && aBodyStyle != item.m_bodyStyle )
  928. continue;
  929. if( aIgnoreHiddenFields && ( item.Type() == LIB_FIELD_T )
  930. && !( (LIB_FIELD&) item ).IsVisible() )
  931. continue;
  932. bBox.Merge( item.GetBoundingBox() );
  933. }
  934. return bBox;
  935. }
  936. void LIB_SYMBOL::ViewGetLayers( int aLayers[], int& aCount ) const
  937. {
  938. aCount = 0;
  939. aLayers[ aCount++ ] = LAYER_DEVICE;
  940. aLayers[ aCount++ ] = LAYER_DEVICE_BACKGROUND;
  941. aLayers[ aCount++ ] = LAYER_REFERENCEPART;
  942. aLayers[ aCount++ ] = LAYER_VALUEPART;
  943. aLayers[ aCount++ ] = LAYER_FIELDS;
  944. aLayers[ aCount++ ] = LAYER_PRIVATE_NOTES;
  945. aLayers[ aCount++ ] = LAYER_NOTES_BACKGROUND;
  946. aLayers[ aCount++ ] = LAYER_SELECTION_SHADOWS;
  947. }
  948. const BOX2I LIB_SYMBOL::GetBodyBoundingBox( int aUnit, int aBodyStyle, bool aIncludePins,
  949. bool aIncludePrivateItems ) const
  950. {
  951. BOX2I bbox;
  952. for( const LIB_ITEM& item : m_drawings )
  953. {
  954. if( item.m_unit > 0 && aUnit > 0 && aUnit != item.m_unit )
  955. continue;
  956. if( item.m_bodyStyle > 0 && aBodyStyle > 0 && aBodyStyle != item.m_bodyStyle )
  957. continue;
  958. if( item.IsPrivate() && !aIncludePrivateItems )
  959. continue;
  960. if( item.Type() == LIB_FIELD_T )
  961. continue;
  962. if( item.Type() == LIB_PIN_T )
  963. {
  964. const LIB_PIN& pin = static_cast<const LIB_PIN&>( item );
  965. if( pin.IsVisible() )
  966. {
  967. // Note: the roots of the pins are always included for symbols that don't have
  968. // a well-defined body.
  969. if( aIncludePins )
  970. bbox.Merge( pin.GetBoundingBox( false, false, false ) );
  971. else
  972. bbox.Merge( pin.GetPinRoot() );
  973. }
  974. }
  975. else
  976. {
  977. bbox.Merge( item.GetBoundingBox() );
  978. }
  979. }
  980. return bbox;
  981. }
  982. void LIB_SYMBOL::deleteAllFields()
  983. {
  984. m_drawings[ LIB_FIELD_T ].clear();
  985. }
  986. void LIB_SYMBOL::AddField( LIB_FIELD* aField )
  987. {
  988. AddDrawItem( aField );
  989. }
  990. void LIB_SYMBOL::SetFields( const std::vector<LIB_FIELD>& aFields )
  991. {
  992. deleteAllFields();
  993. for( size_t ii = 0; ii < aFields.size(); ++ii )
  994. {
  995. // drawings is a ptr_vector, new and copy an object on the heap.
  996. LIB_FIELD* field = new LIB_FIELD( aFields[ ii ] );
  997. field->SetParent( this );
  998. m_drawings.push_back( field );
  999. }
  1000. m_drawings.sort();
  1001. }
  1002. void LIB_SYMBOL::GetFields( std::vector<LIB_FIELD*>& aList )
  1003. {
  1004. // Grab the MANDATORY_FIELDS first, in expected order given by enum MANDATORY_FIELD_T
  1005. for( int id = 0; id < MANDATORY_FIELDS; ++id )
  1006. aList.push_back( GetFieldById( id ) );
  1007. // Now grab all the rest of fields.
  1008. for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  1009. {
  1010. LIB_FIELD* field = static_cast<LIB_FIELD*>( &item );
  1011. if( !field->IsMandatory() )
  1012. aList.push_back( field );
  1013. }
  1014. }
  1015. void LIB_SYMBOL::GetFields( std::vector<LIB_FIELD>& aList )
  1016. {
  1017. // Grab the MANDATORY_FIELDS first, in expected order given by enum MANDATORY_FIELD_T
  1018. for( int id = 0; id < MANDATORY_FIELDS; ++id )
  1019. aList.push_back( *GetFieldById( id ) );
  1020. // Now grab all the rest of fields.
  1021. for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  1022. {
  1023. LIB_FIELD* field = static_cast<LIB_FIELD*>( &item );
  1024. if( !field->IsMandatory() )
  1025. aList.push_back( *field );
  1026. }
  1027. }
  1028. LIB_FIELD* LIB_SYMBOL::GetFieldById( int aId ) const
  1029. {
  1030. for( const LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  1031. {
  1032. LIB_FIELD* field = ( LIB_FIELD* ) &item;
  1033. if( field->GetId() == aId )
  1034. return field;
  1035. }
  1036. return nullptr;
  1037. }
  1038. LIB_FIELD* LIB_SYMBOL::FindField( const wxString& aFieldName, bool aCaseInsensitive )
  1039. {
  1040. for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  1041. {
  1042. if( aCaseInsensitive )
  1043. {
  1044. if( static_cast<LIB_FIELD*>( &item )->GetCanonicalName().Upper() == aFieldName.Upper() )
  1045. return static_cast<LIB_FIELD*>( &item );
  1046. }
  1047. else
  1048. {
  1049. if( static_cast<LIB_FIELD*>( &item )->GetCanonicalName() == aFieldName )
  1050. return static_cast<LIB_FIELD*>( &item );
  1051. }
  1052. }
  1053. return nullptr;
  1054. }
  1055. const LIB_FIELD* LIB_SYMBOL::FindField( const wxString& aFieldName,
  1056. const bool aCaseInsensitive ) const
  1057. {
  1058. for( const LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  1059. {
  1060. if( aCaseInsensitive )
  1061. {
  1062. if( static_cast<const LIB_FIELD*>( &item )->GetCanonicalName().Upper()
  1063. == aFieldName.Upper() )
  1064. return static_cast<const LIB_FIELD*>( &item );
  1065. }
  1066. else
  1067. {
  1068. if( static_cast<const LIB_FIELD*>( &item )->GetCanonicalName() == aFieldName )
  1069. return static_cast<const LIB_FIELD*>( &item );
  1070. }
  1071. }
  1072. return nullptr;
  1073. }
  1074. LIB_FIELD& LIB_SYMBOL::GetValueField()
  1075. {
  1076. LIB_FIELD* field = GetFieldById( VALUE_FIELD );
  1077. wxASSERT( field != nullptr );
  1078. return *field;
  1079. }
  1080. LIB_FIELD& LIB_SYMBOL::GetReferenceField()
  1081. {
  1082. LIB_FIELD* field = GetFieldById( REFERENCE_FIELD );
  1083. wxASSERT( field != nullptr );
  1084. return *field;
  1085. }
  1086. LIB_FIELD& LIB_SYMBOL::GetFootprintField()
  1087. {
  1088. LIB_FIELD* field = GetFieldById( FOOTPRINT_FIELD );
  1089. wxASSERT( field != nullptr );
  1090. return *field;
  1091. }
  1092. LIB_FIELD& LIB_SYMBOL::GetDatasheetField()
  1093. {
  1094. LIB_FIELD* field = GetFieldById( DATASHEET_FIELD );
  1095. wxASSERT( field != nullptr );
  1096. return *field;
  1097. }
  1098. LIB_FIELD& LIB_SYMBOL::GetDescriptionField()
  1099. {
  1100. LIB_FIELD* field = GetFieldById( DESCRIPTION_FIELD );
  1101. wxASSERT( field != nullptr );
  1102. return *field;
  1103. }
  1104. wxString LIB_SYMBOL::GetPrefix()
  1105. {
  1106. wxString refDesignator = GetFieldById( REFERENCE_FIELD )->GetText();
  1107. refDesignator.Replace( wxS( "~" ), wxS( " " ) );
  1108. wxString prefix = refDesignator;
  1109. while( prefix.Length() )
  1110. {
  1111. wxUniCharRef last = prefix.Last();
  1112. if( ( last >= '0' && last <= '9' ) || last == '?' || last == '*' )
  1113. prefix.RemoveLast();
  1114. else
  1115. break;
  1116. }
  1117. // Avoid a prefix containing trailing/leading spaces
  1118. prefix.Trim( true );
  1119. prefix.Trim( false );
  1120. return prefix;
  1121. }
  1122. void LIB_SYMBOL::RunOnChildren( const std::function<void( LIB_ITEM* )>& aFunction )
  1123. {
  1124. for( LIB_ITEM& item : m_drawings )
  1125. aFunction( &item );
  1126. }
  1127. int LIB_SYMBOL::UpdateFieldOrdinals()
  1128. {
  1129. int retv = 0;
  1130. int lastOrdinal = MANDATORY_FIELDS;
  1131. for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  1132. {
  1133. LIB_FIELD* field = dynamic_cast<LIB_FIELD*>( &item );
  1134. wxCHECK2( field, continue );
  1135. // Mandatory fields were already resolved always have the same ordinal values.
  1136. if( field->IsMandatory() )
  1137. continue;
  1138. if( field->GetId() != lastOrdinal )
  1139. {
  1140. field->SetId( lastOrdinal );
  1141. retv += 1;
  1142. }
  1143. lastOrdinal += 1;
  1144. }
  1145. return retv;
  1146. }
  1147. int LIB_SYMBOL::GetNextAvailableFieldId() const
  1148. {
  1149. int retv = MANDATORY_FIELDS;
  1150. while( GetFieldById( retv ) )
  1151. retv += 1;
  1152. return retv;
  1153. }
  1154. void LIB_SYMBOL::SetOffset( const VECTOR2I& aOffset )
  1155. {
  1156. for( LIB_ITEM& item : m_drawings )
  1157. item.Offset( aOffset );
  1158. }
  1159. void LIB_SYMBOL::RemoveDuplicateDrawItems()
  1160. {
  1161. m_drawings.unique();
  1162. }
  1163. bool LIB_SYMBOL::HasAlternateBodyStyle() const
  1164. {
  1165. for( const LIB_ITEM& item : m_drawings )
  1166. {
  1167. if( item.m_bodyStyle > LIB_ITEM::BODY_STYLE::BASE )
  1168. return true;
  1169. }
  1170. if( LIB_SYMBOL_SPTR parent = m_parent.lock() )
  1171. {
  1172. for( const LIB_ITEM& item : parent->GetDrawItems() )
  1173. {
  1174. if( item.m_bodyStyle > LIB_ITEM::BODY_STYLE::BASE )
  1175. return true;
  1176. }
  1177. }
  1178. return false;
  1179. }
  1180. int LIB_SYMBOL::GetMaxPinNumber() const
  1181. {
  1182. int maxPinNumber = 0;
  1183. LIB_SYMBOL_SPTR parent = m_parent.lock();
  1184. const LIB_ITEMS_CONTAINER& drawItems = parent ? parent->m_drawings : m_drawings;
  1185. for( const LIB_ITEM& item : drawItems[LIB_PIN_T] )
  1186. {
  1187. const LIB_PIN* pin = static_cast<const LIB_PIN*>( &item );
  1188. long currentPinNumber = 0;
  1189. if( pin->GetNumber().ToLong( &currentPinNumber ) )
  1190. maxPinNumber = std::max( maxPinNumber, (int) currentPinNumber );
  1191. }
  1192. return maxPinNumber;
  1193. }
  1194. void LIB_SYMBOL::ClearTempFlags()
  1195. {
  1196. for( LIB_ITEM& item : m_drawings )
  1197. item.ClearTempFlags();
  1198. }
  1199. void LIB_SYMBOL::ClearEditFlags()
  1200. {
  1201. for( LIB_ITEM& item : m_drawings )
  1202. item.ClearEditFlags();
  1203. }
  1204. LIB_ITEM* LIB_SYMBOL::LocateDrawItem( int aUnit, int aBodyStyle, KICAD_T aType,
  1205. const VECTOR2I& aPoint )
  1206. {
  1207. for( LIB_ITEM& item : m_drawings )
  1208. {
  1209. if( ( aUnit && item.m_unit && aUnit != item.m_unit )
  1210. || ( aBodyStyle && item.m_bodyStyle && aBodyStyle != item.m_bodyStyle )
  1211. || ( item.Type() != aType && aType != TYPE_NOT_INIT ) )
  1212. {
  1213. continue;
  1214. }
  1215. if( item.HitTest( aPoint ) )
  1216. return &item;
  1217. }
  1218. return nullptr;
  1219. }
  1220. LIB_ITEM* LIB_SYMBOL::LocateDrawItem( int aUnit, int aBodyStyle, KICAD_T aType,
  1221. const VECTOR2I& aPoint, const TRANSFORM& aTransform )
  1222. {
  1223. /* we use LocateDrawItem( int aUnit, int convert, KICAD_T type, const
  1224. * VECTOR2I& pt ) to search items.
  1225. * because this function uses DefaultTransform as orient/mirror matrix
  1226. * we temporary copy aTransform in DefaultTransform
  1227. */
  1228. LIB_ITEM* item;
  1229. TRANSFORM transform = DefaultTransform;
  1230. DefaultTransform = aTransform;
  1231. item = LocateDrawItem( aUnit, aBodyStyle, aType, aPoint );
  1232. // Restore matrix
  1233. DefaultTransform = transform;
  1234. return item;
  1235. }
  1236. INSPECT_RESULT LIB_SYMBOL::Visit( INSPECTOR aInspector, void* aTestData,
  1237. const std::vector<KICAD_T>& aScanTypes )
  1238. {
  1239. // The part itself is never inspected, only its children
  1240. for( LIB_ITEM& item : m_drawings )
  1241. {
  1242. if( item.IsType( aScanTypes ) )
  1243. {
  1244. if( aInspector( &item, aTestData ) == INSPECT_RESULT::QUIT )
  1245. return INSPECT_RESULT::QUIT;
  1246. }
  1247. }
  1248. return INSPECT_RESULT::CONTINUE;
  1249. }
  1250. void LIB_SYMBOL::SetUnitCount( int aCount, bool aDuplicateDrawItems )
  1251. {
  1252. if( m_unitCount == aCount )
  1253. return;
  1254. if( aCount < m_unitCount )
  1255. {
  1256. LIB_ITEMS_CONTAINER::ITERATOR i = m_drawings.begin();
  1257. while( i != m_drawings.end() )
  1258. {
  1259. if( i->m_unit > aCount )
  1260. i = m_drawings.erase( i );
  1261. else
  1262. ++i;
  1263. }
  1264. }
  1265. else if( aDuplicateDrawItems )
  1266. {
  1267. int prevCount = m_unitCount;
  1268. // Temporary storage for new items, as adding new items directly to
  1269. // m_drawings may cause the buffer reallocation which invalidates the
  1270. // iterators
  1271. std::vector< LIB_ITEM* > tmp;
  1272. for( LIB_ITEM& item : m_drawings )
  1273. {
  1274. if( item.m_unit != 1 )
  1275. continue;
  1276. for( int j = prevCount + 1; j <= aCount; j++ )
  1277. {
  1278. LIB_ITEM* newItem = (LIB_ITEM*) item.Duplicate();
  1279. newItem->m_unit = j;
  1280. tmp.push_back( newItem );
  1281. }
  1282. }
  1283. for( LIB_ITEM* item : tmp )
  1284. m_drawings.push_back( item );
  1285. }
  1286. m_drawings.sort();
  1287. m_unitCount = aCount;
  1288. }
  1289. int LIB_SYMBOL::GetUnitCount() const
  1290. {
  1291. if( LIB_SYMBOL_SPTR parent = m_parent.lock() )
  1292. return parent->GetUnitCount();
  1293. return m_unitCount;
  1294. }
  1295. void LIB_SYMBOL::SetHasAlternateBodyStyle( bool aHasAlternate, bool aDuplicatePins )
  1296. {
  1297. if( aHasAlternate == HasAlternateBodyStyle() )
  1298. return;
  1299. // Duplicate items to create the converted shape
  1300. if( aHasAlternate )
  1301. {
  1302. if( aDuplicatePins )
  1303. {
  1304. std::vector< LIB_ITEM* > tmp; // Temporarily store the duplicated pins here.
  1305. for( LIB_ITEM& item : m_drawings )
  1306. {
  1307. // Only pins are duplicated.
  1308. if( item.Type() != LIB_PIN_T )
  1309. continue;
  1310. if( item.m_bodyStyle == 1 )
  1311. {
  1312. LIB_ITEM* newItem = (LIB_ITEM*) item.Duplicate();
  1313. newItem->m_bodyStyle = 2;
  1314. tmp.push_back( newItem );
  1315. }
  1316. }
  1317. // Transfer the new pins to the LIB_SYMBOL.
  1318. for( unsigned i = 0; i < tmp.size(); i++ )
  1319. m_drawings.push_back( tmp[i] );
  1320. }
  1321. }
  1322. else
  1323. {
  1324. // Delete converted shape items because the converted shape does
  1325. // not exist
  1326. LIB_ITEMS_CONTAINER::ITERATOR i = m_drawings.begin();
  1327. while( i != m_drawings.end() )
  1328. {
  1329. if( i->m_bodyStyle > 1 )
  1330. i = m_drawings.erase( i );
  1331. else
  1332. ++i;
  1333. }
  1334. }
  1335. m_drawings.sort();
  1336. }
  1337. std::vector<LIB_ITEM*> LIB_SYMBOL::GetUnitDrawItems( int aUnit, int aBodyStyle )
  1338. {
  1339. std::vector<LIB_ITEM*> unitItems;
  1340. for( LIB_ITEM& item : m_drawings )
  1341. {
  1342. if( item.Type() == LIB_FIELD_T )
  1343. continue;
  1344. if( ( aBodyStyle == -1 && item.GetUnit() == aUnit )
  1345. || ( aUnit == -1 && item.GetBodyStyle() == aBodyStyle )
  1346. || ( aUnit == item.GetUnit() && aBodyStyle == item.GetBodyStyle() ) )
  1347. {
  1348. unitItems.push_back( &item );
  1349. }
  1350. }
  1351. return unitItems;
  1352. }
  1353. std::vector<struct LIB_SYMBOL_UNIT> LIB_SYMBOL::GetUnitDrawItems()
  1354. {
  1355. std::vector<struct LIB_SYMBOL_UNIT> units;
  1356. for( LIB_ITEM& item : m_drawings )
  1357. {
  1358. if( item.Type() == LIB_FIELD_T )
  1359. continue;
  1360. int unit = item.GetUnit();
  1361. int bodyStyle = item.GetBodyStyle();
  1362. auto it = std::find_if( units.begin(), units.end(),
  1363. [unit, bodyStyle]( const LIB_SYMBOL_UNIT& a )
  1364. {
  1365. return a.m_unit == unit && a.m_bodyStyle == bodyStyle;
  1366. } );
  1367. if( it == units.end() )
  1368. {
  1369. struct LIB_SYMBOL_UNIT newUnit;
  1370. newUnit.m_unit = item.GetUnit();
  1371. newUnit.m_bodyStyle = item.GetBodyStyle();
  1372. newUnit.m_items.push_back( &item );
  1373. units.emplace_back( newUnit );
  1374. }
  1375. else
  1376. {
  1377. it->m_items.push_back( &item );
  1378. }
  1379. }
  1380. return units;
  1381. }
  1382. std::vector<struct LIB_SYMBOL_UNIT> LIB_SYMBOL::GetUniqueUnits()
  1383. {
  1384. int unitNum;
  1385. size_t i;
  1386. struct LIB_SYMBOL_UNIT unit;
  1387. std::vector<LIB_ITEM*> compareDrawItems;
  1388. std::vector<LIB_ITEM*> currentDrawItems;
  1389. std::vector<struct LIB_SYMBOL_UNIT> uniqueUnits;
  1390. // The first unit is guaranteed to be unique so always include it.
  1391. unit.m_unit = 1;
  1392. unit.m_bodyStyle = 1;
  1393. unit.m_items = GetUnitDrawItems( 1, 1 );
  1394. // There are no unique units if there are no draw items other than fields.
  1395. if( unit.m_items.size() == 0 )
  1396. return uniqueUnits;
  1397. uniqueUnits.emplace_back( unit );
  1398. if( ( GetUnitCount() == 1 || UnitsLocked() ) && !HasAlternateBodyStyle() )
  1399. return uniqueUnits;
  1400. currentDrawItems = unit.m_items;
  1401. for( unitNum = 2; unitNum <= GetUnitCount(); unitNum++ )
  1402. {
  1403. compareDrawItems = GetUnitDrawItems( unitNum, 1 );
  1404. wxCHECK2_MSG( compareDrawItems.size() != 0, continue,
  1405. "Multiple unit symbol defined with empty units." );
  1406. if( currentDrawItems.size() != compareDrawItems.size() )
  1407. {
  1408. unit.m_unit = unitNum;
  1409. unit.m_bodyStyle = 1;
  1410. unit.m_items = compareDrawItems;
  1411. uniqueUnits.emplace_back( unit );
  1412. }
  1413. else
  1414. {
  1415. for( i = 0; i < currentDrawItems.size(); i++ )
  1416. {
  1417. if( currentDrawItems[i]->compare( *compareDrawItems[i],
  1418. LIB_ITEM::COMPARE_FLAGS::UNIT ) != 0 )
  1419. {
  1420. unit.m_unit = unitNum;
  1421. unit.m_bodyStyle = 1;
  1422. unit.m_items = compareDrawItems;
  1423. uniqueUnits.emplace_back( unit );
  1424. }
  1425. }
  1426. }
  1427. }
  1428. if( HasAlternateBodyStyle() )
  1429. {
  1430. currentDrawItems = GetUnitDrawItems( 1, 2 );
  1431. if( ( GetUnitCount() == 1 || UnitsLocked() ) )
  1432. {
  1433. unit.m_unit = 1;
  1434. unit.m_bodyStyle = 2;
  1435. unit.m_items = currentDrawItems;
  1436. uniqueUnits.emplace_back( unit );
  1437. return uniqueUnits;
  1438. }
  1439. for( unitNum = 2; unitNum <= GetUnitCount(); unitNum++ )
  1440. {
  1441. compareDrawItems = GetUnitDrawItems( unitNum, 2 );
  1442. wxCHECK2_MSG( compareDrawItems.size() != 0, continue,
  1443. "Multiple unit symbol defined with empty units." );
  1444. if( currentDrawItems.size() != compareDrawItems.size() )
  1445. {
  1446. unit.m_unit = unitNum;
  1447. unit.m_bodyStyle = 2;
  1448. unit.m_items = compareDrawItems;
  1449. uniqueUnits.emplace_back( unit );
  1450. }
  1451. else
  1452. {
  1453. for( i = 0; i < currentDrawItems.size(); i++ )
  1454. {
  1455. if( currentDrawItems[i]->compare( *compareDrawItems[i],
  1456. LIB_ITEM::COMPARE_FLAGS::UNIT ) != 0 )
  1457. {
  1458. unit.m_unit = unitNum;
  1459. unit.m_bodyStyle = 2;
  1460. unit.m_items = compareDrawItems;
  1461. uniqueUnits.emplace_back( unit );
  1462. }
  1463. }
  1464. }
  1465. }
  1466. }
  1467. return uniqueUnits;
  1468. }
  1469. bool LIB_SYMBOL::operator==( const LIB_SYMBOL& aOther ) const
  1470. {
  1471. if( m_libId != aOther.m_libId )
  1472. return false;
  1473. if( m_excludedFromBoard != aOther.m_excludedFromBoard )
  1474. return false;
  1475. if( m_excludedFromBOM != aOther.m_excludedFromBOM )
  1476. return false;
  1477. if( m_excludedFromSim != aOther.m_excludedFromSim )
  1478. return false;
  1479. if( m_flags != aOther.m_flags )
  1480. return false;
  1481. if( m_unitCount != aOther.m_unitCount )
  1482. return false;
  1483. if( m_pinNameOffset != aOther.m_pinNameOffset )
  1484. return false;
  1485. if( m_showPinNames != aOther.m_showPinNames )
  1486. return false;
  1487. if( m_showPinNumbers != aOther.m_showPinNumbers )
  1488. return false;
  1489. if( m_drawings.size() != aOther.m_drawings.size() )
  1490. return false;
  1491. for( auto it1 = m_drawings.begin(), it2 = aOther.m_drawings.begin();
  1492. it1 != m_drawings.end(); ++it1, ++it2 )
  1493. {
  1494. if( !( *it1 == *it2 ) )
  1495. return false;
  1496. }
  1497. const LIB_PINS thisPinList = GetAllLibPins();
  1498. const LIB_PINS otherPinList = aOther.GetAllLibPins();
  1499. if( thisPinList.size() != otherPinList.size() )
  1500. return false;
  1501. for( auto it1 = thisPinList.begin(), it2 = otherPinList.begin();
  1502. it1 != thisPinList.end(); ++it1, ++it2 )
  1503. {
  1504. if( !( **it1 == **it2 ) )
  1505. return false;
  1506. }
  1507. for( size_t ii = 0; ii < thisPinList.size(); ++ii )
  1508. {
  1509. if( !( *thisPinList[ii] == *otherPinList[ii] ) )
  1510. return false;
  1511. }
  1512. return true;
  1513. }
  1514. double LIB_SYMBOL::Similarity( const LIB_SYMBOL& aOther ) const
  1515. {
  1516. double similarity = 0.0;
  1517. int totalItems = 0;
  1518. if( m_Uuid == aOther.m_Uuid )
  1519. return 1.0;
  1520. for( const LIB_ITEM& item : m_drawings )
  1521. {
  1522. totalItems += 1;
  1523. double max_similarity = 0.0;
  1524. for( const LIB_ITEM& otherItem : aOther.m_drawings )
  1525. {
  1526. double temp_similarity = item.Similarity( otherItem );
  1527. max_similarity = std::max( max_similarity, temp_similarity );
  1528. if( max_similarity == 1.0 )
  1529. break;
  1530. }
  1531. similarity += max_similarity;
  1532. }
  1533. for( const LIB_PIN* pin : GetAllLibPins() )
  1534. {
  1535. totalItems += 1;
  1536. double max_similarity = 0.0;
  1537. for( const LIB_PIN* otherPin : aOther.GetAllLibPins() )
  1538. {
  1539. double temp_similarity = pin->Similarity( *otherPin );
  1540. max_similarity = std::max( max_similarity, temp_similarity );
  1541. if( max_similarity == 1.0 )
  1542. break;
  1543. }
  1544. similarity += max_similarity;
  1545. }
  1546. if( totalItems == 0 )
  1547. similarity = 0.0;
  1548. else
  1549. similarity /= totalItems;
  1550. if( m_excludedFromBoard != aOther.m_excludedFromBoard )
  1551. similarity *= 0.9;
  1552. if( m_excludedFromBOM != aOther.m_excludedFromBOM )
  1553. similarity *= 0.9;
  1554. if( m_excludedFromSim != aOther.m_excludedFromSim )
  1555. similarity *= 0.9;
  1556. if( m_flags != aOther.m_flags )
  1557. similarity *= 0.9;
  1558. if( m_unitCount != aOther.m_unitCount )
  1559. similarity *= 0.5;
  1560. if( m_pinNameOffset != aOther.m_pinNameOffset )
  1561. similarity *= 0.9;
  1562. if( m_showPinNames != aOther.m_showPinNames )
  1563. similarity *= 0.9;
  1564. if( m_showPinNumbers != aOther.m_showPinNumbers )
  1565. similarity *= 0.9;
  1566. return similarity;
  1567. }