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.

264 lines
7.8 KiB

17 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) 2007-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2012 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. /**
  25. * @file ratsnest.cpp
  26. * @brief Ratsnets functions.
  27. */
  28. #include <fctsys.h>
  29. #include <gr_basic.h>
  30. #include <common.h>
  31. #include <class_drawpanel.h>
  32. #include <wxBasePcbFrame.h>
  33. #include <macros.h>
  34. #include <class_board.h>
  35. #include <class_module.h>
  36. #include <class_track.h>
  37. #include <pcbnew.h>
  38. #include <connectivity_data.h>
  39. #include <ratsnest_data.h>
  40. #include <wxPcbStruct.h>
  41. /**
  42. * Function Compile_Ratsnest
  43. * Create the entire board ratsnest.
  44. * Must be called after a board change (changes for
  45. * pads, footprints or a read netlist ).
  46. * @param aDC = the current device context (can be NULL)
  47. * @param aDisplayStatus : if true, display the computation results
  48. */
  49. void PCB_BASE_FRAME::Compile_Ratsnest( wxDC* aDC, bool aDisplayStatus )
  50. {
  51. GetBoard()->GetConnectivity()->RecalculateRatsnest();
  52. GetBoard()->m_Status_Pcb = 0; // we want a full ratsnest computation, from the scratch
  53. if( GetBoard()->IsElementVisible( LAYER_RATSNEST ) && aDC )
  54. DrawGeneralRatsnest( aDC, 0 );
  55. wxString msg;
  56. ClearMsgPanel();
  57. if( aDisplayStatus )
  58. {
  59. msg.Printf( wxT( " %d" ), m_Pcb->GetConnectivity()->GetPadCount() );
  60. AppendMsgPanel( wxT( "Pads" ), msg, RED );
  61. msg.Printf( wxT( " %d" ), m_Pcb->GetConnectivity()->GetNetCount() );
  62. AppendMsgPanel( wxT( "Nets" ), msg, CYAN );
  63. SetMsgPanel( m_Pcb );
  64. }
  65. }
  66. /**
  67. * function DrawGeneralRatsnest
  68. * Only ratsnest items with the status bit CH_VISIBLE set are displayed
  69. * @param aDC = the current device context (can be NULL)
  70. * @param aNetcode: if > 0, Display only the ratsnest relative to the
  71. * corresponding net_code
  72. */
  73. void PCB_BASE_FRAME::DrawGeneralRatsnest( wxDC* aDC, int aNetcode )
  74. {
  75. if( ( m_Pcb->m_Status_Pcb & DO_NOT_SHOW_GENERAL_RASTNEST ) )
  76. {
  77. return;
  78. }
  79. if( aDC == NULL )
  80. return;
  81. auto connectivity = m_Pcb->GetConnectivity();
  82. if( !connectivity->TryLock() )
  83. return;
  84. COLOR4D color = Settings().Colors().GetItemColor( LAYER_RATSNEST );
  85. for( int i = 1; i < connectivity->GetNetCount(); ++i )
  86. {
  87. RN_NET* net = connectivity->GetRatsnestForNet( i );
  88. if( !net )
  89. continue;
  90. if( ( aNetcode <= 0 ) || ( aNetcode == i ) )
  91. {
  92. for( const auto& edge : net->GetEdges() )
  93. {
  94. auto s = edge.GetSourcePos();
  95. auto d = edge.GetTargetPos();
  96. auto sn = edge.GetSourceNode();
  97. auto dn = edge.GetTargetNode();
  98. bool enable = !sn->GetNoLine() && !dn->GetNoLine();
  99. bool show = sn->Parent()->GetLocalRatsnestVisible()
  100. || dn->Parent()->GetLocalRatsnestVisible();
  101. if( enable && show )
  102. GRLine( m_canvas->GetClipBox(), aDC, wxPoint( s.x, s.y ), wxPoint( d.x,
  103. d.y ), 0, color );
  104. }
  105. }
  106. }
  107. connectivity->Unlock();
  108. }
  109. void PCB_BASE_FRAME::TraceModuleRatsNest( wxDC* DC )
  110. {
  111. if( DC == NULL )
  112. return;
  113. COLOR4D tmpcolor = Settings().Colors().GetItemColor( LAYER_RATSNEST );
  114. for( const auto& l : GetBoard()->GetConnectivity()->GetDynamicRatsnest() )
  115. {
  116. GRLine( m_canvas->GetClipBox(), DC, wxPoint( l.a.x, l.a.y ), wxPoint( l.b.x,
  117. l.b.y ), 0, tmpcolor );
  118. }
  119. }
  120. /*
  121. * PCB_BASE_FRAME::BuildAirWiresTargetsList and
  122. * PCB_BASE_FRAME::TraceAirWiresToTargets
  123. * are 2 function to show the near connecting points when
  124. * a new track is created, by displaying g_MaxLinksShowed airwires
  125. * between the on grid mouse cursor and these connecting points
  126. * during the creation of a track
  127. */
  128. /* Buffer to store pads coordinates when creating a track.
  129. * these pads are members of the net
  130. * and when the mouse is moved, the g_MaxLinksShowed links to neighbors are
  131. * drawn
  132. */
  133. static wxPoint s_CursorPos; // Coordinate of the moving point (mouse cursor and
  134. // end of current track segment)
  135. /* Function BuildAirWiresTargetsList
  136. * Build a list of candidates that can be a coonection point
  137. * when a track is started.
  138. * This functions prepares data to show airwires to nearest connecting points (pads)
  139. * from the current new track to candidates during track creation
  140. */
  141. static BOARD_CONNECTED_ITEM* s_ref = nullptr;
  142. static int s_refNet = -1;
  143. void PCB_BASE_FRAME::BuildAirWiresTargetsList( BOARD_CONNECTED_ITEM* aItemRef,
  144. const wxPoint& aPosition, int aNet )
  145. {
  146. s_CursorPos = aPosition; // needed for sort_by_distance
  147. s_ref = aItemRef;
  148. s_refNet = aNet;
  149. }
  150. static MODULE movedModule( nullptr );
  151. void PCB_BASE_FRAME::build_ratsnest_module( MODULE* aModule, wxPoint aMoveVector )
  152. {
  153. auto connectivity = GetBoard()->GetConnectivity();
  154. movedModule = *aModule;
  155. movedModule.Move( -aMoveVector );
  156. connectivity->ClearDynamicRatsnest();
  157. connectivity->BlockRatsnestItems( { aModule } );
  158. connectivity->ComputeDynamicRatsnest( { &movedModule } );
  159. }
  160. void PCB_BASE_FRAME::TraceAirWiresToTargets( wxDC* aDC )
  161. {
  162. auto connectivity = GetBoard()->GetConnectivity();
  163. auto displ_opts = (PCB_DISPLAY_OPTIONS*) GetDisplayOptions();
  164. auto targets = connectivity->NearestUnconnectedTargets( s_ref, s_CursorPos, s_refNet );
  165. if( aDC == NULL )
  166. return;
  167. GRSetDrawMode( aDC, GR_XOR );
  168. for( int i = 0; i < std::min( (int) displ_opts->m_MaxLinksShowed, (int) targets.size() ); i++ )
  169. {
  170. auto p = targets[i];
  171. GRLine( m_canvas->GetClipBox(), aDC, s_CursorPos, wxPoint( p.x, p.y ), 0, YELLOW );
  172. }
  173. }
  174. // Redraw in XOR mode the outlines of the module.
  175. void MODULE::DrawOutlinesWhenMoving( EDA_DRAW_PANEL* panel, wxDC* DC,
  176. const wxPoint& aMoveVector )
  177. {
  178. int pad_fill_tmp;
  179. D_PAD* pt_pad;
  180. DrawEdgesOnly( panel, DC, aMoveVector, GR_XOR );
  181. auto displ_opts = (PCB_DISPLAY_OPTIONS*) ( panel->GetDisplayOptions() );
  182. // Show pads in sketch mode to speedu up drawings
  183. pad_fill_tmp = displ_opts->m_DisplayPadFill;
  184. displ_opts->m_DisplayPadFill = true;
  185. pt_pad = PadsList();
  186. for( ; pt_pad != NULL; pt_pad = pt_pad->Next() )
  187. pt_pad->Draw( panel, DC, GR_XOR, aMoveVector );
  188. displ_opts->m_DisplayPadFill = pad_fill_tmp;
  189. if( displ_opts->m_Show_Module_Ratsnest )
  190. {
  191. PCB_BASE_FRAME* frame = (PCB_BASE_FRAME*) panel->GetParent();
  192. frame->build_ratsnest_module( this, aMoveVector );
  193. frame->TraceModuleRatsNest( DC );
  194. }
  195. }
  196. void PCB_EDIT_FRAME::Show_1_Ratsnest( EDA_ITEM* item, wxDC* DC )
  197. {
  198. if( item && item->Type() == PCB_MODULE_T )
  199. {
  200. auto mod = static_cast<MODULE*> (item);
  201. for( auto pad : mod->Pads() )
  202. {
  203. pad->SetLocalRatsnestVisible( true );
  204. }
  205. m_canvas->Refresh();
  206. }
  207. }