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.

298 lines
7.8 KiB

1 year ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The 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 <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( 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. DRC_CUSTOM_MARKER_HANDLER* aCustomHandler )
  144. {
  145. PCB_MARKER* marker = new PCB_MARKER( aItem, aPos, aLayer );
  146. if( aCustomHandler )
  147. ( *aCustomHandler )( marker );
  148. commit.Add( marker );
  149. } );
  150. m_drcEngine->RunTests( m_editFrame->GetUserUnits(), aReportAllTrackErrors, aTestFootprints,
  151. &commit );
  152. m_drcEngine->SetProgressReporter( nullptr );
  153. m_drcEngine->ClearViolationHandler();
  154. if( m_drcDialog )
  155. {
  156. m_drcDialog->SetDrcRun();
  157. if( aTestFootprints && netlistFetched )
  158. m_drcDialog->SetFootprintTestsRun();
  159. }
  160. commit.Push( _( "DRC" ), SKIP_UNDO | SKIP_SET_DIRTY );
  161. m_drcRunning = false;
  162. m_editFrame->ShowSolderMask();
  163. // update the m_drcDialog listboxes
  164. updatePointers( aProgressReporter->IsCancelled() );
  165. }
  166. void DRC_TOOL::updatePointers( bool aDRCWasCancelled )
  167. {
  168. // update my pointers, m_editFrame is the only unchangeable one
  169. m_pcb = m_editFrame->GetBoard();
  170. m_editFrame->ResolveDRCExclusions( aDRCWasCancelled );
  171. if( m_drcDialog )
  172. m_drcDialog->UpdateData();
  173. }
  174. int DRC_TOOL::PrevMarker( const TOOL_EVENT& aEvent )
  175. {
  176. if( m_drcDialog )
  177. {
  178. m_drcDialog->Show( true );
  179. m_drcDialog->Raise();
  180. m_drcDialog->PrevMarker();
  181. }
  182. else
  183. {
  184. ShowDRCDialog( nullptr );
  185. }
  186. return 0;
  187. }
  188. int DRC_TOOL::NextMarker( const TOOL_EVENT& aEvent )
  189. {
  190. if( m_drcDialog )
  191. {
  192. m_drcDialog->Show( true );
  193. m_drcDialog->Raise();
  194. m_drcDialog->NextMarker();
  195. }
  196. else
  197. {
  198. ShowDRCDialog( nullptr );
  199. }
  200. return 0;
  201. }
  202. int DRC_TOOL::CrossProbe( const TOOL_EVENT& aEvent )
  203. {
  204. if( m_drcDialog && m_drcDialog->IsShownOnScreen() )
  205. {
  206. PCB_SELECTION_TOOL* selectionTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
  207. PCB_SELECTION& selection = selectionTool->GetSelection();
  208. if( selection.GetSize() == 1 && selection.Front()->Type() == PCB_MARKER_T )
  209. m_drcDialog->SelectMarker( static_cast<PCB_MARKER*>( selection.Front() ) );
  210. }
  211. return 0;
  212. }
  213. void DRC_TOOL::CrossProbe( const PCB_MARKER* aMarker )
  214. {
  215. if( !IsDRCDialogShown() )
  216. ShowDRCDialog( nullptr );
  217. m_drcDialog->SelectMarker( aMarker );
  218. }
  219. int DRC_TOOL::ExcludeMarker( const TOOL_EVENT& aEvent )
  220. {
  221. if( m_drcDialog )
  222. m_drcDialog->ExcludeMarker();
  223. return 0;
  224. }
  225. void DRC_TOOL::setTransitions()
  226. {
  227. Go( &DRC_TOOL::ShowDRCDialog, PCB_ACTIONS::runDRC.MakeEvent() );
  228. Go( &DRC_TOOL::PrevMarker, ACTIONS::prevMarker.MakeEvent() );
  229. Go( &DRC_TOOL::NextMarker, ACTIONS::nextMarker.MakeEvent() );
  230. Go( &DRC_TOOL::ExcludeMarker, ACTIONS::excludeMarker.MakeEvent() );
  231. Go( &DRC_TOOL::CrossProbe, EVENTS::PointSelectedEvent );
  232. Go( &DRC_TOOL::CrossProbe, EVENTS::SelectedEvent );
  233. }