KiCad PCB EDA Suite
lib_rectangle.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 
29 #include <fctsys.h>
30 #include <gr_basic.h>
31 #include <macros.h>
32 #include <class_drawpanel.h>
33 #include <plot_common.h>
34 #include <trigo.h>
35 #include <wxstruct.h>
36 #include <richio.h>
37 #include <base_units.h>
38 #include <msgpanel.h>
39 #include <bitmaps.h>
40 
41 #include <general.h>
42 #include <lib_rectangle.h>
43 #include <transform.h>
44 
45 
47  LIB_ITEM( LIB_RECTANGLE_T, aParent )
48 {
49  m_Width = 0;
50  m_Fill = NO_FILL;
51  m_isFillable = true;
52  m_typeName = _( "Rectangle" );
53  m_isHeightLocked = false;
54  m_isWidthLocked = false;
55  m_isStartPointSelected = false;
56 }
57 
58 
60 {
61  aFormatter.Print( 0, "S %d %d %d %d %d %d %d %c\n", m_Pos.x, m_Pos.y,
63 
64  return true;
65 }
66 
67 
68 bool LIB_RECTANGLE::Load( LINE_READER& aLineReader, wxString& aErrorMsg )
69 {
70  int cnt;
71  char tmp[256] = "";
72  char* line = (char*)aLineReader;
73 
74  cnt = sscanf( line + 2, "%d %d %d %d %d %d %d %255s", &m_Pos.x, &m_Pos.y,
75  &m_End.x, &m_End.y, &m_Unit, &m_Convert, &m_Width, tmp );
76 
77  if( cnt < 7 )
78  {
79  aErrorMsg.Printf( _( "Rectangle only had %d parameters of the required 7" ), cnt );
80  return false;
81  }
82 
83  if( tmp[0] == 'F' )
85 
86  if( tmp[0] == 'f' )
88 
89  return true;
90 }
91 
92 
94 {
95  return new LIB_RECTANGLE( *this );
96 }
97 
98 
99 int LIB_RECTANGLE::compare( const LIB_ITEM& aOther ) const
100 {
101  wxASSERT( aOther.Type() == LIB_RECTANGLE_T );
102 
103  const LIB_RECTANGLE* tmp = ( LIB_RECTANGLE* ) &aOther;
104 
105  if( m_Pos.x != tmp->m_Pos.x )
106  return m_Pos.x - tmp->m_Pos.x;
107 
108  if( m_Pos.y != tmp->m_Pos.y )
109  return m_Pos.y - tmp->m_Pos.y;
110 
111  if( m_End.x != tmp->m_End.x )
112  return m_End.x - tmp->m_End.x;
113 
114  if( m_End.y != tmp->m_End.y )
115  return m_End.y - tmp->m_End.y;
116 
117  return 0;
118 }
119 
120 
121 void LIB_RECTANGLE::SetOffset( const wxPoint& aOffset )
122 {
123  m_Pos += aOffset;
124  m_End += aOffset;
125 }
126 
127 
128 bool LIB_RECTANGLE::Inside( EDA_RECT& aRect ) const
129 {
130  return aRect.Contains( m_Pos.x, -m_Pos.y ) || aRect.Contains( m_End.x, -m_End.y );
131 }
132 
133 
134 void LIB_RECTANGLE::Move( const wxPoint& aPosition )
135 {
136  wxPoint size = m_End - m_Pos;
137  m_Pos = aPosition;
138  m_End = aPosition + size;
139 }
140 
141 
143 {
144  m_Pos.x -= aCenter.x;
145  m_Pos.x *= -1;
146  m_Pos.x += aCenter.x;
147  m_End.x -= aCenter.x;
148  m_End.x *= -1;
149  m_End.x += aCenter.x;
150 }
151 
152 
154 {
155  m_Pos.y -= aCenter.y;
156  m_Pos.y *= -1;
157  m_Pos.y += aCenter.y;
158  m_End.y -= aCenter.y;
159  m_End.y *= -1;
160  m_End.y += aCenter.y;
161 }
162 
163 
164 void LIB_RECTANGLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
165 {
166  int rot_angle = aRotateCCW ? -900 : 900;
167  RotatePoint( &m_Pos, aCenter, rot_angle );
168  RotatePoint( &m_End, aCenter, rot_angle );
169 }
170 
171 
172 void LIB_RECTANGLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
173  const TRANSFORM& aTransform )
174 {
175  wxASSERT( aPlotter != NULL );
176 
177  wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
178  wxPoint end = aTransform.TransformCoordinate( m_End ) + aOffset;
179 
180  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
181  {
183  aPlotter->Rect( pos, end, FILLED_WITH_BG_BODYCOLOR, 0 );
184  }
185 
186  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
187  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
188  aPlotter->Rect( pos, end, already_filled ? NO_FILL : m_Fill, GetPenSize() );
189 }
190 
191 
193 {
194  return ( m_Width == 0 ) ? GetDefaultLineThickness() : m_Width;
195 }
196 
197 
199  const wxPoint& aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode,
200  void* aData, const TRANSFORM& aTransform )
201 {
202  wxPoint pos1, pos2;
203 
205 
206  if( aColor == COLOR4D::UNSPECIFIED ) // Used normal color or selected color
207  {
208  if( IsSelected() )
209  color = GetItemSelectedColor();
210  }
211  else
212  {
213  color = aColor;
214  }
215 
216  pos1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
217  pos2 = aTransform.TransformCoordinate( m_End ) + aOffset;
218 
219  FILL_T fill = aData ? NO_FILL : m_Fill;
220 
221  if( aColor != COLOR4D::UNSPECIFIED )
222  fill = NO_FILL;
223 
224  GRSetDrawMode( aDC, aDrawMode );
225 
226  EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
227  if( fill == FILLED_WITH_BG_BODYCOLOR && !aData )
228  GRFilledRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y, GetPenSize( ),
231  else if( m_Fill == FILLED_SHAPE && !aData )
232  GRFilledRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y,
233  GetPenSize(), color, color );
234  else
235  GRRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y, GetPenSize(), color );
236 
237  /* Set to one (1) to draw bounding box around rectangle to validate
238  * bounding box calculation. */
239 #if 0
240  EDA_RECT bBox = GetBoundingBox();
241  bBox.RevertYAxis();
242  bBox = aTransform.TransformCoordinate( bBox );
243  bBox.Move( aOffset );
244  GRRect( clipbox, aDC, bBox, 0, LIGHTMAGENTA );
245 #endif
246 }
247 
248 
250 {
251  wxString msg;
252 
253  LIB_ITEM::GetMsgPanelInfo( aList );
254 
255  msg = StringFromValue( g_UserUnit, m_Width, true );
256 
257  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
258 }
259 
260 
262 {
263  EDA_RECT rect;
264 
265  rect.SetOrigin( m_Pos );
266  rect.SetEnd( m_End );
267  rect.Inflate( ( GetPenSize()+1 ) / 2 );
268 
269  rect.RevertYAxis();
270 
271  return rect;
272 }
273 
274 
275 bool LIB_RECTANGLE::HitTest( const wxPoint& aPosition ) const
276 {
277  int mindist = ( GetPenSize() / 2 ) + 1;
278 
279  // Have a minimal tolerance for hit test
280  if( mindist < MINIMUM_SELECTION_DISTANCE )
281  mindist = MINIMUM_SELECTION_DISTANCE;
282 
283  return HitTest( aPosition, mindist, DefaultTransform );
284 }
285 
286 
287 bool LIB_RECTANGLE::HitTest( const wxPoint &aPosition, int aThreshold, const TRANSFORM& aTransform ) const
288 {
289  if( aThreshold < 0 )
290  aThreshold = GetPenSize() / 2;
291 
292  wxPoint actualStart = aTransform.TransformCoordinate( m_Pos );
293  wxPoint actualEnd = aTransform.TransformCoordinate( m_End );
294 
295  // locate lower segment
296  wxPoint start, end;
297 
298  start = actualStart;
299  end.x = actualEnd.x;
300  end.y = actualStart.y;
301 
302  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
303  return true;
304 
305  // locate right segment
306  start.x = actualEnd.x;
307  end.y = actualEnd.y;
308 
309  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
310  return true;
311 
312  // locate upper segment
313  start.y = actualEnd.y;
314  end.x = actualStart.x;
315 
316  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
317  return true;
318 
319  // locate left segment
320  start = actualStart;
321  end.x = actualStart.x;
322  end.y = actualEnd.y;
323 
324  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
325  return true;
326 
327  return false;
328 }
329 
330 
332 {
333  return wxString::Format( _( "Rectangle from (%s, %s) to (%s, %s)" ),
338 }
339 
340 
342 {
343  return add_rectangle_xpm;
344 }
345 
346 
347 void LIB_RECTANGLE::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
348 {
349  wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
350  wxT( "Invalid edit mode for LIB_RECTANGLE object." ) );
351 
352  if( aEditMode == IS_NEW )
353  {
354  m_Pos = m_End = aPosition;
355  }
356  else if( aEditMode == IS_RESIZED )
357  {
359  || abs( m_Pos.y - aPosition.y ) < MINIMUM_SELECTION_DISTANCE;
360 
362  {
365  }
366  else
367  {
370  }
371 
373  }
374  else if( aEditMode == IS_MOVED )
375  {
377  m_initialCursorPos = aPosition;
379  }
380 
381  m_Flags = aEditMode;
382 }
383 
384 
385 bool LIB_RECTANGLE::ContinueEdit( const wxPoint aPosition )
386 {
387  wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0, false,
388  wxT( "Bad call to ContinueEdit(). LIB_RECTANGLE is not being edited." ) );
389 
390  return false;
391 }
392 
393 
394 void LIB_RECTANGLE::EndEdit( const wxPoint& aPosition, bool aAbort )
395 {
396  wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
397  wxT( "Bad call to EndEdit(). LIB_RECTANGLE is not being edited." ) );
398 
399  m_Flags = 0;
400  m_isHeightLocked = false;
401  m_isWidthLocked = false;
402  SetEraseLastDrawItem( false );
403 }
404 
405 
406 void LIB_RECTANGLE::calcEdit( const wxPoint& aPosition )
407 {
408  if( m_Flags == IS_NEW )
409  {
410  m_End = aPosition;
412  }
413  else if( m_Flags == IS_RESIZED )
414  {
415  if( m_isHeightLocked )
416  {
418  m_Pos.x = aPosition.x;
419  else
420  m_End.x = aPosition.x;
421  }
422  else if( m_isWidthLocked )
423  {
425  m_Pos.y = aPosition.y;
426  else
427  m_End.y = aPosition.y;
428  }
429  else
430  {
432  m_Pos = aPosition;
433  else
434  m_End = aPosition;
435  }
436  }
437  else if( m_Flags == IS_MOVED )
438  {
439  Move( m_initialPos + aPosition - m_initialCursorPos );
440  }
441 }
Definition: colors.h:57
void SetOffset(const wxPoint &aOffset) override
Function Offset sets the drawing object by aOffset from the current position.
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
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
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.
void MirrorHorizontal(const wxPoint &aCenter) override
Function MirrorHorizontal mirrors the draw object along the horizontal (X) axis about aCenter point...
void BeginEdit(STATUS_FLAGS aEditMode, const wxPoint aStartPoint=wxPoint(0, 0)) override
Begin an editing a component library draw item in aEditMode at aPosition.
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.
void SetEraseLastDrawItem(bool aErase=true)
bool Save(OUTPUTFORMATTER &aFormatter) override
Function Save writes draw item object to aFormatter in component library "*.lib" format.
Implementation of conversion functions that require both schematic and board internal units...
virtual void SetColor(COLOR4D color)=0
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_draw_item.h:52
bool Contains(const wxPoint &aPoint) const
Function Contains.
void GRFilledRect(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:1117
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 GetPenSize() const override
Function GetPenSize.
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i...
int compare(const LIB_ITEM &aOther) const override
Function compare provides the draw object specific comparison called by the == and < operators...
bool IsSelected() const
Definition: base_struct.h:235
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
bool Inside(EDA_RECT &aRect) const override
Function Inside tests if any part of the draw object is inside rectangle bounds of aRect...
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:167
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size) ...
bool m_isHeightLocked
Definition: lib_rectangle.h:41
#define abs(a)
Definition: auxiliary.h:84
void calcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
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.
wxString GetSelectMenuText() const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
#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:1077
Class for tranforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
GR_DRAWMODE
Drawmode. Compositing mode plus a flag or two.
Definition: gr_basic.h:41
Base window classes and related definitions.
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
void SetEnd(int x, int y)
bool ContinueEdit(const wxPoint aNextPoint) override
Continue an edit in progress at aPosition.
bool m_isFillable
Flag to indicate if draw item is fillable.
Class LIB_PART defines a library part object.
unsigned STATUS_FLAGS
Definition: base_struct.h:158
EDA_RECT * GetClipBox()
Common plot library Plot settings, and plotting engines (Postscript, Gerber, HPGL and DXF) ...
virtual void Rotate()
Rotate the draw item.
bool Load(LINE_READER &aLineReader, wxString &aErrorMsg) override
void MirrorVertical(const wxPoint &aCenter) override
Function MirrorVertical mirrors the draw object along the MirrorVertical (Y) axis about aCenter point...
void drawGraphic(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode, void *aData, const TRANSFORM &aTransform) override
Function drawGraphic.
bool m_isWidthLocked
Definition: lib_rectangle.h:40
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
void Move(const wxPoint &aPosition) override
Function Move moves a draw object to aPosition.
void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo displays basic info (type, part and convert) about the current item in messa...
Base plotter engine class.
Definition: plot_common.h:86
LIB_RECTANGLE(LIB_PART *aParent)
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
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
void Plot(PLOTTER *aPlotter, const wxPoint &aOffset, bool aFill, const TRANSFORM &aTransform) override
Plot the draw item using the plot object.
wxPoint m_initialPos
Temporary position when moving an existing item.
FILL_T m_Fill
The body fill type.
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:189
Class EDA_RECT handles the component boundary box.
bool HitTest(const wxPoint &aPosition) const override
Function HitTest tests if aPosition is contained within or on the bounding area of an item...
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:165
void EndEdit(const wxPoint &aPosition, bool aAbort=false) override
End an object editing action.
#define IS_RESIZED
Item being resized.
Definition: base_struct.h:128
virtual void Rect(const wxPoint &p1, const wxPoint &p2, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH)=0
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:102
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
COLOR4D GetItemSelectedColor()
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
bool m_isStartPointSelected
Definition: lib_rectangle.h:42
bool TestSegmentHit(const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist)
Function TestSegmentHit test for hit on line segment i.e.
Definition: trigo.cpp:142
Message panel definition file.
const EDA_RECT GetBoundingBox() const override
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
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