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.

1193 lines
30 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.IsNew() )
  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. wxASSERT( aItem != NULL );
  435. m_drawings.push_back( aItem );
  436. }
  437. LIB_ITEM* LIB_PART::GetNextDrawItem( LIB_ITEM* aItem, KICAD_T aType )
  438. {
  439. if( m_drawings.empty( aType ) )
  440. return NULL;
  441. if( aItem == NULL )
  442. return &( *( m_drawings.begin( aType ) ) );
  443. // Search for the last item, assume aItem is of type aType
  444. wxASSERT( ( aType == TYPE_NOT_INIT ) || ( aType == aItem->Type() ) );
  445. LIB_ITEMS_CONTAINER::ITERATOR it = m_drawings.begin( aType );
  446. while( ( it != m_drawings.end( aType ) ) && ( aItem != &( *it ) ) )
  447. ++it;
  448. // Search the next item
  449. if( it != m_drawings.end( aType ) )
  450. {
  451. ++it;
  452. if( it != m_drawings.end( aType ) )
  453. return &( *it );
  454. }
  455. return NULL;
  456. }
  457. void LIB_PART::GetPins( LIB_PINS& aList, int aUnit, int aConvert )
  458. {
  459. if( m_drawings.empty( LIB_PIN_T ) )
  460. return;
  461. /* Notes:
  462. * when aUnit == 0: no unit filtering
  463. * when aConvert == 0: no convert (shape selection) filtering
  464. * when .m_Unit == 0, the body item is common to units
  465. * when .m_Convert == 0, the body item is common to shapes
  466. */
  467. for( LIB_ITEM& item : m_drawings[ LIB_PIN_T ] )
  468. {
  469. // Unit filtering:
  470. if( aUnit && item.m_Unit && ( item.m_Unit != aUnit ) )
  471. continue;
  472. // Shape filtering:
  473. if( aConvert && item.m_Convert && ( item.m_Convert != aConvert ) )
  474. continue;
  475. aList.push_back( (LIB_PIN*) &item );
  476. }
  477. }
  478. LIB_PIN* LIB_PART::GetPin( const wxString& aNumber, int aUnit, int aConvert )
  479. {
  480. LIB_PINS pinList;
  481. GetPins( pinList, aUnit, aConvert );
  482. for( size_t i = 0; i < pinList.size(); i++ )
  483. {
  484. wxASSERT( pinList[i]->Type() == LIB_PIN_T );
  485. if( aNumber == pinList[i]->GetNumber() )
  486. return pinList[i];
  487. }
  488. return NULL;
  489. }
  490. bool LIB_PART::PinsConflictWith( LIB_PART& aOtherPart, bool aTestNums, bool aTestNames,
  491. bool aTestType, bool aTestOrientation, bool aTestLength )
  492. {
  493. LIB_PINS thisPinList;
  494. GetPins( thisPinList, /* aUnit */ 0, /* aConvert */ 0 );
  495. for( LIB_PIN* eachThisPin : thisPinList )
  496. {
  497. wxASSERT( eachThisPin );
  498. LIB_PINS otherPinList;
  499. aOtherPart.GetPins( otherPinList, /* aUnit */ 0, /* aConvert */ 0 );
  500. bool foundMatch = false;
  501. for( LIB_PIN* eachOtherPin : otherPinList )
  502. {
  503. wxASSERT( eachOtherPin );
  504. // Same position?
  505. if( eachThisPin->GetPosition() != eachOtherPin->GetPosition() )
  506. continue;
  507. // Same number?
  508. if( aTestNums && ( eachThisPin->GetNumber() != eachOtherPin->GetNumber() ))
  509. continue;
  510. // Same name?
  511. if( aTestNames && ( eachThisPin->GetName() != eachOtherPin->GetName() ))
  512. continue;
  513. // Same electrical type?
  514. if( aTestType && ( eachThisPin->GetType() != eachOtherPin->GetType() ))
  515. continue;
  516. // Same orientation?
  517. if( aTestOrientation && ( eachThisPin->GetOrientation() != eachOtherPin->GetOrientation() ))
  518. continue;
  519. // Same length?
  520. if( aTestLength && ( eachThisPin->GetLength() != eachOtherPin->GetLength() ))
  521. continue;
  522. foundMatch = true;
  523. }
  524. if( !foundMatch )
  525. {
  526. // This means there was not an identical (according to the arguments)
  527. // pin at the same position in the other component.
  528. return true;
  529. }
  530. }
  531. // The loop never gave up, so no conflicts were found.
  532. return false;
  533. }
  534. const EDA_RECT LIB_PART::GetUnitBoundingBox( int aUnit, int aConvert ) const
  535. {
  536. EDA_RECT bBox;
  537. bool initialized = false;
  538. for( const LIB_ITEM& item : m_drawings )
  539. {
  540. if( ( item.m_Unit > 0 ) && ( ( m_unitCount > 1 ) && ( aUnit > 0 )
  541. && ( aUnit != item.m_Unit ) ) )
  542. continue;
  543. if( item.m_Convert > 0 && ( ( aConvert > 0 ) && ( aConvert != item.m_Convert ) ) )
  544. continue;
  545. if ( ( item.Type() == LIB_FIELD_T ) && !( ( LIB_FIELD& ) item ).IsVisible() )
  546. continue;
  547. if( initialized )
  548. bBox.Merge( item.GetBoundingBox() );
  549. else
  550. {
  551. bBox = item.GetBoundingBox();
  552. initialized = true;
  553. }
  554. }
  555. return bBox;
  556. }
  557. void LIB_PART::ViewGetLayers( int aLayers[], int& aCount ) const
  558. {
  559. aCount = 2;
  560. aLayers[0] = LAYER_DEVICE;
  561. aLayers[1] = LAYER_DEVICE_BACKGROUND;
  562. }
  563. const EDA_RECT LIB_PART::GetBodyBoundingBox( int aUnit, int aConvert ) const
  564. {
  565. EDA_RECT bBox;
  566. bool initialized = false;
  567. for( const LIB_ITEM& item : m_drawings )
  568. {
  569. if( ( item.m_Unit > 0 ) && ( ( m_unitCount > 1 ) && ( aUnit > 0 )
  570. && ( aUnit != item.m_Unit ) ) )
  571. continue;
  572. if( item.m_Convert > 0 && ( ( aConvert > 0 ) && ( aConvert != item.m_Convert ) ) )
  573. continue;
  574. if( item.Type() == LIB_FIELD_T )
  575. continue;
  576. if( initialized )
  577. bBox.Merge( item.GetBoundingBox() );
  578. else
  579. {
  580. bBox = item.GetBoundingBox();
  581. initialized = true;
  582. }
  583. }
  584. return bBox;
  585. }
  586. void LIB_PART::deleteAllFields()
  587. {
  588. m_drawings[ LIB_FIELD_T ].clear();
  589. }
  590. void LIB_PART::SetFields( const std::vector <LIB_FIELD>& aFields )
  591. {
  592. deleteAllFields();
  593. for( unsigned i=0; i<aFields.size(); ++i )
  594. {
  595. // drawings is a ptr_vector, new and copy an object on the heap.
  596. LIB_FIELD* field = new LIB_FIELD( aFields[i] );
  597. field->SetParent( this );
  598. m_drawings.push_back( field );
  599. }
  600. }
  601. void LIB_PART::GetFields( LIB_FIELDS& aList )
  602. {
  603. LIB_FIELD* field;
  604. // Grab the MANDATORY_FIELDS first, in expected order given by
  605. // enum NumFieldType
  606. for( int id=0; id<MANDATORY_FIELDS; ++id )
  607. {
  608. field = GetField( id );
  609. // the MANDATORY_FIELDS are exactly that in RAM.
  610. wxASSERT( field );
  611. aList.push_back( *field );
  612. }
  613. // Now grab all the rest of fields.
  614. for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  615. {
  616. field = ( LIB_FIELD* ) &item;
  617. if( (unsigned) field->GetId() < MANDATORY_FIELDS )
  618. continue; // was added above
  619. aList.push_back( *field );
  620. }
  621. }
  622. LIB_FIELD* LIB_PART::GetField( int aId )
  623. {
  624. for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  625. {
  626. LIB_FIELD* field = ( LIB_FIELD* ) &item;
  627. if( field->GetId() == aId )
  628. return field;
  629. }
  630. return NULL;
  631. }
  632. LIB_FIELD* LIB_PART::FindField( const wxString& aFieldName )
  633. {
  634. for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  635. {
  636. LIB_FIELD* field = ( LIB_FIELD* ) &item;
  637. if( field->GetName() == aFieldName )
  638. return field;
  639. }
  640. return NULL;
  641. }
  642. LIB_FIELD& LIB_PART::GetValueField()
  643. {
  644. LIB_FIELD* field = GetField( VALUE );
  645. wxASSERT( field != NULL );
  646. return *field;
  647. }
  648. LIB_FIELD& LIB_PART::GetReferenceField()
  649. {
  650. LIB_FIELD* field = GetField( REFERENCE );
  651. wxASSERT( field != NULL );
  652. return *field;
  653. }
  654. LIB_FIELD& LIB_PART::GetFootprintField()
  655. {
  656. LIB_FIELD* field = GetField( FOOTPRINT );
  657. wxASSERT( field != NULL );
  658. return *field;
  659. }
  660. bool LIB_PART::SaveDateAndTime( OUTPUTFORMATTER& aFormatter )
  661. {
  662. int year, mon, day, hour, min, sec;
  663. if( m_dateLastEdition == 0 )
  664. return true;
  665. sec = m_dateLastEdition & 63;
  666. min = ( m_dateLastEdition >> 6 ) & 63;
  667. hour = ( m_dateLastEdition >> 12 ) & 31;
  668. day = ( m_dateLastEdition >> 17 ) & 31;
  669. mon = ( m_dateLastEdition >> 22 ) & 15;
  670. year = ( m_dateLastEdition >> 26 ) + 1990;
  671. aFormatter.Print( 0, "Ti %d/%d/%d %d:%d:%d\n", year, mon, day, hour, min, sec );
  672. return true;
  673. }
  674. bool LIB_PART::LoadDateAndTime( char* aLine )
  675. {
  676. int year, mon, day, hour, min, sec;
  677. year = mon = day = hour = min = sec = 0;
  678. strtok( aLine, " \r\t\n" );
  679. strtok( NULL, " \r\t\n" );
  680. if( sscanf( aLine, "%d/%d/%d %d:%d:%d", &year, &mon, &day, &hour, &min, &sec ) != 6 )
  681. return false;
  682. m_dateLastEdition = ( sec & 63 ) + ( ( min & 63 ) << 6 ) +
  683. ( ( hour & 31 ) << 12 ) + ( ( day & 31 ) << 17 ) +
  684. ( ( mon & 15 ) << 22 ) + ( ( year - 1990 ) << 26 );
  685. return true;
  686. }
  687. void LIB_PART::SetOffset( const wxPoint& aOffset )
  688. {
  689. for( LIB_ITEM& item : m_drawings )
  690. item.SetOffset( aOffset );
  691. }
  692. void LIB_PART::RemoveDuplicateDrawItems()
  693. {
  694. m_drawings.unique();
  695. }
  696. bool LIB_PART::HasConversion() const
  697. {
  698. for( const LIB_ITEM& item : m_drawings )
  699. {
  700. if( item.m_Convert > LIB_ITEM::LIB_CONVERT::BASE )
  701. return true;
  702. }
  703. return false;
  704. }
  705. void LIB_PART::ClearStatus()
  706. {
  707. for( LIB_ITEM& item : m_drawings )
  708. {
  709. item.m_Flags = 0;
  710. }
  711. }
  712. LIB_ITEM* LIB_PART::LocateDrawItem( int aUnit, int aConvert,
  713. KICAD_T aType, const wxPoint& aPoint )
  714. {
  715. for( LIB_ITEM& item : m_drawings )
  716. {
  717. if( ( aUnit && item.m_Unit && ( aUnit != item.m_Unit) )
  718. || ( aConvert && item.m_Convert && ( aConvert != item.m_Convert ) )
  719. || ( ( item.Type() != aType ) && ( aType != TYPE_NOT_INIT ) ) )
  720. continue;
  721. if( item.HitTest( aPoint ) )
  722. return &item;
  723. }
  724. return NULL;
  725. }
  726. LIB_ITEM* LIB_PART::LocateDrawItem( int aUnit, int aConvert, KICAD_T aType,
  727. const wxPoint& aPoint, const TRANSFORM& aTransform )
  728. {
  729. /* we use LocateDrawItem( int aUnit, int convert, KICAD_T type, const
  730. * wxPoint& pt ) to search items.
  731. * because this function uses DefaultTransform as orient/mirror matrix
  732. * we temporary copy aTransform in DefaultTransform
  733. */
  734. LIB_ITEM* item;
  735. TRANSFORM transform = DefaultTransform;
  736. DefaultTransform = aTransform;
  737. item = LocateDrawItem( aUnit, aConvert, aType, aPoint );
  738. // Restore matrix
  739. DefaultTransform = transform;
  740. return item;
  741. }
  742. void LIB_PART::SetUnitCount( int aCount )
  743. {
  744. if( m_unitCount == aCount )
  745. return;
  746. if( aCount < m_unitCount )
  747. {
  748. LIB_ITEMS_CONTAINER::ITERATOR i = m_drawings.begin();
  749. while( i != m_drawings.end() )
  750. {
  751. if( i->m_Unit > aCount )
  752. i = m_drawings.erase( i );
  753. else
  754. ++i;
  755. }
  756. }
  757. else
  758. {
  759. int prevCount = m_unitCount;
  760. // Temporary storage for new items, as adding new items directly to
  761. // m_drawings may cause the buffer reallocation which invalidates the
  762. // iterators
  763. std::vector< LIB_ITEM* > tmp;
  764. for( LIB_ITEM& item : m_drawings )
  765. {
  766. if( item.m_Unit != 1 )
  767. continue;
  768. for( int j = prevCount + 1; j <= aCount; j++ )
  769. {
  770. LIB_ITEM* newItem = (LIB_ITEM*) item.Clone();
  771. newItem->m_Unit = j;
  772. tmp.push_back( newItem );
  773. }
  774. }
  775. for( auto item : tmp )
  776. m_drawings.push_back( item );
  777. }
  778. m_unitCount = aCount;
  779. }
  780. void LIB_PART::SetConversion( bool aSetConvert )
  781. {
  782. if( aSetConvert == HasConversion() )
  783. return;
  784. // Duplicate items to create the converted shape
  785. if( aSetConvert )
  786. {
  787. std::vector< LIB_ITEM* > tmp; // Temporarily store the duplicated pins here.
  788. for( LIB_ITEM& item : m_drawings )
  789. {
  790. // Only pins are duplicated.
  791. if( item.Type() != LIB_PIN_T )
  792. continue;
  793. if( item.m_Convert == 1 )
  794. {
  795. LIB_ITEM* newItem = (LIB_ITEM*) item.Clone();
  796. newItem->m_Convert = 2;
  797. tmp.push_back( newItem );
  798. }
  799. }
  800. // Transfer the new pins to the LIB_PART.
  801. for( unsigned i = 0; i < tmp.size(); i++ )
  802. m_drawings.push_back( tmp[i] );
  803. }
  804. else
  805. {
  806. // Delete converted shape items because the converted shape does
  807. // not exist
  808. LIB_ITEMS_CONTAINER::ITERATOR i = m_drawings.begin();
  809. while( i != m_drawings.end() )
  810. {
  811. if( i->m_Convert > 1 )
  812. i = m_drawings.erase( i );
  813. else
  814. ++i;
  815. }
  816. }
  817. }
  818. wxArrayString LIB_PART::GetAliasNames( bool aIncludeRoot ) const
  819. {
  820. wxArrayString names;
  821. LIB_ALIASES::const_iterator it;
  822. for( it=m_aliases.begin(); it != m_aliases.end(); ++it )
  823. {
  824. if( !aIncludeRoot && (*it)->IsRoot() )
  825. continue;
  826. names.Add( (*it)->GetName() );
  827. }
  828. return names;
  829. }
  830. bool LIB_PART::HasAlias( const wxString& aName ) const
  831. {
  832. wxCHECK2_MSG( !aName.IsEmpty(), return false,
  833. wxT( "Cannot get alias with an empty name, bad programmer." ) );
  834. for( size_t i = 0; i < m_aliases.size(); i++ )
  835. {
  836. if( aName == m_aliases[i]->GetName() )
  837. return true;
  838. }
  839. return false;
  840. }
  841. void LIB_PART::RemoveAlias( const wxString& aName )
  842. {
  843. LIB_ALIAS* a = GetAlias( aName );
  844. if( a )
  845. RemoveAlias( a );
  846. }
  847. LIB_ALIAS* LIB_PART::RemoveAlias( LIB_ALIAS* aAlias )
  848. {
  849. wxCHECK_MSG( aAlias, NULL, wxT( "Cannot remove alias by NULL pointer." ) );
  850. LIB_ALIAS* nextAlias = NULL;
  851. LIB_ALIASES::iterator it = find( m_aliases.begin(), m_aliases.end(), aAlias );
  852. if( it != m_aliases.end() )
  853. {
  854. bool rename = aAlias->IsRoot();
  855. wxLogTrace( traceSchLibMem,
  856. wxT( "%s: symbol:'%s', alias:'%s', alias count %llu, reference count %ld." ),
  857. GetChars( wxString::FromAscii( __WXFUNCTION__ ) ),
  858. GetChars( GetName() ),
  859. GetChars( aAlias->GetName() ),
  860. (long long unsigned) m_aliases.size(),
  861. m_me.use_count() );
  862. it = m_aliases.erase( it );
  863. if( !m_aliases.empty() )
  864. {
  865. if( it == m_aliases.end() )
  866. it = m_aliases.begin();
  867. nextAlias = *it;
  868. if( rename )
  869. SetName( nextAlias->GetName() );
  870. }
  871. }
  872. return nextAlias;
  873. }
  874. void LIB_PART::RemoveAllAliases()
  875. {
  876. // Remove all of the aliases except the root alias.
  877. while( m_aliases.size() > 1 )
  878. m_aliases.pop_back();
  879. }
  880. LIB_ALIAS* LIB_PART::GetAlias( const wxString& aName )
  881. {
  882. wxCHECK2_MSG( !aName.IsEmpty(), return NULL,
  883. wxT( "Cannot get alias with an empty name. Bad programmer!" ) );
  884. for( size_t i = 0; i < m_aliases.size(); i++ )
  885. {
  886. if( aName == m_aliases[i]->GetName() )
  887. return m_aliases[i];
  888. }
  889. return NULL;
  890. }
  891. LIB_ALIAS* LIB_PART::GetAlias( size_t aIndex )
  892. {
  893. wxCHECK2_MSG( aIndex < m_aliases.size(), return NULL,
  894. wxT( "Illegal alias list index, bad programmer." ) );
  895. return m_aliases[aIndex];
  896. }
  897. void LIB_PART::AddAlias( const wxString& aName )
  898. {
  899. m_aliases.push_back( new LIB_ALIAS( aName, this ) );
  900. }
  901. void LIB_PART::AddAlias( LIB_ALIAS* aAlias )
  902. {
  903. m_aliases.push_back( aAlias );
  904. }
  905. void LIB_PART::SetSubpartIdNotation( int aSep, int aFirstId )
  906. {
  907. m_subpartFirstId = 'A';
  908. m_subpartIdSeparator = 0;
  909. if( aSep == '.' || aSep == '-' || aSep == '_' )
  910. m_subpartIdSeparator = aSep;
  911. if( aFirstId == '1' && aSep != 0 )
  912. m_subpartFirstId = aFirstId;
  913. }