KiCad PCB EDA Suite
export_hyperlynx.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) 2019 CERN
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <kiface_i.h>
25 #include <pcb_edit_frame.h>
26 #include <pcbnew.h>
27 
28 #include <class_board.h>
29 #include <class_board_item.h>
30 #include <class_drawsegment.h>
31 #include <class_edge_mod.h>
32 #include <class_module.h>
33 #include <class_track.h>
34 #include <class_zone.h>
35 #include <cstdio>
36 #include <vector>
37 
38 #include <ki_exception.h>
39 #include <reporter.h>
40 
42 
43 static double iu2hyp( double iu )
44 {
45  return iu / 1e9 / 0.0254;
46 }
47 
48 
49 class HYPERLYNX_EXPORTER;
50 
52 {
53 public:
54  friend class HYPERLYNX_EXPORTER;
55 
56  HYPERLYNX_PAD_STACK( BOARD* aBoard, const D_PAD* aPad );
57  HYPERLYNX_PAD_STACK( BOARD* aBoard, const VIA* aVia );
59 
60  bool isThrough() const
61  {
63  }
64 
65  bool operator==( const HYPERLYNX_PAD_STACK& other ) const
66  {
67  if( m_shape != other.m_shape )
68  return false;
69 
70  if( m_type != other.m_type )
71  return false;
72 
73  if( isThrough() && other.isThrough() && m_drill != other.m_drill )
74  return false;
75 
76  if( m_sx != other.m_sx )
77  return false;
78 
79  if( m_sy != other.m_sy )
80  return false;
81 
82  if( m_layers != other.m_layers )
83  return false;
84 
85  if( m_angle != other.m_angle )
86  return false;
87 
88  return true;
89  }
90 
91  bool isSMD() const
92  {
93  return m_type == PAD_ATTRIB_SMD;
94  }
95 
97  {
98  for( auto l : LSET::AllCuMask().Seq() )
99  {
100  if( m_layers[l] )
101  return l;
102  }
103  return F_Cu;
104  }
105 
106  void SetId( int id )
107  {
108  m_id = id;
109  }
110 
111  int GetId() const
112  {
113  return m_id;
114  }
115 
117  {
118  switch( m_shape )
119  {
120  case PAD_SHAPE_CIRCLE:
121  case PAD_SHAPE_OVAL:
122  case PAD_SHAPE_ROUNDRECT:
123  case PAD_SHAPE_RECT: return true;
124  default: return false;
125  }
126  }
127 
128  bool isEmpty() const
129  {
130  LSET layerMask = LSET::AllCuMask() & m_board->GetEnabledLayers();
131  LSET outLayers = m_layers & layerMask;
132 
133  return outLayers.none();
134  }
135 
136 private:
138  int m_id;
139  int m_drill;
141  int m_sx, m_sy;
142  double m_angle;
145 };
146 
147 
149 {
150 public:
153 
154  virtual bool Run() override;
155 
156 private:
158  {
159  for( auto p : m_padStacks )
160  {
161  if( *p == stack )
162  return p;
163  }
164 
165  stack.SetId( m_padStacks.size() );
166  m_padStacks.push_back( new HYPERLYNX_PAD_STACK( stack ) );
167 
168  return m_padStacks.back();
169  }
170 
171  const std::string formatPadShape( HYPERLYNX_PAD_STACK& aStack )
172  {
173  int shapeId = 0;
174  char buf[1024];
175 
176  switch( aStack.m_shape )
177  {
178  case PAD_SHAPE_CIRCLE:
179  case PAD_SHAPE_OVAL: shapeId = 0; break;
180  case PAD_SHAPE_ROUNDRECT: shapeId = 2; break;
181  case PAD_SHAPE_RECT: shapeId = 1; break;
182  default:
183  shapeId = 0;
184 
185  if( m_reporter )
186  {
188  _( "File contains pad shapes that are not supported by the Hyperlynx exporter\n"
189  "(Supported shapes are oval, rectangle, circle.)\n"
190  "They have been exported as oval pads." ),
192  }
193  break;
194  }
195 
196  snprintf( buf, sizeof( buf ), "%d, %.9f, %.9f, %.1f, M", shapeId,
197  iu2hyp( aStack.m_sx ),
198  iu2hyp( aStack.m_sy ),
199  aStack.m_angle );
200 
201  return buf;
202  }
203 
204  bool generateHeaders();
205  bool writeBoardInfo();
206  bool writeStackupInfo();
207  bool writeDevices();
208  bool writePadStacks();
209  bool writeNets();
210  bool writeNetObjects( const std::vector<BOARD_ITEM*>& aObjects );
211 
212 
214 
215  const std::vector<BOARD_ITEM*> collectNetObjects( int netcode );
216 
217  std::vector<HYPERLYNX_PAD_STACK*> m_padStacks;
218  std::map<BOARD_ITEM*, HYPERLYNX_PAD_STACK*> m_padMap;
219 
220 
221  std::shared_ptr<FILE_OUTPUTFORMATTER> m_out;
222  int m_polyId;
223 };
224 
225 
227 {
228  m_board = aBoard;
229  m_sx = aPad->GetSize().x;
230  m_sy = aPad->GetSize().y;
231  m_angle = 180.0 - ( aPad->GetOrientation() / 10.0 );
232 
233  if( m_angle < 0.0 )
234  {
235  m_angle += 360.0;
236  }
237 
238  m_layers = aPad->GetLayerSet();
239  m_drill = aPad->GetDrillSize().x;
240  m_shape = aPad->GetShape();
242 }
243 
244 
246 {
247  m_board = aBoard;
248  m_sx = m_sy = aVia->GetWidth();
249  m_angle = 0;
251  m_drill = aVia->GetDrillValue();
254 }
255 
256 
258 {
259  m_out->Print( 0, "{VERSION=2.14}\n" );
260  m_out->Print( 0, "{UNITS=ENGLISH LENGTH}\n\n" );
261  return true;
262 }
263 
264 
266 {
267  LSET layerMask = LSET::AllCuMask() & m_board->GetEnabledLayers();
268  LSET outLayers = aStack.m_layers & layerMask;
269 
270  if( outLayers.none() )
271  return;
272 
273  m_out->Print( 0, "{PADSTACK=%d, %.9f\n", aStack.m_id, iu2hyp( aStack.m_drill ) );
274 
275  if( outLayers == layerMask )
276  {
277  m_out->Print( 1, "(\"%s\", %s)\n", "MDEF", formatPadShape( aStack ).c_str() );
278  }
279  else
280  {
281  for( auto l : outLayers.Seq() )
282  {
283  m_out->Print( 1, "(\"%s\", %s)\n", (const char*) m_board->GetLayerName( l ).c_str(),
284  formatPadShape( aStack ).c_str() );
285  }
286  }
287 
288  m_out->Print( 0, "}\n\n" );
289 }
290 
291 
293 {
294  SHAPE_POLY_SET outlines;
295  wxString errText;
296  wxPoint errLoc;
297 
298  m_out->Print( 0, "{BOARD \"%s\"\n", (const char*) m_board->GetFileName().c_str() );
299 
300  if( !m_board->GetBoardPolygonOutlines( outlines, &errText, &errLoc ) )
301  {
302  return false;
303  }
304 
305  for( int o = 0; o < outlines.OutlineCount(); o++ )
306  {
307  const auto& outl = outlines.COutline( o );
308 
309  for( int i = 0; i < outl.SegmentCount(); i++ )
310  {
311  const auto& s = outl.CSegment( i );
312  m_out->Print( 1, "(PERIMETER_SEGMENT X1=%.9f Y1=%.9f X2=%.9f Y2=%.9f)\n",
313  iu2hyp( s.A.x ), iu2hyp( s.A.y ), iu2hyp( s.B.x ), iu2hyp( s.B.y ) );
314  }
315  }
316 
317  m_out->Print( 0, "}\n\n" );
318 
319  return true;
320 }
321 
322 
324 {
325  auto layers = m_board->GetDesignSettings().GetEnabledLayers().CuStack();
326 
327  m_out->Print( 0, "{STACKUP\n" );
328 
329  for( auto l : layers )
330  {
331  const auto name = m_board->GetLayerName( l );
332 
333  m_out->Print( 1, "(SIGNAL T=0.002284 P=0.000000 C=1.724e-8 L=\"%s\" M=COPPER)\n",
334  (const char*) name.c_str() );
335 
336  if( l != B_Cu )
337  {
338  m_out->Print( 1, "(DIELECTRIC T=0.007087 C=3.660000 L=\"DE_%s\" M=FR4)\n",
339  (const char*) name.c_str() );
340  }
341  }
342  m_out->Print( 0, "}\n\n" );
343 
344  return true;
345 }
346 
347 
349 {
350  m_out->Print( 0, "{DEVICES\n" );
351 
352  for( auto mod : m_board->Modules() )
353  {
354  wxString ref = mod->GetReference();
355  auto layerName = m_board->GetLayerName( mod->GetLayer() );
356 
357  if( ref.IsEmpty() )
358  ref = "EMPTY";
359 
360  m_out->Print( 1, "(? REF=\"%s\" L=\"%s\")\n", (const char*) ref.c_str(),
361  (const char*) layerName.c_str() );
362  }
363  m_out->Print( 0, "}\n\n" );
364 
365  return true;
366 }
367 
368 
370 {
371  for( auto mod : m_board->Modules() )
372  {
373  for( auto pad : mod->Pads() )
374  {
375  auto ps = addPadStack( HYPERLYNX_PAD_STACK( m_board, pad ) );
376  m_padMap[pad] = ps;
377  }
378  }
379 
380  for( auto trk : m_board->Tracks() )
381  {
382  if( VIA* via = dyn_cast<VIA*>( trk ) )
383  {
384  auto ps = addPadStack( HYPERLYNX_PAD_STACK( m_board, via ) );
385  m_padMap[via] = ps;
386  }
387  }
388 
389  for( auto pstack : m_padStacks )
390  writeSinglePadStack( *pstack );
391 
392  return true;
393 }
394 
395 
396 bool HYPERLYNX_EXPORTER::writeNetObjects( const std::vector<BOARD_ITEM*>& aObjects )
397 {
398  for( auto item : aObjects )
399  {
400  if( D_PAD* pad = dyn_cast<D_PAD*>( item ) )
401  {
402  auto pstackIter = m_padMap.find( pad );
403 
404  if( pstackIter != m_padMap.end() )
405  {
406  wxString ref = pad->GetParent()->GetReference();
407 
408  if( ref.IsEmpty() )
409  ref = "EMPTY";
410 
411  auto padName = pad->GetName();
412 
413  if( padName.IsEmpty() )
414  padName = "1";
415 
416 
417  m_out->Print( 1, "(PIN X=%.10f Y=%.10f R=\"%s.%s\" P=%d)\n",
418  iu2hyp( pad->GetPosition().x ), iu2hyp( pad->GetPosition().y ),
419  (const char*) ref.c_str(), (const char*) padName.c_str(),
420  pstackIter->second->GetId() );
421  }
422  }
423  else if( VIA* via = dyn_cast<VIA*>( item ) )
424  {
425  auto pstackIter = m_padMap.find( via );
426 
427  if( pstackIter != m_padMap.end() )
428  {
429  m_out->Print( 1, "(VIA X=%.10f Y=%.10f P=%d)\n", iu2hyp( via->GetPosition().x ),
430  iu2hyp( via->GetPosition().y ), pstackIter->second->GetId() );
431  }
432  }
433  else if( TRACK* track = dyn_cast<TRACK*>( item ) )
434  {
435  const auto layerName = m_board->GetLayerName( track->GetLayer() );
436 
437  m_out->Print( 1, "(SEG X1=%.10f Y1=%.10f X2=%.10f Y2=%.10f W=%.10f L=\"%s\")\n",
438  iu2hyp( track->GetStart().x ), iu2hyp( track->GetStart().y ),
439  iu2hyp( track->GetEnd().x ), iu2hyp( track->GetEnd().y ),
440  iu2hyp( track->GetWidth() ), (const char*) layerName.c_str() );
441  }
442  else if( ZONE_CONTAINER* zone = dyn_cast<ZONE_CONTAINER*>( item ) )
443  {
444  const auto layerName = m_board->GetLayerName( zone->GetLayer() );
445  SHAPE_POLY_SET filledShape = zone->GetFilledPolysList();
446 
447  filledShape.Simplify( SHAPE_POLY_SET::PM_FAST );
448 
449  for( int i = 0; i < filledShape.OutlineCount(); i++ )
450  {
451  const auto& outl = filledShape.COutline( i );
452 
453  auto p0 = outl.CPoint( 0 );
454  m_out->Print( 1, "{POLYGON T=POUR L=\"%s\" ID=%d X=%.10f Y=%.10f\n",
455  (const char*) layerName.c_str(), m_polyId, iu2hyp( p0.x ), iu2hyp( p0.y ) );
456 
457  for( int v = 0; v < outl.PointCount(); v++ )
458  {
459  m_out->Print( 2, "(LINE X=%.10f Y=%.10f)\n", iu2hyp( outl.CPoint( v ).x ),
460  iu2hyp( outl.CPoint( v ).y ) );
461  }
462 
463  m_out->Print( 2, "(LINE X=%.10f Y=%.10f)\n", iu2hyp( p0.x ), iu2hyp( p0.y ) );
464  m_out->Print( 1, "}\n" );
465 
466  for( int h = 0; h < filledShape.HoleCount( i ); h++ )
467  {
468  const auto& holeShape = filledShape.CHole( i, h );
469  auto ph0 = holeShape.CPoint( 0 );
470 
471  m_out->Print( 1, "{POLYVOID ID=%d X=%.10f Y=%.10f\n", m_polyId, iu2hyp( ph0.x ),
472  iu2hyp( ph0.y ) );
473 
474  for( int v = 0; v < holeShape.PointCount(); v++ )
475  {
476  m_out->Print( 2, "(LINE X=%.10f Y=%.10f)\n",
477  iu2hyp( holeShape.CPoint( v ).x ),
478  iu2hyp( holeShape.CPoint( v ).y ) );
479  }
480 
481  m_out->Print( 2, "(LINE X=%.10f Y=%.10f)\n", iu2hyp( ph0.x ), iu2hyp( ph0.y ) );
482  m_out->Print( 1, "}\n" );
483  }
484 
485  m_polyId++;
486  }
487  }
488  }
489 
490  return true;
491 }
492 
493 
494 const std::vector<BOARD_ITEM*> HYPERLYNX_EXPORTER::collectNetObjects( int netcode )
495 {
496  std::vector<BOARD_ITEM*> rv;
497 
498  auto check = [&]( BOARD_CONNECTED_ITEM* item ) -> bool {
499  if( ( item->GetLayerSet() & LSET::AllCuMask() ).none() )
500  return false;
501  if( item->GetNetCode() == netcode || ( netcode < 0 && item->GetNetCode() <= 0 ) )
502  return true;
503  return false;
504  };
505 
506  for( auto mod : m_board->Modules() )
507  {
508  for( auto pad : mod->Pads() )
509  {
510  if( check( pad ) )
511  rv.push_back( pad );
512  }
513  }
514 
515  for( auto item : m_board->Tracks() )
516  if( check( item ) )
517  rv.push_back( item );
518 
519  for( int i = 0; i < m_board->GetAreaCount(); i++ )
520  {
521  auto zone = m_board->GetArea( i );
522  if( check( zone ) )
523  rv.push_back( zone );
524  }
525  return rv;
526 }
527 
528 
530 {
531  m_polyId = 1;
532 
533  for( const auto netInfo : m_board->GetNetInfo() )
534  {
535  int netcode = netInfo->GetNet();
536  bool isNullNet = netInfo->GetNet() <= 0 || netInfo->GetNetname().IsEmpty();
537 
538  if( isNullNet )
539  continue;
540 
541  auto netObjects = collectNetObjects( netcode );
542 
543  if( netObjects.size() )
544  {
545  m_out->Print( 0, "{NET=\"%s\"\n", (const char*) netInfo->GetNetname().c_str() );
546  writeNetObjects( netObjects );
547  m_out->Print( 0, "}\n\n" );
548  }
549  }
550 
551  auto nullNetObjects = collectNetObjects( -1 );
552 
553  int idx = 0;
554 
555  for( auto item : nullNetObjects )
556  {
557  m_out->Print( 0, "{NET=\"EmptyNet%d\"\n", idx );
558  writeNetObjects( { item } );
559  m_out->Print( 0, "}\n\n" );
560  idx++;
561  }
562 
563  return true;
564 }
565 
566 
568 {
569  LOCALE_IO toggle; // toggles on, then off, the C locale.
570 
571  try
572  {
573  m_out.reset( new FILE_OUTPUTFORMATTER( m_outputFilePath.GetFullPath() ) );
574 
575  generateHeaders();
576  writeBoardInfo();
578  writeDevices();
579  writePadStacks();
580  writeNets();
581  }
582  catch( IO_ERROR& err )
583  {
584  return false;
585  }
586 
587  return true;
588 }
589 
590 
591 bool ExportBoardToHyperlynx( BOARD* aBoard, const wxFileName& aPath )
592 {
593  HYPERLYNX_EXPORTER exporter;
594  exporter.SetBoard( aBoard );
595  exporter.SetOutputFilename( aPath );
596  return exporter.Run();
597 }
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Function AllCuMask returns a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:676
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:57
bool writeNetObjects(const std::vector< BOARD_ITEM * > &aObjects)
LSEQ CuStack() const
Function CuStack returns a sequence of copper layers in starting from the front/top and extending to ...
Definition: lset.cpp:150
bool ExportBoardToHyperlynx(BOARD *aBoard, const wxFileName &aPath)
int OutlineCount() const
Returns the number of outlines in the set
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Function GetLayerName returns the name of a layer given by aLayer.
like PAD_STANDARD, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:66
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:154
PAD_ATTR_T
Enum PAD_ATTR_T is the set of pad shapes, used with D_PAD::{Set,Get}Attribute() The double name is fo...
Definition: pad_shapes.h:59
bool operator==(const HYPERLYNX_PAD_STACK &other) const
int IsSupportedByExporter() const
Classes BOARD_ITEM and BOARD_CONNECTED_ITEM.
Smd pad, appears on the solder paste layer (default)
Definition: pad_shapes.h:62
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings.
Definition: class_board.h:540
const SHAPE_LINE_CHAIN & CHole(int aOutline, int aHole) const
LSET GetEnabledLayers() const
Function GetEnabledLayers is a proxy function that calls the corresponding function in m_BoardSetting...
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Function Seq returns an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:367
const wxString & GetFileName() const
Definition: class_board.h:225
Class BOARD_CONNECTED_ITEM is a base class derived from BOARD_ITEM for items that can be connected an...
PAD_SHAPE_T
Enum PAD_SHAPE_T is the set of pad shapes, used with D_PAD::{Set,Get}Shape()
Definition: pad_shapes.h:31
Definitions for tracks, vias and zones.
std::vector< HYPERLYNX_PAD_STACK * > m_padStacks
void SetOutputFilename(const wxFileName &aPath)
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
HYPERLYNX_PAD_STACK * addPadStack(HYPERLYNX_PAD_STACK stack)
PCB_LAYER_ID
A quick note on layer IDs:
int GetAreaCount() const
Function GetAreaCount.
Definition: class_board.h:955
Class LSET is a set of PCB_LAYER_IDs.
int GetDrillValue() const
Function GetDrillValue "calculates" the drill value for vias (m-Drill if > 0, or default drill value ...
MODULES & Modules()
Definition: class_board.h:236
Class SHAPE_POLY_SET.
std::map< BOARD_ITEM *, HYPERLYNX_PAD_STACK * > m_padMap
LSET GetLayerSet() const override
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
Definition: class_pad.h:426
HYPERLYNX_PAD_STACK(BOARD *aBoard, const D_PAD *aPad)
void Simplify(POLYGON_MODE aFastMode)
Simplifies the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFast...
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, wxString *aErrorText=nullptr, wxPoint *aErrorLocation=nullptr)
Function GetBoardPolygonOutlines Extracts the board outlines and build a closed polygon from lines,...
NETINFO_LIST & GetNetInfo()
Definition: class_board.h:750
#define _(s)
int HoleCount(int aOutline) const
Returns the number of holes in a given outline
string & err
Definition: json11.cpp:598
const std::string formatPadShape(HYPERLYNX_PAD_STACK &aStack)
PCB_LAYER_ID getSMDLayer() const
void writeSinglePadStack(HYPERLYNX_PAD_STACK &aStack)
int GetWidth() const
Definition: class_track.h:103
Class to handle a graphic segment.
const char * name
Definition: DXF_plotter.cpp:61
const SEG CSegment(int aIndex) const
Function CSegment()
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:161
LSET GetEnabledLayers() const
Function GetEnabledLayers returns a bit-mask of all the layers that are enabled.
void SetBoard(BOARD *aBoard)
double GetOrientation() const
Function GetOrientation returns the rotation angle of the pad in tenths of degrees,...
Definition: class_pad.h:406
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
size_t i
Definition: json11.cpp:597
const wxSize & GetDrillSize() const
Definition: class_pad.h:290
const std::vector< BOARD_ITEM * > collectNetObjects(int netcode)
std::shared_ptr< FILE_OUTPUTFORMATTER > m_out
virtual bool Run() override
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
Class FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492
PAD_SHAPE_T GetShape() const
Function GetShape.
Definition: class_pad.h:221
Module description (excepted pads)
ZONE_CONTAINER * GetArea(int index) const
Function GetArea returns the Area (Zone Container) at a given index.
Definition: class_board.h:926
const wxSize & GetSize() const
Definition: class_pad.h:284
EDGE_MODULE class definition.
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
#define mod(a, n)
Definition: greymap.cpp:24
static double iu2hyp(double iu)
TRACKS & Tracks()
Definition: class_board.h:227