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 <tuple>
33 
34 #include "convert_to_biu.h"
35 
36 
38 {
40  m_millimeterToIu = Millimeter2iu( 1.0 );
41 }
42 
43 
45 {
46  VECTOR2D coord = ( aCoordinate + GetImportOffsetMM() ) * ImportScalingFactor();
47  return wxPoint( (int) coord.x, (int) coord.y );
48 }
49 
50 
52 {
53  if( aLineWidth <= 0.0 )
54  return int( GetLineWidthMM() * ImportScalingFactor() );
55 
56  // aLineWidth is in mm:
57  return int( aLineWidth * ImportScalingFactor() );
58 }
59 
60 
61 void GRAPHICS_IMPORTER_PCBNEW::AddLine( const VECTOR2D& aOrigin, const VECTOR2D& aEnd, double aWidth )
62 {
63  unique_ptr<DRAWSEGMENT> line( createDrawing() );
64  line->SetShape( S_SEGMENT );
65  line->SetLayer( GetLayer() );
66  line->SetWidth( MapLineWidth( aWidth ) );
67  line->SetStart( MapCoordinate( aOrigin ) );
68  line->SetEnd( MapCoordinate( aEnd ) );
69 
70  if( line->Type() == PCB_MODULE_EDGE_T )
71  static_cast<EDGE_MODULE*>( line.get() )->SetLocalCoord();
72 
73  addItem( std::move( line ) );
74 }
75 
76 
77 void GRAPHICS_IMPORTER_PCBNEW::AddCircle( const VECTOR2D& aCenter, double aRadius, double aWidth )
78 {
79  unique_ptr<DRAWSEGMENT> circle( createDrawing() );
80  circle->SetShape( S_CIRCLE );
81  circle->SetLayer( GetLayer() );
82  circle->SetWidth( MapLineWidth( aWidth ) );
83  circle->SetCenter( MapCoordinate( aCenter ) );
84  circle->SetArcStart( MapCoordinate( VECTOR2D( aCenter.x + aRadius, aCenter.y ) ) );
85 
86  if( circle->Type() == PCB_MODULE_EDGE_T )
87  static_cast<EDGE_MODULE*>( circle.get() )->SetLocalCoord();
88 
89  addItem( std::move( circle ) );
90 }
91 
92 
93 void GRAPHICS_IMPORTER_PCBNEW::AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart, double aAngle, double aWidth )
94 {
95  unique_ptr<DRAWSEGMENT> arc( createDrawing() );
96  arc->SetShape( S_ARC );
97  arc->SetLayer( GetLayer() );
98  arc->SetWidth( MapLineWidth( aWidth ) );
99  arc->SetCenter( MapCoordinate( aCenter) );
100  arc->SetArcStart( MapCoordinate( aStart ) );
101  arc->SetAngle( aAngle );
102 
103  if( arc->Type() == PCB_MODULE_EDGE_T )
104  static_cast<EDGE_MODULE*>( arc.get() )->SetLocalCoord();
105 
106  addItem( std::move( arc ) );
107 }
108 
109 
110 void GRAPHICS_IMPORTER_PCBNEW::AddPolygon( const std::vector< VECTOR2D >& aVertices, double aWidth )
111 {
112  std::vector< wxPoint > convertedPoints;
113  convertedPoints.reserve( convertedPoints.size() );
114 
115  for( const VECTOR2D& precisePoint : aVertices )
116  convertedPoints.emplace_back( MapCoordinate( precisePoint ) );
117 
118  unique_ptr<DRAWSEGMENT> polygon( createDrawing() );
119  polygon->SetShape( S_POLYGON );
120  polygon->SetLayer( GetLayer() );
121  polygon->SetPolyPoints( convertedPoints );
122 
123  if( polygon->Type() == PCB_MODULE_EDGE_T )
124  static_cast<EDGE_MODULE*>( polygon.get() )->SetLocalCoord();
125 
126  polygon->SetWidth( MapLineWidth( aWidth ) );
127  addItem( std::move( polygon ) );
128 }
129 
130 
131 void GRAPHICS_IMPORTER_PCBNEW::AddText( const VECTOR2D& aOrigin, const wxString& aText,
132  double aHeight, double aWidth, double aThickness, double aOrientation,
133  EDA_TEXT_HJUSTIFY_T aHJustify, EDA_TEXT_VJUSTIFY_T aVJustify )
134 {
135  unique_ptr<BOARD_ITEM> boardItem;
136  EDA_TEXT* textItem;
137  tie( boardItem, textItem ) = createText();
138  boardItem->SetLayer( GetLayer() );
139  textItem->SetThickness( MapLineWidth( aThickness ) );
140  textItem->SetTextPos( MapCoordinate( aOrigin ) );
141  textItem->SetTextAngle( aOrientation );
142  textItem->SetTextWidth( aWidth * ImportScalingFactor() );
143  textItem->SetTextHeight( aHeight * ImportScalingFactor() );
144  textItem->SetVertJustify( aVJustify );
145  textItem->SetHorizJustify( aHJustify );
146  textItem->SetText( aText );
147 
148  if( boardItem->Type() == PCB_MODULE_TEXT_T )
149  static_cast<TEXTE_MODULE*>( boardItem.get() )->SetLocalCoord();
150 
151  addItem( std::move( boardItem ) );
152 }
153 
154 
155 void GRAPHICS_IMPORTER_PCBNEW::AddSpline( const VECTOR2D& aStart, const VECTOR2D& BezierControl1,
156  const VECTOR2D& BezierControl2, const VECTOR2D& aEnd, double aWidth )
157 {
158  unique_ptr<DRAWSEGMENT> spline( createDrawing() );
159  spline->SetShape( S_CURVE );
160  spline->SetLayer( GetLayer() );
161  spline->SetWidth( MapLineWidth( aWidth ) );
162  spline->SetStart( MapCoordinate( aStart ) );
163  spline->SetBezControl1( MapCoordinate( BezierControl1 ) );
164  spline->SetBezControl2( MapCoordinate( BezierControl2 ) );
165  spline->SetEnd( MapCoordinate( aEnd ) );
166  spline->RebuildBezierToSegmentsPointsList( aWidth );
167 
168  if( spline->Type() == PCB_MODULE_EDGE_T )
169  static_cast<EDGE_MODULE*>( spline.get() )->SetLocalCoord();
170 
171  addItem( std::move( spline ) );
172 }
173 
174 
175 unique_ptr<DRAWSEGMENT> GRAPHICS_IMPORTER_BOARD::createDrawing()
176 {
177  return unique_ptr<DRAWSEGMENT>( new DRAWSEGMENT( m_board ) );
178 }
179 
180 
181 std::pair<unique_ptr<BOARD_ITEM>, EDA_TEXT*> GRAPHICS_IMPORTER_BOARD::createText()
182 {
183  TEXTE_PCB* text = new TEXTE_PCB( m_board );
184  return make_pair( unique_ptr<BOARD_ITEM>( text ), static_cast<EDA_TEXT*>( text ) );
185 }
186 
187 
189 {
190  return unique_ptr<DRAWSEGMENT>( new EDGE_MODULE( m_module ) );
191 }
192 
193 
194 std::pair<unique_ptr<BOARD_ITEM>, EDA_TEXT*> GRAPHICS_IMPORTER_MODULE::createText()
195 {
196  TEXTE_MODULE* text = new TEXTE_MODULE( m_module );
197  return make_pair( unique_ptr<BOARD_ITEM>( text ), static_cast<EDA_TEXT*>( text ) );
198 }
void SetTextAngle(double aAngle)
Definition: eda_text.h:169
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:62
TEXTE_PCB class definition.
void AddArc(const VECTOR2D &aCenter, const VECTOR2D &aStart, double aAngle, double aWidth) override
Creates an object representing an arc.
Class BOARD to handle a board.
polygon (not yet used for tracks, but could be in microwave apps)
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:236
std::unique_ptr< DRAWSEGMENT > createDrawing() override
Create an object representing a graphical shape.
usual segment : line with rounded ends
wxPoint MapCoordinate(const VECTOR2D &aCoordinate)
convert a imported coordinate to a board coordinate, according to the internal units, the user scale and offset
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels...
Definition: eda_text.h:127
double m_millimeterToIu
factor to convert millimeters to Internal Units
int MapLineWidth(double aLineWidth)
Footprint text class description.
virtual std::unique_ptr< DRAWSEGMENT > createDrawing()=0
Create an object representing a graphical shape.
std::pair< std::unique_ptr< BOARD_ITEM >, EDA_TEXT * > createText() override
Create an object representing a text.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:586
const VECTOR2D & GetImportOffsetMM() const
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:207
Arcs (with rounded ends)
PCB_LAYER_ID m_layer
Target layer for the imported shapes.
double ImportScalingFactor() const
Bezier Curve.
void AddSpline(const VECTOR2D &aStart, const VECTOR2D &aBezierControl1, const VECTOR2D &aBezierControl2, const VECTOR2D &aEnd, double aWidth) override
Creates an object representing an arc.
PCB_LAYER_ID GetLayer() const
Returns the target layer for the imported shapes.
double GetLineWidthMM() const
Returns the line width used for importing the outlines (in mm).
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
Creates an object representing a text.
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:69
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
void SetTextWidth(int aWidth)
Definition: eda_text.h:230
void addItem(std::unique_ptr< EDA_ITEM > aItem)
Adds an item to the imported shapes list.
std::unique_ptr< DRAWSEGMENT > createDrawing() override
Create an object representing a graphical shape.
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:206
void SetTextHeight(int aHeight)
Definition: eda_text.h:233
virtual std::pair< std::unique_ptr< BOARD_ITEM >, EDA_TEXT * > createText()=0
Create an object representing a text.
void AddPolygon(const std::vector< VECTOR2D > &aVertices, double aWidth) override
void AddCircle(const VECTOR2D &aOrigin, double aRadius, double aWidth) override
Creates an object representing a circle.
void AddLine(const VECTOR2D &aOrigin, const VECTOR2D &aEnd, double aWidth) override
Creates an object representing a line segment.
EDGE_MODULE class definition.
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:161
virtual void SetText(const wxString &aText)
Definition: eda_text.h:154