KiCad PCB EDA Suite
pcb.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 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 <wx/config.h>
32 
33 #include <common.h>
34 
35 #include <pcb.h>
36 #include <pcb_arc.h>
37 #include <pcb_copper_pour.h>
38 #include <pcb_cutout.h>
39 #include <pcb_keepout.h>
40 #include <pcb_line.h>
41 #include <pcb_module.h>
42 #include <pcb_pad_shape.h>
43 #include <pcb_via_shape.h>
44 #include <pcb_pad.h>
45 #include <pcb_text.h>
46 #include <pcb_via.h>
47 #include <s_expr_loader.h>
48 
49 namespace PCAD2KICAD {
50 
52 {
53  wxASSERT( aPCadLayer >= 0 && aPCadLayer < MAX_PCAD_LAYER_QTY );
54  return m_layersMap[aPCadLayer].KiCadLayer;
55 }
56 
58 {
59  wxASSERT( aPCadLayer >= 0 && aPCadLayer < MAX_PCAD_LAYER_QTY );
60  return m_layersMap[aPCadLayer].layerType;
61 }
62 
63 wxString PCB::GetLayerNetNameRef( int aPCadLayer )
64 {
65  wxASSERT( aPCadLayer >= 0 && aPCadLayer < MAX_PCAD_LAYER_QTY );
66  return m_layersMap[aPCadLayer].netNameRef;
67 }
68 
69 PCB::PCB( BOARD* aBoard ) : PCB_MODULE( this, aBoard )
70 {
71  int i;
72 
73  m_defaultMeasurementUnit = wxT( "mil" );
74 
75  for( i = 0; i < MAX_PCAD_LAYER_QTY; i++ )
76  {
77  m_layersMap[i].KiCadLayer = F_Mask; // default
79  m_layersMap[i].netNameRef = wxT( "" ); // default
80  }
81 
82  m_sizeX = 0;
83  m_sizeY = 0;
84 
87 
90 
94  m_timestamp_cnt = 0x10000000;
95 }
96 
97 
99 {
100  int i;
101 
102  for( i = 0; i < (int) m_pcbComponents.GetCount(); i++ )
103  {
104  delete m_pcbComponents[i];
105  }
106 
107  for( i = 0; i < (int) m_pcbNetlist.GetCount(); i++ )
108  {
109  delete m_pcbNetlist[i];
110  }
111 }
112 
113 
115 {
116  return m_timestamp_cnt++;
117 }
118 
119 int PCB::GetNetCode( wxString aNetName )
120 {
121  PCB_NET* net;
122 
123  for( int i = 0; i < (int) m_pcbNetlist.GetCount(); i++ )
124  {
125  net = m_pcbNetlist[i];
126 
127  if( net->m_name == aNetName )
128  {
129  return net->m_netCode;
130  }
131  }
132 
133  return 0;
134 }
135 
136 XNODE* PCB::FindCompDefName( XNODE* aNode, const wxString& aName )
137 {
138  XNODE* result = NULL, * lNode;
139  wxString propValue;
140 
141  lNode = FindNode( aNode, wxT( "compDef" ) );
142 
143  while( lNode )
144  {
145  if( lNode->GetName() == wxT( "compDef" ) )
146  {
147  lNode->GetAttribute( wxT( "Name" ), &propValue );
148 
149  if( propValue == aName )
150  {
151  result = lNode;
152  lNode = NULL;
153  }
154  }
155 
156  if( lNode )
157  lNode = lNode->GetNext();
158  }
159 
160  return result;
161 }
162 
163 
164 void PCB::SetTextProperty( XNODE* aNode, TTEXTVALUE* aTextValue,
165  const wxString& aPatGraphRefName,
166  const wxString& aXmlName,
167  const wxString& aActualConversion )
168 {
169  XNODE* tNode, * t1Node;
170  wxString n, nnew, pn, propValue, str;
171 
172  // aNode is pattern now
173  tNode = aNode;
174  t1Node = aNode;
175  n = aXmlName;
176 
177  // new file format version
178  if( FindNode( tNode, wxT( "patternGraphicsNameRef" ) ) )
179  {
180  FindNode( tNode,
181  wxT( "patternGraphicsNameRef" ) )->GetAttribute( wxT( "Name" ),
182  &pn );
183  pn.Trim( false );
184  pn.Trim( true );
185  tNode = FindNode( tNode, wxT( "patternGraphicsRef" ) );
186 
187  while( tNode )
188  {
189  if( tNode->GetName() == wxT( "patternGraphicsRef" ) )
190  {
191  if( FindNode( tNode, wxT( "patternGraphicsNameRef" ) ) )
192  {
193  FindNode( tNode,
194  wxT( "patternGraphicsNameRef" ) )->GetAttribute( wxT( "Name" ),
195  &propValue );
196 
197  if( propValue == pn )
198  {
199  t1Node = tNode; // find correct section with same name.
200  str = aTextValue->text;
201  str.Trim( false );
202  str.Trim( true );
203  nnew = n; // new file version
204  n = n + wxT( ' ' ) + str; // old file version
205  tNode = NULL;
206  }
207  }
208  }
209 
210  if( tNode )
211  tNode = tNode->GetNext();
212  }
213  }
214 
215  // old version and compatibile fr both from this point
216  tNode = FindNode( t1Node, wxT( "attr" ) );
217 
218  while( tNode )
219  {
220  tNode->GetAttribute( wxT( "Name" ), &propValue );
221  propValue.Trim( false );
222  propValue.Trim( true );
223 
224  if( propValue == n || propValue == nnew )
225  break;
226 
227  tNode = tNode->GetNext();
228  }
229 
230  if( tNode )
231  SetTextParameters( tNode, aTextValue, m_defaultMeasurementUnit, aActualConversion );
232 }
233 
234 
236  wxXmlDocument* aXmlDoc,
237  const wxString& aActualConversion,
238  wxStatusBar* aStatusBar )
239 {
240  XNODE* lNode, * tNode, * mNode;
241  PCB_MODULE* mc;
242  PCB_PAD* pad;
243  PCB_VIA* via;
244  PCB_KEEPOUT* keepOut;
245  wxString cn, str, propValue;
246 
247  lNode = aNode->GetChildren();
248 
249  while( lNode )
250  {
251  mc = NULL;
252 
253  if( lNode->GetName() == wxT( "pattern" ) )
254  {
255  FindNode( lNode, wxT( "patternRef" ) )->GetAttribute( wxT( "Name" ),
256  &cn );
257  cn = ValidateName( cn );
258  tNode = FindNode( (XNODE *)aXmlDoc->GetRoot(), wxT( "library" ) );
259 
260  if( tNode && cn.Len() > 0 )
261  {
262  tNode = FindModulePatternDefName( tNode, cn );
263 
264  if( tNode )
265  {
266  mc = new PCB_MODULE( this, m_board );
267 
268  mNode = FindNode( lNode, wxT( "patternGraphicsNameRef" ) );
269  if( mNode )
270  mNode->GetAttribute( wxT( "Name" ), &mc->m_patGraphRefName );
271 
272  mc->Parse( tNode, aStatusBar, m_defaultMeasurementUnit, aActualConversion );
273  }
274  }
275 
276  if( mc )
277  {
278  mc->m_compRef = cn; // default - in new version of file it is updated later....
279  tNode = FindNode( lNode, wxT( "refDesRef" ) );
280 
281  if( tNode )
282  {
283  tNode->GetAttribute( wxT( "Name" ), &mc->m_name.text );
284  SetTextProperty( lNode, &mc->m_name, mc->m_patGraphRefName, wxT(
285  "RefDes" ), aActualConversion );
286  SetTextProperty( lNode, &mc->m_value, mc->m_patGraphRefName, wxT(
287  "Value" ), aActualConversion );
288  }
289 
290  tNode = FindNode( lNode, wxT( "pt" ) );
291 
292  if( tNode )
293  SetPosition( tNode->GetNodeContent(),
295  &mc->m_positionX,
296  &mc->m_positionY,
297  aActualConversion );
298 
299  tNode = FindNode( lNode, wxT( "rotation" ) );
300 
301  if( tNode )
302  {
303  str = tNode->GetNodeContent();
304  str.Trim( false );
305  mc->m_rotation = StrToInt1Units( str );
306  }
307 
308  str = FindNodeGetContent( lNode, wxT( "isFlipped" ) );
309 
310  if( str == wxT( "True" ) )
311  mc->m_mirror = 1;
312 
313  tNode = aNode;
314 
315  while( tNode->GetName() != wxT( "www.lura.sk" ) )
316  tNode = tNode->GetParent();
317 
318  tNode = FindNode( tNode, wxT( "netlist" ) );
319 
320  if( tNode )
321  {
322  tNode = FindNode( tNode, wxT( "compInst" ) );
323 
324  while( tNode )
325  {
326  tNode->GetAttribute( wxT( "Name" ), &propValue );
327 
328  if( propValue == mc->m_name.text )
329  {
330  if( FindNode( tNode, wxT( "compValue" ) ) )
331  {
332  FindNode( tNode,
333  wxT( "compValue" ) )->GetAttribute( wxT( "Name" ),
334  &mc->m_value.text );
335  mc->m_value.text.Trim( false );
336  mc->m_value.text.Trim( true );
337  }
338 
339  if( FindNode( tNode, wxT( "compRef" ) ) )
340  {
341  FindNode( tNode,
342  wxT( "compRef" ) )->GetAttribute( wxT( "Name" ),
343  &mc->m_compRef );
344  mc->m_compRef.Trim( false );
345  mc->m_compRef.Trim( true );
346  }
347 
348  tNode = NULL;
349  }
350  else
351  tNode = tNode->GetNext();
352  }
353  }
354 
355  // map pins
356  tNode = FindNode( (XNODE *)aXmlDoc->GetRoot(), wxT( "library" ) );
357  tNode = FindCompDefName( tNode, mc->m_compRef );
358 
359  if( tNode )
360  {
361  tNode = FindPinMap( tNode );
362 
363  if( tNode )
364  {
365  mNode = tNode->GetChildren();
366 
367  while( mNode )
368  {
369  if( mNode->GetName() == wxT( "padNum" ) )
370  {
371  str = mNode->GetNodeContent();
372  mNode = mNode->GetNext();
373 
374  if( !mNode )
375  break;
376 
377  mNode->GetAttribute( wxT( "Name" ), &propValue );
378  mc->SetName( str, propValue );
379  mNode = mNode->GetNext();
380  }
381  else
382  {
383  mNode = mNode->GetNext();
384 
385  if( !mNode )
386  break;
387 
388  mNode = mNode->GetNext();
389  }
390  }
391  }
392  }
393 
394  m_pcbComponents.Add( mc );
395  }
396  }
397  else if( lNode->GetName() == wxT( "pad" ) )
398  {
399  pad = new PCB_PAD( this, m_board );
400  pad->Parse( lNode, m_defaultMeasurementUnit, aActualConversion );
401  m_pcbComponents.Add( pad );
402  }
403  else if( lNode->GetName() == wxT( "via" ) )
404  {
405  via = new PCB_VIA( this, m_board );
406  via->Parse( lNode, m_defaultMeasurementUnit, aActualConversion );
407  m_pcbComponents.Add( via );
408  }
409  else if( lNode->GetName() == wxT( "polyKeepOut" ) )
410  {
411  keepOut = new PCB_KEEPOUT( m_callbacks, m_board, 0 );
412 
413  if( keepOut->Parse( lNode, m_defaultMeasurementUnit, aActualConversion ) )
414  m_pcbComponents.Add( keepOut );
415  else
416  delete keepOut;
417  }
418 
419  lNode = lNode->GetNext();
420  }
421 }
422 
423 
424 void PCB::ConnectPinToNet( const wxString& aCompRef,
425  const wxString& aPinRef,
426  const wxString& aNetName )
427 {
428  PCB_MODULE* module;
429  PCB_PAD* cp;
430  int i, j;
431 
432  for( i = 0; i < (int) m_pcbComponents.GetCount(); i++ )
433  {
434  module = (PCB_MODULE*) m_pcbComponents[i];
435 
436  if( module->m_objType == wxT( 'M' ) && module->m_name.text == aCompRef )
437  {
438  for( j = 0; j < (int) module->m_moduleObjects.GetCount(); j++ )
439  {
440  if( module->m_moduleObjects[j]->m_objType == wxT( 'P' ) )
441  {
442  cp = (PCB_PAD*) module->m_moduleObjects[j];
443 
444  if( cp->m_name.text == aPinRef )
445  cp->m_net = aNetName;
446  }
447  }
448  }
449  }
450 }
451 
452 
453 int PCB::FindLayer( const wxString& aLayerName )
454 {
455  for( LAYER_NUM i = 0; i < (int)m_layersStackup.GetCount(); ++i )
456  {
457  if( m_layersStackup[i] == aLayerName )
458  return i;
459  }
460 
461  return -1;
462 }
463 
464 
465 /* KiCad layers
466  * 0 Copper layer
467  * 1 to 14 Inner layers
468  * 15 Component layer
469  * 16 Copper side adhesive layer Technical layers
470  * 17 Component side adhesive layer
471  * 18 Copper side Solder paste layer
472  * 19 Component Solder paste layer
473  * 20 Copper side Silk screen layer
474  * 21 Component Silk screen layer
475  * 22 Copper side Solder mask layer
476  * 23 Component Solder mask layer
477  * 24 Draw layer (Used for general drawings)
478  * 25 Comment layer (Other layer used for general drawings)
479  * 26 ECO1 layer (Other layer used for general drawings) // BUG
480  * 26 ECO2 layer (Other layer used for general drawings) // BUG 27
481  * 27 Edge layer. Items on Edge layer are seen on all layers // BUG 28
482  */
483 void PCB::MapLayer( XNODE* aNode )
484 {
485  wxString lName, layerType;
486  PCB_LAYER_ID KiCadLayer;
487  long num = 0;
488 
489  aNode->GetAttribute( wxT( "Name" ), &lName );
490  lName = lName.MakeUpper();
491 
492  if( lName == wxT( "TOP ASSY" ) )
493  KiCadLayer = F_Fab;
494  else if( lName == wxT( "TOP SILK" ) )
495  KiCadLayer = F_SilkS;
496  else if( lName == wxT( "TOP PASTE" ) )
497  KiCadLayer = F_Paste;
498  else if( lName == wxT( "TOP MASK" ) )
499  KiCadLayer = F_Mask;
500  else if( lName == wxT( "TOP" ) )
501  KiCadLayer = F_Cu;
502  else if( lName == wxT( "BOTTOM" ) )
503  KiCadLayer = B_Cu;
504  else if( lName == wxT( "BOT MASK" ) )
505  KiCadLayer = B_Mask;
506  else if( lName == wxT( "BOT PASTE" ) )
507  KiCadLayer = B_Paste;
508  else if( lName == wxT( "BOT SILK" ) )
509  KiCadLayer = B_SilkS;
510  else if( lName == wxT( "BOT ASSY" ) )
511  KiCadLayer = B_Fab;
512  else if( lName == wxT( "BOARD" ) )
513  KiCadLayer = Edge_Cuts;
514  else
515  {
516  int layernum = FindLayer( lName );
517 
518  if( layernum == -1 )
519  KiCadLayer = Dwgs_User; // default
520  else
521 #if 0 // was:
522  KiCadLayer = FIRST_COPPER_LAYER + m_layersStackup.GetCount() - 1 - layernum;
523 #else
524  KiCadLayer = ToLAYER_ID( layernum );
525 #endif
526  }
527 
528  if( FindNode( aNode, wxT( "layerNum" ) ) )
529  FindNode( aNode, wxT( "layerNum" ) )->GetNodeContent().ToLong( &num );
530 
531  if( num < 0 || num >= MAX_PCAD_LAYER_QTY )
532  THROW_IO_ERROR( wxString::Format( wxT( "layerNum = %ld is out of range" ), num ) );
533 
534  m_layersMap[(int) num].KiCadLayer = KiCadLayer;
535 
536  if( FindNode( aNode, wxT( "layerType" ) ) )
537  {
538  layerType = FindNode( aNode, wxT( "layerType" ) )->GetNodeContent().Trim( false );
539 
540  if( layerType == wxT( "NonSignal" ) )
541  m_layersMap[(int) num].layerType = LAYER_TYPE_NONSIGNAL;
542  if( layerType == wxT( "Signal" ) )
544  if( layerType == wxT( "Plane" ) )
546  }
547 
548  if( FindNode( aNode, wxT( "netNameRef" ) ) )
549  {
550  FindNode( aNode, wxT( "netNameRef" ) )->GetAttribute( wxT( "Name" ),
551  &m_layersMap[(int) num].netNameRef );
552  }
553 }
554 
555 int PCB::FindOutlinePoint( VERTICES_ARRAY* aOutline, wxRealPoint aPoint )
556 {
557  int i;
558 
559  for( i = 0; i < (int) aOutline->GetCount(); i++ )
560  if( *((*aOutline)[i]) == aPoint )
561  return i;
562 
563  return -1;
564 }
565 
566 /*int cmpFunc( wxRealPoint **first, wxRealPoint **second )
567 {
568  return sqrt( pow( (double) aPointA.x - (double) aPointB.x, 2 ) +
569  pow( (double) aPointA.y - (double) aPointB.y, 2 ) );
570 
571  return 0;
572 }*/
573 double PCB::GetDistance( wxRealPoint* aPoint1, wxRealPoint* aPoint2 )
574 {
575  return sqrt( ( aPoint1->x - aPoint2->x ) *
576  ( aPoint1->x - aPoint2->x ) +
577  ( aPoint1->y - aPoint2->y ) *
578  ( aPoint1->y - aPoint2->y ) );
579 }
580 
581 void PCB::GetBoardOutline( wxXmlDocument* aXmlDoc, const wxString& aActualConversion )
582 {
583  XNODE* iNode, *lNode, *pNode;
584  long PCadLayer = 0;
585  int x, y, i, j, targetInd;
586  wxRealPoint* xchgPoint;
587  double minDistance, distance;
588 
589  iNode = FindNode( (XNODE *)aXmlDoc->GetRoot(), wxT( "pcbDesign" ) );
590 
591  if( iNode )
592  {
593  // COMPONENTS AND OBJECTS
594  iNode = iNode->GetChildren();
595 
596  while( iNode )
597  {
598  // objects
599  if( iNode->GetName() == wxT( "layerContents" ) )
600  {
601  if( FindNode( iNode, wxT( "layerNumRef" ) ) )
602  FindNode( iNode, wxT( "layerNumRef" ) )->GetNodeContent().ToLong( &PCadLayer );
603 
604  if( GetKiCadLayer( PCadLayer ) == Edge_Cuts )
605  {
606  lNode = iNode->GetChildren();
607  while( lNode )
608  {
609  if( lNode->GetName() == wxT( "line" ) )
610  {
611  pNode = FindNode( lNode, wxT( "pt" ) );
612 
613  if( pNode )
614  {
615  SetPosition( pNode->GetNodeContent(), m_defaultMeasurementUnit,
616  &x, &y, aActualConversion );
617 
618  if( FindOutlinePoint( &m_boardOutline, wxRealPoint( x, y) ) == -1 )
619  m_boardOutline.Add( new wxRealPoint( x, y ) );
620  }
621 
622  if( pNode )
623  pNode = pNode->GetNext();
624 
625  if( pNode )
626  {
627  SetPosition( pNode->GetNodeContent(), m_defaultMeasurementUnit,
628  &x, &y, aActualConversion );
629 
630  if( FindOutlinePoint( &m_boardOutline, wxRealPoint( x, y) ) == -1 )
631  m_boardOutline.Add( new wxRealPoint( x, y ) );
632  }
633  }
634 
635  lNode = lNode->GetNext();
636  }
637 
638  //m_boardOutline.Sort( cmpFunc );
639  // sort vertices according to the distances between them
640  if( m_boardOutline.GetCount() > 3 )
641  {
642  for( i = 0; i < (int) m_boardOutline.GetCount() - 1; i++ )
643  {
644  minDistance = GetDistance( m_boardOutline[i], m_boardOutline[i + 1] );
645  targetInd = i + 1;
646 
647  for( j = i + 2; j < (int) m_boardOutline.GetCount(); j++ )
648  {
649  distance = GetDistance( m_boardOutline[i], m_boardOutline[j] );
650  if( distance < minDistance )
651  {
652  minDistance = distance;
653  targetInd = j;
654  }
655  }
656 
657  xchgPoint = m_boardOutline[i + 1];
658  m_boardOutline[i + 1] = m_boardOutline[targetInd];
659  m_boardOutline[targetInd] = xchgPoint;
660  }
661  }
662 
663  break;
664  }
665  }
666 
667  iNode = iNode->GetNext();
668  }
669  }
670 }
671 
672 void PCB::ParseBoard( wxStatusBar* aStatusBar, wxXmlDocument* aXmlDoc, const wxString& aActualConversion )
673 {
674  XNODE* aNode;//, *aaNode;
675  PCB_NET* net;
676  PCB_COMPONENT* comp;
677  PCB_MODULE* module;
678  wxString compRef, pinRef, layerName, layerType;
679  int i, j, netCode;
680 
681  // Defaut measurement units
682  aNode = FindNode( (XNODE *)aXmlDoc->GetRoot(), wxT( "asciiHeader" ) );
683 
684  if( aNode )
685  {
686  aNode = FindNode( aNode, wxT( "fileUnits" ) );
687 
688  if( aNode )
689  {
690  m_defaultMeasurementUnit = aNode->GetNodeContent().Lower();
691  m_defaultMeasurementUnit.Trim( true );
692  m_defaultMeasurementUnit.Trim( false );
693  }
694  }
695 
696  // Determine layers stackup
697  aNode = FindNode( (XNODE *)aXmlDoc->GetRoot(), wxT( "pcbDesign" ) );
698 
699  /*if( aNode )
700  {
701  aNode = FindNode( aNode, wxT( "layersStackup" ) );
702 
703  if( aNode )
704  {
705  aNode = FindNode( aNode, wxT( "layerStackupData" ) );
706 
707  while( aNode )
708  {
709  if( aNode->GetName() == wxT( "layerStackupData" ) )
710  {
711  aaNode = FindNode( aNode, wxT( "layerStackupName" ) );
712 
713  if( aaNode ) {
714  aaNode->GetAttribute( wxT( "Name" ), &layerName );
715  layerName = layerName.MakeUpper();
716  m_layersStackup.Add( layerName );
717  }
718  }
719 
720  aNode = aNode->GetNext();
721  }
722  }
723  }*/
724 
725  if( aNode )
726  {
727  aNode = FindNode( aNode, wxT( "layerDef" ) );
728 
729  while( aNode )
730  {
731  if( aNode->GetName() == wxT( "layerDef" ) )
732  {
733  if( FindNode( aNode, wxT( "layerType" ) ) )
734  {
735  layerType = FindNode( aNode,
736  wxT( "layerType" ) )->GetNodeContent().Trim( false );
737 
738  if( layerType == wxT( "Signal" ) || layerType == wxT( "Plane" ) )
739  {
740  aNode->GetAttribute( wxT( "Name" ), &layerName );
741  layerName = layerName.MakeUpper();
742  m_layersStackup.Add( layerName );
743  }
744  }
745  }
746 
747  aNode = aNode->GetNext();
748  }
749  }
750 
751  // Layers mapping
752  aNode = FindNode( (XNODE *)aXmlDoc->GetRoot(), wxT( "pcbDesign" ) );
753 
754  if( aNode )
755  {
756  aNode = FindNode( aNode, wxT( "layerDef" ) );
757 
758  while( aNode )
759  {
760  if( aNode->GetName() == wxT( "layerDef" ) )
761  MapLayer( aNode );
762 
763  aNode = aNode->GetNext();
764  }
765  }
766 
767  GetBoardOutline( aXmlDoc, aActualConversion );
768 
769  // NETLIST
770  // aStatusBar->SetStatusText( wxT( "Loading NETLIST " ) );
771 
772  aNode = FindNode( (XNODE *)aXmlDoc->GetRoot(), wxT( "netlist" ) );
773 
774  if( aNode )
775  {
776  aNode = FindNode( aNode, wxT( "net" ) );
777 
778  netCode = 1;
779 
780  while( aNode )
781  {
782  net = new PCB_NET( netCode++ );
783  net->Parse( aNode );
784  m_pcbNetlist.Add( net );
785 
786  aNode = aNode->GetNext();
787  }
788  }
789 
790  // BOARD FILE
791  // aStatusBar->SetStatusText( wxT( "Loading BOARD DEFINITION " ) );
792 
793  aNode = FindNode( (XNODE *)aXmlDoc->GetRoot(), wxT( "pcbDesign" ) );
794 
795  if( aNode )
796  {
797  // COMPONENTS AND OBJECTS
798  aNode = aNode->GetChildren();
799 
800  while( aNode )
801  {
802  // Components/modules
803  if( aNode->GetName() == wxT( "multiLayer" ) )
804  DoPCBComponents( aNode, aXmlDoc, aActualConversion, aStatusBar );
805 
806  // objects
807  if( aNode->GetName() == wxT( "layerContents" ) )
808  DoLayerContentsObjects( aNode, NULL, &m_pcbComponents, aStatusBar,
809  m_defaultMeasurementUnit, aActualConversion );
810 
811  aNode = aNode->GetNext();
812  }
813 
814  // POSTPROCESS -- SET NETLIST REFERENCES
815  // aStatusBar->SetStatusText( wxT( "Processing NETLIST " ) );
816 
817  for( i = 0; i < (int) m_pcbNetlist.GetCount(); i++ )
818  {
819  net = m_pcbNetlist[i];
820 
821  for( j = 0; j < (int) net->m_netNodes.GetCount(); j++ )
822  {
823  compRef = net->m_netNodes[j]->m_compRef;
824  compRef.Trim( false );
825  compRef.Trim( true );
826  pinRef = net->m_netNodes[j]->m_pinRef;
827  pinRef.Trim( false );
828  pinRef.Trim( true );
829  ConnectPinToNet( compRef, pinRef, net->m_name );
830  }
831  }
832 
833  // POSTPROCESS -- FLIP COMPONENTS
834  for( i = 0; i < (int) m_pcbComponents.GetCount(); i++ )
835  {
836  if( m_pcbComponents[i]->m_objType == wxT( 'M' ) )
837  ( (PCB_MODULE*) m_pcbComponents[i] )->Flip();
838  }
839 
840  // POSTPROCESS -- SET/OPTIMIZE NEW PCB POSITION
841  // aStatusBar->SetStatusText( wxT( "Optimizing BOARD POSITION " ) );
842 
843  m_sizeX = 10000000;
844  m_sizeY = 0;
845 
846  for( i = 0; i < (int) m_pcbComponents.GetCount(); i++ )
847  {
848  comp = m_pcbComponents[i];
849 
850  if( comp->m_positionY < m_sizeY )
851  m_sizeY = comp->m_positionY; // max Y
852 
853  if( comp->m_positionX < m_sizeX && comp->m_positionX > 0 )
854  m_sizeX = comp->m_positionX; // Min X
855  }
856 
857  m_sizeY -= 10000;
858  m_sizeX -= 10000;
859  // aStatusBar->SetStatusText( wxT( " POSITIONING POSTPROCESS " ) );
860 
861  for( i = 0; i < (int) m_pcbComponents.GetCount(); i++ )
863 
864  m_sizeX = 0;
865  m_sizeY = 0;
866 
867  for( i = 0; i < (int) m_pcbComponents.GetCount(); i++ )
868  {
869  comp = m_pcbComponents[i];
870 
871  if( comp->m_positionY < m_sizeY )
872  m_sizeY = comp->m_positionY; // max Y
873 
874  if( comp->m_positionX > m_sizeX )
875  m_sizeX = comp->m_positionX; // Min X
876  }
877 
878  // SHEET SIZE CALCULATION
879  m_sizeY = -m_sizeY; // it is in absolute units
880  m_sizeX += 10000;
881  m_sizeY += 10000;
882 
883  // A4 is minimum $Descr A4 11700 8267
884  if( m_sizeX < 11700 )
885  m_sizeX = 11700;
886 
887  if( m_sizeY < 8267 )
888  m_sizeY = 8267;
889  }
890  else
891  {
892  // LIBRARY FILE
893  // aStatusBar->SetStatusText( wxT( "Processing LIBRARY FILE " ) );
894 
895  aNode = FindNode( (XNODE *)aXmlDoc->GetRoot(), wxT( "library" ) );
896 
897  if( aNode )
898  {
899  aNode = FindNode( aNode, wxT( "compDef" ) );
900 
901  while( aNode )
902  {
903  // aStatusBar->SetStatusText( wxT( "Processing COMPONENTS " ) );
904 
905  if( aNode->GetName() == wxT( "compDef" ) )
906  {
907  module = new PCB_MODULE( this, m_board );
908  module->Parse( aNode, aStatusBar, m_defaultMeasurementUnit,
909  aActualConversion );
910  m_pcbComponents.Add( module );
911  }
912 
913  aNode = aNode->GetNext();
914  }
915  }
916  }
917 }
918 
919 
921 {
922  int i;
923  PCB_NET* net;
924 
926 
927  for( i = 0; i < (int) m_pcbNetlist.GetCount(); i++ )
928  {
929  net = m_pcbNetlist[i];
930 
931  m_board->Add( new NETINFO_ITEM( m_board, net->m_name, net->m_netCode ) );
932  }
933 
934  for( i = 0; i < (int) m_pcbComponents.GetCount(); i++ )
935  {
936  m_pcbComponents[i]->AddToBoard();
937  }
938 }
939 
940 } // namespace PCAD2KICAD
wxString netNameRef
Definition: pcb_callbacks.h:47
void ParseBoard(wxStatusBar *aStatusBar, wxXmlDocument *aXmlDoc, const wxString &aActualConversion)
Definition: pcb.cpp:672
wxString m_name
Definition: pcb_net.h:54
void SetPosition(wxString aStr, const wxString &aDefaultMeasurementUnit, int *aX, int *aY, const wxString &aActualConversion)
int m_sizeX
Definition: pcb.h:50
PCB_NETS_ARRAY m_pcbNetlist
Definition: pcb.h:47
#define MAX_PCAD_LAYER_QTY
Definition: pcb.h:41
void SetCopperLayerCount(int aCount)
void SetName(const wxString &aPin, const wxString &aName)
Definition: pcb_module.cpp:372
virtual void Parse(XNODE *aNode, const wxString &aDefaultMeasurementUnit, const wxString &aActualConversion)
Definition: pcb_pad.cpp:59
PCB_NET_NODES_ARRAY m_netNodes
Definition: pcb_net.h:56
TLAYER m_layersMap[MAX_PCAD_LAYER_QTY]
Definition: pcb.h:49
void SetTextProperty(XNODE *aNode, TTEXTVALUE *aTextValue, const wxString &aPatGraphRefName, const wxString &aXmlName, const wxString &aActualConversion)
Definition: pcb.cpp:164
const string & str
Definition: json11.cpp:596
PCB_LAYER_ID GetKiCadLayer()
Definition: pcb_component.h:73
XNODE * FindModulePatternDefName(XNODE *aNode, const wxString &aName)
Definition: pcb_module.cpp:71
LAYER_TYPE_T GetLayerType(int aPCadLayer) override
Definition: pcb.cpp:57
XNODE * GetParent() const
Definition: xnode.h:73
virtual void SetPosOffset(int aX_offs, int aY_offs)
PCB(BOARD *aBoard)
Definition: pcb.cpp:69
void ConnectPinToNet(const wxString &aCr, const wxString &aPr, const wxString &aNetName)
Definition: pcb.cpp:424
wxString ValidateName(wxString aName)
PCB_LAYER_ID
A quick note on layer IDs:
void AddToBoard() override
Definition: pcb.cpp:920
wxArrayString m_layersStackup
Definition: pcb.h:70
int m_sizeY
Definition: pcb.h:51
int FindOutlinePoint(VERTICES_ARRAY *aOutline, wxRealPoint aPoint)
Definition: pcb.cpp:555
PCB_MODULE(PCB_CALLBACKS *aCallbacks, BOARD *aBoard)
Definition: pcb_module.cpp:50
void DoPCBComponents(XNODE *aNode, wxXmlDocument *aXmlDoc, const wxString &aActualConversion, wxStatusBar *aStatusBar)
Definition: pcb.cpp:235
virtual void Flip() override
Definition: pcb_module.cpp:630
wxString FindNodeGetContent(XNODE *aChild, const wxString &aTag)
void SetTextParameters(XNODE *aNode, TTEXTVALUE *aTextValue, const wxString &aDefaultMeasurementUnit, const wxString &aActualConversion)
XNODE * GetChildren() const
Definition: xnode.h:63
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
Adds an item to the container.
virtual void Parse(XNODE *aNode, wxStatusBar *aStatusBar, const wxString &aDefaultMeasurementUnit, const wxString &aActualConversion)
Definition: pcb_module.cpp:390
void GetBoardOutline(wxXmlDocument *aXmlDoc, const wxString &aActualConversion)
Definition: pcb.cpp:581
XNODE * FindCompDefName(XNODE *aNode, const wxString &aName)
Definition: pcb.cpp:136
PCB_LAYER_ID KiCadLayer
Definition: pcb_callbacks.h:45
int LAYER_NUM
Type LAYER_NUM can be replaced with int and removed.
virtual bool Parse(XNODE *aNode, const wxString &aDefaultMeasurementUnit, const wxString &aActualConversion) override
Definition: pcb_keepout.cpp:52
Class XNODE holds an XML or S-expression element.
Definition: xnode.h:43
int FindLayer(const wxString &aLayerName)
Definition: pcb.cpp:453
Class NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:69
int m_timestamp_cnt
Definition: pcb.h:69
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void DoLayerContentsObjects(XNODE *aNode, PCB_MODULE *aPCBModule, PCB_COMPONENTS_ARRAY *aList, wxStatusBar *aStatusBar, const wxString &aDefaultMeasurementUnit, const wxString &aActualConversion)
Definition: pcb_module.cpp:217
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:169
double GetDistance(wxRealPoint *aPoint1, wxRealPoint *aPoint2)
Definition: pcb.cpp:573
size_t i
Definition: json11.cpp:597
void MapLayer(XNODE *aNode)
Definition: pcb.cpp:483
PCB_COMPONENTS_ARRAY m_moduleObjects
Definition: pcb_module.h:44
PCB_COMPONENTS_ARRAY m_pcbComponents
Definition: pcb.h:46
XNODE * FindNode(XNODE *aChild, const wxString &aTag)
wxString GetLayerNetNameRef(int aPCadLayer) override
Definition: pcb.cpp:63
The common library.
#define FIRST_COPPER_LAYER
int GetNewTimestamp() override
Definition: pcb.cpp:114
PCB_CALLBACKS * m_callbacks
Definition: pcb_component.h:78
LAYER_TYPE_T
Definition: pcb_callbacks.h:36
virtual void Parse(XNODE *aNode, const wxString &aDefaultMeasurementUnit, const wxString &aActualConversion) override
Definition: pcb_via.cpp:49
void Parse(XNODE *aNode)
Definition: pcb_net.cpp:66
XNODE * GetNext() const
Definition: xnode.h:68
LAYER_TYPE_T layerType
Definition: pcb_callbacks.h:46
XNODE * FindPinMap(XNODE *aNode)
int StrToInt1Units(const wxString &aStr)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:796
int GetNetCode(wxString aNetName) override
Definition: pcb.cpp:119
wxString m_defaultMeasurementUnit
Definition: pcb.h:48
VERTICES_ARRAY m_boardOutline
Definition: pcb_module.h:46