2013-11-27 13:20:42 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2019-08-10 18:29:54 +00:00
|
|
|
* Copyright (C) 2019 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
|
2013-11-27 13:20:42 +00:00
|
|
|
* Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
|
|
|
* Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
|
|
|
|
*
|
2022-09-27 13:30:17 +00:00
|
|
|
* Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-11-27 13:20:42 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file spread_footprints.cpp
|
|
|
|
* @brief functions to spread footprints on free areas outside a board.
|
2021-06-09 19:32:58 +00:00
|
|
|
* this is useful after reading a netlist, when new footprints are loaded
|
2013-11-27 13:20:42 +00:00
|
|
|
* and stacked at 0,0 coordinate.
|
|
|
|
* Often, spread them on a free area near the board being edited make more easy
|
|
|
|
* their selection.
|
|
|
|
*/
|
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
#include <spread_footprints.h>
|
2013-11-27 13:20:42 +00:00
|
|
|
#include <algorithm>
|
2022-09-27 13:30:17 +00:00
|
|
|
#include <refdes_utils.h>
|
|
|
|
#include <string_utils.h>
|
2013-11-27 13:20:42 +00:00
|
|
|
#include <confirm.h>
|
2018-01-29 20:58:58 +00:00
|
|
|
#include <pcb_edit_frame.h>
|
2020-11-12 20:19:22 +00:00
|
|
|
#include <board.h>
|
2022-09-27 13:30:17 +00:00
|
|
|
#include <rectpack2d/finders_interface.h>
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2015-02-22 21:25:29 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
constexpr bool allow_flip = true;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
using spaces_type = rectpack2D::empty_spaces<allow_flip, rectpack2D::default_empty_spaces>;
|
|
|
|
using rect_type = rectpack2D::output_rect_t<spaces_type>;
|
|
|
|
using rect_ptr = rect_type*;
|
|
|
|
using rect_vector = std::vector<rect_type>;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
|
|
|
// Use 0.01 mm units to calculate placement, to avoid long calculation time
|
2022-09-17 00:45:14 +00:00
|
|
|
const int scale = (int) ( 0.01 * pcbIUScale.IU_PER_MM );
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2018-09-13 21:29:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
static bool compareFootprintsbyRef( FOOTPRINT* ref, FOOTPRINT* compare )
|
2013-11-27 13:20:42 +00:00
|
|
|
{
|
2022-09-27 13:30:17 +00:00
|
|
|
const wxString& refPrefix = UTIL::GetRefDesPrefix( ref->GetReference() );
|
|
|
|
const wxString& cmpPrefix = UTIL::GetRefDesPrefix( compare->GetReference() );
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
if( refPrefix != cmpPrefix )
|
2013-11-27 13:20:42 +00:00
|
|
|
{
|
2022-09-27 13:30:17 +00:00
|
|
|
return refPrefix < cmpPrefix;
|
2013-11-27 13:20:42 +00:00
|
|
|
}
|
2022-09-27 13:30:17 +00:00
|
|
|
else
|
2013-11-27 13:20:42 +00:00
|
|
|
{
|
2022-09-27 13:30:17 +00:00
|
|
|
const int refInt = GetTrailingInt( ref->GetReference() );
|
|
|
|
const int cmpInt = GetTrailingInt( compare->GetReference() );
|
|
|
|
|
|
|
|
return refInt < cmpInt;
|
2013-11-27 13:20:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-11-27 13:20:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Spread a list of rectangles inside a placement area
|
2022-09-27 13:30:17 +00:00
|
|
|
rectpack2D::rect_wh spreadRectangles( rect_vector& vecSubRects, int areaSizeX, int areaSizeY )
|
2013-11-27 13:20:42 +00:00
|
|
|
{
|
2022-09-27 13:30:17 +00:00
|
|
|
areaSizeX /= scale;
|
|
|
|
areaSizeY /= scale;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
rectpack2D::rect_wh result;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
int max_side = std::max( areaSizeX, areaSizeY );
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
while( true )
|
2013-11-27 13:20:42 +00:00
|
|
|
{
|
2022-09-27 13:30:17 +00:00
|
|
|
bool anyUnsuccessful = false;
|
|
|
|
const int discard_step = 1;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
auto report_successful = [&]( rect_type& )
|
2013-11-27 13:20:42 +00:00
|
|
|
{
|
2022-09-27 13:30:17 +00:00
|
|
|
return rectpack2D::callback_result::CONTINUE_PACKING;
|
|
|
|
};
|
2020-02-29 16:25:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
auto report_unsuccessful = [&]( rect_type& r )
|
|
|
|
{
|
|
|
|
anyUnsuccessful = true;
|
|
|
|
return rectpack2D::callback_result::ABORT_PACKING;
|
|
|
|
};
|
2020-02-29 16:25:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
result = rectpack2D::find_best_packing<spaces_type>(
|
|
|
|
vecSubRects,
|
|
|
|
make_finder_input( max_side, discard_step, report_successful, report_unsuccessful,
|
|
|
|
rectpack2D::flipping_option::DISABLED ) );
|
2020-02-29 16:25:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
if( anyUnsuccessful )
|
|
|
|
{
|
|
|
|
max_side = (int) ( max_side * 1.2 );
|
|
|
|
continue;
|
2013-11-27 13:20:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
break;
|
2013-11-27 13:20:42 +00:00
|
|
|
}
|
2022-09-27 13:30:17 +00:00
|
|
|
|
|
|
|
return result;
|
2013-11-27 13:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
void SpreadFootprints( std::vector<FOOTPRINT*>* aFootprints, VECTOR2I aTargetBoxPosition,
|
|
|
|
bool aGroupBySheet, int aComponentGap, int aGroupGap )
|
2013-11-27 13:20:42 +00:00
|
|
|
{
|
2022-09-27 13:30:17 +00:00
|
|
|
using FpBBoxToFootprintsPair = std::pair<BOX2I, std::vector<FOOTPRINT*>>;
|
|
|
|
using SheetBBoxToFootprintsMapPair =
|
|
|
|
std::pair<BOX2I, std::map<VECTOR2I, FpBBoxToFootprintsPair>>;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
std::map<wxString, SheetBBoxToFootprintsMapPair> sheetsMap;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
// Fill in the maps
|
|
|
|
for( FOOTPRINT* footprint : *aFootprints )
|
2013-11-27 13:20:42 +00:00
|
|
|
{
|
2022-09-27 13:30:17 +00:00
|
|
|
wxString path =
|
|
|
|
aGroupBySheet ? footprint->GetPath().AsString().BeforeLast( '/' ) : wxS( "" );
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
VECTOR2I size = footprint->GetBoundingBox( false, false ).GetSize();
|
|
|
|
size.x += aComponentGap;
|
|
|
|
size.y += aComponentGap;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
sheetsMap[path].second[size].second.push_back( footprint );
|
2013-11-27 13:20:42 +00:00
|
|
|
}
|
2019-08-10 18:29:54 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
for( auto& [sheetPath, sheetPair] : sheetsMap )
|
2013-11-27 13:20:42 +00:00
|
|
|
{
|
2022-09-27 13:30:17 +00:00
|
|
|
auto& [sheet_bbox, sizeToFpMap] = sheetPair;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
for( auto& [fpSize, fpPair] : sizeToFpMap )
|
|
|
|
{
|
|
|
|
auto& [block_bbox, footprints] = fpPair;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
// Find optimal arrangement of same-size footprints
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
double blockEstimateArea = (double) fpSize.x * fpSize.y * footprints.size();
|
|
|
|
double initialSide = std::sqrt( blockEstimateArea );
|
|
|
|
bool vertical = fpSize.x >= fpSize.y;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
int initialCountPerLine = footprints.size();
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
const int singleLineRatio = 5;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
// Wrap the line if the ratio is not satisfied
|
|
|
|
if( vertical )
|
|
|
|
{
|
|
|
|
if( ( fpSize.y * footprints.size() / fpSize.x ) > singleLineRatio )
|
|
|
|
initialCountPerLine = initialSide / fpSize.y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( ( fpSize.x * footprints.size() / fpSize.y ) > singleLineRatio )
|
|
|
|
initialCountPerLine = initialSide / fpSize.x;
|
|
|
|
}
|
2020-02-29 16:25:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
int optimalCountPerLine = initialCountPerLine;
|
|
|
|
int optimalRemainder = footprints.size() % optimalCountPerLine;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
if( optimalRemainder != 0 )
|
|
|
|
{
|
|
|
|
for( int i = std::max( 2, initialCountPerLine - 2 );
|
|
|
|
i <= std::min( (int) footprints.size() - 2, initialCountPerLine + 2 ); i++ )
|
|
|
|
{
|
|
|
|
int r = footprints.size() % i;
|
2014-03-05 17:40:23 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
if( r == 0 || r >= optimalRemainder )
|
|
|
|
{
|
|
|
|
optimalCountPerLine = i;
|
|
|
|
optimalRemainder = r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
std::sort( footprints.begin(), footprints.end(), compareFootprintsbyRef );
|
2020-02-29 16:25:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
// Arrange footprints in rows or columns (blocks)
|
|
|
|
for( unsigned i = 0; i < footprints.size(); i++ )
|
2013-11-27 13:20:42 +00:00
|
|
|
{
|
2022-09-27 13:30:17 +00:00
|
|
|
FOOTPRINT* footprint = footprints[i];
|
2020-02-29 16:25:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
VECTOR2I position = fpSize / 2;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
if( vertical )
|
|
|
|
{
|
|
|
|
position.x += fpSize.x * ( i / optimalCountPerLine );
|
|
|
|
position.y += fpSize.y * ( i % optimalCountPerLine );
|
|
|
|
}
|
|
|
|
else
|
2013-11-27 13:20:42 +00:00
|
|
|
{
|
2022-09-27 13:30:17 +00:00
|
|
|
position.x += fpSize.x * ( i % optimalCountPerLine );
|
|
|
|
position.y += fpSize.y * ( i / optimalCountPerLine );
|
2013-11-27 13:20:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
BOX2I old_fp_bbox = footprint->GetBoundingBox( false, false );
|
|
|
|
footprint->Move( position - old_fp_bbox.GetOrigin() );
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
BOX2I new_fp_bbox = footprint->GetBoundingBox( false, false );
|
|
|
|
new_fp_bbox.Inflate( aComponentGap / 2 );
|
|
|
|
block_bbox.Merge( new_fp_bbox );
|
|
|
|
}
|
|
|
|
}
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
rect_vector vecSubRects;
|
|
|
|
long long blocksArea = 0;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
// Fill in arrays for packing of blocks
|
|
|
|
for( auto& [fpSize, fpPair] : sizeToFpMap )
|
|
|
|
{
|
|
|
|
auto& [block_bbox, footprints] = fpPair;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
vecSubRects.emplace_back( 0, 0, block_bbox.GetWidth() / scale,
|
|
|
|
block_bbox.GetHeight() / scale, false );
|
|
|
|
|
|
|
|
blocksArea += block_bbox.GetArea();
|
2013-11-27 13:20:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
// Pack the blocks
|
|
|
|
int areaSide = std::sqrt( blocksArea );
|
|
|
|
spreadRectangles( vecSubRects, areaSide, areaSide );
|
|
|
|
|
|
|
|
unsigned block_i = 0;
|
|
|
|
|
|
|
|
// Move footprints to the new block locations
|
|
|
|
for( auto& [fpSize, pair] : sizeToFpMap )
|
2013-11-27 13:20:42 +00:00
|
|
|
{
|
2022-09-27 13:30:17 +00:00
|
|
|
auto& [src_bbox, footprints] = pair;
|
2020-02-29 16:25:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
rect_type srect = vecSubRects[block_i];
|
2020-02-29 16:25:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
VECTOR2I target_pos( srect.x * scale, srect.y * scale );
|
|
|
|
VECTOR2I target_size( srect.w * scale, srect.h * scale );
|
2020-02-29 16:25:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
// Avoid too large coordinates: Overlapping components
|
|
|
|
// are better than out of screen components
|
|
|
|
if( (uint64_t) target_pos.x + (uint64_t) target_size.x > INT_MAX / 2 )
|
|
|
|
target_pos.x -= INT_MAX / 2;
|
2020-02-29 16:25:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
if( (uint64_t) target_pos.y + (uint64_t) target_size.y > INT_MAX / 2 )
|
|
|
|
target_pos.y -= INT_MAX / 2;
|
2013-11-27 13:20:42 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
for( FOOTPRINT* footprint : footprints )
|
2013-11-27 13:20:42 +00:00
|
|
|
{
|
2022-09-27 13:30:17 +00:00
|
|
|
footprint->Move( target_pos - src_bbox.GetPosition() );
|
|
|
|
sheet_bbox.Merge( footprint->GetBoundingBox( false, false ) );
|
|
|
|
}
|
2020-02-29 16:25:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
block_i++;
|
|
|
|
}
|
|
|
|
}
|
2020-02-29 16:25:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
rect_vector vecSubRects;
|
|
|
|
long long sheetsArea = 0;
|
2020-02-29 16:25:52 +00:00
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
// Fill in arrays for packing of hierarchical sheet groups
|
|
|
|
for( auto& [sheetPath, sheetPair] : sheetsMap )
|
|
|
|
{
|
|
|
|
auto& [sheet_bbox, sizeToFpMap] = sheetPair;
|
|
|
|
BOX2I rect = sheet_bbox;
|
|
|
|
|
|
|
|
// Add a margin around the sheet placement area:
|
|
|
|
rect.Inflate( aGroupGap );
|
|
|
|
|
|
|
|
vecSubRects.emplace_back( 0, 0, rect.GetWidth() / scale, rect.GetHeight() / scale, false );
|
|
|
|
|
|
|
|
sheetsArea += sheet_bbox.GetArea();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pack the hierarchical sheet groups
|
|
|
|
int areaSide = std::sqrt( sheetsArea );
|
|
|
|
spreadRectangles( vecSubRects, areaSide, areaSide );
|
|
|
|
|
|
|
|
unsigned srect_i = 0;
|
|
|
|
|
|
|
|
// Move footprints to the new hierarchical sheet group locations
|
|
|
|
for( auto& [sheetPath, sheetPair] : sheetsMap )
|
|
|
|
{
|
|
|
|
auto& [src_bbox, sizeToFpMap] = sheetPair;
|
|
|
|
|
|
|
|
rect_type srect = vecSubRects[srect_i];
|
|
|
|
|
|
|
|
VECTOR2I target_pos( srect.x * scale + aTargetBoxPosition.x,
|
|
|
|
srect.y * scale + aTargetBoxPosition.y );
|
|
|
|
VECTOR2I target_size( srect.w * scale, srect.h * scale );
|
|
|
|
|
|
|
|
// Avoid too large coordinates: Overlapping components
|
|
|
|
// are better than out of screen components
|
|
|
|
if( (uint64_t) target_pos.x + (uint64_t) target_size.x > INT_MAX / 2 )
|
|
|
|
target_pos.x -= INT_MAX / 2;
|
|
|
|
|
|
|
|
if( (uint64_t) target_pos.y + (uint64_t) target_size.y > INT_MAX / 2 )
|
|
|
|
target_pos.y -= INT_MAX / 2;
|
|
|
|
|
|
|
|
for( auto& [fpSize, fpPair] : sizeToFpMap )
|
|
|
|
{
|
|
|
|
auto& [block_bbox, footprints] = fpPair;
|
|
|
|
for( FOOTPRINT* footprint : footprints )
|
|
|
|
{
|
|
|
|
footprint->Move( target_pos - src_bbox.GetPosition() );
|
2013-11-27 13:20:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 13:30:17 +00:00
|
|
|
srect_i++;
|
|
|
|
}
|
2013-11-27 13:20:42 +00:00
|
|
|
}
|