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) 2012 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2004-2012 KiCad Developers, see change_log.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 <plot_common.h>
35 #include <trigo.h>
36 #include <wxstruct.h>
37 #include <richio.h>
38 #include <base_units.h>
39 #include <msgpanel.h>
40 #include <bitmaps.h>
41 
42 #include <general.h>
43 #include <lib_circle.h>
44 #include <transform.h>
45 
46 
48  LIB_ITEM( LIB_CIRCLE_T, aParent )
49 {
50  m_Radius = 0;
51  m_Width = 0;
52  m_Fill = NO_FILL;
53  m_isFillable = true;
54  m_typeName = _( "Circle" );
55 }
56 
57 
58 bool LIB_CIRCLE::Save( OUTPUTFORMATTER& aFormatter )
59 {
60  aFormatter.Print( 0, "C %d %d %d %d %d %d %c\n", m_Pos.x, m_Pos.y,
62 
63  return true;
64 }
65 
66 
67 bool LIB_CIRCLE::Load( LINE_READER& aLineReader, wxString& aErrorMsg )
68 {
69  char tmp[256];
70  char* line = (char*) aLineReader;
71 
72  int cnt = sscanf( line + 2, "%d %d %d %d %d %d %255s", &m_Pos.x, &m_Pos.y,
73  &m_Radius, &m_Unit, &m_Convert, &m_Width, tmp );
74 
75  if( cnt < 6 )
76  {
77  aErrorMsg.Printf( _( "Circle only had %d parameters of the required 6" ), cnt );
78  return false;
79  }
80 
81  if( tmp[0] == 'F' )
83 
84  if( tmp[0] == 'f' )
86 
87  return true;
88 }
89 
90 
91 bool LIB_CIRCLE::HitTest( const wxPoint& aPosRef ) const
92 {
93  int mindist = GetPenSize() / 2;
94 
95  // Have a minimal tolerance for hit test
96  if( mindist < MINIMUM_SELECTION_DISTANCE )
98 
99  return HitTest( aPosRef, mindist, DefaultTransform );
100 }
101 
102 
103 bool LIB_CIRCLE::HitTest( const wxPoint &aPosRef, int aThreshold, const TRANSFORM& aTransform ) const
104 {
105  if( aThreshold < 0 )
106  aThreshold = GetPenSize() / 2;
107 
108  int dist = KiROUND( GetLineLength( aPosRef, aTransform.TransformCoordinate( m_Pos ) ) );
109 
110  if( abs( dist - m_Radius ) <= aThreshold )
111  return true;
112  return false;
113 }
114 
115 
117 {
118  return new LIB_CIRCLE( *this );
119 }
120 
121 
122 int LIB_CIRCLE::compare( const LIB_ITEM& aOther ) const
123 {
124  wxASSERT( aOther.Type() == LIB_CIRCLE_T );
125 
126  const LIB_CIRCLE* tmp = ( LIB_CIRCLE* ) &aOther;
127 
128  if( m_Pos.x != tmp->m_Pos.x )
129  return m_Pos.x - tmp->m_Pos.x;
130 
131  if( m_Pos.y != tmp->m_Pos.y )
132  return m_Pos.y - tmp->m_Pos.y;
133 
134  if( m_Radius != tmp->m_Radius )
135  return m_Radius - tmp->m_Radius;
136 
137  return 0;
138 }
139 
140 
141 void LIB_CIRCLE::SetOffset( const wxPoint& aOffset )
142 {
143  m_Pos += aOffset;
144 }
145 
146 
147 bool LIB_CIRCLE::Inside( EDA_RECT& aRect ) const
148 {
149  wxPoint center(m_Pos.x, -m_Pos.y);
150  return aRect.IntersectsCircle( center, m_Radius );
151 }
152 
153 
154 void LIB_CIRCLE::Move( const wxPoint& aPosition )
155 {
156  m_Pos = aPosition;
157 }
158 
159 
160 void LIB_CIRCLE::MirrorHorizontal( const wxPoint& aCenter )
161 {
162  m_Pos.x -= aCenter.x;
163  m_Pos.x *= -1;
164  m_Pos.x += aCenter.x;
165 }
166 
167 
168 void LIB_CIRCLE::MirrorVertical( const wxPoint& aCenter )
169 {
170  m_Pos.y -= aCenter.y;
171  m_Pos.y *= -1;
172  m_Pos.y += aCenter.y;
173 }
174 
175 
176 void LIB_CIRCLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
177 {
178  int rot_angle = aRotateCCW ? -900 : 900;
179 
180  RotatePoint( &m_Pos, aCenter, rot_angle );
181 }
182 
183 
184 void LIB_CIRCLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
185  const TRANSFORM& aTransform )
186 {
187  wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
188 
189  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
190  {
192  aPlotter->Circle( pos, m_Radius * 2, FILLED_SHAPE, 0 );
193  }
194 
195  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
196  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
197  aPlotter->Circle( pos, m_Radius * 2, already_filled ? NO_FILL : m_Fill, GetPenSize() );
198 }
199 
200 
202 {
203  return ( m_Width == 0 ) ? GetDefaultLineThickness() : m_Width;
204 }
205 
206 
207 void LIB_CIRCLE::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
208  COLOR4D aColor, GR_DRAWMODE aDrawMode, void* aData,
209  const TRANSFORM& aTransform )
210 {
211  wxPoint pos1;
212 
214 
215  if( aColor == COLOR4D::UNSPECIFIED ) // Used normal color or selected color
216  {
217  if( IsSelected() )
218  color = GetItemSelectedColor();
219  }
220  else
221  {
222  color = aColor;
223  }
224 
225  pos1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
226  GRSetDrawMode( aDC, aDrawMode );
227 
228  FILL_T fill = aData ? NO_FILL : m_Fill;
229  if( aColor != COLOR4D::UNSPECIFIED )
230  fill = NO_FILL;
231 
232  EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
233  if( fill == FILLED_WITH_BG_BODYCOLOR )
234  GRFilledCircle( clipbox, aDC, pos1.x, pos1.y, m_Radius, GetPenSize(),
237  else if( fill == FILLED_SHAPE )
238  GRFilledCircle( clipbox, aDC, pos1.x, pos1.y, m_Radius, 0, color, color );
239  else
240  GRCircle( clipbox, aDC, pos1.x, pos1.y, m_Radius, GetPenSize(), color );
241 
242  /* Set to one (1) to draw bounding box around circle to validate bounding
243  * box calculation. */
244 #if 0
245  EDA_RECT bBox = GetBoundingBox();
246  bBox.RevertYAxis();
247  bBox = aTransform.TransformCoordinate( bBox );
248  bBox.Move( aOffset );
249  GRRect( clipbox, aDC, bBox, 0, LIGHTMAGENTA );
250 #endif
251 }
252 
253 
255 {
256  EDA_RECT rect;
257 
258  rect.SetOrigin( m_Pos.x - m_Radius, m_Pos.y - m_Radius );
259  rect.SetEnd( m_Pos.x + m_Radius, m_Pos.y + m_Radius );
260  rect.Inflate( ( GetPenSize()+1 ) / 2 );
261 
262  rect.RevertYAxis();
263 
264  return rect;
265 }
266 
267 
269 {
270  wxString msg;
271  EDA_RECT bBox = GetBoundingBox();
272 
273  LIB_ITEM::GetMsgPanelInfo( aList );
274 
275  msg = StringFromValue( g_UserUnit, m_Width, true );
276 
277  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
278 
279  msg = StringFromValue( g_UserUnit, m_Radius, true );
280  aList.push_back( MSG_PANEL_ITEM( _( "Radius" ), msg, RED ) );
281 
282  msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
283  bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
284 
285  aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
286 }
287 
288 
290 {
291  return wxString::Format( _( "Circle center (%s, %s), radius %s" ),
295 }
296 
297 
299 {
300  return add_circle_xpm;
301 }
302 
303 
304 void LIB_CIRCLE::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
305 {
306  wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
307  wxT( "Invalid edit mode for LIB_CIRCLE object." ) );
308 
309  if( aEditMode == IS_NEW )
310  {
311  m_Pos = m_initialPos = aPosition;
312  }
313  else if( aEditMode == IS_MOVED )
314  {
316  m_initialCursorPos = aPosition;
318  }
319  else if( aEditMode == IS_RESIZED )
320  {
322  }
323 
324  m_Flags = aEditMode;
325 }
326 
327 
328 bool LIB_CIRCLE::ContinueEdit( const wxPoint aPosition )
329 {
330  wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0, false,
331  wxT( "Bad call to ContinueEdit(). LIB_CIRCLE is not being edited." ) );
332 
333  return false;
334 }
335 
336 
337 void LIB_CIRCLE::EndEdit( const wxPoint& aPosition, bool aAbort )
338 {
339  wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
340  wxT( "Bad call to EndEdit(). LIB_CIRCLE is not being edited." ) );
341 
342  SetEraseLastDrawItem( false );
343  m_Flags = 0;
344 }
345 
346 
347 void LIB_CIRCLE::calcEdit( const wxPoint& aPosition )
348 {
349  if( m_Flags == IS_NEW || m_Flags == IS_RESIZED )
350  {
351  if( m_Flags == IS_NEW )
353 
354  m_Radius = KiROUND( GetLineLength( m_Pos, aPosition ) );
355  }
356  else
357  {
358  Move( m_initialPos + aPosition - m_initialCursorPos );
359  }
360 }
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:117
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212
Class LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived...
Definition: richio.h:81
void Move(const wxPoint &aMoveVector)
Function Move moves the rectangle by the aMoveVector.
int GetPenSize() const override
Function GetPenSize.
Definition: lib_circle.cpp:201
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
bool Load(LINE_READER &aLineReader, wxString &aErrorMsg) override
Definition: lib_circle.cpp:67
PNG memory record (file in memory).
Definition: bitmap_types.h:38
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:290
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
Function drawGraphic.
Definition: lib_circle.cpp:207
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:203
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:235
void calcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
Definition: lib_circle.cpp:347
void SetOrigin(const wxPoint &pos)
int m_Unit
Unit identification for multiple parts per package.
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
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:833
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: lib_circle.cpp:116
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size) ...
#define abs(a)
Definition: auxiliary.h:84
Class LIB_ITEM is 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.
bool Save(OUTPUTFORMATTER &aFormatter) override
Function Save writes draw item object to aFormatter in component library "*.lib" format.
Definition: lib_circle.cpp:58
#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
const wxPoint & GetOrigin() const
void GRRect(EDA_RECT *aClipBox, wxDC *aDC, int x1, int y1, int x2, int y2, COLOR4D aColor)
Definition: gr_basic.cpp:1077
bool ContinueEdit(const wxPoint aNextPoint) override
Continue an edit in progress at aPosition.
Definition: lib_circle.cpp:328
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:91
void SetOffset(const wxPoint &aOffset) override
Function Offset sets the drawing object by aOffset from the current position.
Definition: lib_circle.cpp:141
GR_DRAWMODE
Drawmode. Compositing mode plus a flag or two.
Definition: gr_basic.h:41
Base window classes and related definitions.
void MirrorVertical(const wxPoint &aCenter) override
Function MirrorVertical mirrors the draw object along the MirrorVertical (Y) axis about aCenter point...
Definition: lib_circle.cpp:168
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
Function compare provides the draw object specific comparison called by the == and < operators...
Definition: lib_circle.cpp:122
Class LIB_PART defines a library part 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:298
EDA_RECT * GetClipBox()
Common plot library Plot settings, and plotting engines (Postscript, Gerber, HPGL and DXF) ...
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:337
void Move(const wxPoint &aPosition) override
Function Move moves a draw object to aPosition.
Definition: lib_circle.cpp:154
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:289
Base plotter engine class.
Definition: plot_common.h:86
virtual void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo displays basic info (type, part and convert) about the current item in messa...
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:47
const int fill_tab[3]
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
Function MirrorHorizontal mirrors the draw object along the horizontal (X) axis about aCenter point...
Definition: lib_circle.cpp:160
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:791
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:189
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:165
#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
Function Inside tests if any part of the draw object is inside rectangle bounds of aRect...
Definition: lib_circle.cpp:147
int m_Convert
Shape identification for alternate body styles.
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
Function GetMsgPanelInfo displays basic info (type, part and convert) about the current item in messa...
Definition: lib_circle.cpp:268
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:304
Class EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:408
Message panel definition file.
const EDA_RECT GetBoundingBox() const override
Definition: lib_circle.cpp:254
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:184
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