2017-03-19 15:38:30 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
|
|
|
* Copyright (C) 2015 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2023-02-28 15:34:07 +00:00
|
|
|
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-03-19 15:38:30 +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
|
|
|
|
*/
|
|
|
|
|
2023-12-04 06:50:29 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
|
2017-03-19 15:38:30 +00:00
|
|
|
#include <trigo.h>
|
|
|
|
#include <macros.h>
|
|
|
|
|
2019-03-01 13:36:53 +00:00
|
|
|
#include <math/vector2d.h>
|
2020-10-04 23:34:59 +00:00
|
|
|
#include <pcb_shape.h>
|
2020-11-12 20:19:22 +00:00
|
|
|
#include <footprint.h>
|
2021-06-06 19:03:10 +00:00
|
|
|
#include <pad.h>
|
2017-03-19 15:38:30 +00:00
|
|
|
#include <base_units.h>
|
|
|
|
#include <convert_basic_shapes_to_polygon.h>
|
2019-03-01 13:36:53 +00:00
|
|
|
#include <geometry/shape_poly_set.h>
|
2018-03-08 12:48:29 +00:00
|
|
|
#include <geometry/geometry_utils.h>
|
2021-07-14 20:03:32 +00:00
|
|
|
#include <convert_shape_list_to_polygon.h>
|
2023-10-12 12:03:58 +00:00
|
|
|
#include <board.h>
|
|
|
|
#include <collectors.h>
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
#include <wx/log.h>
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag to enable debug tracing for the board outline creation
|
|
|
|
*
|
|
|
|
* Use "KICAD_BOARD_OUTLINE" to enable.
|
|
|
|
*
|
|
|
|
* @ingroup trace_env_vars
|
|
|
|
*/
|
|
|
|
const wxChar* traceBoardOutline = wxT( "KICAD_BOARD_OUTLINE" );
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
|
2023-12-04 07:44:10 +00:00
|
|
|
class SCOPED_FLAGS_CLEANER : public std::unordered_set<EDA_ITEM*>
|
2023-12-04 06:50:29 +00:00
|
|
|
{
|
|
|
|
EDA_ITEM_FLAGS m_flagsToClear;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SCOPED_FLAGS_CLEANER( const EDA_ITEM_FLAGS& aFlagsToClear ) : m_flagsToClear( aFlagsToClear ) {}
|
|
|
|
|
|
|
|
~SCOPED_FLAGS_CLEANER()
|
|
|
|
{
|
|
|
|
for( EDA_ITEM* item : *this )
|
|
|
|
item->ClearFlags( m_flagsToClear );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-03-19 15:38:30 +00:00
|
|
|
/**
|
2023-10-12 16:27:30 +00:00
|
|
|
* Local and tunable method of qualifying the proximity of two points.
|
2017-03-19 15:38:30 +00:00
|
|
|
*
|
2023-10-12 16:27:30 +00:00
|
|
|
* @param aLeft is the first point.
|
|
|
|
* @param aRight is the second point.
|
2017-03-19 15:38:30 +00:00
|
|
|
* @param aLimit is a measure of proximity that the caller knows about.
|
2023-10-12 16:27:30 +00:00
|
|
|
* @return true if the two points are close enough, else false.
|
2017-03-19 15:38:30 +00:00
|
|
|
*/
|
2023-09-04 03:44:22 +00:00
|
|
|
static bool close_enough( VECTOR2I aLeft, VECTOR2I aRight, unsigned aLimit )
|
2017-03-19 15:38:30 +00:00
|
|
|
{
|
2020-12-27 01:00:41 +00:00
|
|
|
return ( aLeft - aRight ).SquaredEuclideanNorm() <= SEG::Square( aLimit );
|
2017-03-19 15:38:30 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
|
2017-03-19 15:38:30 +00:00
|
|
|
/**
|
2020-12-25 14:01:34 +00:00
|
|
|
* Local method which qualifies whether the start or end point of a segment is closest to a point.
|
2017-03-19 15:38:30 +00:00
|
|
|
*
|
2020-12-27 01:00:41 +00:00
|
|
|
* @param aRef is the reference point
|
2017-03-19 15:38:30 +00:00
|
|
|
* @param aFirst is the first point
|
|
|
|
* @param aSecond is the second point
|
2023-10-12 16:27:30 +00:00
|
|
|
* @return true if the first point is closest to the reference, otherwise false.
|
2017-03-19 15:38:30 +00:00
|
|
|
*/
|
2023-09-04 03:44:22 +00:00
|
|
|
static bool closer_to_first( VECTOR2I aRef, VECTOR2I aFirst, VECTOR2I aSecond )
|
2017-03-19 15:38:30 +00:00
|
|
|
{
|
2020-12-27 01:00:41 +00:00
|
|
|
return ( aRef - aFirst ).SquaredEuclideanNorm() < ( aRef - aSecond ).SquaredEuclideanNorm();
|
2017-03-19 15:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-10-12 16:27:30 +00:00
|
|
|
* Search for a #PCB_SHAPE matching a given end point or start point in a list.
|
|
|
|
*
|
2020-10-22 20:27:46 +00:00
|
|
|
* @param aShape The starting shape.
|
2017-03-19 15:38:30 +00:00
|
|
|
* @param aPoint The starting or ending point to search for.
|
2017-06-17 08:40:38 +00:00
|
|
|
* @param aList The list to remove from.
|
2017-03-19 15:38:30 +00:00
|
|
|
* @param aLimit is the distance from \a aPoint that still constitutes a valid find.
|
2023-10-12 16:27:30 +00:00
|
|
|
* @return The first #PCB_SHAPE that has a start or end point matching aPoint, otherwise nullptr.
|
2017-03-19 15:38:30 +00:00
|
|
|
*/
|
2022-01-01 06:04:08 +00:00
|
|
|
static PCB_SHAPE* findNext( PCB_SHAPE* aShape, const VECTOR2I& aPoint,
|
2020-10-22 20:27:46 +00:00
|
|
|
const std::vector<PCB_SHAPE*>& aList, unsigned aLimit )
|
2017-03-19 15:38:30 +00:00
|
|
|
{
|
2020-12-27 01:00:41 +00:00
|
|
|
// Look for an unused, exact hit
|
|
|
|
for( PCB_SHAPE* graphic : aList )
|
2017-03-19 15:38:30 +00:00
|
|
|
{
|
2020-12-27 01:00:41 +00:00
|
|
|
if( graphic == aShape || ( graphic->GetFlags() & SKIP_STRUCT ) != 0 )
|
2020-10-22 20:27:46 +00:00
|
|
|
continue;
|
|
|
|
|
2021-07-17 19:56:18 +00:00
|
|
|
if( aPoint == graphic->GetStart() || aPoint == graphic->GetEnd() )
|
|
|
|
return graphic;
|
2020-12-27 01:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search again for anything that's close, even something already used. (The latter is
|
|
|
|
// important for error reporting.)
|
|
|
|
VECTOR2I pt( aPoint );
|
|
|
|
SEG::ecoord closest_dist_sq = SEG::Square( aLimit );
|
|
|
|
PCB_SHAPE* closest_graphic = nullptr;
|
|
|
|
SEG::ecoord d_sq;
|
|
|
|
|
|
|
|
for( PCB_SHAPE* graphic : aList )
|
|
|
|
{
|
|
|
|
if( graphic == aShape )
|
|
|
|
continue;
|
|
|
|
|
2021-07-17 19:56:18 +00:00
|
|
|
d_sq = ( pt - graphic->GetStart() ).SquaredEuclideanNorm();
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2021-07-17 19:56:18 +00:00
|
|
|
if( d_sq < closest_dist_sq )
|
|
|
|
{
|
|
|
|
closest_dist_sq = d_sq;
|
|
|
|
closest_graphic = graphic;
|
|
|
|
}
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2021-07-17 19:56:18 +00:00
|
|
|
d_sq = ( pt - graphic->GetEnd() ).SquaredEuclideanNorm();
|
2020-12-27 01:00:41 +00:00
|
|
|
|
2021-07-17 19:56:18 +00:00
|
|
|
if( d_sq < closest_dist_sq )
|
|
|
|
{
|
|
|
|
closest_dist_sq = d_sq;
|
|
|
|
closest_graphic = graphic;
|
2017-03-19 15:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 01:00:41 +00:00
|
|
|
return closest_graphic; // Note: will be nullptr if nothing within aLimit
|
2017-03-19 15:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-04 03:44:22 +00:00
|
|
|
static bool isCopperOutside( const FOOTPRINT* aFootprint, SHAPE_POLY_SET& aShape )
|
2022-12-02 16:33:00 +00:00
|
|
|
{
|
|
|
|
bool padOutside = false;
|
|
|
|
|
|
|
|
for( PAD* pad : aFootprint->Pads() )
|
|
|
|
{
|
|
|
|
SHAPE_POLY_SET poly = aShape.CloneDropTriangulation();
|
|
|
|
|
2024-05-18 19:50:05 +00:00
|
|
|
poly.ClearArcs();
|
|
|
|
|
2023-10-13 11:25:52 +00:00
|
|
|
poly.BooleanIntersection( *pad->GetEffectivePolygon( ERROR_INSIDE ),
|
|
|
|
SHAPE_POLY_SET::PM_FAST );
|
2022-12-02 16:33:00 +00:00
|
|
|
|
|
|
|
if( poly.OutlineCount() == 0 )
|
|
|
|
{
|
|
|
|
VECTOR2I padPos = pad->GetPosition();
|
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Tested pad (%d, %d): outside" ),
|
|
|
|
padPos.x, padPos.y );
|
|
|
|
padOutside = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
VECTOR2I padPos = pad->GetPosition();
|
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Tested pad (%d, %d): not outside" ),
|
|
|
|
padPos.x, padPos.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
return padOutside;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-09 16:24:47 +00:00
|
|
|
bool doConvertOutlineToPolygon( std::vector<PCB_SHAPE*>& aShapeList, SHAPE_POLY_SET& aPolygons,
|
|
|
|
int aErrorMax, int aChainingEpsilon, bool aAllowDisjoint,
|
|
|
|
OUTLINE_ERROR_HANDLER* aErrorHandler, bool aAllowUseArcsInPolygons,
|
|
|
|
SCOPED_FLAGS_CLEANER& aCleaner )
|
2017-03-19 15:38:30 +00:00
|
|
|
{
|
2022-10-14 19:30:56 +00:00
|
|
|
if( aShapeList.size() == 0 )
|
2017-03-19 15:38:30 +00:00
|
|
|
return true;
|
|
|
|
|
2020-12-26 10:59:06 +00:00
|
|
|
bool selfIntersecting = false;
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2020-10-22 20:27:46 +00:00
|
|
|
wxString msg;
|
2020-12-26 10:59:06 +00:00
|
|
|
PCB_SHAPE* graphic = nullptr;
|
2020-10-22 20:27:46 +00:00
|
|
|
|
2022-10-14 19:30:56 +00:00
|
|
|
std::set<PCB_SHAPE*> startCandidates( aShapeList.begin(), aShapeList.end() );
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2022-10-14 19:30:56 +00:00
|
|
|
// Keep a list of where the various shapes came from so after doing our combined-polygon
|
2020-12-25 14:01:34 +00:00
|
|
|
// tests we can still report errors against the individual graphic items.
|
2022-10-14 19:30:56 +00:00
|
|
|
std::map<std::pair<VECTOR2I, VECTOR2I>, PCB_SHAPE*> shapeOwners;
|
2020-11-21 20:42:27 +00:00
|
|
|
|
|
|
|
auto fetchOwner =
|
|
|
|
[&]( const SEG& seg ) -> PCB_SHAPE*
|
|
|
|
{
|
2022-10-14 19:30:56 +00:00
|
|
|
auto it = shapeOwners.find( std::make_pair( seg.A, seg.B ) );
|
|
|
|
return it == shapeOwners.end() ? nullptr : it->second;
|
2020-11-21 20:42:27 +00:00
|
|
|
};
|
|
|
|
|
2020-12-26 10:59:06 +00:00
|
|
|
PCB_SHAPE* prevGraphic = nullptr;
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I prevPt;
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2022-10-14 19:30:56 +00:00
|
|
|
std::vector<SHAPE_LINE_CHAIN> contours;
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2022-10-14 19:30:56 +00:00
|
|
|
for( PCB_SHAPE* shape : startCandidates )
|
|
|
|
shape->ClearFlags( SKIP_STRUCT );
|
2020-02-27 17:52:16 +00:00
|
|
|
|
2020-10-22 20:27:46 +00:00
|
|
|
while( startCandidates.size() )
|
2017-03-19 15:38:30 +00:00
|
|
|
{
|
2020-10-22 20:27:46 +00:00
|
|
|
graphic = (PCB_SHAPE*) *startCandidates.begin();
|
|
|
|
graphic->SetFlags( SKIP_STRUCT );
|
2023-12-09 16:24:47 +00:00
|
|
|
aCleaner.insert( graphic );
|
2020-10-22 20:27:46 +00:00
|
|
|
startCandidates.erase( startCandidates.begin() );
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2022-10-14 19:30:56 +00:00
|
|
|
contours.emplace_back();
|
|
|
|
|
2022-12-01 14:55:51 +00:00
|
|
|
SHAPE_LINE_CHAIN& currContour = contours.back();
|
2023-06-12 18:12:39 +00:00
|
|
|
currContour.SetWidth( graphic->GetWidth() );
|
2022-10-14 19:30:56 +00:00
|
|
|
bool firstPt = true;
|
|
|
|
|
|
|
|
// Circles, rects and polygons are closed shapes unto themselves (and do not combine
|
|
|
|
// with other shapes), so process them separately.
|
2021-07-21 18:31:25 +00:00
|
|
|
if( graphic->GetShape() == SHAPE_T::POLY )
|
2018-12-02 04:41:17 +00:00
|
|
|
{
|
|
|
|
for( auto it = graphic->GetPolyShape().CIterate(); it; it++ )
|
|
|
|
{
|
2020-11-21 20:42:27 +00:00
|
|
|
VECTOR2I pt = *it;
|
2018-12-02 15:33:12 +00:00
|
|
|
|
2022-12-01 14:55:51 +00:00
|
|
|
currContour.Append( pt );
|
2020-11-21 20:42:27 +00:00
|
|
|
|
|
|
|
if( firstPt )
|
|
|
|
firstPt = false;
|
|
|
|
else
|
2022-10-14 19:30:56 +00:00
|
|
|
shapeOwners[ std::make_pair( prevPt, pt ) ] = graphic;
|
2020-11-21 20:42:27 +00:00
|
|
|
|
2022-01-02 02:06:40 +00:00
|
|
|
prevPt = pt;
|
2018-12-02 04:41:17 +00:00
|
|
|
}
|
2022-10-14 19:30:56 +00:00
|
|
|
|
2022-12-01 14:55:51 +00:00
|
|
|
currContour.SetClosed( true );
|
2018-12-02 04:41:17 +00:00
|
|
|
}
|
2021-07-21 18:31:25 +00:00
|
|
|
else if( graphic->GetShape() == SHAPE_T::CIRCLE )
|
2017-03-19 15:38:30 +00:00
|
|
|
{
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I center = graphic->GetCenter();
|
2023-05-16 07:59:53 +00:00
|
|
|
int radius = graphic->GetRadius();
|
2023-05-16 11:39:16 +00:00
|
|
|
VECTOR2I start = center;
|
2023-05-16 07:59:53 +00:00
|
|
|
start.x += radius;
|
2020-11-21 20:42:27 +00:00
|
|
|
|
2023-05-16 11:39:16 +00:00
|
|
|
// Add 360 deg Arc in currContour
|
|
|
|
SHAPE_ARC arc360( center, start, ANGLE_360, 0 );
|
|
|
|
currContour.Append( arc360, aErrorMax );
|
|
|
|
currContour.SetClosed( true );
|
2023-05-16 07:59:53 +00:00
|
|
|
|
2023-05-16 11:39:16 +00:00
|
|
|
// set shapeOwners for currContour points created by appending the arc360:
|
|
|
|
for( int ii = 1; ii < currContour.PointCount(); ++ii )
|
|
|
|
{
|
|
|
|
shapeOwners[ std::make_pair( currContour.CPoint( ii-1 ),
|
|
|
|
currContour.CPoint( ii ) ) ] = graphic;
|
2017-03-19 15:38:30 +00:00
|
|
|
}
|
2023-05-16 07:59:53 +00:00
|
|
|
|
2023-05-16 11:39:16 +00:00
|
|
|
if( !aAllowUseArcsInPolygons )
|
|
|
|
currContour.ClearArcs();
|
2017-03-19 15:38:30 +00:00
|
|
|
}
|
2023-07-24 16:07:56 +00:00
|
|
|
else if( graphic->GetShape() == SHAPE_T::RECTANGLE )
|
2020-06-15 19:50:20 +00:00
|
|
|
{
|
2022-01-01 06:04:08 +00:00
|
|
|
std::vector<VECTOR2I> pts = graphic->GetRectCorners();
|
2020-06-15 19:50:20 +00:00
|
|
|
|
2022-01-01 06:04:08 +00:00
|
|
|
for( const VECTOR2I& pt : pts )
|
2020-11-21 20:42:27 +00:00
|
|
|
{
|
2022-12-01 14:55:51 +00:00
|
|
|
currContour.Append( pt );
|
2020-11-21 20:42:27 +00:00
|
|
|
|
|
|
|
if( firstPt )
|
|
|
|
firstPt = false;
|
|
|
|
else
|
2022-10-14 19:30:56 +00:00
|
|
|
shapeOwners[ std::make_pair( prevPt, pt ) ] = graphic;
|
2020-11-21 20:42:27 +00:00
|
|
|
|
2022-01-01 06:04:08 +00:00
|
|
|
prevPt = pt;
|
2020-11-21 20:42:27 +00:00
|
|
|
}
|
2022-10-14 19:30:56 +00:00
|
|
|
|
2022-12-01 14:55:51 +00:00
|
|
|
currContour.SetClosed( true );
|
2020-06-15 19:50:20 +00:00
|
|
|
}
|
2017-03-19 15:38:30 +00:00
|
|
|
else
|
|
|
|
{
|
2020-12-25 14:01:34 +00:00
|
|
|
// Polygon start point. Arbitrarily chosen end of the segment and build the poly
|
|
|
|
// from here.
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I startPt = graphic->GetEnd();
|
2021-07-17 19:56:18 +00:00
|
|
|
prevPt = startPt;
|
2022-12-01 14:55:51 +00:00
|
|
|
currContour.Append( prevPt );
|
2017-03-19 15:38:30 +00:00
|
|
|
|
|
|
|
// do not append the other end point yet, this first 'graphic' might be an arc
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
switch( graphic->GetShape() )
|
|
|
|
{
|
2023-07-24 16:07:56 +00:00
|
|
|
case SHAPE_T::RECTANGLE:
|
2022-10-14 19:30:56 +00:00
|
|
|
case SHAPE_T::CIRCLE:
|
|
|
|
{
|
|
|
|
// As a non-first item, closed shapes can't be anything but self-intersecting
|
|
|
|
if( aErrorHandler )
|
|
|
|
{
|
|
|
|
wxASSERT( prevGraphic );
|
2023-10-12 16:27:30 +00:00
|
|
|
(*aErrorHandler)( _( "(self-intersecting)" ), prevGraphic, graphic,
|
|
|
|
prevPt );
|
2022-10-14 19:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
selfIntersecting = true;
|
|
|
|
|
|
|
|
// A closed shape will finish where it started, so no point in updating prevPt
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-21 18:31:25 +00:00
|
|
|
case SHAPE_T::SEGMENT:
|
2023-10-12 16:27:30 +00:00
|
|
|
{
|
|
|
|
VECTOR2I nextPt;
|
|
|
|
|
|
|
|
// Use the line segment end point furthest away from prevPt as we assume
|
|
|
|
// the other end to be ON prevPt or very close to it.
|
|
|
|
if( closer_to_first( prevPt, graphic->GetStart(), graphic->GetEnd()) )
|
|
|
|
nextPt = graphic->GetEnd();
|
|
|
|
else
|
|
|
|
nextPt = graphic->GetStart();
|
|
|
|
|
|
|
|
currContour.Append( nextPt );
|
|
|
|
shapeOwners[ std::make_pair( prevPt, nextPt ) ] = graphic;
|
|
|
|
prevPt = nextPt;
|
|
|
|
}
|
|
|
|
break;
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2021-07-21 18:31:25 +00:00
|
|
|
case SHAPE_T::ARC:
|
2023-10-12 16:27:30 +00:00
|
|
|
{
|
|
|
|
VECTOR2I pstart = graphic->GetStart();
|
|
|
|
VECTOR2I pmid = graphic->GetArcMid();
|
|
|
|
VECTOR2I pend = graphic->GetEnd();
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
if( !close_enough( prevPt, pstart, aChainingEpsilon ) )
|
|
|
|
{
|
|
|
|
wxASSERT( close_enough( prevPt, graphic->GetEnd(), aChainingEpsilon ) );
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
std::swap( pstart, pend );
|
|
|
|
}
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
SHAPE_ARC sarc( pstart, pmid, pend, 0 );
|
2023-08-24 18:49:34 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
SHAPE_LINE_CHAIN arcChain;
|
|
|
|
arcChain.Append( sarc, aErrorMax );
|
2021-11-26 13:03:14 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
if( !aAllowUseArcsInPolygons )
|
|
|
|
arcChain.ClearArcs();
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
// set shapeOwners for arcChain points created by appending the sarc:
|
|
|
|
for( int ii = 1; ii < arcChain.PointCount(); ++ii )
|
|
|
|
{
|
|
|
|
shapeOwners[std::make_pair( arcChain.CPoint( ii - 1 ),
|
|
|
|
arcChain.CPoint( ii ) )] = graphic;
|
|
|
|
}
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
currContour.Append( arcChain );
|
2023-08-24 18:49:34 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
prevPt = pend;
|
|
|
|
}
|
|
|
|
break;
|
2018-07-09 17:15:36 +00:00
|
|
|
|
2021-07-21 18:31:25 +00:00
|
|
|
case SHAPE_T::BEZIER:
|
2023-10-12 16:27:30 +00:00
|
|
|
{
|
2020-12-25 14:01:34 +00:00
|
|
|
// We do not support Bezier curves in polygons, so approximate with a series
|
|
|
|
// of short lines and put those line segments into the !same! PATH.
|
2023-10-12 16:27:30 +00:00
|
|
|
VECTOR2I nextPt;
|
|
|
|
bool reverse = false;
|
2018-07-09 17:15:36 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
// Use the end point furthest away from prevPt as we assume the other
|
|
|
|
// end to be ON prevPt or very close to it.
|
|
|
|
if( closer_to_first( prevPt, graphic->GetStart(), graphic->GetEnd()) )
|
|
|
|
{
|
|
|
|
nextPt = graphic->GetEnd();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nextPt = graphic->GetStart();
|
|
|
|
reverse = true;
|
|
|
|
}
|
2018-07-09 17:15:36 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
// Ensure the approximated Bezier shape is built
|
|
|
|
// a good value is between (Bezier curve width / 2) and (Bezier curve width)
|
|
|
|
// ( and at least 0.05 mm to avoid very small segments)
|
|
|
|
int min_segm_length = std::max( pcbIUScale.mmToIU( 0.05 ),
|
|
|
|
graphic->GetWidth() );
|
|
|
|
graphic->RebuildBezierToSegmentsPointsList( min_segm_length );
|
2023-02-28 15:34:07 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
if( reverse )
|
|
|
|
{
|
|
|
|
for( int jj = graphic->GetBezierPoints().size()-1; jj >= 0; jj-- )
|
2018-07-09 17:15:36 +00:00
|
|
|
{
|
2023-10-12 16:27:30 +00:00
|
|
|
const VECTOR2I& pt = graphic->GetBezierPoints()[jj];
|
2023-02-28 15:34:07 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
if( prevPt == pt )
|
|
|
|
continue;
|
2023-02-28 15:34:07 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
currContour.Append( pt );
|
|
|
|
shapeOwners[ std::make_pair( prevPt, pt ) ] = graphic;
|
|
|
|
prevPt = pt;
|
2018-07-09 17:15:36 +00:00
|
|
|
}
|
2023-10-12 16:27:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( const VECTOR2I& pt : graphic->GetBezierPoints() )
|
2018-07-09 17:15:36 +00:00
|
|
|
{
|
2023-10-12 16:27:30 +00:00
|
|
|
if( prevPt == pt )
|
|
|
|
continue;
|
2018-07-09 17:15:36 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
currContour.Append( pt );
|
|
|
|
shapeOwners[ std::make_pair( prevPt, pt ) ] = graphic;
|
|
|
|
prevPt = pt;
|
|
|
|
}
|
2017-03-19 15:38:30 +00:00
|
|
|
}
|
2023-10-12 16:27:30 +00:00
|
|
|
|
|
|
|
prevPt = nextPt;
|
|
|
|
}
|
|
|
|
break;
|
2017-03-19 15:38:30 +00:00
|
|
|
|
|
|
|
default:
|
2021-07-17 19:56:18 +00:00
|
|
|
UNIMPLEMENTED_FOR( graphic->SHAPE_T_asString() );
|
2017-03-20 12:05:38 +00:00
|
|
|
return false;
|
2017-03-19 15:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get next closest segment.
|
2022-10-14 19:30:56 +00:00
|
|
|
PCB_SHAPE* nextGraphic = findNext( graphic, prevPt, aShapeList, aChainingEpsilon );
|
2017-03-19 15:38:30 +00:00
|
|
|
|
2020-11-21 20:42:27 +00:00
|
|
|
if( nextGraphic && !( nextGraphic->GetFlags() & SKIP_STRUCT ) )
|
2020-10-22 20:27:46 +00:00
|
|
|
{
|
2022-10-14 19:30:56 +00:00
|
|
|
prevGraphic = graphic;
|
2020-11-21 20:42:27 +00:00
|
|
|
graphic = nextGraphic;
|
2020-10-22 20:27:46 +00:00
|
|
|
graphic->SetFlags( SKIP_STRUCT );
|
2023-12-09 16:24:47 +00:00
|
|
|
aCleaner.insert( graphic );
|
2020-10-22 20:27:46 +00:00
|
|
|
startCandidates.erase( graphic );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finished, or ran into trouble...
|
2020-12-24 21:00:41 +00:00
|
|
|
if( close_enough( startPt, prevPt, aChainingEpsilon ) )
|
2017-03-19 15:38:30 +00:00
|
|
|
{
|
2022-12-01 14:55:51 +00:00
|
|
|
currContour.SetClosed( true );
|
2020-10-22 20:27:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-11-21 20:42:27 +00:00
|
|
|
else if( nextGraphic ) // encountered already-used segment, but not at the start
|
2020-10-22 20:27:46 +00:00
|
|
|
{
|
2020-12-26 23:00:41 +00:00
|
|
|
if( aErrorHandler )
|
2023-10-12 16:27:30 +00:00
|
|
|
(*aErrorHandler)( _( "(self-intersecting)" ), graphic, nextGraphic,
|
|
|
|
prevPt );
|
2020-12-26 23:00:41 +00:00
|
|
|
|
2020-10-22 20:27:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-11-21 20:42:27 +00:00
|
|
|
else // encountered discontinuity
|
2020-10-22 20:27:46 +00:00
|
|
|
{
|
2020-11-21 20:42:27 +00:00
|
|
|
if( aErrorHandler )
|
|
|
|
(*aErrorHandler)( _( "(not a closed shape)" ), graphic, nullptr, prevPt );
|
2018-11-28 12:11:43 +00:00
|
|
|
|
2017-03-19 15:38:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 19:30:56 +00:00
|
|
|
for( const SHAPE_LINE_CHAIN& contour : contours )
|
|
|
|
{
|
|
|
|
if( !contour.IsClosed() )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, collect the parents of each contour
|
|
|
|
std::map<int, std::vector<int>> contourToParentIndexesMap;
|
|
|
|
|
|
|
|
for( size_t ii = 0; ii < contours.size(); ++ii )
|
|
|
|
{
|
|
|
|
VECTOR2I firstPt = contours[ii].GetPoint( 0 );
|
|
|
|
std::vector<int> parents;
|
|
|
|
|
|
|
|
for( size_t jj = 0; jj < contours.size(); ++jj )
|
|
|
|
{
|
|
|
|
if( jj == ii )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const SHAPE_LINE_CHAIN& parentCandidate = contours[jj];
|
|
|
|
|
|
|
|
if( parentCandidate.PointInside( firstPt ) )
|
|
|
|
parents.push_back( jj );
|
|
|
|
}
|
|
|
|
|
2023-12-18 18:45:02 +00:00
|
|
|
contourToParentIndexesMap[ii] = std::move( parents );
|
2022-10-14 19:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next add those that are top-level outlines to the SHAPE_POLY_SET
|
|
|
|
std::map<int, int> contourToOutlineIdxMap;
|
|
|
|
|
|
|
|
for( const auto& [ contourIndex, parentIndexes ] : contourToParentIndexesMap )
|
|
|
|
{
|
|
|
|
if( parentIndexes.size() %2 == 0 )
|
|
|
|
{
|
|
|
|
// Even number of parents; top-level outline
|
|
|
|
if( !aAllowDisjoint && !aPolygons.IsEmpty() )
|
|
|
|
{
|
|
|
|
if( aErrorHandler )
|
|
|
|
{
|
|
|
|
BOARD_ITEM* a = fetchOwner( aPolygons.Outline( 0 ).GetSegment( 0 ) );
|
|
|
|
BOARD_ITEM* b = fetchOwner( contours[ contourIndex ].GetSegment( 0 ) );
|
|
|
|
|
|
|
|
if( a && b )
|
|
|
|
{
|
|
|
|
(*aErrorHandler)( _( "(multiple board outlines not supported)" ), a, b,
|
|
|
|
contours[ contourIndex ].GetPoint( 0 ) );
|
2023-08-18 09:30:03 +00:00
|
|
|
|
|
|
|
return false;
|
2022-10-14 19:30:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aPolygons.AddOutline( contours[ contourIndex ] );
|
|
|
|
contourToOutlineIdxMap[ contourIndex ] = aPolygons.OutlineCount() - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// And finally add the holes
|
|
|
|
for( const auto& [ contourIndex, parentIndexes ] : contourToParentIndexesMap )
|
|
|
|
{
|
|
|
|
if( parentIndexes.size() %2 == 1 )
|
|
|
|
{
|
|
|
|
// Odd number of parents; we're a hole in the parent which has one fewer parents
|
|
|
|
// than we have.
|
|
|
|
const SHAPE_LINE_CHAIN& hole = contours[ contourIndex ];
|
|
|
|
|
|
|
|
for( int parentContourIdx : parentIndexes )
|
|
|
|
{
|
|
|
|
if( contourToParentIndexesMap[ parentContourIdx ].size() == parentIndexes.size() - 1 )
|
|
|
|
{
|
|
|
|
int outlineIdx = contourToOutlineIdxMap[ parentContourIdx ];
|
|
|
|
aPolygons.AddHole( hole, outlineIdx );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-22 20:27:46 +00:00
|
|
|
|
2020-12-25 14:01:34 +00:00
|
|
|
// All of the silliness that follows is to work around the segment iterator while checking
|
|
|
|
// for collisions.
|
2019-03-01 13:36:53 +00:00
|
|
|
// TODO: Implement proper segment and point iterators that follow std
|
|
|
|
for( auto seg1 = aPolygons.IterateSegmentsWithHoles(); seg1; seg1++ )
|
|
|
|
{
|
|
|
|
auto seg2 = seg1;
|
|
|
|
|
|
|
|
for( ++seg2; seg2; seg2++ )
|
|
|
|
{
|
2020-11-21 20:42:27 +00:00
|
|
|
// Check for exact overlapping segments.
|
|
|
|
if( *seg1 == *seg2 || ( ( *seg1 ).A == ( *seg2 ).B && ( *seg1 ).B == ( *seg2 ).A ) )
|
2019-03-01 13:36:53 +00:00
|
|
|
{
|
2020-11-21 20:42:27 +00:00
|
|
|
if( aErrorHandler )
|
|
|
|
{
|
|
|
|
BOARD_ITEM* a = fetchOwner( *seg1 );
|
|
|
|
BOARD_ITEM* b = fetchOwner( *seg2 );
|
2023-08-18 09:30:03 +00:00
|
|
|
(*aErrorHandler)( _( "(self-intersecting)" ), a, b, ( *seg1 ).A );
|
2020-11-21 20:42:27 +00:00
|
|
|
}
|
2019-03-01 13:36:53 +00:00
|
|
|
|
2020-10-22 20:27:46 +00:00
|
|
|
selfIntersecting = true;
|
2019-03-01 13:36:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 22:50:47 +00:00
|
|
|
if( OPT_VECTOR2I pt = seg1.Get().Intersect( seg2.Get(), true ) )
|
2019-03-01 13:36:53 +00:00
|
|
|
{
|
2020-11-21 20:42:27 +00:00
|
|
|
if( aErrorHandler )
|
|
|
|
{
|
|
|
|
BOARD_ITEM* a = fetchOwner( *seg1 );
|
|
|
|
BOARD_ITEM* b = fetchOwner( *seg2 );
|
2023-08-18 09:30:03 +00:00
|
|
|
(*aErrorHandler)( _( "(self-intersecting)" ), a, b, *pt );
|
2020-11-21 20:42:27 +00:00
|
|
|
}
|
2019-03-01 13:36:53 +00:00
|
|
|
|
2020-10-22 20:27:46 +00:00
|
|
|
selfIntersecting = true;
|
2019-03-01 13:36:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:27:46 +00:00
|
|
|
return !selfIntersecting;
|
2017-03-20 12:05:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 20:42:27 +00:00
|
|
|
|
2023-12-09 16:24:47 +00:00
|
|
|
bool ConvertOutlineToPolygon( std::vector<PCB_SHAPE*>& aShapeList, SHAPE_POLY_SET& aPolygons,
|
|
|
|
int aErrorMax, int aChainingEpsilon, bool aAllowDisjoint,
|
|
|
|
OUTLINE_ERROR_HANDLER* aErrorHandler, bool aAllowUseArcsInPolygons )
|
|
|
|
{
|
|
|
|
SCOPED_FLAGS_CLEANER cleaner( SKIP_STRUCT );
|
|
|
|
|
|
|
|
return doConvertOutlineToPolygon( aShapeList, aPolygons, aErrorMax, aChainingEpsilon,
|
|
|
|
aAllowDisjoint, aErrorHandler, aAllowUseArcsInPolygons,
|
|
|
|
cleaner );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-12 12:03:58 +00:00
|
|
|
bool TestBoardOutlinesGraphicItems( BOARD* aBoard, int aMinDist,
|
|
|
|
OUTLINE_ERROR_HANDLER* aErrorHandler )
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
PCB_TYPE_COLLECTOR items;
|
|
|
|
int min_dist = std::max( 0, aMinDist );
|
|
|
|
|
|
|
|
// Get all the shapes into 'items', then keep only those on layer == Edge_Cuts.
|
|
|
|
items.Collect( aBoard, { PCB_SHAPE_T } );
|
|
|
|
|
|
|
|
std::vector<PCB_SHAPE*> segList;
|
|
|
|
|
|
|
|
for( int ii = 0; ii < items.GetCount(); ii++ )
|
|
|
|
{
|
|
|
|
PCB_SHAPE* seg = static_cast<PCB_SHAPE*>( items[ii] );
|
|
|
|
|
|
|
|
if( seg->GetLayer() == Edge_Cuts )
|
|
|
|
segList.push_back( seg );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( FOOTPRINT* fp : aBoard->Footprints() )
|
|
|
|
{
|
|
|
|
PCB_TYPE_COLLECTOR fpItems;
|
|
|
|
fpItems.Collect( fp, { PCB_SHAPE_T } );
|
|
|
|
|
|
|
|
for( int ii = 0; ii < fpItems.GetCount(); ii++ )
|
|
|
|
{
|
|
|
|
PCB_SHAPE* fpSeg = static_cast<PCB_SHAPE*>( fpItems[ii] );
|
|
|
|
|
|
|
|
if( fpSeg->GetLayer() == Edge_Cuts )
|
|
|
|
segList.push_back( fpSeg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
// Now Test validity of collected items
|
2023-10-12 12:03:58 +00:00
|
|
|
for( PCB_SHAPE* graphic : segList )
|
|
|
|
{
|
|
|
|
switch( graphic->GetShape() )
|
|
|
|
{
|
|
|
|
case SHAPE_T::RECTANGLE:
|
|
|
|
{
|
|
|
|
VECTOR2I seg = graphic->GetEnd() - graphic->GetStart();
|
|
|
|
int dim = seg.EuclideanNorm();
|
|
|
|
|
|
|
|
if( dim <= min_dist )
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
if( aErrorHandler )
|
|
|
|
{
|
2023-10-12 16:27:30 +00:00
|
|
|
(*aErrorHandler)( wxString::Format( _( "(Rectangle has null or very small "
|
|
|
|
"size: %d nm)" ),
|
|
|
|
dim ),
|
2023-10-12 12:03:58 +00:00
|
|
|
graphic, nullptr, graphic->GetStart() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SHAPE_T::CIRCLE:
|
|
|
|
{
|
|
|
|
if( graphic->GetRadius() <= min_dist )
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
if( aErrorHandler )
|
|
|
|
{
|
2023-10-12 16:27:30 +00:00
|
|
|
(*aErrorHandler)( wxString::Format( _( "(Circle has null or very small "
|
|
|
|
"radius: %d nm)" ),
|
|
|
|
(int)graphic->GetRadius() ),
|
2023-10-12 12:03:58 +00:00
|
|
|
graphic, nullptr, graphic->GetStart() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SHAPE_T::SEGMENT:
|
|
|
|
{
|
|
|
|
VECTOR2I seg = graphic->GetEnd() - graphic->GetStart();
|
|
|
|
int dim = seg.EuclideanNorm();
|
|
|
|
|
|
|
|
if( dim <= min_dist )
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
if( aErrorHandler )
|
|
|
|
{
|
2023-10-12 16:27:30 +00:00
|
|
|
(*aErrorHandler)( wxString::Format( _( "(Segment has null or very small "
|
|
|
|
"length: %d nm)" ), dim ),
|
2023-10-12 12:03:58 +00:00
|
|
|
graphic, nullptr, graphic->GetStart() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SHAPE_T::ARC:
|
2023-10-12 17:59:11 +00:00
|
|
|
{
|
|
|
|
// Arc size can be evaluated from the distance between arc middle point and arc ends
|
|
|
|
// We do not need a precise value, just an idea of its size
|
|
|
|
VECTOR2I arcMiddle = graphic->GetArcMid();
|
|
|
|
VECTOR2I seg1 = arcMiddle - graphic->GetStart();
|
|
|
|
VECTOR2I seg2 = graphic->GetEnd() - arcMiddle;
|
|
|
|
int dim = seg1.EuclideanNorm() + seg2.EuclideanNorm();
|
|
|
|
|
|
|
|
if( dim <= min_dist )
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
if( aErrorHandler )
|
|
|
|
{
|
|
|
|
(*aErrorHandler)( wxString::Format( _( "(Arc has null or very small size: %d nm)" ), dim ),
|
|
|
|
graphic, nullptr, graphic->GetStart() );
|
|
|
|
}
|
|
|
|
}
|
2023-10-12 12:03:58 +00:00
|
|
|
break;
|
2023-10-12 17:59:11 +00:00
|
|
|
}
|
2023-10-12 12:03:58 +00:00
|
|
|
|
2023-10-12 13:53:05 +00:00
|
|
|
case SHAPE_T::POLY:
|
|
|
|
break;
|
|
|
|
|
2023-10-12 12:03:58 +00:00
|
|
|
case SHAPE_T::BEZIER:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_FOR( graphic->SHAPE_T_asString() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:05:38 +00:00
|
|
|
|
2020-12-24 21:00:41 +00:00
|
|
|
bool BuildBoardPolygonOutlines( BOARD* aBoard, SHAPE_POLY_SET& aOutlines, int aErrorMax,
|
2023-05-16 11:39:16 +00:00
|
|
|
int aChainingEpsilon, OUTLINE_ERROR_HANDLER* aErrorHandler,
|
|
|
|
bool aAllowUseArcsInPolygons )
|
2017-03-20 12:05:38 +00:00
|
|
|
{
|
2023-12-09 16:24:47 +00:00
|
|
|
PCB_TYPE_COLLECTOR items;
|
|
|
|
SHAPE_POLY_SET fpHoles;
|
|
|
|
bool success = false;
|
2017-03-20 12:05:38 +00:00
|
|
|
|
2023-12-09 16:24:47 +00:00
|
|
|
SCOPED_FLAGS_CLEANER cleaner( SKIP_STRUCT );
|
2022-12-02 16:33:00 +00:00
|
|
|
|
2023-03-30 11:49:23 +00:00
|
|
|
// Get all the shapes into 'items', then keep only those on layer == Edge_Cuts.
|
|
|
|
items.Collect( aBoard, { PCB_SHAPE_T } );
|
2017-03-20 12:05:38 +00:00
|
|
|
|
2022-12-02 16:33:00 +00:00
|
|
|
for( int ii = 0; ii < items.GetCount(); ++ii )
|
|
|
|
items[ii]->ClearFlags( SKIP_STRUCT );
|
|
|
|
|
|
|
|
for( FOOTPRINT* fp : aBoard->Footprints() )
|
|
|
|
{
|
|
|
|
PCB_TYPE_COLLECTOR fpItems;
|
2023-03-30 11:49:23 +00:00
|
|
|
fpItems.Collect( fp, { PCB_SHAPE_T } );
|
2022-12-02 16:33:00 +00:00
|
|
|
|
|
|
|
std::vector<PCB_SHAPE*> fpSegList;
|
|
|
|
|
|
|
|
for( int ii = 0; ii < fpItems.GetCount(); ii++ )
|
|
|
|
{
|
|
|
|
PCB_SHAPE* fpSeg = static_cast<PCB_SHAPE*>( fpItems[ii] );
|
|
|
|
|
|
|
|
if( fpSeg->GetLayer() == Edge_Cuts )
|
|
|
|
fpSegList.push_back( fpSeg );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !fpSegList.empty() )
|
|
|
|
{
|
|
|
|
SHAPE_POLY_SET fpOutlines;
|
2023-12-09 16:24:47 +00:00
|
|
|
success = doConvertOutlineToPolygon( fpSegList, fpOutlines, aErrorMax, aChainingEpsilon,
|
|
|
|
false,
|
|
|
|
// don't report errors here; the second pass also
|
|
|
|
// gets an opportunity to use these segments
|
|
|
|
nullptr, aAllowUseArcsInPolygons, cleaner );
|
2022-12-02 16:33:00 +00:00
|
|
|
|
2023-10-12 16:27:30 +00:00
|
|
|
// Test to see if we should make holes or outlines. Holes are made if the footprint
|
|
|
|
// has copper outside of a single, closed outline. If there are multiple outlines,
|
|
|
|
// we assume that the footprint edges represent holes as we do not support multiple
|
|
|
|
// boards. Similarly, if any of the footprint pads are located outside of the edges,
|
|
|
|
// then the edges are holes
|
2023-04-20 21:54:00 +00:00
|
|
|
if( success && ( isCopperOutside( fp, fpOutlines ) || fpOutlines.OutlineCount() > 1 ) )
|
2022-12-02 16:33:00 +00:00
|
|
|
{
|
|
|
|
fpHoles.Append( fpOutlines );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If it wasn't a closed area, or wasn't a hole, the we want to keep the fpSegs
|
|
|
|
// in contention for the board outline builds.
|
|
|
|
for( int ii = 0; ii < fpItems.GetCount(); ++ii )
|
|
|
|
fpItems[ii]->ClearFlags( SKIP_STRUCT );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:05:38 +00:00
|
|
|
// Make a working copy of aSegList, because the list is modified during calculations
|
2020-10-04 23:34:59 +00:00
|
|
|
std::vector<PCB_SHAPE*> segList;
|
2017-03-20 12:05:38 +00:00
|
|
|
|
|
|
|
for( int ii = 0; ii < items.GetCount(); ii++ )
|
|
|
|
{
|
2022-12-02 16:33:00 +00:00
|
|
|
PCB_SHAPE* seg = static_cast<PCB_SHAPE*>( items[ii] );
|
|
|
|
|
|
|
|
// Skip anything already used to generate footprint holes (above)
|
|
|
|
if( seg->GetFlags() & SKIP_STRUCT )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( seg->GetLayer() == Edge_Cuts )
|
|
|
|
segList.push_back( seg );
|
2017-03-20 12:05:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 20:09:36 +00:00
|
|
|
if( segList.size() )
|
|
|
|
{
|
2023-12-09 16:24:47 +00:00
|
|
|
success = doConvertOutlineToPolygon( segList, aOutlines, aErrorMax, aChainingEpsilon, true,
|
|
|
|
aErrorHandler, aAllowUseArcsInPolygons, cleaner );
|
2020-08-17 20:07:27 +00:00
|
|
|
}
|
2017-03-20 12:05:38 +00:00
|
|
|
|
2017-03-22 10:16:45 +00:00
|
|
|
if( !success || !aOutlines.OutlineCount() )
|
2017-03-20 12:05:38 +00:00
|
|
|
{
|
2020-05-18 20:09:36 +00:00
|
|
|
// Couldn't create a valid polygon outline. Use the board edge cuts bounding box to
|
|
|
|
// create a rectangular outline, or, failing that, the bounding box of the items on
|
|
|
|
// the board.
|
2022-08-30 23:28:18 +00:00
|
|
|
BOX2I bbbox = aBoard->GetBoardEdgesBoundingBox();
|
2017-03-20 12:05:38 +00:00
|
|
|
|
2017-03-22 07:40:48 +00:00
|
|
|
// If null area, uses the global bounding box.
|
|
|
|
if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
|
|
|
|
bbbox = aBoard->ComputeBoundingBox();
|
|
|
|
|
2017-03-20 12:05:38 +00:00
|
|
|
// Ensure non null area. If happen, gives a minimal size.
|
|
|
|
if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
|
2022-09-16 11:33:56 +00:00
|
|
|
bbbox.Inflate( pcbIUScale.mmToIU( 1.0 ) );
|
2017-03-20 12:05:38 +00:00
|
|
|
|
|
|
|
aOutlines.RemoveAllContours();
|
|
|
|
aOutlines.NewOutline();
|
|
|
|
|
2023-02-19 03:40:07 +00:00
|
|
|
VECTOR2I corner;
|
2017-03-22 07:40:48 +00:00
|
|
|
aOutlines.Append( bbbox.GetOrigin() );
|
2017-03-20 12:05:38 +00:00
|
|
|
|
|
|
|
corner.x = bbbox.GetOrigin().x;
|
|
|
|
corner.y = bbbox.GetEnd().y;
|
2017-03-22 07:40:48 +00:00
|
|
|
aOutlines.Append( corner );
|
2017-03-20 12:05:38 +00:00
|
|
|
|
2017-03-22 07:40:48 +00:00
|
|
|
aOutlines.Append( bbbox.GetEnd() );
|
2017-03-20 12:05:38 +00:00
|
|
|
|
|
|
|
corner.x = bbbox.GetEnd().x;
|
|
|
|
corner.y = bbbox.GetOrigin().y;
|
2017-03-22 07:40:48 +00:00
|
|
|
aOutlines.Append( corner );
|
2017-03-20 12:05:38 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 16:33:00 +00:00
|
|
|
for( int ii = 0; ii < fpHoles.OutlineCount(); ++ii )
|
|
|
|
{
|
2022-12-27 14:34:08 +00:00
|
|
|
const VECTOR2I holePt = fpHoles.Outline( ii ).CPoint( 0 );
|
|
|
|
|
2022-12-02 16:33:00 +00:00
|
|
|
for( int jj = 0; jj < aOutlines.OutlineCount(); ++jj )
|
|
|
|
{
|
2022-12-27 14:34:08 +00:00
|
|
|
if( aOutlines.Outline( jj ).PointInside( holePt ) )
|
|
|
|
{
|
2022-12-02 16:33:00 +00:00
|
|
|
aOutlines.AddHole( fpHoles.Outline( ii ), jj );
|
2022-12-27 14:34:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-12-02 16:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:05:38 +00:00
|
|
|
return success;
|
|
|
|
}
|
2020-02-27 17:51:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-30 22:12:13 +00:00
|
|
|
* Get the complete bounding box of the board (including all items).
|
|
|
|
*
|
|
|
|
* The vertex numbers and segment numbers of the rectangle returned.
|
|
|
|
* 1
|
|
|
|
* *---------------*
|
|
|
|
* |1 2|
|
|
|
|
* 0| |2
|
|
|
|
* |0 3|
|
|
|
|
* *---------------*
|
|
|
|
* 3
|
2020-02-27 17:51:11 +00:00
|
|
|
*/
|
|
|
|
void buildBoardBoundingBoxPoly( const BOARD* aBoard, SHAPE_POLY_SET& aOutline )
|
|
|
|
{
|
2022-08-30 23:28:18 +00:00
|
|
|
BOX2I bbbox = aBoard->GetBoundingBox();
|
2020-09-30 22:12:13 +00:00
|
|
|
SHAPE_LINE_CHAIN chain;
|
2020-02-27 17:51:11 +00:00
|
|
|
|
|
|
|
// If null area, uses the global bounding box.
|
|
|
|
if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
|
|
|
|
bbbox = aBoard->ComputeBoundingBox();
|
|
|
|
|
|
|
|
// Ensure non null area. If happen, gives a minimal size.
|
|
|
|
if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
|
2022-09-16 11:33:56 +00:00
|
|
|
bbbox.Inflate( pcbIUScale.mmToIU( 1.0 ) );
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
// Inflate slightly (by 1/10th the size of the box)
|
|
|
|
bbbox.Inflate( bbbox.GetWidth() / 10, bbbox.GetHeight() / 10 );
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
chain.Append( bbbox.GetOrigin() );
|
|
|
|
chain.Append( bbbox.GetOrigin().x, bbbox.GetEnd().y );
|
|
|
|
chain.Append( bbbox.GetEnd() );
|
|
|
|
chain.Append( bbbox.GetEnd().x, bbbox.GetOrigin().y );
|
|
|
|
chain.SetClosed( true );
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
aOutline.RemoveAllContours();
|
|
|
|
aOutline.AddOutline( chain );
|
2020-02-27 17:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VECTOR2I projectPointOnSegment( const VECTOR2I& aEndPoint, const SHAPE_POLY_SET& aOutline,
|
|
|
|
int aOutlineNum = 0 )
|
|
|
|
{
|
|
|
|
int minDistance = -1;
|
|
|
|
VECTOR2I projPoint;
|
|
|
|
|
|
|
|
for( auto it = aOutline.CIterateSegments( aOutlineNum ); it; it++ )
|
|
|
|
{
|
|
|
|
auto seg = it.Get();
|
|
|
|
int dis = seg.Distance( aEndPoint );
|
|
|
|
|
|
|
|
if( minDistance < 0 || ( dis < minDistance ) )
|
|
|
|
{
|
|
|
|
minDistance = dis;
|
|
|
|
projPoint = seg.NearestPoint( aEndPoint );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return projPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
int findEndSegments( SHAPE_LINE_CHAIN& aChain, SEG& aStartSeg, SEG& aEndSeg )
|
|
|
|
{
|
|
|
|
int foundSegs = 0;
|
|
|
|
|
|
|
|
for( int i = 0; i < aChain.SegmentCount(); i++ )
|
|
|
|
{
|
|
|
|
SEG seg = aChain.Segment( i );
|
|
|
|
|
|
|
|
bool foundA = false;
|
|
|
|
bool foundB = false;
|
|
|
|
|
|
|
|
for( int j = 0; j < aChain.SegmentCount(); j++ )
|
|
|
|
{
|
|
|
|
// Don't test the segment against itself
|
|
|
|
if( i == j )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SEG testSeg = aChain.Segment( j );
|
|
|
|
|
|
|
|
if( testSeg.Contains( seg.A ) )
|
|
|
|
foundA = true;
|
|
|
|
|
|
|
|
if( testSeg.Contains( seg.B ) )
|
|
|
|
foundB = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This segment isn't a start or end
|
|
|
|
if( foundA && foundB )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( foundSegs == 0 )
|
|
|
|
{
|
|
|
|
// The first segment we encounter is the "start" segment
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Found start segment: (%d, %d)-(%d, %d)" ),
|
2020-09-30 22:12:13 +00:00
|
|
|
seg.A.x, seg.A.y, seg.B.x, seg.B.y );
|
|
|
|
aStartSeg = seg;
|
|
|
|
foundSegs++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Once we find both start and end, we can stop
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Found end segment: (%d, %d)-(%d, %d)" ),
|
2020-09-30 22:12:13 +00:00
|
|
|
seg.A.x, seg.A.y, seg.B.x, seg.B.y );
|
|
|
|
aEndSeg = seg;
|
|
|
|
foundSegs++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return foundSegs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-24 21:00:41 +00:00
|
|
|
bool BuildFootprintPolygonOutlines( BOARD* aBoard, SHAPE_POLY_SET& aOutlines, int aErrorMax,
|
|
|
|
int aChainingEpsilon, OUTLINE_ERROR_HANDLER* aErrorHandler )
|
2020-11-21 20:42:27 +00:00
|
|
|
|
2020-02-27 17:51:11 +00:00
|
|
|
{
|
2021-02-10 12:35:09 +00:00
|
|
|
FOOTPRINT* footprint = aBoard->GetFirstFootprint();
|
|
|
|
|
|
|
|
// No footprint loaded
|
|
|
|
if( !footprint )
|
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "No footprint found on board" ) );
|
2021-02-10 12:35:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-09 16:24:47 +00:00
|
|
|
PCB_TYPE_COLLECTOR items;
|
|
|
|
SHAPE_POLY_SET outlines;
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
SCOPED_FLAGS_CLEANER cleaner( SKIP_STRUCT );
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-11-08 21:29:04 +00:00
|
|
|
// Get all the SHAPEs into 'items', then keep only those on layer == Edge_Cuts.
|
2023-03-30 11:49:23 +00:00
|
|
|
items.Collect( aBoard, { PCB_SHAPE_T } );
|
2020-02-27 17:51:11 +00:00
|
|
|
|
|
|
|
// Make a working copy of aSegList, because the list is modified during calculations
|
2020-10-04 23:34:59 +00:00
|
|
|
std::vector<PCB_SHAPE*> segList;
|
2020-02-27 17:51:11 +00:00
|
|
|
|
|
|
|
for( int ii = 0; ii < items.GetCount(); ii++ )
|
|
|
|
{
|
|
|
|
if( items[ii]->GetLayer() == Edge_Cuts )
|
2020-10-04 23:34:59 +00:00
|
|
|
segList.push_back( static_cast<PCB_SHAPE*>( items[ii] ) );
|
2020-02-27 17:51:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 12:35:09 +00:00
|
|
|
if( !segList.empty() )
|
2020-09-30 22:12:13 +00:00
|
|
|
{
|
2023-12-09 16:24:47 +00:00
|
|
|
success = doConvertOutlineToPolygon( segList, outlines, aErrorMax, aChainingEpsilon, true,
|
|
|
|
aErrorHandler, false, cleaner );
|
2020-09-30 22:12:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 07:45:17 +00:00
|
|
|
// A closed outline was found on Edge_Cuts
|
2021-02-10 12:35:09 +00:00
|
|
|
if( success )
|
2020-02-27 17:51:11 +00:00
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Closed outline found" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
|
2020-02-27 17:51:11 +00:00
|
|
|
// If copper is outside a closed polygon, treat it as a hole
|
2023-04-20 21:54:00 +00:00
|
|
|
// If there are multiple outlines in the footprint, they are also holes
|
|
|
|
if( isCopperOutside( footprint, outlines ) || outlines.OutlineCount() > 1 )
|
2020-02-27 17:51:11 +00:00
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Treating outline as a hole" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
|
2020-02-27 17:51:11 +00:00
|
|
|
buildBoardBoundingBoxPoly( aBoard, aOutlines );
|
|
|
|
|
|
|
|
// Copy all outlines from the conversion as holes into the new outline
|
|
|
|
for( int i = 0; i < outlines.OutlineCount(); i++ )
|
|
|
|
{
|
2020-09-30 22:12:13 +00:00
|
|
|
SHAPE_LINE_CHAIN& out = outlines.Outline( i );
|
|
|
|
|
|
|
|
if( out.IsClosed() )
|
|
|
|
aOutlines.AddHole( out, -1 );
|
2020-02-27 17:51:11 +00:00
|
|
|
|
|
|
|
for( int j = 0; j < outlines.HoleCount( i ); j++ )
|
|
|
|
{
|
2020-09-30 22:12:13 +00:00
|
|
|
SHAPE_LINE_CHAIN& hole = outlines.Hole( i, j );
|
|
|
|
|
|
|
|
if( hole.IsClosed() )
|
|
|
|
aOutlines.AddHole( hole, -1 );
|
2020-02-27 17:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If all copper is inside, then the computed outline is the board outline
|
|
|
|
else
|
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Treating outline as board edge" ) );
|
2020-02-27 17:51:11 +00:00
|
|
|
aOutlines = outlines;
|
|
|
|
}
|
2020-09-30 22:12:13 +00:00
|
|
|
|
|
|
|
return true;
|
2020-02-27 17:51:11 +00:00
|
|
|
}
|
|
|
|
// No board outlines were found, so use the bounding box
|
2020-09-30 22:12:13 +00:00
|
|
|
else if( outlines.OutlineCount() == 0 )
|
2020-02-27 17:51:11 +00:00
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Using footprint bounding box" ) );
|
2020-02-27 17:51:11 +00:00
|
|
|
buildBoardBoundingBoxPoly( aBoard, aOutlines );
|
2020-09-30 22:12:13 +00:00
|
|
|
|
|
|
|
return true;
|
2020-02-27 17:51:11 +00:00
|
|
|
}
|
|
|
|
// There is an outline present, but it is not closed
|
|
|
|
else
|
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Trying to build outline" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
|
|
|
|
std::vector<SHAPE_LINE_CHAIN> closedChains;
|
|
|
|
std::vector<SHAPE_LINE_CHAIN> openChains;
|
|
|
|
|
2020-12-25 14:01:34 +00:00
|
|
|
// The ConvertOutlineToPolygon function returns only one main outline and the rest as
|
|
|
|
// holes, so we promote the holes and process them
|
2020-09-30 22:12:13 +00:00
|
|
|
openChains.push_back( outlines.Outline( 0 ) );
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
for( int j = 0; j < outlines.HoleCount( 0 ); j++ )
|
2020-02-27 17:51:11 +00:00
|
|
|
{
|
2020-09-30 22:12:13 +00:00
|
|
|
SHAPE_LINE_CHAIN hole = outlines.Hole( 0, j );
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
if( hole.IsClosed() )
|
2020-02-27 17:51:11 +00:00
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Found closed hole" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
closedChains.push_back( hole );
|
2020-02-27 17:51:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Found open hole" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
openChains.push_back( hole );
|
2020-02-27 17:51:11 +00:00
|
|
|
}
|
2020-09-30 22:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SHAPE_POLY_SET bbox;
|
|
|
|
buildBoardBoundingBoxPoly( aBoard, bbox );
|
|
|
|
|
|
|
|
// Treat the open polys as the board edge
|
|
|
|
SHAPE_LINE_CHAIN chain = openChains[0];
|
|
|
|
SHAPE_LINE_CHAIN rect = bbox.Outline( 0 );
|
|
|
|
|
|
|
|
// We know the outline chain is open, so set to non-closed to get better segment count
|
|
|
|
chain.SetClosed( false );
|
|
|
|
|
|
|
|
SEG startSeg;
|
|
|
|
SEG endSeg;
|
|
|
|
|
|
|
|
// The two possible board outlines
|
|
|
|
SHAPE_LINE_CHAIN upper;
|
|
|
|
SHAPE_LINE_CHAIN lower;
|
|
|
|
|
|
|
|
findEndSegments( chain, startSeg, endSeg );
|
|
|
|
|
|
|
|
if( chain.SegmentCount() == 0 )
|
|
|
|
{
|
2020-11-13 02:57:11 +00:00
|
|
|
// Something is wrong, bail out with the overall footprint bounding box
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "No line segments in provided outline" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
aOutlines = bbox;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if( chain.SegmentCount() == 1 )
|
|
|
|
{
|
2020-12-25 14:01:34 +00:00
|
|
|
// This case means there is only 1 line segment making up the edge cuts of the
|
|
|
|
// footprint, so we just need to use it to cut the bounding box in half.
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Only 1 line segment in provided outline" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
|
|
|
|
startSeg = chain.Segment( 0 );
|
|
|
|
|
|
|
|
// Intersect with all the sides of the rectangle
|
|
|
|
OPT_VECTOR2I inter0 = startSeg.IntersectLines( rect.Segment( 0 ) );
|
|
|
|
OPT_VECTOR2I inter1 = startSeg.IntersectLines( rect.Segment( 1 ) );
|
|
|
|
OPT_VECTOR2I inter2 = startSeg.IntersectLines( rect.Segment( 2 ) );
|
|
|
|
OPT_VECTOR2I inter3 = startSeg.IntersectLines( rect.Segment( 3 ) );
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
if( inter0 && inter2 && !inter1 && !inter3 )
|
2020-02-27 17:51:11 +00:00
|
|
|
{
|
2020-09-30 22:12:13 +00:00
|
|
|
// Intersects the vertical rectangle sides only
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Segment intersects only vertical bbox "
|
|
|
|
"sides" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
|
|
|
|
// The upper half
|
|
|
|
upper.Append( *inter0 );
|
|
|
|
upper.Append( rect.GetPoint( 1 ) );
|
|
|
|
upper.Append( rect.GetPoint( 2 ) );
|
|
|
|
upper.Append( *inter2 );
|
|
|
|
upper.SetClosed( true );
|
|
|
|
|
|
|
|
// The lower half
|
|
|
|
lower.Append( *inter0 );
|
|
|
|
lower.Append( rect.GetPoint( 0 ) );
|
|
|
|
lower.Append( rect.GetPoint( 3 ) );
|
|
|
|
lower.Append( *inter2 );
|
|
|
|
lower.SetClosed( true );
|
|
|
|
}
|
|
|
|
else if( inter1 && inter3 && !inter0 && !inter2 )
|
|
|
|
{
|
|
|
|
// Intersects the horizontal rectangle sides only
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Segment intersects only horizontal bbox "
|
|
|
|
"sides" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
|
|
|
|
// The left half
|
|
|
|
upper.Append( *inter1 );
|
|
|
|
upper.Append( rect.GetPoint( 1 ) );
|
|
|
|
upper.Append( rect.GetPoint( 0 ) );
|
|
|
|
upper.Append( *inter3 );
|
|
|
|
upper.SetClosed( true );
|
|
|
|
|
|
|
|
// The right half
|
|
|
|
lower.Append( *inter1 );
|
|
|
|
lower.Append( rect.GetPoint( 2 ) );
|
|
|
|
lower.Append( rect.GetPoint( 3 ) );
|
|
|
|
lower.Append( *inter3 );
|
|
|
|
lower.SetClosed( true );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Angled line segment that cuts across a corner
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Segment intersects two perpendicular bbox "
|
|
|
|
"sides" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
|
2020-12-25 14:01:34 +00:00
|
|
|
// Figure out which actual lines are intersected, since IntersectLines assumes
|
|
|
|
// an infinite line
|
2020-09-30 22:12:13 +00:00
|
|
|
bool hit0 = rect.Segment( 0 ).Contains( *inter0 );
|
|
|
|
bool hit1 = rect.Segment( 1 ).Contains( *inter1 );
|
|
|
|
bool hit2 = rect.Segment( 2 ).Contains( *inter2 );
|
|
|
|
bool hit3 = rect.Segment( 3 ).Contains( *inter3 );
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
if( hit0 && hit1 )
|
2020-02-27 17:51:11 +00:00
|
|
|
{
|
2020-09-30 22:12:13 +00:00
|
|
|
// Cut across the upper left corner
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Segment cuts upper left corner" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
|
|
|
|
// The upper half
|
|
|
|
upper.Append( *inter0 );
|
|
|
|
upper.Append( rect.GetPoint( 1 ) );
|
|
|
|
upper.Append( *inter1 );
|
|
|
|
upper.SetClosed( true );
|
|
|
|
|
|
|
|
// The lower half
|
|
|
|
lower.Append( *inter0 );
|
|
|
|
lower.Append( rect.GetPoint( 0 ) );
|
|
|
|
lower.Append( rect.GetPoint( 3 ) );
|
|
|
|
lower.Append( rect.GetPoint( 2 ) );
|
|
|
|
lower.Append( *inter1 );
|
|
|
|
lower.SetClosed( true );
|
|
|
|
}
|
|
|
|
else if( hit1 && hit2 )
|
|
|
|
{
|
|
|
|
// Cut across the upper right corner
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Segment cuts upper right corner" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
|
|
|
|
// The upper half
|
|
|
|
upper.Append( *inter1 );
|
|
|
|
upper.Append( rect.GetPoint( 2 ) );
|
|
|
|
upper.Append( *inter2 );
|
|
|
|
upper.SetClosed( true );
|
|
|
|
|
|
|
|
// The lower half
|
|
|
|
lower.Append( *inter1 );
|
|
|
|
lower.Append( rect.GetPoint( 1 ) );
|
|
|
|
lower.Append( rect.GetPoint( 0 ) );
|
|
|
|
lower.Append( rect.GetPoint( 3 ) );
|
|
|
|
lower.Append( *inter2 );
|
|
|
|
lower.SetClosed( true );
|
|
|
|
}
|
|
|
|
else if( hit2 && hit3 )
|
|
|
|
{
|
|
|
|
// Cut across the lower right corner
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Segment cuts lower right corner" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
|
|
|
|
// The upper half
|
|
|
|
upper.Append( *inter2 );
|
|
|
|
upper.Append( rect.GetPoint( 2 ) );
|
|
|
|
upper.Append( rect.GetPoint( 1 ) );
|
|
|
|
upper.Append( rect.GetPoint( 0 ) );
|
|
|
|
upper.Append( *inter3 );
|
|
|
|
upper.SetClosed( true );
|
|
|
|
|
|
|
|
// The bottom half
|
|
|
|
lower.Append( *inter2 );
|
|
|
|
lower.Append( rect.GetPoint( 3 ) );
|
|
|
|
lower.Append( *inter3 );
|
|
|
|
lower.SetClosed( true );
|
2020-02-27 17:51:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-30 22:12:13 +00:00
|
|
|
// Cut across the lower left corner
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Segment cuts upper left corner" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
|
|
|
|
// The upper half
|
|
|
|
upper.Append( *inter0 );
|
|
|
|
upper.Append( rect.GetPoint( 1 ) );
|
|
|
|
upper.Append( rect.GetPoint( 2 ) );
|
|
|
|
upper.Append( rect.GetPoint( 3 ) );
|
|
|
|
upper.Append( *inter3 );
|
|
|
|
upper.SetClosed( true );
|
|
|
|
|
|
|
|
// The bottom half
|
|
|
|
lower.Append( *inter0 );
|
|
|
|
lower.Append( rect.GetPoint( 0 ) );
|
|
|
|
lower.Append( *inter3 );
|
|
|
|
lower.SetClosed( true );
|
2020-02-27 17:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-30 22:12:13 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// More than 1 segment
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Multiple segments in outline" ) );
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
// Just a temporary thing
|
|
|
|
aOutlines = bbox;
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
// Figure out which is the correct outline
|
|
|
|
SHAPE_POLY_SET poly1;
|
|
|
|
SHAPE_POLY_SET poly2;
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
poly1.NewOutline();
|
|
|
|
poly1.Append( upper );
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
poly2.NewOutline();
|
|
|
|
poly2.Append( lower );
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-11-08 21:29:04 +00:00
|
|
|
if( isCopperOutside( footprint, poly1 ) )
|
2020-09-30 22:12:13 +00:00
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Using lower shape" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
aOutlines = poly2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Using upper shape" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
aOutlines = poly1;
|
2020-02-27 17:51:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
// Add all closed polys as holes to the main outline
|
|
|
|
for( SHAPE_LINE_CHAIN& closedChain : closedChains )
|
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( traceBoardOutline, wxT( "Adding hole to main outline" ) );
|
2020-09-30 22:12:13 +00:00
|
|
|
aOutlines.AddHole( closedChain, -1 );
|
|
|
|
}
|
2020-02-27 17:51:11 +00:00
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
return true;
|
2020-02-27 17:51:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 22:12:13 +00:00
|
|
|
// We really shouldn't reach this point
|
|
|
|
return false;
|
2020-02-27 17:51:11 +00:00
|
|
|
}
|