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.

1258 lines
33 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-2018 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. wxString LIB_ALIAS::GetSearchText()
  125. {
  126. // Matches are scored by offset from front of string, so inclusion of this spacer
  127. // discounts matches found after it.
  128. static const wxString discount( wxT( " " ) );
  129. wxString text = GetKeyWords() + discount + GetDescription();
  130. // If a footprint is defined for the part, add it to the serach string
  131. if( shared )
  132. {
  133. wxString footprint = shared->GetFootprintField().GetText();
  134. if( !footprint.IsEmpty() )
  135. text += discount + footprint;
  136. }
  137. return text;
  138. }
  139. bool LIB_ALIAS::operator==( const wxChar* aName ) const
  140. {
  141. return name == aName;
  142. }
  143. bool operator<( const LIB_ALIAS& aItem1, const LIB_ALIAS& aItem2 )
  144. {
  145. return aItem1.GetName() < aItem2.GetName();
  146. }
  147. void LIB_ALIAS::ViewGetLayers( int aLayers[], int& aCount ) const
  148. {
  149. // An alias's fields don't know how to fetch their parent's values so we don't let
  150. // them draw themselves. This means the alias always has to draw them, which means
  151. // it has to "own" their layers as well.
  152. aCount = 5;
  153. aLayers[0] = LAYER_DEVICE;
  154. aLayers[1] = LAYER_DEVICE_BACKGROUND;
  155. aLayers[2] = LAYER_REFERENCEPART;
  156. aLayers[3] = LAYER_VALUEPART;
  157. aLayers[4] = LAYER_FIELDS;
  158. }
  159. /// http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/sp_techniques.html#weak_without_shared
  160. struct null_deleter
  161. {
  162. void operator()(void const *) const
  163. {
  164. }
  165. };
  166. LIB_PART::LIB_PART( const wxString& aName, PART_LIB* aLibrary ) :
  167. EDA_ITEM( LIB_PART_T ),
  168. m_me( this, null_deleter() )
  169. {
  170. m_dateLastEdition = 0;
  171. m_unitCount = 1;
  172. m_pinNameOffset = 40;
  173. m_options = ENTRY_NORMAL;
  174. m_unitsLocked = false;
  175. m_showPinNumbers = true;
  176. m_showPinNames = true;
  177. // Add the MANDATORY_FIELDS in RAM only. These are assumed to be present
  178. // when the field editors are invoked.
  179. m_drawings[LIB_FIELD_T].reserve( 4 );
  180. m_drawings[LIB_FIELD_T].push_back( new LIB_FIELD( this, VALUE ) );
  181. m_drawings[LIB_FIELD_T].push_back( new LIB_FIELD( this, REFERENCE ) );
  182. m_drawings[LIB_FIELD_T].push_back( new LIB_FIELD( this, FOOTPRINT ) );
  183. m_drawings[LIB_FIELD_T].push_back( new LIB_FIELD( this, DATASHEET ) );
  184. SetLib( aLibrary );
  185. SetName( aName );
  186. }
  187. LIB_PART::LIB_PART( LIB_PART& aPart, PART_LIB* aLibrary ) :
  188. EDA_ITEM( aPart ),
  189. m_me( this, null_deleter() )
  190. {
  191. LIB_ITEM* newItem;
  192. m_library = aLibrary;
  193. m_FootprintList = aPart.m_FootprintList;
  194. m_unitCount = aPart.m_unitCount;
  195. m_unitsLocked = aPart.m_unitsLocked;
  196. m_pinNameOffset = aPart.m_pinNameOffset;
  197. m_showPinNumbers = aPart.m_showPinNumbers;
  198. m_showPinNames = aPart.m_showPinNames;
  199. m_dateLastEdition = aPart.m_dateLastEdition;
  200. m_options = aPart.m_options;
  201. m_libId = aPart.m_libId;
  202. for( LIB_ITEM& oldItem : aPart.m_drawings )
  203. {
  204. if( oldItem.IsNew() )
  205. continue;
  206. newItem = (LIB_ITEM*) oldItem.Clone();
  207. newItem->SetParent( this );
  208. m_drawings.push_back( newItem );
  209. }
  210. for( size_t i = 0; i < aPart.m_aliases.size(); i++ )
  211. {
  212. LIB_ALIAS* alias = new LIB_ALIAS( *aPart.m_aliases[i], this );
  213. m_aliases.push_back( alias );
  214. }
  215. }
  216. LIB_PART::~LIB_PART()
  217. {
  218. wxLogTrace( traceSchLibMem,
  219. wxT( "%s: destroying symbol with alias list count of %llu" ),
  220. GetChars( wxString::FromAscii( __WXFUNCTION__ ) ),
  221. (long long unsigned) m_aliases.size() );
  222. // If the part is being deleted directly rather than through the library,
  223. // delete all of the aliases.
  224. while( m_aliases.size() )
  225. {
  226. LIB_ALIAS* alias = m_aliases.back();
  227. m_aliases.pop_back();
  228. delete alias;
  229. }
  230. }
  231. const wxString LIB_PART::GetLibraryName()
  232. {
  233. if( m_library )
  234. return m_library->GetName();
  235. return m_libId.GetLibNickname();
  236. }
  237. wxString LIB_PART::SubReference( int aUnit, bool aAddSeparator )
  238. {
  239. wxString subRef;
  240. if( m_subpartIdSeparator != 0 && aAddSeparator )
  241. subRef << wxChar( m_subpartIdSeparator );
  242. if( m_subpartFirstId >= '0' && m_subpartFirstId <= '9' )
  243. subRef << aUnit;
  244. else
  245. {
  246. // use letters as notation. To allow more than 26 units, the sub ref
  247. // use one letter if letter = A .. Z or a ... z, and 2 letters otherwise
  248. // first letter is expected to be 'A' or 'a' (i.e. 26 letters are available)
  249. int u;
  250. aUnit -= 1; // Unit number starts to 1. now to 0.
  251. while( aUnit >= 26 ) // more than one letter are needed
  252. {
  253. u = aUnit / 26;
  254. subRef << wxChar( m_subpartFirstId + u -1 );
  255. aUnit %= 26;
  256. }
  257. u = m_subpartFirstId + aUnit;
  258. subRef << wxChar( u );
  259. }
  260. return subRef;
  261. }
  262. const wxString& LIB_PART::GetName() const
  263. {
  264. static wxString dummy;
  265. wxCHECK_MSG( m_aliases.size(), dummy, "no aliases defined for symbol" );
  266. return m_aliases[0]->GetName();
  267. }
  268. void LIB_PART::SetName( const wxString& aName )
  269. {
  270. // The LIB_ALIAS that is the LIB_PART name has to be created so create it.
  271. if( m_aliases.empty() )
  272. m_aliases.push_back( new LIB_ALIAS( aName, this ) );
  273. else
  274. m_aliases[0]->SetName( aName );
  275. wxString validatedName = LIB_ID::FixIllegalChars( aName, LIB_ID::ID_SCH );
  276. m_libId.SetLibItemName( validatedName, false );
  277. GetValueField().SetText( validatedName );
  278. }
  279. void LIB_PART::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDc, const wxPoint& aOffset,
  280. int aMulti, int aConvert, const PART_DRAW_OPTIONS& aOpts )
  281. {
  282. BASE_SCREEN* screen = aPanel ? aPanel->GetScreen() : NULL;
  283. GRSetDrawMode( aDc, aOpts.draw_mode );
  284. /* draw background for filled items using background option
  285. * Solid lines will be drawn after the background
  286. * Note also, background is not drawn when:
  287. * printing in black and white
  288. * If the color is not the default color (aColor != -1 )
  289. */
  290. if( ! ( screen && screen->m_IsPrinting && GetGRForceBlackPenState() )
  291. && ( aOpts.color == COLOR4D::UNSPECIFIED ) )
  292. {
  293. for( LIB_ITEM& drawItem : m_drawings )
  294. {
  295. if( drawItem.m_Fill != FILLED_WITH_BG_BODYCOLOR )
  296. continue;
  297. if( aOpts.only_selected && !drawItem.IsSelected() )
  298. continue;
  299. // Do not draw an item while moving (the cursor handler does that)
  300. if( drawItem.m_Flags & IS_MOVED )
  301. continue;
  302. // Do not draw items not attached to the current part
  303. if( aMulti && drawItem.m_Unit && ( drawItem.m_Unit != aMulti ) )
  304. continue;
  305. if( aConvert && drawItem.m_Convert && ( drawItem.m_Convert != aConvert ) )
  306. continue;
  307. if( drawItem.Type() == LIB_FIELD_T )
  308. continue;
  309. if( drawItem.Type() == LIB_FIELD_T ) // TODO dead code?
  310. {
  311. drawItem.Draw( aPanel, aDc, aOffset, aOpts.color,
  312. aOpts.draw_mode, (void*) NULL, aOpts.transform );
  313. }
  314. // Now, draw only the background for items with
  315. // m_Fill == FILLED_WITH_BG_BODYCOLOR:
  316. drawItem.Draw( aPanel, aDc, aOffset, aOpts.color,
  317. aOpts.draw_mode, (void*) false, aOpts.transform );
  318. }
  319. }
  320. // Track the index into the dangling pins list
  321. size_t pin_index = 0;
  322. for( LIB_ITEM& drawItem : m_drawings )
  323. {
  324. if( aOpts.only_selected && !drawItem.IsSelected() )
  325. continue;
  326. // Do not draw an item while moving (the cursor handler does that)
  327. if( drawItem.m_Flags & IS_MOVED )
  328. continue;
  329. // Do not draw items not attached to the current part
  330. if( aMulti && drawItem.m_Unit && ( drawItem.m_Unit != aMulti ) )
  331. continue;
  332. if( aConvert && drawItem.m_Convert && ( drawItem.m_Convert != aConvert ) )
  333. continue;
  334. if( drawItem.Type() == LIB_FIELD_T )
  335. {
  336. LIB_FIELD& field = static_cast<LIB_FIELD&>( drawItem );
  337. if( field.IsVisible() && !aOpts.draw_visible_fields )
  338. continue;
  339. if( !field.IsVisible() && !aOpts.draw_hidden_fields )
  340. continue;
  341. }
  342. if( drawItem.Type() == LIB_PIN_T )
  343. {
  344. LIB_PIN& pin = static_cast<LIB_PIN&>( drawItem );
  345. uintptr_t flags = 0;
  346. if( aOpts.show_pin_text )
  347. flags |= PIN_DRAW_TEXTS;
  348. if( aOpts.show_elec_type )
  349. flags |= PIN_DRAW_ELECTRICAL_TYPE_NAME;
  350. if( aOpts.PinIsDangling( pin_index ) )
  351. flags |= PIN_DRAW_DANGLING;
  352. if( pin.IsPowerConnection() && IsPower() )
  353. flags |= PIN_DANGLING_HIDDEN;
  354. drawItem.Draw( aPanel, aDc, aOffset, aOpts.color,
  355. aOpts.draw_mode, (void*) flags, aOpts.transform );
  356. ++pin_index;
  357. }
  358. else if( drawItem.Type() == LIB_FIELD_T )
  359. {
  360. drawItem.Draw( aPanel, aDc, aOffset, aOpts.color,
  361. aOpts.draw_mode, (void*) NULL, aOpts.transform );
  362. }
  363. else
  364. {
  365. bool forceNoFill = drawItem.m_Fill == FILLED_WITH_BG_BODYCOLOR;
  366. drawItem.Draw( aPanel, aDc, aOffset, aOpts.color,
  367. aOpts.draw_mode, (void*) forceNoFill,
  368. aOpts.transform );
  369. }
  370. }
  371. // Enable this to draw the anchor of the component.
  372. #if 0
  373. int len = aDc->DeviceToLogicalXRel( 3 );
  374. EDA_RECT* const clipbox = aPanel ? aPanel->GetClipBox() : NULL;
  375. GRLine( clipbox, aDc, aOffset.x, aOffset.y - len, aOffset.x,
  376. aOffset.y + len, 0, aOpts.color );
  377. GRLine( clipbox, aDc, aOffset.x - len, aOffset.y, aOffset.x + len,
  378. aOffset.y, 0, aOpts.color );
  379. #endif
  380. /* Enable this to draw the bounding box around the component to validate
  381. * the bounding box calculations. */
  382. #if 0
  383. EDA_RECT bBox = GetUnitBoundingBox( aMulti, aConvert );
  384. bBox.RevertYAxis();
  385. bBox = aOpts.transform.TransformCoordinate( bBox );
  386. bBox.Move( aOffset );
  387. GRRect( aPanel ? aPanel->GetClipBox() : NULL, aDc, bBox, 0, LIGHTMAGENTA );
  388. #endif
  389. }
  390. void LIB_PART::Plot( PLOTTER* aPlotter, int aUnit, int aConvert,
  391. const wxPoint& aOffset, const TRANSFORM& aTransform )
  392. {
  393. wxASSERT( aPlotter != NULL );
  394. aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
  395. bool fill = aPlotter->GetColorMode();
  396. // draw background for filled items using background option
  397. // Solid lines will be drawn after the background
  398. for( LIB_ITEM& item : m_drawings )
  399. {
  400. // Lib Fields are not plotted here, because this plot function
  401. // is used to plot schematic items, which have they own fields
  402. if( item.Type() == LIB_FIELD_T )
  403. continue;
  404. if( aUnit && item.m_Unit && ( item.m_Unit != aUnit ) )
  405. continue;
  406. if( aConvert && item.m_Convert && ( item.m_Convert != aConvert ) )
  407. continue;
  408. if( item.m_Fill == FILLED_WITH_BG_BODYCOLOR )
  409. item.Plot( aPlotter, aOffset, fill, aTransform );
  410. }
  411. // Not filled items and filled shapes are now plotted
  412. // (plot only items which are not already plotted)
  413. for( LIB_ITEM& item : m_drawings )
  414. {
  415. if( item.Type() == LIB_FIELD_T )
  416. continue;
  417. if( aUnit && item.m_Unit && ( item.m_Unit != aUnit ) )
  418. continue;
  419. if( aConvert && item.m_Convert && ( item.m_Convert != aConvert ) )
  420. continue;
  421. if( item.m_Fill != FILLED_WITH_BG_BODYCOLOR )
  422. item.Plot( aPlotter, aOffset, fill, aTransform );
  423. }
  424. }
  425. void LIB_PART::PlotLibFields( PLOTTER* aPlotter, int aUnit, int aConvert,
  426. const wxPoint& aOffset, const TRANSFORM& aTransform )
  427. {
  428. wxASSERT( aPlotter != NULL );
  429. aPlotter->SetColor( GetLayerColor( LAYER_FIELDS ) );
  430. bool fill = aPlotter->GetColorMode();
  431. for( LIB_ITEM& item : m_drawings )
  432. {
  433. if( item.Type() != LIB_FIELD_T )
  434. continue;
  435. if( aUnit && item.m_Unit && ( item.m_Unit != aUnit ) )
  436. continue;
  437. if( aConvert && item.m_Convert && ( item.m_Convert != aConvert ) )
  438. continue;
  439. // The reference is a special case: we should change the basic text
  440. // to add '?' and the part id
  441. LIB_FIELD& field = (LIB_FIELD&) item;
  442. wxString tmp = field.GetShownText();
  443. if( field.GetId() == REFERENCE )
  444. {
  445. wxString text = field.GetFullText( aUnit );
  446. field.SetText( text );
  447. }
  448. item.Plot( aPlotter, aOffset, fill, aTransform );
  449. field.SetText( tmp );
  450. }
  451. }
  452. void LIB_PART::RemoveDrawItem( LIB_ITEM* aItem, EDA_DRAW_PANEL* aPanel, wxDC* aDc )
  453. {
  454. wxASSERT( aItem != NULL );
  455. // none of the MANDATORY_FIELDS may be removed in RAM, but they may be
  456. // omitted when saving to disk.
  457. if( aItem->Type() == LIB_FIELD_T )
  458. {
  459. LIB_FIELD* field = (LIB_FIELD*) aItem;
  460. if( field->GetId() < MANDATORY_FIELDS )
  461. {
  462. wxLogWarning( _(
  463. "An attempt was made to remove the %s field from component %s in library %s." ),
  464. GetChars( field->GetName() ), GetChars( GetName() ),
  465. GetChars( GetLibraryName() ) );
  466. return;
  467. }
  468. }
  469. LIB_ITEMS& items = m_drawings[ aItem->Type() ];
  470. for( LIB_ITEMS::iterator i = items.begin(); i != items.end(); i++ )
  471. {
  472. if( *i == aItem )
  473. {
  474. if( aDc != NULL )
  475. aItem->Draw( aPanel, aDc, wxPoint( 0, 0 ), COLOR4D::UNSPECIFIED,
  476. g_XorMode, NULL, DefaultTransform );
  477. items.erase( i );
  478. SetModified();
  479. break;
  480. }
  481. }
  482. }
  483. void LIB_PART::AddDrawItem( LIB_ITEM* aItem )
  484. {
  485. wxASSERT( aItem != NULL );
  486. m_drawings.push_back( aItem );
  487. }
  488. LIB_ITEM* LIB_PART::GetNextDrawItem( LIB_ITEM* aItem, KICAD_T aType )
  489. {
  490. if( m_drawings.empty( aType ) )
  491. return NULL;
  492. if( aItem == NULL )
  493. return &( *( m_drawings.begin( aType ) ) );
  494. // Search for the last item, assume aItem is of type aType
  495. wxASSERT( ( aType == TYPE_NOT_INIT ) || ( aType == aItem->Type() ) );
  496. LIB_ITEMS_CONTAINER::ITERATOR it = m_drawings.begin( aType );
  497. while( ( it != m_drawings.end( aType ) ) && ( aItem != &( *it ) ) )
  498. ++it;
  499. // Search the next item
  500. if( it != m_drawings.end( aType ) )
  501. {
  502. ++it;
  503. if( it != m_drawings.end( aType ) )
  504. return &( *it );
  505. }
  506. return NULL;
  507. }
  508. void LIB_PART::GetPins( LIB_PINS& aList, int aUnit, int aConvert )
  509. {
  510. if( m_drawings.empty( LIB_PIN_T ) )
  511. return;
  512. /* Notes:
  513. * when aUnit == 0: no unit filtering
  514. * when aConvert == 0: no convert (shape selection) filtering
  515. * when .m_Unit == 0, the body item is common to units
  516. * when .m_Convert == 0, the body item is common to shapes
  517. */
  518. for( LIB_ITEM& item : m_drawings[ LIB_PIN_T ] )
  519. {
  520. // Unit filtering:
  521. if( aUnit && item.m_Unit && ( item.m_Unit != aUnit ) )
  522. continue;
  523. // Shape filtering:
  524. if( aConvert && item.m_Convert && ( item.m_Convert != aConvert ) )
  525. continue;
  526. aList.push_back( (LIB_PIN*) &item );
  527. }
  528. }
  529. LIB_PIN* LIB_PART::GetPin( const wxString& aNumber, int aUnit, int aConvert )
  530. {
  531. LIB_PINS pinList;
  532. GetPins( pinList, aUnit, aConvert );
  533. for( size_t i = 0; i < pinList.size(); i++ )
  534. {
  535. wxASSERT( pinList[i]->Type() == LIB_PIN_T );
  536. if( aNumber == pinList[i]->GetNumber() )
  537. return pinList[i];
  538. }
  539. return NULL;
  540. }
  541. bool LIB_PART::PinsConflictWith( LIB_PART& aOtherPart, bool aTestNums, bool aTestNames,
  542. bool aTestType, bool aTestOrientation, bool aTestLength )
  543. {
  544. LIB_PINS thisPinList;
  545. GetPins( thisPinList, /* aUnit */ 0, /* aConvert */ 0 );
  546. for( LIB_PIN* eachThisPin : thisPinList )
  547. {
  548. wxASSERT( eachThisPin );
  549. LIB_PINS otherPinList;
  550. aOtherPart.GetPins( otherPinList, /* aUnit */ 0, /* aConvert */ 0 );
  551. bool foundMatch = false;
  552. for( LIB_PIN* eachOtherPin : otherPinList )
  553. {
  554. wxASSERT( eachOtherPin );
  555. // Same position?
  556. if( eachThisPin->GetPosition() != eachOtherPin->GetPosition() )
  557. continue;
  558. // Same number?
  559. if( aTestNums && ( eachThisPin->GetNumber() != eachOtherPin->GetNumber() ))
  560. continue;
  561. // Same name?
  562. if( aTestNames && ( eachThisPin->GetName() != eachOtherPin->GetName() ))
  563. continue;
  564. // Same electrical type?
  565. if( aTestType && ( eachThisPin->GetType() != eachOtherPin->GetType() ))
  566. continue;
  567. // Same orientation?
  568. if( aTestOrientation && ( eachThisPin->GetOrientation() != eachOtherPin->GetOrientation() ))
  569. continue;
  570. // Same length?
  571. if( aTestLength && ( eachThisPin->GetLength() != eachOtherPin->GetLength() ))
  572. continue;
  573. foundMatch = true;
  574. }
  575. if( !foundMatch )
  576. {
  577. // This means there was not an identical (according to the arguments)
  578. // pin at the same position in the other component.
  579. return true;
  580. }
  581. }
  582. // The loop never gave up, so no conflicts were found.
  583. return false;
  584. }
  585. const EDA_RECT LIB_PART::GetUnitBoundingBox( int aUnit, int aConvert ) const
  586. {
  587. EDA_RECT bBox;
  588. bool initialized = false;
  589. for( const LIB_ITEM& item : m_drawings )
  590. {
  591. if( ( item.m_Unit > 0 ) && ( ( m_unitCount > 1 ) && ( aUnit > 0 )
  592. && ( aUnit != item.m_Unit ) ) )
  593. continue;
  594. if( item.m_Convert > 0 && ( ( aConvert > 0 ) && ( aConvert != item.m_Convert ) ) )
  595. continue;
  596. if ( ( item.Type() == LIB_FIELD_T ) && !( ( LIB_FIELD& ) item ).IsVisible() )
  597. continue;
  598. if( initialized )
  599. bBox.Merge( item.GetBoundingBox() );
  600. else
  601. {
  602. bBox = item.GetBoundingBox();
  603. initialized = true;
  604. }
  605. }
  606. return bBox;
  607. }
  608. void LIB_PART::ViewGetLayers( int aLayers[], int& aCount ) const
  609. {
  610. aCount = 2;
  611. aLayers[0] = LAYER_DEVICE;
  612. aLayers[1] = LAYER_DEVICE_BACKGROUND;
  613. }
  614. const EDA_RECT LIB_PART::GetBodyBoundingBox( int aUnit, int aConvert ) const
  615. {
  616. EDA_RECT bBox;
  617. bool initialized = false;
  618. for( const LIB_ITEM& item : m_drawings )
  619. {
  620. if( ( item.m_Unit > 0 ) && ( ( m_unitCount > 1 ) && ( aUnit > 0 )
  621. && ( aUnit != item.m_Unit ) ) )
  622. continue;
  623. if( item.m_Convert > 0 && ( ( aConvert > 0 ) && ( aConvert != item.m_Convert ) ) )
  624. continue;
  625. if( item.Type() == LIB_FIELD_T )
  626. continue;
  627. if( initialized )
  628. bBox.Merge( item.GetBoundingBox() );
  629. else
  630. {
  631. bBox = item.GetBoundingBox();
  632. initialized = true;
  633. }
  634. }
  635. return bBox;
  636. }
  637. void LIB_PART::deleteAllFields()
  638. {
  639. m_drawings[ LIB_FIELD_T ].clear();
  640. }
  641. void LIB_PART::SetFields( const std::vector <LIB_FIELD>& aFields )
  642. {
  643. deleteAllFields();
  644. for( unsigned i=0; i<aFields.size(); ++i )
  645. {
  646. // drawings is a ptr_vector, new and copy an object on the heap.
  647. LIB_FIELD* field = new LIB_FIELD( aFields[i] );
  648. field->SetParent( this );
  649. m_drawings.push_back( field );
  650. }
  651. }
  652. void LIB_PART::GetFields( LIB_FIELDS& aList )
  653. {
  654. LIB_FIELD* field;
  655. // Grab the MANDATORY_FIELDS first, in expected order given by
  656. // enum NumFieldType
  657. for( int id=0; id<MANDATORY_FIELDS; ++id )
  658. {
  659. field = GetField( id );
  660. // the MANDATORY_FIELDS are exactly that in RAM.
  661. wxASSERT( field );
  662. aList.push_back( *field );
  663. }
  664. // Now grab all the rest of fields.
  665. for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  666. {
  667. field = ( LIB_FIELD* ) &item;
  668. if( (unsigned) field->GetId() < MANDATORY_FIELDS )
  669. continue; // was added above
  670. aList.push_back( *field );
  671. }
  672. }
  673. LIB_FIELD* LIB_PART::GetField( int aId )
  674. {
  675. for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  676. {
  677. LIB_FIELD* field = ( LIB_FIELD* ) &item;
  678. if( field->GetId() == aId )
  679. return field;
  680. }
  681. return NULL;
  682. }
  683. LIB_FIELD* LIB_PART::FindField( const wxString& aFieldName )
  684. {
  685. for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
  686. {
  687. LIB_FIELD* field = ( LIB_FIELD* ) &item;
  688. if( field->GetName() == aFieldName )
  689. return field;
  690. }
  691. return NULL;
  692. }
  693. LIB_FIELD& LIB_PART::GetValueField()
  694. {
  695. LIB_FIELD* field = GetField( VALUE );
  696. wxASSERT( field != NULL );
  697. return *field;
  698. }
  699. LIB_FIELD& LIB_PART::GetReferenceField()
  700. {
  701. LIB_FIELD* field = GetField( REFERENCE );
  702. wxASSERT( field != NULL );
  703. return *field;
  704. }
  705. LIB_FIELD& LIB_PART::GetFootprintField()
  706. {
  707. LIB_FIELD* field = GetField( FOOTPRINT );
  708. wxASSERT( field != NULL );
  709. return *field;
  710. }
  711. bool LIB_PART::SaveDateAndTime( OUTPUTFORMATTER& aFormatter )
  712. {
  713. int year, mon, day, hour, min, sec;
  714. if( m_dateLastEdition == 0 )
  715. return true;
  716. sec = m_dateLastEdition & 63;
  717. min = ( m_dateLastEdition >> 6 ) & 63;
  718. hour = ( m_dateLastEdition >> 12 ) & 31;
  719. day = ( m_dateLastEdition >> 17 ) & 31;
  720. mon = ( m_dateLastEdition >> 22 ) & 15;
  721. year = ( m_dateLastEdition >> 26 ) + 1990;
  722. aFormatter.Print( 0, "Ti %d/%d/%d %d:%d:%d\n", year, mon, day, hour, min, sec );
  723. return true;
  724. }
  725. bool LIB_PART::LoadDateAndTime( char* aLine )
  726. {
  727. int year, mon, day, hour, min, sec;
  728. year = mon = day = hour = min = sec = 0;
  729. strtok( aLine, " \r\t\n" );
  730. strtok( NULL, " \r\t\n" );
  731. if( sscanf( aLine, "%d/%d/%d %d:%d:%d", &year, &mon, &day, &hour, &min, &sec ) != 6 )
  732. return false;
  733. m_dateLastEdition = ( sec & 63 ) + ( ( min & 63 ) << 6 ) +
  734. ( ( hour & 31 ) << 12 ) + ( ( day & 31 ) << 17 ) +
  735. ( ( mon & 15 ) << 22 ) + ( ( year - 1990 ) << 26 );
  736. return true;
  737. }
  738. void LIB_PART::SetOffset( const wxPoint& aOffset )
  739. {
  740. for( LIB_ITEM& item : m_drawings )
  741. item.SetOffset( aOffset );
  742. }
  743. void LIB_PART::RemoveDuplicateDrawItems()
  744. {
  745. m_drawings.unique();
  746. }
  747. bool LIB_PART::HasConversion() const
  748. {
  749. for( const LIB_ITEM& item : m_drawings )
  750. {
  751. if( item.m_Convert > 1 )
  752. return true;
  753. }
  754. return false;
  755. }
  756. void LIB_PART::ClearStatus()
  757. {
  758. for( LIB_ITEM& item : m_drawings )
  759. {
  760. item.m_Flags = 0;
  761. }
  762. }
  763. LIB_ITEM* LIB_PART::LocateDrawItem( int aUnit, int aConvert,
  764. KICAD_T aType, const wxPoint& aPoint )
  765. {
  766. for( LIB_ITEM& item : m_drawings )
  767. {
  768. if( ( aUnit && item.m_Unit && ( aUnit != item.m_Unit) )
  769. || ( aConvert && item.m_Convert && ( aConvert != item.m_Convert ) )
  770. || ( ( item.Type() != aType ) && ( aType != TYPE_NOT_INIT ) ) )
  771. continue;
  772. if( item.HitTest( aPoint ) )
  773. return &item;
  774. }
  775. return NULL;
  776. }
  777. LIB_ITEM* LIB_PART::LocateDrawItem( int aUnit, int aConvert, KICAD_T aType,
  778. const wxPoint& aPoint, const TRANSFORM& aTransform )
  779. {
  780. /* we use LocateDrawItem( int aUnit, int convert, KICAD_T type, const
  781. * wxPoint& pt ) to search items.
  782. * because this function uses DefaultTransform as orient/mirror matrix
  783. * we temporary copy aTransform in DefaultTransform
  784. */
  785. LIB_ITEM* item;
  786. TRANSFORM transform = DefaultTransform;
  787. DefaultTransform = aTransform;
  788. item = LocateDrawItem( aUnit, aConvert, aType, aPoint );
  789. // Restore matrix
  790. DefaultTransform = transform;
  791. return item;
  792. }
  793. void LIB_PART::SetUnitCount( int aCount )
  794. {
  795. if( m_unitCount == aCount )
  796. return;
  797. if( aCount < m_unitCount )
  798. {
  799. LIB_ITEMS_CONTAINER::ITERATOR i = m_drawings.begin();
  800. while( i != m_drawings.end() )
  801. {
  802. if( i->m_Unit > aCount )
  803. i = m_drawings.erase( i );
  804. else
  805. ++i;
  806. }
  807. }
  808. else
  809. {
  810. int prevCount = m_unitCount;
  811. // Temporary storage for new items, as adding new items directly to
  812. // m_drawings may cause the buffer reallocation which invalidates the
  813. // iterators
  814. std::vector< LIB_ITEM* > tmp;
  815. for( LIB_ITEM& item : m_drawings )
  816. {
  817. if( item.m_Unit != 1 )
  818. continue;
  819. for( int j = prevCount + 1; j <= aCount; j++ )
  820. {
  821. LIB_ITEM* newItem = (LIB_ITEM*) item.Clone();
  822. newItem->m_Unit = j;
  823. tmp.push_back( newItem );
  824. }
  825. }
  826. for( auto item : tmp )
  827. m_drawings.push_back( item );
  828. }
  829. m_unitCount = aCount;
  830. }
  831. void LIB_PART::SetConversion( bool aSetConvert )
  832. {
  833. if( aSetConvert == HasConversion() )
  834. return;
  835. // Duplicate items to create the converted shape
  836. if( aSetConvert )
  837. {
  838. std::vector< LIB_ITEM* > tmp; // Temporarily store the duplicated pins here.
  839. for( LIB_ITEM& item : m_drawings )
  840. {
  841. // Only pins are duplicated.
  842. if( item.Type() != LIB_PIN_T )
  843. continue;
  844. if( item.m_Convert == 1 )
  845. {
  846. LIB_ITEM* newItem = (LIB_ITEM*) item.Clone();
  847. newItem->m_Convert = 2;
  848. tmp.push_back( newItem );
  849. }
  850. }
  851. // Transfer the new pins to the LIB_PART.
  852. for( unsigned i = 0; i < tmp.size(); i++ )
  853. m_drawings.push_back( tmp[i] );
  854. }
  855. else
  856. {
  857. // Delete converted shape items because the converted shape does
  858. // not exist
  859. LIB_ITEMS_CONTAINER::ITERATOR i = m_drawings.begin();
  860. while( i != m_drawings.end() )
  861. {
  862. if( i->m_Convert > 1 )
  863. i = m_drawings.erase( i );
  864. else
  865. ++i;
  866. }
  867. }
  868. }
  869. wxArrayString LIB_PART::GetAliasNames( bool aIncludeRoot ) const
  870. {
  871. wxArrayString names;
  872. LIB_ALIASES::const_iterator it;
  873. for( it=m_aliases.begin(); it != m_aliases.end(); ++it )
  874. {
  875. if( !aIncludeRoot && (*it)->IsRoot() )
  876. continue;
  877. names.Add( (*it)->GetName() );
  878. }
  879. return names;
  880. }
  881. bool LIB_PART::HasAlias( const wxString& aName ) const
  882. {
  883. wxCHECK2_MSG( !aName.IsEmpty(), return false,
  884. wxT( "Cannot get alias with an empty name, bad programmer." ) );
  885. for( size_t i = 0; i < m_aliases.size(); i++ )
  886. {
  887. if( aName == m_aliases[i]->GetName() )
  888. return true;
  889. }
  890. return false;
  891. }
  892. void LIB_PART::RemoveAlias( const wxString& aName )
  893. {
  894. LIB_ALIAS* a = GetAlias( aName );
  895. if( a )
  896. RemoveAlias( a );
  897. }
  898. LIB_ALIAS* LIB_PART::RemoveAlias( LIB_ALIAS* aAlias )
  899. {
  900. wxCHECK_MSG( aAlias, NULL, wxT( "Cannot remove alias by NULL pointer." ) );
  901. LIB_ALIAS* nextAlias = NULL;
  902. LIB_ALIASES::iterator it = find( m_aliases.begin(), m_aliases.end(), aAlias );
  903. if( it != m_aliases.end() )
  904. {
  905. bool rename = aAlias->IsRoot();
  906. wxLogTrace( traceSchLibMem,
  907. wxT( "%s: symbol:'%s', alias:'%s', alias count %llu, reference count %ld." ),
  908. GetChars( wxString::FromAscii( __WXFUNCTION__ ) ),
  909. GetChars( GetName() ),
  910. GetChars( aAlias->GetName() ),
  911. (long long unsigned) m_aliases.size(),
  912. m_me.use_count() );
  913. it = m_aliases.erase( it );
  914. if( !m_aliases.empty() )
  915. {
  916. if( it == m_aliases.end() )
  917. it = m_aliases.begin();
  918. nextAlias = *it;
  919. if( rename )
  920. SetName( nextAlias->GetName() );
  921. }
  922. }
  923. return nextAlias;
  924. }
  925. void LIB_PART::RemoveAllAliases()
  926. {
  927. // Remove all of the aliases except the root alias.
  928. while( m_aliases.size() > 1 )
  929. m_aliases.pop_back();
  930. }
  931. LIB_ALIAS* LIB_PART::GetAlias( const wxString& aName )
  932. {
  933. wxCHECK2_MSG( !aName.IsEmpty(), return NULL,
  934. wxT( "Cannot get alias with an empty name. Bad programmer!" ) );
  935. for( size_t i = 0; i < m_aliases.size(); i++ )
  936. {
  937. if( aName == m_aliases[i]->GetName() )
  938. return m_aliases[i];
  939. }
  940. return NULL;
  941. }
  942. LIB_ALIAS* LIB_PART::GetAlias( size_t aIndex )
  943. {
  944. wxCHECK2_MSG( aIndex < m_aliases.size(), return NULL,
  945. wxT( "Illegal alias list index, bad programmer." ) );
  946. return m_aliases[aIndex];
  947. }
  948. void LIB_PART::AddAlias( const wxString& aName )
  949. {
  950. m_aliases.push_back( new LIB_ALIAS( aName, this ) );
  951. }
  952. void LIB_PART::AddAlias( LIB_ALIAS* aAlias )
  953. {
  954. m_aliases.push_back( aAlias );
  955. }
  956. void LIB_PART::SetSubpartIdNotation( int aSep, int aFirstId )
  957. {
  958. m_subpartFirstId = 'A';
  959. m_subpartIdSeparator = 0;
  960. if( aSep == '.' || aSep == '-' || aSep == '_' )
  961. m_subpartIdSeparator = aSep;
  962. if( aFirstId == '1' && aSep != 0 )
  963. m_subpartFirstId = aFirstId;
  964. }