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.

442 lines
13 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <algorithm>
  24. #include <iterator>
  25. #include <map>
  26. #include <vector>
  27. #include <eda_draw_frame.h>
  28. #include <erc/erc_item.h>
  29. #include <erc/erc_settings.h>
  30. #include <geometry/shape_segment.h>
  31. #include <geometry/shape_simple.h>
  32. #include <sch_line.h>
  33. #include <sch_marker.h>
  34. #include <sch_rtree.h>
  35. #include <sch_rule_area.h>
  36. #include <sch_screen.h>
  37. #include <sch_sheet_path.h>
  38. wxString SCH_RULE_AREA::GetClass() const
  39. {
  40. return wxT( "SCH_RULE_AREA" );
  41. }
  42. wxString SCH_RULE_AREA::GetFriendlyName() const
  43. {
  44. return _( "Rule Area" );
  45. }
  46. EDA_ITEM* SCH_RULE_AREA::Clone() const
  47. {
  48. return new SCH_RULE_AREA( *this );
  49. }
  50. std::vector<int> SCH_RULE_AREA::ViewGetLayers() const
  51. {
  52. return { LAYER_RULE_AREAS, LAYER_NOTES_BACKGROUND, LAYER_SELECTION_SHADOWS };
  53. }
  54. std::vector<SHAPE*> SCH_RULE_AREA::MakeEffectiveShapes( bool aEdgeOnly ) const
  55. {
  56. std::vector<SHAPE*> effectiveShapes;
  57. int width = GetEffectiveWidth();
  58. switch( m_shape )
  59. {
  60. case SHAPE_T::POLY:
  61. {
  62. if( GetPolyShape().OutlineCount() == 0 ) // malformed/empty polygon
  63. break;
  64. for( int ii = 0; ii < GetPolyShape().OutlineCount(); ++ii )
  65. {
  66. const SHAPE_LINE_CHAIN& l = GetPolyShape().COutline( ii );
  67. if( IsSolidFill() && !aEdgeOnly )
  68. effectiveShapes.emplace_back( new SHAPE_SIMPLE( l ) );
  69. if( width > 0 || !IsSolidFill() || aEdgeOnly )
  70. {
  71. int segCount = l.SegmentCount();
  72. for( int jj = 0; jj < segCount; jj++ )
  73. effectiveShapes.emplace_back( new SHAPE_SEGMENT( l.CSegment( jj ), width ) );
  74. }
  75. }
  76. }
  77. break;
  78. default:
  79. return SCH_SHAPE::MakeEffectiveShapes( aEdgeOnly );
  80. }
  81. return effectiveShapes;
  82. }
  83. void SCH_RULE_AREA::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
  84. int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
  85. {
  86. if( IsPrivate() )
  87. return;
  88. SCH_RENDER_SETTINGS* renderSettings = getRenderSettings( aPlotter );
  89. int pen_size = GetEffectivePenWidth( renderSettings );
  90. if( GetShape() != SHAPE_T::POLY )
  91. SCH_SHAPE::Plot( aPlotter, aBackground, aPlotOpts, aUnit, aBodyStyle, aOffset, aDimmed );
  92. static std::vector<VECTOR2I> ptList;
  93. ptList.clear();
  94. const std::vector<VECTOR2I>& polyPoints = m_poly.Outline( 0 ).CPoints();
  95. for( const VECTOR2I& pt : polyPoints )
  96. {
  97. ptList.push_back( pt );
  98. }
  99. ptList.push_back( polyPoints[0] );
  100. COLOR4D color = GetStroke().GetColor();
  101. COLOR4D bg = renderSettings->GetBackgroundColor();
  102. LINE_STYLE lineStyle = GetStroke().GetLineStyle();
  103. FILL_T fill = m_fill;
  104. if( aBackground )
  105. {
  106. if( !aPlotter->GetColorMode() )
  107. return;
  108. switch( m_fill )
  109. {
  110. case FILL_T::FILLED_SHAPE:
  111. return;
  112. case FILL_T::FILLED_WITH_COLOR:
  113. color = GetFillColor();
  114. break;
  115. case FILL_T::FILLED_WITH_BG_BODYCOLOR:
  116. color = renderSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
  117. break;
  118. default:
  119. return;
  120. }
  121. pen_size = 0;
  122. lineStyle = LINE_STYLE::SOLID;
  123. }
  124. else /* if( aForeground ) */
  125. {
  126. if( !aPlotter->GetColorMode() || color == COLOR4D::UNSPECIFIED )
  127. color = renderSettings->GetLayerColor( m_layer );
  128. if( lineStyle == LINE_STYLE::DEFAULT )
  129. lineStyle = LINE_STYLE::SOLID;
  130. if( m_fill == FILL_T::FILLED_SHAPE )
  131. fill = m_fill;
  132. else
  133. fill = FILL_T::NO_FILL;
  134. pen_size = GetEffectivePenWidth( renderSettings );
  135. }
  136. if( bg == COLOR4D::UNSPECIFIED || !aPlotter->GetColorMode() )
  137. bg = COLOR4D::WHITE;
  138. if( aDimmed )
  139. {
  140. color.Desaturate();
  141. color = color.Mix( bg, 0.5f );
  142. }
  143. aPlotter->SetColor( color );
  144. aPlotter->SetCurrentLineWidth( pen_size );
  145. aPlotter->SetDash( pen_size, lineStyle );
  146. aPlotter->PlotPoly( ptList, fill, pen_size );
  147. aPlotter->SetDash( pen_size, LINE_STYLE::SOLID );
  148. }
  149. wxString SCH_RULE_AREA::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
  150. {
  151. return _( "Schematic rule area" );
  152. }
  153. void SCH_RULE_AREA::ResetCaches( KIGFX::SCH_VIEW* view )
  154. {
  155. // Save the current state
  156. m_prev_items = m_items;
  157. m_prev_directives = m_directives;
  158. // Reset the rule area
  159. clearContainedItems();
  160. clearDirectives( view );
  161. }
  162. void SCH_RULE_AREA::RefreshContainedItemsAndDirectives(
  163. SCH_SCREEN* screen, KIGFX::SCH_VIEW* view,
  164. std::vector<std::pair<SCH_RULE_AREA*, SCH_SCREEN*>>& forceUpdateRuleAreas )
  165. {
  166. EE_RTREE& items = screen->Items();
  167. const BOX2I boundingBox = GetBoundingBox();
  168. // Get any SCH_DIRECTIVE_LABELs which are attached to the rule area border
  169. std::unordered_set<SCH_DIRECTIVE_LABEL*> attachedDirectives;
  170. EE_RTREE::EE_TYPE candidateDirectives = items.Overlapping( SCH_DIRECTIVE_LABEL_T, boundingBox );
  171. for( SCH_ITEM* candidateDirective : candidateDirectives )
  172. {
  173. SCH_DIRECTIVE_LABEL* label = static_cast<SCH_DIRECTIVE_LABEL*>( candidateDirective );
  174. const std::vector<VECTOR2I> labelConnectionPoints = label->GetConnectionPoints();
  175. assert( labelConnectionPoints.size() == 1 );
  176. if( GetPolyShape().CollideEdge( labelConnectionPoints[0], nullptr, 5 ) )
  177. {
  178. addDirective( label, view );
  179. }
  180. }
  181. // If directives have changed, we need to force an update of the contained items connectivity
  182. if( m_directives != m_prev_directives )
  183. forceUpdateRuleAreas.push_back( { this, screen } );
  184. // Next find any connectable items which lie within the rule area
  185. EE_RTREE::EE_TYPE ruleAreaItems = items.Overlapping( boundingBox );
  186. for( SCH_ITEM* areaItem : ruleAreaItems )
  187. {
  188. if( areaItem->IsType( { SCH_ITEM_LOCATE_WIRE_T, SCH_ITEM_LOCATE_BUS_T } ) )
  189. {
  190. SCH_LINE* lineItem = static_cast<SCH_LINE*>( areaItem );
  191. SHAPE_SEGMENT lineSeg( lineItem->GetStartPoint(), lineItem->GetEndPoint(),
  192. lineItem->GetLineWidth() );
  193. if( GetPolyShape().Collide( &lineSeg ) )
  194. {
  195. addContainedItem( areaItem );
  196. }
  197. }
  198. else if( areaItem->IsType(
  199. { SCH_PIN_T, SCH_LABEL_T, SCH_GLOBAL_LABEL_T, SCH_HIER_LABEL_T } ) )
  200. {
  201. std::vector<VECTOR2I> connectionPoints = areaItem->GetConnectionPoints();
  202. assert( connectionPoints.size() == 1 );
  203. if( GetPolyShape().Collide( connectionPoints[0] ) )
  204. {
  205. addContainedItem( areaItem );
  206. }
  207. }
  208. else if( areaItem->IsType( { SCH_SYMBOL_T } ) )
  209. {
  210. addContainedItem( areaItem );
  211. }
  212. }
  213. }
  214. std::unordered_set<SCH_ITEM*> SCH_RULE_AREA::GetPastAndPresentContainedItems() const
  215. {
  216. std::unordered_set<SCH_ITEM*> items = m_items;
  217. for( SCH_ITEM* item : m_prev_items )
  218. items.insert( item );
  219. return items;
  220. }
  221. std::vector<std::pair<SCH_RULE_AREA*, SCH_SCREEN*>>
  222. SCH_RULE_AREA::UpdateRuleAreasInScreens( std::unordered_set<SCH_SCREEN*>& screens,
  223. KIGFX::SCH_VIEW* view )
  224. {
  225. std::vector<std::pair<SCH_RULE_AREA*, SCH_SCREEN*>> forceUpdateRuleAreas;
  226. for( SCH_SCREEN* screen : screens )
  227. {
  228. // First reset all item caches - must be done first to ensure two rule areas
  229. // on the same item don't overwrite each other's caches
  230. for( SCH_ITEM* ruleAreaAsItem : screen->Items().OfType( SCH_RULE_AREA_T ) )
  231. {
  232. SCH_RULE_AREA* ruleArea = static_cast<SCH_RULE_AREA*>( ruleAreaAsItem );
  233. ruleArea->ResetCaches( view );
  234. }
  235. // Secondly refresh the contained items
  236. for( SCH_ITEM* ruleAreaAsItem : screen->Items().OfType( SCH_RULE_AREA_T ) )
  237. {
  238. SCH_RULE_AREA* ruleArea = static_cast<SCH_RULE_AREA*>( ruleAreaAsItem );
  239. ruleArea->RefreshContainedItemsAndDirectives( screen, view, forceUpdateRuleAreas );
  240. }
  241. }
  242. return forceUpdateRuleAreas;
  243. }
  244. const std::unordered_set<SCH_ITEM*>& SCH_RULE_AREA::GetContainedItems() const
  245. {
  246. return m_items;
  247. }
  248. const std::unordered_set<SCH_DIRECTIVE_LABEL*>& SCH_RULE_AREA::GetDirectives() const
  249. {
  250. return m_directives;
  251. }
  252. const std::vector<std::pair<wxString, SCH_ITEM*>> SCH_RULE_AREA::GetResolvedNetclasses() const
  253. {
  254. std::vector<std::pair<wxString, SCH_ITEM*>> resolvedNetclasses;
  255. for( SCH_DIRECTIVE_LABEL* directive : m_directives )
  256. {
  257. directive->RunOnChildren(
  258. [&]( SCH_ITEM* aChild )
  259. {
  260. if( aChild->Type() == SCH_FIELD_T )
  261. {
  262. SCH_FIELD* field = static_cast<SCH_FIELD*>( aChild );
  263. if( field->GetCanonicalName() == wxT( "Netclass" ) )
  264. {
  265. wxString netclass = field->GetText();
  266. if( netclass != wxEmptyString )
  267. resolvedNetclasses.push_back( { netclass, directive } );
  268. }
  269. }
  270. return true;
  271. } );
  272. }
  273. return resolvedNetclasses;
  274. }
  275. void SCH_RULE_AREA::ResetDirectivesAndItems( KIGFX::SCH_VIEW* view )
  276. {
  277. for( SCH_DIRECTIVE_LABEL* label : m_directives )
  278. {
  279. label->ClearConnectedRuleAreas();
  280. view->Update( label, KIGFX::REPAINT );
  281. }
  282. for( SCH_ITEM* item : m_items )
  283. item->ClearRuleAreasCache();
  284. }
  285. void SCH_RULE_AREA::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  286. {
  287. aList.emplace_back( _( "Rule Area" ), wxEmptyString );
  288. wxString msg;
  289. msg.Printf( wxS( "%d" ), GetPolyShape().Outline( 0 ).PointCount() );
  290. aList.emplace_back( _( "Points" ), msg );
  291. m_stroke.GetMsgPanelInfo( aFrame, aList );
  292. const std::vector<std::pair<wxString, SCH_ITEM*>> netclasses =
  293. SCH_RULE_AREA::GetResolvedNetclasses();
  294. wxString resolvedNetclass = _( "<None>" );
  295. if( netclasses.size() > 0 )
  296. resolvedNetclass = netclasses[0].first;
  297. aList.emplace_back( _( "Resolved netclass" ), resolvedNetclass );
  298. }
  299. void SCH_RULE_AREA::addDirective( SCH_DIRECTIVE_LABEL* label, KIGFX::SCH_VIEW* view )
  300. {
  301. label->AddConnectedRuleArea( this );
  302. m_directives.insert( label );
  303. if( view )
  304. view->Update( label, KIGFX::REPAINT );
  305. }
  306. void SCH_RULE_AREA::clearDirectives( KIGFX::SCH_VIEW* view )
  307. {
  308. for( SCH_DIRECTIVE_LABEL* label : m_directives )
  309. {
  310. label->ClearConnectedRuleAreas();
  311. if( view )
  312. view->Update( label, KIGFX::REPAINT );
  313. }
  314. m_directives.clear();
  315. }
  316. void SCH_RULE_AREA::addContainedItem( SCH_ITEM* item )
  317. {
  318. item->AddRuleAreaToCache( this );
  319. m_items.insert( item );
  320. }
  321. void SCH_RULE_AREA::clearContainedItems()
  322. {
  323. for( SCH_ITEM* item : m_items )
  324. item->ClearRuleAreasCache();
  325. m_items.clear();
  326. }
  327. static struct SCH_RULE_AREA_DESC
  328. {
  329. SCH_RULE_AREA_DESC()
  330. {
  331. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  332. REGISTER_TYPE( SCH_RULE_AREA );
  333. propMgr.AddTypeCast( new TYPE_CAST<SCH_RULE_AREA, SCH_SHAPE> );
  334. propMgr.AddTypeCast( new TYPE_CAST<SCH_RULE_AREA, SCH_ITEM> );
  335. propMgr.AddTypeCast( new TYPE_CAST<SCH_RULE_AREA, EDA_SHAPE> );
  336. propMgr.InheritsAfter( TYPE_HASH( SCH_RULE_AREA ), TYPE_HASH( SCH_SHAPE ) );
  337. propMgr.InheritsAfter( TYPE_HASH( SCH_RULE_AREA ), TYPE_HASH( SCH_ITEM ) );
  338. propMgr.InheritsAfter( TYPE_HASH( SCH_RULE_AREA ), TYPE_HASH( EDA_SHAPE ) );
  339. }
  340. } _SCH_RULE_AREA_DESC;