KiCad PCB EDA Suite
export_gencad.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) 2016 Jean-Pierre Charras, [email protected]
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6  * Copyright (C) 2012 Wayne Stambaugh <[email protected]>
7  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
32 #include <build_version.h>
33 #include <board.h>
34 #include <board_design_settings.h>
36 #include <fp_shape.h>
37 #include <footprint.h>
38 #include <pad.h>
39 #include <pcb_track.h>
40 #include <confirm.h>
41 #include <core/arraydim.h>
43 #include <locale_io.h>
44 #include <macros.h>
45 #include <hash_eda.h>
46 #include <pcb_edit_frame.h>
47 #include <pcbnew_settings.h>
48 #include <pgm_base.h>
49 #include <project/project_file.h> // LAST_PATH_TYPE
50 
51 #include <wx/app.h>
52 #include <wx/filedlg.h>
53 
54 static bool CreateHeaderInfoData( FILE* aFile, PCB_EDIT_FRAME* frame );
55 static void CreateArtworksSection( FILE* aFile );
56 static void CreateTracksInfoData( FILE* aFile, BOARD* aPcb );
57 static void CreateBoardSection( FILE* aFile, BOARD* aPcb );
58 static void CreateComponentsSection( FILE* aFile, BOARD* aPcb );
59 static void CreateDevicesSection( FILE* aFile, BOARD* aPcb );
60 static void CreateRoutesSection( FILE* aFile, BOARD* aPcb );
61 static void CreateSignalsSection( FILE* aFile, BOARD* aPcb );
62 static void CreateShapesSection( FILE* aFile, BOARD* aPcb );
63 static void CreatePadsShapesSection( FILE* aFile, BOARD* aPcb );
64 static void FootprintWriteShape( FILE* File, FOOTPRINT* aFootprint, const wxString& aShapeName );
65 
66 
67 // layer names for Gencad export
68 static std::string GenCADLayerName( int aCuCount, PCB_LAYER_ID aId )
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 }
121 
122 
123 static const PCB_LAYER_ID gc_seq[] = {
124  B_Cu,
125  In30_Cu,
126  In29_Cu,
127  In28_Cu,
128  In27_Cu,
129  In26_Cu,
130  In25_Cu,
131  In24_Cu,
132  In23_Cu,
133  In22_Cu,
134  In21_Cu,
135  In20_Cu,
136  In19_Cu,
137  In18_Cu,
138  In17_Cu,
139  In16_Cu,
140  In15_Cu,
141  In14_Cu,
142  In13_Cu,
143  In12_Cu,
144  In11_Cu,
145  In10_Cu,
146  In9_Cu,
147  In8_Cu,
148  In7_Cu,
149  In6_Cu,
150  In5_Cu,
151  In4_Cu,
152  In3_Cu,
153  In2_Cu,
154  In1_Cu,
155  F_Cu,
156 };
157 
158 
159 // flipped layer name for Gencad export (to make CAM350 imports correct)
160 static std::string GenCADLayerNameFlipped( int aCuCount, PCB_LAYER_ID aId )
161 {
162  if( 1<= aId && aId <= 14 )
163  return StrPrintf( "INNER%d", 14 - aId );
164 
165  return GenCADLayerName( aCuCount, aId );
166 }
167 
168 
169 static wxString escapeString( const wxString& aString )
170 {
171  wxString copy( aString );
172  copy.Replace( wxT( "\"" ), wxT( "\\\"" ) );
173  return copy;
174 }
175 
176 
177 static std::string fmt_mask( LSET aSet )
178 {
179  return StrPrintf( "%08x", (unsigned) ( aSet & LSET::AllCuMask() ).to_ulong() );
180 }
181 
182 
183 // Export options
184 static bool flipBottomPads;
185 static bool uniquePins;
186 static bool individualShapes;
187 static bool storeOriginCoords;
188 
189 // These are the export origin (the auxiliary axis)
191 
192 // Association between shape names (using shapeName index) and components
193 static std::map<FOOTPRINT*, int> componentShapes;
194 static std::map<int, wxString> shapeNames;
195 
196 
197 static const wxString getShapeName( FOOTPRINT* aFootprint )
198 {
199  static const wxString invalid( "invalid" );
200 
201  if( individualShapes )
202  return aFootprint->GetReference();
203 
204  auto itShape = componentShapes.find( aFootprint );
205  wxCHECK( itShape != componentShapes.end(), invalid );
206 
207  auto itName = shapeNames.find( itShape->second );
208  wxCHECK( itName != shapeNames.end(), invalid );
209 
210  return itName->second;
211 }
212 
213 
214 // GerbTool chokes on units different than INCH so this is the conversion factor
215 const static double SCALE_FACTOR = 1000.0 * IU_PER_MILS;
216 
217 
218 /* Two helper functions to calculate coordinates of footprints in gencad values
219  * (GenCAD Y axis from bottom to top)
220  */
221 static double MapXTo( int aX )
222 {
223  return (aX - GencadOffsetX) / SCALE_FACTOR;
224 }
225 
226 
227 static double MapYTo( int aY )
228 {
229  return (GencadOffsetY - aY) / SCALE_FACTOR;
230 }
231 
232 
233 /* Driver function: processing starts here */
234 void PCB_EDIT_FRAME::ExportToGenCAD( wxCommandEvent& aEvent )
235 {
236  // Build default output file name
237  wxString path = GetLastPath( LAST_PATH_GENCAD );
238 
239  if( path.IsEmpty() )
240  {
241  wxFileName brdFile = GetBoard()->GetFileName();
242  brdFile.SetExt( wxT( "cad" ) );
243  path = brdFile.GetFullPath();
244  }
245 
246  DIALOG_GENCAD_EXPORT_OPTIONS optionsDialog( this, path );
247 
248  if( optionsDialog.ShowModal() == wxID_CANCEL )
249  return;
250 
251  path = optionsDialog.GetFileName();
253  FILE* file = wxFopen( path, wxT( "wt" ) );
254 
255  if( !file )
256  {
257  DisplayError( this, wxString::Format( _( "Failed to create file '%s'." ),
258  optionsDialog.GetFileName() ) );
259  return;
260  }
261 
262  // Get options
263  flipBottomPads = optionsDialog.GetOption( FLIP_BOTTOM_PADS );
264  uniquePins = optionsDialog.GetOption( UNIQUE_PIN_NAMES );
265  individualShapes = optionsDialog.GetOption( INDIVIDUAL_SHAPES );
267 
268  // Switch the locale to standard C (needed to print floating point numbers)
269  LOCALE_IO toggle;
270 
271  // Update some board data, to ensure a reliable gencad export
273 
274  // Save the auxiliary origin for the rest of the footprint
275  wxPoint auxOrigin = m_pcb->GetDesignSettings().GetAuxOrigin();
276  GencadOffsetX = optionsDialog.GetOption( USE_AUX_ORIGIN ) ? auxOrigin.x : 0;
277  GencadOffsetY = optionsDialog.GetOption( USE_AUX_ORIGIN ) ? auxOrigin.y : 0;
278 
279  // No idea on *why* this should be needed... maybe to fix net names?
280  Compile_Ratsnest( true );
281 
282  /* Temporary modification of footprints that are flipped (i.e. on bottom
283  * layer) to convert them to non flipped footprints.
284  * This is necessary to easily export shapes to GenCAD,
285  * that are given as normal orientation (non flipped, rotation = 0))
286  * these changes will be undone later
287  */
288  BOARD* pcb = GetBoard();
289 
290  for( FOOTPRINT* footprint : pcb->Footprints() )
291  {
292  footprint->SetFlag( 0 );
293 
294  if( footprint->GetLayer() == B_Cu )
295  {
296  footprint->Flip( footprint->GetPosition(), false );
297  footprint->SetFlag( 1 );
298  }
299  }
300 
301  /* Gencad has some mandatory and some optional sections: some importer
302  * need the padstack section (which is optional) anyway. Also the
303  * order of the section *is* important */
304 
305  CreateHeaderInfoData( file, this ); // Gencad header
306  CreateBoardSection( file, pcb ); // Board perimeter
307 
308  CreatePadsShapesSection( file, pcb ); // Pads and padstacks
309  CreateArtworksSection( file ); // Empty but mandatory
310 
311  /* Gencad splits a component info in shape, component and device.
312  * We don't do any sharing (it would be difficult since each module is
313  * customizable after placement) */
314  CreateShapesSection( file, pcb );
315  CreateComponentsSection( file, pcb );
316  CreateDevicesSection( file, pcb );
317 
318  // In a similar way the netlist is split in net, track and route
319  CreateSignalsSection( file, pcb );
320  CreateTracksInfoData( file, pcb );
321  CreateRoutesSection( file, pcb );
322 
323  fclose( file );
324 
325  // Undo the footprints modifications (flipped footprints)
326  for( FOOTPRINT* footprint : pcb->Footprints() )
327  {
328  if( footprint->GetFlag() )
329  {
330  footprint->Flip( footprint->GetPosition(), false );
331  footprint->SetFlag( 0 );
332  }
333  }
334 
335  componentShapes.clear();
336  shapeNames.clear();
337 }
338 
339 
340 // Sort vias for uniqueness
341 static bool ViaSort( const PCB_VIA* aPadref, const PCB_VIA* aPadcmp )
342 {
343  if( aPadref->GetWidth() != aPadcmp->GetWidth() )
344  return aPadref->GetWidth() < aPadcmp->GetWidth();
345 
346  if( aPadref->GetDrillValue() != aPadcmp->GetDrillValue() )
347  return aPadref->GetDrillValue() < aPadcmp->GetDrillValue();
348 
349  if( aPadref->GetLayerSet() != aPadcmp->GetLayerSet() )
350  return aPadref->GetLayerSet().FmtBin().compare( aPadcmp->GetLayerSet().FmtBin() ) < 0;
351 
352  return false;
353 }
354 
355 
356 // The ARTWORKS section is empty but (officially) mandatory
357 static void CreateArtworksSection( FILE* aFile )
358 {
359  /* The artworks section is empty */
360  fputs( "$ARTWORKS\n", aFile );
361  fputs( "$ENDARTWORKS\n\n", aFile );
362 }
363 
364 
365 // Emit PADS and PADSTACKS. They are sorted and emitted uniquely.
366 // Via name is synthesized from their attributes, pads are numbered
367 static void CreatePadsShapesSection( FILE* aFile, BOARD* aPcb )
368 {
369  std::vector<PAD*> padstacks;
370  std::vector<PCB_VIA*> vias;
371  std::vector<PCB_VIA*> viastacks;
372 
373  padstacks.resize( 1 ); // We count pads from 1
374 
375  // The master layermask (i.e. the enabled layers) for padstack generation
376  LSET master_layermask = aPcb->GetDesignSettings().GetEnabledLayers();
377  int cu_count = aPcb->GetCopperLayerCount();
378 
379  fputs( "$PADS\n", aFile );
380 
381  // Enumerate and sort the pads
382 
383  std::vector<PAD*> pads = aPcb->GetPads();
384  std::sort( pads.begin(), pads.end(), []( const PAD* a, const PAD* b )
385  {
386  return PAD::Compare( a, b ) < 0;
387  } );
388 
389 
390  // The same for vias
391  for( PCB_TRACK* track : aPcb->Tracks() )
392  {
393  if( PCB_VIA* via = dyn_cast<PCB_VIA*>( track ) )
394  vias.push_back( via );
395  }
396 
397  std::sort( vias.begin(), vias.end(), ViaSort );
398  vias.erase( std::unique( vias.begin(), vias.end(), []( const PCB_VIA* a, const PCB_VIA* b )
399  {
400  return ViaSort( a, b ) == false;
401  } ),
402  vias.end() );
403 
404  // Emit vias pads
405  for( PCB_VIA* via : vias )
406  {
407  viastacks.push_back( via );
408  fprintf( aFile, "PAD V%d.%d.%s ROUND %g\nCIRCLE 0 0 %g\n",
409  via->GetWidth(), via->GetDrillValue(),
410  fmt_mask( via->GetLayerSet() & master_layermask ).c_str(),
411  via->GetDrillValue() / SCALE_FACTOR,
412  via->GetWidth() / (SCALE_FACTOR * 2) );
413  }
414 
415  // Emit component pads
416  PAD* old_pad = nullptr;
417  int pad_name_number = 0;
418 
419  for( unsigned i = 0; i<pads.size(); ++i )
420  {
421  PAD* pad = pads[i];
422  const wxPoint& off = pad->GetOffset();
423 
424  pad->SetSubRatsnest( pad_name_number );
425 
426  // @warning: This code is not 100% correct. The #PAD::Compare function does not test
427  // custom pad primitives so there may be duplicate custom pads in the export.
428  if( old_pad && 0 == PAD::Compare( old_pad, pad ) )
429  continue;
430 
431  old_pad = pad;
432 
433  pad_name_number++;
434  pad->SetSubRatsnest( pad_name_number );
435 
436  fprintf( aFile, "PAD P%d", pad->GetSubRatsnest() );
437 
438  padstacks.push_back( pad ); // Will have its own padstack later
439  int dx = pad->GetSize().x / 2;
440  int dy = pad->GetSize().y / 2;
441 
442  switch( pad->GetShape() )
443  {
444  default:
445  wxASSERT_MSG( false, wxT( "Pad type not implemented" ) );
447 
448  case PAD_SHAPE::CIRCLE:
449  fprintf( aFile, " ROUND %g\n",
450  pad->GetDrillSize().x / SCALE_FACTOR );
451 
452  /* Circle is center, radius */
453  fprintf( aFile, "CIRCLE %g %g %g\n",
454  off.x / SCALE_FACTOR,
455  -off.y / SCALE_FACTOR,
456  pad->GetSize().x / (SCALE_FACTOR * 2) );
457  break;
458 
459  case PAD_SHAPE::RECT:
460  fprintf( aFile, " RECTANGULAR %g\n",
461  pad->GetDrillSize().x / SCALE_FACTOR );
462 
463  // Rectangle is begin, size *not* begin, end!
464  fprintf( aFile, "RECTANGLE %g %g %g %g\n",
465  (-dx + off.x ) / SCALE_FACTOR,
466  (-dy - off.y ) / SCALE_FACTOR,
467  dx / (SCALE_FACTOR / 2), dy / (SCALE_FACTOR / 2) );
468  break;
469 
471  case PAD_SHAPE::OVAL:
472  {
473  const wxSize& size = pad->GetSize();
474  int radius = std::min( size.x, size.y ) / 2;
475 
476  if( pad->GetShape() == PAD_SHAPE::ROUNDRECT )
477  {
478  radius = pad->GetRoundRectCornerRadius();
479  }
480 
481  int lineX = size.x / 2 - radius;
482  int lineY = size.y / 2 - radius;
483 
484  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
485 
486  // bottom left arc
487  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
488  ( off.x - lineX - radius ) / SCALE_FACTOR,
489  ( -off.y - lineY ) / SCALE_FACTOR, ( off.x - lineX ) / SCALE_FACTOR,
490  ( -off.y - lineY - radius ) / SCALE_FACTOR,
491  ( off.x - lineX ) / SCALE_FACTOR, ( -off.y - lineY ) / SCALE_FACTOR );
492 
493  // bottom line
494  if( lineX > 0 )
495  {
496  fprintf( aFile, "LINE %g %g %g %g\n",
497  ( off.x - lineX ) / SCALE_FACTOR,
498  ( -off.y - lineY - radius ) / SCALE_FACTOR,
499  ( off.x + lineX ) / SCALE_FACTOR,
500  ( -off.y - lineY - radius ) / SCALE_FACTOR );
501  }
502 
503  // bottom right arc
504  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
505  ( off.x + lineX ) / SCALE_FACTOR,
506  ( -off.y - lineY - radius ) / SCALE_FACTOR,
507  ( off.x + lineX + radius ) / SCALE_FACTOR,
508  ( -off.y - lineY ) / SCALE_FACTOR, ( off.x + lineX ) / SCALE_FACTOR,
509  ( -off.y - lineY ) / SCALE_FACTOR );
510 
511  // right line
512  if( lineY > 0 )
513  {
514  fprintf( aFile, "LINE %g %g %g %g\n",
515  ( off.x + lineX + radius ) / SCALE_FACTOR,
516  ( -off.y + lineY ) / SCALE_FACTOR,
517  ( off.x + lineX + radius ) / SCALE_FACTOR,
518  ( -off.y - lineY ) / SCALE_FACTOR );
519  }
520 
521  // top right arc
522  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
523  ( off.x + lineX + radius ) / SCALE_FACTOR,
524  ( -off.y + lineY ) / SCALE_FACTOR, ( off.x + lineX ) / SCALE_FACTOR,
525  ( -off.y + lineY + radius ) / SCALE_FACTOR,
526  ( off.x + lineX ) / SCALE_FACTOR, ( -off.y + lineY ) / SCALE_FACTOR );
527 
528  // top line
529  if( lineX > 0 )
530  {
531  fprintf( aFile, "LINE %g %g %g %g\n"
532  , ( off.x - lineX ) / SCALE_FACTOR,
533  ( -off.y + lineY + radius ) / SCALE_FACTOR,
534  ( off.x + lineX ) / SCALE_FACTOR,
535  ( -off.y + lineY + radius ) / SCALE_FACTOR );
536  }
537 
538  // top left arc
539  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
540  ( off.x - lineX ) / SCALE_FACTOR,
541  ( -off.y + lineY + radius ) / SCALE_FACTOR,
542  ( off.x - lineX - radius ) / SCALE_FACTOR,
543  ( -off.y + lineY ) / SCALE_FACTOR, ( off.x - lineX ) / SCALE_FACTOR,
544  ( -off.y + lineY ) / SCALE_FACTOR );
545 
546  // left line
547  if( lineY > 0 )
548  {
549  fprintf( aFile, "LINE %g %g %g %g\n",
550  ( off.x - lineX - radius ) / SCALE_FACTOR,
551  ( -off.y - lineY ) / SCALE_FACTOR,
552  ( off.x - lineX - radius ) / SCALE_FACTOR,
553  ( -off.y + lineY ) / SCALE_FACTOR );
554  }
555 
556  break;
557  }
558 
560  {
561  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
562 
563  int ddx = pad->GetDelta().x / 2;
564  int ddy = pad->GetDelta().y / 2;
565 
566  wxPoint poly[4];
567  poly[0] = wxPoint( -dx + ddy, dy + ddx );
568  poly[1] = wxPoint( dx - ddy, dy - ddx );
569  poly[2] = wxPoint( dx + ddy, -dy + ddx );
570  poly[3] = wxPoint( -dx - ddy, -dy - ddx );
571 
572  for( int cur = 0; cur < 4; ++cur )
573  {
574  int next = ( cur + 1 ) % 4;
575  fprintf( aFile, "LINE %g %g %g %g\n",
576  ( off.x + poly[cur].x ) / SCALE_FACTOR,
577  ( -off.y - poly[cur].y ) / SCALE_FACTOR,
578  ( off.x + poly[next].x ) / SCALE_FACTOR,
579  ( -off.y - poly[next].y ) / SCALE_FACTOR );
580  }
581 
582  break;
583  }
584 
586  {
587  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
588 
589  SHAPE_POLY_SET outline;
590  int maxError = aPcb->GetDesignSettings().m_MaxError;
591  wxPoint padOffset( 0, 0 );
592 
593  TransformRoundChamferedRectToPolygon( outline, padOffset, pad->GetSize(),
594  pad->GetOrientation(),
595  pad->GetRoundRectCornerRadius(),
596  pad->GetChamferRectRatio(),
597  pad->GetChamferPositions(), 0, maxError,
598  ERROR_INSIDE );
599 
600  for( int jj = 0; jj < outline.OutlineCount(); ++jj )
601  {
602  const SHAPE_LINE_CHAIN& poly = outline.COutline( jj );
603  int pointCount = poly.PointCount();
604 
605  for( int ii = 0; ii < pointCount; ii++ )
606  {
607  int next = ( ii + 1 ) % pointCount;
608  fprintf( aFile, "LINE %g %g %g %g\n",
609  poly.CPoint( ii ).x / SCALE_FACTOR,
610  -poly.CPoint( ii ).y / SCALE_FACTOR,
611  poly.CPoint( next ).x / SCALE_FACTOR,
612  -poly.CPoint( next ).y / SCALE_FACTOR );
613  }
614  }
615 
616  break;
617  }
618 
619  case PAD_SHAPE::CUSTOM:
620  {
621  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
622 
623  SHAPE_POLY_SET outline;
624  pad->MergePrimitivesAsPolygon( &outline );
625 
626  for( int jj = 0; jj < outline.OutlineCount(); ++jj )
627  {
628  const SHAPE_LINE_CHAIN& poly = outline.COutline( jj );
629  int pointCount = poly.PointCount();
630 
631  for( int ii = 0; ii < pointCount; ii++ )
632  {
633  int next = ( ii + 1 ) % pointCount;
634  fprintf( aFile, "LINE %g %g %g %g\n",
635  ( off.x + poly.CPoint( ii ).x ) / SCALE_FACTOR,
636  ( -off.y - poly.CPoint( ii ).y ) / SCALE_FACTOR,
637  ( off.x + poly.CPoint( next ).x ) / SCALE_FACTOR,
638  ( -off.y - poly.CPoint( next ).y ) / SCALE_FACTOR );
639  }
640  }
641 
642  break;
643  }
644  }
645  }
646 
647  fputs( "\n$ENDPADS\n\n", aFile );
648 
649  // Now emit the padstacks definitions, using the combined layer masks
650  fputs( "$PADSTACKS\n", aFile );
651 
652  // Via padstacks
653  for( unsigned i = 0; i < viastacks.size(); i++ )
654  {
655  PCB_VIA* via = viastacks[i];
656 
657  LSET mask = via->GetLayerSet() & master_layermask;
658 
659  fprintf( aFile, "PADSTACK VIA%d.%d.%s %g\n",
660  via->GetWidth(), via->GetDrillValue(),
661  fmt_mask( mask ).c_str(),
662  via->GetDrillValue() / SCALE_FACTOR );
663 
664  for( LSEQ seq = mask.Seq( gc_seq, arrayDim( gc_seq ) ); seq; ++seq )
665  {
666  PCB_LAYER_ID layer = *seq;
667 
668  fprintf( aFile, "PAD V%d.%d.%s %s 0 0\n",
669  via->GetWidth(), via->GetDrillValue(),
670  fmt_mask( mask ).c_str(),
671  GenCADLayerName( cu_count, layer ).c_str() );
672  }
673  }
674 
675  /* Component padstacks
676  * Older versions of CAM350 don't apply correctly the FLIP semantics for
677  * padstacks, i.e. doesn't swap the top and bottom layers... so I need to
678  * define the shape as MIRRORX and define a separate 'flipped' padstack...
679  * until it appears yet another non-compliant importer */
680  for( unsigned i = 1; i < padstacks.size(); i++ )
681  {
682  PAD* pad = padstacks[i];
683 
684  // Straight padstack
685  fprintf( aFile, "PADSTACK PAD%u %g\n", i, pad->GetDrillSize().x / SCALE_FACTOR );
686 
687  LSET pad_set = pad->GetLayerSet() & master_layermask;
688 
689  // the special gc_seq
690  for( LSEQ seq = pad_set.Seq( gc_seq, arrayDim( gc_seq ) ); seq; ++seq )
691  {
692  PCB_LAYER_ID layer = *seq;
693 
694  fprintf( aFile, "PAD P%u %s 0 0\n", i, GenCADLayerName( cu_count, layer ).c_str() );
695  }
696 
697  // Flipped padstack
698  if( flipBottomPads )
699  {
700  fprintf( aFile, "PADSTACK PAD%uF %g\n", i, pad->GetDrillSize().x / SCALE_FACTOR );
701 
702  // the normal PCB_LAYER_ID sequence is inverted from gc_seq[]
703  for( LSEQ seq = pad_set.Seq(); seq; ++seq )
704  {
705  PCB_LAYER_ID layer = *seq;
706 
707  fprintf( aFile, "PAD P%u %s 0 0\n", i,
708  GenCADLayerNameFlipped( cu_count, layer ).c_str() );
709  }
710  }
711  }
712 
713  fputs( "$ENDPADSTACKS\n\n", aFile );
714 }
715 
716 
718 static size_t hashFootprint( const FOOTPRINT* aFootprint )
719 {
720  size_t ret = 0x11223344;
721  constexpr int flags = HASH_FLAGS::HASH_POS | HASH_FLAGS::REL_COORD
723 
724  for( BOARD_ITEM* i : aFootprint->GraphicalItems() )
725  ret += hash_fp_item( i, flags );
726 
727  for( PAD* i : aFootprint->Pads() )
728  ret += hash_fp_item( i, flags );
729 
730  return ret;
731 }
732 
733 
734 /* Creates the footprint shape list.
735  * Since module shape is customizable after the placement we cannot share them;
736  * instead we opt for the one-module-one-shape-one-component-one-device approach
737  */
738 static void CreateShapesSection( FILE* aFile, BOARD* aPcb )
739 {
740  const char* layer;
741  wxString pinname;
742  const char* mirror = "0";
743  std::map<wxString, size_t> shapes;
744 
745  fputs( "$SHAPES\n", aFile );
746 
747  for( FOOTPRINT* footprint : aPcb->Footprints() )
748  {
749  if( !individualShapes )
750  {
751  // Check if such shape has been already generated, and if so - reuse it
752  // It is necessary to compute hash (i.e. check all children objects) as
753  // certain components instances might have been modified on the board.
754  // In such case the shape will be different despite the same LIB_ID.
755  wxString shapeName = footprint->GetFPID().Format();
756 
757  auto shapeIt = shapes.find( shapeName );
758  size_t modHash = hashFootprint( footprint );
759 
760  if( shapeIt != shapes.end() )
761  {
762  if( modHash != shapeIt->second )
763  {
764  // there is an entry for this footprint, but it has a modified shape,
765  // so we need to create a new entry
766  wxString newShapeName;
767  int suffix = 0;
768 
769  // find an unused name or matching entry
770  do
771  {
772  newShapeName = wxString::Format( wxT( "%s_%d" ), shapeName, suffix );
773  shapeIt = shapes.find( newShapeName );
774  ++suffix;
775  }
776  while( shapeIt != shapes.end() && shapeIt->second != modHash );
777 
778  shapeName = newShapeName;
779  }
780 
781  if( shapeIt != shapes.end() && modHash == shapeIt->second )
782  {
783  // shape found, so reuse it
784  componentShapes[footprint] = modHash;
785  continue;
786  }
787  }
788 
789  // new shape
790  componentShapes[footprint] = modHash;
791  shapeNames[modHash] = shapeName;
792  shapes[shapeName] = modHash;
793  FootprintWriteShape( aFile, footprint, shapeName );
794  }
795  else // individual shape for each component
796  {
797  FootprintWriteShape( aFile, footprint, footprint->GetReference() );
798  }
799 
800  // set of already emitted pins to check for duplicates
801  std::set<wxString> pins;
802 
803  for( PAD* pad : footprint->Pads() )
804  {
805  /* Padstacks are defined using the correct layers for the pads, therefore to
806  * all pads need to be marked as TOP to use the padstack information correctly.
807  */
808  layer = "TOP";
809  pinname = pad->GetNumber();
810 
811  if( pinname.IsEmpty() )
812  pinname = wxT( "none" );
813 
814  if( uniquePins )
815  {
816  int suffix = 0;
817  wxString origPinname( pinname );
818 
819  auto it = pins.find( pinname );
820 
821  while( it != pins.end() )
822  {
823  pinname = wxString::Format( wxT( "%s_%d" ), origPinname, suffix );
824  ++suffix;
825  it = pins.find( pinname );
826  }
827 
828  pins.insert( pinname );
829  }
830 
831  double orient = pad->GetOrientation() - footprint->GetOrientation();
832  NORMALIZE_ANGLE_POS( orient );
833 
834  // Bottom side footprints use the flipped padstack
835  fprintf( aFile, ( flipBottomPads && footprint->GetFlag() ) ?
836  "PIN \"%s\" PAD%dF %g %g %s %g %s\n" :
837  "PIN \"%s\" PAD%d %g %g %s %g %s\n",
838  TO_UTF8( escapeString( pinname ) ), pad->GetSubRatsnest(),
839  pad->GetPos0().x / SCALE_FACTOR,
840  -pad->GetPos0().y / SCALE_FACTOR,
841  layer, orient / 10.0, mirror );
842  }
843  }
844 
845  fputs( "$ENDSHAPES\n\n", aFile );
846 }
847 
848 
849 /* Creates the section $COMPONENTS (Footprints placement)
850  * Bottom side components are difficult to handle: shapes must be mirrored or
851  * flipped, silk layers need to be handled correctly and so on. Also it seems
852  * that *no one* follows the specs...
853  */
854 static void CreateComponentsSection( FILE* aFile, BOARD* aPcb )
855 {
856  fputs( "$COMPONENTS\n", aFile );
857 
858  int cu_count = aPcb->GetCopperLayerCount();
859 
860  for( FOOTPRINT* footprint : aPcb->Footprints() )
861  {
862  const char* mirror;
863  const char* flip;
864  double fp_orient = footprint->GetOrientation();
865 
866  if( footprint->GetFlag() )
867  {
868  mirror = "MIRRORX";
869  flip = "FLIP";
870  NEGATE_AND_NORMALIZE_ANGLE_POS( fp_orient );
871  }
872  else
873  {
874  mirror = "0";
875  flip = "0";
876  }
877 
878  fprintf( aFile, "\nCOMPONENT \"%s\"\n",
879  TO_UTF8( escapeString( footprint->GetReference() ) ) );
880  fprintf( aFile, "DEVICE \"DEV_%s\"\n",
881  TO_UTF8( escapeString( getShapeName( footprint ) ) ) );
882  fprintf( aFile, "PLACE %g %g\n",
883  MapXTo( footprint->GetPosition().x ),
884  MapYTo( footprint->GetPosition().y ) );
885  fprintf( aFile, "LAYER %s\n",
886  footprint->GetFlag() ? "BOTTOM" : "TOP" );
887  fprintf( aFile, "ROTATION %g\n",
888  fp_orient / 10.0 );
889  fprintf( aFile, "SHAPE \"%s\" %s %s\n",
890  TO_UTF8( escapeString( getShapeName( footprint ) ) ),
891  mirror, flip );
892 
893  // Text on silk layer: RefDes and value (are they actually useful?)
894  for( FP_TEXT* textItem : { &footprint->Reference(), &footprint->Value() } )
895  {
896  double txt_orient = textItem->GetTextAngle();
897  std::string layer = GenCADLayerName( cu_count,
898  footprint->GetFlag() ? B_SilkS : F_SilkS );
899 
900  fprintf( aFile, "TEXT %g %g %g %g %s %s \"%s\"",
901  textItem->GetPos0().x / SCALE_FACTOR,
902  -textItem->GetPos0().y / SCALE_FACTOR,
903  textItem->GetTextWidth() / SCALE_FACTOR,
904  txt_orient / 10.0,
905  mirror,
906  layer.c_str(),
907  TO_UTF8( escapeString( textItem->GetText() ) ) );
908 
909  // Please note, the width is approx
910  fprintf( aFile, " 0 0 %g %g\n",
911  ( textItem->GetTextWidth() * textItem->GetLength() ) / SCALE_FACTOR,
912  textItem->GetTextHeight() / SCALE_FACTOR );
913  }
914 
915  // The SHEET is a 'generic description' for referencing the component
916  fprintf( aFile, "SHEET \"RefDes: %s, Value: %s\"\n",
917  TO_UTF8( footprint->GetReference() ),
918  TO_UTF8( footprint->GetValue() ) );
919  }
920 
921  fputs( "$ENDCOMPONENTS\n\n", aFile );
922 }
923 
924 
925 /* Emit the netlist (which is actually the thing for which GenCAD is used these
926  * days!); tracks are handled later */
927 static void CreateSignalsSection( FILE* aFile, BOARD* aPcb )
928 {
929  wxString msg;
930  NETINFO_ITEM* net;
931  int NbNoConn = 1;
932 
933  fputs( "$SIGNALS\n", aFile );
934 
935  for( unsigned ii = 0; ii < aPcb->GetNetCount(); ii++ )
936  {
937  net = aPcb->FindNet( ii );
938 
939  if( net->GetNetname() == wxEmptyString ) // dummy netlist (no connection)
940  {
941  msg.Printf( wxT( "NoConnection%d" ), NbNoConn++ );
942  }
943 
944  if( net->GetNetCode() <= 0 ) // dummy netlist (no connection)
945  continue;
946 
947  msg = wxT( "SIGNAL \"" ) + escapeString( net->GetNetname() ) + wxT( "\"" );
948 
949  fputs( TO_UTF8( msg ), aFile );
950  fputs( "\n", aFile );
951 
952  for( FOOTPRINT* footprint : aPcb->Footprints() )
953  {
954  for( PAD* pad : footprint->Pads() )
955  {
956  if( pad->GetNetCode() != net->GetNetCode() )
957  continue;
958 
959  msg.Printf( wxT( "NODE \"%s\" \"%s\"" ),
960  escapeString( footprint->GetReference() ),
961  escapeString( pad->GetNumber() ) );
962 
963  fputs( TO_UTF8( msg ), aFile );
964  fputs( "\n", aFile );
965  }
966  }
967  }
968 
969  fputs( "$ENDSIGNALS\n\n", aFile );
970 }
971 
972 
973 // Creates the header section
974 static bool CreateHeaderInfoData( FILE* aFile, PCB_EDIT_FRAME* aFrame )
975 {
976  wxString msg;
977  BOARD* board = aFrame->GetBoard();
978 
979  fputs( "$HEADER\n", aFile );
980  fputs( "GENCAD 1.4\n", aFile );
981 
982  // Please note: GenCAD syntax requires quoted strings if they can contain spaces
983  msg.Printf( wxT( "USER \"%s %s\"\n" ),
984  Pgm().App().GetAppName(),
985  GetBuildVersion() );
986  fputs( TO_UTF8( msg ), aFile );
987 
988  msg = wxT( "DRAWING \"" ) + board->GetFileName() + wxT( "\"\n" );
989  fputs( TO_UTF8( msg ), aFile );
990 
991  wxString rev = ExpandTextVars( board->GetTitleBlock().GetRevision(), board->GetProject() );
992  wxString date = ExpandTextVars( board->GetTitleBlock().GetDate(), board->GetProject() );
993  msg = wxT( "REVISION \"" ) + rev + wxT( " " ) + date + wxT( "\"\n" );
994 
995  fputs( TO_UTF8( msg ), aFile );
996  fputs( "UNITS INCH\n", aFile );
997 
998  // giving 0 as the argument to Map{X,Y}To returns the scaled origin point
999  msg.Printf( wxT( "ORIGIN %g %g\n" ),
1000  storeOriginCoords ? MapXTo( 0 ) : 0,
1001  storeOriginCoords ? MapYTo( 0 ) : 0 );
1002  fputs( TO_UTF8( msg ), aFile );
1003 
1004  fputs( "INTERTRACK 0\n", aFile );
1005  fputs( "$ENDHEADER\n\n", aFile );
1006 
1007  return true;
1008 }
1009 
1010 
1011 /* Creates the section ROUTES
1012  * that handles tracks, vias
1013  * TODO: add zones
1014  * section:
1015  * $ROUTE
1016  * ...
1017  * $ENROUTE
1018  * Track segments must be sorted by nets
1019  */
1020 static void CreateRoutesSection( FILE* aFile, BOARD* aPcb )
1021 {
1022  int vianum = 1;
1023  int old_netcode, old_width, old_layer;
1024  LSET master_layermask = aPcb->GetDesignSettings().GetEnabledLayers();
1025 
1026  int cu_count = aPcb->GetCopperLayerCount();
1027 
1028  TRACKS tracks( aPcb->Tracks() );
1029  std::sort( tracks.begin(), tracks.end(),
1030  []( const PCB_TRACK* a, const PCB_TRACK* b )
1031  {
1032  if( a->GetNetCode() == b->GetNetCode() )
1033  {
1034  if( a->GetWidth() == b->GetWidth() )
1035  return ( a->GetLayer() < b->GetLayer() );
1036 
1037  return ( a->GetWidth() < b->GetWidth() );
1038  }
1039 
1040  return ( a->GetNetCode() < b->GetNetCode() );
1041  } );
1042 
1043  fputs( "$ROUTES\n", aFile );
1044 
1045  old_netcode = -1; old_width = -1; old_layer = -1;
1046 
1047  for( PCB_TRACK* track : tracks )
1048  {
1049  if( old_netcode != track->GetNetCode() )
1050  {
1051  old_netcode = track->GetNetCode();
1052  NETINFO_ITEM* net = track->GetNet();
1053  wxString netname;
1054 
1055  if( net && (net->GetNetname() != wxEmptyString) )
1056  netname = net->GetNetname();
1057  else
1058  netname = wxT( "_noname_" );
1059 
1060  fprintf( aFile, "ROUTE \"%s\"\n", TO_UTF8( escapeString( netname ) ) );
1061  }
1062 
1063  if( old_width != track->GetWidth() )
1064  {
1065  old_width = track->GetWidth();
1066  fprintf( aFile, "TRACK TRACK%d\n", track->GetWidth() );
1067  }
1068 
1069  if( track->Type() == PCB_TRACE_T )
1070  {
1071  if( old_layer != track->GetLayer() )
1072  {
1073  old_layer = track->GetLayer();
1074  fprintf( aFile, "LAYER %s\n",
1075  GenCADLayerName( cu_count, track->GetLayer() ).c_str() );
1076  }
1077 
1078  fprintf( aFile, "LINE %g %g %g %g\n",
1079  MapXTo( track->GetStart().x ), MapYTo( track->GetStart().y ),
1080  MapXTo( track->GetEnd().x ), MapYTo( track->GetEnd().y ) );
1081  }
1082 
1083  if( track->Type() == PCB_VIA_T )
1084  {
1085  const PCB_VIA* via = static_cast<const PCB_VIA*>(track);
1086 
1087  LSET vset = via->GetLayerSet() & master_layermask;
1088 
1089  fprintf( aFile, "VIA VIA%d.%d.%s %g %g ALL %g via%d\n",
1090  via->GetWidth(), via->GetDrillValue(),
1091  fmt_mask( vset ).c_str(),
1092  MapXTo( via->GetStart().x ), MapYTo( via->GetStart().y ),
1093  via->GetDrillValue() / SCALE_FACTOR, vianum++ );
1094  }
1095  }
1096 
1097  fputs( "$ENDROUTES\n\n", aFile );
1098 }
1099 
1100 
1101 /* Creates the section $DEVICES
1102  * This is a list of footprints properties
1103  * ( Shapes are in section $SHAPE )
1104  */
1105 static void CreateDevicesSection( FILE* aFile, BOARD* aPcb )
1106 {
1107  std::set<wxString> emitted;
1108  fputs( "$DEVICES\n", aFile );
1109 
1110  for( const auto& componentShape : componentShapes )
1111  {
1112  const wxString& shapeName = shapeNames[componentShape.second];
1113  bool newDevice;
1114  std::tie( std::ignore, newDevice ) = emitted.insert( shapeName );
1115 
1116  if( !newDevice ) // do not repeat device definitions
1117  continue;
1118 
1119  const FOOTPRINT* footprint = componentShape.first;
1120 
1121  fprintf( aFile, "\nDEVICE \"DEV_%s\"\n", TO_UTF8( escapeString( shapeName ) ) );
1122 
1123  fprintf( aFile, "PART \"%s\"\n",
1124  TO_UTF8( escapeString( footprint->GetValue() ) ) );
1125 
1126  fprintf( aFile, "PACKAGE \"%s\"\n",
1127  TO_UTF8( escapeString( footprint->GetFPID().Format() ) ) );
1128  }
1129 
1130  fputs( "$ENDDEVICES\n\n", aFile );
1131 }
1132 
1133 
1134 /* Creates the section $BOARD.
1135  * We output here only the board perimeter
1136  */
1137 static void CreateBoardSection( FILE* aFile, BOARD* aPcb )
1138 {
1139  fputs( "$BOARD\n", aFile );
1140 
1141  // Extract the board edges
1142  for( BOARD_ITEM* drawing : aPcb->Drawings() )
1143  {
1144  if( drawing->Type() == PCB_SHAPE_T )
1145  {
1146  PCB_SHAPE* drawseg = static_cast<PCB_SHAPE*>( drawing );
1147 
1148  if( drawseg->GetLayer() == Edge_Cuts )
1149  {
1150  // XXX GenCAD supports arc boundaries but I've seen nothing that reads them
1151  fprintf( aFile, "LINE %g %g %g %g\n",
1152  MapXTo( drawseg->GetStart().x ), MapYTo( drawseg->GetStart().y ),
1153  MapXTo( drawseg->GetEnd().x ), MapYTo( drawseg->GetEnd().y ) );
1154  }
1155  }
1156  }
1157 
1158  fputs( "$ENDBOARD\n\n", aFile );
1159 }
1160 
1161 
1162 /* Creates the section "$TRACKS"
1163  * This sections give the list of widths (tools) used in tracks and vias
1164  * format:
1165  * $TRACK
1166  * TRACK <name> <width>
1167  * $ENDTRACK
1168  *
1169  * Each tool name is build like this: "TRACK" + track width.
1170  * For instance for a width = 120 : name = "TRACK120".
1171  */
1172 static void CreateTracksInfoData( FILE* aFile, BOARD* aPcb )
1173 {
1174  // Find thickness used for traces
1175 
1176  std::set<int> trackinfo;
1177 
1178  for( PCB_TRACK* track : aPcb->Tracks() )
1179  trackinfo.insert( track->GetWidth() );
1180 
1181  // Write data
1182  fputs( "$TRACKS\n", aFile );
1183 
1184  for( int size : trackinfo )
1185  fprintf( aFile, "TRACK TRACK%d %g\n", size, size / SCALE_FACTOR );
1186 
1187  fputs( "$ENDTRACKS\n\n", aFile );
1188 }
1189 
1190 
1191 /* Creates the shape of a footprint (section SHAPE)
1192  * The shape is always given "normal" (Orient 0, not mirrored)
1193  * It's almost guaranteed that the silk layer will be imported wrong but
1194  * the shape also contains the pads!
1195  */
1196 static void FootprintWriteShape( FILE* aFile, FOOTPRINT* aFootprint, const wxString& aShapeName )
1197 {
1198  FP_SHAPE* shape;
1199 
1200  /* creates header: */
1201  fprintf( aFile, "\nSHAPE \"%s\"\n", TO_UTF8( escapeString( aShapeName ) ) );
1202 
1203  if( aFootprint->GetAttributes() & FP_THROUGH_HOLE )
1204  fprintf( aFile, "INSERT TH\n" );
1205  else
1206  fprintf( aFile, "INSERT SMD\n" );
1207 
1208  // Silk outline; wildly interpreted by various importers:
1209  // CAM350 read it right but only closed shapes
1210  // ProntoPlace double-flip it (at least the pads are correct)
1211  // GerberTool usually get it right...
1212  for( BOARD_ITEM* PtStruct : aFootprint->GraphicalItems() )
1213  {
1214  switch( PtStruct->Type() )
1215  {
1216  case PCB_FP_TEXT_T:
1217 
1218  // If we wanted to export text, this is not the correct section
1219  break;
1220 
1221  case PCB_FP_SHAPE_T:
1222  shape = (FP_SHAPE*) PtStruct;
1223 
1224  if( shape->GetLayer() == F_SilkS || shape->GetLayer() == B_SilkS )
1225  {
1226  switch( shape->GetShape() )
1227  {
1228  case SHAPE_T::SEGMENT:
1229  fprintf( aFile, "LINE %g %g %g %g\n",
1230  shape->GetStart0().x / SCALE_FACTOR,
1231  -shape->GetStart0().y / SCALE_FACTOR,
1232  shape->GetEnd0().x / SCALE_FACTOR,
1233  -shape->GetEnd0().y / SCALE_FACTOR );
1234  break;
1235 
1236  case SHAPE_T::RECT:
1237  {
1238  fprintf( aFile, "LINE %g %g %g %g\n",
1239  shape->GetStart0().x / SCALE_FACTOR,
1240  -shape->GetStart0().y / SCALE_FACTOR,
1241  shape->GetEnd0().x / SCALE_FACTOR,
1242  -shape->GetStart0().y / SCALE_FACTOR );
1243  fprintf( aFile, "LINE %g %g %g %g\n",
1244  shape->GetEnd0().x / SCALE_FACTOR,
1245  -shape->GetStart0().y / SCALE_FACTOR,
1246  shape->GetEnd0().x / SCALE_FACTOR,
1247  -shape->GetEnd0().y / SCALE_FACTOR );
1248  fprintf( aFile, "LINE %g %g %g %g\n",
1249  shape->GetEnd0().x / SCALE_FACTOR,
1250  -shape->GetEnd0().y / SCALE_FACTOR,
1251  shape->GetStart0().x / SCALE_FACTOR,
1252  -shape->GetEnd0().y / SCALE_FACTOR );
1253  fprintf( aFile, "LINE %g %g %g %g\n",
1254  shape->GetStart0().x / SCALE_FACTOR,
1255  -shape->GetEnd0().y / SCALE_FACTOR,
1256  shape->GetStart0().x / SCALE_FACTOR,
1257  -shape->GetStart0().y / SCALE_FACTOR );
1258  }
1259  break;
1260 
1261  case SHAPE_T::CIRCLE:
1262  {
1263  int radius = KiROUND( GetLineLength( shape->GetEnd0(), shape->GetStart0() ) );
1264 
1265  fprintf( aFile, "CIRCLE %g %g %g\n",
1266  shape->GetStart0().x / SCALE_FACTOR,
1267  -shape->GetStart0().y / SCALE_FACTOR,
1268  radius / SCALE_FACTOR );
1269  break;
1270  }
1271 
1272  case SHAPE_T::ARC:
1273  {
1274  wxPoint start = shape->GetStart0();
1275  wxPoint end = shape->GetEnd0();
1276 
1277  if( shape->GetArcAngle() > 0 )
1278  std::swap( start, end );
1279 
1280  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
1281  start.x / SCALE_FACTOR,
1282  -start.y / SCALE_FACTOR,
1283  end.x / SCALE_FACTOR,
1284  -end.y / SCALE_FACTOR,
1285  shape->GetCenter0().x / SCALE_FACTOR,
1286  -shape->GetCenter0().y / SCALE_FACTOR );
1287  }
1288  break;
1289 
1290  case SHAPE_T::POLY:
1291  // Not exported (TODO)
1292  break;
1293 
1294  default:
1295  wxFAIL_MSG( wxString::Format( wxT( "Type Edge Module %d invalid." ),
1296  PtStruct->Type() ) );
1297  break;
1298  }
1299  }
1300  break;
1301 
1302  default:
1303  break;
1304  }
1305  }
1306 }
bool GetOption(GENCAD_EXPORT_OPT aOption) const
Return all export settings.
static const wxString getShapeName(FOOTPRINT *aFootprint)
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:759
static void CreateBoardSection(FILE *aFile, BOARD *aPcb)
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:279
CITER next(CITER it)
Definition: ptree.cpp:126
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition: board.cpp:1328
static void CreateShapesSection(FILE *aFile, BOARD *aPcb)
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
const wxString & GetValue() const
Definition: footprint.h:488
static std::map< FOOTPRINT *, int > componentShapes
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject)
Definition: common.cpp:57
void Compile_Ratsnest(bool aDisplayStatus)
Create the entire board ratsnest.
Definition: ratsnest.cpp:40
This file is part of the common library.
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:106
use coordinates relative to the parent object
Definition: hash_eda.h:43
static void CreateRoutesSection(FILE *aFile, BOARD *aPcb)
static void CreateSignalsSection(FILE *aFile, BOARD *aPcb)
static double MapYTo(int aY)
PROJECT * GetProject() const
Definition: board.h:361
double GetArcAngle() const
Definition: eda_shape.cpp:536
static bool uniquePins
const wxPoint & GetStart0() const
Definition: fp_shape.h:112
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: pcb_track.cpp:375
void ExportToGenCAD(wxCommandEvent &event)
Create a file in GenCAD 1.4 format from the current board.
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
int GetWidth() const
Definition: pcb_track.h:102
static wxString escapeString(const wxString &aString)
static int GencadOffsetY
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:290
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
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
const wxString & GetFileName() const
Definition: board.h:229
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:590
static void CreateTracksInfoData(FILE *aFile, BOARD *aPcb)
wxString GetLastPath(LAST_PATH_TYPE aType)
Get the last path for a particular type.
static void CreateArtworksSection(FILE *aFile)
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
PADS & Pads()
Definition: footprint.h:169
void vset(double *v, double x, double y, double z)
Definition: trackball.cpp:82
This file contains miscellaneous commonly used macros and functions.
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.
wxPoint GetCenter0() const
Definition: fp_shape.cpp:145
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
#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.
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)
static void CreateComponentsSection(FILE *aFile, BOARD *aPcb)
static bool individualShapes
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:131
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:516
unsigned GetNetCount() const
Definition: board.h:711
wxString GetBuildVersion()
Get the full KiCad version string.
const wxString & GetRevision() const
Definition: title_block.h:86
static void CreatePadsShapesSection(FILE *aFile, BOARD *aPcb)
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:1850
FOOTPRINTS & Footprints()
Definition: board.h:234
static const PCB_LAYER_ID gc_seq[]
size_t hash_fp_item(const EDA_ITEM *aItem, int aFlags)
Calculate hash of an EDA_ITEM.
Definition: hash_eda.cpp:49
const wxString & GetNetname() const
Definition: netinfo.h:126
const wxString & GetReference() const
Definition: footprint.h:466
#define _(s)
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
static void FootprintWriteShape(FILE *File, FOOTPRINT *aFootprint, const wxString &aShapeName)
DRAWINGS & GraphicalItems()
Definition: footprint.h:172
const LIB_ID & GetFPID() const
Definition: footprint.h:195
static bool storeOriginCoords
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition: layer_ids.h:477
UTF8 Format() const
Definition: lib_id.cpp:116
bool IsCopperLayer(LAYER_NUM aLayerId)
Tests whether a layer is a copper layer.
Definition: layer_ids.h:808
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 std::string GenCADLayerName(int aCuCount, PCB_LAYER_ID aId)
static int Compare(const PAD *aPadRef, const PAD *aPadCmp)
Compare two pads and return 0 if they are equal.
Definition: pad.cpp:1066
void NEGATE_AND_NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:362
const wxString & GetDate() const
Definition: title_block.h:76
Handle the data for a net.
Definition: netinfo.h:66
static bool ViaSort(const PCB_VIA *aPadref, const PCB_VIA *aPadcmp)
TITLE_BLOCK & GetTitleBlock()
Definition: board.h:542
see class PGM_BASE
int GetAttributes() const
Definition: footprint.h:239
std::string FmtBin() const
Return a binary string showing contents of this LSEQ.
Definition: lset.cpp:297
const wxPoint & GetAuxOrigin()
static bool CreateHeaderInfoData(FILE *aFile, PCB_EDIT_FRAME *frame)
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:191
static void CreateDevicesSection(FILE *aFile, BOARD *aPcb)
LSET GetEnabledLayers() const
Return a bit-mask of all the layers that are enabled.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:65
Definition: layer_ids.h:71
int GetCopperLayerCount() const
Definition: board.cpp:455
The main frame for Pcbnew.
const wxPoint & GetEnd0() const
Definition: fp_shape.h:115
#define IU_PER_MILS
Definition: plotter.cpp:130
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
EDA_RECT ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Calculate the bounding box containing all board items (or board edge segments).
Definition: board.cpp:1085
static int GencadOffsetX
int GetDrillValue() const
Function GetDrillValue "calculates" the drill value for vias (m-Drill if > 0, or default drill value ...
Definition: pcb_track.cpp:166
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
SHAPE_T GetShape() const
Definition: eda_shape.h:101
static std::string fmt_mask(LSET aSet)
BOARD * GetBoard() const
Definition: pad.h:57
static std::map< int, wxString > shapeNames
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:143
DRAWINGS & Drawings()
Definition: board.h:237
TRACKS & Tracks()
Definition: board.h:231
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.
static double MapXTo(int aX)
int GetNetCode() const
Definition: netinfo.h:120