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.

1096 lines
36 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
14 years ago
18 years ago
18 years ago
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
14 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
18 years ago
18 years ago
18 years ago
14 years ago
18 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 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) 2004-2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2014 Dick Hollenbeck, dick@softplc.com
  6. * Copyright (C) 2017 KiCad Developers, see change_log.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. /**
  26. * @file drc.cpp
  27. */
  28. #include <fctsys.h>
  29. #include <wxPcbStruct.h>
  30. #include <trigo.h>
  31. #include <base_units.h>
  32. #include <class_board_design_settings.h>
  33. #include <class_module.h>
  34. #include <class_track.h>
  35. #include <class_pad.h>
  36. #include <class_zone.h>
  37. #include <class_pcb_text.h>
  38. #include <class_draw_panel_gal.h>
  39. #include <view/view.h>
  40. #include <geometry/seg.h>
  41. #include <connectivity_data.h>
  42. #include <connectivity_algo.h>
  43. #include <tool/tool_manager.h>
  44. #include <tools/pcb_actions.h>
  45. #include <pcbnew.h>
  46. #include <drc_stuff.h>
  47. #include <dialog_drc.h>
  48. #include <wx/progdlg.h>
  49. #include <board_commit.h>
  50. void DRC::ShowDRCDialog( wxWindow* aParent )
  51. {
  52. bool show_dlg_modal = true;
  53. // the dialog needs a parent frame. if it is not specified, this is
  54. // the PCB editor frame specified in DRC class.
  55. if( aParent == NULL )
  56. {
  57. // if any parent is specified, the dialog is modal.
  58. // if this is the default PCB editor frame, it is not modal
  59. show_dlg_modal = false;
  60. aParent = m_pcbEditorFrame;
  61. }
  62. if( !m_drcDialog )
  63. {
  64. m_pcbEditorFrame->GetToolManager()->RunAction( PCB_ACTIONS::selectionClear, true );
  65. m_drcDialog = new DIALOG_DRC_CONTROL( this, m_pcbEditorFrame, aParent );
  66. updatePointers();
  67. m_drcDialog->SetRptSettings( m_doCreateRptFile, m_rptFilename);
  68. if( show_dlg_modal )
  69. m_drcDialog->ShowModal();
  70. else
  71. m_drcDialog->Show( true );
  72. }
  73. else // The dialog is just not visible (because the user has double clicked on an error item)
  74. {
  75. updatePointers();
  76. m_drcDialog->Show( true );
  77. }
  78. }
  79. void DRC::addMarkerToPcb( MARKER_PCB* aMarker )
  80. {
  81. BOARD_COMMIT commit ( m_pcbEditorFrame );
  82. commit.Add( aMarker );
  83. commit.Push( wxEmptyString, false );
  84. }
  85. void DRC::DestroyDRCDialog( int aReason )
  86. {
  87. if( m_drcDialog )
  88. {
  89. if( aReason == wxID_OK )
  90. {
  91. // if user clicked OK, save his choices in this DRC object.
  92. m_drcDialog->GetRptSettings( &m_doCreateRptFile, m_rptFilename);
  93. }
  94. m_drcDialog->Destroy();
  95. m_drcDialog = NULL;
  96. }
  97. }
  98. DRC::DRC( PCB_EDIT_FRAME* aPcbWindow )
  99. {
  100. m_pcbEditorFrame = aPcbWindow;
  101. m_pcb = aPcbWindow->GetBoard();
  102. m_drcDialog = NULL;
  103. // establish initial values for everything:
  104. m_doPad2PadTest = true; // enable pad to pad clearance tests
  105. m_doUnconnectedTest = true; // enable unconnected tests
  106. m_doZonesTest = true; // enable zone to items clearance tests
  107. m_doKeepoutTest = true; // enable keepout areas to items clearance tests
  108. m_doFootprintOverlapping = true; // enable courtyards areas overlap tests
  109. m_doNoCourtyardDefined = true; // enable missing courtyard in footprint warning
  110. m_abortDRC = false;
  111. m_drcInProgress = false;
  112. m_doCreateRptFile = false;
  113. // m_rptFilename set to empty by its constructor
  114. m_currentMarker = NULL;
  115. m_segmAngle = 0;
  116. m_segmLength = 0;
  117. m_xcliplo = 0;
  118. m_ycliplo = 0;
  119. m_xcliphi = 0;
  120. m_ycliphi = 0;
  121. }
  122. DRC::~DRC()
  123. {
  124. // maybe someday look at pointainer.h <- google for "pointainer.h"
  125. for( unsigned i = 0; i<m_unconnected.size(); ++i )
  126. delete m_unconnected[i];
  127. }
  128. int DRC::Drc( TRACK* aRefSegm, TRACK* aList )
  129. {
  130. updatePointers();
  131. if( !doTrackDrc( aRefSegm, aList, true ) )
  132. {
  133. wxASSERT( m_currentMarker );
  134. m_pcbEditorFrame->SetMsgPanel( m_currentMarker );
  135. return BAD_DRC;
  136. }
  137. if( !doTrackKeepoutDrc( aRefSegm ) )
  138. {
  139. wxASSERT( m_currentMarker );
  140. m_pcbEditorFrame->SetMsgPanel( m_currentMarker );
  141. return BAD_DRC;
  142. }
  143. return OK_DRC;
  144. }
  145. int DRC::Drc( ZONE_CONTAINER* aArea, int aCornerIndex )
  146. {
  147. updatePointers();
  148. if( !doEdgeZoneDrc( aArea, aCornerIndex ) )
  149. {
  150. wxASSERT( m_currentMarker );
  151. m_pcbEditorFrame->SetMsgPanel( m_currentMarker );
  152. return BAD_DRC;
  153. }
  154. return OK_DRC;
  155. }
  156. void DRC::RunTests( wxTextCtrl* aMessages )
  157. {
  158. // be sure m_pcb is the current board, not a old one
  159. // ( the board can be reloaded )
  160. m_pcb = m_pcbEditorFrame->GetBoard();
  161. // someone should have cleared the two lists before calling this.
  162. if( !testNetClasses() )
  163. {
  164. // testing the netclasses is a special case because if the netclasses
  165. // do not pass the BOARD_DESIGN_SETTINGS checks, then every member of a net
  166. // class (a NET) will cause its items such as tracks, vias, and pads
  167. // to also fail. So quit after *all* netclass errors have been reported.
  168. if( aMessages )
  169. aMessages->AppendText( _( "Aborting\n" ) );
  170. // update the m_drcDialog listboxes
  171. updatePointers();
  172. return;
  173. }
  174. // test pad to pad clearances, nothing to do with tracks, vias or zones.
  175. if( m_doPad2PadTest )
  176. {
  177. if( aMessages )
  178. {
  179. aMessages->AppendText( _( "Pad clearances...\n" ) );
  180. wxSafeYield();
  181. }
  182. testPad2Pad();
  183. }
  184. // test track and via clearances to other tracks, pads, and vias
  185. if( aMessages )
  186. {
  187. aMessages->AppendText( _( "Track clearances...\n" ) );
  188. wxSafeYield();
  189. }
  190. testTracks( aMessages ? aMessages->GetParent() : m_pcbEditorFrame, true );
  191. // Before testing segments and unconnected, refill all zones:
  192. // this is a good caution, because filled areas can be outdated.
  193. if( aMessages )
  194. {
  195. aMessages->AppendText( _( "Fill zones...\n" ) );
  196. wxSafeYield();
  197. }
  198. m_pcbEditorFrame->Fill_All_Zones( aMessages ? aMessages->GetParent() : m_pcbEditorFrame,
  199. false );
  200. // test zone clearances to other zones
  201. if( aMessages )
  202. {
  203. aMessages->AppendText( _( "Test zones...\n" ) );
  204. wxSafeYield();
  205. }
  206. testZones();
  207. // find and gather unconnected pads.
  208. if( m_doUnconnectedTest )
  209. {
  210. if( aMessages )
  211. {
  212. aMessages->AppendText( _( "Unconnected pads...\n" ) );
  213. aMessages->Refresh();
  214. }
  215. testUnconnected();
  216. }
  217. // find and gather vias, tracks, pads inside keepout areas.
  218. if( m_doKeepoutTest )
  219. {
  220. if( aMessages )
  221. {
  222. aMessages->AppendText( _( "Keepout areas ...\n" ) );
  223. aMessages->Refresh();
  224. }
  225. testKeepoutAreas();
  226. }
  227. // find and gather vias, tracks, pads inside text boxes.
  228. if( aMessages )
  229. {
  230. aMessages->AppendText( _( "Test texts...\n" ) );
  231. wxSafeYield();
  232. }
  233. testTexts();
  234. // find overlaping courtyard ares.
  235. if( m_doFootprintOverlapping || m_doNoCourtyardDefined )
  236. {
  237. if( aMessages )
  238. {
  239. aMessages->AppendText( _( "Courtyard areas...\n" ) );
  240. aMessages->Refresh();
  241. }
  242. doFootprintOverlappingDrc();
  243. }
  244. // update the m_drcDialog listboxes
  245. updatePointers();
  246. if( aMessages )
  247. {
  248. // no newline on this one because it is last, don't want the window
  249. // to unnecessarily scroll.
  250. aMessages->AppendText( _( "Finished" ) );
  251. }
  252. }
  253. void DRC::ListUnconnectedPads()
  254. {
  255. testUnconnected();
  256. // update the m_drcDialog listboxes
  257. updatePointers();
  258. }
  259. void DRC::updatePointers()
  260. {
  261. // update my pointers, m_pcbEditorFrame is the only unchangeable one
  262. m_pcb = m_pcbEditorFrame->GetBoard();
  263. if( m_drcDialog ) // Use diag list boxes only in DRC dialog
  264. {
  265. m_drcDialog->m_ClearanceListBox->SetList( new DRC_LIST_MARKERS( m_pcb ) );
  266. m_drcDialog->m_UnconnectedListBox->SetList( new DRC_LIST_UNCONNECTED( &m_unconnected ) );
  267. m_drcDialog->UpdateDisplayedCounts();
  268. }
  269. }
  270. bool DRC::doNetClass( NETCLASSPTR nc, wxString& msg )
  271. {
  272. bool ret = true;
  273. const BOARD_DESIGN_SETTINGS& g = m_pcb->GetDesignSettings();
  274. #define FmtVal( x ) GetChars( StringFromValue( g_UserUnit, x ) )
  275. #if 0 // set to 1 when (if...) BOARD_DESIGN_SETTINGS has a m_MinClearance value
  276. if( nc->GetClearance() < g.m_MinClearance )
  277. {
  278. msg.Printf( _( "NETCLASS: '%s' has Clearance:%s which is less than global:%s" ),
  279. GetChars( nc->GetName() ),
  280. FmtVal( nc->GetClearance() ),
  281. FmtVal( g.m_TrackClearance )
  282. );
  283. addMarkerToPcb( fillMarker( DRCE_NETCLASS_CLEARANCE, msg, m_currentMarker ) );
  284. m_currentMarker = nullptr;
  285. ret = false;
  286. }
  287. #endif
  288. if( nc->GetTrackWidth() < g.m_TrackMinWidth )
  289. {
  290. msg.Printf( _( "NETCLASS: '%s' has TrackWidth:%s which is less than global:%s" ),
  291. GetChars( nc->GetName() ),
  292. FmtVal( nc->GetTrackWidth() ),
  293. FmtVal( g.m_TrackMinWidth )
  294. );
  295. addMarkerToPcb( fillMarker( DRCE_NETCLASS_TRACKWIDTH, msg, m_currentMarker ) );
  296. m_currentMarker = nullptr;
  297. ret = false;
  298. }
  299. if( nc->GetViaDiameter() < g.m_ViasMinSize )
  300. {
  301. msg.Printf( _( "NETCLASS: '%s' has Via Dia:%s which is less than global:%s" ),
  302. GetChars( nc->GetName() ),
  303. FmtVal( nc->GetViaDiameter() ),
  304. FmtVal( g.m_ViasMinSize )
  305. );
  306. addMarkerToPcb( fillMarker( DRCE_NETCLASS_VIASIZE, msg, m_currentMarker ) );
  307. m_currentMarker = nullptr;
  308. ret = false;
  309. }
  310. if( nc->GetViaDrill() < g.m_ViasMinDrill )
  311. {
  312. msg.Printf( _( "NETCLASS: '%s' has Via Drill:%s which is less than global:%s" ),
  313. GetChars( nc->GetName() ),
  314. FmtVal( nc->GetViaDrill() ),
  315. FmtVal( g.m_ViasMinDrill )
  316. );
  317. addMarkerToPcb( fillMarker( DRCE_NETCLASS_VIADRILLSIZE, msg, m_currentMarker ) );
  318. m_currentMarker = nullptr;
  319. ret = false;
  320. }
  321. if( nc->GetuViaDiameter() < g.m_MicroViasMinSize )
  322. {
  323. msg.Printf( _( "NETCLASS: '%s' has uVia Dia:%s which is less than global:%s" ),
  324. GetChars( nc->GetName() ),
  325. FmtVal( nc->GetuViaDiameter() ),
  326. FmtVal( g.m_MicroViasMinSize )
  327. );
  328. addMarkerToPcb( fillMarker( DRCE_NETCLASS_uVIASIZE, msg, m_currentMarker ) );
  329. m_currentMarker = nullptr;
  330. ret = false;
  331. }
  332. if( nc->GetuViaDrill() < g.m_MicroViasMinDrill )
  333. {
  334. msg.Printf( _( "NETCLASS: '%s' has uVia Drill:%s which is less than global:%s" ),
  335. GetChars( nc->GetName() ),
  336. FmtVal( nc->GetuViaDrill() ),
  337. FmtVal( g.m_MicroViasMinDrill )
  338. );
  339. addMarkerToPcb( fillMarker( DRCE_NETCLASS_uVIADRILLSIZE, msg, m_currentMarker ) );
  340. m_currentMarker = nullptr;
  341. ret = false;
  342. }
  343. return ret;
  344. }
  345. bool DRC::testNetClasses()
  346. {
  347. bool ret = true;
  348. NETCLASSES& netclasses = m_pcb->GetDesignSettings().m_NetClasses;
  349. wxString msg; // construct this only once here, not in a loop, since somewhat expensive.
  350. if( !doNetClass( netclasses.GetDefault(), msg ) )
  351. ret = false;
  352. for( NETCLASSES::const_iterator i = netclasses.begin(); i != netclasses.end(); ++i )
  353. {
  354. NETCLASSPTR nc = i->second;
  355. if( !doNetClass( nc, msg ) )
  356. ret = false;
  357. }
  358. return ret;
  359. }
  360. void DRC::testPad2Pad()
  361. {
  362. std::vector<D_PAD*> sortedPads;
  363. m_pcb->GetSortedPadListByXthenYCoord( sortedPads );
  364. // find the max size of the pads (used to stop the test)
  365. int max_size = 0;
  366. for( unsigned i = 0; i < sortedPads.size(); ++i )
  367. {
  368. D_PAD* pad = sortedPads[i];
  369. // GetBoundingRadius() is the radius of the minimum sized circle fully containing the pad
  370. int radius = pad->GetBoundingRadius();
  371. if( radius > max_size )
  372. max_size = radius;
  373. }
  374. // Test the pads
  375. D_PAD** listEnd = &sortedPads[ sortedPads.size() ];
  376. for( unsigned i = 0; i< sortedPads.size(); ++i )
  377. {
  378. D_PAD* pad = sortedPads[i];
  379. int x_limit = max_size + pad->GetClearance() +
  380. pad->GetBoundingRadius() + pad->GetPosition().x;
  381. if( !doPadToPadsDrc( pad, &sortedPads[i], listEnd, x_limit ) )
  382. {
  383. wxASSERT( m_currentMarker );
  384. addMarkerToPcb ( m_currentMarker );
  385. m_currentMarker = nullptr;
  386. }
  387. }
  388. }
  389. void DRC::testTracks( wxWindow *aActiveWindow, bool aShowProgressBar )
  390. {
  391. wxProgressDialog * progressDialog = NULL;
  392. const int delta = 500; // This is the number of tests between 2 calls to the
  393. // progress bar
  394. int count = 0;
  395. for( TRACK* segm = m_pcb->m_Track; segm && segm->Next(); segm = segm->Next() )
  396. count++;
  397. int deltamax = count/delta;
  398. if( aShowProgressBar && deltamax > 3 )
  399. {
  400. progressDialog = new wxProgressDialog( _( "Track clearances" ), wxEmptyString,
  401. deltamax, aActiveWindow,
  402. wxPD_AUTO_HIDE | wxPD_CAN_ABORT |
  403. wxPD_APP_MODAL | wxPD_ELAPSED_TIME );
  404. progressDialog->Update( 0, wxEmptyString );
  405. }
  406. int ii = 0;
  407. count = 0;
  408. for( TRACK* segm = m_pcb->m_Track; segm; segm = segm->Next() )
  409. {
  410. if ( ii++ > delta )
  411. {
  412. ii = 0;
  413. count++;
  414. if( progressDialog )
  415. {
  416. if( !progressDialog->Update( count, wxEmptyString ) )
  417. break; // Aborted by user
  418. #ifdef __WXMAC__
  419. // Work around a dialog z-order issue on OS X
  420. if( count == deltamax )
  421. aActiveWindow->Raise();
  422. #endif
  423. }
  424. }
  425. if( !doTrackDrc( segm, segm->Next(), true ) )
  426. {
  427. wxASSERT( m_currentMarker );
  428. addMarkerToPcb ( m_currentMarker );
  429. m_currentMarker = nullptr;
  430. }
  431. }
  432. if( progressDialog )
  433. progressDialog->Destroy();
  434. }
  435. void DRC::testUnconnected()
  436. {
  437. auto connectivity = m_pcb->GetConnectivity();
  438. connectivity->Clear();
  439. connectivity->Build(m_pcb); // just in case. This really needs to be reliable.
  440. connectivity->RecalculateRatsnest();
  441. std::vector<CN_EDGE> edges;
  442. connectivity->GetUnconnectedEdges( edges );
  443. for( const auto& edge : edges )
  444. {
  445. wxString t_src = edge.GetSourceNode()->Parent()->GetSelectMenuText();
  446. wxString t_dst = edge.GetTargetNode()->Parent()->GetSelectMenuText();
  447. auto src = edge.GetSourcePos();
  448. auto dst = edge.GetTargetPos();
  449. DRC_ITEM* uncItem = new DRC_ITEM( DRCE_UNCONNECTED_ITEMS,
  450. t_src,
  451. t_dst,
  452. wxPoint( src.x, src.y ), wxPoint( dst.x, dst.y ) );
  453. m_unconnected.push_back( uncItem );
  454. }
  455. }
  456. void DRC::testZones()
  457. {
  458. // Test copper areas for valid netcodes
  459. // if a netcode is < 0 the netname was not found when reading a netlist
  460. // if a netcode is == 0 the netname is void, and the zone is not connected.
  461. // This is allowed, but i am not sure this is a good idea
  462. //
  463. // In recent Pcbnew versions, the netcode is always >= 0, but an internal net name
  464. // is stored, and initalized from the file or the zone properpies editor.
  465. // if it differs from the net name from net code, there is a DRC issue
  466. for( int ii = 0; ii < m_pcb->GetAreaCount(); ii++ )
  467. {
  468. ZONE_CONTAINER* test_area = m_pcb->GetArea( ii );
  469. if( !test_area->IsOnCopperLayer() )
  470. continue;
  471. int netcode = test_area->GetNetCode();
  472. // a netcode < 0 or > 0 and no pad in net is a error or strange
  473. // perhaps a "dead" net, which happens when all pads in this net were removed
  474. // Remark: a netcode < 0 should not happen (this is more a bug somewhere)
  475. int pads_in_net = (test_area->GetNetCode() > 0) ?
  476. m_pcb->GetConnectivity()->GetPadCount( test_area->GetNetCode() ) : 1;
  477. if( ( netcode < 0 ) || pads_in_net == 0 )
  478. {
  479. addMarkerToPcb( fillMarker( test_area,
  480. DRCE_SUSPICIOUS_NET_FOR_ZONE_OUTLINE, m_currentMarker ) );
  481. m_currentMarker = nullptr;
  482. }
  483. }
  484. // Test copper areas outlines, and create markers when needed
  485. m_pcb->Test_Drc_Areas_Outlines_To_Areas_Outlines( NULL, true );
  486. }
  487. void DRC::testKeepoutAreas()
  488. {
  489. // Test keepout areas for vias, tracks and pads inside keepout areas
  490. for( int ii = 0; ii < m_pcb->GetAreaCount(); ii++ )
  491. {
  492. ZONE_CONTAINER* area = m_pcb->GetArea( ii );
  493. if( !area->GetIsKeepout() )
  494. {
  495. continue;
  496. }
  497. for( TRACK* segm = m_pcb->m_Track; segm != NULL; segm = segm->Next() )
  498. {
  499. if( segm->Type() == PCB_TRACE_T )
  500. {
  501. if( ! area->GetDoNotAllowTracks() )
  502. continue;
  503. // Ignore if the keepout zone is not on the same layer
  504. if( !area->IsOnLayer( segm->GetLayer() ) )
  505. continue;
  506. if( area->Outline()->Distance( SEG( segm->GetStart(), segm->GetEnd() ),
  507. segm->GetWidth() ) == 0 )
  508. {
  509. addMarkerToPcb( fillMarker( segm, NULL,
  510. DRCE_TRACK_INSIDE_KEEPOUT, m_currentMarker ) );
  511. m_currentMarker = nullptr;
  512. }
  513. }
  514. else if( segm->Type() == PCB_VIA_T )
  515. {
  516. if( ! area->GetDoNotAllowVias() )
  517. continue;
  518. auto viaLayers = segm->GetLayerSet();
  519. if( !area->CommonLayerExists( viaLayers ) )
  520. continue;
  521. if( area->Outline()->Distance( segm->GetPosition() ) < segm->GetWidth()/2 )
  522. {
  523. addMarkerToPcb( fillMarker( segm, NULL,
  524. DRCE_VIA_INSIDE_KEEPOUT, m_currentMarker ) );
  525. m_currentMarker = nullptr;
  526. }
  527. }
  528. }
  529. // Test pads: TODO
  530. }
  531. }
  532. void DRC::testTexts()
  533. {
  534. std::vector<wxPoint> textShape; // a buffer to store the text shape (set of segments)
  535. std::vector<D_PAD*> padList = m_pcb->GetPads();
  536. // Test text areas for vias, tracks and pads inside text areas
  537. for( auto item : m_pcb->Drawings() )
  538. {
  539. // Drc test only items on copper layers
  540. if( ! IsCopperLayer( item->GetLayer() ) )
  541. continue;
  542. // only texts on copper layers are tested
  543. if( item->Type() != PCB_TEXT_T )
  544. continue;
  545. textShape.clear();
  546. // So far the bounding box makes up the text-area
  547. TEXTE_PCB* text = (TEXTE_PCB*) item;
  548. text->TransformTextShapeToSegmentList( textShape );
  549. if( textShape.size() == 0 ) // Should not happen (empty text?)
  550. continue;
  551. for( TRACK* track = m_pcb->m_Track; track != NULL; track = track->Next() )
  552. {
  553. if( ! track->IsOnLayer( item->GetLayer() ) )
  554. continue;
  555. // Test the distance between each segment and the current track/via
  556. int min_dist = ( track->GetWidth() + text->GetThickness() ) /2 +
  557. track->GetClearance(NULL);
  558. if( track->Type() == PCB_TRACE_T )
  559. {
  560. SEG segref( track->GetStart(), track->GetEnd() );
  561. // Error condition: Distance between text segment and track segment is
  562. // smaller than the clearance of the segment
  563. for( unsigned jj = 0; jj < textShape.size(); jj += 2 )
  564. {
  565. SEG segtest( textShape[jj], textShape[jj+1] );
  566. int dist = segref.Distance( segtest );
  567. if( dist < min_dist )
  568. {
  569. addMarkerToPcb( fillMarker( track, text,
  570. DRCE_TRACK_INSIDE_TEXT,
  571. m_currentMarker ) );
  572. m_currentMarker = nullptr;
  573. break;
  574. }
  575. }
  576. }
  577. else if( track->Type() == PCB_VIA_T )
  578. {
  579. // Error condition: Distance between text segment and via is
  580. // smaller than the clearance of the via
  581. for( unsigned jj = 0; jj < textShape.size(); jj += 2 )
  582. {
  583. SEG segtest( textShape[jj], textShape[jj+1] );
  584. if( segtest.PointCloserThan( track->GetPosition(), min_dist ) )
  585. {
  586. addMarkerToPcb( fillMarker( track, text,
  587. DRCE_VIA_INSIDE_TEXT, m_currentMarker ) );
  588. m_currentMarker = nullptr;
  589. break;
  590. }
  591. }
  592. }
  593. }
  594. // Test pads
  595. for( unsigned ii = 0; ii < padList.size(); ii++ )
  596. {
  597. D_PAD* pad = padList[ii];
  598. if( ! pad->IsOnLayer( item->GetLayer() ) )
  599. continue;
  600. wxPoint shape_pos = pad->ShapePos();
  601. for( unsigned jj = 0; jj < textShape.size(); jj += 2 )
  602. {
  603. /* In order to make some calculations more easier or faster,
  604. * pads and tracks coordinates will be made relative
  605. * to the segment origin
  606. */
  607. wxPoint origin = textShape[jj]; // origin will be the origin of other coordinates
  608. m_segmEnd = textShape[jj+1] - origin;
  609. wxPoint delta = m_segmEnd;
  610. m_segmAngle = 0;
  611. // for a non horizontal or vertical segment Compute the segment angle
  612. // in tenths of degrees and its length
  613. if( delta.x || delta.y ) // delta.x == delta.y == 0 for vias
  614. {
  615. // Compute the segment angle in 0,1 degrees
  616. m_segmAngle = ArcTangente( delta.y, delta.x );
  617. // Compute the segment length: we build an equivalent rotated segment,
  618. // this segment is horizontal, therefore dx = length
  619. RotatePoint( &delta, m_segmAngle ); // delta.x = length, delta.y = 0
  620. }
  621. m_segmLength = delta.x;
  622. m_padToTestPos = shape_pos - origin;
  623. if( !checkClearanceSegmToPad( pad, text->GetThickness(),
  624. pad->GetClearance(NULL) ) )
  625. {
  626. addMarkerToPcb( fillMarker( pad, text,
  627. DRCE_PAD_INSIDE_TEXT, m_currentMarker ) );
  628. m_currentMarker = nullptr;
  629. break;
  630. }
  631. }
  632. }
  633. }
  634. }
  635. bool DRC::doTrackKeepoutDrc( TRACK* aRefSeg )
  636. {
  637. // Test keepout areas for vias, tracks and pads inside keepout areas
  638. for( int ii = 0; ii < m_pcb->GetAreaCount(); ii++ )
  639. {
  640. ZONE_CONTAINER* area = m_pcb->GetArea( ii );
  641. if( !area->GetIsKeepout() )
  642. continue;
  643. if( aRefSeg->Type() == PCB_TRACE_T )
  644. {
  645. if( ! area->GetDoNotAllowTracks() )
  646. continue;
  647. if( !area->IsOnLayer( aRefSeg->GetLayer() ) )
  648. continue;
  649. if( area->Outline()->Distance( SEG( aRefSeg->GetStart(), aRefSeg->GetEnd() ),
  650. aRefSeg->GetWidth() ) == 0 )
  651. {
  652. m_currentMarker = fillMarker( aRefSeg, NULL,
  653. DRCE_TRACK_INSIDE_KEEPOUT, m_currentMarker );
  654. return false;
  655. }
  656. }
  657. else if( aRefSeg->Type() == PCB_VIA_T )
  658. {
  659. if( ! area->GetDoNotAllowVias() )
  660. continue;
  661. auto viaLayers = aRefSeg->GetLayerSet();
  662. if( !area->CommonLayerExists( viaLayers ) )
  663. continue;
  664. if( area->Outline()->Distance( aRefSeg->GetPosition() ) < aRefSeg->GetWidth()/2 )
  665. {
  666. m_currentMarker = fillMarker( aRefSeg, NULL,
  667. DRCE_VIA_INSIDE_KEEPOUT, m_currentMarker );
  668. return false;
  669. }
  670. }
  671. }
  672. return true;
  673. }
  674. bool DRC::doPadToPadsDrc( D_PAD* aRefPad, D_PAD** aStart, D_PAD** aEnd, int x_limit )
  675. {
  676. const static LSET all_cu = LSET::AllCuMask();
  677. LSET layerMask = aRefPad->GetLayerSet() & all_cu;
  678. /* used to test DRC pad to holes: this dummy pad has the size and shape of the hole
  679. * to test pad to pad hole DRC, using the pad to pad DRC test function.
  680. * Therefore, this dummy pad is a circle or an oval.
  681. * A pad must have a parent because some functions expect a non null parent
  682. * to find the parent board, and some other data
  683. */
  684. MODULE dummymodule( m_pcb ); // Creates a dummy parent
  685. D_PAD dummypad( &dummymodule );
  686. // Ensure the hole is on all copper layers
  687. dummypad.SetLayerSet( all_cu | dummypad.GetLayerSet() );
  688. // Use the minimal local clearance value for the dummy pad.
  689. // The clearance of the active pad will be used as minimum distance to a hole
  690. // (a value = 0 means use netclass value)
  691. dummypad.SetLocalClearance( 1 );
  692. for( D_PAD** pad_list = aStart; pad_list<aEnd; ++pad_list )
  693. {
  694. D_PAD* pad = *pad_list;
  695. if( pad == aRefPad )
  696. continue;
  697. // We can stop the test when pad->GetPosition().x > x_limit
  698. // because the list is sorted by X values
  699. if( pad->GetPosition().x > x_limit )
  700. break;
  701. // No problem if pads which are on copper layers are on different copper layers,
  702. // (pads can be only on a technical layer, to build complex pads)
  703. // but their hole (if any ) can create DRC error because they are on all
  704. // copper layers, so we test them
  705. if( ( pad->GetLayerSet() & layerMask ) == 0 &&
  706. ( pad->GetLayerSet() & all_cu ) != 0 &&
  707. ( aRefPad->GetLayerSet() & all_cu ) != 0 )
  708. {
  709. // if holes are in the same location and have the same size and shape,
  710. // this can be accepted
  711. if( pad->GetPosition() == aRefPad->GetPosition()
  712. && pad->GetDrillSize() == aRefPad->GetDrillSize()
  713. && pad->GetDrillShape() == aRefPad->GetDrillShape() )
  714. {
  715. if( aRefPad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
  716. continue;
  717. // for oval holes: must also have the same orientation
  718. if( pad->GetOrientation() == aRefPad->GetOrientation() )
  719. continue;
  720. }
  721. /* Here, we must test clearance between holes and pads
  722. * dummy pad size and shape is adjusted to pad drill size and shape
  723. */
  724. if( pad->GetDrillSize().x )
  725. {
  726. // pad under testing has a hole, test this hole against pad reference
  727. dummypad.SetPosition( pad->GetPosition() );
  728. dummypad.SetSize( pad->GetDrillSize() );
  729. dummypad.SetShape( pad->GetDrillShape() == PAD_DRILL_SHAPE_OBLONG ?
  730. PAD_SHAPE_OVAL : PAD_SHAPE_CIRCLE );
  731. dummypad.SetOrientation( pad->GetOrientation() );
  732. if( !checkClearancePadToPad( aRefPad, &dummypad ) )
  733. {
  734. // here we have a drc error on pad!
  735. m_currentMarker = fillMarker( pad, aRefPad,
  736. DRCE_HOLE_NEAR_PAD, m_currentMarker );
  737. return false;
  738. }
  739. }
  740. if( aRefPad->GetDrillSize().x ) // pad reference has a hole
  741. {
  742. dummypad.SetPosition( aRefPad->GetPosition() );
  743. dummypad.SetSize( aRefPad->GetDrillSize() );
  744. dummypad.SetShape( aRefPad->GetDrillShape() == PAD_DRILL_SHAPE_OBLONG ?
  745. PAD_SHAPE_OVAL : PAD_SHAPE_CIRCLE );
  746. dummypad.SetOrientation( aRefPad->GetOrientation() );
  747. if( !checkClearancePadToPad( pad, &dummypad ) )
  748. {
  749. // here we have a drc error on aRefPad!
  750. m_currentMarker = fillMarker( aRefPad, pad,
  751. DRCE_HOLE_NEAR_PAD, m_currentMarker );
  752. return false;
  753. }
  754. }
  755. continue;
  756. }
  757. // The pad must be in a net (i.e pt_pad->GetNet() != 0 ),
  758. // But no problem if pads have the same netcode (same net)
  759. if( pad->GetNetCode() && ( aRefPad->GetNetCode() == pad->GetNetCode() ) )
  760. continue;
  761. // if pads are from the same footprint
  762. if( pad->GetParent() == aRefPad->GetParent() )
  763. {
  764. // and have the same pad number ( equivalent pads )
  765. // one can argue that this 2nd test is not necessary, that any
  766. // two pads from a single module are acceptable. This 2nd test
  767. // should eventually be a configuration option.
  768. if( pad->PadNameEqual( aRefPad ) )
  769. continue;
  770. }
  771. // if either pad has no drill and is only on technical layers, not a clearance violation
  772. if( ( ( pad->GetLayerSet() & layerMask ) == 0 && !pad->GetDrillSize().x ) ||
  773. ( ( aRefPad->GetLayerSet() & layerMask ) == 0 && !aRefPad->GetDrillSize().x ) )
  774. {
  775. continue;
  776. }
  777. if( !checkClearancePadToPad( aRefPad, pad ) )
  778. {
  779. // here we have a drc error!
  780. m_currentMarker = fillMarker( aRefPad, pad, DRCE_PAD_NEAR_PAD1, m_currentMarker );
  781. return false;
  782. }
  783. }
  784. return true;
  785. }
  786. bool DRC::doFootprintOverlappingDrc()
  787. {
  788. // Detects missing (or malformed) footprint courtyard,
  789. // and for footprint with courtyard, courtyards overlap.
  790. wxString msg;
  791. bool success = true;
  792. // Update courtyard polygons, and test for missing courtyard definition:
  793. for( MODULE* footprint = m_pcb->m_Modules; footprint; footprint = footprint->Next() )
  794. {
  795. bool is_ok = footprint->BuildPolyCourtyard();
  796. if( !is_ok && m_doFootprintOverlapping )
  797. {
  798. msg.Printf( _( "footprint '%s' has malformed courtyard" ),
  799. footprint->GetReference().GetData() );
  800. m_currentMarker = fillMarker( footprint->GetPosition(),
  801. DRCE_MALFORMED_COURTYARD_IN_FOOTPRINT,
  802. msg, m_currentMarker );
  803. addMarkerToPcb( m_currentMarker );
  804. m_currentMarker = nullptr;
  805. success = false;
  806. }
  807. if( !m_doNoCourtyardDefined )
  808. continue;
  809. if( footprint->GetPolyCourtyardFront().OutlineCount() == 0 &&
  810. footprint->GetPolyCourtyardBack().OutlineCount() == 0 &&
  811. is_ok )
  812. {
  813. msg.Printf( _( "footprint '%s' has no courtyard defined" ),
  814. footprint->GetReference().GetData() );
  815. m_currentMarker = fillMarker( footprint->GetPosition(),
  816. DRCE_MISSING_COURTYARD_IN_FOOTPRINT,
  817. msg, m_currentMarker );
  818. addMarkerToPcb( m_currentMarker );
  819. m_currentMarker = nullptr;
  820. success = false;
  821. }
  822. }
  823. if( !m_doFootprintOverlapping )
  824. return success;
  825. // Now test for overlapping on top layer:
  826. SHAPE_POLY_SET courtyard; // temporary storage of the courtyard of current footprint
  827. for( MODULE* footprint = m_pcb->m_Modules; footprint; footprint = footprint->Next() )
  828. {
  829. if( footprint->GetPolyCourtyardFront().OutlineCount() == 0 )
  830. continue; // No courtyard defined
  831. for( MODULE* candidate = footprint->Next(); candidate; candidate = candidate->Next() )
  832. {
  833. if( candidate->GetPolyCourtyardFront().OutlineCount() == 0 )
  834. continue; // No courtyard defined
  835. courtyard.RemoveAllContours();
  836. courtyard.Append( footprint->GetPolyCourtyardFront() );
  837. // Build the common area between footprint and the candidate:
  838. courtyard.BooleanIntersection( candidate->GetPolyCourtyardFront(), SHAPE_POLY_SET::PM_FAST );
  839. // If no overlap, courtyard is empty (no common area).
  840. // Therefore if a common polygon exists, this is a DRC error
  841. if( courtyard.OutlineCount() )
  842. {
  843. //Overlap between footprint and candidate
  844. msg.Printf( _( "footprints '%s' and '%s' overlap on front (top) layer" ),
  845. footprint->GetReference().GetData(),
  846. candidate->GetReference().GetData() );
  847. VECTOR2I& pos = courtyard.Vertex( 0, 0, -1 );
  848. wxPoint loc( pos.x, pos.y );
  849. m_currentMarker = fillMarker( loc, DRCE_OVERLAPPING_FOOTPRINTS, msg, m_currentMarker );
  850. addMarkerToPcb( m_currentMarker );
  851. m_currentMarker = nullptr;
  852. success = false;
  853. }
  854. }
  855. }
  856. // Test for overlapping on bottom layer:
  857. for( MODULE* footprint = m_pcb->m_Modules; footprint; footprint = footprint->Next() )
  858. {
  859. if( footprint->GetPolyCourtyardBack().OutlineCount() == 0 )
  860. continue; // No courtyard defined
  861. for( MODULE* candidate = footprint->Next(); candidate; candidate = candidate->Next() )
  862. {
  863. if( candidate->GetPolyCourtyardBack().OutlineCount() == 0 )
  864. continue; // No courtyard defined
  865. courtyard.RemoveAllContours();
  866. courtyard.Append( footprint->GetPolyCourtyardBack() );
  867. // Build the common area between footprint and the candidate:
  868. courtyard.BooleanIntersection( candidate->GetPolyCourtyardBack(), SHAPE_POLY_SET::PM_FAST );
  869. // If no overlap, courtyard is empty (no common area).
  870. // Therefore if a common polygon exists, this is a DRC error
  871. if( courtyard.OutlineCount() )
  872. {
  873. //Overlap between footprint and candidate
  874. msg.Printf( _( "footprints '%s' and '%s' overlap on back (bottom) layer" ),
  875. footprint->GetReference().GetData(),
  876. candidate->GetReference().GetData() );
  877. VECTOR2I& pos = courtyard.Vertex( 0, 0, -1 );
  878. wxPoint loc( pos.x, pos.y );
  879. m_currentMarker = fillMarker( loc, DRCE_OVERLAPPING_FOOTPRINTS, msg, m_currentMarker );
  880. addMarkerToPcb( m_currentMarker );
  881. m_currentMarker = nullptr;
  882. success = false;
  883. }
  884. }
  885. }
  886. return success;
  887. }