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.

334 lines
9.0 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 <math/util.h> // for KiROUND
  44. #include <pgm_base.h>
  45. #include <settings/color_settings.h>
  46. #include <settings/settings_manager.h>
  47. #include <pcb_edit_frame.h>
  48. #include <class_board.h>
  49. #include <class_module.h>
  50. #include <class_edge_mod.h>
  51. #include <view/view.h>
  52. #include <cstdio>
  53. EDGE_MODULE::EDGE_MODULE( MODULE* parent, STROKE_T aShape ) :
  54. DRAWSEGMENT( parent, PCB_MODULE_EDGE_T )
  55. {
  56. m_Shape = aShape;
  57. m_Angle = 0;
  58. m_Layer = F_SilkS;
  59. }
  60. EDGE_MODULE::~EDGE_MODULE()
  61. {
  62. }
  63. void EDGE_MODULE::SetLocalCoord()
  64. {
  65. MODULE* module = (MODULE*) m_Parent;
  66. if( module == NULL )
  67. {
  68. m_Start0 = m_Start;
  69. m_End0 = m_End;
  70. m_Bezier0_C1 = m_BezierC1;
  71. m_Bezier0_C2 = m_BezierC2;
  72. return;
  73. }
  74. m_Start0 = m_Start - module->GetPosition();
  75. m_End0 = m_End - module->GetPosition();
  76. m_Bezier0_C1 = m_BezierC1 - module->GetPosition();
  77. m_Bezier0_C2 = m_BezierC2 - module->GetPosition();
  78. double angle = module->GetOrientation();
  79. RotatePoint( &m_Start0.x, &m_Start0.y, -angle );
  80. RotatePoint( &m_End0.x, &m_End0.y, -angle );
  81. RotatePoint( &m_Bezier0_C1.x, &m_Bezier0_C1.y, -angle );
  82. RotatePoint( &m_Bezier0_C2.x, &m_Bezier0_C2.y, -angle );
  83. }
  84. void EDGE_MODULE::SetDrawCoord()
  85. {
  86. MODULE* module = (MODULE*) m_Parent;
  87. m_Start = m_Start0;
  88. m_End = m_End0;
  89. m_BezierC1 = m_Bezier0_C1;
  90. m_BezierC2 = m_Bezier0_C2;
  91. if( module )
  92. {
  93. RotatePoint( &m_Start.x, &m_Start.y, module->GetOrientation() );
  94. RotatePoint( &m_End.x, &m_End.y, module->GetOrientation() );
  95. RotatePoint( &m_BezierC1.x, &m_BezierC1.y, module->GetOrientation() );
  96. RotatePoint( &m_BezierC2.x, &m_BezierC2.y, module->GetOrientation() );
  97. m_Start += module->GetPosition();
  98. m_End += module->GetPosition();
  99. m_BezierC1 += module->GetPosition();
  100. m_BezierC2 += module->GetPosition();
  101. }
  102. RebuildBezierToSegmentsPointsList( m_Width );
  103. }
  104. // see class_edge_mod.h
  105. void EDGE_MODULE::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  106. {
  107. wxString msg;
  108. MODULE* module = (MODULE*) m_Parent;
  109. if( !module )
  110. return;
  111. BOARD* board = (BOARD*) module->GetParent();
  112. if( !board )
  113. return;
  114. aList.emplace_back( _( "Footprint" ), module->GetReference(), DARKCYAN );
  115. // append the features shared with the base class
  116. DRAWSEGMENT::GetMsgPanelInfo( aFrame, aList );
  117. }
  118. wxString EDGE_MODULE::GetSelectMenuText( EDA_UNITS aUnits ) const
  119. {
  120. return wxString::Format( _( "Graphic %s of %s on %s" ),
  121. ShowShape( m_Shape ),
  122. ((MODULE*) GetParent())->GetReference(),
  123. GetLayerName() );
  124. }
  125. BITMAP_DEF EDGE_MODULE::GetMenuImage() const
  126. {
  127. return show_mod_edge_xpm;
  128. }
  129. EDA_ITEM* EDGE_MODULE::Clone() const
  130. {
  131. return new EDGE_MODULE( *this );
  132. }
  133. void EDGE_MODULE::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
  134. {
  135. wxPoint pt( 0, 0 );
  136. switch( GetShape() )
  137. {
  138. case S_ARC:
  139. SetAngle( -GetAngle() );
  140. KI_FALLTHROUGH;
  141. default:
  142. case S_SEGMENT:
  143. case S_CURVE:
  144. // If Start0 and Start are equal (ie: ModEdit), then flip both sets around the
  145. // centre point.
  146. if( m_Start == m_Start0 )
  147. pt = aCentre;
  148. if( aFlipLeftRight )
  149. {
  150. MIRROR( m_Start.x, aCentre.x );
  151. MIRROR( m_End.x, aCentre.x );
  152. MIRROR( m_BezierC1.x, aCentre.x );
  153. MIRROR( m_BezierC2.x, aCentre.x );
  154. MIRROR( m_Start0.x, pt.x );
  155. MIRROR( m_End0.x, pt.x );
  156. MIRROR( m_Bezier0_C1.x, pt.x );
  157. MIRROR( m_Bezier0_C2.x, pt.x );
  158. }
  159. else
  160. {
  161. MIRROR( m_Start.y, aCentre.y );
  162. MIRROR( m_End.y, aCentre.y );
  163. MIRROR( m_BezierC1.y, aCentre.y );
  164. MIRROR( m_BezierC2.y, aCentre.y );
  165. MIRROR( m_Start0.y, pt.y );
  166. MIRROR( m_End0.y, pt.y );
  167. MIRROR( m_Bezier0_C1.y, pt.y );
  168. MIRROR( m_Bezier0_C2.y, pt.y );
  169. }
  170. RebuildBezierToSegmentsPointsList( m_Width );
  171. break;
  172. case S_POLYGON:
  173. // polygon corners coordinates are always relative to the
  174. // footprint position, orientation 0
  175. m_Poly.Mirror( aFlipLeftRight, !aFlipLeftRight );
  176. break;
  177. }
  178. // DRAWSEGMENT items are not usually on copper layers, but
  179. // it can happen in microwave apps.
  180. // However, currently, only on Front or Back layers.
  181. // So the copper layers count is not taken in account
  182. SetLayer( FlipLayer( GetLayer() ) );
  183. }
  184. bool EDGE_MODULE::IsParentFlipped() const
  185. {
  186. if( GetParent() && GetParent()->GetLayer() == B_Cu )
  187. return true;
  188. return false;
  189. }
  190. void EDGE_MODULE::Mirror( wxPoint aCentre, bool aMirrorAroundXAxis )
  191. {
  192. // Mirror an edge of the footprint. the layer is not modified
  193. // This is a footprint shape modification.
  194. switch( GetShape() )
  195. {
  196. case S_ARC:
  197. SetAngle( -GetAngle() );
  198. KI_FALLTHROUGH;
  199. default:
  200. case S_CURVE:
  201. case S_SEGMENT:
  202. if( aMirrorAroundXAxis )
  203. {
  204. MIRROR( m_Start0.y, aCentre.y );
  205. MIRROR( m_End0.y, aCentre.y );
  206. MIRROR( m_Bezier0_C1.y, aCentre.y );
  207. MIRROR( m_Bezier0_C2.y, aCentre.y );
  208. }
  209. else
  210. {
  211. MIRROR( m_Start0.x, aCentre.x );
  212. MIRROR( m_End0.x, aCentre.x );
  213. MIRROR( m_Bezier0_C1.x, aCentre.x );
  214. MIRROR( m_Bezier0_C2.x, aCentre.x );
  215. }
  216. for( unsigned ii = 0; ii < m_BezierPoints.size(); ii++ )
  217. {
  218. if( aMirrorAroundXAxis )
  219. MIRROR( m_BezierPoints[ii].y, aCentre.y );
  220. else
  221. MIRROR( m_BezierPoints[ii].x, aCentre.x );
  222. }
  223. break;
  224. case S_POLYGON:
  225. // polygon corners coordinates are always relative to the
  226. // footprint position, orientation 0
  227. m_Poly.Mirror( !aMirrorAroundXAxis, aMirrorAroundXAxis );
  228. break;
  229. }
  230. SetDrawCoord();
  231. }
  232. void EDGE_MODULE::Rotate( const wxPoint& aRotCentre, double aAngle )
  233. {
  234. // We should rotate the relative coordinates, but to avoid duplicate code,
  235. // do the base class rotation of draw coordinates, which is acceptable
  236. // because in module editor, m_Pos0 = m_Pos
  237. DRAWSEGMENT::Rotate( aRotCentre, aAngle );
  238. // and now update the relative coordinates, which are
  239. // the reference in most transforms.
  240. SetLocalCoord();
  241. }
  242. void EDGE_MODULE::Move( const wxPoint& aMoveVector )
  243. {
  244. // Move an edge of the footprint.
  245. // This is a footprint shape modification.
  246. m_Start0 += aMoveVector;
  247. m_End0 += aMoveVector;
  248. m_Bezier0_C1 += aMoveVector;
  249. m_Bezier0_C2 += aMoveVector;
  250. switch( GetShape() )
  251. {
  252. default:
  253. break;
  254. case S_POLYGON:
  255. // polygon corners coordinates are always relative to the
  256. // footprint position, orientation 0
  257. m_Poly.Move( VECTOR2I( aMoveVector ) );
  258. break;
  259. }
  260. SetDrawCoord();
  261. }
  262. unsigned int EDGE_MODULE::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
  263. {
  264. const int HIDE = std::numeric_limits<unsigned int>::max();
  265. if( !aView )
  266. return 0;
  267. // Handle Render tab switches
  268. if( !IsParentFlipped() && !aView->IsLayerVisible( LAYER_MOD_FR ) )
  269. return HIDE;
  270. if( IsParentFlipped() && !aView->IsLayerVisible( LAYER_MOD_BK ) )
  271. return HIDE;
  272. // Other layers are shown without any conditions
  273. return 0;
  274. }