You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

598 lines
20 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2007-2014 Jean-Pierre Charras jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <vector>
  25. #include <export_to_pcbnew.h>
  26. #include <confirm.h>
  27. #include <string_utils.h>
  28. #include <locale_io.h>
  29. #include <macros.h>
  30. #include <trigo.h>
  31. #include <gerbview_frame.h>
  32. #include <gerber_file_image.h>
  33. #include <gerber_file_image_list.h>
  34. #include <build_version.h>
  35. #include <wildcards_and_files_ext.h>
  36. #include "excellon_image.h"
  37. // Imported function
  38. extern const wxString GetPCBDefaultLayerName( int aLayerNumber );
  39. GBR_TO_PCB_EXPORTER::GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName )
  40. {
  41. m_gerbview_frame = aFrame;
  42. m_pcb_file_name = aFileName;
  43. m_fp = nullptr;
  44. m_pcbCopperLayersCount = 2;
  45. }
  46. GBR_TO_PCB_EXPORTER::~GBR_TO_PCB_EXPORTER()
  47. {
  48. }
  49. bool GBR_TO_PCB_EXPORTER::ExportPcb( const int* aLayerLookUpTable, int aCopperLayers )
  50. {
  51. LOCALE_IO toggle; // toggles on, then off, the C locale.
  52. m_fp = wxFopen( m_pcb_file_name, wxT( "wt" ) );
  53. if( m_fp == nullptr )
  54. {
  55. wxString msg;
  56. msg.Printf( _( "Failed to create file '%s'." ), m_pcb_file_name );
  57. DisplayError( m_gerbview_frame, msg );
  58. return false;
  59. }
  60. m_pcbCopperLayersCount = aCopperLayers;
  61. writePcbHeader( aLayerLookUpTable );
  62. // create an image of gerber data
  63. const int pcbCopperLayerMax = 31;
  64. GERBER_FILE_IMAGE_LIST* images = m_gerbview_frame->GetGerberLayout()->GetImagesList();
  65. // First collect all the holes. We'll use these to generate pads, vias, etc.
  66. for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
  67. {
  68. int pcb_layer_number = aLayerLookUpTable[layer];
  69. EXCELLON_IMAGE* excellon = dynamic_cast<EXCELLON_IMAGE*>( images->GetGbrImage( layer ) );
  70. GERBER_FILE_IMAGE* gerb = dynamic_cast<GERBER_FILE_IMAGE*>( images->GetGbrImage( layer ) );
  71. if( excellon )
  72. {
  73. for( GERBER_DRAW_ITEM* gerb_item : excellon->GetItems() )
  74. collect_hole( gerb_item );
  75. }
  76. else if( gerb && pcb_layer_number == UNDEFINED_LAYER ) // PCB_LAYER_ID doesn't have an entry for Hole Data,
  77. // but the dialog returns UNDEFINED_LAYER for it
  78. {
  79. for( GERBER_DRAW_ITEM* gerb_item : gerb->GetItems() )
  80. collect_hole( gerb_item );
  81. }
  82. else
  83. {
  84. continue;
  85. }
  86. }
  87. // Next: non copper layers:
  88. for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
  89. {
  90. GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
  91. if( gerber == nullptr ) // Graphic layer not yet used
  92. continue;
  93. int pcb_layer_number = aLayerLookUpTable[layer];
  94. if( !IsPcbLayer( pcb_layer_number ) )
  95. continue;
  96. if( pcb_layer_number <= pcbCopperLayerMax ) // copper layer
  97. continue;
  98. for( GERBER_DRAW_ITEM* gerb_item : gerber->GetItems() )
  99. export_non_copper_item( gerb_item, pcb_layer_number );
  100. }
  101. // Copper layers
  102. for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
  103. {
  104. GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
  105. if( gerber == nullptr ) // Graphic layer not yet used
  106. continue;
  107. int pcb_layer_number = aLayerLookUpTable[layer];
  108. if( pcb_layer_number < 0 || pcb_layer_number > pcbCopperLayerMax )
  109. continue;
  110. for( GERBER_DRAW_ITEM* gerb_item : gerber->GetItems() )
  111. export_copper_item( gerb_item, pcb_layer_number );
  112. }
  113. // Now write out the holes we collected earlier as vias
  114. for( const EXPORT_VIA& via : m_vias )
  115. export_via( via );
  116. fprintf( m_fp, ")\n" );
  117. fclose( m_fp );
  118. m_fp = nullptr;
  119. return true;
  120. }
  121. void GBR_TO_PCB_EXPORTER::export_non_copper_item( const GERBER_DRAW_ITEM* aGbrItem, int aLayer )
  122. {
  123. // used when a D_CODE is not found. default D_CODE to draw a flashed item
  124. static D_CODE dummyD_CODE( 0 );
  125. VECTOR2I seg_start = aGbrItem->m_Start;
  126. VECTOR2I seg_end = aGbrItem->m_End;
  127. D_CODE* d_codeDescr = aGbrItem->GetDcodeDescr();
  128. SHAPE_POLY_SET polygon;
  129. if( d_codeDescr == nullptr )
  130. d_codeDescr = &dummyD_CODE;
  131. switch( aGbrItem->m_Shape )
  132. {
  133. case GBR_POLYGON:
  134. writePcbPolygon( aGbrItem->m_Polygon, aLayer );
  135. break;
  136. case GBR_SPOT_CIRCLE:
  137. {
  138. VECTOR2I center = aGbrItem->GetABPosition( seg_start );
  139. int radius = d_codeDescr->m_Size.x / 2;
  140. writePcbFilledCircle( center, radius, aLayer );
  141. }
  142. break;
  143. case GBR_SPOT_RECT:
  144. case GBR_SPOT_OVAL:
  145. case GBR_SPOT_POLY:
  146. case GBR_SPOT_MACRO:
  147. d_codeDescr->ConvertShapeToPolygon( aGbrItem );
  148. writePcbPolygon( d_codeDescr->m_Polygon, aLayer, aGbrItem->GetABPosition( seg_start ) );
  149. break;
  150. case GBR_ARC:
  151. {
  152. double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ),
  153. (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
  154. double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
  155. (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
  156. double angle = RAD2DEG(b - a);
  157. seg_start = aGbrItem->m_ArcCentre;
  158. // Ensure arc orientation is CCW
  159. if( angle < 0 )
  160. angle += 360.0;
  161. // Reverse Y axis:
  162. seg_start.y = -seg_start.y;
  163. seg_end.y = -seg_end.y;
  164. if( angle == 360.0 || angle == 0 )
  165. {
  166. fprintf( m_fp, "(gr_circle (center %s %s) (end %s %s) (layer %s) (width %s))\n",
  167. FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
  168. FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
  169. FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
  170. FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
  171. TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
  172. FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() );
  173. }
  174. else
  175. {
  176. fprintf( m_fp, "(gr_arc (start %s %s) (end %s %s) (angle %s) (layer %s) (width %s))\n",
  177. FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
  178. FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
  179. FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
  180. FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
  181. FormatDouble2Str( angle ).c_str(),
  182. TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
  183. FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() );
  184. }
  185. }
  186. break;
  187. case GBR_CIRCLE:
  188. // Reverse Y axis:
  189. seg_start.y = -seg_start.y;
  190. seg_end.y = -seg_end.y;
  191. fprintf( m_fp, "(gr_circle (start %s %s) (end %s %s) (layer %s) (width %s))\n",
  192. FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
  193. FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
  194. FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
  195. FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
  196. TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
  197. FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() );
  198. break;
  199. case GBR_SEGMENT:
  200. // Reverse Y axis:
  201. seg_start.y = -seg_start.y;
  202. seg_end.y = -seg_end.y;
  203. fprintf( m_fp, "(gr_line (start %s %s) (end %s %s) (layer %s) (width %s))\n",
  204. FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
  205. FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
  206. FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
  207. FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
  208. TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
  209. FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() );
  210. break;
  211. }
  212. }
  213. void GBR_TO_PCB_EXPORTER::collect_hole( const GERBER_DRAW_ITEM* aGbrItem )
  214. {
  215. int size = std::min( aGbrItem->m_Size.x, aGbrItem->m_Size.y );
  216. m_vias.emplace_back( aGbrItem->m_Start, size + 1, size );
  217. }
  218. void GBR_TO_PCB_EXPORTER::export_via( const EXPORT_VIA& aVia )
  219. {
  220. VECTOR2I via_pos = aVia.m_Pos;
  221. // Reverse Y axis:
  222. via_pos.y = -via_pos.y;
  223. // Layers are Front to Back
  224. fprintf( m_fp, " (via (at %s %s) (size %s) (drill %s)",
  225. FormatDouble2Str( MapToPcbUnits( via_pos.x ) ).c_str(),
  226. FormatDouble2Str( MapToPcbUnits( via_pos.y ) ).c_str(),
  227. FormatDouble2Str( MapToPcbUnits( aVia.m_Size ) ).c_str(),
  228. FormatDouble2Str( MapToPcbUnits( aVia.m_Drill ) ).c_str() );
  229. fprintf( m_fp, " (layers %s %s))\n",
  230. TO_UTF8( GetPCBDefaultLayerName( F_Cu ) ),
  231. TO_UTF8( GetPCBDefaultLayerName( B_Cu ) ) );
  232. }
  233. void GBR_TO_PCB_EXPORTER::export_copper_item( const GERBER_DRAW_ITEM* aGbrItem, int aLayer )
  234. {
  235. switch( aGbrItem->m_Shape )
  236. {
  237. case GBR_SPOT_CIRCLE:
  238. case GBR_SPOT_RECT:
  239. case GBR_SPOT_OVAL:
  240. case GBR_SPOT_POLY:
  241. case GBR_SPOT_MACRO:
  242. export_flashed_copper_item( aGbrItem, aLayer );
  243. break;
  244. case GBR_ARC:
  245. export_segarc_copper_item( aGbrItem, aLayer );
  246. break;
  247. case GBR_POLYGON:
  248. // One can use a polygon or a zone to output a Gerber region.
  249. // none are perfect.
  250. // The current way is use a polygon, as the zone export
  251. // is experimental and only for tests.
  252. #if 1
  253. writePcbPolygon( aGbrItem->m_Polygon, aLayer );
  254. #else
  255. // Only for tests:
  256. writePcbZoneItem( aGbrItem, aLayer );
  257. #endif
  258. break;
  259. default:
  260. export_segline_copper_item( aGbrItem, aLayer );
  261. break;
  262. }
  263. }
  264. void GBR_TO_PCB_EXPORTER::export_segline_copper_item( const GERBER_DRAW_ITEM* aGbrItem, int aLayer )
  265. {
  266. VECTOR2I seg_start, seg_end;
  267. seg_start = aGbrItem->m_Start;
  268. seg_end = aGbrItem->m_End;
  269. // Reverse Y axis:
  270. seg_start.y = -seg_start.y;
  271. seg_end.y = -seg_end.y;
  272. writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
  273. }
  274. void GBR_TO_PCB_EXPORTER::writeCopperLineItem( const VECTOR2I& aStart, const VECTOR2I& aEnd,
  275. int aWidth, int aLayer )
  276. {
  277. fprintf( m_fp, "(segment (start %s %s) (end %s %s) (width %s) (layer %s) (net 0))\n",
  278. FormatDouble2Str( MapToPcbUnits(aStart.x) ).c_str(),
  279. FormatDouble2Str( MapToPcbUnits(aStart.y) ).c_str(),
  280. FormatDouble2Str( MapToPcbUnits(aEnd.x) ).c_str(),
  281. FormatDouble2Str( MapToPcbUnits(aEnd.y) ).c_str(),
  282. FormatDouble2Str( MapToPcbUnits( aWidth ) ).c_str(),
  283. TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
  284. }
  285. void GBR_TO_PCB_EXPORTER::export_segarc_copper_item( const GERBER_DRAW_ITEM* aGbrItem, int aLayer )
  286. {
  287. double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ),
  288. (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
  289. double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
  290. (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
  291. VECTOR2I start = aGbrItem->m_Start;
  292. VECTOR2I end = aGbrItem->m_End;
  293. /* Because Pcbnew does not know arcs in tracks,
  294. * approximate arc by segments (SEG_COUNT__CIRCLE segment per 360 deg)
  295. * The arc is drawn anticlockwise from the start point to the end point.
  296. */
  297. #define SEG_COUNT_CIRCLE 16
  298. #define DELTA_ANGLE 2 * M_PI / SEG_COUNT_CIRCLE
  299. // calculate the number of segments from a to b.
  300. // we want CNT_PER_360 segments fo a circle
  301. if( a > b )
  302. b += 2 * M_PI;
  303. VECTOR2I curr_start = start;
  304. VECTOR2I seg_start, seg_end;
  305. int ii = 1;
  306. for( double rot = a; rot < (b - DELTA_ANGLE); rot += DELTA_ANGLE, ii++ )
  307. {
  308. seg_start = curr_start;
  309. VECTOR2I curr_end = start;
  310. RotatePoint( curr_end, aGbrItem->m_ArcCentre, -EDA_ANGLE( DELTA_ANGLE, RADIANS_T ) * ii );
  311. seg_end = curr_end;
  312. // Reverse Y axis:
  313. seg_start.y = -seg_start.y;
  314. seg_end.y = -seg_end.y;
  315. writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
  316. curr_start = curr_end;
  317. }
  318. if( end != curr_start )
  319. {
  320. seg_start = curr_start;
  321. seg_end = end;
  322. // Reverse Y axis:
  323. seg_start.y = -seg_start.y;
  324. seg_end.y = -seg_end.y;
  325. writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
  326. }
  327. }
  328. #include <wx/log.h>
  329. void GBR_TO_PCB_EXPORTER::export_flashed_copper_item( const GERBER_DRAW_ITEM* aGbrItem, int aLayer )
  330. {
  331. static D_CODE flashed_item_D_CODE( 0 );
  332. D_CODE* d_codeDescr = aGbrItem->GetDcodeDescr();
  333. if( d_codeDescr == nullptr )
  334. d_codeDescr = &flashed_item_D_CODE;
  335. if( aGbrItem->m_Shape == GBR_SPOT_CIRCLE )
  336. {
  337. // See if there's a via that we can enlarge to fit this flashed item
  338. for( EXPORT_VIA& via : m_vias )
  339. {
  340. if( via.m_Pos == aGbrItem->m_Start )
  341. {
  342. via.m_Size = std::max( via.m_Size, aGbrItem->m_Size.x );
  343. return;
  344. }
  345. }
  346. }
  347. VECTOR2I offset = aGbrItem->GetABPosition( aGbrItem->m_Start );
  348. if( aGbrItem->m_Shape == GBR_SPOT_CIRCLE ||
  349. ( aGbrItem->m_Shape == GBR_SPOT_OVAL && d_codeDescr->m_Size.x == d_codeDescr->m_Size.y ) )
  350. {
  351. // export it as filled circle
  352. VECTOR2I center = offset;
  353. int radius = d_codeDescr->m_Size.x / 2;
  354. writePcbFilledCircle( center, radius, aLayer );
  355. return;
  356. }
  357. APERTURE_MACRO* macro = d_codeDescr->GetMacro();
  358. if( macro ) // export a GBR_SPOT_MACRO
  359. {
  360. SHAPE_POLY_SET macroShape;
  361. macroShape = *macro->GetApertureMacroShape( aGbrItem, VECTOR2I( 0, 0 ) );
  362. // Compensate the Y axis orientation ( writePcbPolygon invert the Y coordinate )
  363. macroShape.Outline( 0 ).Mirror( false, true );
  364. writePcbPolygon( macroShape, aLayer, offset );
  365. }
  366. else
  367. {
  368. // Should cover primitives: GBR_SPOT_RECT, GBR_SPOT_OVAL, GBR_SPOT_POLY
  369. d_codeDescr->ConvertShapeToPolygon( aGbrItem );
  370. writePcbPolygon( d_codeDescr->m_Polygon, aLayer, offset );
  371. }
  372. }
  373. void GBR_TO_PCB_EXPORTER::writePcbFilledCircle( const VECTOR2I& aCenterPosition, int aRadius,
  374. int aLayer )
  375. {
  376. fprintf( m_fp, "(gr_circle (center %s %s) (end %s %s)",
  377. FormatDouble2Str( MapToPcbUnits( aCenterPosition.x ) ).c_str(),
  378. FormatDouble2Str( MapToPcbUnits( aCenterPosition.y ) ).c_str(),
  379. FormatDouble2Str( MapToPcbUnits( aCenterPosition.x + aRadius ) ).c_str(),
  380. FormatDouble2Str( MapToPcbUnits( aCenterPosition.y ) ).c_str() );
  381. fprintf( m_fp, "(layer %s) (width 0) (fill solid) )\n",
  382. TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
  383. }
  384. void GBR_TO_PCB_EXPORTER::writePcbHeader( const int* aLayerLookUpTable )
  385. {
  386. fprintf( m_fp, "(kicad_pcb (version 4) (generator gerbview)\n\n" );
  387. // Write layers section
  388. fprintf( m_fp, " (layers \n" );
  389. for( int ii = 0; ii < m_pcbCopperLayersCount; ii++ )
  390. {
  391. int id = ii;
  392. if( ii == m_pcbCopperLayersCount-1)
  393. id = B_Cu;
  394. fprintf( m_fp, " (%d %s signal)\n", id, TO_UTF8( GetPCBDefaultLayerName( id ) ) );
  395. }
  396. for( int ii = B_Adhes; ii < PCB_LAYER_ID_COUNT; ii++ )
  397. {
  398. if( GetPCBDefaultLayerName( ii ).IsEmpty() ) // Layer not available for export
  399. continue;
  400. fprintf( m_fp, " (%d %s user)\n", ii, TO_UTF8( GetPCBDefaultLayerName( ii ) ) );
  401. }
  402. fprintf( m_fp, " )\n\n" );
  403. }
  404. void GBR_TO_PCB_EXPORTER::writePcbPolygon( const SHAPE_POLY_SET& aPolys, int aLayer,
  405. const VECTOR2I& aOffset )
  406. {
  407. // Ensure the polygon is valid:
  408. if( aPolys.OutlineCount() < 1 )
  409. return;
  410. // aPolys is expected having only one outline and no hole
  411. // (because it comes from a gerber file or is built from a aperture )
  412. const SHAPE_LINE_CHAIN& poly = aPolys.COutline( 0 );
  413. fprintf( m_fp, "(gr_poly (pts " );
  414. #define MAX_COORD_CNT 4
  415. int jj = MAX_COORD_CNT;
  416. int cnt_max = poly.PointCount() -1;
  417. // Do not generate last corner, if it is the same point as the first point:
  418. if( poly.CPoint( 0 ) == poly.CPoint( cnt_max ) )
  419. cnt_max--;
  420. for( int ii = 0; ii <= cnt_max; ii++ )
  421. {
  422. if( --jj == 0 )
  423. {
  424. jj = MAX_COORD_CNT;
  425. fprintf( m_fp, "\n" );
  426. }
  427. fprintf( m_fp, " (xy %s %s)",
  428. FormatDouble2Str( MapToPcbUnits( poly.CPoint( ii ).x + aOffset.x ) ).c_str(),
  429. FormatDouble2Str( MapToPcbUnits( -poly.CPoint( ii ).y + aOffset.y ) ).c_str() );
  430. }
  431. fprintf( m_fp, ")" );
  432. if( jj != MAX_COORD_CNT )
  433. fprintf( m_fp, "\n" );
  434. fprintf( m_fp, "(layer %s) (width 0) )\n", TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
  435. }
  436. void GBR_TO_PCB_EXPORTER::writePcbZoneItem( const GERBER_DRAW_ITEM* aGbrItem, int aLayer )
  437. {
  438. SHAPE_POLY_SET polys = aGbrItem->m_Polygon.CloneDropTriangulation();
  439. polys.Simplify( SHAPE_POLY_SET::PM_FAST );
  440. if( polys.OutlineCount() == 0 )
  441. return;
  442. fprintf( m_fp, "(zone (net 0) (net_name \"\") (layer %s) (tstamp 0000000) (hatch edge 0.508)\n",
  443. TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
  444. fprintf( m_fp, " (connect_pads (clearance 0.0))\n" );
  445. fprintf( m_fp, " (min_thickness 0.1) (filled_areas_thickness no)\n"
  446. " (fill (thermal_gap 0.3) (thermal_bridge_width 0.3))\n" );
  447. // Now, write the zone outlines with holes.
  448. // first polygon is the main outline, next are holes
  449. // One cannot know the initial zone outline.
  450. // However most of (if not all) holes are just items with clearance,
  451. // not really a hole in the initial zone outline.
  452. // So we build a zone outline only with no hole.
  453. fprintf( m_fp, " (polygon\n (pts" );
  454. SHAPE_LINE_CHAIN& poly = polys.Outline( 0 );
  455. #define MAX_COORD_CNT 4
  456. int jj = MAX_COORD_CNT;
  457. int cnt_max = poly.PointCount() -1;
  458. // Do not generate last corner, if it is the same point as the first point:
  459. if( poly.CPoint( 0 ) == poly.CPoint( cnt_max ) )
  460. cnt_max--;
  461. for( int ii = 0; ii <= cnt_max; ii++ )
  462. {
  463. if( --jj == 0 )
  464. {
  465. jj = MAX_COORD_CNT;
  466. fprintf( m_fp, "\n " );
  467. }
  468. fprintf( m_fp, " (xy %s %s)", FormatDouble2Str( MapToPcbUnits( poly.CPoint( ii ).x ) ).c_str(),
  469. FormatDouble2Str( MapToPcbUnits( -poly.CPoint( ii ).y ) ).c_str() );
  470. }
  471. fprintf( m_fp, ")\n" );
  472. fprintf( m_fp, " )\n)\n" );
  473. }