2012-05-03 18:37:56 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-11-16 11:45:53 +00:00
|
|
|
* Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2020-02-13 13:39:52 +00:00
|
|
|
* Copyright (C) 2004-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2019-08-14 08:28:07 +00:00
|
|
|
* Copyright (C) 2019 CERN
|
2012-05-03 18:37:56 +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
|
|
|
|
*/
|
|
|
|
|
2018-08-03 12:18:26 +00:00
|
|
|
#include <sch_draw_panel.h>
|
2018-01-28 18:12:26 +00:00
|
|
|
#include <plotter.h>
|
2012-05-03 18:37:56 +00:00
|
|
|
#include <trigo.h>
|
|
|
|
#include <base_units.h>
|
2013-01-12 17:32:24 +00:00
|
|
|
#include <msgpanel.h>
|
2017-02-20 12:20:39 +00:00
|
|
|
#include <bitmaps.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/util.h> // for KiROUND
|
2020-04-24 13:36:10 +00:00
|
|
|
#include <eda_draw_frame.h>
|
2012-05-03 18:37:56 +00:00
|
|
|
#include <general.h>
|
|
|
|
#include <lib_arc.h>
|
|
|
|
#include <transform.h>
|
2020-03-06 03:00:30 +00:00
|
|
|
#include <settings/color_settings.h>
|
2019-06-29 13:39:46 +00:00
|
|
|
#include <status_popup.h>
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2012-12-27 16:42:41 +00:00
|
|
|
// Helper function
|
2013-05-04 11:57:09 +00:00
|
|
|
static inline wxPoint twoPointVector( const wxPoint &startPoint, const wxPoint &endPoint )
|
2012-12-27 16:42:41 +00:00
|
|
|
{
|
|
|
|
return endPoint - startPoint;
|
|
|
|
}
|
|
|
|
|
2012-05-03 18:37:56 +00:00
|
|
|
|
Modular KiCad Blueprint Milestone B), major portions:
*) When kicad.exe closes a project, close any open KIFACEs so that they cannot
get disassociated from their true PROJECT.
*) Allow loading eeschema library editor from kicad.exe
*) Allow loading pcbnew library editor from kicad.exe
*) Rename LIB_COMPONENT to LIB_PART.
*) Add class PART_LIBS, and PART_LIB.
*) Make PART_LIBS non-global, i.e. PROJECT specific.
*) Implement "data on demand" for PART_LIBS
*) Implement "data on demand" for schematic SEARCH_STACK.
*) Use RSTRINGs to retain eeschema editor's notion of last library and part being edited.
*) Get rid of library search on every SCH_COMPONENT::Draw() call, instead use
a weak pointer.
*) Remove all chdir() calls so projects don't need to be CWD.
*) Romove APPEND support from OpenProjectFiles().
*) Make OpenProjectFiles() robust, even for creating new projects.
*) Load EESCHEMA colors in the KIWAY::OnKiwayStart() rather in window open,
and save them in the .eeschema config file, not in the project file.
*) Fix bug with wxDir() while accessing protected dirs in kicad.exe
*) Consolidate template copying into PROJECT class, not in kicad.exe source.
*) Generally untangle eeschema, making its libraries not global but rather
held in the PROJECT.
2014-08-13 20:28:54 +00:00
|
|
|
LIB_ARC::LIB_ARC( LIB_PART* aParent ) : LIB_ITEM( LIB_ARC_T, aParent )
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
|
|
|
m_Radius = 0;
|
|
|
|
m_t1 = 0;
|
|
|
|
m_t2 = 0;
|
|
|
|
m_Width = 0;
|
|
|
|
m_Fill = NO_FILL;
|
|
|
|
m_isFillable = true;
|
|
|
|
m_editState = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-05 10:33:34 +00:00
|
|
|
bool LIB_ARC::HitTest( const wxPoint& aRefPoint, int aAccuracy ) const
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
2020-04-14 12:25:00 +00:00
|
|
|
int mindist = std::max( aAccuracy + GetPenWidth() / 2,
|
2019-12-30 18:28:00 +00:00
|
|
|
Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
|
2019-05-05 10:33:34 +00:00
|
|
|
wxPoint relativePosition = aRefPoint;
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2015-06-26 13:41:56 +00:00
|
|
|
relativePosition.y = -relativePosition.y; // reverse Y axis
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2013-05-04 11:57:09 +00:00
|
|
|
int distance = KiROUND( GetLineLength( m_Pos, relativePosition ) );
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2019-05-05 10:33:34 +00:00
|
|
|
if( abs( distance - m_Radius ) > mindist )
|
2012-05-03 18:37:56 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// We are on the circle, ensure we are only on the arc, i.e. between
|
|
|
|
// m_ArcStart and m_ArcEnd
|
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
wxPoint startEndVector = twoPointVector( m_ArcStart, m_ArcEnd );
|
2012-12-27 16:42:41 +00:00
|
|
|
wxPoint startRelativePositionVector = twoPointVector( m_ArcStart, relativePosition );
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2012-12-27 16:42:41 +00:00
|
|
|
wxPoint centerStartVector = twoPointVector( m_Pos, m_ArcStart );
|
|
|
|
wxPoint centerEndVector = twoPointVector( m_Pos, m_ArcEnd );
|
|
|
|
wxPoint centerRelativePositionVector = twoPointVector( m_Pos, relativePosition );
|
2012-05-03 18:37:56 +00:00
|
|
|
|
|
|
|
// Compute the cross product to check if the point is in the sector
|
2012-12-27 16:42:41 +00:00
|
|
|
double crossProductStart = CrossProduct( centerStartVector, centerRelativePositionVector );
|
|
|
|
double crossProductEnd = CrossProduct( centerEndVector, centerRelativePositionVector );
|
2012-05-03 18:37:56 +00:00
|
|
|
|
|
|
|
// The cross products need to be exchanged, depending on which side the center point
|
|
|
|
// relative to the start point to end point vector lies
|
|
|
|
if( CrossProduct( startEndVector, startRelativePositionVector ) < 0 )
|
|
|
|
{
|
2015-06-26 13:41:56 +00:00
|
|
|
std::swap( crossProductStart, crossProductEnd );
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When the cross products have a different sign, the point lies in sector
|
|
|
|
// also check, if the reference is near start or end point
|
|
|
|
return HitTestPoints( m_ArcStart, relativePosition, MINIMUM_SELECTION_DISTANCE ) ||
|
2019-05-05 10:33:34 +00:00
|
|
|
HitTestPoints( m_ArcEnd, relativePosition, MINIMUM_SELECTION_DISTANCE ) ||
|
|
|
|
( crossProductStart <= 0 && crossProductEnd >= 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LIB_ARC::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
|
|
|
|
{
|
|
|
|
if( m_Flags & ( STRUCT_DELETED | SKIP_STRUCT ) )
|
|
|
|
return false;
|
|
|
|
|
2019-05-10 22:24:27 +00:00
|
|
|
wxPoint center = DefaultTransform.TransformCoordinate( GetPosition() );
|
|
|
|
int radius = GetRadius();
|
|
|
|
int lineWidth = GetWidth();
|
|
|
|
EDA_RECT sel = aRect ;
|
2019-05-05 10:33:34 +00:00
|
|
|
|
|
|
|
if ( aAccuracy )
|
2019-05-10 22:24:27 +00:00
|
|
|
sel.Inflate( aAccuracy );
|
2019-05-05 10:33:34 +00:00
|
|
|
|
|
|
|
if( aContained )
|
2019-05-10 22:24:27 +00:00
|
|
|
return sel.Contains( GetBoundingBox() );
|
2019-05-05 10:33:34 +00:00
|
|
|
|
2019-05-10 22:24:27 +00:00
|
|
|
EDA_RECT arcRect = GetBoundingBox().Common( sel );
|
|
|
|
|
|
|
|
/* All following tests must pass:
|
|
|
|
* 1. Rectangle must intersect arc BoundingBox
|
|
|
|
* 2. Rectangle must cross the outside of the arc
|
|
|
|
*/
|
|
|
|
return arcRect.Intersects( sel ) && arcRect.IntersectsCircleEdge( center, radius, lineWidth );
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EDA_ITEM* LIB_ARC::Clone() const
|
|
|
|
{
|
|
|
|
return new LIB_ARC( *this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
int LIB_ARC::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
|
|
|
wxASSERT( aOther.Type() == LIB_ARC_T );
|
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
int retv = LIB_ITEM::compare( aOther );
|
|
|
|
|
|
|
|
if( retv )
|
|
|
|
return retv;
|
|
|
|
|
2012-05-03 18:37:56 +00:00
|
|
|
const LIB_ARC* tmp = ( LIB_ARC* ) &aOther;
|
|
|
|
|
|
|
|
if( m_Pos.x != tmp->m_Pos.x )
|
|
|
|
return m_Pos.x - tmp->m_Pos.x;
|
|
|
|
|
|
|
|
if( m_Pos.y != tmp->m_Pos.y )
|
|
|
|
return m_Pos.y - tmp->m_Pos.y;
|
|
|
|
|
|
|
|
if( m_t1 != tmp->m_t1 )
|
|
|
|
return m_t1 - tmp->m_t1;
|
|
|
|
|
|
|
|
if( m_t2 != tmp->m_t2 )
|
|
|
|
return m_t2 - tmp->m_t2;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-09 07:57:07 +00:00
|
|
|
void LIB_ARC::Offset( const wxPoint& aOffset )
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
|
|
|
m_Pos += aOffset;
|
|
|
|
m_ArcStart += aOffset;
|
|
|
|
m_ArcEnd += aOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-09 07:57:07 +00:00
|
|
|
void LIB_ARC::MoveTo( const wxPoint& aPosition )
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
|
|
|
wxPoint offset = aPosition - m_Pos;
|
|
|
|
m_Pos = aPosition;
|
|
|
|
m_ArcStart += offset;
|
|
|
|
m_ArcEnd += offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LIB_ARC::MirrorHorizontal( const wxPoint& aCenter )
|
|
|
|
{
|
|
|
|
m_Pos.x -= aCenter.x;
|
|
|
|
m_Pos.x *= -1;
|
|
|
|
m_Pos.x += aCenter.x;
|
|
|
|
m_ArcStart.x -= aCenter.x;
|
|
|
|
m_ArcStart.x *= -1;
|
|
|
|
m_ArcStart.x += aCenter.x;
|
|
|
|
m_ArcEnd.x -= aCenter.x;
|
|
|
|
m_ArcEnd.x *= -1;
|
|
|
|
m_ArcEnd.x += aCenter.x;
|
2015-06-26 13:41:56 +00:00
|
|
|
std::swap( m_ArcStart, m_ArcEnd );
|
|
|
|
std::swap( m_t1, m_t2 );
|
2013-02-10 18:02:45 +00:00
|
|
|
m_t1 = 1800 - m_t1;
|
|
|
|
m_t2 = 1800 - m_t2;
|
|
|
|
if( m_t1 > 3600 || m_t2 > 3600 )
|
|
|
|
{
|
|
|
|
m_t1 -= 3600;
|
|
|
|
m_t2 -= 3600;
|
|
|
|
}
|
|
|
|
else if( m_t1 < -3600 || m_t2 < -3600 )
|
|
|
|
{
|
|
|
|
m_t1 += 3600;
|
|
|
|
m_t2 += 3600;
|
|
|
|
}
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LIB_ARC::MirrorVertical( const wxPoint& aCenter )
|
|
|
|
{
|
|
|
|
m_Pos.y -= aCenter.y;
|
|
|
|
m_Pos.y *= -1;
|
|
|
|
m_Pos.y += aCenter.y;
|
|
|
|
m_ArcStart.y -= aCenter.y;
|
|
|
|
m_ArcStart.y *= -1;
|
|
|
|
m_ArcStart.y += aCenter.y;
|
|
|
|
m_ArcEnd.y -= aCenter.y;
|
|
|
|
m_ArcEnd.y *= -1;
|
|
|
|
m_ArcEnd.y += aCenter.y;
|
2015-06-26 13:41:56 +00:00
|
|
|
std::swap( m_ArcStart, m_ArcEnd );
|
|
|
|
std::swap( m_t1, m_t2 );
|
2013-02-10 18:02:45 +00:00
|
|
|
m_t1 = - m_t1;
|
|
|
|
m_t2 = - m_t2;
|
|
|
|
if( m_t1 > 3600 || m_t2 > 3600 )
|
|
|
|
{
|
|
|
|
m_t1 -= 3600;
|
|
|
|
m_t2 -= 3600;
|
|
|
|
}
|
|
|
|
else if( m_t1 < -3600 || m_t2 < -3600 )
|
|
|
|
{
|
|
|
|
m_t1 += 3600;
|
|
|
|
m_t2 += 3600;
|
|
|
|
}
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LIB_ARC::Rotate( const wxPoint& aCenter, bool aRotateCCW )
|
|
|
|
{
|
|
|
|
int rot_angle = aRotateCCW ? -900 : 900;
|
|
|
|
RotatePoint( &m_Pos, aCenter, rot_angle );
|
|
|
|
RotatePoint( &m_ArcStart, aCenter, rot_angle );
|
|
|
|
RotatePoint( &m_ArcEnd, aCenter, rot_angle );
|
2013-02-10 18:02:45 +00:00
|
|
|
m_t1 -= rot_angle;
|
|
|
|
m_t2 -= rot_angle;
|
|
|
|
if( m_t1 > 3600 || m_t2 > 3600 )
|
|
|
|
{
|
|
|
|
m_t1 -= 3600;
|
|
|
|
m_t2 -= 3600;
|
|
|
|
}
|
|
|
|
else if( m_t1 < -3600 || m_t2 < -3600 )
|
|
|
|
{
|
|
|
|
m_t1 += 3600;
|
|
|
|
m_t2 += 3600;
|
|
|
|
}
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LIB_ARC::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
|
|
|
|
const TRANSFORM& aTransform )
|
|
|
|
{
|
|
|
|
wxASSERT( aPlotter != NULL );
|
|
|
|
|
|
|
|
int t1 = m_t1;
|
|
|
|
int t2 = m_t2;
|
|
|
|
wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
|
|
|
|
|
|
|
|
aTransform.MapAngles( &t1, &t2 );
|
|
|
|
|
|
|
|
if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
|
|
|
|
{
|
2020-04-14 12:25:00 +00:00
|
|
|
aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE_BACKGROUND ) );
|
2017-11-16 18:49:07 +00:00
|
|
|
aPlotter->Arc( pos, -t2, -t1, m_Radius, FILLED_WITH_BG_BODYCOLOR, 0 );
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
|
2020-04-21 17:12:36 +00:00
|
|
|
int pen_size = GetPenWidth();
|
2019-03-11 06:41:00 +00:00
|
|
|
|
|
|
|
if( !already_filled || pen_size > 0 )
|
|
|
|
{
|
2020-09-08 19:18:50 +00:00
|
|
|
pen_size = std::max( pen_size, aPlotter->RenderSettings()->GetMinPenWidth() );
|
2020-04-21 17:12:36 +00:00
|
|
|
|
2020-04-14 12:25:00 +00:00
|
|
|
aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE ) );
|
2019-05-08 18:56:03 +00:00
|
|
|
aPlotter->Arc( pos, -t2, -t1, m_Radius, already_filled ? NO_FILL : m_Fill, pen_size );
|
2019-03-11 06:41:00 +00:00
|
|
|
}
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-14 12:25:00 +00:00
|
|
|
int LIB_ARC::GetPenWidth() const
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
2020-04-21 17:12:36 +00:00
|
|
|
// Historically 0 meant "default width" and negative numbers meant "don't stroke".
|
|
|
|
if( m_Width < 0 && GetFillMode() != NO_FILL )
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return std::max( m_Width, 1 );
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-14 12:25:00 +00:00
|
|
|
void LIB_ARC::print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
|
|
|
|
const TRANSFORM& aTransform )
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
2020-04-21 17:12:36 +00:00
|
|
|
bool forceNoFill = static_cast<bool>( aData );
|
|
|
|
int penWidth = GetPenWidth();
|
|
|
|
|
|
|
|
if( forceNoFill && m_Fill != NO_FILL && penWidth == 0 )
|
|
|
|
return;
|
|
|
|
|
2020-04-14 12:25:00 +00:00
|
|
|
wxDC* DC = aSettings->GetPrintDC();
|
2012-05-03 18:37:56 +00:00
|
|
|
wxPoint pos1, pos2, posc;
|
2020-04-14 12:25:00 +00:00
|
|
|
COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
|
2012-05-03 18:37:56 +00:00
|
|
|
|
|
|
|
pos1 = aTransform.TransformCoordinate( m_ArcEnd ) + aOffset;
|
|
|
|
pos2 = aTransform.TransformCoordinate( m_ArcStart ) + aOffset;
|
|
|
|
posc = aTransform.TransformCoordinate( m_Pos ) + aOffset;
|
|
|
|
int pt1 = m_t1;
|
|
|
|
int pt2 = m_t2;
|
|
|
|
bool swap = aTransform.MapAngles( &pt1, &pt2 );
|
|
|
|
|
|
|
|
if( swap )
|
|
|
|
{
|
2015-06-26 13:41:56 +00:00
|
|
|
std::swap( pos1.x, pos2.x );
|
|
|
|
std::swap( pos1.y, pos2.y );
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 17:12:36 +00:00
|
|
|
if( forceNoFill || m_Fill == NO_FILL )
|
|
|
|
{
|
|
|
|
penWidth = std::max( penWidth, aSettings->GetDefaultPenWidth() );
|
2014-02-18 21:41:27 +00:00
|
|
|
|
2020-04-21 17:12:36 +00:00
|
|
|
GRArc1( nullptr, DC, pos1.x, pos1.y, pos2.x, pos2.y, posc.x, posc.y, penWidth, color );
|
|
|
|
}
|
2012-05-03 18:37:56 +00:00
|
|
|
else
|
2020-04-21 17:12:36 +00:00
|
|
|
{
|
|
|
|
if( m_Fill == FILLED_WITH_BG_BODYCOLOR )
|
|
|
|
color = aSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
|
|
|
|
|
|
|
|
GRFilledArc( nullptr, DC, posc.x, posc.y, pt1, pt2, m_Radius, penWidth, color, color );
|
|
|
|
}
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-24 17:48:14 +00:00
|
|
|
const EDA_RECT LIB_ARC::GetBoundingBox() const
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
|
|
|
int minX, minY, maxX, maxY, angleStart, angleEnd;
|
|
|
|
EDA_RECT rect;
|
|
|
|
wxPoint nullPoint, startPos, endPos, centerPos;
|
|
|
|
wxPoint normStart = m_ArcStart - m_Pos;
|
|
|
|
wxPoint normEnd = m_ArcEnd - m_Pos;
|
|
|
|
|
|
|
|
if( ( normStart == nullPoint ) || ( normEnd == nullPoint ) || ( m_Radius == 0 ) )
|
|
|
|
return rect;
|
|
|
|
|
|
|
|
endPos = DefaultTransform.TransformCoordinate( m_ArcEnd );
|
|
|
|
startPos = DefaultTransform.TransformCoordinate( m_ArcStart );
|
|
|
|
centerPos = DefaultTransform.TransformCoordinate( m_Pos );
|
|
|
|
angleStart = m_t1;
|
|
|
|
angleEnd = m_t2;
|
|
|
|
|
|
|
|
if( DefaultTransform.MapAngles( &angleStart, &angleEnd ) )
|
|
|
|
{
|
2015-06-26 13:41:56 +00:00
|
|
|
std::swap( endPos.x, startPos.x );
|
|
|
|
std::swap( endPos.y, startPos.y );
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Start with the start and end point of the arc. */
|
2012-09-22 11:19:37 +00:00
|
|
|
minX = std::min( startPos.x, endPos.x );
|
|
|
|
minY = std::min( startPos.y, endPos.y );
|
|
|
|
maxX = std::max( startPos.x, endPos.x );
|
|
|
|
maxY = std::max( startPos.y, endPos.y );
|
2012-05-03 18:37:56 +00:00
|
|
|
|
|
|
|
/* Zero degrees is a special case. */
|
|
|
|
if( angleStart == 0 )
|
|
|
|
maxX = centerPos.x + m_Radius;
|
|
|
|
|
|
|
|
/* Arc end angle wrapped passed 360. */
|
|
|
|
if( angleStart > angleEnd )
|
|
|
|
angleEnd += 3600;
|
|
|
|
|
|
|
|
if( angleStart <= 900 && angleEnd >= 900 ) /* 90 deg */
|
|
|
|
maxY = centerPos.y + m_Radius;
|
|
|
|
|
|
|
|
if( angleStart <= 1800 && angleEnd >= 1800 ) /* 180 deg */
|
|
|
|
minX = centerPos.x - m_Radius;
|
|
|
|
|
|
|
|
if( angleStart <= 2700 && angleEnd >= 2700 ) /* 270 deg */
|
|
|
|
minY = centerPos.y - m_Radius;
|
|
|
|
|
|
|
|
if( angleStart <= 3600 && angleEnd >= 3600 ) /* 0 deg */
|
|
|
|
maxX = centerPos.x + m_Radius;
|
|
|
|
|
|
|
|
rect.SetOrigin( minX, minY );
|
|
|
|
rect.SetEnd( maxX, maxY );
|
2020-04-14 12:25:00 +00:00
|
|
|
rect.Inflate( ( GetPenWidth() / 2 ) + 1 );
|
2012-05-03 18:37:56 +00:00
|
|
|
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-24 13:36:10 +00:00
|
|
|
void LIB_ARC::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
EDA_RECT bBox = GetBoundingBox();
|
|
|
|
|
2020-04-24 13:36:10 +00:00
|
|
|
LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2020-04-24 13:36:10 +00:00
|
|
|
msg = MessageTextFromValue( aFrame->GetUserUnits(), m_Width, true );
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2019-12-05 15:41:21 +00:00
|
|
|
aList.emplace_back( _( "Line Width" ), msg, BLUE );
|
2012-05-03 18:37:56 +00:00
|
|
|
|
|
|
|
msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
|
|
|
|
bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
|
|
|
|
|
2019-12-05 15:41:21 +00:00
|
|
|
aList.emplace_back( _( "Bounding Box" ), msg, BROWN );
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-20 14:11:39 +00:00
|
|
|
wxString LIB_ARC::GetSelectMenuText( EDA_UNITS aUnits ) const
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
2020-09-19 16:12:00 +00:00
|
|
|
return wxString::Format( _( "Arc, radius %s" ),
|
2018-04-10 10:52:12 +00:00
|
|
|
MessageTextFromValue( aUnits, m_Radius ) );
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-20 12:20:39 +00:00
|
|
|
BITMAP_DEF LIB_ARC::GetMenuImage() const
|
|
|
|
{
|
|
|
|
return add_arc_xpm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-20 10:23:32 +00:00
|
|
|
void LIB_ARC::BeginEdit( const wxPoint aPosition )
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
2019-05-20 10:23:32 +00:00
|
|
|
m_ArcStart = m_ArcEnd = aPosition;
|
2019-06-29 13:39:46 +00:00
|
|
|
m_editState = 1;
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-29 13:39:46 +00:00
|
|
|
void LIB_ARC::CalcEdit( const wxPoint& aPosition )
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
2019-07-14 17:09:38 +00:00
|
|
|
#define sq( x ) pow( x, 2 )
|
|
|
|
|
2019-06-29 13:39:46 +00:00
|
|
|
// Edit state 0: drawing: place ArcStart
|
|
|
|
// Edit state 1: drawing: place ArcEnd (center calculated for 90-degree subtended angle)
|
|
|
|
// Edit state 2: point editing: move ArcStart (center calculated for invariant subtended angle)
|
|
|
|
// Edit state 3: point editing: move ArcEnd (center calculated for invariant subtended angle)
|
|
|
|
// Edit state 4: point editing: move center
|
|
|
|
|
|
|
|
switch( m_editState )
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
2019-06-29 13:39:46 +00:00
|
|
|
case 0:
|
|
|
|
m_ArcStart = aPosition;
|
2019-05-20 10:23:32 +00:00
|
|
|
m_ArcEnd = aPosition;
|
2019-06-29 13:39:46 +00:00
|
|
|
m_Pos = aPosition;
|
2019-07-14 17:09:38 +00:00
|
|
|
m_Radius = 0;
|
|
|
|
m_t1 = 0;
|
|
|
|
m_t2 = 0;
|
|
|
|
return;
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2019-06-29 13:39:46 +00:00
|
|
|
case 1:
|
|
|
|
m_ArcEnd = aPosition;
|
|
|
|
m_Radius = KiROUND( sqrt( pow( GetLineLength( m_ArcStart, m_ArcEnd ), 2 ) / 2.0 ) );
|
|
|
|
break;
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2019-06-29 13:39:46 +00:00
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
wxPoint v = m_ArcStart - m_ArcEnd;
|
2019-07-14 17:09:38 +00:00
|
|
|
double chordBefore = sq( v.x ) + sq( v.y );
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2019-06-29 13:39:46 +00:00
|
|
|
if( m_editState == 2 )
|
|
|
|
m_ArcStart = aPosition;
|
|
|
|
else
|
|
|
|
m_ArcEnd = aPosition;
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2019-06-29 13:39:46 +00:00
|
|
|
v = m_ArcStart - m_ArcEnd;
|
2019-07-14 17:09:38 +00:00
|
|
|
double chordAfter = sq( v.x ) + sq( v.y );
|
2019-06-29 13:39:46 +00:00
|
|
|
double ratio = chordAfter / chordBefore;
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2019-08-08 20:22:13 +00:00
|
|
|
if( ratio > 0 )
|
|
|
|
{
|
|
|
|
m_Radius = int( sqrt( m_Radius * m_Radius * ratio ) ) + 1;
|
|
|
|
m_Radius = std::max( m_Radius, int( sqrt( chordAfter ) / 2 ) + 1 );
|
|
|
|
}
|
|
|
|
|
2019-06-29 13:39:46 +00:00
|
|
|
break;
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
2019-05-20 10:23:32 +00:00
|
|
|
|
2019-06-29 13:39:46 +00:00
|
|
|
case 4:
|
2019-08-08 20:22:13 +00:00
|
|
|
{
|
|
|
|
double chordA = GetLineLength( m_ArcStart, aPosition );
|
|
|
|
double chordB = GetLineLength( m_ArcEnd, aPosition );
|
|
|
|
m_Radius = int( ( chordA + chordB ) / 2.0 ) + 1;
|
2019-06-29 13:39:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-08-08 20:22:13 +00:00
|
|
|
}
|
2019-06-29 13:39:46 +00:00
|
|
|
|
|
|
|
// Calculate center based on start, end, and radius
|
|
|
|
//
|
|
|
|
// Let 'l' be the length of the chord and 'm' the middle point of the chord
|
|
|
|
double l = GetLineLength( m_ArcStart, m_ArcEnd );
|
|
|
|
wxPoint m = ( m_ArcStart + m_ArcEnd ) / 2;
|
|
|
|
|
|
|
|
// Calculate 'd', the vector from the chord midpoint to the center
|
|
|
|
wxPoint d;
|
2019-07-14 17:09:38 +00:00
|
|
|
d.x = KiROUND( sqrt( sq( m_Radius ) - sq( l/2 ) ) * ( m_ArcStart.y - m_ArcEnd.y ) / l );
|
|
|
|
d.y = KiROUND( sqrt( sq( m_Radius ) - sq( l/2 ) ) * ( m_ArcEnd.x - m_ArcStart.x ) / l );
|
2019-06-29 13:39:46 +00:00
|
|
|
|
|
|
|
wxPoint c1 = m + d;
|
|
|
|
wxPoint c2 = m - d;
|
|
|
|
|
2019-07-14 17:09:38 +00:00
|
|
|
// Solution gives us 2 centers; we need to pick one:
|
|
|
|
switch( m_editState )
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
// Keep center clockwise from chord while drawing
|
|
|
|
wxPoint chordVector = twoPointVector( m_ArcStart, m_ArcEnd );
|
|
|
|
double chordAngle = ArcTangente( chordVector.y, chordVector.x );
|
|
|
|
NORMALIZE_ANGLE_POS( chordAngle );
|
|
|
|
|
|
|
|
wxPoint c1Test = c1;
|
|
|
|
RotatePoint( &c1Test, m_ArcStart, -chordAngle );
|
|
|
|
|
|
|
|
m_Pos = c1Test.x > 0 ? c2 : c1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
// Pick the one closer to the old center
|
|
|
|
m_Pos = ( GetLineLength( c1, m_Pos ) < GetLineLength( c2, m_Pos ) ) ? c1 : c2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
// Pick the one closer to the mouse position
|
|
|
|
m_Pos = ( GetLineLength( c1, aPosition ) < GetLineLength( c2, aPosition ) ) ? c1 : c2;
|
|
|
|
break;
|
|
|
|
}
|
2019-06-29 13:39:46 +00:00
|
|
|
|
2019-05-20 10:23:32 +00:00
|
|
|
CalcRadiusAngles();
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-25 13:51:07 +00:00
|
|
|
void LIB_ARC::CalcRadiusAngles()
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
2012-12-27 16:42:41 +00:00
|
|
|
wxPoint centerStartVector = twoPointVector( m_Pos, m_ArcStart );
|
|
|
|
wxPoint centerEndVector = twoPointVector( m_Pos, m_ArcEnd );
|
2012-05-03 18:37:56 +00:00
|
|
|
|
|
|
|
m_Radius = KiROUND( EuclideanNorm( centerStartVector ) );
|
|
|
|
|
2013-05-01 17:32:36 +00:00
|
|
|
// Angles in eeschema are still integers
|
|
|
|
m_t1 = KiROUND( ArcTangente( centerStartVector.y, centerStartVector.x ) );
|
|
|
|
m_t2 = KiROUND( ArcTangente( centerEndVector.y, centerEndVector.x ) );
|
2012-05-03 18:37:56 +00:00
|
|
|
|
|
|
|
NORMALIZE_ANGLE_POS( m_t1 );
|
|
|
|
NORMALIZE_ANGLE_POS( m_t2 ); // angles = 0 .. 3600
|
|
|
|
|
|
|
|
// Restrict angle to less than 180 to avoid PBS display mirror Trace because it is
|
|
|
|
// assumed that the arc is less than 180 deg to find orientation after rotate or mirror.
|
|
|
|
if( (m_t2 - m_t1) > 1800 )
|
|
|
|
m_t2 -= 3600;
|
|
|
|
else if( (m_t2 - m_t1) <= -1800 )
|
|
|
|
m_t2 += 3600;
|
|
|
|
|
|
|
|
while( (m_t2 - m_t1) >= 1800 )
|
|
|
|
{
|
|
|
|
m_t2--;
|
|
|
|
m_t1++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while( (m_t1 - m_t2) >= 1800 )
|
|
|
|
{
|
|
|
|
m_t2++;
|
|
|
|
m_t1--;
|
|
|
|
}
|
|
|
|
|
|
|
|
NORMALIZE_ANGLE_POS( m_t1 );
|
|
|
|
|
|
|
|
if( !IsMoving() )
|
|
|
|
NORMALIZE_ANGLE_POS( m_t2 );
|
|
|
|
}
|
2020-02-28 14:03:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
VECTOR2I LIB_ARC::CalcMidPoint() const
|
|
|
|
{
|
|
|
|
VECTOR2D midPoint;
|
|
|
|
double startAngle = static_cast<double>( m_t1 ) / 10.0;
|
|
|
|
double endAngle = static_cast<double>( m_t2 ) / 10.0;
|
|
|
|
|
2020-03-10 15:04:05 +00:00
|
|
|
if( endAngle < startAngle )
|
|
|
|
endAngle -= 360.0;
|
2020-02-28 14:03:09 +00:00
|
|
|
|
2020-03-10 15:04:05 +00:00
|
|
|
double midPointAngle = ( ( endAngle - startAngle ) / 2.0 ) + startAngle;
|
|
|
|
double x = cos( DEG2RAD( midPointAngle ) ) * m_Radius;
|
|
|
|
double y = sin( DEG2RAD( midPointAngle ) ) * m_Radius;
|
2020-02-28 14:03:09 +00:00
|
|
|
|
2020-03-10 15:04:05 +00:00
|
|
|
midPoint.x = KiROUND( x ) + m_Pos.x;
|
|
|
|
midPoint.y = KiROUND( y ) + m_Pos.y;
|
2020-02-28 14:03:09 +00:00
|
|
|
|
|
|
|
return midPoint;
|
|
|
|
}
|