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 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, see <https://www.gnu.org/licenses/>.
20 */
21
23
24#include <memory>
25#include <tuple>
26
27#include <sch_line.h>
28#include <sch_shape.h>
29#include <sch_text.h>
30
31
36
37
39{
40 VECTOR2D coord = aCoordinate;
41 coord *= GetScale();
42 coord += GetImportOffsetMM();
43 coord *= GetMillimeterToIuFactor();
44
45 return KiROUND( coord );
46}
47
48
50{
52 double scale = ( std::abs( factor.x ) + std::abs( factor.y ) ) * 0.5;
53
54 if( aLineWidth <= 0.0 )
55 return int( GetLineWidthMM() * scale );
56
57 // aLineWidth is in mm:
58 return int( aLineWidth * scale );
59}
60
61
63{
64 // Historicaly -1 meant no-stroke in Eeschema.
65 int width = ( aStroke.GetWidth() == -1 ) ? -1 : MapLineWidth( aStroke.GetWidth() );
66
67 return STROKE_PARAMS( width, aStroke.GetPlotStyle(), aStroke.GetColor() );
68}
69
70
71void GRAPHICS_IMPORTER_SCH::AddLine( const VECTOR2D& aStart, const VECTOR2D& aEnd,
72 const IMPORTED_STROKE& aStroke )
73{
74 VECTOR2I pt0 = MapCoordinate( aStart );
75 VECTOR2I pt1 = MapCoordinate( aEnd );
76
77 // Skip 0 len lines:
78 if( pt0 == pt1 )
79 return;
80
81 std::unique_ptr<SCH_LINE> line = std::make_unique<SCH_LINE>();
82 line->SetStroke( MapStrokeParams( aStroke ) );
83
84 line->SetStartPoint( pt0 );
85 line->SetEndPoint( pt1 );
86
87 addItem( std::move( line ) );
88}
89
90
91void GRAPHICS_IMPORTER_SCH::AddCircle( const VECTOR2D& aCenter, double aRadius,
92 const IMPORTED_STROKE& aStroke, bool aFilled,
93 const COLOR4D& aFillColor )
94{
95 std::unique_ptr<SCH_SHAPE> circle = std::make_unique<SCH_SHAPE>( SHAPE_T::CIRCLE );
96 circle->SetFillColor( aFillColor );
97 circle->SetFilled( aFilled );
98 circle->SetStroke( MapStrokeParams( aStroke ) );
99 circle->SetStart( MapCoordinate( aCenter ) );
100 circle->SetEnd( MapCoordinate( VECTOR2D( aCenter.x + aRadius, aCenter.y ) ) );
101
102 addItem( std::move( circle ) );
103}
104
105
106void GRAPHICS_IMPORTER_SCH::AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart,
107 const EDA_ANGLE& aAngle, const IMPORTED_STROKE& aStroke )
108{
109 std::unique_ptr<SCH_SHAPE> arc = std::make_unique<SCH_SHAPE>( SHAPE_T::ARC );
110
115 VECTOR2D end = aStart;
116 VECTOR2D mid = aStart;
117
118 RotatePoint( end, aCenter, -aAngle );
119 RotatePoint( mid, aCenter, -aAngle / 2.0 );
120
121 arc->SetArcGeometry( MapCoordinate( aStart ), MapCoordinate( mid ), MapCoordinate( end ) );
122
123 // Ensure the arc can be handled by KiCad. Arcs with a too big radius cannot.
124 // The criteria used here is radius < MAX_INT / 2.
125 // this is not perfect, but we do not know the exact final position of the arc, so
126 // we cannot test the coordinate values, because the arc can be moved before being placed.
127 VECTOR2D center = CalcArcCenter( arc->GetStart(), arc->GetEnd(), aAngle );
128 double radius = ( center - arc->GetStart() ).EuclideanNorm();
129 constexpr double rd_max_value = std::numeric_limits<VECTOR2I::coord_type>::max() / 2.0;
130
131 if( radius >= rd_max_value )
132 {
133 // Arc cannot be handled: convert it to a segment
134 AddLine( aStart, end, aStroke );
135 return;
136 }
137
138 arc->SetStroke( MapStrokeParams( aStroke ) );
139
140 addItem( std::move( arc ) );
141}
142
143
144void GRAPHICS_IMPORTER_SCH::AddPolygon( const std::vector<VECTOR2D>& aVertices,
145 const IMPORTED_STROKE& aStroke, bool aFilled,
146 const COLOR4D& aFillColor )
147{
148 std::vector<VECTOR2I> convertedPoints;
149 convertedPoints.reserve( aVertices.size() );
150
151 for( const VECTOR2D& precisePoint : aVertices )
152 convertedPoints.emplace_back( MapCoordinate( precisePoint ) );
153
154 if( convertedPoints.empty() )
155 return;
156
157 std::unique_ptr<SCH_SHAPE> polygon = std::make_unique<SCH_SHAPE>( SHAPE_T::POLY );
158
159 if( aFilled )
160 {
161 polygon->SetFillMode( aFillColor != COLOR4D::UNSPECIFIED ? FILL_T::FILLED_WITH_COLOR
163 }
164
165 polygon->SetFillColor( aFillColor );
166 polygon->SetPolyPoints( convertedPoints );
167 polygon->AddPoint( convertedPoints[0] ); // Need to close last point for libedit
168
169 polygon->SetStroke( MapStrokeParams( aStroke ) );
170
171 addItem( std::move( polygon ) );
172}
173
174
175void GRAPHICS_IMPORTER_SCH::AddText( const VECTOR2D& aOrigin, const wxString& aText,
176 double aHeight, double aWidth, double aThickness,
177 double aOrientation, GR_TEXT_H_ALIGN_T aHJustify,
178 GR_TEXT_V_ALIGN_T aVJustify, const COLOR4D& aColor )
179{
180 std::unique_ptr<SCH_TEXT> textItem = std::make_unique<SCH_TEXT>();
181 textItem->SetTextColor( aColor );
182 textItem->SetTextThickness( MapLineWidth( aThickness ) );
183 textItem->SetTextPos( MapCoordinate( aOrigin ) );
184 textItem->SetTextAngle( EDA_ANGLE( aOrientation, DEGREES_T ) );
185 textItem->SetTextWidth( aWidth * ImportScalingFactor().x );
186 textItem->SetTextHeight( aHeight * ImportScalingFactor().y );
187 textItem->SetVertJustify( aVJustify );
188 textItem->SetHorizJustify( aHJustify );
189 textItem->SetText( aText );
190
191 addItem( std::move( textItem ) );
192}
193
194
195void GRAPHICS_IMPORTER_SCH::AddSpline( const VECTOR2D& aStart, const VECTOR2D& aBezierControl1,
196 const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
197 const IMPORTED_STROKE& aStroke )
198{
199 std::unique_ptr<SCH_SHAPE> spline = std::make_unique<SCH_SHAPE>( SHAPE_T::BEZIER );
200 spline->SetStroke( MapStrokeParams( aStroke ) );
201 spline->SetStart( MapCoordinate( aStart ) );
202 spline->SetBezierC1( MapCoordinate( aBezierControl1 ) );
203 spline->SetBezierC2( MapCoordinate( aBezierControl2 ) );
204 spline->SetEnd( MapCoordinate( aEnd ) );
205
206 if( setupSplineOrLine( *spline, aStroke.GetWidth() / 2 ) )
207 {
208 // SCH_LINES aren't SCH_SHAPES
209 if( spline->GetShape() == SHAPE_T::SEGMENT )
210 AddLine( aStart, aEnd, aStroke );
211 else
212 addItem( std::move( spline ) );
213 }
214}
215
216
217void GRAPHICS_IMPORTER_SCH::AddEllipse( const VECTOR2D& aCenter, double aMajorRadius, double aMinorRadius,
218 const EDA_ANGLE& aRotation, const IMPORTED_STROKE& aStroke, bool aFilled,
219 const COLOR4D& aFillColor )
220{
221 std::unique_ptr<SCH_SHAPE> ellipse = std::make_unique<SCH_SHAPE>( SHAPE_T::ELLIPSE );
222 ellipse->SetStroke( MapStrokeParams( aStroke ) );
223 ellipse->SetFilled( aFilled );
224 ellipse->SetFillColor( aFillColor );
225
226 ellipse->SetEllipseCenter( MapCoordinate( aCenter ) );
227 ellipse->SetEllipseMajorRadius( KiROUND( aMajorRadius * ImportScalingFactor().x ) );
228 ellipse->SetEllipseMinorRadius( KiROUND( aMinorRadius * ImportScalingFactor().y ) );
229 ellipse->SetEllipseRotation( aRotation );
230
231 addItem( std::move( ellipse ) );
232}
233
234
235void GRAPHICS_IMPORTER_SCH::AddEllipseArc( const VECTOR2D& aCenter, double aMajorRadius, double aMinorRadius,
236 const EDA_ANGLE& aRotation, const EDA_ANGLE& aStartAngle,
237 const EDA_ANGLE& aEndAngle, const IMPORTED_STROKE& aStroke )
238{
239 std::unique_ptr<SCH_SHAPE> arc = std::make_unique<SCH_SHAPE>( SHAPE_T::ELLIPSE_ARC );
240 arc->SetStroke( MapStrokeParams( aStroke ) );
241
242 arc->SetEllipseCenter( MapCoordinate( aCenter ) );
243 arc->SetEllipseMajorRadius( KiROUND( aMajorRadius * ImportScalingFactor().x ) );
244 arc->SetEllipseMinorRadius( KiROUND( aMinorRadius * ImportScalingFactor().y ) );
245 arc->SetEllipseRotation( aRotation );
246 arc->SetEllipseStartAngle( aStartAngle );
247 arc->SetEllipseEndAngle( aEndAngle );
248
249 addItem( std::move( arc ) );
250}
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:398
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.
void AddEllipse(const VECTOR2D &aCenter, double aMajorRadius, double aMinorRadius, const EDA_ANGLE &aRotation, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor=COLOR4D::UNSPECIFIED) override
Create an object representing a closed ellipse.
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 AddEllipseArc(const VECTOR2D &aCenter, double aMajorRadius, double aMinorRadius, const EDA_ANGLE &aRotation, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aEndAngle, const IMPORTED_STROKE &aStroke) override
Create an object representing an elliptical 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 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 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:101
Simple container to manage line stroke parameters.
@ DEGREES_T
Definition eda_angle.h:31
@ ELLIPSE
Definition eda_shape.h:52
@ SEGMENT
Definition eda_shape.h:46
@ ELLIPSE_ARC
Definition eda_shape.h:53
@ FILLED_WITH_COLOR
Definition eda_shape.h:63
@ FILLED_SHAPE
Fill with object color.
Definition eda_shape.h:61
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
const int scale
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:225
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:530
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682