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