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.

409 lines
14 KiB

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) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2004-2011 KiCad Developers, see change_log.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_item_struct.h
  26. * @brief Base schematic object class definition.
  27. */
  28. #ifndef SCH_ITEM_STRUCT_H
  29. #define SCH_ITEM_STRUCT_H
  30. #include <vector>
  31. #include <class_base_screen.h>
  32. #include <general.h>
  33. class SCH_ITEM;
  34. class SCH_SHEET_PATH;
  35. class LINE_READER;
  36. class SCH_EDIT_FRAME;
  37. class wxFindReplaceData;
  38. class PLOTTER;
  39. class NETLIST_OBJECT;
  40. class NETLIST_OBJECT_LIST;
  41. typedef boost::ptr_vector< SCH_ITEM > SCH_ITEMS;
  42. typedef SCH_ITEMS::iterator SCH_ITEMS_ITR;
  43. typedef std::vector< SCH_ITEMS_ITR > SCH_ITEMS_ITRS;
  44. #define FMT_IU SCH_ITEM::FormatInternalUnits
  45. #define FMT_ANGLE SCH_ITEM::FormatAngle
  46. /// Flag to enable find item tracing using the WXTRACE environment variable. This
  47. /// flag generates a lot of debug output.
  48. extern const wxString traceFindItem;
  49. enum DANGLING_END_T {
  50. UNKNOWN = 0,
  51. WIRE_START_END,
  52. WIRE_END_END,
  53. BUS_START_END,
  54. BUS_END_END,
  55. JUNCTION_END,
  56. PIN_END,
  57. LABEL_END,
  58. ENTRY_END,
  59. SHEET_LABEL_END
  60. };
  61. /**
  62. * Class DANGLING_END_ITEM
  63. * is a helper class used to store the state of schematic items that can be connected to
  64. * other schematic items.
  65. */
  66. class DANGLING_END_ITEM
  67. {
  68. private:
  69. /// A pointer to the connectable object.
  70. const EDA_ITEM* m_item;
  71. /// The position of the connection point.
  72. wxPoint m_pos;
  73. /// The type of connection of #m_item.
  74. DANGLING_END_T m_type;
  75. public:
  76. DANGLING_END_ITEM( DANGLING_END_T aType, const EDA_ITEM* aItem, const wxPoint& aPosition )
  77. {
  78. m_item = aItem;
  79. m_type = aType;
  80. m_pos = aPosition;
  81. }
  82. wxPoint GetPosition() const { return m_pos; }
  83. const EDA_ITEM* GetItem() const { return m_item; }
  84. DANGLING_END_T GetType() const { return m_type; }
  85. };
  86. /**
  87. * Class SCH_ITEM
  88. * is a base class for any item which can be embedded within the SCHEMATIC
  89. * container class, and therefore instances of derived classes should only be
  90. * found in EESCHEMA or other programs that use class SCHEMATIC and its contents.
  91. * The corresponding class in Pcbnew is BOARD_ITEM.
  92. */
  93. class SCH_ITEM : public EDA_ITEM
  94. {
  95. protected:
  96. LayerNumber m_Layer;
  97. EDA_ITEMS m_connections; ///< List of items connected to this item.
  98. public:
  99. SCH_ITEM( EDA_ITEM* aParent, KICAD_T aType );
  100. SCH_ITEM( const SCH_ITEM& aItem );
  101. ~SCH_ITEM();
  102. virtual wxString GetClass() const
  103. {
  104. return wxT( "SCH_ITEM" );
  105. }
  106. /**
  107. * Function SwapData
  108. * swap the internal data structures \a aItem with the schematic item.
  109. * Obviously, aItem must have the same type than me
  110. * @param aItem The item to swap the data structures with.
  111. */
  112. virtual void SwapData( SCH_ITEM* aItem );
  113. SCH_ITEM* Next() const { return static_cast<SCH_ITEM*>( Pnext ); }
  114. SCH_ITEM* Back() const { return static_cast<SCH_ITEM*>( Pback ); }
  115. /**
  116. * Function GetLayer
  117. * returns the layer this item is on.
  118. */
  119. LayerNumber GetLayer() const { return m_Layer; }
  120. /**
  121. * Function SetLayer
  122. * sets the layer this item is on.
  123. * @param aLayer The layer number.
  124. */
  125. void SetLayer( LayerNumber aLayer ) { m_Layer = aLayer; }
  126. /**
  127. * Function GetPenSize virtual pure
  128. * @return the size of the "pen" that be used to draw or plot this item
  129. */
  130. virtual int GetPenSize() const { return 0; }
  131. /**
  132. * Function Draw
  133. */
  134. virtual void Draw( EDA_DRAW_PANEL* aPanel,
  135. wxDC* aDC,
  136. const wxPoint& aOffset,
  137. GR_DRAWMODE aDrawMode,
  138. EDA_COLOR_T aColor = UNSPECIFIED_COLOR ) = 0;
  139. /**
  140. * Function Move
  141. * moves the item by \a aMoveVector to a new position.
  142. * @param aMoveVector = the displacement vector
  143. */
  144. virtual void Move( const wxPoint& aMoveVector ) = 0;
  145. /**
  146. * Function MirrorY
  147. * mirrors item relative to the Y axis about \a aYaxis_position.
  148. * @param aYaxis_position The Y axis position to mirror around.
  149. */
  150. virtual void MirrorY( int aYaxis_position ) = 0;
  151. /**
  152. * Function MirrorX
  153. * mirrors item relative to the X axis about \a aXaxis_position.
  154. * @param aXaxis_position The X axis position to mirror around.
  155. */
  156. virtual void MirrorX( int aXaxis_position ) = 0;
  157. /**
  158. * Function Rotate
  159. * rotates the item around \a aPosition 90 degrees in the clockwise direction.
  160. * @param aPosition A reference to a wxPoint object containing the coordinates to
  161. * rotate around.
  162. */
  163. virtual void Rotate( wxPoint aPosition ) = 0;
  164. /**
  165. * Function Save
  166. * writes the data structures for this object out to a FILE in "*.sch" format.
  167. * @param aFile The FILE to write to.
  168. * @return bool - true if success writing else false.
  169. */
  170. virtual bool Save( FILE* aFile ) const = 0;
  171. /**
  172. * Function Load
  173. * reads a schematic item from \a aLine in a .sch file.
  174. *
  175. * @param aLine - Essentially this is file to read the object from.
  176. * @param aErrorMsg - Description of the error if an error occurs while loading the object.
  177. * @return True if the object loaded successfully.
  178. */
  179. virtual bool Load( LINE_READER& aLine, wxString& aErrorMsg ) { return false; }
  180. /**
  181. * Function GetEndPoints
  182. * adds the schematic item end points to \a aItemList if the item has end points.
  183. *
  184. * The default version doesn't do anything since many of the schematic object cannot
  185. * be tested for dangling ends. If you add a new schematic item that can have a
  186. * dangling end ( no connect ), override this method to provide the correct end
  187. * points.
  188. *
  189. * @param aItemList - List of DANGLING_END_ITEMS to add to.
  190. */
  191. virtual void GetEndPoints( std::vector< DANGLING_END_ITEM >& aItemList ) {}
  192. /**
  193. * Function IsDanglingStateChanged
  194. * tests the schematic item to \a aItemList to check if it's dangling state has changed.
  195. *
  196. * Note that the return value only true when the state of the test has changed. Use
  197. * the IsDangling() method to get the current dangling state of the item. Some of
  198. * the schematic objects cannot be tested for a dangling state, the default method
  199. * always returns false. Only override the method if the item can be tested for a
  200. * dangling state.
  201. *
  202. * @param aItemList - List of items to test item against.
  203. * @return True if the dangling state has changed from it's current setting.
  204. */
  205. virtual bool IsDanglingStateChanged( std::vector< DANGLING_END_ITEM >& aItemList ) { return false; }
  206. virtual bool IsDangling() const { return false; }
  207. /**
  208. * Function IsSelectStateChanged
  209. * checks if the selection state of an item inside \a aRect has changed.
  210. *
  211. * This is used by the block selection code to verify if an item is selected or not.
  212. * True is be return anytime the select state changes. If you need to know the
  213. * the current selection state, use the IsSelected() method.
  214. *
  215. * @param aRect - Rectangle to test against.
  216. */
  217. virtual bool IsSelectStateChanged( const wxRect& aRect ) { return false; }
  218. /**
  219. * Function IsConnectable
  220. * returns true if the schematic item can connect to another schematic item.
  221. */
  222. virtual bool IsConnectable() const { return false; }
  223. /**
  224. * Function GetConnectionPoints
  225. * add all the connection points for this item to \a aPoints.
  226. *
  227. * Not all schematic items have connection points so the default method does nothing.
  228. *
  229. * @param aPoints List of connection points to add to.
  230. */
  231. virtual void GetConnectionPoints( std::vector< wxPoint >& aPoints ) const { }
  232. /**
  233. * Function ClearConnections
  234. * clears all of the connection items from the list.
  235. *
  236. * The vector release method is used to prevent the item pointers from being deleted.
  237. * Do not use the vector erase method on the connection list.
  238. */
  239. void ClearConnections() { m_connections.clear(); }
  240. /**
  241. * Function IsConnected
  242. * tests the item to see if it is connected to \a aPoint.
  243. *
  244. * @param aPoint A reference to a wxPoint object containing the coordinates to test.
  245. * @return True if connection to \a aPoint exists.
  246. */
  247. bool IsConnected( const wxPoint& aPoint ) const;
  248. /** @copydoc EDA_ITEM::HitTest(const wxPoint&) */
  249. virtual bool HitTest( const wxPoint& aPosition ) const
  250. {
  251. return HitTest( aPosition, 0 );
  252. }
  253. /**
  254. * Function HitTest
  255. * tests if \a aPosition is contained within or on the bounding box of an item.
  256. *
  257. * @param aPosition A reference to a wxPoint object containing the coordinates to test.
  258. * @param aAccuracy Increase the item bounding box by this amount.
  259. * @return True if \a aPosition is within the item bounding box.
  260. */
  261. virtual bool HitTest( const wxPoint& aPosition, int aAccuracy ) const { return false; }
  262. /**
  263. * Function HitTest
  264. * tests if \a aRect intersects or is contained within the bounding box of an item.
  265. *
  266. * @param aRect A reference to a EDA_RECT object containing the rectangle to test.
  267. * @param aContained Set to true to test for containment instead of an intersection.
  268. * @param aAccuracy Increase \a aRect by this amount.
  269. * @return True if \a aRect contains or intersects the item bounding box.
  270. */
  271. virtual bool HitTest( const EDA_RECT& aRect, bool aContained = false, int aAccuracy = 0 ) const
  272. {
  273. return false;
  274. }
  275. virtual bool CanIncrementLabel() const { return false; }
  276. /**
  277. * Function Plot
  278. * plots the schematic item to \a aPlotter.
  279. *
  280. * @param aPlotter A pointer to a #PLOTTER object.
  281. */
  282. virtual void Plot( PLOTTER* aPlotter );
  283. /**
  284. * Function GetNetListItem
  285. * creates a new #NETLIST_OBJECT for the schematic object and adds it to
  286. * \a aNetListItems.
  287. * <p>
  288. * Not all schematic objects have net list items associated with them. This
  289. * method only needs to be overridden for those schematic objects that have
  290. * net list objects associated with them.
  291. * </p>
  292. */
  293. virtual void GetNetListItem( NETLIST_OBJECT_LIST& aNetListItems,
  294. SCH_SHEET_PATH* aSheetPath ) { }
  295. /**
  296. * Function GetPosition
  297. * @return A wxPoint object containing the schematic item position.
  298. */
  299. virtual wxPoint GetPosition() const = 0;
  300. /**
  301. * Function SetPosition
  302. * set the schematic item position to \a aPosition.
  303. *
  304. * @param aPosition A reference to a wxPoint object containing the new position.
  305. */
  306. virtual void SetPosition( const wxPoint& aPosition ) = 0;
  307. virtual bool operator <( const SCH_ITEM& aItem ) const;
  308. /**
  309. * Function FormatInternalUnits
  310. * converts \a aValue from schematic internal units to a string appropriate for writing
  311. * to file.
  312. *
  313. * @param aValue A coordinate value to convert.
  314. * @return A std::string object containing the converted value.
  315. */
  316. static std::string FormatInternalUnits( int aValue );
  317. /**
  318. * Function FormatAngle
  319. * converts \a aAngle from board units to a string appropriate for writing to file.
  320. *
  321. * @note Internal angles for board items can be either degrees or tenths of degree
  322. * on how KiCad is built.
  323. * @param aAngle A angle value to convert.
  324. * @return A std::string object containing the converted angle.
  325. */
  326. static std::string FormatAngle( double aAngle );
  327. static std::string FormatInternalUnits( const wxPoint& aPoint );
  328. static std::string FormatInternalUnits( const wxSize& aSize );
  329. private:
  330. /**
  331. * Function doIsConnected
  332. * provides the object specific test to see if it is connected to \a aPosition.
  333. *
  334. * @note Override this function if the derived object can be connect to another
  335. * object such as a wire, bus, or junction. Do not override this function
  336. * for objects that cannot have connections. The default will always return
  337. * false. This functions is call through the public function IsConnected()
  338. * which performs tests common to all schematic items before calling the
  339. * item specific connection testing.
  340. *
  341. * @param aPosition A reference to a wxPoint object containing the test position.
  342. * @return True if connection to \a aPosition exists.
  343. */
  344. virtual bool doIsConnected( const wxPoint& aPosition ) const { return false; }
  345. };
  346. extern bool sort_schematic_items( const SCH_ITEM* aItem1, const SCH_ITEM* aItem2 );
  347. #endif /* SCH_ITEM_STRUCT_H */