KiCad PCB EDA Suite
export_to_pcbnew.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2007-2014 Jean-Pierre Charras jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2017 KiCad Developers, see change_log.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <vector>
31 
32 #include <fctsys.h>
33 #include <common.h>
34 #include <confirm.h>
35 #include <macros.h>
36 #include <trigo.h>
37 #include <gerbview.h>
38 #include <gerbview_frame.h>
39 #include <gerber_file_image.h>
40 #include <gerber_file_image_list.h>
41 #include <select_layers_to_pcb.h>
42 #include <build_version.h>
44 
45 
46 // Imported function
47 extern const wxString GetPCBDefaultLayerName( LAYER_NUM aLayerNumber );
48 
49 #define TO_PCB_UNIT( x ) ( x / IU_PER_MM)
50 
51 /* A helper class to export a Gerber set of files to Pcbnew
52  */
54 {
55 private:
56  GERBVIEW_FRAME* m_gerbview_frame; // the main gerber frame
57  wxString m_pcb_file_name; // BOARD file to write to
58  FILE* m_fp; // the board file
60  std::vector<wxPoint> m_vias_coordinates; // list of already generated vias,
61  // used to export only once a via
62  // having a given coordinate
63 public:
64  GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName );
66 
71  bool ExportPcb( LAYER_NUM* aLayerLookUpTable, int aCopperLayers );
72 
73 private:
79  void export_non_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
80 
86  void writePcbPolygonItem( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
87 
93  void export_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
94 
101 
108  void export_segline_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
109 
117  void export_segarc_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
118 
124  void writePcbLineItem( bool aIsArc, wxPoint& aStart, wxPoint& aEnd,
125  int aWidth, LAYER_NUM aLayer, double aAngle = 0 );
126 
132  void writeCopperLineItem( wxPoint& aStart, wxPoint& aEnd,
133  int aWidth, LAYER_NUM aLayer );
134 
139  void writePcbHeader( LAYER_NUM* aLayerLookUpTable );
140 };
141 
142 
143 GBR_TO_PCB_EXPORTER::GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName )
144 {
145  m_gerbview_frame = aFrame;
146  m_pcb_file_name = aFileName;
147  m_fp = NULL;
149 }
150 
151 
153 {
154 }
155 
156 
157 /* Export data in Pcbnew format
158  * remember Pcbnew uses a Y reversed axis, so we must negate all Y coordinates
159  */
160 void GERBVIEW_FRAME::ExportDataInPcbnewFormat( wxCommandEvent& event )
161 {
162  int layercount = 0;
163 
164  GERBER_FILE_IMAGE_LIST* images = GetGerberLayout()->GetImagesList();
165 
166  // Count the Gerber layers which are actually currently used
167  for( LAYER_NUM ii = 0; ii < (LAYER_NUM)images->ImagesMaxCount(); ++ii )
168  {
169  if( images->GetGbrImage( ii ) )
170  layercount++;
171  }
172 
173  if( layercount == 0 )
174  {
175  DisplayInfoMessage( this,
176  _( "None of the Gerber layers contain any data" ) );
177  return;
178  }
179 
180  wxString fileName;
181  wxString path = m_mruPath;
182 
183  wxFileDialog filedlg( this, _( "Board File Name" ),
184  path, fileName, PcbFileWildcard(),
185  wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
186 
187  if( filedlg.ShowModal() == wxID_CANCEL )
188  return;
189 
190  fileName = filedlg.GetPath();
191 
192  /* Install a dialog frame to choose the mapping
193  * between gerber layers and Pcbnew layers
194  */
195  LAYERS_MAP_DIALOG* layerdlg = new LAYERS_MAP_DIALOG( this );
196  int ok = layerdlg->ShowModal();
197  layerdlg->Destroy();
198 
199  if( ok != wxID_OK )
200  return;
201 
202  m_mruPath = wxFileName( fileName ).GetPath();
203 
204  GBR_TO_PCB_EXPORTER gbr_exporter( this, fileName );
205 
206  gbr_exporter.ExportPcb( layerdlg->GetLayersLookUpTable(), layerdlg->GetCopperLayersCount() );
207 }
208 
209 
210 bool GBR_TO_PCB_EXPORTER::ExportPcb( LAYER_NUM* aLayerLookUpTable, int aCopperLayers )
211 {
212  LOCALE_IO toggle; // toggles on, then off, the C locale.
213 
214  m_fp = wxFopen( m_pcb_file_name, wxT( "wt" ) );
215 
216  if( m_fp == NULL )
217  {
218  wxString msg;
219  msg.Printf( _( "Cannot create file \"%s\"" ), GetChars( m_pcb_file_name ) );
221  return false;
222  }
223 
224  m_pcbCopperLayersCount = aCopperLayers;
225 
226  writePcbHeader( aLayerLookUpTable );
227 
228  // create an image of gerber data
229  // First: non copper layers:
230  const int pcbCopperLayerMax = 31;
232 
233  for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
234  {
235  GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
236 
237  if( gerber == NULL ) // Graphic layer not yet used
238  continue;
239 
240  LAYER_NUM pcb_layer_number = aLayerLookUpTable[layer];
241 
242  if( !IsPcbLayer( pcb_layer_number ) )
243  continue;
244 
245  if( pcb_layer_number <= pcbCopperLayerMax ) // copper layer
246  continue;
247 
248  GERBER_DRAW_ITEM* gerb_item = gerber->GetItemsList();
249 
250  for( ; gerb_item; gerb_item = gerb_item->Next() )
251  export_non_copper_item( gerb_item, pcb_layer_number );
252  }
253 
254  // Copper layers
255  for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
256  {
257  GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
258 
259  if( gerber == NULL ) // Graphic layer not yet used
260  continue;
261 
262  LAYER_NUM pcb_layer_number = aLayerLookUpTable[layer];
263 
264  if( pcb_layer_number < 0 || pcb_layer_number > pcbCopperLayerMax )
265  continue;
266 
267  GERBER_DRAW_ITEM* gerb_item = gerber->GetItemsList();
268 
269  for( ; gerb_item; gerb_item = gerb_item->Next() )
270  export_copper_item( gerb_item, pcb_layer_number );
271  }
272 
273  fprintf( m_fp, ")\n" );
274 
275  fclose( m_fp );
276  m_fp = NULL;
277  return true;
278 }
279 
280 
282 {
283  bool isArc = false;
284 
285  double angle = 0;
286  wxPoint seg_start = aGbrItem->m_Start;
287  wxPoint seg_end = aGbrItem->m_End;
288 
289  if( aGbrItem->m_Shape == GBR_POLYGON )
290  {
291  writePcbPolygonItem( aGbrItem, aLayer );
292  return;
293  }
294 
295  if( aGbrItem->m_Shape == GBR_ARC )
296  {
297  double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y),
298  (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
299  double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
300  (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
301 
302  isArc = true;
303  angle = RAD2DEG(b - a);
304  seg_start = aGbrItem->m_ArcCentre;
305 
306  // Ensure arc orientation is CCW
307  if( angle < 0 )
308  angle += 360.0;
309  }
310 
311  // Reverse Y axis:
312  seg_start.y = -seg_start.y;
313  seg_end.y = -seg_end.y;
314  writePcbLineItem( isArc, seg_start, seg_end, aGbrItem->m_Size.x, aLayer, angle );
315 }
316 
317 
319 {
320  switch( aGbrItem->m_Shape )
321  {
322  case GBR_SPOT_CIRCLE:
323  case GBR_SPOT_RECT:
324  case GBR_SPOT_OVAL:
325  // replace spots with vias when possible
326  export_flashed_copper_item( aGbrItem );
327  break;
328 
329  case GBR_ARC:
330  export_segarc_copper_item( aGbrItem, aLayer );
331  break;
332 
333  case GBR_POLYGON:
334  // Currently: Pcbnew does not really handle polygons on copper layers (no DRC test).
335  // However, we can export them if the purpose of this export is to help recreate a board
336  writePcbPolygonItem( aGbrItem, aLayer );
337  break;
338 
339  default:
340  export_segline_copper_item( aGbrItem, aLayer );
341  break;
342  }
343 }
344 
345 
347 {
348  wxPoint seg_start, seg_end;
349 
350  seg_start = aGbrItem->m_Start;
351  seg_end = aGbrItem->m_End;
352 
353  // Reverse Y axis:
354  seg_start.y = -seg_start.y;
355  seg_end.y = -seg_end.y;
356 
357  writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
358 }
359 
360 
362  int aWidth, LAYER_NUM aLayer )
363 {
364  fprintf( m_fp, "(segment (start %s %s) (end %s %s) (width %s) (layer %s) (net 0))\n",
365  Double2Str( TO_PCB_UNIT(aStart.x) ).c_str(),
366  Double2Str( TO_PCB_UNIT(aStart.y) ).c_str(),
367  Double2Str( TO_PCB_UNIT(aEnd.x) ).c_str(),
368  Double2Str( TO_PCB_UNIT(aEnd.y) ).c_str(),
369  Double2Str( TO_PCB_UNIT( aWidth ) ).c_str(),
370  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
371 }
372 
373 
375 {
376  double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ),
377  (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
378  double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
379  (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
380 
381  wxPoint start = aGbrItem->m_Start;
382  wxPoint end = aGbrItem->m_End;
383 
384  /* Because Pcbnew does not know arcs in tracks,
385  * approximate arc by segments (SEG_COUNT__CIRCLE segment per 360 deg)
386  * The arc is drawn anticlockwise from the start point to the end point.
387  */
388  #define SEG_COUNT_CIRCLE 16
389  #define DELTA_ANGLE 2 * M_PI / SEG_COUNT_CIRCLE
390 
391  // calculate the number of segments from a to b.
392  // we want CNT_PER_360 segments fo a circle
393  if( a > b )
394  b += 2 * M_PI;
395 
396  wxPoint curr_start = start;
397  wxPoint seg_start, seg_end;
398 
399  int ii = 1;
400 
401  for( double rot = a; rot < (b - DELTA_ANGLE); rot += DELTA_ANGLE, ii++ )
402  {
403  seg_start = curr_start;
404  wxPoint curr_end = start;
405  RotatePoint( &curr_end, aGbrItem->m_ArcCentre,
406  -RAD2DECIDEG( DELTA_ANGLE * ii ) );
407  seg_end = curr_end;
408  // Reverse Y axis:
409  seg_start.y = -seg_start.y;
410  seg_end.y = -seg_end.y;
411  writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
412  curr_start = curr_end;
413  }
414 
415  if( end != curr_start )
416  {
417  seg_start = curr_start;
418  seg_end = end;
419  // Reverse Y axis:
420  seg_start.y = -seg_start.y;
421  seg_end.y = -seg_end.y;
422  writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
423  }
424 }
425 
426 
427 /*
428  * creates a via from a flashed gerber item.
429  * Flashed items are usually pads or vias, so we try to export all of them
430  * using vias
431  */
433 {
434  // First, explore already created vias, before creating a new via
435  for( unsigned ii = 0; ii < m_vias_coordinates.size(); ii++ )
436  {
437  if( m_vias_coordinates[ii] == aGbrItem->m_Start ) // Already created
438  return;
439  }
440 
441  m_vias_coordinates.push_back( aGbrItem->m_Start );
442 
443  wxPoint via_pos = aGbrItem->m_Start;
444  int width = (aGbrItem->m_Size.x + aGbrItem->m_Size.y) / 2;
445  // Reverse Y axis:
446  via_pos.y = -via_pos.y;
447 
448  // Layers are Front to Back
449  fprintf( m_fp, " (via (at %s %s) (size %s)",
450  Double2Str( TO_PCB_UNIT(via_pos.x) ).c_str(),
451  Double2Str( TO_PCB_UNIT(via_pos.y) ).c_str(),
452  Double2Str( TO_PCB_UNIT( width ) ).c_str() );
453 
454  fprintf( m_fp, " (layers %s %s))\n",
457 }
458 
460 {
461  fprintf( m_fp, "(kicad_pcb (version 4) (host Gerbview \"%s\")\n\n",
462  TO_UTF8( GetBuildVersion() ) );
463 
464  // Write layers section
465  fprintf( m_fp, " (layers \n" );
466 
467  for( int ii = 0; ii < m_pcbCopperLayersCount; ii++ )
468  {
469  int id = ii;
470 
471  if( ii == m_pcbCopperLayersCount-1)
472  id = B_Cu;
473 
474  fprintf( m_fp, " (%d %s signal)\n", id, TO_UTF8( GetPCBDefaultLayerName( id ) ) );
475  }
476 
477  for( int ii = B_Adhes; ii < PCB_LAYER_ID_COUNT; ii++ )
478  {
479  fprintf( m_fp, " (%d %s user)\n", ii, TO_UTF8( GetPCBDefaultLayerName( ii ) ) );
480  }
481 
482  fprintf( m_fp, " )\n\n" );
483 }
484 
485 
486 void GBR_TO_PCB_EXPORTER::writePcbLineItem( bool aIsArc, wxPoint& aStart, wxPoint& aEnd,
487  int aWidth, LAYER_NUM aLayer, double aAngle )
488 {
489  if( aIsArc && ( aAngle == 360.0 || aAngle == 0 ) )
490  {
491  fprintf( m_fp, "(gr_circle (center %s %s) (end %s %s)(layer %s) (width %s))\n",
492  Double2Str( TO_PCB_UNIT(aStart.x) ).c_str(),
493  Double2Str( TO_PCB_UNIT(aStart.y) ).c_str(),
494  Double2Str( TO_PCB_UNIT(aEnd.x) ).c_str(),
495  Double2Str( TO_PCB_UNIT(aEnd.y) ).c_str(),
496  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
497  Double2Str( TO_PCB_UNIT( aWidth ) ).c_str()
498  );
499  }
500  else if( aIsArc )
501  {
502  fprintf( m_fp, "(gr_arc (start %s %s) (end %s %s) (angle %s)(layer %s) (width %s))\n",
503  Double2Str( TO_PCB_UNIT(aStart.x) ).c_str(),
504  Double2Str( TO_PCB_UNIT(aStart.y) ).c_str(),
505  Double2Str( TO_PCB_UNIT(aEnd.x) ).c_str(),
506  Double2Str( TO_PCB_UNIT(aEnd.y) ).c_str(),
507  Double2Str( aAngle ).c_str(),
508  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
509  Double2Str( TO_PCB_UNIT( aWidth ) ).c_str()
510  );
511  }
512  else
513  {
514  fprintf( m_fp, "(gr_line (start %s %s) (end %s %s)(layer %s) (width %s))\n",
515  Double2Str( TO_PCB_UNIT(aStart.x) ).c_str(),
516  Double2Str( TO_PCB_UNIT(aStart.y) ).c_str(),
517  Double2Str( TO_PCB_UNIT(aEnd.x) ).c_str(),
518  Double2Str( TO_PCB_UNIT(aEnd.y) ).c_str(),
519  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
520  Double2Str( TO_PCB_UNIT( aWidth ) ).c_str()
521  );
522  }
523 }
524 
525 
527 {
528  fprintf( m_fp, "(gr_poly (pts " );
529 
530  SHAPE_POLY_SET polys = aGbrItem->m_Polygon;
531  SHAPE_LINE_CHAIN& poly = polys.Outline( 0 );
532 
533  #define MAX_COORD_CNT 4
534  int jj = MAX_COORD_CNT;
535  int cnt_max = poly.PointCount() -1;
536 
537  // Do not generate last corner, if it is the same point as the first point:
538  if( poly.Point( 0 ) == poly.Point( cnt_max ) )
539  cnt_max--;
540 
541  for( int ii = 0; ii <= cnt_max; ii++ )
542  {
543  if( --jj == 0 )
544  {
545  jj = MAX_COORD_CNT;
546  fprintf( m_fp, "\n" );
547  }
548 
549  fprintf( m_fp, " (xy %s %s)",
550  Double2Str( TO_PCB_UNIT( poly.Point( ii ).x ) ).c_str(),
551  Double2Str( TO_PCB_UNIT( -poly.Point( ii ).y ) ).c_str() );
552  }
553 
554  fprintf( m_fp, ")" );
555 
556  if( jj != MAX_COORD_CNT )
557  fprintf( m_fp, "\n" );
558 
559  fprintf( m_fp, "(layer %s) (width 0) )\n",
560  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
561 }
void writePcbLineItem(bool aIsArc, wxPoint &aStart, wxPoint &aEnd, int aWidth, LAYER_NUM aLayer, double aAngle=0)
function writePcbLineItem basic write function to write a DRAWSEGMENT item or a TRACK item to the boa...
#define TO_PCB_UNIT(x)
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
This file is part of the common library.
int PointCount() const
Function PointCount()
GBR_TO_PCB_EXPORTER(GERBVIEW_FRAME *aFrame, const wxString &aFileName)
double RAD2DEG(double rad)
Definition: trigo.h:200
Class GERBER_FILE_IMAGE holds the Image data and parameters for one gerber file and layer parameters ...
LAYER_NUM * GetLayersLookUpTable()
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.
double RAD2DECIDEG(double rad)
Definition: trigo.h:204
void export_non_copper_item(GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
write a non copper line or arc to the board file.
std::string Double2Str(double aValue)
Helper function Double2Str to print a float number without using scientific notation and no trailing ...
Definition: base_units.cpp:61
#define DELTA_ANGLE
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
SHAPE_POLY_SET m_Polygon
static int GetCopperLayersCount()
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
wxString GetBuildVersion()
Function GetBuildVersion Return the build version string.
bool ExportPcb(LAYER_NUM *aLayerLookUpTable, int aCopperLayers)
Function ExportPcb saves a board from a set of Gerber images.
Class SHAPE_POLY_SET.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
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)...
std::vector< wxPoint > m_vias_coordinates
GERBER_DRAW_ITEM * Next() const
static GERBER_FILE_IMAGE_LIST & GetImagesList()
GERBVIEW_FRAME * m_gerbview_frame
The common library.
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...
GBR_LAYOUT * GetGerberLayout() const
int LAYER_NUM
Type LAYER_NUM can be replaced with int and removed.
void export_copper_item(GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
write a track or via) to the board file.
void ExportDataInPcbnewFormat(wxCommandEvent &event)
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:92
#define MAX_COORD_CNT
GERBER_DRAW_ITEM * GetItemsList()
Function GetItemsList.
Class SHAPE_LINE_CHAIN.
GERBER_FILE_IMAGE_LIST * GetImagesList() const
Definition: gbr_layout.cpp:50
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...
void writePcbPolygonItem(GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
write a non copper polygon item to the board file.
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
const wxString GetPCBDefaultLayerName(LAYER_NUM aLayerNumber)
wxString PcbFileWildcard()
The common library.
VECTOR2I & Point(int aIndex)
Function Point()
bool IsPcbLayer(LAYER_NUM aLayer)
Function IsPcbLayer tests whether a layer is a valid layer for pcbnew.
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:216
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:185
void writePcbHeader(LAYER_NUM *aLayerLookUpTable)
function writePcbHeader Write a very basic header to the board file