KiCad PCB EDA Suite
pcb_pad.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 #include <trigo.h>
32 
33 #include <pcb_pad.h>
34 
35 namespace PCAD2KICAD {
36 
37 PCB_PAD::PCB_PAD( PCB_CALLBACKS* aCallbacks, BOARD* aBoard ) :
38  PCB_COMPONENT( aCallbacks, aBoard )
39 {
40  m_objType = wxT( 'P' );
41  m_Number = 0;
42  m_Hole = 0;
43  m_IsHolePlated = true;
44  m_defaultPinDes = wxEmptyString;
45 }
46 
47 
49 {
50  int i;
51 
52  for( i = 0; i < (int) m_Shapes.GetCount(); i++ )
53  delete m_Shapes[i];
54 }
55 
56 
57 void PCB_PAD::Parse( XNODE* aNode, const wxString& aDefaultMeasurementUnit,
58  const wxString& aActualConversion )
59 {
60  XNODE* lNode;
61  XNODE* cNode;
62  long num;
63  wxString propValue, str, emsg;
64  PCB_PAD_SHAPE* padShape;
65 
66  m_rotation = 0;
67  lNode = FindNode( aNode, wxT( "padNum" ) );
68 
69  if( lNode )
70  {
71  lNode->GetNodeContent().ToLong( &num );
72  m_Number = (int) num;
73  }
74 
75  lNode = FindNode( aNode, wxT( "padStyleRef" ) );
76 
77  if( lNode )
78  {
79  lNode->GetAttribute( wxT( "Name" ), &propValue );
80  propValue.Trim( false );
81  m_name.text = propValue;
82  }
83 
84  lNode = FindNode( aNode, wxT( "pt" ) );
85 
86  if( lNode )
87  {
88  SetPosition( lNode->GetNodeContent(), aDefaultMeasurementUnit, &m_positionX, &m_positionY,
89  aActualConversion );
90  }
91 
92  lNode = FindNode( aNode, wxT( "rotation" ) );
93 
94  if( lNode )
95  {
96  str = lNode->GetNodeContent();
97  str.Trim( false );
98  m_rotation = StrToInt1Units( str );
99  }
100 
101  lNode = FindNode( aNode, wxT( "netNameRef" ) );
102 
103  if( lNode )
104  {
105  lNode->GetAttribute( wxT( "Name" ), &propValue );
106  propValue.Trim( false );
107  propValue.Trim( true );
108  m_net = propValue;
110  }
111 
112  lNode = FindNode( aNode, wxT( "defaultPinDes" ) );
113 
114  if( lNode )
115  {
116  lNode->GetAttribute( wxT( "Name" ), &propValue );
117  //propValue.Trim( false );
118  m_defaultPinDes = propValue;
119  }
120 
121  lNode = aNode;
122 
123  while( lNode && lNode->GetName() != wxT( "www.lura.sk" ) )
124  lNode = lNode->GetParent();
125 
126  lNode = FindNode( lNode, wxT( "library" ) );
127 
128  if ( !lNode )
129  THROW_IO_ERROR( wxT( "Unable to find library section" ) );
130 
131  lNode = FindNode( lNode, wxT( "padStyleDef" ) );
132 
133  while( lNode )
134  {
135  lNode->GetAttribute( wxT( "Name" ), &propValue );
136 
137  if( propValue.IsSameAs( m_name.text, false) )
138  break;
139 
140  lNode = lNode->GetNext();
141  }
142 
143  if ( !lNode )
144  THROW_IO_ERROR( wxString::Format( wxT( "Unable to find padStyleDef " ) + m_name.text ) );
145 
146  cNode = FindNode( lNode, wxT( "holeDiam" ) );
147 
148  if( cNode )
149  SetWidth( cNode->GetNodeContent(), aDefaultMeasurementUnit, &m_Hole, aActualConversion );
150 
151  if( FindNodeGetContent( lNode, wxT( "isHolePlated" ) ) == wxT( "False" ) )
152  m_IsHolePlated = false;
153 
154  cNode = FindNode( lNode, wxT( "padShape" ) );
155 
156  while( cNode )
157  {
158  if( cNode->GetName() == wxT( "padShape" ) )
159  {
160  // we support only Pads on specific layers......
161  // we do not support pads on "Plane", "NonSignal" , "Signal" ... layerr
162  if( FindNode( cNode, wxT( "layerNumRef" ) ) )
163  {
164  padShape = new PCB_PAD_SHAPE( m_callbacks, m_board );
165  padShape->Parse( cNode, aDefaultMeasurementUnit, aActualConversion );
166  m_Shapes.Add( padShape );
167  }
168  }
169 
170  cNode = cNode->GetNext();
171  }
172 }
173 
174 
176 {
177  int i;
178 
180 
181  if( m_objType == wxT( 'P' ) )
183 
184  for( i = 0; i < (int)m_Shapes.GetCount(); i++ )
186 }
187 
188 
189 void PCB_PAD::AddToFootprint( FOOTPRINT* aFootprint, int aRotation, bool aEncapsulatedPad )
190 {
191  PCB_PAD_SHAPE* padShape;
192  wxString padShapeName = wxT( "Ellipse" );
193  PAD_ATTR_T padType;
194  int i;
195  int width = 0;
196  int height = 0;
197 
198  PAD* pad = new PAD( aFootprint );
199 
200  if( !m_IsHolePlated && m_Hole )
201  {
202  // mechanical hole
203  pad->SetShape( PAD_SHAPE_CIRCLE );
205 
207  pad->SetDrillSize( wxSize( m_Hole, m_Hole ) );
208  pad->SetSize( wxSize( m_Hole, m_Hole ) );
209 
210  // Mounting Hole: Solder Mask Margin from Top Layer Width size.
211  // Used the default zone clearance (simplify)
212  if( m_Shapes.GetCount() && m_Shapes[0]->m_Shape == wxT( "MtHole" ) )
213  {
214  int sm_margin = ( m_Shapes[0]->m_Width - m_Hole ) / 2;
215  pad->SetLocalSolderMaskMargin( sm_margin );
216  pad->SetLocalClearance( sm_margin + Millimeter2iu( 0.254 ) );
217  }
218 
219  pad->SetLayerSet( LSET::AllCuMask() | LSET( 2, B_Mask, F_Mask ) );
220  }
221  else
222  {
223  ( m_Hole ) ? padType = PAD_ATTRIB_PTH : padType = PAD_ATTRIB_SMD;
224 
225  // form layer mask
226  for( i = 0; i < (int) m_Shapes.GetCount(); i++ )
227  {
228  padShape = m_Shapes[i];
229 
230  if( padShape->m_Width > 0 && padShape->m_Height > 0 )
231  {
232  if( padShape->m_KiCadLayer == F_Cu ||
233  padShape->m_KiCadLayer == B_Cu )
234  {
235  padShapeName = padShape->m_Shape;
236  width = padShape->m_Width;
237  height = padShape->m_Height;
238 
239  // assume this is SMD pad
240  if( padShape->m_KiCadLayer == F_Cu )
241  pad->SetLayerSet( LSET( 3, F_Cu, F_Paste, F_Mask ) );
242  else
243  pad->SetLayerSet( LSET( 3, B_Cu, B_Paste, B_Mask ) );
244  break;
245  }
246  }
247  }
248 
249  if( width == 0 || height == 0 )
250  {
251  delete pad;
252  return;
253  }
254 
255  if( padType == PAD_ATTRIB_PTH )
256  // actually this is a thru-hole pad
257  pad->SetLayerSet( LSET::AllCuMask() | LSET( 2, B_Mask, F_Mask ) );
258 
259  pad->SetName( m_name.text );
260 
261  if( padShapeName == wxT( "Oval" )
262  || padShapeName == wxT( "Ellipse" )
263  || padShapeName == wxT( "MtHole" ) )
264  {
265  if( width != height )
266  pad->SetShape( PAD_SHAPE_OVAL );
267  else
268  pad->SetShape( PAD_SHAPE_CIRCLE );
269  }
270  else if( padShapeName == wxT( "Rect" ) )
271  pad->SetShape( PAD_SHAPE_RECT );
272  else if( padShapeName == wxT( "RndRect" ) )
274  else if( padShapeName == wxT( "Polygon" ) )
275  pad->SetShape( PAD_SHAPE_RECT ); // approximation
276 
277  pad->SetSize( wxSize( width, height ) );
278  pad->SetDelta( wxSize( 0, 0 ) );
279  pad->SetOrientation( m_rotation + aRotation );
280 
282  pad->SetOffset( wxPoint( 0, 0 ) );
283  pad->SetDrillSize( wxSize( m_Hole, m_Hole ) );
284 
285  pad->SetAttribute( padType );
286 
287  // Set the proper net code
288  NETINFO_ITEM* netinfo = m_board->FindNet( m_net );
289  if( netinfo == NULL ) // I believe this should not happen, but just in case
290  {
291  // It is a new net
292  netinfo = new NETINFO_ITEM( m_board, m_net );
293  m_board->Add( netinfo );
294  }
295 
296  pad->SetNetCode( netinfo->GetNetCode() );
297  }
298 
299  if( !aEncapsulatedPad )
300  {
301  // pad's "Position" is not relative to the footprint's, whereas Pos0 is relative to
302  // the footprint's but is the unrotated coordinate.
303  wxPoint padpos( m_positionX, m_positionY );
304  pad->SetPos0( padpos );
305  RotatePoint( &padpos, aFootprint->GetOrientation() );
306  pad->SetPosition( padpos + aFootprint->GetPosition() );
307  }
308 
309  aFootprint->Add( pad );
310 }
311 
312 
314 {
315  PCB_PAD_SHAPE* padShape;
316  int i;
317  int width = 0;
318  int height = 0;
319 
320  if( m_objType == wxT( 'V' ) ) // via
321  {
322  // choose one of the shapes
323  for( i = 0; i < (int) m_Shapes.GetCount(); i++ )
324  {
325  padShape = m_Shapes[i];
326 
327  if( padShape->m_Width > 0 && padShape->m_Height > 0 )
328  {
329  if( padShape->m_KiCadLayer == F_Cu
330  || padShape->m_KiCadLayer == B_Cu )
331  {
332  width = padShape->m_Width;
333  height = padShape->m_Height;
334 
335  break;
336  }
337  }
338  }
339 
340  if( width == 0 || height == 0 )
341  return;
342 
343  if( IsCopperLayer( m_KiCadLayer ) )
344  {
345  VIA* via = new VIA( m_board );
346  m_board->Add( via );
347 
348  via->SetPosition( wxPoint( m_positionX, m_positionY ) );
349  via->SetEnd( wxPoint( m_positionX, m_positionY ) );
350 
351  via->SetWidth( height );
353  via->SetLayerPair( F_Cu, B_Cu );
354  via->SetDrill( m_Hole );
355 
356  via->SetLayer( m_KiCadLayer );
357  via->SetNetCode( m_netCode );
358  }
359  }
360  else // pad
361  {
362  FOOTPRINT* footprint = new FOOTPRINT( m_board );
363  m_board->Add( footprint, ADD_MODE::APPEND );
364 
366 
367  footprint->SetPosition( wxPoint( m_positionX, m_positionY ) );
368  AddToFootprint( footprint, 0, true );
369  }
370 }
371 
372 } // namespace PCAD2KICAD
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:750
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition: board.cpp:1268
void SetOffset(const wxPoint &aOffset)
Definition: pad.h:248
Definition: track.h:343
PAD_ATTR_T
The set of pad shapes, used with PAD::{Set,Get}Attribute().
Definition: pad_shapes.h:78
void SetPosition(const wxPoint &aPoint) override
Definition: track.h:412
void SetLocalClearance(int aClearance)
Definition: pad.h:383
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:194
void SetPosition(wxString aStr, const wxString &aDefaultMeasurementUnit, int *aX, int *aY, const wxString &aActualConversion)
void SetEnd(const wxPoint &aEnd)
Definition: track.h:112
void AddToBoard() override
Definition: pcb_pad.cpp:313
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition: lset.cpp:521
Smd pad, appears on the solder paste layer (default)
Definition: pad_shapes.h:81
void SetName(const wxString &aName)
Set the pad name (sometimes called pad number, although it can be an array reference like AA12).
Definition: pad.h:132
virtual void Parse(XNODE *aNode, const wxString &aDefaultMeasurementUnit, const wxString &aActualConversion)
double GetOrientation() const
Definition: footprint.h:186
bool SetNetCode(int aNetCode, bool aNoAssert)
Set net using a net code.
void SetWidth(wxString aStr, const wxString &aDefaultMeasurementUnit, int *aWidth, const wxString &aActualConversion)
PCB_PAD(PCB_CALLBACKS *aCallbacks, BOARD *aBoard)
Definition: pcb_pad.cpp:37
virtual void Parse(XNODE *aNode, const wxString &aDefaultMeasurementUnit, const wxString &aActualConversion)
Definition: pcb_pad.cpp:57
void SetSize(const wxSize &aSize)
Definition: pad.h:231
void SetDelta(const wxSize &aSize)
Definition: pad.h:238
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:228
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:85
void AddToFootprint(FOOTPRINT *aFootprint) override
Definition: pcb_pad.h:52
void SetWidth(int aWidth)
Definition: track.h:109
void SetLocalSolderMaskMargin(int aMargin)
Definition: pad.h:379
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Adds an item to the container.
Definition: board.cpp:563
LSET is a set of PCB_LAYER_IDs.
#define NULL
void SetPos0(const wxPoint &aPos)
Definition: pad.h:225
XNODE * GetParent() const
Definition: xnode.h:72
wxString FindNodeGetContent(XNODE *aChild, const wxString &aTag)
void SetDrillSize(const wxSize &aSize)
Definition: pad.h:241
int GetNetCode(wxString aNetName) const
Definition: pcb_component.h:71
wxString m_defaultPinDes
Definition: pcb_pad.h:62
void SetLayerPair(PCB_LAYER_ID aTopLayer, PCB_LAYER_ID aBottomLayer)
Function SetLayerPair For a via m_layer contains the top layer, the other layer is in m_bottomLayer.
Definition: track.cpp:414
void SetShape(PAD_SHAPE_T aShape)
Set the new shape of this pad.
Definition: pad.h:160
virtual void Flip() override
Definition: pcb_pad.cpp:175
void SetDrillShape(PAD_DRILL_SHAPE_T aShape)
Definition: pad.h:345
Hold an XML or S-expression element.
Definition: xnode.h:43
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
Handle the data for a net.
Definition: netinfo.h:64
XNODE * GetNext() const
Definition: xnode.h:67
void SetLayerSet(LSET aLayers) override
Definition: pad.h:359
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
void SetPosition(const wxPoint &aPos) override
Definition: pad.h:171
Plated through hole pad.
Definition: pad_shapes.h:80
void SetDrill(int aDrill)
Function SetDrill sets the drill value for vias.
Definition: track.h:474
XNODE * FindNode(XNODE *aChild, const wxString &aTag)
wxPoint GetPosition() const override
Definition: footprint.h:182
bool IsCopperLayer(LAYER_NUM aLayerId)
Tests whether a layer is a copper layer.
PCB_CALLBACKS * m_callbacks
Definition: pcb_component.h:74
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Removes an item from the container.
Definition: footprint.cpp:467
Definition: pad.h:60
void SetPosition(const wxPoint &aPos) override
Definition: footprint.cpp:1437
void SetViaType(VIATYPE aViaType)
Definition: track.h:374
static constexpr int Millimeter2iu(double mm)
PCB_PAD_SHAPES_ARRAY m_Shapes
Definition: pcb_pad.h:42
int StrToInt1Units(const wxString &aStr)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
void SetOrientation(double aAngle)
Set the rotation angle of the pad.
Definition: pcbnew/pad.cpp:579
int GetNetCode() const
Definition: netinfo.h:113
void SetAttribute(PAD_ATTR_T aAttribute)
Definition: pcbnew/pad.cpp:560