KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_rule_area.cpp
Go to the documentation of this file.
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
24#include <algorithm>
25#include <iterator>
26#include <map>
27#include <vector>
28
29#include <eda_draw_frame.h>
30#include <erc/erc_item.h>
31#include <erc/erc_settings.h>
34#include <sch_line.h>
35#include <sch_marker.h>
36#include <sch_rtree.h>
37#include <sch_rule_area.h>
38#include <sch_screen.h>
39#include <sch_sheet_path.h>
40
41
43{
44 return wxT( "SCH_RULE_AREA" );
45}
46
47
49{
50 return _( "Rule Area" );
51}
52
53
55{
56 return new SCH_RULE_AREA( *this );
57}
58
59
60void SCH_RULE_AREA::ViewGetLayers( int aLayers[], int& aCount ) const
61{
62 aCount = 3;
63 aLayers[0] = LAYER_RULE_AREAS;
64 aLayers[1] = LAYER_NOTES_BACKGROUND;
65 aLayers[2] = LAYER_SELECTION_SHADOWS;
66}
67
68
69std::vector<SHAPE*> SCH_RULE_AREA::MakeEffectiveShapes( bool aEdgeOnly ) const
70{
71 std::vector<SHAPE*> effectiveShapes;
72 int width = GetEffectiveWidth();
73
74 switch( m_shape )
75 {
76 case SHAPE_T::POLY:
77 {
78 if( GetPolyShape().OutlineCount() == 0 ) // malformed/empty polygon
79 break;
80
81 for( int ii = 0; ii < GetPolyShape().OutlineCount(); ++ii )
82 {
83 const SHAPE_LINE_CHAIN& l = GetPolyShape().COutline( ii );
84
85 if( IsFilled() && !aEdgeOnly )
86 effectiveShapes.emplace_back( new SHAPE_SIMPLE( l ) );
87
88 if( width > 0 || !IsFilled() || aEdgeOnly )
89 {
90 int segCount = l.SegmentCount();
91
92 for( int jj = 0; jj < segCount; jj++ )
93 effectiveShapes.emplace_back( new SHAPE_SEGMENT( l.CSegment( jj ), width ) );
94 }
95 }
96 }
97 break;
98
99 default:
100 return SCH_SHAPE::MakeEffectiveShapes( aEdgeOnly );
101 break;
102 }
103
104 return effectiveShapes;
105}
106
107
108void SCH_RULE_AREA::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
109 int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
110{
111 if( IsPrivate() )
112 return;
113
114 SCH_RENDER_SETTINGS* renderSettings = getRenderSettings( aPlotter );
115 int pen_size = GetEffectivePenWidth( renderSettings );
116
117 if( GetShape() != SHAPE_T::POLY )
118 SCH_SHAPE::Plot( aPlotter, aBackground, aPlotOpts, aUnit, aBodyStyle, aOffset, aDimmed );
119
120 static std::vector<VECTOR2I> ptList;
121
122 ptList.clear();
123
124 const std::vector<VECTOR2I>& polyPoints = m_poly.Outline( 0 ).CPoints();
125
126 for( const VECTOR2I& pt : polyPoints )
127 {
128 ptList.push_back( pt );
129 }
130
131 ptList.push_back( polyPoints[0] );
132
134 COLOR4D bg = renderSettings->GetBackgroundColor();
135 LINE_STYLE lineStyle = GetStroke().GetLineStyle();
136 FILL_T fill = m_fill;
137
138 if( aBackground )
139 {
140 if( !aPlotter->GetColorMode() )
141 return;
142
143 switch( m_fill )
144 {
145 case FILL_T::FILLED_SHAPE:
146 return;
147
148 case FILL_T::FILLED_WITH_COLOR:
150 break;
151
152 case FILL_T::FILLED_WITH_BG_BODYCOLOR:
153 color = renderSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
154 break;
155
156 default:
157 return;
158 }
159
160 pen_size = 0;
161 lineStyle = LINE_STYLE::SOLID;
162 }
163 else /* if( aForeground ) */
164 {
165 if( !aPlotter->GetColorMode() || color == COLOR4D::UNSPECIFIED )
166 color = renderSettings->GetLayerColor( m_layer );
167
168 if( lineStyle == LINE_STYLE::DEFAULT )
169 lineStyle = LINE_STYLE::SOLID;
170
171 if( m_fill == FILL_T::FILLED_SHAPE )
172 fill = m_fill;
173 else
174 fill = FILL_T::NO_FILL;
175
176 pen_size = GetEffectivePenWidth( renderSettings );
177 }
178
179 if( bg == COLOR4D::UNSPECIFIED || !aPlotter->GetColorMode() )
180 bg = COLOR4D::WHITE;
181
182 if( aDimmed )
183 {
184 color.Desaturate();
185 color = color.Mix( bg, 0.5f );
186 }
187
188 aPlotter->SetColor( color );
189 aPlotter->SetCurrentLineWidth( pen_size );
190 aPlotter->SetDash( pen_size, lineStyle );
191
192 aPlotter->PlotPoly( ptList, fill, pen_size );
193
194 aPlotter->SetDash( pen_size, LINE_STYLE::SOLID );
195}
196
197
198wxString SCH_RULE_AREA::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
199{
200 return _( "Schematic rule area" );
201}
202
203
205{
206 // Save the current state
209
210 // Reset the rule area
212 clearDirectives( view );
213}
214
215
217 SCH_SCREEN* screen, KIGFX::SCH_VIEW* view,
218 std::vector<std::pair<SCH_RULE_AREA*, SCH_SCREEN*>>& forceUpdateRuleAreas )
219{
220 EE_RTREE& items = screen->Items();
222
223 // Get any SCH_DIRECTIVE_LABELs which are attached to the rule area border
224 std::unordered_set<SCH_DIRECTIVE_LABEL*> attachedDirectives;
225 EE_RTREE::EE_TYPE candidateDirectives = items.Overlapping( SCH_DIRECTIVE_LABEL_T, boundingBox );
226
227 for( SCH_ITEM* candidateDirective : candidateDirectives )
228 {
229 SCH_DIRECTIVE_LABEL* label = static_cast<SCH_DIRECTIVE_LABEL*>( candidateDirective );
230 const std::vector<VECTOR2I> labelConnectionPoints = label->GetConnectionPoints();
231 assert( labelConnectionPoints.size() == 1 );
232
233 if( GetPolyShape().CollideEdge( labelConnectionPoints[0], nullptr, 5 ) )
234 {
235 addDirective( label, view );
236 }
237 }
238
239 // If directives have changed, we need to force an update of the contained items connectivity
241 forceUpdateRuleAreas.push_back( { this, screen } );
242
243 // Next find any connectable items which lie within the rule area
244 EE_RTREE::EE_TYPE ruleAreaItems = items.Overlapping( boundingBox );
245
246 for( SCH_ITEM* areaItem : ruleAreaItems )
247 {
248 if( areaItem->IsType( { SCH_ITEM_LOCATE_WIRE_T, SCH_ITEM_LOCATE_BUS_T } ) )
249 {
250 SCH_LINE* lineItem = static_cast<SCH_LINE*>( areaItem );
251 SHAPE_SEGMENT lineSeg( lineItem->GetStartPoint(), lineItem->GetEndPoint(),
252 lineItem->GetLineWidth() );
253
254 if( GetPolyShape().Collide( &lineSeg ) )
255 {
256 addContainedItem( areaItem );
257 }
258 }
259 else if( areaItem->IsType(
260 { SCH_PIN_T, SCH_LABEL_T, SCH_GLOBAL_LABEL_T, SCH_HIER_LABEL_T } ) )
261 {
262 std::vector<VECTOR2I> connectionPoints = areaItem->GetConnectionPoints();
263 assert( connectionPoints.size() == 1 );
264
265 if( GetPolyShape().Collide( connectionPoints[0] ) )
266 {
267 addContainedItem( areaItem );
268 }
269 }
270 else if( areaItem->IsType( { SCH_SYMBOL_T } ) )
271 {
272 addContainedItem( areaItem );
273 }
274 }
275}
276
277
278std::unordered_set<SCH_ITEM*> SCH_RULE_AREA::GetPastAndPresentContainedItems() const
279{
280 std::unordered_set<SCH_ITEM*> items = m_items;
281
282 for( SCH_ITEM* item : m_prev_items )
283 items.insert( item );
284
285 return items;
286}
287
288
289std::vector<std::pair<SCH_RULE_AREA*, SCH_SCREEN*>>
290SCH_RULE_AREA::UpdateRuleAreasInScreens( std::unordered_set<SCH_SCREEN*>& screens,
291 KIGFX::SCH_VIEW* view )
292{
293 std::vector<std::pair<SCH_RULE_AREA*, SCH_SCREEN*>> forceUpdateRuleAreas;
294
295 for( SCH_SCREEN* screen : screens )
296 {
297 // First reset all item caches - must be done first to ensure two rule areas
298 // on the same item don't overwrite each other's caches
299 for( SCH_ITEM* ruleAreaAsItem : screen->Items().OfType( SCH_RULE_AREA_T ) )
300 {
301 SCH_RULE_AREA* ruleArea = static_cast<SCH_RULE_AREA*>( ruleAreaAsItem );
302 ruleArea->ResetCaches( view );
303 }
304
305 // Secondly refresh the contained items
306 for( SCH_ITEM* ruleAreaAsItem : screen->Items().OfType( SCH_RULE_AREA_T ) )
307 {
308 SCH_RULE_AREA* ruleArea = static_cast<SCH_RULE_AREA*>( ruleAreaAsItem );
309 ruleArea->RefreshContainedItemsAndDirectives( screen, view, forceUpdateRuleAreas );
310 }
311 }
312
313 return forceUpdateRuleAreas;
314}
315
316
317const std::unordered_set<SCH_ITEM*>& SCH_RULE_AREA::GetContainedItems() const
318{
319 return m_items;
320}
321
322
323const std::unordered_set<SCH_DIRECTIVE_LABEL*>& SCH_RULE_AREA::GetDirectives() const
324{
325 return m_directives;
326}
327
328
329const std::vector<std::pair<wxString, SCH_ITEM*>> SCH_RULE_AREA::GetResolvedNetclasses() const
330{
331 std::vector<std::pair<wxString, SCH_ITEM*>> resolvedNetclasses;
332
333 for( SCH_DIRECTIVE_LABEL* directive : m_directives )
334 {
335 directive->RunOnChildren(
336 [&]( SCH_ITEM* aChild )
337 {
338 if( aChild->Type() == SCH_FIELD_T )
339 {
340 SCH_FIELD* field = static_cast<SCH_FIELD*>( aChild );
341
342 if( field->GetCanonicalName() == wxT( "Netclass" ) )
343 {
344 wxString netclass = field->GetText();
345
346 if( netclass != wxEmptyString )
347 resolvedNetclasses.push_back( { netclass, directive } );
348 }
349 }
350
351 return true;
352 } );
353 }
354
355 return resolvedNetclasses;
356}
357
358
360{
361 for( SCH_DIRECTIVE_LABEL* label : m_directives )
362 {
363 label->ClearConnectedRuleAreas();
364 view->Update( label, KIGFX::REPAINT );
365 }
366
367 for( SCH_ITEM* item : m_items )
368 item->ClearRuleAreasCache();
369}
370
371
372void SCH_RULE_AREA::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
373{
374 aList.emplace_back( _( "Rule Area" ), wxEmptyString );
375
376 wxString msg;
377 msg.Printf( wxS( "%d" ), GetPolyShape().Outline( 0 ).PointCount() );
378 aList.emplace_back( _( "Points" ), msg );
379
380 m_stroke.GetMsgPanelInfo( aFrame, aList );
381
382 const std::vector<std::pair<wxString, SCH_ITEM*>> netclasses =
384 wxString resolvedNetclass = _( "<None>" );
385
386 if( netclasses.size() > 0 )
387 resolvedNetclass = netclasses[0].first;
388
389 aList.emplace_back( _( "Resolved netclass" ), resolvedNetclass );
390}
391
392
394{
395 label->AddConnectedRuleArea( this );
396 m_directives.insert( label );
397
398 if( view )
399 view->Update( label, KIGFX::REPAINT );
400}
401
402
404{
405 for( SCH_DIRECTIVE_LABEL* label : m_directives )
406 {
407 label->ClearConnectedRuleAreas();
408
409 if( view )
410 view->Update( label, KIGFX::REPAINT );
411 }
412
413 m_directives.clear();
414}
415
416
418{
419 item->AddRuleAreaToCache( this );
420 m_items.insert( item );
421}
422
423
425{
426 for( SCH_ITEM* item : m_items )
427 item->ClearRuleAreasCache();
428
429 m_items.clear();
430}
431
432
434{
436 {
445 }
int color
Definition: DXF_plotter.cpp:58
The base class for create windows for drawing purpose.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
SHAPE_T m_shape
Definition: eda_shape.h:414
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:279
bool IsFilled() const
Definition: eda_shape.h:91
SHAPE_T GetShape() const
Definition: eda_shape.h:125
COLOR4D GetFillColor() const
Definition: eda_shape.h:111
SHAPE_POLY_SET m_poly
Definition: eda_shape.h:435
STROKE_PARAMS m_stroke
Definition: eda_shape.h:415
FILL_T m_fill
Definition: eda_shape.h:416
Implements an R-tree for fast spatial and type indexing of schematic items.
Definition: sch_rtree.h:40
EE_TYPE Overlapping(const BOX2I &aRect) const
Definition: sch_rtree.h:243
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: view.cpp:1687
Base plotter engine class.
Definition: plotter.h:105
virtual void SetDash(int aLineWidth, LINE_STYLE aLineStyle)=0
bool GetColorMode() const
Definition: plotter.h:133
virtual void SetCurrentLineWidth(int width, void *aData=nullptr)=0
Set the line width for the next drawing.
virtual void PlotPoly(const std::vector< VECTOR2I > &aCornerList, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=nullptr)=0
Draw a polygon ( filled or not ).
virtual void SetColor(const COLOR4D &color)=0
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:85
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:87
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
void AddConnectedRuleArea(SCH_RULE_AREA *aRuleArea)
Adds an entry to the connected rule area cache.
Definition: sch_label.cpp:1765
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
SCH_RENDER_SETTINGS * getRenderSettings(PLOTTER *aPlotter) const
Definition: sch_item.h:670
bool IsPrivate() const
Definition: sch_item.h:235
void AddRuleAreaToCache(SCH_RULE_AREA *aRuleArea)
Adds a rule area to the item's cache.
Definition: sch_item.h:633
int GetEffectivePenWidth(const SCH_RENDER_SETTINGS *aSettings) const
Definition: sch_item.cpp:475
SCH_LAYER_ID m_layer
Definition: sch_item.h:723
std::vector< VECTOR2I > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
Definition: sch_label.cpp:896
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:41
VECTOR2I GetEndPoint() const
Definition: sch_line.h:141
VECTOR2I GetStartPoint() const
Definition: sch_line.h:136
int GetLineWidth() const
Definition: sch_line.h:181
const KIGFX::COLOR4D & GetBackgroundColor() const override
Return current background color settings.
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
virtual std::vector< SHAPE * > MakeEffectiveShapes(bool aEdgeOnly=false) const override
Make a set of SHAPE objects representing the EDA_SHAPE.
const std::unordered_set< SCH_ITEM * > & GetContainedItems() const
Returns a set of all items contained within the rule area.
void ResetDirectivesAndItems(KIGFX::SCH_VIEW *view)
Clears and resets items and directives attached to this rule area.
void ResetCaches(KIGFX::SCH_VIEW *view)
Resets all item and directive caches, saving the current state first.
void addContainedItem(SCH_ITEM *item)
@briefs Adds an item to the list of items which this rule area affects
void addDirective(SCH_DIRECTIVE_LABEL *label, KIGFX::SCH_VIEW *view)
Adds a directive label which applies to items within ths rule area.
const std::unordered_set< SCH_DIRECTIVE_LABEL * > & GetDirectives() const
Returns the set of all directive labels attached to the rule area border.
wxString GetFriendlyName() const override
virtual void Plot(PLOTTER *aPlotter, bool aBackground, const SCH_PLOT_OPTS &aPlotOpts, int aUnit, int aBodyStyle, const VECTOR2I &aOffset, bool aDimmed) override
Plot the item to aPlotter.
std::unordered_set< SCH_DIRECTIVE_LABEL * > m_directives
All SCH_DIRECTIVE_LABELs attached to the rule area border.
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
std::unordered_set< SCH_ITEM * > m_prev_items
All SCH_ITEMs contained or intersecting the rule area in the previous update.
void RefreshContainedItemsAndDirectives(SCH_SCREEN *screen, KIGFX::SCH_VIEW *view, std::vector< std::pair< SCH_RULE_AREA *, SCH_SCREEN * > > &forceUpdateRuleAreas)
Refreshes the list of items which this rule area affects.
std::unordered_set< SCH_DIRECTIVE_LABEL * > m_prev_directives
All SCH_DIRECTIVE_LABELs attached to the rule area border in the previous update.
std::unordered_set< SCH_ITEM * > GetPastAndPresentContainedItems() const
Fetches all items which were, or are, within the rule area.
static std::vector< std::pair< SCH_RULE_AREA *, SCH_SCREEN * > > UpdateRuleAreasInScreens(std::unordered_set< SCH_SCREEN * > &screens, KIGFX::SCH_VIEW *view)
Updates all rule area connectvity / caches in the given sheet paths.
std::unordered_set< SCH_ITEM * > m_items
All SCH_ITEMs currently contained or intersecting the rule area.
const std::vector< std::pair< wxString, SCH_ITEM * > > GetResolvedNetclasses() const
Resolves the netclass of this rule area from connected directive labels.
void clearContainedItems()
Clears the list of items which this rule area affects.
void ViewGetLayers(int aLayers[], int &aCount) const override
Return the layers the item is drawn on (which may be more than its "home" layer)
void clearDirectives(KIGFX::SCH_VIEW *view)
Clears the list of directives.
wxString GetClass() const override
Return the class name.
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Gets the message panel info for the rule area.
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:108
std::vector< SHAPE * > MakeEffectiveShapes(bool aEdgeOnly=false) const override
Make a set of SHAPE objects representing the SCH_SHAPE.
Definition: sch_shape.h:97
void Plot(PLOTTER *aPlotter, bool aBackground, const SCH_PLOT_OPTS &aPlotOpts, int aUnit, int aBodyStyle, const VECTOR2I &aOffset, bool aDimmed) override
Plot the item to aPlotter.
Definition: sch_shape.cpp:129
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: sch_shape.cpp:265
STROKE_PARAMS GetStroke() const override
Definition: sch_shape.h:55
int GetEffectiveWidth() const override
Definition: sch_shape.cpp:247
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int SegmentCount() const
Return the number of segments in this line chain.
const SEG CSegment(int aIndex) const
Return a constant copy of the aIndex segment in the line chain.
const std::vector< VECTOR2I > & CPoints() const
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int OutlineCount() const
Return the number of outlines in the set.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
Represent a simple polygon consisting of a zero-thickness closed chain of connected line segments.
Definition: shape_simple.h:42
void GetMsgPanelInfo(UNITS_PROVIDER *aUnitsProvider, std::vector< MSG_PANEL_ITEM > &aList, bool aIncludeStyle=true, bool aIncludeWidth=true)
LINE_STYLE GetLineStyle() const
Definition: stroke_params.h:92
KIGFX::COLOR4D GetColor() const
Definition: stroke_params.h:95
#define _(s)
FILL_T
Definition: eda_shape.h:55
@ LAYER_RULE_AREAS
Definition: layer_ids.h:369
@ LAYER_DEVICE_BACKGROUND
Definition: layer_ids.h:387
@ LAYER_NOTES_BACKGROUND
Definition: layer_ids.h:373
@ LAYER_SELECTION_SHADOWS
Definition: layer_ids.h:397
@ REPAINT
Item needs to be redrawn.
Definition: view_item.h:57
#define TYPE_HASH(x)
Definition: property.h:71
#define REGISTER_TYPE(x)
Definition: property_mgr.h:371
static struct SCH_RULE_AREA_DESC _SCH_RULE_AREA_DESC
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
static bool Collide(const SHAPE_CIRCLE &aA, const SHAPE_CIRCLE &aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
BOX2I boundingBox(T aObject, int aLayer)
Used by SHAPE_INDEX to get the bounding box of a generic T object.
Definition: shape_index.h:62
LINE_STYLE
Dashed line types.
Definition: stroke_params.h:46
The EE_TYPE struct provides a type-specific auto-range iterator to the RTree.
Definition: sch_rtree.h:192
@ SCH_FIELD_T
Definition: typeinfo.h:150
@ SCH_DIRECTIVE_LABEL_T
Definition: typeinfo.h:171
@ SCH_RULE_AREA_T
Definition: typeinfo.h:170