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 <fctsys.h>
26 #include <gr_basic.h>
27 #include <macros.h>
28 #include <sch_draw_panel.h>
29 #include <plotter.h>
30 #include <trigo.h>
31 #include <base_units.h>
32 #include <msgpanel.h>
33 #include <bitmaps.h>
34 #include <math/util.h> // for KiROUND
35 
36 #include <general.h>
37 #include <lib_circle.h>
39 #include <transform.h>
40 
41 
43  LIB_ITEM( LIB_CIRCLE_T, aParent )
44 {
45  m_Width = 0;
46  m_Fill = NO_FILL;
47  m_isFillable = true;
48 }
49 
50 
51 bool LIB_CIRCLE::HitTest( const wxPoint& aPosRef, int aAccuracy ) const
52 {
53  int mindist = std::max( aAccuracy + GetPenSize() / 2,
54  Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
56 
57  if( abs( dist - GetRadius() ) <= mindist )
58  return true;
59 
60  return false;
61 }
62 
63 
64 bool LIB_CIRCLE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
65 {
66  if( m_Flags & ( STRUCT_DELETED | SKIP_STRUCT ) )
67  return false;
68 
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 bool LIB_CIRCLE::Inside( EDA_RECT& aRect ) const
129 {
130  wxPoint center(m_Pos.x, -m_Pos.y);
131  return aRect.IntersectsCircle( center, GetRadius() );
132 }
133 
134 
135 void LIB_CIRCLE::MoveTo( const wxPoint& aPosition )
136 {
137  Offset( aPosition - m_Pos );
138 }
139 
140 
141 void LIB_CIRCLE::MirrorHorizontal( const wxPoint& aCenter )
142 {
143  m_Pos.x -= aCenter.x;
144  m_Pos.x *= -1;
145  m_Pos.x += aCenter.x;
146  m_EndPos.x -= aCenter.x;
147  m_EndPos.x *= -1;
148  m_EndPos.x += aCenter.x;
149 }
150 
151 
152 void LIB_CIRCLE::MirrorVertical( const wxPoint& aCenter )
153 {
154  m_Pos.y -= aCenter.y;
155  m_Pos.y *= -1;
156  m_Pos.y += aCenter.y;
157  m_EndPos.y -= aCenter.y;
158  m_EndPos.y *= -1;
159  m_EndPos.y += aCenter.y;
160 }
161 
162 
163 void LIB_CIRCLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
164 {
165  int rot_angle = aRotateCCW ? -900 : 900;
166 
167  RotatePoint( &m_Pos, aCenter, rot_angle );
168  RotatePoint( &m_EndPos, aCenter, rot_angle );
169 }
170 
171 
172 void LIB_CIRCLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
173  const TRANSFORM& aTransform )
174 {
175  wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
176 
177  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
178  {
179  aPlotter->SetColor( aPlotter->ColorSettings()->GetColor( LAYER_DEVICE_BACKGROUND ) );
180  aPlotter->Circle( pos, GetRadius() * 2, FILLED_WITH_BG_BODYCOLOR, 0 );
181  }
182 
183  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
184  auto pen_size = GetPenSize();
185 
186  if( !already_filled || pen_size > 0 )
187  {
188  pen_size = std::max( 0, pen_size );
189  aPlotter->SetColor( aPlotter->ColorSettings()->GetColor( LAYER_DEVICE ) );
190  aPlotter->Circle( pos, GetRadius() * 2, already_filled ? NO_FILL : m_Fill, pen_size );
191  }
192 }
193 
194 
196 {
197  if( m_Width > 0 )
198  return m_Width;
199 
200  if( m_Width == 0 )
201  return GetDefaultLineThickness();
202 
203  return -1; // a value to use a minimal pen size
204 }
205 
206 
207 void LIB_CIRCLE::print( wxDC* aDC, const wxPoint& aOffset, void* aData,
208  const TRANSFORM& aTransform )
209 {
210  wxPoint pos1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
213  FILL_T fill = aData ? NO_FILL : m_Fill;
214 
215  if( fill == FILLED_WITH_BG_BODYCOLOR )
216  GRFilledCircle( nullptr, aDC, pos1.x, pos1.y, GetRadius(), GetPenSize(), bgColor, bgColor );
217  else if( fill == FILLED_SHAPE )
218  GRFilledCircle( nullptr, aDC, pos1.x, pos1.y, GetRadius(), 0, color, color );
219  else
220  GRCircle( nullptr, aDC, pos1.x, pos1.y, GetRadius(), GetPenSize(), color );
221 }
222 
223 
225 {
226  EDA_RECT rect;
227  int radius = GetRadius();
228 
229  rect.SetOrigin( m_Pos.x - radius, m_Pos.y - radius );
230  rect.SetEnd( m_Pos.x + radius, m_Pos.y + radius );
231  rect.Inflate( ( GetPenSize()+1 ) / 2 );
232 
233  rect.RevertYAxis();
234 
235  return rect;
236 }
237 
238 
240 {
241  wxString msg;
242  EDA_RECT bBox = GetBoundingBox();
243 
244  LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
245 
246  msg = MessageTextFromValue( aUnits, m_Width, true );
247 
248  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
249 
250  msg = MessageTextFromValue( aUnits, GetRadius(), true );
251  aList.push_back( MSG_PANEL_ITEM( _( "Radius" ), msg, RED ) );
252 
253  msg.Printf( wxT( "(%d, %d, %d, %d)" ),
254  bBox.GetOrigin().x,
255  bBox.GetOrigin().y,
256  bBox.GetEnd().x,
257  bBox.GetEnd().y );
258 
259  aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
260 }
261 
262 
263 wxString LIB_CIRCLE::GetSelectMenuText( EDA_UNITS aUnits ) const
264 {
265  return wxString::Format( _( "Circle center (%s, %s), radius %s" ),
266  MessageTextFromValue( aUnits, m_Pos.x ),
267  MessageTextFromValue( aUnits, m_Pos.y ),
268  MessageTextFromValue( aUnits, GetRadius() ) );
269 }
270 
271 
273 {
274  return add_circle_xpm;
275 }
276 
277 
278 void LIB_CIRCLE::BeginEdit( const wxPoint aPosition )
279 {
280  m_Pos = aPosition;
281 }
282 
283 
284 void LIB_CIRCLE::CalcEdit( const wxPoint& aPosition )
285 {
286  SetEnd( aPosition );
287 }
Definition: colors.h:57
EDA_UNITS
Definition: common.h:184
int GetPenSize() const override
Definition: lib_circle.cpp:195
double GetLineLength(const wxPoint &aPointA, const wxPoint &aPointB)
Return the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:206
PNG memory record (file in memory).
Definition: bitmap_def.h:29
static const int dist[10][10]
Definition: ar_matrix.cpp:326
void GetMsgPanelInfo(EDA_UNITS aUnits, 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:52
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
Function HitTest tests if aPosition is contained within or on the bounding box of an item.
Definition: lib_circle.cpp:51
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: base_struct.h:131
int color
Definition: DXF_plotter.cpp:61
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i....
void BeginEdit(const wxPoint aStartPoint) override
Begin drawing a component library draw item at aPosition.
Definition: lib_circle.cpp:278
wxPoint GetPosition() const override
Definition: lib_circle.h:76
bool IntersectsCircle(const wxPoint &aCenter, const int aRadius) const
Function IntersectsCircle tests for a common area between a circle and this rectangle.
TRANSFORM DefaultTransform
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
bool IntersectsCircleEdge(const wxPoint &aCenter, const int aRadius, const int aWidth) const
IntersectsCircleEdge Tests for intersection between this rect and the edge (radius) of a circle.
void Rotate(const wxPoint &aCenter, bool aRotateCCW=true) override
Rotate the object about aCenter point.
Definition: lib_circle.cpp:163
void print(wxDC *aDC, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
Definition: lib_circle.cpp:207
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
wxPoint m_Pos
Definition: lib_circle.h:34
int GetWidth() const override
Definition: lib_circle.h:88
void GRFilledCircle(EDA_RECT *ClipBox, wxDC *DC, int x, int y, int r, int width, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:619
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: lib_circle.cpp:88
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size)
Definition: eda_rect.h:209
bool Contains(const wxPoint &aPoint) const
Function Contains.
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:60
This file contains miscellaneous commonly used macros and functions.
const wxPoint GetEnd() const
Definition: eda_rect.h:116
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:116
COLOR_SETTINGS * ColorSettings()
Definition: plotter.h:145
void MirrorVertical(const wxPoint &aCenter) override
Mirror the draw object along the MirrorVertical (Y) axis about aCenter point.
Definition: lib_circle.cpp:152
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
void SetEnd(int x, int y)
Definition: eda_rect.h:192
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_item.h:46
bool m_isFillable
Definition: lib_item.h:94
Define a library symbol object.
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Helper for all the old plotting/printing code while it still exists.
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
Definition: lib_circle.cpp:272
#define STRUCT_DELETED
flag indication structures to be erased
Definition: base_struct.h:129
Definition: colors.h:60
int GetRadius() const
Definition: lib_circle.h:92
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
Definition: lib_circle.cpp:263
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils, EDA_DATA_TYPE aType)
Definition: base_units.cpp:127
Base plotter engine class.
Definition: plotter.h:104
COLOR4D GetColor(int aLayer) const
LIB_CIRCLE(LIB_PART *aParent)
Definition: lib_circle.cpp:42
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
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:141
FILL_T m_Fill
The body fill type.
Definition: lib_item.h:93
void GRCircle(EDA_RECT *ClipBox, wxDC *DC, int xc, int yc, int r, int width, COLOR4D Color)
Definition: gr_basic.cpp:596
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:189
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
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:61
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:166
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:102
bool Inside(EDA_RECT &aRect) const override
Test if any part of the draw object is inside rectangle bounds of aRect.
Definition: lib_circle.cpp:128
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests for a common area between rectangles.
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:42
int m_Width
Definition: lib_circle.h:36
wxPoint m_EndPos
Definition: lib_circle.h:35
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_circle.cpp:135
void CalcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
Definition: lib_circle.cpp:284
EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
Message panel definition file.
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
Definition: lib_circle.cpp:224
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:76
const BITMAP_OPAQUE add_circle_xpm[1]
Definition: add_circle.cpp:55
virtual void Circle(const wxPoint &pos, int diametre, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH)=0
void SetEnd(const wxPoint &aPosition)
Definition: lib_circle.h:78
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
void GetMsgPanelInfo(EDA_UNITS aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo populates aList of MSG_PANEL_ITEM objects with it's internal state for displ...
Definition: lib_circle.cpp:239
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:172
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40
Definition: colors.h:62