KiCad PCB EDA Suite
Loading...
Searching...
No Matches
graphics_importer_sch.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 <memory>
29#include <tuple>
30
31#include <sch_line.h>
32#include <sch_shape.h>
33#include <sch_text.h>
34
35
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
75void GRAPHICS_IMPORTER_SCH::AddLine( const VECTOR2D& aStart, const VECTOR2D& aEnd,
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<SCH_LINE> line = std::make_unique<SCH_LINE>();
86 line->SetStroke( MapStrokeParams( aStroke ) );
87
88 line->SetStartPoint( pt0 );
89 line->SetEndPoint( pt1 );
90
91 addItem( std::move( line ) );
92}
93
94
95void GRAPHICS_IMPORTER_SCH::AddCircle( const VECTOR2D& aCenter, double aRadius,
96 const IMPORTED_STROKE& aStroke, bool aFilled,
97 const COLOR4D& aFillColor )
98{
99 std::unique_ptr<SCH_SHAPE> circle = std::make_unique<SCH_SHAPE>( SHAPE_T::CIRCLE );
100 circle->SetFillColor( aFillColor );
101 circle->SetFilled( aFilled );
102 circle->SetStroke( MapStrokeParams( aStroke ) );
103 circle->SetStart( MapCoordinate( aCenter ) );
104 circle->SetEnd( MapCoordinate( VECTOR2D( aCenter.x + aRadius, aCenter.y ) ) );
105
106 addItem( std::move( circle ) );
107}
108
109
110void GRAPHICS_IMPORTER_SCH::AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart,
111 const EDA_ANGLE& aAngle, const IMPORTED_STROKE& aStroke )
112{
113 std::unique_ptr<SCH_SHAPE> arc = std::make_unique<SCH_SHAPE>( SHAPE_T::ARC );
114
119 VECTOR2D end = aStart;
120 VECTOR2D mid = aStart;
121
122 RotatePoint( end, aCenter, -aAngle );
123 RotatePoint( mid, aCenter, -aAngle / 2.0 );
124
125 arc->SetArcGeometry( MapCoordinate( aStart ), MapCoordinate( mid ), MapCoordinate( end ) );
126
127 // Ensure the arc can be handled by KiCad. Arcs with a too big radius cannot.
128 // The criteria used here is radius < MAX_INT / 2.
129 // this is not perfect, but we do not know the exact final position of the arc, so
130 // we cannot test the coordinate values, because the arc can be moved before being placed.
131 VECTOR2D center = CalcArcCenter( arc->GetStart(), arc->GetEnd(), aAngle );
132 double radius = ( center - arc->GetStart() ).EuclideanNorm();
133 constexpr double rd_max_value = std::numeric_limits<VECTOR2I::coord_type>::max() / 2.0;
134
135 if( radius >= rd_max_value )
136 {
137 // Arc cannot be handled: convert it to a segment
138 AddLine( aStart, end, aStroke );
139 return;
140 }
141
142 arc->SetStroke( MapStrokeParams( aStroke ) );
143
144 addItem( std::move( arc ) );
145}
146
147
148void GRAPHICS_IMPORTER_SCH::AddPolygon( const std::vector<VECTOR2D>& aVertices,
149 const IMPORTED_STROKE& aStroke, bool aFilled,
150 const COLOR4D& aFillColor )
151{
152 std::vector<VECTOR2I> convertedPoints;
153 convertedPoints.reserve( aVertices.size() );
154
155 for( const VECTOR2D& precisePoint : aVertices )
156 convertedPoints.emplace_back( MapCoordinate( precisePoint ) );
157
158 if( convertedPoints.empty() )
159 return;
160
161 std::unique_ptr<SCH_SHAPE> polygon = std::make_unique<SCH_SHAPE>( SHAPE_T::POLY );
162
163 if( aFilled )
164 {
165 polygon->SetFillMode( aFillColor != COLOR4D::UNSPECIFIED ? FILL_T::FILLED_WITH_COLOR
166 : FILL_T::FILLED_SHAPE );
167 }
168
169 polygon->SetFillColor( aFillColor );
170 polygon->SetPolyPoints( convertedPoints );
171 polygon->AddPoint( convertedPoints[0] ); // Need to close last point for libedit
172
173 polygon->SetStroke( MapStrokeParams( aStroke ) );
174
175 addItem( std::move( polygon ) );
176}
177
178
179void GRAPHICS_IMPORTER_SCH::AddText( const VECTOR2D& aOrigin, const wxString& aText,
180 double aHeight, double aWidth, double aThickness,
181 double aOrientation, GR_TEXT_H_ALIGN_T aHJustify,
182 GR_TEXT_V_ALIGN_T aVJustify, const COLOR4D& aColor )
183{
184 std::unique_ptr<SCH_TEXT> textItem = std::make_unique<SCH_TEXT>();
185 textItem->SetTextColor( aColor );
186 textItem->SetTextThickness( MapLineWidth( aThickness ) );
187 textItem->SetTextPos( MapCoordinate( aOrigin ) );
188 textItem->SetTextAngle( EDA_ANGLE( aOrientation, DEGREES_T ) );
189 textItem->SetTextWidth( aWidth * ImportScalingFactor().x );
190 textItem->SetTextHeight( aHeight * ImportScalingFactor().y );
191 textItem->SetVertJustify( aVJustify );
192 textItem->SetHorizJustify( aHJustify );
193 textItem->SetText( aText );
194
195 addItem( std::move( textItem ) );
196}
197
198
200 const VECTOR2D& aBezierControl1,
201 const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
202 const IMPORTED_STROKE& aStroke )
203{
204 std::unique_ptr<SCH_SHAPE> spline = std::make_unique<SCH_SHAPE>( SHAPE_T::BEZIER );
205 spline->SetStroke( MapStrokeParams( aStroke ) );
206 spline->SetStart( MapCoordinate( aStart ) );
207 spline->SetBezierC1( MapCoordinate( aBezierControl1 ) );
208 spline->SetBezierC2( MapCoordinate( aBezierControl2 ) );
209 spline->SetEnd( MapCoordinate( aEnd ) );
210 spline->RebuildBezierToSegmentsPointsList( aStroke.GetWidth() / 2 );
211
212 // If the spline is degenerated (i.e. a segment) add it as segment or discard it if
213 // null (i.e. very small) length
214 if( spline->GetBezierPoints().size() <= 2 )
215 {
216 spline->SetShape( SHAPE_T::SEGMENT );
217 int dist = VECTOR2I( spline->GetStart() - spline->GetEnd() ).EuclideanNorm();
218
219// segment smaller than MIN_SEG_LEN_ACCEPTABLE_NM nanometers are skipped.
220#define MIN_SEG_LEN_ACCEPTABLE_NM 20
221 if( dist < MIN_SEG_LEN_ACCEPTABLE_NM )
222 return;
223 }
224
225 addItem( std::move( spline ) );
226}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:110
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
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 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.
VECTOR2I MapCoordinate(const VECTOR2D &aCoordinate)
Convert an imported coordinate to a board coordinate, according to the internal units,...
STROKE_PARAMS MapStrokeParams(const IMPORTED_STROKE &aStroke)
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 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 AddSpline(const VECTOR2D &aStart, const VECTOR2D &aBezierControl1, const VECTOR2D &aBezierControl2, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke) override
Create an object representing an arc.
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
Simple container to manage line stroke parameters.
Definition: stroke_params.h:79
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:283
@ DEGREES_T
Definition: eda_angle.h:31
#define MIN_SEG_LEN_ACCEPTABLE_NM
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:390
const int scale
constexpr int mmToIU(double mm) const
Definition: base_units.h:88
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:229
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:521
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690