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