kicad/pcbnew/pad_custom_shape_functions.cpp

313 lines
8.9 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
*
* 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
*/
#include <board.h>
#include <board_design_settings.h>
2020-11-14 18:11:28 +00:00
#include <board_item.h>
#include <pcb_shape.h>
#include <pad.h>
#include <convert_basic_shapes_to_polygon.h>
#include <geometry/shape_rect.h>
/*
* 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
*/
2020-11-14 01:16:02 +00:00
void PAD::AddPrimitivePoly( const SHAPE_POLY_SET& aPoly, int aThickness, bool aFilled )
{
std::vector<wxPoint> points;
// 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++ )
points.emplace_back( iter->x, iter->y );
2020-11-14 01:16:02 +00:00
AddPrimitivePoly( points, aThickness, aFilled );
}
2020-11-14 01:16:02 +00:00
void PAD::AddPrimitivePoly( const std::vector<wxPoint>& aPoly, int aThickness, bool aFilled )
{
PCB_SHAPE* item = new PCB_SHAPE();
2021-05-01 02:33:25 +00:00
item->SetShape( PCB_SHAPE_TYPE::POLYGON );
2020-11-14 01:16:02 +00:00
item->SetFilled( aFilled );
item->SetPolyPoints( aPoly );
item->SetWidth( aThickness );
item->SetParent( this );
m_editPrimitives.emplace_back( item );
2021-01-08 00:26:32 +00:00
SetDirty();
}
2020-11-12 22:30:02 +00:00
void PAD::AddPrimitiveSegment( const wxPoint& aStart, const wxPoint& aEnd, int aThickness )
{
PCB_SHAPE* item = new PCB_SHAPE();
2021-05-01 02:33:25 +00:00
item->SetShape( PCB_SHAPE_TYPE::SEGMENT );
2020-11-14 01:16:02 +00:00
item->SetFilled( false );
item->SetStart( aStart );
item->SetEnd( aEnd );
item->SetWidth( aThickness );
item->SetParent( this );
m_editPrimitives.emplace_back( item );
2021-01-08 00:26:32 +00:00
SetDirty();
}
2020-11-12 22:30:02 +00:00
void PAD::AddPrimitiveArc( const wxPoint& aCenter, const wxPoint& aStart, int aArcAngle,
int aThickness )
{
PCB_SHAPE* item = new PCB_SHAPE();
2021-05-01 02:33:25 +00:00
item->SetShape( PCB_SHAPE_TYPE::ARC );
2020-11-14 01:16:02 +00:00
item->SetFilled( false );
item->SetCenter( aCenter );
item->SetArcStart( aStart );
item->SetAngle( aArcAngle );
item->SetWidth( aThickness );
item->SetParent( this );
m_editPrimitives.emplace_back( item );
2021-01-08 00:26:32 +00:00
SetDirty();
}
2020-11-12 22:30:02 +00:00
void PAD::AddPrimitiveCurve( const wxPoint& aStart, const wxPoint& aEnd, const wxPoint& aCtrl1,
const wxPoint& aCtrl2, int aThickness )
{
PCB_SHAPE* item = new PCB_SHAPE();
2021-05-01 02:33:25 +00:00
item->SetShape( PCB_SHAPE_TYPE::CURVE );
2020-11-14 01:16:02 +00:00
item->SetFilled( false );
item->SetStart( aStart );
item->SetEnd( aEnd );
item->SetBezControl1( aCtrl1 );
item->SetBezControl2( aCtrl2 );
item->SetWidth( aThickness );
item->SetParent( this );
m_editPrimitives.emplace_back( item );
2021-01-08 00:26:32 +00:00
SetDirty();
}
2020-11-14 01:16:02 +00:00
void PAD::AddPrimitiveCircle( const wxPoint& aCenter, int aRadius, int aThickness, bool aFilled )
{
PCB_SHAPE* item = new PCB_SHAPE();
2021-05-01 02:33:25 +00:00
item->SetShape( PCB_SHAPE_TYPE::CIRCLE );
2020-11-14 01:16:02 +00:00
item->SetFilled( aFilled );
item->SetStart( aCenter );
item->SetEnd( wxPoint( aCenter.x + aRadius, aCenter.y ) );
item->SetWidth( aThickness );
item->SetParent( this );
m_editPrimitives.emplace_back( item );
2021-01-08 00:26:32 +00:00
SetDirty();
}
2020-11-14 01:16:02 +00:00
void PAD::AddPrimitiveRect( const wxPoint& aStart, const wxPoint& aEnd, int aThickness,
bool aFilled)
{
PCB_SHAPE* item = new PCB_SHAPE();
2021-05-01 02:33:25 +00:00
item->SetShape( PCB_SHAPE_TYPE::RECT );
2020-11-14 01:16:02 +00:00
item->SetFilled( aFilled );
item->SetStart( aStart );
item->SetEnd( aEnd );
item->SetWidth( aThickness );
item->SetParent( this );
m_editPrimitives.emplace_back( item );
2021-01-08 00:26:32 +00:00
SetDirty();
}
2020-11-12 22:30:02 +00:00
void PAD::ReplacePrimitives( const std::vector<std::shared_ptr<PCB_SHAPE>>& aPrimitivesList )
{
// clear old list
DeletePrimitivesList();
// Import to the given shape list
if( aPrimitivesList.size() )
AppendPrimitives( aPrimitivesList );
2021-01-08 00:26:32 +00:00
SetDirty();
}
2020-11-12 22:30:02 +00:00
void PAD::AppendPrimitives( const std::vector<std::shared_ptr<PCB_SHAPE>>& aPrimitivesList )
{
// Add duplicates of aPrimitivesList to the pad primitives list:
for( const std::shared_ptr<PCB_SHAPE>& prim : aPrimitivesList )
AddPrimitive( new PCB_SHAPE( *prim ) );
2021-01-08 00:26:32 +00:00
SetDirty();
}
2020-11-12 22:30:02 +00:00
void PAD::AddPrimitive( PCB_SHAPE* aPrimitive )
{
aPrimitive->SetParent( this );
m_editPrimitives.emplace_back( aPrimitive );
2021-01-08 00:26:32 +00:00
SetDirty();
}
// clear the basic shapes list and associated data
2020-11-12 22:30:02 +00:00
void PAD::DeletePrimitivesList()
{
m_editPrimitives.clear();
2021-01-08 00:26:32 +00:00
SetDirty();
}
2020-11-12 22:30:02 +00:00
void PAD::addPadPrimitivesToPolygon( SHAPE_POLY_SET* aMergedPolygon, PCB_LAYER_ID aLayer,
int aError, ERROR_LOC aErrorLoc ) const
{
SHAPE_POLY_SET polyset;
for( const std::shared_ptr<PCB_SHAPE>& primitive : m_editPrimitives )
primitive->TransformShapeWithClearanceToPolygon( polyset, aLayer, 0, aError, aErrorLoc );
polyset.Simplify( SHAPE_POLY_SET::PM_FAST );
// Merge all polygons with the initial pad anchor shape
if( polyset.OutlineCount() )
{
aMergedPolygon->BooleanAdd( polyset, SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
aMergedPolygon->Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
}
}
void PAD::MergePrimitivesAsPolygon( SHAPE_POLY_SET* aMergedPolygon, PCB_LAYER_ID aLayer,
ERROR_LOC aErrorLoc ) const
{
BOARD* board = GetBoard();
int maxError = board ? board->GetDesignSettings().m_MaxError: ARC_HIGH_DEF;
aMergedPolygon->RemoveAllContours();
// Add the anchor pad shape in aMergedPolygon, others in aux_polyset:
// The anchor pad is always at 0,0
switch( GetAnchorPadShape() )
{
2021-05-01 12:22:35 +00:00
case PAD_SHAPE::RECT:
{
SHAPE_RECT rect( -GetSize().x / 2, -GetSize().y / 2, GetSize().x, GetSize().y );
aMergedPolygon->AddOutline( rect.Outline() );
}
break;
default:
2021-05-01 12:22:35 +00:00
case PAD_SHAPE::CIRCLE:
TransformCircleToPolygon( *aMergedPolygon, wxPoint( 0, 0 ), GetSize().x / 2, maxError,
ERROR_INSIDE );
break;
}
addPadPrimitivesToPolygon( aMergedPolygon, aLayer, maxError, aErrorLoc );
}
2020-11-12 22:30:02 +00:00
bool PAD::GetBestAnchorPosition( VECTOR2I& aPos )
{
SHAPE_POLY_SET poly;
addPadPrimitivesToPolygon( &poly, UNDEFINED_LAYER, ARC_LOW_DEF, ERROR_INSIDE );
if( poly.OutlineCount() > 1 )
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 ) );
VECTOR2I center = bbox.Centre();
int64_t minDist = std::numeric_limits<int64_t>::max();
int64_t minDistEdge;
2021-05-01 12:22:35 +00:00
if( GetAnchorPadShape() == PAD_SHAPE::CIRCLE )
{
minDistEdge = GetSize().x;
}
else
{
minDistEdge = std::max( GetSize().x, GetSize().y );
}
OPT<VECTOR2I> bestAnchor( []()->OPT<VECTOR2I> { return NULLOPT; }() );
for( int y = 0; y < stepsY ; y++ )
2017-10-20 11:48:36 +00:00
{
for( int x = 0; x < stepsX; x++ )
{
VECTOR2I p = bbox.GetPosition();
p.x += rescale( x, bbox.GetWidth(), (stepsX - 1) );
p.y += rescale( y, bbox.GetHeight(), (stepsY - 1) );
if( poly.Contains(p) )
{
int dist = (center - p).EuclideanNorm();
int distEdge = poly.COutline(0).Distance( p, true );
if( distEdge >= minDistEdge )
{
if( dist < minDist )
{
bestAnchor = p;
minDist = dist;
}
}
}
}
2017-10-20 11:48:36 +00:00
}
if( bestAnchor )
2017-10-20 11:48:36 +00:00
{
aPos = *bestAnchor;
return true;
}
2017-10-20 11:48:36 +00:00
return false;
}