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