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-2015 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>
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:
80  void export_non_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
81 
88  void export_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
89 
96 
103  void export_segline_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
104 
112  void export_segarc_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
113 
119  void writePcbLineItem( bool aIsArc, wxPoint& aStart, wxPoint& aEnd,
120  int aWidth, LAYER_NUM aLayer, double aAngle = 0 );
121 
127  void writeCopperLineItem( wxPoint& aStart, wxPoint& aEnd,
128  int aWidth, LAYER_NUM aLayer );
129 
134  void writePcbHeader( LAYER_NUM* aLayerLookUpTable );
135 };
136 
137 
138 GBR_TO_PCB_EXPORTER::GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName )
139 {
140  m_gerbview_frame = aFrame;
141  m_pcb_file_name = aFileName;
142  m_fp = NULL;
144 }
145 
146 
148 {
149 }
150 
151 
152 /* Export data in Pcbnew format
153  * remember Pcbnew uses a Y reversed axis, so we must negate all Y coordinates
154  */
155 void GERBVIEW_FRAME::ExportDataInPcbnewFormat( wxCommandEvent& event )
156 {
157  int layercount = 0;
158 
160  // Count the Gerber layers which are actually currently used
161  for( LAYER_NUM ii = 0; ii < (LAYER_NUM)images->ImagesMaxCount(); ++ii )
162  {
163  if( images->GetGbrImage( ii ) )
164  layercount++;
165  }
166 
167  if( layercount == 0 )
168  {
169  DisplayInfoMessage( this,
170  _( "None of the Gerber layers contain any data" ) );
171  return;
172  }
173 
174  wxString fileName;
175  wxString path = m_mruPath;
176 
177  wxFileDialog filedlg( this, _( "Board file name:" ),
178  path, fileName, PcbFileWildcard,
179  wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
180 
181  if( filedlg.ShowModal() == wxID_CANCEL )
182  return;
183 
184  fileName = filedlg.GetPath();
185 
186  /* Install a dialog frame to choose the mapping
187  * between gerber layers and Pcbnew layers
188  */
189  LAYERS_MAP_DIALOG* layerdlg = new LAYERS_MAP_DIALOG( this );
190  int ok = layerdlg->ShowModal();
191  layerdlg->Destroy();
192 
193  if( ok != wxID_OK )
194  return;
195 
196  m_mruPath = wxFileName( fileName ).GetPath();
197 
198  GBR_TO_PCB_EXPORTER gbr_exporter( this, fileName );
199 
200  gbr_exporter.ExportPcb( layerdlg->GetLayersLookUpTable(), layerdlg->GetCopperLayersCount() );
201 }
202 
203 
204 bool GBR_TO_PCB_EXPORTER::ExportPcb( LAYER_NUM* aLayerLookUpTable, int aCopperLayers )
205 {
206  LOCALE_IO toggle; // toggles on, then off, the C locale.
207 
208  m_fp = wxFopen( m_pcb_file_name, wxT( "wt" ) );
209 
210  if( m_fp == NULL )
211  {
212  wxString msg;
213  msg.Printf( _( "Cannot create file '%s'" ), GetChars( m_pcb_file_name ) );
215  return false;
216  }
217 
218  m_pcbCopperLayersCount = aCopperLayers;
219 
220  writePcbHeader( aLayerLookUpTable );
221 
222  // create an image of gerber data
223  // First: non copper layers:
224  const int pcbCopperLayerMax = 31;
226 
227  for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
228  {
229  GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
230 
231  if( gerber == NULL ) // Graphic layer not yet used
232  continue;
233 
234  LAYER_NUM pcb_layer_number = aLayerLookUpTable[layer];
235 
236  if( !IsPcbLayer( pcb_layer_number ) )
237  continue;
238 
239  if( pcb_layer_number <= pcbCopperLayerMax ) // copper layer
240  continue;
241 
242  GERBER_DRAW_ITEM* gerb_item = gerber->GetItemsList();
243 
244  for( ; gerb_item; gerb_item = gerb_item->Next() )
245  export_non_copper_item( gerb_item, pcb_layer_number );
246  }
247 
248  // Copper layers
249  for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
250  {
251  GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
252 
253  if( gerber == NULL ) // Graphic layer not yet used
254  continue;
255 
256  LAYER_NUM pcb_layer_number = aLayerLookUpTable[layer];
257 
258  if( pcb_layer_number < 0 || pcb_layer_number > pcbCopperLayerMax )
259  continue;
260 
261  GERBER_DRAW_ITEM* gerb_item = gerber->GetItemsList();
262 
263  for( ; gerb_item; gerb_item = gerb_item->Next() )
264  export_copper_item( gerb_item, pcb_layer_number );
265  }
266 
267  fprintf( m_fp, ")\n" );
268 
269  fclose( m_fp );
270  m_fp = NULL;
271  return true;
272 }
273 
274 
276 {
277  bool isArc = false;
278 
279  double angle = 0;
280  wxPoint seg_start = aGbrItem->m_Start;
281  wxPoint seg_end = aGbrItem->m_End;
282 
283  if( aGbrItem->m_Shape == GBR_ARC )
284  {
285  double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y),
286  (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
287  double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
288  (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
289 
290  isArc = true;
291  angle = RAD2DEG(b - a);
292  seg_start = aGbrItem->m_ArcCentre;
293 
294  // Ensure arc orientation is CCW
295  if( angle < 0 )
296  angle += 360.0;
297  }
298 
299  // Reverse Y axis:
300  seg_start.y = -seg_start.y;
301  seg_end.y = -seg_end.y;
302  writePcbLineItem( isArc, seg_start, seg_end, aGbrItem->m_Size.x, aLayer, angle );
303 }
304 
305 
307 {
308  switch( aGbrItem->m_Shape )
309  {
310  case GBR_SPOT_CIRCLE:
311  case GBR_SPOT_RECT:
312  case GBR_SPOT_OVAL:
313  // replace spots with vias when possible
314  export_flashed_copper_item( aGbrItem );
315  break;
316 
317  case GBR_ARC:
318  export_segarc_copper_item( aGbrItem, aLayer );
319  break;
320 
321  default:
322  export_segline_copper_item( aGbrItem, aLayer );
323  break;
324  }
325 }
326 
327 
329 {
330  wxPoint seg_start, seg_end;
331 
332  seg_start = aGbrItem->m_Start;
333  seg_end = aGbrItem->m_End;
334 
335  // Reverse Y axis:
336  seg_start.y = -seg_start.y;
337  seg_end.y = -seg_end.y;
338 
339  writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
340 }
341 
342 
344  int aWidth, LAYER_NUM aLayer )
345 {
346  fprintf( m_fp, "(segment (start %s %s) (end %s %s) (width %s) (layer %s) (net 0))\n",
347  Double2Str( TO_PCB_UNIT(aStart.x) ).c_str(),
348  Double2Str( TO_PCB_UNIT(aStart.y) ).c_str(),
349  Double2Str( TO_PCB_UNIT(aEnd.x) ).c_str(),
350  Double2Str( TO_PCB_UNIT(aEnd.y) ).c_str(),
351  Double2Str( TO_PCB_UNIT( aWidth ) ).c_str(),
352  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
353 }
354 
355 
357 {
358  double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ),
359  (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
360  double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
361  (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
362 
363  wxPoint start = aGbrItem->m_Start;
364  wxPoint end = aGbrItem->m_End;
365 
366  /* Because Pcbnew does not know arcs in tracks,
367  * approximate arc by segments (SEG_COUNT__CIRCLE segment per 360 deg)
368  * The arc is drawn anticlockwise from the start point to the end point.
369  */
370  #define SEG_COUNT_CIRCLE 16
371  #define DELTA_ANGLE 2 * M_PI / SEG_COUNT_CIRCLE
372 
373  // calculate the number of segments from a to b.
374  // we want CNT_PER_360 segments fo a circle
375  if( a > b )
376  b += 2 * M_PI;
377 
378  wxPoint curr_start = start;
379  wxPoint seg_start, seg_end;
380 
381  int ii = 1;
382 
383  for( double rot = a; rot < (b - DELTA_ANGLE); rot += DELTA_ANGLE, ii++ )
384  {
385  seg_start = curr_start;
386  wxPoint curr_end = start;
387  RotatePoint( &curr_end, aGbrItem->m_ArcCentre,
388  -RAD2DECIDEG( DELTA_ANGLE * ii ) );
389  seg_end = curr_end;
390  // Reverse Y axis:
391  seg_start.y = -seg_start.y;
392  seg_end.y = -seg_end.y;
393  writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
394  curr_start = curr_end;
395  }
396 
397  if( end != curr_start )
398  {
399  seg_start = curr_start;
400  seg_end = end;
401  // Reverse Y axis:
402  seg_start.y = -seg_start.y;
403  seg_end.y = -seg_end.y;
404  writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
405  }
406 }
407 
408 
409 /*
410  * creates a via from a flashed gerber item.
411  * Flashed items are usually pads or vias, so we try to export all of them
412  * using vias
413  */
415 {
416  // First, explore already created vias, before creating a new via
417  for( unsigned ii = 0; ii < m_vias_coordinates.size(); ii++ )
418  {
419  if( m_vias_coordinates[ii] == aGbrItem->m_Start ) // Already created
420  return;
421  }
422 
423  m_vias_coordinates.push_back( aGbrItem->m_Start );
424 
425  wxPoint via_pos = aGbrItem->m_Start;
426  int width = (aGbrItem->m_Size.x + aGbrItem->m_Size.y) / 2;
427  // Reverse Y axis:
428  via_pos.y = -via_pos.y;
429 
430  // Layers are Front to Back
431  fprintf( m_fp, " (via (at %s %s) (size %s)",
432  Double2Str( TO_PCB_UNIT(via_pos.x) ).c_str(),
433  Double2Str( TO_PCB_UNIT(via_pos.y) ).c_str(),
434  Double2Str( TO_PCB_UNIT( width ) ).c_str() );
435 
436  fprintf( m_fp, " (layers %s %s))\n",
439 }
440 
442 {
443  fprintf( m_fp, "(kicad_pcb (version 4) (host Gerbview \"%s\")\n\n",
444  TO_UTF8( GetBuildVersion() ) );
445 
446  // Write layers section
447  fprintf( m_fp, " (layers \n" );
448 
449  for( int ii = 0; ii < m_pcbCopperLayersCount; ii++ )
450  {
451  int id = ii;
452 
453  if( ii == m_pcbCopperLayersCount-1)
454  id = B_Cu;
455 
456  fprintf( m_fp, " (%d %s signal)\n", id, TO_UTF8( GetPCBDefaultLayerName( id ) ) );
457  }
458 
459  for( int ii = B_Adhes; ii < PCB_LAYER_ID_COUNT; ii++ )
460  {
461  fprintf( m_fp, " (%d %s user)\n", ii, TO_UTF8( GetPCBDefaultLayerName( ii ) ) );
462  }
463 
464  fprintf( m_fp, " )\n\n" );
465 }
466 
467 
468 void GBR_TO_PCB_EXPORTER::writePcbLineItem( bool aIsArc, wxPoint& aStart, wxPoint& aEnd,
469  int aWidth, LAYER_NUM aLayer, double aAngle )
470 {
471  if( aIsArc && ( aAngle == 360.0 || aAngle == 0 ) )
472  {
473  fprintf( m_fp, "(gr_circle (center %s %s) (end %s %s)(layer %s) (width %s))\n",
474  Double2Str( TO_PCB_UNIT(aStart.x) ).c_str(),
475  Double2Str( TO_PCB_UNIT(aStart.y) ).c_str(),
476  Double2Str( TO_PCB_UNIT(aEnd.x) ).c_str(),
477  Double2Str( TO_PCB_UNIT(aEnd.y) ).c_str(),
478  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
479  Double2Str( TO_PCB_UNIT( aWidth ) ).c_str()
480  );
481  }
482  else if( aIsArc )
483  {
484  fprintf( m_fp, "(gr_arc (start %s %s) (end %s %s) (angle %s)(layer %s) (width %s))\n",
485  Double2Str( TO_PCB_UNIT(aStart.x) ).c_str(),
486  Double2Str( TO_PCB_UNIT(aStart.y) ).c_str(),
487  Double2Str( TO_PCB_UNIT(aEnd.x) ).c_str(),
488  Double2Str( TO_PCB_UNIT(aEnd.y) ).c_str(),
489  Double2Str( aAngle ).c_str(),
490  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
491  Double2Str( TO_PCB_UNIT( aWidth ) ).c_str()
492  );
493  }
494  else
495  {
496  fprintf( m_fp, "(gr_line (start %s %s) (end %s %s)(layer %s) (width %s))\n",
497  Double2Str( TO_PCB_UNIT(aStart.x) ).c_str(),
498  Double2Str( TO_PCB_UNIT(aStart.y) ).c_str(),
499  Double2Str( TO_PCB_UNIT(aEnd.x) ).c_str(),
500  Double2Str( TO_PCB_UNIT(aEnd.y) ).c_str(),
501  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
502  Double2Str( TO_PCB_UNIT( aWidth ) ).c_str()
503  );
504  }
505 }
GERBER_FILE_IMAGE_LIST * GetImagesList()
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...
wxString m_mruPath
Most recently used path.
Definition: wxstruct.h:155
#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.
GBR_TO_PCB_EXPORTER(GERBVIEW_FRAME *aFrame, const wxString &aFileName)
double RAD2DEG(double rad)
Definition: trigo.h:192
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:196
void export_non_copper_item(GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
Function export_non_copper_item 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:60
#define DELTA_ANGLE
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
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.
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
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)
Function export_copper_item write a track or via) to the board file.
void ExportDataInPcbnewFormat(wxCommandEvent &event)
void DisplayInfoMessage(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:89
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
GERBER_DRAW_ITEM * GetItemsList()
Function GetItemsList.
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...
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
const wxString GetPCBDefaultLayerName(LAYER_NUM aLayerNumber)
The common library.
bool IsPcbLayer(LAYER_NUM aLayer)
Function IsPcbLayer tests whether a layer is a valid layer for pcbnew.
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:69
const wxString PcbFileWildcard
void writePcbHeader(LAYER_NUM *aLayerLookUpTable)
function writePcbHeader Write a very basic header to the board file