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.

432 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
14 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
14 years ago
17 years ago
17 years ago
14 years ago
14 years ago
17 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) 2012 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-2012 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 <wxPcbStruct.h>
  38. #include <base_units.h>
  39. #include <module_editor_frame.h>
  40. #include <class_board.h>
  41. #include <class_module.h>
  42. #include <class_edge_mod.h>
  43. #include <pcbnew.h>
  44. static void ShowNewEdgeModule( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
  45. bool erase );
  46. static void Abort_Move_ModuleOutline( EDA_DRAW_PANEL* Panel, wxDC* DC );
  47. static void ShowCurrentOutlineWhileMoving( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  48. const wxPoint& aPosition, bool aErase );
  49. static double ArcValue = 900;
  50. static wxPoint MoveVector; // Move vector for move edge
  51. static wxPoint CursorInitialPosition; // Mouse cursor initial position for move command
  52. void FOOTPRINT_EDIT_FRAME::Start_Move_EdgeMod( EDGE_MODULE* aEdge, wxDC* DC )
  53. {
  54. if( aEdge == NULL )
  55. return;
  56. aEdge->Draw( m_canvas, DC, GR_XOR );
  57. aEdge->SetFlags( IS_MOVED );
  58. MoveVector.x = MoveVector.y = 0;
  59. CursorInitialPosition = GetScreen()->GetCrossHairPosition();
  60. m_canvas->SetMouseCapture( ShowCurrentOutlineWhileMoving, Abort_Move_ModuleOutline );
  61. SetCurItem( aEdge );
  62. m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
  63. }
  64. void FOOTPRINT_EDIT_FRAME::Place_EdgeMod( EDGE_MODULE* aEdge )
  65. {
  66. if( aEdge == NULL )
  67. return;
  68. aEdge->SetStart( aEdge->GetStart() - MoveVector );
  69. aEdge->SetEnd( aEdge->GetEnd() - MoveVector );
  70. aEdge->SetStart0( aEdge->GetStart0() - MoveVector );
  71. aEdge->SetEnd0( aEdge->GetEnd0() - MoveVector );
  72. aEdge->ClearFlags();
  73. m_canvas->SetMouseCapture( NULL, NULL );
  74. SetCurItem( NULL );
  75. OnModify();
  76. MODULE* module = (MODULE*) aEdge->GetParent();
  77. module->CalculateBoundingBox();
  78. m_canvas->Refresh( );
  79. }
  80. /* Redraw the current moved graphic item when mouse is moving
  81. * Use this function to show an existing outline, in move command
  82. */
  83. static void ShowCurrentOutlineWhileMoving( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  84. const wxPoint& aPosition, bool aErase )
  85. {
  86. BASE_SCREEN* screen = aPanel->GetScreen();
  87. EDGE_MODULE* edge = (EDGE_MODULE*) screen->GetCurItem();
  88. if( edge == NULL )
  89. return;
  90. MODULE* module = (MODULE*) edge->GetParent();
  91. if( aErase )
  92. {
  93. edge->Draw( aPanel, aDC, GR_XOR, MoveVector );
  94. }
  95. MoveVector = -(screen->GetCrossHairPosition() - CursorInitialPosition);
  96. edge->Draw( aPanel, aDC, GR_XOR, MoveVector );
  97. module->CalculateBoundingBox();
  98. }
  99. /* Redraw the current graphic item during its creation
  100. * Use this function to show a new outline, in begin command
  101. */
  102. static void ShowNewEdgeModule( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
  103. bool aErase )
  104. {
  105. BASE_SCREEN* screen = aPanel->GetScreen();
  106. EDGE_MODULE* edge = (EDGE_MODULE*) screen->GetCurItem();
  107. if( edge == NULL )
  108. return;
  109. MODULE* module = (MODULE*) edge->GetParent();
  110. // if( erase )
  111. {
  112. edge->Draw( aPanel, aDC, GR_XOR );
  113. }
  114. edge->SetEnd( screen->GetCrossHairPosition() );
  115. // Update relative coordinate.
  116. edge->SetEnd0( edge->GetEnd() - module->GetPosition() );
  117. wxPoint pt( edge->GetEnd0() );
  118. RotatePoint( &pt, -module->GetOrientation() );
  119. edge->SetEnd0( pt );
  120. edge->Draw( aPanel, aDC, GR_XOR );
  121. module->CalculateBoundingBox();
  122. }
  123. void FOOTPRINT_EDIT_FRAME::Edit_Edge_Width( EDGE_MODULE* aEdge )
  124. {
  125. MODULE* module = GetBoard()->m_Modules;
  126. SaveCopyInUndoList( module, UR_MODEDIT );
  127. if( aEdge == NULL )
  128. {
  129. aEdge = (EDGE_MODULE*) (BOARD_ITEM*) module->GraphicalItems();
  130. for( ; aEdge != NULL; aEdge = aEdge->Next() )
  131. {
  132. if( aEdge->Type() != PCB_MODULE_EDGE_T )
  133. continue;
  134. aEdge->SetWidth( GetDesignSettings().m_ModuleSegmentWidth );
  135. }
  136. }
  137. else
  138. {
  139. aEdge->SetWidth( GetDesignSettings().m_ModuleSegmentWidth );
  140. }
  141. OnModify();
  142. module->CalculateBoundingBox();
  143. module->SetLastEditTime();
  144. }
  145. void FOOTPRINT_EDIT_FRAME::Edit_Edge_Layer( EDGE_MODULE* aEdge )
  146. {
  147. MODULE* module = GetBoard()->m_Modules;
  148. LAYER_NUM new_layer = SILKSCREEN_N_FRONT;
  149. if( aEdge )
  150. new_layer = aEdge->GetLayer();
  151. // Ask for the new layer
  152. new_layer = SelectLayer( new_layer, FIRST_COPPER_LAYER, ECO2_N );
  153. if( new_layer < 0 )
  154. return;
  155. if( IsCopperLayer( new_layer ) )
  156. {
  157. /* an edge is put on a copper layer, and it is very dangerous. a
  158. *confirmation is requested */
  159. if( !IsOK( this,
  160. _( "The graphic item will be on a copper layer. This is very dangerous. Are you sure?" ) ) )
  161. return;
  162. }
  163. SaveCopyInUndoList( module, UR_MODEDIT );
  164. if( aEdge == NULL )
  165. {
  166. aEdge = (EDGE_MODULE*) (BOARD_ITEM*) module->GraphicalItems();
  167. for( ; aEdge != NULL; aEdge = aEdge->Next() )
  168. {
  169. if( aEdge->Type() != PCB_MODULE_EDGE_T )
  170. continue;
  171. aEdge->SetLayer( new_layer );
  172. }
  173. }
  174. else
  175. {
  176. aEdge->SetLayer( new_layer );
  177. }
  178. OnModify();
  179. module->CalculateBoundingBox();
  180. module->SetLastEditTime();
  181. }
  182. void FOOTPRINT_EDIT_FRAME::Enter_Edge_Width( EDGE_MODULE* aEdge )
  183. {
  184. wxString buffer;
  185. buffer = ReturnStringFromValue( g_UserUnit, GetDesignSettings().m_ModuleSegmentWidth );
  186. wxTextEntryDialog dlg( this, _( "New Width:" ), _( "Edge Width" ), buffer );
  187. if( dlg.ShowModal() != wxID_OK )
  188. return; // canceled by user
  189. buffer = dlg.GetValue( );
  190. GetDesignSettings().m_ModuleSegmentWidth = ReturnValueFromString( g_UserUnit, buffer );
  191. if( aEdge )
  192. {
  193. MODULE* module = GetBoard()->m_Modules;
  194. aEdge->SetWidth( GetDesignSettings().m_ModuleSegmentWidth );
  195. module->CalculateBoundingBox();
  196. OnModify();
  197. }
  198. }
  199. void FOOTPRINT_EDIT_FRAME::Delete_Edge_Module( EDGE_MODULE* aEdge )
  200. {
  201. if( aEdge == NULL )
  202. return;
  203. if( aEdge->Type() != PCB_MODULE_EDGE_T )
  204. {
  205. DisplayError( this, wxT( "StructType error: PCB_MODULE_EDGE_T expected" ) );
  206. return;
  207. }
  208. MODULE* module = (MODULE*) aEdge->GetParent();
  209. // Delete segment.
  210. aEdge->DeleteStructure();
  211. module->SetLastEditTime();
  212. module->CalculateBoundingBox();
  213. OnModify();
  214. }
  215. /* abort function in moving outline.
  216. */
  217. static void Abort_Move_ModuleOutline( EDA_DRAW_PANEL* Panel, wxDC* DC )
  218. {
  219. EDGE_MODULE* edge = (EDGE_MODULE*) Panel->GetScreen()->GetCurItem();
  220. Panel->SetMouseCapture( NULL, NULL );
  221. if( edge && ( edge->Type() == PCB_MODULE_EDGE_T ) )
  222. {
  223. if( edge->IsNew() ) // On aborting, delete new outline.
  224. {
  225. MODULE* module = (MODULE*) edge->GetParent();
  226. edge->Draw( Panel, DC, GR_XOR, MoveVector );
  227. edge->DeleteStructure();
  228. module->CalculateBoundingBox();
  229. }
  230. else // On aborting, move existing outline to its initial position.
  231. {
  232. edge->Draw( Panel, DC, GR_XOR, MoveVector );
  233. edge->ClearFlags();
  234. edge->Draw( Panel, DC, GR_OR );
  235. }
  236. }
  237. Panel->GetScreen()->SetCurItem( NULL );
  238. }
  239. EDGE_MODULE* FOOTPRINT_EDIT_FRAME::Begin_Edge_Module( EDGE_MODULE* aEdge,
  240. wxDC* DC,
  241. STROKE_T type_edge )
  242. {
  243. MODULE* module = GetBoard()->m_Modules;
  244. if( module == NULL )
  245. return NULL;
  246. if( aEdge == NULL ) // Start a new edge item
  247. {
  248. SaveCopyInUndoList( module, UR_MODEDIT );
  249. aEdge = new EDGE_MODULE( module );
  250. MoveVector.x = MoveVector.y = 0;
  251. // Add the new item to the Drawings list head
  252. module->GraphicalItems().PushFront( aEdge );
  253. // Update characteristics of the segment or arc.
  254. aEdge->SetFlags( IS_NEW );
  255. aEdge->SetAngle( 0 );
  256. aEdge->SetShape( type_edge );
  257. if( aEdge->GetShape() == S_ARC )
  258. aEdge->SetAngle( ArcValue );
  259. aEdge->SetWidth( GetDesignSettings().m_ModuleSegmentWidth );
  260. aEdge->SetLayer( module->GetLayer() );
  261. // The default layer for an edge is the corresponding silk layer
  262. if( module->IsFlipped() )
  263. aEdge->SetLayer( SILKSCREEN_N_BACK );
  264. else
  265. aEdge->SetLayer( SILKSCREEN_N_FRONT );
  266. // Initialize the starting point of the new segment or arc
  267. aEdge->SetStart( GetScreen()->GetCrossHairPosition() );
  268. // Initialize the ending point of the new segment or arc
  269. aEdge->SetEnd( aEdge->GetStart() );
  270. // Initialize the relative coordinates
  271. aEdge->SetStart0( aEdge->GetStart() - module->GetPosition() );
  272. RotatePoint( &aEdge->m_Start0, -module->GetOrientation() );
  273. aEdge->m_End0 = aEdge->m_Start0;
  274. module->CalculateBoundingBox();
  275. m_canvas->SetMouseCapture( ShowNewEdgeModule, Abort_Move_ModuleOutline );
  276. }
  277. /* Segment creation in progress.
  278. * The ending coordinate is updated by the function
  279. * ShowNewEdgeModule() called on move mouse event
  280. * during the segment creation
  281. */
  282. else
  283. {
  284. if( type_edge == S_SEGMENT )
  285. {
  286. if( aEdge->m_Start0 != aEdge->m_End0 )
  287. {
  288. aEdge->Draw( m_canvas, DC, GR_OR );
  289. EDGE_MODULE* newedge = new EDGE_MODULE( *aEdge );
  290. // insert _after_ aEdge, which is the same as inserting before aEdge->Next()
  291. module->GraphicalItems().Insert( newedge, aEdge->Next() );
  292. aEdge->ClearFlags();
  293. aEdge = newedge; // point now new item
  294. aEdge->SetFlags( IS_NEW );
  295. aEdge->SetWidth( GetDesignSettings().m_ModuleSegmentWidth );
  296. aEdge->SetStart( GetScreen()->GetCrossHairPosition() );
  297. aEdge->SetEnd( aEdge->GetStart() );
  298. // Update relative coordinate.
  299. aEdge->SetStart0( aEdge->GetStart() - module->GetPosition() );
  300. wxPoint pt( aEdge->GetStart0() );
  301. RotatePoint( &pt, -module->GetOrientation() );
  302. aEdge->SetStart0( pt );
  303. aEdge->SetEnd0( aEdge->GetStart0() );
  304. module->CalculateBoundingBox();
  305. module->SetLastEditTime();
  306. OnModify();
  307. }
  308. }
  309. else
  310. {
  311. wxMessageBox( wxT( "Begin_Edge() error" ) );
  312. }
  313. }
  314. return aEdge;
  315. }
  316. void FOOTPRINT_EDIT_FRAME::End_Edge_Module( EDGE_MODULE* aEdge )
  317. {
  318. MODULE* module = GetBoard()->m_Modules;
  319. if( aEdge )
  320. {
  321. aEdge->ClearFlags();
  322. // If last segment length is 0: remove it
  323. if( aEdge->GetStart() == aEdge->GetEnd() )
  324. aEdge->DeleteStructure();
  325. }
  326. module->CalculateBoundingBox();
  327. module->SetLastEditTime();
  328. OnModify();
  329. m_canvas->SetMouseCapture( NULL, NULL );
  330. }