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, jean-pierre.charras@ujf-grenoble.fr
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
7  * Copyright (C) 1992-2016 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 <class_board.h>
34 #include <class_edge_mod.h>
35 #include <class_module.h>
36 #include <class_track.h>
37 #include <confirm.h>
39 #include <fctsys.h>
40 #include <gestfich.h>
41 #include <hash_eda.h>
42 #include <macros.h>
43 #include <math/util.h> // for KiROUND
44 #include <pcb_edit_frame.h>
45 #include <pcbnew.h>
46 #include <pcbnew_settings.h>
47 #include <pgm_base.h>
48 #include <trigo.h>
49 
50 static bool CreateHeaderInfoData( FILE* aFile, PCB_EDIT_FRAME* frame );
51 static void CreateArtworksSection( FILE* aFile );
52 static void CreateTracksInfoData( FILE* aFile, BOARD* aPcb );
53 static void CreateBoardSection( FILE* aFile, BOARD* aPcb );
54 static void CreateComponentsSection( FILE* aFile, BOARD* aPcb );
55 static void CreateDevicesSection( FILE* aFile, BOARD* aPcb );
56 static void CreateRoutesSection( FILE* aFile, BOARD* aPcb );
57 static void CreateSignalsSection( FILE* aFile, BOARD* aPcb );
58 static void CreateShapesSection( FILE* aFile, BOARD* aPcb );
59 static void CreatePadsShapesSection( FILE* aFile, BOARD* aPcb );
60 static void FootprintWriteShape( FILE* File, MODULE* module, const wxString& aShapeName );
61 
62 // layer names for Gencad export
63 
64 #if 0 // was:
65 static const wxString GenCADLayerName[] =
66 {
67  wxT( "BOTTOM" ), wxT( "INNER1" ), wxT( "INNER2" ),
68  wxT( "INNER3" ), wxT( "INNER4" ), wxT( "INNER5" ),
69  wxT( "INNER6" ), wxT( "INNER7" ), wxT( "INNER8" ),
70  wxT( "INNER9" ), wxT( "INNER10" ), wxT( "INNER11" ),
71  wxT( "INNER12" ), wxT( "INNER13" ), wxT( "INNER14" ),
72  wxT( "TOP" ), wxT( "LAYER17" ), wxT( "LAYER18" ),
73  wxT( "SOLDERPASTE_BOTTOM" ), wxT( "SOLDERPASTE_TOP" ),
74  wxT( "SILKSCREEN_BOTTOM" ), wxT( "SILKSCREEN_TOP" ),
75  wxT( "SOLDERMASK_BOTTOM" ), wxT( "SOLDERMASK_TOP" ), wxT( "LAYER25" ),
76  wxT( "LAYER26" ), wxT( "LAYER27" ), wxT( "LAYER28" ),
77  wxT( "LAYER29" ), wxT( "LAYER30" ), wxT( "LAYER31" ),
78  wxT( "LAYER32" )
79 };
80 
81 // flipped layer name for Gencad export (to make CAM350 imports correct)
82 static const wxString GenCADLayerNameFlipped[32] =
83 {
84  wxT( "TOP" ), wxT( "INNER14" ), wxT( "INNER13" ),
85  wxT( "INNER12" ), wxT( "INNER11" ), wxT( "INNER10" ),
86  wxT( "INNER9" ), wxT( "INNER8" ), wxT( "INNER7" ),
87  wxT( "INNER6" ), wxT( "INNER5" ), wxT( "INNER4" ),
88  wxT( "INNER3" ), wxT( "INNER2" ), wxT( "INNER1" ),
89  wxT( "BOTTOM" ), wxT( "LAYER17" ), wxT( "LAYER18" ),
90  wxT( "SOLDERPASTE_TOP" ), wxT( "SOLDERPASTE_BOTTOM" ),
91  wxT( "SILKSCREEN_TOP" ), wxT( "SILKSCREEN_BOTTOM" ),
92  wxT( "SOLDERMASK_TOP" ), wxT( "SOLDERMASK_BOTTOM" ), wxT( "LAYER25" ),
93  wxT( "LAYER26" ), wxT( "LAYER27" ), wxT( "LAYER28" ),
94  wxT( "LAYER29" ), wxT( "LAYER30" ), wxT( "LAYER31" ),
95  wxT( "LAYER32" )
96 };
97 
98 #else
99 
100 static std::string GenCADLayerName( int aCuCount, PCB_LAYER_ID aId )
101 {
102  if( IsCopperLayer( aId ) )
103  {
104  if( aId == F_Cu )
105  return "TOP";
106  else if( aId == B_Cu )
107  return "BOTTOM";
108 
109  else if( aId <= 14 )
110  {
111  return StrPrintf( "INNER%d", aCuCount - aId - 1 );
112  }
113  else
114  {
115  return StrPrintf( "LAYER%d", aId );
116  }
117  }
118 
119  else
120  {
121  const char* txt;
122 
123  // using a switch to clearly show mapping & catch out of bounds index.
124  switch( aId )
125  {
126  // Technicals
127  case B_Adhes: txt = "B.Adhes"; break;
128  case F_Adhes: txt = "F.Adhes"; break;
129  case B_Paste: txt = "SOLDERPASTE_BOTTOM"; break;
130  case F_Paste: txt = "SOLDERPASTE_TOP"; break;
131  case B_SilkS: txt = "SILKSCREEN_BOTTOM"; break;
132  case F_SilkS: txt = "SILKSCREEN_TOP"; break;
133  case B_Mask: txt = "SOLDERMASK_BOTTOM"; break;
134  case F_Mask: txt = "SOLDERMASK_TOP"; break;
135 
136  // Users
137  case Dwgs_User: txt = "Dwgs.User"; break;
138  case Cmts_User: txt = "Cmts.User"; break;
139  case Eco1_User: txt = "Eco1.User"; break;
140  case Eco2_User: txt = "Eco2.User"; break;
141  case Edge_Cuts: txt = "Edge.Cuts"; break;
142  case Margin: txt = "Margin"; break;
143 
144  // Footprint
145  case F_CrtYd: txt = "F_CrtYd"; break;
146  case B_CrtYd: txt = "B_CrtYd"; break;
147  case F_Fab: txt = "F_Fab"; break;
148  case B_Fab: txt = "B_Fab"; break;
149 
150  default:
151  wxASSERT_MSG( 0, wxT( "aId UNEXPECTED" ) );
152  txt = "BAD-INDEX!"; break;
153  }
154 
155  return txt;
156  }
157 }
158 
159 
160 static const PCB_LAYER_ID gc_seq[] = {
161  B_Cu,
162  In30_Cu,
163  In29_Cu,
164  In28_Cu,
165  In27_Cu,
166  In26_Cu,
167  In25_Cu,
168  In24_Cu,
169  In23_Cu,
170  In22_Cu,
171  In21_Cu,
172  In20_Cu,
173  In19_Cu,
174  In18_Cu,
175  In17_Cu,
176  In16_Cu,
177  In15_Cu,
178  In14_Cu,
179  In13_Cu,
180  In12_Cu,
181  In11_Cu,
182  In10_Cu,
183  In9_Cu,
184  In8_Cu,
185  In7_Cu,
186  In6_Cu,
187  In5_Cu,
188  In4_Cu,
189  In3_Cu,
190  In2_Cu,
191  In1_Cu,
192  F_Cu,
193 };
194 
195 
196 // flipped layer name for Gencad export (to make CAM350 imports correct)
197 static std::string GenCADLayerNameFlipped( int aCuCount, PCB_LAYER_ID aId )
198 {
199  if( 1<= aId && aId <= 14 )
200  {
201  return StrPrintf( "INNER%d", 14 - aId );
202  }
203 
204  return GenCADLayerName( aCuCount, aId );
205 }
206 
207 
208 static wxString escapeString( const wxString& aString )
209 {
210  wxString copy( aString );
211  copy.Replace( "\"", "\\\"" );
212  return copy;
213 }
214 
215 #endif
216 
217 static std::string fmt_mask( LSET aSet )
218 {
219 #if 0
220  return aSet.FmtHex();
221 #else
222  return StrPrintf( "%08x", (unsigned) ( aSet & LSET::AllCuMask() ).to_ulong() );
223 #endif
224 }
225 
226 // Export options
227 static bool flipBottomPads;
228 static bool uniquePins;
229 static bool individualShapes;
230 static bool storeOriginCoords;
231 
232 // These are the export origin (the auxiliary axis)
234 
235 // Association between shape names (using shapeName index) and components
236 static std::map<MODULE*, int> componentShapes;
237 static std::map<int, wxString> shapeNames;
238 
239 static const wxString getShapeName( MODULE* aModule )
240 {
241  static const wxString invalid( "invalid" );
242 
243  if( individualShapes )
244  return aModule->GetReference();
245 
246  auto itShape = componentShapes.find( aModule );
247  wxCHECK( itShape != componentShapes.end(), invalid );
248 
249  auto itName = shapeNames.find( itShape->second );
250  wxCHECK( itName != shapeNames.end(), invalid );
251 
252  return itName->second;
253 }
254 
255 // GerbTool chokes on units different than INCH so this is the conversion factor
256 const static double SCALE_FACTOR = 1000.0 * IU_PER_MILS;
257 
258 /* Two helper functions to calculate coordinates of modules in gencad values
259  * (GenCAD Y axis from bottom to top)
260  */
261 static double MapXTo( int aX )
262 {
263  return (aX - GencadOffsetX) / SCALE_FACTOR;
264 }
265 
266 
267 static double MapYTo( int aY )
268 {
269  return (GencadOffsetY - aY) / SCALE_FACTOR;
270 }
271 
272 
273 /* Driver function: processing starts here */
274 void PCB_EDIT_FRAME::ExportToGenCAD( wxCommandEvent& aEvent )
275 {
276  // Build default output file name
277  wxString path = GetLastPath( LAST_PATH_GENCAD );
278 
279  if( path.IsEmpty() )
280  {
281  wxFileName brdFile = GetBoard()->GetFileName();
282  brdFile.SetExt( "cad" );
283  path = brdFile.GetFullPath();
284  }
285 
286  DIALOG_GENCAD_EXPORT_OPTIONS optionsDialog( this, path );
287 
288  if( optionsDialog.ShowModal() == wxID_CANCEL )
289  return;
290 
291  path = optionsDialog.GetFileName();
292  SetLastPath( LAST_PATH_GENCAD, path );
293  FILE* file = wxFopen( path, "wt" );
294 
295  if( !file )
296  {
297  DisplayError( this, wxString::Format( _( "Unable to create \"%s\"" ),
298  optionsDialog.GetFileName() ) );
299  return;
300  }
301 
302  // Get options
303  flipBottomPads = optionsDialog.GetOption( FLIP_BOTTOM_PADS );
304  uniquePins = optionsDialog.GetOption( UNIQUE_PIN_NAMES );
305  individualShapes = optionsDialog.GetOption( INDIVIDUAL_SHAPES );
307 
308  // Switch the locale to standard C (needed to print floating point numbers)
309  LOCALE_IO toggle;
310 
311  // Update some board data, to ensure a reliable gencad export
313 
314  // Save the auxiliary origin for the rest of the module
315  GencadOffsetX = optionsDialog.GetOption( USE_AUX_ORIGIN ) ? GetAuxOrigin().x : 0;
316  GencadOffsetY = optionsDialog.GetOption( USE_AUX_ORIGIN ) ? GetAuxOrigin().y : 0;
317 
318  // No idea on *why* this should be needed... maybe to fix net names?
319  Compile_Ratsnest( true );
320 
321  /* Temporary modification of footprints that are flipped (i.e. on bottom
322  * layer) to convert them to non flipped footprints.
323  * This is necessary to easily export shapes to GenCAD,
324  * that are given as normal orientation (non flipped, rotation = 0))
325  * these changes will be undone later
326  */
327  BOARD* pcb = GetBoard();
328 
329  for( auto module : pcb->Modules() )
330  {
331  module->SetFlag( 0 );
332 
333  if( module->GetLayer() == B_Cu )
334  {
335  module->Flip( module->GetPosition(), Settings().m_FlipLeftRight );
336  module->SetFlag( 1 );
337  }
338  }
339 
340  /* Gencad has some mandatory and some optional sections: some importer
341  * need the padstack section (which is optional) anyway. Also the
342  * order of the section *is* important */
343 
344  CreateHeaderInfoData( file, this ); // Gencad header
345  CreateBoardSection( file, pcb ); // Board perimeter
346 
347  CreatePadsShapesSection( file, pcb ); // Pads and padstacks
348  CreateArtworksSection( file ); // Empty but mandatory
349 
350  /* Gencad splits a component info in shape, component and device.
351  * We don't do any sharing (it would be difficult since each module is
352  * customizable after placement) */
353  CreateShapesSection( file, pcb );
354  CreateComponentsSection( file, pcb );
355  CreateDevicesSection( file, pcb );
356 
357  // In a similar way the netlist is split in net, track and route
358  CreateSignalsSection( file, pcb );
359  CreateTracksInfoData( file, pcb );
360  CreateRoutesSection( file, pcb );
361 
362  fclose( file );
363 
364  // Undo the footprints modifications (flipped footprints)
365  for( auto module : pcb->Modules() )
366  {
367  if( module->GetFlag() )
368  {
369  module->Flip( module->GetPosition(), Settings().m_FlipLeftRight );
370  module->SetFlag( 0 );
371  }
372  }
373 
374  componentShapes.clear();
375  shapeNames.clear();
376 }
377 
378 
379 // Sort vias for uniqueness
380 static bool ViaSort( const VIA* aPadref, const VIA* aPadcmp )
381 {
382  if( aPadref->GetWidth() != aPadcmp->GetWidth() )
383  return aPadref->GetWidth() < aPadcmp->GetWidth();
384 
385  if( aPadref->GetDrillValue() != aPadcmp->GetDrillValue() )
386  return aPadref->GetDrillValue() < aPadcmp->GetDrillValue();
387 
388  if( aPadref->GetLayerSet() != aPadcmp->GetLayerSet() )
389  return aPadref->GetLayerSet().FmtBin().compare( aPadcmp->GetLayerSet().FmtBin() ) < 0;
390 
391  return false;
392 }
393 
394 
395 // The ARTWORKS section is empty but (officially) mandatory
396 static void CreateArtworksSection( FILE* aFile )
397 {
398  /* The artworks section is empty */
399  fputs( "$ARTWORKS\n", aFile );
400  fputs( "$ENDARTWORKS\n\n", aFile );
401 }
402 
403 
404 // Emit PADS and PADSTACKS. They are sorted and emitted uniquely.
405 // Via name is synthesized from their attributes, pads are numbered
406 static void CreatePadsShapesSection( FILE* aFile, BOARD* aPcb )
407 {
408  std::vector<D_PAD*> padstacks;
409  std::vector<VIA*> vias;
410  std::vector<VIA*> viastacks;
411 
412  padstacks.resize( 1 ); // We count pads from 1
413 
414  // The master layermask (i.e. the enabled layers) for padstack generation
415  LSET master_layermask = aPcb->GetDesignSettings().GetEnabledLayers();
416  int cu_count = aPcb->GetCopperLayerCount();
417 
418  fputs( "$PADS\n", aFile );
419 
420  // Enumerate and sort the pads
421 
422  auto pads( aPcb->GetPads() );
423  std::sort( pads.begin(), pads.end(),
424  []( const D_PAD* a, const D_PAD* b ) { return D_PAD::Compare( a, b ) < 0; } );
425 
426 
427  // The same for vias
428  for( auto track : aPcb->Tracks() )
429  {
430  if( auto via = dyn_cast<VIA*>( track ) )
431  vias.push_back( via );
432  }
433 
434  std::sort( vias.begin(), vias.end(), ViaSort );
435  vias.erase( std::unique( vias.begin(), vias.end(),
436  []( const VIA* a, const VIA* b ) { return ViaSort( a, b ) == false; } ),
437  vias.end() );
438 
439  // Emit vias pads
440 
441  for( auto item : vias )
442  {
443  VIA* via = static_cast<VIA*>( item );
444 
445  viastacks.push_back( via );
446  fprintf( aFile, "PAD V%d.%d.%s ROUND %g\nCIRCLE 0 0 %g\n",
447  via->GetWidth(), via->GetDrillValue(),
448  fmt_mask( via->GetLayerSet() & master_layermask ).c_str(),
449  via->GetDrillValue() / SCALE_FACTOR,
450  via->GetWidth() / (SCALE_FACTOR * 2) );
451  }
452 
453  // Emit component pads
454  D_PAD* old_pad = 0;
455  int pad_name_number = 0;
456 
457  for( unsigned i = 0; i<pads.size(); ++i )
458  {
459  D_PAD* pad = pads[i];
460  const wxPoint& off = pad->GetOffset();
461 
462  pad->SetSubRatsnest( pad_name_number );
463 
464  if( old_pad && 0==D_PAD::Compare( old_pad, pad ) )
465  continue; // already created
466 
467  old_pad = pad;
468 
469  pad_name_number++;
470  pad->SetSubRatsnest( pad_name_number );
471 
472  fprintf( aFile, "PAD P%d", pad->GetSubRatsnest() );
473 
474  padstacks.push_back( pad ); // Will have its own padstack later
475  int dx = pad->GetSize().x / 2;
476  int dy = pad->GetSize().y / 2;
477 
478  switch( pad->GetShape() )
479  {
480  default:
481  wxASSERT_MSG( false, "Pad type not implemented" );
482  // fall-through
483 
484  case PAD_SHAPE_CIRCLE:
485  fprintf( aFile, " ROUND %g\n",
486  pad->GetDrillSize().x / SCALE_FACTOR );
487  /* Circle is center, radius */
488  fprintf( aFile, "CIRCLE %g %g %g\n",
489  off.x / SCALE_FACTOR,
490  -off.y / SCALE_FACTOR,
491  pad->GetSize().x / (SCALE_FACTOR * 2) );
492  break;
493 
494  case PAD_SHAPE_RECT:
495  fprintf( aFile, " RECTANGULAR %g\n",
496  pad->GetDrillSize().x / SCALE_FACTOR );
497 
498  // Rectangle is begin, size *not* begin, end!
499  fprintf( aFile, "RECTANGLE %g %g %g %g\n",
500  (-dx + off.x ) / SCALE_FACTOR,
501  (-dy - off.y ) / SCALE_FACTOR,
502  dx / (SCALE_FACTOR / 2), dy / (SCALE_FACTOR / 2) );
503  break;
504 
505  case PAD_SHAPE_ROUNDRECT:
506  case PAD_SHAPE_OVAL:
507  {
508  const wxSize& size = pad->GetSize();
509  int radius;
510 
511  if( pad->GetShape() == PAD_SHAPE_ROUNDRECT )
512  radius = pad->GetRoundRectCornerRadius();
513  else
514  radius = std::min( size.x, size.y ) / 2;
515 
516  int lineX = size.x / 2 - radius;
517  int lineY = size.y / 2 - radius;
518 
519  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
520 
521  // bottom left 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  // bottom 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  // bottom right 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  // right 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  // top right arc
557  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
558  ( off.x + lineX + radius ) / SCALE_FACTOR,
559  ( -off.y + lineY ) / SCALE_FACTOR, ( off.x + lineX ) / SCALE_FACTOR,
560  ( -off.y + lineY + radius ) / SCALE_FACTOR,
561  ( off.x + lineX ) / SCALE_FACTOR, ( -off.y + lineY ) / SCALE_FACTOR );
562 
563  // top line
564  if( lineX > 0 )
565  {
566  fprintf( aFile, "LINE %g %g %g %g\n"
567  , ( off.x - lineX ) / SCALE_FACTOR,
568  ( -off.y + lineY + radius ) / SCALE_FACTOR,
569  ( off.x + lineX ) / SCALE_FACTOR,
570  ( -off.y + lineY + radius ) / SCALE_FACTOR );
571  }
572 
573  // top left arc
574  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
575  ( off.x - lineX ) / SCALE_FACTOR,
576  ( -off.y + lineY + radius ) / SCALE_FACTOR,
577  ( off.x - lineX - radius ) / SCALE_FACTOR,
578  ( -off.y + lineY ) / SCALE_FACTOR, ( off.x - lineX ) / SCALE_FACTOR,
579  ( -off.y + lineY ) / SCALE_FACTOR );
580 
581  // left line
582  if( lineY > 0 )
583  {
584  fprintf( aFile, "LINE %g %g %g %g\n",
585  ( off.x - lineX - radius ) / SCALE_FACTOR,
586  ( -off.y - lineY ) / SCALE_FACTOR,
587  ( off.x - lineX - radius ) / SCALE_FACTOR,
588  ( -off.y + lineY ) / SCALE_FACTOR );
589  }
590  }
591  break;
592 
593  case PAD_SHAPE_TRAPEZOID:
594  {
595  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
596 
597  wxPoint poly[4];
598  pad->BuildPadPolygon( poly, wxSize( 0, 0 ), 0 );
599 
600  for( int cur = 0; cur < 4; ++cur )
601  {
602  int next = ( cur + 1 ) % 4;
603  fprintf( aFile, "LINE %g %g %g %g\n",
604  ( off.x + poly[cur].x ) / SCALE_FACTOR,
605  ( -off.y - poly[cur].y ) / SCALE_FACTOR,
606  ( off.x + poly[next].x ) / SCALE_FACTOR,
607  ( -off.y - poly[next].y ) / SCALE_FACTOR );
608  }
609  }
610  break;
611 
612  case PAD_SHAPE_CUSTOM:
613  {
614  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
615 
616  const SHAPE_POLY_SET& outline = pad->GetCustomShapeAsPolygon();
617 
618  for( int jj = 0; jj < outline.OutlineCount(); ++jj )
619  {
620  const SHAPE_LINE_CHAIN& poly = outline.COutline( jj );
621  int pointCount = poly.PointCount();
622 
623  for( int ii = 0; ii < pointCount; ii++ )
624  {
625  int next = ( ii + 1 ) % pointCount;
626  fprintf( aFile, "LINE %g %g %g %g\n",
627  ( off.x + poly.CPoint( ii ).x ) / SCALE_FACTOR,
628  ( -off.y - poly.CPoint( ii ).y ) / SCALE_FACTOR,
629  ( off.x + poly.CPoint( next ).x ) / SCALE_FACTOR,
630  ( -off.y - poly.CPoint( next ).y ) / SCALE_FACTOR );
631  }
632  }
633  }
634  break;
635  }
636  }
637 
638  fputs( "\n$ENDPADS\n\n", aFile );
639 
640  // Now emit the padstacks definitions, using the combined layer masks
641  fputs( "$PADSTACKS\n", aFile );
642 
643  // Via padstacks
644  for( unsigned i = 0; i < viastacks.size(); i++ )
645  {
646  VIA* via = viastacks[i];
647 
648  LSET mask = via->GetLayerSet() & master_layermask;
649 
650  fprintf( aFile, "PADSTACK VIA%d.%d.%s %g\n",
651  via->GetWidth(), via->GetDrillValue(),
652  fmt_mask( mask ).c_str(),
653  via->GetDrillValue() / SCALE_FACTOR );
654 
655  for( LSEQ seq = mask.Seq( gc_seq, arrayDim( gc_seq ) ); seq; ++seq )
656  {
657  PCB_LAYER_ID layer = *seq;
658 
659  fprintf( aFile, "PAD V%d.%d.%s %s 0 0\n",
660  via->GetWidth(), via->GetDrillValue(),
661  fmt_mask( mask ).c_str(),
662  GenCADLayerName( cu_count, layer ).c_str()
663  );
664  }
665  }
666 
667  /* Component padstacks
668  * Older versions of CAM350 don't apply correctly the FLIP semantics for
669  * padstacks, i.e. doesn't swap the top and bottom layers... so I need to
670  * define the shape as MIRRORX and define a separate 'flipped' padstack...
671  * until it appears yet another noncompliant importer */
672  for( unsigned i = 1; i < padstacks.size(); i++ )
673  {
674  D_PAD* pad = padstacks[i];
675 
676  // Straight padstack
677  fprintf( aFile, "PADSTACK PAD%u %g\n", i, pad->GetDrillSize().x / SCALE_FACTOR );
678 
679  LSET pad_set = pad->GetLayerSet() & master_layermask;
680 
681  // the special gc_seq
682  for( LSEQ seq = pad_set.Seq( gc_seq, arrayDim( gc_seq ) ); seq; ++seq )
683  {
684  PCB_LAYER_ID layer = *seq;
685 
686  fprintf( aFile, "PAD P%u %s 0 0\n", i, GenCADLayerName( cu_count, layer ).c_str() );
687  }
688 
689  // Flipped padstack
690  if( flipBottomPads )
691  {
692  fprintf( aFile, "PADSTACK PAD%uF %g\n", i, pad->GetDrillSize().x / SCALE_FACTOR );
693 
694  // the normal PCB_LAYER_ID sequence is inverted from gc_seq[]
695  for( LSEQ seq = pad_set.Seq(); seq; ++seq )
696  {
697  PCB_LAYER_ID layer = *seq;
698 
699  fprintf( aFile, "PAD P%u %s 0 0\n", i, GenCADLayerNameFlipped( cu_count, layer ).c_str() );
700  }
701  }
702  }
703 
704  fputs( "$ENDPADSTACKS\n\n", aFile );
705 }
706 
707 
709 static size_t hashModule( const MODULE* aModule )
710 {
711  size_t ret = 0x11223344;
712  constexpr int flags = HASH_FLAGS::POSITION | HASH_FLAGS::REL_COORD
714 
715 
716  for( auto i : aModule->GraphicalItems() )
717  ret += hash_eda( i, flags );
718 
719  for( auto i : aModule->Pads() )
720  ret += hash_eda( i, flags );
721 
722  return ret;
723 }
724 
725 
726 /* Creates the footprint shape list.
727  * Since module shape is customizable after the placement we cannot share them;
728  * instead we opt for the one-module-one-shape-one-component-one-device approach
729  */
730 static void CreateShapesSection( FILE* aFile, BOARD* aPcb )
731 {
732  const char* layer;
733  wxString pinname;
734  const char* mirror = "0";
735  std::map<wxString, size_t> shapes;
736 
737  fputs( "$SHAPES\n", aFile );
738 
739  for( auto module : aPcb->Modules() )
740  {
741  if( !individualShapes )
742  {
743  // Check if such shape has been already generated, and if so - reuse it
744  // It is necessary to compute hash (i.e. check all children objects) as
745  // certain components instances might have been modified on the board.
746  // In such case the shape will be different despite the same LIB_ID.
747  wxString shapeName = module->GetFPID().Format();
748 
749  auto shapeIt = shapes.find( shapeName );
750  size_t modHash = hashModule( module );
751 
752  if( shapeIt != shapes.end() )
753  {
754  if( modHash != shapeIt->second )
755  {
756  // there is an entry for this footprint, but it has a modified shape,
757  // so we need to create a new entry
758  wxString newShapeName;
759  int suffix = 0;
760 
761  // find an unused name or matching entry
762  do
763  {
764  newShapeName = wxString::Format( "%s_%d", shapeName, suffix );
765  shapeIt = shapes.find( newShapeName );
766  ++suffix;
767  }
768  while( shapeIt != shapes.end() && shapeIt->second != modHash );
769 
770  shapeName = newShapeName;
771  }
772 
773  if( shapeIt != shapes.end() && modHash == shapeIt->second )
774  {
775  // shape found, so reuse it
776  componentShapes[module] = modHash;
777  continue;
778  }
779  }
780 
781  // new shape
782  componentShapes[module] = modHash;
783  shapeNames[modHash] = shapeName;
784  shapes[shapeName] = modHash;
785  FootprintWriteShape( aFile, module, shapeName );
786  }
787  else // individual shape for each component
788  {
789  FootprintWriteShape( aFile, module, module->GetReference() );
790  }
791 
792  // set of already emitted pins to check for duplicates
793  std::set<wxString> pins;
794 
795  for( auto pad : module->Pads() )
796  {
797  /* Padstacks are defined using the correct layers for the pads, therefore to
798  * all pads need to be marked as TOP to use the padstack information correctly.
799  */
800  layer = "TOP";
801  pinname = pad->GetName();
802 
803  if( pinname.IsEmpty() )
804  pinname = wxT( "none" );
805 
806  if( uniquePins )
807  {
808  int suffix = 0;
809  wxString origPinname( pinname );
810 
811  auto it = pins.find( pinname );
812 
813  while( it != pins.end() )
814  {
815  pinname = wxString::Format( "%s_%d", origPinname, suffix );
816  ++suffix;
817  it = pins.find( pinname );
818  }
819 
820  pins.insert( pinname );
821  }
822 
823  double orient = pad->GetOrientation() - module->GetOrientation();
824  NORMALIZE_ANGLE_POS( orient );
825 
826  // Bottom side modules use the flipped padstack
827  fprintf( aFile, ( flipBottomPads && module->GetFlag() ) ?
828  "PIN \"%s\" PAD%dF %g %g %s %g %s\n" :
829  "PIN \"%s\" PAD%d %g %g %s %g %s\n",
830  TO_UTF8( escapeString( pinname ) ), pad->GetSubRatsnest(),
831  pad->GetPos0().x / SCALE_FACTOR,
832  -pad->GetPos0().y / SCALE_FACTOR,
833  layer, orient / 10.0, mirror );
834  }
835  }
836 
837  fputs( "$ENDSHAPES\n\n", aFile );
838 }
839 
840 
841 /* Creates the section $COMPONENTS (Footprints placement)
842  * Bottom side components are difficult to handle: shapes must be mirrored or
843  * flipped, silk layers need to be handled correctly and so on. Also it seems
844  * that *noone* follows the specs...
845  */
846 static void CreateComponentsSection( FILE* aFile, BOARD* aPcb )
847 {
848  fputs( "$COMPONENTS\n", aFile );
849 
850  int cu_count = aPcb->GetCopperLayerCount();
851 
852  for( auto module : aPcb->Modules() )
853  {
854  const char* mirror;
855  const char* flip;
856  double fp_orient = module->GetOrientation();
857 
858  if( module->GetFlag() )
859  {
860  mirror = "MIRRORX";
861  flip = "FLIP";
862  NEGATE_AND_NORMALIZE_ANGLE_POS( fp_orient );
863  }
864  else
865  {
866  mirror = "0";
867  flip = "0";
868  }
869 
870  fprintf( aFile, "\nCOMPONENT \"%s\"\n",
871  TO_UTF8( escapeString( module->GetReference() ) ) );
872  fprintf( aFile, "DEVICE \"DEV_%s\"\n",
873  TO_UTF8( escapeString( getShapeName( module ) ) ) );
874  fprintf( aFile, "PLACE %g %g\n",
875  MapXTo( module->GetPosition().x ),
876  MapYTo( module->GetPosition().y ) );
877  fprintf( aFile, "LAYER %s\n",
878  module->GetFlag() ? "BOTTOM" : "TOP" );
879  fprintf( aFile, "ROTATION %g\n",
880  fp_orient / 10.0 );
881  fprintf( aFile, "SHAPE \"%s\" %s %s\n",
882  TO_UTF8( escapeString( getShapeName( module ) ) ),
883  mirror, flip );
884 
885  // Text on silk layer: RefDes and value (are they actually useful?)
886  TEXTE_MODULE *textmod = &module->Reference();
887 
888  for( int ii = 0; ii < 2; ii++ )
889  {
890  double txt_orient = textmod->GetTextAngle();
891  std::string layer = GenCADLayerName( cu_count, module->GetFlag() ? B_SilkS : F_SilkS );
892 
893  fprintf( aFile, "TEXT %g %g %g %g %s %s \"%s\"",
894  textmod->GetPos0().x / SCALE_FACTOR,
895  -textmod->GetPos0().y / SCALE_FACTOR,
896  textmod->GetTextWidth() / SCALE_FACTOR,
897  txt_orient / 10.0,
898  mirror,
899  layer.c_str(),
900  TO_UTF8( escapeString( textmod->GetText() ) ) );
901 
902  // Please note, the width is approx
903  fprintf( aFile, " 0 0 %g %g\n",
904  ( textmod->GetTextWidth() * textmod->GetLength() ) / SCALE_FACTOR,
905  textmod->GetTextHeight() / SCALE_FACTOR );
906 
907  textmod = &module->Value(); // Dirty trick for the second iteration
908  }
909 
910  // The SHEET is a 'generic description' for referencing the component
911  fprintf( aFile, "SHEET \"RefDes: %s, Value: %s\"\n",
912  TO_UTF8( module->GetReference() ),
913  TO_UTF8( module->GetValue() ) );
914  }
915 
916  fputs( "$ENDCOMPONENTS\n\n", aFile );
917 }
918 
919 
920 /* Emit the netlist (which is actually the thing for which GenCAD is used these
921  * days!); tracks are handled later */
922 static void CreateSignalsSection( FILE* aFile, BOARD* aPcb )
923 {
924  wxString msg;
925  NETINFO_ITEM* net;
926  int NbNoConn = 1;
927 
928  fputs( "$SIGNALS\n", aFile );
929 
930  for( unsigned ii = 0; ii < aPcb->GetNetCount(); ii++ )
931  {
932  net = aPcb->FindNet( ii );
933 
934  if( net->GetNetname() == wxEmptyString ) // dummy netlist (no connection)
935  {
936  msg.Printf( "NoConnection%d", NbNoConn++ );
937  }
938 
939  if( net->GetNet() <= 0 ) // dummy netlist (no connection)
940  continue;
941 
942  msg = wxT( "SIGNAL \"" ) + escapeString( net->GetNetname() ) + "\"";
943 
944  fputs( TO_UTF8( msg ), aFile );
945  fputs( "\n", aFile );
946 
947  for( auto module : aPcb->Modules() )
948  {
949  for( auto pad : module->Pads() )
950  {
951  if( pad->GetNetCode() != net->GetNet() )
952  continue;
953 
954  msg.Printf( wxT( "NODE \"%s\" \"%s\"" ),
955  GetChars( escapeString( module->GetReference() ) ),
956  GetChars( escapeString( pad->GetName() ) ) );
957 
958  fputs( TO_UTF8( msg ), aFile );
959  fputs( "\n", aFile );
960  }
961  }
962  }
963 
964  fputs( "$ENDSIGNALS\n\n", aFile );
965 }
966 
967 
968 // Creates the header section
969 static bool CreateHeaderInfoData( FILE* aFile, PCB_EDIT_FRAME* aFrame )
970 {
971  wxString msg;
972  BOARD *board = aFrame->GetBoard();
973 
974  fputs( "$HEADER\n", aFile );
975  fputs( "GENCAD 1.4\n", aFile );
976 
977  // Please note: GenCAD syntax requires quoted strings if they can contain spaces
978  msg.Printf( wxT( "USER \"%s %s\"\n" ),
979  GetChars( Pgm().App().GetAppName() ),
980  GetChars( GetBuildVersion() ) );
981  fputs( TO_UTF8( msg ), aFile );
982 
983  msg = wxT( "DRAWING \"" ) + board->GetFileName() + wxT( "\"\n" );
984  fputs( TO_UTF8( msg ), aFile );
985 
986  const TITLE_BLOCK& tb = aFrame->GetTitleBlock();
987 
988  msg = wxT( "REVISION \"" ) + tb.GetRevision() + wxT( " " ) + tb.GetDate() + wxT( "\"\n" );
989 
990  fputs( TO_UTF8( msg ), aFile );
991  fputs( "UNITS INCH\n", aFile );
992 
993  // giving 0 as the argument to Map{X,Y}To returns the scaled origin point
994  msg.Printf( wxT( "ORIGIN %g %g\n" ),
995  storeOriginCoords ? MapXTo( 0 ) : 0,
996  storeOriginCoords ? MapYTo( 0 ) : 0 );
997  fputs( TO_UTF8( msg ), aFile );
998 
999  fputs( "INTERTRACK 0\n", aFile );
1000  fputs( "$ENDHEADER\n\n", aFile );
1001 
1002  return true;
1003 }
1004 
1005 
1006 /* Creates the section ROUTES
1007  * that handles tracks, vias
1008  * TODO: add zones
1009  * section:
1010  * $ROUTE
1011  * ...
1012  * $ENROUTE
1013  * Track segments must be sorted by nets
1014  */
1015 static void CreateRoutesSection( FILE* aFile, BOARD* aPcb )
1016 {
1017  int vianum = 1;
1018  int old_netcode, old_width, old_layer;
1019  LSET master_layermask = aPcb->GetDesignSettings().GetEnabledLayers();
1020 
1021  int cu_count = aPcb->GetCopperLayerCount();
1022 
1023  TRACKS tracks( aPcb->Tracks() );
1024  std::sort( tracks.begin(), tracks.end(), []( const TRACK* a, const TRACK* b ) {
1025  if( a->GetNetCode() == b->GetNetCode() )
1026  {
1027  if( a->GetWidth() == b->GetWidth() )
1028  return ( a->GetLayer() < b->GetLayer() );
1029 
1030  return ( a->GetWidth() < b->GetWidth() );
1031  }
1032 
1033  return ( a->GetNetCode() < b->GetNetCode() );
1034  } );
1035 
1036  fputs( "$ROUTES\n", aFile );
1037 
1038  old_netcode = -1; old_width = -1; old_layer = -1;
1039 
1040  for( auto track : tracks )
1041  {
1042  if( old_netcode != track->GetNetCode() )
1043  {
1044  old_netcode = track->GetNetCode();
1045  NETINFO_ITEM* net = track->GetNet();
1046  wxString netname;
1047 
1048  if( net && (net->GetNetname() != wxEmptyString) )
1049  netname = net->GetNetname();
1050  else
1051  netname = wxT( "_noname_" );
1052 
1053  fprintf( aFile, "ROUTE \"%s\"\n", TO_UTF8( escapeString( netname ) ) );
1054  }
1055 
1056  if( old_width != track->GetWidth() )
1057  {
1058  old_width = track->GetWidth();
1059  fprintf( aFile, "TRACK TRACK%d\n", track->GetWidth() );
1060  }
1061 
1062  if( track->Type() == PCB_TRACE_T )
1063  {
1064  if( old_layer != track->GetLayer() )
1065  {
1066  old_layer = track->GetLayer();
1067  fprintf( aFile, "LAYER %s\n",
1068  GenCADLayerName( cu_count, track->GetLayer() ).c_str() );
1069  }
1070 
1071  fprintf( aFile, "LINE %g %g %g %g\n",
1072  MapXTo( track->GetStart().x ), MapYTo( track->GetStart().y ),
1073  MapXTo( track->GetEnd().x ), MapYTo( track->GetEnd().y ) );
1074  }
1075 
1076  if( track->Type() == PCB_VIA_T )
1077  {
1078  const VIA* via = static_cast<const VIA*>(track);
1079 
1080  LSET vset = via->GetLayerSet() & master_layermask;
1081 
1082  fprintf( aFile, "VIA VIA%d.%d.%s %g %g ALL %g via%d\n",
1083  via->GetWidth(), via->GetDrillValue(),
1084  fmt_mask( vset ).c_str(),
1085  MapXTo( via->GetStart().x ), MapYTo( via->GetStart().y ),
1086  via->GetDrillValue() / SCALE_FACTOR, vianum++ );
1087  }
1088  }
1089 
1090  fputs( "$ENDROUTES\n\n", aFile );
1091 }
1092 
1093 
1094 /* Creates the section $DEVICES
1095  * This is a list of footprints properties
1096  * ( Shapes are in section $SHAPE )
1097  */
1098 static void CreateDevicesSection( FILE* aFile, BOARD* aPcb )
1099 {
1100  std::set<wxString> emitted;
1101  fputs( "$DEVICES\n", aFile );
1102 
1103  for( const auto& componentShape : componentShapes )
1104  {
1105  const wxString& shapeName = shapeNames[componentShape.second];
1106  bool newDevice;
1107  std::tie( std::ignore, newDevice ) = emitted.insert( shapeName );
1108 
1109  if( !newDevice ) // do not repeat device definitions
1110  continue;
1111 
1112  const MODULE* module = componentShape.first;
1113  fprintf( aFile, "\nDEVICE \"DEV_%s\"\n", TO_UTF8( escapeString( shapeName ) ) );
1114  fprintf( aFile, "PART \"%s\"\n", TO_UTF8( escapeString( module->GetValue() ) ) );
1115  fprintf( aFile, "PACKAGE \"%s\"\n", TO_UTF8( escapeString( module->GetFPID().Format() ) ) );
1116 
1117  // The TYPE attribute is almost freeform
1118  const char* ty = "TH";
1119 
1120  if( module->GetAttributes() & MOD_CMS )
1121  ty = "SMD";
1122 
1123  if( module->GetAttributes() & MOD_VIRTUAL )
1124  ty = "VIRTUAL";
1125 
1126  fprintf( aFile, "TYPE %s\n", ty );
1127  }
1128 
1129  fputs( "$ENDDEVICES\n\n", aFile );
1130 }
1131 
1132 
1133 /* Creates the section $BOARD.
1134  * We output here only the board perimeter
1135  */
1136 static void CreateBoardSection( FILE* aFile, BOARD* aPcb )
1137 {
1138  fputs( "$BOARD\n", aFile );
1139 
1140  // Extract the board edges
1141  for( auto drawing : aPcb->Drawings() )
1142  {
1143  if( drawing->Type() == PCB_LINE_T )
1144  {
1145  DRAWSEGMENT* drawseg = static_cast<DRAWSEGMENT*>( drawing );
1146  if( drawseg->GetLayer() == Edge_Cuts )
1147  {
1148  // XXX GenCAD supports arc boundaries but I've seen nothing that reads them
1149  fprintf( aFile, "LINE %g %g %g %g\n",
1150  MapXTo( drawseg->GetStart().x ), MapYTo( drawseg->GetStart().y ),
1151  MapXTo( drawseg->GetEnd().x ), MapYTo( drawseg->GetEnd().y ) );
1152  }
1153  }
1154  }
1155 
1156  fputs( "$ENDBOARD\n\n", aFile );
1157 }
1158 
1159 
1160 /* Creates the section "$TRACKS"
1161  * This sections give the list of widths (tools) used in tracks and vias
1162  * format:
1163  * $TRACK
1164  * TRACK <name> <width>
1165  * $ENDTRACK
1166  *
1167  * Each tool name is build like this: "TRACK" + track width.
1168  * For instance for a width = 120 : name = "TRACK120".
1169  */
1170 static void CreateTracksInfoData( FILE* aFile, BOARD* aPcb )
1171 {
1172  // Find thickness used for traces
1173 
1174  std::set<int> trackinfo;
1175 
1176  for( auto track : aPcb->Tracks() )
1177  trackinfo.insert( track->GetWidth() );
1178 
1179  // Write data
1180  fputs( "$TRACKS\n", aFile );
1181 
1182  for( auto size : trackinfo )
1183  fprintf( aFile, "TRACK TRACK%d %g\n", size, size / SCALE_FACTOR );
1184 
1185  fputs( "$ENDTRACKS\n\n", aFile );
1186 }
1187 
1188 
1189 /* Creates the shape of a footprint (section SHAPE)
1190  * The shape is always given "normal" (Orient 0, not mirrored)
1191  * It's almost guaranteed that the silk layer will be imported wrong but
1192  * the shape also contains the pads!
1193  */
1194 static void FootprintWriteShape( FILE* aFile, MODULE* module, const wxString& aShapeName )
1195 {
1196  EDGE_MODULE* PtEdge;
1197 
1198  /* creates header: */
1199  fprintf( aFile, "\nSHAPE \"%s\"\n", TO_UTF8( escapeString( aShapeName ) ) );
1200 
1201  if( module->GetAttributes() & MOD_VIRTUAL )
1202  {
1203  fprintf( aFile, "INSERT SMD\n" );
1204  }
1205  else
1206  {
1207  if( module->GetAttributes() & MOD_CMS )
1208  {
1209  fprintf( aFile, "INSERT SMD\n" );
1210  }
1211  else
1212  {
1213  fprintf( aFile, "INSERT TH\n" );
1214  }
1215  }
1216 
1217 #if 0 /* ATTRIBUTE name and value is unspecified and the original exporter
1218  * got the syntax wrong, so CAM350 rejected the whole shape! */
1219 
1220  if( module->m_Attributs != MOD_DEFAULT )
1221  {
1222  fprintf( aFile, "ATTRIBUTE" );
1223 
1224  if( module->m_Attributs & MOD_CMS )
1225  fprintf( aFile, " PAD_SMD" );
1226 
1227  if( module->m_Attributs & MOD_VIRTUAL )
1228  fprintf( aFile, " VIRTUAL" );
1229 
1230  fprintf( aFile, "\n" );
1231  }
1232 #endif
1233 
1234  // Silk outline; wildly interpreted by various importers:
1235  // CAM350 read it right but only closed shapes
1236  // ProntoPlace double-flip it (at least the pads are correct)
1237  // GerberTool usually get it right...
1238  for( auto PtStruct : module->GraphicalItems() )
1239  {
1240  switch( PtStruct->Type() )
1241  {
1242  case PCB_MODULE_TEXT_T:
1243 
1244  // If we wanted to export text, this is not the correct section
1245  break;
1246 
1247  case PCB_MODULE_EDGE_T:
1248  PtEdge = (EDGE_MODULE*) PtStruct;
1249  if( PtEdge->GetLayer() == F_SilkS
1250  || PtEdge->GetLayer() == B_SilkS )
1251  {
1252  switch( PtEdge->GetShape() )
1253  {
1254  case S_SEGMENT:
1255  fprintf( aFile, "LINE %g %g %g %g\n",
1256  PtEdge->m_Start0.x / SCALE_FACTOR,
1257  -PtEdge->m_Start0.y / SCALE_FACTOR,
1258  PtEdge->m_End0.x / SCALE_FACTOR,
1259  -PtEdge->m_End0.y / SCALE_FACTOR );
1260  break;
1261 
1262  case S_CIRCLE:
1263  {
1264  int radius = KiROUND( GetLineLength( PtEdge->m_End0,
1265  PtEdge->m_Start0 ) );
1266  fprintf( aFile, "CIRCLE %g %g %g\n",
1267  PtEdge->m_Start0.x / SCALE_FACTOR,
1268  -PtEdge->m_Start0.y / SCALE_FACTOR,
1269  radius / SCALE_FACTOR );
1270  break;
1271  }
1272 
1273  case S_ARC:
1274  {
1275  int arcendx, arcendy;
1276  arcendx = PtEdge->m_End0.x - PtEdge->m_Start0.x;
1277  arcendy = PtEdge->m_End0.y - PtEdge->m_Start0.y;
1278  RotatePoint( &arcendx, &arcendy, -PtEdge->GetAngle() );
1279  arcendx += PtEdge->GetStart0().x;
1280  arcendy += PtEdge->GetStart0().y;
1281 
1282  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
1283  PtEdge->m_End0.x / SCALE_FACTOR,
1284  -PtEdge->GetEnd0().y / SCALE_FACTOR,
1285  arcendx / SCALE_FACTOR,
1286  -arcendy / SCALE_FACTOR,
1287  PtEdge->GetStart0().x / SCALE_FACTOR,
1288  -PtEdge->GetStart0().y / SCALE_FACTOR );
1289  break;
1290  }
1291 
1292  case S_POLYGON:
1293  // Not exported (TODO)
1294  break;
1295 
1296  default:
1297  DisplayError( NULL, wxString::Format( "Type Edge Module %d invalid.", PtStruct->Type() ) );
1298  break;
1299  }
1300  }
1301  break;
1302 
1303  default:
1304  break;
1305  }
1306  }
1307 }
bool GetOption(GENCAD_EXPORT_OPT aOption) const
Checks whether an option has been selected
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Function AllCuMask returns a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:686
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:239
CITER next(CITER it)
Definition: ptree.cpp:130
NETINFO_ITEM * FindNet(int aNetcode) const
Function FindNet searches for a net with the given netcode.
int GetAttributes() const
Definition: class_module.h:259
static const wxString getShapeName(MODULE *aModule)
int GetNetCode() const
Function GetNetCode.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:103
use coordinates relative to the parent object
Definition: hash_eda.h:40
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:206
int StrPrintf(std::string *aResult, const char *aFormat,...)
Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a char...
Definition: richio.cpp:74
virtual LSET GetLayerSet() const override
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
This file is part of the common library TODO brief description.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:202
void Compile_Ratsnest(bool aDisplayStatus)
Function Compile_Ratsnest Create the entire board ratsnest.
This file is part of the common library.
Definition: hash_eda.h:42
const wxPoint & GetStart() const
Definition: class_track.h:111
STROKE_T GetShape() const
static void CreateRoutesSection(FILE *aFile, BOARD *aPcb)
static void CreateSignalsSection(FILE *aFile, BOARD *aPcb)
polygon (not yet used for tracks, but could be in microwave apps)
static double MapYTo(int aY)
PADS & Pads()
Definition: class_module.h:173
const wxString GetValue() const
Function GetValue.
Definition: class_module.h:461
Set for modules listed in the automatic insertion list (usually SMD footprints)
Definition: class_module.h:74
double GetTextAngle() const
Definition: eda_text.h:158
static bool uniquePins
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings.
Definition: class_board.h:512
void ExportToGenCAD(wxCommandEvent &event)
Function ExportToGenCAD creates a file in GenCAD 1.4 format from the current board.
usual segment : line with rounded ends
const LIB_ID & GetFPID() const
Definition: class_module.h:219
void SetSubRatsnest(int aSubRatsnest)
Definition: class_pad.h:742
static wxString escapeString(const wxString &aString)
static int GencadOffsetY
DRAWINGS & GraphicalItems()
Definition: class_module.h:183
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:257
static bool flipBottomPads
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
int PointCount() const
Function PointCount()
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Function Seq returns an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:377
const wxString & GetFileName() const
Definition: class_board.h:218
TITLE_BLOCK holds the information shown in the lower right corner of a plot, printout,...
Definition: title_block.h:40
static void CreateTracksInfoData(FILE *aFile, BOARD *aPcb)
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
wxString GetLastPath(LAST_PATH_TYPE aType)
Get the last path for a particular type.
const wxPoint & GetAuxOrigin() const override
Return the origin of the axis used for plotting and various exports.
static void CreateArtworksSection(FILE *aFile)
Definitions for tracks, vias and zones.
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
wxString GetFileName() const
Returns the selected file path
wxPoint m_End0
End point, relative to module origin, orient 0.
void vset(double *v, double x, double y, double z)
Definition: trackball.cpp:82
This file contains miscellaneous commonly used macros and functions.
const wxString GetReference() const
Function GetReference.
Definition: class_module.h:436
int GetTextHeight() const
Definition: eda_text.h:229
const wxPoint & GetStart0() const
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
const VECTOR2I & CPoint(int aIndex) const
Function Point()
static std::string GenCADLayerNameFlipped(int aCuCount, PCB_LAYER_ID aId)
static void CreateComponentsSection(FILE *aFile, BOARD *aPcb)
PCB_LAYER_ID
A quick note on layer IDs:
static bool individualShapes
LSET is a set of PCB_LAYER_IDs.
#define NULL
unsigned GetNetCount() const
Function GetNetCount.
Definition: class_board.h:742
int GetDrillValue() const
Function GetDrillValue "calculates" the drill value for vias (m-Drill if > 0, or default drill value ...
MODULES & Modules()
Definition: class_board.h:229
wxString GetBuildVersion()
Get the full KiCad version string.
const wxString & GetRevision() const
Definition: title_block.h:89
static void CreatePadsShapesSection(FILE *aFile, BOARD *aPcb)
SHAPE_POLY_SET.
Arcs (with rounded ends)
static int Compare(const D_PAD *padref, const D_PAD *padcmp)
Function Compare compares two pads and return 0 if they are equal.
Definition: class_pad.cpp:1172
const wxPoint & GetOffset() const
Definition: class_pad.h:309
static const PCB_LAYER_ID gc_seq[]
LSET GetLayerSet() const override
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
Definition: class_pad.h:446
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:232
static bool storeOriginCoords
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
UTF8 Format() const
Definition: lib_id.cpp:237
default
Definition: class_module.h:73
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:108
const wxPoint & GetPos0() const
void BuildPadPolygon(wxPoint aCoord[4], wxSize aInflateValue, double aRotation) const
Function BuildPadPolygon Has meaning only for polygonal pads (trapezoid and rectangular) Build the Co...
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
static std::string GenCADLayerName(int aCuCount, PCB_LAYER_ID aId)
void NEGATE_AND_NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:322
const wxString & GetDate() const
Definition: title_block.h:79
const TITLE_BLOCK & GetTitleBlock() const override
double GetAngle() const
const std::vector< D_PAD * > GetPads()
Function GetPads returns a reference to a list of all the pads.
NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:65
int GetLength() const
int GetTextWidth() const
Definition: eda_text.h:226
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:101
see class PGM_BASE
int GetWidth() const
Definition: class_track.h:105
static bool ViaSort(const VIA *aPadref, const VIA *aPadcmp)
std::string FmtBin() const
Function FmtBin returns a binary string showing contents of this LSEQ.
Definition: lset.cpp:264
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
static size_t hashModule(const MODULE *aModule)
Compute hashes for modules without taking into account their position, rotation or layer.
int GetNet() const
Function GetNet.
Definition: netinfo.h:224
static bool CreateHeaderInfoData(FILE *aFile, PCB_EDIT_FRAME *frame)
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:163
static void CreateDevicesSection(FILE *aFile, BOARD *aPcb)
LSET GetEnabledLayers() const
Function GetEnabledLayers returns a bit-mask of all the layers that are enabled.
static void FootprintWriteShape(FILE *File, MODULE *module, const wxString &aShapeName)
#define _(s)
Definition: 3d_actions.cpp:33
SHAPE_LINE_CHAIN.
Virtual component: when created by copper shapes on board (Like edge card connectors,...
Definition: class_module.h:76
int GetCopperLayerCount() const
Function GetCopperLayerCount.
std::string FmtHex() const
Function FmtHex returns a hex string showing contents of this LSEQ.
Definition: lset.cpp:288
const wxSize & GetDrillSize() const
Definition: class_pad.h:306
const SHAPE_POLY_SET & GetCustomShapeAsPolygon() const
Accessor to the custom shape as one polygon.
Definition: class_pad.h:378
PCB_EDIT_FRAME is the main frame for Pcbnew.
PCBNEW_SETTINGS & Settings()
#define IU_PER_MILS
Definition: plotter.cpp:137
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:61
int GetSubRatsnest() const
Function GetSubRatsnest.
Definition: class_pad.h:741
static const double SCALE_FACTOR
int GetRoundRectCornerRadius() const
Function GetRoundRectCornerRadius Has meaning only for rounded rect pads.
Definition: class_pad.h:577
EDA_RECT ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Function ComputeBoundingBox calculates the bounding box containing all board items (or board edge seg...
bool IsCopperLayer(LAYER_NUM aLayerId)
Function IsCopperLayer tests whether a layer is a copper layer.
static int GencadOffsetX
const wxPoint & GetEnd0() const
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
static std::string fmt_mask(LSET aSet)
PAD_SHAPE_T GetShape() const
Function GetShape.
Definition: class_pad.h:237
Module description (excepted pads)
BOARD * GetBoard() const
wxPoint m_Start0
Start point or center, relative to module origin, orient 0.
static std::map< MODULE *, int > componentShapes
const wxSize & GetSize() const
Definition: class_pad.h:300
static std::map< int, wxString > shapeNames
EDGE_MODULE class definition.
class DRAWSEGMENT, a segment not on copper layers
Definition: typeinfo.h:91
size_t hash_eda(const EDA_ITEM *aItem, int aFlags)
Calculates hash of an EDA_ITEM.
Definition: hash_eda.cpp:48
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
DRAWINGS & Drawings()
Definition: class_board.h:238
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:123
TRACKS & Tracks()
Definition: class_board.h:220
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.
int m_Attributs
Definition: class_module.h:694
static double MapXTo(int aX)