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-2020 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>
38 #include <transform.h>
39 
40 
42  LIB_ITEM( LIB_RECTANGLE_T, aParent )
43 {
44  m_Width = 0;
45  m_Fill = NO_FILL;
46  m_isFillable = true;
47 }
48 
49 
51 {
52  return new LIB_RECTANGLE( *this );
53 }
54 
55 
56 int LIB_RECTANGLE::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
57 {
58  wxASSERT( aOther.Type() == LIB_RECTANGLE_T );
59 
60  int retv = LIB_ITEM::compare( aOther );
61 
62  if( retv )
63  return retv;
64 
65  const LIB_RECTANGLE* tmp = ( LIB_RECTANGLE* ) &aOther;
66 
67  if( m_Pos.x != tmp->m_Pos.x )
68  return m_Pos.x - tmp->m_Pos.x;
69 
70  if( m_Pos.y != tmp->m_Pos.y )
71  return m_Pos.y - tmp->m_Pos.y;
72 
73  if( m_End.x != tmp->m_End.x )
74  return m_End.x - tmp->m_End.x;
75 
76  if( m_End.y != tmp->m_End.y )
77  return m_End.y - tmp->m_End.y;
78 
79  return 0;
80 }
81 
82 
83 void LIB_RECTANGLE::Offset( const wxPoint& aOffset )
84 {
85  m_Pos += aOffset;
86  m_End += aOffset;
87 }
88 
89 
90 bool LIB_RECTANGLE::Inside( EDA_RECT& aRect ) const
91 {
92  return aRect.Contains( m_Pos.x, -m_Pos.y ) || aRect.Contains( m_End.x, -m_End.y );
93 }
94 
95 
96 void LIB_RECTANGLE::MoveTo( const wxPoint& aPosition )
97 {
98  wxPoint size = m_End - m_Pos;
99  m_Pos = aPosition;
100  m_End = aPosition + size;
101 }
102 
103 
105 {
106  m_Pos.x -= aCenter.x;
107  m_Pos.x *= -1;
108  m_Pos.x += aCenter.x;
109  m_End.x -= aCenter.x;
110  m_End.x *= -1;
111  m_End.x += aCenter.x;
112 }
113 
114 
116 {
117  m_Pos.y -= aCenter.y;
118  m_Pos.y *= -1;
119  m_Pos.y += aCenter.y;
120  m_End.y -= aCenter.y;
121  m_End.y *= -1;
122  m_End.y += aCenter.y;
123 }
124 
125 
126 void LIB_RECTANGLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
127 {
128  int rot_angle = aRotateCCW ? -900 : 900;
129  RotatePoint( &m_Pos, aCenter, rot_angle );
130  RotatePoint( &m_End, aCenter, rot_angle );
131 }
132 
133 
134 void LIB_RECTANGLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
135  const TRANSFORM& aTransform )
136 {
137  wxASSERT( aPlotter != NULL );
138 
139  wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
140  wxPoint end = aTransform.TransformCoordinate( m_End ) + aOffset;
141 
142  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
143  {
144  aPlotter->SetColor( aPlotter->ColorSettings()->GetColor( LAYER_DEVICE_BACKGROUND ) );
145  aPlotter->Rect( pos, end, FILLED_WITH_BG_BODYCOLOR, 0 );
146  }
147 
148  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
149  auto pen_size = GetPenSize();
150 
151  if( !already_filled || pen_size > 0 )
152  {
153  pen_size = std::max( 0, pen_size );
154  aPlotter->SetColor( aPlotter->ColorSettings()->GetColor( LAYER_DEVICE ) );
155  aPlotter->Rect( pos, end, already_filled ? NO_FILL : m_Fill, pen_size );
156  }
157 }
158 
159 
161 {
162  if( m_Width > 0 )
163  return m_Width;
164 
165  if( m_Width == 0 )
166  return GetDefaultLineThickness();
167 
168  return -1; // a value to use a minimal pen size
169 }
170 
171 
172 void LIB_RECTANGLE::print( wxDC* aDC, const wxPoint& aOffset, void* aData,
173  const TRANSFORM& aTransform )
174 {
177  wxPoint pt1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
178  wxPoint pt2 = aTransform.TransformCoordinate( m_End ) + aOffset;
179 
180  FILL_T fill = aData ? NO_FILL : m_Fill;
181 
182  if( fill == FILLED_WITH_BG_BODYCOLOR && !aData )
183  GRFilledRect( nullptr, aDC, pt1.x, pt1.y, pt2.x, pt2.y, GetPenSize( ), bgColor, bgColor );
184  else if( m_Fill == FILLED_SHAPE && !aData )
185  GRFilledRect( nullptr, aDC, pt1.x, pt1.y, pt2.x, pt2.y, GetPenSize(), color, color );
186  else
187  GRRect( nullptr, aDC, pt1.x, pt1.y, pt2.x, pt2.y, GetPenSize(), color );
188 }
189 
190 
192 {
193  LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
194 
195  wxString msg = MessageTextFromValue( aUnits, m_Width, true );
196 
197  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
198 }
199 
200 
202 {
203  EDA_RECT rect;
204 
205  rect.SetOrigin( m_Pos );
206  rect.SetEnd( m_End );
207  rect.Inflate( ( GetPenSize()+1 ) / 2 );
208 
209  rect.RevertYAxis();
210 
211  return rect;
212 }
213 
214 
215 bool LIB_RECTANGLE::HitTest( const wxPoint& aPosition, int aAccuracy ) const
216 {
217  int mindist = std::max( aAccuracy + GetPenSize() / 2,
218  Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
221 
222  // locate lower segment
223  wxPoint start, end;
224 
225  start = actualStart;
226  end.x = actualEnd.x;
227  end.y = actualStart.y;
228 
229  if( TestSegmentHit( aPosition, start, end, mindist ) )
230  return true;
231 
232  // locate right segment
233  start.x = actualEnd.x;
234  end.y = actualEnd.y;
235 
236  if( TestSegmentHit( aPosition, start, end, mindist ) )
237  return true;
238 
239  // locate upper segment
240  start.y = actualEnd.y;
241  end.x = actualStart.x;
242 
243  if( TestSegmentHit( aPosition, start, end, mindist ) )
244  return true;
245 
246  // locate left segment
247  start = actualStart;
248  end.x = actualStart.x;
249  end.y = actualEnd.y;
250 
251  if( TestSegmentHit( aPosition, start, end, mindist ) )
252  return true;
253 
254  return false;
255 }
256 
257 
259 {
260  return wxString::Format( _( "Rectangle from (%s, %s) to (%s, %s)" ),
261  MessageTextFromValue( aUnits, m_Pos.x ),
262  MessageTextFromValue( aUnits, m_Pos.y ),
263  MessageTextFromValue( aUnits, m_End.x ),
264  MessageTextFromValue( aUnits, m_End.y ) );
265 }
266 
267 
269 {
270  return add_rectangle_xpm;
271 }
272 
273 
274 void LIB_RECTANGLE::BeginEdit( const wxPoint aPosition )
275 {
276  m_Pos = m_End = aPosition;
277 }
278 
279 
280 void LIB_RECTANGLE::CalcEdit( const wxPoint& aPosition )
281 {
282  m_End = aPosition;
283 }
Definition: colors.h:57
EDA_UNITS
Definition: common.h:184
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.
PNG memory record (file in memory).
Definition: bitmap_def.h:29
void GetMsgPanelInfo(EDA_UNITS 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
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:884
int GetPenSize() const override
int color
Definition: DXF_plotter.cpp:61
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i....
TRANSFORM DefaultTransform
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
bool Inside(EDA_RECT &aRect) const override
Test if any part of the draw object is inside rectangle bounds of aRect.
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
void BeginEdit(const wxPoint aStartPoint) override
Begin drawing a component library draw item at aPosition.
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size)
Definition: eda_rect.h:209
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:42
The base class for drawable items used by schematic library components.
Definition: lib_item.h:60
This file contains miscellaneous commonly used macros and functions.
bool TestSegmentHit(const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist)
Test if aRefPoint is with aDistance on the line defined by aStart and aEnd.
Definition: trigo.cpp:129
const BITMAP_OPAQUE add_rectangle_xpm[1]
void GRRect(EDA_RECT *aClipBox, wxDC *aDC, int x1, int y1, int x2, int y2, COLOR4D aColor)
Definition: gr_basic.cpp:844
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
#define NULL
COMPARE_FLAGS
The list of flags used by the compare function.
Definition: lib_item.h:116
COLOR_SETTINGS * ColorSettings()
Definition: plotter.h:145
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:192
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_item.h:46
bool m_isFillable
Definition: lib_item.h:94
Define a library symbol object.
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Helper for all the old plotting/printing code while it still exists.
void MirrorVertical(const wxPoint &aCenter) override
Mirror the draw object along the MirrorVertical (Y) axis about aCenter point.
void GetMsgPanelInfo(EDA_UNITS aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils, EDA_DATA_TYPE aType)
Definition: base_units.cpp:127
Base plotter engine class.
Definition: plotter.h:104
COLOR4D GetColor(int aLayer) const
LIB_RECTANGLE(LIB_PART *aParent)
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 _(s)
Definition: 3d_actions.cpp:33
FILL_T m_Fill
The body fill type.
Definition: lib_item.h:93
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.
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:166
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
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.
EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
Message panel definition file.
virtual int compare(const LIB_ITEM &aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags=LIB_ITEM::COMPARE_FLAGS::NORMAL) const
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_item.cpp:76
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.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212
int compare(const LIB_ITEM &aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags=LIB_ITEM::COMPARE_FLAGS::NORMAL) const override
Provide the draw object specific comparison called by the == and < operators.
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40