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-2019 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>
38 #include <transform.h>
39 
40 
42  LIB_ITEM( LIB_CIRCLE_T, aParent )
43 {
44  m_Width = 0;
45  m_Fill = NO_FILL;
46  m_isFillable = true;
47 }
48 
49 
50 bool LIB_CIRCLE::HitTest( const wxPoint& aPosRef, int aAccuracy ) const
51 {
52  int mindist = std::max( aAccuracy + GetPenSize() / 2,
53  Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
55 
56  if( abs( dist - GetRadius() ) <= mindist )
57  return true;
58 
59  return false;
60 }
61 
62 
63 bool LIB_CIRCLE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
64 {
65  if( m_Flags & ( STRUCT_DELETED | SKIP_STRUCT ) )
66  return false;
67 
69  int radius = GetRadius();
70  int lineWidth = GetWidth();
71  EDA_RECT sel = aRect ;
72 
73  if ( aAccuracy )
74  sel.Inflate( aAccuracy );
75 
76  if( aContained )
77  return sel.Contains( GetBoundingBox() );
78 
79  // If the rectangle does not intersect the bounding box, this is a much quicker test
80  if( !sel.Intersects( GetBoundingBox() ) )
81  return false;
82  else
83  return sel.IntersectsCircleEdge( center, radius, lineWidth );
84 }
85 
86 
88 {
89  return new LIB_CIRCLE( *this );
90 }
91 
92 
93 int LIB_CIRCLE::compare( const LIB_ITEM& aOther ) const
94 {
95  wxASSERT( aOther.Type() == LIB_CIRCLE_T );
96 
97  const LIB_CIRCLE* tmp = ( LIB_CIRCLE* ) &aOther;
98 
99  if( m_Pos.x != tmp->m_Pos.x )
100  return m_Pos.x - tmp->m_Pos.x;
101 
102  if( m_Pos.y != tmp->m_Pos.y )
103  return m_Pos.y - tmp->m_Pos.y;
104 
105  if( m_EndPos.x != tmp->m_EndPos.x )
106  return m_EndPos.x - tmp->m_EndPos.x;
107 
108  if( m_EndPos.y != tmp->m_EndPos.y )
109  return m_EndPos.y - tmp->m_EndPos.y;
110 
111  return 0;
112 }
113 
114 
115 void LIB_CIRCLE::Offset( const wxPoint& aOffset )
116 {
117  m_Pos += aOffset;
118  m_EndPos += aOffset;
119 }
120 
121 
122 bool LIB_CIRCLE::Inside( EDA_RECT& aRect ) const
123 {
124  wxPoint center(m_Pos.x, -m_Pos.y);
125  return aRect.IntersectsCircle( center, GetRadius() );
126 }
127 
128 
129 void LIB_CIRCLE::MoveTo( const wxPoint& aPosition )
130 {
131  Offset( aPosition - m_Pos );
132 }
133 
134 
135 void LIB_CIRCLE::MirrorHorizontal( const wxPoint& aCenter )
136 {
137  m_Pos.x -= aCenter.x;
138  m_Pos.x *= -1;
139  m_Pos.x += aCenter.x;
140  m_EndPos.x -= aCenter.x;
141  m_EndPos.x *= -1;
142  m_EndPos.x += aCenter.x;
143 }
144 
145 
146 void LIB_CIRCLE::MirrorVertical( const wxPoint& aCenter )
147 {
148  m_Pos.y -= aCenter.y;
149  m_Pos.y *= -1;
150  m_Pos.y += aCenter.y;
151  m_EndPos.y -= aCenter.y;
152  m_EndPos.y *= -1;
153  m_EndPos.y += aCenter.y;
154 }
155 
156 
157 void LIB_CIRCLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
158 {
159  int rot_angle = aRotateCCW ? -900 : 900;
160 
161  RotatePoint( &m_Pos, aCenter, rot_angle );
162  RotatePoint( &m_EndPos, aCenter, rot_angle );
163 }
164 
165 
166 void LIB_CIRCLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
167  const TRANSFORM& aTransform )
168 {
169  wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
170 
171  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
172  {
174  aPlotter->Circle( pos, GetRadius() * 2, FILLED_WITH_BG_BODYCOLOR, 0 );
175  }
176 
177  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
178  auto pen_size = GetPenSize();
179 
180  if( !already_filled || pen_size > 0 )
181  {
182  pen_size = std::max( 0, pen_size );
183  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
184  aPlotter->Circle( pos, GetRadius() * 2, already_filled ? NO_FILL : m_Fill, pen_size );
185  }
186 }
187 
188 
190 {
191  if( m_Width > 0 )
192  return m_Width;
193 
194  if( m_Width == 0 )
195  return GetDefaultLineThickness();
196 
197  return -1; // a value to use a minimal pen size
198 }
199 
200 
201 void LIB_CIRCLE::print( wxDC* aDC, const wxPoint& aOffset, void* aData,
202  const TRANSFORM& aTransform )
203 {
204  wxPoint pos1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
207  FILL_T fill = aData ? NO_FILL : m_Fill;
208 
209  if( fill == FILLED_WITH_BG_BODYCOLOR )
210  GRFilledCircle( nullptr, aDC, pos1.x, pos1.y, GetRadius(), GetPenSize(), bgColor, bgColor );
211  else if( fill == FILLED_SHAPE )
212  GRFilledCircle( nullptr, aDC, pos1.x, pos1.y, GetRadius(), 0, color, color );
213  else
214  GRCircle( nullptr, aDC, pos1.x, pos1.y, GetRadius(), GetPenSize(), color );
215 }
216 
217 
219 {
220  EDA_RECT rect;
221  int radius = GetRadius();
222 
223  rect.SetOrigin( m_Pos.x - radius, m_Pos.y - radius );
224  rect.SetEnd( m_Pos.x + radius, m_Pos.y + radius );
225  rect.Inflate( ( GetPenSize()+1 ) / 2 );
226 
227  rect.RevertYAxis();
228 
229  return rect;
230 }
231 
232 
234 {
235  wxString msg;
236  EDA_RECT bBox = GetBoundingBox();
237 
238  LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
239 
240  msg = MessageTextFromValue( aUnits, m_Width, true );
241 
242  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
243 
244  msg = MessageTextFromValue( aUnits, GetRadius(), true );
245  aList.push_back( MSG_PANEL_ITEM( _( "Radius" ), msg, RED ) );
246 
247  msg.Printf( wxT( "(%d, %d, %d, %d)" ),
248  bBox.GetOrigin().x,
249  bBox.GetOrigin().y,
250  bBox.GetEnd().x,
251  bBox.GetEnd().y );
252 
253  aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
254 }
255 
256 
257 wxString LIB_CIRCLE::GetSelectMenuText( EDA_UNITS aUnits ) const
258 {
259  return wxString::Format( _( "Circle center (%s, %s), radius %s" ),
260  MessageTextFromValue( aUnits, m_Pos.x ),
261  MessageTextFromValue( aUnits, m_Pos.y ),
262  MessageTextFromValue( aUnits, GetRadius() ) );
263 }
264 
265 
267 {
268  return add_circle_xpm;
269 }
270 
271 
272 void LIB_CIRCLE::BeginEdit( const wxPoint aPosition )
273 {
274  m_Pos = aPosition;
275 }
276 
277 
278 void LIB_CIRCLE::CalcEdit( const wxPoint& aPosition )
279 {
280  SetEnd( aPosition );
281 }
Definition: colors.h:57
EDA_UNITS
Definition: common.h:72
int GetPenSize() const override
Definition: lib_circle.cpp:189
double GetLineLength(const wxPoint &aPointA, const wxPoint &aPointB)
Function GetLineLength returns the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:202
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
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:127
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:50
#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:272
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:157
void print(wxDC *aDC, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
Definition: lib_circle.cpp:201
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
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:87
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
void MirrorVertical(const wxPoint &aCenter) override
Mirror the draw object along the MirrorVertical (Y) axis about aCenter point.
Definition: lib_circle.cpp:146
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
int compare(const LIB_ITEM &aOther) const override
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_circle.cpp:93
Define a library symbol object.
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
Definition: lib_circle.cpp:266
#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:257
Base plotter engine class.
Definition: plotter.h:103
LIB_CIRCLE(LIB_PART *aParent)
Definition: lib_circle.cpp:41
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
#define _(s)
Definition: 3d_actions.cpp:31
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
Definition: lib_circle.cpp:135
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:184
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:163
void Offset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
Definition: lib_circle.cpp:115
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:122
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:129
void CalcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
Definition: lib_circle.cpp:278
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:218
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:233
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:166
KICAD_T Type() const
Function Type()
Definition: base_struct.h:207
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
Definition: colors.h:62