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.

558 lines
16 KiB

  1. /************************/
  2. /** class LIB_POLYLINE **/
  3. /************************/
  4. #include "fctsys.h"
  5. #include "gr_basic.h"
  6. #include "common.h"
  7. #include "macros.h"
  8. #include "class_drawpanel.h"
  9. #include "plot_common.h"
  10. #include "trigo.h"
  11. #include "wxstruct.h"
  12. #include "general.h"
  13. #include "protos.h"
  14. #include "lib_polyline.h"
  15. #include "transform.h"
  16. #include <boost/foreach.hpp>
  17. LIB_POLYLINE::LIB_POLYLINE( LIB_COMPONENT* aParent ) :
  18. LIB_ITEM( LIB_POLYLINE_T, aParent )
  19. {
  20. m_Fill = NO_FILL;
  21. m_Width = 0;
  22. m_isFillable = true;
  23. m_typeName = _( "PolyLine" );
  24. }
  25. LIB_POLYLINE::LIB_POLYLINE( const LIB_POLYLINE& polyline ) :
  26. LIB_ITEM( polyline )
  27. {
  28. m_PolyPoints = polyline.m_PolyPoints; // Vector copy
  29. m_Width = polyline.m_Width;
  30. }
  31. bool LIB_POLYLINE::Save( FILE* aFile )
  32. {
  33. int ccount = GetCornerCount();
  34. if( fprintf( aFile, "P %d %d %d %d", ccount, m_Unit, m_Convert, m_Width ) < 0 )
  35. return false;
  36. for( unsigned i = 0; i < GetCornerCount(); i++ )
  37. {
  38. if( fprintf( aFile, " %d %d", m_PolyPoints[i].x, m_PolyPoints[i].y ) < 0 )
  39. return false;
  40. }
  41. if( fprintf( aFile, " %c\n", fill_tab[m_Fill] ) < 0 )
  42. return false;
  43. return true;
  44. }
  45. bool LIB_POLYLINE::Load( char* aLine, wxString& aErrorMsg )
  46. {
  47. char* p;
  48. int i, ccount = 0;
  49. wxPoint pt;
  50. i = sscanf( &aLine[2], "%d %d %d %d", &ccount, &m_Unit, &m_Convert, &m_Width );
  51. m_Fill = NO_FILL;
  52. if( i < 4 )
  53. {
  54. aErrorMsg.Printf( _( "polyline only had %d parameters of the required 4" ), i );
  55. return false;
  56. }
  57. if( ccount <= 0 )
  58. {
  59. aErrorMsg.Printf( _( "polyline count parameter %d is invalid" ), ccount );
  60. return false;
  61. }
  62. p = strtok( &aLine[2], " \t\n" );
  63. p = strtok( NULL, " \t\n" );
  64. p = strtok( NULL, " \t\n" );
  65. p = strtok( NULL, " \t\n" );
  66. for( i = 0; i < ccount; i++ )
  67. {
  68. wxPoint point;
  69. p = strtok( NULL, " \t\n" );
  70. if( p == NULL || sscanf( p, "%d", &pt.x ) != 1 )
  71. {
  72. aErrorMsg.Printf( _( "polyline point %d X position not defined" ), i );
  73. return false;
  74. }
  75. p = strtok( NULL, " \t\n" );
  76. if( p == NULL || sscanf( p, "%d", &pt.y ) != 1 )
  77. {
  78. aErrorMsg.Printf( _( "polyline point %d Y position not defined" ), i );
  79. return false;
  80. }
  81. AddPoint( pt );
  82. }
  83. if( ( p = strtok( NULL, " \t\n" ) ) != NULL )
  84. {
  85. if( p[0] == 'F' )
  86. m_Fill = FILLED_SHAPE;
  87. if( p[0] == 'f' )
  88. m_Fill = FILLED_WITH_BG_BODYCOLOR;
  89. }
  90. return true;
  91. }
  92. EDA_ITEM* LIB_POLYLINE::doClone() const
  93. {
  94. return new LIB_POLYLINE( *this );
  95. }
  96. int LIB_POLYLINE::DoCompare( const LIB_ITEM& aOther ) const
  97. {
  98. wxASSERT( aOther.Type() == LIB_POLYLINE_T );
  99. const LIB_POLYLINE* tmp = (LIB_POLYLINE*) &aOther;
  100. if( m_PolyPoints.size() != tmp->m_PolyPoints.size() )
  101. return m_PolyPoints.size() - tmp->m_PolyPoints.size();
  102. for( size_t i = 0; i < m_PolyPoints.size(); i++ )
  103. {
  104. if( m_PolyPoints[i].x != tmp->m_PolyPoints[i].x )
  105. return m_PolyPoints[i].x - tmp->m_PolyPoints[i].x;
  106. if( m_PolyPoints[i].y != tmp->m_PolyPoints[i].y )
  107. return m_PolyPoints[i].y - tmp->m_PolyPoints[i].y;
  108. }
  109. return 0;
  110. }
  111. void LIB_POLYLINE::DoOffset( const wxPoint& aOffset )
  112. {
  113. for( size_t i = 0; i < m_PolyPoints.size(); i++ )
  114. m_PolyPoints[i] += aOffset;
  115. }
  116. bool LIB_POLYLINE::DoTestInside( EDA_RECT& aRect ) const
  117. {
  118. for( size_t i = 0; i < m_PolyPoints.size(); i++ )
  119. {
  120. if( aRect.Contains( m_PolyPoints[i].x, -m_PolyPoints[i].y ) )
  121. return true;
  122. }
  123. return false;
  124. }
  125. void LIB_POLYLINE::DoMove( const wxPoint& aPosition )
  126. {
  127. DoOffset( aPosition - m_PolyPoints[0] );
  128. }
  129. void LIB_POLYLINE::DoMirrorHorizontal( const wxPoint& aCenter )
  130. {
  131. size_t i, imax = m_PolyPoints.size();
  132. for( i = 0; i < imax; i++ )
  133. {
  134. m_PolyPoints[i].x -= aCenter.x;
  135. m_PolyPoints[i].x *= -1;
  136. m_PolyPoints[i].x += aCenter.x;
  137. }
  138. }
  139. void LIB_POLYLINE::DoMirrorVertical( const wxPoint& aCenter )
  140. {
  141. size_t i, imax = m_PolyPoints.size();
  142. for( i = 0; i < imax; i++ )
  143. {
  144. m_PolyPoints[i].y -= aCenter.y;
  145. m_PolyPoints[i].y *= -1;
  146. m_PolyPoints[i].y += aCenter.y;
  147. }
  148. }
  149. void LIB_POLYLINE::DoRotate( const wxPoint& aCenter, bool aRotateCCW )
  150. {
  151. int rot_angle = aRotateCCW ? -900 : 900;
  152. size_t i, imax = m_PolyPoints.size();
  153. for( i = 0; i < imax; i++ )
  154. {
  155. RotatePoint( &m_PolyPoints[i], aCenter, rot_angle );
  156. }
  157. }
  158. void LIB_POLYLINE::DoPlot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
  159. const TRANSFORM& aTransform )
  160. {
  161. wxASSERT( aPlotter != NULL );
  162. static std::vector< wxPoint > cornerList;
  163. cornerList.clear();
  164. for( unsigned ii = 0; ii < m_PolyPoints.size(); ii++ )
  165. {
  166. wxPoint pos = m_PolyPoints[ii];
  167. pos = aTransform.TransformCoordinate( pos ) + aOffset;
  168. cornerList.push_back( pos );
  169. }
  170. if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
  171. {
  172. aPlotter->set_color( ReturnLayerColor( LAYER_DEVICE_BACKGROUND ) );
  173. aPlotter->PlotPoly( cornerList, FILLED_WITH_BG_BODYCOLOR, 0 );
  174. aFill = false; // body is now filled, do not fill it later.
  175. }
  176. bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
  177. aPlotter->set_color( ReturnLayerColor( LAYER_DEVICE ) );
  178. aPlotter->PlotPoly( cornerList, already_filled ? NO_FILL : m_Fill, GetPenSize() );
  179. }
  180. void LIB_POLYLINE::AddPoint( const wxPoint& point )
  181. {
  182. m_PolyPoints.push_back( point );
  183. }
  184. /**
  185. * Function GetPenSize
  186. * @return the size of the "pen" that be used to draw or plot this item
  187. */
  188. int LIB_POLYLINE::GetPenSize() const
  189. {
  190. return ( m_Width == 0 ) ? g_DrawDefaultLineThickness : m_Width;
  191. }
  192. void LIB_POLYLINE::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
  193. int aColor, int aDrawMode, void* aData,
  194. const TRANSFORM& aTransform )
  195. {
  196. wxPoint pos1;
  197. int color = ReturnLayerColor( LAYER_DEVICE );
  198. // Buffer used to store current corners coordinates for drawings
  199. static wxPoint* Buf_Poly_Drawings = NULL;
  200. static unsigned Buf_Poly_Size = 0;
  201. if( aColor < 0 ) // Used normal color or selected color
  202. {
  203. if( m_Selected & IS_SELECTED )
  204. color = g_ItemSelectetColor;
  205. }
  206. else
  207. color = aColor;
  208. // Set the size of the buffer of coordinates
  209. if( Buf_Poly_Drawings == NULL )
  210. {
  211. Buf_Poly_Size = m_PolyPoints.size();
  212. Buf_Poly_Drawings = (wxPoint*) MyMalloc( sizeof(wxPoint) * Buf_Poly_Size );
  213. }
  214. else if( Buf_Poly_Size < m_PolyPoints.size() )
  215. {
  216. Buf_Poly_Size = m_PolyPoints.size();
  217. Buf_Poly_Drawings = (wxPoint*) realloc( Buf_Poly_Drawings,
  218. sizeof(wxPoint) * Buf_Poly_Size );
  219. }
  220. // This should probably throw an exception instead of displaying a warning message.
  221. if( Buf_Poly_Drawings == NULL )
  222. {
  223. wxLogWarning( wxT( "Cannot allocate memory to draw polylines." ) );
  224. return;
  225. }
  226. for( unsigned ii = 0; ii < m_PolyPoints.size(); ii++ )
  227. {
  228. Buf_Poly_Drawings[ii] = aTransform.TransformCoordinate( m_PolyPoints[ii] ) + aOffset;
  229. }
  230. FILL_T fill = aData ? NO_FILL : m_Fill;
  231. if( aColor >= 0 )
  232. fill = NO_FILL;
  233. GRSetDrawMode( aDC, aDrawMode );
  234. if( fill == FILLED_WITH_BG_BODYCOLOR )
  235. GRPoly( &aPanel->m_ClipBox, aDC, m_PolyPoints.size(),
  236. Buf_Poly_Drawings, 1, GetPenSize(),
  237. (m_Flags & IS_MOVED) ? color : ReturnLayerColor( LAYER_DEVICE_BACKGROUND ),
  238. ReturnLayerColor( LAYER_DEVICE_BACKGROUND ) );
  239. else if( fill == FILLED_SHAPE )
  240. GRPoly( &aPanel->m_ClipBox, aDC, m_PolyPoints.size(),
  241. Buf_Poly_Drawings, 1, GetPenSize(), color, color );
  242. else
  243. GRPoly( &aPanel->m_ClipBox, aDC, m_PolyPoints.size(),
  244. Buf_Poly_Drawings, 0, GetPenSize(), color, color );
  245. /* Set to one (1) to draw bounding box around polyline to validate
  246. * bounding box calculation. */
  247. #if 0
  248. EDA_RECT bBox = GetBoundingBox();
  249. bBox.Inflate( m_Thickness + 1, m_Thickness + 1 );
  250. GRRect( &aPanel->m_ClipBox, aDC, bBox.GetOrigin().x, bBox.GetOrigin().y,
  251. bBox.GetEnd().x, bBox.GetEnd().y, 0, LIGHTMAGENTA );
  252. #endif
  253. }
  254. bool LIB_POLYLINE::HitTest( const wxPoint& aPosition )
  255. {
  256. int mindist = GetPenSize() / 2;
  257. // Have a minimal tolerance for hit test
  258. if( mindist < MINIMUM_SELECTION_DISTANCE )
  259. mindist = MINIMUM_SELECTION_DISTANCE;
  260. return HitTest( aPosition, mindist, DefaultTransform );
  261. }
  262. bool LIB_POLYLINE::HitTest( wxPoint aPosition, int aThreshold, const TRANSFORM& aTransform )
  263. {
  264. wxPoint ref, start, end;
  265. if( aThreshold < 0 )
  266. aThreshold = GetPenSize() / 2;
  267. for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
  268. {
  269. start = aTransform.TransformCoordinate( m_PolyPoints[ii - 1] );
  270. end = aTransform.TransformCoordinate( m_PolyPoints[ii] );
  271. if( TestSegmentHit( aPosition, start, end, aThreshold ) )
  272. return true;
  273. }
  274. return false;
  275. }
  276. /**
  277. * Function GetBoundingBox
  278. * @return the boundary box for this, in library coordinates
  279. */
  280. EDA_RECT LIB_POLYLINE::GetBoundingBox() const
  281. {
  282. EDA_RECT rect;
  283. int xmin, xmax, ymin, ymax;
  284. xmin = xmax = m_PolyPoints[0].x;
  285. ymin = ymax = m_PolyPoints[0].y;
  286. for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
  287. {
  288. xmin = MIN( xmin, m_PolyPoints[ii].x );
  289. xmax = MAX( xmax, m_PolyPoints[ii].x );
  290. ymin = MIN( ymin, m_PolyPoints[ii].y );
  291. ymax = MAX( ymax, m_PolyPoints[ii].y );
  292. }
  293. rect.SetOrigin( xmin, ymin * -1 );
  294. rect.SetEnd( xmax, ymax * -1 );
  295. rect.Inflate( m_Width / 2, m_Width / 2 );
  296. return rect;
  297. }
  298. void LIB_POLYLINE::DeleteSegment( const wxPoint aPosition )
  299. {
  300. // First segment is kept, only its end point is changed
  301. while( GetCornerCount() > 2 )
  302. {
  303. m_PolyPoints.pop_back();
  304. if( m_PolyPoints[ GetCornerCount() - 1 ] != aPosition )
  305. {
  306. m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
  307. break;
  308. }
  309. }
  310. }
  311. void LIB_POLYLINE::DisplayInfo( EDA_DRAW_FRAME* aFrame )
  312. {
  313. wxString msg;
  314. EDA_RECT bBox = GetBoundingBox();
  315. LIB_ITEM::DisplayInfo( aFrame );
  316. msg = ReturnStringFromValue( g_UserUnit, m_Width, EESCHEMA_INTERNAL_UNIT, true );
  317. aFrame->AppendMsgPanel( _( "Line width" ), msg, BLUE );
  318. msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
  319. bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
  320. aFrame->AppendMsgPanel( _( "Bounding box" ), msg, BROWN );
  321. }
  322. wxString LIB_POLYLINE::GetSelectMenuText() const
  323. {
  324. return wxString::Format( _( "Polyline at (%s, %s) with %u points" ),
  325. GetChars( CoordinateToString( m_PolyPoints[0].x,
  326. EESCHEMA_INTERNAL_UNIT ) ),
  327. GetChars( CoordinateToString( m_PolyPoints[0].y,
  328. EESCHEMA_INTERNAL_UNIT ) ),
  329. m_PolyPoints.size() );
  330. }
  331. void LIB_POLYLINE::BeginEdit( int aEditMode, const wxPoint aPosition )
  332. {
  333. wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
  334. wxT( "Invalid edit mode for LIB_POLYLINE object." ) );
  335. if( aEditMode == IS_NEW )
  336. {
  337. m_PolyPoints.push_back( aPosition ); // Start point of first segment.
  338. m_PolyPoints.push_back( aPosition ); // End point of first segment.
  339. }
  340. else if( aEditMode == IS_RESIZED )
  341. {
  342. // Drag one edge point of the polyline
  343. // Find the nearest edge point to be dragged
  344. wxPoint startPoint = m_PolyPoints[0];
  345. // Begin with the first list point as nearest point
  346. int index = 0;
  347. m_ModifyIndex = 0;
  348. m_initialPos = startPoint;
  349. // First distance is the current minimum distance
  350. int distanceMin = (aPosition - startPoint).x * (aPosition - startPoint).x
  351. + (aPosition - startPoint).y * (aPosition - startPoint).y;
  352. wxPoint prevPoint = startPoint;
  353. // Find the right index of the point to be dragged
  354. BOOST_FOREACH( wxPoint point, m_PolyPoints ) {
  355. int distancePoint = (aPosition - point).x * (aPosition - point).x +
  356. (aPosition - point).y * (aPosition - point).y;
  357. if( distancePoint < distanceMin )
  358. {
  359. // Save point.
  360. m_initialPos = point;
  361. m_ModifyIndex = index;
  362. distanceMin = distancePoint;
  363. }
  364. // check middle of an edge
  365. wxPoint offset = ( aPosition + aPosition - point - prevPoint );
  366. distancePoint = ( offset.x * offset.x + offset.y * offset.y ) / 4 + 1;
  367. if( distancePoint < distanceMin )
  368. {
  369. // Save point.
  370. m_initialPos = point;
  371. m_ModifyIndex = -index; // negative indicates new vertex is to be inserted
  372. distanceMin = distancePoint;
  373. }
  374. prevPoint = point;
  375. index++;
  376. }
  377. SetEraseLastDrawItem();
  378. }
  379. else if( aEditMode == IS_MOVED )
  380. {
  381. m_initialCursorPos = aPosition;
  382. m_initialPos = m_PolyPoints[0];
  383. SetEraseLastDrawItem();
  384. }
  385. m_Flags = aEditMode;
  386. }
  387. bool LIB_POLYLINE::ContinueEdit( const wxPoint aPosition )
  388. {
  389. wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0, false,
  390. wxT( "Bad call to ContinueEdit(). LIB_POLYLINE is not being edited." ) );
  391. if( m_Flags == IS_NEW )
  392. {
  393. // do not add zero length segments
  394. if( m_PolyPoints[m_PolyPoints.size() - 2] != m_PolyPoints.back() )
  395. m_PolyPoints.push_back( aPosition );
  396. return true;
  397. }
  398. return false;
  399. }
  400. void LIB_POLYLINE::EndEdit( const wxPoint& aPosition, bool aAbort )
  401. {
  402. wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
  403. wxT( "Bad call to EndEdit(). LIB_POLYLINE is not being edited." ) );
  404. // do not include last point twice
  405. if( m_Flags == IS_NEW && 2 < m_PolyPoints.size() )
  406. {
  407. if( m_PolyPoints[ m_PolyPoints.size() - 2 ] == m_PolyPoints.back() )
  408. m_PolyPoints.pop_back();
  409. }
  410. if( (m_Flags == IS_RESIZED) && (m_PolyPoints.size() > 2) ) // do not delete last two points... keep it alive
  411. {
  412. if( ( m_ModifyIndex > 0 && m_PolyPoints[ m_ModifyIndex ] ==
  413. m_PolyPoints[ m_ModifyIndex - 1 ] )
  414. ||
  415. ( m_ModifyIndex < (int) m_PolyPoints.size() - 1
  416. && m_PolyPoints[ m_ModifyIndex ] == m_PolyPoints[ m_ModifyIndex + 1 ] ) )
  417. {
  418. m_PolyPoints.erase( m_PolyPoints.begin() + m_ModifyIndex ); // delete a point on this
  419. }
  420. }
  421. m_Flags = 0;
  422. SetEraseLastDrawItem( false );
  423. }
  424. void LIB_POLYLINE::calcEdit( const wxPoint& aPosition )
  425. {
  426. if( m_Flags == IS_NEW )
  427. {
  428. m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
  429. SetEraseLastDrawItem();
  430. }
  431. else if( m_Flags == IS_RESIZED )
  432. {
  433. if( m_ModifyIndex < 0 ) // negative indicates new vertex is to be inserted
  434. {
  435. m_ModifyIndex = -m_ModifyIndex;
  436. m_PolyPoints.insert( m_PolyPoints.begin() + m_ModifyIndex, aPosition );
  437. }
  438. m_PolyPoints[ m_ModifyIndex ] = aPosition;
  439. }
  440. else if( m_Flags == IS_MOVED )
  441. {
  442. Move( m_initialPos + aPosition - m_initialCursorPos );
  443. }
  444. }