KiCad PCB EDA Suite
pcb_shape.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) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6  * Copyright (C) 2011 Wayne Stambaugh <[email protected]>
7  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include <bitmaps.h>
28 #include <pcb_edit_frame.h>
29 #include <board_design_settings.h>
30 #include <footprint.h>
31 #include <base_units.h>
33 #include <pcb_shape.h>
34 
35 
36 PCB_SHAPE::PCB_SHAPE( BOARD_ITEM* aParent, KICAD_T idtype, SHAPE_T shapetype ) :
37  BOARD_ITEM( aParent, idtype ),
38  EDA_SHAPE( shapetype, Millimeter2iu( DEFAULT_LINE_WIDTH ), FILL_T::NO_FILL, false )
39 {
40 }
41 
42 
43 PCB_SHAPE::PCB_SHAPE( BOARD_ITEM* aParent, SHAPE_T shapetype ) :
44  BOARD_ITEM( aParent, PCB_SHAPE_T ),
45  EDA_SHAPE( shapetype, Millimeter2iu( DEFAULT_LINE_WIDTH ), FILL_T::NO_FILL, false )
46 {
47 }
48 
49 
51 {
52 }
53 
54 
55 const wxPoint PCB_SHAPE::GetFocusPosition() const
56 {
57  // For some shapes return the visual center, but for not filled polygonal shapes,
58  // the center is usually far from the shape: a point on the outline is better
59 
60  switch( m_shape )
61  {
62  case SHAPE_T::CIRCLE:
63  if( !IsFilled() )
64  return wxPoint( GetCenter().x + GetRadius(), GetCenter().y );
65  else
66  return GetCenter();
67 
68  case SHAPE_T::RECT:
69  if( !IsFilled() )
70  return GetStart();
71  else
72  return GetCenter();
73 
74  case SHAPE_T::POLY:
75  if( !IsFilled() )
76  {
77  VECTOR2I pos = GetPolyShape().Outline(0).CPoint(0);
78  return wxPoint( pos.x, pos.y );
79  }
80  else
81  {
82  return GetCenter();
83  }
84 
85  case SHAPE_T::ARC:
86  return GetArcMid();
87 
88  case SHAPE_T::BEZIER:
89  return GetStart();
90 
91  default:
92  return GetCenter();
93  }
94 }
95 
96 
97 void PCB_SHAPE::Move( const wxPoint& aMoveVector )
98 {
99  move( aMoveVector );
100 }
101 
102 
103 void PCB_SHAPE::Scale( double aScale )
104 {
105  scale( aScale );
106 }
107 
108 
109 void PCB_SHAPE::Rotate( const wxPoint& aRotCentre, double aAngle )
110 {
111  rotate( aRotCentre, aAngle );
112 }
113 
114 
115 void PCB_SHAPE::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
116 {
117  flip( aCentre, aFlipLeftRight );
118 
119  SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
120 }
121 
122 
124 {
125  if( !m_parent || m_parent->Type() != PCB_FOOTPRINT_T )
126  return nullptr;
127 
128  return (FOOTPRINT*) m_parent;
129 }
130 
131 
133 {
134  if( GetParentFootprint() )
136  else
137  return 0.0;
138 }
139 
140 
142 {
143  if( GetParentFootprint() )
144  return GetParentFootprint()->GetPosition();
145  else
146  return wxPoint( 0, 0 );
147 }
148 
149 
150 void PCB_SHAPE::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
151 {
152  aList.emplace_back( _( "Type" ), _( "Drawing" ) );
153 
154  if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
155  aList.emplace_back( _( "Status" ), _( "Locked" ) );
156 
157  ShapeGetMsgPanelInfo( aFrame, aList );
158 
159  aList.emplace_back( _( "Layer" ), GetLayerName() );
160 }
161 
162 
163 wxString PCB_SHAPE::GetSelectMenuText( EDA_UNITS aUnits ) const
164 {
165  return wxString::Format( _( "%s on %s" ), ShowShape(), GetLayerName() );
166 }
167 
168 
170 {
172 }
173 
174 
176 {
177  return new PCB_SHAPE( *this );
178 }
179 
180 
182 {
183  BOX2I return_box = EDA_ITEM::ViewBBox();
184 
185  // Inflate the bounding box by just a bit more for safety.
186  return_box.Inflate( GetWidth() );
187 
188  return return_box;
189 }
190 
191 
192 std::shared_ptr<SHAPE> PCB_SHAPE::GetEffectiveShape( PCB_LAYER_ID aLayer ) const
193 {
194  return std::make_shared<SHAPE_COMPOUND>( MakeEffectiveShapes() );
195 }
196 
197 
199 {
200  PCB_SHAPE* image = dynamic_cast<PCB_SHAPE*>( aImage );
201  assert( image );
202 
203  SwapShape( image );
204 
205  std::swap( m_layer, image->m_layer );
206  std::swap( m_fill, image->m_fill );
207  std::swap( m_flags, image->m_flags );
208  std::swap( m_status, image->m_status );
209  std::swap( m_parent, image->m_parent );
210  std::swap( m_forceVisible, image->m_forceVisible );
211 }
212 
213 
215  const BOARD_ITEM* aSecond ) const
216 {
217  if( aFirst->Type() != aSecond->Type() )
218  return aFirst->Type() < aSecond->Type();
219 
220  if( aFirst->GetLayer() != aSecond->GetLayer() )
221  return aFirst->GetLayer() < aSecond->GetLayer();
222 
223  if( aFirst->Type() == PCB_SHAPE_T )
224  {
225  const PCB_SHAPE* dwgA = static_cast<const PCB_SHAPE*>( aFirst );
226  const PCB_SHAPE* dwgB = static_cast<const PCB_SHAPE*>( aSecond );
227 
228  if( dwgA->GetShape() != dwgB->GetShape() )
229  return dwgA->GetShape() < dwgB->GetShape();
230  }
231 
232  return aFirst->m_Uuid < aSecond->m_Uuid;
233 }
234 
235 
237  PCB_LAYER_ID aLayer, int aClearanceValue,
238  int aError, ERROR_LOC aErrorLoc,
239  bool ignoreLineWidth ) const
240 {
241  EDA_SHAPE::TransformShapeWithClearanceToPolygon( aCornerBuffer, aClearanceValue, aError,
242  aErrorLoc, ignoreLineWidth );
243 }
244 
245 
246 static struct PCB_SHAPE_DESC
247 {
249  {
256 
257  propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Thickness" ),
259  // TODO show certain properties depending on the shape
260  //propMgr.AddProperty( new PROPERTY<PCB_SHAPE, double>( _HKI( "Angle" ),
261  // &PCB_SHAPE::SetArcAngle, &PCB_SHAPE::GetAngle, PROPERTY_DISPLAY::DECIDEGREE ) );
262  // TODO or may have different names (arcs)
263  // TODO type?
264  propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "End X" ),
266  propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "End Y" ),
268  }
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:478
SHAPE_T m_shape
Definition: eda_shape.h:301
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:65
int GetWidth() const
Definition: eda_shape.h:89
#define TYPE_HASH(x)
Definition: property.h:59
virtual void SwapData(BOARD_ITEM *aImage) override
Swap data between aItem and aImage.
Definition: pcb_shape.cpp:198
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:163
Implementation of conversion functions that require both schematic and board internal units.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
void Scale(double aScale)
Definition: pcb_shape.cpp:103
const wxPoint & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:97
virtual EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_shape.cpp:175
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition: lset.cpp:530
void TransformShapeWithClearanceToPolygon(SHAPE_POLY_SET &aCornerBuffer, int aClearanceValue, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth) const
Convert the shape to a closed polygon.
Definition: eda_shape.cpp:1477
virtual BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: pcb_shape.cpp:169
int GetEndY()
Definition: eda_shape.h:123
bool operator()(const BOARD_ITEM *aFirst, const BOARD_ITEM *aSecond) const
Definition: pcb_shape.cpp:214
std::vector< SHAPE * > MakeEffectiveShapes() const
Make a set of SHAPE objects representing the EDA_SHAPE.
Definition: eda_shape.cpp:1073
virtual const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
Definition: pcb_shape.cpp:181
double GetOrientation() const
Definition: footprint.h:191
#define DEFAULT_LINE_WIDTH
EDA_ITEM_FLAGS m_status
Definition: eda_item.h:477
void SetEndY(int y)
Definition: eda_shape.h:132
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:480
std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER) const override
Make a set of SHAPE objects representing the PCB_SHAPE.
Definition: pcb_shape.cpp:192
void ShapeGetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList)
Definition: eda_shape.cpp:538
The base class for create windows for drawing purpose.
wxString ShowShape() const
Definition: eda_shape.cpp:56
virtual void Rotate(const wxPoint &aRotCentre, double aAngle) override
Rotate this object.
Definition: pcb_shape.cpp:109
PCB_SHAPE(BOARD_ITEM *aParent, KICAD_T idtype, SHAPE_T shapetype)
Definition: pcb_shape.cpp:36
virtual bool IsLocked() const
Definition: board_item.cpp:64
#define REGISTER_TYPE(x)
Definition: property_mgr.h:248
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
FILL_T
Definition: eda_shape.h:53
wxPoint getParentPosition() const override
Definition: pcb_shape.cpp:141
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
int GetEndX()
Definition: eda_shape.h:124
bool IsFilled() const
Definition: eda_shape.h:81
void rotate(const wxPoint &aRotCentre, double aAngle)
Definition: eda_shape.cpp:224
virtual void Move(const wxPoint &aMoveVector) override
Move this object.
Definition: pcb_shape.cpp:97
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
Display value expressed in distance units (mm/inch)
Definition: property.h:53
void move(const wxPoint &aMoveVector)
Definition: eda_shape.cpp:137
Represent a set of closed polygons.
SHAPE_T
Definition: eda_shape.h:40
SHAPE_LINE_CHAIN & Outline(int aIndex)
wxPoint GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.h:79
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
FILL_T m_fill
Definition: eda_shape.h:303
#define _(s)
void SetWidth(int aWidth)
Definition: eda_shape.h:88
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:207
virtual wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: pcb_shape.cpp:163
class FOOTPRINT, a footprint
Definition: typeinfo.h:88
const KIID m_Uuid
Definition: eda_item.h:474
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
EDA_UNITS
Definition: eda_units.h:38
void SetEndX(int x)
Definition: eda_shape.h:138
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:281
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
FOOTPRINT * GetParentFootprint() const
Return the parent footprint or NULL if PCB_SHAPE does not belong to a footprint.
Definition: pcb_shape.cpp:123
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:65
void SwapShape(EDA_SHAPE *aImage)
Definition: eda_shape.cpp:1423
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
void AddProperty(PROPERTY_BASE *aProperty)
Register a property.
wxPoint GetArcMid() const
Definition: eda_shape.cpp:437
const wxPoint GetFocusPosition() const override
Allows items to return their visual center rather than their anchor.
Definition: pcb_shape.cpp:55
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:99
wxPoint GetPosition() const override
Definition: footprint.h:187
PCB_LAYER_ID m_layer
Definition: board_item.h:318
static struct PCB_SHAPE_DESC _PCB_SHAPE_DESC
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:36
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:62
virtual void Flip(const wxPoint &aCentre, bool aFlipLeftRight) override
Flip this object, i.e.
Definition: pcb_shape.cpp:115
#define PCB_EDIT_FRAME_NAME
SHAPE_T GetShape() const
Definition: eda_shape.h:92
void scale(double aScale)
Definition: eda_shape.cpp:172
int GetRadius() const
Definition: eda_shape.cpp:466
virtual const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
Definition: eda_item.cpp:257
void TransformShapeWithClearanceToPolygon(SHAPE_POLY_SET &aCornerBuffer, PCB_LAYER_ID aLayer, int aClearanceValue, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const override
Convert the shape to a closed polygon.
Definition: pcb_shape.cpp:236
bool m_forceVisible
Definition: eda_item.h:479
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
Definition: board_item.cpp:75
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:90
static constexpr int Millimeter2iu(double mm)
void flip(const wxPoint &aCentre, bool aFlipLeftRight)
Definition: eda_shape.cpp:281
#define _HKI(x)
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Populate aList of MSG_PANEL_ITEM objects with it's internal state for display purposes.
Definition: pcb_shape.cpp:150
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:142
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:112
double getParentOrientation() const override
Definition: pcb_shape.cpp:132