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.

428 lines
14 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017-2019 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include "pad_tool.h"
  24. #include <class_draw_panel_gal.h>
  25. #include <view/view_controls.h>
  26. #include <view/view.h>
  27. #include <tool/tool_manager.h>
  28. #include <bitmaps.h>
  29. #include <class_board_item.h>
  30. #include <class_module.h>
  31. #include <board_commit.h>
  32. #include <dialogs/dialog_push_pad_properties.h>
  33. #include <tools/pcb_actions.h>
  34. #include <tools/selection_tool.h>
  35. #include <tools/pcb_selection_conditions.h>
  36. #include <tools/edit_tool.h>
  37. #include <dialogs/dialog_enum_pads.h>
  38. PAD_TOOL::PAD_TOOL() :
  39. PCB_TOOL_BASE( "pcbnew.PadTool" ),
  40. m_padCopied( false )
  41. {}
  42. PAD_TOOL::~PAD_TOOL()
  43. {}
  44. void PAD_TOOL::Reset( RESET_REASON aReason )
  45. {
  46. m_padCopied = false;
  47. }
  48. bool PAD_TOOL::Init()
  49. {
  50. SELECTION_TOOL* selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
  51. if( selTool )
  52. {
  53. // Add context menu entries that are displayed when selection tool is active
  54. CONDITIONAL_MENU& menu = selTool->GetToolMenu().GetMenu();
  55. SELECTION_CONDITION padSel = SELECTION_CONDITIONS::HasType( PCB_PAD_T );
  56. SELECTION_CONDITION singlePadSel = SELECTION_CONDITIONS::Count( 1 ) &&
  57. SELECTION_CONDITIONS::OnlyType( PCB_PAD_T );
  58. menu.AddSeparator( 400 );
  59. if( m_editModules )
  60. {
  61. menu.AddItem( PCB_ACTIONS::createPadFromShapes, SELECTION_CONDITIONS::NotEmpty, 400 );
  62. menu.AddItem( PCB_ACTIONS::explodePadToShapes, singlePadSel, 400 );
  63. }
  64. menu.AddItem( PCB_ACTIONS::copyPadSettings, singlePadSel, 400 );
  65. menu.AddItem( PCB_ACTIONS::applyPadSettings, padSel, 400 );
  66. menu.AddItem( PCB_ACTIONS::pushPadSettings, singlePadSel, 400 );
  67. menu.AddItem( PCB_ACTIONS::enumeratePads, SELECTION_CONDITIONS::ShowAlways, 400 );
  68. }
  69. return true;
  70. }
  71. int PAD_TOOL::pastePadProperties( const TOOL_EVENT& aEvent )
  72. {
  73. auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
  74. const auto& selection = selTool.GetSelection();
  75. const D_PAD& masterPad = frame()->GetDesignSettings().m_Pad_Master;
  76. BOARD_COMMIT commit( frame() );
  77. // for every selected pad, paste global settings
  78. for( auto item : selection )
  79. {
  80. if( item->Type() == PCB_PAD_T )
  81. {
  82. commit.Modify( item );
  83. static_cast<D_PAD&>( *item ).ImportSettingsFrom( masterPad );
  84. }
  85. }
  86. commit.Push( _( "Paste Pad Properties" ) );
  87. m_toolMgr->ProcessEvent( EVENTS::SelectedItemsModified );
  88. frame()->Refresh();
  89. return 0;
  90. }
  91. int PAD_TOOL::copyPadSettings( const TOOL_EVENT& aEvent )
  92. {
  93. auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
  94. const auto& selection = selTool.GetSelection();
  95. D_PAD& masterPad = frame()->GetDesignSettings().m_Pad_Master;
  96. // can only copy from a single pad
  97. if( selection.Size() == 1 )
  98. {
  99. auto item = selection[0];
  100. if( item->Type() == PCB_PAD_T )
  101. {
  102. const auto& selPad = static_cast<const D_PAD&>( *item );
  103. masterPad.ImportSettingsFrom( selPad );
  104. m_padCopied = true;
  105. }
  106. }
  107. return 0;
  108. }
  109. static void doPushPadProperties( BOARD& board, const D_PAD& aSrcPad, BOARD_COMMIT& commit,
  110. bool aSameFootprints,
  111. bool aPadShapeFilter,
  112. bool aPadOrientFilter,
  113. bool aPadLayerFilter,
  114. bool aPadTypeFilter )
  115. {
  116. const MODULE* moduleRef = aSrcPad.GetParent();
  117. double pad_orient = aSrcPad.GetOrientation() - moduleRef->GetOrientation();
  118. for( auto module : board.Modules() )
  119. {
  120. if( !aSameFootprints && ( module != moduleRef ) )
  121. continue;
  122. if( module->GetFPID() != moduleRef->GetFPID() )
  123. continue;
  124. for( auto pad : module->Pads() )
  125. {
  126. if( aPadShapeFilter && ( pad->GetShape() != aSrcPad.GetShape() ) )
  127. continue;
  128. double currpad_orient = pad->GetOrientation() - module->GetOrientation();
  129. if( aPadOrientFilter && ( currpad_orient != pad_orient ) )
  130. continue;
  131. if( aPadLayerFilter && ( pad->GetLayerSet() != aSrcPad.GetLayerSet() ) )
  132. continue;
  133. if( aPadTypeFilter && ( pad->GetAttribute() != aSrcPad.GetAttribute() ) )
  134. continue;
  135. // Special-case for aperture pads
  136. if( aPadTypeFilter && pad->GetAttribute() == PAD_ATTRIB_CONN )
  137. {
  138. if( pad->IsAperturePad() != aSrcPad.IsAperturePad() )
  139. continue;
  140. }
  141. commit.Modify( pad );
  142. // Apply source pad settings to this pad
  143. pad->ImportSettingsFrom( aSrcPad );
  144. }
  145. }
  146. }
  147. int PAD_TOOL::pushPadSettings( const TOOL_EVENT& aEvent )
  148. {
  149. auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
  150. const auto& selection = selTool.GetSelection();
  151. D_PAD* srcPad;
  152. if( selection.Size() == 1 && selection[0]->Type() == PCB_PAD_T )
  153. srcPad = static_cast<D_PAD*>( selection[0] );
  154. else
  155. return 0;
  156. MODULE* module = srcPad->GetParent();
  157. if( !module )
  158. return 0;
  159. frame()->SetMsgPanel( module );
  160. DIALOG_PUSH_PAD_PROPERTIES dlg( frame() );
  161. int dialogRet = dlg.ShowModal();
  162. if( dialogRet == wxID_CANCEL )
  163. return 0;
  164. const bool edit_Same_Modules = (dialogRet == 1);
  165. BOARD_COMMIT commit( frame() );
  166. doPushPadProperties( *getModel<BOARD>(), *srcPad, commit, edit_Same_Modules,
  167. DIALOG_PUSH_PAD_PROPERTIES::m_Pad_Shape_Filter,
  168. DIALOG_PUSH_PAD_PROPERTIES::m_Pad_Orient_Filter,
  169. DIALOG_PUSH_PAD_PROPERTIES::m_Pad_Layer_Filter,
  170. DIALOG_PUSH_PAD_PROPERTIES::m_Pad_Type_Filter );
  171. commit.Push( _( "Push Pad Settings" ) );
  172. m_toolMgr->ProcessEvent( EVENTS::SelectedItemsModified );
  173. frame()->Refresh();
  174. return 0;
  175. }
  176. int PAD_TOOL::EnumeratePads( const TOOL_EVENT& aEvent )
  177. {
  178. if( !board()->GetFirstModule() || board()->GetFirstModule()->Pads().empty() )
  179. return 0;
  180. DIALOG_ENUM_PADS settingsDlg( frame() );
  181. if( settingsDlg.ShowModal() != wxID_OK )
  182. return 0;
  183. std::string tool = aEvent.GetCommandStr().get();
  184. frame()->PushTool( tool );
  185. Activate();
  186. GENERAL_COLLECTOR collector;
  187. const KICAD_T types[] = { PCB_PAD_T, EOT };
  188. GENERAL_COLLECTORS_GUIDE guide = frame()->GetCollectorsGuide();
  189. guide.SetIgnoreMTextsMarkedNoShow( true );
  190. guide.SetIgnoreMTextsOnBack( true );
  191. guide.SetIgnoreMTextsOnFront( true );
  192. guide.SetIgnoreModulesVals( true );
  193. guide.SetIgnoreModulesRefs( true );
  194. int seqPadNum = settingsDlg.GetStartNumber();
  195. wxString padPrefix = settingsDlg.GetPrefix();
  196. std::deque<int> storedPadNumbers;
  197. m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
  198. getViewControls()->ShowCursor( true );
  199. KIGFX::VIEW* view = m_toolMgr->GetView();
  200. VECTOR2I oldCursorPos; // store the previous mouse cursor position, during mouse drag
  201. std::list<D_PAD*> selectedPads;
  202. BOARD_COMMIT commit( frame() );
  203. std::map<wxString, std::pair<int, wxString>> oldNames;
  204. bool isFirstPoint = true; // used to be sure oldCursorPos will be initialized at least once.
  205. STATUS_TEXT_POPUP statusPopup( frame() );
  206. wxString msg = _( "Click on pad %s%d\nPress <esc> to cancel or double-click to commit" );
  207. statusPopup.SetText( wxString::Format( msg, padPrefix, seqPadNum ) );
  208. statusPopup.Popup();
  209. statusPopup.Move( wxGetMousePosition() + wxPoint( 20, 20 ) );
  210. while( TOOL_EVENT* evt = Wait() )
  211. {
  212. frame()->GetCanvas()->SetCurrentCursor( wxCURSOR_BULLSEYE );
  213. if( evt->IsCancelInteractive() )
  214. {
  215. m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
  216. commit.Revert();
  217. frame()->PopTool( tool );
  218. break;
  219. }
  220. else if( evt->IsActivate() )
  221. {
  222. commit.Push( _( "Renumber pads" ) );
  223. frame()->PopTool( tool );
  224. break;
  225. }
  226. else if( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) )
  227. {
  228. selectedPads.clear();
  229. VECTOR2I cursorPos = getViewControls()->GetCursorPosition();
  230. // Be sure the old cursor mouse position was initialized:
  231. if( isFirstPoint )
  232. {
  233. oldCursorPos = cursorPos;
  234. isFirstPoint = false;
  235. }
  236. // wxWidgets deliver mouse move events not frequently enough, resulting in skipping
  237. // pads if the user moves cursor too fast. To solve it, create a line that approximates
  238. // the mouse move and search pads that are on the line.
  239. int distance = ( cursorPos - oldCursorPos ).EuclideanNorm();
  240. // Search will be made every 0.1 mm:
  241. int segments = distance / int( 0.1*IU_PER_MM ) + 1;
  242. const wxPoint line_step( ( cursorPos - oldCursorPos ) / segments );
  243. collector.Empty();
  244. for( int j = 0; j < segments; ++j )
  245. {
  246. wxPoint testpoint( cursorPos.x - j * line_step.x, cursorPos.y - j * line_step.y );
  247. collector.Collect( board(), types, testpoint, guide );
  248. for( int i = 0; i < collector.GetCount(); ++i )
  249. selectedPads.push_back( static_cast<D_PAD*>( collector[i] ) );
  250. }
  251. selectedPads.unique();
  252. for( D_PAD* pad : selectedPads )
  253. {
  254. // If pad was not selected, then enumerate it
  255. if( !pad->IsSelected() )
  256. {
  257. commit.Modify( pad );
  258. // Rename pad and store the old name
  259. int newval;
  260. if( storedPadNumbers.size() > 0 )
  261. {
  262. newval = storedPadNumbers.front();
  263. storedPadNumbers.pop_front();
  264. }
  265. else
  266. newval = seqPadNum++;
  267. wxString newName = wxString::Format( wxT( "%s%d" ), padPrefix, newval );
  268. oldNames[newName] = { newval, pad->GetName() };
  269. pad->SetName( newName );
  270. pad->SetSelected();
  271. getView()->Update( pad );
  272. // Ensure the popup text shows the correct next value
  273. if( storedPadNumbers.size() > 0 )
  274. newval = storedPadNumbers.front();
  275. else
  276. newval = seqPadNum;
  277. statusPopup.SetText( wxString::Format( msg, padPrefix, newval ) );
  278. }
  279. // ... or restore the old name if it was enumerated and clicked again
  280. else if( pad->IsSelected() && evt->IsClick( BUT_LEFT ) )
  281. {
  282. auto it = oldNames.find( pad->GetName() );
  283. wxASSERT( it != oldNames.end() );
  284. if( it != oldNames.end() )
  285. {
  286. storedPadNumbers.push_back( it->second.first );
  287. pad->SetName( it->second.second );
  288. oldNames.erase( it );
  289. int newval = storedPadNumbers.front();
  290. statusPopup.SetText( wxString::Format( msg, padPrefix, newval ) );
  291. }
  292. pad->ClearSelected();
  293. getView()->Update( pad );
  294. }
  295. }
  296. }
  297. else if( ( evt->IsKeyPressed() && evt->KeyCode() == WXK_RETURN ) ||
  298. evt->IsDblClick( BUT_LEFT ) )
  299. {
  300. commit.Push( _( "Renumber pads" ) );
  301. frame()->PopTool( tool );
  302. break;
  303. }
  304. else if( evt->IsClick( BUT_RIGHT ) )
  305. {
  306. m_menu.ShowContextMenu( selection() );
  307. }
  308. else
  309. evt->SetPassEvent();
  310. // Prepare the next loop by updating the old cursor mouse position
  311. // to this last mouse cursor position
  312. oldCursorPos = getViewControls()->GetCursorPosition();
  313. statusPopup.Move( wxGetMousePosition() + wxPoint( 20, 20 ) );
  314. }
  315. for( auto p : board()->GetFirstModule()->Pads() )
  316. {
  317. p->ClearSelected();
  318. view->Update( p );
  319. }
  320. statusPopup.Hide();
  321. return 0;
  322. }
  323. void PAD_TOOL::setTransitions()
  324. {
  325. Go( &PAD_TOOL::pastePadProperties, PCB_ACTIONS::applyPadSettings.MakeEvent() );
  326. Go( &PAD_TOOL::copyPadSettings, PCB_ACTIONS::copyPadSettings.MakeEvent() );
  327. Go( &PAD_TOOL::pushPadSettings, PCB_ACTIONS::pushPadSettings.MakeEvent() );
  328. Go( &PAD_TOOL::EnumeratePads, PCB_ACTIONS::enumeratePads.MakeEvent() );
  329. }