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.

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