From fa39acfc2f3d4c4e58ee89c2b41c850d6e17baa7 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Sat, 31 Mar 2018 18:43:12 +0200 Subject: [PATCH] Gerbview, export to pcbnew : export polygons as new DRAWSEGMENT polygonal shape. It can creates issues on copper layers, but the only one other alternative is to ignore polygons in export, and this is worst. --- gerbview/export_to_pcbnew.cpp | 59 +++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/gerbview/export_to_pcbnew.cpp b/gerbview/export_to_pcbnew.cpp index 3f35ddf6f4..f428f8d76f 100644 --- a/gerbview/export_to_pcbnew.cpp +++ b/gerbview/export_to_pcbnew.cpp @@ -72,7 +72,6 @@ public: private: /** - * Function export_non_copper_item * write a non copper line or arc to the board file. * @param aGbrItem = the Gerber item (line, arc) to export * @param aLayer = the technical layer to use @@ -80,7 +79,13 @@ private: void export_non_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer ); /** - * Function export_copper_item + * write a non copper polygon item to the board file. + * @param aGbrItem = the Gerber item (line, arc) to export + * @param aLayer = the technical layer to use + */ + void writePcbPolygonItem( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer ); + + /** * write a track or via) to the board file. * @param aGbrItem = the Gerber item (line, arc, flashed) to export * @param aLayer = the copper layer to use @@ -281,6 +286,12 @@ void GBR_TO_PCB_EXPORTER::export_non_copper_item( GERBER_DRAW_ITEM* aGbrItem, LA wxPoint seg_start = aGbrItem->m_Start; wxPoint seg_end = aGbrItem->m_End; + if( aGbrItem->m_Shape == GBR_POLYGON ) + { + writePcbPolygonItem( aGbrItem, aLayer ); + return; + } + if( aGbrItem->m_Shape == GBR_ARC ) { double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y), @@ -319,6 +330,12 @@ void GBR_TO_PCB_EXPORTER::export_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_ export_segarc_copper_item( aGbrItem, aLayer ); break; + case GBR_POLYGON: + // Currently: Pcbnew does not really handle polygons on copper layers (no DRC test). + // However, we can export them if the purpose of this export is to help recreate a board + writePcbPolygonItem( aGbrItem, aLayer ); + break; + default: export_segline_copper_item( aGbrItem, aLayer ); break; @@ -504,3 +521,41 @@ void GBR_TO_PCB_EXPORTER::writePcbLineItem( bool aIsArc, wxPoint& aStart, wxPoin ); } } + + +void GBR_TO_PCB_EXPORTER::writePcbPolygonItem( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer ) +{ + fprintf( m_fp, "(gr_poly (pts " ); + + SHAPE_POLY_SET polys = aGbrItem->m_Polygon; + SHAPE_LINE_CHAIN& poly = polys.Outline( 0 ); + + #define MAX_COORD_CNT 4 + int jj = MAX_COORD_CNT; + int cnt_max = poly.PointCount() -1; + + // Do not generate last corner, if it is the same point as the first point: + if( poly.Point( 0 ) == poly.Point( cnt_max ) ) + cnt_max--; + + for( int ii = 0; ii <= cnt_max; ii++ ) + { + if( --jj == 0 ) + { + jj = MAX_COORD_CNT; + fprintf( m_fp, "\n" ); + } + + fprintf( m_fp, " (xy %s %s)", + Double2Str( TO_PCB_UNIT( poly.Point( ii ).x ) ).c_str(), + Double2Str( TO_PCB_UNIT( -poly.Point( ii ).y ) ).c_str() ); + } + + fprintf( m_fp, ")" ); + + if( jj != MAX_COORD_CNT ) + fprintf( m_fp, "\n" ); + + fprintf( m_fp, "(layer %s) (width 0) )\n", + TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) ); +}