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.

370 lines
9.6 KiB

18 years ago
4 years ago
4 years ago
15 years ago
18 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 The 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. wxCHECK_RET( ( aItem != nullptr ) && ( aItem->Type() == SCH_JUNCTION_T ),
  56. wxT( "Cannot swap junction data with invalid item." ) );
  57. SCH_JUNCTION* item = (SCH_JUNCTION*) aItem;
  58. std::swap( m_pos, item->m_pos );
  59. std::swap( m_diameter, item->m_diameter );
  60. std::swap( m_color, item->m_color );
  61. }
  62. std::vector<int> SCH_JUNCTION::ViewGetLayers() const
  63. {
  64. return { m_layer, 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::MirrorVertically( int aCenter )
  93. {
  94. MIRROR( m_pos.y, aCenter );
  95. }
  96. void SCH_JUNCTION::MirrorHorizontally( int aCenter )
  97. {
  98. MIRROR( m_pos.x, aCenter );
  99. }
  100. void SCH_JUNCTION::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
  101. {
  102. RotatePoint( m_pos, aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
  103. }
  104. void SCH_JUNCTION::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
  105. {
  106. DANGLING_END_ITEM item( JUNCTION_END, this, m_pos );
  107. aItemList.push_back( item );
  108. }
  109. std::vector<VECTOR2I> SCH_JUNCTION::GetConnectionPoints() const
  110. {
  111. return { m_pos };
  112. }
  113. #if defined(DEBUG)
  114. void SCH_JUNCTION::Show( int nestLevel, std::ostream& os ) const
  115. {
  116. // XML output:
  117. wxString s = GetClass();
  118. NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << ", " << m_diameter
  119. << "/>\n";
  120. }
  121. #endif
  122. void SCH_JUNCTION::SetDiameter( int aDiameter )
  123. {
  124. m_diameter = aDiameter;
  125. m_lastResolvedDiameter = aDiameter;
  126. }
  127. COLOR4D SCH_JUNCTION::GetJunctionColor() const
  128. {
  129. if( m_color != COLOR4D::UNSPECIFIED )
  130. m_lastResolvedColor = m_color;
  131. else if( !IsConnectivityDirty() )
  132. m_lastResolvedColor = GetEffectiveNetClass()->GetSchematicColor();
  133. return m_lastResolvedColor;
  134. }
  135. void SCH_JUNCTION::SetColor( const COLOR4D& aColor )
  136. {
  137. m_color = aColor;
  138. m_lastResolvedColor = aColor;
  139. }
  140. int SCH_JUNCTION::GetEffectiveDiameter() const
  141. {
  142. return getEffectiveShape().GetRadius() * 2;
  143. }
  144. bool SCH_JUNCTION::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  145. {
  146. if( aAccuracy >= 0 )
  147. return getEffectiveShape().Collide( SEG( aPosition, aPosition ), aAccuracy );
  148. else
  149. return aPosition == m_pos;
  150. }
  151. bool SCH_JUNCTION::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  152. {
  153. if( m_flags & STRUCT_DELETED || m_flags & SKIP_STRUCT )
  154. return false;
  155. if( aContained )
  156. {
  157. BOX2I selRect( aRect );
  158. return selRect.Inflate( aAccuracy ).Contains( GetBoundingBox() );
  159. }
  160. else
  161. {
  162. SHAPE_CIRCLE junction = getEffectiveShape();
  163. SHAPE_RECT selRect( aRect.GetPosition(), aRect.GetWidth(), aRect.GetHeight() );
  164. return selRect.Collide( &junction, aAccuracy );
  165. }
  166. }
  167. bool SCH_JUNCTION::HasConnectivityChanges( const SCH_ITEM* aItem,
  168. const SCH_SHEET_PATH* aInstance ) const
  169. {
  170. // Do not compare to ourself.
  171. if( aItem == this )
  172. return false;
  173. const SCH_JUNCTION* junction = dynamic_cast<const SCH_JUNCTION*>( aItem );
  174. // Don't compare against a different SCH_ITEM.
  175. wxCHECK( junction, false );
  176. return GetPosition() != junction->GetPosition();
  177. }
  178. bool SCH_JUNCTION::doIsConnected( const VECTOR2I& aPosition ) const
  179. {
  180. return m_pos == aPosition;
  181. }
  182. void SCH_JUNCTION::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
  183. int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
  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, 0 );
  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()->GetHumanReadableName() ) );
  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. propMgr.AddProperty( new PROPERTY<SCH_JUNCTION, COLOR4D>( _HKI( "Color" ),
  270. &SCH_JUNCTION::SetColor, &SCH_JUNCTION::GetColor ) );
  271. }
  272. } _SCH_JUNCTION_DESC;