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 <maciej.suminski@cern.ch>
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 <class_board.h>
29 #include <class_edge_mod.h>
30 #include <class_pcb_text.h>
31 #include <class_text_mod.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( (int) coord.x, (int) 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<DRAWSEGMENT> line( createDrawing() );
65  line->SetShape( S_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_MODULE_EDGE_T )
72  static_cast<EDGE_MODULE*>( 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 )
79 {
80  std::unique_ptr<DRAWSEGMENT> circle( createDrawing() );
81  circle->SetShape( S_CIRCLE );
82  circle->SetLayer( GetLayer() );
83  circle->SetWidth( MapLineWidth( aWidth ) );
84  circle->SetCenter( MapCoordinate( aCenter ) );
85  circle->SetArcStart( MapCoordinate( VECTOR2D( aCenter.x + aRadius, aCenter.y ) ) );
86 
87  if( circle->Type() == PCB_MODULE_EDGE_T )
88  static_cast<EDGE_MODULE*>( circle.get() )->SetLocalCoord();
89 
90  addItem( std::move( circle ) );
91 }
92 
93 
94 void GRAPHICS_IMPORTER_PCBNEW::AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart,
95  double aAngle, double aWidth )
96 {
97  std::unique_ptr<DRAWSEGMENT> arc( createDrawing() );
98  arc->SetShape( S_ARC );
99  arc->SetLayer( GetLayer() );
100  arc->SetWidth( MapLineWidth( aWidth ) );
101  arc->SetCenter( MapCoordinate( aCenter) );
102  arc->SetArcStart( MapCoordinate( aStart ) );
103  arc->SetAngle( aAngle * 10.0 ); // Pcbnew uses the decidegree
104 
105  if( arc->Type() == PCB_MODULE_EDGE_T )
106  static_cast<EDGE_MODULE*>( arc.get() )->SetLocalCoord();
107 
108  addItem( std::move( arc ) );
109 }
110 
111 
112 void GRAPHICS_IMPORTER_PCBNEW::AddPolygon( const std::vector< VECTOR2D >& aVertices, double aWidth )
113 {
114  std::vector< wxPoint > convertedPoints;
115  convertedPoints.reserve( convertedPoints.size() );
116 
117  for( const VECTOR2D& precisePoint : aVertices )
118  convertedPoints.emplace_back( MapCoordinate( precisePoint ) );
119 
120  std::unique_ptr<DRAWSEGMENT> polygon( createDrawing() );
121  polygon->SetShape( S_POLYGON );
122  polygon->SetLayer( GetLayer() );
123  polygon->SetPolyPoints( convertedPoints );
124 
125  if( polygon->Type() == PCB_MODULE_EDGE_T )
126  static_cast<EDGE_MODULE*>( polygon.get() )->SetLocalCoord();
127 
128  polygon->SetWidth( MapLineWidth( aWidth ) );
129  addItem( std::move( polygon ) );
130 }
131 
132 
133 void GRAPHICS_IMPORTER_PCBNEW::AddText( const VECTOR2D& aOrigin, const wxString& aText,
134  double aHeight, double aWidth, double aThickness, double aOrientation,
135  EDA_TEXT_HJUSTIFY_T aHJustify, EDA_TEXT_VJUSTIFY_T aVJustify )
136 {
137  std::unique_ptr<BOARD_ITEM> boardItem;
138  EDA_TEXT* textItem;
139  tie( boardItem, textItem ) = createText();
140  boardItem->SetLayer( GetLayer() );
141  textItem->SetTextThickness( MapLineWidth( aThickness ) );
142  textItem->SetTextPos( MapCoordinate( aOrigin ) );
143  textItem->SetTextAngle( aOrientation * 10.0 ); // Pcbnew uses the decidegree
144  textItem->SetTextWidth( aWidth * ImportScalingFactor() );
145  textItem->SetTextHeight( aHeight * ImportScalingFactor() );
146  textItem->SetVertJustify( aVJustify );
147  textItem->SetHorizJustify( aHJustify );
148  textItem->SetText( aText );
149 
150  if( boardItem->Type() == PCB_MODULE_TEXT_T )
151  static_cast<TEXTE_MODULE*>( boardItem.get() )->SetLocalCoord();
152 
153  addItem( std::move( boardItem ) );
154 }
155 
156 
157 void GRAPHICS_IMPORTER_PCBNEW::AddSpline( const VECTOR2D& aStart, const VECTOR2D& BezierControl1,
158  const VECTOR2D& BezierControl2, const VECTOR2D& aEnd, double aWidth )
159 {
160  std::unique_ptr<DRAWSEGMENT> spline( createDrawing() );
161  spline->SetShape( S_CURVE );
162  spline->SetLayer( GetLayer() );
163  spline->SetWidth( MapLineWidth( aWidth ) );
164  spline->SetStart( MapCoordinate( aStart ) );
165  spline->SetBezControl1( MapCoordinate( BezierControl1 ) );
166  spline->SetBezControl2( MapCoordinate( BezierControl2 ) );
167  spline->SetEnd( MapCoordinate( aEnd ) );
168  spline->RebuildBezierToSegmentsPointsList( aWidth );
169 
170  if( spline->Type() == PCB_MODULE_EDGE_T )
171  static_cast<EDGE_MODULE*>( spline.get() )->SetLocalCoord();
172 
173  addItem( std::move( spline ) );
174 }
175 
176 
177 std::unique_ptr<DRAWSEGMENT> GRAPHICS_IMPORTER_BOARD::createDrawing()
178 {
179  return std::make_unique<DRAWSEGMENT>( m_board );
180 }
181 
182 
183 std::pair<std::unique_ptr<BOARD_ITEM>, EDA_TEXT*> GRAPHICS_IMPORTER_BOARD::createText()
184 {
185  TEXTE_PCB* text = new TEXTE_PCB( m_board );
186  return make_pair( std::unique_ptr<BOARD_ITEM>( text ), static_cast<EDA_TEXT*>( text ) );
187 }
188 
189 
190 std::unique_ptr<DRAWSEGMENT> GRAPHICS_IMPORTER_MODULE::createDrawing()
191 {
192  return std::unique_ptr<DRAWSEGMENT>( new EDGE_MODULE( m_module ) );
193 }
194 
195 
196 std::pair<std::unique_ptr<BOARD_ITEM>, EDA_TEXT*> GRAPHICS_IMPORTER_MODULE::createText()
197 {
198  TEXTE_MODULE* text = new TEXTE_MODULE( m_module );
199  return make_pair( std::unique_ptr<BOARD_ITEM>( text ), static_cast<EDA_TEXT*>( text ) );
200 }
std::pair< std::unique_ptr< BOARD_ITEM >, EDA_TEXT * > createText() override
Create an object representing a text.
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:48
double ImportScalingFactor() const
TEXTE_PCB class definition.
PCB_LAYER_ID GetLayer() const
Returns the target layer for the imported shapes.
void AddArc(const VECTOR2D &aCenter, const VECTOR2D &aStart, double aAngle, double aWidth) override
Create an object representing an arc.
polygon (not yet used for tracks, but could be in microwave apps)
std::unique_ptr< DRAWSEGMENT > createDrawing() override
Create an object representing a graphical shape.
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:247
usual segment : line with rounded ends
wxPoint MapCoordinate(const VECTOR2D &aCoordinate)
convert a imported coordinate to a board coordinate, according to the internal units,...
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
std::unique_ptr< DRAWSEGMENT > createDrawing() override
Create an object representing a graphical shape.
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:113
double m_millimeterToIu
factor to convert millimeters to Internal Units
int MapLineWidth(double aLineWidth)
Footprint text class description.
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:121
virtual std::unique_ptr< DRAWSEGMENT > createDrawing()=0
Create an object representing a graphical shape.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:203
Arcs (with rounded ends)
PCB_LAYER_ID m_layer
Target layer for the imported shapes.
Bezier Curve.
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:55
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
void SetTextWidth(int aWidth)
Definition: eda_text.h:241
void addItem(std::unique_ptr< EDA_ITEM > aItem)
Adds an item to the imported shapes list.
double GetLineWidthMM() const
Returns the line width used for importing the outlines (in mm).
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:202
std::pair< std::unique_ptr< BOARD_ITEM >, EDA_TEXT * > createText() override
Create an object representing a text.
void SetTextHeight(int aHeight)
Definition: eda_text.h:244
virtual std::pair< std::unique_ptr< BOARD_ITEM >, EDA_TEXT * > createText()=0
Create an object representing a text.
const VECTOR2D & GetImportOffsetMM() const
void AddPolygon(const std::vector< VECTOR2D > &aVertices, double aWidth) override
void AddCircle(const VECTOR2D &aOrigin, double aRadius, double aWidth) override
Create an object representing a circle.
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.h:158
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:167
void AddLine(const VECTOR2D &aOrigin, const VECTOR2D &aEnd, double aWidth) override
Create an object representing a line segment.
EDGE_MODULE class definition.
static constexpr int Millimeter2iu(double mm)