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 <fctsys.h>
39 #include <convert_to_biu.h>
40 #include <confirm.h>
41 #include <pcbnew.h>
42 #include <pcb_edit_frame.h>
43 #include <class_board.h>
44 #include <class_module.h>
46 
48 {
49  int n; // Original index of this subrect, before sorting
50 
51  TSubRect() : TRect(),
52  n( 0 )
53  {
54  }
55 
56  TSubRect( int _w, int _h, int _n ) :
57  TRect( 0, 0, _w, _h ), n( _n ) { }
58 };
59 
60 typedef std::vector<TSubRect> CSubRectArray;
61 
62 // Use 0.01 mm units to calculate placement, to avoid long calculation time
63 const int scale = (int)(0.01 * IU_PER_MM);
64 
65 const int PADDING = (int)(1 * IU_PER_MM);
66 
67 // Populates a list of rectangles, from a list of modules
68 void fillRectList( CSubRectArray& vecSubRects, std::vector <MODULE*>& aModuleList )
69 {
70  vecSubRects.clear();
71 
72  for( unsigned ii = 0; ii < aModuleList.size(); ii++ )
73  {
74  EDA_RECT fpBox = aModuleList[ii]->GetFootprintRect();
75  TSubRect fpRect( ( fpBox.GetWidth() + PADDING ) / scale,
76  ( fpBox.GetHeight() + PADDING ) / scale, ii );
77  vecSubRects.push_back( fpRect );
78  }
79 }
80 
81 // Populates a list of rectangles, from a list of EDA_RECT
82 void fillRectList( CSubRectArray& vecSubRects, std::vector <EDA_RECT>& aRectList )
83 {
84  vecSubRects.clear();
85 
86  for( unsigned ii = 0; ii < aRectList.size(); ii++ )
87  {
88  EDA_RECT& rect = aRectList[ii];
89  TSubRect fpRect( rect.GetWidth()/scale, rect.GetHeight()/scale, ii );
90  vecSubRects.push_back( fpRect );
91  }
92 }
93 
94 
95 
96 // Spread a list of rectangles inside a placement area
97 void spreadRectangles( CRectPlacement& aPlacementArea,
98  CSubRectArray& vecSubRects,
99  int areaSizeX, int areaSizeY )
100 {
101  areaSizeX/= scale;
102  areaSizeY/= scale;
103 
104  // Sort the subRects based on dimensions, larger dimension goes first.
105  std::sort( vecSubRects.begin(), vecSubRects.end(), CRectPlacement::TRect::Greater );
106 
107  // gives the initial size to the area
108  aPlacementArea.Init( areaSizeX, areaSizeY );
109 
110  // Add all subrects
111  CSubRectArray::iterator it;
112  for( it = vecSubRects.begin(); it != vecSubRects.end(); )
113  {
114  CRectPlacement::TRect r( 0, 0, it->w, it->h );
115 
116  bool bPlaced = aPlacementArea.AddAtEmptySpotAutoGrow( &r, areaSizeX, areaSizeY );
117 
118  if( !bPlaced ) // No room to place the rectangle: enlarge area and retry
119  {
120  areaSizeX = ceil(areaSizeX * 1.1);
121  areaSizeY = ceil(areaSizeY * 1.1);
122  aPlacementArea.Init( areaSizeX, areaSizeY );
123  it = vecSubRects.begin();
124  continue;
125  }
126 
127  // When correctly placed in a placement area, the coords are returned in r.x and r.y
128  // Store them.
129  it->x = r.x;
130  it->y = r.y;
131 
132  it++;
133  }
134 }
135 
136 
137 void moveFootprintsInArea( CRectPlacement& aPlacementArea,
138  std::vector <MODULE*>& aModuleList,
139  EDA_RECT& aFreeArea,
140  bool aFindAreaOnly )
141 {
142  CSubRectArray vecSubRects;
143 
144  fillRectList( vecSubRects, aModuleList );
145  spreadRectangles( aPlacementArea, vecSubRects,
146  aFreeArea.GetWidth(), aFreeArea.GetHeight() );
147 
148  if( aFindAreaOnly )
149  return;
150 
151  for( unsigned it = 0; it < vecSubRects.size(); ++it )
152  {
153  wxPoint pos( vecSubRects[it].x, vecSubRects[it].y );
154  pos.x *= scale;
155  pos.y *= scale;
156 
157  MODULE * module = aModuleList[vecSubRects[it].n];
158 
159  EDA_RECT fpBBox = module->GetFootprintRect();
160  wxPoint mod_pos = pos + ( module->GetPosition() - fpBBox.GetOrigin() )
161  + aFreeArea.GetOrigin();
162 
163  module->Move( mod_pos - module->GetPosition() );
164  }
165 }
166 
167 static bool sortFootprintsbySheetPath( MODULE* ref, MODULE* compare );
168 
169 
178 void SpreadFootprints( std::vector<MODULE*>* aFootprints,
179  wxPoint aSpreadAreaPosition )
180 {
181  // Build candidate list
182  // calculate also the area needed by these footprints
183  std::vector <MODULE*> footprintList;
184 
185  for( MODULE* footprint : *aFootprints )
186  {
187  if( footprint->IsLocked() )
188  continue;
189 
190  footprint->CalculateBoundingBox();
191  footprintList.push_back( footprint );
192  }
193 
194  if( footprintList.empty() )
195  return;
196 
197  // sort footprints by sheet path. we group them later by sheet
198  sort( footprintList.begin(), footprintList.end(), sortFootprintsbySheetPath );
199 
200  // Extract and place footprints by sheet
201  std::vector <MODULE*> footprintListBySheet;
202  std::vector <EDA_RECT> placementSheetAreas;
203  double subsurface;
204  double placementsurface = 0.0;
205 
206  // The placement uses 2 passes:
207  // the first pass creates the rectangular areas to place footprints
208  // each sheet in schematic creates one rectangular area.
209  // the second pass moves footprints inside these areas
210  for( int pass = 0; pass < 2; pass++ )
211  {
212  int subareaIdx = 0;
213  footprintListBySheet.clear();
214  subsurface = 0.0;
215 
216  for( unsigned ii = 0; ii < footprintList.size(); ii++ )
217  {
218  MODULE* footprint = footprintList[ii];
219  bool islastItem = false;
220 
221  if( ii == footprintList.size() - 1 ||
222  ( footprintList[ii]->GetPath().BeforeLast( '/' ) !=
223  footprintList[ii+1]->GetPath().BeforeLast( '/' ) ) )
224  islastItem = true;
225 
226  footprintListBySheet.push_back( footprint );
227  subsurface += footprint->GetArea( PADDING );
228 
229  if( islastItem )
230  {
231  // end of the footprint sublist relative to the same sheet path
232  // calculate placement of the current sublist
233  EDA_RECT freeArea;
234  int Xsize_allowed = (int) ( sqrt( subsurface ) * 4.0 / 3.0 );
235  int Ysize_allowed = (int) ( subsurface / Xsize_allowed );
236 
237  freeArea.SetWidth( Xsize_allowed );
238  freeArea.SetHeight( Ysize_allowed );
239  CRectPlacement placementArea;
240 
241  if( pass == 1 )
242  {
243  wxPoint areapos = placementSheetAreas[subareaIdx].GetOrigin()
244  + aSpreadAreaPosition;
245  freeArea.SetOrigin( areapos );
246  }
247 
248  bool findAreaOnly = pass == 0;
249  moveFootprintsInArea( placementArea, footprintListBySheet,
250  freeArea, findAreaOnly );
251 
252  if( pass == 0 )
253  {
254  // Populate sheet placement areas list
255  EDA_RECT sub_area;
256  sub_area.SetWidth( placementArea.GetW()*scale );
257  sub_area.SetHeight( placementArea.GetH()*scale );
258  // Add a margin around the sheet placement area:
259  sub_area.Inflate( Millimeter2iu( 1.5 ) );
260 
261  placementSheetAreas.push_back( sub_area );
262 
263  placementsurface += (double) sub_area.GetWidth()*
264  sub_area.GetHeight();
265  }
266 
267  // Prepare buffers for next sheet
268  subsurface = 0.0;
269  footprintListBySheet.clear();
270  subareaIdx++;
271  }
272  }
273 
274  // End of pass:
275  // At the end of the first pass, we have to find position of each sheet
276  // placement area
277  if( pass == 0 )
278  {
279  int Xsize_allowed = (int) ( sqrt( placementsurface ) * 4.0 / 3.0 );
280  int Ysize_allowed = (int) ( placementsurface / Xsize_allowed );
281  CRectPlacement placementArea;
282  CSubRectArray vecSubRects;
283 
284  fillRectList( vecSubRects, placementSheetAreas );
285  spreadRectangles( placementArea, vecSubRects, Xsize_allowed, Ysize_allowed );
286 
287  for( unsigned it = 0; it < vecSubRects.size(); ++it )
288  {
289  TSubRect& srect = vecSubRects[it];
290  wxPoint pos( srect.x*scale, srect.y*scale );
291  wxSize size( srect.w*scale, srect.h*scale );
292  placementSheetAreas[srect.n].SetOrigin( pos );
293  placementSheetAreas[srect.n].SetSize( size );
294  }
295  }
296  } // End pass
297 }
298 
299 
300 // Sort function, used to group footprints by sheet.
301 // Footprints are sorted by their sheet path.
302 // (the full sheet path restricted to the time stamp of the sheet itself,
303 // without the time stamp of the footprint ).
304 static bool sortFootprintsbySheetPath( MODULE* ref, MODULE* compare )
305 {
306  if( ref->GetPath().Length() == compare->GetPath().Length() )
307  return ref->GetPath().BeforeLast( '/' ).Cmp( compare->GetPath().BeforeLast( '/' ) ) < 0;
308 
309  return ref->GetPath().Length() < compare->GetPath().Length();
310 }
double GetArea(int aPadding=0) const
void Init(int w=1, int h=1)
This file is part of the common library.
const wxString & GetPath() const
Definition: class_module.h:206
int GetWidth() const
Definition: eda_rect.h:117
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:124
EDA_RECT GetFootprintRect() const
Function GetFootprintRect() Returns the area of the module footprint excluding any text.
void fillRectList(CSubRectArray &vecSubRects, std::vector< MODULE * > &aModuleList)
static bool Greater(const TRect &a, const TRect &b)
const int PADDING
void SetHeight(int val)
Definition: eda_rect.h:133
const wxPoint GetOrigin() const
Definition: eda_rect.h:112
int GetW() const
static bool sortFootprintsbySheetPath(MODULE *ref, MODULE *compare)
std::vector< TSubRect > CSubRectArray
void SetWidth(int val)
Definition: eda_rect.h:132
int GetHeight() const
Definition: eda_rect.h:118
TSubRect(int _w, int _h, int _n)
const int scale
void Move(const wxPoint &aMoveVector) override
Function Move move this object.
Class 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)
Module description (excepted pads)
void moveFootprintsInArea(CRectPlacement &aPlacementArea, std::vector< MODULE * > &aModuleList, EDA_RECT &aFreeArea, bool aFindAreaOnly)
const wxPoint GetPosition() const override
Definition: class_module.h:188
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.
int GetH() const