KiCad PCB EDA Suite
export_gencad.cpp File Reference

Export GenCAD 1.4 format. More...

#include <build_version.h>
#include <board.h>
#include <board_design_settings.h>
#include <convert_basic_shapes_to_polygon.h>
#include <fp_shape.h>
#include <footprint.h>
#include <pad.h>
#include <pcb_track.h>
#include <confirm.h>
#include <core/arraydim.h>
#include <dialogs/dialog_gencad_export_options.h>
#include <locale_io.h>
#include <macros.h>
#include <hash_eda.h>
#include <pcb_edit_frame.h>
#include <pcbnew_settings.h>
#include <pgm_base.h>
#include <project/project_file.h>
#include <wx/app.h>
#include <wx/filedlg.h>

Go to the source code of this file.

Functions

static bool CreateHeaderInfoData (FILE *aFile, PCB_EDIT_FRAME *frame)
 
static void CreateArtworksSection (FILE *aFile)
 
static void CreateTracksInfoData (FILE *aFile, BOARD *aPcb)
 
static void CreateBoardSection (FILE *aFile, BOARD *aPcb)
 
static void CreateComponentsSection (FILE *aFile, BOARD *aPcb)
 
static void CreateDevicesSection (FILE *aFile, BOARD *aPcb)
 
static void CreateRoutesSection (FILE *aFile, BOARD *aPcb)
 
static void CreateSignalsSection (FILE *aFile, BOARD *aPcb)
 
static void CreateShapesSection (FILE *aFile, BOARD *aPcb)
 
static void CreatePadsShapesSection (FILE *aFile, BOARD *aPcb)
 
static void FootprintWriteShape (FILE *File, FOOTPRINT *aFootprint, const wxString &aShapeName)
 
static std::string GenCADLayerName (int aCuCount, PCB_LAYER_ID aId)
 
static std::string GenCADLayerNameFlipped (int aCuCount, PCB_LAYER_ID aId)
 
static wxString escapeString (const wxString &aString)
 
static std::string fmt_mask (LSET aSet)
 
static const wxString getShapeName (FOOTPRINT *aFootprint)
 
static double MapXTo (int aX)
 
static double MapYTo (int aY)
 
static bool ViaSort (const PCB_VIA *aPadref, const PCB_VIA *aPadcmp)
 
static size_t hashFootprint (const FOOTPRINT *aFootprint)
 Compute hashes for footprints without taking into account their position, rotation or layer. More...
 

Variables

static const PCB_LAYER_ID gc_seq []
 
static bool flipBottomPads
 
static bool uniquePins
 
static bool individualShapes
 
static bool storeOriginCoords
 
static int GencadOffsetX
 
static int GencadOffsetY
 
static std::map< FOOTPRINT *, int > componentShapes
 
static std::map< int, wxString > shapeNames
 
static const double SCALE_FACTOR = 1000.0 * IU_PER_MILS
 

Detailed Description

Export GenCAD 1.4 format.

Definition in file export_gencad.cpp.

Function Documentation

◆ CreateArtworksSection()

static void CreateArtworksSection ( FILE *  aFile)
static

Definition at line 353 of file export_gencad.cpp.

354 {
355  /* The artworks section is empty */
356  fputs( "$ARTWORKS\n", aFile );
357  fputs( "$ENDARTWORKS\n\n", aFile );
358 }

Referenced by PCB_EDIT_FRAME::ExportToGenCAD().

◆ CreateBoardSection()

static void CreateBoardSection ( FILE *  aFile,
BOARD aPcb 
)
static

Definition at line 1123 of file export_gencad.cpp.

1124 {
1125  fputs( "$BOARD\n", aFile );
1126 
1127  // Extract the board edges
1128  for( BOARD_ITEM* drawing : aPcb->Drawings() )
1129  {
1130  if( drawing->Type() == PCB_SHAPE_T )
1131  {
1132  PCB_SHAPE* drawseg = static_cast<PCB_SHAPE*>( drawing );
1133 
1134  if( drawseg->GetLayer() == Edge_Cuts )
1135  {
1136  // XXX GenCAD supports arc boundaries but I've seen nothing that reads them
1137  fprintf( aFile, "LINE %g %g %g %g\n",
1138  MapXTo( drawseg->GetStart().x ), MapYTo( drawseg->GetStart().y ),
1139  MapXTo( drawseg->GetEnd().x ), MapYTo( drawseg->GetEnd().y ) );
1140  }
1141  }
1142  }
1143 
1144  fputs( "$ENDBOARD\n\n", aFile );
1145 }
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
const wxPoint & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:97
static double MapYTo(int aY)
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:122
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:90
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:140
DRAWINGS & Drawings()
Definition: board.h:236
static double MapXTo(int aX)

References BOARD::Drawings(), Edge_Cuts, EDA_SHAPE::GetEnd(), BOARD_ITEM::GetLayer(), EDA_SHAPE::GetStart(), MapXTo(), MapYTo(), and PCB_SHAPE_T.

Referenced by PCB_EDIT_FRAME::ExportToGenCAD().

◆ CreateComponentsSection()

static void CreateComponentsSection ( FILE *  aFile,
BOARD aPcb 
)
static

Definition at line 841 of file export_gencad.cpp.

842 {
843  fputs( "$COMPONENTS\n", aFile );
844 
845  int cu_count = aPcb->GetCopperLayerCount();
846 
847  for( FOOTPRINT* footprint : aPcb->Footprints() )
848  {
849  const char* mirror;
850  const char* flip;
851  double fp_orient = footprint->GetOrientation();
852 
853  if( footprint->GetFlag() )
854  {
855  mirror = "MIRRORX";
856  flip = "FLIP";
857  NEGATE_AND_NORMALIZE_ANGLE_POS( fp_orient );
858  }
859  else
860  {
861  mirror = "0";
862  flip = "0";
863  }
864 
865  fprintf( aFile, "\nCOMPONENT \"%s\"\n",
866  TO_UTF8( escapeString( footprint->GetReference() ) ) );
867  fprintf( aFile, "DEVICE \"DEV_%s\"\n",
868  TO_UTF8( escapeString( getShapeName( footprint ) ) ) );
869  fprintf( aFile, "PLACE %g %g\n",
870  MapXTo( footprint->GetPosition().x ),
871  MapYTo( footprint->GetPosition().y ) );
872  fprintf( aFile, "LAYER %s\n",
873  footprint->GetFlag() ? "BOTTOM" : "TOP" );
874  fprintf( aFile, "ROTATION %g\n",
875  fp_orient / 10.0 );
876  fprintf( aFile, "SHAPE \"%s\" %s %s\n",
877  TO_UTF8( escapeString( getShapeName( footprint ) ) ),
878  mirror, flip );
879 
880  // Text on silk layer: RefDes and value (are they actually useful?)
881  for( FP_TEXT* textItem : { &footprint->Reference(), &footprint->Value() } )
882  {
883  double txt_orient = textItem->GetTextAngle();
884  std::string layer = GenCADLayerName( cu_count, footprint->GetFlag() ? B_SilkS : F_SilkS );
885 
886  fprintf( aFile, "TEXT %g %g %g %g %s %s \"%s\"",
887  textItem->GetPos0().x / SCALE_FACTOR,
888  -textItem->GetPos0().y / SCALE_FACTOR,
889  textItem->GetTextWidth() / SCALE_FACTOR,
890  txt_orient / 10.0,
891  mirror,
892  layer.c_str(),
893  TO_UTF8( escapeString( textItem->GetText() ) ) );
894 
895  // Please note, the width is approx
896  fprintf( aFile, " 0 0 %g %g\n",
897  ( textItem->GetTextWidth() * textItem->GetLength() ) / SCALE_FACTOR,
898  textItem->GetTextHeight() / SCALE_FACTOR );
899  }
900 
901  // The SHEET is a 'generic description' for referencing the component
902  fprintf( aFile, "SHEET \"RefDes: %s, Value: %s\"\n",
903  TO_UTF8( footprint->GetReference() ),
904  TO_UTF8( footprint->GetValue() ) );
905  }
906 
907  fputs( "$ENDCOMPONENTS\n\n", aFile );
908 }
static const wxString getShapeName(FOOTPRINT *aFootprint)
static double MapYTo(int aY)
static wxString escapeString(const wxString &aString)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
FOOTPRINTS & Footprints()
Definition: board.h:233
static std::string GenCADLayerName(int aCuCount, PCB_LAYER_ID aId)
void NEGATE_AND_NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:362
int GetCopperLayerCount() const
Definition: board.cpp:455
static const double SCALE_FACTOR
static double MapXTo(int aX)

References B_SilkS, escapeString(), F_SilkS, BOARD::Footprints(), GenCADLayerName(), BOARD::GetCopperLayerCount(), getShapeName(), MapXTo(), MapYTo(), NEGATE_AND_NORMALIZE_ANGLE_POS(), SCALE_FACTOR, and TO_UTF8.

Referenced by PCB_EDIT_FRAME::ExportToGenCAD().

◆ CreateDevicesSection()

static void CreateDevicesSection ( FILE *  aFile,
BOARD aPcb 
)
static

Definition at line 1091 of file export_gencad.cpp.

1092 {
1093  std::set<wxString> emitted;
1094  fputs( "$DEVICES\n", aFile );
1095 
1096  for( const auto& componentShape : componentShapes )
1097  {
1098  const wxString& shapeName = shapeNames[componentShape.second];
1099  bool newDevice;
1100  std::tie( std::ignore, newDevice ) = emitted.insert( shapeName );
1101 
1102  if( !newDevice ) // do not repeat device definitions
1103  continue;
1104 
1105  const FOOTPRINT* footprint = componentShape.first;
1106 
1107  fprintf( aFile, "\nDEVICE \"DEV_%s\"\n", TO_UTF8( escapeString( shapeName ) ) );
1108 
1109  fprintf( aFile, "PART \"%s\"\n",
1110  TO_UTF8( escapeString( footprint->GetValue() ) ) );
1111 
1112  fprintf( aFile, "PACKAGE \"%s\"\n",
1113  TO_UTF8( escapeString( footprint->GetFPID().Format() ) ) );
1114  }
1115 
1116  fputs( "$ENDDEVICES\n\n", aFile );
1117 }
const wxString & GetValue() const
Definition: footprint.h:485
static std::map< FOOTPRINT *, int > componentShapes
static wxString escapeString(const wxString &aString)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
const LIB_ID & GetFPID() const
Definition: footprint.h:194
UTF8 Format() const
Definition: lib_id.cpp:116
static std::map< int, wxString > shapeNames

References componentShapes, escapeString(), LIB_ID::Format(), FOOTPRINT::GetFPID(), FOOTPRINT::GetValue(), shapeNames, and TO_UTF8.

Referenced by PCB_EDIT_FRAME::ExportToGenCAD().

◆ CreateHeaderInfoData()

static bool CreateHeaderInfoData ( FILE *  aFile,
PCB_EDIT_FRAME frame 
)
static

Definition at line 960 of file export_gencad.cpp.

961 {
962  wxString msg;
963  BOARD* board = aFrame->GetBoard();
964 
965  fputs( "$HEADER\n", aFile );
966  fputs( "GENCAD 1.4\n", aFile );
967 
968  // Please note: GenCAD syntax requires quoted strings if they can contain spaces
969  msg.Printf( wxT( "USER \"%s %s\"\n" ),
970  Pgm().App().GetAppName(),
971  GetBuildVersion() );
972  fputs( TO_UTF8( msg ), aFile );
973 
974  msg = wxT( "DRAWING \"" ) + board->GetFileName() + wxT( "\"\n" );
975  fputs( TO_UTF8( msg ), aFile );
976 
977  const TITLE_BLOCK& tb = aFrame->GetTitleBlock();
978 
979  msg = wxT( "REVISION \"" ) + tb.GetRevision() + wxT( " " ) + tb.GetDate() + wxT( "\"\n" );
980 
981  fputs( TO_UTF8( msg ), aFile );
982  fputs( "UNITS INCH\n", aFile );
983 
984  // giving 0 as the argument to Map{X,Y}To returns the scaled origin point
985  msg.Printf( wxT( "ORIGIN %g %g\n" ),
986  storeOriginCoords ? MapXTo( 0 ) : 0,
987  storeOriginCoords ? MapYTo( 0 ) : 0 );
988  fputs( TO_UTF8( msg ), aFile );
989 
990  fputs( "INTERTRACK 0\n", aFile );
991  fputs( "$ENDHEADER\n\n", aFile );
992 
993  return true;
994 }
static double MapYTo(int aY)
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
const wxString & GetFileName() const
Definition: board.h:228
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition: title_block.h:40
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
wxString GetBuildVersion()
Get the full KiCad version string.
const wxString & GetRevision() const
Definition: title_block.h:86
static bool storeOriginCoords
const wxString & GetDate() const
Definition: title_block.h:76
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:36
static double MapXTo(int aX)

References PCB_BASE_FRAME::GetBoard(), GetBuildVersion(), TITLE_BLOCK::GetDate(), BOARD::GetFileName(), TITLE_BLOCK::GetRevision(), PCB_BASE_FRAME::GetTitleBlock(), MapXTo(), MapYTo(), Pgm(), storeOriginCoords, and TO_UTF8.

Referenced by PCB_EDIT_FRAME::ExportToGenCAD().

◆ CreatePadsShapesSection()

static void CreatePadsShapesSection ( FILE *  aFile,
BOARD aPcb 
)
static

Definition at line 363 of file export_gencad.cpp.

364 {
365  std::vector<PAD*> padstacks;
366  std::vector<PCB_VIA*> vias;
367  std::vector<PCB_VIA*> viastacks;
368 
369  padstacks.resize( 1 ); // We count pads from 1
370 
371  // The master layermask (i.e. the enabled layers) for padstack generation
372  LSET master_layermask = aPcb->GetDesignSettings().GetEnabledLayers();
373  int cu_count = aPcb->GetCopperLayerCount();
374 
375  fputs( "$PADS\n", aFile );
376 
377  // Enumerate and sort the pads
378 
379  std::vector<PAD*> pads = aPcb->GetPads();
380  std::sort( pads.begin(), pads.end(), []( const PAD* a, const PAD* b )
381  {
382  return PAD::Compare( a, b ) < 0;
383  } );
384 
385 
386  // The same for vias
387  for( PCB_TRACK* track : aPcb->Tracks() )
388  {
389  if( PCB_VIA* via = dyn_cast<PCB_VIA*>( track ) )
390  vias.push_back( via );
391  }
392 
393  std::sort( vias.begin(), vias.end(), ViaSort );
394  vias.erase( std::unique( vias.begin(), vias.end(), []( const PCB_VIA* a, const PCB_VIA* b )
395  {
396  return ViaSort( a, b ) == false;
397  } ),
398  vias.end() );
399 
400  // Emit vias pads
401 
402  for( PCB_VIA* via : vias )
403  {
404  viastacks.push_back( via );
405  fprintf( aFile, "PAD V%d.%d.%s ROUND %g\nCIRCLE 0 0 %g\n",
406  via->GetWidth(), via->GetDrillValue(),
407  fmt_mask( via->GetLayerSet() & master_layermask ).c_str(),
408  via->GetDrillValue() / SCALE_FACTOR,
409  via->GetWidth() / (SCALE_FACTOR * 2) );
410  }
411 
412  // Emit component pads
413  PAD* old_pad = nullptr;
414  int pad_name_number = 0;
415 
416  for( unsigned i = 0; i<pads.size(); ++i )
417  {
418  PAD* pad = pads[i];
419  const wxPoint& off = pad->GetOffset();
420 
421  pad->SetSubRatsnest( pad_name_number );
422 
423  if( old_pad && 0 == PAD::Compare( old_pad, pad ) )
424  continue; // already created
425 
426  old_pad = pad;
427 
428  pad_name_number++;
429  pad->SetSubRatsnest( pad_name_number );
430 
431  fprintf( aFile, "PAD P%d", pad->GetSubRatsnest() );
432 
433  padstacks.push_back( pad ); // Will have its own padstack later
434  int dx = pad->GetSize().x / 2;
435  int dy = pad->GetSize().y / 2;
436 
437  switch( pad->GetShape() )
438  {
439  default:
440  wxASSERT_MSG( false, "Pad type not implemented" );
442 
443  case PAD_SHAPE::CIRCLE:
444  fprintf( aFile, " ROUND %g\n",
445  pad->GetDrillSize().x / SCALE_FACTOR );
446  /* Circle is center, radius */
447  fprintf( aFile, "CIRCLE %g %g %g\n",
448  off.x / SCALE_FACTOR,
449  -off.y / SCALE_FACTOR,
450  pad->GetSize().x / (SCALE_FACTOR * 2) );
451  break;
452 
453  case PAD_SHAPE::RECT:
454  fprintf( aFile, " RECTANGULAR %g\n",
455  pad->GetDrillSize().x / SCALE_FACTOR );
456 
457  // Rectangle is begin, size *not* begin, end!
458  fprintf( aFile, "RECTANGLE %g %g %g %g\n",
459  (-dx + off.x ) / SCALE_FACTOR,
460  (-dy - off.y ) / SCALE_FACTOR,
461  dx / (SCALE_FACTOR / 2), dy / (SCALE_FACTOR / 2) );
462  break;
463 
465  case PAD_SHAPE::OVAL:
466  {
467  const wxSize& size = pad->GetSize();
468  int radius = std::min( size.x, size.y ) / 2;
469 
470  if( pad->GetShape() == PAD_SHAPE::ROUNDRECT )
471  {
472  radius = pad->GetRoundRectCornerRadius();
473  }
474 
475  int lineX = size.x / 2 - radius;
476  int lineY = size.y / 2 - radius;
477 
478  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
479 
480  // bottom left arc
481  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
482  ( off.x - lineX - radius ) / SCALE_FACTOR,
483  ( -off.y - lineY ) / SCALE_FACTOR, ( off.x - lineX ) / SCALE_FACTOR,
484  ( -off.y - lineY - radius ) / SCALE_FACTOR,
485  ( off.x - lineX ) / SCALE_FACTOR, ( -off.y - lineY ) / SCALE_FACTOR );
486  // bottom line
487  if( lineX > 0 )
488  {
489  fprintf( aFile, "LINE %g %g %g %g\n",
490  ( off.x - lineX ) / SCALE_FACTOR,
491  ( -off.y - lineY - radius ) / SCALE_FACTOR,
492  ( off.x + lineX ) / SCALE_FACTOR,
493  ( -off.y - lineY - radius ) / SCALE_FACTOR );
494  }
495 
496  // bottom right arc
497  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
498  ( off.x + lineX ) / SCALE_FACTOR,
499  ( -off.y - lineY - radius ) / SCALE_FACTOR,
500  ( off.x + lineX + radius ) / SCALE_FACTOR,
501  ( -off.y - lineY ) / SCALE_FACTOR, ( off.x + lineX ) / SCALE_FACTOR,
502  ( -off.y - lineY ) / SCALE_FACTOR );
503 
504  // right line
505  if( lineY > 0 )
506  {
507  fprintf( aFile, "LINE %g %g %g %g\n",
508  ( off.x + lineX + radius ) / SCALE_FACTOR,
509  ( -off.y + lineY ) / SCALE_FACTOR,
510  ( off.x + lineX + radius ) / SCALE_FACTOR,
511  ( -off.y - lineY ) / SCALE_FACTOR );
512  }
513 
514  // top right arc
515  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
516  ( off.x + lineX + radius ) / SCALE_FACTOR,
517  ( -off.y + lineY ) / SCALE_FACTOR, ( off.x + lineX ) / SCALE_FACTOR,
518  ( -off.y + lineY + radius ) / SCALE_FACTOR,
519  ( off.x + lineX ) / SCALE_FACTOR, ( -off.y + lineY ) / SCALE_FACTOR );
520 
521  // top line
522  if( lineX > 0 )
523  {
524  fprintf( aFile, "LINE %g %g %g %g\n"
525  , ( off.x - lineX ) / SCALE_FACTOR,
526  ( -off.y + lineY + radius ) / SCALE_FACTOR,
527  ( off.x + lineX ) / SCALE_FACTOR,
528  ( -off.y + lineY + radius ) / SCALE_FACTOR );
529  }
530 
531  // top left arc
532  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
533  ( off.x - lineX ) / SCALE_FACTOR,
534  ( -off.y + lineY + radius ) / SCALE_FACTOR,
535  ( off.x - lineX - radius ) / SCALE_FACTOR,
536  ( -off.y + lineY ) / SCALE_FACTOR, ( off.x - lineX ) / SCALE_FACTOR,
537  ( -off.y + lineY ) / SCALE_FACTOR );
538 
539  // left line
540  if( lineY > 0 )
541  {
542  fprintf( aFile, "LINE %g %g %g %g\n",
543  ( off.x - lineX - radius ) / SCALE_FACTOR,
544  ( -off.y - lineY ) / SCALE_FACTOR,
545  ( off.x - lineX - radius ) / SCALE_FACTOR,
546  ( -off.y + lineY ) / SCALE_FACTOR );
547  }
548  }
549  break;
550 
552  {
553  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
554 
555  int ddx = pad->GetDelta().x / 2;
556  int ddy = pad->GetDelta().y / 2;
557 
558  wxPoint poly[4];
559  poly[0] = wxPoint( -dx + ddy, dy + ddx );
560  poly[1] = wxPoint( dx - ddy, dy - ddx );
561  poly[2] = wxPoint( dx + ddy, -dy + ddx );
562  poly[3] = wxPoint( -dx - ddy, -dy - ddx );
563 
564  for( int cur = 0; cur < 4; ++cur )
565  {
566  int next = ( cur + 1 ) % 4;
567  fprintf( aFile, "LINE %g %g %g %g\n",
568  ( off.x + poly[cur].x ) / SCALE_FACTOR,
569  ( -off.y - poly[cur].y ) / SCALE_FACTOR,
570  ( off.x + poly[next].x ) / SCALE_FACTOR,
571  ( -off.y - poly[next].y ) / SCALE_FACTOR );
572  }
573  }
574  break;
575 
577  {
578  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
579 
580  SHAPE_POLY_SET outline;
581  int maxError = aPcb->GetDesignSettings().m_MaxError;
582 
583  TransformRoundChamferedRectToPolygon( outline, pad->GetPosition(), pad->GetSize(),
584  pad->GetOrientation(), pad->GetRoundRectCornerRadius(),
585  pad->GetChamferRectRatio(), pad->GetChamferPositions(), 0, maxError,
586  ERROR_INSIDE );
587 
588  for( int jj = 0; jj < outline.OutlineCount(); ++jj )
589  {
590  const SHAPE_LINE_CHAIN& poly = outline.COutline( jj );
591  int pointCount = poly.PointCount();
592 
593  for( int ii = 0; ii < pointCount; ii++ )
594  {
595  int next = ( ii + 1 ) % pointCount;
596  fprintf( aFile, "LINE %g %g %g %g\n",
597  ( off.x + poly.CPoint( ii ).x ) / SCALE_FACTOR,
598  ( -off.y - poly.CPoint( ii ).y ) / SCALE_FACTOR,
599  ( off.x + poly.CPoint( next ).x ) / SCALE_FACTOR,
600  ( -off.y - poly.CPoint( next ).y ) / SCALE_FACTOR );
601  }
602  }
603 
604  break;
605  }
606 
607  case PAD_SHAPE::CUSTOM:
608  {
609  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
610 
611  SHAPE_POLY_SET outline;
612  pad->MergePrimitivesAsPolygon( &outline );
613 
614  for( int jj = 0; jj < outline.OutlineCount(); ++jj )
615  {
616  const SHAPE_LINE_CHAIN& poly = outline.COutline( jj );
617  int pointCount = poly.PointCount();
618 
619  for( int ii = 0; ii < pointCount; ii++ )
620  {
621  int next = ( ii + 1 ) % pointCount;
622  fprintf( aFile, "LINE %g %g %g %g\n",
623  ( off.x + poly.CPoint( ii ).x ) / SCALE_FACTOR,
624  ( -off.y - poly.CPoint( ii ).y ) / SCALE_FACTOR,
625  ( off.x + poly.CPoint( next ).x ) / SCALE_FACTOR,
626  ( -off.y - poly.CPoint( next ).y ) / SCALE_FACTOR );
627  }
628  }
629  }
630  break;
631  }
632  }
633 
634  fputs( "\n$ENDPADS\n\n", aFile );
635 
636  // Now emit the padstacks definitions, using the combined layer masks
637  fputs( "$PADSTACKS\n", aFile );
638 
639  // Via padstacks
640  for( unsigned i = 0; i < viastacks.size(); i++ )
641  {
642  PCB_VIA* via = viastacks[i];
643 
644  LSET mask = via->GetLayerSet() & master_layermask;
645 
646  fprintf( aFile, "PADSTACK VIA%d.%d.%s %g\n",
647  via->GetWidth(), via->GetDrillValue(),
648  fmt_mask( mask ).c_str(),
649  via->GetDrillValue() / SCALE_FACTOR );
650 
651  for( LSEQ seq = mask.Seq( gc_seq, arrayDim( gc_seq ) ); seq; ++seq )
652  {
653  PCB_LAYER_ID layer = *seq;
654 
655  fprintf( aFile, "PAD V%d.%d.%s %s 0 0\n",
656  via->GetWidth(), via->GetDrillValue(),
657  fmt_mask( mask ).c_str(),
658  GenCADLayerName( cu_count, layer ).c_str()
659  );
660  }
661  }
662 
663  /* Component padstacks
664  * Older versions of CAM350 don't apply correctly the FLIP semantics for
665  * padstacks, i.e. doesn't swap the top and bottom layers... so I need to
666  * define the shape as MIRRORX and define a separate 'flipped' padstack...
667  * until it appears yet another noncompliant importer */
668  for( unsigned i = 1; i < padstacks.size(); i++ )
669  {
670  PAD* pad = padstacks[i];
671 
672  // Straight padstack
673  fprintf( aFile, "PADSTACK PAD%u %g\n", i, pad->GetDrillSize().x / SCALE_FACTOR );
674 
675  LSET pad_set = pad->GetLayerSet() & master_layermask;
676 
677  // the special gc_seq
678  for( LSEQ seq = pad_set.Seq( gc_seq, arrayDim( gc_seq ) ); seq; ++seq )
679  {
680  PCB_LAYER_ID layer = *seq;
681 
682  fprintf( aFile, "PAD P%u %s 0 0\n", i, GenCADLayerName( cu_count, layer ).c_str() );
683  }
684 
685  // Flipped padstack
686  if( flipBottomPads )
687  {
688  fprintf( aFile, "PADSTACK PAD%uF %g\n", i, pad->GetDrillSize().x / SCALE_FACTOR );
689 
690  // the normal PCB_LAYER_ID sequence is inverted from gc_seq[]
691  for( LSEQ seq = pad_set.Seq(); seq; ++seq )
692  {
693  PCB_LAYER_ID layer = *seq;
694 
695  fprintf( aFile, "PAD P%u %s 0 0\n", i, GenCADLayerNameFlipped( cu_count, layer ).c_str() );
696  }
697  }
698  }
699 
700  fputs( "$ENDPADSTACKS\n\n", aFile );
701 }
CITER next(CITER it)
Definition: ptree.cpp:126
static int Compare(const PAD *padref, const PAD *padcmp)
Compare two pads and return 0 if they are equal.
Definition: pad.cpp:1023
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
static bool flipBottomPads
int PointCount() const
Return the number of points (vertices) in this line chain.
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:411
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:590
void TransformRoundChamferedRectToPolygon(SHAPE_POLY_SET &aCornerBuffer, const wxPoint &aPosition, const wxSize &aSize, double aRotation, int aCornerRadius, double aChamferRatio, int aChamferCorners, int aInflate, int aError, ERROR_LOC aErrorLoc)
Convert a rectangle with rounded corners and/or chamfered corners to a polygon.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
static std::string GenCADLayerNameFlipped(int aCuCount, PCB_LAYER_ID aId)
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:504
Represent a set of closed polygons.
const std::vector< PAD * > GetPads() const
Return a reference to a list of all the pads.
Definition: board.cpp:1895
static const PCB_LAYER_ID gc_seq[]
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition: layer_ids.h:465
static std::string GenCADLayerName(int aCuCount, PCB_LAYER_ID aId)
static bool ViaSort(const PCB_VIA *aPadref, const PCB_VIA *aPadcmp)
LSET GetEnabledLayers() const
Return a bit-mask of all the layers that are enabled.
Represent a polyline (an zero-thickness chain of connected line segments).
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:65
int GetCopperLayerCount() const
Definition: board.cpp:455
static const double SCALE_FACTOR
static std::string fmt_mask(LSET aSet)
Definition: pad.h:57
TRACKS & Tracks()
Definition: board.h:230

References arrayDim(), CHAMFERED_RECT, CIRCLE, PAD::Compare(), SHAPE_LINE_CHAIN::CPoint(), CUSTOM, ERROR_INSIDE, flipBottomPads, fmt_mask(), gc_seq, GenCADLayerName(), GenCADLayerNameFlipped(), BOARD::GetCopperLayerCount(), BOARD::GetDesignSettings(), BOARD_DESIGN_SETTINGS::GetEnabledLayers(), BOARD::GetPads(), KI_FALLTHROUGH, BOARD_DESIGN_SETTINGS::m_MaxError, next(), OVAL, pad, SHAPE_LINE_CHAIN::PointCount(), RECT, ROUNDRECT, SCALE_FACTOR, LSET::Seq(), BOARD::Tracks(), TransformRoundChamferedRectToPolygon(), TRAPEZOID, via, ViaSort(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by PCB_EDIT_FRAME::ExportToGenCAD().

◆ CreateRoutesSection()

static void CreateRoutesSection ( FILE *  aFile,
BOARD aPcb 
)
static

Definition at line 1006 of file export_gencad.cpp.

1007 {
1008  int vianum = 1;
1009  int old_netcode, old_width, old_layer;
1010  LSET master_layermask = aPcb->GetDesignSettings().GetEnabledLayers();
1011 
1012  int cu_count = aPcb->GetCopperLayerCount();
1013 
1014  TRACKS tracks( aPcb->Tracks() );
1015  std::sort( tracks.begin(), tracks.end(),
1016  []( const PCB_TRACK* a, const PCB_TRACK* b )
1017  {
1018  if( a->GetNetCode() == b->GetNetCode() )
1019  {
1020  if( a->GetWidth() == b->GetWidth() )
1021  return ( a->GetLayer() < b->GetLayer() );
1022 
1023  return ( a->GetWidth() < b->GetWidth() );
1024  }
1025 
1026  return ( a->GetNetCode() < b->GetNetCode() );
1027  } );
1028 
1029  fputs( "$ROUTES\n", aFile );
1030 
1031  old_netcode = -1; old_width = -1; old_layer = -1;
1032 
1033  for( PCB_TRACK* track : tracks )
1034  {
1035  if( old_netcode != track->GetNetCode() )
1036  {
1037  old_netcode = track->GetNetCode();
1038  NETINFO_ITEM* net = track->GetNet();
1039  wxString netname;
1040 
1041  if( net && (net->GetNetname() != wxEmptyString) )
1042  netname = net->GetNetname();
1043  else
1044  netname = wxT( "_noname_" );
1045 
1046  fprintf( aFile, "ROUTE \"%s\"\n", TO_UTF8( escapeString( netname ) ) );
1047  }
1048 
1049  if( old_width != track->GetWidth() )
1050  {
1051  old_width = track->GetWidth();
1052  fprintf( aFile, "TRACK TRACK%d\n", track->GetWidth() );
1053  }
1054 
1055  if( track->Type() == PCB_TRACE_T )
1056  {
1057  if( old_layer != track->GetLayer() )
1058  {
1059  old_layer = track->GetLayer();
1060  fprintf( aFile, "LAYER %s\n",
1061  GenCADLayerName( cu_count, track->GetLayer() ).c_str() );
1062  }
1063 
1064  fprintf( aFile, "LINE %g %g %g %g\n",
1065  MapXTo( track->GetStart().x ), MapYTo( track->GetStart().y ),
1066  MapXTo( track->GetEnd().x ), MapYTo( track->GetEnd().y ) );
1067  }
1068 
1069  if( track->Type() == PCB_VIA_T )
1070  {
1071  const PCB_VIA* via = static_cast<const PCB_VIA*>(track);
1072 
1073  LSET vset = via->GetLayerSet() & master_layermask;
1074 
1075  fprintf( aFile, "VIA VIA%d.%d.%s %g %g ALL %g via%d\n",
1076  via->GetWidth(), via->GetDrillValue(),
1077  fmt_mask( vset ).c_str(),
1078  MapXTo( via->GetStart().x ), MapYTo( via->GetStart().y ),
1079  via->GetDrillValue() / SCALE_FACTOR, vianum++ );
1080  }
1081  }
1082 
1083  fputs( "$ENDROUTES\n\n", aFile );
1084 }
static double MapYTo(int aY)
int GetWidth() const
Definition: pcb_track.h:102
static wxString escapeString(const wxString &aString)
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:590
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
void vset(double *v, double x, double y, double z)
Definition: trackball.cpp:82
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:504
const wxString & GetNetname() const
Definition: netinfo.h:119
static std::string GenCADLayerName(int aCuCount, PCB_LAYER_ID aId)
Handle the data for a net.
Definition: netinfo.h:64
LSET GetEnabledLayers() const
Return a bit-mask of all the layers that are enabled.
int GetCopperLayerCount() const
Definition: board.cpp:455
static const double SCALE_FACTOR
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
static std::string fmt_mask(LSET aSet)
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:140
TRACKS & Tracks()
Definition: board.h:230
static double MapXTo(int aX)

References escapeString(), fmt_mask(), GenCADLayerName(), BOARD::GetCopperLayerCount(), BOARD::GetDesignSettings(), BOARD_DESIGN_SETTINGS::GetEnabledLayers(), BOARD_ITEM::GetLayer(), BOARD_CONNECTED_ITEM::GetNetCode(), NETINFO_ITEM::GetNetname(), PCB_TRACK::GetWidth(), MapXTo(), MapYTo(), PCB_TRACE_T, PCB_VIA_T, SCALE_FACTOR, TO_UTF8, BOARD::Tracks(), via, and vset().

Referenced by PCB_EDIT_FRAME::ExportToGenCAD().

◆ CreateShapesSection()

static void CreateShapesSection ( FILE *  aFile,
BOARD aPcb 
)
static

Definition at line 725 of file export_gencad.cpp.

726 {
727  const char* layer;
728  wxString pinname;
729  const char* mirror = "0";
730  std::map<wxString, size_t> shapes;
731 
732  fputs( "$SHAPES\n", aFile );
733 
734  for( FOOTPRINT* footprint : aPcb->Footprints() )
735  {
736  if( !individualShapes )
737  {
738  // Check if such shape has been already generated, and if so - reuse it
739  // It is necessary to compute hash (i.e. check all children objects) as
740  // certain components instances might have been modified on the board.
741  // In such case the shape will be different despite the same LIB_ID.
742  wxString shapeName = footprint->GetFPID().Format();
743 
744  auto shapeIt = shapes.find( shapeName );
745  size_t modHash = hashFootprint( footprint );
746 
747  if( shapeIt != shapes.end() )
748  {
749  if( modHash != shapeIt->second )
750  {
751  // there is an entry for this footprint, but it has a modified shape,
752  // so we need to create a new entry
753  wxString newShapeName;
754  int suffix = 0;
755 
756  // find an unused name or matching entry
757  do
758  {
759  newShapeName = wxString::Format( "%s_%d", shapeName, suffix );
760  shapeIt = shapes.find( newShapeName );
761  ++suffix;
762  }
763  while( shapeIt != shapes.end() && shapeIt->second != modHash );
764 
765  shapeName = newShapeName;
766  }
767 
768  if( shapeIt != shapes.end() && modHash == shapeIt->second )
769  {
770  // shape found, so reuse it
771  componentShapes[footprint] = modHash;
772  continue;
773  }
774  }
775 
776  // new shape
777  componentShapes[footprint] = modHash;
778  shapeNames[modHash] = shapeName;
779  shapes[shapeName] = modHash;
780  FootprintWriteShape( aFile, footprint, shapeName );
781  }
782  else // individual shape for each component
783  {
784  FootprintWriteShape( aFile, footprint, footprint->GetReference() );
785  }
786 
787  // set of already emitted pins to check for duplicates
788  std::set<wxString> pins;
789 
790  for( PAD* pad : footprint->Pads() )
791  {
792  /* Padstacks are defined using the correct layers for the pads, therefore to
793  * all pads need to be marked as TOP to use the padstack information correctly.
794  */
795  layer = "TOP";
796  pinname = pad->GetNumber();
797 
798  if( pinname.IsEmpty() )
799  pinname = wxT( "none" );
800 
801  if( uniquePins )
802  {
803  int suffix = 0;
804  wxString origPinname( pinname );
805 
806  auto it = pins.find( pinname );
807 
808  while( it != pins.end() )
809  {
810  pinname = wxString::Format( "%s_%d", origPinname, suffix );
811  ++suffix;
812  it = pins.find( pinname );
813  }
814 
815  pins.insert( pinname );
816  }
817 
818  double orient = pad->GetOrientation() - footprint->GetOrientation();
819  NORMALIZE_ANGLE_POS( orient );
820 
821  // Bottom side footprints use the flipped padstack
822  fprintf( aFile, ( flipBottomPads && footprint->GetFlag() ) ?
823  "PIN \"%s\" PAD%dF %g %g %s %g %s\n" :
824  "PIN \"%s\" PAD%d %g %g %s %g %s\n",
825  TO_UTF8( escapeString( pinname ) ), pad->GetSubRatsnest(),
826  pad->GetPos0().x / SCALE_FACTOR,
827  -pad->GetPos0().y / SCALE_FACTOR,
828  layer, orient / 10.0, mirror );
829  }
830  }
831 
832  fputs( "$ENDSHAPES\n\n", aFile );
833 }
static std::map< FOOTPRINT *, int > componentShapes
static bool uniquePins
static wxString escapeString(const wxString &aString)
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:290
static bool flipBottomPads
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
static size_t hashFootprint(const FOOTPRINT *aFootprint)
Compute hashes for footprints without taking into account their position, rotation or layer.
static bool individualShapes
FOOTPRINTS & Footprints()
Definition: board.h:233
static void FootprintWriteShape(FILE *File, FOOTPRINT *aFootprint, const wxString &aShapeName)
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
static const double SCALE_FACTOR
Definition: pad.h:57
static std::map< int, wxString > shapeNames

References componentShapes, escapeString(), flipBottomPads, BOARD::Footprints(), FootprintWriteShape(), Format(), hashFootprint(), individualShapes, NORMALIZE_ANGLE_POS(), pad, SCALE_FACTOR, shapeNames, TO_UTF8, and uniquePins.

Referenced by PCB_EDIT_FRAME::ExportToGenCAD().

◆ CreateSignalsSection()

static void CreateSignalsSection ( FILE *  aFile,
BOARD aPcb 
)
static

Definition at line 913 of file export_gencad.cpp.

914 {
915  wxString msg;
916  NETINFO_ITEM* net;
917  int NbNoConn = 1;
918 
919  fputs( "$SIGNALS\n", aFile );
920 
921  for( unsigned ii = 0; ii < aPcb->GetNetCount(); ii++ )
922  {
923  net = aPcb->FindNet( ii );
924 
925  if( net->GetNetname() == wxEmptyString ) // dummy netlist (no connection)
926  {
927  msg.Printf( "NoConnection%d", NbNoConn++ );
928  }
929 
930  if( net->GetNetCode() <= 0 ) // dummy netlist (no connection)
931  continue;
932 
933  msg = wxT( "SIGNAL \"" ) + escapeString( net->GetNetname() ) + "\"";
934 
935  fputs( TO_UTF8( msg ), aFile );
936  fputs( "\n", aFile );
937 
938  for( FOOTPRINT* footprint : aPcb->Footprints() )
939  {
940  for( PAD* pad : footprint->Pads() )
941  {
942  if( pad->GetNetCode() != net->GetNetCode() )
943  continue;
944 
945  msg.Printf( wxT( "NODE \"%s\" \"%s\"" ),
946  escapeString( footprint->GetReference() ),
947  escapeString( pad->GetNumber() ) );
948 
949  fputs( TO_UTF8( msg ), aFile );
950  fputs( "\n", aFile );
951  }
952  }
953  }
954 
955  fputs( "$ENDSIGNALS\n\n", aFile );
956 }
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition: board.cpp:1325
static wxString escapeString(const wxString &aString)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
unsigned GetNetCount() const
Definition: board.h:710
FOOTPRINTS & Footprints()
Definition: board.h:233
const wxString & GetNetname() const
Definition: netinfo.h:119
Handle the data for a net.
Definition: netinfo.h:64
Definition: pad.h:57
int GetNetCode() const
Definition: netinfo.h:113

References escapeString(), BOARD::FindNet(), BOARD::Footprints(), NETINFO_ITEM::GetNetCode(), BOARD::GetNetCount(), NETINFO_ITEM::GetNetname(), pad, and TO_UTF8.

Referenced by PCB_EDIT_FRAME::ExportToGenCAD().

◆ CreateTracksInfoData()

static void CreateTracksInfoData ( FILE *  aFile,
BOARD aPcb 
)
static

Definition at line 1158 of file export_gencad.cpp.

1159 {
1160  // Find thickness used for traces
1161 
1162  std::set<int> trackinfo;
1163 
1164  for( PCB_TRACK* track : aPcb->Tracks() )
1165  trackinfo.insert( track->GetWidth() );
1166 
1167  // Write data
1168  fputs( "$TRACKS\n", aFile );
1169 
1170  for( int size : trackinfo )
1171  fprintf( aFile, "TRACK TRACK%d %g\n", size, size / SCALE_FACTOR );
1172 
1173  fputs( "$ENDTRACKS\n\n", aFile );
1174 }
static const double SCALE_FACTOR
TRACKS & Tracks()
Definition: board.h:230

References SCALE_FACTOR, and BOARD::Tracks().

Referenced by PCB_EDIT_FRAME::ExportToGenCAD().

◆ escapeString()

static wxString escapeString ( const wxString &  aString)
static

Definition at line 169 of file export_gencad.cpp.

170 {
171  wxString copy( aString );
172  copy.Replace( "\"", "\\\"" );
173  return copy;
174 }

References copy.

Referenced by CreateComponentsSection(), CreateDevicesSection(), CreateRoutesSection(), CreateShapesSection(), CreateSignalsSection(), and FootprintWriteShape().

◆ fmt_mask()

static std::string fmt_mask ( LSET  aSet)
static

Definition at line 177 of file export_gencad.cpp.

178 {
179  return StrPrintf( "%08x", (unsigned) ( aSet & LSET::AllCuMask() ).to_ulong() );
180 }
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:750
int StrPrintf(std::string *result, const char *format,...)
This is like sprintf() but the output is appended to a std::string instead of to a character array.
Definition: richio.cpp:79

References LSET::AllCuMask(), and StrPrintf().

Referenced by CreatePadsShapesSection(), and CreateRoutesSection().

◆ FootprintWriteShape()

static void FootprintWriteShape ( FILE *  File,
FOOTPRINT aFootprint,
const wxString &  aShapeName 
)
static

Definition at line 1182 of file export_gencad.cpp.

1183 {
1184  FP_SHAPE* shape;
1185 
1186  /* creates header: */
1187  fprintf( aFile, "\nSHAPE \"%s\"\n", TO_UTF8( escapeString( aShapeName ) ) );
1188 
1189  if( aFootprint->GetAttributes() & FP_THROUGH_HOLE )
1190  fprintf( aFile, "INSERT TH\n" );
1191  else
1192  fprintf( aFile, "INSERT SMD\n" );
1193 
1194  // Silk outline; wildly interpreted by various importers:
1195  // CAM350 read it right but only closed shapes
1196  // ProntoPlace double-flip it (at least the pads are correct)
1197  // GerberTool usually get it right...
1198  for( BOARD_ITEM* PtStruct : aFootprint->GraphicalItems() )
1199  {
1200  switch( PtStruct->Type() )
1201  {
1202  case PCB_FP_TEXT_T:
1203 
1204  // If we wanted to export text, this is not the correct section
1205  break;
1206 
1207  case PCB_FP_SHAPE_T:
1208  shape = (FP_SHAPE*) PtStruct;
1209  if( shape->GetLayer() == F_SilkS || shape->GetLayer() == B_SilkS )
1210  {
1211  switch( shape->GetShape() )
1212  {
1213  case SHAPE_T::SEGMENT:
1214  fprintf( aFile, "LINE %g %g %g %g\n",
1215  shape->GetStart0().x / SCALE_FACTOR,
1216  -shape->GetStart0().y / SCALE_FACTOR,
1217  shape->GetEnd0().x / SCALE_FACTOR,
1218  -shape->GetEnd0().y / SCALE_FACTOR );
1219  break;
1220 
1221  case SHAPE_T::RECT:
1222  {
1223  fprintf( aFile, "LINE %g %g %g %g\n",
1224  shape->GetStart0().x / SCALE_FACTOR,
1225  -shape->GetStart0().y / SCALE_FACTOR,
1226  shape->GetEnd0().x / SCALE_FACTOR,
1227  -shape->GetStart0().y / SCALE_FACTOR );
1228  fprintf( aFile, "LINE %g %g %g %g\n",
1229  shape->GetEnd0().x / SCALE_FACTOR,
1230  -shape->GetStart0().y / SCALE_FACTOR,
1231  shape->GetEnd0().x / SCALE_FACTOR,
1232  -shape->GetEnd0().y / SCALE_FACTOR );
1233  fprintf( aFile, "LINE %g %g %g %g\n",
1234  shape->GetEnd0().x / SCALE_FACTOR,
1235  -shape->GetEnd0().y / SCALE_FACTOR,
1236  shape->GetStart0().x / SCALE_FACTOR,
1237  -shape->GetEnd0().y / SCALE_FACTOR );
1238  fprintf( aFile, "LINE %g %g %g %g\n",
1239  shape->GetStart0().x / SCALE_FACTOR,
1240  -shape->GetEnd0().y / SCALE_FACTOR,
1241  shape->GetStart0().x / SCALE_FACTOR,
1242  -shape->GetStart0().y / SCALE_FACTOR );
1243  }
1244  break;
1245 
1246  case SHAPE_T::CIRCLE:
1247  {
1248  int radius = KiROUND( GetLineLength( shape->GetEnd0(), shape->GetStart0() ) );
1249 
1250  fprintf( aFile, "CIRCLE %g %g %g\n",
1251  shape->GetStart0().x / SCALE_FACTOR,
1252  -shape->GetStart0().y / SCALE_FACTOR,
1253  radius / SCALE_FACTOR );
1254  break;
1255  }
1256 
1257  case SHAPE_T::ARC:
1258  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
1259  shape->GetStart0().x / SCALE_FACTOR,
1260  -shape->GetStart0().y / SCALE_FACTOR,
1261  shape->GetEnd0().x / SCALE_FACTOR,
1262  -shape->GetEnd0().y / SCALE_FACTOR,
1263  shape->GetCenter0().x / SCALE_FACTOR,
1264  -shape->GetCenter0().y / SCALE_FACTOR );
1265  break;
1266 
1267  case SHAPE_T::POLY:
1268  // Not exported (TODO)
1269  break;
1270 
1271  default:
1272  wxFAIL_MSG( wxString::Format( "Type Edge Module %d invalid.",
1273  PtStruct->Type() ) );
1274  break;
1275  }
1276  }
1277  break;
1278 
1279  default:
1280  break;
1281  }
1282  }
1283 }
double GetLineLength(const wxPoint &aPointA, const wxPoint &aPointB)
Return the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:222
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
const wxPoint & GetStart0() const
Definition: fp_shape.h:112
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
static wxString escapeString(const wxString &aString)
wxPoint GetCenter0() const
Definition: fp_shape.cpp:141
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
DRAWINGS & GraphicalItems()
Definition: footprint.h:171
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
int GetAttributes() const
Definition: footprint.h:235
const wxPoint & GetEnd0() const
Definition: fp_shape.h:115
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:73
static const double SCALE_FACTOR
SHAPE_T GetShape() const
Definition: eda_shape.h:92
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:140

References ARC, B_SilkS, CIRCLE, escapeString(), F_SilkS, Format(), FP_THROUGH_HOLE, FOOTPRINT::GetAttributes(), FP_SHAPE::GetCenter0(), FP_SHAPE::GetEnd0(), BOARD_ITEM::GetLayer(), GetLineLength(), EDA_SHAPE::GetShape(), FP_SHAPE::GetStart0(), FOOTPRINT::GraphicalItems(), KiROUND(), PCB_FP_SHAPE_T, PCB_FP_TEXT_T, POLY, RECT, SCALE_FACTOR, SEGMENT, and TO_UTF8.

Referenced by CreateShapesSection().

◆ GenCADLayerName()

static std::string GenCADLayerName ( int  aCuCount,
PCB_LAYER_ID  aId 
)
static

Definition at line 68 of file export_gencad.cpp.

69 {
70  if( IsCopperLayer( aId ) )
71  {
72  if( aId == F_Cu )
73  return "TOP";
74  else if( aId == B_Cu )
75  return "BOTTOM";
76  else if( aId <= 14 )
77  return StrPrintf( "INNER%d", aCuCount - aId - 1 );
78  else
79  return StrPrintf( "LAYER%d", aId );
80  }
81 
82  else
83  {
84  const char* txt;
85 
86  // using a switch to clearly show mapping & catch out of bounds index.
87  switch( aId )
88  {
89  // Technicals
90  case B_Adhes: txt = "B.Adhes"; break;
91  case F_Adhes: txt = "F.Adhes"; break;
92  case B_Paste: txt = "SOLDERPASTE_BOTTOM"; break;
93  case F_Paste: txt = "SOLDERPASTE_TOP"; break;
94  case B_SilkS: txt = "SILKSCREEN_BOTTOM"; break;
95  case F_SilkS: txt = "SILKSCREEN_TOP"; break;
96  case B_Mask: txt = "SOLDERMASK_BOTTOM"; break;
97  case F_Mask: txt = "SOLDERMASK_TOP"; break;
98 
99  // Users
100  case Dwgs_User: txt = "Dwgs.User"; break;
101  case Cmts_User: txt = "Cmts.User"; break;
102  case Eco1_User: txt = "Eco1.User"; break;
103  case Eco2_User: txt = "Eco2.User"; break;
104  case Edge_Cuts: txt = "Edge.Cuts"; break;
105  case Margin: txt = "Margin"; break;
106 
107  // Footprint
108  case F_CrtYd: txt = "F_CrtYd"; break;
109  case B_CrtYd: txt = "B_CrtYd"; break;
110  case F_Fab: txt = "F_Fab"; break;
111  case B_Fab: txt = "B_Fab"; break;
112 
113  default:
114  wxASSERT_MSG( 0, wxT( "aId UNEXPECTED" ) );
115  txt = "BAD-INDEX!"; break;
116  }
117 
118  return txt;
119  }
120 }
int StrPrintf(std::string *result, const char *format,...)
This is like sprintf() but the output is appended to a std::string instead of to a character array.
Definition: richio.cpp:79
bool IsCopperLayer(LAYER_NUM aLayerId)
Tests whether a layer is a copper layer.
Definition: layer_ids.h:796
Definition: layer_ids.h:71

References B_Adhes, B_CrtYd, B_Cu, B_Fab, B_Mask, B_Paste, B_SilkS, Cmts_User, Dwgs_User, Eco1_User, Eco2_User, Edge_Cuts, F_Adhes, F_CrtYd, F_Cu, F_Fab, F_Mask, F_Paste, F_SilkS, IsCopperLayer(), Margin, and StrPrintf().

Referenced by CreateComponentsSection(), CreatePadsShapesSection(), CreateRoutesSection(), and GenCADLayerNameFlipped().

◆ GenCADLayerNameFlipped()

static std::string GenCADLayerNameFlipped ( int  aCuCount,
PCB_LAYER_ID  aId 
)
static

Definition at line 160 of file export_gencad.cpp.

161 {
162  if( 1<= aId && aId <= 14 )
163  return StrPrintf( "INNER%d", 14 - aId );
164 
165  return GenCADLayerName( aCuCount, aId );
166 }
int StrPrintf(std::string *result, const char *format,...)
This is like sprintf() but the output is appended to a std::string instead of to a character array.
Definition: richio.cpp:79
static std::string GenCADLayerName(int aCuCount, PCB_LAYER_ID aId)

References GenCADLayerName(), and StrPrintf().

Referenced by CreatePadsShapesSection().

◆ getShapeName()

static const wxString getShapeName ( FOOTPRINT aFootprint)
static

Definition at line 195 of file export_gencad.cpp.

196 {
197  static const wxString invalid( "invalid" );
198 
199  if( individualShapes )
200  return aFootprint->GetReference();
201 
202  auto itShape = componentShapes.find( aFootprint );
203  wxCHECK( itShape != componentShapes.end(), invalid );
204 
205  auto itName = shapeNames.find( itShape->second );
206  wxCHECK( itName != shapeNames.end(), invalid );
207 
208  return itName->second;
209 }
static std::map< FOOTPRINT *, int > componentShapes
static bool individualShapes
const wxString & GetReference() const
Definition: footprint.h:463
static std::map< int, wxString > shapeNames

References componentShapes, FOOTPRINT::GetReference(), individualShapes, and shapeNames.

Referenced by CreateComponentsSection().

◆ hashFootprint()

static size_t hashFootprint ( const FOOTPRINT aFootprint)
static

Compute hashes for footprints without taking into account their position, rotation or layer.

Definition at line 705 of file export_gencad.cpp.

706 {
707  size_t ret = 0x11223344;
708  constexpr int flags = HASH_FLAGS::HASH_POS | HASH_FLAGS::REL_COORD
710 
711  for( BOARD_ITEM* i : aFootprint->GraphicalItems() )
712  ret += hash_fp_item( i, flags );
713 
714  for( PAD* i : aFootprint->Pads() )
715  ret += hash_fp_item( i, flags );
716 
717  return ret;
718 }
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
use coordinates relative to the parent object
Definition: hash_eda.h:43
PADS & Pads()
Definition: footprint.h:168
size_t hash_fp_item(const EDA_ITEM *aItem, int aFlags)
Calculate hash of an EDA_ITEM.
Definition: hash_eda.cpp:49
DRAWINGS & GraphicalItems()
Definition: footprint.h:171
Definition: pad.h:57

References FOOTPRINT::GraphicalItems(), hash_fp_item(), HASH_LAYER, HASH_POS, HASH_ROT, FOOTPRINT::Pads(), and REL_COORD.

Referenced by CreateShapesSection().

◆ MapXTo()

static double MapXTo ( int  aX)
static

Definition at line 217 of file export_gencad.cpp.

218 {
219  return (aX - GencadOffsetX) / SCALE_FACTOR;
220 }
static const double SCALE_FACTOR
static int GencadOffsetX

References GencadOffsetX, and SCALE_FACTOR.

Referenced by CreateBoardSection(), CreateComponentsSection(), CreateHeaderInfoData(), and CreateRoutesSection().

◆ MapYTo()

static double MapYTo ( int  aY)
static

Definition at line 223 of file export_gencad.cpp.

224 {
225  return (GencadOffsetY - aY) / SCALE_FACTOR;
226 }
static int GencadOffsetY
static const double SCALE_FACTOR

References GencadOffsetY, and SCALE_FACTOR.

Referenced by CreateBoardSection(), CreateComponentsSection(), CreateHeaderInfoData(), and CreateRoutesSection().

◆ ViaSort()

static bool ViaSort ( const PCB_VIA aPadref,
const PCB_VIA aPadcmp 
)
static

Definition at line 337 of file export_gencad.cpp.

338 {
339  if( aPadref->GetWidth() != aPadcmp->GetWidth() )
340  return aPadref->GetWidth() < aPadcmp->GetWidth();
341 
342  if( aPadref->GetDrillValue() != aPadcmp->GetDrillValue() )
343  return aPadref->GetDrillValue() < aPadcmp->GetDrillValue();
344 
345  if( aPadref->GetLayerSet() != aPadcmp->GetLayerSet() )
346  return aPadref->GetLayerSet().FmtBin().compare( aPadcmp->GetLayerSet().FmtBin() ) < 0;
347 
348  return false;
349 }
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: pcb_track.cpp:402
int GetWidth() const
Definition: pcb_track.h:102
std::string FmtBin() const
Return a binary string showing contents of this LSEQ.
Definition: lset.cpp:297
int GetDrillValue() const
Function GetDrillValue "calculates" the drill value for vias (m-Drill if > 0, or default drill value ...
Definition: pcb_track.cpp:193

References LSET::FmtBin(), PCB_VIA::GetDrillValue(), PCB_VIA::GetLayerSet(), and PCB_TRACK::GetWidth().

Referenced by CreatePadsShapesSection().

Variable Documentation

◆ componentShapes

std::map<FOOTPRINT*, int> componentShapes
static

◆ flipBottomPads

bool flipBottomPads
static

◆ gc_seq

const PCB_LAYER_ID gc_seq[]
static

Definition at line 123 of file export_gencad.cpp.

Referenced by CreatePadsShapesSection().

◆ GencadOffsetX

int GencadOffsetX
static

Definition at line 189 of file export_gencad.cpp.

Referenced by PCB_EDIT_FRAME::ExportToGenCAD(), and MapXTo().

◆ GencadOffsetY

int GencadOffsetY
static

Definition at line 189 of file export_gencad.cpp.

Referenced by PCB_EDIT_FRAME::ExportToGenCAD(), and MapYTo().

◆ individualShapes

bool individualShapes
static

◆ SCALE_FACTOR

◆ shapeNames

◆ storeOriginCoords

bool storeOriginCoords
static

Definition at line 186 of file export_gencad.cpp.

Referenced by CreateHeaderInfoData(), and PCB_EDIT_FRAME::ExportToGenCAD().

◆ uniquePins

bool uniquePins
static

Definition at line 184 of file export_gencad.cpp.

Referenced by CreateShapesSection(), and PCB_EDIT_FRAME::ExportToGenCAD().