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.

881 lines
28 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
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
14 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) 2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 1992-2022 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. #include <wx/radiobut.h>
  26. #include <kiface_base.h>
  27. #include <confirm.h>
  28. #include <pcb_edit_frame.h>
  29. #include <pcbnew_settings.h>
  30. #include <zones.h>
  31. #include <widgets/unit_binder.h>
  32. #include <zone.h>
  33. #include <pad.h>
  34. #include <board.h>
  35. #include <trigo.h>
  36. #include <eda_pattern_match.h>
  37. #include <dialog_copper_zones_base.h>
  38. #include <string_utils.h>
  39. class DIALOG_COPPER_ZONE : public DIALOG_COPPER_ZONE_BASE
  40. {
  41. public:
  42. DIALOG_COPPER_ZONE( PCB_BASE_FRAME* aParent, ZONE_SETTINGS* aSettings,
  43. CONVERT_SETTINGS* aConvertSettings );
  44. private:
  45. using NET_FILTER = std::unique_ptr<EDA_PATTERN_MATCH>;
  46. using NET_FILTER_LIST = std::vector<NET_FILTER>;
  47. static constexpr int INVALID_NET_CODE{ 0 };
  48. static constexpr int DEFAULT_SORT_CONFIG{ -1 };
  49. static constexpr int NO_PERSISTENT_SORT_MODE{ 0 };
  50. static constexpr int HIDE_ANONYMOUS_NETS{ 1 << 0 };
  51. static constexpr int SORT_BY_PAD_COUNT{ 1 << 1 };
  52. bool TransferDataToWindow() override;
  53. bool TransferDataFromWindow() override;
  54. /**
  55. * @param aUseExportableSetupOnly is true to use exportable parameters only (used to
  56. * export this setup to other zones).
  57. * @return bool - false if incorrect options, true if ok.
  58. */
  59. bool AcceptOptions( bool aUseExportableSetupOnly = false );
  60. void OnStyleSelection( wxCommandEvent& event ) override;
  61. void OnLayerSelection( wxDataViewEvent& event ) override;
  62. void OnNetSortingOptionSelected( wxCommandEvent& event ) override;
  63. void ExportSetupToOtherCopperZones( wxCommandEvent& event ) override;
  64. void OnShowNetNameFilterChange( wxCommandEvent& event ) override;
  65. void OnUpdateUI( wxUpdateUIEvent& ) override;
  66. void OnButtonCancelClick( wxCommandEvent& event ) override;
  67. void OnClose( wxCloseEvent& event ) override;
  68. void OnNetSelectionUpdated( wxCommandEvent& event ) override;
  69. void OnRemoveIslandsSelection( wxCommandEvent& event ) override;
  70. void readNetInformation();
  71. void readFilteringAndSortingCriteria();
  72. wxArrayString buildListOfNetsToDisplay();
  73. void sortNetsByPadCount( std::vector<NETINFO_ITEM*>& nets, const int maxNetCode );
  74. void updateDisplayedListOfNets();
  75. int ensureSelectedNetIsVisible( int selectedNetCode, wxArrayString& netsList );
  76. void displayNetsList( const wxArrayString& netNamesList, int selectIndex );
  77. void updateShowNetsFilter();
  78. wxString getUnescapedNetName( const NETINFO_ITEM* net );
  79. void sortNetsIfRequired();
  80. void updateCurrentNetSelection();
  81. void updateInfoBar();
  82. void storePersistentNetSortConfigurations();
  83. void loadPersistentNetSortConfigurations();
  84. private:
  85. PCB_BASE_FRAME* m_Parent;
  86. bool m_settingsExported; // settings will be written to all other zones
  87. ZONE_SETTINGS m_settings;
  88. ZONE_SETTINGS* m_ptr;
  89. bool m_netSortingByPadCount;
  90. NET_FILTER_LIST m_showNetsFilter;
  91. int m_cornerSmoothingType;
  92. int m_maxNetCode;
  93. int m_currentlySelectedNetcode;
  94. UNIT_BINDER m_outlineHatchPitch;
  95. UNIT_BINDER m_cornerRadius;
  96. UNIT_BINDER m_clearance;
  97. UNIT_BINDER m_minWidth;
  98. UNIT_BINDER m_antipadClearance;
  99. UNIT_BINDER m_spokeWidth;
  100. UNIT_BINDER m_gridStyleRotation;
  101. UNIT_BINDER m_gridStyleThickness;
  102. UNIT_BINDER m_gridStyleGap;
  103. UNIT_BINDER m_islandThreshold;
  104. bool m_hideAutoGeneratedNets;
  105. bool m_isTeardrop;
  106. std::map<wxString, int> m_netNameToNetCode;
  107. std::vector<NETINFO_ITEM*> m_netInfoItemList;
  108. CONVERT_SETTINGS* m_convertSettings;
  109. wxRadioButton* m_rbCenterline;
  110. wxRadioButton* m_rbEnvelope;
  111. wxCheckBox* m_cbDeleteOriginals;
  112. };
  113. int InvokeCopperZonesEditor( PCB_BASE_FRAME* aCaller, ZONE_SETTINGS* aSettings,
  114. CONVERT_SETTINGS* aConvertSettings )
  115. {
  116. DIALOG_COPPER_ZONE dlg( aCaller, aSettings, aConvertSettings );
  117. return dlg.ShowQuasiModal();
  118. }
  119. // The pad count for each netcode, stored in a buffer for a fast access.
  120. // This is needed by the sort function sortNetsByNodes()
  121. static std::vector<int> padCountListByNet;
  122. // Sort nets by decreasing pad count.
  123. // For same pad count, sort by alphabetic names
  124. static bool sortNetsByNodes( const NETINFO_ITEM* a, const NETINFO_ITEM* b )
  125. {
  126. int countA = padCountListByNet[a->GetNetCode()];
  127. int countB = padCountListByNet[b->GetNetCode()];
  128. if( countA == countB )
  129. return a->GetNetname() < b->GetNetname();
  130. else
  131. return countB < countA;
  132. }
  133. // Sort nets by alphabetic names
  134. static bool sortNetsByNames( const NETINFO_ITEM* a, const NETINFO_ITEM* b )
  135. {
  136. return a->GetNetname() < b->GetNetname();
  137. }
  138. DIALOG_COPPER_ZONE::DIALOG_COPPER_ZONE( PCB_BASE_FRAME* aParent, ZONE_SETTINGS* aSettings,
  139. CONVERT_SETTINGS* aConvertSettings ) :
  140. DIALOG_COPPER_ZONE_BASE( aParent ),
  141. m_cornerSmoothingType( ZONE_SETTINGS::SMOOTHING_UNDEFINED ),
  142. m_outlineHatchPitch( aParent, m_stBorderHatchPitchText,
  143. m_outlineHatchPitchCtrl, m_outlineHatchUnits ),
  144. m_cornerRadius( aParent, m_cornerRadiusLabel, m_cornerRadiusCtrl, m_cornerRadiusUnits ),
  145. m_clearance( aParent, m_clearanceLabel, m_clearanceCtrl, m_clearanceUnits ),
  146. m_minWidth( aParent, m_minWidthLabel, m_minWidthCtrl, m_minWidthUnits ),
  147. m_antipadClearance( aParent, m_antipadLabel, m_antipadCtrl, m_antipadUnits ),
  148. m_spokeWidth( aParent, m_spokeWidthLabel, m_spokeWidthCtrl, m_spokeWidthUnits ),
  149. m_gridStyleRotation( aParent, m_staticTextGrindOrient, m_tcGridStyleOrientation,
  150. m_staticTextRotUnits ),
  151. m_gridStyleThickness( aParent, m_staticTextStyleThickness, m_tcGridStyleThickness,
  152. m_GridStyleThicknessUnits ),
  153. m_gridStyleGap( aParent, m_staticTextGridGap, m_tcGridStyleGap, m_GridStyleGapUnits ),
  154. m_islandThreshold( aParent, m_islandThresholdLabel, m_tcIslandThreshold,
  155. m_islandThresholdUnits ),
  156. m_hideAutoGeneratedNets{ false },
  157. m_convertSettings( aConvertSettings ),
  158. m_rbCenterline( nullptr ),
  159. m_rbEnvelope( nullptr ),
  160. m_cbDeleteOriginals( nullptr )
  161. {
  162. m_Parent = aParent;
  163. m_ptr = aSettings;
  164. m_settings = *aSettings;
  165. m_settings.SetupLayersList( m_layers, m_Parent,
  166. LSET::AllCuMask( aParent->GetBoard()->GetCopperLayerCount() ),
  167. false );
  168. m_isTeardrop = m_settings.m_TeardropType != TEARDROP_TYPE::TD_NONE;
  169. switch( m_settings.m_TeardropType )
  170. {
  171. case TEARDROP_TYPE::TD_NONE:
  172. // standard copper zone
  173. break;
  174. case TEARDROP_TYPE::TD_VIAPAD:
  175. SetTitle( _( "Teardrop on Vias/Pads Properties" ) );
  176. break;
  177. case TEARDROP_TYPE::TD_TRACKEND:
  178. SetTitle( _( "Teardrop on Tracks Properties" ) );
  179. break;
  180. default:
  181. SetTitle( _( "Teardrop Properties" ) );
  182. break;
  183. }
  184. if( aConvertSettings )
  185. {
  186. wxStaticBox* bConvertBox = new wxStaticBox( this, wxID_ANY, _( "Conversion Settings" ) );
  187. wxStaticBoxSizer* bConvertSizer = new wxStaticBoxSizer( bConvertBox, wxVERTICAL );
  188. m_rbCenterline = new wxRadioButton( this, wxID_ANY, _( "Use centerlines" ) );
  189. bConvertSizer->Add( m_rbCenterline, 0, wxLEFT|wxRIGHT, 5 );
  190. bConvertSizer->AddSpacer( 2 );
  191. m_rbEnvelope = new wxRadioButton( this, wxID_ANY, _( "Create bounding hull" ) );
  192. bConvertSizer->Add( m_rbEnvelope, 0, wxLEFT|wxRIGHT, 5 );
  193. bConvertSizer->AddSpacer( 6 );
  194. m_cbDeleteOriginals = new wxCheckBox( this, wxID_ANY, _( "Delete source objects after conversion" ) );
  195. bConvertSizer->Add( m_cbDeleteOriginals, 0, wxALL, 5 );
  196. GetSizer()->Insert( 0, bConvertSizer, 0, wxALL|wxEXPAND, 10 );
  197. wxStaticLine* line = new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
  198. wxLI_HORIZONTAL );
  199. GetSizer()->Insert( 1, line, 0, wxLEFT|wxRIGHT|wxEXPAND, 10 );
  200. SetTitle( _( "Convert to Copper Zone" ) );
  201. }
  202. m_settingsExported = false;
  203. m_currentlySelectedNetcode = INVALID_NET_CODE;
  204. m_maxNetCode = INVALID_NET_CODE;
  205. m_netSortingByPadCount = true; // false = alphabetic sort, true = pad count sort
  206. m_ShowNetNameFilter->SetHint( _( "Filter" ) );
  207. m_cbRemoveIslands->Bind( wxEVT_CHOICE,
  208. [&]( wxCommandEvent& )
  209. {
  210. // Area mode is index 2
  211. m_islandThreshold.Enable( m_cbRemoveIslands->GetSelection() == 2 );
  212. } );
  213. SetupStandardButtons();
  214. finishDialogSettings();
  215. }
  216. bool DIALOG_COPPER_ZONE::TransferDataToWindow()
  217. {
  218. if( m_convertSettings )
  219. {
  220. if( m_convertSettings->m_Strategy == BOUNDING_HULL )
  221. m_rbEnvelope->SetValue( true );
  222. else
  223. m_rbCenterline->SetValue( true );
  224. m_cbDeleteOriginals->SetValue( m_convertSettings->m_DeleteOriginals );
  225. }
  226. m_cbLocked->SetValue( m_settings.m_Locked );
  227. m_cornerSmoothingChoice->SetSelection( m_settings.GetCornerSmoothingType() );
  228. m_cornerRadius.SetValue( m_settings.GetCornerRadius() );
  229. m_PriorityLevelCtrl->SetValue( m_settings.m_ZonePriority );
  230. if( m_isTeardrop ) // outlines are never smoothed: they have already the right shape
  231. {
  232. m_cornerSmoothingChoice->SetSelection( 0 );
  233. m_cornerSmoothingChoice->Enable( false );
  234. m_cornerRadius.SetValue( 0 );
  235. m_cornerRadius.Enable( false );
  236. }
  237. switch( m_settings.m_ZoneBorderDisplayStyle )
  238. {
  239. case ZONE_BORDER_DISPLAY_STYLE::NO_HATCH: m_OutlineDisplayCtrl->SetSelection( 0 ); break;
  240. case ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_EDGE: m_OutlineDisplayCtrl->SetSelection( 1 ); break;
  241. case ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_FULL: m_OutlineDisplayCtrl->SetSelection( 2 ); break;
  242. }
  243. m_outlineHatchPitch.SetValue( m_settings.m_BorderHatchPitch );
  244. m_clearance.SetValue( m_settings.m_ZoneClearance );
  245. m_minWidth.SetValue( m_settings.m_ZoneMinThickness );
  246. switch( m_settings.GetPadConnection() )
  247. {
  248. default:
  249. case ZONE_CONNECTION::THERMAL: m_PadInZoneOpt->SetSelection( 1 ); break;
  250. case ZONE_CONNECTION::THT_THERMAL: m_PadInZoneOpt->SetSelection( 2 ); break;
  251. case ZONE_CONNECTION::NONE: m_PadInZoneOpt->SetSelection( 3 ); break;
  252. case ZONE_CONNECTION::FULL: m_PadInZoneOpt->SetSelection( 0 ); break;
  253. }
  254. if( m_isTeardrop )
  255. {
  256. m_PadInZoneOpt->SetSelection( 0 );
  257. m_PadInZoneOpt->Enable( false );
  258. }
  259. // Do not enable/disable antipad clearance and spoke width. They might be needed if
  260. // a footprint or pad overrides the zone to specify a thermal connection.
  261. m_antipadClearance.SetValue( m_settings.m_ThermalReliefGap );
  262. m_spokeWidth.SetValue( m_settings.m_ThermalReliefSpokeWidth );
  263. m_islandThreshold.SetDataType( EDA_DATA_TYPE::AREA );
  264. m_islandThreshold.SetDoubleValue( static_cast<double>( m_settings.GetMinIslandArea() ) );
  265. m_cbRemoveIslands->SetSelection( static_cast<int>( m_settings.GetIslandRemovalMode() ) );
  266. m_islandThreshold.Enable( m_settings.GetIslandRemovalMode() == ISLAND_REMOVAL_MODE::AREA );
  267. loadPersistentNetSortConfigurations();
  268. m_sortByPadsOpt->SetValue( m_netSortingByPadCount );
  269. m_hideAutoGenNetNamesOpt->SetValue( m_hideAutoGeneratedNets );
  270. m_currentlySelectedNetcode = m_settings.m_NetcodeSelection;
  271. // Initialize information required to display nets list
  272. readNetInformation();
  273. if( !m_isTeardrop && m_settings.m_FillMode == ZONE_FILL_MODE::HATCH_PATTERN )
  274. m_GridStyleCtrl->SetSelection( 1 );
  275. else
  276. m_GridStyleCtrl->SetSelection( 0 );
  277. m_GridStyleCtrl->Enable( !m_isTeardrop );
  278. m_gridStyleRotation.SetUnits( EDA_UNITS::DEGREES );
  279. m_gridStyleRotation.SetAngleValue( m_settings.m_HatchOrientation );
  280. m_gridStyleThickness.SetValue( m_settings.m_HatchThickness );
  281. m_gridStyleGap.SetValue( m_settings.m_HatchGap );
  282. m_spinCtrlSmoothLevel->SetValue( m_settings.m_HatchSmoothingLevel );
  283. m_spinCtrlSmoothValue->SetValue( m_settings.m_HatchSmoothingValue );
  284. m_tcZoneName->SetValue( m_settings.m_Name );
  285. SetInitialFocus( m_ShowNetNameFilter );
  286. // Enable/Disable some widgets
  287. wxCommandEvent event;
  288. OnStyleSelection( event );
  289. OnNetSelectionUpdated( event );
  290. Fit();
  291. return true;
  292. }
  293. void DIALOG_COPPER_ZONE::readNetInformation()
  294. {
  295. const NETINFO_LIST& netInfoList = m_Parent->GetBoard()->GetNetInfo();
  296. m_netInfoItemList.clear();
  297. m_netInfoItemList.reserve( netInfoList.GetNetCount() );
  298. m_netNameToNetCode.clear();
  299. m_netNameToNetCode[ _( "<no net>" ) ] = INVALID_NET_CODE;
  300. m_maxNetCode = INVALID_NET_CODE;
  301. for( NETINFO_ITEM* net : netInfoList )
  302. {
  303. const int netCode = net->GetNetCode();
  304. const wxString& netName = getUnescapedNetName( net );
  305. m_netNameToNetCode[netName] = netCode;
  306. if( netCode > INVALID_NET_CODE && net->IsCurrent() )
  307. {
  308. m_netInfoItemList.push_back( net );
  309. m_maxNetCode = std::max( netCode, m_maxNetCode );
  310. }
  311. }
  312. updateDisplayedListOfNets();
  313. }
  314. void DIALOG_COPPER_ZONE::OnUpdateUI( wxUpdateUIEvent& )
  315. {
  316. if( m_cornerSmoothingType != m_cornerSmoothingChoice->GetSelection() )
  317. {
  318. m_cornerSmoothingType = m_cornerSmoothingChoice->GetSelection();
  319. if( m_cornerSmoothingChoice->GetSelection() == ZONE_SETTINGS::SMOOTHING_CHAMFER )
  320. m_cornerRadiusLabel->SetLabel( _( "Chamfer distance:" ) );
  321. else
  322. m_cornerRadiusLabel->SetLabel( _( "Fillet radius:" ) );
  323. }
  324. m_cornerRadiusCtrl->Enable(m_cornerSmoothingType > ZONE_SETTINGS::SMOOTHING_NONE );
  325. }
  326. void DIALOG_COPPER_ZONE::OnButtonCancelClick( wxCommandEvent& event )
  327. {
  328. // After an "Export Settings to Other Zones" cancel and close must return
  329. // ZONE_EXPORT_VALUES instead of wxID_CANCEL.
  330. Close( true );
  331. }
  332. void DIALOG_COPPER_ZONE::OnNetSelectionUpdated( wxCommandEvent& event )
  333. {
  334. updateCurrentNetSelection();
  335. updateInfoBar();
  336. // When info bar is updated, the nets-list shrinks.
  337. // Therefore, we need to reestablish the list and maintain the
  338. // correct selection
  339. updateDisplayedListOfNets();
  340. // Zones with no net never have islands removed
  341. if( m_currentlySelectedNetcode == INVALID_NET_CODE )
  342. {
  343. if( m_cbRemoveIslands->IsEnabled() )
  344. m_settings.SetIslandRemovalMode( (ISLAND_REMOVAL_MODE) m_cbRemoveIslands->GetSelection() );
  345. m_cbRemoveIslands->SetSelection( 1 );
  346. m_staticText40->Enable( false );
  347. m_cbRemoveIslands->Enable( false );
  348. }
  349. else if( !m_cbRemoveIslands->IsEnabled() )
  350. {
  351. m_cbRemoveIslands->SetSelection( static_cast<int>( m_settings.GetIslandRemovalMode() ) );
  352. m_staticText40->Enable( true );
  353. m_cbRemoveIslands->Enable( true );
  354. }
  355. }
  356. void DIALOG_COPPER_ZONE::OnRemoveIslandsSelection( wxCommandEvent& event )
  357. {
  358. m_islandThreshold.Enable( m_cbRemoveIslands->GetSelection() == 2 );
  359. }
  360. bool DIALOG_COPPER_ZONE::TransferDataFromWindow()
  361. {
  362. if( m_GridStyleCtrl->GetSelection() > 0 )
  363. m_settings.m_FillMode = ZONE_FILL_MODE::HATCH_PATTERN;
  364. else
  365. m_settings.m_FillMode = ZONE_FILL_MODE::POLYGONS;
  366. if( !AcceptOptions() )
  367. return false;
  368. if( m_convertSettings )
  369. {
  370. if( m_rbEnvelope->GetValue() )
  371. m_convertSettings->m_Strategy = BOUNDING_HULL;
  372. else
  373. m_convertSettings->m_Strategy = CENTERLINE;
  374. m_convertSettings->m_DeleteOriginals = m_cbDeleteOriginals->GetValue();
  375. }
  376. m_settings.m_HatchOrientation = m_gridStyleRotation.GetAngleValue();
  377. m_settings.m_HatchThickness = m_gridStyleThickness.GetIntValue();
  378. m_settings.m_HatchGap = m_gridStyleGap.GetIntValue();
  379. m_settings.m_HatchSmoothingLevel = m_spinCtrlSmoothLevel->GetValue();
  380. m_settings.m_HatchSmoothingValue = m_spinCtrlSmoothValue->GetValue();
  381. *m_ptr = m_settings;
  382. return true;
  383. }
  384. void DIALOG_COPPER_ZONE::OnClose( wxCloseEvent& event )
  385. {
  386. SetReturnCode( m_settingsExported ? ZONE_EXPORT_VALUES : wxID_CANCEL );
  387. event.Skip();
  388. }
  389. bool DIALOG_COPPER_ZONE::AcceptOptions( bool aUseExportableSetupOnly )
  390. {
  391. if( !m_clearance.Validate( 0, pcbIUScale.mmToIU( ZONE_CLEARANCE_MAX_VALUE_MM ) ) )
  392. return false;
  393. if( !m_minWidth.Validate( pcbIUScale.mmToIU( ZONE_THICKNESS_MIN_VALUE_MM ), INT_MAX ) )
  394. return false;
  395. if( !m_cornerRadius.Validate( 0, INT_MAX ) )
  396. return false;
  397. if( !m_spokeWidth.Validate( 0, INT_MAX ) )
  398. return false;
  399. m_gridStyleRotation.SetValue( NormalizeAngle180( m_gridStyleRotation.GetValue() ) );
  400. if( m_settings.m_FillMode == ZONE_FILL_MODE::HATCH_PATTERN )
  401. {
  402. int minThickness = m_minWidth.GetIntValue();
  403. if( !m_gridStyleThickness.Validate( minThickness, INT_MAX ) )
  404. return false;
  405. if( !m_gridStyleGap.Validate( minThickness, INT_MAX ) )
  406. return false;
  407. }
  408. switch( m_PadInZoneOpt->GetSelection() )
  409. {
  410. case 3: m_settings.SetPadConnection( ZONE_CONNECTION::NONE ); break;
  411. case 2: m_settings.SetPadConnection( ZONE_CONNECTION::THT_THERMAL ); break;
  412. case 1: m_settings.SetPadConnection( ZONE_CONNECTION::THERMAL ); break;
  413. case 0: m_settings.SetPadConnection( ZONE_CONNECTION::FULL ); break;
  414. }
  415. switch( m_OutlineDisplayCtrl->GetSelection() )
  416. {
  417. case 0: m_settings.m_ZoneBorderDisplayStyle = ZONE_BORDER_DISPLAY_STYLE::NO_HATCH; break;
  418. case 1: m_settings.m_ZoneBorderDisplayStyle = ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_EDGE; break;
  419. case 2: m_settings.m_ZoneBorderDisplayStyle = ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_FULL; break;
  420. }
  421. if( !m_outlineHatchPitch.Validate( pcbIUScale.mmToIU( ZONE_BORDER_HATCH_MINDIST_MM ),
  422. pcbIUScale.mmToIU( ZONE_BORDER_HATCH_MAXDIST_MM ) ) )
  423. return false;
  424. m_settings.m_BorderHatchPitch = m_outlineHatchPitch.GetIntValue();
  425. m_settings.m_ZoneClearance = m_clearance.GetIntValue();
  426. m_settings.m_ZoneMinThickness = m_minWidth.GetIntValue();
  427. m_settings.SetCornerSmoothingType( m_cornerSmoothingChoice->GetSelection() );
  428. if( m_settings.GetCornerSmoothingType() == ZONE_SETTINGS::SMOOTHING_NONE )
  429. m_settings.SetCornerRadius( 0 );
  430. else
  431. m_settings.SetCornerRadius( m_cornerRadius.GetIntValue() );
  432. m_settings.m_ZonePriority = m_PriorityLevelCtrl->GetValue();
  433. m_settings.m_Locked = m_cbLocked->GetValue();
  434. m_settings.m_ThermalReliefGap = m_antipadClearance.GetValue();
  435. m_settings.m_ThermalReliefSpokeWidth = m_spokeWidth.GetValue();
  436. if( m_settings.m_ThermalReliefSpokeWidth < m_settings.m_ZoneMinThickness )
  437. {
  438. DisplayError( this, _( "Thermal spoke width cannot be smaller than the minimum width." ) );
  439. return false;
  440. }
  441. storePersistentNetSortConfigurations();
  442. m_settings.SetIslandRemovalMode( (ISLAND_REMOVAL_MODE) m_cbRemoveIslands->GetSelection() );
  443. m_settings.SetMinIslandArea( m_islandThreshold.GetValue() );
  444. // If we use only exportable to others zones parameters, exit here:
  445. if( aUseExportableSetupOnly )
  446. return true;
  447. // Get the layer selection for this zone
  448. int layers = 0;
  449. for( int ii = 0; ii < m_layers->GetItemCount(); ++ii )
  450. {
  451. if( m_layers->GetToggleValue( (unsigned) ii, 0 ) )
  452. layers++;
  453. }
  454. if( layers == 0 )
  455. {
  456. DisplayError( this, _( "No layer selected." ) );
  457. return false;
  458. }
  459. m_settings.m_NetcodeSelection = m_currentlySelectedNetcode;
  460. m_settings.m_Name = m_tcZoneName->GetValue();
  461. return true;
  462. }
  463. void DIALOG_COPPER_ZONE::updateCurrentNetSelection()
  464. {
  465. const int netSelection = m_ListNetNameSelection->GetSelection();
  466. if( netSelection > 0 )
  467. {
  468. const wxString& selectedNetName = m_ListNetNameSelection->GetString( netSelection );
  469. m_currentlySelectedNetcode = m_netNameToNetCode[selectedNetName];
  470. }
  471. else
  472. {
  473. m_currentlySelectedNetcode = INVALID_NET_CODE;
  474. }
  475. }
  476. void DIALOG_COPPER_ZONE::OnStyleSelection( wxCommandEvent& event )
  477. {
  478. bool enable = m_GridStyleCtrl->GetSelection() >= 1;
  479. m_tcGridStyleThickness->Enable( enable );
  480. m_tcGridStyleGap->Enable( enable );
  481. m_tcGridStyleOrientation->Enable( enable );
  482. m_spinCtrlSmoothLevel->Enable( enable );
  483. m_spinCtrlSmoothValue->Enable( enable );
  484. }
  485. void DIALOG_COPPER_ZONE::OnLayerSelection( wxDataViewEvent& event )
  486. {
  487. if( event.GetColumn() != 0 )
  488. return;
  489. int row = m_layers->ItemToRow( event.GetItem() );
  490. bool checked = m_layers->GetToggleValue( row, 0 );
  491. wxVariant layerID;
  492. m_layers->GetValue( layerID, row, 2 );
  493. m_settings.m_Layers.set( ToLAYER_ID( layerID.GetInteger() ), checked );
  494. }
  495. void DIALOG_COPPER_ZONE::OnNetSortingOptionSelected( wxCommandEvent& event )
  496. {
  497. updateDisplayedListOfNets();
  498. }
  499. void DIALOG_COPPER_ZONE::storePersistentNetSortConfigurations()
  500. {
  501. // These configurations are persistent across multiple invocations of
  502. // this dialog
  503. int newConfig = NO_PERSISTENT_SORT_MODE;
  504. if( m_hideAutoGeneratedNets )
  505. newConfig |= HIDE_ANONYMOUS_NETS;
  506. if( m_netSortingByPadCount )
  507. newConfig |= SORT_BY_PAD_COUNT;
  508. PCBNEW_SETTINGS* cfg = m_Parent->GetPcbNewSettings();
  509. cfg->m_Zones.net_sort_mode = newConfig;
  510. }
  511. void DIALOG_COPPER_ZONE::loadPersistentNetSortConfigurations()
  512. {
  513. PCBNEW_SETTINGS* cfg = m_Parent->GetPcbNewSettings();
  514. int sortMode = cfg->m_Zones.net_sort_mode;
  515. if( sortMode == DEFAULT_SORT_CONFIG )
  516. sortMode = HIDE_ANONYMOUS_NETS;
  517. m_hideAutoGeneratedNets = ( sortMode & HIDE_ANONYMOUS_NETS );
  518. m_netSortingByPadCount = ( sortMode & SORT_BY_PAD_COUNT );
  519. }
  520. void DIALOG_COPPER_ZONE::ExportSetupToOtherCopperZones( wxCommandEvent& event )
  521. {
  522. if( !AcceptOptions( true ) )
  523. return;
  524. // Export settings ( but layer and netcode ) to others copper zones
  525. BOARD* pcb = m_Parent->GetBoard();
  526. for( ZONE* zone : pcb->Zones() )
  527. {
  528. // Cannot export settings from a copper zone
  529. // to a zone keepout:
  530. if( zone->GetIsRuleArea() )
  531. continue;
  532. // Export only to similar zones:
  533. // Teardrop area -> teardrop area of same type
  534. // copper zone -> copper zone
  535. // Exporting current settings to a different zone type make no sense
  536. if( m_settings.m_TeardropType != zone->GetTeardropAreaType() )
  537. continue;
  538. m_settings.ExportSetting( *zone, false ); // false = partial export
  539. m_settingsExported = true;
  540. m_Parent->OnModify();
  541. }
  542. }
  543. void DIALOG_COPPER_ZONE::OnShowNetNameFilterChange( wxCommandEvent& event )
  544. {
  545. updateDisplayedListOfNets();
  546. }
  547. void DIALOG_COPPER_ZONE::updateDisplayedListOfNets()
  548. {
  549. readFilteringAndSortingCriteria();
  550. wxArrayString listOfNets = buildListOfNetsToDisplay();
  551. const int selectedNet = ensureSelectedNetIsVisible( m_currentlySelectedNetcode, listOfNets );
  552. displayNetsList( listOfNets, selectedNet );
  553. }
  554. void DIALOG_COPPER_ZONE::readFilteringAndSortingCriteria()
  555. {
  556. updateShowNetsFilter();
  557. // Hide nets filter criteria
  558. m_hideAutoGeneratedNets = m_hideAutoGenNetNamesOpt->GetValue();
  559. // Nets sort criteria
  560. m_netSortingByPadCount = m_sortByPadsOpt->GetValue();
  561. }
  562. void DIALOG_COPPER_ZONE::updateShowNetsFilter()
  563. {
  564. wxString netNameShowFilter = m_ShowNetNameFilter->GetValue();
  565. if( netNameShowFilter.Len() == 0 )
  566. netNameShowFilter = wxT( "*" );
  567. wxStringTokenizer showFilters( netNameShowFilter.Lower(), wxT( "," ) );
  568. m_showNetsFilter.clear();
  569. while( showFilters.HasMoreTokens() )
  570. {
  571. wxString filter = showFilters.GetNextToken();
  572. filter.Trim( false );
  573. filter.Trim( true );
  574. if( !filter.IsEmpty() )
  575. {
  576. m_showNetsFilter.emplace_back( std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
  577. m_showNetsFilter.back()->SetPattern( filter );
  578. }
  579. }
  580. }
  581. wxArrayString DIALOG_COPPER_ZONE::buildListOfNetsToDisplay()
  582. {
  583. sortNetsIfRequired();
  584. wxArrayString netNames;
  585. for( NETINFO_ITEM* net : m_netInfoItemList )
  586. {
  587. if( m_hideAutoGeneratedNets && net->HasAutoGeneratedNetname() )
  588. continue;
  589. const wxString& netName = getUnescapedNetName( net );
  590. for( const NET_FILTER& filter : m_showNetsFilter )
  591. {
  592. if( filter->Find( netName.Lower() ) )
  593. {
  594. netNames.Add( netName );
  595. break;
  596. }
  597. }
  598. }
  599. netNames.Insert( _( "<no net>" ), INVALID_NET_CODE );
  600. return netNames;
  601. }
  602. void DIALOG_COPPER_ZONE::sortNetsIfRequired()
  603. {
  604. if( m_netSortingByPadCount )
  605. sortNetsByPadCount( m_netInfoItemList, m_maxNetCode );
  606. else
  607. sort( m_netInfoItemList.begin(), m_netInfoItemList.end(), sortNetsByNames );
  608. }
  609. void DIALOG_COPPER_ZONE::sortNetsByPadCount( std::vector<NETINFO_ITEM*>& nets,
  610. const int maxNetCode )
  611. {
  612. const std::vector<PAD*> pads = m_Parent->GetBoard()->GetPads();
  613. padCountListByNet.clear();
  614. // +1 is required for <no-net> item
  615. padCountListByNet.assign( maxNetCode + 1, 0 );
  616. for( PAD* pad : pads )
  617. {
  618. const int netCode = pad->GetNetCode();
  619. if( netCode > INVALID_NET_CODE )
  620. padCountListByNet[netCode]++;
  621. }
  622. sort( nets.begin(), nets.end(), sortNetsByNodes );
  623. }
  624. void DIALOG_COPPER_ZONE::displayNetsList( const wxArrayString& netNamesList, int selectIndex )
  625. {
  626. m_ListNetNameSelection->Clear();
  627. m_ListNetNameSelection->InsertItems( netNamesList, 0 );
  628. m_ListNetNameSelection->SetSelection( selectIndex );
  629. m_ListNetNameSelection->EnsureVisible( selectIndex );
  630. }
  631. int DIALOG_COPPER_ZONE::ensureSelectedNetIsVisible( int selectedNetCode, wxArrayString& netsList )
  632. {
  633. int selectedIndex = 0;
  634. if( selectedNetCode > INVALID_NET_CODE )
  635. {
  636. NETINFO_ITEM* selectedNet = m_Parent->GetBoard()->FindNet( selectedNetCode );
  637. if( selectedNet )
  638. {
  639. const wxString& netName = getUnescapedNetName( selectedNet );
  640. selectedIndex = netsList.Index( netName );
  641. if( wxNOT_FOUND == selectedIndex )
  642. {
  643. // the currently selected net must *always* be visible.
  644. // <no net> is the zero'th index, so pick next lowest
  645. netsList.Insert( netName, 1 );
  646. selectedIndex = 1;
  647. }
  648. }
  649. }
  650. return selectedIndex;
  651. }
  652. wxString DIALOG_COPPER_ZONE::getUnescapedNetName( const NETINFO_ITEM* net )
  653. {
  654. return UnescapeString( net->GetNetname() );
  655. }
  656. void DIALOG_COPPER_ZONE::updateInfoBar()
  657. {
  658. if( m_currentlySelectedNetcode <= INVALID_NET_CODE
  659. && !m_copperZoneInfo->IsShown()
  660. && !m_convertSettings )
  661. {
  662. m_copperZoneInfo->ShowMessage( _( "<no net> will result in an isolated copper island." ),
  663. wxICON_WARNING );
  664. }
  665. else if( m_copperZoneInfo->IsShown() )
  666. {
  667. m_copperZoneInfo->Dismiss();
  668. }
  669. }