KiCad PCB EDA Suite
Loading...
Searching...
No Matches
graphics_importer_lib_symbol.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 <[email protected]>
6 * Copyright (C) 2018-2023 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 <lib_symbol.h>
29#include <lib_shape.h>
30#include <lib_text.h>
31#include <memory>
32#include <tuple>
33
34
36 m_symbol( aSymbol ), m_unit( aUnit )
37{
39}
40
41
43{
44 VECTOR2D coord = aCoordinate;
45 coord *= GetScale();
46 coord += GetImportOffsetMM();
47 coord *= GetMillimeterToIuFactor();
48
49 return VECTOR2I( KiROUND( coord.x ), KiROUND( coord.y ) );
50}
51
52
54{
56 double scale = ( std::abs( factor.x ) + std::abs( factor.y ) ) * 0.5;
57
58 if( aLineWidth <= 0.0 )
59 return int( GetLineWidthMM() * scale );
60
61 // aLineWidth is in mm:
62 return int( aLineWidth * scale );
63}
64
65
67{
68 double width = aStroke.GetWidth();
69
70 return STROKE_PARAMS( width != -1 ? MapLineWidth( width ) : -1, aStroke.GetPlotStyle(),
71 aStroke.GetColor() );
72}
73
74
76 const IMPORTED_STROKE& aStroke )
77{
78 VECTOR2I pt0 = MapCoordinate( aStart );
79 VECTOR2I pt1 = MapCoordinate( aEnd );
80
81 // Skip 0 len lines:
82 if( pt0 == pt1 )
83 return;
84
85 std::unique_ptr<LIB_SHAPE> line = std::make_unique<LIB_SHAPE>( m_symbol, SHAPE_T::POLY );
86 line->SetUnit( m_unit );
87 line->SetStroke( MapStrokeParams( aStroke ) );
88
89 line->AddPoint( pt0 );
90 line->AddPoint( pt1 );
91
92 addItem( std::move( line ) );
93}
94
95
96void GRAPHICS_IMPORTER_LIB_SYMBOL::AddCircle( const VECTOR2D& aCenter, double aRadius,
97 const IMPORTED_STROKE& aStroke, bool aFilled,
98 const COLOR4D& aFillColor )
99{
100 std::unique_ptr<LIB_SHAPE> circle = std::make_unique<LIB_SHAPE>( m_symbol, SHAPE_T::CIRCLE );
101 circle->SetUnit( m_unit );
102 circle->SetFillColor( aFillColor );
103 circle->SetFilled( aFilled );
104 circle->SetStroke( MapStrokeParams( aStroke ) );
105 circle->SetStart( MapCoordinate( aCenter ) );
106 circle->SetEnd( MapCoordinate( VECTOR2D( aCenter.x + aRadius, aCenter.y ) ) );
107
108 addItem( std::move( circle ) );
109}
110
111
112void GRAPHICS_IMPORTER_LIB_SYMBOL::AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart,
113 const EDA_ANGLE& aAngle, const IMPORTED_STROKE& aStroke )
114{
115 std::unique_ptr<LIB_SHAPE> arc = std::make_unique<LIB_SHAPE>( m_symbol, SHAPE_T::ARC );
116 arc->SetUnit( m_unit );
117
122 VECTOR2D end = aStart;
123 VECTOR2D mid = aStart;
124
125 RotatePoint( end, aCenter, -aAngle );
126 RotatePoint( mid, aCenter, -aAngle / 2.0 );
127
128 arc->SetArcGeometry( MapCoordinate( aStart ), MapCoordinate( mid ), MapCoordinate( end ) );
129
130 // Ensure the arc can be handled by KiCad. Arcs with a too big radius cannot.
131 // The criteria used here is radius < MAX_INT / 2.
132 // this is not perfect, but we do not know the exact final position of the arc, so
133 // we cannot test the coordinate values, because the arc can be moved before being placed.
134 VECTOR2D center = CalcArcCenter( arc->GetStart(), arc->GetEnd(), aAngle );
135 double radius = ( center - arc->GetStart() ).EuclideanNorm();
136 constexpr double rd_max_value = std::numeric_limits<VECTOR2I::coord_type>::max() / 2.0;
137
138 if( radius >= rd_max_value )
139 {
140 // Arc cannot be handled: convert it to a segment
141 AddLine( aStart, end, aStroke );
142 return;
143 }
144
145 arc->SetStroke( MapStrokeParams( aStroke ) );
146
147 addItem( std::move( arc ) );
148}
149
150
151void GRAPHICS_IMPORTER_LIB_SYMBOL::AddPolygon( const std::vector<VECTOR2D>& aVertices,
152 const IMPORTED_STROKE& aStroke, bool aFilled,
153 const COLOR4D& aFillColor )
154{
155 std::vector<VECTOR2I> convertedPoints;
156 convertedPoints.reserve( aVertices.size() );
157
158 for( const VECTOR2D& precisePoint : aVertices )
159 convertedPoints.emplace_back( MapCoordinate( precisePoint ) );
160
161 if( convertedPoints.empty() )
162 return;
163
164 std::unique_ptr<LIB_SHAPE> polygon = std::make_unique<LIB_SHAPE>( m_symbol, SHAPE_T::POLY );
165 polygon->SetUnit( m_unit );
166
167 if( aFilled )
168 {
169 polygon->SetFillMode( aFillColor != COLOR4D::UNSPECIFIED ? FILL_T::FILLED_WITH_COLOR
170 : FILL_T::FILLED_SHAPE );
171 }
172
173 polygon->SetFillColor( aFillColor );
174 polygon->SetPolyPoints( convertedPoints );
175 polygon->AddPoint( convertedPoints[0] ); // Need to close last point for libedit
176
177 polygon->SetStroke( MapStrokeParams( aStroke ) );
178
179 addItem( std::move( polygon ) );
180}
181
182
183void GRAPHICS_IMPORTER_LIB_SYMBOL::AddText( const VECTOR2D& aOrigin, const wxString& aText,
184 double aHeight, double aWidth, double aThickness,
185 double aOrientation, GR_TEXT_H_ALIGN_T aHJustify,
186 GR_TEXT_V_ALIGN_T aVJustify, const COLOR4D& aColor )
187{
188 std::unique_ptr<LIB_TEXT> textItem = std::make_unique<LIB_TEXT>( m_symbol );
189 textItem->SetUnit( m_unit );
190 textItem->SetTextColor( aColor );
191 textItem->SetTextThickness( MapLineWidth( aThickness ) );
192 textItem->SetTextPos( MapCoordinate( aOrigin ) );
193 textItem->SetTextAngle( EDA_ANGLE( aOrientation, DEGREES_T ) );
194 textItem->SetTextWidth( aWidth * ImportScalingFactor().x );
195 textItem->SetTextHeight( aHeight * ImportScalingFactor().y );
196 textItem->SetVertJustify( aVJustify );
197 textItem->SetHorizJustify( aHJustify );
198 textItem->SetText( aText );
199
200 addItem( std::move( textItem ) );
201}
202
203
205 const VECTOR2D& aBezierControl1,
206 const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
207 const IMPORTED_STROKE& aStroke )
208{
209 std::unique_ptr<LIB_SHAPE> spline = std::make_unique<LIB_SHAPE>( m_symbol, SHAPE_T::BEZIER );
210 spline->SetUnit( m_unit );
211 spline->SetStroke( MapStrokeParams( aStroke ) );
212 spline->SetStart( MapCoordinate( aStart ) );
213 spline->SetBezierC1( MapCoordinate( aBezierControl1 ) );
214 spline->SetBezierC2( MapCoordinate( aBezierControl2 ) );
215 spline->SetEnd( MapCoordinate( aEnd ) );
216 spline->RebuildBezierToSegmentsPointsList( aStroke.GetWidth() );
217
218 // If the spline is degenerated (i.e. a segment) add it as segment or discard it if
219 // null (i.e. very small) length
220 if( spline->GetBezierPoints().size() <= 2 )
221 {
222 spline->SetShape( SHAPE_T::SEGMENT );
223 int dist = VECTOR2I( spline->GetStart() - spline->GetEnd() ).EuclideanNorm();
224
225// segment smaller than MIN_SEG_LEN_ACCEPTABLE_NM nanometers are skipped.
226#define MIN_SEG_LEN_ACCEPTABLE_NM 20
227 if( dist < MIN_SEG_LEN_ACCEPTABLE_NM )
228 return;
229 }
230
231 addItem( std::move( spline ) );
232}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:111
VECTOR2I MapCoordinate(const VECTOR2D &aCoordinate)
Convert an imported coordinate to a board coordinate, according to the internal units,...
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.
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.
STROKE_PARAMS MapStrokeParams(const IMPORTED_STROKE &aStroke)
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.
void AddLine(const VECTOR2D &aStart, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke) override
Create an object representing a line segment.
void AddArc(const VECTOR2D &aCenter, const VECTOR2D &aStart, const EDA_ANGLE &aAngle, 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.
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.
GRAPHICS_IMPORTER_LIB_SYMBOL(LIB_SYMBOL *aSymbol, int aUnit)
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
Offset (in mm) for imported coordinates.
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
Define a library symbol object.
Definition: lib_symbol.h:99
Simple container to manage line stroke parameters.
Definition: stroke_params.h:81
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:265
@ DEGREES_T
Definition: eda_angle.h:31
#define MIN_SEG_LEN_ACCEPTABLE_NM
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:426
const int scale
constexpr int mmToIU(double mm) const
Definition: base_units.h:89
GR_TEXT_H_ALIGN_T
GR_TEXT_V_ALIGN_T
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:228
const VECTOR2I CalcArcCenter(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Determine the center of an arc or circle given three points on its circumference.
Definition: trigo.cpp:520
double EuclideanNorm(const VECTOR2I &vector)
Definition: trigo.h:128
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85
VECTOR2< double > VECTOR2D
Definition: vector2d.h:587
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588