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-2017 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 
30 #include <fctsys.h>
31 #include <gr_basic.h>
32 #include <macros.h>
33 #include <class_drawpanel.h>
34 #include <class_plotter.h>
35 #include <trigo.h>
36 #include <wxstruct.h>
37 #include <base_units.h>
38 #include <msgpanel.h>
39 #include <bitmaps.h>
40 
41 #include <general.h>
42 #include <lib_circle.h>
43 #include <transform.h>
44 
45 
47  LIB_ITEM( LIB_CIRCLE_T, aParent )
48 {
49  m_Radius = 0;
50  m_Width = 0;
51  m_Fill = NO_FILL;
52  m_isFillable = true;
53  m_typeName = _( "Circle" );
54 }
55 
56 
57 bool LIB_CIRCLE::HitTest( const wxPoint& aPosRef ) const
58 {
59  int mindist = GetPenSize() / 2;
60 
61  // Have a minimal tolerance for hit test
62  if( mindist < MINIMUM_SELECTION_DISTANCE )
64 
65  return HitTest( aPosRef, mindist, DefaultTransform );
66 }
67 
68 
69 bool LIB_CIRCLE::HitTest( const wxPoint &aPosRef, int aThreshold, const TRANSFORM& aTransform ) const
70 {
71  if( aThreshold < 0 )
72  aThreshold = GetPenSize() / 2;
73 
74  int dist = KiROUND( GetLineLength( aPosRef, aTransform.TransformCoordinate( m_Pos ) ) );
75 
76  if( abs( dist - m_Radius ) <= aThreshold )
77  return true;
78  return false;
79 }
80 
81 
83 {
84  return new LIB_CIRCLE( *this );
85 }
86 
87 
88 int LIB_CIRCLE::compare( const LIB_ITEM& aOther ) const
89 {
90  wxASSERT( aOther.Type() == LIB_CIRCLE_T );
91 
92  const LIB_CIRCLE* tmp = ( LIB_CIRCLE* ) &aOther;
93 
94  if( m_Pos.x != tmp->m_Pos.x )
95  return m_Pos.x - tmp->m_Pos.x;
96 
97  if( m_Pos.y != tmp->m_Pos.y )
98  return m_Pos.y - tmp->m_Pos.y;
99 
100  if( m_Radius != tmp->m_Radius )
101  return m_Radius - tmp->m_Radius;
102 
103  return 0;
104 }
105 
106 
107 void LIB_CIRCLE::SetOffset( const wxPoint& aOffset )
108 {
109  m_Pos += aOffset;
110 }
111 
112 
113 bool LIB_CIRCLE::Inside( EDA_RECT& aRect ) const
114 {
115  wxPoint center(m_Pos.x, -m_Pos.y);
116  return aRect.IntersectsCircle( center, m_Radius );
117 }
118 
119 
120 void LIB_CIRCLE::Move( const wxPoint& aPosition )
121 {
122  m_Pos = aPosition;
123 }
124 
125 
126 void LIB_CIRCLE::MirrorHorizontal( const wxPoint& aCenter )
127 {
128  m_Pos.x -= aCenter.x;
129  m_Pos.x *= -1;
130  m_Pos.x += aCenter.x;
131 }
132 
133 
134 void LIB_CIRCLE::MirrorVertical( const wxPoint& aCenter )
135 {
136  m_Pos.y -= aCenter.y;
137  m_Pos.y *= -1;
138  m_Pos.y += aCenter.y;
139 }
140 
141 
142 void LIB_CIRCLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
143 {
144  int rot_angle = aRotateCCW ? -900 : 900;
145 
146  RotatePoint( &m_Pos, aCenter, rot_angle );
147 }
148 
149 
150 void LIB_CIRCLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
151  const TRANSFORM& aTransform )
152 {
153  wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
154 
155  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
156  {
158  aPlotter->Circle( pos, m_Radius * 2, FILLED_WITH_BG_BODYCOLOR, 0 );
159  }
160 
161  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
162  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
163  aPlotter->Circle( pos, m_Radius * 2, already_filled ? NO_FILL : m_Fill, GetPenSize() );
164 }
165 
166 
168 {
169  return ( m_Width == 0 ) ? GetDefaultLineThickness() : m_Width;
170 }
171 
172 
173 void LIB_CIRCLE::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
174  COLOR4D aColor, GR_DRAWMODE aDrawMode, void* aData,
175  const TRANSFORM& aTransform )
176 {
177  wxPoint pos1;
178 
180 
181  if( aColor == COLOR4D::UNSPECIFIED ) // Used normal color or selected color
182  {
183  if( IsSelected() )
184  color = GetItemSelectedColor();
185  }
186  else
187  {
188  color = aColor;
189  }
190 
191  pos1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
192  GRSetDrawMode( aDC, aDrawMode );
193 
194  FILL_T fill = aData ? NO_FILL : m_Fill;
195  if( aColor != COLOR4D::UNSPECIFIED )
196  fill = NO_FILL;
197 
198  EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
199  if( fill == FILLED_WITH_BG_BODYCOLOR )
200  GRFilledCircle( clipbox, aDC, pos1.x, pos1.y, m_Radius, GetPenSize(),
203  else if( fill == FILLED_SHAPE )
204  GRFilledCircle( clipbox, aDC, pos1.x, pos1.y, m_Radius, 0, color, color );
205  else
206  GRCircle( clipbox, aDC, pos1.x, pos1.y, m_Radius, GetPenSize(), color );
207 
208  /* Set to one (1) to draw bounding box around circle to validate bounding
209  * box calculation. */
210 #if 0
211  EDA_RECT bBox = GetBoundingBox();
212  bBox.RevertYAxis();
213  bBox = aTransform.TransformCoordinate( bBox );
214  bBox.Move( aOffset );
215  GRRect( clipbox, aDC, bBox, 0, LIGHTMAGENTA );
216 #endif
217 }
218 
219 
221 {
222  EDA_RECT rect;
223 
224  rect.SetOrigin( m_Pos.x - m_Radius, m_Pos.y - m_Radius );
225  rect.SetEnd( m_Pos.x + m_Radius, m_Pos.y + m_Radius );
226  rect.Inflate( ( GetPenSize()+1 ) / 2 );
227 
228  rect.RevertYAxis();
229 
230  return rect;
231 }
232 
233 
235 {
236  wxString msg;
237  EDA_RECT bBox = GetBoundingBox();
238 
239  LIB_ITEM::GetMsgPanelInfo( aList );
240 
241  msg = StringFromValue( g_UserUnit, m_Width, true );
242 
243  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
244 
245  msg = StringFromValue( g_UserUnit, m_Radius, true );
246  aList.push_back( MSG_PANEL_ITEM( _( "Radius" ), msg, RED ) );
247 
248  msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
249  bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
250 
251  aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
252 }
253 
254 
256 {
257  return wxString::Format( _( "Circle center (%s, %s), radius %s" ),
261 }
262 
263 
265 {
266  return add_circle_xpm;
267 }
268 
269 
270 void LIB_CIRCLE::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
271 {
272  wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
273  wxT( "Invalid edit mode for LIB_CIRCLE object." ) );
274 
275  if( aEditMode == IS_NEW )
276  {
277  m_Pos = m_initialPos = aPosition;
278  }
279  else if( aEditMode == IS_MOVED )
280  {
282  m_initialCursorPos = aPosition;
284  }
285  else if( aEditMode == IS_RESIZED )
286  {
288  }
289 
290  m_Flags = aEditMode;
291 }
292 
293 
294 bool LIB_CIRCLE::ContinueEdit( const wxPoint aPosition )
295 {
296  wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0, false,
297  wxT( "Bad call to ContinueEdit(). LIB_CIRCLE is not being edited." ) );
298 
299  return false;
300 }
301 
302 
303 void LIB_CIRCLE::EndEdit( const wxPoint& aPosition, bool aAbort )
304 {
305  wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
306  wxT( "Bad call to EndEdit(). LIB_CIRCLE is not being edited." ) );
307 
308  SetEraseLastDrawItem( false );
309  m_Flags = 0;
310 }
311 
312 
313 void LIB_CIRCLE::calcEdit( const wxPoint& aPosition )
314 {
315  if( m_Flags == IS_NEW || m_Flags == IS_RESIZED )
316  {
317  if( m_Flags == IS_NEW )
319 
320  m_Radius = KiROUND( GetLineLength( m_Pos, aPosition ) );
321  }
322  else
323  {
324  Move( m_initialPos + aPosition - m_initialCursorPos );
325  }
326 }
Definition: colors.h:57
wxString CoordinateToString(int aValue, bool aConvertToMils)
Function CoordinateToString is a helper to convert the integer coordinate aValue to a string in inche...
Definition: base_units.cpp:118
KICAD_T Type() const
Function Type()
Definition: base_struct.h:225
void Move(const wxPoint &aMoveVector)
Function Move moves the rectangle by the aMoveVector.
int GetPenSize() const override
Definition: lib_circle.cpp:167
const wxPoint GetOrigin() const
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:191
PNG memory record (file in memory).
Definition: bitmap_types.h:41
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:318
wxString m_typeName
Name of object displayed in the message panel.
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
void SetEraseLastDrawItem(bool aErase=true)
Implementation of conversion functions that require both schematic and board internal units...
void drawGraphic(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode, void *aData, const TRANSFORM &aTransform) override
Draw the item on aPanel.
Definition: lib_circle.cpp:173
virtual void SetColor(COLOR4D color)=0
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_draw_item.h:52
bool IntersectsCircle(const wxPoint &aCenter, const int aRadius) const
Function IntersectsCircle tests for a common area between a circle and this rectangle.
wxString StringFromValue(EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:204
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i...
static const int dist[10][10]
Definition: dist.cpp:57
bool IsSelected() const
Definition: base_struct.h:248
void calcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
Definition: lib_circle.cpp:313
void SetOrigin(const wxPoint &pos)
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
wxPoint m_Pos
Definition: lib_circle.h:38
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:167
void GRFilledCircle(EDA_RECT *ClipBox, wxDC *DC, int x, int y, int r, int width, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:873
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: lib_circle.cpp:82
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size) ...
#define abs(a)
Definition: auxiliary.h:84
The base class for drawable items used by schematic library components.
Definition: lib_draw_item.h:66
This file contains miscellaneous commonly used macros and functions.
#define IS_NEW
New item, just created.
Definition: base_struct.h:127
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:41
void GRRect(EDA_RECT *aClipBox, wxDC *aDC, int x1, int y1, int x2, int y2, COLOR4D aColor)
Definition: gr_basic.cpp:1098
bool ContinueEdit(const wxPoint aNextPoint) override
Continue an edit in progress at aPosition.
Definition: lib_circle.cpp:294
Class for tranforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
bool HitTest(const wxPoint &aPosition) const override
Function HitTest tests if aPosition is contained within or on the bounding area of an item...
Definition: lib_circle.cpp:57
void SetOffset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
Definition: lib_circle.cpp:107
GR_DRAWMODE
Drawmode. Compositing mode plus a flag or two.
Definition: gr_basic.h:37
Base window classes and related definitions.
void MirrorVertical(const wxPoint &aCenter) override
Mirror the draw object along the MirrorVertical (Y) axis about aCenter point.
Definition: lib_circle.cpp:134
void SetEnd(int x, int y)
bool m_isFillable
Flag to indicate if draw item is fillable.
int compare(const LIB_ITEM &aOther) const override
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_circle.cpp:88
Define a library symbol object.
unsigned STATUS_FLAGS
Definition: base_struct.h:158
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
Definition: lib_circle.cpp:264
EDA_RECT * GetClipBox()
Definition: colors.h:60
virtual void Rotate()
Rotate the draw item.
void EndEdit(const wxPoint &aPosition, bool aAbort=false) override
End an object editing action.
Definition: lib_circle.cpp:303
void Move(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_circle.cpp:120
const wxPoint GetEnd() const
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
wxString GetSelectMenuText() const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
Definition: lib_circle.cpp:255
Base plotter engine class.
Definition: class_plotter.h:96
virtual void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
TRANSFORM DefaultTransform
Definition: eeschema.cpp:58
int m_Radius
Definition: lib_circle.h:37
LIB_CIRCLE(LIB_PART *aParent)
Definition: lib_circle.cpp:46
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
wxPoint m_initialPos
Temporary position when moving an existing item.
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
Definition: lib_circle.cpp:126
FILL_T m_Fill
The body fill type.
void GRCircle(EDA_RECT *ClipBox, wxDC *DC, int xc, int yc, int r, int width, COLOR4D Color)
Definition: gr_basic.cpp:850
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:202
Class EDA_RECT handles the component boundary box.
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:178
#define IS_RESIZED
Item being resized.
Definition: base_struct.h:128
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:113
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:70
void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
Definition: lib_circle.cpp:234
COLOR4D GetItemSelectedColor()
int m_Width
Definition: lib_circle.h:39
void BeginEdit(STATUS_FLAGS aEditMode, const wxPoint aStartPoint=wxPoint(0, 0)) override
Begin an editing a component library draw item in aEditMode at aPosition.
Definition: lib_circle.cpp:270
Class 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
Definition: lib_circle.cpp:220
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)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
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:150
wxPoint m_initialCursorPos
Initial cursor position at the beginning of a move.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
#define IS_MOVED
Item being moved.
Definition: base_struct.h:126
Definition: colors.h:62