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.

280 lines
7.3 KiB

4 years ago
18 years ago
4 years ago
4 years ago
15 years ago
18 years ago
18 years ago
3 years ago
4 years ago
4 years ago
4 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2022 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. #include <sch_draw_panel.h>
  25. #include <trigo.h>
  26. #include <common.h>
  27. #include <plotters/plotter.h>
  28. #include <bitmaps.h>
  29. #include <core/mirror.h>
  30. #include <geometry/shape_rect.h>
  31. #include <sch_painter.h>
  32. #include <sch_junction.h>
  33. #include <sch_connection.h>
  34. #include <schematic.h>
  35. #include <settings/color_settings.h>
  36. #include <connection_graph.h>
  37. SCH_JUNCTION::SCH_JUNCTION( const VECTOR2I& aPosition, int aDiameter, SCH_LAYER_ID aLayer ) :
  38. SCH_ITEM( nullptr, SCH_JUNCTION_T )
  39. {
  40. m_pos = aPosition;
  41. m_color = COLOR4D::UNSPECIFIED;
  42. m_diameter = aDiameter;
  43. m_layer = aLayer;
  44. m_lastResolvedDiameter = KiROUND( schIUScale.MilsToIU( DEFAULT_WIRE_WIDTH_MILS ) * 1.7 );
  45. m_lastResolvedColor = COLOR4D::UNSPECIFIED;
  46. }
  47. EDA_ITEM* SCH_JUNCTION::Clone() const
  48. {
  49. return new SCH_JUNCTION( *this );
  50. }
  51. void SCH_JUNCTION::SwapData( SCH_ITEM* aItem )
  52. {
  53. wxCHECK_RET( ( aItem != nullptr ) && ( aItem->Type() == SCH_JUNCTION_T ),
  54. wxT( "Cannot swap junction data with invalid item." ) );
  55. SCH_JUNCTION* item = (SCH_JUNCTION*) aItem;
  56. std::swap( m_pos, item->m_pos );
  57. std::swap( m_diameter, item->m_diameter );
  58. std::swap( m_color, item->m_color );
  59. }
  60. void SCH_JUNCTION::ViewGetLayers( int aLayers[], int& aCount ) const
  61. {
  62. aCount = 2;
  63. aLayers[0] = m_layer;
  64. aLayers[1] = LAYER_SELECTION_SHADOWS;
  65. }
  66. SHAPE_CIRCLE SCH_JUNCTION::getEffectiveShape() const
  67. {
  68. if( m_diameter != 0 )
  69. m_lastResolvedDiameter = m_diameter;
  70. else if( Schematic() )
  71. m_lastResolvedDiameter = Schematic()->Settings().m_JunctionSize;
  72. else
  73. m_lastResolvedDiameter = schIUScale.MilsToIU( DEFAULT_JUNCTION_DIAM );
  74. if( m_lastResolvedDiameter != 1 ) // Diameter 1 means user doesn't want to draw junctions
  75. {
  76. // If we know what we're connected to, then enforce a minimum size of 170% of the
  77. // connected wire width:
  78. if( !IsConnectivityDirty() )
  79. {
  80. m_lastResolvedDiameter = std::max<int>( m_lastResolvedDiameter,
  81. GetEffectiveNetClass()->GetWireWidth() * 1.7 );
  82. }
  83. }
  84. return SHAPE_CIRCLE( m_pos, std::max( m_lastResolvedDiameter / 2, 1 ) );
  85. }
  86. const BOX2I SCH_JUNCTION::GetBoundingBox() const
  87. {
  88. BOX2I bbox( m_pos );
  89. bbox.Inflate( getEffectiveShape().GetRadius() );
  90. return bbox;
  91. }
  92. void SCH_JUNCTION::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset )
  93. {
  94. wxDC* DC = aSettings->GetPrintDC();
  95. COLOR4D color = GetJunctionColor();
  96. if( color == COLOR4D::UNSPECIFIED )
  97. color = aSettings->GetLayerColor( GetLayer() );
  98. SHAPE_CIRCLE circle = getEffectiveShape();
  99. GRFilledCircle( DC, circle.GetCenter() + aOffset, circle.GetRadius(), 0, color, color );
  100. }
  101. void SCH_JUNCTION::MirrorVertically( int aCenter )
  102. {
  103. MIRROR( m_pos.y, aCenter );
  104. }
  105. void SCH_JUNCTION::MirrorHorizontally( int aCenter )
  106. {
  107. MIRROR( m_pos.x, aCenter );
  108. }
  109. void SCH_JUNCTION::Rotate( const VECTOR2I& aCenter )
  110. {
  111. RotatePoint( m_pos, aCenter, ANGLE_90 );
  112. }
  113. void SCH_JUNCTION::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
  114. {
  115. DANGLING_END_ITEM item( JUNCTION_END, this, m_pos );
  116. aItemList.push_back( item );
  117. }
  118. std::vector<VECTOR2I> SCH_JUNCTION::GetConnectionPoints() const
  119. {
  120. return { m_pos };
  121. }
  122. #if defined(DEBUG)
  123. void SCH_JUNCTION::Show( int nestLevel, std::ostream& os ) const
  124. {
  125. // XML output:
  126. wxString s = GetClass();
  127. NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << wxS( ", " ) << m_diameter
  128. << wxS( "/>\n" );
  129. }
  130. #endif
  131. void SCH_JUNCTION::SetDiameter( int aDiameter )
  132. {
  133. m_diameter = aDiameter;
  134. m_lastResolvedDiameter = aDiameter;
  135. }
  136. COLOR4D SCH_JUNCTION::GetJunctionColor() const
  137. {
  138. if( m_color != COLOR4D::UNSPECIFIED )
  139. m_lastResolvedColor = m_color;
  140. else if( !IsConnectivityDirty() )
  141. m_lastResolvedColor = GetEffectiveNetClass()->GetSchematicColor();
  142. return m_lastResolvedColor;
  143. }
  144. void SCH_JUNCTION::SetColor( const COLOR4D& aColor )
  145. {
  146. m_color = aColor;
  147. m_lastResolvedColor = aColor;
  148. }
  149. int SCH_JUNCTION::GetEffectiveDiameter() const
  150. {
  151. return getEffectiveShape().GetRadius() * 2;
  152. }
  153. bool SCH_JUNCTION::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  154. {
  155. if( aAccuracy >= 0 )
  156. return getEffectiveShape().Collide( SEG( aPosition, aPosition ), aAccuracy );
  157. else
  158. return aPosition == m_pos;
  159. }
  160. bool SCH_JUNCTION::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  161. {
  162. if( m_flags & STRUCT_DELETED || m_flags & SKIP_STRUCT )
  163. return false;
  164. if( aContained )
  165. {
  166. BOX2I selRect( aRect );
  167. return selRect.Inflate( aAccuracy ).Contains( GetBoundingBox() );
  168. }
  169. else
  170. {
  171. SHAPE_CIRCLE junction = getEffectiveShape();
  172. SHAPE_RECT selRect( aRect.GetPosition(), aRect.GetWidth(), aRect.GetHeight() );
  173. return selRect.Collide( &junction, aAccuracy );
  174. }
  175. }
  176. bool SCH_JUNCTION::doIsConnected( const VECTOR2I& aPosition ) const
  177. {
  178. return m_pos == aPosition;
  179. }
  180. void SCH_JUNCTION::Plot( PLOTTER* aPlotter, bool aBackground ) const
  181. {
  182. if( aBackground )
  183. return;
  184. RENDER_SETTINGS* settings = aPlotter->RenderSettings();
  185. COLOR4D color = GetJunctionColor();
  186. if( color == COLOR4D::UNSPECIFIED )
  187. color = settings->GetLayerColor( GetLayer() );
  188. aPlotter->SetColor( color );
  189. aPlotter->Circle( m_pos, GetEffectiveDiameter(), FILL_T::FILLED_SHAPE );
  190. }
  191. BITMAPS SCH_JUNCTION::GetMenuImage() const
  192. {
  193. return BITMAPS::add_junction;
  194. }
  195. bool SCH_JUNCTION::operator <( const SCH_ITEM& aItem ) const
  196. {
  197. if( Type() != aItem.Type() )
  198. return Type() < aItem.Type();
  199. if( GetLayer() != aItem.GetLayer() )
  200. return GetLayer() < aItem.GetLayer();
  201. const SCH_JUNCTION* junction = static_cast<const SCH_JUNCTION*>( &aItem );
  202. if( GetPosition().x != junction->GetPosition().x )
  203. return GetPosition().x < junction->GetPosition().x;
  204. if( GetPosition().y != junction->GetPosition().y )
  205. return GetPosition().y < junction->GetPosition().y;
  206. if( GetDiameter() != junction->GetDiameter() )
  207. return GetDiameter() < junction->GetDiameter();
  208. return GetColor() < junction->GetColor();
  209. }