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 (C) 2024 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. void SCH_RULE_AREA::ViewGetLayers( int aLayers[], int& aCount ) const
  51. {
  52. aCount = 3;
  53. aLayers[0] = LAYER_RULE_AREAS;
  54. aLayers[1] = LAYER_NOTES_BACKGROUND;
  55. aLayers[2] = LAYER_SELECTION_SHADOWS;
  56. }
  57. std::vector<SHAPE*> SCH_RULE_AREA::MakeEffectiveShapes( bool aEdgeOnly ) const
  58. {
  59. std::vector<SHAPE*> effectiveShapes;
  60. int width = GetEffectiveWidth();
  61. switch( m_shape )
  62. {
  63. case SHAPE_T::POLY:
  64. {
  65. if( GetPolyShape().OutlineCount() == 0 ) // malformed/empty polygon
  66. break;
  67. for( int ii = 0; ii < GetPolyShape().OutlineCount(); ++ii )
  68. {
  69. const SHAPE_LINE_CHAIN& l = GetPolyShape().COutline( ii );
  70. if( IsFilled() && !aEdgeOnly )
  71. effectiveShapes.emplace_back( new SHAPE_SIMPLE( l ) );
  72. if( width > 0 || !IsFilled() || aEdgeOnly )
  73. {
  74. int segCount = l.SegmentCount();
  75. for( int jj = 0; jj < segCount; jj++ )
  76. effectiveShapes.emplace_back( new SHAPE_SEGMENT( l.CSegment( jj ), width ) );
  77. }
  78. }
  79. }
  80. break;
  81. default:
  82. return SCH_SHAPE::MakeEffectiveShapes( aEdgeOnly );
  83. break;
  84. }
  85. return effectiveShapes;
  86. }
  87. void SCH_RULE_AREA::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
  88. int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
  89. {
  90. if( IsPrivate() )
  91. return;
  92. SCH_RENDER_SETTINGS* renderSettings = getRenderSettings( aPlotter );
  93. int pen_size = GetEffectivePenWidth( renderSettings );
  94. if( GetShape() != SHAPE_T::POLY )
  95. SCH_SHAPE::Plot( aPlotter, aBackground, aPlotOpts, aUnit, aBodyStyle, aOffset, aDimmed );
  96. static std::vector<VECTOR2I> ptList;
  97. ptList.clear();
  98. const std::vector<VECTOR2I>& polyPoints = m_poly.Outline( 0 ).CPoints();
  99. for( const VECTOR2I& pt : polyPoints )
  100. {
  101. ptList.push_back( pt );
  102. }
  103. ptList.push_back( polyPoints[0] );
  104. COLOR4D color = GetStroke().GetColor();
  105. COLOR4D bg = renderSettings->GetBackgroundColor();
  106. LINE_STYLE lineStyle = GetStroke().GetLineStyle();
  107. FILL_T fill = m_fill;
  108. if( aBackground )
  109. {
  110. if( !aPlotter->GetColorMode() )
  111. return;
  112. switch( m_fill )
  113. {
  114. case FILL_T::FILLED_SHAPE:
  115. return;
  116. case FILL_T::FILLED_WITH_COLOR:
  117. color = GetFillColor();
  118. break;
  119. case FILL_T::FILLED_WITH_BG_BODYCOLOR:
  120. color = renderSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
  121. break;
  122. default:
  123. return;
  124. }
  125. pen_size = 0;
  126. lineStyle = LINE_STYLE::SOLID;
  127. }
  128. else /* if( aForeground ) */
  129. {
  130. if( !aPlotter->GetColorMode() || color == COLOR4D::UNSPECIFIED )
  131. color = renderSettings->GetLayerColor( m_layer );
  132. if( lineStyle == LINE_STYLE::DEFAULT )
  133. lineStyle = LINE_STYLE::SOLID;
  134. if( m_fill == FILL_T::FILLED_SHAPE )
  135. fill = m_fill;
  136. else
  137. fill = FILL_T::NO_FILL;
  138. pen_size = GetEffectivePenWidth( renderSettings );
  139. }
  140. if( bg == COLOR4D::UNSPECIFIED || !aPlotter->GetColorMode() )
  141. bg = COLOR4D::WHITE;
  142. if( aDimmed )
  143. {
  144. color.Desaturate();
  145. color = color.Mix( bg, 0.5f );
  146. }
  147. aPlotter->SetColor( color );
  148. aPlotter->SetCurrentLineWidth( pen_size );
  149. aPlotter->SetDash( pen_size, lineStyle );
  150. aPlotter->PlotPoly( ptList, fill, pen_size );
  151. aPlotter->SetDash( pen_size, LINE_STYLE::SOLID );
  152. }
  153. wxString SCH_RULE_AREA::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
  154. {
  155. return _( "Schematic rule area" );
  156. }
  157. void SCH_RULE_AREA::ResetCaches( KIGFX::SCH_VIEW* view )
  158. {
  159. // Save the current state
  160. m_prev_items = m_items;
  161. m_prev_directives = m_directives;
  162. // Reset the rule area
  163. clearContainedItems();
  164. clearDirectives( view );
  165. }
  166. void SCH_RULE_AREA::RefreshContainedItemsAndDirectives(
  167. SCH_SCREEN* screen, KIGFX::SCH_VIEW* view,
  168. std::vector<std::pair<SCH_RULE_AREA*, SCH_SCREEN*>>& forceUpdateRuleAreas )
  169. {
  170. EE_RTREE& items = screen->Items();
  171. const BOX2I boundingBox = GetBoundingBox();
  172. // Get any SCH_DIRECTIVE_LABELs which are attached to the rule area border
  173. std::unordered_set<SCH_DIRECTIVE_LABEL*> attachedDirectives;
  174. EE_RTREE::EE_TYPE candidateDirectives = items.Overlapping( SCH_DIRECTIVE_LABEL_T, boundingBox );
  175. for( SCH_ITEM* candidateDirective : candidateDirectives )
  176. {
  177. SCH_DIRECTIVE_LABEL* label = static_cast<SCH_DIRECTIVE_LABEL*>( candidateDirective );
  178. const std::vector<VECTOR2I> labelConnectionPoints = label->GetConnectionPoints();
  179. assert( labelConnectionPoints.size() == 1 );
  180. if( GetPolyShape().CollideEdge( labelConnectionPoints[0], nullptr, 5 ) )
  181. {
  182. addDirective( label, view );
  183. }
  184. }
  185. // If directives have changed, we need to force an update of the contained items connectivity
  186. if( m_directives != m_prev_directives )
  187. forceUpdateRuleAreas.push_back( { this, screen } );
  188. // Next find any connectable items which lie within the rule area
  189. EE_RTREE::EE_TYPE ruleAreaItems = items.Overlapping( boundingBox );
  190. for( SCH_ITEM* areaItem : ruleAreaItems )
  191. {
  192. if( areaItem->IsType( { SCH_ITEM_LOCATE_WIRE_T, SCH_ITEM_LOCATE_BUS_T } ) )
  193. {
  194. SCH_LINE* lineItem = static_cast<SCH_LINE*>( areaItem );
  195. SHAPE_SEGMENT lineSeg( lineItem->GetStartPoint(), lineItem->GetEndPoint(),
  196. lineItem->GetLineWidth() );
  197. if( GetPolyShape().Collide( &lineSeg ) )
  198. {
  199. addContainedItem( areaItem );
  200. }
  201. }
  202. else if( areaItem->IsType(
  203. { SCH_PIN_T, SCH_LABEL_T, SCH_GLOBAL_LABEL_T, SCH_HIER_LABEL_T } ) )
  204. {
  205. std::vector<VECTOR2I> connectionPoints = areaItem->GetConnectionPoints();
  206. assert( connectionPoints.size() == 1 );
  207. if( GetPolyShape().Collide( connectionPoints[0] ) )
  208. {
  209. addContainedItem( areaItem );
  210. }
  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;