2016-09-02 10:08:40 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 "kicadcurve.h"
|
|
|
|
|
2019-04-15 17:09:27 +00:00
|
|
|
#include <sexpr/sexpr.h>
|
|
|
|
|
|
|
|
#include <wx/log.h>
|
|
|
|
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <cmath>
|
2019-04-15 17:09:27 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2021-10-17 15:40:52 +00:00
|
|
|
#include <../../../libs/kimath/include/geometry/shape_arc.h>
|
2019-04-15 17:09:27 +00:00
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
KICADCURVE::KICADCURVE()
|
|
|
|
{
|
|
|
|
m_form = CURVE_NONE;
|
|
|
|
m_angle = 0.0;
|
|
|
|
m_radius = 0.0;
|
|
|
|
m_layer = LAYER_NONE;
|
|
|
|
m_startangle = 0.0;
|
|
|
|
m_endangle = 0.0;
|
2021-10-17 15:40:52 +00:00
|
|
|
m_arcHasMiddlePoint = false;
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
KICADCURVE::~KICADCURVE()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-20 12:28:43 +00:00
|
|
|
#include <sexpr/sexpr_parser.h>
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
bool KICADCURVE::Read( SEXPR::SEXPR* aEntry, CURVE_TYPE aCurveType )
|
|
|
|
{
|
2020-10-20 12:28:43 +00:00
|
|
|
if( CURVE_LINE != aCurveType && CURVE_ARC != aCurveType
|
2021-02-25 17:49:32 +00:00
|
|
|
&& CURVE_CIRCLE != aCurveType && CURVE_BEZIER != aCurveType
|
|
|
|
&& CURVE_POLYGON != aCurveType )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2020-10-20 12:28:43 +00:00
|
|
|
wxLogMessage( "* Unsupported curve type: %d\n", aCurveType );
|
2016-09-02 10:08:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_form = aCurveType;
|
|
|
|
|
|
|
|
int nchild = aEntry->GetNumberOfChildren();
|
|
|
|
|
|
|
|
if( ( CURVE_CIRCLE == aCurveType && nchild < 5 )
|
|
|
|
|| ( CURVE_ARC == aCurveType && nchild < 6 )
|
2020-10-20 12:28:43 +00:00
|
|
|
|| ( CURVE_LINE == aCurveType && nchild < 5 )
|
2021-02-25 17:49:32 +00:00
|
|
|
|| ( CURVE_BEZIER == aCurveType && nchild < 5 )
|
|
|
|
|| ( CURVE_POLYGON == aCurveType && nchild < 5 ) )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2020-10-20 12:28:43 +00:00
|
|
|
wxLogMessage( "* bad curve data; not enough parameters\n" );
|
2016-09-02 10:08:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEXPR::SEXPR* child;
|
|
|
|
std::string text;
|
|
|
|
|
|
|
|
for( int i = 1; i < nchild; ++i )
|
|
|
|
{
|
|
|
|
child = aEntry->GetChild( i );
|
|
|
|
|
|
|
|
if( !child->IsList() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
text = child->GetChild( 0 )->GetSymbol();
|
|
|
|
|
2020-10-20 12:28:43 +00:00
|
|
|
if( text == "pts" )
|
|
|
|
{
|
|
|
|
// Parse and extract the list of xy coordinates
|
|
|
|
SEXPR::PARSER parser;
|
|
|
|
std::unique_ptr<SEXPR::SEXPR> prms = parser.Parse( child->AsString() );
|
|
|
|
|
2021-02-25 17:49:32 +00:00
|
|
|
// We need 4 XY parameters (and "pts" that is the first parameter)
|
|
|
|
if( ( aCurveType == CURVE_BEZIER && prms->GetNumberOfChildren() != 5 )
|
|
|
|
|| ( aCurveType == CURVE_POLYGON && prms->GetNumberOfChildren() < 4 ) )
|
2020-10-20 12:28:43 +00:00
|
|
|
return false;
|
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
// Extract xy coordinates from pts list
|
2020-10-20 12:28:43 +00:00
|
|
|
SEXPR::SEXPR_VECTOR const* list = prms->GetChildren();
|
|
|
|
|
|
|
|
// The first parameter is "pts", so skip it.
|
2021-02-25 17:49:32 +00:00
|
|
|
for( SEXPR::SEXPR_VECTOR::size_type ii = 1; ii < list->size(); ++ii )
|
2020-10-20 12:28:43 +00:00
|
|
|
{
|
2021-02-25 17:49:32 +00:00
|
|
|
SEXPR::SEXPR* sub_child = ( *list )[ii];
|
2020-10-20 12:28:43 +00:00
|
|
|
text = sub_child->GetChild( 0 )->GetSymbol();
|
|
|
|
|
|
|
|
if( text == "xy" )
|
|
|
|
{
|
|
|
|
DOUBLET coord;
|
|
|
|
|
|
|
|
if( !Get2DCoordinate( sub_child, coord ) )
|
|
|
|
return false;
|
|
|
|
|
2021-02-25 17:49:32 +00:00
|
|
|
if( aCurveType == CURVE_BEZIER )
|
2020-10-20 12:28:43 +00:00
|
|
|
{
|
2021-02-25 17:49:32 +00:00
|
|
|
switch( ii )
|
|
|
|
{
|
2021-03-22 09:58:07 +00:00
|
|
|
case 1: m_start = coord; break;
|
|
|
|
case 2: m_bezierctrl1 = coord; break;
|
|
|
|
case 3: m_bezierctrl2 = coord; break;
|
|
|
|
case 4: m_end = coord; break;
|
2021-02-25 17:49:32 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-10-20 12:28:43 +00:00
|
|
|
}
|
2021-02-25 17:49:32 +00:00
|
|
|
else
|
|
|
|
m_poly.push_back( coord );
|
2020-10-20 12:28:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( text == "start" || text == "center" )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
if( !Get2DCoordinate( child, m_start ) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if( text == "end" )
|
|
|
|
{
|
|
|
|
if( !Get2DCoordinate( child, m_end ) )
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-17 15:40:52 +00:00
|
|
|
else if( text == "mid" )
|
|
|
|
{
|
|
|
|
if( !Get2DCoordinate( child, m_middle ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_arcHasMiddlePoint = true;
|
|
|
|
}
|
2016-09-02 10:08:40 +00:00
|
|
|
else if( text == "angle" )
|
|
|
|
{
|
|
|
|
if( child->GetNumberOfChildren() < 2
|
|
|
|
|| ( !child->GetChild( 1 )->IsDouble()
|
|
|
|
&& !child->GetChild( 1 )->IsInteger() ) )
|
|
|
|
{
|
2020-10-20 12:28:43 +00:00
|
|
|
wxLogMessage( "* bad angle data\n" );
|
2016-09-02 10:08:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( child->GetChild( 1 )->IsDouble() )
|
|
|
|
m_angle = child->GetChild( 1 )->GetDouble();
|
|
|
|
else
|
|
|
|
m_angle = child->GetChild( 1 )->GetInteger();
|
|
|
|
|
|
|
|
m_angle = m_angle / 180.0 * M_PI;
|
|
|
|
}
|
|
|
|
else if( text == "layer" )
|
|
|
|
{
|
2019-04-15 17:29:03 +00:00
|
|
|
const OPT<std::string> layer = GetLayerName( *child );
|
2019-04-15 08:56:01 +00:00
|
|
|
|
|
|
|
if( !layer )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2019-04-15 08:56:01 +00:00
|
|
|
ostr << "* bad layer data: " << child->AsString();
|
2016-09-02 10:08:40 +00:00
|
|
|
wxLogMessage( "%s\n", ostr.str().c_str() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: for the moment we only process Edge.Cuts
|
2019-04-15 08:56:01 +00:00
|
|
|
if( *layer == "Edge.Cuts" )
|
2016-09-02 10:08:40 +00:00
|
|
|
m_layer = LAYER_EDGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 15:40:52 +00:00
|
|
|
// New arcs are defined by start middle and end points instead of center,
|
|
|
|
// start and arc angle
|
|
|
|
// So convert new params to old params
|
|
|
|
if( CURVE_ARC == aCurveType && m_arcHasMiddlePoint )
|
|
|
|
{
|
|
|
|
// To caculate old params, we are using SHAPE_ARC, but SHAPE_ARC use
|
|
|
|
// integer coords. So to avoid truncations, use a scaling factor.
|
|
|
|
// 1e5 is enough.
|
|
|
|
const double scale = 1e5;
|
|
|
|
SHAPE_ARC new_arc( VECTOR2I( m_start.x*scale, m_start.y*scale ),
|
|
|
|
VECTOR2I( m_middle.x*scale, m_middle.y*scale ),
|
|
|
|
VECTOR2I( m_end.x*scale, m_end.y*scale ), 0 );
|
|
|
|
|
|
|
|
VECTOR2I center = new_arc.GetCenter();
|
|
|
|
m_start.x = center.x/scale;
|
|
|
|
m_start.y = center.y/scale;
|
|
|
|
m_end.x = new_arc.GetP0().x/scale;
|
|
|
|
m_end.y = new_arc.GetP0().y/scale;
|
|
|
|
m_ep.x = new_arc.GetP1().x/scale;
|
|
|
|
m_ep.y = new_arc.GetP1().y/scale;
|
2022-01-15 12:52:20 +00:00
|
|
|
m_angle = new_arc.GetCentralAngle().AsRadians();
|
2021-10-17 15:40:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-06-18 12:20:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
std::string KICADCURVE::Describe() const
|
|
|
|
{
|
|
|
|
std::ostringstream desc;
|
|
|
|
|
|
|
|
switch( m_form )
|
|
|
|
{
|
|
|
|
case CURVE_LINE:
|
|
|
|
desc << "line start: " << m_start << " end: " << m_end;
|
|
|
|
break;
|
2018-10-02 06:34:07 +00:00
|
|
|
|
2018-06-18 12:20:25 +00:00
|
|
|
case CURVE_ARC:
|
|
|
|
desc << "arc center: " << m_start << " radius: " << m_radius
|
2018-10-02 06:34:07 +00:00
|
|
|
<< " angle: " << 180.0 * m_angle / M_PI
|
|
|
|
<< " arc start: " << m_end << " arc end: " << m_ep;
|
2018-06-18 12:20:25 +00:00
|
|
|
break;
|
2018-10-02 06:34:07 +00:00
|
|
|
|
2018-06-18 12:20:25 +00:00
|
|
|
case CURVE_CIRCLE:
|
|
|
|
desc << "circle center: " << m_start << " radius: " << m_radius;
|
|
|
|
break;
|
2018-10-02 06:34:07 +00:00
|
|
|
|
2020-10-20 12:28:43 +00:00
|
|
|
case CURVE_BEZIER:
|
|
|
|
desc << "bezier start: " << m_start << " end: " << m_end
|
|
|
|
<< " ctrl1: " << m_bezierctrl1 << " ctrl2: " << m_bezierctrl2 ;
|
|
|
|
break;
|
|
|
|
|
2018-06-18 12:20:25 +00:00
|
|
|
default:
|
|
|
|
desc << "<invalid curve type>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return desc.str();
|
|
|
|
}
|