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.

408 lines
12 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
16 years ago
17 years ago
17 years ago
14 years ago
14 years ago
17 years ago
17 years ago
17 years ago
14 years ago
17 years ago
14 years ago
14 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
14 years ago
14 years ago
14 years ago
17 years ago
17 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
  7. * Copyright (C) 1992-2013 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 edgemod.cpp:
  28. * @brief Functions to edit graphic items used to draw footprint edges.
  29. *
  30. * @todo - Arc functions not compete but menus are ready to use.
  31. */
  32. #include <fctsys.h>
  33. #include <trigo.h>
  34. #include <gr_basic.h>
  35. #include <class_drawpanel.h>
  36. #include <confirm.h>
  37. #include <pcb_edit_frame.h>
  38. #include <base_units.h>
  39. #include <dialog_text_entry.h>
  40. #include <footprint_edit_frame.h>
  41. #include <class_board.h>
  42. #include <class_module.h>
  43. #include <class_edge_mod.h>
  44. #include <pcbnew.h>
  45. static void ShowNewEdgeModule( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
  46. bool erase );
  47. static void Abort_Move_ModuleOutline( EDA_DRAW_PANEL* Panel, wxDC* DC );
  48. static void ShowCurrentOutlineWhileMoving( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  49. const wxPoint& aPosition, bool aErase );
  50. static double ArcValue = 900;
  51. static wxPoint MoveVector; // Move vector for move edge
  52. static wxPoint CursorInitialPosition; // Mouse cursor initial position for move command
  53. void FOOTPRINT_EDIT_FRAME::Start_Move_EdgeMod( EDGE_MODULE* aEdge, wxDC* DC )
  54. {
  55. if( aEdge == NULL )
  56. return;
  57. aEdge->Draw( m_canvas, DC, GR_XOR );
  58. aEdge->SetFlags( IS_MOVED );
  59. MoveVector.x = MoveVector.y = 0;
  60. CursorInitialPosition = GetCrossHairPosition();
  61. m_canvas->SetMouseCapture( ShowCurrentOutlineWhileMoving, Abort_Move_ModuleOutline );
  62. SetCurItem( aEdge );
  63. m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
  64. }
  65. void FOOTPRINT_EDIT_FRAME::Place_EdgeMod( EDGE_MODULE* aEdge )
  66. {
  67. if( aEdge == NULL )
  68. return;
  69. aEdge->Move( -MoveVector );
  70. aEdge->ClearFlags();
  71. m_canvas->SetMouseCapture( NULL, NULL );
  72. SetCurItem( NULL );
  73. OnModify();
  74. MODULE* module = (MODULE*) aEdge->GetParent();
  75. module->CalculateBoundingBox();
  76. m_canvas->Refresh( );
  77. }
  78. /* Redraw the current moved graphic item when mouse is moving
  79. * Use this function to show an existing outline, in move command
  80. */
  81. static void ShowCurrentOutlineWhileMoving( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  82. const wxPoint& aPosition, bool aErase )
  83. {
  84. BASE_SCREEN* screen = aPanel->GetScreen();
  85. EDGE_MODULE* edge = (EDGE_MODULE*) screen->GetCurItem();
  86. if( edge == NULL )
  87. return;
  88. MODULE* module = (MODULE*) edge->GetParent();
  89. if( aErase )
  90. {
  91. edge->Draw( aPanel, aDC, GR_XOR, MoveVector );
  92. }
  93. MoveVector = -(aPanel->GetParent()->GetCrossHairPosition() - CursorInitialPosition);
  94. edge->Draw( aPanel, aDC, GR_XOR, MoveVector );
  95. module->CalculateBoundingBox();
  96. }
  97. /* Redraw the current graphic item during its creation
  98. * Use this function to show a new outline, in begin command
  99. */
  100. static void ShowNewEdgeModule( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
  101. bool aErase )
  102. {
  103. BASE_SCREEN* screen = aPanel->GetScreen();
  104. EDGE_MODULE* edge = (EDGE_MODULE*) screen->GetCurItem();
  105. if( edge == NULL )
  106. return;
  107. MODULE* module = (MODULE*) edge->GetParent();
  108. // if( erase )
  109. {
  110. edge->Draw( aPanel, aDC, GR_XOR );
  111. }
  112. edge->SetEnd( aPanel->GetParent()->GetCrossHairPosition() );
  113. // Update relative coordinate.
  114. edge->SetEnd0( edge->GetEnd() - module->GetPosition() );
  115. wxPoint pt( edge->GetEnd0() );
  116. RotatePoint( &pt, -module->GetOrientation() );
  117. edge->SetEnd0( pt );
  118. edge->Draw( aPanel, aDC, GR_XOR );
  119. module->CalculateBoundingBox();
  120. }
  121. void FOOTPRINT_EDIT_FRAME::Edit_Edge_Width( EDGE_MODULE* aEdge )
  122. {
  123. MODULE* module = GetBoard()->m_Modules;
  124. SaveCopyInUndoList( module, UR_CHANGED );
  125. if( aEdge == NULL )
  126. {
  127. for( BOARD_ITEM *item = module->GraphicalItemsList(); item; item = item->Next() )
  128. {
  129. aEdge = dyn_cast<EDGE_MODULE*>( item );
  130. if( aEdge )
  131. aEdge->SetWidth( GetDesignSettings().GetLineThickness( aEdge->GetLayer() ) );
  132. }
  133. }
  134. else
  135. {
  136. aEdge->SetWidth( GetDesignSettings().GetLineThickness( aEdge->GetLayer() ) );
  137. }
  138. OnModify();
  139. module->CalculateBoundingBox();
  140. module->SetLastEditTime();
  141. }
  142. void FOOTPRINT_EDIT_FRAME::Edit_Edge_Layer( EDGE_MODULE* aEdge )
  143. {
  144. // note: if aEdge == NULL, all outline segments will be modified
  145. MODULE* module = GetBoard()->m_Modules;
  146. PCB_LAYER_ID layer = F_SilkS;
  147. bool modified = false;
  148. if( aEdge )
  149. layer = aEdge->GetLayer();
  150. // Ask for the new layer
  151. PCB_LAYER_ID new_layer = SelectLayer( layer, Edge_Cuts );
  152. if( layer < 0 )
  153. return;
  154. if( IsCopperLayer( new_layer ) )
  155. {
  156. // an edge is put on a copper layer, and it is very dangerous.
  157. // A confirmation is requested
  158. if( !IsOK( this,
  159. _( "The graphic item will be on a copper layer.\n"
  160. "This is very dangerous. Are you sure?" ) ) )
  161. return;
  162. }
  163. if( !aEdge )
  164. {
  165. for( BOARD_ITEM *item = module->GraphicalItemsList() ; item != NULL;
  166. item = item->Next() )
  167. {
  168. aEdge = dyn_cast<EDGE_MODULE*>( item );
  169. if( aEdge && (aEdge->GetLayer() != new_layer) )
  170. {
  171. if( ! modified ) // save only once
  172. SaveCopyInUndoList( module, UR_CHANGED );
  173. aEdge->SetLayer( new_layer );
  174. modified = true;
  175. }
  176. }
  177. }
  178. else if( aEdge->GetLayer() != new_layer )
  179. {
  180. SaveCopyInUndoList( module, UR_CHANGED );
  181. aEdge->SetLayer( new_layer );
  182. modified = true;
  183. }
  184. if( modified )
  185. {
  186. module->CalculateBoundingBox();
  187. module->SetLastEditTime();
  188. }
  189. }
  190. void FOOTPRINT_EDIT_FRAME::Delete_Edge_Module( EDGE_MODULE* aEdge )
  191. {
  192. if( aEdge == NULL )
  193. return;
  194. if( aEdge->Type() != PCB_MODULE_EDGE_T )
  195. {
  196. DisplayError( this, wxT( "StructType error: PCB_MODULE_EDGE_T expected" ) );
  197. return;
  198. }
  199. MODULE* module = (MODULE*) aEdge->GetParent();
  200. // Delete segment.
  201. aEdge->DeleteStructure();
  202. module->SetLastEditTime();
  203. module->CalculateBoundingBox();
  204. OnModify();
  205. }
  206. /* abort function in moving outline.
  207. */
  208. static void Abort_Move_ModuleOutline( EDA_DRAW_PANEL* Panel, wxDC* DC )
  209. {
  210. EDGE_MODULE* edge = (EDGE_MODULE*) Panel->GetScreen()->GetCurItem();
  211. Panel->SetMouseCapture( NULL, NULL );
  212. if( edge && ( edge->Type() == PCB_MODULE_EDGE_T ) )
  213. {
  214. if( edge->IsNew() ) // On aborting, delete new outline.
  215. {
  216. MODULE* module = (MODULE*) edge->GetParent();
  217. edge->Draw( Panel, DC, GR_XOR, MoveVector );
  218. edge->DeleteStructure();
  219. module->CalculateBoundingBox();
  220. }
  221. else // On aborting, move existing outline to its initial position.
  222. {
  223. edge->Draw( Panel, DC, GR_XOR, MoveVector );
  224. edge->ClearFlags();
  225. edge->Draw( Panel, DC, GR_OR );
  226. }
  227. }
  228. Panel->GetScreen()->SetCurItem( NULL );
  229. }
  230. EDGE_MODULE* FOOTPRINT_EDIT_FRAME::Begin_Edge_Module( EDGE_MODULE* aEdge,
  231. wxDC* DC,
  232. STROKE_T type_edge )
  233. {
  234. MODULE* module = GetBoard()->m_Modules;
  235. if( module == NULL )
  236. return NULL;
  237. if( aEdge == NULL ) // Start a new edge item
  238. {
  239. SaveCopyInUndoList( module, UR_CHANGED );
  240. aEdge = new EDGE_MODULE( module );
  241. MoveVector.x = MoveVector.y = 0;
  242. // Add the new item to the Drawings list head
  243. module->GraphicalItemsList().PushFront( aEdge );
  244. // Update characteristics of the segment or arc.
  245. aEdge->SetFlags( IS_NEW );
  246. aEdge->SetAngle( 0 );
  247. aEdge->SetShape( type_edge );
  248. if( aEdge->GetShape() == S_ARC )
  249. aEdge->SetAngle( ArcValue );
  250. aEdge->SetWidth( GetDesignSettings().GetLineThickness( GetActiveLayer() ) );
  251. aEdge->SetLayer( GetActiveLayer() );
  252. // Initialize the starting point of the new segment or arc
  253. aEdge->SetStart( GetCrossHairPosition() );
  254. // Initialize the ending point of the new segment or arc
  255. aEdge->SetEnd( aEdge->GetStart() );
  256. // Initialize the relative coordinates
  257. aEdge->SetStart0( aEdge->GetStart() - module->GetPosition() );
  258. RotatePoint( &aEdge->m_Start0, -module->GetOrientation() );
  259. aEdge->m_End0 = aEdge->m_Start0;
  260. module->CalculateBoundingBox();
  261. m_canvas->SetMouseCapture( ShowNewEdgeModule, Abort_Move_ModuleOutline );
  262. }
  263. /* Segment creation in progress.
  264. * The ending coordinate is updated by the function
  265. * ShowNewEdgeModule() called on move mouse event
  266. * during the segment creation
  267. */
  268. else
  269. {
  270. if( type_edge == S_SEGMENT )
  271. {
  272. if( aEdge->m_Start0 != aEdge->m_End0 )
  273. {
  274. aEdge->Draw( m_canvas, DC, GR_OR );
  275. EDGE_MODULE* newedge = new EDGE_MODULE( *aEdge );
  276. // insert _after_ aEdge, which is the same as inserting before aEdge->Next()
  277. module->GraphicalItemsList().Insert( newedge, aEdge->Next() );
  278. aEdge->ClearFlags();
  279. aEdge = newedge; // point now new item
  280. aEdge->SetFlags( IS_NEW );
  281. aEdge->SetWidth( GetDesignSettings().GetLineThickness( aEdge->GetLayer() ) );
  282. aEdge->SetStart( GetCrossHairPosition() );
  283. aEdge->SetEnd( aEdge->GetStart() );
  284. // Update relative coordinate.
  285. aEdge->SetStart0( aEdge->GetStart() - module->GetPosition() );
  286. wxPoint pt( aEdge->GetStart0() );
  287. RotatePoint( &pt, -module->GetOrientation() );
  288. aEdge->SetStart0( pt );
  289. aEdge->SetEnd0( aEdge->GetStart0() );
  290. module->CalculateBoundingBox();
  291. module->SetLastEditTime();
  292. OnModify();
  293. }
  294. }
  295. else
  296. {
  297. wxMessageBox( wxT( "Begin_Edge() error" ) );
  298. }
  299. }
  300. return aEdge;
  301. }
  302. void FOOTPRINT_EDIT_FRAME::End_Edge_Module( EDGE_MODULE* aEdge )
  303. {
  304. MODULE* module = GetBoard()->m_Modules;
  305. if( aEdge )
  306. {
  307. aEdge->ClearFlags();
  308. // If last segment length is 0: remove it
  309. if( aEdge->GetStart() == aEdge->GetEnd() )
  310. aEdge->DeleteStructure();
  311. }
  312. module->CalculateBoundingBox();
  313. module->SetLastEditTime();
  314. OnModify();
  315. m_canvas->SetMouseCapture( NULL, NULL );
  316. }