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.

291 lines
7.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020-2022 KiCad Developers, see change_log.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 <pcb_edit_frame.h>
  24. #include <tool/tool_manager.h>
  25. #include <tools/pcb_actions.h>
  26. #include <tools/pcb_tool_base.h>
  27. #include <tools/zone_filler_tool.h>
  28. #include <tools/pcb_selection_tool.h>
  29. #include <tools/drc_tool.h>
  30. #include <kiface_base.h>
  31. #include <dialog_drc.h>
  32. #include <board_commit.h>
  33. #include <board_design_settings.h>
  34. #include <progress_reporter.h>
  35. #include <drc/drc_engine.h>
  36. #include <drc/drc_item.h>
  37. #include <netlist_reader/pcb_netlist.h>
  38. #include <macros.h>
  39. DRC_TOOL::DRC_TOOL() :
  40. PCB_TOOL_BASE( "pcbnew.DRCTool" ),
  41. m_editFrame( nullptr ),
  42. m_pcb( nullptr ),
  43. m_drcDialog( nullptr ),
  44. m_drcRunning( false )
  45. {
  46. }
  47. DRC_TOOL::~DRC_TOOL()
  48. {
  49. }
  50. void DRC_TOOL::Reset( RESET_REASON aReason )
  51. {
  52. m_editFrame = getEditFrame<PCB_EDIT_FRAME>();
  53. if( m_pcb != m_editFrame->GetBoard() )
  54. {
  55. if( m_drcDialog )
  56. DestroyDRCDialog();
  57. m_pcb = m_editFrame->GetBoard();
  58. m_drcEngine = m_pcb->GetDesignSettings().m_DRCEngine;
  59. }
  60. }
  61. void DRC_TOOL::ShowDRCDialog( wxWindow* aParent )
  62. {
  63. bool show_dlg_modal = true;
  64. // the dialog needs a parent frame. if it is not specified, this is the PCB editor frame
  65. // specified in DRC_TOOL class.
  66. if( !aParent )
  67. {
  68. // if any parent is specified, the dialog is modal.
  69. // if this is the default PCB editor frame, it is not modal
  70. show_dlg_modal = false;
  71. aParent = m_editFrame;
  72. }
  73. Activate();
  74. m_toolMgr->RunAction( PCB_ACTIONS::selectionClear );
  75. if( !m_drcDialog )
  76. {
  77. m_drcDialog = new DIALOG_DRC( m_editFrame, aParent );
  78. updatePointers( false );
  79. if( show_dlg_modal )
  80. m_drcDialog->ShowModal();
  81. else
  82. m_drcDialog->Show( true );
  83. }
  84. else // The dialog is just not visible (because the user has double clicked on an error item)
  85. {
  86. updatePointers( false );
  87. m_drcDialog->Show( true );
  88. }
  89. }
  90. int DRC_TOOL::ShowDRCDialog( const TOOL_EVENT& aEvent )
  91. {
  92. ShowDRCDialog( nullptr );
  93. return 0;
  94. }
  95. bool DRC_TOOL::IsDRCDialogShown()
  96. {
  97. if( m_drcDialog )
  98. return m_drcDialog->IsShownOnScreen();
  99. return false;
  100. }
  101. void DRC_TOOL::DestroyDRCDialog()
  102. {
  103. if( m_drcDialog )
  104. {
  105. m_drcDialog->Destroy();
  106. m_drcDialog = nullptr;
  107. }
  108. }
  109. void DRC_TOOL::RunTests( PROGRESS_REPORTER* aProgressReporter, bool aRefillZones,
  110. bool aReportAllTrackErrors, bool aTestFootprints )
  111. {
  112. // One at a time, please.
  113. // Note that the main GUI entry points to get here are blocked, so this is really an
  114. // insurance policy and as such we make no attempts to queue up the DRC run or anything.
  115. if( m_drcRunning )
  116. return;
  117. ZONE_FILLER_TOOL* zoneFiller = m_toolMgr->GetTool<ZONE_FILLER_TOOL>();
  118. BOARD_COMMIT commit( m_editFrame );
  119. NETLIST netlist;
  120. bool netlistFetched = false;
  121. wxWindowDisabler disabler( /* disable everything except: */ m_drcDialog );
  122. m_drcRunning = true;
  123. if( aRefillZones )
  124. {
  125. aProgressReporter->AdvancePhase( _( "Refilling all zones..." ) );
  126. zoneFiller->FillAllZones( m_drcDialog, aProgressReporter );
  127. }
  128. m_drcEngine->SetDrawingSheet( m_editFrame->GetCanvas()->GetDrawingSheet() );
  129. if( aTestFootprints && !Kiface().IsSingle() )
  130. {
  131. if( m_editFrame->FetchNetlistFromSchematic( netlist, _( "Schematic parity tests require a "
  132. "fully annotated schematic." ) ) )
  133. {
  134. netlistFetched = true;
  135. }
  136. if( m_drcDialog )
  137. m_drcDialog->Raise();
  138. m_drcEngine->SetSchematicNetlist( &netlist );
  139. }
  140. m_drcEngine->SetProgressReporter( aProgressReporter );
  141. m_drcEngine->SetViolationHandler(
  142. [&]( const std::shared_ptr<DRC_ITEM>& aItem, VECTOR2I aPos, int aLayer )
  143. {
  144. PCB_MARKER* marker = new PCB_MARKER( aItem, aPos, aLayer );
  145. commit.Add( marker );
  146. } );
  147. m_drcEngine->RunTests( m_editFrame->GetUserUnits(), aReportAllTrackErrors, aTestFootprints );
  148. m_drcEngine->SetProgressReporter( nullptr );
  149. m_drcEngine->ClearViolationHandler();
  150. if( m_drcDialog )
  151. {
  152. m_drcDialog->SetDrcRun();
  153. if( aTestFootprints && netlistFetched )
  154. m_drcDialog->SetFootprintTestsRun();
  155. }
  156. commit.Push( _( "DRC" ), SKIP_UNDO | SKIP_SET_DIRTY );
  157. m_drcRunning = false;
  158. m_editFrame->ShowSolderMask();
  159. // update the m_drcDialog listboxes
  160. updatePointers( aProgressReporter->IsCancelled() );
  161. }
  162. void DRC_TOOL::updatePointers( bool aDRCWasCancelled )
  163. {
  164. // update my pointers, m_editFrame is the only unchangeable one
  165. m_pcb = m_editFrame->GetBoard();
  166. m_editFrame->ResolveDRCExclusions( aDRCWasCancelled );
  167. if( m_drcDialog )
  168. m_drcDialog->UpdateData();
  169. }
  170. int DRC_TOOL::PrevMarker( const TOOL_EVENT& aEvent )
  171. {
  172. if( m_drcDialog )
  173. {
  174. m_drcDialog->Show( true );
  175. m_drcDialog->Raise();
  176. m_drcDialog->PrevMarker();
  177. }
  178. else
  179. {
  180. ShowDRCDialog( nullptr );
  181. }
  182. return 0;
  183. }
  184. int DRC_TOOL::NextMarker( const TOOL_EVENT& aEvent )
  185. {
  186. if( m_drcDialog )
  187. {
  188. m_drcDialog->Show( true );
  189. m_drcDialog->Raise();
  190. m_drcDialog->NextMarker();
  191. }
  192. else
  193. {
  194. ShowDRCDialog( nullptr );
  195. }
  196. return 0;
  197. }
  198. int DRC_TOOL::CrossProbe( const TOOL_EVENT& aEvent )
  199. {
  200. if( m_drcDialog && m_drcDialog->IsShownOnScreen() )
  201. {
  202. PCB_SELECTION_TOOL* selectionTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
  203. PCB_SELECTION& selection = selectionTool->GetSelection();
  204. if( selection.GetSize() == 1 && selection.Front()->Type() == PCB_MARKER_T )
  205. m_drcDialog->SelectMarker( static_cast<PCB_MARKER*>( selection.Front() ) );
  206. }
  207. return 0;
  208. }
  209. void DRC_TOOL::CrossProbe( const PCB_MARKER* aMarker )
  210. {
  211. if( !IsDRCDialogShown() )
  212. ShowDRCDialog( nullptr );
  213. m_drcDialog->SelectMarker( aMarker );
  214. }
  215. int DRC_TOOL::ExcludeMarker( const TOOL_EVENT& aEvent )
  216. {
  217. if( m_drcDialog )
  218. m_drcDialog->ExcludeMarker();
  219. return 0;
  220. }
  221. void DRC_TOOL::setTransitions()
  222. {
  223. Go( &DRC_TOOL::ShowDRCDialog, PCB_ACTIONS::runDRC.MakeEvent() );
  224. Go( &DRC_TOOL::PrevMarker, ACTIONS::prevMarker.MakeEvent() );
  225. Go( &DRC_TOOL::NextMarker, ACTIONS::nextMarker.MakeEvent() );
  226. Go( &DRC_TOOL::ExcludeMarker, ACTIONS::excludeMarker.MakeEvent() );
  227. Go( &DRC_TOOL::CrossProbe, EVENTS::PointSelectedEvent );
  228. Go( &DRC_TOOL::CrossProbe, EVENTS::SelectedEvent );
  229. }