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.

434 lines
12 KiB

16 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
14 years ago
15 years ago
15 years ago
15 years ago
14 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
17 years ago
17 years ago
14 years ago
17 years ago
14 years ago
14 years ago
17 years ago
14 years ago
17 years ago
14 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
17 years ago
17 years ago
14 years ago
14 years ago
17 years ago
14 years ago
17 years ago
14 years ago
17 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) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file edgemod.cpp:
  27. * @brief Functions to edit graphic items used to draw footprint edges.
  28. *
  29. * @todo - Arc functions not compete but menus are ready to use.
  30. */
  31. #include "fctsys.h"
  32. #include "trigo.h"
  33. #include "gr_basic.h"
  34. #include "class_drawpanel.h"
  35. #include "confirm.h"
  36. #include "wxPcbStruct.h"
  37. #include "module_editor_frame.h"
  38. #include "class_board.h"
  39. #include "class_module.h"
  40. #include "class_edge_mod.h"
  41. #include "pcbnew.h"
  42. static void ShowNewEdgeModule( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
  43. bool erase );
  44. static void Abort_Move_ModuleOutline( EDA_DRAW_PANEL* Panel, wxDC* DC );
  45. static void ShowCurrentOutlineWhileMoving( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  46. const wxPoint& aPosition, bool aErase );
  47. static double ArcValue = 900;
  48. static wxPoint MoveVector; // Move vector for move edge
  49. static wxPoint CursorInitialPosition; // Mouse cursor initial position for move command
  50. void FOOTPRINT_EDIT_FRAME::Start_Move_EdgeMod( EDGE_MODULE* Edge, wxDC* DC )
  51. {
  52. if( Edge == NULL )
  53. return;
  54. Edge->Draw( m_canvas, DC, GR_XOR );
  55. Edge->SetFlags( IS_MOVED );
  56. MoveVector.x = MoveVector.y = 0;
  57. CursorInitialPosition = GetScreen()->GetCrossHairPosition();
  58. m_canvas->SetMouseCapture( ShowCurrentOutlineWhileMoving, Abort_Move_ModuleOutline );
  59. SetCurItem( Edge );
  60. m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
  61. }
  62. void FOOTPRINT_EDIT_FRAME::Place_EdgeMod( EDGE_MODULE* aEdge )
  63. {
  64. if( aEdge == NULL )
  65. return;
  66. aEdge->SetStart( aEdge->GetStart() - MoveVector );
  67. aEdge->SetEnd( aEdge->GetEnd() - MoveVector );
  68. aEdge->SetStart0( aEdge->GetStart0() - MoveVector );
  69. aEdge->SetEnd0( aEdge->GetEnd0() - 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 = -(screen->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( screen->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_MODEDIT );
  125. if( aEdge == NULL )
  126. {
  127. aEdge = (EDGE_MODULE*) (BOARD_ITEM*) Module->m_Drawings;
  128. for( ; aEdge != NULL; aEdge = aEdge->Next() )
  129. {
  130. if( aEdge->Type() != PCB_MODULE_EDGE_T )
  131. continue;
  132. aEdge->SetWidth( g_ModuleSegmentWidth );
  133. }
  134. }
  135. else
  136. {
  137. aEdge->SetWidth( g_ModuleSegmentWidth );
  138. }
  139. OnModify();
  140. Module->CalculateBoundingBox();
  141. Module->m_LastEdit_Time = time( NULL );
  142. }
  143. void FOOTPRINT_EDIT_FRAME::Edit_Edge_Layer( EDGE_MODULE* Edge )
  144. {
  145. MODULE* Module = GetBoard()->m_Modules;
  146. int new_layer = SILKSCREEN_N_FRONT;
  147. if( Edge != NULL )
  148. new_layer = Edge->GetLayer();
  149. /* Ask for the new layer */
  150. new_layer = SelectLayer( new_layer, FIRST_COPPER_LAYER, LAST_NO_COPPER_LAYER );
  151. if( new_layer < 0 )
  152. return;
  153. if( IsValidCopperLayerIndex( new_layer ) )
  154. {
  155. /* an edge is put on a copper layer, and it is very dangerous. a
  156. *confirmation is requested */
  157. if( !IsOK( this,
  158. _( "The graphic item will be on a copper layer. It is very dangerous. Are you sure?" ) ) )
  159. return;
  160. }
  161. SaveCopyInUndoList( Module, UR_MODEDIT );
  162. if( Edge == NULL )
  163. {
  164. Edge = (EDGE_MODULE*) (BOARD_ITEM*) Module->m_Drawings;
  165. for( ; Edge != NULL; Edge = Edge->Next() )
  166. {
  167. if( Edge->Type() != PCB_MODULE_EDGE_T )
  168. continue;
  169. Edge->SetLayer( new_layer );
  170. }
  171. }
  172. else
  173. {
  174. Edge->SetLayer( new_layer );
  175. }
  176. OnModify();
  177. Module->CalculateBoundingBox();
  178. Module->m_LastEdit_Time = time( NULL );
  179. }
  180. void FOOTPRINT_EDIT_FRAME::Enter_Edge_Width( EDGE_MODULE* aEdge )
  181. {
  182. wxString buffer;
  183. buffer = ReturnStringFromValue( g_UserUnit, g_ModuleSegmentWidth,
  184. GetScreen()->GetInternalUnits() );
  185. wxTextEntryDialog dlg( this, _( "New Width:" ), _( "Edge Width" ), buffer );
  186. if( dlg.ShowModal() != wxID_OK )
  187. return; // canceled by user
  188. buffer = dlg.GetValue( );
  189. g_ModuleSegmentWidth = ReturnValueFromString( g_UserUnit, buffer,
  190. GetScreen()->GetInternalUnits() );
  191. if( aEdge )
  192. {
  193. MODULE* module = GetBoard()->m_Modules;
  194. aEdge->SetWidth( g_ModuleSegmentWidth );
  195. module->CalculateBoundingBox();
  196. OnModify();
  197. }
  198. }
  199. void FOOTPRINT_EDIT_FRAME::Delete_Edge_Module( EDGE_MODULE* Edge )
  200. {
  201. if( Edge == NULL )
  202. return;
  203. if( Edge->Type() != PCB_MODULE_EDGE_T )
  204. {
  205. DisplayError( this, wxT( "StructType error: PCB_MODULE_EDGE_T expected" ) );
  206. return;
  207. }
  208. MODULE* Module = (MODULE*) Edge->GetParent();
  209. /* Delete segment. */
  210. Edge->DeleteStructure();
  211. Module->m_LastEdit_Time = time( NULL );
  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* Edge,
  240. wxDC* DC,
  241. int type_edge )
  242. {
  243. MODULE* module = GetBoard()->m_Modules;
  244. int angle = 0;
  245. if( module == NULL )
  246. return NULL;
  247. if( Edge == NULL ) /* Start a new edge item */
  248. {
  249. SaveCopyInUndoList( module, UR_MODEDIT );
  250. Edge = new EDGE_MODULE( module );
  251. MoveVector.x = MoveVector.y = 0;
  252. // Add the new item to the Drawings list head
  253. module->m_Drawings.PushFront( Edge );
  254. // Update characteristics of the segment or arc.
  255. Edge->SetFlags( IS_NEW );
  256. Edge->SetAngle( angle );
  257. Edge->SetShape( type_edge );
  258. if( Edge->GetShape() == S_ARC )
  259. Edge->SetAngle( ArcValue );
  260. Edge->SetWidth( g_ModuleSegmentWidth );
  261. Edge->SetLayer( module->GetLayer() );
  262. if( module->GetLayer() == LAYER_N_FRONT )
  263. Edge->SetLayer( SILKSCREEN_N_FRONT );
  264. if( module->GetLayer() == LAYER_N_BACK )
  265. Edge->SetLayer( SILKSCREEN_N_BACK );
  266. // Initialize the starting point of the new segment or arc
  267. Edge->SetStart( GetScreen()->GetCrossHairPosition() );
  268. // Initialize the ending point of the new segment or arc
  269. Edge->SetEnd( Edge->GetStart() );
  270. // Initialize the relative coordinates
  271. Edge->SetStart0( Edge->GetStart() - module->GetPosition() );
  272. RotatePoint( &Edge->m_Start0, -module->m_Orient );
  273. Edge->m_End0 = Edge->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( Edge->m_Start0 != Edge->m_End0 )
  287. {
  288. Edge->Draw( m_canvas, DC, GR_OR );
  289. EDGE_MODULE* newedge = new EDGE_MODULE( module );
  290. newedge->Copy( Edge );
  291. // insert _after_ Edge, which is the same as inserting before Edge->Next()
  292. module->m_Drawings.Insert( newedge, Edge->Next() );
  293. Edge->ClearFlags();
  294. Edge = newedge; // point now new item
  295. Edge->SetFlags( IS_NEW );
  296. Edge->SetWidth( g_ModuleSegmentWidth );
  297. Edge->SetStart( GetScreen()->GetCrossHairPosition() );
  298. Edge->SetEnd( Edge->GetStart() );
  299. // Update relative coordinate.
  300. Edge->SetStart0( Edge->GetStart() - module->GetPosition() );
  301. wxPoint pt( Edge->GetStart0() );
  302. RotatePoint( &pt, -module->GetOrientation() );
  303. Edge->SetStart0( pt );
  304. Edge->SetEnd0( Edge->GetStart0() );
  305. module->CalculateBoundingBox();
  306. module->m_LastEdit_Time = time( NULL );
  307. OnModify();
  308. }
  309. }
  310. else
  311. {
  312. wxMessageBox( wxT( "Begin_Edge() error" ) );
  313. }
  314. }
  315. return Edge;
  316. }
  317. void FOOTPRINT_EDIT_FRAME::End_Edge_Module( EDGE_MODULE* Edge )
  318. {
  319. MODULE* Module = GetBoard()->m_Modules;
  320. if( Edge )
  321. {
  322. Edge->ClearFlags();
  323. /* If last segment length is 0: remove it */
  324. if( Edge->GetStart() == Edge->GetEnd() )
  325. Edge->DeleteStructure();
  326. }
  327. Module->CalculateBoundingBox();
  328. Module->m_LastEdit_Time = time( NULL );
  329. OnModify();
  330. m_canvas->SetMouseCapture( NULL, NULL );
  331. }