KiCad PCB EDA Suite
spread_footprints.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) 2019 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
5  * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
7  *
8  * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23  * or you may search the http://www.gnu.org website for the version 2 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27 
37 #include <algorithm>
38 #include <convert_to_biu.h>
39 #include <confirm.h>
40 #include <pcb_edit_frame.h>
41 #include <class_board.h>
42 #include <class_module.h>
44 
46 {
47  int n; // Original index of this subrect, before sorting
48 
49  TSubRect() : TRect(),
50  n( 0 )
51  {
52  }
53 
54  TSubRect( int _w, int _h, int _n ) :
55  TRect( 0, 0, _w, _h ), n( _n ) { }
56 };
57 
58 typedef std::vector<TSubRect> CSubRectArray;
59 
60 // Use 0.01 mm units to calculate placement, to avoid long calculation time
61 const int scale = (int)(0.01 * IU_PER_MM);
62 
63 const int PADDING = (int)(1 * IU_PER_MM);
64 
65 // Populates a list of rectangles, from a list of modules
66 void fillRectList( CSubRectArray& vecSubRects, std::vector <MODULE*>& aModuleList )
67 {
68  vecSubRects.clear();
69 
70  for( unsigned ii = 0; ii < aModuleList.size(); ii++ )
71  {
72  EDA_RECT fpBox = aModuleList[ii]->GetFootprintRect();
73  TSubRect fpRect( ( fpBox.GetWidth() + PADDING ) / scale,
74  ( fpBox.GetHeight() + PADDING ) / scale, ii );
75  vecSubRects.push_back( fpRect );
76  }
77 }
78 
79 // Populates a list of rectangles, from a list of EDA_RECT
80 void fillRectList( CSubRectArray& vecSubRects, std::vector <EDA_RECT>& aRectList )
81 {
82  vecSubRects.clear();
83 
84  for( unsigned ii = 0; ii < aRectList.size(); ii++ )
85  {
86  EDA_RECT& rect = aRectList[ii];
87  TSubRect fpRect( rect.GetWidth()/scale, rect.GetHeight()/scale, ii );
88  vecSubRects.push_back( fpRect );
89  }
90 }
91 
92 
93 
94 // Spread a list of rectangles inside a placement area
95 void spreadRectangles( CRectPlacement& aPlacementArea,
96  CSubRectArray& vecSubRects,
97  int areaSizeX, int areaSizeY )
98 {
99  areaSizeX/= scale;
100  areaSizeY/= scale;
101 
102  // Sort the subRects based on dimensions, larger dimension goes first.
103  std::sort( vecSubRects.begin(), vecSubRects.end(), CRectPlacement::TRect::Greater );
104 
105  // gives the initial size to the area
106  aPlacementArea.Init( areaSizeX, areaSizeY );
107 
108  // Add all subrects
109  CSubRectArray::iterator it;
110 
111  for( it = vecSubRects.begin(); it != vecSubRects.end(); )
112  {
113  CRectPlacement::TRect r( 0, 0, it->w, it->h );
114 
115  bool bPlaced = aPlacementArea.AddAtEmptySpotAutoGrow( &r, areaSizeX, areaSizeY );
116 
117  if( !bPlaced ) // No room to place the rectangle: enlarge area and retry
118  {
119  bool retry = false;
120 
121  if( areaSizeX < INT_MAX/2 )
122  {
123  retry = true;
124  areaSizeX = areaSizeX * 1.2;
125  }
126 
127  if( areaSizeX < INT_MAX/2 )
128  {
129  retry = true;
130  areaSizeY = areaSizeY * 1.2;
131  }
132 
133  if( retry )
134  {
135  aPlacementArea.Init( areaSizeX, areaSizeY );
136  it = vecSubRects.begin();
137  continue;
138  }
139  }
140 
141  // When correctly placed in a placement area, the coords are returned in r.x and r.y
142  // Store them.
143  it->x = r.x;
144  it->y = r.y;
145 
146  it++;
147  }
148 }
149 
150 
151 void moveFootprintsInArea( CRectPlacement& aPlacementArea,
152  std::vector <MODULE*>& aModuleList,
153  EDA_RECT& aFreeArea,
154  bool aFindAreaOnly )
155 {
156  CSubRectArray vecSubRects;
157 
158  fillRectList( vecSubRects, aModuleList );
159  spreadRectangles( aPlacementArea, vecSubRects,
160  aFreeArea.GetWidth(), aFreeArea.GetHeight() );
161 
162  if( aFindAreaOnly )
163  return;
164 
165  for( unsigned it = 0; it < vecSubRects.size(); ++it )
166  {
167  wxPoint pos( vecSubRects[it].x, vecSubRects[it].y );
168  pos.x *= scale;
169  pos.y *= scale;
170 
171  MODULE * module = aModuleList[vecSubRects[it].n];
172 
173  EDA_RECT fpBBox = module->GetFootprintRect();
174  wxPoint mod_pos = pos + ( module->GetPosition() - fpBBox.GetOrigin() )
175  + aFreeArea.GetOrigin();
176 
177  module->Move( mod_pos - module->GetPosition() );
178  }
179 }
180 
181 static bool sortFootprintsbySheetPath( MODULE* ref, MODULE* compare );
182 
183 
192 void SpreadFootprints( std::vector<MODULE*>* aFootprints,
193  wxPoint aSpreadAreaPosition )
194 {
195  // Build candidate list
196  // calculate also the area needed by these footprints
197  std::vector <MODULE*> footprintList;
198 
199  for( MODULE* footprint : *aFootprints )
200  {
201  if( footprint->IsLocked() )
202  continue;
203 
204  footprint->CalculateBoundingBox();
205  footprintList.push_back( footprint );
206  }
207 
208  if( footprintList.empty() )
209  return;
210 
211  // sort footprints by sheet path. we group them later by sheet
212  sort( footprintList.begin(), footprintList.end(), sortFootprintsbySheetPath );
213 
214  // Extract and place footprints by sheet
215  std::vector <MODULE*> footprintListBySheet;
216  std::vector <EDA_RECT> placementSheetAreas;
217  double subsurface;
218  double placementsurface = 0.0;
219 
220  // The placement uses 2 passes:
221  // the first pass creates the rectangular areas to place footprints
222  // each sheet in schematic creates one rectangular area.
223  // the second pass moves footprints inside these areas
224  for( int pass = 0; pass < 2; pass++ )
225  {
226  int subareaIdx = 0;
227  footprintListBySheet.clear();
228  subsurface = 0.0;
229 
230  int fp_max_width = 0;
231  int fp_max_height = 0;
232 
233  for( unsigned ii = 0; ii < footprintList.size(); ii++ )
234  {
235  MODULE* footprint = footprintList[ii];
236  bool islastItem = false;
237 
238  if( ii == footprintList.size() - 1 ||
239  ( footprintList[ii]->GetPath().AsString().BeforeLast( '/' ) !=
240  footprintList[ii+1]->GetPath().AsString().BeforeLast( '/' ) ) )
241  islastItem = true;
242 
243  footprintListBySheet.push_back( footprint );
244  subsurface += footprint->GetArea( PADDING );
245 
246  // Calculate min size of placement area:
247  EDA_RECT bbox = footprint->GetFootprintRect();
248  fp_max_width = std::max( fp_max_width, bbox.GetWidth() );
249  fp_max_height = std::max( fp_max_height, bbox.GetHeight() );
250 
251  if( islastItem )
252  {
253  // end of the footprint sublist relative to the same sheet path
254  // calculate placement of the current sublist
255  EDA_RECT freeArea;
256  int Xsize_allowed = (int) ( sqrt( subsurface ) * 4.0 / 3.0 );
257  Xsize_allowed = std::max( fp_max_width, Xsize_allowed );
258 
259  int Ysize_allowed = (int) ( subsurface / Xsize_allowed );
260  Ysize_allowed = std::max( fp_max_height, Ysize_allowed );
261 
262  freeArea.SetWidth( Xsize_allowed );
263  freeArea.SetHeight( Ysize_allowed );
264  CRectPlacement placementArea;
265 
266  if( pass == 1 )
267  {
268  wxPoint areapos = placementSheetAreas[subareaIdx].GetOrigin()
269  + aSpreadAreaPosition;
270  freeArea.SetOrigin( areapos );
271  }
272 
273  bool findAreaOnly = pass == 0;
274  moveFootprintsInArea( placementArea, footprintListBySheet,
275  freeArea, findAreaOnly );
276 
277  if( pass == 0 )
278  {
279  // Populate sheet placement areas list
280  EDA_RECT sub_area;
281  sub_area.SetWidth( placementArea.GetW()*scale );
282  sub_area.SetHeight( placementArea.GetH()*scale );
283  // Add a margin around the sheet placement area:
284  sub_area.Inflate( Millimeter2iu( 1.5 ) );
285 
286  placementSheetAreas.push_back( sub_area );
287 
288  placementsurface += (double) sub_area.GetWidth()*
289  sub_area.GetHeight();
290  }
291 
292  // Prepare buffers for next sheet
293  subsurface = 0.0;
294  footprintListBySheet.clear();
295  subareaIdx++;
296  }
297  }
298 
299  // End of pass:
300  // At the end of the first pass, we have to find position of each sheet
301  // placement area
302  if( pass == 0 )
303  {
304  int Xsize_allowed = (int) ( sqrt( placementsurface ) * 4.0 / 3.0 );
305 
306  if( Xsize_allowed < 0 || Xsize_allowed > INT_MAX/2 )
307  Xsize_allowed = INT_MAX/2;
308 
309  int Ysize_allowed = (int) ( placementsurface / Xsize_allowed );
310 
311  if( Ysize_allowed < 0 || Ysize_allowed > INT_MAX/2 )
312  Ysize_allowed = INT_MAX/2;
313 
314  CRectPlacement placementArea;
315  CSubRectArray vecSubRects;
316  fillRectList( vecSubRects, placementSheetAreas );
317  spreadRectangles( placementArea, vecSubRects, Xsize_allowed, Ysize_allowed );
318 
319  for( unsigned it = 0; it < vecSubRects.size(); ++it )
320  {
321  TSubRect& srect = vecSubRects[it];
322  wxPoint pos( srect.x*scale, srect.y*scale );
323  wxSize size( srect.w*scale, srect.h*scale );
324 
325  // Avoid too large coordinates: Overlapping components
326  // are better than out of screen components
327  if( (uint64_t)pos.x + (uint64_t)size.x > INT_MAX/2 )
328  pos.x = 0;
329 
330  if( (uint64_t)pos.y + (uint64_t)size.y > INT_MAX/2 )
331  pos.y = 0;
332 
333  placementSheetAreas[srect.n].SetOrigin( pos );
334  placementSheetAreas[srect.n].SetSize( size );
335  }
336  }
337  } // End pass
338 }
339 
340 
341 // Sort function, used to group footprints by sheet.
342 // Footprints are sorted by their sheet path.
343 // (the full sheet path restricted to the time stamp of the sheet itself,
344 // without the time stamp of the footprint ).
345 static bool sortFootprintsbySheetPath( MODULE* ref, MODULE* compare )
346 {
347  return ref->GetPath() < compare->GetPath();
348 }
double GetArea(int aPadding=0) const
void Init(int w=1, int h=1)
This file is part of the common library.
static constexpr double IU_PER_MM
Mock up a conversion function.
int GetWidth() const
Definition: eda_rect.h:119
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
EDA_RECT GetFootprintRect() const
Function GetFootprintRect() Build and returns the boundary box of the module footprint excluding any ...
void fillRectList(CSubRectArray &vecSubRects, std::vector< MODULE * > &aModuleList)
const KIID_PATH & GetPath() const
Definition: class_module.h:219
static bool Greater(const TRect &a, const TRect &b)
const int PADDING
void SetHeight(int val)
Definition: eda_rect.h:186
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
int GetW() const
static bool sortFootprintsbySheetPath(MODULE *ref, MODULE *compare)
std::vector< TSubRect > CSubRectArray
void SetWidth(int val)
Definition: eda_rect.h:180
int GetHeight() const
Definition: eda_rect.h:120
TSubRect(int _w, int _h, int _n)
const int scale
void Move(const wxPoint &aMoveVector) override
Function Move move this object.
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
void SpreadFootprints(std::vector< MODULE * > *aFootprints, wxPoint aSpreadAreaPosition)
Footprints (after loaded by reading a netlist for instance) are moved to be in a small free area (out...
bool AddAtEmptySpotAutoGrow(TRect *pRect, int maxW, int maxH)
wxPoint GetPosition() const override
Definition: class_module.h:201
static constexpr int Millimeter2iu(double mm)
void moveFootprintsInArea(CRectPlacement &aPlacementArea, std::vector< MODULE * > &aModuleList, EDA_RECT &aFreeArea, bool aFindAreaOnly)
void spreadRectangles(CRectPlacement &aPlacementArea, CSubRectArray &vecSubRects, int areaSizeX, int areaSizeY)
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:363
int GetH() const