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.

1914 lines
49 KiB

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