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.

367 lines
9.4 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_edit_frame.h>
  34. #include <sch_connection.h>
  35. #include <schematic.h>
  36. #include <settings/color_settings.h>
  37. #include <connection_graph.h>
  38. #include <string_utils.h>
  39. SCH_JUNCTION::SCH_JUNCTION( const VECTOR2I& aPosition, int aDiameter, SCH_LAYER_ID aLayer ) :
  40. SCH_ITEM( nullptr, SCH_JUNCTION_T )
  41. {
  42. m_pos = aPosition;
  43. m_color = COLOR4D::UNSPECIFIED;
  44. m_diameter = aDiameter;
  45. m_layer = aLayer;
  46. m_lastResolvedDiameter = KiROUND( schIUScale.MilsToIU( DEFAULT_WIRE_WIDTH_MILS ) * 1.7 );
  47. m_lastResolvedColor = COLOR4D::UNSPECIFIED;
  48. }
  49. EDA_ITEM* SCH_JUNCTION::Clone() const
  50. {
  51. return new SCH_JUNCTION( *this );
  52. }
  53. void SCH_JUNCTION::SwapData( SCH_ITEM* aItem )
  54. {
  55. SCH_ITEM::SwapFlags( aItem );
  56. wxCHECK_RET( ( aItem != nullptr ) && ( aItem->Type() == SCH_JUNCTION_T ),
  57. wxT( "Cannot swap junction data with invalid item." ) );
  58. SCH_JUNCTION* item = (SCH_JUNCTION*) aItem;
  59. std::swap( m_pos, item->m_pos );
  60. std::swap( m_diameter, item->m_diameter );
  61. std::swap( m_color, item->m_color );
  62. }
  63. void SCH_JUNCTION::ViewGetLayers( int aLayers[], int& aCount ) const
  64. {
  65. aCount = 2;
  66. aLayers[0] = m_layer;
  67. aLayers[1] = LAYER_SELECTION_SHADOWS;
  68. }
  69. SHAPE_CIRCLE SCH_JUNCTION::getEffectiveShape() const
  70. {
  71. if( m_diameter != 0 )
  72. m_lastResolvedDiameter = m_diameter;
  73. else if( Schematic() )
  74. m_lastResolvedDiameter = Schematic()->Settings().m_JunctionSize;
  75. else
  76. m_lastResolvedDiameter = schIUScale.MilsToIU( DEFAULT_JUNCTION_DIAM );
  77. if( m_lastResolvedDiameter != 1 ) // Diameter 1 means user doesn't want to draw junctions
  78. {
  79. // If we know what we're connected to, then enforce a minimum size of 170% of the
  80. // connected wire width:
  81. if( !IsConnectivityDirty() )
  82. {
  83. m_lastResolvedDiameter = std::max<int>( m_lastResolvedDiameter,
  84. GetEffectiveNetClass()->GetWireWidth() * 1.7 );
  85. }
  86. }
  87. return SHAPE_CIRCLE( m_pos, std::max( m_lastResolvedDiameter / 2, 1 ) );
  88. }
  89. const BOX2I SCH_JUNCTION::GetBoundingBox() const
  90. {
  91. BOX2I bbox( m_pos );
  92. bbox.Inflate( getEffectiveShape().GetRadius() );
  93. return bbox;
  94. }
  95. void SCH_JUNCTION::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset )
  96. {
  97. wxDC* DC = aSettings->GetPrintDC();
  98. COLOR4D color = GetJunctionColor();
  99. if( color == COLOR4D::UNSPECIFIED )
  100. color = aSettings->GetLayerColor( GetLayer() );
  101. SHAPE_CIRCLE circle = getEffectiveShape();
  102. GRFilledCircle( DC, circle.GetCenter() + aOffset, circle.GetRadius(), 0, color, color );
  103. }
  104. void SCH_JUNCTION::MirrorVertically( int aCenter )
  105. {
  106. MIRROR( m_pos.y, aCenter );
  107. }
  108. void SCH_JUNCTION::MirrorHorizontally( int aCenter )
  109. {
  110. MIRROR( m_pos.x, aCenter );
  111. }
  112. void SCH_JUNCTION::Rotate( const VECTOR2I& aCenter )
  113. {
  114. RotatePoint( m_pos, aCenter, ANGLE_90 );
  115. }
  116. void SCH_JUNCTION::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
  117. {
  118. DANGLING_END_ITEM item( JUNCTION_END, this, m_pos );
  119. aItemList.push_back( item );
  120. }
  121. std::vector<VECTOR2I> SCH_JUNCTION::GetConnectionPoints() const
  122. {
  123. return { m_pos };
  124. }
  125. #if defined(DEBUG)
  126. void SCH_JUNCTION::Show( int nestLevel, std::ostream& os ) const
  127. {
  128. // XML output:
  129. wxString s = GetClass();
  130. NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << wxS( ", " ) << m_diameter
  131. << wxS( "/>\n" );
  132. }
  133. #endif
  134. void SCH_JUNCTION::SetDiameter( int aDiameter )
  135. {
  136. m_diameter = aDiameter;
  137. m_lastResolvedDiameter = aDiameter;
  138. }
  139. COLOR4D SCH_JUNCTION::GetJunctionColor() const
  140. {
  141. if( m_color != COLOR4D::UNSPECIFIED )
  142. m_lastResolvedColor = m_color;
  143. else if( !IsConnectivityDirty() )
  144. m_lastResolvedColor = GetEffectiveNetClass()->GetSchematicColor();
  145. return m_lastResolvedColor;
  146. }
  147. void SCH_JUNCTION::SetColor( const COLOR4D& aColor )
  148. {
  149. m_color = aColor;
  150. m_lastResolvedColor = aColor;
  151. }
  152. int SCH_JUNCTION::GetEffectiveDiameter() const
  153. {
  154. return getEffectiveShape().GetRadius() * 2;
  155. }
  156. bool SCH_JUNCTION::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  157. {
  158. if( aAccuracy >= 0 )
  159. return getEffectiveShape().Collide( SEG( aPosition, aPosition ), aAccuracy );
  160. else
  161. return aPosition == m_pos;
  162. }
  163. bool SCH_JUNCTION::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  164. {
  165. if( m_flags & STRUCT_DELETED || m_flags & SKIP_STRUCT )
  166. return false;
  167. if( aContained )
  168. {
  169. BOX2I selRect( aRect );
  170. return selRect.Inflate( aAccuracy ).Contains( GetBoundingBox() );
  171. }
  172. else
  173. {
  174. SHAPE_CIRCLE junction = getEffectiveShape();
  175. SHAPE_RECT selRect( aRect.GetPosition(), aRect.GetWidth(), aRect.GetHeight() );
  176. return selRect.Collide( &junction, aAccuracy );
  177. }
  178. }
  179. bool SCH_JUNCTION::doIsConnected( const VECTOR2I& aPosition ) const
  180. {
  181. return m_pos == aPosition;
  182. }
  183. void SCH_JUNCTION::Plot( PLOTTER* aPlotter, bool aBackground ) const
  184. {
  185. if( aBackground )
  186. return;
  187. RENDER_SETTINGS* settings = aPlotter->RenderSettings();
  188. COLOR4D color = GetJunctionColor();
  189. if( color == COLOR4D::UNSPECIFIED )
  190. color = settings->GetLayerColor( GetLayer() );
  191. aPlotter->SetColor( color );
  192. aPlotter->Circle( m_pos, GetEffectiveDiameter(), FILL_T::FILLED_SHAPE );
  193. }
  194. BITMAPS SCH_JUNCTION::GetMenuImage() const
  195. {
  196. return BITMAPS::add_junction;
  197. }
  198. bool SCH_JUNCTION::operator <( const SCH_ITEM& aItem ) const
  199. {
  200. if( Type() != aItem.Type() )
  201. return Type() < aItem.Type();
  202. if( GetLayer() != aItem.GetLayer() )
  203. return GetLayer() < aItem.GetLayer();
  204. const SCH_JUNCTION* junction = static_cast<const SCH_JUNCTION*>( &aItem );
  205. if( GetPosition().x != junction->GetPosition().x )
  206. return GetPosition().x < junction->GetPosition().x;
  207. if( GetPosition().y != junction->GetPosition().y )
  208. return GetPosition().y < junction->GetPosition().y;
  209. if( GetDiameter() != junction->GetDiameter() )
  210. return GetDiameter() < junction->GetDiameter();
  211. return GetColor() < junction->GetColor();
  212. }
  213. void SCH_JUNCTION::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  214. {
  215. aList.emplace_back( _( "Junction" ), wxEmptyString );
  216. aList.emplace_back( _( "Size" ), aFrame->MessageTextFromValue( GetEffectiveDiameter() ) );
  217. SCH_CONNECTION* conn = nullptr;
  218. if( !IsConnectivityDirty() && dynamic_cast<SCH_EDIT_FRAME*>( aFrame ) )
  219. conn = Connection();
  220. if( conn )
  221. {
  222. conn->AppendInfoToMsgPanel( aList );
  223. if( !conn->IsBus() )
  224. {
  225. aList.emplace_back( _( "Resolved Netclass" ),
  226. UnescapeString( GetEffectiveNetClass()->GetName() ) );
  227. }
  228. }
  229. }
  230. bool SCH_JUNCTION::operator==( const SCH_ITEM& aOther ) const
  231. {
  232. if( Type() != aOther.Type() )
  233. return false;
  234. const SCH_JUNCTION& other = static_cast<const SCH_JUNCTION&>( aOther );
  235. if( m_pos != other.m_pos )
  236. return false;
  237. if( m_diameter != other.m_diameter )
  238. return false;
  239. if( m_color != other.m_color )
  240. return false;
  241. return true;
  242. }
  243. double SCH_JUNCTION::Similarity( const SCH_ITEM& aOther ) const
  244. {
  245. if( m_Uuid == aOther.m_Uuid )
  246. return 1.0;
  247. if( aOther.Type() != Type() )
  248. return 0.0;
  249. const SCH_JUNCTION& other = static_cast<const SCH_JUNCTION&>( aOther );
  250. double similarity = 1.0;
  251. if( m_pos != other.m_pos )
  252. similarity *= 0.9;
  253. if( m_diameter != other.m_diameter )
  254. similarity *= 0.9;
  255. if( m_color != other.m_color )
  256. similarity *= 0.9;
  257. return similarity;
  258. }
  259. static struct SCH_JUNCTION_DESC
  260. {
  261. SCH_JUNCTION_DESC()
  262. {
  263. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  264. REGISTER_TYPE( SCH_JUNCTION );
  265. propMgr.InheritsAfter( TYPE_HASH( SCH_JUNCTION ), TYPE_HASH( SCH_ITEM ) );
  266. propMgr.AddProperty( new PROPERTY<SCH_JUNCTION, int>( _HKI( "Diameter" ),
  267. &SCH_JUNCTION::SetDiameter, &SCH_JUNCTION::GetDiameter,
  268. PROPERTY_DISPLAY::PT_SIZE ) );
  269. }
  270. } _SCH_JUNCTION_DESC;