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-2019 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::Offset( 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::MoveTo( 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, pen_size );
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 
173 void LIB_RECTANGLE::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
174  void* aData, const TRANSFORM& aTransform )
175 {
176  wxPoint pt1, pt2;
177 
180 
181  pt1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
182  pt2 = aTransform.TransformCoordinate( m_End ) + aOffset;
183 
184  FILL_T fill = aData ? NO_FILL : m_Fill;
185 
186  EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
187  if( fill == FILLED_WITH_BG_BODYCOLOR && !aData )
188  GRFilledRect( clipbox, aDC, pt1.x, pt1.y, pt2.x, pt2.y, GetPenSize( ), bgColor, bgColor );
189  else if( m_Fill == FILLED_SHAPE && !aData )
190  GRFilledRect( clipbox, aDC, pt1.x, pt1.y, pt2.x, pt2.y, GetPenSize(), color, color );
191  else
192  GRRect( clipbox, aDC, pt1.x, pt1.y, pt2.x, pt2.y, GetPenSize(), color );
193 }
194 
195 
197 {
198  wxString msg;
199 
200  LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
201 
202  msg = MessageTextFromValue( aUnits, m_Width, true );
203 
204  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
205 }
206 
207 
209 {
210  EDA_RECT rect;
211 
212  rect.SetOrigin( m_Pos );
213  rect.SetEnd( m_End );
214  rect.Inflate( ( GetPenSize()+1 ) / 2 );
215 
216  rect.RevertYAxis();
217 
218  return rect;
219 }
220 
221 
222 bool LIB_RECTANGLE::HitTest( const wxPoint& aPosition, int aAccuracy ) const
223 {
224  int mindist = std::max( aAccuracy + GetPenSize() / 2, MINIMUM_SELECTION_DISTANCE );
225  wxPoint actualStart = DefaultTransform.TransformCoordinate( m_Pos );
226  wxPoint actualEnd = DefaultTransform.TransformCoordinate( m_End );
227 
228  // locate lower segment
229  wxPoint start, end;
230 
231  start = actualStart;
232  end.x = actualEnd.x;
233  end.y = actualStart.y;
234 
235  if( TestSegmentHit( aPosition, start, end, mindist ) )
236  return true;
237 
238  // locate right segment
239  start.x = actualEnd.x;
240  end.y = actualEnd.y;
241 
242  if( TestSegmentHit( aPosition, start, end, mindist ) )
243  return true;
244 
245  // locate upper segment
246  start.y = actualEnd.y;
247  end.x = actualStart.x;
248 
249  if( TestSegmentHit( aPosition, start, end, mindist ) )
250  return true;
251 
252  // locate left segment
253  start = actualStart;
254  end.x = actualStart.x;
255  end.y = actualEnd.y;
256 
257  if( TestSegmentHit( aPosition, start, end, mindist ) )
258  return true;
259 
260  return false;
261 }
262 
263 
265 {
266  return wxString::Format( _( "Rectangle from (%s, %s) to (%s, %s)" ),
267  MessageTextFromValue( aUnits, m_Pos.x ),
268  MessageTextFromValue( aUnits, m_Pos.y ),
269  MessageTextFromValue( aUnits, m_End.x ),
270  MessageTextFromValue( aUnits, m_End.y ) );
271 }
272 
273 
274 BITMAP_DEF LIB_RECTANGLE::GetMenuImage() const
275 {
276  return add_rectangle_xpm;
277 }
278 
279 
280 void LIB_RECTANGLE::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
281 {
282  LIB_ITEM::BeginEdit( aEditMode, aPosition );
283 
284  if( aEditMode == IS_NEW )
285  {
286  m_Pos = m_End = aPosition;
287  }
288  else if( aEditMode == IS_MOVED )
289  {
291  m_initialCursorPos = aPosition;
292  }
293 }
294 
295 
296 void LIB_RECTANGLE::EndEdit( const wxPoint& aPosition )
297 {
298  LIB_ITEM::EndEdit( aPosition );
299 
300  m_isHeightLocked = false;
301  m_isWidthLocked = false;
302 }
303 
304 
305 void LIB_RECTANGLE::CalcEdit( const wxPoint& aPosition )
306 {
307  if( IsNew() )
308  {
309  m_End = aPosition;
310  }
311  else if( IsMoving() )
312  {
313  MoveTo( m_initialPos + aPosition - m_initialCursorPos );
314  }
315 }
Definition: colors.h:57
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 MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Function HitTest tests if aPosition is contained within or on the bounding box of an item.
virtual void EndEdit(const wxPoint &aPosition)
End an object editing action.
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
bool IsMoving() const
Definition: base_struct.h:224
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 BeginEdit(STATUS_FLAGS aEditMode, const wxPoint aStartPoint) override
Begin an editing a component library draw item in aEditMode at aPosition.
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:124
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
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
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.
bool IsNew() const
Definition: base_struct.h:222
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
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
void EndEdit(const wxPoint &aPosition) override
End an object editing action.
void SetEnd(int x, int y)
Definition: eda_rect.h:134
bool m_isFillable
Flag to indicate if draw item is fillable.
Define a library symbol object.
unsigned STATUS_FLAGS
Definition: base_struct.h:150
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.
bool m_isWidthLocked
Definition: lib_rectangle.h:40
virtual void BeginEdit(STATUS_FLAGS aEditMode, const wxPoint aPosition)
Begin an editing a component library draw item in aEditMode at 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.
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
void Offset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:157
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
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
void drawGraphic(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Draw the item on aPanel.
Message panel definition file.
const EDA_RECT GetBoundingBox() const override
EDA_UNITS_T
Definition: common.h:157
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:204
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