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 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
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#include <geometry/shape_rect.h>
41
42
44{
45 return wxT( "SCH_RULE_AREA" );
46}
47
48
50{
51 return _( "Rule Area" );
52}
53
54
56{
57 return new SCH_RULE_AREA( *this );
58}
59
60
65
66
67std::vector<SHAPE*> SCH_RULE_AREA::MakeEffectiveShapes( bool aEdgeOnly ) const
68{
69 std::vector<SHAPE*> effectiveShapes;
70 int width = GetEffectiveWidth();
71
72 switch( m_shape )
73 {
74 case SHAPE_T::POLY:
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( IsSolidFill() && !aEdgeOnly )
83 effectiveShapes.emplace_back( new SHAPE_SIMPLE( l ) );
84
85 if( width > 0 || !IsSolidFill() || 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 }
99
100 return effectiveShapes;
101}
102
103
104void SCH_RULE_AREA::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
105 int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
106{
107 if( IsPrivate() )
108 return;
109
110 SCH_RENDER_SETTINGS* renderSettings = getRenderSettings( aPlotter );
111 int pen_size = GetEffectivePenWidth( renderSettings );
112
113 if( GetShape() != SHAPE_T::POLY )
114 SCH_SHAPE::Plot( aPlotter, aBackground, aPlotOpts, aUnit, aBodyStyle, aOffset, aDimmed );
115
116 static std::vector<VECTOR2I> ptList;
117
118 ptList.clear();
119
120 const std::vector<VECTOR2I>& polyPoints = m_poly.Outline( 0 ).CPoints();
121
122 for( const VECTOR2I& pt : polyPoints )
123 ptList.push_back( pt );
124
125 ptList.push_back( polyPoints[0] );
126
127 COLOR4D color = GetStroke().GetColor();
128 COLOR4D bg = renderSettings->GetBackgroundColor();
129 LINE_STYLE lineStyle = GetStroke().GetLineStyle();
130 FILL_T fill = m_fill;
131
132 if( aBackground )
133 {
134 if( !aPlotter->GetColorMode() )
135 return;
136
137 switch( m_fill )
138 {
140 return;
141
143 color = GetFillColor();
144 break;
145
147 color = renderSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
148 break;
149
150 default:
151 return;
152 }
153
154 pen_size = 0;
155 lineStyle = LINE_STYLE::SOLID;
156 }
157 else /* if( aForeground ) */
158 {
159 if( !aPlotter->GetColorMode() || color == COLOR4D::UNSPECIFIED )
160 color = renderSettings->GetLayerColor( m_layer );
161
162 if( lineStyle == LINE_STYLE::DEFAULT )
163 lineStyle = LINE_STYLE::SOLID;
164
166 fill = m_fill;
167 else
168 fill = FILL_T::NO_FILL;
169
170 pen_size = GetEffectivePenWidth( renderSettings );
171 }
172
173 if( bg == COLOR4D::UNSPECIFIED || !aPlotter->GetColorMode() )
174 bg = COLOR4D::WHITE;
175
176 if( aDimmed )
177 {
178 color.Desaturate();
179 color = color.Mix( bg, 0.5f );
180 }
181
182 aPlotter->SetColor( color );
183 aPlotter->SetCurrentLineWidth( pen_size );
184 aPlotter->SetDash( pen_size, lineStyle );
185
186 aPlotter->PlotPoly( ptList, fill, pen_size, nullptr );
187
188 aPlotter->SetDash( pen_size, LINE_STYLE::SOLID );
189}
190
191
192wxString SCH_RULE_AREA::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
193{
194 return _( "Schematic rule area" );
195}
196
197
199{
200 // Save the current state
203
204 // Reset the rule area
205 // Do NOT assume these pointers are valid.
206 m_items.clear();
207 m_itemIDs.clear();
208 m_directives.clear();
209 m_directiveIDs.clear();
210}
211
212
214{
215 EE_RTREE& items = screen->Items();
217
218 // Get any SCH_DIRECTIVE_LABELs which are attached to the rule area border
219 std::unordered_set<SCH_DIRECTIVE_LABEL*> attachedDirectives;
220 EE_RTREE::EE_TYPE candidateDirectives = items.Overlapping( SCH_DIRECTIVE_LABEL_T, boundingBox );
221
222 for( SCH_ITEM* candidateDirective : candidateDirectives )
223 {
224 SCH_DIRECTIVE_LABEL* label = static_cast<SCH_DIRECTIVE_LABEL*>( candidateDirective );
225 const std::vector<VECTOR2I> labelConnectionPoints = label->GetConnectionPoints();
226 assert( labelConnectionPoints.size() == 1 );
227
228 if( GetPolyShape().CollideEdge( labelConnectionPoints[0], nullptr, 5 ) )
229 addDirective( label );
230 }
231
232 // Next find any connectable items which lie within the rule area
233 EE_RTREE::EE_TYPE ruleAreaItems = items.Overlapping( boundingBox );
234
235 for( SCH_ITEM* areaItem : ruleAreaItems )
236 {
237 if( areaItem->IsType( { SCH_ITEM_LOCATE_WIRE_T, SCH_ITEM_LOCATE_BUS_T } ) )
238 {
239 SCH_LINE* lineItem = static_cast<SCH_LINE*>( areaItem );
240 SHAPE_SEGMENT lineSeg( lineItem->GetStartPoint(), lineItem->GetEndPoint(),
241 lineItem->GetLineWidth() );
242
243 if( GetPolyShape().Collide( &lineSeg ) )
244 addContainedItem( areaItem );
245 }
246 else if( areaItem->IsType( { SCH_PIN_T, SCH_LABEL_T, SCH_GLOBAL_LABEL_T, SCH_HIER_LABEL_T } ) )
247 {
248 std::vector<VECTOR2I> connectionPoints = areaItem->GetConnectionPoints();
249 wxASSERT( connectionPoints.size() == 1 );
250
251 if( GetPolyShape().Collide( connectionPoints[0] ) )
252 addContainedItem( areaItem );
253 }
254 else if( areaItem->IsType( { SCH_SYMBOL_T } ) )
255 {
256 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( areaItem );
257 const BOX2I symbolBb = symbol->GetBoundingBox();
258 const SHAPE_RECT rect( symbolBb );
259
260 if( GetPolyShape().Collide( &rect ) )
261 {
262 addContainedItem( areaItem );
263
264 // Add child pins which are within the rule area
265 for( SCH_PIN* pin : symbol->GetPins() )
266 {
267 if( GetPolyShape().Collide( pin->GetPosition() ) )
269 }
270 }
271 }
272 else if( areaItem->IsType( { SCH_SHEET_T } ) )
273 {
274 const BOX2I sheetBb = areaItem->GetBoundingBox();
275 const SHAPE_RECT rect( sheetBb );
276
277 if( GetPolyShape().Collide( &rect ) )
278 {
279 addContainedItem( areaItem );
280 }
281 }
282 }
283}
284
285
286std::vector<std::pair<SCH_RULE_AREA*, SCH_SCREEN*>>
287SCH_RULE_AREA::UpdateRuleAreasInScreens( std::unordered_set<SCH_SCREEN*>& screens, KIGFX::SCH_VIEW* view )
288{
289 std::vector<std::pair<SCH_RULE_AREA*, SCH_SCREEN*>> forceUpdateRuleAreas;
290
291 for( SCH_SCREEN* screen : screens )
292 {
293 // First reset all item caches - must be done first to ensure two rule areas
294 // on the same item don't overwrite each other's caches
295 for( SCH_ITEM* item : screen->Items() )
296 {
297 if( item->Type() == SCH_RULE_AREA_T )
298 static_cast<SCH_RULE_AREA*>( item )->resetCaches();
299
300 if( item->Type() == SCH_DIRECTIVE_LABEL_T && view )
301 view->Update( item, KIGFX::REPAINT );
302
303 item->ClearRuleAreasCache();
304 }
305
306 // Secondly refresh the contained items
307 for( SCH_ITEM* ruleAreaAsItem : screen->Items().OfType( SCH_RULE_AREA_T ) )
308 {
309 SCH_RULE_AREA* ruleArea = static_cast<SCH_RULE_AREA*>( ruleAreaAsItem );
310
311 ruleArea->RefreshContainedItemsAndDirectives( screen );
312
313 if( ruleArea->m_directiveIDs != ruleArea->m_prev_directives )
314 forceUpdateRuleAreas.push_back( { ruleArea, screen } );
315 }
316 }
317
318 return forceUpdateRuleAreas;
319}
320
321
322const std::unordered_set<SCH_ITEM*>& SCH_RULE_AREA::GetContainedItems() const
323{
324 return m_items;
325}
326
327
328const std::unordered_set<SCH_DIRECTIVE_LABEL*>& SCH_RULE_AREA::GetDirectives() const
329{
330 return m_directives;
331}
332
333
334const std::unordered_set<KIID>& SCH_RULE_AREA::GetPastContainedItems() const
335{
336 return m_prev_items;
337}
338
339
340const std::vector<std::pair<wxString, SCH_ITEM*>>
342{
343 std::vector<std::pair<wxString, SCH_ITEM*>> resolvedNetclasses;
344
345 for( SCH_DIRECTIVE_LABEL* directive : m_directives )
346 {
347 directive->RunOnChildren(
348 [&]( SCH_ITEM* aChild )
349 {
350 if( aChild->Type() == SCH_FIELD_T )
351 {
352 SCH_FIELD* field = static_cast<SCH_FIELD*>( aChild );
353
354 if( field->GetCanonicalName() == wxT( "Netclass" ) )
355 {
356 wxString netclass = field->GetShownText( aSheetPath, false );
357
358 if( netclass != wxEmptyString )
359 resolvedNetclasses.push_back( { netclass, directive } );
360 }
361 }
362
363 return true;
364 },
366 }
367
368 return resolvedNetclasses;
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 = SCH_RULE_AREA::GetResolvedNetclasses( nullptr );
383 wxString resolvedNetclass = _( "<None>" );
384
385 if( netclasses.size() > 0 )
386 resolvedNetclass = netclasses[0].first;
387
388 aList.emplace_back( _( "Resolved netclass" ), resolvedNetclass );
389}
390
391
393{
394 label->AddConnectedRuleArea( this );
395 m_directives.insert( label );
396 m_directiveIDs.insert( label->m_Uuid );
397}
398
399
401{
402 item->AddRuleAreaToCache( this );
403 m_items.insert( item );
404 m_itemIDs.insert( item->m_Uuid );
405}
406
407
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
static const COLOR4D WHITE
Definition color4d.h:402
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:399
The base class for create windows for drawing purpose.
const KIID m_Uuid
Definition eda_item.h:516
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:39
SHAPE_T m_shape
Definition eda_shape.h:493
SHAPE_POLY_SET & GetPolyShape()
Definition eda_shape.h:336
SHAPE_T GetShape() const
Definition eda_shape.h:168
bool IsSolidFill() const
Definition eda_shape.h:117
COLOR4D GetFillColor() const
Definition eda_shape.h:152
SHAPE_POLY_SET m_poly
Definition eda_shape.h:515
STROKE_PARAMS m_stroke
Definition eda_shape.h:494
FILL_T m_fill
Definition eda_shape.h:495
Implement 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:246
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
COLOR4D & Desaturate()
Removes color (in HSL model)
Definition color4d.cpp:511
COLOR4D Mix(const COLOR4D &aColor, double aFactor) const
Return a color that is mixed with the input by a factor.
Definition color4d.h:296
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
void Update(const KIGFX::VIEW_ITEM *aItem, int aUpdateFlags) const override
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition sch_view.cpp:60
Base plotter engine class.
Definition plotter.h:136
virtual void SetDash(int aLineWidth, LINE_STYLE aLineStyle)=0
bool GetColorMode() const
Definition plotter.h:164
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, void *aData)=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.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
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.
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:167
SCH_RENDER_SETTINGS * getRenderSettings(PLOTTER *aPlotter) const
Definition sch_item.h:702
bool IsPrivate() const
Definition sch_item.h:253
void AddRuleAreaToCache(SCH_RULE_AREA *aRuleArea)
Add a rule area to the item's cache.
Definition sch_item.h:654
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType, int aUnit=0, int aBodyStyle=0)
Definition sch_item.cpp:51
int GetEffectivePenWidth(const SCH_RENDER_SETTINGS *aSettings) const
Definition sch_item.cpp:647
SCH_LAYER_ID m_layer
Definition sch_item.h:753
std::vector< VECTOR2I > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:42
VECTOR2I GetEndPoint() const
Definition sch_line.h:148
VECTOR2I GetStartPoint() const
Definition sch_line.h:139
int GetLineWidth() const
Definition sch_line.h:198
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.
std::unordered_set< KIID > m_directiveIDs
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
Return a set of all items contained within the rule area.
void addContainedItem(SCH_ITEM *item)
Add an item to the list of items which this rule area affects.
std::unordered_set< KIID > m_prev_items
All SCH_ITEM objectss contained or intersecting the rule area in the previous update.
std::unordered_set< KIID > m_prev_directives
All SCH_DIRECTIVE_LABEL objects attached to the rule area border in the previous update.
void RefreshContainedItemsAndDirectives(SCH_SCREEN *screen)
Refresh the list of items which this rule area affects.
void resetCaches()
Reset all item and directive caches, saving the current state first.
const std::unordered_set< SCH_DIRECTIVE_LABEL * > & GetDirectives() const
Return the set of all directive labels attached to the rule area border.
void addDirective(SCH_DIRECTIVE_LABEL *label)
Add a directive label which applies to items within ths rule area.
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_LABEL objects attached to the rule area border. No ownership.
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
const std::vector< std::pair< wxString, SCH_ITEM * > > GetResolvedNetclasses(const SCH_SHEET_PATH *aSheetPath) const
Resolve the netclass of this rule area from connected directive labels.
static std::vector< std::pair< SCH_RULE_AREA *, SCH_SCREEN * > > UpdateRuleAreasInScreens(std::unordered_set< SCH_SCREEN * > &screens, KIGFX::SCH_VIEW *view)
Update all rule area connectvity / caches in the given sheet paths.
std::unordered_set< SCH_ITEM * > m_items
All SCH_ITEM objects currently contained or intersecting the rule area. No ownership.
const std::unordered_set< KIID > & GetPastContainedItems() const
std::unordered_set< KIID > m_itemIDs
wxString GetClass() const override
Return the class name.
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Get the message panel info for the rule area.
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:117
std::vector< SHAPE * > MakeEffectiveShapes(bool aEdgeOnly=false) const override
Make a set of SHAPE objects representing the SCH_SHAPE.
Definition sch_shape.h:112
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.
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
STROKE_PARAMS GetStroke() const override
Definition sch_shape.h:58
int GetEffectiveWidth() const override
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Schematic symbol object.
Definition sch_symbol.h:76
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
std::vector< SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
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.
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.
LINE_STYLE GetLineStyle() const
KIGFX::COLOR4D GetColor() const
#define _(s)
@ NO_RECURSE
Definition eda_item.h:52
FILL_T
Definition eda_shape.h:56
@ FILLED_WITH_COLOR
Definition eda_shape.h:60
@ NO_FILL
Definition eda_shape.h:57
@ FILLED_WITH_BG_BODYCOLOR
Definition eda_shape.h:59
@ FILLED_SHAPE
Fill with object color.
Definition eda_shape.h:58
@ LAYER_RULE_AREAS
Definition layer_ids.h:465
@ LAYER_DEVICE_BACKGROUND
Definition layer_ids.h:484
@ LAYER_NOTES_BACKGROUND
Definition layer_ids.h:469
@ LAYER_SELECTION_SHADOWS
Definition layer_ids.h:495
@ REPAINT
Item needs to be redrawn.
Definition view_item.h:58
#define TYPE_HASH(x)
Definition property.h:74
#define REGISTER_TYPE(x)
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.
The EE_TYPE struct provides a type-specific auto-range iterator to the RTree.
Definition sch_rtree.h:195
KIBIS_PIN * pin
@ SCH_FIELD_T
Definition typeinfo.h:154
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:175
@ SCH_RULE_AREA_T
Definition typeinfo.h:174
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695