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 
25 #include <fctsys.h>
26 #include <gr_basic.h>
27 #include <macros.h>
28 #include <sch_draw_panel.h>
29 #include <plotter.h>
30 #include <trigo.h>
31 #include <base_units.h>
32 #include <msgpanel.h>
33 #include <bitmaps.h>
34 
35 #include <general.h>
36 #include <lib_rectangle.h>
37 #include <transform.h>
38 
39 
41  LIB_ITEM( LIB_RECTANGLE_T, aParent )
42 {
43  m_Width = 0;
44  m_Fill = NO_FILL;
45  m_isFillable = true;
46 }
47 
48 
50 {
51  return new LIB_RECTANGLE( *this );
52 }
53 
54 
55 int LIB_RECTANGLE::compare( const LIB_ITEM& aOther ) const
56 {
57  wxASSERT( aOther.Type() == LIB_RECTANGLE_T );
58 
59  const LIB_RECTANGLE* tmp = ( LIB_RECTANGLE* ) &aOther;
60 
61  if( m_Pos.x != tmp->m_Pos.x )
62  return m_Pos.x - tmp->m_Pos.x;
63 
64  if( m_Pos.y != tmp->m_Pos.y )
65  return m_Pos.y - tmp->m_Pos.y;
66 
67  if( m_End.x != tmp->m_End.x )
68  return m_End.x - tmp->m_End.x;
69 
70  if( m_End.y != tmp->m_End.y )
71  return m_End.y - tmp->m_End.y;
72 
73  return 0;
74 }
75 
76 
77 void LIB_RECTANGLE::Offset( const wxPoint& aOffset )
78 {
79  m_Pos += aOffset;
80  m_End += aOffset;
81 }
82 
83 
84 bool LIB_RECTANGLE::Inside( EDA_RECT& aRect ) const
85 {
86  return aRect.Contains( m_Pos.x, -m_Pos.y ) || aRect.Contains( m_End.x, -m_End.y );
87 }
88 
89 
90 void LIB_RECTANGLE::MoveTo( const wxPoint& aPosition )
91 {
92  wxPoint size = m_End - m_Pos;
93  m_Pos = aPosition;
94  m_End = aPosition + size;
95 }
96 
97 
98 void LIB_RECTANGLE::MirrorHorizontal( const wxPoint& aCenter )
99 {
100  m_Pos.x -= aCenter.x;
101  m_Pos.x *= -1;
102  m_Pos.x += aCenter.x;
103  m_End.x -= aCenter.x;
104  m_End.x *= -1;
105  m_End.x += aCenter.x;
106 }
107 
108 
109 void LIB_RECTANGLE::MirrorVertical( const wxPoint& aCenter )
110 {
111  m_Pos.y -= aCenter.y;
112  m_Pos.y *= -1;
113  m_Pos.y += aCenter.y;
114  m_End.y -= aCenter.y;
115  m_End.y *= -1;
116  m_End.y += aCenter.y;
117 }
118 
119 
120 void LIB_RECTANGLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
121 {
122  int rot_angle = aRotateCCW ? -900 : 900;
123  RotatePoint( &m_Pos, aCenter, rot_angle );
124  RotatePoint( &m_End, aCenter, rot_angle );
125 }
126 
127 
128 void LIB_RECTANGLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
129  const TRANSFORM& aTransform )
130 {
131  wxASSERT( aPlotter != NULL );
132 
133  wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
134  wxPoint end = aTransform.TransformCoordinate( m_End ) + aOffset;
135 
136  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
137  {
139  aPlotter->Rect( pos, end, FILLED_WITH_BG_BODYCOLOR, 0 );
140  }
141 
142  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
143  auto pen_size = GetPenSize();
144 
145  if( !already_filled || pen_size > 0 )
146  {
147  pen_size = std::max( 0, pen_size );
148  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
149  aPlotter->Rect( pos, end, already_filled ? NO_FILL : m_Fill, pen_size );
150  }
151 }
152 
153 
155 {
156  if( m_Width > 0 )
157  return m_Width;
158 
159  if( m_Width == 0 )
160  return GetDefaultLineThickness();
161 
162  return -1; // a value to use a minimal pen size
163 }
164 
165 
166 void LIB_RECTANGLE::print( wxDC* aDC, const wxPoint& aOffset, void* aData,
167  const TRANSFORM& aTransform )
168 {
171  wxPoint pt1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
172  wxPoint pt2 = aTransform.TransformCoordinate( m_End ) + aOffset;
173 
174  FILL_T fill = aData ? NO_FILL : m_Fill;
175 
176  if( fill == FILLED_WITH_BG_BODYCOLOR && !aData )
177  GRFilledRect( nullptr, aDC, pt1.x, pt1.y, pt2.x, pt2.y, GetPenSize( ), bgColor, bgColor );
178  else if( m_Fill == FILLED_SHAPE && !aData )
179  GRFilledRect( nullptr, aDC, pt1.x, pt1.y, pt2.x, pt2.y, GetPenSize(), color, color );
180  else
181  GRRect( nullptr, aDC, pt1.x, pt1.y, pt2.x, pt2.y, GetPenSize(), color );
182 }
183 
184 
186 {
187  LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
188 
189  wxString msg = MessageTextFromValue( aUnits, m_Width, true );
190 
191  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
192 }
193 
194 
196 {
197  EDA_RECT rect;
198 
199  rect.SetOrigin( m_Pos );
200  rect.SetEnd( m_End );
201  rect.Inflate( ( GetPenSize()+1 ) / 2 );
202 
203  rect.RevertYAxis();
204 
205  return rect;
206 }
207 
208 
209 bool LIB_RECTANGLE::HitTest( const wxPoint& aPosition, int aAccuracy ) const
210 {
211  int mindist = std::max( aAccuracy + GetPenSize() / 2, MINIMUM_SELECTION_DISTANCE );
212  wxPoint actualStart = DefaultTransform.TransformCoordinate( m_Pos );
213  wxPoint actualEnd = DefaultTransform.TransformCoordinate( m_End );
214 
215  // locate lower segment
216  wxPoint start, end;
217 
218  start = actualStart;
219  end.x = actualEnd.x;
220  end.y = actualStart.y;
221 
222  if( TestSegmentHit( aPosition, start, end, mindist ) )
223  return true;
224 
225  // locate right segment
226  start.x = actualEnd.x;
227  end.y = actualEnd.y;
228 
229  if( TestSegmentHit( aPosition, start, end, mindist ) )
230  return true;
231 
232  // locate upper segment
233  start.y = actualEnd.y;
234  end.x = actualStart.x;
235 
236  if( TestSegmentHit( aPosition, start, end, mindist ) )
237  return true;
238 
239  // locate left segment
240  start = actualStart;
241  end.x = actualStart.x;
242  end.y = actualEnd.y;
243 
244  if( TestSegmentHit( aPosition, start, end, mindist ) )
245  return true;
246 
247  return false;
248 }
249 
250 
252 {
253  return wxString::Format( _( "Rectangle from (%s, %s) to (%s, %s)" ),
254  MessageTextFromValue( aUnits, m_Pos.x ),
255  MessageTextFromValue( aUnits, m_Pos.y ),
256  MessageTextFromValue( aUnits, m_End.x ),
257  MessageTextFromValue( aUnits, m_End.y ) );
258 }
259 
260 
261 BITMAP_DEF LIB_RECTANGLE::GetMenuImage() const
262 {
263  return add_rectangle_xpm;
264 }
265 
266 
267 void LIB_RECTANGLE::BeginEdit( const wxPoint aPosition )
268 {
269  m_Pos = m_End = aPosition;
270 }
271 
272 
273 void LIB_RECTANGLE::CalcEdit( const wxPoint& aPosition )
274 {
275  m_End = aPosition;
276 }
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.
Definition: lib_item.cpp:52
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.
void print(wxDC *aDC, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
Implementation of conversion functions that require both schematic and board internal units.
virtual void SetColor(COLOR4D color)=0
void GRFilledRect(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:888
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
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:229
bool Inside(EDA_RECT &aRect) const override
Test if any part of the draw object is inside rectangle bounds of aRect.
void BeginEdit(const wxPoint aStartPoint) override
Begin drawing a component library draw item at aPosition.
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:160
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size)
Definition: eda_rect.h:144
bool Contains(const wxPoint &aPoint) const
Function Contains.
void Rotate(const wxPoint &aCenter, bool aRotateCCW=true) override
Rotate the object about aCenter point.
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_item.h:61
This file contains miscellaneous commonly used macros and functions.
wxString MessageTextFromValue(EDA_UNITS_T aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:125
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:848
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 SetEnd(int x, int y)
Definition: eda_rect.h:134
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_item.h:47
bool m_isFillable
Definition: lib_item.h:95
Define a library symbol object.
#define _(s)
void MirrorVertical(const wxPoint &aCenter) override
Mirror the draw object along the MirrorVertical (Y) axis about aCenter point.
Base plotter engine class.
Definition: plotter.h:97
LIB_RECTANGLE(LIB_PART *aParent)
TRANSFORM DefaultTransform
Definition: eeschema.cpp:50
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
FILL_T m_Fill
The body fill type.
Definition: lib_item.h:94
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:163
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:42
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 TestSegmentHit(const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist)
Function TestSegmentHit test for hit on line segment i.e.
Definition: trigo.cpp:126
Message panel definition file.
const EDA_RECT GetBoundingBox() const override
EDA_UNITS_T
Definition: common.h:154
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:210
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39