KiCad PCB EDA Suite
sch_junction.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) 2009 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
29 #include <sch_draw_panel.h>
30 #include <trigo.h>
31 #include <common.h>
32 #include <plotters/plotter.h>
33 #include <bitmaps.h>
34 #include <core/mirror.h>
35 #include <geometry/shape_rect.h>
36 #include <sch_painter.h>
37 #include <sch_junction.h>
38 #include <sch_connection.h>
39 #include <schematic.h>
41 #include <advanced_config.h>
42 #include <connection_graph.h>
43 
44 
45 SCH_JUNCTION::SCH_JUNCTION( const wxPoint& aPosition, int aDiameter, SCH_LAYER_ID aLayer ) :
46  SCH_ITEM( nullptr, SCH_JUNCTION_T )
47 {
48  m_pos = aPosition;
49  m_color = COLOR4D::UNSPECIFIED;
50  m_diameter = aDiameter;
51  m_layer = aLayer;
52 
54  m_lastResolvedColor = COLOR4D::UNSPECIFIED;
55 }
56 
57 
59 {
60  return new SCH_JUNCTION( *this );
61 }
62 
63 
65 {
66  wxCHECK_RET( ( aItem != nullptr ) && ( aItem->Type() == SCH_JUNCTION_T ),
67  wxT( "Cannot swap junction data with invalid item." ) );
68 
69  SCH_JUNCTION* item = (SCH_JUNCTION*) aItem;
70  std::swap( m_pos, item->m_pos );
71  std::swap( m_diameter, item->m_diameter );
72  std::swap( m_color, item->m_color );
73 }
74 
75 
76 void SCH_JUNCTION::ViewGetLayers( int aLayers[], int& aCount ) const
77 {
78  aCount = 2;
79  aLayers[0] = m_layer;
80  aLayers[1] = LAYER_SELECTION_SHADOWS;
81 }
82 
83 
85 {
86  if( m_diameter != 0 )
88  else if( Schematic() )
90  else
92 
93  if( m_lastResolvedDiameter != 1 ) // Diameter 1 means users doesn't want to draw junction dots
94  {
95  if( !IsConnectivityDirty() )
96  {
97  NETCLASSPTR netclass = NetClass();
98 
99  if( netclass )
100  m_lastResolvedDiameter = std::max( m_lastResolvedDiameter, KiROUND( netclass->GetWireWidth() * 1.7 ) );
101  }
102  }
103 
104  return SHAPE_CIRCLE( m_pos, std::max( m_lastResolvedDiameter / 2, 1 ) );
105 }
106 
107 
109 {
110  EDA_RECT rect;
111 
112  rect.SetOrigin( m_pos );
113  rect.Inflate( getEffectiveShape().GetRadius() );
114 
115  return rect;
116 }
117 
118 
119 void SCH_JUNCTION::Print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset )
120 {
121  wxDC* DC = aSettings->GetPrintDC();
123 
124  if( color == COLOR4D::UNSPECIFIED )
125  color = aSettings->GetLayerColor( GetLayer() );
126 
127  SHAPE_CIRCLE circle = getEffectiveShape();
128 
129  GRFilledCircle( nullptr, DC, (wxPoint) circle.GetCenter() + aOffset, circle.GetRadius(),
130  color );
131 }
132 
133 
135 {
136  MIRROR( m_pos.y, aCenter );
137 }
138 
139 
141 {
142  MIRROR( m_pos.x, aCenter );
143 }
144 
145 
146 void SCH_JUNCTION::Rotate( const wxPoint& aCenter )
147 {
148  RotatePoint( &m_pos, aCenter, 900 );
149 }
150 
151 
152 void SCH_JUNCTION::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
153 {
154  DANGLING_END_ITEM item( JUNCTION_END, this, m_pos );
155  aItemList.push_back( item );
156 }
157 
158 
159 std::vector<wxPoint> SCH_JUNCTION::GetConnectionPoints() const
160 {
161  return { m_pos };
162 }
163 
164 
165 #if defined(DEBUG)
166 void SCH_JUNCTION::Show( int nestLevel, std::ostream& os ) const
167 {
168  // XML output:
169  wxString s = GetClass();
170 
171  NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << ", " << m_diameter
172  << "/>\n";
173 }
174 #endif
175 
176 
178 {
179  if( m_color != COLOR4D::UNSPECIFIED )
180  {
182  }
183  else if( !IsConnectivityDirty() )
184  {
185  NETCLASSPTR netclass = NetClass();
186 
187  if( netclass )
188  m_lastResolvedColor = netclass->GetSchematicColor();
189  }
190 
191  return m_lastResolvedColor;
192 }
193 
194 
196 {
197  return getEffectiveShape().GetRadius() * 2;
198 }
199 
200 
201 bool SCH_JUNCTION::HitTest( const wxPoint& aPosition, int aAccuracy ) const
202 {
203  if( aAccuracy >= 0 )
204  return getEffectiveShape().Collide( SEG( aPosition, aPosition ), aAccuracy );
205  else
206  return aPosition == m_pos;
207 }
208 
209 
210 bool SCH_JUNCTION::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
211 {
213  return false;
214 
215  if( aContained )
216  {
217  EDA_RECT selRect = aRect;
218 
219  selRect.Inflate( aAccuracy );
220 
221  return selRect.Contains( GetBoundingBox() );
222  }
223  else
224  {
225  SHAPE_CIRCLE junction = getEffectiveShape();
226  SHAPE_RECT selRect( aRect.GetPosition(), aRect.GetWidth(), aRect.GetHeight() );
227 
228  return selRect.Collide( &junction, aAccuracy );
229  }
230 }
231 
232 
233 bool SCH_JUNCTION::doIsConnected( const wxPoint& aPosition ) const
234 {
235  return m_pos == aPosition;
236 }
237 
238 
239 void SCH_JUNCTION::Plot( PLOTTER* aPlotter ) const
240 {
241  auto* settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( aPlotter->RenderSettings() );
243 
244  if( color == COLOR4D::UNSPECIFIED )
245  color = settings->GetLayerColor( GetLayer() );
246 
247  aPlotter->SetColor( color );
248 
250 }
251 
252 
254 {
255  return BITMAPS::add_junction;
256 }
257 
258 
259 bool SCH_JUNCTION::operator <( const SCH_ITEM& aItem ) const
260 {
261  if( Type() != aItem.Type() )
262  return Type() < aItem.Type();
263 
264  if( GetLayer() != aItem.GetLayer() )
265  return GetLayer() < aItem.GetLayer();
266 
267  auto junction = static_cast<const SCH_JUNCTION*>( &aItem );
268 
269  if( GetPosition().x != junction->GetPosition().x )
270  return GetPosition().x < junction->GetPosition().x;
271 
272  if( GetPosition().y != junction->GetPosition().y )
273  return GetPosition().y < junction->GetPosition().y;
274 
275  if( GetDiameter() != junction->GetDiameter() )
276  return GetDiameter() < junction->GetDiameter();
277 
278  return GetColor() < junction->GetColor();
279 }
280 
wxString GetClass() const override
Return the class name.
Definition: sch_junction.h:50
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
Definition: shape_rect.h:98
COLOR4D GetJunctionColor() const
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
COLOR4D m_color
Color of the junction. #COLOR4D::UNSPECIFIED is user default.
Definition: sch_junction.h:138
int GetRadius() const
Definition: shape_circle.h:107
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
#define DEFAULT_WIRE_WIDTH_MILS
The default bus width in mils. (can be changed in preference menu)
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:167
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
int color
Definition: DXF_plotter.cpp:57
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:104
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
void Rotate(const wxPoint &aCenter) override
Rotate the item around aCenter 90 degrees in the clockwise direction.
int GetWidth() const
Definition: eda_rect.h:118
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:130
void SwapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
int GetDiameter() const
Definition: sch_junction.h:110
std::vector< wxPoint > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
#define DEFAULT_JUNCTION_DIAM
The default bus and wire entry size in mils.
const VECTOR2I GetCenter() const
Definition: shape_circle.h:112
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:480
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:229
virtual bool operator<(const SCH_ITEM &aItem) const override
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
bool IsConnectivityDirty() const
Definition: sch_item.h:413
bool doIsConnected(const wxPoint &aPosition) const override
Provide the object specific test to see if it is connected to aPosition.
void MIRROR(T &aPoint, const T &aMirrorRef)
Updates aPoint with the mirror of aPoint relative to the aMirrorRef.
Definition: mirror.h:40
SCH_JUNCTION(const wxPoint &aPosition=wxPoint(0, 0), int aDiameter=0, SCH_LAYER_ID aLayer=LAYER_JUNCTION)
COLOR4D GetColor() const
Definition: sch_junction.h:115
NETCLASSPTR NetClass(const SCH_SHEET_PATH *aSheet=nullptr) const
Definition: sch_item.cpp:158
SCH_LAYER_ID m_layer
Definition: sch_item.h:487
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
Definition: shape_circle.h:76
SCH_LAYER_ID
Eeschema drawing layers.
Definition: layer_ids.h:335
wxPoint GetPosition() const override
Definition: sch_junction.h:103
const wxPoint GetPosition() const
Definition: eda_rect.h:111
COLOR4D m_lastResolvedColor
Definition: sch_junction.h:144
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
void Plot(PLOTTER *aPlotter) const override
Plot the schematic item to aPlotter.
#define STRUCT_DELETED
flag indication structures to be erased
void MirrorHorizontally(int aCenter) override
Mirror item horizontally about aCenter.
wxDC * GetPrintDC() const
void ViewGetLayers(int aLayers[], int &aCount) const override
Return the layers the item is drawn on (which may be more than its "home" layer)
int GetHeight() const
Definition: eda_rect.h:119
wxPoint m_pos
Position of the junction.
Definition: sch_junction.h:136
virtual void SetColor(const COLOR4D &color)=0
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition: sch_item.h:259
Definition: seg.h:40
void GetEndPoints(std::vector< DANGLING_END_ITEM > &aItemList) override
Add the schematic item end points to aItemList if the item has end points.
void Print(const RENDER_SETTINGS *aSettings, const wxPoint &aOffset) override
Print a schematic item.
Base plotter engine class.
Definition: plotter.h:121
void MirrorVertically(int aCenter) override
Mirror item vertically about aCenter.
RENDER_SETTINGS * RenderSettings()
Definition: plotter.h:156
#define SKIP_STRUCT
flag indicating that the structure should be ignored
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
int GetEffectiveDiameter() const
int m_lastResolvedDiameter
Definition: sch_junction.h:143
Handle the component boundary box.
Definition: eda_rect.h:42
SHAPE_CIRCLE getEffectiveShape() const
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:73
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:99
The common library.
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition: sch_item.h:79
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
int m_diameter
Diameter of the junction. Zero is user default.
Definition: sch_junction.h:137
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:182
virtual void Circle(const wxPoint &pos, int diametre, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH)=0
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:364
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:112
void GRFilledCircle(EDA_RECT *ClipBox, wxDC *DC, int x, int y, int r, int width, const COLOR4D &Color, const COLOR4D &BgColor)
Definition: gr_basic.cpp:529
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103