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  /*
150  * FIXME: This fails to take into account the radius around the center
151  * point.
152  */
153  return aRect.Contains( m_Pos.x, -m_Pos.y );
154 }
155 
156 
157 void LIB_CIRCLE::Move( const wxPoint& aPosition )
158 {
159  m_Pos = aPosition;
160 }
161 
162 
163 void LIB_CIRCLE::MirrorHorizontal( const wxPoint& aCenter )
164 {
165  m_Pos.x -= aCenter.x;
166  m_Pos.x *= -1;
167  m_Pos.x += aCenter.x;
168 }
169 
170 
171 void LIB_CIRCLE::MirrorVertical( const wxPoint& aCenter )
172 {
173  m_Pos.y -= aCenter.y;
174  m_Pos.y *= -1;
175  m_Pos.y += aCenter.y;
176 }
177 
178 
179 void LIB_CIRCLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
180 {
181  int rot_angle = aRotateCCW ? -900 : 900;
182 
183  RotatePoint( &m_Pos, aCenter, rot_angle );
184 }
185 
186 
187 void LIB_CIRCLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
188  const TRANSFORM& aTransform )
189 {
190  wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
191 
192  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
193  {
195  aPlotter->Circle( pos, m_Radius * 2, FILLED_SHAPE, 0 );
196  }
197 
198  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
199  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
200  aPlotter->Circle( pos, m_Radius * 2, already_filled ? NO_FILL : m_Fill, GetPenSize() );
201 }
202 
203 
205 {
206  return ( m_Width == 0 ) ? GetDefaultLineThickness() : m_Width;
207 }
208 
209 
210 void LIB_CIRCLE::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
211  COLOR4D aColor, GR_DRAWMODE aDrawMode, void* aData,
212  const TRANSFORM& aTransform )
213 {
214  wxPoint pos1;
215 
217 
218  if( aColor == COLOR4D::UNSPECIFIED ) // Used normal color or selected color
219  {
220  if( IsSelected() )
221  color = GetItemSelectedColor();
222  }
223  else
224  {
225  color = aColor;
226  }
227 
228  pos1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
229  GRSetDrawMode( aDC, aDrawMode );
230 
231  FILL_T fill = aData ? NO_FILL : m_Fill;
232  if( aColor != COLOR4D::UNSPECIFIED )
233  fill = NO_FILL;
234 
235  EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
236  if( fill == FILLED_WITH_BG_BODYCOLOR )
237  GRFilledCircle( clipbox, aDC, pos1.x, pos1.y, m_Radius, GetPenSize(),
240  else if( fill == FILLED_SHAPE )
241  GRFilledCircle( clipbox, aDC, pos1.x, pos1.y, m_Radius, 0, color, color );
242  else
243  GRCircle( clipbox, aDC, pos1.x, pos1.y, m_Radius, GetPenSize(), color );
244 
245  /* Set to one (1) to draw bounding box around circle to validate bounding
246  * box calculation. */
247 #if 0
248  EDA_RECT bBox = GetBoundingBox();
249  bBox.RevertYAxis();
250  bBox = aTransform.TransformCoordinate( bBox );
251  bBox.Move( aOffset );
252  GRRect( clipbox, aDC, bBox, 0, LIGHTMAGENTA );
253 #endif
254 }
255 
256 
258 {
259  EDA_RECT rect;
260 
261  rect.SetOrigin( m_Pos.x - m_Radius, m_Pos.y - m_Radius );
262  rect.SetEnd( m_Pos.x + m_Radius, m_Pos.y + m_Radius );
263  rect.Inflate( ( GetPenSize()+1 ) / 2 );
264 
265  rect.RevertYAxis();
266 
267  return rect;
268 }
269 
270 
272 {
273  wxString msg;
274  EDA_RECT bBox = GetBoundingBox();
275 
276  LIB_ITEM::GetMsgPanelInfo( aList );
277 
278  msg = StringFromValue( g_UserUnit, m_Width, true );
279 
280  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
281 
282  msg = StringFromValue( g_UserUnit, m_Radius, true );
283  aList.push_back( MSG_PANEL_ITEM( _( "Radius" ), msg, RED ) );
284 
285  msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
286  bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
287 
288  aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
289 }
290 
291 
293 {
294  return wxString::Format( _( "Circle center (%s, %s), radius %s" ),
298 }
299 
300 
302 {
303  return add_circle_xpm;
304 }
305 
306 
307 void LIB_CIRCLE::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
308 {
309  wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
310  wxT( "Invalid edit mode for LIB_CIRCLE object." ) );
311 
312  if( aEditMode == IS_NEW )
313  {
314  m_Pos = m_initialPos = aPosition;
315  }
316  else if( aEditMode == IS_MOVED )
317  {
319  m_initialCursorPos = aPosition;
321  }
322  else if( aEditMode == IS_RESIZED )
323  {
325  }
326 
327  m_Flags = aEditMode;
328 }
329 
330 
331 bool LIB_CIRCLE::ContinueEdit( const wxPoint aPosition )
332 {
333  wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0, false,
334  wxT( "Bad call to ContinueEdit(). LIB_CIRCLE is not being edited." ) );
335 
336  return false;
337 }
338 
339 
340 void LIB_CIRCLE::EndEdit( const wxPoint& aPosition, bool aAbort )
341 {
342  wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
343  wxT( "Bad call to EndEdit(). LIB_CIRCLE is not being edited." ) );
344 
345  SetEraseLastDrawItem( false );
346  m_Flags = 0;
347 }
348 
349 
350 void LIB_CIRCLE::calcEdit( const wxPoint& aPosition )
351 {
352  if( m_Flags == IS_NEW || m_Flags == IS_RESIZED )
353  {
354  if( m_Flags == IS_NEW )
356 
357  m_Radius = KiROUND( GetLineLength( m_Pos, aPosition ) );
358  }
359  else
360  {
361  Move( m_initialPos + aPosition - m_initialCursorPos );
362  }
363 }
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:198
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:204
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:183
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:128
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:210
virtual void SetColor(COLOR4D color)=0
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_draw_item.h:54
bool Contains(const wxPoint &aPoint) const
Function Contains.
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:221
void calcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
Definition: lib_circle.cpp:350
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
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:77
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
COLOR4D GetLayerColor(LAYERSCH_ID aLayer)
Definition: eeschema.cpp:166
#define IS_NEW
New item, just created.
Definition: base_struct.h:113
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:331
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:171
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:144
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
Definition: lib_circle.cpp:301
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:340
void Move(const wxPoint &aPosition) override
Function Move moves a draw object to aPosition.
Definition: lib_circle.cpp:157
const wxPoint GetEnd() const
EDA_UNITS_T g_UserUnit
display units
Definition: common.cpp:57
wxString GetSelectMenuText() const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
Definition: lib_circle.cpp:292
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:57
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:163
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:175
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:151
#define IS_RESIZED
Item being resized.
Definition: base_struct.h:114
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:56
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:271
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:307
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:257
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:187
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:112
Definition: colors.h:62