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.

512 lines
12 KiB

  1. /******************/
  2. /* Class SCH_LINE */
  3. /******************/
  4. #include "fctsys.h"
  5. #include "gr_basic.h"
  6. #include "macros.h"
  7. #include "class_drawpanel.h"
  8. #include "trigo.h"
  9. #include "richio.h"
  10. #include "general.h"
  11. #include "protos.h"
  12. #include "sch_line.h"
  13. #include <boost/foreach.hpp>
  14. SCH_LINE::SCH_LINE( const wxPoint& pos, int layer ) :
  15. SCH_ITEM( NULL, SCH_LINE_T )
  16. {
  17. m_Start = pos;
  18. m_End = pos;
  19. m_Width = 0; // Default thickness used
  20. m_StartIsDangling = m_EndIsDangling = FALSE;
  21. switch( layer )
  22. {
  23. default:
  24. m_Layer = LAYER_NOTES;
  25. break;
  26. case LAYER_WIRE:
  27. m_Layer = LAYER_WIRE;
  28. break;
  29. case LAYER_BUS:
  30. m_Layer = LAYER_BUS;
  31. break;
  32. }
  33. }
  34. SCH_LINE::SCH_LINE( const SCH_LINE& aLine ) :
  35. SCH_ITEM( aLine )
  36. {
  37. m_Start = aLine.m_Start;
  38. m_End = aLine.m_End;
  39. m_Width = aLine.m_Width;
  40. m_StartIsDangling = m_EndIsDangling = false;
  41. }
  42. EDA_ITEM* SCH_LINE::doClone() const
  43. {
  44. return new SCH_LINE( *this );
  45. }
  46. void SCH_LINE::Move( const wxPoint& aOffset )
  47. {
  48. if( (m_Flags & STARTPOINT) == 0 && aOffset != wxPoint( 0, 0 ) )
  49. {
  50. m_Start += aOffset;
  51. SetModified();
  52. }
  53. if( (m_Flags & ENDPOINT) == 0 && aOffset != wxPoint( 0, 0 ) )
  54. {
  55. m_End += aOffset;
  56. SetModified();
  57. }
  58. }
  59. #if defined(DEBUG)
  60. void SCH_LINE::Show( int nestLevel, std::ostream& os ) const
  61. {
  62. NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str()
  63. << " layer=\"" << m_Layer << '"'
  64. << " width=\"" << m_Width << '"'
  65. << " startIsDangling=\"" << m_StartIsDangling
  66. << '"' << " endIsDangling=\""
  67. << m_EndIsDangling << '"' << ">"
  68. << " <start" << m_Start << "/>"
  69. << " <end" << m_End << "/>" << "</"
  70. << GetClass().Lower().mb_str() << ">\n";
  71. }
  72. #endif
  73. EDA_RECT SCH_LINE::GetBoundingBox() const
  74. {
  75. int width = 25;
  76. int xmin = MIN( m_Start.x, m_End.x ) - width;
  77. int ymin = MIN( m_Start.y, m_End.y ) - width;
  78. int xmax = MAX( m_Start.x, m_End.x ) + width;
  79. int ymax = MAX( m_Start.y, m_End.y ) + width;
  80. // return a rectangle which is [pos,dim) in nature. therefore the +1
  81. EDA_RECT ret( wxPoint( xmin, ymin ), wxSize( xmax - xmin + 1, ymax - ymin + 1 ) );
  82. return ret;
  83. }
  84. double SCH_LINE::GetLength() const
  85. {
  86. return GetLineLength( m_Start, m_End );
  87. }
  88. bool SCH_LINE::Save( FILE* aFile ) const
  89. {
  90. bool success = true;
  91. const char* layer = "Notes";
  92. const char* width = "Line";
  93. if( GetLayer() == LAYER_WIRE )
  94. layer = "Wire";
  95. if( GetLayer() == LAYER_BUS )
  96. layer = "Bus";
  97. if( fprintf( aFile, "Wire %s %s\n", layer, width ) == EOF )
  98. {
  99. success = false;
  100. }
  101. if( fprintf( aFile, "\t%-4d %-4d %-4d %-4d\n", m_Start.x, m_Start.y,
  102. m_End.x, m_End.y ) == EOF )
  103. {
  104. success = false;
  105. }
  106. return success;
  107. }
  108. bool SCH_LINE::Load( LINE_READER& aLine, wxString& aErrorMsg )
  109. {
  110. char Name1[256];
  111. char Name2[256];
  112. char* line = (char*) aLine;
  113. while( (*line != ' ' ) && *line )
  114. line++;
  115. if( sscanf( line, "%s %s", Name1, Name2 ) != 2 )
  116. {
  117. aErrorMsg.Printf( wxT( "EESchema file segment error at line %d, aborted" ),
  118. aLine.LineNumber() );
  119. aErrorMsg << wxT( "\n" ) << FROM_UTF8( (char*) aLine );
  120. return false;
  121. }
  122. m_Layer = LAYER_NOTES;
  123. if( Name1[0] == 'W' )
  124. m_Layer = LAYER_WIRE;
  125. if( Name1[0] == 'B' )
  126. m_Layer = LAYER_BUS;
  127. if( !aLine.ReadLine() || sscanf( (char*) aLine, "%d %d %d %d ",
  128. &m_Start.x, &m_Start.y, &m_End.x, &m_End.y ) != 4 )
  129. {
  130. aErrorMsg.Printf( wxT( "EESchema file Segment struct error at line %d, aborted" ),
  131. aLine.LineNumber() );
  132. aErrorMsg << wxT( "\n" ) << FROM_UTF8( (char*) aLine );
  133. return false;
  134. }
  135. return true;
  136. }
  137. int SCH_LINE::GetPenSize() const
  138. {
  139. int pensize = ( m_Width == 0 ) ? g_DrawDefaultLineThickness : m_Width;
  140. if( m_Layer == LAYER_BUS && m_Width == 0 )
  141. {
  142. pensize = wxRound( g_DrawDefaultLineThickness * BUS_WIDTH_EXPAND );
  143. pensize = MAX( pensize, 3 );
  144. }
  145. return pensize;
  146. }
  147. void SCH_LINE::Draw( EDA_DRAW_PANEL* panel, wxDC* DC, const wxPoint& offset,
  148. int DrawMode, int Color )
  149. {
  150. int color;
  151. int width = GetPenSize();
  152. if( Color >= 0 )
  153. color = Color;
  154. else
  155. color = ReturnLayerColor( m_Layer );
  156. GRSetDrawMode( DC, DrawMode );
  157. wxPoint start = m_Start;
  158. wxPoint end = m_End;
  159. if( ( m_Flags & STARTPOINT ) == 0 )
  160. start += offset;
  161. if( ( m_Flags & ENDPOINT ) == 0 )
  162. end += offset;
  163. if( m_Layer == LAYER_NOTES )
  164. GRDashedLine( &panel->m_ClipBox, DC, start.x, start.y, end.x, end.y, width, color );
  165. else
  166. GRLine( &panel->m_ClipBox, DC, start, end, width, color );
  167. if( m_StartIsDangling )
  168. DrawDanglingSymbol( panel, DC, start, color );
  169. if( m_EndIsDangling )
  170. DrawDanglingSymbol( panel, DC, end, color );
  171. }
  172. void SCH_LINE::Mirror_X( int aXaxis_position )
  173. {
  174. m_Start.y -= aXaxis_position;
  175. NEGATE( m_Start.y );
  176. m_Start.y += aXaxis_position;
  177. m_End.y -= aXaxis_position;
  178. NEGATE( m_End.y );
  179. m_End.y += aXaxis_position;
  180. }
  181. void SCH_LINE::Mirror_Y( int aYaxis_position )
  182. {
  183. m_Start.x -= aYaxis_position;
  184. NEGATE( m_Start.x );
  185. m_Start.x += aYaxis_position;
  186. m_End.x -= aYaxis_position;
  187. NEGATE( m_End.x );
  188. m_End.x += aYaxis_position;
  189. }
  190. void SCH_LINE::Rotate( wxPoint rotationPoint )
  191. {
  192. RotatePoint( &m_Start, rotationPoint, 900 );
  193. RotatePoint( &m_End, rotationPoint, 900 );
  194. }
  195. bool SCH_LINE::MergeOverlap( SCH_LINE* aLine )
  196. {
  197. wxCHECK_MSG( aLine != NULL && aLine->Type() == SCH_LINE_T, false,
  198. wxT( "Cannot test line segment for overlap." ) );
  199. if( this == aLine || GetLayer() != aLine->GetLayer() )
  200. return false;
  201. // Search for a common end, and modify coordinates to ensure RefSegm->m_End
  202. // == TstSegm->m_Start
  203. if( m_Start == aLine->m_Start )
  204. {
  205. if( m_End == aLine->m_End )
  206. return true;
  207. EXCHG( m_Start, m_End );
  208. }
  209. else if( m_Start == aLine->m_End )
  210. {
  211. EXCHG( m_Start, m_End );
  212. EXCHG( aLine->m_Start, aLine->m_End );
  213. }
  214. else if( m_End == aLine->m_End )
  215. {
  216. EXCHG( aLine->m_Start, aLine->m_End );
  217. }
  218. else if( m_End != aLine->m_Start )
  219. {
  220. // No common end point, segments cannot be merged.
  221. return false;
  222. }
  223. /* Test alignment: */
  224. if( m_Start.y == m_End.y ) // Horizontal segment
  225. {
  226. if( aLine->m_Start.y == aLine->m_End.y )
  227. {
  228. m_End = aLine->m_End;
  229. return true;
  230. }
  231. }
  232. else if( m_Start.x == m_End.x ) // Vertical segment
  233. {
  234. if( aLine->m_Start.x == aLine->m_End.x )
  235. {
  236. m_End = aLine->m_End;
  237. return true;
  238. }
  239. }
  240. else
  241. {
  242. if( atan2( (double) ( m_Start.x - m_End.x ), (double) ( m_Start.y - m_End.y ) )
  243. == atan2( (double) ( aLine->m_Start.x - aLine->m_End.x ),
  244. (double) ( aLine->m_Start.y - aLine->m_End.y ) ) )
  245. {
  246. m_End = aLine->m_End;
  247. return true;
  248. }
  249. }
  250. return false;
  251. }
  252. void SCH_LINE::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
  253. {
  254. if( GetLayer() == LAYER_NOTES )
  255. return;
  256. if( ( GetLayer() == LAYER_BUS ) || ( GetLayer() == LAYER_WIRE ) )
  257. {
  258. DANGLING_END_ITEM item( (GetLayer() == LAYER_BUS) ? BUS_START_END : WIRE_START_END, this );
  259. item.m_Pos = m_Start;
  260. DANGLING_END_ITEM item1( (GetLayer() == LAYER_BUS) ? BUS_END_END : WIRE_END_END, this );
  261. item1.m_Pos = m_End;
  262. aItemList.push_back( item );
  263. aItemList.push_back( item1 );
  264. }
  265. }
  266. bool SCH_LINE::IsDanglingStateChanged( std::vector< DANGLING_END_ITEM >& aItemList )
  267. {
  268. bool previousStartState = m_StartIsDangling;
  269. bool previousEndState = m_EndIsDangling;
  270. m_StartIsDangling = m_EndIsDangling = true;
  271. if( GetLayer() == LAYER_WIRE )
  272. {
  273. BOOST_FOREACH( DANGLING_END_ITEM item, aItemList )
  274. {
  275. if( item.m_Item == this )
  276. continue;
  277. if( m_Start == item.m_Pos )
  278. m_StartIsDangling = false;
  279. if( m_End == item.m_Pos )
  280. m_EndIsDangling = false;
  281. if( (m_StartIsDangling == false) && (m_EndIsDangling == false) )
  282. break;
  283. }
  284. }
  285. else if( GetLayer() == LAYER_BUS || GetLayer() == LAYER_NOTES )
  286. {
  287. // Lines on the notes layer and the bus layer cannot be tested for dangling ends.
  288. previousStartState = previousEndState = m_StartIsDangling = m_EndIsDangling = false;
  289. }
  290. return ( previousStartState != m_StartIsDangling ) || ( previousEndState != m_EndIsDangling );
  291. }
  292. bool SCH_LINE::IsSelectStateChanged( const wxRect& aRect )
  293. {
  294. bool previousState = IsSelected();
  295. if( aRect.Contains( m_Start ) && aRect.Contains( m_End ) )
  296. {
  297. m_Flags |= SELECTED;
  298. m_Flags &= ~(STARTPOINT | ENDPOINT);
  299. }
  300. else if( aRect.Contains( m_Start ) )
  301. {
  302. m_Flags &= ~STARTPOINT;
  303. m_Flags |= ( SELECTED | ENDPOINT );
  304. }
  305. else if( aRect.Contains( m_End ) )
  306. {
  307. m_Flags &= ~ENDPOINT;
  308. m_Flags |= ( SELECTED | STARTPOINT );
  309. }
  310. else
  311. {
  312. m_Flags &= ~( SELECTED | STARTPOINT | ENDPOINT );
  313. }
  314. return previousState != IsSelected();
  315. }
  316. bool SCH_LINE::IsConnectable() const
  317. {
  318. if( m_Layer == LAYER_WIRE || m_Layer == LAYER_BUS )
  319. return true;
  320. return false;
  321. }
  322. void SCH_LINE::GetConnectionPoints( vector< wxPoint >& aPoints ) const
  323. {
  324. aPoints.push_back( m_Start );
  325. aPoints.push_back( m_End );
  326. }
  327. wxString SCH_LINE::GetSelectMenuText() const
  328. {
  329. wxString menuText, txtfmt, orient;
  330. if( m_Start.x == m_End.x )
  331. orient = _("Vert.");
  332. else if( m_Start.y == m_End.y )
  333. orient = _("Horiz.");
  334. switch( m_Layer )
  335. {
  336. case LAYER_NOTES:
  337. txtfmt = _( "%s Graphic Line from (%s,%s) to (%s,%s) " );
  338. break;
  339. case LAYER_WIRE:
  340. txtfmt = _( "%s Wire from (%s,%s) to (%s,%s)" );
  341. break;
  342. case LAYER_BUS:
  343. txtfmt = _( "%s Bus from (%s,%s) to (%s,%s)" );
  344. break;
  345. default:
  346. txtfmt += _( "%s Line on Unkown Layer from (%s,%s) to (%s,%s)" );
  347. }
  348. menuText.Printf( txtfmt, GetChars( orient ),
  349. GetChars(CoordinateToString( m_Start.x, EESCHEMA_INTERNAL_UNIT )),
  350. GetChars(CoordinateToString( m_Start.y, EESCHEMA_INTERNAL_UNIT )),
  351. GetChars(CoordinateToString( m_End.x, EESCHEMA_INTERNAL_UNIT )),
  352. GetChars(CoordinateToString( m_End.y, EESCHEMA_INTERNAL_UNIT )) );
  353. return menuText;
  354. }
  355. const char** SCH_LINE::GetMenuImage() const
  356. {
  357. if( m_Layer == LAYER_NOTES )
  358. return (const char**) add_dashed_line_xpm;
  359. else if( m_Layer == LAYER_WIRE )
  360. return (const char**) add_line_xpm;
  361. return (const char**) add_bus_xpm;
  362. }
  363. bool SCH_LINE::operator <( const SCH_ITEM& aItem ) const
  364. {
  365. if( Type() != aItem.Type() )
  366. return Type() < aItem.Type();
  367. SCH_LINE* line = (SCH_LINE*) &aItem;
  368. if( GetLength() != line->GetLength() )
  369. return GetLength() < line->GetLength();
  370. if( m_Start.x != line->m_Start.x )
  371. return m_Start.x < line->m_Start.x;
  372. if( m_Start.y != line->m_Start.y )
  373. return m_Start.y < line->m_Start.y;
  374. return false;
  375. }
  376. bool SCH_LINE::doHitTest( const wxPoint& aPoint, int aAccuracy ) const
  377. {
  378. return TestSegmentHit( aPoint, m_Start, m_End, aAccuracy );
  379. }
  380. bool SCH_LINE::doHitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  381. {
  382. EDA_RECT rect = aRect;
  383. rect.Inflate( aAccuracy );
  384. if( aContained )
  385. return rect.Contains( GetBoundingBox() );
  386. return rect.Intersects( GetBoundingBox() );
  387. }
  388. bool SCH_LINE::doIsConnected( const wxPoint& aPosition ) const
  389. {
  390. if( m_Layer != LAYER_WIRE && m_Layer != LAYER_BUS )
  391. return false;
  392. return IsEndPoint( aPosition );
  393. }