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) 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 
29 #include <fctsys.h>
30 #include <gr_basic.h>
31 #include <macros.h>
32 #include <sch_draw_panel.h>
33 #include <plotter.h>
34 #include <trigo.h>
35 #include <base_units.h>
36 #include <msgpanel.h>
37 #include <bitmaps.h>
38 
39 #include <general.h>
40 #include <lib_rectangle.h>
41 #include <transform.h>
42 
43 
45  LIB_ITEM( LIB_RECTANGLE_T, aParent )
46 {
47  m_Width = 0;
48  m_Fill = NO_FILL;
49  m_isFillable = true;
50  m_isHeightLocked = false;
51  m_isWidthLocked = false;
52  m_isStartPointSelected = false;
53 }
54 
55 
57 {
58  return new LIB_RECTANGLE( *this );
59 }
60 
61 
62 int LIB_RECTANGLE::compare( const LIB_ITEM& aOther ) const
63 {
64  wxASSERT( aOther.Type() == LIB_RECTANGLE_T );
65 
66  const LIB_RECTANGLE* tmp = ( LIB_RECTANGLE* ) &aOther;
67 
68  if( m_Pos.x != tmp->m_Pos.x )
69  return m_Pos.x - tmp->m_Pos.x;
70 
71  if( m_Pos.y != tmp->m_Pos.y )
72  return m_Pos.y - tmp->m_Pos.y;
73 
74  if( m_End.x != tmp->m_End.x )
75  return m_End.x - tmp->m_End.x;
76 
77  if( m_End.y != tmp->m_End.y )
78  return m_End.y - tmp->m_End.y;
79 
80  return 0;
81 }
82 
83 
84 void LIB_RECTANGLE::SetOffset( const wxPoint& aOffset )
85 {
86  m_Pos += aOffset;
87  m_End += aOffset;
88 }
89 
90 
91 bool LIB_RECTANGLE::Inside( EDA_RECT& aRect ) const
92 {
93  return aRect.Contains( m_Pos.x, -m_Pos.y ) || aRect.Contains( m_End.x, -m_End.y );
94 }
95 
96 
97 void LIB_RECTANGLE::Move( const wxPoint& aPosition )
98 {
99  wxPoint size = m_End - m_Pos;
100  m_Pos = aPosition;
101  m_End = aPosition + size;
102 }
103 
104 
105 void LIB_RECTANGLE::MirrorHorizontal( const wxPoint& aCenter )
106 {
107  m_Pos.x -= aCenter.x;
108  m_Pos.x *= -1;
109  m_Pos.x += aCenter.x;
110  m_End.x -= aCenter.x;
111  m_End.x *= -1;
112  m_End.x += aCenter.x;
113 }
114 
115 
116 void LIB_RECTANGLE::MirrorVertical( const wxPoint& aCenter )
117 {
118  m_Pos.y -= aCenter.y;
119  m_Pos.y *= -1;
120  m_Pos.y += aCenter.y;
121  m_End.y -= aCenter.y;
122  m_End.y *= -1;
123  m_End.y += aCenter.y;
124 }
125 
126 
127 void LIB_RECTANGLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
128 {
129  int rot_angle = aRotateCCW ? -900 : 900;
130  RotatePoint( &m_Pos, aCenter, rot_angle );
131  RotatePoint( &m_End, aCenter, rot_angle );
132 }
133 
134 
135 void LIB_RECTANGLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
136  const TRANSFORM& aTransform )
137 {
138  wxASSERT( aPlotter != NULL );
139 
140  wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
141  wxPoint end = aTransform.TransformCoordinate( m_End ) + aOffset;
142 
143  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
144  {
146  aPlotter->Rect( pos, end, FILLED_WITH_BG_BODYCOLOR, 0 );
147  }
148 
149  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
150  auto pen_size = GetPenSize();
151 
152  if( !already_filled || pen_size > 0 )
153  {
154  pen_size = std::max( 0, pen_size );
155  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
156  aPlotter->Rect( pos, end, already_filled ? NO_FILL : m_Fill, GetPenSize() );
157  }
158 }
159 
160 
162 {
163  if( m_Width > 0 )
164  return m_Width;
165 
166  if( m_Width == 0 )
167  return GetDefaultLineThickness();
168 
169  return -1; // a value to use a minimal pen size
170 }
171 
172 
174  const wxPoint& aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode,
175  void* aData, const TRANSFORM& aTransform )
176 {
177  wxPoint pos1, pos2;
178 
180 
181  if( aColor == COLOR4D::UNSPECIFIED ) // Used normal color or selected color
182  {
183  if( IsSelected() )
185  }
186  else
187  {
188  color = aColor;
189  }
190 
191  pos1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
192  pos2 = aTransform.TransformCoordinate( m_End ) + aOffset;
193 
194  FILL_T fill = aData ? NO_FILL : m_Fill;
195 
196  if( aColor != COLOR4D::UNSPECIFIED )
197  fill = NO_FILL;
198 
199  GRSetDrawMode( aDC, aDrawMode );
200 
201  EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
202  if( fill == FILLED_WITH_BG_BODYCOLOR && !aData )
203  GRFilledRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y, GetPenSize( ),
206  else if( m_Fill == FILLED_SHAPE && !aData )
207  GRFilledRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y,
208  GetPenSize(), color, color );
209  else
210  GRRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y, GetPenSize(), color );
211 
212  /* Set to one (1) to draw bounding box around rectangle to validate
213  * bounding 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  wxString msg;
227 
228  LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
229 
230  msg = MessageTextFromValue( aUnits, m_Width, true );
231 
232  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
233 }
234 
235 
237 {
238  EDA_RECT rect;
239 
240  rect.SetOrigin( m_Pos );
241  rect.SetEnd( m_End );
242  rect.Inflate( ( GetPenSize()+1 ) / 2 );
243 
244  rect.RevertYAxis();
245 
246  return rect;
247 }
248 
249 
250 bool LIB_RECTANGLE::HitTest( const wxPoint& aPosition ) const
251 {
252  int mindist = ( GetPenSize() / 2 ) + 1;
253 
254  // Have a minimal tolerance for hit test
255  if( mindist < MINIMUM_SELECTION_DISTANCE )
256  mindist = MINIMUM_SELECTION_DISTANCE;
257 
258  return HitTest( aPosition, mindist, DefaultTransform );
259 }
260 
261 
262 bool LIB_RECTANGLE::HitTest( const wxPoint &aPosition, int aThreshold, const TRANSFORM& aTransform ) const
263 {
264  if( aThreshold < 0 )
265  aThreshold = GetPenSize() / 2;
266 
267  wxPoint actualStart = aTransform.TransformCoordinate( m_Pos );
268  wxPoint actualEnd = aTransform.TransformCoordinate( m_End );
269 
270  // locate lower segment
271  wxPoint start, end;
272 
273  start = actualStart;
274  end.x = actualEnd.x;
275  end.y = actualStart.y;
276 
277  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
278  return true;
279 
280  // locate right segment
281  start.x = actualEnd.x;
282  end.y = actualEnd.y;
283 
284  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
285  return true;
286 
287  // locate upper segment
288  start.y = actualEnd.y;
289  end.x = actualStart.x;
290 
291  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
292  return true;
293 
294  // locate left segment
295  start = actualStart;
296  end.x = actualStart.x;
297  end.y = actualEnd.y;
298 
299  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
300  return true;
301 
302  return false;
303 }
304 
305 
307 {
308  return wxString::Format( _( "Rectangle from (%s, %s) to (%s, %s)" ),
309  MessageTextFromValue( aUnits, m_Pos.x ),
310  MessageTextFromValue( aUnits, m_Pos.y ),
311  MessageTextFromValue( aUnits, m_End.x ),
312  MessageTextFromValue( aUnits, m_End.y ) );
313 }
314 
315 
316 BITMAP_DEF LIB_RECTANGLE::GetMenuImage() const
317 {
318  return add_rectangle_xpm;
319 }
320 
321 
322 void LIB_RECTANGLE::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
323 {
324  wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
325  wxT( "Invalid edit mode for LIB_RECTANGLE object." ) );
326 
327  if( aEditMode == IS_NEW )
328  {
329  m_Pos = m_End = aPosition;
330  }
331  else if( aEditMode == IS_RESIZED )
332  {
334  || abs( m_Pos.y - aPosition.y ) < MINIMUM_SELECTION_DISTANCE;
335 
337  {
338  m_isWidthLocked = abs( m_Pos.x - aPosition.x ) >= MINIMUM_SELECTION_DISTANCE;
339  m_isHeightLocked = abs( m_Pos.y - aPosition.y ) >= MINIMUM_SELECTION_DISTANCE;
340  }
341  else
342  {
343  m_isWidthLocked = abs( m_End.x - aPosition.x ) >= MINIMUM_SELECTION_DISTANCE;
344  m_isHeightLocked = abs( m_End.y - aPosition.y ) >= MINIMUM_SELECTION_DISTANCE;
345  }
346 
347  }
348  else if( aEditMode == IS_MOVED )
349  {
351  m_initialCursorPos = aPosition;
352  }
353 
354  m_Flags = aEditMode;
355 }
356 
357 
358 bool LIB_RECTANGLE::ContinueEdit( const wxPoint aPosition )
359 {
360  wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0, false,
361  wxT( "Bad call to ContinueEdit(). LIB_RECTANGLE is not being edited." ) );
362 
363  return false;
364 }
365 
366 
367 void LIB_RECTANGLE::EndEdit( const wxPoint& aPosition, bool aAbort )
368 {
369  wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
370  wxT( "Bad call to EndEdit(). LIB_RECTANGLE is not being edited." ) );
371 
372  m_Flags = 0;
373  m_isHeightLocked = false;
374  m_isWidthLocked = false;
375 }
376 
377 
378 void LIB_RECTANGLE::CalcEdit( const wxPoint& aPosition )
379 {
380  if( m_Flags == IS_NEW )
381  {
382  m_End = aPosition;
383  }
384  else if( m_Flags == IS_RESIZED )
385  {
386  if( m_isHeightLocked )
387  {
389  m_Pos.x = aPosition.x;
390  else
391  m_End.x = aPosition.x;
392  }
393  else if( m_isWidthLocked )
394  {
396  m_Pos.y = aPosition.y;
397  else
398  m_End.y = aPosition.y;
399  }
400  else
401  {
403  m_Pos = aPosition;
404  else
405  m_End = aPosition;
406  }
407  }
408  else if( m_Flags == IS_MOVED )
409  {
410  Move( m_initialPos + aPosition - m_initialCursorPos );
411  }
412 }
Definition: colors.h:57
void SetOffset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
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.
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
void Move(const wxPoint &aMoveVector)
Function Move moves the rectangle by the aMoveVector.
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror 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.
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:223
bool IsSelected() const
Definition: base_struct.h:224
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:53
void GRFilledRect(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:1043
int GetPenSize() const override
int color
Definition: DXF_plotter.cpp:62
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
Provide the draw object specific comparison called by the == and < operators.
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
bool Inside(EDA_RECT &aRect) const override
Test if any part of the draw object is inside rectangle bounds of aRect.
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:174
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size)
Definition: eda_rect.h:144
bool m_isHeightLocked
Definition: lib_rectangle.h:41
#define abs(a)
Definition: auxiliary.h:84
bool Contains(const wxPoint &aPoint) const
Function Contains.
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:41
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
wxString GetSelectMenuText(EDA_UNITS_T aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
void GRRect(EDA_RECT *aClipBox, wxDC *aDC, int x1, int y1, int x2, int y2, COLOR4D aColor)
Definition: gr_basic.cpp:1003
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:37
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
void SetEnd(int x, int y)
Definition: eda_rect.h:134
bool ContinueEdit(const wxPoint aNextPoint) override
Continue an edit in progress at aPosition.
bool m_isFillable
Flag to indicate if draw item is fillable.
Define a library symbol object.
unsigned STATUS_FLAGS
Definition: base_struct.h:147
virtual void Rotate()
Rotate the draw item.
void MirrorVertical(const wxPoint &aCenter) override
Mirror 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
Draw the item on aPanel.
bool m_isWidthLocked
Definition: lib_rectangle.h:40
void Move(const wxPoint &aPosition) override
Move a draw object to aPosition.
Base plotter engine class.
Definition: plotter.h:97
LIB_RECTANGLE(LIB_PART *aParent)
TRANSFORM DefaultTransform
Definition: eeschema.cpp:59
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.
#define max(a, b)
Definition: auxiliary.h:86
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:178
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
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:154
void EndEdit(const wxPoint &aPosition, bool aAbort=false) override
End an object editing action.
#define IS_RESIZED
Item being resized.
Definition: base_struct.h:115
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
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.
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()
void CalcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
Class EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
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:122
Message panel definition file.
const EDA_RECT GetBoundingBox() const override
EDA_UNITS_T
Definition: common.h:160
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.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201
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