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 <[email protected]>
5  * Copyright (C) 2007, 2008, 2012-2013 Alexander Lunev <[email protected]>
6  * Copyright (C) 2012-2020 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 
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->SetCenter0( wxPoint( m_positionX, m_positionY ) );
178  arc->SetStart0( wxPoint( m_StartX, m_StartY ) );
179  arc->SetArcAngleAndEnd0( -m_Angle, true );
180 
181  arc->SetWidth( m_Width );
182  arc->SetLayer( m_KiCadLayer );
183 
184  arc->SetDrawCoord();
185  }
186 }
187 
188 
190 {
192 
193  m_board->Add( arc, ADD_MODE::APPEND );
194 
195  arc->SetFilled( false );
196  arc->SetLayer( m_KiCadLayer );
197  arc->SetCenter( wxPoint( m_positionX, m_positionY ) );
198  arc->SetStart( wxPoint( m_StartX, m_StartY ) );
199  arc->SetArcAngleAndEnd( -m_Angle, true );
200  arc->SetWidth( m_Width );
201 }
202 
203 
205 {
206  return ( m_Angle == 3600 );
207 }
208 
209 } // namespace PCAD2KICAD
void SetFilled(bool aFlag)
Definition: eda_shape.h:92
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:164
void SetWidth(const wxString &aStr, const wxString &aDefaultMeasurementUnit, int *aWidth, const wxString &aActualConversion)
void SetArcAngleAndEnd0(double aAngle, bool aCheckNegativeAngle=false)
Sets the angle for arcs, and normalizes it within the range 0 - 360 degrees.
Definition: fp_shape.cpp:194
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition: lset.cpp:530
virtual void SetPosOffset(int aX_offs, int aY_offs) override
Definition: pcb_arc.cpp:150
void SetArcAngleAndEnd(double aAngle, bool aCheckNegativeAngle=false)
Set the end point from the angle center and start.
Definition: eda_shape.cpp:547
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 SetStart(const wxPoint &aStart)
Definition: eda_shape.h:110
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Adds an item to the container.
Definition: board.cpp:608
virtual void Parse(XNODE *aNode, int aLayer, const wxString &aDefaultUnits, const wxString &aActualConversion)
Definition: pcb_arc.cpp:56
bool IsNonCopperLayer(LAYER_NUM aLayerId)
Test whether a layer is a non copper layer.
Definition: layer_ids.h:819
virtual void Flip() override
Definition: pcb_arc.cpp:159
void SetWidth(int aWidth)
Definition: eda_shape.h:97
void SetCenter(const wxPoint &aCenter)
Definition: eda_shape.cpp:419
void SetDrawCoord()
Set draw coordinates (absolute values ) from relative coordinates.
Definition: fp_shape.cpp:81
E_SERIE r
Definition: eserie.cpp:41
void AddToBoard() override
Definition: pcb_arc.cpp:189
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
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:191
PCB_ARC(PCB_CALLBACKS *aCallbacks, BOARD *aBoard)
Definition: pcb_arc.cpp:40
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
void SetCenter0(const wxPoint &aPt)
Definition: fp_shape.cpp:162
int StrToInt1Units(const wxString &aStr)
void SetPosition(const wxString &aStr, const wxString &aDefaultMeasurementUnit, int *aX, int *aY, const wxString &aActualConversion)