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,
94  double aAngle, double aWidth )
95 {
96  unique_ptr<DRAWSEGMENT> arc( createDrawing() );
97  arc->SetShape( S_ARC );
98  arc->SetLayer( GetLayer() );
99  arc->SetWidth( MapLineWidth( aWidth ) );
100  arc->SetCenter( MapCoordinate( aCenter) );
101  arc->SetArcStart( MapCoordinate( aStart ) );
102  arc->SetAngle( aAngle * 10.0 ); // Pcbnew uses the decidegree
103 
104  if( arc->Type() == PCB_MODULE_EDGE_T )
105  static_cast<EDGE_MODULE*>( arc.get() )->SetLocalCoord();
106 
107  addItem( std::move( arc ) );
108 }
109 
110 
111 void GRAPHICS_IMPORTER_PCBNEW::AddPolygon( const std::vector< VECTOR2D >& aVertices, double aWidth )
112 {
113  std::vector< wxPoint > convertedPoints;
114  convertedPoints.reserve( convertedPoints.size() );
115 
116  for( const VECTOR2D& precisePoint : aVertices )
117  convertedPoints.emplace_back( MapCoordinate( precisePoint ) );
118 
119  unique_ptr<DRAWSEGMENT> polygon( createDrawing() );
120  polygon->SetShape( S_POLYGON );
121  polygon->SetLayer( GetLayer() );
122  polygon->SetPolyPoints( convertedPoints );
123 
124  if( polygon->Type() == PCB_MODULE_EDGE_T )
125  static_cast<EDGE_MODULE*>( polygon.get() )->SetLocalCoord();
126 
127  polygon->SetWidth( MapLineWidth( aWidth ) );
128  addItem( std::move( polygon ) );
129 }
130 
131 
132 void GRAPHICS_IMPORTER_PCBNEW::AddText( const VECTOR2D& aOrigin, const wxString& aText,
133  double aHeight, double aWidth, double aThickness, double aOrientation,
134  EDA_TEXT_HJUSTIFY_T aHJustify, EDA_TEXT_VJUSTIFY_T aVJustify )
135 {
136  unique_ptr<BOARD_ITEM> boardItem;
137  EDA_TEXT* textItem;
138  tie( boardItem, textItem ) = createText();
139  boardItem->SetLayer( GetLayer() );
140  textItem->SetThickness( MapLineWidth( aThickness ) );
141  textItem->SetTextPos( MapCoordinate( aOrigin ) );
142  textItem->SetTextAngle( aOrientation * 10.0 ); // Pcbnew uses the decidegree
143  textItem->SetTextWidth( aWidth * ImportScalingFactor() );
144  textItem->SetTextHeight( aHeight * ImportScalingFactor() );
145  textItem->SetVertJustify( aVJustify );
146  textItem->SetHorizJustify( aHJustify );
147  textItem->SetText( aText );
148 
149  if( boardItem->Type() == PCB_MODULE_TEXT_T )
150  static_cast<TEXTE_MODULE*>( boardItem.get() )->SetLocalCoord();
151 
152  addItem( std::move( boardItem ) );
153 }
154 
155 
156 void GRAPHICS_IMPORTER_PCBNEW::AddSpline( const VECTOR2D& aStart, const VECTOR2D& BezierControl1,
157  const VECTOR2D& BezierControl2, const VECTOR2D& aEnd, double aWidth )
158 {
159  unique_ptr<DRAWSEGMENT> spline( createDrawing() );
160  spline->SetShape( S_CURVE );
161  spline->SetLayer( GetLayer() );
162  spline->SetWidth( MapLineWidth( aWidth ) );
163  spline->SetStart( MapCoordinate( aStart ) );
164  spline->SetBezControl1( MapCoordinate( BezierControl1 ) );
165  spline->SetBezControl2( MapCoordinate( BezierControl2 ) );
166  spline->SetEnd( MapCoordinate( aEnd ) );
167  spline->RebuildBezierToSegmentsPointsList( aWidth );
168 
169  if( spline->Type() == PCB_MODULE_EDGE_T )
170  static_cast<EDGE_MODULE*>( spline.get() )->SetLocalCoord();
171 
172  addItem( std::move( spline ) );
173 }
174 
175 
176 unique_ptr<DRAWSEGMENT> GRAPHICS_IMPORTER_BOARD::createDrawing()
177 {
178  return unique_ptr<DRAWSEGMENT>( new DRAWSEGMENT( m_board ) );
179 }
180 
181 
182 std::pair<unique_ptr<BOARD_ITEM>, EDA_TEXT*> GRAPHICS_IMPORTER_BOARD::createText()
183 {
184  TEXTE_PCB* text = new TEXTE_PCB( m_board );
185  return make_pair( unique_ptr<BOARD_ITEM>( text ), static_cast<EDA_TEXT*>( text ) );
186 }
187 
188 
190 {
191  return unique_ptr<DRAWSEGMENT>( new EDGE_MODULE( m_module ) );
192 }
193 
194 
195 std::pair<unique_ptr<BOARD_ITEM>, EDA_TEXT*> GRAPHICS_IMPORTER_MODULE::createText()
196 {
197  TEXTE_MODULE* text = new TEXTE_MODULE( m_module );
198  return make_pair( unique_ptr<BOARD_ITEM>( text ), static_cast<EDA_TEXT*>( text ) );
199 }
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
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
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,...
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
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.
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.
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.
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: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.
const VECTOR2D & GetImportOffsetMM() const
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