KiCad PCB EDA Suite
Loading...
Searching...
No Matches
export_gencad_writer.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6* This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
20
21#include <build_version.h>
22#include <common.h>
23#include <board.h>
26#include <pcb_shape.h>
27#include <footprint.h>
28#include <pad.h>
29#include <pcb_track.h>
30#include <string_utils.h>
31#include <macros.h>
32#include <hash_eda.h>
33#include <set>
34#include <unordered_map>
35#include <fmt.h>
36
37
39static std::string genCADLayerName( int aCuCount, PCB_LAYER_ID aId )
40{
41 if( IsCopperLayer( aId ) )
42 {
43 if( aId == F_Cu )
44 return "TOP";
45 else if( aId == B_Cu )
46 return "BOTTOM";
47 else
48 return fmt::format( "INNER{}", CopperLayerToOrdinal( aId ) );
49 }
50
51 else
52 {
53 const char* txt;
54
55 // using a switch to clearly show mapping & catch out of bounds index.
56 switch( aId )
57 {
58 // Technicals
59 case B_Adhes: txt = "B.Adhes"; break;
60 case F_Adhes: txt = "F.Adhes"; break;
61 case B_Paste: txt = "SOLDERPASTE_BOTTOM"; break;
62 case F_Paste: txt = "SOLDERPASTE_TOP"; break;
63 case B_SilkS: txt = "SILKSCREEN_BOTTOM"; break;
64 case F_SilkS: txt = "SILKSCREEN_TOP"; break;
65 case B_Mask: txt = "SOLDERMASK_BOTTOM"; break;
66 case F_Mask: txt = "SOLDERMASK_TOP"; break;
67
68 // Users
69 case Dwgs_User: txt = "Dwgs.User"; break;
70 case Cmts_User: txt = "Cmts.User"; break;
71 case Eco1_User: txt = "Eco1.User"; break;
72 case Eco2_User: txt = "Eco2.User"; break;
73 case Edge_Cuts: txt = "Edge.Cuts"; break;
74 case Margin: txt = "Margin"; break;
75
76 // Footprint
77 case F_CrtYd: txt = "F_CrtYd"; break;
78 case B_CrtYd: txt = "B_CrtYd"; break;
79 case F_Fab: txt = "F_Fab"; break;
80 case B_Fab: txt = "B_Fab"; break;
81
82 default:
83 wxASSERT_MSG( 0, wxT( "aId UNEXPECTED" ) );
84 txt = "BAD-INDEX!"; break;
85 }
86
87 return txt;
88 }
89}
90
91
94static std::string genCADLayerNameFlipped( int aCuCount, PCB_LAYER_ID aId )
95{
96 if( IsInnerCopperLayer( aId ) )
97 return fmt::format( "INNER{}", aCuCount - 1 - CopperLayerToOrdinal( aId ) );
98
99 return genCADLayerName( aCuCount, aId );
100}
101
102
103static wxString escapeString( const wxString& aString )
104{
105 wxString copy( aString );
106 copy.Replace( wxT( "\"" ), wxT( "\\\"" ) );
107 return copy;
108}
109
110
111static std::string fmt_mask( const LSET& aSet )
112{
113 std::string retv = ( aSet & LSET::AllCuMask() ).to_string();
114 retv.erase( 0, retv.find_first_not_of( '0' ) );
115 return retv;
116}
117
118
120static std::map<FOOTPRINT*, int> componentShapes;
121static std::map<int, wxString> shapeNames;
122
123
124const wxString GENCAD_EXPORTER::getShapeName( FOOTPRINT* aFootprint )
125{
126 static const wxString invalid( "invalid" );
127
129 return aFootprint->GetReference();
130
131 auto itShape = componentShapes.find( aFootprint );
132 wxCHECK( itShape != componentShapes.end(), invalid );
133
134 auto itName = shapeNames.find( itShape->second );
135 wxCHECK( itName != shapeNames.end(), invalid );
136
137 return itName->second;
138}
139
140
141// GerbTool chokes on units different than INCH so this is the conversion factor
142const static double SCALE_FACTOR = 1000.0 * pcbIUScale.IU_PER_MILS;
143
144
146{
147 return ( aX - m_gencadOffset.x ) / SCALE_FACTOR;
148}
149
150
152{
153 return ( m_gencadOffset.y - aY ) / SCALE_FACTOR;
154}
155
156
157bool GENCAD_EXPORTER::WriteFile( const wxString& aFullFileName )
158{
159 componentShapes.clear();
160 shapeNames.clear();
161
162 m_file = wxFopen( aFullFileName, wxT( "wt" ) );
163
164 if( !m_file )
165 return false;
166
167 BOARD* pcb = m_board;
168
169 // Update some board data, to ensure a reliable GenCAD export.
170 pcb->ComputeBoundingBox( false, false );
171
172 /* Temporary modification of footprints that are flipped (i.e. on bottom
173 * layer) to convert them to non flipped footprints.
174 * This is necessary to easily export shapes to GenCAD,
175 * that are given as normal orientation (non flipped, rotation = 0))
176 * these changes will be undone later
177 */
178
179 for( FOOTPRINT* footprint : pcb->Footprints() )
180 {
181 footprint->SetFlag( 0 );
182
183 if( footprint->GetLayer() == B_Cu )
184 {
185 footprint->Flip( footprint->GetPosition(), FLIP_DIRECTION::TOP_BOTTOM );
186 footprint->SetFlag( 1 );
187 }
188 }
189
190 bool success = true;
191 try
192 {
193 /* GenCAD has some mandatory and some optional sections: some importer
194 * need the padstack section (which is optional) anyway. Also the
195 * order of the section *is* important */
196
197 createHeaderInfoData(); // GenCAD header
198 createBoardSection(); // Board perimeter
199
200 createPadsShapesSection(); // Pads and padstacks
201 createArtworksSection(); // Empty but mandatory
202
203 /* GenCAD splits a footprint information in shape, component and device.
204 * We don't do any sharing (it would be difficult since each module is
205 * customizable after placement) */
209
210 // In a similar way the netlist is split in net, track and route
214 }
215 catch( ... )
216 {
217 success = false;
218 }
219
220 fclose( m_file );
221
222 // Undo the footprints modifications (flipped footprints)
223 for( FOOTPRINT* footprint : pcb->Footprints() )
224 {
225 if( footprint->GetFlag() )
226 {
227 footprint->Flip( footprint->GetPosition(), FLIP_DIRECTION::TOP_BOTTOM );
228 footprint->SetFlag( 0 );
229 }
230 }
231
232 componentShapes.clear();
233 shapeNames.clear();
234
235 return success;
236}
237
238
240static bool viaSort( const PCB_VIA* aPadref, const PCB_VIA* aPadcmp )
241{
242 if( aPadref->GetDrillValue() != aPadcmp->GetDrillValue() )
243 return aPadref->GetDrillValue() < aPadcmp->GetDrillValue();
244
245 if( aPadref->GetLayerSet() != aPadcmp->GetLayerSet() )
246 return aPadref->GetLayerSet().FmtBin().compare( aPadcmp->GetLayerSet().FmtBin() ) < 0;
247
248 // Two vias share a padstack only when their copper matches on every layer that defines it
249 std::vector<PCB_LAYER_ID> refLayers = aPadref->Padstack().UniqueLayers();
250 std::vector<PCB_LAYER_ID> cmpLayers = aPadcmp->Padstack().UniqueLayers();
251
252 if( refLayers != cmpLayers )
253 return refLayers < cmpLayers;
254
255 for( PCB_LAYER_ID layer : refLayers )
256 {
257 if( aPadref->GetWidth( layer ) != aPadcmp->GetWidth( layer ) )
258 return aPadref->GetWidth( layer ) < aPadcmp->GetWidth( layer );
259 }
260
261 return false;
262}
263
264
266static std::string padShapeName( const std::string& aStem, const PADSTACK& aPadstack,
267 PCB_LAYER_ID aPadLayer )
268{
269 if( aPadstack.Mode() == PADSTACK::MODE::NORMAL )
270 return aStem;
271
272 return fmt::format( "{}_{}", aStem, TO_UTF8( LSET::Name( aPadLayer ) ) );
273}
274
275
276static std::string viaShapeStem( const PCB_VIA* aVia, PCB_LAYER_ID aViaLayer, const LSET& aMask )
277{
278 return fmt::format( "V{}.{}.{}", aVia->GetWidth( aViaLayer ), aVia->GetDrillValue(),
279 fmt_mask( aMask ) );
280}
281
282
284static std::string viaStackName( const PCB_VIA* aVia, const LSET& aMask )
285{
286 std::string name = "VIA";
287
289 [&]( PCB_LAYER_ID aViaLayer )
290 {
291 name += fmt::format( "{}.", aVia->GetWidth( aViaLayer ) );
292 } );
293
294 return name + fmt::format( "{}.{}", aVia->GetDrillValue(), fmt_mask( aMask ) );
295}
296
297
299static int viaNominalWidth( const PCB_VIA* aVia )
300{
301 int width = 0;
302
304 [&]( PCB_LAYER_ID aViaLayer )
305 {
306 width = std::max( width, aVia->GetWidth( aViaLayer ) );
307 } );
308
309 return width;
310}
311
312
314{
315 // The ARTWORKS section is empty but (officially) mandatory
316 fmt::print( m_file, "$ARTWORKS\n" );
317 fmt::print( m_file, "$ENDARTWORKS\n\n" );
318}
319
320
321void GENCAD_EXPORTER::writePadShape( const std::string& aName, PAD* aPad, PCB_LAYER_ID aPadLayer )
322{
323 const VECTOR2I& off = aPad->GetOffset( aPadLayer );
324
325 int dx = aPad->GetSize( aPadLayer ).x / 2;
326 int dy = aPad->GetSize( aPadLayer ).y / 2;
327
328 fmt::print( m_file, "PAD {}", aName );
329
330 switch( aPad->GetShape( aPadLayer ) )
331 {
332 default:
333 UNIMPLEMENTED_FOR( aPad->ShowPadShape( aPadLayer ) );
335
337 fmt::print( m_file, " ROUND {}\n",
338 aPad->GetDrillSize().x / SCALE_FACTOR );
339
340 /* Circle is center, radius */
341 fmt::print( m_file, "CIRCLE {} {} {}\n",
342 off.x / SCALE_FACTOR,
343 -off.y / SCALE_FACTOR,
344 aPad->GetSize( aPadLayer ).x / (SCALE_FACTOR * 2) );
345 break;
346
348 fmt::print( m_file, " RECTANGULAR {}\n",
349 aPad->GetDrillSize().x / SCALE_FACTOR );
350
351 // Rectangle is begin, size *not* begin, end!
352 fmt::print( m_file, "RECTANGLE {} {} {} {}\n",
353 (-dx + off.x ) / SCALE_FACTOR,
354 (-dy - off.y ) / SCALE_FACTOR,
355 dx / (SCALE_FACTOR / 2), dy / (SCALE_FACTOR / 2) );
356 break;
357
359 case PAD_SHAPE::OVAL:
360 {
361 const VECTOR2I& size = aPad->GetSize( aPadLayer );
362 int radius = std::min( size.x, size.y ) / 2;
363
364 if( aPad->GetShape( aPadLayer ) == PAD_SHAPE::ROUNDRECT )
365 radius = aPad->GetRoundRectCornerRadius( aPadLayer );
366
367 int lineX = size.x / 2 - radius;
368 int lineY = size.y / 2 - radius;
369
370 fmt::print( m_file, " POLYGON {}\n", aPad->GetDrillSize().x / SCALE_FACTOR );
371
372 // bottom left arc
373 fmt::print( m_file, "ARC {} {} {} {} {} {}\n",
374 ( off.x - lineX - radius ) / SCALE_FACTOR,
375 ( -off.y - lineY ) / SCALE_FACTOR,
376 ( off.x - lineX ) / SCALE_FACTOR,
377 ( -off.y - lineY - radius ) / SCALE_FACTOR,
378 ( off.x - lineX ) / SCALE_FACTOR,
379 ( -off.y - lineY ) / SCALE_FACTOR );
380
381 // bottom line
382 if( lineX > 0 )
383 {
384 fmt::print( m_file, "LINE {} {} {} {}\n",
385 ( off.x - lineX ) / SCALE_FACTOR,
386 ( -off.y - lineY - radius ) / SCALE_FACTOR,
387 ( off.x + lineX ) / SCALE_FACTOR,
388 ( -off.y - lineY - radius ) / SCALE_FACTOR );
389 }
390
391 // bottom right arc
392 fmt::print( m_file, "ARC {} {} {} {} {} {}\n",
393 ( off.x + lineX ) / SCALE_FACTOR,
394 ( -off.y - lineY - radius ) / SCALE_FACTOR,
395 ( off.x + lineX + radius ) / SCALE_FACTOR,
396 ( -off.y - lineY ) / SCALE_FACTOR,
397 ( off.x + lineX ) / SCALE_FACTOR,
398 ( -off.y - lineY ) / SCALE_FACTOR );
399
400 // right line
401 if( lineY > 0 )
402 {
403 fmt::print( m_file, "LINE {} {} {} {}\n",
404 ( off.x + lineX + radius ) / SCALE_FACTOR,
405 ( -off.y + lineY ) / SCALE_FACTOR,
406 ( off.x + lineX + radius ) / SCALE_FACTOR,
407 ( -off.y - lineY ) / SCALE_FACTOR );
408 }
409
410 // top right arc
411 fmt::print( m_file, "ARC {} {} {} {} {} {}\n",
412 ( off.x + lineX + radius ) / SCALE_FACTOR,
413 ( -off.y + lineY ) / SCALE_FACTOR,
414 ( off.x + lineX ) / SCALE_FACTOR,
415 ( -off.y + lineY + radius ) / SCALE_FACTOR,
416 ( off.x + lineX ) / SCALE_FACTOR,
417 ( -off.y + lineY ) / SCALE_FACTOR );
418
419 // top line
420 if( lineX > 0 )
421 {
422 fmt::print( m_file, "LINE {} {} {} {}\n",
423 ( off.x - lineX ) / SCALE_FACTOR,
424 ( -off.y + lineY + radius ) / SCALE_FACTOR,
425 ( off.x + lineX ) / SCALE_FACTOR,
426 ( -off.y + lineY + radius ) / SCALE_FACTOR );
427 }
428
429 // top left arc
430 fmt::print( m_file, "ARC {} {} {} {} {} {}\n",
431 ( off.x - lineX ) / SCALE_FACTOR,
432 ( -off.y + lineY + radius ) / SCALE_FACTOR,
433 ( off.x - lineX - radius ) / SCALE_FACTOR,
434 ( -off.y + lineY ) / SCALE_FACTOR,
435 ( off.x - lineX ) / SCALE_FACTOR,
436 ( -off.y + lineY ) / SCALE_FACTOR );
437
438 // left line
439 if( lineY > 0 )
440 {
441 fmt::print( m_file, "LINE {} {} {} {}\n",
442 ( off.x - lineX - radius ) / SCALE_FACTOR,
443 ( -off.y - lineY ) / SCALE_FACTOR,
444 ( off.x - lineX - radius ) / SCALE_FACTOR,
445 ( -off.y + lineY ) / SCALE_FACTOR );
446 }
447
448 break;
449 }
450
452 {
453 fmt::print( m_file, " POLYGON {}\n", aPad->GetDrillSize().x / SCALE_FACTOR );
454
455 int ddx = aPad->GetDelta( aPadLayer ).x / 2;
456 int ddy = aPad->GetDelta( aPadLayer ).y / 2;
457
458 VECTOR2I poly[4];
459 poly[0] = VECTOR2I( -dx + ddy, dy + ddx );
460 poly[1] = VECTOR2I( dx - ddy, dy - ddx );
461 poly[2] = VECTOR2I( dx + ddy, -dy + ddx );
462 poly[3] = VECTOR2I( -dx - ddy, -dy - ddx );
463
464 for( int cur = 0; cur < 4; ++cur )
465 {
466 int next = ( cur + 1 ) % 4;
467 fmt::print( m_file, "LINE {} {} {} {}\n",
468 ( off.x + poly[cur].x ) / SCALE_FACTOR,
469 ( -off.y - poly[cur].y ) / SCALE_FACTOR,
470 ( off.x + poly[next].x ) / SCALE_FACTOR,
471 ( -off.y - poly[next].y ) / SCALE_FACTOR );
472 }
473
474 break;
475 }
476
478 {
479 fmt::print( m_file, " POLYGON {}\n", aPad->GetDrillSize().x / SCALE_FACTOR );
480
481 SHAPE_POLY_SET outline;
482 VECTOR2I padOffset( 0, 0 );
483
484 TransformRoundChamferedRectToPolygon( outline, padOffset,
485 aPad->GetSize( aPadLayer ),
486 aPad->GetOrientation(),
487 aPad->GetRoundRectCornerRadius( aPadLayer ),
488 aPad->GetChamferRectRatio( aPadLayer ),
489 aPad->GetChamferPositions( aPadLayer ),
490 0, aPad->GetMaxError(), ERROR_INSIDE );
491
492 for( int jj = 0; jj < outline.OutlineCount(); ++jj )
493 {
494 const SHAPE_LINE_CHAIN& poly = outline.COutline( jj );
495 int pointCount = poly.PointCount();
496
497 for( int ii = 0; ii < pointCount; ii++ )
498 {
499 int next = ( ii + 1 ) % pointCount;
500 fmt::print( m_file, "LINE {} {} {} {}\n",
501 poly.CPoint( ii ).x / SCALE_FACTOR,
502 -poly.CPoint( ii ).y / SCALE_FACTOR,
503 poly.CPoint( next ).x / SCALE_FACTOR,
504 -poly.CPoint( next ).y / SCALE_FACTOR );
505 }
506 }
507
508 break;
509 }
510
512 {
513 fmt::print( m_file, " POLYGON {}\n", aPad->GetDrillSize().x / SCALE_FACTOR );
514
515 SHAPE_POLY_SET outline;
516 aPad->MergePrimitivesAsPolygon( aPadLayer, &outline );
517
518 for( int jj = 0; jj < outline.OutlineCount(); ++jj )
519 {
520 const SHAPE_LINE_CHAIN& poly = outline.COutline( jj );
521 int pointCount = poly.PointCount();
522
523 for( int ii = 0; ii < pointCount; ii++ )
524 {
525 int next = ( ii + 1 ) % pointCount;
526 fmt::print( m_file, "LINE {} {} {} {}\n",
527 ( off.x + poly.CPoint( ii ).x ) / SCALE_FACTOR,
528 ( -off.y - poly.CPoint( ii ).y ) / SCALE_FACTOR,
529 ( off.x + poly.CPoint( next ).x ) / SCALE_FACTOR,
530 ( -off.y - poly.CPoint( next ).y ) / SCALE_FACTOR );
531 }
532 }
533
534 break;
535 }
536 }
537}
538
539
541{
542 // Emit PADS and PADSTACKS. They are sorted and emitted uniquely.
543 // Via name is synthesized from their attributes, pads are numbered
544
545 std::vector<PAD*> padstacks;
546 std::vector<PCB_VIA*> vias;
547 std::vector<PCB_VIA*> viastacks;
548
549 padstacks.resize( 1 ); // We count pads from 1
550
551 LSEQ gc_seq = m_board->GetEnabledLayers().CuStack();
552 std::reverse(gc_seq.begin(), gc_seq.end());
553
554 // The master layermask (i.e. the enabled layers) for padstack generation
555 LSET master_layermask = m_board->GetDesignSettings().GetEnabledLayers();
556 int cu_count = m_board->GetCopperLayerCount();
557
558 fmt::print( m_file, "$PADS\n" );
559
560 // Enumerate and sort the pads
561 std::vector<PAD*> pads = m_board->GetPads();
562 std::sort( pads.begin(), pads.end(), []( const PAD* a, const PAD* b )
563 {
564 return PAD::Compare( a, b ) < 0;
565 } );
566
567 // The same for vias
568 for( PCB_TRACK* track : m_board->Tracks() )
569 {
570 if( PCB_VIA* via = dyn_cast<PCB_VIA*>( track ) )
571 vias.push_back( via );
572 }
573
574 std::sort( vias.begin(), vias.end(), viaSort );
575 vias.erase( std::unique( vias.begin(), vias.end(), []( const PCB_VIA* a, const PCB_VIA* b )
576 {
577 return viaSort( a, b ) == false;
578 } ),
579 vias.end() );
580
581 // Emit vias pads
582 for( PCB_VIA* via : vias )
583 {
584 LSET mask = via->GetLayerSet() & master_layermask;
585
586 viastacks.push_back( via );
587
588 // One shape per distinct diameter. The name already separates them, so layers of
589 // equal width share an entry
590 std::set<std::string> emitted;
591
592 via->Padstack().ForEachUniqueLayer(
593 [&]( PCB_LAYER_ID aViaLayer )
594 {
595 std::string stem = viaShapeStem( via, aViaLayer, mask );
596
597 if( !emitted.insert( stem ).second )
598 return;
599
600 fmt::print( m_file, "PAD {} ROUND {}\nCIRCLE 0 0 {}\n",
601 stem,
602 via->GetDrillValue() / SCALE_FACTOR,
603 via->GetWidth( aViaLayer ) / (SCALE_FACTOR * 2) );
604 } );
605 }
606
607 // Emit component pads
608 PAD* old_pad = nullptr;
609 int pad_name_number = 0;
610
611 for( unsigned i = 0; i<pads.size(); ++i )
612 {
613 PAD* pad = pads[i];
614
615 pad->SetSubRatsnest( pad_name_number );
616
617 // @warning: This code is not 100% correct. The #PAD::Compare function does not test
618 // custom pad primitives so there may be duplicate custom pads in the export.
619 if( old_pad && 0 == PAD::Compare( old_pad, pad ) )
620 continue;
621
622 old_pad = pad;
623
624 pad_name_number++;
625 pad->SetSubRatsnest( pad_name_number );
626
627 padstacks.push_back( pad ); // Will have its own padstack later
628
629 // One shape per unique layer, which the PADSTACK entry then references layer by layer
630 std::string stem = fmt::format( "P{}", pad->GetSubRatsnest() );
631
632 pad->Padstack().ForEachUniqueLayer(
633 [&]( PCB_LAYER_ID aPadLayer )
634 {
635 writePadShape( padShapeName( stem, pad->Padstack(), aPadLayer ), pad,
636 aPadLayer );
637 } );
638 }
639
640 fmt::print( m_file, "\n$ENDPADS\n\n" );
641
642 // Now emit the padstacks definitions, using the combined layer masks
643 fmt::print( m_file, "$PADSTACKS\n" );
644
645 // Via padstacks
646 for( unsigned i = 0; i < viastacks.size(); i++ )
647 {
648 PCB_VIA* via = viastacks[i];
649
650 LSET mask = via->GetLayerSet() & master_layermask;
651
652 fmt::print( m_file, "PADSTACK {} {}\n",
653 viaStackName( via, mask ),
654 via->GetDrillValue() / SCALE_FACTOR );
655
656 for( PCB_LAYER_ID layer : mask.Seq( gc_seq ) )
657 {
658 fmt::print( m_file, "PAD {} {} 0 0\n",
659 viaShapeStem( via, via->Padstack().EffectiveLayerFor( layer ), mask ),
660 genCADLayerName( cu_count, layer ).c_str() );
661 }
662 }
663
664 /* Component padstacks
665 * Older versions of CAM350 don't apply correctly the FLIP semantics for
666 * padstacks, i.e. doesn't swap the top and bottom layers... so I need to
667 * define the shape as MIRRORX and define a separate 'flipped' padstack...
668 * until it appears yet another non-compliant importer */
669 for( unsigned i = 1; i < padstacks.size(); i++ )
670 {
671 PAD* pad = padstacks[i];
672
673 // Straight padstack
674 fmt::print( m_file, "PADSTACK PAD{} {}\n",
675 i,
676 pad->GetDrillSize().x / SCALE_FACTOR );
677
678 LSET pad_set = pad->GetLayerSet() & master_layermask;
679 std::string stem = fmt::format( "P{}", i );
680
681 // the special gc_seq
682 for( PCB_LAYER_ID layer : pad_set.Seq( gc_seq ) )
683 {
684 fmt::print( m_file, "PAD {} {} 0 0\n",
685 padShapeName( stem, pad->Padstack(),
686 pad->Padstack().EffectiveLayerFor( layer ) ),
687 genCADLayerName( cu_count, layer ).c_str() );
688 }
689
690 // Flipped padstack
691 if( m_flipBottomPads )
692 {
693 fmt::print( m_file, "PADSTACK PAD{}F {}\n",
694 i,
695 pad->GetDrillSize().x / SCALE_FACTOR );
696
697 // the normal PCB_LAYER_ID sequence is inverted from gc_seq[]
698 for( PCB_LAYER_ID layer : pad_set.Seq() )
699 {
700 fmt::print( m_file, "PAD {} {} 0 0\n",
701 padShapeName( stem, pad->Padstack(),
702 pad->Padstack().EffectiveLayerFor( layer ) ),
703 genCADLayerNameFlipped( cu_count, layer ).c_str() );
704 }
705 }
706 }
707
708 fputs( "$ENDPADSTACKS\n\n", m_file );
709}
710
711
713static size_t hashFootprint( const FOOTPRINT* aFootprint )
714{
715 size_t ret = 0x11223344;
716 constexpr int flags = HASH_FLAGS::HASH_POS | HASH_FLAGS::REL_COORD
718
719 for( BOARD_ITEM* i : aFootprint->GraphicalItems() )
720 ret += hash_fp_item( i, flags );
721
722 for( PAD* i : aFootprint->Pads() )
723 ret += hash_fp_item( i, flags );
724
725 return ret;
726}
727
728
730{
731 const char* layer;
732 wxString pinname;
733 const char* mirror = "0";
734 std::map<wxString, size_t> shapes;
735
736 fmt::print( m_file, "$SHAPES\n" );
737
738 for( FOOTPRINT* footprint : m_board->Footprints() )
739 {
741 {
742 // Check if such shape has been already generated, and if so - reuse it
743 // It is necessary to compute hash (i.e. check all children objects) as
744 // certain components instances might have been modified on the board.
745 // In such case the shape will be different despite the same LIB_ID.
746 wxString shapeName = footprint->GetFPID().Format();
747
748 auto shapeIt = shapes.find( shapeName );
749 size_t modHash = hashFootprint( footprint );
750
751 if( shapeIt != shapes.end() )
752 {
753 if( modHash != shapeIt->second )
754 {
755 // there is an entry for this footprint, but it has a modified shape,
756 // so we need to create a new entry
757 wxString newShapeName;
758 int suffix = 0;
759
760 // find an unused name or matching entry
761 do
762 {
763 newShapeName = wxString::Format( wxT( "%s_%d" ), shapeName, suffix );
764 shapeIt = shapes.find( newShapeName );
765 ++suffix;
766 }
767 while( shapeIt != shapes.end() && shapeIt->second != modHash );
768
769 shapeName = newShapeName;
770 }
771
772 if( shapeIt != shapes.end() && modHash == shapeIt->second )
773 {
774 // shape found, so reuse it
775 componentShapes[footprint] = modHash;
776 continue;
777 }
778 }
779
780 // new shape
781 componentShapes[footprint] = modHash;
782 shapeNames[modHash] = shapeName;
783 shapes[shapeName] = modHash;
784 footprintWriteShape( footprint, shapeName );
785 }
786 else // individual shape for each component
787 {
788 footprintWriteShape( footprint, footprint->GetReference() );
789 }
790
791 // set of already emitted pins to check for duplicates
792 std::set<wxString> pins;
793
794 for( PAD* pad : footprint->Pads() )
795 {
796 /* Padstacks are defined using the correct layers for the pads, therefore to
797 * all pads need to be marked as TOP to use the padstack information correctly.
798 */
799 layer = "TOP";
800 pinname = pad->GetNumber();
801
802 if( pinname.IsEmpty() )
803 pinname = wxT( "none" );
804
805 if( m_useUniquePins )
806 {
807 int suffix = 0;
808 wxString origPinname( pinname );
809
810 auto it = pins.find( pinname );
811
812 while( it != pins.end() )
813 {
814 pinname = wxString::Format( wxT( "%s_%d" ), origPinname, suffix );
815 ++suffix;
816 it = pins.find( pinname );
817 }
818
819 pins.insert( pinname );
820 }
821
822 EDA_ANGLE orient = pad->GetOrientation() - footprint->GetOrientation();
823 orient.Normalize();
824
825 VECTOR2I padPos = pad->GetFPRelativePosition();
826
827 std::string flipStr = ( m_flipBottomPads && footprint->GetFlag() ) ? "F" : "";
828
829 // Bottom side footprints use the flipped padstack
830 fmt::print( m_file,
831 "PIN \"{}\" PAD{}{} {} {} {} {} {}\n",
832 TO_UTF8( escapeString( pinname ) ),
833 pad->GetSubRatsnest(),
834 flipStr,
835 padPos.x / SCALE_FACTOR,
836 -padPos.y / SCALE_FACTOR,
837 layer,
838 orient.AsDegrees(),
839 mirror );
840 }
841 }
842
843 fmt::print( m_file, "$ENDSHAPES\n\n" );
844}
845
846
848{
849 fmt::print( m_file, "$COMPONENTS\n" );
850
851 int cu_count = m_board->GetCopperLayerCount();
852
853 for( FOOTPRINT* footprint : m_board->Footprints() )
854 {
855 const char* mirror;
856 const char* flip;
857 EDA_ANGLE fp_orient = footprint->GetOrientation();
858
859 if( footprint->GetFlag() )
860 {
861 mirror = "MIRRORX";
862 flip = "FLIP";
863 fp_orient = fp_orient.Invert().Normalize();
864 }
865 else
866 {
867 mirror = "0";
868 flip = "0";
869 }
870
871 fmt::print( m_file, "\nCOMPONENT \"{}\"\n",
872 TO_UTF8( escapeString( footprint->GetReference() ) ) );
873 fmt::print( m_file, "DEVICE \"DEV_{}\"\n",
874 TO_UTF8( escapeString( getShapeName( footprint ) ) ) );
875 fmt::print( m_file, "PLACE {} {}\n",
876 mapXTo( footprint->GetPosition().x ),
877 mapYTo( footprint->GetPosition().y ) );
878 fmt::print( m_file, "LAYER {}\n",
879 footprint->GetFlag() ? "BOTTOM" : "TOP" );
880 fmt::print( m_file, "ROTATION {}\n",
881 fp_orient.AsDegrees() );
882 fmt::print( m_file, "SHAPE \"{}\" {} {}\n",
883 TO_UTF8( escapeString( getShapeName( footprint ) ) ),
884 mirror, flip );
885
886 // Text on silk layer: RefDes and value (are they actually useful?)
887 for( PCB_TEXT* textItem : { &footprint->Reference(), &footprint->Value() } )
888 {
889 std::string layer = genCADLayerName( cu_count, footprint->GetFlag() ? B_SilkS : F_SilkS );
890
891 fmt::print( m_file, "TEXT {} {} {} {} {} {} \"{}\"",
892 textItem->GetFPRelativePosition().x / SCALE_FACTOR,
893 -textItem->GetFPRelativePosition().y / SCALE_FACTOR,
894 textItem->GetTextWidth() / SCALE_FACTOR,
895 textItem->GetTextAngle().AsDegrees(),
896 mirror,
897 layer.c_str(),
898 TO_UTF8( escapeString( textItem->GetText() ) ) );
899
900 BOX2I textBox = textItem->GetTextBox( nullptr );
901
902 fmt::print( m_file, " 0 0 {} {}\n",
903 textBox.GetWidth() / SCALE_FACTOR,
904 textBox.GetHeight() / SCALE_FACTOR );
905 }
906
907 // The SHEET is a 'generic description' for referencing the component
908 fmt::print( m_file, "SHEET \"RefDes: {}, Value: {}\"\n",
909 TO_UTF8( footprint->GetReference() ),
910 TO_UTF8( footprint->GetValue() ) );
911 }
912
913 fmt::print( m_file, "$ENDCOMPONENTS\n\n" );
914}
915
916
918{
919 // Emit the netlist (which is actually the thing for which GenCAD is used these
920 // days!); tracks are handled later
921
922 std::unordered_map<int, std::vector<std::pair<FOOTPRINT*, PAD*>>> padsByNet;
923
924 for( FOOTPRINT* footprint : m_board->Footprints() )
925 {
926 for( PAD* pad : footprint->Pads() )
927 {
928 if( pad->GetNetCode() > 0 )
929 padsByNet[pad->GetNetCode()].emplace_back( footprint, pad );
930 }
931 }
932
933 wxString msg;
934 NETINFO_ITEM* net;
935
936 fmt::print( m_file, "$SIGNALS\n" );
937
938 for( unsigned ii = 0; ii < m_board->GetNetCount(); ii++ )
939 {
940 net = m_board->FindNet( ii );
941
942 if( net )
943 {
944 if( net->GetNetCode() <= 0 ) // dummy netlist (no connection)
945 continue;
946
947 msg = wxT( "SIGNAL \"" ) + escapeString( net->GetNetname() ) + wxT( "\"" );
948
949 fmt::print( m_file, "{}", TO_UTF8( msg ) );
950 fmt::print( m_file, "\n" );
951
952 auto pads = padsByNet.find( net->GetNetCode() );
953
954 if( pads == padsByNet.end() )
955 continue;
956
957 for( const auto& [footprint, pad] : pads->second )
958 {
959 msg.Printf( wxT( "NODE \"%s\" \"%s\"" ),
960 escapeString( footprint->GetReference() ),
961 escapeString( pad->GetNumber() ) );
962
963 fmt::print( m_file, "{}", TO_UTF8( msg ) );
964 fmt::print( m_file, "\n" );
965 }
966 }
967 }
968
969 fmt::print( m_file, "$ENDSIGNALS\n\n" );
970}
971
972
974{
975 fmt::print( m_file, "$HEADER\n" );
976 fmt::print( m_file, "GENCAD 1.4\n" );
977
978 // Please note: GenCAD syntax requires quoted strings if they can contain spaces
979 fmt::print( m_file, "USER \"KiCad {}\"\n", GetBuildVersion() );
980
981 fmt::print( m_file, "DRAWING \"{}\"\n", m_board->GetFileName() );
982
983 wxString rev = ExpandTextVars( m_board->GetTitleBlock().GetRevision(), m_board->GetProject(), FOR_GUI );
984 wxString date = ExpandTextVars( m_board->GetTitleBlock().GetDate(), m_board->GetProject(), FOR_GUI );
985
986 fmt::print( m_file, "REVISION \"{} {}\"\n", rev, date );
987 fmt::print( m_file, "UNITS INCH\n" );
988
989 // giving 0 as the argument to Map{X,Y}To returns the scaled origin point
990 fmt::print( m_file, "ORIGIN {} {}\n", m_storeOriginCoords ? mapXTo( 0 ) : 0,
991 m_storeOriginCoords ? mapYTo( 0 ) : 0 );
992
993 fmt::print( m_file, "INTERTRACK 0\n" );
994 fmt::print( m_file, "$ENDHEADER\n\n" );
995
996 return true;
997}
998
999
1001{
1002 int vianum = 1;
1003 int old_netcode, old_width, old_layer;
1004 LSET master_layermask = m_board->GetDesignSettings().GetEnabledLayers();
1005 int cu_count = m_board->GetCopperLayerCount();
1006 TRACKS tracks( m_board->Tracks() );
1007
1008 std::sort( tracks.begin(), tracks.end(),
1009 []( const PCB_TRACK* a, const PCB_TRACK* b )
1010 {
1011 int widthA = 0;
1012 int widthB = 0;
1013
1014 if( a->Type() == PCB_VIA_T )
1015 widthA = viaNominalWidth( static_cast<const PCB_VIA*>( a ) );
1016 else
1017 widthA = a->GetWidth();
1018
1019 if( b->Type() == PCB_VIA_T )
1020 widthB = viaNominalWidth( static_cast<const PCB_VIA*>( b ) );
1021 else
1022 widthB = b->GetWidth();
1023
1024 if( a->GetNetCode() == b->GetNetCode() )
1025 {
1026 if( widthA == widthB )
1027 return ( a->GetLayer() < b->GetLayer() );
1028
1029 return ( widthA < widthB );
1030 }
1031
1032 return ( a->GetNetCode() < b->GetNetCode() );
1033 } );
1034
1035 fmt::print( m_file, "$ROUTES\n" );
1036
1037 old_netcode = -1;
1038 old_width = -1;
1039 old_layer = -1;
1040
1041 for( PCB_TRACK* track : tracks )
1042 {
1043 if( old_netcode != track->GetNetCode() )
1044 {
1045 old_netcode = track->GetNetCode();
1046 NETINFO_ITEM* net = track->GetNet();
1047 wxString netname;
1048
1049 if( net && (net->GetNetname() != wxEmptyString) )
1050 netname = net->GetNetname();
1051 else
1052 netname = wxT( "_noname_" );
1053
1054 fmt::print( m_file, "ROUTE \"{}\"\n", TO_UTF8( escapeString( netname ) ) );
1055 }
1056
1057 int currentWidth = 0;
1058
1059 if( track->Type() == PCB_VIA_T )
1060 currentWidth = viaNominalWidth( static_cast<const PCB_VIA*>( track ) );
1061 else
1062 currentWidth = track->GetWidth();
1063
1064 if( old_width != currentWidth )
1065 {
1066 old_width = currentWidth;
1067 fmt::print( m_file, "TRACK TRACK{}\n", currentWidth );
1068 }
1069
1070 if( track->Type() == PCB_TRACE_T )
1071 {
1072 if( old_layer != track->GetLayer() )
1073 {
1074 old_layer = track->GetLayer();
1075 fmt::print( m_file, "LAYER {}\n",
1076 genCADLayerName( cu_count, track->GetLayer() ).c_str() );
1077 }
1078
1079 fmt::print( m_file, "LINE {} {} {} {}\n",
1080 mapXTo( track->GetStart().x ), mapYTo( track->GetStart().y ),
1081 mapXTo( track->GetEnd().x ), mapYTo( track->GetEnd().y ) );
1082 }
1083 else if( track->Type() == PCB_ARC_T )
1084 {
1085 if( old_layer != track->GetLayer() )
1086 {
1087 old_layer = track->GetLayer();
1088 fmt::print( m_file, "LAYER {}\n",
1089 genCADLayerName( cu_count, track->GetLayer() ).c_str() );
1090 }
1091
1092 VECTOR2I start = track->GetStart();
1093 VECTOR2I end = track->GetEnd();
1094
1095 const PCB_ARC* arc = static_cast<const PCB_ARC*>( track );
1096
1097 // GenCAD arcs are always drawn counter-clockwise (IsCCW works backwards because Y-axis is up in GenCAD).
1098 if( arc->IsCCW() )
1099 std::swap( start, end );
1100
1101 VECTOR2I center = arc->GetCenter();
1102
1103 fmt::print( m_file, "ARC {} {} {} {} {} {}\n",
1104 mapXTo( start.x ), mapYTo( start.y ),
1105 mapXTo( end.x ), mapYTo( end.y ),
1106 mapXTo( center.x ), mapYTo( center.y ) );
1107 }
1108 else if( track->Type() == PCB_VIA_T )
1109 {
1110 const PCB_VIA* via = static_cast<const PCB_VIA*>( track );
1111
1112 LSET vset = via->GetLayerSet() & master_layermask;
1113
1114 fmt::print( m_file, "VIA {} {} {} ALL {} via{}\n",
1115 viaStackName( via, vset ),
1116 mapXTo( via->GetStart().x ), mapYTo( via->GetStart().y ),
1117 via->GetDrillValue() / SCALE_FACTOR,
1118 vianum++ );
1119 }
1120 }
1121
1122 fmt::print( m_file, "$ENDROUTES\n\n" );
1123}
1124
1125
1127{
1128 std::set<wxString> emitted;
1129
1130 fmt::print( m_file, "$DEVICES\n" );
1131
1132 // componentShapes (as a std::map<>) does not give the same order for items between 2 runs.
1133 // This is annoying when one want to compare 2 similar files.
1134 // Therefore we store the strings in a wxArrayString, and after created, strings will be sorted.
1135 // This is not perfect, because the selected footprint used to create the DEVICE section is
1136 // not always the same between runs, but this is much better than no sort
1137 wxArrayString data;
1138
1139 for( const auto& componentShape : componentShapes )
1140 {
1141 const wxString& shapeName = shapeNames[componentShape.second];
1142 bool newDevice;
1143 std::tie( std::ignore, newDevice ) = emitted.insert( shapeName );
1144
1145 if( !newDevice ) // do not repeat device definitions
1146 continue;
1147
1148 const FOOTPRINT* footprint = componentShape.first;
1149
1150 wxString txt;
1151 txt.Printf( "\nDEVICE \"DEV_%s\"\n", escapeString( shapeName ) );
1152 txt += wxString::Format( "PART \"%s\"\n", escapeString( footprint->GetValue() ) );
1153 txt += wxString::Format( "PACKAGE \"%s\"\n", escapeString( footprint->GetFPID().Format() ) );
1154
1155 data.Add( txt );
1156 }
1157
1158 data.Sort();
1159
1160 for( wxString& item : data )
1161 fmt::print( m_file, "{}", TO_UTF8( item ) );
1162
1163 fmt::print( m_file, "$ENDDEVICES\n\n" );
1164}
1165
1166
1168{
1169 // Creates the section $BOARD.
1170 // We output here only the board perimeter
1171 fmt::print( m_file, "$BOARD\n" );
1172
1173 // Extract the board edges
1174 SHAPE_POLY_SET outline;
1175
1176 if( !m_board->GetBoardPolygonOutlines( outline, true ) )
1177 wxLogError( _( "Board outline is malformed. Run DRC for a full analysis." ) );
1178
1179 for( auto seg1 = outline.IterateSegmentsWithHoles(); seg1; seg1++ )
1180 {
1181 SEG seg = *seg1;
1182 fmt::print( m_file, "LINE {} {} {} {}\n",
1183 mapXTo( seg.A.x ), mapYTo( seg.A.y ),
1184 mapXTo( seg.B.x ), mapYTo( seg.B.y ) );
1185 }
1186
1187 fmt::print( m_file, "$ENDBOARD\n\n" );
1188}
1189
1190
1192{
1193 // Find thickness used for traces
1194 std::set<int> trackinfo;
1195
1196 for( PCB_TRACK* track : m_board->Tracks() )
1197 {
1198 if( track->Type() == PCB_VIA_T )
1199 continue;
1200
1201 trackinfo.insert( track->GetWidth() );
1202 }
1203
1204 // Write data
1205 fmt::print( m_file, "$TRACKS\n" );
1206
1207 for( int size : trackinfo )
1208 fmt::print( m_file, "TRACK TRACK{} {}\n", size, size / SCALE_FACTOR );
1209
1210 fmt::print( m_file, "$ENDTRACKS\n\n" );
1211}
1212
1213
1214void GENCAD_EXPORTER::footprintWriteShape( FOOTPRINT* aFootprint, const wxString& aShapeName )
1215{
1216 /* creates header: */
1217 fmt::print( m_file, "\nSHAPE \"{}\"\n", TO_UTF8( escapeString( aShapeName ) ) );
1218
1219 if( aFootprint->GetAttributes() & FP_THROUGH_HOLE )
1220 fmt::print( m_file, "INSERT TH\n" );
1221 else
1222 fmt::print( m_file, "INSERT SMD\n" );
1223
1224 // Silk outline; wildly interpreted by various importers:
1225 // CAM350 read it right but only closed shapes
1226 // ProntoPlace double-flip it (at least the pads are correct)
1227 // GerberTool usually get it right...
1228 for( BOARD_ITEM* item : aFootprint->GraphicalItems() )
1229 {
1230 if( item->Type() == PCB_SHAPE_T && ( item->GetLayer() == F_SilkS || item->GetLayer() == B_SilkS ) )
1231 {
1232 PCB_SHAPE* shape = static_cast<PCB_SHAPE*>( item );
1233 VECTOR2I start = shape->GetStart() - aFootprint->GetPosition();
1234 VECTOR2I end = shape->GetEnd() - aFootprint->GetPosition();
1235 VECTOR2I center = shape->GetCenter() - aFootprint->GetPosition();
1236
1237 RotatePoint( start, -aFootprint->GetOrientation() );
1238 RotatePoint( end, -aFootprint->GetOrientation() );
1239 RotatePoint( center, -aFootprint->GetOrientation() );
1240
1241 switch( shape->GetShape() )
1242 {
1243 case SHAPE_T::SEGMENT:
1244 fmt::print( m_file, "LINE {} {} {} {}\n",
1245 start.x / SCALE_FACTOR,
1246 -start.y / SCALE_FACTOR,
1247 end.x / SCALE_FACTOR,
1248 -end.y / SCALE_FACTOR );
1249 break;
1250
1251 case SHAPE_T::RECTANGLE:
1252 fmt::print( m_file, "LINE {} {} {} {}\n",
1253 start.x / SCALE_FACTOR,
1254 -start.y / SCALE_FACTOR,
1255 end.x / SCALE_FACTOR,
1256 -end.y / SCALE_FACTOR );
1257 fmt::print( m_file, "LINE {} {} {} {}\n",
1258 end.x / SCALE_FACTOR,
1259 -start.y / SCALE_FACTOR,
1260 end.x / SCALE_FACTOR,
1261 -end.y / SCALE_FACTOR );
1262 fmt::print( m_file, "LINE {} {} {} {}\n",
1263 end.x / SCALE_FACTOR,
1264 -end.y / SCALE_FACTOR,
1265 start.x / SCALE_FACTOR,
1266 -end.y / SCALE_FACTOR );
1267 fmt::print( m_file, "LINE {} {} {} {}\n",
1268 start.x / SCALE_FACTOR,
1269 -end.y / SCALE_FACTOR,
1270 start.x / SCALE_FACTOR,
1271 -start.y / SCALE_FACTOR );
1272 break;
1273
1274 case SHAPE_T::CIRCLE:
1275 {
1276 int radius = KiROUND( end.Distance( start ) );
1277
1278 fmt::print( m_file, "CIRCLE {} {} {}\n",
1279 start.x / SCALE_FACTOR,
1280 -start.y / SCALE_FACTOR,
1281 radius / SCALE_FACTOR );
1282 break;
1283 }
1284
1285 case SHAPE_T::ARC:
1286 if( shape->GetArcAngle() > ANGLE_0 )
1287 std::swap( start, end );
1288
1289 fmt::print( m_file, "ARC {} {} {} {} {} {}\n",
1290 start.x / SCALE_FACTOR,
1291 -start.y / SCALE_FACTOR,
1292 end.x / SCALE_FACTOR,
1293 -end.y / SCALE_FACTOR,
1294 center.x / SCALE_FACTOR,
1295 -center.y / SCALE_FACTOR );
1296 break;
1297
1298 case SHAPE_T::POLY:
1299 // Not exported (TODO)
1300 break;
1301
1302 default:
1303 wxFAIL_MSG( wxString::Format( wxT( "Shape type %d invalid." ), item->Type() ) );
1304 break;
1305 }
1306 }
1307 }
1308}
const char * name
@ ERROR_INSIDE
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
BOX2< VECTOR2I > BOX2I
Definition box2.h:914
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:982
wxString GetBuildVersion()
Get the full KiCad version string.
std::string FmtBin() const
Return a binary string showing contents of this set.
Definition base_set.h:286
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
int GetMaxError() const
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:410
const FOOTPRINTS & Footprints() const
Definition board.h:464
BOX2I ComputeBoundingBox(bool aBoardEdgesOnly=false, bool aPhysicalLayersOnly=false) const
Calculate the bounding box containing all board items (or board edge segments).
Definition board.cpp:2742
constexpr size_type GetWidth() const
Definition box2.h:211
constexpr size_type GetHeight() const
Definition box2.h:212
EDA_ANGLE Normalize()
Definition eda_angle.h:228
double AsDegrees() const
Definition eda_angle.h:115
EDA_ANGLE Invert() const
Definition eda_angle.h:172
EDA_ANGLE GetArcAngle() const
SHAPE_T GetShape() const
Definition eda_shape.h:175
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:325
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:275
EDA_ANGLE GetOrientation() const
Definition footprint.h:439
std::deque< PAD * > & Pads()
Definition footprint.h:405
int GetAttributes() const
Definition footprint.h:551
const LIB_ID & GetFPID() const
Definition footprint.h:474
const wxString & GetValue() const
Definition footprint.h:933
const wxString & GetReference() const
Definition footprint.h:909
VECTOR2I GetPosition() const override
Definition footprint.h:436
DRAWINGS & GraphicalItems()
Definition footprint.h:408
void createRoutesSection()
Create the $ROUTES section.
void createTracksInfoData()
Create the "$TRACKS" section.
void createShapesSection()
Create the footprint shape list.
const wxString getShapeName(FOOTPRINT *aFootprint)
void createDevicesSection()
Create the $DEVICES section.
bool createHeaderInfoData()
Creates the header section.
double mapXTo(int aX)
Helper functions to calculate coordinates of footprints in GenCAD values.
void footprintWriteShape(FOOTPRINT *aFootprint, const wxString &aShapeName)
Create the shape of a footprint (SHAPE section)
void writePadShape(const std::string &aName, PAD *aPad, PCB_LAYER_ID aPadLayer)
Write one "PAD" entry for the copper aPad carries on aPadLayer, named aName as referenced from a PADS...
bool WriteFile(const wxString &aFullFileName)
Export a GenCAD file.
void createComponentsSection()
Create the $COMPONENTS GenCAD section.
UTF8 Format() const
Definition lib_id.cpp:132
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition lseq.h:47
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition lset.cpp:309
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition lset.cpp:184
Handle the data for a net.
Definition netinfo.h:50
const wxString & GetNetname() const
Definition netinfo.h:110
int GetNetCode() const
Definition netinfo.h:104
A PADSTACK defines the characteristics of a single or multi-layer pad, in the IPC sense of the word.
Definition padstack.h:156
void ForEachUniqueLayer(const std::function< void(PCB_LAYER_ID)> &aMethod) const
Runs the given callable for each active unique copper layer in this padstack, meaning F_Cu for MODE::...
std::vector< PCB_LAYER_ID > UniqueLayers() const
@ NORMAL
Shape is the same on all layers.
Definition padstack.h:170
MODE Mode() const
Definition padstack.h:344
Definition pad.h:61
static wxString ShowPadShape(PAD_SHAPE aShape)
Definition pad.cpp:2559
void MergePrimitivesAsPolygon(PCB_LAYER_ID aLayer, SHAPE_POLY_SET *aMergedPolygon, ERROR_LOC aErrorLoc=ERROR_INSIDE) const
Merge all basic shapes to a SHAPE_POLY_SET.
Definition pad.cpp:3751
int GetRoundRectCornerRadius(PCB_LAYER_ID aLayer) const
Definition pad.cpp:1182
static int Compare(const PAD *aPadRef, const PAD *aPadCmp)
Compare two pads and return 0 if they are equal.
Definition pad.cpp:2483
const VECTOR2I & GetDelta(PCB_LAYER_ID aLayer) const
Definition pad.h:307
VECTOR2I GetOffset(PCB_LAYER_ID aLayer) const
Definition pad.cpp:838
VECTOR2I GetDrillSize() const
Definition pad.h:320
PAD_SHAPE GetShape(PCB_LAYER_ID aLayer) const
Definition pad.h:205
VECTOR2I GetSize(PCB_LAYER_ID aLayer) const
Definition pad.cpp:298
EDA_ANGLE GetOrientation() const
Return the rotation angle of the pad.
Definition pad.cpp:1757
int GetChamferPositions(PCB_LAYER_ID aLayer) const
Definition pad.h:852
double GetChamferRectRatio(PCB_LAYER_ID aLayer) const
Definition pad.h:834
bool IsCCW() const
virtual VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition pcb_track.h:302
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition pcb_shape.h:78
const PADSTACK & Padstack() const
Definition pcb_track.h:427
int GetWidth() const override
int GetDrillValue() const
Calculate the drill value for vias (m_drill if > 0, or default drill value for the board).
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition seg.h:38
VECTOR2I A
Definition seg.h:45
VECTOR2I B
Definition seg.h:46
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.
Represent a set of closed polygons.
int OutlineCount() const
Return the number of outlines in the set.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
SEGMENT_ITERATOR IterateSegmentsWithHoles()
Returns an iterator object, for all outlines in the set (with holes)
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, RESOLUTION_CONTEXT aContext)
Definition common.cpp:60
@ FOR_GUI
Definition common.h:91
void TransformRoundChamferedRectToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aPosition, const VECTOR2I &aSize, const EDA_ANGLE &aRotation, int aCornerRadius, double aChamferRatio, int aChamferCorners, int aInflate, int aError, ERROR_LOC aErrorLoc)
Convert a rectangle with rounded corners and/or chamfered corners to a polygon.
#define SCALE_FACTOR(x)
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:448
@ SEGMENT
Definition eda_shape.h:56
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:57
static std::string genCADLayerNameFlipped(int aCuCount, PCB_LAYER_ID aId)
The flipped layer name for GenCAD export (to make CAM350 imports correct).
static std::map< int, wxString > shapeNames
static std::string genCADLayerName(int aCuCount, PCB_LAYER_ID aId)
Layer names for GenCAD export.
static std::string viaShapeStem(const PCB_VIA *aVia, PCB_LAYER_ID aViaLayer, const LSET &aMask)
static std::string viaStackName(const PCB_VIA *aVia, const LSET &aMask)
The name carries every distinct diameter, or two vias that differ on one inner layer collide.
static std::string padShapeName(const std::string &aStem, const PADSTACK &aPadstack, PCB_LAYER_ID aPadLayer)
A uniform padstack keeps the bare name, so it still reads as one shape and not as a stack.
static size_t hashFootprint(const FOOTPRINT *aFootprint)
Compute hashes for footprints without taking into account their position, rotation or layer.
static int viaNominalWidth(const PCB_VIA *aVia)
The padstack describes the copper, so the one width a route entry holds is the widest.
static std::map< FOOTPRINT *, int > componentShapes
Association between shape names (using shapeName index) and components.
static std::string fmt_mask(const LSET &aSet)
static bool viaSort(const PCB_VIA *aPadref, const PCB_VIA *aPadcmp)
Sort vias for uniqueness.
static wxString escapeString(const wxString &aString)
@ FP_THROUGH_HOLE
Definition footprint.h:86
size_t hash_fp_item(const EDA_ITEM *aItem, int aFlags)
Calculate hash of an EDA_ITEM.
Definition hash_eda.cpp:54
Hashing functions for EDA_ITEMs.
@ HASH_POS
Definition hash_eda.h:43
@ REL_COORD
Use coordinates relative to the parent object.
Definition hash_eda.h:46
@ HASH_LAYER
Definition hash_eda.h:51
@ HASH_ROT
Definition hash_eda.h:50
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition idf_helpers.h:37
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition layer_ids.h:703
size_t CopperLayerToOrdinal(PCB_LAYER_ID aLayer)
Converts KiCad copper layer enum to an ordinal between the front and back layers.
Definition layer_ids.h:945
bool IsInnerCopperLayer(int aLayerId)
Test whether a layer is an inner (In1_Cu to In30_Cu) copper layer.
Definition layer_ids.h:725
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ F_CrtYd
Definition layer_ids.h:112
@ B_Adhes
Definition layer_ids.h:99
@ Edge_Cuts
Definition layer_ids.h:108
@ Dwgs_User
Definition layer_ids.h:103
@ F_Paste
Definition layer_ids.h:100
@ Cmts_User
Definition layer_ids.h:104
@ F_Adhes
Definition layer_ids.h:98
@ B_Mask
Definition layer_ids.h:94
@ B_Cu
Definition layer_ids.h:61
@ Eco1_User
Definition layer_ids.h:105
@ F_Mask
Definition layer_ids.h:93
@ B_Paste
Definition layer_ids.h:101
@ F_Fab
Definition layer_ids.h:115
@ Margin
Definition layer_ids.h:109
@ F_SilkS
Definition layer_ids.h:96
@ B_CrtYd
Definition layer_ids.h:111
@ Eco2_User
Definition layer_ids.h:106
@ B_SilkS
Definition layer_ids.h:97
@ F_Cu
Definition layer_ids.h:60
@ B_Fab
Definition layer_ids.h:114
This file contains miscellaneous commonly used macros and functions.
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition macros.h:79
#define UNIMPLEMENTED_FOR(type)
Definition macros.h:92
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:25
@ CHAMFERED_RECT
Definition padstack.h:59
@ ROUNDRECT
Definition padstack.h:56
@ TRAPEZOID
Definition padstack.h:55
@ RECTANGLE
Definition padstack.h:53
std::deque< PCB_TRACK * > TRACKS
CITER next(CITER it)
Definition ptree.cpp:120
VECTOR2I center
int radius
VECTOR2I end
void vset(double *v, double x, double y, double z)
Definition trackball.cpp:84
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:227
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:90
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88
Casted dyn_cast(From aObject)
A lightweight dynamic downcast.
Definition typeinfo.h:55
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708