KiCad PCB EDA Suite
GBR_TO_PCB_EXPORTER Class Reference

Public Member Functions

 GBR_TO_PCB_EXPORTER (GERBVIEW_FRAME *aFrame, const wxString &aFileName)
 
 ~GBR_TO_PCB_EXPORTER ()
 
bool ExportPcb (LAYER_NUM *aLayerLookUpTable, int aCopperLayers)
 Function ExportPcb saves a board from a set of Gerber images. More...
 

Private Member Functions

void export_non_copper_item (GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
 write a non copper line or arc to the board file. More...
 
void writePcbPolygon (const SHAPE_POLY_SET &aPolys, LAYER_NUM aLayer, const wxPoint &aOffset={ 0, 0 })
 write a non-copper polygon to the board file. More...
 
void writePcbZoneItem (GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
 write a zone item to the board file. More...
 
void export_copper_item (GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
 write a track or via) to the board file. More...
 
void export_flashed_copper_item (GERBER_DRAW_ITEM *aGbrItem)
 Function export_flashed_copper_item write a via to the board file (always uses a via through). More...
 
void export_segline_copper_item (GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
 Function export_segline_copper_item write a track (not via) to the board file. More...
 
void export_segarc_copper_item (GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
 Function export_segarc_copper_item write a set of tracks (arcs are approximated by track segments) to the board file. More...
 
void writeCopperLineItem (wxPoint &aStart, wxPoint &aEnd, int aWidth, LAYER_NUM aLayer)
 function writeCopperLineItem basic write function to write a a TRACK item to the board file, from a non flashed item More...
 
void writePcbHeader (LAYER_NUM *aLayerLookUpTable)
 function writePcbHeader Write a very basic header to the board file More...
 
double MapToPcbUnits (int aValue)
 In Pcbnew files units are mm for coordinates. More...
 

Private Attributes

GERBVIEW_FRAMEm_gerbview_frame
 
wxString m_pcb_file_name
 
FILE * m_fp
 
int m_pcbCopperLayersCount
 
std::vector< wxPointm_vias_coordinates
 

Detailed Description

Definition at line 52 of file export_to_pcbnew.cpp.

Constructor & Destructor Documentation

◆ GBR_TO_PCB_EXPORTER()

GBR_TO_PCB_EXPORTER::GBR_TO_PCB_EXPORTER ( GERBVIEW_FRAME aFrame,
const wxString &  aFileName 
)

Definition at line 151 of file export_to_pcbnew.cpp.

152 {
153  m_gerbview_frame = aFrame;
154  m_pcb_file_name = aFileName;
155  m_fp = NULL;
157 }
#define NULL
GERBVIEW_FRAME * m_gerbview_frame

References m_fp, m_gerbview_frame, m_pcb_file_name, m_pcbCopperLayersCount, and NULL.

◆ ~GBR_TO_PCB_EXPORTER()

GBR_TO_PCB_EXPORTER::~GBR_TO_PCB_EXPORTER ( )

Definition at line 160 of file export_to_pcbnew.cpp.

161 {
162 }

Member Function Documentation

◆ export_copper_item()

void GBR_TO_PCB_EXPORTER::export_copper_item ( GERBER_DRAW_ITEM aGbrItem,
LAYER_NUM  aLayer 
)
private

write a track or via) to the board file.

Parameters
aGbrItem= the Gerber item (line, arc, flashed) to export
aLayer= the copper layer to use

Definition at line 391 of file export_to_pcbnew.cpp.

392 {
393  switch( aGbrItem->m_Shape )
394  {
395  case GBR_SPOT_CIRCLE:
396  case GBR_SPOT_RECT:
397  case GBR_SPOT_OVAL:
398  // replace spots with vias when possible
399  export_flashed_copper_item( aGbrItem );
400  break;
401 
402  case GBR_ARC:
403  export_segarc_copper_item( aGbrItem, aLayer );
404  break;
405 
406  case GBR_POLYGON:
407  // One can use a polygon or a zone to output a Gerber region.
408  // none are perfect.
409  // The current way is use a polygon, as the zone export
410  // is exprimental and only for tests.
411 #if 1
412  writePcbPolygon( aGbrItem->m_Polygon, aLayer );
413 #else
414  // Only for tests:
415  writePcbZoneItem( aGbrItem, aLayer );
416 #endif
417  break;
418 
419  default:
420  export_segline_copper_item( aGbrItem, aLayer );
421  break;
422  }
423 }
void writePcbPolygon(const SHAPE_POLY_SET &aPolys, LAYER_NUM aLayer, const wxPoint &aOffset={ 0, 0 })
write a non-copper polygon to the board file.
void export_segline_copper_item(GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
Function export_segline_copper_item write a track (not via) to the board file.
SHAPE_POLY_SET m_Polygon
void writePcbZoneItem(GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
write a zone item to the board file.
void export_flashed_copper_item(GERBER_DRAW_ITEM *aGbrItem)
Function export_flashed_copper_item write a via to the board file (always uses a via through).
void export_segarc_copper_item(GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
Function export_segarc_copper_item write a set of tracks (arcs are approximated by track segments) to...

References export_flashed_copper_item(), export_segarc_copper_item(), export_segline_copper_item(), GBR_ARC, GBR_POLYGON, GBR_SPOT_CIRCLE, GBR_SPOT_OVAL, GBR_SPOT_RECT, GERBER_DRAW_ITEM::m_Polygon, GERBER_DRAW_ITEM::m_Shape, writePcbPolygon(), and writePcbZoneItem().

Referenced by ExportPcb().

◆ export_flashed_copper_item()

void GBR_TO_PCB_EXPORTER::export_flashed_copper_item ( GERBER_DRAW_ITEM aGbrItem)
private

Function export_flashed_copper_item write a via to the board file (always uses a via through).

Parameters
aGbrItem= the flashed Gerber item to export

Definition at line 512 of file export_to_pcbnew.cpp.

513 {
514  // First, explore already created vias, before creating a new via
515  for( unsigned ii = 0; ii < m_vias_coordinates.size(); ii++ )
516  {
517  if( m_vias_coordinates[ii] == aGbrItem->m_Start ) // Already created
518  return;
519  }
520 
521  m_vias_coordinates.push_back( aGbrItem->m_Start );
522 
523  wxPoint via_pos = aGbrItem->m_Start;
524  int width = (aGbrItem->m_Size.x + aGbrItem->m_Size.y) / 2;
525  // Reverse Y axis:
526  via_pos.y = -via_pos.y;
527 
528  // Layers are Front to Back
529  fprintf( m_fp, " (via (at %s %s) (size %s)",
530  Double2Str( MapToPcbUnits(via_pos.x) ).c_str(),
531  Double2Str( MapToPcbUnits(via_pos.y) ).c_str(),
532  Double2Str( MapToPcbUnits( width ) ).c_str() );
533 
534  fprintf( m_fp, " (layers %s %s))\n",
537 }
std::string Double2Str(double aValue)
Helper function Double2Str to print a float number without using scientific notation and no trailing ...
Definition: base_units.cpp:62
std::vector< wxPoint > m_vias_coordinates
const wxString GetPCBDefaultLayerName(LAYER_NUM aLayerNumber)
#define TO_UTF8(wxstring)
double MapToPcbUnits(int aValue)
In Pcbnew files units are mm for coordinates.

References B_Cu, Double2Str(), F_Cu, GetPCBDefaultLayerName(), m_fp, GERBER_DRAW_ITEM::m_Size, GERBER_DRAW_ITEM::m_Start, m_vias_coordinates, MapToPcbUnits(), TO_UTF8, wxPoint::x, and wxPoint::y.

Referenced by export_copper_item().

◆ export_non_copper_item()

void GBR_TO_PCB_EXPORTER::export_non_copper_item ( GERBER_DRAW_ITEM aGbrItem,
LAYER_NUM  aLayer 
)
private

write a non copper line or arc to the board file.

Parameters
aGbrItem= the Gerber item (line, arc) to export
aLayer= the technical layer to use

Definition at line 288 of file export_to_pcbnew.cpp.

289 {
290  // used when a D_CODE is not found. default D_CODE to draw a flashed item
291  static D_CODE dummyD_CODE( 0 );
292 
293  wxPoint seg_start = aGbrItem->m_Start;
294  wxPoint seg_end = aGbrItem->m_End;
295  D_CODE* d_codeDescr = aGbrItem->GetDcodeDescr();
296  SHAPE_POLY_SET polygon;
297 
298  if( d_codeDescr == NULL )
299  d_codeDescr = &dummyD_CODE;
300 
301  switch( aGbrItem->m_Shape )
302  {
303  case GBR_POLYGON:
304  writePcbPolygon( aGbrItem->m_Polygon, aLayer );
305  break;
306 
307  case GBR_SPOT_CIRCLE:
308  case GBR_SPOT_RECT:
309  case GBR_SPOT_OVAL:
310  case GBR_SPOT_POLY:
311  case GBR_SPOT_MACRO:
312  d_codeDescr->ConvertShapeToPolygon();
313  writePcbPolygon( d_codeDescr->m_Polygon, aLayer, aGbrItem->GetABPosition( seg_start ) );
314  break;
315 
316  case GBR_ARC:
317  {
318  double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ),
319  (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
320  double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
321  (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
322 
323  double angle = RAD2DEG(b - a);
324  seg_start = aGbrItem->m_ArcCentre;
325 
326  // Ensure arc orientation is CCW
327  if( angle < 0 )
328  angle += 360.0;
329 
330  // Reverse Y axis:
331  seg_start.y = -seg_start.y;
332  seg_end.y = -seg_end.y;
333 
334  if( angle == 360.0 || angle == 0 )
335  {
336  fprintf( m_fp, "(gr_circle (center %s %s) (end %s %s) (layer %s) (width %s))\n",
337  Double2Str( MapToPcbUnits(seg_start.x) ).c_str(),
338  Double2Str( MapToPcbUnits(seg_start.y) ).c_str(),
339  Double2Str( MapToPcbUnits(seg_end.x) ).c_str(),
340  Double2Str( MapToPcbUnits(seg_end.y) ).c_str(),
341  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
342  Double2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str()
343  );
344  }
345  else
346  {
347  fprintf( m_fp, "(gr_arc (start %s %s) (end %s %s) (angle %s) (layer %s) (width %s))\n",
348  Double2Str( MapToPcbUnits(seg_start.x) ).c_str(),
349  Double2Str( MapToPcbUnits(seg_start.y) ).c_str(),
350  Double2Str( MapToPcbUnits(seg_end.x) ).c_str(),
351  Double2Str( MapToPcbUnits(seg_end.y) ).c_str(),
352  Double2Str( angle ).c_str(),
353  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
354  Double2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str()
355  );
356  }
357  }
358  break;
359 
360  case GBR_CIRCLE:
361  // Reverse Y axis:
362  seg_start.y = -seg_start.y;
363  seg_end.y = -seg_end.y;
364 
365  fprintf( m_fp, "(gr_circle (start %s %s) (end %s %s) (layer %s) (width %s))\n",
366  Double2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
367  Double2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
368  Double2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
369  Double2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
370  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
371  Double2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() );
372  break;
373 
374  case GBR_SEGMENT:
375  // Reverse Y axis:
376  seg_start.y = -seg_start.y;
377  seg_end.y = -seg_end.y;
378 
379  fprintf( m_fp, "(gr_line (start %s %s) (end %s %s) (layer %s) (width %s))\n",
380  Double2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
381  Double2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
382  Double2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
383  Double2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
384  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
385  Double2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() );
386  break;
387  }
388 }
D_CODE * GetDcodeDescr() const
Function GetDcodeDescr returns the GetDcodeDescr of this object, or NULL.
void writePcbPolygon(const SHAPE_POLY_SET &aPolys, LAYER_NUM aLayer, const wxPoint &aOffset={ 0, 0 })
write a non-copper polygon to the board file.
double RAD2DEG(double rad)
Definition: trigo.h:215
std::string Double2Str(double aValue)
Helper function Double2Str to print a float number without using scientific notation and no trailing ...
Definition: base_units.cpp:62
SHAPE_POLY_SET m_Polygon
wxPoint GetABPosition(const wxPoint &aXYPosition) const
Function GetABPosition returns the image position of aPosition for this object.
#define NULL
SHAPE_POLY_SET.
D_CODE holds a gerber DCODE (also called Aperture) definition.
Definition: dcode.h:82
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
const wxString GetPCBDefaultLayerName(LAYER_NUM aLayerNumber)
SHAPE_POLY_SET m_Polygon
Definition: dcode.h:107
#define TO_UTF8(wxstring)
void ConvertShapeToPolygon()
Function ConvertShapeToPolygon convert a shape to an equivalent polygon.
Definition: dcode.cpp:301
double MapToPcbUnits(int aValue)
In Pcbnew files units are mm for coordinates.

References PNS::angle(), D_CODE::ConvertShapeToPolygon(), Double2Str(), GBR_ARC, GBR_CIRCLE, GBR_POLYGON, GBR_SEGMENT, GBR_SPOT_CIRCLE, GBR_SPOT_MACRO, GBR_SPOT_OVAL, GBR_SPOT_POLY, GBR_SPOT_RECT, GERBER_DRAW_ITEM::GetABPosition(), GERBER_DRAW_ITEM::GetDcodeDescr(), GetPCBDefaultLayerName(), GERBER_DRAW_ITEM::m_ArcCentre, GERBER_DRAW_ITEM::m_End, m_fp, GERBER_DRAW_ITEM::m_Polygon, D_CODE::m_Polygon, GERBER_DRAW_ITEM::m_Shape, GERBER_DRAW_ITEM::m_Size, GERBER_DRAW_ITEM::m_Start, MapToPcbUnits(), NULL, RAD2DEG(), TO_UTF8, writePcbPolygon(), wxPoint::x, and wxPoint::y.

Referenced by ExportPcb().

◆ export_segarc_copper_item()

void GBR_TO_PCB_EXPORTER::export_segarc_copper_item ( GERBER_DRAW_ITEM aGbrItem,
LAYER_NUM  aLayer 
)
private

Function export_segarc_copper_item write a set of tracks (arcs are approximated by track segments) to the board file.

Parameters
aGbrItem= the Gerber item (arc only) to export
aLayer= the copper layer to use

Definition at line 454 of file export_to_pcbnew.cpp.

455 {
456  double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ),
457  (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
458  double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
459  (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
460 
461  wxPoint start = aGbrItem->m_Start;
462  wxPoint end = aGbrItem->m_End;
463 
464  /* Because Pcbnew does not know arcs in tracks,
465  * approximate arc by segments (SEG_COUNT__CIRCLE segment per 360 deg)
466  * The arc is drawn anticlockwise from the start point to the end point.
467  */
468  #define SEG_COUNT_CIRCLE 16
469  #define DELTA_ANGLE 2 * M_PI / SEG_COUNT_CIRCLE
470 
471  // calculate the number of segments from a to b.
472  // we want CNT_PER_360 segments fo a circle
473  if( a > b )
474  b += 2 * M_PI;
475 
476  wxPoint curr_start = start;
477  wxPoint seg_start, seg_end;
478 
479  int ii = 1;
480 
481  for( double rot = a; rot < (b - DELTA_ANGLE); rot += DELTA_ANGLE, ii++ )
482  {
483  seg_start = curr_start;
484  wxPoint curr_end = start;
485  RotatePoint( &curr_end, aGbrItem->m_ArcCentre,
486  -RAD2DECIDEG( DELTA_ANGLE * ii ) );
487  seg_end = curr_end;
488  // Reverse Y axis:
489  seg_start.y = -seg_start.y;
490  seg_end.y = -seg_end.y;
491  writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
492  curr_start = curr_end;
493  }
494 
495  if( end != curr_start )
496  {
497  seg_start = curr_start;
498  seg_end = end;
499  // Reverse Y axis:
500  seg_start.y = -seg_start.y;
501  seg_end.y = -seg_end.y;
502  writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
503  }
504 }
double RAD2DECIDEG(double rad)
Definition: trigo.h:219
#define DELTA_ANGLE
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
void writeCopperLineItem(wxPoint &aStart, wxPoint &aEnd, int aWidth, LAYER_NUM aLayer)
function writeCopperLineItem basic write function to write a a TRACK item to the board file,...

References DELTA_ANGLE, GERBER_DRAW_ITEM::m_ArcCentre, GERBER_DRAW_ITEM::m_End, GERBER_DRAW_ITEM::m_Size, GERBER_DRAW_ITEM::m_Start, RAD2DECIDEG(), RotatePoint(), writeCopperLineItem(), wxPoint::x, and wxPoint::y.

Referenced by export_copper_item().

◆ export_segline_copper_item()

void GBR_TO_PCB_EXPORTER::export_segline_copper_item ( GERBER_DRAW_ITEM aGbrItem,
LAYER_NUM  aLayer 
)
private

Function export_segline_copper_item write a track (not via) to the board file.

Parameters
aGbrItem= the Gerber item (line only) to export
aLayer= the copper layer to use

Definition at line 426 of file export_to_pcbnew.cpp.

427 {
428  wxPoint seg_start, seg_end;
429 
430  seg_start = aGbrItem->m_Start;
431  seg_end = aGbrItem->m_End;
432 
433  // Reverse Y axis:
434  seg_start.y = -seg_start.y;
435  seg_end.y = -seg_end.y;
436 
437  writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
438 }
void writeCopperLineItem(wxPoint &aStart, wxPoint &aEnd, int aWidth, LAYER_NUM aLayer)
function writeCopperLineItem basic write function to write a a TRACK item to the board file,...

References GERBER_DRAW_ITEM::m_End, GERBER_DRAW_ITEM::m_Size, GERBER_DRAW_ITEM::m_Start, writeCopperLineItem(), and wxPoint::y.

Referenced by export_copper_item().

◆ ExportPcb()

bool GBR_TO_PCB_EXPORTER::ExportPcb ( LAYER_NUM aLayerLookUpTable,
int  aCopperLayers 
)

Function ExportPcb saves a board from a set of Gerber images.

Definition at line 221 of file export_to_pcbnew.cpp.

222 {
223  LOCALE_IO toggle; // toggles on, then off, the C locale.
224 
225  m_fp = wxFopen( m_pcb_file_name, wxT( "wt" ) );
226 
227  if( m_fp == NULL )
228  {
229  wxString msg;
230  msg.Printf( _( "Cannot create file \"%s\"" ), GetChars( m_pcb_file_name ) );
232  return false;
233  }
234 
235  m_pcbCopperLayersCount = aCopperLayers;
236 
237  writePcbHeader( aLayerLookUpTable );
238 
239  // create an image of gerber data
240  // First: non copper layers:
241  const int pcbCopperLayerMax = 31;
243 
244  for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
245  {
246  GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
247 
248  if( gerber == NULL ) // Graphic layer not yet used
249  continue;
250 
251  LAYER_NUM pcb_layer_number = aLayerLookUpTable[layer];
252 
253  if( !IsPcbLayer( pcb_layer_number ) )
254  continue;
255 
256  if( pcb_layer_number <= pcbCopperLayerMax ) // copper layer
257  continue;
258 
259  for( GERBER_DRAW_ITEM* gerb_item : gerber->GetItems() )
260  export_non_copper_item( gerb_item, pcb_layer_number );
261  }
262 
263  // Copper layers
264  for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
265  {
266  GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
267 
268  if( gerber == NULL ) // Graphic layer not yet used
269  continue;
270 
271  LAYER_NUM pcb_layer_number = aLayerLookUpTable[layer];
272 
273  if( pcb_layer_number < 0 || pcb_layer_number > pcbCopperLayerMax )
274  continue;
275 
276  for( GERBER_DRAW_ITEM* gerb_item : gerber->GetItems() )
277  export_copper_item( gerb_item, pcb_layer_number );
278  }
279 
280  fprintf( m_fp, ")\n" );
281 
282  fclose( m_fp );
283  m_fp = NULL;
284  return true;
285 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:239
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:214
GERBER_DRAW_ITEMS & GetItems()
GERBER_FILE_IMAGE holds the Image data and parameters for one gerber file and layer parameters (TODO:...
void export_non_copper_item(GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
write a non copper line or arc to the board file.
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
#define NULL
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
GERBVIEW_FRAME * m_gerbview_frame
int LAYER_NUM
Type LAYER_NUM can be replaced with int and removed.
GBR_LAYOUT * GetGerberLayout() const
void export_copper_item(GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
write a track or via) to the board file.
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:153
#define _(s)
Definition: 3d_actions.cpp:33
GERBER_FILE_IMAGE_LIST * GetImagesList() const
Definition: gbr_layout.cpp:44
bool IsPcbLayer(LAYER_NUM aLayer)
Function IsPcbLayer tests whether a layer is a valid layer for pcbnew.
void writePcbHeader(LAYER_NUM *aLayerLookUpTable)
function writePcbHeader Write a very basic header to the board file

References _, DisplayError(), export_copper_item(), export_non_copper_item(), GetChars(), GERBER_FILE_IMAGE_LIST::GetGbrImage(), GERBVIEW_FRAME::GetGerberLayout(), GBR_LAYOUT::GetImagesList(), GERBER_FILE_IMAGE::GetItems(), GERBER_FILE_IMAGE_LIST::ImagesMaxCount(), IsPcbLayer(), m_fp, m_gerbview_frame, m_pcb_file_name, m_pcbCopperLayersCount, NULL, and writePcbHeader().

Referenced by GERBVIEW_FRAME::ExportDataInPcbnewFormat().

◆ MapToPcbUnits()

double GBR_TO_PCB_EXPORTER::MapToPcbUnits ( int  aValue)
inlineprivate

In Pcbnew files units are mm for coordinates.

So MapToPcbUnits converts internal gerbview to mm any pcbnew value

Parameters
aValueis a coordinate value to convert in mm

Definition at line 144 of file export_to_pcbnew.cpp.

145  {
146  return aValue / IU_PER_MM;
147  }

Referenced by export_flashed_copper_item(), export_non_copper_item(), writeCopperLineItem(), writePcbPolygon(), and writePcbZoneItem().

◆ writeCopperLineItem()

void GBR_TO_PCB_EXPORTER::writeCopperLineItem ( wxPoint aStart,
wxPoint aEnd,
int  aWidth,
LAYER_NUM  aLayer 
)
private

function writeCopperLineItem basic write function to write a a TRACK item to the board file, from a non flashed item

Definition at line 441 of file export_to_pcbnew.cpp.

443 {
444  fprintf( m_fp, "(segment (start %s %s) (end %s %s) (width %s) (layer %s) (net 0))\n",
445  Double2Str( MapToPcbUnits(aStart.x) ).c_str(),
446  Double2Str( MapToPcbUnits(aStart.y) ).c_str(),
447  Double2Str( MapToPcbUnits(aEnd.x) ).c_str(),
448  Double2Str( MapToPcbUnits(aEnd.y) ).c_str(),
449  Double2Str( MapToPcbUnits( aWidth ) ).c_str(),
450  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
451 }
std::string Double2Str(double aValue)
Helper function Double2Str to print a float number without using scientific notation and no trailing ...
Definition: base_units.cpp:62
const wxString GetPCBDefaultLayerName(LAYER_NUM aLayerNumber)
#define TO_UTF8(wxstring)
double MapToPcbUnits(int aValue)
In Pcbnew files units are mm for coordinates.

References Double2Str(), GetPCBDefaultLayerName(), m_fp, MapToPcbUnits(), TO_UTF8, wxPoint::x, and wxPoint::y.

Referenced by export_segarc_copper_item(), and export_segline_copper_item().

◆ writePcbHeader()

void GBR_TO_PCB_EXPORTER::writePcbHeader ( LAYER_NUM aLayerLookUpTable)
private

function writePcbHeader Write a very basic header to the board file

Definition at line 539 of file export_to_pcbnew.cpp.

540 {
541  fprintf( m_fp, "(kicad_pcb (version 4) (host Gerbview \"%s\")\n\n",
542  TO_UTF8( GetBuildVersion() ) );
543 
544  // Write layers section
545  fprintf( m_fp, " (layers \n" );
546 
547  for( int ii = 0; ii < m_pcbCopperLayersCount; ii++ )
548  {
549  int id = ii;
550 
551  if( ii == m_pcbCopperLayersCount-1)
552  id = B_Cu;
553 
554  fprintf( m_fp, " (%d %s signal)\n", id, TO_UTF8( GetPCBDefaultLayerName( id ) ) );
555  }
556 
557  for( int ii = B_Adhes; ii < PCB_LAYER_ID_COUNT; ii++ )
558  {
559  if( GetPCBDefaultLayerName( ii ).IsEmpty() ) // Layer not available for export
560  continue;
561 
562  fprintf( m_fp, " (%d %s user)\n", ii, TO_UTF8( GetPCBDefaultLayerName( ii ) ) );
563  }
564 
565  fprintf( m_fp, " )\n\n" );
566 }
wxString GetBuildVersion()
Get the full KiCad version string.
const wxString GetPCBDefaultLayerName(LAYER_NUM aLayerNumber)
#define TO_UTF8(wxstring)

References B_Adhes, B_Cu, GetBuildVersion(), GetPCBDefaultLayerName(), m_fp, m_pcbCopperLayersCount, PCB_LAYER_ID_COUNT, and TO_UTF8.

Referenced by ExportPcb().

◆ writePcbPolygon()

void GBR_TO_PCB_EXPORTER::writePcbPolygon ( const SHAPE_POLY_SET aPolys,
LAYER_NUM  aLayer,
const wxPoint aOffset = { 0, 0 } 
)
private

write a non-copper polygon to the board file.

Parameters
aLayer= the technical layer to use

Definition at line 569 of file export_to_pcbnew.cpp.

571 {
572  SHAPE_POLY_SET polys = aPolys;
573 
574  // Cleanup the polygon
576 
577  // Ensure the polygon is valid:
578  if( polys.OutlineCount() == 0 )
579  return;
580 
582 
583  SHAPE_LINE_CHAIN& poly = polys.Outline( 0 );
584 
585  fprintf( m_fp, "(gr_poly (pts " );
586 
587  #define MAX_COORD_CNT 4
588  int jj = MAX_COORD_CNT;
589  int cnt_max = poly.PointCount() -1;
590 
591  // Do not generate last corner, if it is the same point as the first point:
592  if( poly.CPoint( 0 ) == poly.CPoint( cnt_max ) )
593  cnt_max--;
594 
595  for( int ii = 0; ii <= cnt_max; ii++ )
596  {
597  if( --jj == 0 )
598  {
599  jj = MAX_COORD_CNT;
600  fprintf( m_fp, "\n" );
601  }
602 
603  fprintf( m_fp, " (xy %s %s)",
604  Double2Str( MapToPcbUnits( poly.CPoint( ii ).x + aOffset.x ) ).c_str(),
605  Double2Str( MapToPcbUnits( -poly.CPoint( ii ).y + aOffset.y ) ).c_str() );
606  }
607 
608  fprintf( m_fp, ")" );
609 
610  if( jj != MAX_COORD_CNT )
611  fprintf( m_fp, "\n" );
612 
613  fprintf( m_fp, "(layer %s) (width 0) )\n",
614  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
615 }
int OutlineCount() const
Returns the number of outlines in the set
std::string Double2Str(double aValue)
Helper function Double2Str to print a float number without using scientific notation and no trailing ...
Definition: base_units.cpp:62
int PointCount() const
Function PointCount()
const VECTOR2I & CPoint(int aIndex) const
Function Point()
SHAPE_POLY_SET.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
void Simplify(POLYGON_MODE aFastMode)
Simplifies the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFast...
void Fracture(POLYGON_MODE aFastMode)
Converts a set of polygons with holes to a singe outline with "slits"/"fractures" connecting the oute...
#define MAX_COORD_CNT
SHAPE_LINE_CHAIN.
const wxString GetPCBDefaultLayerName(LAYER_NUM aLayerNumber)
#define TO_UTF8(wxstring)
double MapToPcbUnits(int aValue)
In Pcbnew files units are mm for coordinates.

References SHAPE_LINE_CHAIN::CPoint(), Double2Str(), SHAPE_POLY_SET::Fracture(), GetPCBDefaultLayerName(), m_fp, MapToPcbUnits(), MAX_COORD_CNT, SHAPE_POLY_SET::Outline(), SHAPE_POLY_SET::OutlineCount(), SHAPE_POLY_SET::PM_STRICTLY_SIMPLE, SHAPE_LINE_CHAIN::PointCount(), SHAPE_POLY_SET::Simplify(), TO_UTF8, wxPoint::x, VECTOR2< T >::x, wxPoint::y, and VECTOR2< T >::y.

Referenced by export_copper_item(), and export_non_copper_item().

◆ writePcbZoneItem()

void GBR_TO_PCB_EXPORTER::writePcbZoneItem ( GERBER_DRAW_ITEM aGbrItem,
LAYER_NUM  aLayer 
)
private

write a zone item to the board file.

Currently: only experimental, for tests

Parameters
aGbrItem= the Gerber item (line, arc) to export
aLayer= the technical layer to use

Definition at line 618 of file export_to_pcbnew.cpp.

619 {
620  SHAPE_POLY_SET polys = aGbrItem->m_Polygon;
622 
623  if( polys.OutlineCount() == 0 )
624  return;
625 
626  fprintf( m_fp, "(zone (net 0) (net_name \"\") (layer %s) (tstamp 0000000) (hatch edge 0.508)\n",
627  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
628 
629  fprintf( m_fp, " (connect_pads (clearance 0.0))\n" );
630 
631  fprintf( m_fp, " (min_thickness 0.1) (filled_areas_thickness no)\n"
632  " (fill (thermal_gap 0.3) (thermal_bridge_width 0.3))\n" );
633 
634  // Now, write the zone outlines with holes.
635  // first polygon is the main outline, next are holes
636  // One cannot know the initial zone outline.
637  // However most of (if not all) holes are just items with clearance,
638  // not really a hole in the initial zone outline.
639  // So we build a zone outline only with no hole.
640  fprintf( m_fp, " (polygon\n (pts" );
641 
642  SHAPE_LINE_CHAIN& poly = polys.Outline( 0 );
643 
644  #define MAX_COORD_CNT 4
645  int jj = MAX_COORD_CNT;
646  int cnt_max = poly.PointCount() -1;
647 
648  // Do not generate last corner, if it is the same point as the first point:
649  if( poly.CPoint( 0 ) == poly.CPoint( cnt_max ) )
650  cnt_max--;
651 
652  for( int ii = 0; ii <= cnt_max; ii++ )
653  {
654  if( --jj == 0 )
655  {
656  jj = MAX_COORD_CNT;
657  fprintf( m_fp, "\n " );
658  }
659 
660  fprintf( m_fp, " (xy %s %s)", Double2Str( MapToPcbUnits( poly.CPoint( ii ).x ) ).c_str(),
661  Double2Str( MapToPcbUnits( -poly.CPoint( ii ).y ) ).c_str() );
662  }
663 
664  fprintf( m_fp, ")\n" );
665 
666  fprintf( m_fp, " )\n)\n" );
667 }
int OutlineCount() const
Returns the number of outlines in the set
std::string Double2Str(double aValue)
Helper function Double2Str to print a float number without using scientific notation and no trailing ...
Definition: base_units.cpp:62
int PointCount() const
Function PointCount()
SHAPE_POLY_SET m_Polygon
const VECTOR2I & CPoint(int aIndex) const
Function Point()
SHAPE_POLY_SET.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
void Simplify(POLYGON_MODE aFastMode)
Simplifies the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFast...
#define MAX_COORD_CNT
SHAPE_LINE_CHAIN.
const wxString GetPCBDefaultLayerName(LAYER_NUM aLayerNumber)
#define TO_UTF8(wxstring)
double MapToPcbUnits(int aValue)
In Pcbnew files units are mm for coordinates.

References SHAPE_LINE_CHAIN::CPoint(), Double2Str(), GetPCBDefaultLayerName(), m_fp, GERBER_DRAW_ITEM::m_Polygon, MapToPcbUnits(), MAX_COORD_CNT, SHAPE_POLY_SET::Outline(), SHAPE_POLY_SET::OutlineCount(), SHAPE_POLY_SET::PM_STRICTLY_SIMPLE, SHAPE_LINE_CHAIN::PointCount(), SHAPE_POLY_SET::Simplify(), TO_UTF8, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by export_copper_item().

Member Data Documentation

◆ m_fp

◆ m_gerbview_frame

GERBVIEW_FRAME* GBR_TO_PCB_EXPORTER::m_gerbview_frame
private

Definition at line 55 of file export_to_pcbnew.cpp.

Referenced by ExportPcb(), and GBR_TO_PCB_EXPORTER().

◆ m_pcb_file_name

wxString GBR_TO_PCB_EXPORTER::m_pcb_file_name
private

Definition at line 56 of file export_to_pcbnew.cpp.

Referenced by ExportPcb(), and GBR_TO_PCB_EXPORTER().

◆ m_pcbCopperLayersCount

int GBR_TO_PCB_EXPORTER::m_pcbCopperLayersCount
private

Definition at line 58 of file export_to_pcbnew.cpp.

Referenced by ExportPcb(), GBR_TO_PCB_EXPORTER(), and writePcbHeader().

◆ m_vias_coordinates

std::vector<wxPoint> GBR_TO_PCB_EXPORTER::m_vias_coordinates
private

Definition at line 59 of file export_to_pcbnew.cpp.

Referenced by export_flashed_copper_item().


The documentation for this class was generated from the following file: