KiCad PCB EDA Suite
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 <[email protected]>
6  * Copyright (C) 2018 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 <fp_shape.h>
30 #include <pcb_text.h>
31 #include <fp_text.h>
32 #include <memory>
33 #include <tuple>
34 
35 #include "convert_to_biu.h"
36 
37 
39 {
42 }
43 
44 
46 {
47  VECTOR2D coord = ( aCoordinate + GetImportOffsetMM() ) * ImportScalingFactor();
48  return wxPoint( KiROUND( coord.x ), KiROUND( coord.y ) );
49 }
50 
51 
53 {
54  if( aLineWidth <= 0.0 )
55  return int( GetLineWidthMM() * ImportScalingFactor() );
56 
57  // aLineWidth is in mm:
58  return int( aLineWidth * ImportScalingFactor() );
59 }
60 
61 
62 void GRAPHICS_IMPORTER_PCBNEW::AddLine( const VECTOR2D& aOrigin, const VECTOR2D& aEnd, double aWidth )
63 {
64  std::unique_ptr<PCB_SHAPE> line( createDrawing() );
65  line->SetShape( SHAPE_T::SEGMENT );
66  line->SetLayer( GetLayer() );
67  line->SetWidth( MapLineWidth( aWidth ) );
68  line->SetStart( MapCoordinate( aOrigin ) );
69  line->SetEnd( MapCoordinate( aEnd ) );
70 
71  if( line->Type() == PCB_FP_SHAPE_T )
72  static_cast<FP_SHAPE*>( line.get() )->SetLocalCoord();
73 
74  addItem( std::move( line ) );
75 }
76 
77 
78 void GRAPHICS_IMPORTER_PCBNEW::AddCircle( const VECTOR2D& aCenter, double aRadius, double aWidth, bool aFilled )
79 {
80  std::unique_ptr<PCB_SHAPE> circle( createDrawing() );
81  circle->SetShape( SHAPE_T::CIRCLE );
82  circle->SetFilled( aFilled );
83  circle->SetLayer( GetLayer() );
84  circle->SetWidth( MapLineWidth( aWidth ) );
85  circle->SetStart( MapCoordinate( aCenter ));
86  circle->SetEnd( MapCoordinate( VECTOR2D( aCenter.x + aRadius, aCenter.y ) ) );
87 
88  if( circle->Type() == PCB_FP_SHAPE_T )
89  static_cast<FP_SHAPE*>( circle.get() )->SetLocalCoord();
90 
91  addItem( std::move( circle ) );
92 }
93 
94 
95 void GRAPHICS_IMPORTER_PCBNEW::AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart,
96  double aAngle, double aWidth )
97 {
98  std::unique_ptr<PCB_SHAPE> arc( createDrawing() );
99  arc->SetShape( SHAPE_T::ARC );
100  arc->SetLayer( GetLayer() );
101 
106  VECTOR2D end = aStart - aCenter;
107  VECTOR2D mid = aStart - aCenter;
108 
109  end = aCenter + end.Rotate( DEG2RAD( aAngle ) );
110  mid = aCenter + mid.Rotate( DEG2RAD( aAngle / 2.0 ) );
111 
112  arc->SetArcGeometry( MapCoordinate( aStart ), MapCoordinate( mid ), MapCoordinate( end ) );
113 
114  arc->SetWidth( MapLineWidth( aWidth ) );
115 
116  if( arc->Type() == PCB_FP_SHAPE_T )
117  static_cast<FP_SHAPE*>( arc.get() )->SetLocalCoord();
118 
119  addItem( std::move( arc ) );
120 }
121 
122 
123 void GRAPHICS_IMPORTER_PCBNEW::AddPolygon( const std::vector< VECTOR2D >& aVertices, double aWidth )
124 {
125  std::vector< wxPoint > convertedPoints;
126  convertedPoints.reserve( convertedPoints.size() );
127 
128  for( const VECTOR2D& precisePoint : aVertices )
129  convertedPoints.emplace_back( MapCoordinate( precisePoint ) );
130 
131  std::unique_ptr<PCB_SHAPE> polygon( createDrawing() );
132  polygon->SetShape( SHAPE_T::POLY );
133  polygon->SetFilled( GetLayer() != Edge_Cuts );
134  polygon->SetLayer( GetLayer() );
135  polygon->SetPolyPoints( convertedPoints );
136 
137  if( polygon->Type() == PCB_FP_SHAPE_T )
138  static_cast<FP_SHAPE*>( polygon.get() )->SetLocalCoord();
139 
140  polygon->SetWidth( MapLineWidth( aWidth ) );
141  addItem( std::move( polygon ) );
142 }
143 
144 
145 void GRAPHICS_IMPORTER_PCBNEW::AddText( const VECTOR2D& aOrigin, const wxString& aText,
146  double aHeight, double aWidth, double aThickness, double aOrientation,
147  EDA_TEXT_HJUSTIFY_T aHJustify, EDA_TEXT_VJUSTIFY_T aVJustify )
148 {
149  std::unique_ptr<BOARD_ITEM> boardItem;
150  EDA_TEXT* textItem;
151  tie( boardItem, textItem ) = createText();
152  boardItem->SetLayer( GetLayer() );
153  textItem->SetTextThickness( MapLineWidth( aThickness ) );
154  textItem->SetTextPos( MapCoordinate( aOrigin ) );
155  textItem->SetTextAngle( aOrientation * 10.0 ); // Pcbnew uses the decidegree
156  textItem->SetTextWidth( aWidth * ImportScalingFactor() );
157  textItem->SetTextHeight( aHeight * ImportScalingFactor() );
158  textItem->SetVertJustify( aVJustify );
159  textItem->SetHorizJustify( aHJustify );
160  textItem->SetText( aText );
161 
162  if( boardItem->Type() == PCB_FP_TEXT_T )
163  static_cast<FP_TEXT*>( boardItem.get() )->SetLocalCoord();
164 
165  addItem( std::move( boardItem ) );
166 }
167 
168 
169 void GRAPHICS_IMPORTER_PCBNEW::AddSpline( const VECTOR2D& aStart, const VECTOR2D& BezierControl1,
170  const VECTOR2D& BezierControl2, const VECTOR2D& aEnd, double aWidth )
171 {
172  std::unique_ptr<PCB_SHAPE> spline( createDrawing() );
173  spline->SetShape( SHAPE_T::BEZIER );
174  spline->SetLayer( GetLayer() );
175  spline->SetWidth( MapLineWidth( aWidth ) );
176  spline->SetStart( MapCoordinate( aStart ) );
177  spline->SetBezierC1( MapCoordinate( BezierControl1 ));
178  spline->SetBezierC2( MapCoordinate( BezierControl2 ));
179  spline->SetEnd( MapCoordinate( aEnd ) );
180  spline->RebuildBezierToSegmentsPointsList( aWidth );
181 
182  if( spline->Type() == PCB_FP_SHAPE_T )
183  static_cast<FP_SHAPE*>( spline.get() )->SetLocalCoord();
184 
185  addItem( std::move( spline ) );
186 }
187 
188 
189 std::unique_ptr<PCB_SHAPE> GRAPHICS_IMPORTER_BOARD::createDrawing()
190 {
191  return std::make_unique<PCB_SHAPE>( m_board );
192 }
193 
194 
195 std::pair<std::unique_ptr<BOARD_ITEM>, EDA_TEXT*> GRAPHICS_IMPORTER_BOARD::createText()
196 {
197  PCB_TEXT* text = new PCB_TEXT( m_board );
198  return make_pair( std::unique_ptr<BOARD_ITEM>( text ), static_cast<EDA_TEXT*>( text ) );
199 }
200 
201 
202 std::unique_ptr<PCB_SHAPE> GRAPHICS_IMPORTER_FOOTPRINT::createDrawing()
203 {
204  return std::make_unique<FP_SHAPE>( m_footprint );
205 }
206 
207 
208 std::pair<std::unique_ptr<BOARD_ITEM>, EDA_TEXT*> GRAPHICS_IMPORTER_FOOTPRINT::createText()
209 {
210  FP_TEXT* text = new FP_TEXT( m_footprint );
211  return make_pair( std::unique_ptr<BOARD_ITEM>( text ), static_cast<EDA_TEXT*>( text ) );
212 }
std::pair< std::unique_ptr< BOARD_ITEM >, EDA_TEXT * > createText() override
Target layer for the imported shapes.
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:82
void AddCircle(const VECTOR2D &aOrigin, double aRadius, double aWidth, bool aFilled) override
Create an object representing a circle.
double ImportScalingFactor() const
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
PCB_LAYER_ID GetLayer() const
Return the target layer for the imported shapes.
virtual std::unique_ptr< PCB_SHAPE > createDrawing()=0
< Create an object representing a graphical shape.
void AddArc(const VECTOR2D &aCenter, const VECTOR2D &aStart, double aAngle, double aWidth) override
Create an object representing an arc.
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:267
std::pair< std::unique_ptr< BOARD_ITEM >, EDA_TEXT * > createText() override
Target layer for the imported shapes.
std::unique_ptr< PCB_SHAPE > createDrawing() override
< Create an object representing a graphical shape.
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
wxPoint MapCoordinate(const VECTOR2D &aCoordinate)
Convert an imported coordinate to a board coordinate, according to the internal units,...
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:140
double m_millimeterToIu
Offset (in mm) for imported coordinates.
int MapLineWidth(double aLineWidth)
If aLineWidth < 0, the default line thickness value is returned.
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:124
VECTOR2< double > VECTOR2D
Definition: vector2d.h:621
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:223
std::unique_ptr< PCB_SHAPE > createDrawing() override
< Create an object representing a graphical shape.
void AddSpline(const VECTOR2D &aStart, const VECTOR2D &aBezierControl1, const VECTOR2D &aBezierControl2, const VECTOR2D &aEnd, double aWidth) override
Create an object representing an arc.
void AddText(const VECTOR2D &aOrigin, const wxString &aText, double aHeight, double aWidth, double aThickness, double aOrientation, EDA_TEXT_HJUSTIFY_T aHJustify, EDA_TEXT_VJUSTIFY_T aVJustify) override
Create an object representing a text.
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:89
void SetTextWidth(int aWidth)
Definition: eda_text.h:261
VECTOR2< T > Rotate(double aAngle) const
Rotate the vector by a given angle.
Definition: vector2d.h:371
void addItem(std::unique_ptr< EDA_ITEM > aItem)
< Add an item to the imported shapes list.
double DEG2RAD(double deg)
Definition: trigo.h:229
double GetLineWidthMM() const
Return the line width used for importing the outlines (in mm).
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:222
void SetTextHeight(int aHeight)
Definition: eda_text.h:264
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:73
virtual std::pair< std::unique_ptr< BOARD_ITEM >, EDA_TEXT * > createText()=0
Target layer for the imported shapes.
const VECTOR2D & GetImportOffsetMM() const
void AddPolygon(const std::vector< VECTOR2D > &aVertices, double aWidth) override
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.h:180
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:188
void AddLine(const VECTOR2D &aOrigin, const VECTOR2D &aEnd, double aWidth) override
Create an object representing a line segment.
static constexpr int Millimeter2iu(double mm)