KiCad PCB EDA Suite
pad_custom_shape_functions.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) 1992-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 
30 #include <class_board.h>
31 #include <class_board_item.h>
32 #include <pcb_shape.h>
33 #include <class_pad.h>
35 #include <geometry/shape_rect.h>
36 
37 
38 /*
39  * Has meaning only for free shape pads.
40  * add a free shape to the shape list.
41  * the shape is a polygon (can be with thick outline), segment, circle or arc
42  */
43 
44 void D_PAD::AddPrimitivePoly( const SHAPE_POLY_SET& aPoly, int aThickness )
45 {
46  std::vector<wxPoint> points;
47 
48  // If aPoly has holes, convert it to a polygon with no holes.
49  SHAPE_POLY_SET poly_no_hole;
50  poly_no_hole.Append( aPoly );
52 
53  for( auto iter = poly_no_hole.CIterate(); iter; iter++ )
54  points.emplace_back( iter->x, iter->y );
55 
56  AddPrimitivePoly( points, aThickness );
57 }
58 
59 
60 void D_PAD::AddPrimitivePoly( const std::vector<wxPoint>& aPoly, int aThickness )
61 {
62  PCB_SHAPE* item = new PCB_SHAPE();
63  item->SetShape( S_POLYGON );
64  item->SetPolyPoints( aPoly );
65  item->SetWidth( aThickness );
66  m_editPrimitives.emplace_back( item );
67  m_shapesDirty = true;
68 }
69 
70 
71 void D_PAD::AddPrimitiveSegment( const wxPoint& aStart, const wxPoint& aEnd, int aThickness )
72 {
73  PCB_SHAPE* item = new PCB_SHAPE();
74  item->SetStart( aStart );
75  item->SetEnd( aEnd );
76  item->SetWidth( aThickness );
77  m_editPrimitives.emplace_back( item );
78  m_shapesDirty = true;
79 }
80 
81 
82 void D_PAD::AddPrimitiveArc( const wxPoint& aCenter, const wxPoint& aStart, int aArcAngle,
83  int aThickness )
84 {
85  PCB_SHAPE* item = new PCB_SHAPE();
86  item->SetShape( S_ARC );
87  item->SetCenter( aCenter );
88  item->SetArcStart( aStart );
89  item->SetAngle( aArcAngle );
90  item->SetWidth( aThickness );
91  m_editPrimitives.emplace_back( item );
92  m_shapesDirty = true;
93 }
94 
95 
96 void D_PAD::AddPrimitiveCurve( const wxPoint& aStart, const wxPoint& aEnd, const wxPoint& aCtrl1,
97  const wxPoint& aCtrl2, int aThickness )
98 {
99  PCB_SHAPE* item = new PCB_SHAPE();
100  item->SetShape( S_CURVE );
101  item->SetStart( aStart );
102  item->SetEnd( aEnd );
103  item->SetBezControl1( aCtrl1 );
104  item->SetBezControl2( aCtrl2 );
105  item->SetWidth( aThickness );
106  m_editPrimitives.emplace_back( item );
107  m_shapesDirty = true;
108 }
109 
110 
111 void D_PAD::AddPrimitiveCircle( const wxPoint& aCenter, int aRadius, int aThickness )
112 {
113  PCB_SHAPE* item = new PCB_SHAPE();
114  item->SetShape( S_CIRCLE );
115  item->SetStart( aCenter );
116  item->SetEnd( wxPoint( aCenter.x + aRadius, aCenter.y ) );
117  item->SetWidth( aThickness );
118  m_editPrimitives.emplace_back( item );
119  m_shapesDirty = true;
120 }
121 
122 
123 void D_PAD::AddPrimitiveRect( const wxPoint& aStart, const wxPoint& aEnd, int aThickness )
124 {
125  PCB_SHAPE* item = new PCB_SHAPE();
126  item->SetShape( S_RECT );
127  item->SetStart( aStart );
128  item->SetEnd( aEnd );
129  item->SetWidth( aThickness );
130  m_editPrimitives.emplace_back( item );
131  m_shapesDirty = true;
132 }
133 
134 
135 void D_PAD::ReplacePrimitives( const std::vector<std::shared_ptr<PCB_SHAPE>>& aPrimitivesList )
136 {
137  // clear old list
139 
140  // Import to the given shape list
141  if( aPrimitivesList.size() )
142  AppendPrimitives( aPrimitivesList );
143 
144  m_shapesDirty = true;
145 }
146 
147 
148 void D_PAD::AppendPrimitives( const std::vector<std::shared_ptr<PCB_SHAPE>>& aPrimitivesList )
149 {
150  // Add duplicates of aPrimitivesList to the pad primitives list:
151  for( const std::shared_ptr<PCB_SHAPE>& prim : aPrimitivesList )
152  AddPrimitive( new PCB_SHAPE( *prim ) );
153 
154  m_shapesDirty = true;
155 }
156 
157 
158 void D_PAD::AddPrimitive( PCB_SHAPE* aPrimitive )
159 {
160  m_editPrimitives.emplace_back( aPrimitive );
161 
162  m_shapesDirty = true;
163 }
164 
165 
166 // clear the basic shapes list and associated data
168 {
169  m_editPrimitives.clear();
170 
171  m_shapesDirty = true;
172 }
173 
174 
176  int aError, ERROR_LOC aErrorLoc ) const
177 {
178  SHAPE_POLY_SET polyset;
179 
180  for( const std::shared_ptr<PCB_SHAPE>& primitive : m_editPrimitives )
181  primitive->TransformShapeWithClearanceToPolygon( polyset, aLayer, 0, aError, aErrorLoc );
182 
184 
185  // Merge all polygons with the initial pad anchor shape
186  if( polyset.OutlineCount() )
187  {
188  aMergedPolygon->BooleanAdd( polyset, SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
189  aMergedPolygon->Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
190  }
191 }
192 
193 void D_PAD::MergePrimitivesAsPolygon( SHAPE_POLY_SET* aMergedPolygon, PCB_LAYER_ID aLayer ) const
194 {
195  BOARD* board = GetBoard();
196  int maxError = board ? board->GetDesignSettings().m_MaxError: ARC_HIGH_DEF;
197 
198  aMergedPolygon->RemoveAllContours();
199 
200  // Add the anchor pad shape in aMergedPolygon, others in aux_polyset:
201  // The anchor pad is always at 0,0
202  switch( GetAnchorPadShape() )
203  {
204  case PAD_SHAPE_RECT:
205  {
206  SHAPE_RECT rect( -GetSize().x / 2, -GetSize().y / 2, GetSize().x, GetSize().y );
207  aMergedPolygon->AddOutline( rect.Outline() );
208  }
209  break;
210 
211  default:
212  case PAD_SHAPE_CIRCLE:
213  TransformCircleToPolygon( *aMergedPolygon, wxPoint( 0, 0 ), GetSize().x / 2, maxError,
214  ERROR_INSIDE );
215  break;
216  }
217 
218  addPadPrimitivesToPolygon( aMergedPolygon, aLayer, maxError, ERROR_INSIDE );
219 }
220 
221 
223 {
224  SHAPE_POLY_SET poly;
226 
227  if( poly.OutlineCount() > 1 )
228  return false;
229 
230  const int minSteps = 10;
231  const int maxSteps = 50;
232 
233  int stepsX, stepsY;
234 
235  auto bbox = poly.BBox();
236 
237  if( bbox.GetWidth() < bbox.GetHeight() )
238  {
239  stepsX = minSteps;
240  stepsY = minSteps * (double) bbox.GetHeight() / (double )(bbox.GetWidth() + 1);
241  }
242  else
243  {
244  stepsY = minSteps;
245  stepsX = minSteps * (double) bbox.GetWidth() / (double )(bbox.GetHeight() + 1);
246  }
247 
248  stepsX = std::max(minSteps, std::min( maxSteps, stepsX ) );
249  stepsY = std::max(minSteps, std::min( maxSteps, stepsY ) );
250 
251  VECTOR2I center = bbox.Centre();
252 
253  int64_t minDist = std::numeric_limits<int64_t>::max();
254  int64_t minDistEdge;
255 
257  {
258  minDistEdge = GetSize().x;
259  }
260  else
261  {
262  minDistEdge = std::max( GetSize().x, GetSize().y );
263  }
264 
265  OPT<VECTOR2I> bestAnchor( []()->OPT<VECTOR2I> { return NULLOPT; }() );
266 
267  for( int y = 0; y < stepsY ; y++ )
268  {
269  for( int x = 0; x < stepsX; x++ )
270  {
271  VECTOR2I p = bbox.GetPosition();
272  p.x += rescale( x, bbox.GetWidth(), (stepsX - 1) );
273  p.y += rescale( y, bbox.GetHeight(), (stepsY - 1) );
274 
275  if( poly.Contains(p) )
276  {
277 
278  int dist = (center - p).EuclideanNorm();
279  int distEdge = poly.COutline(0).Distance( p, true );
280 
281  if( distEdge >= minDistEdge )
282  {
283  if( dist < minDist )
284  {
285  bestAnchor = p;
286  minDist = dist;
287  }
288  }
289  }
290  }
291  }
292 
293  if( bestAnchor )
294  {
295  aPos = *bestAnchor;
296  return true;
297  }
298 
299  return false;
300 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:134
void MergePrimitivesAsPolygon(SHAPE_POLY_SET *aMergedPolygon, PCB_LAYER_ID aLayer) const
Merge all basic shapes to a SHAPE_POLY_SET Note: The results are relative to the pad position,...
int OutlineCount() const
Returns the number of outlines in the set
const SHAPE_LINE_CHAIN Outline() const
Definition: shape_rect.h:176
void BooleanAdd(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Performs boolean polyset union For aFastMode meaning, see function booleanOp
void SetBezControl2(const wxPoint &aPoint)
Definition: pcb_shape.h:119
PAD_SHAPE_T GetAnchorPadShape() const
Function GetAnchorPadShape.
Definition: class_pad.h:178
Classes BOARD_ITEM and BOARD_CONNECTED_ITEM.
bool GetBestAnchorPosition(VECTOR2I &aPos)
bool m_shapesDirty
Definition: class_pad.h:666
Bezier Curve.
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: class_board.h:558
int Distance(const VECTOR2I &aP, bool aOutlineOnly=false) const
Function Distance()
bool Contains(const VECTOR2I &aP, int aSubpolyIndex=-1, int aAccuracy=0, bool aUseBBoxCaches=false) const
Returns true if a given subpolygon contains the point aP.
Arcs (with rounded ends)
void AppendPrimitives(const std::vector< std::shared_ptr< PCB_SHAPE >> &aPrimitivesList)
Import a custom shape primites list (composed of basic shapes) and add items to the current list.
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
PCB_LAYER_ID
A quick note on layer IDs:
void AddPrimitiveRect(const wxPoint &aStart, const wxPoint &aEnd, int aThickness)
segment with non rounded ends
const auto NULLOPT
Definition: optional.h:9
void SetShape(PCB_SHAPE_TYPE_T aShape)
Definition: pcb_shape.h:113
SHAPE_POLY_SET.
void AddPrimitiveCurve(const wxPoint &aStart, const wxPoint &aEnd, const wxPoint &aCtrl1, const wxPoint &aCtrl2, int aThickness)
virtual BOARD * GetBoard() const
Function GetBoard returns the BOARD in which this BOARD_ITEM resides, or NULL if none.
void ReplacePrimitives(const std::vector< std::shared_ptr< PCB_SHAPE >> &aPrimitivesList)
Clear the current custom shape primitives list and import a new list.
void Simplify(POLYGON_MODE aFastMode)
Simplifies the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFast...
void SetCenter(const wxPoint &aCenterPoint)
For arcs and circles:
Definition: pcb_shape.h:213
void SetBezControl1(const wxPoint &aPoint)
Definition: pcb_shape.h:116
CONST_ITERATOR CIterate(int aFirst, int aLast, bool aIterateHoles=false) const
void Fracture(POLYGON_MODE aFastMode)
Converts a set of polygons with holes to a singe outline with "slits"/"fractures" connecting the oute...
Pad object description.
void TransformCircleToPolygon(SHAPE_POLY_SET &aCornerBuffer, wxPoint aCenter, int aRadius, int aError, ERROR_LOC aErrorLoc)
Function TransformCircleToPolygon convert a circle to a polygon, using multiple straight lines.
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index
void AddPrimitiveArc(const wxPoint &aCenter, const wxPoint &aStart, int aArcAngle, int aThickness)
void addPadPrimitivesToPolygon(SHAPE_POLY_SET *aMergedPolygon, PCB_LAYER_ID aLayer, int aError, ERROR_LOC aErrorLoc) const
std::vector< std::shared_ptr< PCB_SHAPE > > m_editPrimitives
Definition: class_pad.h:663
void SetStart(const wxPoint &aStart)
Definition: pcb_shape.h:132
Information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:186
void AddPrimitivePoly(const SHAPE_POLY_SET &aPoly, int aThickness)
Has meaning only for custom shape pads.
polygon (not yet used for tracks, but could be in microwave apps)
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
void RemoveAllContours()
Removes all outlines & holes (clears) the polygon set.
void SetWidth(int aWidth)
Definition: pcb_shape.h:99
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
Definition: util.h:95
coord_type GetHeight() const
Definition: box2.h:198
void AddPrimitiveCircle(const wxPoint &aCenter, int aRadius, int aThickness)
boost::optional< T > OPT
Definition: optional.h:7
void AddPrimitive(PCB_SHAPE *aPrimitive)
Add item to the custom shape primitives list.
const wxSize & GetSize() const
Definition: class_pad.h:231
virtual void SetAngle(double aAngle, bool aUpdateEnd=true)
Function SetAngle sets the angle for arcs, and normalizes it within the range 0 - 360 degrees.
Definition: pcb_shape.cpp:435
const BOX2I BBox(int aClearance=0) const override
Function BBox()
void SetPolyPoints(const std::vector< wxPoint > &aPoints)
Definition: pcb_shape.cpp:1057
void DeletePrimitivesList()
clear the basic shapes list
void SetEnd(const wxPoint &aEnd)
Definition: pcb_shape.h:143
void SetArcStart(const wxPoint &aArcStartPoint)
Initialize the start arc point.
Definition: pcb_shape.h:197
void AddPrimitiveSegment(const wxPoint &aStart, const wxPoint &aEnd, int aThickness)
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)