KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
graphics_importer_pcbnew.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (C) 2016 CERN
5 * @author Maciej Suminski <maciej.suminski@cern.ch>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
27
28#include <board.h>
29#include <footprint.h>
30#include <pcb_shape.h>
31#include <pcb_text.h>
32#include <memory>
33#include <tuple>
34
35
37 m_parent( aParent )
38{
41}
42
43
45{
46 VECTOR2D coord = aCoordinate;
47 coord *= GetScale();
48 coord += GetImportOffsetMM();
49 coord *= GetMillimeterToIuFactor();
50
51 return VECTOR2I( KiROUND( coord.x ), KiROUND( coord.y ) );
52}
53
54
56{
58 double scale = ( factor.x + factor.y ) * 0.5;
59
60 if( aLineWidth <= 0.0 )
61 return int( GetLineWidthMM() * scale );
62
63 // aLineWidth is in mm:
64 return int( aLineWidth * scale );
65}
66
67
69{
70 // Historicaly -1 meant no-stroke in Eeschema, but this has never been the case for
71 // PCBNew. (The importer, which doesn't know which program it's creating content for,
72 // also uses -1 for no-stroke.)
73 int width = ( aStroke.GetWidth() == -1 ) ? 0 : MapLineWidth( aStroke.GetWidth() );
74
75 return STROKE_PARAMS( width, aStroke.GetPlotStyle(), aStroke.GetColor() );
76}
77
78
79void GRAPHICS_IMPORTER_PCBNEW::AddLine( const VECTOR2D& aStart, const VECTOR2D& aEnd,
80 const IMPORTED_STROKE& aStroke )
81{
82 std::unique_ptr<PCB_SHAPE> line = std::make_unique<PCB_SHAPE>( m_parent );
83 line->SetShape( SHAPE_T::SEGMENT );
84 line->SetLayer( GetLayer() );
85 line->SetStroke( MapStrokeParams( aStroke ) );
86 line->SetStart( MapCoordinate( aStart ) );
87 line->SetEnd( MapCoordinate( aEnd ) );
88
89 // Skip 0 len lines:
90 if( line->GetStart() == line->GetEnd() )
91 return;
92
93 addItem( std::move( line ) );
94}
95
96
97void GRAPHICS_IMPORTER_PCBNEW::AddCircle( const VECTOR2D& aCenter, double aRadius,
98 const IMPORTED_STROKE& aStroke, bool aFilled,
99 const COLOR4D& aFillColor )
100{
101 std::unique_ptr<PCB_SHAPE> circle = std::make_unique<PCB_SHAPE>( m_parent );
102 circle->SetShape( SHAPE_T::CIRCLE );
103 circle->SetFilled( aFilled );
104 circle->SetLayer( GetLayer() );
105 circle->SetStroke( MapStrokeParams( aStroke ) );
106 circle->SetStart( MapCoordinate( aCenter ) );
107 circle->SetEnd( MapCoordinate( VECTOR2D( aCenter.x + aRadius, aCenter.y ) ) );
108
109 addItem( std::move( circle ) );
110}
111
112
113void GRAPHICS_IMPORTER_PCBNEW::AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart,
114 const EDA_ANGLE& aAngle, const IMPORTED_STROKE& aStroke )
115{
120 VECTOR2D end = aStart;
121 VECTOR2D mid = aStart;
122
123 RotatePoint( end, aCenter, -aAngle );
124 RotatePoint( mid, aCenter, -aAngle / 2.0 );
125
126 // Ensure the arc can be handled by Pcbnew. Arcs with a too big radius cannot.
127 // The criteria used here is radius < MAX_INT / 2.
128 // this is not perfect, but we do not know the exact final position of the arc, so
129 // we cannot test the coordinate values, because the arc can be moved before being placed.
130 VECTOR2D center = MapCoordinate( aCenter );
131 double radius = ( center - MapCoordinate( aStart ) ).EuclideanNorm();
132 double rd_max_value = std::numeric_limits<VECTOR2I::coord_type>::max() / 2.0;
133
134 if( radius >= rd_max_value )
135 {
136 // Arc cannot be handled: convert it to a segment
137 AddLine( aStart, end, aStroke );
138 return;
139 }
140
141 std::unique_ptr<PCB_SHAPE> arc = std::make_unique<PCB_SHAPE>( m_parent );
142 arc->SetShape( SHAPE_T::ARC );
143 arc->SetLayer( GetLayer() );
144
145 arc->SetArcGeometry( MapCoordinate( aStart ), MapCoordinate( mid ), MapCoordinate( end ) );
146 arc->SetStroke( MapStrokeParams( aStroke ) );
147
148 addItem( std::move( arc ) );
149}
150
151
152void GRAPHICS_IMPORTER_PCBNEW::AddPolygon( const std::vector<VECTOR2D>& aVertices,
153 const IMPORTED_STROKE& aStroke, bool aFilled,
154 const COLOR4D& aFillColor )
155{
156 std::vector<VECTOR2I> convertedPoints;
157 convertedPoints.reserve( aVertices.size() );
158
159 for( const VECTOR2D& precisePoint : aVertices )
160 convertedPoints.emplace_back( MapCoordinate( precisePoint ) );
161
162 std::unique_ptr<PCB_SHAPE> polygon = std::make_unique<PCB_SHAPE>( m_parent );
163 polygon->SetShape( SHAPE_T::POLY );
164 polygon->SetFilled( aFilled );
165 polygon->SetLayer( GetLayer() );
166 polygon->SetPolyPoints( convertedPoints );
167
168 if( FOOTPRINT* parentFP = polygon->GetParentFootprint() )
169 {
170 polygon->Rotate( { 0, 0 }, parentFP->GetOrientation() );
171 polygon->Move( parentFP->GetPosition() );
172 }
173
174 polygon->SetStroke( MapStrokeParams( aStroke ) );
175
176 if( polygon->IsPolyShapeValid() )
177 addItem( std::move( polygon ) );
178}
179
180
181void GRAPHICS_IMPORTER_PCBNEW::AddText( const VECTOR2D& aOrigin, const wxString& aText,
182 double aHeight, double aWidth, double aThickness,
183 double aOrientation, GR_TEXT_H_ALIGN_T aHJustify,
184 GR_TEXT_V_ALIGN_T aVJustify, const COLOR4D& aColor )
185{
186 std::unique_ptr<PCB_TEXT> textItem = std::make_unique<PCB_TEXT>( m_parent );
187 textItem->SetLayer( GetLayer() );
188 textItem->SetTextThickness( MapLineWidth( aThickness ) );
189 textItem->SetTextPos( MapCoordinate( aOrigin ) );
190 textItem->SetTextAngle( EDA_ANGLE( aOrientation, DEGREES_T ) );
191 textItem->SetTextWidth( aWidth * ImportScalingFactor().x );
192 textItem->SetTextHeight( aHeight * ImportScalingFactor().y );
193 textItem->SetVertJustify( aVJustify );
194 textItem->SetHorizJustify( aHJustify );
195 textItem->SetText( aText );
196
197 addItem( std::move( textItem ) );
198}
199
200
201void GRAPHICS_IMPORTER_PCBNEW::AddSpline( const VECTOR2D& aStart, const VECTOR2D& aBezierControl1,
202 const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
203 const IMPORTED_STROKE& aStroke )
204{
205 std::unique_ptr<PCB_SHAPE> spline = std::make_unique<PCB_SHAPE>( m_parent );
206
207 spline->SetLayer( GetLayer() );
208 spline->SetStroke( MapStrokeParams( aStroke ) );
209 spline->SetStart( MapCoordinate( aStart ) );
210 spline->SetBezierC1( MapCoordinate( aBezierControl1 ));
211 spline->SetBezierC2( MapCoordinate( aBezierControl2 ));
212 spline->SetEnd( MapCoordinate( aEnd ) );
213
214 if( setupSplineOrLine( *spline, ARC_HIGH_DEF ) )
215 {
216 addItem( std::move( spline ) );
217 }
218}
constexpr int ARC_HIGH_DEF
Definition: base_units.h:120
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
Abstract interface for BOARD_ITEMs capable of storing other items inside.
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:305
GRAPHICS_IMPORTER_PCBNEW(BOARD_ITEM_CONTAINER *aParent)
void AddText(const VECTOR2D &aOrigin, const wxString &aText, double aHeight, double aWidth, double aThickness, double aOrientation, GR_TEXT_H_ALIGN_T aHJustify, GR_TEXT_V_ALIGN_T aVJustify, const COLOR4D &aColor=COLOR4D::UNSPECIFIED) override
Create an object representing a text.
BOARD_ITEM_CONTAINER * m_parent
void AddArc(const VECTOR2D &aCenter, const VECTOR2D &aStart, const EDA_ANGLE &aAngle, const IMPORTED_STROKE &aStroke) override
Create an object representing an arc.
void AddCircle(const VECTOR2D &aCenter, double aRadius, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor=COLOR4D::UNSPECIFIED) override
Create an object representing a circle.
void AddLine(const VECTOR2D &aStart, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke) override
Create an object representing a line segment.
void AddSpline(const VECTOR2D &aStart, const VECTOR2D &aBezierControl1, const VECTOR2D &aBezierControl2, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke) override
Create an object representing an arc.
int MapLineWidth(double aLineWidth)
If aLineWidth < 0, the default line thickness value is returned.
PCB_LAYER_ID m_layer
< Target layer for the imported shapes.
void AddPolygon(const std::vector< VECTOR2D > &aVertices, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor=COLOR4D::UNSPECIFIED) override
Create an object representing a polygon.
STROKE_PARAMS MapStrokeParams(const IMPORTED_STROKE &aStroke)
VECTOR2I MapCoordinate(const VECTOR2D &aCoordinate)
Convert an imported coordinate to a board coordinate, according to the internal units,...
VECTOR2D GetScale() const
double GetMillimeterToIuFactor()
double GetLineWidthMM() const
Return the line width used for importing the outlines (in mm).
void addItem(std::unique_ptr< EDA_ITEM > aItem)
Add an item to the imported shapes list.
double m_millimeterToIu
Factor to convert millimeters to Internal Units.
bool setupSplineOrLine(EDA_SHAPE &aShape, int aAccuracy)
Configure a shape as a spline or a line segment if it's degenerate.
const VECTOR2D & GetImportOffsetMM() const
VECTOR2D ImportScalingFactor() const
A clone of IMPORTED_STROKE, but with floating-point width.
LINE_STYLE GetPlotStyle() const
double GetWidth() const
KIGFX::COLOR4D GetColor() const
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
Simple container to manage line stroke parameters.
Definition: stroke_params.h:94
@ DEGREES_T
Definition: eda_angle.h:31
@ Dwgs_User
Definition: layer_ids.h:107
const int scale
constexpr int mmToIU(double mm) const
Definition: base_units.h:88
VECTOR2I center
int radius
VECTOR2I end
SHAPE_CIRCLE circle(c.m_circle_center, c.m_circle_radius)
GR_TEXT_H_ALIGN_T
This is API surface mapped to common.types.HorizontalAlignment.
GR_TEXT_V_ALIGN_T
This is API surface mapped to common.types.VertialAlignment.
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:229
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695
VECTOR2< double > VECTOR2D
Definition: vector2d.h:694