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 <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 
42 SCH_JUNCTION::SCH_JUNCTION( const wxPoint& aPosition, int aDiameter, SCH_LAYER_ID aLayer ) :
43  SCH_ITEM( nullptr, SCH_JUNCTION_T )
44 {
45  m_pos = aPosition;
46  m_color = COLOR4D::UNSPECIFIED;
47  m_diameter = aDiameter;
48  m_layer = aLayer;
49 }
50 
51 
53 {
54  return new SCH_JUNCTION( *this );
55 }
56 
57 
59 {
60  wxCHECK_RET( ( aItem != nullptr ) && ( aItem->Type() == SCH_JUNCTION_T ),
61  wxT( "Cannot swap junction data with invalid item." ) );
62 
63  SCH_JUNCTION* item = (SCH_JUNCTION*) aItem;
64  std::swap( m_pos, item->m_pos );
65  std::swap( m_diameter, item->m_diameter );
66  std::swap( m_color, item->m_color );
67 }
68 
69 
70 void SCH_JUNCTION::ViewGetLayers( int aLayers[], int& aCount ) const
71 {
72  aCount = 2;
73  aLayers[0] = m_layer;
74  aLayers[1] = LAYER_SELECTION_SHADOWS;
75 }
76 
77 
79 {
80  int diameter;
81 
82  if( m_diameter != 0 )
83  diameter = m_diameter;
84  else if( Schematic() )
85  diameter = Schematic()->Settings().m_JunctionSize;
86  else
87  diameter = Mils2iu( DEFAULT_JUNCTION_DIAM );
88 
89  if( diameter != 1 ) // Diameter 1 means users doesn't want to draw junction dots
90  {
91  NETCLASSPTR netclass = NetClass();
92 
93  if( netclass )
94  diameter = std::max( diameter, KiROUND( netclass->GetWireWidth() * 1.7 ) );
95  }
96 
97  return SHAPE_CIRCLE( m_pos, std::max( diameter / 2, 1 ) );
98 }
99 
100 
102 {
103  EDA_RECT rect;
104 
105  rect.SetOrigin( m_pos );
106  rect.Inflate( getEffectiveShape().GetRadius() );
107 
108  return rect;
109 }
110 
111 
112 void SCH_JUNCTION::Print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset )
113 {
114  wxDC* DC = aSettings->GetPrintDC();
116 
117  if( color == COLOR4D::UNSPECIFIED )
118  color = aSettings->GetLayerColor( GetLayer() );
119 
120  SHAPE_CIRCLE circle = getEffectiveShape();
121 
122  GRFilledCircle( nullptr, DC, (wxPoint) circle.GetCenter() + aOffset, circle.GetRadius(),
123  color );
124 }
125 
126 
128 {
129  MIRROR( m_pos.y, aCenter );
130 }
131 
132 
134 {
135  MIRROR( m_pos.x, aCenter );
136 }
137 
138 
139 void SCH_JUNCTION::Rotate( const wxPoint& aCenter )
140 {
141  RotatePoint( &m_pos, aCenter, 900 );
142 }
143 
144 
145 void SCH_JUNCTION::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
146 {
147  DANGLING_END_ITEM item( JUNCTION_END, this, m_pos );
148  aItemList.push_back( item );
149 }
150 
151 
152 std::vector<wxPoint> SCH_JUNCTION::GetConnectionPoints() const
153 {
154  return { m_pos };
155 }
156 
157 
158 #if defined(DEBUG)
159 void SCH_JUNCTION::Show( int nestLevel, std::ostream& os ) const
160 {
161  // XML output:
162  wxString s = GetClass();
163 
164  NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << ", " << m_diameter
165  << "/>\n";
166 }
167 #endif
168 
169 
171 {
172  if( m_color != COLOR4D::UNSPECIFIED )
173  return m_color;
174 
175  NETCLASSPTR netclass = NetClass();
176 
177  if( netclass )
178  return netclass->GetSchematicColor();
179 
180  return COLOR4D::UNSPECIFIED;
181 }
182 
183 
185 {
186  return getEffectiveShape().GetRadius() * 2;
187 }
188 
189 
190 bool SCH_JUNCTION::HitTest( const wxPoint& aPosition, int aAccuracy ) const
191 {
192  if( aAccuracy >= 0 )
193  return getEffectiveShape().Collide( SEG( aPosition, aPosition ), aAccuracy );
194  else
195  return aPosition == m_pos;
196 }
197 
198 
199 bool SCH_JUNCTION::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
200 {
202  return false;
203 
204  if( aContained )
205  {
206  EDA_RECT selRect = aRect;
207 
208  selRect.Inflate( aAccuracy );
209 
210  return selRect.Contains( GetBoundingBox() );
211  }
212  else
213  {
214  SHAPE_CIRCLE junction = getEffectiveShape();
215  SHAPE_RECT selRect( aRect.GetPosition(), aRect.GetWidth(), aRect.GetHeight() );
216 
217  return selRect.Collide( &junction, aAccuracy );
218  }
219 }
220 
221 
222 bool SCH_JUNCTION::doIsConnected( const wxPoint& aPosition ) const
223 {
224  return m_pos == aPosition;
225 }
226 
227 
228 void SCH_JUNCTION::Plot( PLOTTER* aPlotter ) const
229 {
230  auto* settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( aPlotter->RenderSettings() );
232 
233  if( color == COLOR4D::UNSPECIFIED )
234  color = settings->GetLayerColor( GetLayer() );
235 
236  aPlotter->SetColor( color );
237 
239 }
240 
241 
243 {
244  return BITMAPS::add_junction;
245 }
246 
247 
248 bool SCH_JUNCTION::operator <( const SCH_ITEM& aItem ) const
249 {
250  if( Type() != aItem.Type() )
251  return Type() < aItem.Type();
252 
253  if( GetLayer() != aItem.GetLayer() )
254  return GetLayer() < aItem.GetLayer();
255 
256  auto junction = static_cast<const SCH_JUNCTION*>( &aItem );
257 
258  if( GetPosition().x != junction->GetPosition().x )
259  return GetPosition().x < junction->GetPosition().x;
260 
261  if( GetPosition().y != junction->GetPosition().y )
262  return GetPosition().y < junction->GetPosition().y;
263 
264  if( GetDiameter() != junction->GetDiameter() )
265  return GetDiameter() < junction->GetDiameter();
266 
267  return GetColor() < junction->GetColor();
268 }
269 
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:125
int GetRadius() const
Definition: shape_circle.h:107
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
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:60
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:97
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:109
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:121
void SwapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
int GetDiameter() const
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:481
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:228
virtual bool operator<(const SCH_ITEM &aItem) const override
void GRFilledCircle(EDA_RECT *ClipBox, wxDC *DC, int x, int y, int r, int width, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:621
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
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:102
NETCLASSPTR NetClass(const SCH_SHEET_PATH *aSheet=nullptr) const
Definition: sch_item.cpp:145
SCH_LAYER_ID m_layer
Definition: sch_item.h:496
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
wxPoint GetPosition() const override
Definition: sch_junction.h:92
const wxPoint GetPosition() const
Definition: eda_rect.h:102
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Test if aPosition is contained within or on the bounding box of an 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
SCH_LAYER_ID
Eeschema drawing layers.
virtual void Circle(const wxPoint &pos, int diametre, FILL_TYPE fill, int width=USE_DEFAULT_LINE_WIDTH)=0
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:110
wxPoint m_pos
Position of the junction.
Definition: sch_junction.h:123
virtual void SetColor(const COLOR4D &color)=0
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition: sch_item.h:272
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
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:100
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:87
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:124
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:197
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:113
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103