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.

669 lines
22 KiB

  1. /******************************************/
  2. /* Library component object definitions. */
  3. /******************************************/
  4. #ifndef CLASS_LIBENTRY_H
  5. #define CLASS_LIBENTRY_H
  6. #include "classes_body_items.h"
  7. #include "class_libentry_fields.h"
  8. #include <boost/ptr_container/ptr_vector.hpp>
  9. class CMP_LIBRARY;
  10. /* Types for components in libraries
  11. * components can be a true component or an alias of a true component.
  12. */
  13. enum LibrEntryType
  14. {
  15. ROOT, /* This is a true component standard LIB_COMPONENT */
  16. ALIAS /* This is an alias of a true component */
  17. };
  18. /* values for member .m_options */
  19. enum LibrEntryOptions
  20. {
  21. ENTRY_NORMAL, // Libentry is a standard component (real or alias)
  22. ENTRY_POWER // Libentry is a power symbol
  23. };
  24. /**
  25. * Base class to describe library components and aliases.
  26. *
  27. * This class is not to be used directly.
  28. */
  29. class CMP_LIB_ENTRY : public EDA_BaseStruct
  30. {
  31. protected:
  32. wxString name;
  33. /** Library object that entry is attached to. */
  34. CMP_LIBRARY* library;
  35. /** Entry type, either ROOT or ALIAS. */
  36. LibrEntryType type;
  37. wxString description; /* documentation for info */
  38. wxString keyWords; /* keyword list (used for search for
  39. * components by keyword) */
  40. wxString docFileName; /* Associate doc file name */
  41. public:
  42. CMP_LIB_ENTRY( LibrEntryType aType, const wxString& aName,
  43. CMP_LIBRARY* aLibrary = NULL );
  44. CMP_LIB_ENTRY( CMP_LIB_ENTRY& aEntry, CMP_LIBRARY* aLibrary = NULL );
  45. virtual ~CMP_LIB_ENTRY();
  46. virtual wxString GetClass() const
  47. {
  48. return wxT( "CMP_LIB_ENTRY" );
  49. }
  50. wxString GetLibraryName();
  51. CMP_LIBRARY* GetLibrary() {return library;}
  52. virtual const wxString& GetName() const { return name; }
  53. virtual void SetName( const wxString& aName ) { name = aName; }
  54. bool isComponent() { return type == ROOT; }
  55. bool isAlias() { return type == ALIAS; }
  56. int GetType() { return type; }
  57. void SetDescription( const wxString& aDescription )
  58. {
  59. description = aDescription;
  60. }
  61. wxString GetDescription() { return description; }
  62. void SetKeyWords( const wxString& aKeyWords )
  63. {
  64. keyWords = aKeyWords;
  65. }
  66. wxString GetKeyWords() { return keyWords; }
  67. void SetDocFileName( const wxString& aDocFileName )
  68. {
  69. docFileName = aDocFileName;
  70. }
  71. wxString GetDocFileName() { return docFileName; }
  72. /**
  73. * Write the entry document information to a FILE in "*.dcm" format.
  74. *
  75. * @param aFile The FILE to write to.
  76. * @return True if success writing else false.
  77. */
  78. bool SaveDoc( FILE* aFile );
  79. /**
  80. * Case insensitive comparison of the component entry name.
  81. */
  82. bool operator==( const wxChar* aName ) const;
  83. bool operator!=( const wxChar* aName ) const
  84. {
  85. return !( *this == aName );
  86. }
  87. };
  88. typedef boost::ptr_vector< CMP_LIB_ENTRY > LIB_ENTRY_LIST;
  89. extern bool operator<( const CMP_LIB_ENTRY& aItem1, const CMP_LIB_ENTRY& aItem2 );
  90. extern int LibraryEntryCompare( const CMP_LIB_ENTRY* aItem1, const CMP_LIB_ENTRY* aItem2 );
  91. /**
  92. * Library component object definition.
  93. *
  94. * Library component object definition.
  95. *
  96. * A library component object is typically saved and loaded
  97. * in a component library file (.lib).
  98. * Library components are different from schematic components.
  99. */
  100. class LIB_COMPONENT : public CMP_LIB_ENTRY
  101. {
  102. public:
  103. wxArrayString m_AliasList; /* ALIAS list for the component */
  104. wxArrayString m_FootprintList; /* list of suitable footprint names
  105. * for the component (wildcard names
  106. * accepted) */
  107. bool m_UnitSelectionLocked; /* True if units are different
  108. * and their selection is
  109. * locked (i.e. if part A cannot
  110. * be automatically changed in
  111. * part B */
  112. int m_TextInside; /* if 0: pin name drawn on the pin
  113. * itself if > 0 pin name drawn inside
  114. * the component, with a distance of
  115. * m_TextInside in mils */
  116. bool m_DrawPinNum;
  117. bool m_DrawPinName;
  118. long m_LastDate; // Last change Date
  119. protected:
  120. LibrEntryOptions m_options; // special features (i.e. Entry is a POWER)
  121. int unitCount; /* Units (parts) per package */
  122. LIB_DRAW_ITEM_LIST drawings; /* How to draw this part */
  123. public:
  124. /* Offsets used in editing library component,
  125. * for handle aliases data in m_AliasListData array string
  126. * when editing a library component, aliases data is stored
  127. * in m_AliasListData.
  128. * 4 strings by alias are stored:
  129. * name, doc, keywords and doc filename
  130. * these constants are indexes in m_AliasListData
  131. * to read/write strings for a given alias
  132. */
  133. enum alias_idx
  134. {
  135. ALIAS_NAME_IDX = 0,
  136. ALIAS_DOC_IDX = 1,
  137. ALIAS_KEYWORD_IDX = 2,
  138. ALIAS_DOC_FILENAME_IDX = 3,
  139. ALIAS_NEXT_IDX = 4
  140. };
  141. private:
  142. wxArrayString m_aliasListData; /* ALIAS data (name, doc, keywords and doc filename).
  143. * Used only by the component editor LibEdit
  144. * to store aliases info during edition
  145. * usually void outside the component editor */
  146. void deleteAllFields();
  147. public:
  148. LIB_COMPONENT( const wxString& aName, CMP_LIBRARY* aLibrary = NULL );
  149. LIB_COMPONENT( LIB_COMPONENT& aComponent, CMP_LIBRARY* aLibrary = NULL );
  150. ~LIB_COMPONENT();
  151. virtual wxString GetClass() const
  152. {
  153. return wxT( "LIB_COMPONENT" );
  154. }
  155. virtual void SetName( const wxString& aName )
  156. {
  157. CMP_LIB_ENTRY::SetName( aName );
  158. GetValueField().m_Text = aName;
  159. }
  160. /* accessors to aliases data, used by the component editor, during edition
  161. */
  162. /** Function CollectAliasesData
  163. * store in m_aliasListData alias data (doc, keywords, docfile)
  164. * for each alias found in m_AliasList
  165. */
  166. void CollectAliasesData( CMP_LIBRARY* aLibrary );
  167. /** Function LocateAliasData
  168. * @return an index in array string to the alias data (doc, keywords, docfile)
  169. * or -1 if not found
  170. * @param aAliasName = the alias name
  171. * @param aCreateIfNotExist = true if the alias data must be created, when not exists
  172. */
  173. int LocateAliasData( const wxString & aAliasName, bool aCreateIfNotExist = false);
  174. /** Function GetAliasDataDoc
  175. * @param aAliasName = the alias name
  176. * @return the Doc string
  177. */
  178. wxString GetAliasDataDoc( const wxString & aAliasName );
  179. /** Function GetAliasDataKeyWords
  180. * @param aAliasName = the alias name
  181. * @return aAliasData = the keywords string
  182. */
  183. wxString GetAliasDataKeyWords( const wxString & aAliasName );
  184. /** Function GetAliasDataDocFileName
  185. * @param aAliasName = the alias name
  186. * @return the Doc filename string
  187. */
  188. wxString GetAliasDataDocFileName( const wxString & aAliasName );
  189. /** Function SetAliasDataDoc
  190. * @param aAliasName = the alias name
  191. * @return aAliasData = the Doc string
  192. */
  193. void SetAliasDataDoc( const wxString & aAliasName, const wxString & aAliasData );
  194. /** Function SetAliasDataKeywords
  195. * @param aAliasName = the alias name
  196. * @param aAliasData = the keywords string
  197. */
  198. void SetAliasDataKeywords( const wxString & aAliasName, const wxString & aAliasData );
  199. /** Function SetAliasDataDocFileName
  200. * @param aAliasName = the alias name
  201. * @param aAliasData = the Doc filename string
  202. */
  203. void SetAliasDataDocFileName( const wxString & aAliasName, const wxString & aAliasData );
  204. /** Function ClearAliasDataDoc
  205. * clear aliases data list
  206. */
  207. void ClearAliasDataDoc( ) { m_aliasListData.Clear(); }
  208. /** Function RemoveAliasData
  209. * remove an alias data from list
  210. * @param aAliasName = the alias name
  211. */
  212. void RemoveAliasData(const wxString & aAliasName );
  213. EDA_Rect GetBoundaryBox( int aUnit, int aConvert );
  214. bool SaveDateAndTime( FILE* aFile );
  215. bool LoadDateAndTime( char* aLine );
  216. /**
  217. * Write the data structures out to a FILE in "*.lib" format.
  218. *
  219. * @param aFile - The FILE to write to.
  220. * @return True if success writing else false.
  221. */
  222. bool Save( FILE* aFile );
  223. /**
  224. * Load component definition from /a aFile.
  225. *
  226. * @param aFile - File descriptor of file to load form.
  227. * @param aLine - The first line of the component definition.
  228. * @param aLineNum - The current line number in the file.
  229. * @param aErrorMsg - Description of error on load failure.
  230. * @return True if the load was successful, false if there was an error.
  231. */
  232. bool Load( FILE* aFile, char* aLine, int* aLineNum, wxString& aErrorMsg );
  233. bool LoadField( char* aLine, wxString& aErrorMsg );
  234. bool LoadDrawEntries( FILE* aFile, char* aLine,
  235. int* aLineNum, wxString& aErrorMsg );
  236. bool LoadAliases( char* aLine, wxString& aErrorMsg );
  237. bool LoadFootprints( FILE* aFile, char* aLine,
  238. int* aLineNum, wxString& aErrorMsg );
  239. bool isPower() { return m_options == ENTRY_POWER; }
  240. bool isNormal() { return m_options == ENTRY_NORMAL; }
  241. void SetPower() { m_options = ENTRY_POWER; }
  242. void SetNormal() { m_options = ENTRY_NORMAL; }
  243. /**
  244. * Function SetFields
  245. * overwrites all the existing in this component with fields supplied
  246. * in \a aFieldsList. The only known caller of this function is the
  247. * library component field editor, and it establishes needed behavior.
  248. *
  249. * @param aFieldsList is a set of fields to import, removing all previous fields.
  250. */
  251. void SetFields( const std::vector <LIB_FIELD>& aFieldsList );
  252. /**
  253. * Function GetFields
  254. * returns a list of fields withing this component. The only known caller of
  255. * this function is the library component field editor, and it establishes
  256. * needed behavior.
  257. *
  258. * @param aList - List to add fields to
  259. */
  260. void GetFields( LIB_FIELD_LIST& aList );
  261. /**
  262. * Function FindField
  263. * finds a field within this component matching \a aFieldName and returns
  264. * it or NULL if not found.
  265. */
  266. LIB_FIELD* FindField( const wxString& aFieldName );
  267. /**
  268. * Return pointer to the requested field.
  269. *
  270. * @param aId - Id of field to return.
  271. * @return The field if found, otherwise NULL.
  272. */
  273. LIB_FIELD* GetField( int aId );
  274. /** Return reference to the value field. */
  275. LIB_FIELD& GetValueField();
  276. /** Return reference to the reference designator field. */
  277. LIB_FIELD& GetReferenceField();
  278. /**
  279. * Draw component.
  280. *
  281. * @param aPanel - Window to draw on.
  282. * @param aDc - Device context to draw on.
  283. * @param aOffset - Position to component.
  284. * @param aMulti - Component unit if multiple parts per component.
  285. * @param aConvert - Component conversion (DeMorgan) if available.
  286. * @param aDrawMode - Device context drawing mode, see wxDC.
  287. * @param aColor - Color to draw component.
  288. * @param aTransformMatrix - Coordinate adjustment settings.
  289. * @param aShowPinText - Show pin text if true.
  290. * @param aDrawFields - Draw field text if true otherwise just draw
  291. * body items (useful to draw a body in schematic,
  292. * because fields of schematic components replace
  293. * the lib component fields).
  294. * @param aOnlySelected - Draws only the body items that are selected.
  295. * Used for block move redraws.
  296. */
  297. void Draw( WinEDA_DrawPanel* aPanel, wxDC* aDc, const wxPoint& aOffset,
  298. int aMulti, int aConvert, int aDrawMode, int aColor = -1,
  299. const int aTransform[2][2] = DefaultTransformMatrix,
  300. bool aShowPinText = true, bool aDrawFields = true,
  301. bool aOnlySelected = false );
  302. /**
  303. * Plot component to plotter.
  304. *
  305. * @param aPlotter - Plotter object to plot to.
  306. * @param aUnit - Component part to plot.
  307. * @param aConvert - Component alternate body style to plot.
  308. * @param aTransform - Component plot transform matrix.
  309. */
  310. void Plot( PLOTTER* aPlotter, int aUnit, int aConvert, const wxPoint& aOffset,
  311. const int aTransform[2][2] );
  312. /**
  313. * Add a new draw /a aItem to the draw object list.
  314. *
  315. * @param item - New draw object to add to component.
  316. */
  317. void AddDrawItem( LIB_DRAW_ITEM* aItem );
  318. /**
  319. * Remove draw /a aItem from list.
  320. *
  321. * @param aItem - Draw item to remove from list.
  322. * @param aPanel - Panel to remove part from.
  323. * @param aDc - Device context to remove part from.
  324. */
  325. void RemoveDrawItem( LIB_DRAW_ITEM* aItem,
  326. WinEDA_DrawPanel* aPanel = NULL,
  327. wxDC* aDc = NULL );
  328. /**
  329. * Return the next draw object pointer.
  330. *
  331. * @param aItem - Pointer to the current draw item. Setting item NULL
  332. * with return the first item of type in the list.
  333. * @param aType - type of searched item (filter).
  334. * if TYPE_NOT_INIT search for all items types
  335. * @return - The next drawing object in the list if found, otherwise NULL.
  336. */
  337. LIB_DRAW_ITEM* GetNextDrawItem( LIB_DRAW_ITEM* aItem = NULL,
  338. KICAD_T aType = TYPE_NOT_INIT );
  339. /**
  340. * Return the next pin object from the draw list.
  341. *
  342. * This is just a pin object specific version of GetNextDrawItem().
  343. *
  344. * @param item - Pointer to the previous pin item, or NULL to get the
  345. * first pin in the draw object list.
  346. * @return - The next pin object in the list if found, otherwise NULL.
  347. */
  348. LIB_PIN* GetNextPin( LIB_PIN* aItem = NULL )
  349. {
  350. return (LIB_PIN*) GetNextDrawItem( (LIB_DRAW_ITEM*) aItem,
  351. COMPONENT_PIN_DRAW_TYPE );
  352. }
  353. /**
  354. * Return a list of pin object pointers from the draw item list.
  355. *
  356. * Note pin objects are owned by the draw list of the component.
  357. * Deleting any of the objects will leave list in a unstable state
  358. * and will likely segfault when the list is destroyed.
  359. *
  360. * @param aList - Pin list to place pin object pointers into.
  361. * @param aUnit - Unit number of pin to add to list. Set to 0 to
  362. * get pins from any component part.
  363. * @param aConvert - Convert number of pin to add to list. Set to 0 to
  364. * get pins from any convert of component.
  365. */
  366. void GetPins( LIB_PIN_LIST& aList, int aUnit = 0, int aConvert = 0 );
  367. /**
  368. * Return pin object with the requested pin /a aNumber.
  369. *
  370. * @param aNumber - Number of the pin to find.
  371. * @param aUnit - Unit of the component to find. Set to 0 if a specific
  372. * unit number is not required.
  373. * @param aConvert - Alternate body style filter (DeMorgan). Set to 0 if
  374. * no alternate body style is required.
  375. * @return The pin object if found. Otherwise NULL.
  376. */
  377. LIB_PIN* GetPin( const wxString& aNumber, int aUnit = 0, int aConvert = 0 );
  378. /**
  379. * Move the component /a aOffset.
  380. *
  381. * @param aOffset - Offset displacement.
  382. */
  383. void SetOffset( const wxPoint& aOffset );
  384. /**
  385. * Remove duplicate draw items from list.
  386. */
  387. void RemoveDuplicateDrawItems();
  388. /**
  389. * Test if component has more than one body conversion type (DeMorgan).
  390. *
  391. * @return True if component has more than one conversion.
  392. */
  393. bool HasConversion() const;
  394. /**
  395. * Test if alias /a aName is in component alias list.
  396. *
  397. * Alias name comparisons are case insensitive.
  398. *
  399. * @param aName - Name of alias.
  400. * @return True if alias name in alias list.
  401. */
  402. bool HasAlias( const wxChar* aName )
  403. {
  404. wxASSERT( aName != NULL );
  405. return m_AliasList.Index( aName ) != wxNOT_FOUND;
  406. }
  407. /**
  408. * Clears the status flag all draw objects in this component.
  409. */
  410. void ClearStatus();
  411. /**
  412. * Checks all draw objects of component to see if they are with block.
  413. *
  414. * Use this method to mark draw objects as selected during block
  415. * functions.
  416. *
  417. * @param aRect - The bounding rectangle to test in draw items are inside.
  418. * @param aUnit - The current unit number to test against.
  419. * @param aConvert - Are the draw items being selected a conversion.
  420. * @param aEditPinByPin - Used to ignore pin selections when in edit pin
  421. * by pin mode is enabled.
  422. * @return The number of draw objects found inside the block select
  423. * rectangle.
  424. */
  425. int SelectItems( EDA_Rect& aRect, int aUnit, int aConvert,
  426. bool aEditPinByPin );
  427. /**
  428. * Clears all the draw items marked by a block select.
  429. */
  430. void ClearSelectedItems();
  431. /**
  432. * Deletes the select draw items marked by a block select.
  433. *
  434. * The name and reference field will not be deleted. They are the
  435. * minimum drawing items required for any component. Their properties
  436. * can be changed but the cannot be removed.
  437. */
  438. void DeleteSelectedItems();
  439. /**
  440. * Move the selected draw items marked by a block select.
  441. */
  442. void MoveSelectedItems( const wxPoint& aOffset );
  443. /**
  444. * Make a copy of the selected draw items marked by a block select.
  445. *
  446. * Fields are not copied. Only component body items are copied.
  447. * Copying fields would result in duplicate fields which does not
  448. * make sense in this context.
  449. */
  450. void CopySelectedItems( const wxPoint& aOffset );
  451. /**
  452. * Horizontally (X axis) mirror selected draw items about a point.
  453. *
  454. * @param aCenter - Center point to mirror around.
  455. */
  456. void MirrorSelectedItemsH( const wxPoint& aCenter );
  457. /**
  458. * Locate a draw object.
  459. *
  460. * @param aUnit - Unit number of draw item.
  461. * @param aConvert - Body style of draw item.
  462. * @param aType - Draw object type, set to 0 to search for any type.
  463. * @param aPoint - Coordinate for hit testing.
  464. * @return The draw object if found. Otherwise NULL.
  465. */
  466. LIB_DRAW_ITEM* LocateDrawItem( int aUnit, int aConvert, KICAD_T aType,
  467. const wxPoint& aPoint );
  468. /**
  469. * Locate a draw object (overlaid)
  470. *
  471. * @param aUnit - Unit number of draw item.
  472. * @param aConvert - Body style of draw item.
  473. * @param aType - Draw object type, set to 0 to search for any type.
  474. * @param aPoint - Coordinate for hit testing.
  475. * @param aTransform = the transform matrix
  476. * @return The draw object if found. Otherwise NULL.
  477. */
  478. LIB_DRAW_ITEM* LocateDrawItem( int aUnit, int aConvert, KICAD_T aType,
  479. const wxPoint& aPoint,
  480. const int aTransfrom[2][2] );
  481. /**
  482. * Return a reference to the draw item list.
  483. *
  484. * @return LIB_DRAW_ITEM_LIST& - Reference to the draw item object list.
  485. */
  486. LIB_DRAW_ITEM_LIST& GetDrawItemList() { return drawings; }
  487. /**
  488. * Set the part per package count.
  489. *
  490. * If the count is greater than the current count, then the all of the
  491. * current draw items are duplicated for each additional part. If the
  492. * count is less than the current count, all draw objects for parts
  493. * greater that count are removed from the component.
  494. *
  495. * @param count - Number of parts per package.
  496. */
  497. void SetPartCount( int count );
  498. int GetPartCount() { return unitCount; }
  499. /** function IsMulti
  500. * @return true if the component has multiple parts per package.
  501. * When happens, the reference has a sub reference ti identify part
  502. */
  503. bool IsMulti() { return unitCount > 1; }
  504. /** function IsMulti
  505. * @return the sub reference for component having multiple parts per package.
  506. * The sub reference identify the part (or unit)
  507. * @param aUnit = the part identifier ( 1 to max count)
  508. * Note: this is a static function.
  509. */
  510. static wxString ReturnSubReference( int aUnit );
  511. /**
  512. * Set or clear the alternate body style (DeMorgan) for the component.
  513. *
  514. * If the component already has an alternate body style set and a
  515. * asConvert if false, all of the existing draw items for the alternate
  516. * body style are remove. If the alternate body style is not set and
  517. * asConvert is true, than the base draw items are duplicated and
  518. * added to the component.
  519. *
  520. * @param aSetConvert - Set or clear the component alternate body style.
  521. */
  522. void SetConversion( bool aSetConvert );
  523. };
  524. /**
  525. * Component library alias object definition.
  526. *
  527. * Component aliases are not really components. They are references
  528. * to an actual component object.
  529. */
  530. class LIB_ALIAS : public CMP_LIB_ENTRY
  531. {
  532. protected:
  533. /**
  534. * The actual component of the alias.
  535. *
  536. * @note - Do not delete the root component. The root component is owned
  537. * by library the component is part of. Deleting the root component
  538. * will likely cause EESchema to crash.
  539. * Or, if the root component is deleted, aliases must be deleted or their .root member
  540. * must be changed to point a new root component
  541. */
  542. LIB_COMPONENT* root;
  543. public:
  544. LIB_ALIAS( const wxString& aName, LIB_COMPONENT* aRootComponent,
  545. CMP_LIBRARY* aLibrary = NULL );
  546. LIB_ALIAS( LIB_ALIAS& aAlias, CMP_LIBRARY* aLibrary = NULL );
  547. ~LIB_ALIAS();
  548. virtual wxString GetClass() const
  549. {
  550. return wxT( "LIB_ALIAS" );
  551. }
  552. /**
  553. * Get the alias root component.
  554. */
  555. LIB_COMPONENT* GetComponent() const
  556. {
  557. return root;
  558. }
  559. /**
  560. * Set the alias root component.
  561. */
  562. void SetComponent( LIB_COMPONENT* aComponent );
  563. };
  564. #endif // CLASS_LIBENTRY_H