2017-01-13 17:51:22 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2018-08-28 15:48:13 +00:00
|
|
|
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2019-05-22 13:33:48 +00:00
|
|
|
* Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-01-13 17:51:22 +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 class_pad_custom_shape_functions.cpp
|
2020-01-10 14:31:00 +00:00
|
|
|
* D_PAD functions specific to custom shaped pads.
|
2017-01-13 17:51:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fctsys.h>
|
|
|
|
#include <trigo.h>
|
|
|
|
#include <pcbnew.h>
|
2019-05-22 14:47:38 +00:00
|
|
|
#include <class_board.h>
|
2019-07-11 08:48:11 +00:00
|
|
|
#include <class_board_item.h>
|
2017-01-13 17:51:22 +00:00
|
|
|
#include <class_drawsegment.h>
|
2019-05-22 14:47:38 +00:00
|
|
|
#include <class_pad.h>
|
2017-01-13 17:51:22 +00:00
|
|
|
#include <convert_basic_shapes_to_polygon.h>
|
2019-05-22 14:47:38 +00:00
|
|
|
#include <geometry/shape_rect.h>
|
2017-01-13 17:51:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Has meaning only for free shape pads.
|
|
|
|
* add a free shape to the shape list.
|
|
|
|
* the shape is a polygon (can be with thick outline), segment, circle or arc
|
|
|
|
*/
|
2017-10-19 21:16:06 +00:00
|
|
|
|
2020-06-22 19:35:09 +00:00
|
|
|
void D_PAD::AddPrimitivePoly( const SHAPE_POLY_SET& aPoly, int aThickness )
|
2017-10-19 21:16:06 +00:00
|
|
|
{
|
|
|
|
std::vector<wxPoint> points;
|
|
|
|
|
2018-08-28 15:48:13 +00:00
|
|
|
// If aPoly has holes, convert it to a polygon with no holes.
|
|
|
|
SHAPE_POLY_SET poly_no_hole;
|
|
|
|
poly_no_hole.Append( aPoly );
|
|
|
|
poly_no_hole.Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
|
|
|
|
|
|
|
|
for( auto iter = poly_no_hole.CIterate(); iter; iter++ )
|
2019-12-05 15:41:21 +00:00
|
|
|
points.emplace_back( iter->x, iter->y );
|
2017-10-19 21:16:06 +00:00
|
|
|
|
2020-06-22 19:35:09 +00:00
|
|
|
AddPrimitivePoly( points, aThickness );
|
2017-10-19 21:16:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 20:46:43 +00:00
|
|
|
|
2020-06-22 19:35:09 +00:00
|
|
|
void D_PAD::AddPrimitivePoly( const std::vector<wxPoint>& aPoly, int aThickness )
|
2017-01-13 17:51:22 +00:00
|
|
|
{
|
2020-06-24 11:12:39 +00:00
|
|
|
DRAWSEGMENT* item = new DRAWSEGMENT();
|
|
|
|
item->SetShape( S_POLYGON );
|
|
|
|
item->SetPolyPoints( aPoly );
|
|
|
|
item->SetWidth( aThickness );
|
|
|
|
m_editPrimitives.emplace_back( item );
|
2020-06-22 19:35:09 +00:00
|
|
|
m_shapesDirty = true;
|
2017-01-13 17:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-22 19:35:09 +00:00
|
|
|
void D_PAD::AddPrimitiveSegment( const wxPoint& aStart, const wxPoint& aEnd, int aThickness )
|
2017-01-13 17:51:22 +00:00
|
|
|
{
|
2020-06-24 11:12:39 +00:00
|
|
|
DRAWSEGMENT* item = new DRAWSEGMENT();
|
|
|
|
item->SetStart( aStart );
|
|
|
|
item->SetEnd( aEnd );
|
|
|
|
item->SetWidth( aThickness );
|
|
|
|
m_editPrimitives.emplace_back( item );
|
2020-06-22 19:35:09 +00:00
|
|
|
m_shapesDirty = true;
|
2017-01-13 17:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-19 15:09:25 +00:00
|
|
|
void D_PAD::AddPrimitiveArc( const wxPoint& aCenter, const wxPoint& aStart, int aArcAngle,
|
2020-06-22 19:35:09 +00:00
|
|
|
int aThickness )
|
2017-01-13 17:51:22 +00:00
|
|
|
{
|
2020-06-24 11:12:39 +00:00
|
|
|
DRAWSEGMENT* item = new DRAWSEGMENT();
|
|
|
|
item->SetShape( S_ARC );
|
|
|
|
item->SetCenter( aCenter );
|
|
|
|
item->SetArcStart( aStart );
|
|
|
|
item->SetAngle( aArcAngle );
|
|
|
|
item->SetWidth( aThickness );
|
|
|
|
m_editPrimitives.emplace_back( item );
|
2020-06-22 19:35:09 +00:00
|
|
|
m_shapesDirty = true;
|
2017-01-13 17:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-19 15:09:25 +00:00
|
|
|
void D_PAD::AddPrimitiveCurve( const wxPoint& aStart, const wxPoint& aEnd, const wxPoint& aCtrl1,
|
2020-06-22 19:35:09 +00:00
|
|
|
const wxPoint& aCtrl2, int aThickness )
|
2019-04-14 17:58:35 +00:00
|
|
|
{
|
2020-06-24 11:12:39 +00:00
|
|
|
DRAWSEGMENT* item = new DRAWSEGMENT();
|
|
|
|
item->SetShape( S_CURVE );
|
|
|
|
item->SetStart( aStart );
|
|
|
|
item->SetEnd( aEnd );
|
|
|
|
item->SetBezControl1( aCtrl1 );
|
|
|
|
item->SetBezControl2( aCtrl2 );
|
|
|
|
item->SetWidth( aThickness );
|
|
|
|
m_editPrimitives.emplace_back( item );
|
2020-06-22 19:35:09 +00:00
|
|
|
m_shapesDirty = true;
|
2019-04-14 17:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-22 19:35:09 +00:00
|
|
|
void D_PAD::AddPrimitiveCircle( const wxPoint& aCenter, int aRadius, int aThickness )
|
2017-01-13 17:51:22 +00:00
|
|
|
{
|
2020-06-24 11:12:39 +00:00
|
|
|
DRAWSEGMENT* item = new DRAWSEGMENT();
|
|
|
|
item->SetShape( S_CIRCLE );
|
|
|
|
item->SetStart( aCenter );
|
|
|
|
item->SetEnd( wxPoint( aCenter.x + aRadius, aCenter.y ) );
|
|
|
|
item->SetWidth( aThickness );
|
|
|
|
m_editPrimitives.emplace_back( item );
|
2020-06-22 19:35:09 +00:00
|
|
|
m_shapesDirty = true;
|
2020-06-19 15:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-22 19:35:09 +00:00
|
|
|
void D_PAD::AddPrimitiveRect( const wxPoint& aStart, const wxPoint& aEnd, int aThickness )
|
2020-06-19 15:09:25 +00:00
|
|
|
{
|
2020-06-24 11:12:39 +00:00
|
|
|
DRAWSEGMENT* item = new DRAWSEGMENT();
|
|
|
|
item->SetShape( S_RECT );
|
|
|
|
item->SetStart( aStart );
|
|
|
|
item->SetEnd( aEnd );
|
|
|
|
item->SetWidth( aThickness );
|
|
|
|
m_editPrimitives.emplace_back( item );
|
2020-06-22 19:35:09 +00:00
|
|
|
m_shapesDirty = true;
|
2017-01-13 17:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-24 11:12:39 +00:00
|
|
|
void D_PAD::SetPrimitives( const std::vector<std::shared_ptr<DRAWSEGMENT>>& aPrimitivesList )
|
2017-01-13 17:51:22 +00:00
|
|
|
{
|
|
|
|
// clear old list
|
2020-06-24 11:12:39 +00:00
|
|
|
m_editPrimitives.clear();
|
2017-01-13 17:51:22 +00:00
|
|
|
|
|
|
|
// Import to the basic shape list
|
2017-09-20 08:28:52 +00:00
|
|
|
if( aPrimitivesList.size() )
|
2020-06-24 11:12:39 +00:00
|
|
|
m_editPrimitives = aPrimitivesList;
|
2017-01-13 17:51:22 +00:00
|
|
|
|
2020-06-22 19:35:09 +00:00
|
|
|
m_shapesDirty = true;
|
2017-01-13 17:51:22 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 20:46:43 +00:00
|
|
|
|
2020-06-24 11:12:39 +00:00
|
|
|
void D_PAD::AddPrimitives( const std::vector<std::shared_ptr<DRAWSEGMENT>>& aPrimitivesList )
|
2018-03-06 13:58:25 +00:00
|
|
|
{
|
2020-06-24 11:12:39 +00:00
|
|
|
for( const std::shared_ptr<DRAWSEGMENT>& prim : aPrimitivesList )
|
|
|
|
m_editPrimitives.push_back( prim );
|
2018-03-06 13:58:25 +00:00
|
|
|
|
2020-06-22 19:35:09 +00:00
|
|
|
m_shapesDirty = true;
|
2018-03-06 13:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-27 11:57:40 +00:00
|
|
|
void D_PAD::AddPrimitive( DRAWSEGMENT* aPrimitive )
|
|
|
|
{
|
|
|
|
m_editPrimitives.emplace_back( aPrimitive );
|
|
|
|
|
|
|
|
m_shapesDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-13 17:51:22 +00:00
|
|
|
// clear the basic shapes list and associated data
|
2017-09-20 08:28:52 +00:00
|
|
|
void D_PAD::DeletePrimitivesList()
|
2017-01-13 17:51:22 +00:00
|
|
|
{
|
2020-06-24 11:12:39 +00:00
|
|
|
m_editPrimitives.clear();
|
2020-06-22 19:35:09 +00:00
|
|
|
m_shapesDirty = true;
|
2017-01-13 17:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-24 11:12:39 +00:00
|
|
|
void D_PAD::addPadPrimitivesToPolygon( SHAPE_POLY_SET* aMergedPolygon, int aError ) const
|
2017-10-19 21:16:06 +00:00
|
|
|
{
|
2020-06-22 19:35:09 +00:00
|
|
|
SHAPE_POLY_SET polyset;
|
2017-01-13 17:51:22 +00:00
|
|
|
|
2020-06-24 11:12:39 +00:00
|
|
|
for( const std::shared_ptr<DRAWSEGMENT>& primitive : m_editPrimitives )
|
2020-07-02 16:06:09 +00:00
|
|
|
primitive->TransformShapeWithClearanceToPolygon( polyset, 0, aError );
|
2017-01-13 17:51:22 +00:00
|
|
|
|
2020-06-22 19:35:09 +00:00
|
|
|
polyset.Simplify( SHAPE_POLY_SET::PM_FAST );
|
2017-10-19 21:16:06 +00:00
|
|
|
|
2018-05-18 06:38:03 +00:00
|
|
|
// Merge all polygons with the initial pad anchor shape
|
2020-06-22 19:35:09 +00:00
|
|
|
if( polyset.OutlineCount() )
|
2017-01-13 17:51:22 +00:00
|
|
|
{
|
2020-06-22 19:35:09 +00:00
|
|
|
aMergedPolygon->BooleanAdd( polyset, SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
|
2018-05-18 06:38:03 +00:00
|
|
|
aMergedPolygon->Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
|
2017-01-13 17:51:22 +00:00
|
|
|
}
|
2017-10-19 21:16:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 19:35:09 +00:00
|
|
|
void D_PAD::MergePrimitivesAsPolygon( SHAPE_POLY_SET* aMergedPolygon ) const
|
2017-10-19 21:16:06 +00:00
|
|
|
{
|
2019-05-22 14:47:38 +00:00
|
|
|
auto board = GetBoard();
|
|
|
|
int maxError = ARC_HIGH_DEF;
|
|
|
|
|
|
|
|
if( board )
|
|
|
|
maxError = board->GetDesignSettings().m_MaxError;
|
|
|
|
|
2017-10-19 21:16:06 +00:00
|
|
|
aMergedPolygon->RemoveAllContours();
|
|
|
|
|
|
|
|
// Add the anchor pad shape in aMergedPolygon, others in aux_polyset:
|
|
|
|
// The anchor pad is always at 0,0
|
|
|
|
switch( GetAnchorPadShape() )
|
|
|
|
{
|
|
|
|
case PAD_SHAPE_RECT:
|
2019-05-14 12:39:34 +00:00
|
|
|
{
|
|
|
|
SHAPE_RECT rect( -GetSize().x / 2, -GetSize().y / 2, GetSize().x, GetSize().y );
|
2017-10-19 21:16:06 +00:00
|
|
|
aMergedPolygon->AddOutline( rect.Outline() );
|
2020-06-22 19:35:09 +00:00
|
|
|
}
|
|
|
|
break;
|
2019-05-14 12:39:34 +00:00
|
|
|
|
2020-06-22 19:35:09 +00:00
|
|
|
default:
|
|
|
|
case PAD_SHAPE_CIRCLE:
|
|
|
|
TransformCircleToPolygon( *aMergedPolygon, wxPoint( 0, 0 ), GetSize().x / 2, maxError );
|
2017-10-19 21:16:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-01-13 17:51:22 +00:00
|
|
|
|
2020-06-24 11:12:39 +00:00
|
|
|
addPadPrimitivesToPolygon( aMergedPolygon, maxError );
|
2017-01-13 17:51:22 +00:00
|
|
|
}
|
2017-10-19 21:16:06 +00:00
|
|
|
|
2020-06-19 13:07:50 +00:00
|
|
|
|
2017-10-19 21:16:06 +00:00
|
|
|
bool D_PAD::GetBestAnchorPosition( VECTOR2I& aPos )
|
|
|
|
{
|
|
|
|
SHAPE_POLY_SET poly;
|
2020-06-24 11:12:39 +00:00
|
|
|
addPadPrimitivesToPolygon( &poly, ARC_LOW_DEF );
|
2017-10-19 21:16:06 +00:00
|
|
|
|
2020-06-22 19:35:09 +00:00
|
|
|
if( poly.OutlineCount() > 1 )
|
2017-10-19 21:16:06 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const int minSteps = 10;
|
|
|
|
const int maxSteps = 50;
|
|
|
|
|
|
|
|
int stepsX, stepsY;
|
|
|
|
|
|
|
|
auto bbox = poly.BBox();
|
|
|
|
|
|
|
|
if( bbox.GetWidth() < bbox.GetHeight() )
|
|
|
|
{
|
|
|
|
stepsX = minSteps;
|
|
|
|
stepsY = minSteps * (double) bbox.GetHeight() / (double )(bbox.GetWidth() + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stepsY = minSteps;
|
|
|
|
stepsX = minSteps * (double) bbox.GetWidth() / (double )(bbox.GetHeight() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
stepsX = std::max(minSteps, std::min( maxSteps, stepsX ) );
|
|
|
|
stepsY = std::max(minSteps, std::min( maxSteps, stepsY ) );
|
|
|
|
|
2020-06-19 13:07:50 +00:00
|
|
|
VECTOR2I center = bbox.Centre();
|
2017-10-19 21:16:06 +00:00
|
|
|
|
2020-06-19 13:07:50 +00:00
|
|
|
int64_t minDist = std::numeric_limits<int64_t>::max();
|
2017-10-19 21:16:06 +00:00
|
|
|
int64_t minDistEdge;
|
|
|
|
|
|
|
|
if( GetAnchorPadShape() == PAD_SHAPE_CIRCLE )
|
|
|
|
{
|
|
|
|
minDistEdge = GetSize().x;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
minDistEdge = std::max( GetSize().x, GetSize().y );
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:58:25 +00:00
|
|
|
OPT<VECTOR2I> bestAnchor( []()->OPT<VECTOR2I> { return NULLOPT; }() );
|
2017-10-19 21:16:06 +00:00
|
|
|
|
2020-06-19 13:07:50 +00:00
|
|
|
for( int y = 0; y < stepsY ; y++ )
|
2017-10-20 11:48:36 +00:00
|
|
|
{
|
2020-06-19 13:07:50 +00:00
|
|
|
for( int x = 0; x < stepsX; x++ )
|
2017-10-19 21:16:06 +00:00
|
|
|
{
|
|
|
|
VECTOR2I p = bbox.GetPosition();
|
|
|
|
p.x += rescale( x, bbox.GetWidth(), (stepsX - 1) );
|
|
|
|
p.y += rescale( y, bbox.GetHeight(), (stepsY - 1) );
|
|
|
|
|
2020-06-19 13:07:50 +00:00
|
|
|
if( poly.Contains(p) )
|
2017-10-19 21:16:06 +00:00
|
|
|
{
|
|
|
|
|
2020-06-19 13:07:50 +00:00
|
|
|
int dist = (center - p).EuclideanNorm();
|
|
|
|
int distEdge = poly.COutline(0).Distance( p, true );
|
|
|
|
|
|
|
|
if( distEdge >= minDistEdge )
|
2017-10-19 21:16:06 +00:00
|
|
|
{
|
2020-06-19 13:07:50 +00:00
|
|
|
if( dist < minDist )
|
2017-10-19 21:16:06 +00:00
|
|
|
{
|
|
|
|
bestAnchor = p;
|
|
|
|
minDist = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 11:48:36 +00:00
|
|
|
}
|
2017-10-19 21:16:06 +00:00
|
|
|
|
2020-06-19 13:07:50 +00:00
|
|
|
if( bestAnchor )
|
2017-10-20 11:48:36 +00:00
|
|
|
{
|
|
|
|
aPos = *bestAnchor;
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-19 21:16:06 +00:00
|
|
|
|
2017-10-20 11:48:36 +00:00
|
|
|
return false;
|
2017-10-19 21:16:06 +00:00
|
|
|
}
|