KiCad PCB EDA Suite
Loading...
Searching...
No Matches
export_to_pcbnew.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2007-2023 Jean-Pierre Charras jp.charras at wanadoo.fr
5 * Copyright The 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
25#include <vector>
26
27#include <export_to_pcbnew.h>
28
29#include <confirm.h>
30#include <string_utils.h>
31#include <lset.h>
32#include <macros.h>
33#include <trigo.h>
34#include <gerbview_frame.h>
35#include <gerber_file_image.h>
37#include <build_version.h>
39#include "excellon_image.h"
40#include <wx/log.h>
42
43
44GBR_TO_PCB_EXPORTER::GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName )
45{
46 m_gerbview_frame = aFrame;
47 m_pcb_file_name = aFileName;
48 m_fp = nullptr;
50}
51
52
56
57
58bool GBR_TO_PCB_EXPORTER::ExportPcb( const int* aLayerLookUpTable, int aCopperLayers )
59{
60 m_fp = wxFopen( m_pcb_file_name, wxT( "wt" ) );
61
62 if( m_fp == nullptr )
63 {
64 wxString msg;
65 msg.Printf( _( "Failed to create file '%s'." ), m_pcb_file_name );
67 return false;
68 }
69
70 m_pcbCopperLayersCount = aCopperLayers;
71
72 writePcbHeader( aLayerLookUpTable );
73
74 // create an image of gerber data
75 GERBER_FILE_IMAGE_LIST* images = m_gerbview_frame->GetGerberLayout()->GetImagesList();
76
77 // First collect all the holes. We'll use these to generate pads, vias, etc.
78 for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
79 {
80 int pcb_layer_number = aLayerLookUpTable[layer];
81 EXCELLON_IMAGE* excellon = dynamic_cast<EXCELLON_IMAGE*>( images->GetGbrImage( layer ) );
82 GERBER_FILE_IMAGE* gerb = dynamic_cast<GERBER_FILE_IMAGE*>( images->GetGbrImage( layer ) );
83
84 if( excellon )
85 {
86 for( GERBER_DRAW_ITEM* gerb_item : excellon->GetItems() )
87 collect_hole( gerb_item );
88 }
89 else if( gerb && pcb_layer_number == UNDEFINED_LAYER ) // PCB_LAYER_ID doesn't have an entry for Hole Data,
90 // but the dialog returns UNDEFINED_LAYER for it
91 {
92 for( GERBER_DRAW_ITEM* gerb_item : gerb->GetItems() )
93 collect_hole( gerb_item );
94 }
95 else
96 {
97 continue;
98 }
99 }
100
101 // Next: non copper layers:
102 for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
103 {
104 GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
105
106 if( gerber == nullptr ) // Graphic layer not yet used
107 continue;
108
109 int pcb_layer_number = aLayerLookUpTable[layer];
110
111 if( !IsPcbLayer( pcb_layer_number ) || IsCopperLayer( pcb_layer_number ) )
112 continue;
113
114 for( GERBER_DRAW_ITEM* gerb_item : gerber->GetItems() )
115 export_non_copper_item( gerb_item, pcb_layer_number );
116 }
117
118 // Copper layers
119 for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
120 {
121 GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
122
123 if( gerber == nullptr ) // Graphic layer not yet used
124 continue;
125
126 int pcb_layer_number = aLayerLookUpTable[layer];
127
128 if( !IsCopperLayer( pcb_layer_number ) )
129 continue;
130
131 for( GERBER_DRAW_ITEM* gerb_item : gerber->GetItems() )
132 export_copper_item( gerb_item, pcb_layer_number );
133 }
134
135 // Now write out the holes we collected earlier as vias
136 for( const EXPORT_VIA& via : m_vias )
137 export_via( via );
138
139 for( const EXPORT_SLOT& slot : m_slots )
140 export_slot( slot );
141
142 fprintf( m_fp, ")\n" );
143
144 fclose( m_fp );
145 m_fp = nullptr;
146 return true;
147}
148
149
151{
152 if( aGbrItem->GetLayerPolarity() )
153 return;
154
155 // used when a D_CODE is not found. default D_CODE to draw a flashed item
156 static D_CODE dummyD_CODE( 0 );
157
158 VECTOR2I seg_start = aGbrItem->m_Start;
159 VECTOR2I seg_end = aGbrItem->m_End;
160 D_CODE* d_codeDescr = aGbrItem->GetDcodeDescr();
161 SHAPE_POLY_SET polygon;
162
163 if( d_codeDescr == nullptr )
164 d_codeDescr = &dummyD_CODE;
165
166 switch( aGbrItem->m_ShapeType )
167 {
168 case GBR_POLYGON:
169 writePcbPolygon( aGbrItem->m_ShapeAsPolygon, aLayer );
170 break;
171
172 case GBR_SPOT_CIRCLE:
173 {
174 VECTOR2I center = aGbrItem->GetABPosition( seg_start );
175 int radius = d_codeDescr->m_Size.x / 2;
177 break;
178 }
179
180 case GBR_SPOT_RECT:
181 case GBR_SPOT_OVAL:
182 case GBR_SPOT_POLY:
183 case GBR_SPOT_MACRO:
184 {
185 d_codeDescr->ConvertShapeToPolygon( aGbrItem );
186 SHAPE_POLY_SET polyshape = d_codeDescr->m_Polygon;
187
188 if( polyshape.OutlineCount() == 0 )
189 break;
190
191 // Compensate the Y axis orientation ( writePcbPolygon invert the Y coordinate )
192 polyshape.Outline( 0 ).Mirror( { 0, 0 }, FLIP_DIRECTION::TOP_BOTTOM );
193 writePcbPolygon( polyshape, aLayer, aGbrItem->GetABPosition( seg_start ) );
194 break;
195 }
196
197 case GBR_ARC:
198 export_non_copper_arc( aGbrItem, aLayer );
199 break;
200
201 case GBR_CIRCLE:
202 // Reverse Y axis:
203 seg_start.y = -seg_start.y;
204 seg_end.y = -seg_end.y;
205
206 fprintf( m_fp, "\t(gr_circle (start %s %s) (end %s %s) (layer %s)\n",
207 FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
208 FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
209 FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
210 FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
211 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
212 export_stroke_info( aGbrItem->m_Size.x );
213 fprintf( m_fp, "\t)\n" );
214 break;
215
216 case GBR_SEGMENT:
217 if( d_codeDescr->m_ApertType == APT_RECT )
218 {
219 // Using a rectangular aperture to draw a line is deprecated since 2020
220 // However old gerber file can use it (rare case) and can generate
221 // strange shapes, because the rect aperture is not rotated to match the
222 // line orientation.
223 // So draw this line as polygon
224 SHAPE_POLY_SET polyshape;
225 aGbrItem->ConvertSegmentToPolygon( &polyshape );
226 writePcbPolygon( polyshape, aLayer );
227 }
228 else
229 {
230 // Reverse Y axis:
231 seg_start.y = -seg_start.y;
232 seg_end.y = -seg_end.y;
233
234 fprintf( m_fp, "\t(gr_line\n\t\t(start %s %s) (end %s %s) (layer %s)\n",
235 FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
236 FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
237 FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
238 FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
239 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
240
241 export_stroke_info( aGbrItem->m_Size.x );
242 fprintf( m_fp, "\t)\n" );
243 }
244
245 break;
246 }
247}
248
249
251{
252 double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ),
253 (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
254 double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
255 (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
256
257 VECTOR2I arc_center = aGbrItem->m_ArcCentre;
258 VECTOR2I seg_start = aGbrItem->m_Start;
259 VECTOR2I seg_end = aGbrItem->m_End;
260
261 if( a > b )
262 b += 2 * M_PI;
263
264 if( seg_start == seg_end )
265 {
266 // Reverse Y axis:
267 arc_center.y = -arc_center.y;
268 seg_end.y = -seg_end.y;
269
270 fprintf( m_fp, "\t(gr_circle\n\t\t(center %s %s) (end %s %s) (layer %s)\n",
271 FormatDouble2Str( MapToPcbUnits( arc_center.x ) ).c_str(),
272 FormatDouble2Str( MapToPcbUnits( arc_center.y ) ).c_str(),
273 FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
274 FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
275 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
276 export_stroke_info( aGbrItem->m_Size.x );
277 fprintf( m_fp, "\t)\n" );
278 }
279 else
280 {
281 VECTOR2I seg_middle = GetRotated( seg_start, arc_center,
282 -EDA_ANGLE( (b-a)/2, RADIANS_T ));
283
284 // Reverse Y axis:
285 seg_middle.y = -seg_middle.y;
286 seg_start.y = -seg_start.y;
287 seg_end.y = -seg_end.y;
288
289 fprintf( m_fp, "\t(gr_arc\n\t\t(start %s %s) (mid %s %s) (end %s %s) (layer %s)\n",
290 FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
291 FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
292 FormatDouble2Str( MapToPcbUnits( seg_middle.x ) ).c_str(),
293 FormatDouble2Str( MapToPcbUnits( seg_middle.y ) ).c_str(),
294 FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
295 FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
296 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
297
298 export_stroke_info( aGbrItem->m_Size.x );
299 fprintf( m_fp, "\t)\n" );
300 }
301}
302
303
305{
306 if( aGbrItem->m_ShapeType == GBR_SPOT_CIRCLE )
307 m_vias.emplace_back( aGbrItem->m_Start, aGbrItem->m_Size.x + 1, aGbrItem->m_Size.x );
308 else if( aGbrItem->m_ShapeType == GBR_SEGMENT )
309 m_slots.emplace_back( aGbrItem->m_Start, aGbrItem->m_End, aGbrItem->m_Size.x );
310}
311
312
314{
315 VECTOR2I via_pos = aVia.m_Pos;
316
317 // Reverse Y axis:
318 via_pos.y = -via_pos.y;
319
320 // Layers are Front to Back
321 fprintf( m_fp, "\t(via (at %s %s) (size %s) (drill %s)",
322 FormatDouble2Str( MapToPcbUnits( via_pos.x ) ).c_str(),
323 FormatDouble2Str( MapToPcbUnits( via_pos.y ) ).c_str(),
324 FormatDouble2Str( MapToPcbUnits( aVia.m_Size ) ).c_str(),
325 FormatDouble2Str( MapToPcbUnits( aVia.m_Drill ) ).c_str() );
326
327 fprintf( m_fp, " (layers %s %s))\n",
328 LSET::Name( F_Cu ).ToStdString().c_str(),
329 LSET::Name( B_Cu ).ToStdString().c_str() );
330}
331
332
334{
335 VECTOR2I start = aSlot.m_Start;
336 VECTOR2I end = aSlot.m_End;
337
338 // Reverse Y axis:
339 start.y = -start.y;
340 end.y = -end.y;
341
342 VECTOR2I dir = end - start;
343 int minorAxis = aSlot.m_Width;
344 int majorAxis = aSlot.m_Width + dir.EuclideanNorm();
345 VECTOR2I center = ( start + end ) / 2;
346
347 fprintf( m_fp, "\t(footprint \"slot\" (pad 1 thru_hole oval (at %s %s %s) (size %s %s) (drill oval %s %s)))\n",
348 FormatDouble2Str( MapToPcbUnits( center.x ) ).c_str(),
349 FormatDouble2Str( MapToPcbUnits( center.y ) ).c_str(),
350 FormatDouble2Str( EDA_ANGLE( dir ).AsDegrees() ).c_str(),
351 FormatDouble2Str( MapToPcbUnits( majorAxis + 1 ) ).c_str(),
352 FormatDouble2Str( MapToPcbUnits( minorAxis + 1 ) ).c_str(),
353 FormatDouble2Str( MapToPcbUnits( majorAxis ) ).c_str(),
354 FormatDouble2Str( MapToPcbUnits( minorAxis ) ).c_str() );
355}
356
357
359{
360 if( aGbrItem->GetLayerPolarity() )
361 return;
362
363 switch( aGbrItem->m_ShapeType )
364 {
365 case GBR_SPOT_CIRCLE:
366 case GBR_SPOT_RECT:
367 case GBR_SPOT_OVAL:
368 case GBR_SPOT_POLY:
369 case GBR_SPOT_MACRO:
370 export_flashed_copper_item( aGbrItem, aLayer );
371 break;
372
373 case GBR_CIRCLE:
374 case GBR_ARC:
375 export_segarc_copper_item( aGbrItem, aLayer );
376 break;
377
378 case GBR_POLYGON:
379 // One can use a polygon or a zone to output a Gerber region.
380 // none are perfect.
381 // The current way is use a polygon, as the zone export
382 // is experimental and only for tests.
383#if 1
384 writePcbPolygon( aGbrItem->m_ShapeAsPolygon, aLayer );
385#else
386 // Only for tests:
387 writePcbZoneItem( aGbrItem, aLayer );
388#endif
389 break;
390
391 case GBR_SEGMENT:
392 {
393 D_CODE* code = aGbrItem->GetDcodeDescr();
394
395 if( code && code->m_ApertType == APT_RECT )
396 {
397 if( aGbrItem->m_ShapeAsPolygon.OutlineCount() == 0 )
398 const_cast<GERBER_DRAW_ITEM*>( aGbrItem )->ConvertSegmentToPolygon();
399
400 writePcbPolygon( aGbrItem->m_ShapeAsPolygon, aLayer );
401 }
402 else
403 {
404 export_segline_copper_item( aGbrItem, aLayer );
405 }
406
407 break;
408 }
409
410 default:
411 break;
412 }
413}
414
415
417{
418 VECTOR2I seg_start, seg_end;
419
420 seg_start = aGbrItem->m_Start;
421 seg_end = aGbrItem->m_End;
422
423 // Reverse Y axis:
424 seg_start.y = -seg_start.y;
425 seg_end.y = -seg_end.y;
426
427 writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
428}
429
430
432 int aWidth, int aLayer )
433{
434 fprintf( m_fp, "\t(segment (start %s %s) (end %s %s) (width %s) (layer %s) (net 0))\n",
435 FormatDouble2Str( MapToPcbUnits(aStart.x) ).c_str(),
436 FormatDouble2Str( MapToPcbUnits(aStart.y) ).c_str(),
437 FormatDouble2Str( MapToPcbUnits(aEnd.x) ).c_str(),
438 FormatDouble2Str( MapToPcbUnits(aEnd.y) ).c_str(),
439 FormatDouble2Str( MapToPcbUnits( aWidth ) ).c_str(),
440 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
441}
442
443
445{
446 fprintf( m_fp, "\t\t(stroke (width %s) (type solid))\n",
447 FormatDouble2Str( MapToPcbUnits( aWidth ) ).c_str() );
448}
449
450
452{
453 double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ),
454 (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
455 double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
456 (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
457
458 if( a > b )
459 b += 2 * M_PI;
460
461 VECTOR2I arc_center = aGbrItem->m_ArcCentre;
462 VECTOR2I seg_end = aGbrItem->m_End;
463 VECTOR2I seg_start = aGbrItem->m_Start;
464
465 VECTOR2I seg_middle = GetRotated( seg_start, arc_center,
466 -EDA_ANGLE( (b-a)/2, RADIANS_T ));
467
468 // Reverse Y axis:
469 seg_end.y = -seg_end.y;
470 seg_start.y = -seg_start.y;
471 seg_middle.y = -seg_middle.y;
472
473 fprintf( m_fp, "\t(arc\n\t\t(start %s %s) (mid %s %s) (end %s %s) (layer %s)\n",
474 FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
475 FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
476 FormatDouble2Str( MapToPcbUnits( seg_middle.x ) ).c_str(),
477 FormatDouble2Str( MapToPcbUnits( seg_middle.y ) ).c_str(),
478 FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
479 FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
480 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
481
482 fprintf( m_fp, "\t\t(width %s) (net 0 )\n",
483 FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() );
484 fprintf( m_fp, "\t)\n" );
485}
486
487
489{
490 static D_CODE flashed_item_D_CODE( 0 );
491
492 D_CODE* d_codeDescr = aGbrItem->GetDcodeDescr();
493
494 if( d_codeDescr == nullptr )
495 d_codeDescr = &flashed_item_D_CODE;
496
497 if( aGbrItem->m_ShapeType == GBR_SPOT_CIRCLE )
498 {
499 // See if there's a via that we can enlarge to fit this flashed item
500 for( EXPORT_VIA& via : m_vias )
501 {
502 if( via.m_Pos == aGbrItem->m_Start )
503 {
504 via.m_Size = std::max( via.m_Size, aGbrItem->m_Size.x );
505 return;
506 }
507 }
508 }
509
510 VECTOR2I offset = aGbrItem->GetABPosition( aGbrItem->m_Start );
511
512 if( aGbrItem->m_ShapeType == GBR_SPOT_CIRCLE ||
513 ( aGbrItem->m_ShapeType == GBR_SPOT_OVAL && d_codeDescr->m_Size.x == d_codeDescr->m_Size.y ) )
514 {
515 // export it as filled circle
516 VECTOR2I center = offset;
517 int radius = d_codeDescr->m_Size.x / 2;
519 return;
520 }
521
522 APERTURE_MACRO* macro = d_codeDescr->GetMacro();
523
524 if( macro ) // export a GBR_SPOT_MACRO
525 {
526 SHAPE_POLY_SET macroShape;
527 macroShape = *macro->GetApertureMacroShape( aGbrItem, VECTOR2I( 0, 0 ) );
528
529 if( macroShape.OutlineCount() == 0 )
530 return;
531
532 // Compensate the Y axis orientation ( writePcbPolygon invert the Y coordinate )
533 macroShape.Outline( 0 ).Mirror( { 0, 0 }, FLIP_DIRECTION::TOP_BOTTOM );
534
535 writePcbPolygon( macroShape, aLayer, offset );
536 }
537 else
538 {
539 // Should cover primitives: GBR_SPOT_RECT, GBR_SPOT_OVAL, GBR_SPOT_POLY
540 d_codeDescr->ConvertShapeToPolygon( aGbrItem );
541 writePcbPolygon( d_codeDescr->m_Polygon, aLayer, offset );
542 }
543}
544
545
546void GBR_TO_PCB_EXPORTER::writePcbFilledCircle( const VECTOR2I& aCenterPosition, int aRadius,
547 int aLayer )
548{
549 fprintf( m_fp, "\t(gr_circle\n\t\t(center %s %s) (end %s %s)\n",
550 FormatDouble2Str( MapToPcbUnits( aCenterPosition.x ) ).c_str(),
551 FormatDouble2Str( MapToPcbUnits( aCenterPosition.y ) ).c_str(),
552 FormatDouble2Str( MapToPcbUnits( aCenterPosition.x + aRadius ) ).c_str(),
553 FormatDouble2Str( MapToPcbUnits( aCenterPosition.y ) ).c_str() );
554
556 fprintf( m_fp, "\t\t(fill yes) (layer %s)",
557 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
558 fprintf( m_fp, "\n\t)\n" );
559}
560
561
562void GBR_TO_PCB_EXPORTER::writePcbHeader( const int* aLayerLookUpTable )
563{
564 // Note: the .kicad_pcb version used here is after layers_id changes
565 fprintf( m_fp, "(kicad_pcb (version 20240928)\n" );
566 fprintf( m_fp, "\t(generator \"gerbview\")\n\t(generator_version \"%s\")\n\n",
567 GetMajorMinorVersion().c_str().AsChar() );
568
569 // Write layers section
570 fprintf( m_fp, "\t(layers \n" );
571
573
574 for( auto cu_it = layer_set.copper_layers_begin(); cu_it != layer_set.copper_layers_end(); ++cu_it )
575 {
576 fprintf( m_fp, "\t\t(%d %s signal)\n",
577 *cu_it, LSET::Name( *cu_it ).ToStdString().c_str() );
578 }
579
580 for( auto non_cu_it = layer_set.non_copper_layers_begin(); non_cu_it != layer_set.non_copper_layers_end(); ++non_cu_it )
581 {
582 fprintf( m_fp, "\t\t(%d %s user)\n",
583 *non_cu_it, LSET::Name( *non_cu_it ).ToStdString().c_str() );
584 }
585
586 fprintf( m_fp, "\t)\n\n" );
587}
588
589
591 const VECTOR2I& aOffset )
592{
593 // Ensure the polygon is valid:
594 if( aPolys.OutlineCount() < 1 )
595 return;
596
597 // aPolys is expected having only one outline and no hole
598 // (because it comes from a gerber file or is built from a aperture )
599 const SHAPE_LINE_CHAIN& poly = aPolys.COutline( 0 );
600
601 fprintf( m_fp, "\t(gr_poly\n\t\t(pts\n\t\t\t" );
602
603 #define MAX_COORD_CNT 4
604 int jj = MAX_COORD_CNT;
605 int cnt_max = poly.PointCount() -1;
606
607 // Do not generate last corner, if it is the same point as the first point:
608 if( poly.CPoint( 0 ) == poly.CPoint( cnt_max ) )
609 cnt_max--;
610
611 for( int ii = 0; ii <= cnt_max; ii++ )
612 {
613 if( --jj == 0 )
614 {
615 jj = MAX_COORD_CNT;
616 fprintf( m_fp, "\n\t\t\t" );
617 }
618
619 fprintf( m_fp, " (xy %s %s)",
620 FormatDouble2Str( MapToPcbUnits( poly.CPoint( ii ).x + aOffset.x ) ).c_str(),
621 FormatDouble2Str( MapToPcbUnits( -poly.CPoint( ii ).y + aOffset.y ) ).c_str() );
622 }
623
624 fprintf( m_fp, ")" );
625
626 fprintf( m_fp, "\n" );
628 fprintf( m_fp, "\t\t(fill yes) (layer %s)",
629 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
630 fprintf( m_fp, "\n\t)\n" );
631}
632
633
635{
637 polys.Simplify();
638
639 if( polys.OutlineCount() == 0 )
640 return;
641
642 fprintf( m_fp, "\t(zone (net 0) (net_name \"\") (layer %s) (tstamp 0000000) (hatch edge 0.508)\n",
643 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
644
645 fprintf( m_fp, " (connect_pads (clearance 0.0))\n" );
646
647 fprintf( m_fp, " (min_thickness 0.1) (filled_areas_thickness no)\n"
648 " (fill (thermal_gap 0.3) (thermal_bridge_width 0.3))\n" );
649
650 // Now, write the zone outlines with holes.
651 // first polygon is the main outline, next are holes
652 // One cannot know the initial zone outline.
653 // However most of (if not all) holes are just items with clearance,
654 // not really a hole in the initial zone outline.
655 // So we build a zone outline only with no hole.
656 fprintf( m_fp, " (polygon\n (pts" );
657
658 SHAPE_LINE_CHAIN& poly = polys.Outline( 0 );
659
660 #define MAX_COORD_CNT 4
661 int jj = MAX_COORD_CNT;
662 int cnt_max = poly.PointCount() -1;
663
664 // Do not generate last corner, if it is the same point as the first point:
665 if( poly.CPoint( 0 ) == poly.CPoint( cnt_max ) )
666 cnt_max--;
667
668 for( int ii = 0; ii <= cnt_max; ii++ )
669 {
670 if( --jj == 0 )
671 {
672 jj = MAX_COORD_CNT;
673 fprintf( m_fp, "\n " );
674 }
675
676 fprintf( m_fp, " (xy %s %s)", FormatDouble2Str( MapToPcbUnits( poly.CPoint( ii ).x ) ).c_str(),
677 FormatDouble2Str( MapToPcbUnits( -poly.CPoint( ii ).y ) ).c_str() );
678 }
679
680 fprintf( m_fp, ")\n" );
681
682 fprintf( m_fp, " )\n)\n" );
683}
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
Support the "aperture macro" defined within standard RS274X.
SHAPE_POLY_SET * GetApertureMacroShape(const GERBER_DRAW_ITEM *aParent, const VECTOR2I &aShapePos)
Calculate the primitive shape for flashed items.
A gerber DCODE (also called Aperture) definition.
Definition dcode.h:80
APERTURE_MACRO * GetMacro() const
Definition dcode.h:136
VECTOR2I m_Size
Horizontal and vertical dimensions.
Definition dcode.h:201
APERTURE_T m_ApertType
Aperture type ( Line, rectangle, circle, oval poly, macro )
Definition dcode.h:202
SHAPE_POLY_SET m_Polygon
Definition dcode.h:217
void ConvertShapeToPolygon(const GERBER_DRAW_ITEM *aParent)
Convert a shape to an equivalent polygon.
Definition dcode.cpp:297
Handle a drill image.
void export_copper_item(const GERBER_DRAW_ITEM *aGbrItem, int aLayer)
Write a track (or via) to the board file.
void export_stroke_info(double aWidth)
Write the stroke info (thickness, line type) to the board file.
void export_non_copper_arc(const GERBER_DRAW_ITEM *aGbrItem, int aLayer)
Write a non copper arc to the board file.
void export_non_copper_item(const GERBER_DRAW_ITEM *aGbrItem, int aLayer)
Write a non copper line or arc to the board file.
void writePcbZoneItem(const GERBER_DRAW_ITEM *aGbrItem, int aLayer)
Write a zone item to the board file.
GERBVIEW_FRAME * m_gerbview_frame
void export_segarc_copper_item(const GERBER_DRAW_ITEM *aGbrItem, int aLayer)
Write a set of tracks (arcs are approximated by track segments) to the board file.
GBR_TO_PCB_EXPORTER(GERBVIEW_FRAME *aFrame, const wxString &aFileName)
void export_flashed_copper_item(const GERBER_DRAW_ITEM *aGbrItem, int aLayer)
Write a synthetic pad to the board file.
double MapToPcbUnits(int aValue) const
Map GerbView internal units to millimeters for Pcbnew board files.
std::vector< EXPORT_VIA > m_vias
void collect_hole(const GERBER_DRAW_ITEM *aGbrItem)
Collect holes from a drill layer.
void writePcbPolygon(const SHAPE_POLY_SET &aPolys, int aLayer, const VECTOR2I &aOffset={ 0, 0 })
Write a non-copper polygon to the board file.
void writePcbFilledCircle(const VECTOR2I &aCenterPosition, int aRadius, int aLayer)
Write a filled circle to the board file (with line thickness = 0).
std::vector< EXPORT_SLOT > m_slots
void export_segline_copper_item(const GERBER_DRAW_ITEM *aGbrItem, int aLayer)
Write a track (not via) to the board file.
void writePcbHeader(const int *aLayerLookUpTable)
Write a very basic header to the board file.
void export_slot(const EXPORT_SLOT &aSlot)
bool ExportPcb(const int *aLayerLookUpTable, int aCopperLayers)
Save a board from a set of Gerber images.
void writeCopperLineItem(const VECTOR2I &aStart, const VECTOR2I &aEnd, int aWidth, int aLayer)
Basic write function to write a a PCB_TRACK to the board file from a non flashed item.
void export_via(const EXPORT_VIA &aVia)
Write a via to the board file.
D_CODE * GetDcodeDescr() const
Return the GetDcodeDescr of this object, or NULL.
VECTOR2I GetABPosition(const VECTOR2I &aXYPosition) const
Return the image position of aPosition for this object.
SHAPE_POLY_SET m_ShapeAsPolygon
bool GetLayerPolarity() const
void ConvertSegmentToPolygon()
Convert a line to an equivalent polygon.
GBR_BASIC_SHAPE_TYPE m_ShapeType
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
Hold the image data and parameters for one gerber file and layer parameters.
GERBER_DRAW_ITEMS & GetItems()
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
copper_layers_iterator copper_layers_end() const
Definition lset.cpp:923
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:608
static const LSET & UserMask()
Definition lset.cpp:690
copper_layers_iterator copper_layers_begin() const
Definition lset.cpp:917
non_copper_layers_iterator non_copper_layers_begin() const
Definition lset.cpp:929
static const LSET & AllTechMask()
Return a mask holding all technical layers (no CU layer) on both side.
Definition lset.cpp:676
non_copper_layers_iterator non_copper_layers_end() const
Definition lset.cpp:935
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition lset.cpp:188
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
void Mirror(const VECTOR2I &aRef, FLIP_DIRECTION aFlipDirection)
Mirror the line points about y or x (or both).
Represent a set of closed polygons.
void Simplify()
Simplify the polyset (merges overlapping polys, eliminates degeneracy/self-intersections)
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int OutlineCount() const
Return the number of outlines in the set.
SHAPE_POLY_SET CloneDropTriangulation() const
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector2d.h:283
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:196
This file is part of the common library.
@ APT_RECT
Definition dcode.h:50
#define _(s)
@ RADIANS_T
Definition eda_angle.h:32
#define MAX_COORD_CNT
@ GBR_SPOT_OVAL
@ GBR_SEGMENT
@ GBR_SPOT_POLY
@ GBR_SPOT_RECT
@ GBR_CIRCLE
@ GBR_POLYGON
@ GBR_SPOT_CIRCLE
@ GBR_ARC
@ GBR_SPOT_MACRO
bool IsPcbLayer(int aLayer)
Test whether a layer is a valid layer for Pcbnew.
Definition layer_ids.h:668
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition layer_ids.h:679
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ B_Cu
Definition layer_ids.h:65
@ UNDEFINED_LAYER
Definition layer_ids.h:61
@ F_Cu
Definition layer_ids.h:64
This file contains miscellaneous commonly used macros and functions.
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:29
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
VECTOR2I center
int radius
VECTOR2I end
#define M_PI
VECTOR2I GetRotated(const VECTOR2I &aVector, const EDA_ANGLE &aAngle)
Return a new VECTOR2I that is the result of rotating aVector by aAngle.
Definition trigo.h:77
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687
Definition of file extensions used in Kicad.