kicad/pcbnew/fp_shape.cpp

434 lines
12 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
2022-01-13 17:27:36 +00:00
* Copyright (C) 1992-2022 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 <core/wx_stl_compat.h>
#include <bitmaps.h>
#include <core/mirror.h>
#include <macros.h>
#include <math/util.h> // for KiROUND
#include <settings/color_settings.h>
#include <settings/settings_manager.h>
2018-01-29 20:58:58 +00:00
#include <pcb_edit_frame.h>
#include <footprint.h>
#include <fp_shape.h>
#include <view/view.h>
2022-01-30 10:52:52 +00:00
FP_SHAPE::FP_SHAPE( FOOTPRINT* parent, SHAPE_T aShape, KICAD_T aItemType ) :
PCB_SHAPE( parent, aItemType, aShape )
{
2020-11-14 14:29:11 +00:00
m_layer = F_SilkS;
}
FP_SHAPE::~FP_SHAPE()
{
}
void FP_SHAPE::SetLocalCoord()
{
2020-11-14 18:11:28 +00:00
FOOTPRINT* fp = static_cast<FOOTPRINT*>( m_parent );
2020-10-14 23:37:26 +00:00
if( fp == NULL )
{
m_start0 = m_start;
m_end0 = m_end;
m_arcCenter0 = m_arcCenter;
m_bezierC1_0 = m_bezierC1;
m_bezierC2_0 = m_bezierC2;
return;
}
m_start0 = m_start - fp->GetPosition();
m_end0 = m_end - fp->GetPosition();
m_arcCenter0 = m_arcCenter - fp->GetPosition();
m_bezierC1_0 = m_bezierC1 - fp->GetPosition();
m_bezierC2_0 = m_bezierC2 - fp->GetPosition();
2022-01-13 17:27:36 +00:00
RotatePoint( &m_start0.x, &m_start0.y, - fp->GetOrientation() );
RotatePoint( &m_end0.x, &m_end0.y, - fp->GetOrientation() );
RotatePoint( &m_arcCenter0.x, &m_arcCenter0.y, - fp->GetOrientation() );
RotatePoint( &m_bezierC1_0.x, &m_bezierC1_0.y, - fp->GetOrientation() );
RotatePoint( &m_bezierC2_0.x, &m_bezierC2_0.y, - fp->GetOrientation() );
}
void FP_SHAPE::SetDrawCoord()
{
2020-11-14 18:11:28 +00:00
FOOTPRINT* fp = static_cast<FOOTPRINT*>( m_parent );
m_start = m_start0;
m_end = m_end0;
m_arcCenter = m_arcCenter0;
m_bezierC1 = m_bezierC1_0;
m_bezierC2 = m_bezierC2_0;
2020-10-14 23:37:26 +00:00
if( fp )
{
2020-11-13 21:00:28 +00:00
RotatePoint( &m_start.x, &m_start.y, fp->GetOrientation() );
RotatePoint( &m_end.x, &m_end.y, fp->GetOrientation() );
RotatePoint( &m_arcCenter.x, &m_arcCenter.y, fp->GetOrientation() );
2020-11-13 21:00:28 +00:00
RotatePoint( &m_bezierC1.x, &m_bezierC1.y, fp->GetOrientation() );
RotatePoint( &m_bezierC2.x, &m_bezierC2.y, fp->GetOrientation() );
m_start += fp->GetPosition();
m_end += fp->GetPosition();
m_arcCenter += fp->GetPosition();
m_bezierC1 += fp->GetPosition();
m_bezierC2 += fp->GetPosition();
}
RebuildBezierToSegmentsPointsList( GetWidth() );
}
void FP_SHAPE::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
{
if( aFrame->GetName() == PCB_EDIT_FRAME_NAME )
{
FOOTPRINT* fp = static_cast<FOOTPRINT*>( m_parent );
2009-11-20 19:51:39 +00:00
if( fp )
aList.emplace_back( _( "Footprint" ), fp->GetReference() );
}
// append the features shared with the base class
PCB_SHAPE::GetMsgPanelInfo( aFrame, aList );
}
wxString FP_SHAPE::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
{
return wxString::Format( _( "%s on %s" ),
ShowShape(),
GetLayerName() );
}
BITMAPS FP_SHAPE::GetMenuImage() const
{
return BITMAPS::show_mod_edge;
}
EDA_ITEM* FP_SHAPE::Clone() const
{
return new FP_SHAPE( *this );
}
VECTOR2I FP_SHAPE::GetCenter0() const
{
switch( m_shape )
{
case SHAPE_T::ARC:
return m_arcCenter0;
case SHAPE_T::CIRCLE:
return m_start0;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
return VECTOR2I();
}
}
void FP_SHAPE::SetCenter0( const VECTOR2I& aCenter )
{
switch( m_shape )
{
case SHAPE_T::ARC:
m_arcCenter0 = aCenter;
break;
case SHAPE_T::CIRCLE:
m_start0 = aCenter;
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
}
}
VECTOR2I FP_SHAPE::GetArcMid0() const
{
// If none of the input data have changed since we loaded the arc,
// keep the original mid point data to minimize churn
if( m_arcMidData_0.start == m_start && m_arcMidData_0.end == m_end
&& m_arcMidData_0.center == m_arcCenter )
return m_arcMidData_0.mid;
VECTOR2I mid0 = m_start0;
RotatePoint( mid0, m_arcCenter0, -GetArcAngle() / 2.0 );
return mid0;
}
2022-01-16 16:15:07 +00:00
void FP_SHAPE::SetArcAngleAndEnd0( const EDA_ANGLE& aAngle, bool aCheckNegativeAngle )
{
2022-01-16 16:15:07 +00:00
EDA_ANGLE angle( aAngle );
m_end0 = m_start0;
2022-01-16 16:15:07 +00:00
RotatePoint( m_end0, m_arcCenter0, -angle.Normalize720() );
2022-01-16 16:15:07 +00:00
if( aCheckNegativeAngle && aAngle < ANGLE_0 )
std::swap( m_start0, m_end0 );
}
2022-01-01 18:57:44 +00:00
void FP_SHAPE::SetArcGeometry0( const VECTOR2I& aStart0, const VECTOR2I& aMid0,
const VECTOR2I& aEnd0 )
{
m_start0 = aStart0;
m_end0 = aEnd0;
m_arcCenter0 = CalcArcCenter( aStart0, aMid0, aEnd0 );
m_arcMidData_0.center = m_arcCenter0;
m_arcMidData_0.end = m_end0;
m_arcMidData_0.mid = aMid0;
m_arcMidData_0.start = m_start0;
}
void FP_SHAPE::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
{
VECTOR2I pt( 0, 0 );
switch( GetShape() )
{
case SHAPE_T::ARC:
case SHAPE_T::SEGMENT:
case SHAPE_T::RECT:
case SHAPE_T::CIRCLE:
case SHAPE_T::BEZIER:
2020-11-16 00:45:43 +00:00
// If Start0 and Start are equal (ie: Footprint Editor), then flip both sets around the
// centre point.
if( m_start == m_start0 )
pt = aCentre;
if( aFlipLeftRight )
{
2020-11-13 21:00:28 +00:00
MIRROR( m_start.x, aCentre.x );
MIRROR( m_end.x, aCentre.x );
MIRROR( m_arcCenter.x, aCentre.x );
2020-11-13 21:00:28 +00:00
MIRROR( m_bezierC1.x, aCentre.x );
MIRROR( m_bezierC2.x, aCentre.x );
MIRROR( m_start0.x, pt.x );
MIRROR( m_end0.x, pt.x );
MIRROR( m_arcCenter0.x, pt.x );
MIRROR( m_bezierC1_0.x, pt.x );
MIRROR( m_bezierC2_0.x, pt.x );
}
else
{
2020-11-13 21:00:28 +00:00
MIRROR( m_start.y, aCentre.y );
MIRROR( m_end.y, aCentre.y );
MIRROR( m_arcCenter.y, aCentre.y );
2020-11-13 21:00:28 +00:00
MIRROR( m_bezierC1.y, aCentre.y );
MIRROR( m_bezierC2.y, aCentre.y );
MIRROR( m_start0.y, pt.y );
MIRROR( m_end0.y, pt.y );
MIRROR( m_arcCenter0.y, pt.y );
MIRROR( m_bezierC1_0.y, pt.y );
MIRROR( m_bezierC2_0.y, pt.y );
}
if( GetShape() == SHAPE_T::BEZIER )
RebuildBezierToSegmentsPointsList( GetWidth() );
if( GetShape() == SHAPE_T::ARC )
{
std::swap( m_start, m_end );
std::swap( m_start0, m_end0 );
}
break;
case SHAPE_T::POLY:
2020-06-27 18:01:00 +00:00
// polygon corners coordinates are relative to the footprint position, orientation 0
2020-11-13 21:00:28 +00:00
m_poly.Mirror( aFlipLeftRight, !aFlipLeftRight );
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
}
SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
}
bool FP_SHAPE::IsParentFlipped() const
{
if( GetParent() && GetParent()->GetLayer() == B_Cu )
return true;
return false;
}
void FP_SHAPE::Mirror( const VECTOR2I& aCentre, bool aMirrorAroundXAxis )
{
// Mirror an edge of the footprint. the layer is not modified
// This is a footprint shape modification.
switch( GetShape() )
{
case SHAPE_T::ARC:
case SHAPE_T::SEGMENT:
case SHAPE_T::RECT:
case SHAPE_T::CIRCLE:
case SHAPE_T::BEZIER:
if( aMirrorAroundXAxis )
{
MIRROR( m_start0.y, aCentre.y );
MIRROR( m_end0.y, aCentre.y );
MIRROR( m_arcCenter0.y, aCentre.y );
MIRROR( m_bezierC1_0.y, aCentre.y );
MIRROR( m_bezierC2_0.y, aCentre.y );
}
else
{
MIRROR( m_start0.x, aCentre.x );
MIRROR( m_end0.x, aCentre.x );
MIRROR( m_arcCenter0.x, aCentre.x );
MIRROR( m_bezierC1_0.x, aCentre.x );
MIRROR( m_bezierC2_0.x, aCentre.x );
}
if( GetShape() == SHAPE_T::ARC )
{
std::swap( m_start, m_end );
std::swap( m_start0, m_end0 );
}
if( GetShape() == SHAPE_T::BEZIER )
RebuildBezierToSegmentsPointsList( GetWidth() );
break;
case SHAPE_T::POLY:
m_poly.Mirror( !aMirrorAroundXAxis, aMirrorAroundXAxis, aCentre );
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
}
SetDrawCoord();
}
void FP_SHAPE::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
{
2020-06-27 18:01:00 +00:00
// We should rotate the relative coordinates, but to avoid duplicate code do the base class
2020-10-14 23:37:26 +00:00
// rotation of draw coordinates, which is acceptable because in the footprint editor
// m_Pos0 = m_Pos
PCB_SHAPE::Rotate( aRotCentre, aAngle );
2020-06-27 18:01:00 +00:00
// and now update the relative coordinates, which are the reference in most transforms.
SetLocalCoord();
}
void FP_SHAPE::Move( const VECTOR2I& aMoveVector )
{
// Move an edge of the footprint.
// This is a footprint shape modification.
switch( GetShape() )
{
case SHAPE_T::ARC:
case SHAPE_T::SEGMENT:
case SHAPE_T::RECT:
case SHAPE_T::CIRCLE:
case SHAPE_T::BEZIER:
m_start0 += aMoveVector;
m_end0 += aMoveVector;
m_arcCenter0 += aMoveVector;
m_bezierC1_0 += aMoveVector;
m_bezierC2_0 += aMoveVector;
break;
case SHAPE_T::POLY:
// polygon corners coordinates are always relative to the
// footprint position, orientation 0
2020-11-13 21:00:28 +00:00
m_poly.Move( VECTOR2I( aMoveVector ) );
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
}
SetDrawCoord();
}
double FP_SHAPE::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
{
constexpr double HIDE = std::numeric_limits<double>::max();
if( !aView )
return 0;
// Handle Render tab switches
if( !IsParentFlipped() && !aView->IsLayerVisible( LAYER_MOD_FR ) )
return HIDE;
if( IsParentFlipped() && !aView->IsLayerVisible( LAYER_MOD_BK ) )
return HIDE;
// Other layers are shown without any conditions
return 0.0;
}
wxString FP_SHAPE::GetParentAsString() const
{
if( FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( m_parent ) )
return fp->GetReference();
return m_parent->m_Uuid.AsString();
}
2020-10-14 23:37:26 +00:00
static struct FP_SHAPE_DESC
{
2020-10-14 23:37:26 +00:00
FP_SHAPE_DESC()
{
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
REGISTER_TYPE( FP_SHAPE );
propMgr.AddTypeCast( new TYPE_CAST<FP_SHAPE, BOARD_ITEM> );
propMgr.AddTypeCast( new TYPE_CAST<FP_SHAPE, EDA_SHAPE> );
propMgr.AddTypeCast( new TYPE_CAST<FP_SHAPE, PCB_SHAPE> );
propMgr.InheritsAfter( TYPE_HASH( FP_SHAPE ), TYPE_HASH( BOARD_ITEM ) );
propMgr.InheritsAfter( TYPE_HASH( FP_SHAPE ), TYPE_HASH( EDA_SHAPE ) );
propMgr.InheritsAfter( TYPE_HASH( FP_SHAPE ), TYPE_HASH( PCB_SHAPE ) );
propMgr.AddProperty( new PROPERTY<FP_SHAPE, wxString>( _HKI( "Parent" ),
NO_SETTER( FP_SHAPE, wxString ), &FP_SHAPE::GetParentAsString ) );
}
2020-10-14 23:37:26 +00:00
} _FP_SHAPE_DESC;