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.

1214 lines
31 KiB

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