KiCad PCB EDA Suite
pcb_arc.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) 2007, 2008 Lubo Racko <developer@lura.sk>
5  * Copyright (C) 2007, 2008, 2012-2013 Alexander Lunev <al.lunev@yahoo.com>
6  * Copyright (C) 2012-2020 KiCad Developers, see CHANGELOG.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 
26 #include <pcad/pcb_arc.h>
27 
28 #include <board.h>
29 #include <footprint.h>
30 #include <fp_shape.h>
31 #include <math/util.h> // for KiROUND
32 #include <pcb_shape.h>
33 #include <trigo.h>
34 #include <xnode.h>
35 
36 #include <wx/string.h>
37 
38 namespace PCAD2KICAD {
39 
40 PCB_ARC::PCB_ARC( PCB_CALLBACKS* aCallbacks, BOARD* aBoard ) :
41  PCB_COMPONENT( aCallbacks, aBoard )
42 {
43  m_objType = wxT( 'A' );
44  m_StartX = 0;
45  m_StartY = 0;
46  m_Angle = 0;
47  m_Width = 0;
48 }
49 
50 
52 {
53 }
54 
55 
56 void PCB_ARC::Parse( XNODE* aNode, int aLayer, const wxString& aDefaultUnits,
57  const wxString& aActualConversion )
58 {
59  XNODE* lNode;
60  double a = 0.0;
61  int r = 0;
62  int endX = 0;
63  int endY = 0;
64 
65  m_PCadLayer = aLayer;
67 
68  if( FindNode( aNode, wxT( "width" ) ) )
69  {
70  SetWidth( FindNode( aNode, wxT( "width" ) )->GetNodeContent(), aDefaultUnits, &m_Width,
71  aActualConversion );
72  }
73 
74  if( aNode->GetName() == wxT( "triplePointArc" ) )
75  {
76  // center point
77  lNode = FindNode( aNode, wxT( "pt" ) );
78 
79  if( lNode )
80  {
81  SetPosition( lNode->GetNodeContent(), aDefaultUnits, &m_positionX, &m_positionY,
82  aActualConversion );
83  }
84 
85  // start point
86  if( lNode )
87  lNode = lNode->GetNext();
88 
89  if( lNode )
90  {
91  SetPosition( lNode->GetNodeContent(), aDefaultUnits, &m_StartX, &m_StartY,
92  aActualConversion );
93  }
94 
95  // end point
96  if( lNode )
97  lNode = lNode->GetNext();
98 
99  if( lNode )
100  SetPosition( lNode->GetNodeContent(), aDefaultUnits, &endX, &endY, aActualConversion );
101 
102  if( m_StartX == endX && m_StartY == endY )
103  {
104  m_Angle = 3600;
105  }
106  else
107  {
108  double alpha1 = ArcTangente( m_StartY - m_positionY, m_StartX - m_positionX );
109  double alpha2 = ArcTangente( endY - m_positionY, endX - m_positionX );
110  m_Angle = alpha1 - alpha2;
111 
113  }
114  }
115  else if( aNode->GetName() == wxT( "arc" ) )
116  {
117  lNode = FindNode( aNode, wxT( "pt" ) );
118 
119  if( lNode )
120  {
121  SetPosition( lNode->GetNodeContent(), aDefaultUnits, &m_positionX, &m_positionY,
122  aActualConversion );
123  }
124 
125  lNode = FindNode( aNode, wxT( "radius" ) );
126 
127  if( lNode)
128  {
129  SetWidth( FindNode( aNode, wxT( "radius" ) )->GetNodeContent(), aDefaultUnits, &r,
130  aActualConversion );
131  }
132 
133 
134  lNode = FindNode( aNode, wxT( "startAngle" ) );
135 
136  if( lNode )
137  a = StrToInt1Units( lNode->GetNodeContent() );
138 
139  lNode = FindNode( aNode, wxT( "sweepAngle" ) );
140 
141  if( lNode )
142  m_Angle = StrToInt1Units( lNode->GetNodeContent() );
143 
144  m_StartX = m_positionX + KiROUND( cosdecideg( r, a ) );
145  m_StartY = m_positionY - KiROUND( sindecideg( r, a ) );
146  }
147 }
148 
149 
150 void PCB_ARC::SetPosOffset( int aX_offs, int aY_offs )
151 {
152  PCB_COMPONENT::SetPosOffset( aX_offs, aY_offs );
153 
154  m_StartX += aX_offs;
155  m_StartY += aY_offs;
156 }
157 
158 
160 {
162 
163  m_StartX = -m_StartX;
164  m_Angle = -m_Angle;
165 
167 }
168 
169 
171 {
173  {
174  FP_SHAPE* arc = new FP_SHAPE( aFootprint, IsCircle() ? SHAPE_T::CIRCLE : SHAPE_T::ARC );
175  aFootprint->Add( arc );
176 
177  arc->SetStart0( wxPoint( m_positionX, m_positionY ) );
178  arc->SetEnd0( wxPoint( m_StartX, m_StartY ) );
179 
180  // Setting angle will set m_thirdPoint0, so must be done after setting
181  // m_start0 and m_end0
182  arc->SetAngle( -m_Angle );
183 
184  arc->SetWidth( m_Width );
185  arc->SetLayer( m_KiCadLayer );
186 
187  arc->SetDrawCoord();
188  }
189 }
190 
191 
193 {
194  PCB_SHAPE* arc = new PCB_SHAPE( m_board );
195 
196  m_board->Add( arc, ADD_MODE::APPEND );
197 
199  arc->SetFilled( false );
200  arc->SetLayer( m_KiCadLayer );
201  arc->SetStart( wxPoint( m_positionX, m_positionY ) );
202  arc->SetEnd( wxPoint( m_StartX, m_StartY ) );
203  arc->SetAngle( -m_Angle );
204  arc->SetWidth( m_Width );
205 }
206 
207 
209 {
210  return ( m_Angle == 3600 );
211 }
212 
213 } // namespace PCAD2KICAD
Arcs (with rounded ends)
void SetEnd0(const wxPoint &aPoint)
Definition: fp_shape.h:114
void SetShape(SHAPE_T aShape)
Definition: pcb_shape.h:109
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:192
void SetWidth(const wxString &aStr, const wxString &aDefaultMeasurementUnit, int *aWidth, const wxString &aActualConversion)
void SetFilled(bool aFlag)
Definition: pcb_shape.h:73
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition: lset.cpp:521
virtual void SetPosOffset(int aX_offs, int aY_offs) override
Definition: pcb_arc.cpp:150
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:290
void AddToFootprint(FOOTPRINT *aFootprint) override
Definition: pcb_arc.cpp:170
virtual void SetPosOffset(int aX_offs, int aY_offs)
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Adds an item to the container.
Definition: board.cpp:607
virtual void Parse(XNODE *aNode, int aLayer, const wxString &aDefaultUnits, const wxString &aActualConversion)
Definition: pcb_arc.cpp:56
void SetAngle(double aAngle, bool aUpdateEnd=true) override
Sets the angle for arcs, and normalizes it within the range 0 - 360 degrees.
Definition: fp_shape.cpp:142
bool IsNonCopperLayer(LAYER_NUM aLayerId)
Test whether a layer is a non copper layer.
Definition: layer_ids.h:798
virtual void Flip() override
Definition: pcb_arc.cpp:159
void SetDrawCoord()
Set draw coordinates (absolute values ) from relative coordinates.
Definition: fp_shape.cpp:82
void AddToBoard() override
Definition: pcb_arc.cpp:192
void SetStart0(const wxPoint &aPoint)
Definition: fp_shape.h:111
PCB_LAYER_ID GetKiCadLayer() const
Definition: pcb_component.h:56
Hold an XML or S-expression element.
Definition: xnode.h:43
double cosdecideg(double r, double a)
Circle generation utility: computes r * cos(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:452
XNODE * GetNext() const
Definition: xnode.h:67
double sindecideg(double r, double a)
Circle generation utility: computes r * sin(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:443
void SetStart(const wxPoint &aStart)
Definition: pcb_shape.h:127
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
PCB_ARC(PCB_CALLBACKS *aCallbacks, BOARD *aBoard)
Definition: pcb_arc.cpp:40
void SetWidth(int aWidth)
Definition: pcb_shape.h:96
XNODE * FindNode(XNODE *aChild, const wxString &aTag)
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
double ArcTangente(int dy, int dx)
Definition: trigo.cpp:183
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Removes an item from the container.
Definition: footprint.cpp:513
virtual void SetAngle(double aAngle, bool aUpdateEnd=true)
Set the angle for arcs, and normalizes it within the range 0 - 360 degrees.
Definition: pcb_shape.cpp:519
int StrToInt1Units(const wxString &aStr)
void SetPosition(const wxString &aStr, const wxString &aDefaultMeasurementUnit, int *aX, int *aY, const wxString &aActualConversion)
void SetEnd(const wxPoint &aEnd)
Definition: pcb_shape.h:137