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.

445 lines
12 KiB

14 years ago
14 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
  5. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
  7. * Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. /**
  27. * @file class_edge_mod.cpp
  28. * @brief EDGE_MODULE class definition.
  29. */
  30. #include <fctsys.h>
  31. #include <gr_basic.h>
  32. #include <trigo.h>
  33. #include <pcb_screen.h>
  34. #include <confirm.h>
  35. #include <kicad_string.h>
  36. #include <richio.h>
  37. #include <macros.h>
  38. #include <math_for_graphics.h>
  39. #include <pcb_base_frame.h>
  40. #include <msgpanel.h>
  41. #include <base_units.h>
  42. #include <bitmaps.h>
  43. #include <pcb_edit_frame.h>
  44. #include <class_board.h>
  45. #include <class_module.h>
  46. #include <class_edge_mod.h>
  47. #include <view/view.h>
  48. #include <stdio.h>
  49. EDGE_MODULE::EDGE_MODULE( MODULE* parent, STROKE_T aShape ) :
  50. DRAWSEGMENT( parent, PCB_MODULE_EDGE_T )
  51. {
  52. m_Shape = aShape;
  53. m_Angle = 0;
  54. m_Layer = F_SilkS;
  55. }
  56. EDGE_MODULE::~EDGE_MODULE()
  57. {
  58. }
  59. void EDGE_MODULE::SetLocalCoord()
  60. {
  61. MODULE* module = (MODULE*) m_Parent;
  62. if( module == NULL )
  63. {
  64. m_Start0 = m_Start;
  65. m_End0 = m_End;
  66. m_Bezier0_C1 = m_BezierC1;
  67. m_Bezier0_C2 = m_BezierC2;
  68. return;
  69. }
  70. m_Start0 = m_Start - module->GetPosition();
  71. m_End0 = m_End - module->GetPosition();
  72. m_Bezier0_C1 = m_BezierC1 - module->GetPosition();
  73. m_Bezier0_C2 = m_BezierC2 - module->GetPosition();
  74. double angle = module->GetOrientation();
  75. RotatePoint( &m_Start0.x, &m_Start0.y, -angle );
  76. RotatePoint( &m_End0.x, &m_End0.y, -angle );
  77. RotatePoint( &m_Bezier0_C1.x, &m_Bezier0_C1.y, -angle );
  78. RotatePoint( &m_Bezier0_C2.x, &m_Bezier0_C2.y, -angle );
  79. }
  80. void EDGE_MODULE::SetDrawCoord()
  81. {
  82. MODULE* module = (MODULE*) m_Parent;
  83. m_Start = m_Start0;
  84. m_End = m_End0;
  85. m_BezierC1 = m_Bezier0_C1;
  86. m_BezierC2 = m_Bezier0_C2;
  87. if( module )
  88. {
  89. RotatePoint( &m_Start.x, &m_Start.y, module->GetOrientation() );
  90. RotatePoint( &m_End.x, &m_End.y, module->GetOrientation() );
  91. RotatePoint( &m_BezierC1.x, &m_BezierC1.y, module->GetOrientation() );
  92. RotatePoint( &m_BezierC2.x, &m_BezierC2.y, module->GetOrientation() );
  93. m_Start += module->GetPosition();
  94. m_End += module->GetPosition();
  95. m_BezierC1 += module->GetPosition();
  96. m_BezierC2 += module->GetPosition();
  97. }
  98. RebuildBezierToSegmentsPointsList( m_Width );
  99. }
  100. void EDGE_MODULE::Print( PCB_BASE_FRAME* aFrame, wxDC* DC, const wxPoint& offset )
  101. {
  102. int ux0, uy0, dx, dy, radius, StAngle, EndAngle;
  103. MODULE* module = (MODULE*) m_Parent;
  104. BOARD* brd = GetBoard( );
  105. if( !module || !brd->IsLayerVisible( m_Layer ) )
  106. return;
  107. auto color = aFrame->Settings().Colors().GetLayerColor( m_Layer );
  108. auto displ_opts = (PCB_DISPLAY_OPTIONS*)( aFrame->GetDisplayOptions() );
  109. ux0 = m_Start.x - offset.x;
  110. uy0 = m_Start.y - offset.y;
  111. dx = m_End.x - offset.x;
  112. dy = m_End.y - offset.y;
  113. bool filled = displ_opts ? displ_opts->m_DisplayModEdgeFill : FILLED;
  114. if( IsCopperLayer( m_Layer ) )
  115. filled = displ_opts ? displ_opts->m_DisplayPcbTrackFill : FILLED;
  116. switch( m_Shape )
  117. {
  118. case S_SEGMENT:
  119. if( filled )
  120. GRLine( nullptr, DC, ux0, uy0, dx, dy, m_Width, color );
  121. else
  122. // SKETCH Mode
  123. GRCSegm( nullptr, DC, ux0, uy0, dx, dy, m_Width, color );
  124. break;
  125. case S_CIRCLE:
  126. radius = KiROUND( Distance( ux0, uy0, dx, dy ) );
  127. if( filled )
  128. {
  129. GRCircle( nullptr, DC, ux0, uy0, radius, m_Width, color );
  130. }
  131. else // SKETCH Mode
  132. {
  133. GRCircle( nullptr, DC, ux0, uy0, radius + (m_Width / 2), color );
  134. GRCircle( nullptr, DC, ux0, uy0, radius - (m_Width / 2), color );
  135. }
  136. break;
  137. case S_ARC:
  138. radius = KiROUND( Distance( ux0, uy0, dx, dy ) );
  139. StAngle = ArcTangente( dy - uy0, dx - ux0 );
  140. EndAngle = StAngle + m_Angle;
  141. if( StAngle > EndAngle )
  142. std::swap( StAngle, EndAngle );
  143. if( filled )
  144. {
  145. GRArc( nullptr, DC, ux0, uy0, StAngle, EndAngle, radius, m_Width, color );
  146. }
  147. else // SKETCH Mode
  148. {
  149. GRArc( nullptr, DC, ux0, uy0, StAngle, EndAngle, radius + (m_Width / 2), color );
  150. GRArc( nullptr, DC, ux0, uy0, StAngle, EndAngle, radius - (m_Width / 2), color );
  151. }
  152. break;
  153. case S_POLYGON:
  154. if( m_Poly.IsEmpty() )
  155. break;
  156. {
  157. // We must compute absolute coordinates from m_PolyPoints
  158. // which are relative to module position, orientation 0
  159. std::vector<wxPoint> points;
  160. for( auto iter = m_Poly.CIterate(); iter; iter++ )
  161. {
  162. points.push_back( wxPoint( iter->x,iter->y ) );
  163. }
  164. for( unsigned ii = 0; ii < points.size(); ii++ )
  165. {
  166. wxPoint& pt = points[ii];
  167. RotatePoint( &pt.x, &pt.y, module->GetOrientation() );
  168. pt += module->GetPosition() - offset;
  169. }
  170. GRPoly( nullptr, DC, points.size(), &points[0], true, m_Width, color, color );
  171. }
  172. break;
  173. case S_CURVE:
  174. {
  175. RebuildBezierToSegmentsPointsList( m_Width );
  176. wxPoint& startp = m_BezierPoints[0];
  177. for( unsigned int i = 1; i < m_BezierPoints.size(); i++ )
  178. {
  179. wxPoint& endp = m_BezierPoints[i];
  180. if( filled )
  181. GRFilledSegment( nullptr, DC, startp-offset, endp-offset, m_Width, color );
  182. else
  183. GRCSegm( nullptr, DC, startp-offset, endp-offset, m_Width, color );
  184. startp = m_BezierPoints[i];
  185. }
  186. }
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. // see class_edge_mod.h
  193. void EDGE_MODULE::GetMsgPanelInfo( EDA_UNITS_T aUnits, std::vector< MSG_PANEL_ITEM >& aList )
  194. {
  195. wxString msg;
  196. MODULE* module = (MODULE*) m_Parent;
  197. if( !module )
  198. return;
  199. BOARD* board = (BOARD*) module->GetParent();
  200. if( !board )
  201. return;
  202. aList.push_back( MSG_PANEL_ITEM( _( "Footprint" ), module->GetReference(), DARKCYAN ) );
  203. // append the features shared with the base class
  204. DRAWSEGMENT::GetMsgPanelInfo( aUnits, aList );
  205. }
  206. wxString EDGE_MODULE::GetSelectMenuText( EDA_UNITS_T aUnits ) const
  207. {
  208. return wxString::Format( _( "Graphic %s of %s on %s" ),
  209. ShowShape( m_Shape ),
  210. ((MODULE*) GetParent())->GetReference(),
  211. GetLayerName() );
  212. }
  213. BITMAP_DEF EDGE_MODULE::GetMenuImage() const
  214. {
  215. return show_mod_edge_xpm;
  216. }
  217. EDA_ITEM* EDGE_MODULE::Clone() const
  218. {
  219. return new EDGE_MODULE( *this );
  220. }
  221. void EDGE_MODULE::Flip( const wxPoint& aCentre )
  222. {
  223. wxPoint pt;
  224. switch( GetShape() )
  225. {
  226. case S_ARC:
  227. SetAngle( -GetAngle() );
  228. //Fall through
  229. default:
  230. case S_SEGMENT:
  231. case S_CURVE:
  232. pt = GetStart();
  233. MIRROR( pt.y, aCentre.y );
  234. SetStart( pt );
  235. pt = GetEnd();
  236. MIRROR( pt.y, aCentre.y );
  237. SetEnd( pt );
  238. MIRROR( m_BezierC1.y, aCentre.y );
  239. MIRROR( m_BezierC2.y, aCentre.y );
  240. MIRROR( m_Start0.y, 0 );
  241. MIRROR( m_End0.y, 0 );
  242. MIRROR( m_Bezier0_C1.y, 0 );
  243. MIRROR( m_Bezier0_C2.y, 0 );
  244. RebuildBezierToSegmentsPointsList( m_Width );
  245. break;
  246. case S_POLYGON:
  247. // polygon corners coordinates are always relative to the
  248. // footprint position, orientation 0
  249. for( auto iter = m_Poly.Iterate(); iter; iter++ )
  250. {
  251. MIRROR( iter->y, 0 );
  252. }
  253. break;
  254. }
  255. // DRAWSEGMENT items are not usually on copper layers, but
  256. // it can happen in microwave apps.
  257. // However, currently, only on Front or Back layers.
  258. // So the copper layers count is not taken in account
  259. SetLayer( FlipLayer( GetLayer() ) );
  260. }
  261. bool EDGE_MODULE::IsParentFlipped() const
  262. {
  263. if( GetParent() && GetParent()->GetLayer() == B_Cu )
  264. return true;
  265. return false;
  266. }
  267. void EDGE_MODULE::Mirror( wxPoint aCentre, bool aMirrorAroundXAxis )
  268. {
  269. // Mirror an edge of the footprint. the layer is not modified
  270. // This is a footprint shape modification.
  271. switch( GetShape() )
  272. {
  273. case S_ARC:
  274. SetAngle( -GetAngle() );
  275. //Fall through
  276. default:
  277. case S_CURVE:
  278. case S_SEGMENT:
  279. if( aMirrorAroundXAxis )
  280. {
  281. MIRROR( m_Start0.y, aCentre.y );
  282. MIRROR( m_End0.y, aCentre.y );
  283. MIRROR( m_Bezier0_C1.y, aCentre.y );
  284. MIRROR( m_Bezier0_C2.y, aCentre.y );
  285. }
  286. else
  287. {
  288. MIRROR( m_Start0.x, aCentre.x );
  289. MIRROR( m_End0.x, aCentre.x );
  290. MIRROR( m_Bezier0_C1.x, aCentre.x );
  291. MIRROR( m_Bezier0_C2.x, aCentre.x );
  292. }
  293. for( unsigned ii = 0; ii < m_BezierPoints.size(); ii++ )
  294. {
  295. if( aMirrorAroundXAxis )
  296. MIRROR( m_BezierPoints[ii].y, aCentre.y );
  297. else
  298. MIRROR( m_BezierPoints[ii].x, aCentre.x );
  299. }
  300. break;
  301. case S_POLYGON:
  302. // polygon corners coordinates are always relative to the
  303. // footprint position, orientation 0
  304. for( auto iter = m_Poly.Iterate(); iter; iter++ )
  305. {
  306. if( aMirrorAroundXAxis )
  307. MIRROR( iter->y, aCentre.y );
  308. else
  309. MIRROR( iter->x, aCentre.x );
  310. }
  311. }
  312. SetDrawCoord();
  313. }
  314. void EDGE_MODULE::Rotate( const wxPoint& aRotCentre, double aAngle )
  315. {
  316. // We should rotate the relative coordinates, but to avoid duplicate code,
  317. // do the base class rotation of draw coordinates, which is acceptable
  318. // because in module editor, m_Pos0 = m_Pos
  319. DRAWSEGMENT::Rotate( aRotCentre, aAngle );
  320. // and now update the relative coordinates, which are
  321. // the reference in most transforms.
  322. SetLocalCoord();
  323. }
  324. void EDGE_MODULE::Move( const wxPoint& aMoveVector )
  325. {
  326. // Move an edge of the footprint.
  327. // This is a footprint shape modification.
  328. m_Start0 += aMoveVector;
  329. m_End0 += aMoveVector;
  330. m_Bezier0_C1 += aMoveVector;
  331. m_Bezier0_C2 += aMoveVector;
  332. switch( GetShape() )
  333. {
  334. default:
  335. break;
  336. case S_POLYGON:
  337. // polygon corners coordinates are always relative to the
  338. // footprint position, orientation 0
  339. for( auto iter = m_Poly.Iterate(); iter; iter++ )
  340. *iter += VECTOR2I( aMoveVector );
  341. break;
  342. }
  343. SetDrawCoord();
  344. }
  345. unsigned int EDGE_MODULE::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
  346. {
  347. const int HIDE = std::numeric_limits<unsigned int>::max();
  348. if( !aView )
  349. return 0;
  350. // Handle Render tab switches
  351. if( !IsParentFlipped() && !aView->IsLayerVisible( LAYER_MOD_FR ) )
  352. return HIDE;
  353. if( IsParentFlipped() && !aView->IsLayerVisible( LAYER_MOD_BK ) )
  354. return HIDE;
  355. // Other layers are shown without any conditions
  356. return 0;
  357. }