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
60std::vector<int> SCH_RULE_AREA::ViewGetLayers() const
61{
63}
64
65
66std::vector<SHAPE*> SCH_RULE_AREA::MakeEffectiveShapes( bool aEdgeOnly ) const
67{
68 std::vector<SHAPE*> effectiveShapes;
69 int width = GetEffectiveWidth();
70
71 switch( m_shape )
72 {
73 case SHAPE_T::POLY:
74 {
75 if( GetPolyShape().OutlineCount() == 0 ) // malformed/empty polygon
76 break;
77
78 for( int ii = 0; ii < GetPolyShape().OutlineCount(); ++ii )
79 {
80 const SHAPE_LINE_CHAIN& l = GetPolyShape().COutline( ii );
81
82 if( IsFilled() && !aEdgeOnly )
83 effectiveShapes.emplace_back( new SHAPE_SIMPLE( l ) );
84
85 if( width > 0 || !IsFilled() || aEdgeOnly )
86 {
87 int segCount = l.SegmentCount();
88
89 for( int jj = 0; jj < segCount; jj++ )
90 effectiveShapes.emplace_back( new SHAPE_SEGMENT( l.CSegment( jj ), width ) );
91 }
92 }
93 }
94 break;
95
96 default:
97 return SCH_SHAPE::MakeEffectiveShapes( aEdgeOnly );
98 break;
99 }
100
101 return effectiveShapes;
102}
103
104
105void SCH_RULE_AREA::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
106 int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
107{
108 if( IsPrivate() )
109 return;
110
111 SCH_RENDER_SETTINGS* renderSettings = getRenderSettings( aPlotter );
112 int pen_size = GetEffectivePenWidth( renderSettings );
113
114 if( GetShape() != SHAPE_T::POLY )
115 SCH_SHAPE::Plot( aPlotter, aBackground, aPlotOpts, aUnit, aBodyStyle, aOffset, aDimmed );
116
117 static std::vector<VECTOR2I> ptList;
118
119 ptList.clear();
120
121 const std::vector<VECTOR2I>& polyPoints = m_poly.Outline( 0 ).CPoints();
122
123 for( const VECTOR2I& pt : polyPoints )
124 {
125 ptList.push_back( pt );
126 }
127
128 ptList.push_back( polyPoints[0] );
129
131 COLOR4D bg = renderSettings->GetBackgroundColor();
132 LINE_STYLE lineStyle = GetStroke().GetLineStyle();
133 FILL_T fill = m_fill;
134
135 if( aBackground )
136 {
137 if( !aPlotter->GetColorMode() )
138 return;
139
140 switch( m_fill )
141 {
142 case FILL_T::FILLED_SHAPE:
143 return;
144
145 case FILL_T::FILLED_WITH_COLOR:
147 break;
148
149 case FILL_T::FILLED_WITH_BG_BODYCOLOR:
150 color = renderSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
151 break;
152
153 default:
154 return;
155 }
156
157 pen_size = 0;
158 lineStyle = LINE_STYLE::SOLID;
159 }
160 else /* if( aForeground ) */
161 {
162 if( !aPlotter->GetColorMode() || color == COLOR4D::UNSPECIFIED )
163 color = renderSettings->GetLayerColor( m_layer );
164
165 if( lineStyle == LINE_STYLE::DEFAULT )
166 lineStyle = LINE_STYLE::SOLID;
167
168 if( m_fill == FILL_T::FILLED_SHAPE )
169 fill = m_fill;
170 else
171 fill = FILL_T::NO_FILL;
172
173 pen_size = GetEffectivePenWidth( renderSettings );
174 }
175
176 if( bg == COLOR4D::UNSPECIFIED || !aPlotter->GetColorMode() )
177 bg = COLOR4D::WHITE;
178
179 if( aDimmed )
180 {
181 color.Desaturate();
182 color = color.Mix( bg, 0.5f );
183 }
184
185 aPlotter->SetColor( color );
186 aPlotter->SetCurrentLineWidth( pen_size );
187 aPlotter->SetDash( pen_size, lineStyle );
188
189 aPlotter->PlotPoly( ptList, fill, pen_size );
190
191 aPlotter->SetDash( pen_size, LINE_STYLE::SOLID );
192}
193
194
195wxString SCH_RULE_AREA::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
196{
197 return _( "Schematic rule area" );
198}
199
200
202{
203 // Save the current state
206
207 // Reset the rule area
209 clearDirectives( view );
210}
211
212
214 SCH_SCREEN* screen, KIGFX::SCH_VIEW* view,
215 std::vector<std::pair<SCH_RULE_AREA*, SCH_SCREEN*>>& forceUpdateRuleAreas )
216{
217 EE_RTREE& items = screen->Items();
219
220 // Get any SCH_DIRECTIVE_LABELs which are attached to the rule area border
221 std::unordered_set<SCH_DIRECTIVE_LABEL*> attachedDirectives;
222 EE_RTREE::EE_TYPE candidateDirectives = items.Overlapping( SCH_DIRECTIVE_LABEL_T, boundingBox );
223
224 for( SCH_ITEM* candidateDirective : candidateDirectives )
225 {
226 SCH_DIRECTIVE_LABEL* label = static_cast<SCH_DIRECTIVE_LABEL*>( candidateDirective );
227 const std::vector<VECTOR2I> labelConnectionPoints = label->GetConnectionPoints();
228 assert( labelConnectionPoints.size() == 1 );
229
230 if( GetPolyShape().CollideEdge( labelConnectionPoints[0], nullptr, 5 ) )
231 {
232 addDirective( label, view );
233 }
234 }
235
236 // If directives have changed, we need to force an update of the contained items connectivity
238 forceUpdateRuleAreas.push_back( { this, screen } );
239
240 // Next find any connectable items which lie within the rule area
241 EE_RTREE::EE_TYPE ruleAreaItems = items.Overlapping( boundingBox );
242
243 for( SCH_ITEM* areaItem : ruleAreaItems )
244 {
245 if( areaItem->IsType( { SCH_ITEM_LOCATE_WIRE_T, SCH_ITEM_LOCATE_BUS_T } ) )
246 {
247 SCH_LINE* lineItem = static_cast<SCH_LINE*>( areaItem );
248 SHAPE_SEGMENT lineSeg( lineItem->GetStartPoint(), lineItem->GetEndPoint(),
249 lineItem->GetLineWidth() );
250
251 if( GetPolyShape().Collide( &lineSeg ) )
252 {
253 addContainedItem( areaItem );
254 }
255 }
256 else if( areaItem->IsType(
257 { SCH_PIN_T, SCH_LABEL_T, SCH_GLOBAL_LABEL_T, SCH_HIER_LABEL_T } ) )
258 {
259 std::vector<VECTOR2I> connectionPoints = areaItem->GetConnectionPoints();
260 assert( connectionPoints.size() == 1 );
261
262 if( GetPolyShape().Collide( connectionPoints[0] ) )
263 {
264 addContainedItem( areaItem );
265 }
266 }
267 else if( areaItem->IsType( { SCH_SYMBOL_T } ) )
268 {
269 addContainedItem( areaItem );
270 }
271 }
272}
273
274
275std::unordered_set<SCH_ITEM*> SCH_RULE_AREA::GetPastAndPresentContainedItems() const
276{
277 std::unordered_set<SCH_ITEM*> items = m_items;
278
279 for( SCH_ITEM* item : m_prev_items )
280 items.insert( item );
281
282 return items;
283}
284
285
286std::vector<std::pair<SCH_RULE_AREA*, SCH_SCREEN*>>
287SCH_RULE_AREA::UpdateRuleAreasInScreens( std::unordered_set<SCH_SCREEN*>& screens,
288 KIGFX::SCH_VIEW* view )
289{
290 std::vector<std::pair<SCH_RULE_AREA*, SCH_SCREEN*>> forceUpdateRuleAreas;
291
292 for( SCH_SCREEN* screen : screens )
293 {
294 // First reset all item caches - must be done first to ensure two rule areas
295 // on the same item don't overwrite each other's caches
296 for( SCH_ITEM* ruleAreaAsItem : screen->Items().OfType( SCH_RULE_AREA_T ) )
297 {
298 SCH_RULE_AREA* ruleArea = static_cast<SCH_RULE_AREA*>( ruleAreaAsItem );
299 ruleArea->ResetCaches( view );
300 }
301
302 // Secondly refresh the contained items
303 for( SCH_ITEM* ruleAreaAsItem : screen->Items().OfType( SCH_RULE_AREA_T ) )
304 {
305 SCH_RULE_AREA* ruleArea = static_cast<SCH_RULE_AREA*>( ruleAreaAsItem );
306 ruleArea->RefreshContainedItemsAndDirectives( screen, view, forceUpdateRuleAreas );
307 }
308 }
309
310 return forceUpdateRuleAreas;
311}
312
313
314const std::unordered_set<SCH_ITEM*>& SCH_RULE_AREA::GetContainedItems() const
315{
316 return m_items;
317}
318
319
320const std::unordered_set<SCH_DIRECTIVE_LABEL*>& SCH_RULE_AREA::GetDirectives() const
321{
322 return m_directives;
323}
324
325
326const std::vector<std::pair<wxString, SCH_ITEM*>> SCH_RULE_AREA::GetResolvedNetclasses() const
327{
328 std::vector<std::pair<wxString, SCH_ITEM*>> resolvedNetclasses;
329
330 for( SCH_DIRECTIVE_LABEL* directive : m_directives )
331 {
332 directive->RunOnChildren(
333 [&]( SCH_ITEM* aChild )
334 {
335 if( aChild->Type() == SCH_FIELD_T )
336 {
337 SCH_FIELD* field = static_cast<SCH_FIELD*>( aChild );
338
339 if( field->GetCanonicalName() == wxT( "Netclass" ) )
340 {
341 wxString netclass = field->GetText();
342
343 if( netclass != wxEmptyString )
344 resolvedNetclasses.push_back( { netclass, directive } );
345 }
346 }
347
348 return true;
349 } );
350 }
351
352 return resolvedNetclasses;
353}
354
355
357{
358 for( SCH_DIRECTIVE_LABEL* label : m_directives )
359 {
360 label->ClearConnectedRuleAreas();
361 view->Update( label, KIGFX::REPAINT );
362 }
363
364 for( SCH_ITEM* item : m_items )
365 item->ClearRuleAreasCache();
366}
367
368
369void SCH_RULE_AREA::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
370{
371 aList.emplace_back( _( "Rule Area" ), wxEmptyString );
372
373 wxString msg;
374 msg.Printf( wxS( "%d" ), GetPolyShape().Outline( 0 ).PointCount() );
375 aList.emplace_back( _( "Points" ), msg );
376
377 m_stroke.GetMsgPanelInfo( aFrame, aList );
378
379 const std::vector<std::pair<wxString, SCH_ITEM*>> netclasses =
381 wxString resolvedNetclass = _( "<None>" );
382
383 if( netclasses.size() > 0 )
384 resolvedNetclass = netclasses[0].first;
385
386 aList.emplace_back( _( "Resolved netclass" ), resolvedNetclass );
387}
388
389
391{
392 label->AddConnectedRuleArea( this );
393 m_directives.insert( label );
394
395 if( view )
396 view->Update( label, KIGFX::REPAINT );
397}
398
399
401{
402 for( SCH_DIRECTIVE_LABEL* label : m_directives )
403 {
404 label->ClearConnectedRuleAreas();
405
406 if( view )
407 view->Update( label, KIGFX::REPAINT );
408 }
409
410 m_directives.clear();
411}
412
413
415{
416 item->AddRuleAreaToCache( this );
417 m_items.insert( item );
418}
419
420
422{
423 for( SCH_ITEM* item : m_items )
424 item->ClearRuleAreasCache();
425
426 m_items.clear();
427}
428
429
431{
433 {
442 }
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:421
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:286
bool IsFilled() const
Definition: eda_shape.h:98
SHAPE_T GetShape() const
Definition: eda_shape.h:132
COLOR4D GetFillColor() const
Definition: eda_shape.h:118
SHAPE_POLY_SET m_poly
Definition: eda_shape.h:442
STROKE_PARAMS m_stroke
Definition: eda_shape.h:422
FILL_T m_fill
Definition: eda_shape.h:423
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:1669
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:1763
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:473
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.
std::vector< int > ViewGetLayers() const override
Return the layers the item is drawn on (which may be more than its "home" layer)
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 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:56
@ 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