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.

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