2018-10-13 18:37:28 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 CERN
|
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
2023-03-30 11:49:23 +00:00
|
|
|
* Copyright (C) 2018-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2018-10-13 18:37:28 +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.
|
|
|
|
*
|
2018-11-05 16:04:05 +00:00
|
|
|
* This program is distributed in the hope that it will be useful
|
2018-10-13 18:37:28 +00:00
|
|
|
* 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 "graphics_importer_pcbnew.h"
|
|
|
|
|
2020-11-12 20:19:22 +00:00
|
|
|
#include <board.h>
|
2023-03-30 11:49:23 +00:00
|
|
|
#include <footprint.h>
|
|
|
|
#include <pcb_shape.h>
|
2020-10-04 23:34:59 +00:00
|
|
|
#include <pcb_text.h>
|
2019-12-05 13:43:55 +00:00
|
|
|
#include <memory>
|
2018-10-13 18:37:28 +00:00
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
|
2023-10-18 13:20:34 +00:00
|
|
|
GRAPHICS_IMPORTER_PCBNEW::GRAPHICS_IMPORTER_PCBNEW( BOARD_ITEM_CONTAINER* aParent ) :
|
|
|
|
m_parent( aParent )
|
2018-11-05 16:04:05 +00:00
|
|
|
{
|
|
|
|
m_layer = Dwgs_User;
|
2022-09-16 11:33:56 +00:00
|
|
|
m_millimeterToIu = pcbIUScale.mmToIU( 1.0 );
|
2018-11-05 16:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-19 03:40:07 +00:00
|
|
|
VECTOR2I GRAPHICS_IMPORTER_PCBNEW::MapCoordinate( const VECTOR2D& aCoordinate )
|
2018-11-05 16:04:05 +00:00
|
|
|
{
|
2023-08-18 18:27:48 +00:00
|
|
|
VECTOR2D coord = aCoordinate;
|
|
|
|
coord *= GetScale();
|
|
|
|
coord += GetImportOffsetMM();
|
|
|
|
coord *= GetMillimeterToIuFactor();
|
|
|
|
|
2023-02-19 03:40:07 +00:00
|
|
|
return VECTOR2I( KiROUND( coord.x ), KiROUND( coord.y ) );
|
2018-11-05 16:04:05 +00:00
|
|
|
}
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2018-11-05 16:04:05 +00:00
|
|
|
|
|
|
|
int GRAPHICS_IMPORTER_PCBNEW::MapLineWidth( double aLineWidth )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2023-08-18 18:27:48 +00:00
|
|
|
VECTOR2D factor = ImportScalingFactor();
|
|
|
|
double scale = ( factor.x + factor.y ) * 0.5;
|
|
|
|
|
2018-11-05 16:04:05 +00:00
|
|
|
if( aLineWidth <= 0.0 )
|
2023-08-18 18:27:48 +00:00
|
|
|
return int( GetLineWidthMM() * scale );
|
2018-11-05 16:04:05 +00:00
|
|
|
|
|
|
|
// aLineWidth is in mm:
|
2023-08-18 18:27:48 +00:00
|
|
|
return int( aLineWidth * scale );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-09 03:56:13 +00:00
|
|
|
STROKE_PARAMS GRAPHICS_IMPORTER_PCBNEW::MapStrokeParams( const IMPORTED_STROKE& aStroke )
|
2023-10-09 00:54:34 +00:00
|
|
|
{
|
2023-10-09 03:56:13 +00:00
|
|
|
double width = aStroke.GetWidth();
|
2023-10-09 00:54:34 +00:00
|
|
|
|
|
|
|
return STROKE_PARAMS( width != -1 ? MapLineWidth( width ) : -1, aStroke.GetPlotStyle(),
|
|
|
|
aStroke.GetColor() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GRAPHICS_IMPORTER_PCBNEW::AddLine( const VECTOR2D& aStart, const VECTOR2D& aEnd,
|
2023-10-09 03:56:13 +00:00
|
|
|
const IMPORTED_STROKE& aStroke )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2023-10-18 13:20:34 +00:00
|
|
|
std::unique_ptr<PCB_SHAPE> line = std::make_unique<PCB_SHAPE>( m_parent );
|
2021-07-21 18:31:25 +00:00
|
|
|
line->SetShape( SHAPE_T::SEGMENT );
|
2018-10-13 18:37:28 +00:00
|
|
|
line->SetLayer( GetLayer() );
|
2023-10-09 00:54:34 +00:00
|
|
|
line->SetStroke( MapStrokeParams( aStroke ) );
|
|
|
|
line->SetStart( MapCoordinate( aStart ) );
|
2018-11-05 16:04:05 +00:00
|
|
|
line->SetEnd( MapCoordinate( aEnd ) );
|
|
|
|
|
2022-12-13 19:23:40 +00:00
|
|
|
// Skip 0 len lines:
|
|
|
|
if( line->GetStart() == line->GetEnd() )
|
|
|
|
return;
|
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
addItem( std::move( line ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
void GRAPHICS_IMPORTER_PCBNEW::AddCircle( const VECTOR2D& aCenter, double aRadius,
|
2023-10-09 03:56:13 +00:00
|
|
|
const IMPORTED_STROKE& aStroke, bool aFilled,
|
2023-10-09 00:54:34 +00:00
|
|
|
const COLOR4D& aFillColor )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2023-10-18 13:20:34 +00:00
|
|
|
std::unique_ptr<PCB_SHAPE> circle = std::make_unique<PCB_SHAPE>( m_parent );
|
2021-07-21 18:31:25 +00:00
|
|
|
circle->SetShape( SHAPE_T::CIRCLE );
|
2020-11-20 01:07:08 +00:00
|
|
|
circle->SetFilled( aFilled );
|
2018-10-13 18:37:28 +00:00
|
|
|
circle->SetLayer( GetLayer() );
|
2023-10-09 00:54:34 +00:00
|
|
|
circle->SetStroke( MapStrokeParams( aStroke ) );
|
|
|
|
circle->SetStart( MapCoordinate( aCenter ) );
|
2021-07-17 19:56:18 +00:00
|
|
|
circle->SetEnd( MapCoordinate( VECTOR2D( aCenter.x + aRadius, aCenter.y ) ) );
|
2018-11-05 16:04:05 +00:00
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
addItem( std::move( circle ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-22 18:59:43 +00:00
|
|
|
void GRAPHICS_IMPORTER_PCBNEW::AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart,
|
2023-10-09 03:56:13 +00:00
|
|
|
const EDA_ANGLE& aAngle, const IMPORTED_STROKE& aStroke )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2023-10-18 13:20:34 +00:00
|
|
|
std::unique_ptr<PCB_SHAPE> arc = std::make_unique<PCB_SHAPE>( m_parent );
|
2021-07-21 18:31:25 +00:00
|
|
|
arc->SetShape( SHAPE_T::ARC );
|
2018-10-13 18:37:28 +00:00
|
|
|
arc->SetLayer( GetLayer() );
|
2021-12-07 18:51:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We need to perform the rotation/conversion here while still using floating point values
|
|
|
|
* to avoid rounding errors when operating in integer space in pcbnew
|
|
|
|
*/
|
2022-01-20 23:08:48 +00:00
|
|
|
VECTOR2D end = aStart;
|
|
|
|
VECTOR2D mid = aStart;
|
2021-12-07 18:51:58 +00:00
|
|
|
|
2022-01-20 23:08:48 +00:00
|
|
|
RotatePoint( end, aCenter, -aAngle );
|
|
|
|
RotatePoint( mid, aCenter, -aAngle / 2.0 );
|
2021-12-07 18:51:58 +00:00
|
|
|
|
|
|
|
arc->SetArcGeometry( MapCoordinate( aStart ), MapCoordinate( mid ), MapCoordinate( end ) );
|
|
|
|
|
2023-06-19 08:43:30 +00:00
|
|
|
// Ensure the arc can be handled by Pcbnew. Arcs with a too big radius cannot.
|
|
|
|
// The criteria used here is radius < MAX_INT / 2.
|
|
|
|
// this is not perfect, but we do not know the exact final position of the arc, so
|
|
|
|
// we cannot test the coordinate values, because the arc can be moved before being placed.
|
|
|
|
VECTOR2D center = CalcArcCenter( arc->GetStart(), arc->GetEnd(), aAngle );
|
|
|
|
double radius = ( center - arc->GetStart() ).EuclideanNorm();
|
|
|
|
double rd_max_value = std::numeric_limits<VECTOR2I::coord_type>::max() / 2.0;
|
|
|
|
|
|
|
|
if( radius >= rd_max_value )
|
|
|
|
{
|
|
|
|
// Arc cannot be handled: convert it to a segment
|
2023-10-09 00:54:34 +00:00
|
|
|
AddLine( aStart, end, aStroke );
|
2023-06-19 08:43:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
arc->SetStroke( MapStrokeParams( aStroke ) );
|
2018-11-05 16:04:05 +00:00
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
addItem( std::move( arc ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
void GRAPHICS_IMPORTER_PCBNEW::AddPolygon( const std::vector<VECTOR2D>& aVertices,
|
2023-10-09 03:56:13 +00:00
|
|
|
const IMPORTED_STROKE& aStroke, bool aFilled,
|
2023-10-09 00:54:34 +00:00
|
|
|
const COLOR4D& aFillColor )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2022-01-01 06:04:08 +00:00
|
|
|
std::vector<VECTOR2I> convertedPoints;
|
2022-08-17 03:38:08 +00:00
|
|
|
convertedPoints.reserve( aVertices.size() );
|
2018-11-05 16:04:05 +00:00
|
|
|
|
|
|
|
for( const VECTOR2D& precisePoint : aVertices )
|
|
|
|
convertedPoints.emplace_back( MapCoordinate( precisePoint ) );
|
|
|
|
|
2023-10-18 13:20:34 +00:00
|
|
|
std::unique_ptr<PCB_SHAPE> polygon = std::make_unique<PCB_SHAPE>( m_parent );
|
2021-07-21 18:31:25 +00:00
|
|
|
polygon->SetShape( SHAPE_T::POLY );
|
2020-11-14 01:16:02 +00:00
|
|
|
polygon->SetFilled( GetLayer() != Edge_Cuts );
|
2018-10-13 18:37:28 +00:00
|
|
|
polygon->SetLayer( GetLayer() );
|
2018-11-05 16:04:05 +00:00
|
|
|
polygon->SetPolyPoints( convertedPoints );
|
|
|
|
|
2023-03-30 11:49:23 +00:00
|
|
|
if( FOOTPRINT* parentFP = polygon->GetParentFootprint() )
|
|
|
|
{
|
|
|
|
polygon->Rotate( { 0, 0 }, parentFP->GetOrientation() );
|
|
|
|
polygon->Move( parentFP->GetPosition() );
|
|
|
|
}
|
2018-11-05 16:04:05 +00:00
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
polygon->SetStroke( MapStrokeParams( aStroke ) );
|
2018-10-13 18:37:28 +00:00
|
|
|
addItem( std::move( polygon ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GRAPHICS_IMPORTER_PCBNEW::AddText( const VECTOR2D& aOrigin, const wxString& aText,
|
2021-07-17 19:56:18 +00:00
|
|
|
double aHeight, double aWidth, double aThickness,
|
2021-12-28 22:13:54 +00:00
|
|
|
double aOrientation, GR_TEXT_H_ALIGN_T aHJustify,
|
2023-08-18 18:27:48 +00:00
|
|
|
GR_TEXT_V_ALIGN_T aVJustify, const COLOR4D& aColor )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2023-10-18 13:20:34 +00:00
|
|
|
std::unique_ptr<PCB_TEXT> textItem = std::make_unique<PCB_TEXT>( m_parent );
|
2023-03-30 11:49:23 +00:00
|
|
|
textItem->SetLayer( GetLayer() );
|
2020-04-14 12:25:00 +00:00
|
|
|
textItem->SetTextThickness( MapLineWidth( aThickness ) );
|
2018-11-05 16:04:05 +00:00
|
|
|
textItem->SetTextPos( MapCoordinate( aOrigin ) );
|
2023-03-30 11:49:23 +00:00
|
|
|
textItem->SetTextAngle( EDA_ANGLE( aOrientation, DEGREES_T ) );
|
2023-08-18 18:27:48 +00:00
|
|
|
textItem->SetTextWidth( aWidth * ImportScalingFactor().x );
|
|
|
|
textItem->SetTextHeight( aHeight * ImportScalingFactor().y );
|
2018-10-13 18:37:28 +00:00
|
|
|
textItem->SetVertJustify( aVJustify );
|
|
|
|
textItem->SetHorizJustify( aHJustify );
|
|
|
|
textItem->SetText( aText );
|
2018-11-05 16:04:05 +00:00
|
|
|
|
2023-03-30 11:49:23 +00:00
|
|
|
addItem( std::move( textItem ) );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
void GRAPHICS_IMPORTER_PCBNEW::AddSpline( const VECTOR2D& aStart, const VECTOR2D& aBezierControl1,
|
|
|
|
const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
|
2023-10-09 03:56:13 +00:00
|
|
|
const IMPORTED_STROKE& aStroke )
|
2018-11-01 09:47:41 +00:00
|
|
|
{
|
2023-10-18 13:20:34 +00:00
|
|
|
std::unique_ptr<PCB_SHAPE> spline = std::make_unique<PCB_SHAPE>( m_parent );
|
2021-07-21 18:31:25 +00:00
|
|
|
spline->SetShape( SHAPE_T::BEZIER );
|
2018-11-01 09:47:41 +00:00
|
|
|
spline->SetLayer( GetLayer() );
|
2023-10-09 00:54:34 +00:00
|
|
|
spline->SetStroke( MapStrokeParams( aStroke ) );
|
2018-11-05 16:04:05 +00:00
|
|
|
spline->SetStart( MapCoordinate( aStart ) );
|
2023-10-09 00:54:34 +00:00
|
|
|
spline->SetBezierC1( MapCoordinate( aBezierControl1 ));
|
|
|
|
spline->SetBezierC2( MapCoordinate( aBezierControl2 ));
|
2018-11-05 16:04:05 +00:00
|
|
|
spline->SetEnd( MapCoordinate( aEnd ) );
|
2023-10-09 00:54:34 +00:00
|
|
|
spline->RebuildBezierToSegmentsPointsList( aStroke.GetWidth() );
|
2018-11-05 16:04:05 +00:00
|
|
|
|
2022-12-13 19:23:40 +00:00
|
|
|
// If the spline is degenerated (i.e. a segment) add it as segment or discard it if
|
|
|
|
// null (i.e. very small) length
|
|
|
|
if( spline->GetBezierPoints().size() <= 2 )
|
|
|
|
{
|
|
|
|
spline->SetShape( SHAPE_T::SEGMENT );
|
|
|
|
int dist = VECTOR2I(spline->GetStart()- spline->GetEnd()).EuclideanNorm();
|
|
|
|
|
|
|
|
// segment smaller than MIN_SEG_LEN_ACCEPTABLE_NM nanometers are skipped.
|
|
|
|
#define MIN_SEG_LEN_ACCEPTABLE_NM 20
|
|
|
|
if( dist < MIN_SEG_LEN_ACCEPTABLE_NM )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-01 09:47:41 +00:00
|
|
|
addItem( std::move( spline ) );
|
|
|
|
}
|
|
|
|
|