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.

832 lines
24 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 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-2015 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 lib_arc.cpp
  26. */
  27. #include <fctsys.h>
  28. #include <gr_basic.h>
  29. #include <macros.h>
  30. #include <class_drawpanel.h>
  31. #include <plot_common.h>
  32. #include <trigo.h>
  33. #include <wxstruct.h>
  34. #include <richio.h>
  35. #include <base_units.h>
  36. #include <msgpanel.h>
  37. #include <bitmaps.h>
  38. #include <general.h>
  39. #include <lib_arc.h>
  40. #include <transform.h>
  41. // Helper function
  42. static inline wxPoint twoPointVector( const wxPoint &startPoint, const wxPoint &endPoint )
  43. {
  44. return endPoint - startPoint;
  45. }
  46. //! @brief Given three points A B C, compute the circumcenter of the resulting triangle
  47. //! reference: http://en.wikipedia.org/wiki/Circumscribed_circle
  48. //! Coordinates of circumcenter in Cartesian coordinates
  49. static wxPoint calcCenter( const wxPoint& A, const wxPoint& B, const wxPoint& C )
  50. {
  51. double circumCenterX, circumCenterY;
  52. double Ax = (double) A.x;
  53. double Ay = (double) A.y;
  54. double Bx = (double) B.x;
  55. double By = (double) B.y;
  56. double Cx = (double) C.x;
  57. double Cy = (double) C.y;
  58. wxPoint circumCenter;
  59. double D = 2.0 * ( Ax * ( By - Cy ) + Bx * ( Cy - Ay ) + Cx * ( Ay - By ) );
  60. // prevent division / 0
  61. if( fabs( D ) < 1e-7 )
  62. D = 1e-7;
  63. circumCenterX = ( (Ay * Ay + Ax * Ax) * (By - Cy) +
  64. (By * By + Bx * Bx) * (Cy - Ay) +
  65. (Cy * Cy + Cx * Cx) * (Ay - By) ) / D;
  66. circumCenterY = ( (Ay * Ay + Ax * Ax) * (Cx - Bx) +
  67. (By * By + Bx * Bx) * (Ax - Cx) +
  68. (Cy * Cy + Cx * Cx) * (Bx - Ax) ) / D;
  69. circumCenter.x = (int) circumCenterX;
  70. circumCenter.y = (int) circumCenterY;
  71. return circumCenter;
  72. }
  73. LIB_ARC::LIB_ARC( LIB_PART* aParent ) : LIB_ITEM( LIB_ARC_T, aParent )
  74. {
  75. m_Radius = 0;
  76. m_t1 = 0;
  77. m_t2 = 0;
  78. m_Width = 0;
  79. m_Fill = NO_FILL;
  80. m_isFillable = true;
  81. m_typeName = _( "Arc" );
  82. m_editState = 0;
  83. m_lastEditState = 0;
  84. m_editCenterDistance = 0.0;
  85. m_editSelectPoint = ARC_STATUS_START;
  86. m_editDirection = 0;
  87. }
  88. bool LIB_ARC::Save( OUTPUTFORMATTER& aFormatter )
  89. {
  90. int x1 = m_t1;
  91. if( x1 > 1800 )
  92. x1 -= 3600;
  93. int x2 = m_t2;
  94. if( x2 > 1800 )
  95. x2 -= 3600;
  96. aFormatter.Print( 0, "A %d %d %d %d %d %d %d %d %c %d %d %d %d\n",
  97. m_Pos.x, m_Pos.y, m_Radius, x1, x2, m_Unit, m_Convert, m_Width,
  98. fill_tab[m_Fill], m_ArcStart.x, m_ArcStart.y, m_ArcEnd.x,
  99. m_ArcEnd.y );
  100. return true;
  101. }
  102. bool LIB_ARC::Load( LINE_READER& aLineReader, wxString& aErrorMsg )
  103. {
  104. int startx, starty, endx, endy, cnt;
  105. char tmp[256] = "";
  106. char* line = (char*) aLineReader;
  107. cnt = sscanf( line + 2, "%d %d %d %d %d %d %d %d %255s %d %d %d %d",
  108. &m_Pos.x, &m_Pos.y, &m_Radius, &m_t1, &m_t2, &m_Unit,
  109. &m_Convert, &m_Width, tmp, &startx, &starty, &endx, &endy );
  110. if( cnt < 8 )
  111. {
  112. aErrorMsg.Printf( _( "Arc only had %d parameters of the required 8" ), cnt );
  113. return false;
  114. }
  115. if( tmp[0] == 'F' )
  116. m_Fill = FILLED_SHAPE;
  117. if( tmp[0] == 'f' )
  118. m_Fill = FILLED_WITH_BG_BODYCOLOR;
  119. NORMALIZE_ANGLE_POS( m_t1 );
  120. NORMALIZE_ANGLE_POS( m_t2 );
  121. // Actual Coordinates of arc ends are read from file
  122. if( cnt >= 13 )
  123. {
  124. m_ArcStart.x = startx;
  125. m_ArcStart.y = starty;
  126. m_ArcEnd.x = endx;
  127. m_ArcEnd.y = endy;
  128. }
  129. else
  130. {
  131. // Actual Coordinates of arc ends are not read from file
  132. // (old library), calculate them
  133. m_ArcStart.x = m_Radius;
  134. m_ArcStart.y = 0;
  135. m_ArcEnd.x = m_Radius;
  136. m_ArcEnd.y = 0;
  137. RotatePoint( &m_ArcStart.x, &m_ArcStart.y, -m_t1 );
  138. m_ArcStart.x += m_Pos.x;
  139. m_ArcStart.y += m_Pos.y;
  140. RotatePoint( &m_ArcEnd.x, &m_ArcEnd.y, -m_t2 );
  141. m_ArcEnd.x += m_Pos.x;
  142. m_ArcEnd.y += m_Pos.y;
  143. }
  144. return true;
  145. }
  146. bool LIB_ARC::HitTest( const wxPoint& aRefPoint ) const
  147. {
  148. int mindist = GetPenSize() / 2;
  149. // Have a minimal tolerance for hit test
  150. if( mindist < MINIMUM_SELECTION_DISTANCE )
  151. mindist = MINIMUM_SELECTION_DISTANCE;
  152. return HitTest( aRefPoint, mindist, DefaultTransform );
  153. }
  154. bool LIB_ARC::HitTest( const wxPoint &aPosition, int aThreshold, const TRANSFORM& aTransform ) const
  155. {
  156. if( aThreshold < 0 )
  157. aThreshold = GetPenSize() / 2;
  158. // TODO: use aTransMat to calculates parameters
  159. wxPoint relativePosition = aPosition;
  160. relativePosition.y = -relativePosition.y; // reverse Y axis
  161. int distance = KiROUND( GetLineLength( m_Pos, relativePosition ) );
  162. if( abs( distance - m_Radius ) > aThreshold )
  163. return false;
  164. // We are on the circle, ensure we are only on the arc, i.e. between
  165. // m_ArcStart and m_ArcEnd
  166. wxPoint startEndVector = twoPointVector( m_ArcStart, m_ArcEnd);
  167. wxPoint startRelativePositionVector = twoPointVector( m_ArcStart, relativePosition );
  168. wxPoint centerStartVector = twoPointVector( m_Pos, m_ArcStart );
  169. wxPoint centerEndVector = twoPointVector( m_Pos, m_ArcEnd );
  170. wxPoint centerRelativePositionVector = twoPointVector( m_Pos, relativePosition );
  171. // Compute the cross product to check if the point is in the sector
  172. double crossProductStart = CrossProduct( centerStartVector, centerRelativePositionVector );
  173. double crossProductEnd = CrossProduct( centerEndVector, centerRelativePositionVector );
  174. // The cross products need to be exchanged, depending on which side the center point
  175. // relative to the start point to end point vector lies
  176. if( CrossProduct( startEndVector, startRelativePositionVector ) < 0 )
  177. {
  178. std::swap( crossProductStart, crossProductEnd );
  179. }
  180. // When the cross products have a different sign, the point lies in sector
  181. // also check, if the reference is near start or end point
  182. return HitTestPoints( m_ArcStart, relativePosition, MINIMUM_SELECTION_DISTANCE ) ||
  183. HitTestPoints( m_ArcEnd, relativePosition, MINIMUM_SELECTION_DISTANCE ) ||
  184. ( crossProductStart <= 0 && crossProductEnd >= 0 );
  185. }
  186. EDA_ITEM* LIB_ARC::Clone() const
  187. {
  188. return new LIB_ARC( *this );
  189. }
  190. int LIB_ARC::compare( const LIB_ITEM& aOther ) const
  191. {
  192. wxASSERT( aOther.Type() == LIB_ARC_T );
  193. const LIB_ARC* tmp = ( LIB_ARC* ) &aOther;
  194. if( m_Pos.x != tmp->m_Pos.x )
  195. return m_Pos.x - tmp->m_Pos.x;
  196. if( m_Pos.y != tmp->m_Pos.y )
  197. return m_Pos.y - tmp->m_Pos.y;
  198. if( m_t1 != tmp->m_t1 )
  199. return m_t1 - tmp->m_t1;
  200. if( m_t2 != tmp->m_t2 )
  201. return m_t2 - tmp->m_t2;
  202. return 0;
  203. }
  204. void LIB_ARC::SetOffset( const wxPoint& aOffset )
  205. {
  206. m_Pos += aOffset;
  207. m_ArcStart += aOffset;
  208. m_ArcEnd += aOffset;
  209. }
  210. bool LIB_ARC::Inside( EDA_RECT& aRect ) const
  211. {
  212. return aRect.Contains( m_ArcStart.x, -m_ArcStart.y )
  213. || aRect.Contains( m_ArcEnd.x, -m_ArcEnd.y );
  214. }
  215. void LIB_ARC::Move( const wxPoint& aPosition )
  216. {
  217. wxPoint offset = aPosition - m_Pos;
  218. m_Pos = aPosition;
  219. m_ArcStart += offset;
  220. m_ArcEnd += offset;
  221. }
  222. void LIB_ARC::MirrorHorizontal( const wxPoint& aCenter )
  223. {
  224. m_Pos.x -= aCenter.x;
  225. m_Pos.x *= -1;
  226. m_Pos.x += aCenter.x;
  227. m_ArcStart.x -= aCenter.x;
  228. m_ArcStart.x *= -1;
  229. m_ArcStart.x += aCenter.x;
  230. m_ArcEnd.x -= aCenter.x;
  231. m_ArcEnd.x *= -1;
  232. m_ArcEnd.x += aCenter.x;
  233. std::swap( m_ArcStart, m_ArcEnd );
  234. std::swap( m_t1, m_t2 );
  235. m_t1 = 1800 - m_t1;
  236. m_t2 = 1800 - m_t2;
  237. if( m_t1 > 3600 || m_t2 > 3600 )
  238. {
  239. m_t1 -= 3600;
  240. m_t2 -= 3600;
  241. }
  242. else if( m_t1 < -3600 || m_t2 < -3600 )
  243. {
  244. m_t1 += 3600;
  245. m_t2 += 3600;
  246. }
  247. }
  248. void LIB_ARC::MirrorVertical( const wxPoint& aCenter )
  249. {
  250. m_Pos.y -= aCenter.y;
  251. m_Pos.y *= -1;
  252. m_Pos.y += aCenter.y;
  253. m_ArcStart.y -= aCenter.y;
  254. m_ArcStart.y *= -1;
  255. m_ArcStart.y += aCenter.y;
  256. m_ArcEnd.y -= aCenter.y;
  257. m_ArcEnd.y *= -1;
  258. m_ArcEnd.y += aCenter.y;
  259. std::swap( m_ArcStart, m_ArcEnd );
  260. std::swap( m_t1, m_t2 );
  261. m_t1 = - m_t1;
  262. m_t2 = - m_t2;
  263. if( m_t1 > 3600 || m_t2 > 3600 )
  264. {
  265. m_t1 -= 3600;
  266. m_t2 -= 3600;
  267. }
  268. else if( m_t1 < -3600 || m_t2 < -3600 )
  269. {
  270. m_t1 += 3600;
  271. m_t2 += 3600;
  272. }
  273. }
  274. void LIB_ARC::Rotate( const wxPoint& aCenter, bool aRotateCCW )
  275. {
  276. int rot_angle = aRotateCCW ? -900 : 900;
  277. RotatePoint( &m_Pos, aCenter, rot_angle );
  278. RotatePoint( &m_ArcStart, aCenter, rot_angle );
  279. RotatePoint( &m_ArcEnd, aCenter, rot_angle );
  280. m_t1 -= rot_angle;
  281. m_t2 -= rot_angle;
  282. if( m_t1 > 3600 || m_t2 > 3600 )
  283. {
  284. m_t1 -= 3600;
  285. m_t2 -= 3600;
  286. }
  287. else if( m_t1 < -3600 || m_t2 < -3600 )
  288. {
  289. m_t1 += 3600;
  290. m_t2 += 3600;
  291. }
  292. }
  293. void LIB_ARC::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
  294. const TRANSFORM& aTransform )
  295. {
  296. wxASSERT( aPlotter != NULL );
  297. int t1 = m_t1;
  298. int t2 = m_t2;
  299. wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
  300. aTransform.MapAngles( &t1, &t2 );
  301. if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
  302. {
  303. aPlotter->SetColor( GetLayerColor( LAYER_DEVICE_BACKGROUND ) );
  304. aPlotter->Arc( pos, -t2, -t1, m_Radius, FILLED_SHAPE, 0 );
  305. }
  306. bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
  307. aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
  308. aPlotter->Arc( pos, -t2, -t1, m_Radius, already_filled ? NO_FILL : m_Fill, GetPenSize() );
  309. }
  310. int LIB_ARC::GetPenSize() const
  311. {
  312. return ( m_Width == 0 ) ? GetDefaultLineThickness() : m_Width;
  313. }
  314. void LIB_ARC::drawEditGraphics( EDA_RECT* aClipBox, wxDC* aDC, COLOR4D aColor )
  315. {
  316. // The edit indicators only get drawn when a new arc is being drawn.
  317. if( !IsNew() )
  318. return;
  319. // Use the last edit state so when the drawing switches from the end mode to the center
  320. // point mode, the last line between the center points gets erased.
  321. if( m_lastEditState == 1 )
  322. {
  323. GRLine( aClipBox, aDC, m_ArcStart.x, -m_ArcStart.y, m_ArcEnd.x, -m_ArcEnd.y, 0, aColor );
  324. }
  325. else
  326. {
  327. GRDashedLine( aClipBox, aDC, m_ArcStart.x, -m_ArcStart.y, m_Pos.x, -m_Pos.y, 0, aColor );
  328. GRDashedLine( aClipBox, aDC, m_ArcEnd.x, -m_ArcEnd.y, m_Pos.x, -m_Pos.y, 0, aColor );
  329. }
  330. }
  331. void LIB_ARC::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
  332. COLOR4D aColor, GR_DRAWMODE aDrawMode, void* aData,
  333. const TRANSFORM& aTransform )
  334. {
  335. // Don't draw the arc until the end point is selected. Only the edit indicators
  336. // get drawn at this time.
  337. if( IsNew() && m_lastEditState == 1 )
  338. return;
  339. wxPoint pos1, pos2, posc;
  340. COLOR4D color = GetLayerColor( LAYER_DEVICE );
  341. if( aColor == COLOR4D::UNSPECIFIED ) // Used normal color or selected color
  342. {
  343. if( IsSelected() )
  344. color = GetItemSelectedColor();
  345. }
  346. else
  347. {
  348. color = aColor;
  349. }
  350. pos1 = aTransform.TransformCoordinate( m_ArcEnd ) + aOffset;
  351. pos2 = aTransform.TransformCoordinate( m_ArcStart ) + aOffset;
  352. posc = aTransform.TransformCoordinate( m_Pos ) + aOffset;
  353. int pt1 = m_t1;
  354. int pt2 = m_t2;
  355. bool swap = aTransform.MapAngles( &pt1, &pt2 );
  356. if( swap )
  357. {
  358. std::swap( pos1.x, pos2.x );
  359. std::swap( pos1.y, pos2.y );
  360. }
  361. GRSetDrawMode( aDC, aDrawMode );
  362. FILL_T fill = aData ? NO_FILL : m_Fill;
  363. if( aColor != COLOR4D::UNSPECIFIED )
  364. fill = NO_FILL;
  365. EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
  366. if( fill == FILLED_WITH_BG_BODYCOLOR )
  367. {
  368. GRFilledArc( clipbox, aDC, posc.x, posc.y, pt1, pt2,
  369. m_Radius, GetPenSize( ),
  370. (m_Flags & IS_MOVED) ? color : GetLayerColor( LAYER_DEVICE_BACKGROUND ),
  371. GetLayerColor( LAYER_DEVICE_BACKGROUND ) );
  372. }
  373. else if( fill == FILLED_SHAPE && !aData )
  374. {
  375. GRFilledArc( clipbox, aDC, posc.x, posc.y, pt1, pt2, m_Radius,
  376. color, color );
  377. }
  378. else
  379. {
  380. #ifdef DRAW_ARC_WITH_ANGLE
  381. GRArc( clipbox, aDC, posc.x, posc.y, pt1, pt2, m_Radius,
  382. GetPenSize(), color );
  383. #else
  384. GRArc1( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y,
  385. posc.x, posc.y, GetPenSize(), color );
  386. #endif
  387. }
  388. /* Set to one (1) to draw bounding box around arc to validate bounding box
  389. * calculation. */
  390. #if 0
  391. EDA_RECT bBox = GetBoundingBox();
  392. bBox.RevertYAxis();
  393. bBox = aTransform.TransformCoordinate( bBox );
  394. bBox.Move( aOffset );
  395. GRRect( clipbox, aDC, bBox, 0, LIGHTMAGENTA );
  396. #endif
  397. }
  398. const EDA_RECT LIB_ARC::GetBoundingBox() const
  399. {
  400. int minX, minY, maxX, maxY, angleStart, angleEnd;
  401. EDA_RECT rect;
  402. wxPoint nullPoint, startPos, endPos, centerPos;
  403. wxPoint normStart = m_ArcStart - m_Pos;
  404. wxPoint normEnd = m_ArcEnd - m_Pos;
  405. if( ( normStart == nullPoint ) || ( normEnd == nullPoint ) || ( m_Radius == 0 ) )
  406. {
  407. wxLogDebug( wxT("Invalid arc drawing definition, center(%d, %d) \
  408. start(%d, %d), end(%d, %d), radius %d" ),
  409. m_Pos.x, m_Pos.y, m_ArcStart.x, m_ArcStart.y, m_ArcEnd.x,
  410. m_ArcEnd.y, m_Radius );
  411. return rect;
  412. }
  413. endPos = DefaultTransform.TransformCoordinate( m_ArcEnd );
  414. startPos = DefaultTransform.TransformCoordinate( m_ArcStart );
  415. centerPos = DefaultTransform.TransformCoordinate( m_Pos );
  416. angleStart = m_t1;
  417. angleEnd = m_t2;
  418. if( DefaultTransform.MapAngles( &angleStart, &angleEnd ) )
  419. {
  420. std::swap( endPos.x, startPos.x );
  421. std::swap( endPos.y, startPos.y );
  422. }
  423. /* Start with the start and end point of the arc. */
  424. minX = std::min( startPos.x, endPos.x );
  425. minY = std::min( startPos.y, endPos.y );
  426. maxX = std::max( startPos.x, endPos.x );
  427. maxY = std::max( startPos.y, endPos.y );
  428. /* Zero degrees is a special case. */
  429. if( angleStart == 0 )
  430. maxX = centerPos.x + m_Radius;
  431. /* Arc end angle wrapped passed 360. */
  432. if( angleStart > angleEnd )
  433. angleEnd += 3600;
  434. if( angleStart <= 900 && angleEnd >= 900 ) /* 90 deg */
  435. maxY = centerPos.y + m_Radius;
  436. if( angleStart <= 1800 && angleEnd >= 1800 ) /* 180 deg */
  437. minX = centerPos.x - m_Radius;
  438. if( angleStart <= 2700 && angleEnd >= 2700 ) /* 270 deg */
  439. minY = centerPos.y - m_Radius;
  440. if( angleStart <= 3600 && angleEnd >= 3600 ) /* 0 deg */
  441. maxX = centerPos.x + m_Radius;
  442. rect.SetOrigin( minX, minY );
  443. rect.SetEnd( maxX, maxY );
  444. rect.Inflate( ( GetPenSize()+1 ) / 2 );
  445. return rect;
  446. }
  447. void LIB_ARC::GetMsgPanelInfo( std::vector< MSG_PANEL_ITEM >& aList )
  448. {
  449. wxString msg;
  450. EDA_RECT bBox = GetBoundingBox();
  451. LIB_ITEM::GetMsgPanelInfo( aList );
  452. msg = StringFromValue( g_UserUnit, m_Width, true );
  453. aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
  454. msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
  455. bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
  456. aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
  457. }
  458. wxString LIB_ARC::GetSelectMenuText() const
  459. {
  460. return wxString::Format( _( "Arc center (%s, %s), radius %s" ),
  461. GetChars( CoordinateToString( m_Pos.x ) ),
  462. GetChars( CoordinateToString( m_Pos.y ) ),
  463. GetChars( CoordinateToString( m_Radius ) ) );
  464. }
  465. BITMAP_DEF LIB_ARC::GetMenuImage() const
  466. {
  467. return add_arc_xpm;
  468. }
  469. void LIB_ARC::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
  470. {
  471. wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
  472. wxT( "Invalid edit mode for LIB_ARC object." ) );
  473. if( aEditMode == IS_NEW )
  474. {
  475. m_ArcStart = m_ArcEnd = aPosition;
  476. m_editState = m_lastEditState = 1;
  477. }
  478. else if( aEditMode == IS_MOVED )
  479. {
  480. m_initialPos = m_Pos;
  481. m_initialCursorPos = aPosition;
  482. SetEraseLastDrawItem();
  483. }
  484. else
  485. {
  486. // The arc center point has to be rotated with while adjusting the
  487. // start or end point, determine the side of this point and the distance
  488. // from the start / end point
  489. wxPoint middlePoint = wxPoint( (m_ArcStart.x + m_ArcEnd.x) / 2,
  490. (m_ArcStart.y + m_ArcEnd.y) / 2 );
  491. wxPoint centerVector = m_Pos - middlePoint;
  492. wxPoint startEndVector = twoPointVector( m_ArcStart, m_ArcEnd );
  493. m_editCenterDistance = EuclideanNorm( centerVector );
  494. // Determine on which side is the center point
  495. m_editDirection = CrossProduct( startEndVector, centerVector ) ? 1 : -1;
  496. // Drag either the start, end point or the outline
  497. if( HitTestPoints( m_ArcStart, aPosition, MINIMUM_SELECTION_DISTANCE ) )
  498. {
  499. m_editSelectPoint = ARC_STATUS_START;
  500. }
  501. else if( HitTestPoints( m_ArcEnd, aPosition, MINIMUM_SELECTION_DISTANCE ) )
  502. {
  503. m_editSelectPoint = ARC_STATUS_END;
  504. }
  505. else
  506. {
  507. m_editSelectPoint = ARC_STATUS_OUTLINE;
  508. }
  509. m_editState = 0;
  510. SetEraseLastDrawItem();
  511. }
  512. m_Flags = aEditMode;
  513. }
  514. bool LIB_ARC::ContinueEdit( const wxPoint aPosition )
  515. {
  516. wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0, false,
  517. wxT( "Bad call to ContinueEdit(). LIB_ARC is not being edited." ) );
  518. if( m_Flags == IS_NEW )
  519. {
  520. if( m_editState == 1 ) // Second position yields the arc segment length.
  521. {
  522. m_ArcEnd = aPosition;
  523. m_editState = 2;
  524. SetEraseLastDrawItem( false );
  525. return true; // Need third position to calculate center point.
  526. }
  527. }
  528. return false;
  529. }
  530. void LIB_ARC::EndEdit( const wxPoint& aPosition, bool aAbort )
  531. {
  532. wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
  533. wxT( "Bad call to EndEdit(). LIB_ARC is not being edited." ) );
  534. SetEraseLastDrawItem( false );
  535. m_lastEditState = 0;
  536. m_editState = 0;
  537. m_Flags = 0;
  538. }
  539. void LIB_ARC::calcEdit( const wxPoint& aPosition )
  540. {
  541. if( m_Flags == IS_RESIZED )
  542. {
  543. wxPoint newCenterPoint, startPos, endPos;
  544. // Choose the point of the arc to be adjusted
  545. if( m_editSelectPoint == ARC_STATUS_START )
  546. {
  547. startPos = aPosition;
  548. endPos = m_ArcEnd;
  549. }
  550. else if( m_editSelectPoint == ARC_STATUS_END )
  551. {
  552. endPos = aPosition;
  553. startPos = m_ArcStart;
  554. }
  555. else
  556. {
  557. // Use the cursor for adjusting the arc curvature
  558. startPos = m_ArcStart;
  559. endPos = m_ArcEnd;
  560. // If the distance is too small, use the old center point
  561. // else the new center point is calculated over the three points start/end/cursor
  562. if( DistanceLinePoint( startPos, endPos, aPosition ) > MINIMUM_SELECTION_DISTANCE )
  563. {
  564. newCenterPoint = calcCenter( startPos, aPosition, endPos );
  565. }
  566. else
  567. {
  568. newCenterPoint = m_Pos;
  569. }
  570. // Determine if the arc angle is larger than 180 degrees -> this happens if both
  571. // points (cursor position, center point) lie on the same side of the vector
  572. // start-end
  573. double crossA = CrossProduct( twoPointVector( startPos, endPos ),
  574. twoPointVector( endPos, aPosition ) );
  575. double crossB = CrossProduct( twoPointVector( startPos, endPos ),
  576. twoPointVector( endPos, newCenterPoint ) );
  577. if( ( crossA < 0 && crossB < 0 ) || ( crossA >= 0 && crossB >= 0 ) )
  578. newCenterPoint = m_Pos;
  579. }
  580. if( m_editSelectPoint == ARC_STATUS_START || m_editSelectPoint == ARC_STATUS_END )
  581. {
  582. // Compute the new center point when the start/end points are modified
  583. wxPoint middlePoint = wxPoint( (startPos.x + endPos.x) / 2,
  584. (startPos.y + endPos.y) / 2 );
  585. wxPoint startEndVector = twoPointVector( startPos, endPos );
  586. wxPoint perpendicularVector = wxPoint( -startEndVector.y, startEndVector.x );
  587. double lengthPerpendicularVector = EuclideanNorm( perpendicularVector );
  588. // prevent too large values, division / 0
  589. if( lengthPerpendicularVector < 1e-1 )
  590. lengthPerpendicularVector = 1e-1;
  591. perpendicularVector.x = (int) ( (double) perpendicularVector.x *
  592. m_editCenterDistance /
  593. lengthPerpendicularVector ) * m_editDirection;
  594. perpendicularVector.y = (int) ( (double) perpendicularVector.y *
  595. m_editCenterDistance /
  596. lengthPerpendicularVector ) * m_editDirection;
  597. newCenterPoint = middlePoint + perpendicularVector;
  598. m_ArcStart = startPos;
  599. m_ArcEnd = endPos;
  600. }
  601. m_Pos = newCenterPoint;
  602. calcRadiusAngles();
  603. }
  604. else if( m_Flags == IS_NEW )
  605. {
  606. if( m_editState == 1 )
  607. {
  608. m_ArcEnd = aPosition;
  609. }
  610. if( m_editState != m_lastEditState )
  611. m_lastEditState = m_editState;
  612. // Keep the arc center point up to date. Otherwise, there will be edit graphic
  613. // artifacts left behind from the initial draw.
  614. int dx, dy;
  615. int cX, cY;
  616. double angle;
  617. cX = aPosition.x;
  618. cY = aPosition.y;
  619. dx = m_ArcEnd.x - m_ArcStart.x;
  620. dy = m_ArcEnd.y - m_ArcStart.y;
  621. cX -= m_ArcStart.x;
  622. cY -= m_ArcStart.y;
  623. angle = ArcTangente( dy, dx );
  624. RotatePoint( &dx, &dy, angle ); /* The segment dx, dy is horizontal
  625. * -> Length = dx, dy = 0 */
  626. RotatePoint( &cX, &cY, angle );
  627. cX = dx / 2; /* cX, cY is on the median segment 0.0 a dx, 0 */
  628. RotatePoint( &cX, &cY, -angle );
  629. cX += m_ArcStart.x;
  630. cY += m_ArcStart.y;
  631. m_Pos.x = cX;
  632. m_Pos.y = cY;
  633. calcRadiusAngles();
  634. SetEraseLastDrawItem();
  635. }
  636. else if( m_Flags == IS_MOVED )
  637. {
  638. Move( m_initialPos + aPosition - m_initialCursorPos );
  639. }
  640. }
  641. void LIB_ARC::calcRadiusAngles()
  642. {
  643. wxPoint centerStartVector = twoPointVector( m_Pos, m_ArcStart );
  644. wxPoint centerEndVector = twoPointVector( m_Pos, m_ArcEnd );
  645. m_Radius = KiROUND( EuclideanNorm( centerStartVector ) );
  646. // Angles in eeschema are still integers
  647. m_t1 = KiROUND( ArcTangente( centerStartVector.y, centerStartVector.x ) );
  648. m_t2 = KiROUND( ArcTangente( centerEndVector.y, centerEndVector.x ) );
  649. NORMALIZE_ANGLE_POS( m_t1 );
  650. NORMALIZE_ANGLE_POS( m_t2 ); // angles = 0 .. 3600
  651. // Restrict angle to less than 180 to avoid PBS display mirror Trace because it is
  652. // assumed that the arc is less than 180 deg to find orientation after rotate or mirror.
  653. if( (m_t2 - m_t1) > 1800 )
  654. m_t2 -= 3600;
  655. else if( (m_t2 - m_t1) <= -1800 )
  656. m_t2 += 3600;
  657. while( (m_t2 - m_t1) >= 1800 )
  658. {
  659. m_t2--;
  660. m_t1++;
  661. }
  662. while( (m_t1 - m_t2) >= 1800 )
  663. {
  664. m_t2++;
  665. m_t1--;
  666. }
  667. NORMALIZE_ANGLE_POS( m_t1 );
  668. if( !IsMoving() )
  669. NORMALIZE_ANGLE_POS( m_t2 );
  670. }