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.

427 lines
14 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
17 years ago
18 years ago
18 years ago
18 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file sch_component.h
  26. * @brief Definition the SCH_COMPONENT class for Eeschema.
  27. */
  28. #ifndef COMPONENT_CLASS_H
  29. #define COMPONENT_CLASS_H
  30. #include <sch_field.h>
  31. #include <transform.h>
  32. #include <general.h>
  33. class SCH_SHEET_PATH;
  34. class LIB_ITEM;
  35. class LIB_PIN;
  36. class LIB_COMPONENT;
  37. class NETLIST_OBJECT_LIST;
  38. /// A container for several SCH_FIELD items
  39. typedef std::vector<SCH_FIELD> SCH_FIELDS;
  40. /**
  41. * Class SCH_COMPONENT
  42. * describes a real schematic component
  43. */
  44. class SCH_COMPONENT : public SCH_ITEM
  45. {
  46. friend class DIALOG_EDIT_COMPONENT_IN_SCHEMATIC;
  47. wxPoint m_Pos;
  48. wxString m_ChipName; ///< Name to look for in the library, i.e. "74LS00".
  49. int m_unit; ///< The unit for multiple part per package components.
  50. int m_convert; ///< The alternate body style for components that have more than
  51. ///< one body style defined. Primarily used for components that
  52. ///< have a De Morgan conversion.
  53. wxString m_prefix; ///< C, R, U, Q etc - the first character which typically indicates
  54. ///< what the component is. Determined, upon placement, from the
  55. ///< library component. Created upon file load, by the first
  56. ///< non-digits in the reference fields.
  57. TRANSFORM m_transform; ///< The rotation/mirror transformation matrix.
  58. SCH_FIELDS m_Fields; ///< Variable length list of fields.
  59. /**
  60. * A temporary sheet path is required to generate the correct reference designator string
  61. * in complex heirarchies. Hopefully this is only a temporary hack to decouple schematic
  62. * objects from the drawing window until a better design for handling complex heirarchies
  63. * can be implemented.
  64. */
  65. const SCH_SHEET_PATH* m_currentSheetPath;
  66. /**
  67. * Defines the hierarchical path and reference of the component. This allows support
  68. * for hierarchical sheets that reference the same schematic. The format for the path
  69. * is /&ltsheet time stamp&gt/&ltsheet time stamp&gt/.../&lscomponent time stamp&gt.
  70. * A single / denotes the root sheet.
  71. */
  72. wxArrayString m_PathsAndReferences;
  73. void Init( const wxPoint& pos = wxPoint( 0, 0 ) );
  74. EDA_RECT GetBodyBoundingBox() const;
  75. public:
  76. SCH_COMPONENT( const wxPoint& pos = wxPoint( 0, 0 ), SCH_ITEM* aParent = NULL );
  77. /**
  78. * Create schematic component from library component object.
  79. *
  80. * @param libComponent - Component library object to create schematic
  81. * component from.
  82. * @param sheet - Schematic sheet the component is place into.
  83. * @param unit - Part for components that have multiple parts per
  84. * package.
  85. * @param convert - Use the alternate body style for the schematic
  86. * component.
  87. * @param pos - Position to place new component.
  88. * @param setNewItemFlag - Set the component IS_NEW and IS_MOVED flags.
  89. */
  90. SCH_COMPONENT( LIB_COMPONENT& libComponent, SCH_SHEET_PATH* sheet,
  91. int unit = 0, int convert = 0,
  92. const wxPoint& pos = wxPoint( 0, 0 ),
  93. bool setNewItemFlag = false );
  94. /**
  95. * Copy Constructor
  96. * clones \a aComponent into a new object. All fields are copied as is except
  97. * for the linked list management pointers which are set to NULL, and the
  98. * SCH_FIELD's m_Parent pointers which are set to the new parent,
  99. * i.e. this new object.
  100. */
  101. SCH_COMPONENT( const SCH_COMPONENT& aComponent );
  102. ~SCH_COMPONENT() { }
  103. wxString GetClass() const
  104. {
  105. return wxT( "SCH_COMPONENT" );
  106. }
  107. wxString GetLibName() const { return m_ChipName; }
  108. void SetLibName( const wxString& aName );
  109. int GetUnit() const { return m_unit; }
  110. /**
  111. * change the unit id to aUnit
  112. * has maening only for multiple parts per package
  113. * Also set the modified flag bit
  114. * @param aUnit = the new unit Id
  115. */
  116. void SetUnit( int aUnit );
  117. /**
  118. * change the unit id to aUnit
  119. * has maening only for multiple parts per package
  120. * Do not change the modified flag bit, and should be used when
  121. * change is not due to an edition command
  122. * @param aUnit = the new unit Id
  123. */
  124. void UpdateUnit( int aUnit );
  125. int GetConvert() const { return m_convert; }
  126. void SetConvert( int aConvert );
  127. wxString GetPrefix() const { return m_prefix; }
  128. TRANSFORM& GetTransform() const { return const_cast< TRANSFORM& >( m_transform ); }
  129. void SetTransform( const TRANSFORM& aTransform );
  130. /**
  131. * Function GetPartCount
  132. * returns the number of parts per package of the component.
  133. *
  134. * @return The number of parts per package or zero if the library entry cannot be found.
  135. */
  136. int GetPartCount() const;
  137. bool Save( FILE* aFile ) const;
  138. bool Load( LINE_READER& aLine, wxString& aErrorMsg );
  139. /**
  140. * Function SetOrientation
  141. * computes the new transform matrix based on \a aOrientation for the component which is
  142. * applied to the current transform.
  143. * @param aOrientation The orientation to apply to the transform.
  144. */
  145. void SetOrientation( int aOrientation );
  146. /**
  147. * Function GetOrientation
  148. * Used to display component orientation (in dialog editor or info)
  149. * @return the orientation and mirror
  150. * Note: Because there are different ways to have a given orientation/mirror,
  151. * the orientation/mirror is not necessary what the used does
  152. * (example : a mirrorX then a mirrorY give no mirror but rotate the
  153. * component).
  154. * So this function find a rotation and a mirror value
  155. * ( CMP_MIRROR_X because this is the first mirror option tested)
  156. * but can differs from the orientation made by an user
  157. * ( a CMP_MIRROR_Y is find as a CMP_MIRROR_X + orientation 180, because
  158. * they are equivalent)
  159. */
  160. int GetOrientation();
  161. /**
  162. * Function GetScreenCoord
  163. * Returns the coordinated point relative to the orientation of the component of \a aPoint.
  164. * The coordinates are always relative to the anchor position of the component.
  165. * @param aPoint The coordinates to transform.
  166. * @return The transformed point.
  167. */
  168. wxPoint GetScreenCoord( const wxPoint& aPoint );
  169. void GetMsgPanelInfo( std::vector< MSG_PANEL_ITEM >& aList );
  170. /**
  171. * Function ClearAnnotation
  172. * clears exiting component annotation ( i.i IC23 changed to IC? and part reset to 1)
  173. * @param aSheetPath: SCH_SHEET_PATH value: if NULL remove all annotations,
  174. * else remove annotation relative to this sheetpath
  175. */
  176. void ClearAnnotation( SCH_SHEET_PATH* aSheetPath );
  177. /**
  178. * Function SetTimeStamp
  179. * changes the time stamp to \a aNewTimeStamp updates the reference path.
  180. * @see m_PathsAndReferences
  181. * @param aNewTimeStamp = new time stamp
  182. */
  183. void SetTimeStamp( time_t aNewTimeStamp );
  184. EDA_RECT GetBoundingBox() const;
  185. //-----<Fields>-----------------------------------------------------------
  186. /**
  187. * Function GetField
  188. * returns a field.
  189. * @param aFieldNdx An index into the array of fields, not a field id.
  190. * @return SCH_FIELD* - the field value or NULL if does not exist
  191. */
  192. SCH_FIELD* GetField( int aFieldNdx ) const;
  193. /**
  194. * Function AddField
  195. * adds a field to the component. The field is copied as it is put into
  196. * the component.
  197. * @param aField A const reference to the SCH_FIELD to add.
  198. * @return SCH_FIELD* - the newly inserted field.
  199. */
  200. SCH_FIELD* AddField( const SCH_FIELD& aField );
  201. /**
  202. * Function FindField
  203. * searches for SCH_FIELD with \a aFieldName and returns it if found, else NULL.
  204. */
  205. SCH_FIELD* FindField( const wxString& aFieldName );
  206. void SetFields( const SCH_FIELDS& aFields )
  207. {
  208. m_Fields = aFields; // vector copying, length is changed possibly
  209. }
  210. //-----</Fields>----------------------------------------------------------
  211. /**
  212. * Function GetFieldCount
  213. * returns the number of fields in this component.
  214. */
  215. int GetFieldCount() const { return (int) m_Fields.size(); }
  216. /**
  217. * Function GetPin
  218. * finds a component pin by number.
  219. *
  220. * @param number - The number of the pin to find.
  221. * @return Pin object if found, otherwise NULL.
  222. */
  223. LIB_PIN* GetPin( const wxString& number );
  224. void Draw( EDA_DRAW_PANEL* panel,
  225. wxDC* DC,
  226. const wxPoint& offset,
  227. GR_DRAWMODE draw_mode,
  228. EDA_COLOR_T Color = UNSPECIFIED_COLOR )
  229. {
  230. Draw( panel, DC, offset, draw_mode, Color, true );
  231. }
  232. void Draw( EDA_DRAW_PANEL* panel,
  233. wxDC* DC,
  234. const wxPoint& offset,
  235. GR_DRAWMODE draw_mode,
  236. EDA_COLOR_T Color,
  237. bool DrawPinText );
  238. void SwapData( SCH_ITEM* aItem );
  239. // returns a unique ID, in the form of a path.
  240. wxString GetPath( const SCH_SHEET_PATH* sheet ) const;
  241. /**
  242. * Function IsReferenceStringValid (static)
  243. * Tests for an acceptable reference string
  244. * An acceptable reference string must support unannotation
  245. * i.e starts by letter
  246. * @param aReferenceString = the reference string to validate
  247. * @return true if OK
  248. */
  249. static bool IsReferenceStringValid( const wxString& aReferenceString );
  250. void SetCurrentSheetPath( const SCH_SHEET_PATH* aSheetPath )
  251. {
  252. m_currentSheetPath = aSheetPath;
  253. }
  254. /**
  255. * Function GetRef
  256. * returns the reference, for the given sheet path.
  257. */
  258. const wxString GetRef( const SCH_SHEET_PATH* sheet );
  259. /**
  260. * Set the reference, for the given sheet path.
  261. */
  262. void SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref );
  263. /**
  264. * Function AddHierarchicalReference
  265. * adds a full hierarchical reference (path + local reference)
  266. * @param aPath Hierarchical path (/&ltsheet timestamp&gt/&ltcomponent
  267. * timestamp&gt like /05678E50/A23EF560)
  268. * @param aRef :local reference like C45, R56
  269. * @param aMulti Part selection, used in multi part per package (0 or 1 for non multi)
  270. */
  271. void AddHierarchicalReference( const wxString& aPath,
  272. const wxString& aRef,
  273. int aMulti );
  274. // returns the unit selection, for the given sheet path.
  275. int GetUnitSelection( SCH_SHEET_PATH* aSheet );
  276. // Set the unit selection, for the given sheet path.
  277. void SetUnitSelection( SCH_SHEET_PATH* aSheet, int aUnitSelection );
  278. // Geometric transforms (used in block operations):
  279. void Move( const wxPoint& aMoveVector )
  280. {
  281. if( aMoveVector == wxPoint( 0, 0 ) )
  282. return;
  283. m_Pos += aMoveVector;
  284. for( int ii = 0; ii < GetFieldCount(); ii++ )
  285. GetField( ii )->Move( aMoveVector );
  286. SetModified();
  287. }
  288. void MirrorY( int aYaxis_position );
  289. void MirrorX( int aXaxis_position );
  290. void Rotate( wxPoint aPosition );
  291. bool Matches( wxFindReplaceData& aSearchData, void* aAuxData, wxPoint* aFindLocation );
  292. void GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList );
  293. wxPoint GetPinPhysicalPosition( LIB_PIN* Pin );
  294. bool IsSelectStateChanged( const wxRect& aRect );
  295. bool IsConnectable() const { return true; }
  296. /**
  297. * @return true if the component is in netlist
  298. * which means this is not a power component, or something
  299. * like a component reference starting by #
  300. */
  301. bool IsInNetlist() const;
  302. void GetConnectionPoints( vector< wxPoint >& aPoints ) const;
  303. SEARCH_RESULT Visit( INSPECTOR* inspector, const void* testData,
  304. const KICAD_T scanTypes[] );
  305. /**
  306. * Function GetDrawItem().
  307. * Return the component library item at \a aPosition that is part of this component.
  308. *
  309. * @param aPosition - Schematic position of the component library object.
  310. * @param aType - Type of component library object to find or any if set to TYPE_NOT_INIT.
  311. * @return A pointer to the component library object if found, otherwise NULL.
  312. */
  313. LIB_ITEM* GetDrawItem( const wxPoint& aPosition, KICAD_T aType = TYPE_NOT_INIT );
  314. wxString GetSelectMenuText() const;
  315. BITMAP_DEF GetMenuImage() const { return add_component_xpm; }
  316. void GetNetListItem( NETLIST_OBJECT_LIST& aNetListItems,
  317. SCH_SHEET_PATH* aSheetPath );
  318. bool operator <( const SCH_ITEM& aItem ) const;
  319. bool operator==( const SCH_COMPONENT& aComponent) const;
  320. bool operator!=( const SCH_COMPONENT& aComponent) const;
  321. SCH_ITEM& operator=( const SCH_ITEM& aItem );
  322. bool IsReplaceable() const { return true; }
  323. wxPoint GetPosition() const { return m_Pos; }
  324. void SetPosition( const wxPoint& aPosition ) { Move( aPosition - m_Pos ); }
  325. bool HitTest( const wxPoint& aPosition, int aAccuracy ) const;
  326. bool HitTest( const EDA_RECT& aRect, bool aContained = false, int aAccuracy = 0 ) const;
  327. void Plot( PLOTTER* aPlotter );
  328. EDA_ITEM* Clone() const;
  329. #if defined(DEBUG)
  330. void Show( int nestLevel, std::ostream& os ) const; // override
  331. #endif
  332. private:
  333. bool doIsConnected( const wxPoint& aPosition ) const;
  334. };
  335. #endif /* COMPONENT_CLASS_H */