KiCad PCB EDA Suite
lib_circle.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) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2004-2020 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 
25 #include <sch_draw_panel.h>
26 #include <plotter.h>
27 #include <trigo.h>
28 #include <base_units.h>
29 #include <widgets/msgpanel.h>
30 #include <bitmaps.h>
31 #include <math/util.h> // for KiROUND
32 #include <eda_draw_frame.h>
33 #include <general.h>
34 #include <lib_circle.h>
36 #include <transform.h>
37 
38 
40  LIB_ITEM( LIB_CIRCLE_T, aParent )
41 {
42  m_Width = 0;
44  m_isFillable = true;
45 }
46 
47 
48 bool LIB_CIRCLE::HitTest( const wxPoint& aPosRef, int aAccuracy ) const
49 {
50  int mindist = std::max( aAccuracy + GetPenWidth() / 2,
51  Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
52  int dist = KiROUND( GetLineLength( aPosRef, DefaultTransform.TransformCoordinate( m_Pos ) ) );
53 
54  if( abs( dist - GetRadius() ) <= mindist )
55  return true;
56 
57  if( m_fill == FILL_TYPE::NO_FILL )
58  return false;
59 
60  return dist <= GetRadius();
61 }
62 
63 
64 bool LIB_CIRCLE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
65 {
67  return false;
68 
69  wxPoint center = DefaultTransform.TransformCoordinate( GetPosition() );
70  int radius = GetRadius();
71  int lineWidth = GetWidth();
72  EDA_RECT sel = aRect ;
73 
74  if ( aAccuracy )
75  sel.Inflate( aAccuracy );
76 
77  if( aContained )
78  return sel.Contains( GetBoundingBox() );
79 
80  // If the rectangle does not intersect the bounding box, this is a much quicker test
81  if( !sel.Intersects( GetBoundingBox() ) )
82  return false;
83  else
84  return sel.IntersectsCircleEdge( center, radius, lineWidth );
85 }
86 
87 
89 {
90  return new LIB_CIRCLE( *this );
91 }
92 
93 
94 int LIB_CIRCLE::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
95 {
96  wxASSERT( aOther.Type() == LIB_CIRCLE_T );
97 
98  int retv = LIB_ITEM::compare( aOther, aCompareFlags );
99 
100  if( retv )
101  return retv;
102 
103  const LIB_CIRCLE* tmp = ( LIB_CIRCLE* ) &aOther;
104 
105  if( m_Pos.x != tmp->m_Pos.x )
106  return m_Pos.x - tmp->m_Pos.x;
107 
108  if( m_Pos.y != tmp->m_Pos.y )
109  return m_Pos.y - tmp->m_Pos.y;
110 
111  if( m_EndPos.x != tmp->m_EndPos.x )
112  return m_EndPos.x - tmp->m_EndPos.x;
113 
114  if( m_EndPos.y != tmp->m_EndPos.y )
115  return m_EndPos.y - tmp->m_EndPos.y;
116 
117  return 0;
118 }
119 
120 
121 void LIB_CIRCLE::Offset( const wxPoint& aOffset )
122 {
123  m_Pos += aOffset;
124  m_EndPos += aOffset;
125 }
126 
127 
128 void LIB_CIRCLE::MoveTo( const wxPoint& aPosition )
129 {
130  Offset( aPosition - m_Pos );
131 }
132 
133 
134 void LIB_CIRCLE::MirrorHorizontal( const wxPoint& aCenter )
135 {
136  m_Pos.x -= aCenter.x;
137  m_Pos.x *= -1;
138  m_Pos.x += aCenter.x;
139  m_EndPos.x -= aCenter.x;
140  m_EndPos.x *= -1;
141  m_EndPos.x += aCenter.x;
142 }
143 
144 
145 void LIB_CIRCLE::MirrorVertical( const wxPoint& aCenter )
146 {
147  m_Pos.y -= aCenter.y;
148  m_Pos.y *= -1;
149  m_Pos.y += aCenter.y;
150  m_EndPos.y -= aCenter.y;
151  m_EndPos.y *= -1;
152  m_EndPos.y += aCenter.y;
153 }
154 
155 
156 void LIB_CIRCLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
157 {
158  int rot_angle = aRotateCCW ? -900 : 900;
159 
160  RotatePoint( &m_Pos, aCenter, rot_angle );
161  RotatePoint( &m_EndPos, aCenter, rot_angle );
162 }
163 
164 
165 void LIB_CIRCLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
166  const TRANSFORM& aTransform )
167 {
168  wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
169 
171  {
172  aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE_BACKGROUND ) );
173  aPlotter->Circle( pos, GetRadius() * 2, FILL_TYPE::FILLED_WITH_BG_BODYCOLOR, 0 );
174  }
175 
176  bool already_filled = m_fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR;
177  int pen_size = GetPenWidth();
178 
179  if( !already_filled || pen_size > 0 )
180  {
181  pen_size = std::max( pen_size, aPlotter->RenderSettings()->GetMinPenWidth() );
182 
183  aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE ) );
184  aPlotter->Circle( pos, GetRadius() * 2, already_filled ? FILL_TYPE::NO_FILL : m_fill,
185  pen_size );
186  }
187 }
188 
189 
191 {
192  // Historically 0 meant "default width" and negative numbers meant "don't stroke".
193  if( m_Width < 0 && GetFillMode() != FILL_TYPE::NO_FILL )
194  return 0;
195  else
196  return std::max( m_Width, 1 );
197 }
198 
199 
200 void LIB_CIRCLE::print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset,
201  void* aData, const TRANSFORM& aTransform )
202 {
203  bool forceNoFill = static_cast<bool>( aData );
204  int penWidth = GetPenWidth();
205 
206  if( forceNoFill && m_fill != FILL_TYPE::NO_FILL && penWidth == 0 )
207  return;
208 
209  wxDC* DC = aSettings->GetPrintDC();
210  wxPoint pos1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
211  COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
212 
213  if( forceNoFill || m_fill == FILL_TYPE::NO_FILL )
214  {
215  penWidth = std::max( penWidth, aSettings->GetDefaultPenWidth() );
216 
217  GRCircle( nullptr, DC, pos1.x, pos1.y, GetRadius(), penWidth, color );
218  }
219  else
220  {
223 
224  GRFilledCircle( nullptr, DC, pos1.x, pos1.y, GetRadius(), 0, color, color );
225  }
226 }
227 
228 
230 {
231  EDA_RECT rect;
232  int radius = GetRadius();
233 
234  rect.SetOrigin( m_Pos.x - radius, m_Pos.y - radius );
235  rect.SetEnd( m_Pos.x + radius, m_Pos.y + radius );
236  rect.Inflate( ( GetPenWidth() / 2 ) + 1 );
237 
238  rect.RevertYAxis();
239 
240  return rect;
241 }
242 
243 
245 {
246  wxString msg;
247  EDA_RECT bBox = GetBoundingBox();
248 
249  LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
250 
251  msg = MessageTextFromValue( aFrame->GetUserUnits(), m_Width );
252 
253  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg ) );
254 
255  msg = MessageTextFromValue( aFrame->GetUserUnits(), GetRadius() );
256  aList.push_back( MSG_PANEL_ITEM( _( "Radius" ), msg ) );
257 
258  msg.Printf( wxT( "(%d, %d, %d, %d)" ),
259  bBox.GetOrigin().x,
260  bBox.GetOrigin().y,
261  bBox.GetEnd().x,
262  bBox.GetEnd().y );
263 
264  aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg ) );
265 }
266 
267 
268 wxString LIB_CIRCLE::GetSelectMenuText( EDA_UNITS aUnits ) const
269 {
270  return wxString::Format( _( "Circle, radius %s" ),
271  MessageTextFromValue( aUnits, GetRadius() ) );
272 }
273 
274 
276 {
277  return add_circle_xpm;
278 }
279 
280 
281 void LIB_CIRCLE::BeginEdit( const wxPoint aPosition )
282 {
283  m_Pos = aPosition;
284 }
285 
286 
287 void LIB_CIRCLE::CalcEdit( const wxPoint& aPosition )
288 {
289  SetEnd( aPosition );
290 }
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:125
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
double GetLineLength(const wxPoint &aPointA, const wxPoint &aPointB)
Return the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:223
PNG memory record (file in memory).
Definition: bitmap_def.h:29
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
Implementation of conversion functions that require both schematic and board internal units.
virtual void SetColor(COLOR4D color)=0
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Test if aPosition is contained within or on the bounding box of an item.
Definition: lib_circle.cpp:48
int color
Definition: DXF_plotter.cpp:60
void BeginEdit(const wxPoint aStartPoint) override
Begin drawing a component library draw item at aPosition.
Definition: lib_circle.cpp:281
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
wxPoint GetPosition() const override
Definition: lib_circle.h:72
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:126
bool IntersectsCircleEdge(const wxPoint &aCenter, const int aRadius, const int aWidth) const
Test for intersection between this rect and the edge (radius) of a circle.
Definition: eda_rect.cpp:330
void Rotate(const wxPoint &aCenter, bool aRotateCCW=true) override
Rotate the object about aCenter point.
Definition: lib_circle.cpp:156
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:228
wxPoint m_Pos
Definition: lib_circle.h:35
The base class for create windows for drawing purpose.
int GetWidth() const override
Definition: lib_circle.h:84
void GRFilledCircle(EDA_RECT *ClipBox, wxDC *DC, int x, int y, int r, int width, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:617
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: lib_circle.cpp:88
void RevertYAxis()
Mirror the rectangle from the X axis (negate Y pos and size).
Definition: eda_rect.h:203
FILL_TYPE m_fill
The body fill type.
Definition: lib_item.h:326
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:42
The base class for drawable items used by schematic library components.
Definition: lib_item.h:62
const wxPoint GetEnd() const
Definition: eda_rect.h:108
FILL_TYPE GetFillMode() const
Definition: lib_item.h:268
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
COMPARE_FLAGS
The list of flags used by the compare function.
Definition: lib_item.h:84
void MirrorVertical(const wxPoint &aCenter) override
Mirror the draw object along the MirrorVertical (Y) axis about aCenter point.
Definition: lib_circle.cpp:145
const wxPoint GetOrigin() const
Definition: eda_rect.h:106
void SetEnd(int x, int y)
Definition: eda_rect.h:187
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_item.h:48
bool m_isFillable
Definition: lib_item.h:327
Define a library symbol object.
Definition: lib_symbol.h:93
int GetPenWidth() const override
Definition: lib_circle.cpp:190
BITMAP_DEF GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: lib_circle.cpp:275
wxDC * GetPrintDC() const
#define STRUCT_DELETED
flag indication structures to be erased
Definition: eda_item.h:115
int GetRadius() const
Definition: lib_circle.h:88
virtual void Circle(const wxPoint &pos, int diametre, FILL_TYPE fill, int width=USE_DEFAULT_LINE_WIDTH)=0
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: lib_circle.cpp:268
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
Definition: lib_item.cpp:50
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
EDA_UNITS
Definition: eda_units.h:38
Base plotter engine class.
Definition: plotter.h:121
RENDER_SETTINGS * RenderSettings()
Definition: plotter.h:155
TRANSFORM DefaultTransform
Definition: eeschema.cpp:55
LIB_CIRCLE(LIB_PART *aParent)
Definition: lib_circle.cpp:39
int compare(const LIB_ITEM &aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags=LIB_ITEM::COMPARE_FLAGS::NORMAL) const override
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_circle.cpp:94
#define _(s)
Definition: 3d_actions.cpp:33
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
Definition: lib_circle.cpp:134
void GRCircle(EDA_RECT *ClipBox, wxDC *DC, int xc, int yc, int r, int width, COLOR4D Color)
Definition: gr_basic.cpp:594
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Populate aList of MSG_PANEL_ITEM objects with it's internal state for display purposes.
Definition: lib_circle.cpp:244
Handle the component boundary box.
Definition: eda_rect.h:42
void print(const RENDER_SETTINGS *aSettings, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
Definition: lib_circle.cpp:200
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:68
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:149
void Offset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
Definition: lib_circle.cpp:121
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:97
bool Intersects(const EDA_RECT &aRect) const
Test for a common area between rectangles.
Definition: eda_rect.cpp:150
int GetDefaultPenWidth() const
int m_Width
Definition: lib_circle.h:37
wxPoint m_EndPos
Definition: lib_circle.h:36
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_circle.cpp:128
void CalcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
Definition: lib_circle.cpp:287
EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:54
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: eda_item.h:117
STATUS_FLAGS m_flags
Definition: eda_item.h:530
Message panel definition file.
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: lib_circle.cpp:229
virtual int compare(const LIB_ITEM &aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags=LIB_ITEM::COMPARE_FLAGS::NORMAL) const
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_item.cpp:74
const BITMAP_OPAQUE add_circle_xpm[1]
Definition: add_circle.cpp:32
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void SetEnd(const wxPoint &aPosition)
Definition: lib_circle.h:74
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:363
void Plot(PLOTTER *aPlotter, const wxPoint &aOffset, bool aFill, const TRANSFORM &aTransform) override
Plot the draw item using the plot object.
Definition: lib_circle.cpp:165
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:162
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98