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.

338 lines
12 KiB

7 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-2021 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. #ifndef _SCH_LINE_H_
  25. #define _SCH_LINE_H_
  26. #include <sch_item.h>
  27. #include <wx/pen.h> // for wxPenStyle
  28. #include <list> // for std::list
  29. class NETLIST_OBJECT_LIST;
  30. /**
  31. * Segment description base class to describe items which have 2 end points (track, wire,
  32. * draw line ...)
  33. */
  34. class SCH_LINE : public SCH_ITEM
  35. {
  36. public:
  37. static const enum wxPenStyle PenStyle[];
  38. SCH_LINE( const VECTOR2I& pos = VECTOR2I( 0, 0 ), int layer = LAYER_NOTES );
  39. SCH_LINE( const VECTOR2D& pos, int layer = LAYER_NOTES ) :
  40. SCH_LINE( VECTOR2I( pos.x, pos.y ), layer )
  41. {}
  42. SCH_LINE( const SCH_LINE& aLine );
  43. ~SCH_LINE() { }
  44. static inline bool ClassOf( const EDA_ITEM* aItem )
  45. {
  46. return aItem && SCH_LINE_T == aItem->Type();
  47. }
  48. wxString GetClass() const override
  49. {
  50. return wxT( "SCH_LINE" );
  51. }
  52. /**
  53. * @brief This function travel though all the connected wire segments
  54. * to look for connected labels.
  55. * @param aSheet - the sheet where the current wire segment is located
  56. * @return returns the name of the wire if connected labels found, otherwise empty string
  57. */
  58. wxString GetNetname(const SCH_SHEET_PATH &aSheet);
  59. bool IsType( const std::vector<KICAD_T>& aScanTypes ) const override
  60. {
  61. if( SCH_ITEM::IsType( aScanTypes ) )
  62. return true;
  63. for( KICAD_T scanType : aScanTypes )
  64. {
  65. if( scanType == SCH_ITEM_LOCATE_WIRE_T && m_layer == LAYER_WIRE )
  66. return true;
  67. if ( scanType == SCH_ITEM_LOCATE_BUS_T && m_layer == LAYER_BUS )
  68. return true;
  69. if ( scanType == SCH_ITEM_LOCATE_GRAPHIC_LINE_T && m_layer == LAYER_NOTES )
  70. return true;
  71. }
  72. return false;
  73. }
  74. bool IsEndPoint( const VECTOR2I& aPoint ) const
  75. {
  76. return aPoint == m_start || aPoint == m_end;
  77. }
  78. int GetAngleFrom( const VECTOR2I& aPoint ) const;
  79. int GetReverseAngleFrom( const VECTOR2I& aPoint ) const;
  80. /**
  81. * Gets the angle between the start and end lines.
  82. *
  83. * @return Line angle in radians.
  84. */
  85. inline EDA_ANGLE Angle() const
  86. {
  87. return ( EDA_ANGLE( (VECTOR2I) m_end - (VECTOR2I) m_start ) );
  88. }
  89. /**
  90. * Saves the current line angle. Useful when dragging a line and its important to
  91. * be able to restart the line from length 0 in the correct direction.
  92. */
  93. inline void StoreAngle()
  94. {
  95. if( !IsNull() )
  96. m_storedAngle = Angle();
  97. }
  98. inline void StoreAngle( const EDA_ANGLE& aAngle ) { m_storedAngle = aAngle; }
  99. /**
  100. * Returns the angle stored by StoreAngle()
  101. *
  102. * @return Stored angle in radians.
  103. */
  104. inline EDA_ANGLE GetStoredAngle() const { return m_storedAngle; }
  105. /**
  106. * Checks if line is orthogonal (to the grid).
  107. *
  108. * @return True if orthogonal, false if not or the line is zero length.
  109. */
  110. inline bool IsOrthogonal() const { return Angle().IsCardinal(); }
  111. bool IsNull() const { return m_start == m_end; }
  112. VECTOR2I GetStartPoint() const { return m_start; }
  113. void SetStartPoint( const VECTOR2I& aPosition ) { m_start = aPosition; }
  114. VECTOR2I GetMidPoint() const { return ( m_start + m_end ) / 2; }
  115. VECTOR2I GetEndPoint() const { return m_end; }
  116. void SetEndPoint( const VECTOR2I& aPosition ) { m_end = aPosition; }
  117. void SetLastResolvedState( const SCH_ITEM* aItem ) override
  118. {
  119. const SCH_LINE* aLine = dynamic_cast<const SCH_LINE*>( aItem );
  120. if( aLine )
  121. {
  122. m_lastResolvedLineStyle = aLine->m_lastResolvedLineStyle;
  123. m_lastResolvedWidth = aLine->m_lastResolvedWidth;
  124. m_lastResolvedColor = aLine->m_lastResolvedColor;
  125. }
  126. }
  127. void SetLineStyle( const PLOT_DASH_TYPE aStyle );
  128. void SetLineStyle( const int aStyleId );
  129. PLOT_DASH_TYPE GetLineStyle() const;
  130. /// @return the style that the line should be drawn in
  131. /// this might be set on the line or inherited from the line's netclass
  132. PLOT_DASH_TYPE GetEffectiveLineStyle() const;
  133. void SetLineColor( const COLOR4D& aColor );
  134. void SetLineColor( const double r, const double g, const double b, const double a );
  135. /// Returns COLOR4D::UNSPECIFIED if a custom color hasn't been set for this line
  136. COLOR4D GetLineColor() const;
  137. void SetLineWidth( const int aSize );
  138. virtual bool HasLineStroke() const override { return true; }
  139. virtual STROKE_PARAMS GetStroke() const override { return m_stroke; }
  140. virtual void SetStroke( const STROKE_PARAMS& aStroke ) override { m_stroke = aStroke; }
  141. bool IsStrokeEquivalent( const SCH_LINE* aLine )
  142. {
  143. if( m_stroke.GetWidth() != aLine->GetStroke().GetWidth() )
  144. return false;
  145. if( m_stroke.GetColor() != aLine->GetStroke().GetColor() )
  146. return false;
  147. PLOT_DASH_TYPE style_a = m_stroke.GetPlotStyle();
  148. PLOT_DASH_TYPE style_b = aLine->GetStroke().GetPlotStyle();
  149. return style_a == style_b
  150. || ( style_a == PLOT_DASH_TYPE::DEFAULT && style_b == PLOT_DASH_TYPE::SOLID )
  151. || ( style_a == PLOT_DASH_TYPE::SOLID && style_b == PLOT_DASH_TYPE::DEFAULT );
  152. }
  153. int GetLineSize() const { return m_stroke.GetWidth(); }
  154. void ViewGetLayers( int aLayers[], int& aCount ) const override;
  155. const BOX2I GetBoundingBox() const override;
  156. /**
  157. * @return The length of the line segment.
  158. */
  159. double GetLength() const;
  160. void Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset ) override;
  161. int GetPenWidth() const override;
  162. void Move( const VECTOR2I& aMoveVector ) override;
  163. void MoveStart( const VECTOR2I& aMoveVector );
  164. void MoveEnd( const VECTOR2I& aMoveVector );
  165. void MirrorVertically( int aCenter ) override;
  166. void MirrorHorizontally( int aCenter ) override;
  167. void Rotate( const VECTOR2I& aCenter ) override;
  168. void RotateStart( const VECTOR2I& aCenter );
  169. void RotateEnd( const VECTOR2I& aCenter );
  170. /**
  171. * Check line against \a aLine to see if it overlaps and merge if it does.
  172. *
  173. * This method will return an equivalent of the union of line and \a aLine if the
  174. * two lines overlap. This method is used to merge multiple line segments into a single
  175. * line.
  176. *
  177. * @param aScreen is the current screen.
  178. * @param aLine is the line to compare.
  179. * @param aCheckJunctions is used to indicate if we need to check for a junction if the two
  180. * segments are colinear and touch.
  181. * @return New line that combines the two or NULL on non-overlapping segments.
  182. */
  183. SCH_LINE* MergeOverlap( SCH_SCREEN* aScreen, SCH_LINE* aLine, bool aCheckJunctions );
  184. bool IsParallel( const SCH_LINE* aLine ) const;
  185. void GetEndPoints( std::vector<DANGLING_END_ITEM>& aItemList ) override;
  186. bool UpdateDanglingState( std::vector<DANGLING_END_ITEM>& aItemList,
  187. const SCH_SHEET_PATH* aPath = nullptr ) override;
  188. bool IsStartDangling() const { return m_startIsDangling; }
  189. bool IsEndDangling() const { return m_endIsDangling; }
  190. bool IsDangling() const override { return m_startIsDangling || m_endIsDangling; }
  191. bool IsConnectable() const override;
  192. std::vector<VECTOR2I> GetConnectionPoints() const override;
  193. bool ConnectionPropagatesTo( const EDA_ITEM* aItem ) const override;
  194. void GetSelectedPoints( std::vector<VECTOR2I>& aPoints ) const;
  195. bool CanConnect( const SCH_ITEM* aItem ) const override;
  196. wxString GetSelectMenuText( UNITS_PROVIDER* aUnitsProvider ) const override;
  197. BITMAPS GetMenuImage() const override;
  198. bool operator <( const SCH_ITEM& aItem ) const override;
  199. VECTOR2I GetPosition() const override { return m_start; }
  200. void SetPosition( const VECTOR2I& aPosition ) override;
  201. VECTOR2I GetSortPosition() const override { return GetMidPoint(); }
  202. bool IsPointClickableAnchor( const VECTOR2I& aPos ) const override
  203. {
  204. return ( GetStartPoint() == aPos && IsStartDangling() )
  205. || ( GetEndPoint() == aPos && IsEndDangling() );
  206. }
  207. bool HitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const override;
  208. bool HitTest( const BOX2I& aRect, bool aContained, int aAccuracy = 0 ) const override;
  209. void Plot( PLOTTER* aPlotter, bool aBackground ) const override;
  210. EDA_ITEM* Clone() const override;
  211. void SwapData( SCH_ITEM* aItem ) override;
  212. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  213. #if defined(DEBUG)
  214. void Show( int nestLevel, std::ostream& os ) const override;
  215. #endif
  216. /**
  217. * Return if the line is a graphic (non electrical line)
  218. *
  219. * Currently, anything on the internal NOTES layer is a graphic line
  220. */
  221. bool IsGraphicLine() const;
  222. /**
  223. * Return true if the line is a wire.
  224. *
  225. * @return true if this line is on the wire layer.
  226. */
  227. bool IsWire() const;
  228. /**
  229. * Return true if the line is a bus.
  230. *
  231. * @return true if this line is on the bus layer.
  232. */
  233. bool IsBus() const;
  234. private:
  235. /**
  236. * @brief Recursively called function to travel through the connected wires and find a connected
  237. * net name label
  238. * @param line - the wire segment to start the recursive lookup
  239. * @param checkedLines - a lsit containing the already checked wire segments, to prevent the
  240. * infinite recursion in the case if someone draws a rectangle for e.g.
  241. * @param aSheet - the sheet where the lookup is performed
  242. * @return With the net name if a connected label found, otherwise with an empty string
  243. */
  244. wxString FindWireSegmentNetNameRecursive( SCH_LINE *line, std::list<const SCH_LINE*>& checkedLines,
  245. const SCH_SHEET_PATH &aSheet ) const;
  246. bool doIsConnected( const VECTOR2I& aPosition ) const override;
  247. bool m_startIsDangling; ///< True if start point is not connected.
  248. bool m_endIsDangling; ///< True if end point is not connected.
  249. VECTOR2I m_start; ///< Line start point
  250. VECTOR2I m_end; ///< Line end point
  251. EDA_ANGLE m_storedAngle; ///< Stored angle
  252. STROKE_PARAMS m_stroke; ///< Line stroke properties.
  253. // If real-time connectivity gets disabled (due to being too slow on a particular
  254. // design), we can no longer rely on getting the NetClass to find netclass-specific
  255. // linestyles, linewidths and colors.
  256. mutable PLOT_DASH_TYPE m_lastResolvedLineStyle;
  257. mutable int m_lastResolvedWidth;
  258. mutable COLOR4D m_lastResolvedColor;
  259. };
  260. #endif // _SCH_LINE_H_