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.

1897 lines
49 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004-2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2008-2015 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 2004-2016 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 <class_drawpanel.h>
  32. #include <plot_common.h>
  33. #include <gr_basic.h>
  34. #include <class_sch_screen.h>
  35. #include <richio.h>
  36. #include <general.h>
  37. #include <template_fieldnames.h>
  38. #include <transform.h>
  39. #include <class_library.h>
  40. #include <class_libentry.h>
  41. #include <lib_pin.h>
  42. #include <lib_arc.h>
  43. #include <lib_bezier.h>
  44. #include <lib_circle.h>
  45. #include <lib_polyline.h>
  46. #include <lib_rectangle.h>
  47. #include <lib_text.h>
  48. // the separator char between the subpart id and the reference
  49. // 0 (no separator) or '.' or some other character
  50. int LIB_PART::m_subpartIdSeparator = 0;
  51. // the ascii char value to calculate the subpart symbol id from the part number:
  52. // 'A' or '1' usually. (to print U1.A or U1.1)
  53. // if this a a digit, a number is used as id symbol
  54. int LIB_PART::m_subpartFirstId = 'A';
  55. const wxChar traceSchLibMem[] = wxT( "KISCHLIBMEM" ); // public
  56. LIB_ALIAS::LIB_ALIAS( const wxString& aName, LIB_PART* aRootPart ):
  57. EDA_ITEM( LIB_ALIAS_T ),
  58. shared( aRootPart )
  59. {
  60. name = aName;
  61. }
  62. LIB_ALIAS::LIB_ALIAS( const LIB_ALIAS& aAlias, LIB_PART* aRootPart ) :
  63. EDA_ITEM( aAlias ),
  64. shared( aRootPart )
  65. {
  66. name = aAlias.name;
  67. description = aAlias.description;
  68. keyWords = aAlias.keyWords;
  69. docFileName = aAlias.docFileName;
  70. }
  71. LIB_ALIAS::~LIB_ALIAS()
  72. {
  73. wxASSERT_MSG( shared, wxT( "~LIB_ALIAS() without a LIB_PART" ) );
  74. wxLogTrace( traceSchLibMem,
  75. wxT( "%s: destroying alias:'%s' of part:'%s'." ),
  76. GetChars( wxString::FromAscii( __WXFUNCTION__ ) ), GetChars( name ),
  77. GetChars( shared->GetName() ) );
  78. if( shared )
  79. shared->RemoveAlias( this );
  80. }
  81. const wxString LIB_ALIAS::GetLibraryName()
  82. {
  83. wxASSERT_MSG( shared, wxT( "LIB_ALIAS without a LIB_PART" ) );
  84. if( shared )
  85. return shared->GetLibraryName();
  86. return wxString( _( "none" ) );
  87. }
  88. bool LIB_ALIAS::IsRoot() const
  89. {
  90. return name == shared->GetName();
  91. }
  92. PART_LIB* LIB_ALIAS::GetLib()
  93. {
  94. return shared->GetLib();
  95. }
  96. bool LIB_ALIAS::SaveDoc( OUTPUTFORMATTER& aFormatter )
  97. {
  98. if( description.IsEmpty() && keyWords.IsEmpty() && docFileName.IsEmpty() )
  99. return true;
  100. try
  101. {
  102. aFormatter.Print( 0, "#\n$CMP %s\n", TO_UTF8( name ) );
  103. if( !description.IsEmpty() )
  104. aFormatter.Print( 0, "D %s\n", TO_UTF8( description ) );
  105. if( !keyWords.IsEmpty() )
  106. aFormatter.Print( 0, "K %s\n", TO_UTF8( keyWords ) );
  107. if( !docFileName.IsEmpty() )
  108. aFormatter.Print( 0, "F %s\n", TO_UTF8( docFileName ) );
  109. aFormatter.Print( 0, "$ENDCMP\n" );
  110. }
  111. catch( const IO_ERROR& ioe )
  112. {
  113. return false;
  114. }
  115. return true;
  116. }
  117. bool LIB_ALIAS::operator==( const wxChar* aName ) const
  118. {
  119. return name == aName;
  120. }
  121. bool operator<( const LIB_ALIAS& aItem1, const LIB_ALIAS& aItem2 )
  122. {
  123. return aItem1.GetName() < aItem2.GetName();
  124. }
  125. int LibraryEntryCompare( const LIB_ALIAS* aItem1, const LIB_ALIAS* aItem2 )
  126. {
  127. return aItem1->GetName().Cmp( aItem2->GetName() );
  128. }
  129. /// http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/sp_techniques.html#weak_without_shared
  130. struct null_deleter
  131. {
  132. void operator()(void const *) const
  133. {
  134. }
  135. };
  136. LIB_PART::LIB_PART( const wxString& aName, PART_LIB* aLibrary ) :
  137. EDA_ITEM( LIB_PART_T ),
  138. m_me( this, null_deleter() )
  139. {
  140. m_name = aName;
  141. m_library = aLibrary;
  142. m_dateModified = 0;
  143. m_unitCount = 1;
  144. m_pinNameOffset = 40;
  145. m_options = ENTRY_NORMAL;
  146. m_unitsLocked = false;
  147. m_showPinNumbers = true;
  148. m_showPinNames = true;
  149. // Create the default alias if the name parameter is not empty.
  150. if( !aName.IsEmpty() )
  151. m_aliases.push_back( new LIB_ALIAS( aName, this ) );
  152. // Add the MANDATORY_FIELDS in RAM only. These are assumed to be present
  153. // when the field editors are invoked.
  154. LIB_FIELD* value = new LIB_FIELD( this, VALUE );
  155. value->SetText( aName );
  156. drawings.push_back( value );
  157. drawings.push_back( new LIB_FIELD( this, REFERENCE ) );
  158. drawings.push_back( new LIB_FIELD( this, FOOTPRINT ) );
  159. drawings.push_back( new LIB_FIELD( this, DATASHEET ) );
  160. }
  161. LIB_PART::LIB_PART( LIB_PART& aPart, PART_LIB* aLibrary ) :
  162. EDA_ITEM( aPart ),
  163. m_me( this, null_deleter() )
  164. {
  165. LIB_ITEM* newItem;
  166. m_library = aLibrary;
  167. m_name = aPart.m_name;
  168. m_FootprintList = aPart.m_FootprintList;
  169. m_unitCount = aPart.m_unitCount;
  170. m_unitsLocked = aPart.m_unitsLocked;
  171. m_pinNameOffset = aPart.m_pinNameOffset;
  172. m_showPinNumbers = aPart.m_showPinNumbers;
  173. m_showPinNames = aPart.m_showPinNames;
  174. m_dateModified = aPart.m_dateModified;
  175. m_options = aPart.m_options;
  176. for( LIB_ITEM& oldItem : aPart.GetDrawItemList() )
  177. {
  178. if( oldItem.IsNew() )
  179. continue;
  180. newItem = (LIB_ITEM*) oldItem.Clone();
  181. newItem->SetParent( this );
  182. drawings.push_back( newItem );
  183. }
  184. for( size_t i = 0; i < aPart.m_aliases.size(); i++ )
  185. {
  186. LIB_ALIAS* alias = new LIB_ALIAS( *aPart.m_aliases[i], this );
  187. m_aliases.push_back( alias );
  188. }
  189. }
  190. LIB_PART::~LIB_PART()
  191. {
  192. wxLogTrace( traceSchLibMem,
  193. wxT( "%s: destroying part '%s' with alias list count of %llu." ),
  194. GetChars( wxString::FromAscii( __WXFUNCTION__ ) ), GetChars( GetName() ),
  195. (long long unsigned) m_aliases.size() );
  196. // If the part is being deleted directly rather than through the library,
  197. // delete all of the aliases.
  198. while( m_aliases.size() )
  199. {
  200. LIB_ALIAS* alias = m_aliases.back();
  201. m_aliases.pop_back();
  202. delete alias;
  203. }
  204. }
  205. const wxString LIB_PART::GetLibraryName()
  206. {
  207. if( m_library )
  208. return m_library->GetName();
  209. return wxString( _( "none" ) );
  210. }
  211. wxString LIB_PART::SubReference( int aUnit, bool aAddSeparator )
  212. {
  213. wxString subRef;
  214. if( m_subpartIdSeparator != 0 && aAddSeparator )
  215. subRef << wxChar( m_subpartIdSeparator );
  216. if( m_subpartFirstId >= '0' && m_subpartFirstId <= '9' )
  217. subRef << aUnit;
  218. else
  219. {
  220. // use letters as notation. To allow more than 26 units, the sub ref
  221. // use one letter if letter = A .. Z or a ... z, and 2 letters otherwise
  222. // first letter is expected to be 'A' or 'a' (i.e. 26 letters are available)
  223. int u;
  224. aUnit -= 1; // Unit number starts to 1. now to 0.
  225. while( aUnit >= 26 ) // more than one letter are needed
  226. {
  227. u = aUnit / 26;
  228. subRef << wxChar( m_subpartFirstId + u -1 );
  229. aUnit %= 26;
  230. }
  231. u = m_subpartFirstId + aUnit;
  232. subRef << wxChar( u );
  233. }
  234. return subRef;
  235. }
  236. void LIB_PART::SetName( const wxString& aName )
  237. {
  238. m_name = aName;
  239. GetValueField().SetText( aName );
  240. m_aliases[0]->SetName( aName );
  241. }
  242. void LIB_PART::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDc, const wxPoint& aOffset, int aMulti,
  243. int aConvert, GR_DRAWMODE aDrawMode, EDA_COLOR_T aColor,
  244. const TRANSFORM& aTransform, bool aShowPinText, bool aDrawFields,
  245. bool aOnlySelected, const std::vector<bool>* aPinsDangling )
  246. {
  247. BASE_SCREEN* screen = aPanel ? aPanel->GetScreen() : NULL;
  248. GRSetDrawMode( aDc, aDrawMode );
  249. /* draw background for filled items using background option
  250. * Solid lines will be drawn after the background
  251. * Note also, background is not drawn when:
  252. * printing in black and white
  253. * If the color is not the default color (aColor != -1 )
  254. */
  255. if( ! (screen && screen->m_IsPrinting && GetGRForceBlackPenState())
  256. && (aColor == UNSPECIFIED_COLOR) )
  257. {
  258. for( LIB_ITEM& drawItem : drawings )
  259. {
  260. if( drawItem.m_Fill != FILLED_WITH_BG_BODYCOLOR )
  261. continue;
  262. if( aOnlySelected && !drawItem.IsSelected() )
  263. continue;
  264. // Do not draw an item while moving (the cursor handler does that)
  265. if( drawItem.m_Flags & IS_MOVED )
  266. continue;
  267. // Do not draw items not attached to the current part
  268. if( aMulti && drawItem.m_Unit && ( drawItem.m_Unit != aMulti ) )
  269. continue;
  270. if( aConvert && drawItem.m_Convert && ( drawItem.m_Convert != aConvert ) )
  271. continue;
  272. if( drawItem.Type() == LIB_FIELD_T )
  273. continue;
  274. if( drawItem.Type() == LIB_FIELD_T )
  275. {
  276. drawItem.Draw( aPanel, aDc, aOffset, aColor, aDrawMode, (void*) NULL, aTransform );
  277. }
  278. // Now, draw only the background for items with
  279. // m_Fill == FILLED_WITH_BG_BODYCOLOR:
  280. drawItem.Draw( aPanel, aDc, aOffset, aColor, aDrawMode, (void*) false, aTransform );
  281. }
  282. }
  283. // Track the index into the dangling pins list
  284. size_t pin_index = 0;
  285. for( LIB_ITEM& drawItem : drawings )
  286. {
  287. if( aOnlySelected && !drawItem.IsSelected() )
  288. continue;
  289. // Do not draw an item while moving (the cursor handler does that)
  290. if( drawItem.m_Flags & IS_MOVED )
  291. continue;
  292. // Do not draw items not attached to the current part
  293. if( aMulti && drawItem.m_Unit && ( drawItem.m_Unit != aMulti ) )
  294. continue;
  295. if( aConvert && drawItem.m_Convert && ( drawItem.m_Convert != aConvert ) )
  296. continue;
  297. if( !aDrawFields && drawItem.Type() == LIB_FIELD_T )
  298. continue;
  299. if( drawItem.Type() == LIB_PIN_T )
  300. {
  301. LIB_PIN& pin = dynamic_cast<LIB_PIN&>( drawItem );
  302. uintptr_t flags = 0;
  303. if( aShowPinText )
  304. flags |= PIN_DRAW_TEXTS;
  305. if( !aPinsDangling || (aPinsDangling->size() > pin_index && (*aPinsDangling)[pin_index] ) )
  306. flags |= PIN_DRAW_DANGLING;
  307. if( pin.IsPowerConnection() && IsPower() )
  308. flags |= PIN_DANGLING_HIDDEN;
  309. drawItem.Draw( aPanel, aDc, aOffset, aColor, aDrawMode, (void*) flags, aTransform );
  310. ++pin_index;
  311. }
  312. else if( drawItem.Type() == LIB_FIELD_T )
  313. {
  314. drawItem.Draw( aPanel, aDc, aOffset, aColor, aDrawMode, (void*) NULL, aTransform );
  315. }
  316. else
  317. {
  318. bool forceNoFill = drawItem.m_Fill == FILLED_WITH_BG_BODYCOLOR;
  319. drawItem.Draw( aPanel, aDc, aOffset, aColor, aDrawMode, (void*) forceNoFill,
  320. aTransform );
  321. }
  322. }
  323. // Enable this to draw the anchor of the component.
  324. #if 0
  325. int len = aDc->DeviceToLogicalXRel( 3 );
  326. EDA_RECT* const clipbox = aPanel ? aPanel->GetClipBox() : NULL;
  327. GRLine( clipbox, aDc, aOffset.x, aOffset.y - len, aOffset.x,
  328. aOffset.y + len, 0, aColor );
  329. GRLine( clipbox, aDc, aOffset.x - len, aOffset.y, aOffset.x + len,
  330. aOffset.y, 0, aColor );
  331. #endif
  332. /* Enable this to draw the bounding box around the component to validate
  333. * the bounding box calculations. */
  334. #if 0
  335. EDA_RECT bBox = GetBoundingBox( aMulti, aConvert );
  336. bBox.RevertYAxis();
  337. bBox = aTransform.TransformCoordinate( bBox );
  338. bBox.Move( aOffset );
  339. GRRect( aPanel ? aPanel->GetClipBox() : NULL, aDc, bBox, 0, LIGHTMAGENTA );
  340. #endif
  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 : 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 : 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 : 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::iterator i;
  422. for( i = drawings.begin(); i != drawings.end(); i++ )
  423. {
  424. if( *i == aItem )
  425. {
  426. if( aDc != NULL )
  427. aItem->Draw( aPanel, aDc, wxPoint( 0, 0 ), UNSPECIFIED_COLOR,
  428. g_XorMode, NULL, DefaultTransform );
  429. drawings.erase( i );
  430. SetModified();
  431. break;
  432. }
  433. }
  434. }
  435. void LIB_PART::AddDrawItem( LIB_ITEM* aItem )
  436. {
  437. wxASSERT( aItem != NULL );
  438. drawings.push_back( aItem );
  439. drawings.sort();
  440. }
  441. LIB_ITEM* LIB_PART::GetNextDrawItem( LIB_ITEM* aItem, KICAD_T aType )
  442. {
  443. /* Return the next draw object pointer.
  444. * If item is NULL return the first item of type in the list.
  445. */
  446. if( drawings.empty() )
  447. return NULL;
  448. if( aItem == NULL && aType == TYPE_NOT_INIT ) // type is unspecified
  449. return &drawings[0];
  450. // Search for last item
  451. size_t idx = 0;
  452. if( aItem )
  453. {
  454. for( ; idx < drawings.size(); idx++ )
  455. {
  456. if( aItem == &drawings[idx] )
  457. {
  458. idx++; // Prepare the next item search
  459. break;
  460. }
  461. }
  462. }
  463. // Search the next item
  464. for( ; idx < drawings.size(); idx++ )
  465. {
  466. if( aType == TYPE_NOT_INIT || drawings[ idx ].Type() == aType )
  467. return &drawings[ idx ];
  468. }
  469. return NULL;
  470. }
  471. void LIB_PART::GetPins( LIB_PINS& aList, int aUnit, int aConvert )
  472. {
  473. /* Notes:
  474. * when aUnit == 0: no unit filtering
  475. * when aConvert == 0: no convert (shape selection) filtering
  476. * when .m_Unit == 0, the body item is common to units
  477. * when .m_Convert == 0, the body item is common to shapes
  478. */
  479. for( LIB_ITEM& item : drawings )
  480. {
  481. if( item.Type() != LIB_PIN_T ) // we search pins only
  482. continue;
  483. // Unit filtering:
  484. if( aUnit && item.m_Unit && ( item.m_Unit != aUnit ) )
  485. continue;
  486. // Shape filtering:
  487. if( aConvert && item.m_Convert && ( item.m_Convert != aConvert ) )
  488. continue;
  489. aList.push_back( (LIB_PIN*) &item );
  490. }
  491. }
  492. LIB_PIN* LIB_PART::GetPin( const wxString& aNumber, int aUnit, int aConvert )
  493. {
  494. wxString pNumber;
  495. LIB_PINS pinList;
  496. GetPins( pinList, aUnit, aConvert );
  497. for( size_t i = 0; i < pinList.size(); i++ )
  498. {
  499. wxASSERT( pinList[i]->Type() == LIB_PIN_T );
  500. pinList[i]->PinStringNum( pNumber );
  501. if( aNumber == pNumber )
  502. return pinList[i];
  503. }
  504. return NULL;
  505. }
  506. bool LIB_PART::PinsConflictWith( LIB_PART& aOtherPart, bool aTestNums, bool aTestNames,
  507. bool aTestType, bool aTestOrientation, bool aTestLength )
  508. {
  509. LIB_PINS thisPinList;
  510. GetPins( thisPinList, /* aUnit */ 0, /* aConvert */ 0 );
  511. for( LIB_PIN* eachThisPin : thisPinList )
  512. {
  513. wxASSERT( eachThisPin );
  514. LIB_PINS otherPinList;
  515. aOtherPart.GetPins( otherPinList, /* aUnit */ 0, /* aConvert */ 0 );
  516. bool foundMatch = false;
  517. for( LIB_PIN* eachOtherPin : otherPinList )
  518. {
  519. wxASSERT( eachOtherPin );
  520. // Same position?
  521. if( eachThisPin->GetPosition() != eachOtherPin->GetPosition() )
  522. continue;
  523. // Same number?
  524. wxString eachThisPinNumber, eachOtherPinNumber;
  525. eachThisPin->PinStringNum( eachThisPinNumber );
  526. eachOtherPin->PinStringNum( eachOtherPinNumber );
  527. if( aTestNums && ( eachThisPinNumber != eachOtherPinNumber ))
  528. continue;
  529. // Same name?
  530. if( aTestNames && ( eachThisPin->GetName() != eachOtherPin->GetName() ))
  531. continue;
  532. // Same electrical type?
  533. if( aTestType && ( eachThisPin->GetType() != eachOtherPin->GetType() ))
  534. continue;
  535. // Same orientation?
  536. if( aTestOrientation && ( eachThisPin->GetOrientation() != eachOtherPin->GetOrientation() ))
  537. continue;
  538. // Same length?
  539. if( aTestLength && ( eachThisPin->GetLength() != eachOtherPin->GetLength() ))
  540. continue;
  541. foundMatch = true;
  542. }
  543. if( !foundMatch )
  544. {
  545. // This means there was not an identical (according to the arguments)
  546. // pin at the same position in the other component.
  547. return true;
  548. }
  549. }
  550. // The loop never gave up, so no conflicts were found.
  551. return false;
  552. }
  553. bool LIB_PART::Save( OUTPUTFORMATTER& aFormatter )
  554. {
  555. LIB_FIELD& value = GetValueField();
  556. // First line: it s a comment (component name for readers)
  557. aFormatter.Print( 0, "#\n# %s\n#\n", TO_UTF8( value.GetText() ) );
  558. // Save data
  559. aFormatter.Print( 0, "DEF" );
  560. #if 0 && defined(DEBUG)
  561. if( value.GetText() == wxT( "R" ) )
  562. {
  563. int breakhere = 1;
  564. (void) breakhere;
  565. }
  566. #endif
  567. if( value.IsVisible() )
  568. {
  569. aFormatter.Print( 0, " %s", TO_UTF8( value.GetText() ) );
  570. }
  571. else
  572. {
  573. aFormatter.Print( 0, " ~%s", TO_UTF8( value.GetText() ) );
  574. }
  575. LIB_FIELD& reference = GetReferenceField();
  576. if( !reference.GetText().IsEmpty() )
  577. {
  578. aFormatter.Print( 0, " %s", TO_UTF8( reference.GetText() ) );
  579. }
  580. else
  581. {
  582. aFormatter.Print( 0, " ~" );
  583. }
  584. aFormatter.Print( 0, " %d %d %c %c %d %c %c\n",
  585. 0, m_pinNameOffset,
  586. m_showPinNumbers ? 'Y' : 'N',
  587. m_showPinNames ? 'Y' : 'N',
  588. m_unitCount, m_unitsLocked ? 'L' : 'F',
  589. m_options == ENTRY_POWER ? 'P' : 'N' );
  590. if( !SaveDateAndTime( aFormatter ) )
  591. return false;
  592. LIB_FIELDS fields;
  593. GetFields( fields );
  594. // Mandatory fields:
  595. // may have their own save policy so there is a separate loop for them.
  596. // Empty fields are saved, because the user may have set visibility,
  597. // size and orientation
  598. for( int i = 0; i < MANDATORY_FIELDS; ++i )
  599. {
  600. if( !fields[i].Save( aFormatter ) )
  601. return false;
  602. }
  603. // User defined fields:
  604. // may have their own save policy so there is a separate loop for them.
  605. int fieldId = MANDATORY_FIELDS; // really wish this would go away.
  606. for( unsigned i = MANDATORY_FIELDS; i < fields.size(); ++i )
  607. {
  608. // There is no need to save empty fields, i.e. no reason to preserve field
  609. // names now that fields names come in dynamically through the template
  610. // fieldnames.
  611. if( !fields[i].GetText().IsEmpty() )
  612. {
  613. fields[i].SetId( fieldId++ );
  614. if( !fields[i].Save( aFormatter ) )
  615. return false;
  616. }
  617. }
  618. // Save the alias list: a line starting by "ALIAS". The first alias is the root
  619. // and has the same name as the component. In the old library file format this
  620. // alias does not get added to the alias list.
  621. if( m_aliases.size() > 1 )
  622. {
  623. aFormatter.Print( 0, "ALIAS" );
  624. for( unsigned i = 1; i < m_aliases.size(); i++ )
  625. {
  626. aFormatter.Print( 0, " %s", TO_UTF8( m_aliases[i]->GetName() ) );
  627. }
  628. aFormatter.Print( 0, "\n" );
  629. }
  630. // Write the footprint filter list
  631. if( m_FootprintList.GetCount() != 0 )
  632. {
  633. aFormatter.Print( 0, "$FPLIST\n" );
  634. for( unsigned i = 0; i < m_FootprintList.GetCount(); i++ )
  635. {
  636. aFormatter.Print( 0, " %s\n", TO_UTF8( m_FootprintList[i] ) );
  637. }
  638. aFormatter.Print( 0, "$ENDFPLIST\n" );
  639. }
  640. // Save graphics items (including pins)
  641. if( !drawings.empty() )
  642. {
  643. /* we sort the draw items, in order to have an edition more easy,
  644. * when a file editing "by hand" is made */
  645. drawings.sort();
  646. aFormatter.Print( 0, "DRAW\n" );
  647. for( LIB_ITEM& item : drawings )
  648. {
  649. if( item.Type() == LIB_FIELD_T )
  650. continue;
  651. if( !item.Save( aFormatter ) )
  652. return false;
  653. }
  654. aFormatter.Print( 0, "ENDDRAW\n" );
  655. }
  656. aFormatter.Print( 0, "ENDDEF\n" );
  657. return true;
  658. }
  659. bool LIB_PART::Load( LINE_READER& aLineReader, wxString& aErrorMsg )
  660. {
  661. int unused;
  662. char* p;
  663. char* componentName;
  664. char* prefix = NULL;
  665. char* line;
  666. bool result;
  667. wxString Msg;
  668. line = aLineReader.Line();
  669. p = strtok( line, " \t\r\n" );
  670. if( strcmp( p, "DEF" ) != 0 )
  671. {
  672. aErrorMsg.Printf( wxT( "DEF command expected in line %d, aborted." ),
  673. aLineReader.LineNumber() );
  674. return false;
  675. }
  676. // Read DEF line:
  677. char drawnum = 0;
  678. char drawname = 0;
  679. if( ( componentName = strtok( NULL, " \t\n" ) ) == NULL // Part name:
  680. || ( prefix = strtok( NULL, " \t\n" ) ) == NULL // Prefix name:
  681. || ( p = strtok( NULL, " \t\n" ) ) == NULL // NumOfPins:
  682. || sscanf( p, "%d", &unused ) != 1
  683. || ( p = strtok( NULL, " \t\n" ) ) == NULL // TextInside:
  684. || sscanf( p, "%d", &m_pinNameOffset ) != 1
  685. || ( p = strtok( NULL, " \t\n" ) ) == NULL // DrawNums:
  686. || sscanf( p, "%c", &drawnum ) != 1
  687. || ( p = strtok( NULL, " \t\n" ) ) == NULL // DrawNums:
  688. || sscanf( p, "%c", &drawname ) != 1
  689. || ( p = strtok( NULL, " \t\n" ) ) == NULL // m_unitCount:
  690. || sscanf( p, "%d", &m_unitCount ) != 1 )
  691. {
  692. aErrorMsg.Printf( wxT( "Wrong DEF format in line %d, skipped." ),
  693. aLineReader.LineNumber() );
  694. while( (line = aLineReader.ReadLine()) != NULL )
  695. {
  696. p = strtok( line, " \t\n" );
  697. if( p && strcasecmp( p, "ENDDEF" ) == 0 )
  698. break;
  699. }
  700. return false;
  701. }
  702. // Ensure m_unitCount is >= 1 (could be read as 0 in old libraries)
  703. if( m_unitCount < 1 )
  704. m_unitCount = 1;
  705. m_showPinNumbers = ( drawnum == 'N' ) ? false : true;
  706. m_showPinNames = ( drawname == 'N' ) ? false : true;
  707. // Copy part name and prefix.
  708. LIB_FIELD& value = GetValueField();
  709. if( componentName[0] != '~' )
  710. {
  711. m_name = FROM_UTF8( componentName );
  712. value.SetText( m_name );
  713. }
  714. else
  715. {
  716. m_name = FROM_UTF8( &componentName[1] );
  717. value.SetText( m_name );
  718. value.SetVisible( false );
  719. }
  720. // Add the root alias to the alias list.
  721. m_aliases.push_back( new LIB_ALIAS( m_name, this ) );
  722. LIB_FIELD& reference = GetReferenceField();
  723. if( strcmp( prefix, "~" ) == 0 )
  724. {
  725. reference.Empty();
  726. reference.SetVisible( false );
  727. }
  728. else
  729. {
  730. reference.SetText( FROM_UTF8( prefix ) );
  731. }
  732. // Copy optional infos
  733. if( ( p = strtok( NULL, " \t\n" ) ) != NULL && *p == 'L' )
  734. m_unitsLocked = true;
  735. if( ( p = strtok( NULL, " \t\n" ) ) != NULL && *p == 'P' )
  736. m_options = ENTRY_POWER;
  737. // Read next lines, until "ENDDEF" is found
  738. while( ( line = aLineReader.ReadLine() ) != NULL )
  739. {
  740. p = strtok( line, " \t\r\n" );
  741. // This is the error flag ( if an error occurs, result = false)
  742. result = true;
  743. if( *line == '#' ) // a comment
  744. continue;
  745. if( p == NULL ) // empty line
  746. continue;
  747. if( line[0] == 'T' && line[1] == 'i' )
  748. result = LoadDateAndTime( aLineReader );
  749. else if( *line == 'F' )
  750. result = LoadField( aLineReader, Msg );
  751. else if( strcmp( p, "ENDDEF" ) == 0 ) // End of component description
  752. goto ok;
  753. else if( strcmp( p, "DRAW" ) == 0 )
  754. result = LoadDrawEntries( aLineReader, Msg );
  755. else if( strncmp( p, "ALIAS", 5 ) == 0 )
  756. {
  757. p = strtok( NULL, "\r\n" );
  758. result = LoadAliases( p, aErrorMsg );
  759. }
  760. else if( strncmp( p, "$FPLIST", 5 ) == 0 )
  761. result = LoadFootprints( aLineReader, Msg );
  762. // End line or block analysis: test for an error
  763. if( !result )
  764. {
  765. if( Msg.IsEmpty() )
  766. aErrorMsg.Printf( wxT( "error occurred at line %d " ), aLineReader.LineNumber() );
  767. else
  768. aErrorMsg.Printf( wxT( "error <%s> occurred at line %d " ),
  769. GetChars( Msg ), aLineReader.LineNumber() );
  770. return false;
  771. }
  772. }
  773. return false;
  774. ok:
  775. // If we are here, this part is O.k. - put it in:
  776. drawings.sort();
  777. return true;
  778. }
  779. bool LIB_PART::LoadDrawEntries( LINE_READER& aLineReader, wxString& aErrorMsg )
  780. {
  781. char* line;
  782. LIB_ITEM* newEntry = NULL;
  783. while( true )
  784. {
  785. if( !( line = aLineReader.ReadLine() ) )
  786. {
  787. aErrorMsg = wxT( "file ended prematurely loading component draw element" );
  788. return false;
  789. }
  790. if( strncmp( line, "ENDDRAW", 7 ) == 0 )
  791. break;
  792. newEntry = NULL;
  793. switch( line[0] )
  794. {
  795. case 'A': // Arc
  796. newEntry = ( LIB_ITEM* ) new LIB_ARC( this );
  797. break;
  798. case 'C': // Circle
  799. newEntry = ( LIB_ITEM* ) new LIB_CIRCLE( this );
  800. break;
  801. case 'T': // Text
  802. newEntry = ( LIB_ITEM* ) new LIB_TEXT( this );
  803. break;
  804. case 'S': // Square
  805. newEntry = ( LIB_ITEM* ) new LIB_RECTANGLE( this );
  806. break;
  807. case 'X': // Pin Description
  808. newEntry = ( LIB_ITEM* ) new LIB_PIN( this );
  809. break;
  810. case 'P': // Polyline
  811. newEntry = ( LIB_ITEM* ) new LIB_POLYLINE( this );
  812. break;
  813. case 'B': // Bezier Curves
  814. newEntry = ( LIB_ITEM* ) new LIB_BEZIER( this );
  815. break;
  816. case '#': // Comment
  817. continue;
  818. case '\n':
  819. case '\r':
  820. case 0: // empty line
  821. continue;
  822. default:
  823. aErrorMsg.Printf( wxT( "undefined DRAW command %c" ), line[0] );
  824. return false;
  825. }
  826. if( !newEntry->Load( aLineReader, aErrorMsg ) )
  827. {
  828. aErrorMsg.Printf( wxT( "error '%s' in DRAW command %c" ),
  829. GetChars( aErrorMsg ), line[0] );
  830. delete newEntry;
  831. // Flush till end of draw section
  832. do
  833. {
  834. if( !aLineReader.ReadLine() )
  835. {
  836. aErrorMsg = wxT( "file ended prematurely while attempting "
  837. "to flush to end of drawing section." );
  838. return false;
  839. }
  840. } while( strncmp( line, "ENDDRAW", 7 ) != 0 );
  841. return false;
  842. }
  843. else
  844. {
  845. drawings.push_back( newEntry );
  846. }
  847. }
  848. return true;
  849. }
  850. bool LIB_PART::LoadAliases( char* aLine, wxString& aErrorMsg )
  851. {
  852. char* text = strtok( aLine, " \t\r\n" );
  853. while( text )
  854. {
  855. m_aliases.push_back( new LIB_ALIAS( FROM_UTF8( text ), this ) );
  856. text = strtok( NULL, " \t\r\n" );
  857. }
  858. return true;
  859. }
  860. bool LIB_PART::LoadField( LINE_READER& aLineReader, wxString& aErrorMsg )
  861. {
  862. LIB_FIELD* field = new LIB_FIELD( this );
  863. if( !field->Load( aLineReader, aErrorMsg ) )
  864. {
  865. delete field;
  866. return false;
  867. }
  868. if( field->GetId() < MANDATORY_FIELDS )
  869. {
  870. LIB_FIELD* fixedField = GetField( field->GetId() );
  871. // this will fire only if somebody broke a constructor or editor.
  872. // MANDATORY_FIELDS are always present in ram resident components, no
  873. // exceptions, and they always have their names set, even fixed fields.
  874. wxASSERT( fixedField );
  875. *fixedField = *field;
  876. if( field->GetId() == VALUE )
  877. m_name = field->GetText();
  878. delete field;
  879. }
  880. else
  881. {
  882. drawings.push_back( field );
  883. }
  884. return true;
  885. }
  886. bool LIB_PART::LoadFootprints( LINE_READER& aLineReader, wxString& aErrorMsg )
  887. {
  888. char* line;
  889. char* p;
  890. while( true )
  891. {
  892. if( !( line = aLineReader.ReadLine() ) )
  893. {
  894. aErrorMsg = wxT( "file ended prematurely while loading footprints" );
  895. return false;
  896. }
  897. p = strtok( line, " \t\r\n" );
  898. if( strcasecmp( p, "$ENDFPLIST" ) == 0 )
  899. break;
  900. m_FootprintList.Add( FROM_UTF8( p ) );
  901. }
  902. return true;
  903. }
  904. const EDA_RECT LIB_PART::GetBoundingBox( int aUnit, int aConvert ) const
  905. {
  906. EDA_RECT bBox;
  907. bool initialized = false;
  908. for( unsigned ii = 0; ii < drawings.size(); ii++ )
  909. {
  910. const LIB_ITEM& item = drawings[ii];
  911. if( ( item.m_Unit > 0 ) && ( ( m_unitCount > 1 ) && ( aUnit > 0 )
  912. && ( aUnit != item.m_Unit ) ) )
  913. continue;
  914. if( item.m_Convert > 0 && ( ( aConvert > 0 ) && ( aConvert != item.m_Convert ) ) )
  915. continue;
  916. if ( ( item.Type() == LIB_FIELD_T ) && !( ( LIB_FIELD& ) item ).IsVisible() )
  917. continue;
  918. if( initialized )
  919. bBox.Merge( item.GetBoundingBox() );
  920. else
  921. {
  922. bBox = item.GetBoundingBox();
  923. initialized = true;
  924. }
  925. }
  926. return bBox;
  927. }
  928. const EDA_RECT LIB_PART::GetBodyBoundingBox( int aUnit, int aConvert ) const
  929. {
  930. EDA_RECT bBox;
  931. bool initialized = false;
  932. for( unsigned ii = 0; ii < drawings.size(); ii++ )
  933. {
  934. const LIB_ITEM& item = drawings[ii];
  935. if( ( item.m_Unit > 0 ) && ( ( m_unitCount > 1 ) && ( aUnit > 0 )
  936. && ( aUnit != item.m_Unit ) ) )
  937. continue;
  938. if( item.m_Convert > 0 && ( ( aConvert > 0 ) && ( aConvert != item.m_Convert ) ) )
  939. continue;
  940. if( item.Type() == LIB_FIELD_T )
  941. continue;
  942. if( initialized )
  943. bBox.Merge( item.GetBoundingBox() );
  944. else
  945. {
  946. bBox = item.GetBoundingBox();
  947. initialized = true;
  948. }
  949. }
  950. return bBox;
  951. }
  952. void LIB_PART::deleteAllFields()
  953. {
  954. LIB_ITEMS::iterator it;
  955. for( it = drawings.begin(); it != drawings.end(); /* deleting */ )
  956. {
  957. if( it->Type() != LIB_FIELD_T )
  958. {
  959. ++it;
  960. continue;
  961. }
  962. // 'it' is not advanced, but should point to next in list after erase()
  963. it = drawings.erase( it );
  964. }
  965. }
  966. void LIB_PART::SetFields( const std::vector <LIB_FIELD>& aFields )
  967. {
  968. deleteAllFields();
  969. for( unsigned i=0; i<aFields.size(); ++i )
  970. {
  971. // drawings is a ptr_vector, new and copy an object on the heap.
  972. LIB_FIELD* field = new LIB_FIELD( aFields[i] );
  973. field->SetParent( this );
  974. drawings.push_back( field );
  975. }
  976. // Reorder drawings: transparent polygons first, pins and text last.
  977. // so texts have priority on screen.
  978. drawings.sort();
  979. }
  980. void LIB_PART::GetFields( LIB_FIELDS& aList )
  981. {
  982. LIB_FIELD* field;
  983. // The only caller of this function is the library field editor, so it
  984. // establishes policy here.
  985. // Grab the MANDATORY_FIELDS first, in expected order given by
  986. // enum NumFieldType
  987. for( int id=0; id<MANDATORY_FIELDS; ++id )
  988. {
  989. field = GetField( id );
  990. // the MANDATORY_FIELDS are exactly that in RAM.
  991. wxASSERT( field );
  992. aList.push_back( *field );
  993. }
  994. // Now grab all the rest of fields.
  995. for( LIB_ITEM& item : drawings )
  996. {
  997. if( item.Type() != LIB_FIELD_T )
  998. continue;
  999. field = ( LIB_FIELD* ) &item;
  1000. if( (unsigned) field->GetId() < MANDATORY_FIELDS )
  1001. continue; // was added above
  1002. aList.push_back( *field );
  1003. }
  1004. }
  1005. LIB_FIELD* LIB_PART::GetField( int aId )
  1006. {
  1007. for( LIB_ITEM& item : drawings )
  1008. {
  1009. if( item.Type() != LIB_FIELD_T )
  1010. continue;
  1011. LIB_FIELD* field = ( LIB_FIELD* ) &item;
  1012. if( field->GetId() == aId )
  1013. return field;
  1014. }
  1015. return NULL;
  1016. }
  1017. LIB_FIELD* LIB_PART::FindField( const wxString& aFieldName )
  1018. {
  1019. for( LIB_ITEM& item : drawings )
  1020. {
  1021. if( item.Type() != LIB_FIELD_T )
  1022. continue;
  1023. LIB_FIELD* field = ( LIB_FIELD* ) &item;
  1024. if( field->GetName() == aFieldName )
  1025. return field;
  1026. }
  1027. return NULL;
  1028. }
  1029. LIB_FIELD& LIB_PART::GetValueField()
  1030. {
  1031. LIB_FIELD* field = GetField( VALUE );
  1032. wxASSERT( field != NULL );
  1033. return *field;
  1034. }
  1035. LIB_FIELD& LIB_PART::GetReferenceField()
  1036. {
  1037. LIB_FIELD* field = GetField( REFERENCE );
  1038. wxASSERT( field != NULL );
  1039. return *field;
  1040. }
  1041. bool LIB_PART::SaveDateAndTime( OUTPUTFORMATTER& aFormatter )
  1042. {
  1043. int year, mon, day, hour, min, sec;
  1044. if( m_dateModified == 0 )
  1045. return true;
  1046. sec = m_dateModified & 63;
  1047. min = ( m_dateModified >> 6 ) & 63;
  1048. hour = ( m_dateModified >> 12 ) & 31;
  1049. day = ( m_dateModified >> 17 ) & 31;
  1050. mon = ( m_dateModified >> 22 ) & 15;
  1051. year = ( m_dateModified >> 26 ) + 1990;
  1052. aFormatter.Print( 0, "Ti %d/%d/%d %d:%d:%d\n", year, mon, day, hour, min, sec );
  1053. return true;
  1054. }
  1055. bool LIB_PART::LoadDateAndTime( char* aLine )
  1056. {
  1057. int year, mon, day, hour, min, sec;
  1058. year = mon = day = hour = min = sec = 0;
  1059. strtok( aLine, " \r\t\n" );
  1060. strtok( NULL, " \r\t\n" );
  1061. if( sscanf( aLine, "%d/%d/%d %d:%d:%d", &year, &mon, &day, &hour, &min, &sec ) != 6 )
  1062. return false;
  1063. m_dateModified = ( sec & 63 ) + ( ( min & 63 ) << 6 ) +
  1064. ( ( hour & 31 ) << 12 ) + ( ( day & 31 ) << 17 ) +
  1065. ( ( mon & 15 ) << 22 ) + ( ( year - 1990 ) << 26 );
  1066. return true;
  1067. }
  1068. void LIB_PART::SetOffset( const wxPoint& aOffset )
  1069. {
  1070. for( LIB_ITEM& item : drawings )
  1071. {
  1072. item.SetOffset( aOffset );
  1073. }
  1074. }
  1075. void LIB_PART::RemoveDuplicateDrawItems()
  1076. {
  1077. drawings.unique();
  1078. }
  1079. bool LIB_PART::HasConversion() const
  1080. {
  1081. for( unsigned ii = 0; ii < drawings.size(); ii++ )
  1082. {
  1083. const LIB_ITEM& item = drawings[ii];
  1084. if( item.m_Convert > 1 )
  1085. return true;
  1086. }
  1087. return false;
  1088. }
  1089. void LIB_PART::ClearStatus()
  1090. {
  1091. for( LIB_ITEM& item : drawings )
  1092. {
  1093. item.m_Flags = 0;
  1094. }
  1095. }
  1096. int LIB_PART::SelectItems( EDA_RECT& aRect, int aUnit, int aConvert, bool aEditPinByPin )
  1097. {
  1098. int itemCount = 0;
  1099. for( LIB_ITEM& item : drawings )
  1100. {
  1101. item.ClearFlags( SELECTED );
  1102. if( ( item.m_Unit && item.m_Unit != aUnit )
  1103. || ( item.m_Convert && item.m_Convert != aConvert ) )
  1104. {
  1105. if( item.Type() != LIB_PIN_T )
  1106. continue;
  1107. // Specific rules for pins.
  1108. if( aEditPinByPin || m_unitsLocked
  1109. || ( item.m_Convert && item.m_Convert != aConvert ) )
  1110. continue;
  1111. }
  1112. if( item.Inside( aRect ) )
  1113. {
  1114. item.SetFlags( SELECTED );
  1115. itemCount++;
  1116. }
  1117. }
  1118. return itemCount;
  1119. }
  1120. void LIB_PART::MoveSelectedItems( const wxPoint& aOffset )
  1121. {
  1122. for( LIB_ITEM& item : drawings )
  1123. {
  1124. if( !item.IsSelected() )
  1125. continue;
  1126. item.SetOffset( aOffset );
  1127. item.m_Flags = 0;
  1128. }
  1129. drawings.sort();
  1130. }
  1131. void LIB_PART::ClearSelectedItems()
  1132. {
  1133. for( LIB_ITEM& item : drawings )
  1134. {
  1135. item.m_Flags = 0;
  1136. }
  1137. }
  1138. void LIB_PART::DeleteSelectedItems()
  1139. {
  1140. LIB_ITEMS::iterator item = drawings.begin();
  1141. // We *do not* remove the 2 mandatory fields: reference and value
  1142. // so skip them (do not remove) if they are flagged selected.
  1143. // Skip also not visible items.
  1144. // But I think fields must not be deleted by a block delete command or other global command
  1145. // because they are not really graphic items
  1146. while( item != drawings.end() )
  1147. {
  1148. if( item->Type() == LIB_FIELD_T )
  1149. {
  1150. #if 0 // Set to 1 to allows fields deletion on block delete or other global command
  1151. LIB_FIELD& field = ( LIB_FIELD& ) *item;
  1152. if( (field.GetId() == REFERENCE) || (field.m_FieldId == VALUE) ||
  1153. (field.m_Attributs & TEXT_NO_VISIBLE) )
  1154. #endif
  1155. item->ClearFlags( SELECTED );
  1156. }
  1157. if( !item->IsSelected() )
  1158. item++;
  1159. else
  1160. item = drawings.erase( item );
  1161. }
  1162. }
  1163. void LIB_PART::CopySelectedItems( const wxPoint& aOffset )
  1164. {
  1165. /* *do not* use iterators here, because new items
  1166. * are added to drawings that is a boost::ptr_vector.
  1167. * When push_back elements in buffer,
  1168. * a memory reallocation can happen and will break pointers
  1169. */
  1170. unsigned icnt = drawings.size();
  1171. for( unsigned ii = 0; ii < icnt; ii++ )
  1172. {
  1173. LIB_ITEM& item = drawings[ii];
  1174. // We *do not* copy fields because they are unique for the whole component
  1175. // so skip them (do not duplicate) if they are flagged selected.
  1176. if( item.Type() == LIB_FIELD_T )
  1177. item.ClearFlags( SELECTED );
  1178. if( !item.IsSelected() )
  1179. continue;
  1180. item.ClearFlags( SELECTED );
  1181. LIB_ITEM* newItem = (LIB_ITEM*) item.Clone();
  1182. newItem->SetFlags( SELECTED );
  1183. drawings.push_back( newItem );
  1184. }
  1185. MoveSelectedItems( aOffset );
  1186. drawings.sort();
  1187. }
  1188. void LIB_PART::MirrorSelectedItemsH( const wxPoint& aCenter )
  1189. {
  1190. for( LIB_ITEM& item : drawings )
  1191. {
  1192. if( !item.IsSelected() )
  1193. continue;
  1194. item.MirrorHorizontal( aCenter );
  1195. item.m_Flags = 0;
  1196. }
  1197. drawings.sort();
  1198. }
  1199. void LIB_PART::MirrorSelectedItemsV( const wxPoint& aCenter )
  1200. {
  1201. for( LIB_ITEM& item : drawings )
  1202. {
  1203. if( !item.IsSelected() )
  1204. continue;
  1205. item.MirrorVertical( aCenter );
  1206. item.m_Flags = 0;
  1207. }
  1208. drawings.sort();
  1209. }
  1210. void LIB_PART::RotateSelectedItems( const wxPoint& aCenter )
  1211. {
  1212. for( LIB_ITEM& item : drawings )
  1213. {
  1214. if( !item.IsSelected() )
  1215. continue;
  1216. item.Rotate( aCenter );
  1217. item.m_Flags = 0;
  1218. }
  1219. drawings.sort();
  1220. }
  1221. LIB_ITEM* LIB_PART::LocateDrawItem( int aUnit, int aConvert,
  1222. KICAD_T aType, const wxPoint& aPoint )
  1223. {
  1224. for( LIB_ITEM& item : drawings )
  1225. {
  1226. if( ( aUnit && item.m_Unit && ( aUnit != item.m_Unit) )
  1227. || ( aConvert && item.m_Convert && ( aConvert != item.m_Convert ) )
  1228. || ( ( item.Type() != aType ) && ( aType != TYPE_NOT_INIT ) ) )
  1229. continue;
  1230. if( item.HitTest( aPoint ) )
  1231. return &item;
  1232. }
  1233. return NULL;
  1234. }
  1235. LIB_ITEM* LIB_PART::LocateDrawItem( int aUnit, int aConvert, KICAD_T aType,
  1236. const wxPoint& aPoint, const TRANSFORM& aTransform )
  1237. {
  1238. /* we use LocateDrawItem( int aUnit, int convert, KICAD_T type, const
  1239. * wxPoint& pt ) to search items.
  1240. * because this function uses DefaultTransform as orient/mirror matrix
  1241. * we temporary copy aTransform in DefaultTransform
  1242. */
  1243. LIB_ITEM* item;
  1244. TRANSFORM transform = DefaultTransform;
  1245. DefaultTransform = aTransform;
  1246. item = LocateDrawItem( aUnit, aConvert, aType, aPoint );
  1247. // Restore matrix
  1248. DefaultTransform = transform;
  1249. return item;
  1250. }
  1251. void LIB_PART::SetUnitCount( int aCount )
  1252. {
  1253. if( m_unitCount == aCount )
  1254. return;
  1255. if( aCount < m_unitCount )
  1256. {
  1257. LIB_ITEMS::iterator i;
  1258. i = drawings.begin();
  1259. while( i != drawings.end() )
  1260. {
  1261. if( i->m_Unit > aCount )
  1262. i = drawings.erase( i );
  1263. else
  1264. i++;
  1265. }
  1266. }
  1267. else
  1268. {
  1269. int prevCount = m_unitCount;
  1270. // We cannot use an iterator here, because when adding items in vector
  1271. // the buffer can be reallocated, that change the previous value of
  1272. // .begin() and .end() iterators and invalidate others iterators
  1273. unsigned imax = drawings.size();
  1274. for( unsigned ii = 0; ii < imax; ii++ )
  1275. {
  1276. if( drawings[ii].m_Unit != 1 )
  1277. continue;
  1278. for( int j = prevCount + 1; j <= aCount; j++ )
  1279. {
  1280. LIB_ITEM* newItem = (LIB_ITEM*) drawings[ii].Clone();
  1281. newItem->m_Unit = j;
  1282. drawings.push_back( newItem );
  1283. }
  1284. }
  1285. drawings.sort();
  1286. }
  1287. m_unitCount = aCount;
  1288. }
  1289. void LIB_PART::SetConversion( bool aSetConvert )
  1290. {
  1291. if( aSetConvert == HasConversion() )
  1292. return;
  1293. // Duplicate items to create the converted shape
  1294. if( aSetConvert )
  1295. {
  1296. std::vector< LIB_ITEM* > tmp; // Temporarily store the duplicated pins here.
  1297. for( LIB_ITEM& item : drawings )
  1298. {
  1299. // Only pins are duplicated.
  1300. if( item.Type() != LIB_PIN_T )
  1301. continue;
  1302. if( item.m_Convert == 1 )
  1303. {
  1304. LIB_ITEM* newItem = (LIB_ITEM*) item.Clone();
  1305. newItem->m_Convert = 2;
  1306. tmp.push_back( newItem );
  1307. }
  1308. }
  1309. // Transfer the new pins to the LIB_PART.
  1310. for( unsigned i = 0; i < tmp.size(); i++ )
  1311. drawings.push_back( tmp[i] );
  1312. }
  1313. else
  1314. {
  1315. // Delete converted shape items because the converted shape does
  1316. // not exist
  1317. LIB_ITEMS::iterator i = drawings.begin();
  1318. while( i != drawings.end() )
  1319. {
  1320. if( i->m_Convert > 1 )
  1321. i = drawings.erase( i );
  1322. else
  1323. i++;
  1324. }
  1325. }
  1326. }
  1327. wxArrayString LIB_PART::GetAliasNames( bool aIncludeRoot ) const
  1328. {
  1329. wxArrayString names;
  1330. LIB_ALIASES::const_iterator it;
  1331. for( it=m_aliases.begin(); it != m_aliases.end(); ++it )
  1332. {
  1333. if( !aIncludeRoot && (*it)->IsRoot() )
  1334. continue;
  1335. names.Add( (*it)->GetName() );
  1336. }
  1337. return names;
  1338. }
  1339. bool LIB_PART::HasAlias( const wxString& aName ) const
  1340. {
  1341. wxCHECK2_MSG( !aName.IsEmpty(), return false,
  1342. wxT( "Cannot get alias with an empty name, bad programmer." ) );
  1343. for( size_t i = 0; i < m_aliases.size(); i++ )
  1344. {
  1345. if( aName == m_aliases[i]->GetName() )
  1346. return true;
  1347. }
  1348. return false;
  1349. }
  1350. void LIB_PART::SetAliases( const wxArrayString& aAliasList )
  1351. {
  1352. wxCHECK_RET( !m_library,
  1353. wxT( "Part aliases cannot be changed when they are owned by a library." ) );
  1354. if( aAliasList == GetAliasNames() )
  1355. return;
  1356. // Add names not existing in the current component alias list.
  1357. for( size_t i = 0; i < aAliasList.GetCount(); i++ )
  1358. {
  1359. if( HasAlias( aAliasList[ i ] ) )
  1360. continue;
  1361. m_aliases.push_back( new LIB_ALIAS( aAliasList[ i ], this ) );
  1362. }
  1363. // Remove names in the current component that are not in the new alias list.
  1364. LIB_ALIASES::iterator it = m_aliases.begin();
  1365. while( it != m_aliases.end() )
  1366. {
  1367. int index = aAliasList.Index( (*it)->GetName(), false );
  1368. if( index != wxNOT_FOUND || (*it)->IsRoot() )
  1369. ++it;
  1370. else
  1371. it = m_aliases.erase( it );
  1372. }
  1373. }
  1374. #if 0 // this version looked suspect to me, it did not rename a deleted root
  1375. void LIB_PART::RemoveAlias( const wxString& aName )
  1376. {
  1377. wxCHECK_RET( m_library == NULL,
  1378. wxT( "Part aliases cannot be changed when they are owned by a library." ) );
  1379. wxCHECK_RET( !aName.IsEmpty(), wxT( "Cannot get alias with an empty name." ) );
  1380. LIB_ALIASES::iterator it;
  1381. for( it = m_aliases.begin(); it != m_aliases.end(); it++ )
  1382. {
  1383. if( aName == (*it)->GetName() )
  1384. {
  1385. m_aliases.erase( it );
  1386. break;
  1387. }
  1388. }
  1389. }
  1390. #else
  1391. void LIB_PART::RemoveAlias( const wxString& aName )
  1392. {
  1393. LIB_ALIAS* a = GetAlias( aName );
  1394. if( a )
  1395. RemoveAlias( a );
  1396. }
  1397. #endif
  1398. LIB_ALIAS* LIB_PART::RemoveAlias( LIB_ALIAS* aAlias )
  1399. {
  1400. wxCHECK_MSG( aAlias, NULL, wxT( "Cannot remove alias by NULL pointer." ) );
  1401. LIB_ALIAS* nextAlias = NULL;
  1402. LIB_ALIASES::iterator it = find( m_aliases.begin(), m_aliases.end(), aAlias );
  1403. if( it != m_aliases.end() )
  1404. {
  1405. bool rename = aAlias->IsRoot();
  1406. wxLogTrace( traceSchLibMem,
  1407. wxT( "%s: part:'%s', alias:'%s', alias count %llu, reference count %ld." ),
  1408. GetChars( wxString::FromAscii( __WXFUNCTION__ ) ),
  1409. GetChars( m_name ),
  1410. GetChars( aAlias->GetName() ),
  1411. (long long unsigned) m_aliases.size(),
  1412. m_me.use_count() );
  1413. it = m_aliases.erase( it );
  1414. if( !m_aliases.empty() )
  1415. {
  1416. if( it == m_aliases.end() )
  1417. it = m_aliases.begin();
  1418. nextAlias = *it;
  1419. if( rename )
  1420. SetName( nextAlias->GetName() );
  1421. }
  1422. }
  1423. return nextAlias;
  1424. }
  1425. void LIB_PART::RemoveAllAliases()
  1426. {
  1427. // Remove all of the aliases except the root alias.
  1428. while( m_aliases.size() > 1 )
  1429. m_aliases.pop_back();
  1430. }
  1431. LIB_ALIAS* LIB_PART::GetAlias( const wxString& aName )
  1432. {
  1433. wxCHECK2_MSG( !aName.IsEmpty(), return NULL,
  1434. wxT( "Cannot get alias with an empty name. Bad programmer!" ) );
  1435. for( size_t i = 0; i < m_aliases.size(); i++ )
  1436. {
  1437. if( aName == m_aliases[i]->GetName() )
  1438. return m_aliases[i];
  1439. }
  1440. return NULL;
  1441. }
  1442. LIB_ALIAS* LIB_PART::GetAlias( size_t aIndex )
  1443. {
  1444. wxCHECK2_MSG( aIndex < m_aliases.size(), return NULL,
  1445. wxT( "Illegal alias list index, bad programmer." ) );
  1446. return m_aliases[aIndex];
  1447. }
  1448. void LIB_PART::AddAlias( const wxString& aName )
  1449. {
  1450. wxCHECK_RET( !HasAlias( aName ),
  1451. wxT( "Part <" ) + GetName() + wxT( "> already has an alias <" ) +
  1452. aName + wxT( ">. Bad programmer." ) );
  1453. m_aliases.push_back( new LIB_ALIAS( aName, this ) );
  1454. }
  1455. void LIB_PART::SetSubpartIdNotation( int aSep, int aFirstId )
  1456. {
  1457. m_subpartFirstId = 'A';
  1458. m_subpartIdSeparator = 0;
  1459. if( aSep == '.' || aSep == '-' || aSep == '_' )
  1460. m_subpartIdSeparator = aSep;
  1461. if( aFirstId == '1' && aSep != 0 )
  1462. m_subpartFirstId = aFirstId;
  1463. }