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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <vector>
22
23#include <export_to_pcbnew.h>
24
25#include <confirm.h>
26#include <string_utils.h>
27#include <lset.h>
28#include <macros.h>
29#include <trigo.h>
30#include <gerber_file_image.h>
32#include <build_version.h>
34#include "excellon_image.h"
35#include <wx/log.h>
37
38
40 const wxString& aFileName )
41{
42 m_images = aImages;
43 m_parent = aParent;
44 m_pcb_file_name = aFileName;
45 m_fp = nullptr;
47}
48
49
53
54
55bool GBR_TO_PCB_EXPORTER::ExportPcb( const int* aLayerLookUpTable, int aCopperLayers )
56{
57 m_fp = wxFopen( m_pcb_file_name, wxT( "wt" ) );
58
59 if( m_fp == nullptr )
60 {
61 wxString msg;
62 msg.Printf( _( "Failed to create file '%s'." ), m_pcb_file_name );
63 DisplayError( m_parent, msg );
64 return false;
65 }
66
67 m_pcbCopperLayersCount = aCopperLayers;
68
69 writePcbHeader( aLayerLookUpTable );
70
72
73 // First collect all the holes. We'll use these to generate pads, vias, etc.
74 for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
75 {
76 int pcb_layer_number = aLayerLookUpTable[layer];
77 EXCELLON_IMAGE* excellon = dynamic_cast<EXCELLON_IMAGE*>( images->GetGbrImage( layer ) );
78 GERBER_FILE_IMAGE* gerb = dynamic_cast<GERBER_FILE_IMAGE*>( images->GetGbrImage( layer ) );
79
80 if( excellon )
81 {
82 for( GERBER_DRAW_ITEM* gerb_item : excellon->GetItems() )
83 collect_hole( gerb_item );
84 }
85 else if( gerb && pcb_layer_number == UNDEFINED_LAYER ) // PCB_LAYER_ID doesn't have an entry for Hole Data,
86 // but the dialog returns UNDEFINED_LAYER for it
87 {
88 for( GERBER_DRAW_ITEM* gerb_item : gerb->GetItems() )
89 collect_hole( gerb_item );
90 }
91 else
92 {
93 continue;
94 }
95 }
96
97 // Next: non copper layers:
98 for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
99 {
100 GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
101
102 if( gerber == nullptr ) // Graphic layer not yet used
103 continue;
104
105 int pcb_layer_number = aLayerLookUpTable[layer];
106
107 if( !IsPcbLayer( pcb_layer_number ) || IsCopperLayer( pcb_layer_number ) )
108 continue;
109
110 collect_clear_shapes( gerber );
111
112 const GERBER_DRAW_ITEMS& items = gerber->GetItems();
113
114 for( size_t ii = 0; ii < items.size(); ++ii )
115 export_non_copper_item( items[ii], ii, 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 collect_clear_shapes( gerber );
132
133 const GERBER_DRAW_ITEMS& items = gerber->GetItems();
134
135 for( size_t ii = 0; ii < items.size(); ++ii )
136 export_copper_item( items[ii], ii, pcb_layer_number );
137 }
138
139 // Now write out the holes we collected earlier as vias
140 for( const EXPORT_VIA& via : m_vias )
141 export_via( via );
142
143 for( const EXPORT_SLOT& slot : m_slots )
144 export_slot( slot );
145
146 fprintf( m_fp, ")\n" );
147
148 fclose( m_fp );
149 m_fp = nullptr;
150 return true;
151}
152
153
154void GBR_TO_PCB_EXPORTER::export_non_copper_item( const GERBER_DRAW_ITEM* aGbrItem, size_t aIndex, int aLayer )
155{
156 if( aGbrItem->GetLayerPolarity() )
157 return;
158
159 // used when a D_CODE is not found. default D_CODE to draw a flashed item
160 static D_CODE dummyD_CODE( 0 );
161
162 VECTOR2I seg_start = aGbrItem->m_Start;
163 VECTOR2I seg_end = aGbrItem->m_End;
164 D_CODE* d_codeDescr = aGbrItem->GetDcodeDescr();
165 SHAPE_POLY_SET polygon;
166
167 if( d_codeDescr == nullptr )
168 d_codeDescr = &dummyD_CODE;
169
170 switch( aGbrItem->m_ShapeType )
171 {
172 case GBR_POLYGON: write_trimmed_polygon( aGbrItem, aIndex, aLayer ); break;
173
174 case GBR_SPOT_CIRCLE:
175 {
176 VECTOR2I center = aGbrItem->GetABPosition( seg_start );
177 int radius = d_codeDescr->m_Size.x / 2;
179 break;
180 }
181
182 case GBR_SPOT_RECT:
183 case GBR_SPOT_OVAL:
184 case GBR_SPOT_POLY:
185 case GBR_SPOT_MACRO:
186 {
187 d_codeDescr->ConvertShapeToPolygon( aGbrItem );
188 SHAPE_POLY_SET polyshape = d_codeDescr->m_Polygon;
189
190 if( polyshape.OutlineCount() == 0 )
191 break;
192
193 // Compensate the Y axis orientation ( writePcbPolygon invert the Y coordinate )
194 polyshape.Outline( 0 ).Mirror( { 0, 0 }, FLIP_DIRECTION::TOP_BOTTOM );
195 writePcbPolygon( polyshape, aLayer, aGbrItem->GetABPosition( seg_start ) );
196 break;
197 }
198
199 case GBR_ARC:
200 export_non_copper_arc( aGbrItem, aLayer );
201 break;
202
203 case GBR_CIRCLE:
204 // Reverse Y axis:
205 seg_start.y = -seg_start.y;
206 seg_end.y = -seg_end.y;
207
208 fprintf( m_fp, "\t(gr_circle (start %s %s) (end %s %s) (layer %s)\n",
209 FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
210 FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
211 FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
212 FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
213 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
214 export_stroke_info( aGbrItem->m_Size.x );
215 fprintf( m_fp, "\t)\n" );
216 break;
217
218 case GBR_SEGMENT:
219 if( d_codeDescr->m_ApertType == APT_RECT )
220 {
221 // Using a rectangular aperture to draw a line is deprecated since 2020
222 // However old gerber file can use it (rare case) and can generate
223 // strange shapes, because the rect aperture is not rotated to match the
224 // line orientation.
225 // So draw this line as polygon
226 SHAPE_POLY_SET polyshape;
227 aGbrItem->ConvertSegmentToPolygon( &polyshape );
228 writePcbPolygon( polyshape, aLayer );
229 }
230 else
231 {
232 // Reverse Y axis:
233 seg_start.y = -seg_start.y;
234 seg_end.y = -seg_end.y;
235
236 fprintf( m_fp, "\t(gr_line\n\t\t(start %s %s) (end %s %s) (layer %s)\n",
237 FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
238 FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
239 FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
240 FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
241 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
242
243 export_stroke_info( aGbrItem->m_Size.x );
244 fprintf( m_fp, "\t)\n" );
245 }
246
247 break;
248 }
249}
250
251
253{
254 double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ),
255 (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
256 double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
257 (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
258
259 VECTOR2I arc_center = aGbrItem->m_ArcCentre;
260 VECTOR2I seg_start = aGbrItem->m_Start;
261 VECTOR2I seg_end = aGbrItem->m_End;
262
263 if( a > b )
264 b += 2 * M_PI;
265
266 if( seg_start == seg_end )
267 {
268 // Reverse Y axis:
269 arc_center.y = -arc_center.y;
270 seg_end.y = -seg_end.y;
271
272 fprintf( m_fp, "\t(gr_circle\n\t\t(center %s %s) (end %s %s) (layer %s)\n",
273 FormatDouble2Str( MapToPcbUnits( arc_center.x ) ).c_str(),
274 FormatDouble2Str( MapToPcbUnits( arc_center.y ) ).c_str(),
275 FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
276 FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
277 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
278 export_stroke_info( aGbrItem->m_Size.x );
279 fprintf( m_fp, "\t)\n" );
280 }
281 else
282 {
283 VECTOR2I seg_middle = GetRotated( seg_start, arc_center,
284 -EDA_ANGLE( (b-a)/2, RADIANS_T ));
285
286 // Reverse Y axis:
287 seg_middle.y = -seg_middle.y;
288 seg_start.y = -seg_start.y;
289 seg_end.y = -seg_end.y;
290
291 fprintf( m_fp, "\t(gr_arc\n\t\t(start %s %s) (mid %s %s) (end %s %s) (layer %s)\n",
292 FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
293 FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
294 FormatDouble2Str( MapToPcbUnits( seg_middle.x ) ).c_str(),
295 FormatDouble2Str( MapToPcbUnits( seg_middle.y ) ).c_str(),
296 FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
297 FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
298 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
299
300 export_stroke_info( aGbrItem->m_Size.x );
301 fprintf( m_fp, "\t)\n" );
302 }
303}
304
305
307{
308 if( aGbrItem->m_ShapeType == GBR_SPOT_CIRCLE )
309 m_vias.emplace_back( aGbrItem->m_Start, aGbrItem->m_Size.x + 1, aGbrItem->m_Size.x );
310 else if( aGbrItem->m_ShapeType == GBR_SEGMENT )
311 m_slots.emplace_back( aGbrItem->m_Start, aGbrItem->m_End, aGbrItem->m_Size.x );
312}
313
314
316{
317 VECTOR2I via_pos = aVia.m_Pos;
318
319 // Reverse Y axis:
320 via_pos.y = -via_pos.y;
321
322 // Layers are Front to Back
323 fprintf( m_fp, "\t(via (at %s %s) (size %s) (drill %s)",
324 FormatDouble2Str( MapToPcbUnits( via_pos.x ) ).c_str(),
325 FormatDouble2Str( MapToPcbUnits( via_pos.y ) ).c_str(),
326 FormatDouble2Str( MapToPcbUnits( aVia.m_Size ) ).c_str(),
327 FormatDouble2Str( MapToPcbUnits( aVia.m_Drill ) ).c_str() );
328
329 fprintf( m_fp, " (layers %s %s))\n",
330 LSET::Name( F_Cu ).ToStdString().c_str(),
331 LSET::Name( B_Cu ).ToStdString().c_str() );
332}
333
334
336{
337 VECTOR2I start = aSlot.m_Start;
338 VECTOR2I end = aSlot.m_End;
339
340 // Reverse Y axis:
341 start.y = -start.y;
342 end.y = -end.y;
343
344 VECTOR2I dir = end - start;
345 int minorAxis = aSlot.m_Width;
346 int majorAxis = aSlot.m_Width + dir.EuclideanNorm();
347 VECTOR2I center = ( start + end ) / 2;
348
349 fprintf( m_fp, "\t(footprint \"slot\" (pad 1 thru_hole oval (at %s %s %s) (size %s %s) (drill oval %s %s)))\n",
350 FormatDouble2Str( MapToPcbUnits( center.x ) ).c_str(),
351 FormatDouble2Str( MapToPcbUnits( center.y ) ).c_str(),
352 FormatDouble2Str( EDA_ANGLE( dir ).AsDegrees() ).c_str(),
353 FormatDouble2Str( MapToPcbUnits( majorAxis + 1 ) ).c_str(),
354 FormatDouble2Str( MapToPcbUnits( minorAxis + 1 ) ).c_str(),
355 FormatDouble2Str( MapToPcbUnits( majorAxis ) ).c_str(),
356 FormatDouble2Str( MapToPcbUnits( minorAxis ) ).c_str() );
357}
358
359
360void GBR_TO_PCB_EXPORTER::export_copper_item( const GERBER_DRAW_ITEM* aGbrItem, size_t aIndex, int aLayer )
361{
362 if( aGbrItem->GetLayerPolarity() )
363 return;
364
365 switch( aGbrItem->m_ShapeType )
366 {
367 case GBR_SPOT_CIRCLE:
368 case GBR_SPOT_RECT:
369 case GBR_SPOT_OVAL:
370 case GBR_SPOT_POLY:
371 case GBR_SPOT_MACRO:
372 export_flashed_copper_item( aGbrItem, aLayer );
373 break;
374
375 case GBR_CIRCLE:
376 case GBR_ARC:
377 export_segarc_copper_item( aGbrItem, aLayer );
378 break;
379
380 case GBR_POLYGON:
381 // One can use a polygon or a zone to output a Gerber region.
382 // none are perfect.
383 // The current way is use a polygon, as the zone export
384 // is experimental and only for tests.
385#if 1
386 write_trimmed_polygon( aGbrItem, aIndex, aLayer );
387#else
388 // Only for tests:
389 writePcbZoneItem( aGbrItem, aLayer );
390#endif
391 break;
392
393 case GBR_SEGMENT:
394 {
395 D_CODE* code = aGbrItem->GetDcodeDescr();
396
397 if( code && code->m_ApertType == APT_RECT )
398 {
399 if( aGbrItem->m_ShapeAsPolygon.OutlineCount() == 0 )
400 const_cast<GERBER_DRAW_ITEM*>( aGbrItem )->ConvertSegmentToPolygon();
401
402 writePcbPolygon( aGbrItem->m_ShapeAsPolygon, aLayer );
403 }
404 else
405 {
406 export_segline_copper_item( aGbrItem, aLayer );
407 }
408
409 break;
410 }
411
412 default:
413 break;
414 }
415}
416
417
419{
420 VECTOR2I seg_start, seg_end;
421
422 seg_start = aGbrItem->m_Start;
423 seg_end = aGbrItem->m_End;
424
425 // Reverse Y axis:
426 seg_start.y = -seg_start.y;
427 seg_end.y = -seg_end.y;
428
429 writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
430}
431
432
434 int aWidth, int aLayer )
435{
436 fprintf( m_fp, "\t(segment (start %s %s) (end %s %s) (width %s) (layer %s) (net 0))\n",
437 FormatDouble2Str( MapToPcbUnits(aStart.x) ).c_str(),
438 FormatDouble2Str( MapToPcbUnits(aStart.y) ).c_str(),
439 FormatDouble2Str( MapToPcbUnits(aEnd.x) ).c_str(),
440 FormatDouble2Str( MapToPcbUnits(aEnd.y) ).c_str(),
441 FormatDouble2Str( MapToPcbUnits( aWidth ) ).c_str(),
442 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
443}
444
445
447{
448 fprintf( m_fp, "\t\t(stroke (width %s) (type solid))\n",
449 FormatDouble2Str( MapToPcbUnits( aWidth ) ).c_str() );
450}
451
452
454{
455 double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ),
456 (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
457 double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
458 (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
459
460 if( a > b )
461 b += 2 * M_PI;
462
463 VECTOR2I arc_center = aGbrItem->m_ArcCentre;
464 VECTOR2I seg_end = aGbrItem->m_End;
465 VECTOR2I seg_start = aGbrItem->m_Start;
466
467 VECTOR2I seg_middle = GetRotated( seg_start, arc_center,
468 -EDA_ANGLE( (b-a)/2, RADIANS_T ));
469
470 // Reverse Y axis:
471 seg_end.y = -seg_end.y;
472 seg_start.y = -seg_start.y;
473 seg_middle.y = -seg_middle.y;
474
475 fprintf( m_fp, "\t(arc\n\t\t(start %s %s) (mid %s %s) (end %s %s) (layer %s)\n",
476 FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
477 FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
478 FormatDouble2Str( MapToPcbUnits( seg_middle.x ) ).c_str(),
479 FormatDouble2Str( MapToPcbUnits( seg_middle.y ) ).c_str(),
480 FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
481 FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
482 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
483
484 fprintf( m_fp, "\t\t(width %s) (net 0 )\n",
485 FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() );
486 fprintf( m_fp, "\t)\n" );
487}
488
489
491{
492 static D_CODE flashed_item_D_CODE( 0 );
493
494 D_CODE* d_codeDescr = aGbrItem->GetDcodeDescr();
495
496 if( d_codeDescr == nullptr )
497 d_codeDescr = &flashed_item_D_CODE;
498
499 if( aGbrItem->m_ShapeType == GBR_SPOT_CIRCLE )
500 {
501 // See if there's a via that we can enlarge to fit this flashed item
502 for( EXPORT_VIA& via : m_vias )
503 {
504 if( via.m_Pos == aGbrItem->m_Start )
505 {
506 via.m_Size = std::max( via.m_Size, aGbrItem->m_Size.x );
507 return;
508 }
509 }
510 }
511
512 VECTOR2I offset = aGbrItem->GetABPosition( aGbrItem->m_Start );
513
514 if( aGbrItem->m_ShapeType == GBR_SPOT_CIRCLE ||
515 ( aGbrItem->m_ShapeType == GBR_SPOT_OVAL && d_codeDescr->m_Size.x == d_codeDescr->m_Size.y ) )
516 {
517 // export it as filled circle
518 VECTOR2I center = offset;
519 int radius = d_codeDescr->m_Size.x / 2;
521 return;
522 }
523
524 APERTURE_MACRO* macro = d_codeDescr->GetMacro();
525
526 if( macro ) // export a GBR_SPOT_MACRO
527 {
528 SHAPE_POLY_SET macroShape;
529 macroShape = *macro->GetApertureMacroShape( aGbrItem, VECTOR2I( 0, 0 ) );
530
531 if( macroShape.OutlineCount() == 0 )
532 return;
533
534 // Compensate the Y axis orientation ( writePcbPolygon invert the Y coordinate )
535 macroShape.Outline( 0 ).Mirror( { 0, 0 }, FLIP_DIRECTION::TOP_BOTTOM );
536
537 writePcbPolygon( macroShape, aLayer, offset );
538 }
539 else
540 {
541 // Should cover primitives: GBR_SPOT_RECT, GBR_SPOT_OVAL, GBR_SPOT_POLY
542 d_codeDescr->ConvertShapeToPolygon( aGbrItem );
543 writePcbPolygon( d_codeDescr->m_Polygon, aLayer, offset );
544 }
545}
546
547
548void GBR_TO_PCB_EXPORTER::writePcbFilledCircle( const VECTOR2I& aCenterPosition, int aRadius,
549 int aLayer )
550{
551 fprintf( m_fp, "\t(gr_circle\n\t\t(center %s %s) (end %s %s)\n",
552 FormatDouble2Str( MapToPcbUnits( aCenterPosition.x ) ).c_str(),
553 FormatDouble2Str( MapToPcbUnits( aCenterPosition.y ) ).c_str(),
554 FormatDouble2Str( MapToPcbUnits( aCenterPosition.x + aRadius ) ).c_str(),
555 FormatDouble2Str( MapToPcbUnits( aCenterPosition.y ) ).c_str() );
556
558 fprintf( m_fp, "\t\t(fill yes) (layer %s)",
559 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
560 fprintf( m_fp, "\n\t)\n" );
561}
562
563
564void GBR_TO_PCB_EXPORTER::writePcbHeader( const int* aLayerLookUpTable )
565{
566 // Note: the .kicad_pcb version used here is after layers_id changes
567 fprintf( m_fp, "(kicad_pcb (version 20240928)\n" );
568 fprintf( m_fp, "\t(generator \"gerbview\")\n\t(generator_version \"%s\")\n\n",
569 GetMajorMinorVersion().c_str().AsChar() );
570
571 // Write layers section
572 fprintf( m_fp, "\t(layers \n" );
573
575
576 for( auto cu_it = layer_set.copper_layers_begin(); cu_it != layer_set.copper_layers_end(); ++cu_it )
577 {
578 fprintf( m_fp, "\t\t(%d %s signal)\n",
579 *cu_it, LSET::Name( *cu_it ).ToStdString().c_str() );
580 }
581
582 for( auto non_cu_it = layer_set.non_copper_layers_begin(); non_cu_it != layer_set.non_copper_layers_end(); ++non_cu_it )
583 {
584 fprintf( m_fp, "\t\t(%d %s user)\n",
585 *non_cu_it, LSET::Name( *non_cu_it ).ToStdString().c_str() );
586 }
587
588 fprintf( m_fp, "\t)\n\n" );
589}
590
591
593{
594 const int maxError = gerbIUScale.mmToIU( 0.005 );
595
596 m_clearShapes.clear();
597
598 const GERBER_DRAW_ITEMS& items = aImage->GetItems();
599
600 for( size_t ii = 0; ii < items.size(); ++ii )
601 {
602 const GERBER_DRAW_ITEM* item = items[ii];
603
604 if( !item->GetLayerPolarity() )
605 continue;
606
607 D_CODE* code = item->GetDcodeDescr();
608 SHAPE_POLY_SET shape;
609
610 switch( item->m_ShapeType )
611 {
612 case GBR_POLYGON: shape = item->m_ShapeAsPolygon; break;
613
614 case GBR_SEGMENT:
615 if( code && code->m_ApertType == APT_RECT )
616 {
617 item->ConvertSegmentToPolygon( &shape );
618 }
619 else
620 {
621 TransformOvalToPolygon( shape, item->m_Start, item->m_End, item->m_Size.x, maxError, ERROR_OUTSIDE );
622 }
623
624 break;
625
626 case GBR_ARC:
627 {
628 VECTOR2I centre = item->m_ArcCentre;
629
630 if( item->m_Start == item->m_End )
631 {
632 TransformRingToPolygon( shape, centre, KiROUND( item->m_Start.Distance( centre ) ), item->m_Size.x,
633 maxError, ERROR_OUTSIDE );
634 break;
635 }
636
637 double a = atan2( (double) ( item->m_Start.y - centre.y ), (double) ( item->m_Start.x - centre.x ) );
638 double b = atan2( (double) ( item->m_End.y - centre.y ), (double) ( item->m_End.x - centre.x ) );
639
640 if( a > b )
641 b += 2 * M_PI;
642
643 VECTOR2I mid = GetRotated( item->m_Start, centre, -EDA_ANGLE( ( b - a ) / 2, RADIANS_T ) );
644
645 TransformArcToPolygon( shape, item->m_Start, mid, item->m_End, item->m_Size.x, maxError, ERROR_OUTSIDE );
646 break;
647 }
648
649 case GBR_CIRCLE:
650 TransformRingToPolygon( shape, item->m_Start, KiROUND( item->m_Start.Distance( item->m_End ) ),
651 item->m_Size.x, maxError, ERROR_OUTSIDE );
652 break;
653
654 default:
655 if( code )
656 {
657 code->ConvertShapeToPolygon( item );
658 shape = code->m_Polygon;
659 shape.Move( item->m_Start );
660 }
661
662 break;
663 }
664
665 if( shape.IsEmpty() )
666 continue;
667
668 BOX2I bbox = shape.BBox();
669 m_clearShapes.push_back( { ii, bbox, std::move( shape ) } );
670 }
671}
672
673
674void GBR_TO_PCB_EXPORTER::write_trimmed_polygon( const GERBER_DRAW_ITEM* aGbrItem, size_t aIndex, int aLayer )
675{
676 SHAPE_POLY_SET polygon = aGbrItem->m_ShapeAsPolygon;
677 BOX2I bbox = polygon.BBox();
678 SHAPE_POLY_SET clearMask;
679
680 for( const CLEAR_SHAPE& clear : m_clearShapes )
681 {
682 if( clear.m_Index > aIndex && clear.m_BBox.Intersects( bbox ) )
683 clearMask.Append( clear.m_Shape );
684 }
685
686 if( clearMask.IsEmpty() )
687 {
688 writePcbPolygon( polygon, aLayer );
689 return;
690 }
691
692 polygon.BooleanSubtract( clearMask );
693 polygon.Fracture();
694
695 for( int ii = 0; ii < polygon.OutlineCount(); ++ii )
696 writePcbPolygon( SHAPE_POLY_SET( polygon.COutline( ii ) ), aLayer );
697}
698
699
701 const VECTOR2I& aOffset )
702{
703 // Ensure the polygon is valid:
704 if( aPolys.OutlineCount() < 1 )
705 return;
706
707 // aPolys is expected having only one outline and no hole
708 // (because it comes from a gerber file or is built from a aperture )
709 const SHAPE_LINE_CHAIN& poly = aPolys.COutline( 0 );
710
711 fprintf( m_fp, "\t(gr_poly\n\t\t(pts\n\t\t\t" );
712
713 #define MAX_COORD_CNT 4
714 int jj = MAX_COORD_CNT;
715 int cnt_max = poly.PointCount() -1;
716
717 // Do not generate last corner, if it is the same point as the first point:
718 if( poly.CPoint( 0 ) == poly.CPoint( cnt_max ) )
719 cnt_max--;
720
721 for( int ii = 0; ii <= cnt_max; ii++ )
722 {
723 if( --jj == 0 )
724 {
725 jj = MAX_COORD_CNT;
726 fprintf( m_fp, "\n\t\t\t" );
727 }
728
729 fprintf( m_fp, " (xy %s %s)",
730 FormatDouble2Str( MapToPcbUnits( poly.CPoint( ii ).x + aOffset.x ) ).c_str(),
731 FormatDouble2Str( MapToPcbUnits( -poly.CPoint( ii ).y + aOffset.y ) ).c_str() );
732 }
733
734 fprintf( m_fp, ")" );
735
736 fprintf( m_fp, "\n" );
738 fprintf( m_fp, "\t\t(fill yes) (layer %s)",
739 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
740 fprintf( m_fp, "\n\t)\n" );
741}
742
743
745{
747 polys.Simplify();
748
749 if( polys.OutlineCount() == 0 )
750 return;
751
752 fprintf( m_fp, "\t(zone (net 0) (net_name \"\") (layer %s) (tstamp 0000000) (hatch edge 0.508)\n",
753 LSET::Name( PCB_LAYER_ID( aLayer ) ).ToStdString().c_str() );
754
755 fprintf( m_fp, " (connect_pads (clearance 0.0))\n" );
756
757 fprintf( m_fp, " (min_thickness 0.1) (filled_areas_thickness no)\n"
758 " (fill (thermal_gap 0.3) (thermal_bridge_width 0.3))\n" );
759
760 // Now, write the zone outlines with holes.
761 // first polygon is the main outline, next are holes
762 // One cannot know the initial zone outline.
763 // However most of (if not all) holes are just items with clearance,
764 // not really a hole in the initial zone outline.
765 // So we build a zone outline only with no hole.
766 fprintf( m_fp, " (polygon\n (pts" );
767
768 SHAPE_LINE_CHAIN& poly = polys.Outline( 0 );
769
770 #define MAX_COORD_CNT 4
771 int jj = MAX_COORD_CNT;
772 int cnt_max = poly.PointCount() -1;
773
774 // Do not generate last corner, if it is the same point as the first point:
775 if( poly.CPoint( 0 ) == poly.CPoint( cnt_max ) )
776 cnt_max--;
777
778 for( int ii = 0; ii <= cnt_max; ii++ )
779 {
780 if( --jj == 0 )
781 {
782 jj = MAX_COORD_CNT;
783 fprintf( m_fp, "\n " );
784 }
785
786 fprintf( m_fp, " (xy %s %s)", FormatDouble2Str( MapToPcbUnits( poly.CPoint( ii ).x ) ).c_str(),
787 FormatDouble2Str( MapToPcbUnits( -poly.CPoint( ii ).y ) ).c_str() );
788 }
789
790 fprintf( m_fp, ")\n" );
791
792 fprintf( m_fp, " )\n)\n" );
793}
@ ERROR_OUTSIDE
constexpr EDA_IU_SCALE gerbIUScale
Definition base_units.h:127
BOX2< VECTOR2I > BOX2I
Definition box2.h:914
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:982
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:76
APERTURE_MACRO * GetMacro() const
Definition dcode.h:132
VECTOR2I m_Size
Horizontal and vertical dimensions.
Definition dcode.h:197
APERTURE_T m_ApertType
Aperture type ( Line, rectangle, circle, oval poly, macro )
Definition dcode.h:198
SHAPE_POLY_SET m_Polygon
Definition dcode.h:213
void ConvertShapeToPolygon(const GERBER_DRAW_ITEM *aParent)
Convert a shape to an equivalent polygon.
Definition dcode.cpp:288
Handle a drill image.
GERBER_FILE_IMAGE_LIST * m_images
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.
GBR_TO_PCB_EXPORTER(GERBER_FILE_IMAGE_LIST *aImages, wxWindow *aParent, const wxString &aFileName)
void writePcbZoneItem(const GERBER_DRAW_ITEM *aGbrItem, int aLayer)
Write a zone item to the board file.
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.
void write_trimmed_polygon(const GERBER_DRAW_ITEM *aGbrItem, size_t aIndex, int aLayer)
Write a dark region to the board file, minus the clear items drawn after it.
void export_copper_item(const GERBER_DRAW_ITEM *aGbrItem, size_t aIndex, int aLayer)
Write a track (or via) to the board file.
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 export_non_copper_item(const GERBER_DRAW_ITEM *aGbrItem, size_t aIndex, int aLayer)
Write a non copper line or arc 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.
std::vector< CLEAR_SHAPE > m_clearShapes
void collect_clear_shapes(const GERBER_FILE_IMAGE *aImage)
Convert every clear polarity item of an image to a polygon, in file order.
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 lines in m_ShapeAsPolygon 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:921
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
static const LSET & UserMask()
Definition lset.cpp:686
copper_layers_iterator copper_layers_begin() const
Definition lset.cpp:915
non_copper_layers_iterator non_copper_layers_begin() const
Definition lset.cpp:927
static const LSET & AllTechMask()
Return a mask holding all technical layers (no CU layer) on both side.
Definition lset.cpp:672
non_copper_layers_iterator non_copper_layers_end() const
Definition lset.cpp:933
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition lset.cpp:184
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.
bool IsEmpty() const
Return true if the set is empty (no polygons at all)
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
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.
void Move(const VECTOR2I &aVector) override
void Fracture(bool aSimplify=true)
Convert a set of polygons with holes to a single outline with "slits"/"fractures" connecting the oute...
SHAPE_POLY_SET CloneDropTriangulation() const
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
double Distance(const VECTOR2< extended_type > &aVector) const
Compute the distance between two vectors.
Definition vector2d.h:574
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector2d.h:281
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
This file is part of the common library.
void TransformRingToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aCentre, int aRadius, int aWidth, int aError, ERROR_LOC aErrorLoc)
Convert arcs to multiple straight segments.
void TransformArcToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd, int aWidth, int aError, ERROR_LOC aErrorLoc)
Convert arc to multiple straight segments.
void TransformOvalToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aWidth, int aError, ERROR_LOC aErrorLoc, int aMinSegCount=0)
Convert a oblong shape to a polygon, using multiple segments.
@ APT_RECT
Definition dcode.h:46
static std::string ToStdString(const wxString &aStr)
#define _(s)
@ RADIANS_T
Definition eda_angle.h:31
#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
std::vector< GERBER_DRAW_ITEM * > GERBER_DRAW_ITEMS
bool IsPcbLayer(int aLayer)
Test whether a layer is a valid layer for Pcbnew.
Definition layer_ids.h:692
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition layer_ids.h:703
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ B_Cu
Definition layer_ids.h:61
@ UNDEFINED_LAYER
Definition layer_ids.h:57
@ F_Cu
Definition layer_ids.h:60
This file contains miscellaneous commonly used macros and functions.
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:25
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
netlist clear()
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:73
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
Definition of file extensions used in Kicad.