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 <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( (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<PCB_SHAPE> 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_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( S_CIRCLE );
82  circle->SetFilled( aFilled );
83  circle->SetLayer( GetLayer() );
84  circle->SetWidth( MapLineWidth( aWidth ) );
85  circle->SetCenter( MapCoordinate( aCenter ) );
86  circle->SetArcStart( 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( S_ARC );
100  arc->SetLayer( GetLayer() );
101  arc->SetWidth( MapLineWidth( aWidth ) );
102  arc->SetCenter( MapCoordinate( aCenter) );
103  arc->SetArcStart( MapCoordinate( aStart ) );
104  arc->SetAngle( aAngle * 10.0 ); // Pcbnew uses the decidegree
105 
106  if( arc->Type() == PCB_FP_SHAPE_T )
107  static_cast<FP_SHAPE*>( arc.get() )->SetLocalCoord();
108 
109  addItem( std::move( arc ) );
110 }
111 
112 
113 void GRAPHICS_IMPORTER_PCBNEW::AddPolygon( const std::vector< VECTOR2D >& aVertices, double aWidth )
114 {
115  std::vector< wxPoint > convertedPoints;
116  convertedPoints.reserve( convertedPoints.size() );
117 
118  for( const VECTOR2D& precisePoint : aVertices )
119  convertedPoints.emplace_back( MapCoordinate( precisePoint ) );
120 
121  std::unique_ptr<PCB_SHAPE> polygon( createDrawing() );
122  polygon->SetShape( S_POLYGON );
123  polygon->SetFilled( GetLayer() != Edge_Cuts );
124  polygon->SetLayer( GetLayer() );
125  polygon->SetPolyPoints( convertedPoints );
126 
127  if( polygon->Type() == PCB_FP_SHAPE_T )
128  static_cast<FP_SHAPE*>( polygon.get() )->SetLocalCoord();
129 
130  polygon->SetWidth( MapLineWidth( aWidth ) );
131  addItem( std::move( polygon ) );
132 }
133 
134 
135 void GRAPHICS_IMPORTER_PCBNEW::AddText( const VECTOR2D& aOrigin, const wxString& aText,
136  double aHeight, double aWidth, double aThickness, double aOrientation,
137  EDA_TEXT_HJUSTIFY_T aHJustify, EDA_TEXT_VJUSTIFY_T aVJustify )
138 {
139  std::unique_ptr<BOARD_ITEM> boardItem;
140  EDA_TEXT* textItem;
141  tie( boardItem, textItem ) = createText();
142  boardItem->SetLayer( GetLayer() );
143  textItem->SetTextThickness( MapLineWidth( aThickness ) );
144  textItem->SetTextPos( MapCoordinate( aOrigin ) );
145  textItem->SetTextAngle( aOrientation * 10.0 ); // Pcbnew uses the decidegree
146  textItem->SetTextWidth( aWidth * ImportScalingFactor() );
147  textItem->SetTextHeight( aHeight * ImportScalingFactor() );
148  textItem->SetVertJustify( aVJustify );
149  textItem->SetHorizJustify( aHJustify );
150  textItem->SetText( aText );
151 
152  if( boardItem->Type() == PCB_FP_TEXT_T )
153  static_cast<FP_TEXT*>( boardItem.get() )->SetLocalCoord();
154 
155  addItem( std::move( boardItem ) );
156 }
157 
158 
159 void GRAPHICS_IMPORTER_PCBNEW::AddSpline( const VECTOR2D& aStart, const VECTOR2D& BezierControl1,
160  const VECTOR2D& BezierControl2, const VECTOR2D& aEnd, double aWidth )
161 {
162  std::unique_ptr<PCB_SHAPE> spline( createDrawing() );
163  spline->SetShape( S_CURVE );
164  spline->SetLayer( GetLayer() );
165  spline->SetWidth( MapLineWidth( aWidth ) );
166  spline->SetStart( MapCoordinate( aStart ) );
167  spline->SetBezControl1( MapCoordinate( BezierControl1 ) );
168  spline->SetBezControl2( MapCoordinate( BezierControl2 ) );
169  spline->SetEnd( MapCoordinate( aEnd ) );
170  spline->RebuildBezierToSegmentsPointsList( aWidth );
171 
172  if( spline->Type() == PCB_FP_SHAPE_T )
173  static_cast<FP_SHAPE*>( spline.get() )->SetLocalCoord();
174 
175  addItem( std::move( spline ) );
176 }
177 
178 
179 std::unique_ptr<PCB_SHAPE> GRAPHICS_IMPORTER_BOARD::createDrawing()
180 {
181  return std::make_unique<PCB_SHAPE>( m_board );
182 }
183 
184 
185 std::pair<std::unique_ptr<BOARD_ITEM>, EDA_TEXT*> GRAPHICS_IMPORTER_BOARD::createText()
186 {
187  PCB_TEXT* text = new PCB_TEXT( m_board );
188  return make_pair( std::unique_ptr<BOARD_ITEM>( text ), static_cast<EDA_TEXT*>( text ) );
189 }
190 
191 
192 std::unique_ptr<PCB_SHAPE> GRAPHICS_IMPORTER_FOOTPRINT::createDrawing()
193 {
194  return std::make_unique<FP_SHAPE>( m_footprint );
195 }
196 
197 
198 std::pair<std::unique_ptr<BOARD_ITEM>, EDA_TEXT*> GRAPHICS_IMPORTER_FOOTPRINT::createText()
199 {
200  FP_TEXT* text = new FP_TEXT( m_footprint );
201  return make_pair( std::unique_ptr<BOARD_ITEM>( text ), static_cast<EDA_TEXT*>( text ) );
202 }
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:61
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:253
polygon (not yet used for tracks, but could be in microwave apps)
Definition: board_item.h:54
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.
usual segment : line with rounded ends
Definition: board_item.h:50
Arcs (with rounded ends)
Definition: board_item.h:52
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:119
double m_millimeterToIu
< factor to convert millimeters to Internal Units
int MapLineWidth(double aLineWidth)
If aLineWidth < 0, the default line thickness value is returned.
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:121
VECTOR2< double > VECTOR2D
Definition: vector2d.h:622
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:209
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:68
void SetTextWidth(int aWidth)
Definition: eda_text.h:247
void addItem(std::unique_ptr< EDA_ITEM > aItem)
< Add an item to the imported shapes list.
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:208
void SetTextHeight(int aHeight)
Definition: eda_text.h:250
virtual std::pair< std::unique_ptr< BOARD_ITEM >, EDA_TEXT * > createText()=0
Target layer for the imported shapes.
ring
Definition: board_item.h:53
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:166
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:174
void AddLine(const VECTOR2D &aOrigin, const VECTOR2D &aEnd, double aWidth) override
Create an object representing a line segment.
Bezier Curve.
Definition: board_item.h:55
static constexpr int Millimeter2iu(double mm)