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.

457 lines
16 KiB

4 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009-2016 Jean-Pierre Charras, jean-pierre.charras at wanadoo.fr
  5. * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <pcb_edit_frame.h>
  25. #include <board.h>
  26. #include <board_design_settings.h>
  27. #include <pcb_track.h>
  28. #include <pcb_group.h>
  29. #include <connectivity/connectivity_data.h>
  30. #include <pcb_layer_box_selector.h>
  31. #include <tool/tool_manager.h>
  32. #include <tools/pcb_selection_tool.h>
  33. #include <tools/global_edit_tool.h>
  34. #include "dialog_global_edit_tracks_and_vias_base.h"
  35. // Columns of netclasses grid
  36. enum {
  37. GRID_NAME = 0,
  38. GRID_TRACKSIZE,
  39. GRID_VIASIZE,
  40. GRID_VIADRILL,
  41. GRID_uVIASIZE,
  42. GRID_uVIADRILL,
  43. GRID_DIFF_PAIR_WIDTH, // not currently included in grid
  44. GRID_DIFF_PAIR_GAP, // not currently included in grid
  45. GRID_DIFF_PAIR_VIA_GAP // not currently included in grid
  46. };
  47. // Globals to remember control settings during a session
  48. static bool g_modifyTracks = true;
  49. static bool g_modifyVias = true;
  50. static bool g_filterByNetclass;
  51. static wxString g_netclassFilter;
  52. static bool g_filterByNet;
  53. static wxString g_netFilter;
  54. static bool g_filterByLayer;
  55. static int g_layerFilter;
  56. static bool g_filterSelected = false;
  57. class DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS : public DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS_BASE
  58. {
  59. private:
  60. PCB_EDIT_FRAME* m_parent;
  61. BOARD* m_brd;
  62. int* m_originalColWidths;
  63. PCB_SELECTION m_selection;
  64. public:
  65. DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS( PCB_EDIT_FRAME* aParent );
  66. ~DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS() override;
  67. protected:
  68. void onSpecifiedValuesUpdateUi( wxUpdateUIEvent& event ) override;
  69. void OnSizeNetclassGrid( wxSizeEvent& event ) override;
  70. void OnNetclassFilterSelect( wxCommandEvent& event ) override
  71. {
  72. m_netclassFilterOpt->SetValue( true );
  73. }
  74. void OnLayerFilterSelect( wxCommandEvent& event ) override
  75. {
  76. m_layerFilterOpt->SetValue( true );
  77. }
  78. void onUnitsChanged( wxCommandEvent& aEvent );
  79. private:
  80. void visitItem( PICKED_ITEMS_LIST* aUndoList, PCB_TRACK* aItem );
  81. void processItem( PICKED_ITEMS_LIST* aUndoList, PCB_TRACK* aItem );
  82. bool TransferDataToWindow() override;
  83. bool TransferDataFromWindow() override;
  84. void AdjustNetclassGridColumns( int aWidth );
  85. void OnNetFilterSelect( wxCommandEvent& event )
  86. {
  87. m_netFilterOpt->SetValue( true );
  88. }
  89. void buildNetclassesGrid();
  90. void buildFilterLists();
  91. };
  92. DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS( PCB_EDIT_FRAME* aParent ) :
  93. DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS_BASE( aParent )
  94. {
  95. m_parent = aParent;
  96. m_brd = m_parent->GetBoard();
  97. m_originalColWidths = new int[ m_netclassGrid->GetNumberCols() ];
  98. for( int i = 0; i < m_netclassGrid->GetNumberCols(); ++i )
  99. m_originalColWidths[ i ] = m_netclassGrid->GetColSize( i );
  100. buildFilterLists();
  101. m_parent->UpdateTrackWidthSelectBox( m_trackWidthSelectBox, false );
  102. m_trackWidthSelectBox->Append( INDETERMINATE_ACTION );
  103. m_parent->UpdateViaSizeSelectBox( m_viaSizesSelectBox, false );
  104. m_viaSizesSelectBox->Append( INDETERMINATE_ACTION );
  105. m_layerBox->SetBoardFrame( m_parent );
  106. m_layerBox->SetLayersHotkeys( false );
  107. m_layerBox->SetNotAllowedLayerSet( LSET::AllNonCuMask() );
  108. m_layerBox->SetUndefinedLayerName( INDETERMINATE_ACTION );
  109. m_layerBox->Resync();
  110. m_netclassGrid->SetDefaultCellFont( KIUI::GetInfoFont( this ) );
  111. buildNetclassesGrid();
  112. m_netclassGrid->SetCellHighlightPenWidth( 0 );
  113. SetupStandardButtons();
  114. m_netFilter->Connect( NET_SELECTED,
  115. wxCommandEventHandler( DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::OnNetFilterSelect ),
  116. nullptr, this );
  117. m_parent->Bind( UNITS_CHANGED, &DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::onUnitsChanged, this );
  118. finishDialogSettings();
  119. }
  120. DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::~DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS()
  121. {
  122. g_modifyTracks = m_tracks->GetValue();
  123. g_modifyVias = m_vias->GetValue();
  124. g_filterByNetclass = m_netclassFilterOpt->GetValue();
  125. g_netclassFilter = m_netclassFilter->GetStringSelection();
  126. g_filterByNet = m_netFilterOpt->GetValue();
  127. g_netFilter = m_netFilter->GetSelectedNetname();
  128. g_filterByLayer = m_layerFilterOpt->GetValue();
  129. g_layerFilter = m_layerFilter->GetLayerSelection();
  130. g_filterSelected = m_selectedItemsFilter->GetValue();
  131. m_netFilter->Disconnect( NET_SELECTED,
  132. wxCommandEventHandler( DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::OnNetFilterSelect ),
  133. nullptr, this );
  134. m_parent->Unbind( UNITS_CHANGED, &DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::onUnitsChanged, this );
  135. delete[] m_originalColWidths;
  136. }
  137. void DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::onUnitsChanged( wxCommandEvent& aEvent )
  138. {
  139. int trackSel = m_trackWidthSelectBox->GetSelection();
  140. int viaSel = m_viaSizesSelectBox->GetSelection();
  141. m_parent->UpdateTrackWidthSelectBox( m_trackWidthSelectBox, false );
  142. m_trackWidthSelectBox->Append( INDETERMINATE_ACTION );
  143. m_parent->UpdateViaSizeSelectBox( m_viaSizesSelectBox, false );
  144. m_viaSizesSelectBox->Append( INDETERMINATE_ACTION );
  145. m_trackWidthSelectBox->SetSelection( trackSel );
  146. m_viaSizesSelectBox->SetSelection( viaSel );
  147. m_netclassGrid->ClearGrid();
  148. buildNetclassesGrid();
  149. aEvent.Skip();
  150. }
  151. void DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::buildFilterLists()
  152. {
  153. // Populate the net filter list with net names
  154. m_netFilter->SetBoard( m_brd );
  155. m_netFilter->SetNetInfo( &m_brd->GetNetInfo() );
  156. if( !m_brd->GetHighLightNetCodes().empty() )
  157. m_netFilter->SetSelectedNetcode( *m_brd->GetHighLightNetCodes().begin() );
  158. // Populate the netclass filter list with netclass names
  159. wxArrayString netclassNames;
  160. NETCLASSES& netclasses = m_brd->GetDesignSettings().GetNetClasses();
  161. netclassNames.push_back( netclasses.GetDefaultPtr()->GetName() );
  162. for( NETCLASSES::const_iterator nc = netclasses.begin(); nc != netclasses.end(); ++nc )
  163. netclassNames.push_back( nc->second->GetName() );
  164. m_netclassFilter->Set( netclassNames );
  165. m_netclassFilter->SetStringSelection( m_brd->GetDesignSettings().GetCurrentNetClassName() );
  166. // Populate the layer filter list
  167. m_layerFilter->SetBoardFrame( m_parent );
  168. m_layerFilter->SetLayersHotkeys( false );
  169. m_layerFilter->SetNotAllowedLayerSet( LSET::AllNonCuMask() );
  170. m_layerFilter->Resync();
  171. m_layerFilter->SetLayerSelection( m_parent->GetActiveLayer() );
  172. }
  173. void DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::buildNetclassesGrid()
  174. {
  175. #define SET_NETCLASS_VALUE( row, col, val ) \
  176. m_netclassGrid->SetCellValue( row, col, StringFromValue( m_parent->GetUserUnits(), val, true ) )
  177. m_netclassGrid->SetCellValue( 0, GRID_TRACKSIZE, _( "Track Width" ) );
  178. m_netclassGrid->SetCellValue( 0, GRID_VIASIZE, _( "Via Size" ) );
  179. m_netclassGrid->SetCellValue( 0, GRID_VIADRILL, _( "Via Drill" ) );
  180. m_netclassGrid->SetCellValue( 0, GRID_uVIASIZE, _( "uVia Size" ) );
  181. m_netclassGrid->SetCellValue( 0, GRID_uVIADRILL, _( "uVia Drill" ) );
  182. NETCLASSES& netclasses = m_brd->GetDesignSettings().GetNetClasses();
  183. NETCLASS* defaultNetclass = m_brd->GetDesignSettings().GetDefault();
  184. m_netclassGrid->AppendRows( netclasses.GetCount() + 1 );
  185. m_netclassGrid->SetCellValue( 1, GRID_NAME, defaultNetclass->GetName() );
  186. SET_NETCLASS_VALUE( 1, GRID_TRACKSIZE, defaultNetclass->GetTrackWidth() );
  187. SET_NETCLASS_VALUE( 1, GRID_VIASIZE, defaultNetclass->GetViaDiameter() );
  188. SET_NETCLASS_VALUE( 1, GRID_VIADRILL, defaultNetclass->GetViaDrill() );
  189. SET_NETCLASS_VALUE( 1, GRID_uVIASIZE, defaultNetclass->GetuViaDiameter() );
  190. SET_NETCLASS_VALUE( 1, GRID_uVIADRILL, defaultNetclass->GetuViaDrill() );
  191. int row = 2;
  192. for( const std::pair<const wxString, std::shared_ptr<NETCLASS>>& netclass : netclasses )
  193. {
  194. m_netclassGrid->SetCellValue( row, GRID_NAME, netclass.first );
  195. SET_NETCLASS_VALUE( row, GRID_TRACKSIZE, netclass.second->GetTrackWidth() );
  196. SET_NETCLASS_VALUE( row, GRID_VIASIZE, netclass.second->GetViaDiameter() );
  197. SET_NETCLASS_VALUE( row, GRID_VIADRILL, netclass.second->GetViaDrill() );
  198. SET_NETCLASS_VALUE( row, GRID_uVIASIZE, netclass.second->GetuViaDiameter() );
  199. SET_NETCLASS_VALUE( row, GRID_uVIADRILL, netclass.second->GetuViaDrill() );
  200. row++;
  201. }
  202. }
  203. bool DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::TransferDataToWindow()
  204. {
  205. PCB_SELECTION_TOOL* selTool = m_parent->GetToolManager()->GetTool<PCB_SELECTION_TOOL>();
  206. m_selection = selTool->GetSelection();
  207. BOARD_CONNECTED_ITEM* item = dynamic_cast<BOARD_CONNECTED_ITEM*>( m_selection.Front() );
  208. m_tracks->SetValue( g_modifyTracks );
  209. m_vias->SetValue( g_modifyVias );
  210. if( g_filterByNetclass && m_netclassFilter->SetStringSelection( g_netclassFilter ) )
  211. m_netclassFilterOpt->SetValue( true );
  212. else if( item )
  213. m_netclassFilter->SetStringSelection( item->GetNet()->GetNetClassName() );
  214. if( g_filterByNet && m_brd->FindNet( g_netFilter ) != nullptr )
  215. {
  216. m_netFilter->SetSelectedNet( g_netFilter );
  217. m_netFilterOpt->SetValue( true );
  218. }
  219. else if( item )
  220. {
  221. m_netFilter->SetSelectedNetcode( item->GetNetCode() );
  222. }
  223. if( g_filterByLayer && m_layerFilter->SetLayerSelection( g_layerFilter ) != wxNOT_FOUND )
  224. m_layerFilterOpt->SetValue( true );
  225. else if( item )
  226. m_layerFilter->SetLayerSelection( item->GetLayer() );
  227. m_trackWidthSelectBox->SetSelection( (int) m_trackWidthSelectBox->GetCount() - 1 );
  228. m_viaSizesSelectBox->SetSelection( (int) m_viaSizesSelectBox->GetCount() - 1 );
  229. m_layerBox->SetStringSelection( INDETERMINATE_ACTION );
  230. m_selectedItemsFilter->SetValue( g_filterSelected );
  231. return true;
  232. }
  233. void DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::onSpecifiedValuesUpdateUi( wxUpdateUIEvent& event )
  234. {
  235. // Enable the items in the use specified values section
  236. event.Enable( m_setToSpecifiedValues->GetValue() );
  237. }
  238. void DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::processItem( PICKED_ITEMS_LIST* aUndoList,
  239. PCB_TRACK* aItem )
  240. {
  241. BOARD_DESIGN_SETTINGS& brdSettings = m_brd->GetDesignSettings();
  242. bool isTrack = aItem->Type() == PCB_TRACE_T;
  243. bool isArc = aItem->Type() == PCB_ARC_T;
  244. bool isVia = aItem->Type() == PCB_VIA_T;
  245. if( m_setToSpecifiedValues->GetValue() )
  246. {
  247. if( ( isArc || isTrack )
  248. && m_trackWidthSelectBox->GetStringSelection() != INDETERMINATE_ACTION )
  249. {
  250. unsigned int prevTrackWidthIndex = brdSettings.GetTrackWidthIndex();
  251. int trackWidthIndex = m_trackWidthSelectBox->GetSelection();
  252. if( trackWidthIndex >= 0 )
  253. brdSettings.SetTrackWidthIndex( static_cast<unsigned>( trackWidthIndex ) );
  254. m_parent->SetTrackSegmentWidth( aItem, aUndoList, false );
  255. brdSettings.SetTrackWidthIndex( prevTrackWidthIndex );
  256. }
  257. else if( isVia && m_viaSizesSelectBox->GetStringSelection() != INDETERMINATE_ACTION )
  258. {
  259. unsigned int prevViaSizeIndex = brdSettings.GetViaSizeIndex();
  260. int viaSizeIndex = m_viaSizesSelectBox->GetSelection();
  261. if( viaSizeIndex >= 0 )
  262. brdSettings.SetViaSizeIndex( static_cast<unsigned>( viaSizeIndex ) );
  263. m_parent->SetTrackSegmentWidth( aItem, aUndoList, false );
  264. brdSettings.SetViaSizeIndex( prevViaSizeIndex );
  265. }
  266. if( ( isArc || isTrack ) && m_layerBox->GetLayerSelection() != UNDEFINED_LAYER )
  267. {
  268. if( aUndoList->FindItem( aItem ) < 0 )
  269. {
  270. ITEM_PICKER picker( nullptr, aItem, UNDO_REDO::CHANGED );
  271. picker.SetLink( aItem->Clone() );
  272. aUndoList->PushItem( picker );
  273. }
  274. aItem->SetLayer( ToLAYER_ID( m_layerBox->GetLayerSelection() ) );
  275. m_parent->GetBoard()->GetConnectivity()->Update( aItem );
  276. }
  277. }
  278. else
  279. {
  280. m_parent->SetTrackSegmentWidth( aItem, aUndoList, true );
  281. }
  282. m_brd->OnItemChanged( aItem );
  283. }
  284. void DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::visitItem( PICKED_ITEMS_LIST* aUndoList, PCB_TRACK* aItem )
  285. {
  286. if( m_selectedItemsFilter->GetValue() )
  287. {
  288. if( !aItem->IsSelected() )
  289. {
  290. PCB_GROUP* group = aItem->GetParentGroup();
  291. while( group && !group->IsSelected() )
  292. group = group->GetParentGroup();
  293. if( !group )
  294. return;
  295. }
  296. }
  297. if( m_netFilterOpt->GetValue() && m_netFilter->GetSelectedNetcode() >= 0 )
  298. {
  299. if( aItem->GetNetCode() != m_netFilter->GetSelectedNetcode() )
  300. return;
  301. }
  302. if( m_netclassFilterOpt->GetValue() && !m_netclassFilter->GetStringSelection().IsEmpty() )
  303. {
  304. if( aItem->GetNetClassName() != m_netclassFilter->GetStringSelection() )
  305. return;
  306. }
  307. if( m_layerFilterOpt->GetValue() && m_layerFilter->GetLayerSelection() != UNDEFINED_LAYER )
  308. {
  309. if( aItem->GetLayer() != m_layerFilter->GetLayerSelection() )
  310. return;
  311. }
  312. processItem( aUndoList, aItem );
  313. }
  314. bool DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::TransferDataFromWindow()
  315. {
  316. PICKED_ITEMS_LIST itemsListPicker;
  317. wxBusyCursor dummy;
  318. // Examine segments
  319. for( PCB_TRACK* track : m_brd->Tracks() )
  320. {
  321. if( m_tracks->GetValue() && track->Type() == PCB_TRACE_T )
  322. visitItem( &itemsListPicker, track );
  323. else if ( m_tracks->GetValue() && track->Type() == PCB_ARC_T )
  324. visitItem( &itemsListPicker, track );
  325. else if ( m_vias->GetValue() && track->Type() == PCB_VIA_T )
  326. visitItem( &itemsListPicker, track );
  327. }
  328. if( itemsListPicker.GetCount() > 0 )
  329. {
  330. m_parent->SaveCopyInUndoList( itemsListPicker, UNDO_REDO::CHANGED );
  331. for( PCB_TRACK* track : m_brd->Tracks() )
  332. m_parent->GetCanvas()->GetView()->Update( track );
  333. }
  334. return true;
  335. }
  336. void DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::AdjustNetclassGridColumns( int aWidth )
  337. {
  338. for( int i = 1; i < m_netclassGrid->GetNumberCols(); i++ )
  339. {
  340. m_netclassGrid->SetColSize( i, m_originalColWidths[ i ] );
  341. aWidth -= m_originalColWidths[ i ];
  342. }
  343. m_netclassGrid->SetColSize( 0, aWidth );
  344. }
  345. void DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::OnSizeNetclassGrid( wxSizeEvent& event )
  346. {
  347. AdjustNetclassGridColumns( event.GetSize().GetX() );
  348. event.Skip();
  349. }
  350. int GLOBAL_EDIT_TOOL::EditTracksAndVias( const TOOL_EVENT& aEvent )
  351. {
  352. PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
  353. DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS dlg( editFrame );
  354. dlg.ShowQuasiModal(); // QuasiModal required for NET_SELECTOR
  355. return 0;
  356. }