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 
30 #include <wx/wx.h>
31 
32 #include <math/util.h> // for KiROUND
33 #include <trigo.h>
34 #include <fp_shape.h>
35 #include <pcb_arc.h>
36 #include <pcb_shape.h>
37 
38 namespace PCAD2KICAD {
39 
40 PCB_ARC::PCB_ARC( PCB_CALLBACKS* aCallbacks, BOARD* aBoard ) : PCB_COMPONENT( aCallbacks, aBoard )
41 {
42  m_objType = wxT( 'A' );
43  m_startX = 0;
44  m_startY = 0;
45  m_angle = 0;
46  m_width = 0;
47 }
48 
49 
51 {
52 }
53 
54 
55 void PCB_ARC::Parse( XNODE* aNode,
56  int aLayer,
57  const wxString& aDefaultMeasurementUnit,
58  const wxString& aActualConversion )
59 {
60  XNODE* lNode;
61  double a = 0.0;
62  int r = 0;
63  int endX = 0;
64  int endY = 0;
65 
66  m_PCadLayer = aLayer;
68 
69  if( FindNode( aNode, wxT( "width" ) ) )
70  SetWidth( FindNode( aNode, wxT( "width" ) )->GetNodeContent(),
71  aDefaultMeasurementUnit, &m_width, aActualConversion );
72 
73  if( aNode->GetName() == wxT( "triplePointArc" ) )
74  {
75  // center point
76  lNode = FindNode( aNode, wxT( "pt" ) );
77 
78  if( lNode )
79  SetPosition( lNode->GetNodeContent(), aDefaultMeasurementUnit,
80  &m_positionX, &m_positionY, aActualConversion );
81 
82  // start point
83  if( lNode )
84  lNode = lNode->GetNext();
85 
86  if( lNode )
87  SetPosition( lNode->GetNodeContent(), aDefaultMeasurementUnit,
88  &m_startX, &m_startY, aActualConversion );
89 
90  // end point
91  if( lNode )
92  lNode = lNode->GetNext();
93 
94  if( lNode )
95  SetPosition( lNode->GetNodeContent(), aDefaultMeasurementUnit,
96  &endX, &endY, aActualConversion );
97 
98  if( m_startX == endX && m_startY == endY )
99  {
100  m_angle = 3600;
101  }
102  else
103  {
104  double alpha1 = ArcTangente( m_startY - m_positionY, m_startX - m_positionX );
105  double alpha2 = ArcTangente( endY - m_positionY, endX - m_positionX );
106  m_angle = alpha1 - alpha2;
107 
109  }
110  }
111  else if( aNode->GetName() == wxT( "arc" ) )
112  {
113  lNode = FindNode( aNode, wxT( "pt" ) );
114 
115  if( lNode )
116  SetPosition( lNode->GetNodeContent(), aDefaultMeasurementUnit,
117  &m_positionX, &m_positionY, aActualConversion );
118 
119  lNode = FindNode( aNode, wxT( "radius" ) );
120  if( lNode)
121  SetWidth( FindNode( aNode, wxT( "radius" ) )->GetNodeContent(),
122  aDefaultMeasurementUnit, &r, aActualConversion );
123 
124 
125  lNode = FindNode( aNode, wxT( "startAngle" ) );
126  if( lNode )
127  a = StrToInt1Units( lNode->GetNodeContent() );
128 
129  lNode = FindNode( aNode, wxT( "sweepAngle" ) );
130  if( lNode )
131  m_angle = StrToInt1Units( lNode->GetNodeContent() );
132 
133  m_startX = m_positionX + KiROUND( cosdecideg( r, a ) );
134  m_startY = m_positionY - KiROUND( sindecideg( r, a ) );
135  }
136 }
137 
138 
139 void PCB_ARC::SetPosOffset( int aX_offs, int aY_offs )
140 {
141  PCB_COMPONENT::SetPosOffset( aX_offs, aY_offs );
142 
143  m_startX += aX_offs;
144  m_startY += aY_offs;
145 }
146 
147 
149 {
151 
152  m_startX = -m_startX;
153  m_angle = -m_angle;
154 
156 }
157 
158 
159 void PCB_ARC::AddToModule( MODULE* aModule )
160 {
162  {
163  FP_SHAPE* arc = new FP_SHAPE( aModule, ( IsCircle() ? S_CIRCLE : S_ARC ) );
164  aModule->Add( arc );
165 
166  arc->m_Start0 = wxPoint( m_positionX, m_positionY );
167  arc->m_End0 = wxPoint( m_startX, m_startY );
168 
169  // Setting angle will set m_ThirdPoint0, so must be done after setting
170  // m_Start0 and m_End0
171  arc->SetAngle( -m_angle );
172 
173  arc->SetWidth( m_width );
174  arc->SetLayer( m_KiCadLayer );
175 
176  arc->SetDrawCoord();
177  }
178 }
179 
180 
182 {
183  PCB_SHAPE* arc = new PCB_SHAPE( m_board );
184 
185  m_board->Add( arc, ADD_MODE::APPEND );
186 
187  arc->SetShape( IsCircle() ? S_CIRCLE : S_ARC );
188  arc->SetLayer( m_KiCadLayer );
189  arc->SetStart( wxPoint( m_positionX, m_positionY ) );
190  arc->SetEnd( wxPoint( m_startX, m_startY ) );
191  arc->SetAngle( -m_angle );
192  arc->SetWidth( m_width );
193 }
194 
195 
197 {
198  return ( m_angle == 3600 );
199 }
200 
201 } // namespace PCAD2KICAD
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
void SetPosition(wxString aStr, const wxString &aDefaultMeasurementUnit, int *aX, int *aY, const wxString &aActualConversion)
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition: lset.cpp:520
void SetWidth(wxString aStr, const wxString &aDefaultMeasurementUnit, int *aWidth, const wxString &aActualConversion)
PCB_ARC(PCB_CALLBACKS *aCallbacks, BOARD *aBoard)
Definition: pcb_arc.cpp:40
void AddToBoard() override
Definition: pcb_arc.cpp:181
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:277
Arcs (with rounded ends)
PCB_LAYER_ID GetKiCadLayer()
Definition: pcb_component.h:71
bool IsNonCopperLayer(LAYER_NUM aLayerId)
Test whether a layer is a non copper layer.
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.
void SetShape(PCB_SHAPE_TYPE_T aShape)
Definition: pcb_shape.h:113
void SetAngle(double aAngle, bool aUpdateEnd=true) override
Function SetAngle sets the angle for arcs, and normalizes it within the range 0 - 360 degrees.
Definition: fp_shape.cpp:151
wxPoint m_End0
End point, relative to module origin, orient 0.
Definition: fp_shape.h:153
wxPoint m_Start0
Start point or center, relative to module origin, orient 0.
Definition: fp_shape.h:152
void SetDrawCoord()
Set draw coordinates (absolute values ) from relative coordinates.
Definition: fp_shape.cpp:80
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
XNODE holds 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:428
XNODE * GetNext() const
Definition: xnode.h:68
double sindecideg(double r, double a)
Circle generation utility: computes r * sin(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:419
virtual void Parse(XNODE *aNode, int aLayer, const wxString &aDefaultMeasurementUnit, const wxString &aActualConversion)
Definition: pcb_arc.cpp:55
virtual void Flip() override
Definition: pcb_arc.cpp:148
void SetStart(const wxPoint &aStart)
Definition: pcb_shape.h:132
Information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:186
void AddToModule(MODULE *aModule) override
Definition: pcb_arc.cpp:159
void SetWidth(int aWidth)
Definition: pcb_shape.h:99
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:68
virtual void SetPosOffset(int aX_offs, int aY_offs) override
Definition: pcb_arc.cpp:139
double ArcTangente(int dy, int dx)
Definition: trigo.cpp:162
virtual void SetAngle(double aAngle, bool aUpdateEnd=true)
Function SetAngle sets the angle for arcs, and normalizes it within the range 0 - 360 degrees.
Definition: pcb_shape.cpp:435
int StrToInt1Units(const wxString &aStr)
void SetEnd(const wxPoint &aEnd)
Definition: pcb_shape.h:143