KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io_ipc2581.cpp
Go to the documentation of this file.
1
19
20#include "pcb_io_ipc2581.h"
21#include "ipc2581_avl_order.h"
22#include "ipc2581_types.h"
23
24#include <base_units.h>
25#include <bezier_curves.h>
26#include <board.h>
29#include <build_version.h>
30#include <callback_gal.h>
43#include <font/font.h>
44#include <footprint.h>
45#include <hash.h>
46#include <hash_eda.h>
47#include <padstack.h>
48#include <pad.h>
50#include <pcb_dimension.h>
51#include <pcb_field.h>
52#include <pcb_shape.h>
53#include <pcb_textbox.h>
54#include <pcb_track.h>
55#include <pgm_base.h>
56#include <progress_reporter.h>
58#include <scoped_set_reset.h>
59#include <string_utils.h>
60#include <wx_fstream_progress.h>
61
66
67#include <wx/log.h>
68#include <wx/numformatter.h>
69#include <functional>
70
71#include <ki_exception.h>
72#include <wx/stream.h>
73#include <wx/xml/xml.h>
74#include <properties/property.h>
76
77
83static const wxChar traceIpc2581[] = wxT( "KICAD_IPC_2581" );
84
85
86// Extend the padstack identity with secondary/tertiary drill (backdrill) and
87// post-machining data so pads/vias with identical geometry but different
88// backdrill configuration do not collapse onto the same padstack entry.
89static void mixBackdrillIntoPadstackHash( size_t& aHash, const PADSTACK& aPadstack )
90{
91 auto mixDrill =
92 [&]( const PADSTACK::DRILL_PROPS& aDrill )
93 {
94 hash_combine( aHash, static_cast<int>( aDrill.start ),
95 static_cast<int>( aDrill.end ), aDrill.size.x, aDrill.size.y,
96 static_cast<int>( aDrill.shape ), aDrill.is_capped.has_value(),
97 aDrill.is_capped.value_or( false ), aDrill.is_filled.has_value(),
98 aDrill.is_filled.value_or( false ) );
99 };
100
101 auto mixPostMachining =
102 [&]( const PADSTACK::POST_MACHINING_PROPS& aPost )
103 {
104 hash_combine( aHash, aPost.mode.has_value(),
105 static_cast<int>(
106 aPost.mode.value_or( PAD_DRILL_POST_MACHINING_MODE::UNKNOWN ) ),
107 aPost.size, aPost.depth, aPost.angle );
108 };
109
110 mixDrill( aPadstack.SecondaryDrill() );
111 mixDrill( aPadstack.TertiaryDrill() );
112 mixPostMachining( aPadstack.FrontPostMachining() );
113 mixPostMachining( aPadstack.BackPostMachining() );
114}
115
116
117static size_t ipcPadstackHash( const PCB_VIA* aVia, const DRILL_OPERATION* aOperation )
118{
119 size_t hash = hash_fp_item( aVia, 0 );
120 mixBackdrillIntoPadstackHash( hash, aVia->Padstack() );
121
122 if( aOperation )
123 {
124 hash_combine( hash, aOperation->m_Filled, aOperation->m_Capped,
125 aOperation->m_TopCovered, aOperation->m_BottomCovered,
126 aOperation->m_TopPlugged, aOperation->m_BottomPlugged,
127 aOperation->m_TopTented, aOperation->m_BottomTented );
128 }
129
130 return hash;
131}
132
133
134static size_t ipcPadstackHash( const PAD* aPad )
135{
136 size_t hash = hash_fp_item( aPad, 0 );
137 mixBackdrillIntoPadstackHash( hash, aPad->Padstack() );
138
139 // The padstack definition emits one entry per layer in the pad's layer set, and the mask
140 // and paste entries include the per-side margins. Neither is captured by hash_fp_item(),
141 // so mix them in to keep distinct padstacks from collapsing onto a single definition.
142 hash_combine( hash, std::hash<BASE_SET>{}( aPad->GetLayerSet() ) );
144
145 VECTOR2I frontPaste = aPad->GetSolderPasteMargin( F_Paste );
146 VECTOR2I backPaste = aPad->GetSolderPasteMargin( B_Paste );
147 hash_combine( hash, frontPaste.x, frontPaste.y, backPaste.x, backPaste.y );
148
149 return hash;
150}
151
152
156static const std::map<wxString, surfaceFinishType> surfaceFinishMap =
157{
158 { wxEmptyString, surfaceFinishType::NONE },
159 { wxT( "ENIG" ), surfaceFinishType::ENIG_N },
160 { wxT( "ENEPIG" ), surfaceFinishType::ENEPIG_N },
161 { wxT( "HAL SNPB" ), surfaceFinishType::S },
162 { wxT( "HAL LEAD-FREE" ), surfaceFinishType::S },
163 { wxT( "HARD GOLD" ), surfaceFinishType::G },
164 { wxT( "IMMERSION TIN" ), surfaceFinishType::ISN },
165 { wxT( "IMMERSION NICKEL" ), surfaceFinishType::N },
166 { wxT( "IMMERSION SILVER" ), surfaceFinishType::IAG },
167 { wxT( "IMMERSION GOLD" ), surfaceFinishType::DIG },
168 { wxT( "HT_OSP" ), surfaceFinishType::HT_OSP },
169 { wxT( "OSP" ), surfaceFinishType::OSP },
170 { wxT( "NONE" ), surfaceFinishType::NONE },
171 { wxT( "NOT SPECIFIED" ), surfaceFinishType::NONE },
172 { wxT( "USER DEFINED" ), surfaceFinishType::NONE },
173};
174
175
179static const std::map<surfaceFinishType, wxString> surfaceFinishTypeToString =
180{
181 { surfaceFinishType::ENIG_N, wxT( "ENIG-N" ) },
182 { surfaceFinishType::ENEPIG_N, wxT( "ENEPIG-N" ) },
183 { surfaceFinishType::OSP, wxT( "OSP" ) },
184 { surfaceFinishType::HT_OSP, wxT( "HT_OSP" ) },
185 { surfaceFinishType::IAG, wxT( "IAg" ) },
186 { surfaceFinishType::ISN, wxT( "ISn" ) },
187 { surfaceFinishType::G, wxT( "G" ) },
188 { surfaceFinishType::N, wxT( "N" ) },
189 { surfaceFinishType::DIG, wxT( "DIG" ) },
190 { surfaceFinishType::S, wxT( "S" ) },
191 { surfaceFinishType::OTHER, wxT( "OTHER" ) },
192};
193
194
195static surfaceFinishType getSurfaceFinishType( const wxString& aFinish )
196{
197 auto it = surfaceFinishMap.find( aFinish.Upper() );
198 return ( it != surfaceFinishMap.end() ) ? it->second : surfaceFinishType::OTHER;
199}
200
201
207
208
210{
211 for( FOOTPRINT* fp : m_loaded_footprints )
212 delete fp;
213
214 m_loaded_footprints.clear();
215}
216
217
219{
220 std::vector<FOOTPRINT*> retval;
221
222 for( FOOTPRINT* fp : m_loaded_footprints )
223 retval.push_back( static_cast<FOOTPRINT*>( fp->Clone() ) );
224
225 return retval;
226}
227
228
229void PCB_IO_IPC2581::insertNode( wxXmlNode* aParent, wxXmlNode* aNode )
230{
231 // insertNode places the node at the start of the list of children
232
233 if( aParent->GetChildren() )
234 aNode->SetNext( aParent->GetChildren() );
235 else
236 aNode->SetNext( nullptr );
237
238 aParent->SetChildren( aNode );
239 aNode->SetParent( aParent );
240 m_total_bytes += 2 * aNode->GetName().size() + 5;
241}
242
243
244void PCB_IO_IPC2581::insertNodeAfter( wxXmlNode* aPrev, wxXmlNode* aNode )
245{
246 // insertNode places the node directly after aPrev
247
248 wxCHECK( aPrev, /* void */ );
249
250 aNode->SetNext( aPrev->GetNext() );
251 aPrev->SetNext( aNode );
252 aNode->SetParent( aPrev->GetParent() );
253 m_total_bytes += 2 * aNode->GetName().size() + 5;
254}
255
256
257void PCB_IO_IPC2581::deleteNode( wxXmlNode*& aNode )
258{
259 // When deleting a node, invalidate the appendNode optimization cache if it points
260 // to the node being deleted or any of its descendants
262 {
263 wxXmlNode* check = m_lastAppendedNode;
264
265 while( check )
266 {
267 if( check == aNode )
268 {
269 m_lastAppendedNode = nullptr;
270 break;
271 }
272
273 check = check->GetParent();
274 }
275 }
276
277 if( aNode->GetParent() )
278 aNode->GetParent()->RemoveChild( aNode );
279
280 delete aNode;
281 aNode = nullptr;
282}
283
284
285wxXmlNode* PCB_IO_IPC2581::insertNode( wxXmlNode* aParent, const wxString& aName )
286{
287 // Opening tag, closing tag, brackets and the closing slash
288 m_total_bytes += 2 * aName.size() + 5;
289 wxXmlNode* node = new wxXmlNode( wxXML_ELEMENT_NODE, aName );
290 insertNode( aParent, node );
291 return node;
292}
293
294
295void PCB_IO_IPC2581::appendNode( wxXmlNode* aParent, wxXmlNode* aNode )
296{
297 // AddChild iterates through the entire list of children, so we want to avoid
298 // that if possible. When we share a parent and our next sibling is null,
299 // then we are the last child and can just append to the end of the list.
300
301 if( m_lastAppendedNode && m_lastAppendedNode->GetParent() == aParent
302 && m_lastAppendedNode->GetNext() == nullptr )
303 {
304 aNode->SetParent( aParent );
305 m_lastAppendedNode->SetNext( aNode );
306 }
307 else
308 {
309 aParent->AddChild( aNode );
310 }
311
312 m_lastAppendedNode = aNode;
313
314 // Opening tag, closing tag, brackets and the closing slash
315 m_total_bytes += 2 * aNode->GetName().size() + 5;
316}
317
318
319wxXmlNode* PCB_IO_IPC2581::appendNode( wxXmlNode* aParent, const wxString& aName )
320{
321 wxXmlNode* node = new wxXmlNode( wxXML_ELEMENT_NODE, aName );
322
323 appendNode( aParent, node );
324 return node;
325}
326
327
328wxString PCB_IO_IPC2581::sanitizeId( const wxString& aStr ) const
329{
330 wxString str;
331
333 {
334 str = aStr;
335 str.Replace( wxT( ":" ), wxT( "_" ) );
336 }
337 else
338 {
339 for( wxString::const_iterator iter = aStr.begin(); iter != aStr.end(); ++iter )
340 {
341 if( !m_acceptable_chars.count( *iter ) )
342 str.Append( '_' );
343 else
344 str.Append( *iter );
345 }
346 }
347
348 return str;
349}
350
351
352wxString PCB_IO_IPC2581::genString( const wxString& aStr, const char* aPrefix ) const
353{
354 // Build a key using the prefix and original string so that repeated calls for the same
355 // element return the same generated name.
356 wxString key = aPrefix ? wxString( aPrefix ) + wxT( ":" ) + aStr : aStr;
357
358 auto it = m_generated_names.find( key );
359
360 if( it != m_generated_names.end() )
361 return it->second;
362
363 wxString str = sanitizeId( aStr );
364
365 wxString base = str;
366 wxString name = base;
367 int suffix = 1;
368 // Designators and OEM part numbers use separate schema keys; other names share one set
369 wxString prefix = aPrefix ? wxString( aPrefix ) : wxString();
370 bool ownSet = prefix == wxS( "CMP" ) || prefix == wxS( "REF" );
371 std::set<wxString>& names = m_element_names[ownSet ? prefix : wxString()];
372
373 while( names.count( name ) )
374 name = wxString::Format( "%s_%d", base, suffix++ );
375
376 names.insert( name );
377 m_generated_names[key] = name;
378
379 return name;
380}
381
382
383wxString PCB_IO_IPC2581::genLayerString( PCB_LAYER_ID aLayer, const char* aPrefix ) const
384{
385 return genString( m_board->GetLayerName( aLayer ), aPrefix );
386}
387
388
389wxString PCB_IO_IPC2581::stackupLayerName( const BOARD_STACKUP_ITEM* aItem, int aSublayerId, const char* aPrefix ) const
390{
391 wxString name;
392
393 if( aItem->GetType() == BS_ITEM_TYPE_DIELECTRIC )
394 {
395 name = wxString::Format( "DIELECTRIC_%d", aItem->GetDielectricLayerId() );
396 }
397 else
398 {
399 name = aItem->GetLayerName();
400
401 if( name.IsEmpty() && IsValidLayer( aItem->GetBrdLayerId() ) )
402 name = m_board->GetLayerName( aItem->GetBrdLayerId() );
403 }
404
405 if( aSublayerId > 0 )
406 name += wxString::Format( "_%d", aSublayerId );
407
408 return genString( name, aPrefix );
409}
410
411
412wxString PCB_IO_IPC2581::genLayersString( PCB_LAYER_ID aTop, PCB_LAYER_ID aBottom, const char* aPrefix ) const
413{
414 return genString( wxString::Format( wxS( "%s_%s" ),
415 m_board->GetLayerName( aTop ),
416 m_board->GetLayerName( aBottom ) ), aPrefix );
417}
418
419
420wxString PCB_IO_IPC2581::pinName( const PAD* aPad ) const
421{
422 FOOTPRINT* footprint = aPad->GetParentFootprint();
423 size_t index = 0;
424
425 if( ( aPad->GetAttribute() == PAD_ATTRIB::NPTH || aPad->GetNumber().IsEmpty() ) && footprint )
426 {
427 for( ; index < footprint->Pads().size(); ++index )
428 {
429 if( footprint->Pads()[index] == aPad )
430 {
431 break;
432 }
433 }
434 }
435
436 // Pins are scoped per-package, so we only sanitize; uniqueness is handled by
437 // the per-package pin_nodes map in addPackage().
438 return sanitizeId( MakeFabPin( *aPad, index ).m_name );
439}
440
441
443{
444 auto tryInsert =
445 [&]( const wxString& aName )
446 {
447 if( m_footprint_refdes_dict.count( aName ) )
448 {
449 if( m_footprint_refdes_dict.at( aName ) != aFootprint )
450 return false;
451 }
452 else
453 {
454 m_footprint_refdes_dict.insert( { aName, aFootprint } );
455 }
456
457 return true;
458 };
459
460 if( m_footprint_refdes_reverse_dict.count( aFootprint ) )
461 return m_footprint_refdes_reverse_dict.at( aFootprint );
462
463 // Component@refDes and Bom/BomItem/RefDes@name are mandatory
464 // Thus the export replaces the designator and does not remove it
465 if( m_omitRefDes )
466 {
467 auto [it, inserted] = m_anon_refdes_dict.emplace( aFootprint, wxString() );
468
469 if( inserted )
470 it->second = wxString::Format( "CMP_%zu", m_anon_refdes_dict.size() );
471
472 return it->second;
473 }
474
475 wxString ref = aFootprint->GetReference();
476
477 if( ref.IsEmpty() )
478 ref = wxT( "NOREF_" ) + aFootprint->m_Uuid.AsString().Left( 8 );
479
480 wxString baseName = genString( ref, "CMP" );
481 wxString name = baseName;
482 int suffix = 1;
483
484 while( !tryInsert( name ) )
485 name = wxString::Format( "%s_%d", baseName, suffix++ );
486
488
489 return name;
490}
491
492
493wxString PCB_IO_IPC2581::floatVal( double aVal, int aSigFig ) const
494{
495 return FormatTrimmedDecimal( aVal, aSigFig == -1 ? m_sigfig : aSigFig );
496}
497
498
499void PCB_IO_IPC2581::addXY( wxXmlNode* aNode, const VECTOR2I& aVec, const char* aXName, const char* aYName )
500{
501 if( aXName )
502 addAttribute( aNode, aXName, floatVal( m_scale * aVec.x ) );
503 else
504 addAttribute( aNode, "x", floatVal( m_scale * aVec.x ) );
505
506 if( aYName )
507 addAttribute( aNode, aYName, floatVal( -m_scale * aVec.y ) );
508 else
509 addAttribute( aNode, "y", floatVal( -m_scale * aVec.y ) );
510}
511
512
513void PCB_IO_IPC2581::addAttribute( wxXmlNode* aNode, const wxString& aName, const wxString& aValue )
514{
515 m_total_bytes += aName.size() + aValue.size() + 4;
516 aNode->AddAttribute( aName, aValue );
517}
518
519
521{
522 wxXmlNode* xmlHeaderNode = new wxXmlNode(wxXML_ELEMENT_NODE, "IPC-2581");
523 addAttribute( xmlHeaderNode, "revision", m_revision == IPC2581::REVISION::C ? wxT( "C" ) : wxT( "B" ) );
524 addAttribute( xmlHeaderNode, "xmlns", "http://webstds.ipc.org/2581");
525 addAttribute( xmlHeaderNode, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
526 addAttribute( xmlHeaderNode, "xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
527
529 {
530 addAttribute( xmlHeaderNode, "xsi:schemaLocation",
531 "http://webstds.ipc.org/2581 http://webstds.ipc.org/2581/IPC-2581B1.xsd" );
532 }
533 else
534 {
535 addAttribute( xmlHeaderNode, "xsi:schemaLocation",
536 "http://webstds.ipc.org/2581 http://webstds.ipc.org/2581/IPC-2581C.xsd" );
537 }
538
539 m_xml_doc->SetRoot( xmlHeaderNode );
540
541 return xmlHeaderNode;
542}
543
544
546{
547 reportPhase( _( "Generating content section" ) );
548
549 m_contentNode = appendNode( m_xml_root, "Content" );
550 wxXmlNode* contentNode = m_contentNode;
551 addAttribute( contentNode, "roleRef", "Owner" );
552
553 wxXmlNode* node = appendNode( contentNode, "FunctionMode" );
554 addAttribute( node, "mode", IPC2581::ModeToken( m_mode ) );
555
556 // Revision B grades each function mode from 1 to 3 and sets USERDEF to 1
557 // Revision C removed the attribute
559 addAttribute( node, "level", m_mode == IPC2581::MODE::USERDEF ? "1" : "3" );
560
561 // The sectionKey names the content of the file thus it comes after the generation
562 m_functionModeNode = node;
563
564 if( IPC2581::NeedsCadData( m_resolved.m_included ) )
565 {
566 node = appendNode( contentNode, "StepRef" );
567 wxFileName fn( m_board->GetFileName() );
568 addAttribute( node, "name", genString( fn.GetName(), "BOARD" ) );
569 }
570
571 wxXmlNode* color_node = included( IPC2581::SECTION::STACKUP )
572 ? generateContentStackup( contentNode )
573 : new wxXmlNode( wxXML_ELEMENT_NODE, "DictionaryColor" );
574
576 {
577 contentNode->AddChild( color_node );
578 m_line_node = appendNode( contentNode, "DictionaryLineDesc" );
580
581 wxXmlNode* fillNode = appendNode( contentNode, "DictionaryFillDesc" );
582 addAttribute( fillNode, "units", m_units_str );
583
584 m_shape_std_node = appendNode( contentNode, "DictionaryStandard" );
586
587 m_shape_user_node = appendNode( contentNode, "DictionaryUser" );
589 }
590 else
591 {
592 m_shape_std_node = appendNode( contentNode, "DictionaryStandard" );
594
595 m_shape_user_node = appendNode( contentNode, "DictionaryUser" );
597
598 m_line_node = appendNode( contentNode, "DictionaryLineDesc" );
600
601 contentNode->AddChild( color_node );
602 }
603
604 return contentNode;
605}
606
607
608void PCB_IO_IPC2581::addLocationNode( wxXmlNode* aNode, double aX, double aY )
609{
610 wxXmlNode* location_node = appendNode( aNode, "Location" );
611 addXY( location_node, VECTOR2I( aX, aY ) );
612}
613
614
615void PCB_IO_IPC2581::addLocationNode( wxXmlNode* aNode, const PAD& aPad, bool aRelative, PCB_LAYER_ID aLayer )
616{
617 VECTOR2D pos{};
618
619 if( aRelative )
620 pos = aPad.GetFPRelativePosition();
621 else
622 pos = aPad.GetPosition();
623
624 const VECTOR2I& offset = aPad.GetOffset( aPad.Padstack().EffectiveLayerFor( aLayer ) );
625
626 if( offset.x != 0 || offset.y != 0 )
627 pos += offset;
628
629 addLocationNode( aNode, pos.x, pos.y );
630}
631
632
633void PCB_IO_IPC2581::addLocationNode( wxXmlNode* aNode, const PCB_SHAPE& aShape )
634{
635 VECTOR2D pos{};
636
637 switch( aShape.GetShape() )
638 {
639 // Rectangles in KiCad are mapped by their corner while IPC2581 uses the center
641 pos = aShape.GetPosition() + VECTOR2I( KiROUND( aShape.GetRectangleWidth() / 2.0 ),
642 KiROUND( aShape.GetRectangleHeight() / 2.0 ) );
643 break;
644 // Both KiCad and IPC2581 use the center of the circle
645 case SHAPE_T::CIRCLE:
646 case SHAPE_T::ELLIPSE:
647 case SHAPE_T::ELLIPSE_ARC: pos = aShape.GetPosition(); break;
648
649 // KiCad uses the exact points on the board, so we want the reference location to be 0,0
650 case SHAPE_T::POLY:
651 case SHAPE_T::BEZIER:
652 case SHAPE_T::SEGMENT:
653 case SHAPE_T::ARC:
654 pos = VECTOR2D( 0, 0 );
655 break;
656
658 wxFAIL;
659 }
660
661 addLocationNode( aNode, pos.x, pos.y );
662}
663
664
665size_t PCB_IO_IPC2581::lineHash( int aWidth, LINE_STYLE aDashType )
666{
667 size_t hash = hash_val( aWidth );
668 hash_combine( hash, aDashType );
669
670 return hash;
671}
672
673
674wxXmlNode* PCB_IO_IPC2581::generateContentStackup( wxXmlNode* aContentNode )
675{
676 const BOARD_STACKUP& stackup = m_fabStackup->Stackup();
677
678 wxXmlNode* color_node = new wxXmlNode( wxXML_ELEMENT_NODE, "DictionaryColor" );
679
680 for( BOARD_STACKUP_ITEM* item: stackup.GetList() )
681 {
682 for( int sub_idx = 0; sub_idx < item->GetSublayersCount(); sub_idx++ )
683 {
684 wxString sub_layer_name = stackupLayerName( item, sub_idx, "LAYER" );
685
686 if( sub_idx == 0 && item->GetType() != BS_ITEM_TYPE_DIELECTRIC )
687 m_layer_name_map.emplace( item->GetBrdLayerId(), sub_layer_name );
688
689 if( !IsPrmSpecified( item->GetColor( sub_idx ) ) )
690 continue;
691
692 wxXmlNode* entry_color = appendNode( color_node, "EntryColor" );
693 addAttribute( entry_color, "id", genString( sub_layer_name, "COLOR" ) );
694 wxXmlNode* color = appendNode( entry_color, "Color" );
695
696 wxString colorName = item->GetColor( sub_idx );
697
698 if( colorName.StartsWith( wxT( "#" ) ) ) // This is a user defined color, not in standard color list.
699 {
700 COLOR4D layer_color( colorName );
701 addAttribute( color, "r", wxString::Format( "%d", KiROUND( layer_color.r * 255 ) ) );
702 addAttribute( color, "g", wxString::Format( "%d", KiROUND( layer_color.g * 255 ) ) );
703 addAttribute( color, "b", wxString::Format( "%d", KiROUND( layer_color.b * 255 ) ) );
704 }
705 else
706 {
707 for( const FAB_LAYER_COLOR& fab_color : GetStandardColors( item->GetType() ) )
708 {
709 if( fab_color.GetName() == colorName )
710 {
711 KIGFX::COLOR4D fab_color_for_type = fab_color.GetColor( item->GetType() );
712 addAttribute( color, "r", wxString::Format( "%d", KiROUND( fab_color_for_type.r * 255 ) ) );
713 addAttribute( color, "g", wxString::Format( "%d", KiROUND( fab_color_for_type.g * 255 ) ) );
714 addAttribute( color, "b", wxString::Format( "%d", KiROUND( fab_color_for_type.b * 255 ) ) );
715 break;
716 }
717 }
718 }
719 }
720 }
721
722 return color_node;
723}
724
725
726wxString PCB_IO_IPC2581::netName( const wxString& aNetname, const wxString& aSuffix )
727{
728 if( m_anonymizeNets )
729 {
730 auto it = m_net_name_dict.find( aNetname );
731
732 if( it != m_net_name_dict.end() )
733 return it->second + aSuffix;
734
735 return wxS( "NET_0" ) + aSuffix;
736 }
737
738 // Add the suffix before genString so that the name stays unique
739 return genString( aNetname + aSuffix, "NET" );
740}
741
742
743void PCB_IO_IPC2581::reportPhase( const wxString& aMessage )
744{
745 if( !m_progressReporter )
746 return;
747
748 m_progressReporter->AdvancePhase( aMessage );
749
750 if( !m_progressReporter->KeepRefreshing() )
752}
753
754
755void PCB_IO_IPC2581::tickProgress( const wxString& aMessage )
756{
757 if( !m_progressReporter )
758 return;
759
760 if( !aMessage.IsEmpty() )
761 m_progressReporter->Report( aMessage );
762
763 m_progressReporter->AdvanceProgress();
764
765 // A repaint for each item costs more than the reported work
766 if( ++m_progress_tick % 32 != 0 )
767 return;
768
769 if( !m_progressReporter->KeepRefreshing() )
771}
772
773
774bool PCB_IO_IPC2581::stackupRowIncluded( bool aDielectric, PCB_LAYER_ID aLayer ) const
775{
776 return aDielectric ? included( IPC2581::SECTION::DIELECTRIC ) : layerIncluded( aLayer );
777}
778
779
781{
782 std::optional<IPC2581::SECTION> section = IPC2581::SectionForBoardLayer( aLayer );
783
784 return section && included( *section );
785}
786
787
789{
790 IPC2581::SECTION_SET sections;
791
792 if( !m_xml_root )
793 return sections;
794
795 std::function<void( wxXmlNode* )> visit =
796 [&]( wxXmlNode* aNode )
797 {
798 for( wxXmlNode* child = aNode->GetChildren(); child; child = child->GetNext() )
799 {
800 const wxString& name = child->GetName();
801
802 if( name == "Layer" )
803 {
804 std::optional<IPC2581::SECTION> section = IPC2581::SectionForLayerFunction(
805 child->GetAttribute( "layerFunction" ),
806 child->GetAttribute( "side" ) );
807
808 if( section )
809 sections.Set( *section );
810 }
811 else if( name == "Bom" )
812 sections.Set( IPC2581::SECTION::BOM_AVL );
813 else if( name == "PadStackDef" )
814 sections.Set( IPC2581::SECTION::PADSTACKS );
815 else if( name == "Package" )
816 sections.Set( IPC2581::SECTION::PACKAGES );
817 else if( name == "Component" )
818 sections.Set( IPC2581::SECTION::COMPONENTS );
819 else if( name == "Stackup" )
820 sections.Set( IPC2581::SECTION::STACKUP );
821 else if( name == "Profile" )
822 sections.Set( IPC2581::SECTION::PROFILE );
823 else if( name == "LogicalNet" )
824 sections.Set( IPC2581::SECTION::LOGICAL_NET );
825 else if( name == "PhyNetGroup" )
826 sections.Set( IPC2581::SECTION::PHYSICAL_NET );
827 else if( name == "Dfx" )
828 sections.Set( IPC2581::SECTION::DFX );
829
830 visit( child );
831 }
832 };
833
834 visit( m_xml_root );
835
836 return sections;
837}
838
839
841{
842 if( !m_contentNode )
843 return;
844
845 wxXmlNode* child = m_contentNode->GetChildren();
846
847 while( child )
848 {
849 wxXmlNode* next = child->GetNext();
850
851 if( child->GetName() == "StepRef" )
852 deleteNode( child );
853
854 child = next;
855 }
856}
857
858
859void PCB_IO_IPC2581::generateContentLayerRefs( wxXmlNode* aCadDataNode )
860{
861 if( !m_contentNode || !aCadDataNode )
862 return;
863
864 wxXmlNode* insertBefore = nullptr;
865
866 for( wxXmlNode* child = m_contentNode->GetChildren(); child; child = child->GetNext() )
867 {
868 if( child->GetName().StartsWith( "Dictionary" ) )
869 {
870 insertBefore = child;
871 break;
872 }
873 }
874
875 for( wxXmlNode* layer = aCadDataNode->GetChildren(); layer; layer = layer->GetNext() )
876 {
877 if( layer->GetName() != "Layer" )
878 continue;
879
880 wxXmlNode* ref = new wxXmlNode( wxXML_ELEMENT_NODE, "LayerRef" );
881 addAttribute( ref, "name", layer->GetAttribute( "name" ) );
882
883 if( insertBefore )
884 m_contentNode->InsertChild( ref, insertBefore );
885 else
886 m_contentNode->AddChild( ref );
887 }
888}
889
890
891void PCB_IO_IPC2581::addFillDesc( wxXmlNode* aNode, FILL_T aFill, bool aForce )
892{
893 if( aFill == FILL_T::FILLED_SHAPE )
894 {
895 // By default, we do not fill shapes because FILL is the default value for most.
896 // But for some outlines, we may need to force a fill.
897 if( aForce )
898 {
899 wxXmlNode* fillDesc_node = appendNode( aNode, "FillDesc" );
900 addAttribute( fillDesc_node, "fillProperty", "FILL" );
901 }
902 }
903 else
904 {
905 wxXmlNode* fillDesc_node = appendNode( aNode, "FillDesc" );
906 addAttribute( fillDesc_node, "fillProperty", "HOLLOW" );
907 }
908}
909
910
911void PCB_IO_IPC2581::addLineDesc( wxXmlNode* aNode, int aWidth, LINE_STYLE aDashType, bool aForce )
912{
913 wxCHECK_RET( aNode, "aNode is null" );
914
915 if( aWidth < 0 )
916 return;
917
918 wxXmlNode* entry_node = nullptr;
919
920 if( !aForce )
921 {
922 size_t hash = lineHash( aWidth, aDashType );
923 wxString name = wxString::Format( "LINE_%zu", m_line_dict.size() + 1 );
924 auto[ iter, inserted ] = m_line_dict.emplace( hash, name );
925
926 // Either add a new entry or reference an existing one
927 wxXmlNode* lineDesc_node = appendNode( aNode, "LineDescRef" );
928 addAttribute( lineDesc_node, "id", iter->second );
929
930 if( !inserted )
931 return;
932
933 entry_node = appendNode( m_line_node, "EntryLineDesc" );
934 addAttribute( entry_node, "id", name );
935 }
936 else
937 {
938 // Force the LineDesc to be added directly to the parent node
939 entry_node = aNode;
940 }
941
942 wxXmlNode* line_node = appendNode( entry_node, "LineDesc" );
943 addAttribute( line_node, "lineWidth", floatVal( m_scale * aWidth ) );
944 addAttribute( line_node, "lineEnd", "ROUND" );
945
946 switch( aDashType )
947 {
948 case LINE_STYLE::DOT: addAttribute( line_node, "lineProperty", "DOTTED" ); break;
949 case LINE_STYLE::DASH: addAttribute( line_node, "lineProperty", "DASHED" ); break;
950 case LINE_STYLE::DASHDOT: addAttribute( line_node, "lineProperty", "CENTER" ); break;
951 case LINE_STYLE::DASHDOTDOT: addAttribute( line_node, "lineProperty", "PHANTOM" ); break;
952 default: break;
953 }
954}
955
956
957void PCB_IO_IPC2581::addKnockoutText( wxXmlNode* aContentNode, PCB_TEXT* aText )
958{
959 SHAPE_POLY_SET finalPoly;
960
961 aText->TransformTextToPolySet( finalPoly, 0, ARC_HIGH_DEF, ERROR_INSIDE );
962 finalPoly.Fracture();
963
964 const int outlineCount = finalPoly.OutlineCount();
965
966 if( outlineCount == 0 )
967 return;
968
969 // The IPC-2581 schema allows only one top-level Feature under Features/Marking,
970 // so wrap multiple glyph contours in a UserSpecial (a UserPrimitive Feature that
971 // may contain any number of child Features).
972
973 if( outlineCount == 1 )
974 {
975 addContourNode( aContentNode, finalPoly, 0 );
976 return;
977 }
978
979 wxXmlNode* special_node = appendNode( aContentNode, "UserSpecial" );
980
981 for( int ii = 0; ii < outlineCount; ++ii )
982 addContourNode( special_node, finalPoly, ii );
983}
984
985
986void PCB_IO_IPC2581::addText( wxXmlNode* aContentNode, EDA_TEXT* aText, const KIFONT::METRICS& aFontMetrics )
987{
989 KIFONT::FONT* font = aText->GetDrawFont( nullptr );
990 TEXT_ATTRIBUTES attrs = aText->GetAttributes();
991
993 attrs.m_Angle = aText->GetDrawRotation();
994 attrs.m_Multiline = false;
995
996 wxXmlNode* text_node = appendNode( aContentNode, "UserSpecial" );
997
998 std::list<VECTOR2I> pts;
999
1000 auto push_pts =
1001 [&]()
1002 {
1003 if( pts.size() < 2 )
1004 return;
1005
1006 wxXmlNode* line_node = nullptr;
1007
1008 // Polylines are only allowed for more than 3 points (in version B).
1009 // Otherwise, we have to use a line
1010 if( pts.size() < 3 )
1011 {
1012 line_node = appendNode( text_node, "Line" );
1013 addXY( line_node, pts.front(), "startX", "startY" );
1014 addXY( line_node, pts.back(), "endX", "endY" );
1015 }
1016 else
1017 {
1018 line_node = appendNode( text_node, "Polyline" );
1019 wxXmlNode* point_node = appendNode( line_node, "PolyBegin" );
1020 addXY( point_node, pts.front() );
1021
1022 auto iter = pts.begin();
1023
1024 for( ++iter; iter != pts.end(); ++iter )
1025 {
1026 wxXmlNode* step_node = appendNode( line_node, "PolyStepSegment" );
1027 addXY( step_node, *iter );
1028 }
1029 }
1030
1031 addLineDesc( line_node, attrs.m_StrokeWidth, LINE_STYLE::SOLID );
1032 pts.clear();
1033 };
1034
1035 CALLBACK_GAL callback_gal( empty_opts,
1036 // Stroke callback
1037 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
1038 {
1039 if( !pts.empty() )
1040 {
1041 if( aPt1 == pts.back() )
1042 pts.push_back( aPt2 );
1043 else if( aPt2 == pts.front() )
1044 pts.push_front( aPt1 );
1045 else if( aPt1 == pts.front() )
1046 pts.push_front( aPt2 );
1047 else if( aPt2 == pts.back() )
1048 pts.push_back( aPt1 );
1049 else
1050 {
1051 push_pts();
1052 pts.push_back( aPt1 );
1053 pts.push_back( aPt2 );
1054 }
1055 }
1056 else
1057 {
1058 pts.push_back( aPt1 );
1059 pts.push_back( aPt2 );
1060 }
1061 },
1062 // Polygon callback
1063 [&]( const SHAPE_LINE_CHAIN& aPoly )
1064 {
1065 if( aPoly.PointCount() < 3 )
1066 return;
1067
1068 wxXmlNode* outline_node = appendNode( text_node, "Outline" );
1069 wxXmlNode* poly_node = appendNode( outline_node, "Polygon" );
1070 addLineDesc( outline_node, 0, LINE_STYLE::SOLID );
1071
1072 const std::vector<VECTOR2I>& polyPts = aPoly.CPoints();
1073 wxXmlNode* point_node = appendNode( poly_node, "PolyBegin" );
1074 addXY( point_node, polyPts.front() );
1075
1076 for( size_t ii = 1; ii < polyPts.size(); ++ii )
1077 {
1078 wxXmlNode* poly_step_node = appendNode( poly_node, "PolyStepSegment" );
1079 addXY( poly_step_node, polyPts[ii] );
1080 }
1081
1082 point_node = appendNode( poly_node, "PolyStepSegment" );
1083 addXY( point_node, polyPts.front() );
1084 } );
1085
1086 //TODO: handle multiline text
1087
1088 font->Draw( &callback_gal, aText->GetShownText( FOR_CANVAS ), aText->GetDrawPos(), attrs, aFontMetrics );
1089
1090 if( !pts.empty() )
1091 push_pts();
1092
1093 if( text_node->GetChildren() == nullptr )
1094 {
1095 aContentNode->RemoveChild( text_node );
1096 delete text_node;
1097 }
1098}
1099
1100
1101void PCB_IO_IPC2581::addShape( wxXmlNode* aContentNode, const PAD& aPad, PCB_LAYER_ID aLayer )
1102{
1103 int maxError = m_board->GetDesignSettings().m_MaxError;
1104 wxString name;
1105 VECTOR2I margin = ResolvePadMargin( aPad, aLayer );
1106
1107 // The same pad yields different geometry per layer: complex padstacks differ between
1108 // copper layers and the mask/paste margins are layer-specific. The primitive cache must
1109 // therefore be keyed on the effective shape layer and the margin as well as the pad
1110 // itself, otherwise the first layer processed (copper) is reused for the mask and paste
1111 // layers, silently dropping the margins.
1112 size_t hash = hash_fp_item( &aPad, 0 );
1113 hash_combine( hash, aPad.Padstack().EffectiveLayerFor( aLayer ), margin.x, margin.y );
1114
1115 auto iter = m_std_shape_dict.find( hash );
1116
1117 if( iter != m_std_shape_dict.end() )
1118 {
1119 wxXmlNode* shape_node = appendNode( aContentNode, "StandardPrimitiveRef" );
1120 addAttribute( shape_node, "id", iter->second );
1121 return;
1122 }
1123
1124 PAD_LAYER_GEOMETRY geometry = ResolvePadLayer( aPad, aLayer, maxError );
1125 const PAD& resolved = geometry.Pad();
1126
1127 switch( aPad.GetShape( aLayer ) )
1128 {
1129 case PAD_SHAPE::CIRCLE:
1130 {
1131 name = wxString::Format( "CIRCLE_%zu", m_std_shape_dict.size() + 1 );
1132 m_std_shape_dict.emplace( hash, name );
1133
1134 wxXmlNode* entry_node = appendNode( m_shape_std_node, "EntryStandard" );
1135 addAttribute( entry_node, "id", name );
1136
1137 int diameter = resolved.GetSize( aLayer ).x;
1138
1139 wxXmlNode* circle_node = appendNode( entry_node, "Circle" );
1140 addAttribute( circle_node, "diameter", floatVal( m_scale * diameter ) );
1141 break;
1142 }
1143
1145 {
1146 name = wxString::Format( "RECT_%zu", m_std_shape_dict.size() + 1 );
1147 m_std_shape_dict.emplace( hash, name );
1148
1149 wxXmlNode* entry_node = appendNode( m_shape_std_node, "EntryStandard" );
1150 addAttribute( entry_node, "id", name );
1151
1152 VECTOR2I pad_size = resolved.GetSize( aLayer );
1153
1154 // A positive margin inflates the rectangle into a rounded rectangle (the Minkowski
1155 // sum of the rectangle and a disc of radius margin); the board plotter promotes the
1156 // shape the same way.
1157 if( margin.x > 0 )
1158 {
1159 wxXmlNode* rect_node = appendNode( entry_node, "RectRound" );
1160 addAttribute( rect_node, "width", floatVal( m_scale * std::abs( pad_size.x ) ) );
1161 addAttribute( rect_node, "height", floatVal( m_scale * std::abs( pad_size.y ) ) );
1162 addAttribute( rect_node, "radius", floatVal( m_scale * geometry.CornerRadius() ) );
1163 addAttribute( rect_node, "upperRight", "true" );
1164 addAttribute( rect_node, "upperLeft", "true" );
1165 addAttribute( rect_node, "lowerRight", "true" );
1166 addAttribute( rect_node, "lowerLeft", "true" );
1167 }
1168 else
1169 {
1170 wxXmlNode* rect_node = appendNode( entry_node, "RectCenter" );
1171 addAttribute( rect_node, "width", floatVal( m_scale * std::abs( pad_size.x ) ) );
1172 addAttribute( rect_node, "height", floatVal( m_scale * std::abs( pad_size.y ) ) );
1173 }
1174
1175 break;
1176 }
1177
1178 case PAD_SHAPE::OVAL:
1179 {
1180 name = wxString::Format( "OVAL_%zu", m_std_shape_dict.size() + 1 );
1181 m_std_shape_dict.emplace( hash, name );
1182
1183 wxXmlNode* entry_node = appendNode( m_shape_std_node, "EntryStandard" );
1184 addAttribute( entry_node, "id", name );
1185
1186 VECTOR2I pad_size = resolved.GetSize( aLayer );
1187
1188 wxXmlNode* oval_node = appendNode( entry_node, "Oval" );
1189 addAttribute( oval_node, "width", floatVal( m_scale * pad_size.x ) );
1190 addAttribute( oval_node, "height", floatVal( m_scale * pad_size.y ) );
1191
1192 break;
1193 }
1194
1196 {
1197 name = wxString::Format( "ROUNDRECT_%zu", m_std_shape_dict.size() + 1 );
1198 m_std_shape_dict.emplace( hash, name );
1199
1200 wxXmlNode* entry_node = appendNode( m_shape_std_node, "EntryStandard" );
1201 addAttribute( entry_node, "id", name );
1202
1203 VECTOR2I pad_size = resolved.GetSize( aLayer );
1204 int radius = geometry.CornerRadius();
1205
1206 wxXmlNode* roundrect_node = appendNode( entry_node, "RectRound" );
1207 addAttribute( roundrect_node, "width", floatVal( m_scale * pad_size.x ) );
1208 addAttribute( roundrect_node, "height", floatVal( m_scale * pad_size.y ) );
1209 addAttribute( roundrect_node, "radius", floatVal( m_scale * radius ) );
1210 addAttribute( roundrect_node, "upperRight", "true" );
1211 addAttribute( roundrect_node, "upperLeft", "true" );
1212 addAttribute( roundrect_node, "lowerRight", "true" );
1213 addAttribute( roundrect_node, "lowerLeft", "true" );
1214
1215 break;
1216 }
1217
1219 {
1220 name = wxString::Format( "RECTCHAMFERED_%zu", m_std_shape_dict.size() + 1 );
1221 m_std_shape_dict.emplace( hash, name );
1222
1223 wxXmlNode* entry_node = appendNode( m_shape_std_node, "EntryStandard" );
1224 addAttribute( entry_node, "id", name );
1225
1226 if( margin.x <= 0 || margin.x != margin.y )
1227 {
1228 // A deflated (or anisotropically inflated) chamfered rectangle keeps its
1229 // parametric shape, with the chamfer ratio applied to the adjusted size; this is
1230 // what PlotStandardLayer() plots.
1231 VECTOR2I pad_size = resolved.GetSize( aLayer );
1232
1233 wxXmlNode* chamfered_node = appendNode( entry_node, "RectCham" );
1234 addAttribute( chamfered_node, "width", floatVal( m_scale * pad_size.x ) );
1235 addAttribute( chamfered_node, "height", floatVal( m_scale * pad_size.y ) );
1236
1237 int shorterSide = std::min( pad_size.x, pad_size.y );
1238 int chamfer = std::max( 0, KiROUND( aPad.GetChamferRectRatio( aLayer ) * shorterSide ) );
1239
1240 addAttribute( chamfered_node, "chamfer", floatVal( m_scale * chamfer ) );
1241
1242 int positions = aPad.GetChamferPositions( aLayer );
1243
1244 if( positions & RECT_CHAMFER_TOP_LEFT )
1245 addAttribute( chamfered_node, "upperLeft", "true" );
1246 if( positions & RECT_CHAMFER_TOP_RIGHT )
1247 addAttribute( chamfered_node, "upperRight", "true" );
1248 if( positions & RECT_CHAMFER_BOTTOM_LEFT )
1249 addAttribute( chamfered_node, "lowerLeft", "true" );
1250 if( positions & RECT_CHAMFER_BOTTOM_RIGHT )
1251 addAttribute( chamfered_node, "lowerRight", "true" );
1252 }
1253 else
1254 {
1255 // An isotropically inflated chamfered rectangle is no longer a chamfered
1256 // rectangle (the chamfer corners become rounded), so export the polygon the
1257 // board plotter produces: the original outline inflated with rounded corners.
1258 addContourNode( entry_node, geometry.Contour() );
1259 }
1260
1261 break;
1262 }
1263
1265 {
1266 name = wxString::Format( "TRAPEZOID_%zu", m_std_shape_dict.size() + 1 );
1267 m_std_shape_dict.emplace( hash, name );
1268
1269 wxXmlNode* entry_node = appendNode( m_shape_std_node, "EntryStandard" );
1270 addAttribute( entry_node, "id", name );
1271
1272 addContourNode( entry_node, geometry.Contour() );
1273
1274 break;
1275 }
1276 case PAD_SHAPE::CUSTOM:
1277 {
1278 name = wxString::Format( "CUSTOM_%zu", m_std_shape_dict.size() + 1 );
1279 m_std_shape_dict.emplace( hash, name );
1280
1281 wxXmlNode* entry_node = appendNode( m_shape_std_node, "EntryStandard" );
1282 addAttribute( entry_node, "id", name );
1283
1284 addContourNode( entry_node, geometry.Contour() );
1285 break;
1286 }
1287 default:
1288 Report( _( "Pad has unsupported type; it was skipped." ), RPT_SEVERITY_WARNING );
1289 break;
1290 }
1291
1292 if( !name.empty() )
1293 {
1294 wxXmlNode* shape_node = appendNode( aContentNode, "StandardPrimitiveRef" );
1295 addAttribute( shape_node, "id", name );
1296 }
1297}
1298
1299
1300void PCB_IO_IPC2581::addShape( wxXmlNode* aContentNode, const PCB_SHAPE& aShape, bool aInline )
1301{
1302 size_t hash = hash_fp_item( &aShape, HASH_POS );
1303 auto iter = m_user_shape_dict.find( hash );
1304 wxString name;
1305
1306 // When not inline, check for existing shape in dictionary and reference it
1307 if( !aInline && iter != m_user_shape_dict.end() )
1308 {
1309 wxXmlNode* shape_node = appendNode( aContentNode, "UserPrimitiveRef" );
1310 addAttribute( shape_node, "id", iter->second );
1311 return;
1312 }
1313
1314 switch( aShape.GetShape() )
1315 {
1316 case SHAPE_T::CIRCLE:
1317 {
1318 if( aInline )
1319 {
1320 // For inline shapes (e.g., in Marking elements), output geometry directly as a
1321 // Polyline with two arcs forming a circle
1322 int radius = aShape.GetRadius();
1323 int width = aShape.GetStroke().GetWidth();
1324 LINE_STYLE dash = aShape.GetStroke().GetLineStyle();
1325
1326 wxXmlNode* polyline_node = appendNode( aContentNode, "Polyline" );
1327
1328 // Create a circle using two semicircular arcs
1329 // Start at the rightmost point of the circle
1330 VECTOR2I center = aShape.GetCenter();
1331 VECTOR2I start( center.x + radius, center.y );
1332 VECTOR2I mid( center.x - radius, center.y );
1333
1334 wxXmlNode* begin_node = appendNode( polyline_node, "PolyBegin" );
1335 addXY( begin_node, start );
1336
1337 // First arc from start to mid (top semicircle)
1338 wxXmlNode* arc1_node = appendNode( polyline_node, "PolyStepCurve" );
1339 addXY( arc1_node, mid );
1340 addXY( arc1_node, center, "centerX", "centerY" );
1341 addAttribute( arc1_node, "clockwise", "true" );
1342
1343 // Second arc from mid back to start (bottom semicircle)
1344 wxXmlNode* arc2_node = appendNode( polyline_node, "PolyStepCurve" );
1345 addXY( arc2_node, start );
1346 addXY( arc2_node, center, "centerX", "centerY" );
1347 addAttribute( arc2_node, "clockwise", "true" );
1348
1349 addLineDesc( polyline_node, width, dash, true );
1350
1351 break;
1352 }
1353
1354 name = wxString::Format( "UCIRCLE_%zu", m_user_shape_dict.size() + 1 );
1355 m_user_shape_dict.emplace( hash, name );
1356
1357 int diameter = KiROUND( aShape.GetRadius() * 2.0 );
1358 int width = aShape.GetStroke().GetWidth();
1359 LINE_STYLE dash = aShape.GetStroke().GetLineStyle();
1360
1361
1362 wxXmlNode* entry_node = appendNode( m_shape_user_node, "EntryUser" );
1363 addAttribute( entry_node, "id", name );
1364 wxXmlNode* special_node = appendNode( entry_node, "UserSpecial" );
1365
1366 wxXmlNode* circle_node = appendNode( special_node, "Circle" );
1367
1368 if( aShape.GetFillMode() == FILL_T::NO_FILL )
1369 {
1370 addAttribute( circle_node, "diameter", floatVal( m_scale * diameter ) );
1371 addLineDesc( circle_node, width, dash, true );
1372 }
1373 else
1374 {
1375 // IPC2581 does not allow strokes on filled elements
1376 addAttribute( circle_node, "diameter", floatVal( m_scale * ( diameter + width ) ) );
1377 }
1378
1379 addFillDesc( circle_node, aShape.GetFillMode() );
1380
1381 break;
1382 }
1383
1384 case SHAPE_T::RECTANGLE:
1385 {
1386 if( aInline )
1387 {
1388 // For inline shapes, output as a Polyline with the rectangle corners
1389 int stroke_width = aShape.GetStroke().GetWidth();
1390 LINE_STYLE dash = aShape.GetStroke().GetLineStyle();
1391
1392 wxXmlNode* polyline_node = appendNode( aContentNode, "Polyline" );
1393
1394 // Get the rectangle corners. Use GetRectCorners for proper handling
1395 std::vector<VECTOR2I> corners = aShape.GetRectCorners();
1396
1397 wxXmlNode* begin_node = appendNode( polyline_node, "PolyBegin" );
1398 addXY( begin_node, corners[0] );
1399
1400 for( size_t i = 1; i < corners.size(); ++i )
1401 {
1402 wxXmlNode* step_node = appendNode( polyline_node, "PolyStepSegment" );
1403 addXY( step_node, corners[i] );
1404 }
1405
1406 // Close the rectangle
1407 wxXmlNode* close_node = appendNode( polyline_node, "PolyStepSegment" );
1408 addXY( close_node, corners[0] );
1409
1410 addLineDesc( polyline_node, stroke_width, dash, true );
1411
1412 break;
1413 }
1414
1415 name = wxString::Format( "URECT_%zu", m_user_shape_dict.size() + 1 );
1416 m_user_shape_dict.emplace( hash, name );
1417
1418 wxXmlNode* entry_node = appendNode( m_shape_user_node, "EntryUser" );
1419 addAttribute( entry_node, "id", name );
1420 wxXmlNode* special_node = appendNode( entry_node, "UserSpecial" );
1421
1422 int width = std::abs( aShape.GetRectangleWidth() );
1423 int height = std::abs( aShape.GetRectangleHeight() );
1424 int stroke_width = aShape.GetStroke().GetWidth();
1425 int radius = aShape.GetCornerRadius();
1426
1427 if( aShape.GetFillMode() != FILL_T::NO_FILL )
1428 {
1429 // IPC2581 does not allow strokes on filled elements, so fold the stroke into the
1430 // body; the round stroke joins also round the corners by half the stroke width.
1431 width += stroke_width;
1432 height += stroke_width;
1433 radius += stroke_width / 2;
1434 }
1435
1436 // A rectangle with square corners must be a RectCenter: RectRound requires at least
1437 // one rounded corner (IPC-2581C 3.5.9.14).
1438 wxXmlNode* rect_node = appendNode( special_node, radius > 0 ? "RectRound" : "RectCenter" );
1439
1440 if( aShape.GetFillMode() == FILL_T::NO_FILL )
1441 addLineDesc( rect_node, stroke_width, aShape.GetStroke().GetLineStyle(), true );
1442
1443 addFillDesc( rect_node, aShape.GetFillMode() );
1444
1445 addAttribute( rect_node, "width", floatVal( m_scale * width ) );
1446 addAttribute( rect_node, "height", floatVal( m_scale * height ) );
1447
1448 if( radius > 0 )
1449 {
1450 addAttribute( rect_node, "radius", floatVal( m_scale * radius ) );
1451 addAttribute( rect_node, "upperRight", "true" );
1452 addAttribute( rect_node, "upperLeft", "true" );
1453 addAttribute( rect_node, "lowerRight", "true" );
1454 addAttribute( rect_node, "lowerLeft", "true" );
1455 }
1456
1457 break;
1458 }
1459
1460 case SHAPE_T::POLY:
1461 {
1462 if( aInline )
1463 {
1464 // For inline shapes, output as Polyline elements directly
1465 const SHAPE_POLY_SET& poly_set = aShape.GetPolyShape();
1466 int stroke_width = aShape.GetStroke().GetWidth();
1467 LINE_STYLE dash = aShape.GetStroke().GetLineStyle();
1468
1469 for( int ii = 0; ii < poly_set.OutlineCount(); ++ii )
1470 {
1471 const SHAPE_LINE_CHAIN& outline = poly_set.Outline( ii );
1472
1473 if( outline.PointCount() < 2 )
1474 continue;
1475
1476 wxXmlNode* polyline_node = appendNode( aContentNode, "Polyline" );
1477 const std::vector<VECTOR2I>& pts = outline.CPoints();
1478
1479 wxXmlNode* begin_node = appendNode( polyline_node, "PolyBegin" );
1480 addXY( begin_node, pts[0] );
1481
1482 for( size_t jj = 1; jj < pts.size(); ++jj )
1483 {
1484 wxXmlNode* step_node = appendNode( polyline_node, "PolyStepSegment" );
1485 addXY( step_node, pts[jj] );
1486 }
1487
1488 // Close the polygon if needed
1489 if( pts.size() > 2 && pts.front() != pts.back() )
1490 {
1491 wxXmlNode* close_node = appendNode( polyline_node, "PolyStepSegment" );
1492 addXY( close_node, pts[0] );
1493 }
1494
1495 addLineDesc( polyline_node, stroke_width, dash, true );
1496 }
1497
1498 break;
1499 }
1500
1501 name = wxString::Format( "UPOLY_%zu", m_user_shape_dict.size() + 1 );
1502 m_user_shape_dict.emplace( hash, name );
1503
1504 wxXmlNode* entry_node = appendNode( m_shape_user_node, "EntryUser" );
1505 addAttribute( entry_node, "id", name );
1506
1507 // If we are stroking a polygon, we need two contours. This is only allowed
1508 // inside a "UserSpecial" shape
1509 wxXmlNode* special_node = appendNode( entry_node, "UserSpecial" );
1510
1511 const SHAPE_POLY_SET& poly_set = aShape.GetPolyShape();
1512
1513 for( int ii = 0; ii < poly_set.OutlineCount(); ++ii )
1514 {
1515 if( aShape.GetFillMode() != FILL_T::NO_FILL )
1516 {
1517 // IPC2581 does not allow strokes on filled elements
1518 addContourNode( special_node, poly_set, ii, FILL_T::FILLED_SHAPE, 0, LINE_STYLE::SOLID );
1519 }
1520
1521 addContourNode( special_node, poly_set, ii, FILL_T::NO_FILL, aShape.GetStroke().GetWidth(),
1522 aShape.GetStroke().GetLineStyle() );
1523 }
1524
1525 break;
1526 }
1527
1528 case SHAPE_T::ARC:
1529 {
1530 wxXmlNode* arc_node = appendNode( aContentNode, "Arc" );
1531 addXY( arc_node, aShape.GetStart(), "startX", "startY" );
1532 addXY( arc_node, aShape.GetEnd(), "endX", "endY" );
1533 addXY( arc_node, aShape.GetCenter(), "centerX", "centerY" );
1534
1535 //N.B. because our coordinate system is flipped, we need to flip the arc direction
1536 addAttribute( arc_node, "clockwise", !aShape.IsClockwiseArc() ? "true" : "false" );
1537
1538 addLineDesc( arc_node, aShape.GetStroke().GetWidth(), aShape.GetStroke().GetLineStyle(), true );
1539
1540 break;
1541 }
1542
1543 case SHAPE_T::BEZIER:
1544 {
1545 wxXmlNode* polyline_node = appendNode( aContentNode, "Polyline" );
1546 std::vector<VECTOR2I> ctrlPoints = { aShape.GetStart(), aShape.GetBezierC1(), aShape.GetBezierC2(),
1547 aShape.GetEnd() };
1548 BEZIER_POLY converter( ctrlPoints );
1549 std::vector<VECTOR2I> points;
1550
1551 converter.GetPoly( points, ARC_HIGH_DEF );
1552
1553 wxXmlNode* point_node = appendNode( polyline_node, "PolyBegin" );
1554 addXY( point_node, points[0] );
1555
1556 for( size_t i = 1; i < points.size(); i++ )
1557 {
1558 wxXmlNode* seg_node = appendNode( polyline_node, "PolyStepSegment" );
1559 addXY( seg_node, points[i] );
1560 }
1561
1562 addLineDesc( polyline_node, aShape.GetStroke().GetWidth(), aShape.GetStroke().GetLineStyle(), true );
1563
1564 break;
1565 }
1566
1567 case SHAPE_T::SEGMENT:
1568 {
1569 wxXmlNode* line_node = appendNode( aContentNode, "Line" );
1570 addXY( line_node, aShape.GetStart(), "startX", "startY" );
1571 addXY( line_node, aShape.GetEnd(), "endX", "endY" );
1572
1573 addLineDesc( line_node, aShape.GetStroke().GetWidth(), aShape.GetStroke().GetLineStyle(), true );
1574
1575 break;
1576 }
1577
1578 case SHAPE_T::ELLIPSE:
1580 {
1581 // Tessellate to a polyline
1582 const bool isArc = ( aShape.GetShape() == SHAPE_T::ELLIPSE_ARC );
1583
1584 SHAPE_ELLIPSE e = isArc ? SHAPE_ELLIPSE( aShape.GetEllipseCenter(), aShape.GetEllipseMajorRadius(),
1585 aShape.GetEllipseMinorRadius(), aShape.GetEllipseRotation(),
1586 aShape.GetEllipseStartAngle(), aShape.GetEllipseEndAngle() )
1588 aShape.GetEllipseMinorRadius(), aShape.GetEllipseRotation() );
1589
1591
1592 if( aInline )
1593 {
1594 int stroke_width = aShape.GetStroke().GetWidth();
1595 LINE_STYLE dash = aShape.GetStroke().GetLineStyle();
1596
1597 if( chain.PointCount() < 2 )
1598 break;
1599
1600 wxXmlNode* polyline_node = appendNode( aContentNode, "Polyline" );
1601 const std::vector<VECTOR2I>& pts = chain.CPoints();
1602
1603 wxXmlNode* begin_node = appendNode( polyline_node, "PolyBegin" );
1604 addXY( begin_node, pts[0] );
1605
1606 for( size_t jj = 1; jj < pts.size(); ++jj )
1607 {
1608 wxXmlNode* step_node = appendNode( polyline_node, "PolyStepSegment" );
1609 addXY( step_node, pts[jj] );
1610 }
1611
1612 // Close closed ellipses (not arcs).
1613 if( !isArc && pts.size() > 2 && pts.front() != pts.back() )
1614 {
1615 wxXmlNode* close_node = appendNode( polyline_node, "PolyStepSegment" );
1616 addXY( close_node, pts[0] );
1617 }
1618
1619 addLineDesc( polyline_node, stroke_width, dash, true );
1620
1621 break;
1622 }
1623
1624 name = wxString::Format( "UPOLY_%zu", m_user_shape_dict.size() + 1 );
1625 m_user_shape_dict.emplace( hash, name );
1626
1627 wxXmlNode* entry_node = appendNode( m_shape_user_node, "EntryUser" );
1628 addAttribute( entry_node, "id", name );
1629 wxXmlNode* special_node = appendNode( entry_node, "UserSpecial" );
1630
1631 SHAPE_POLY_SET poly_set;
1632 poly_set.NewOutline();
1633
1634 for( const VECTOR2I& pt : chain.CPoints() )
1635 poly_set.Append( pt );
1636
1637 if( aShape.GetFillMode() != FILL_T::NO_FILL && !isArc )
1638 {
1639 // IPC2581 does not allow strokes on filled elements
1640 addContourNode( special_node, poly_set, 0, FILL_T::FILLED_SHAPE, 0, LINE_STYLE::SOLID );
1641 }
1642
1643 addContourNode( special_node, poly_set, 0, FILL_T::NO_FILL, aShape.GetStroke().GetWidth(),
1644 aShape.GetStroke().GetLineStyle() );
1645
1646 break;
1647 }
1648
1649 case SHAPE_T::UNDEFINED:
1650 wxFAIL;
1651 }
1652
1653 // Only add UserPrimitiveRef when not in inline mode and a dictionary entry was created
1654 if( !aInline && !name.empty() )
1655 {
1656 wxXmlNode* shape_node = appendNode( aContentNode, "UserPrimitiveRef" );
1657 addAttribute( shape_node, "id", name );
1658 }
1659
1660}
1661
1662
1663void PCB_IO_IPC2581::addSlotCavity( wxXmlNode* aNode, const PAD& aPad, const wxString& aName )
1664{
1665 wxXmlNode* slotNode = appendNode( aNode, "SlotCavity" );
1666 addAttribute( slotNode, "name", aName );
1667 addAttribute( slotNode, "platingStatus", aPad.GetAttribute() == PAD_ATTRIB::PTH ? "PLATED"
1668 : "NONPLATED" );
1669 addAttribute( slotNode, "plusTol", "0.0" );
1670 addAttribute( slotNode, "minusTol", "0.0" );
1671
1673 addLocationNode( slotNode, aPad.GetPosition().x, aPad.GetPosition().y );
1674
1675 // B1 SlotCavity accepts only Simple shapes, so revision B writes the hole outline
1677 {
1678 VECTOR2I drill_size = aPad.GetDrillSize();
1679 EDA_ANGLE rotation = aPad.GetOrientation().Normalize();
1680
1681 // IPC-2581C requires width >= height for Oval primitive
1682 // Swap dimensions if needed and adjust rotation accordingly
1683 if( drill_size.y > drill_size.x )
1684 {
1685 std::swap( drill_size.x, drill_size.y );
1686 rotation = ( rotation + ANGLE_90 ).Normalize();
1687 }
1688
1689 // Add Xform if rotation is needed (must come before Feature per IPC-2581C schema)
1690 if( rotation != ANGLE_0 )
1691 {
1692 wxXmlNode* xformNode = appendNode( slotNode, "Xform" );
1693 addAttribute( xformNode, "rotation", floatVal( rotation.AsDegrees() ) );
1694 }
1695
1696 // Use IPC-2581 Oval primitive for oblong slots
1697 wxXmlNode* ovalNode = appendNode( slotNode, "Oval" );
1698 addAttribute( ovalNode, "width", floatVal( m_scale * drill_size.x ) );
1699 addAttribute( ovalNode, "height", floatVal( m_scale * drill_size.y ) );
1700 }
1701 else
1702 {
1703 // Polygon outline for non-oblong drills and revision B
1704 SHAPE_POLY_SET poly_set;
1705 int maxError = m_board->GetDesignSettings().m_MaxError;
1706 aPad.TransformHoleToPolygon( poly_set, 0, maxError, ERROR_INSIDE );
1707
1708 addOutlineNode( slotNode, poly_set );
1709 }
1710}
1711
1712
1714{
1715 wxXmlNode* logisticNode = appendNode( m_xml_root, "LogisticHeader" );
1716
1717 wxXmlNode* roleNode = appendNode( logisticNode, "Role" );
1718 addAttribute( roleNode, "id", "Owner" );
1719 addAttribute( roleNode, "roleFunction", "SENDER" );
1720
1721 m_enterpriseNode = appendNode( logisticNode, "Enterprise" );
1722 addAttribute( m_enterpriseNode, "id", "UNKNOWN" );
1723 addAttribute( m_enterpriseNode, "code", "NONE" );
1724
1725 wxXmlNode* personNode = appendNode( logisticNode, "Person" );
1726 addAttribute( personNode, "name", "UNKNOWN" );
1727 addAttribute( personNode, "enterpriseRef", "UNKNOWN" );
1728 addAttribute( personNode, "roleRef", "Owner" );
1729
1730 return logisticNode;
1731}
1732
1733
1735{
1736 reportPhase( _( "Generating history section" ) );
1737
1738 wxXmlNode* historyNode = appendNode( m_xml_root, "HistoryRecord" );
1739 addAttribute( historyNode, "number", "1" );
1740 addAttribute( historyNode, "origination", wxDateTime::Now().FormatISOCombined() );
1741 addAttribute( historyNode, "software", "KiCad EDA" );
1742 addAttribute( historyNode, "lastChange", wxDateTime::Now().FormatISOCombined() );
1743
1744 wxXmlNode* fileRevisionNode = appendNode( historyNode, "FileRevision" );
1745 addAttribute( fileRevisionNode, "fileRevisionId", "1" );
1746 addAttribute( fileRevisionNode, "comment", "NO COMMENT" );
1747 addAttribute( fileRevisionNode, "label", "NO LABEL" );
1748
1749 wxXmlNode* softwarePackageNode = appendNode( fileRevisionNode, "SoftwarePackage" );
1750 addAttribute( softwarePackageNode, "name", "KiCad" );
1751 addAttribute( softwarePackageNode, "revision", GetMajorMinorPatchVersion() );
1752 addAttribute( softwarePackageNode, "vendor", "KiCad EDA" );
1753
1754 wxXmlNode* certificationNode = appendNode( softwarePackageNode, "Certification" );
1755 addAttribute( certificationNode, "certificationStatus", "SELFTEST" );
1756
1757 return historyNode;
1758}
1759
1760
1761wxXmlNode* PCB_IO_IPC2581::generateBOMSection( wxXmlNode* aEcadNode )
1762{
1763 if( !included( IPC2581::SECTION::BOM_AVL ) )
1764 return nullptr;
1765
1766 reportPhase( _( "Generating BOM section" ) );
1767
1768 struct REFDES
1769 {
1770 wxString m_name;
1771 wxString m_pkg;
1772 bool m_populate;
1773 wxString m_layer;
1774 };
1775
1776 struct BOM_ENTRY
1777 {
1778 BOM_ENTRY()
1779 {
1780 m_refdes = new std::vector<REFDES>();
1781 m_props = new std::map<wxString, wxString>();
1782 m_count = 0;
1783 m_pads = 0;
1784 }
1785
1786 ~BOM_ENTRY()
1787 {
1788 delete m_refdes;
1789 delete m_props;
1790 }
1791
1792 wxString m_OEMDesignRef; // String combining LIB+FP+VALUE
1793 int m_count;
1794 int m_pads;
1795 wxString m_type;
1796 wxString m_description;
1797
1798 std::vector<REFDES>* m_refdes;
1799 std::map<wxString, wxString>* m_props;
1800 };
1801
1802 std::set<std::unique_ptr<BOM_ENTRY>,
1803 std::function<bool( const std::unique_ptr<BOM_ENTRY>&, const std::unique_ptr<BOM_ENTRY>& )>> bom_entries(
1804 []( const std::unique_ptr<BOM_ENTRY>& a, const std::unique_ptr<BOM_ENTRY>& b )
1805 {
1806 return a->m_OEMDesignRef < b->m_OEMDesignRef;
1807 } );
1808
1809 for( FOOTPRINT* fp_it : m_board->Footprints() )
1810 {
1812 std::unique_ptr<FOOTPRINT> fp( static_cast<FOOTPRINT*>( fp_it->Clone() ) );
1813 fp->SetParentGroup( nullptr );
1814 fp->SetPosition( {0, 0} );
1815 fp->SetOrientation( ANGLE_0 );
1816
1817 // Normalize to unflipped state to match hash computed in addPackage
1818 if( fp->IsFlipped() )
1819 fp->Flip( fp->GetPosition(), FLIP_DIRECTION::TOP_BOTTOM );
1820
1821 size_t hash = hash_fp_item( fp.get(), HASH_POS | RELATIVE_TO_PARENT_COORDS );
1822 auto iter = m_footprint_dict.find( hash );
1823
1824 if( iter == m_footprint_dict.end() )
1825 {
1826 Report( wxString::Format( _( "Footprint %s not found in dictionary; BOM data may be incomplete." ),
1827 fp->GetFPID().GetLibItemName().wx_str() ),
1829 continue;
1830 }
1831
1832 auto entry = std::make_unique<struct BOM_ENTRY>();
1833
1836 if( auto it = m_OEMRef_dict.find( fp_it ); it != m_OEMRef_dict.end() )
1837 {
1838 entry->m_OEMDesignRef = it->second;
1839 }
1840 else
1841 {
1842 Report( wxString::Format( _( "Component \"%s\" missing OEM reference; BOM entry will be skipped." ),
1843 fp->GetFPID().GetLibItemName().wx_str() ),
1845 }
1846
1847 entry->m_OEMDesignRef = genString( entry->m_OEMDesignRef, "REF" );
1848 entry->m_count = 1;
1849 entry->m_pads = fp->GetPadCount();
1850
1851 // TODO: The options are "ELECTRICAL", "MECHANICAL", "PROGRAMMABLE", "DOCUMENT", "MATERIAL"
1852 // We need to figure out how to determine this.
1853 if( entry->m_pads == 0 || row.m_excludedFromBom )
1854 entry->m_type = "DOCUMENT";
1855 else
1856 entry->m_type = "ELECTRICAL";
1857
1858 // Use the footprint's Description field if it exists
1859 const PCB_FIELD* descField = fp_it->GetField( FIELD_T::DESCRIPTION );
1860
1861 if( descField && !descField->GetShownText( FOR_GUI ).IsEmpty() )
1862 entry->m_description = descField->GetShownText( FOR_GUI );
1863
1864 auto[ bom_iter, inserted ] = bom_entries.insert( std::move( entry ) );
1865
1866 if( !inserted )
1867 ( *bom_iter )->m_count++;
1868
1869 REFDES refdes;
1870 refdes.m_name = componentName( fp_it );
1871 refdes.m_pkg = iter->second;
1872 refdes.m_populate = !row.m_dnp && !row.m_excludedFromBom;
1873 refdes.m_layer = m_layer_name_map[fp_it->GetLayer()];
1874
1875 ( *bom_iter )->m_refdes->push_back( refdes );
1876
1877 // TODO: This amalgamates all the properties from all the footprints. We need to decide
1878 // if we want to group footprints by their properties
1879 for( PCB_FIELD* prop : fp_it->GetFields() )
1880 {
1881 // We don't include Reference, Datasheet, or Description in BOM characteristics.
1882 // Value and any user-defined fields are included. Reference is captured above,
1883 // and Description is used for the BomItem description attribute.
1884 if( prop->IsMandatory() && !prop->IsValue() )
1885 continue;
1886
1887 ( *bom_iter )->m_props->emplace( prop->GetName(), row.Field( prop->GetName() ) );
1888 }
1889 }
1890
1891 if( bom_entries.empty() )
1892 return nullptr;
1893
1894 wxFileName fn( m_board->GetFileName() );
1895
1896 wxXmlNode* bomNode = new wxXmlNode( wxXML_ELEMENT_NODE, "Bom" );
1897 m_xml_root->InsertChild( bomNode, aEcadNode );
1898 addAttribute( bomNode, "name", genString( fn.GetName(), "BOM" ) );
1899
1900 wxXmlNode* bomHeaderNode = appendNode( bomNode, "BomHeader" );
1901 wxString bomRevision = m_bomRev;
1902
1903 if( bomRevision.IsEmpty() )
1904 bomRevision = m_board->GetTitleBlock().GetRevision();
1905
1906 if( bomRevision.IsEmpty() )
1907 bomRevision = wxS( "1.0" );
1908
1909 addAttribute( bomHeaderNode, "revision", bomRevision );
1910 addAttribute( bomHeaderNode, "assembly", genString( fn.GetName() ) );
1911
1912 if( m_haveCadData )
1913 {
1914 wxXmlNode* stepRefNode = appendNode( bomHeaderNode, "StepRef" );
1915 addAttribute( stepRefNode, "name", genString( fn.GetName(), "BOARD" ) );
1916 }
1917
1918 for( const std::unique_ptr<BOM_ENTRY>& entry : bom_entries )
1919 {
1920 wxXmlNode* bomEntryNode = appendNode( bomNode, "BomItem" );
1921 addAttribute( bomEntryNode, "OEMDesignNumberRef", entry->m_OEMDesignRef );
1922 addAttribute( bomEntryNode, "quantity", wxString::Format( "%d", entry->m_count ) );
1923 addAttribute( bomEntryNode, "pinCount", wxString::Format( "%d", entry->m_pads ) );
1924 addAttribute( bomEntryNode, "category", entry->m_type );
1925
1926 if( !entry->m_description.IsEmpty() )
1927 addAttribute( bomEntryNode, "description", entry->m_description );
1928
1929 for( const REFDES& refdes : *( entry->m_refdes ) )
1930 {
1932 break;
1933
1934 wxXmlNode* refdesNode = appendNode( bomEntryNode, "RefDes" );
1935 addAttribute( refdesNode, "name", refdes.m_name );
1936
1937 if( !suppressed( IPC2581::SUPPRESS::BOM_REFDES_PACKAGEREF ) && !refdes.m_pkg.IsEmpty() )
1938 addAttribute( refdesNode, "packageRef", refdes.m_pkg );
1939
1940 addAttribute( refdesNode, "populate", refdes.m_populate ? "true" : "false" );
1941
1942 if( !suppressed( IPC2581::SUPPRESS::BOM_REFDES_LAYERREF ) && !refdes.m_layer.IsEmpty() )
1943 addAttribute( refdesNode, "layerRef", refdes.m_layer );
1944 }
1945
1946 wxXmlNode* characteristicsNode = appendNode( bomEntryNode, "Characteristics" );
1947 addAttribute( characteristicsNode, "category", entry->m_type );
1948
1949 for( const auto& [propName, propValue] : *( entry->m_props ) )
1950 {
1951 wxXmlNode* textualDefNode = appendNode( characteristicsNode, "Textual" );
1952 addAttribute( textualDefNode, "definitionSource", "KICAD" );
1953 addAttribute( textualDefNode, "textualCharacteristicName", propName );
1954 addAttribute( textualDefNode, "textualCharacteristicValue", propValue );
1955 }
1956 }
1957
1958 return bomNode;
1959}
1960
1961
1963{
1964 reportPhase( _( "Generating CAD data" ) );
1965
1966 wxXmlNode* ecadNode = appendNode( m_xml_root, "Ecad" );
1967 addAttribute( ecadNode, "name", "Design" );
1968
1969 addCadHeader( ecadNode );
1970
1971 wxXmlNode* cadDataNode = appendNode( ecadNode, "CadData" );
1972 generateCadLayers( cadDataNode );
1973
1974 if( included( IPC2581::SECTION::DRILL_ROUT ) )
1975 generateDrillLayers( cadDataNode );
1976
1977 if( included( IPC2581::SECTION::MISC_FAB ) )
1978 generateAuxilliaryLayers( cadDataNode );
1979
1980 if( included( IPC2581::SECTION::STACKUP ) )
1981 generateStackup( cadDataNode );
1982
1983 generateStepSection( cadDataNode );
1984
1986
1987 // CadData needs a minimum of one Layer
1988 // The loop above must run because the BOM reads the dictionaries that it fills
1989 bool haveLayer = false;
1990
1991 for( wxXmlNode* child = cadDataNode->GetChildren(); child; child = child->GetNext() )
1992 {
1993 if( child->GetName() == "Layer" )
1994 {
1995 haveLayer = true;
1996 break;
1997 }
1998 }
1999
2000 m_haveCadData = IPC2581::NeedsCadData( m_resolved.m_included ) && haveLayer;
2001
2002 if( !m_haveCadData )
2003 {
2004 deleteNode( cadDataNode );
2006 return ecadNode;
2007 }
2008
2009 generateContentLayerRefs( cadDataNode );
2010
2011 return ecadNode;
2012}
2013
2014
2015void PCB_IO_IPC2581::generateCadSpecs( wxXmlNode* aCadLayerNode )
2016{
2017 const BOARD_STACKUP& stackup = m_fabStackup->Stackup();
2018
2019 std::vector<BOARD_STACKUP_ITEM*> layers = stackup.GetList();
2020 std::set<PCB_LAYER_ID> added_layers;
2021
2022 for( int i = 0; i < stackup.GetCount(); i++ )
2023 {
2024 BOARD_STACKUP_ITEM* stackup_item = layers.at( i );
2025
2026 for( int sublayer_id = 0; sublayer_id < stackup_item->GetSublayersCount(); sublayer_id++ )
2027 {
2028 wxString ly_name = stackupLayerName( stackup_item, sublayer_id, "SPEC_LAYER" );
2029
2030 wxXmlNode* specNode = appendNode( aCadLayerNode, "Spec" );
2031 addAttribute( specNode, "name", ly_name );
2032 wxXmlNode* generalNode = appendNode( specNode, "General" );
2033 addAttribute( generalNode, "type", "MATERIAL" );
2034 wxXmlNode* propertyNode = appendNode( generalNode, "Property" );
2035
2036 switch ( stackup_item->GetType() )
2037 {
2039 {
2040 addAttribute( propertyNode, "text", "COPPER" );
2041 wxXmlNode* conductorNode = appendNode( specNode, "Conductor" );
2042 addAttribute( conductorNode, "type", "CONDUCTIVITY" );
2043 propertyNode = appendNode( conductorNode, "Property" );
2044 addAttribute( propertyNode, "unit", wxT( "SIEMENS/M" ) );
2045 addAttribute( propertyNode, "value", wxT( "5.959E7" ) );
2046 break;
2047 }
2048
2050 {
2051 addAttribute( propertyNode, "text", stackup_item->GetMaterial() );
2052 propertyNode = appendNode( generalNode, "Property" );
2053 addAttribute( propertyNode, "text", wxString::Format( "Type : %s",
2054 stackup_item->GetTypeName() ) );
2055 wxXmlNode* dielectricNode = appendNode( specNode, "Dielectric" );
2056 addAttribute( dielectricNode, "type", "DIELECTRIC_CONSTANT" );
2057 propertyNode = appendNode( dielectricNode, "Property" );
2058 addAttribute( propertyNode, "value", floatVal( stackup_item->GetEpsilonR( sublayer_id ) ) );
2059 dielectricNode = appendNode( specNode, "Dielectric" );
2060 addAttribute( dielectricNode, "type", "LOSS_TANGENT" );
2061 propertyNode = appendNode( dielectricNode, "Property" );
2062 addAttribute( propertyNode, "value", floatVal( stackup_item->GetLossTangent( sublayer_id ) ) );
2063 break;
2064 }
2065
2067 addAttribute( propertyNode, "text", stackup_item->GetTypeName() );
2068 propertyNode = appendNode( generalNode, "Property" );
2069 addAttribute( propertyNode, "text", wxString::Format( "Color : %s", stackup_item->GetColor() ) );
2070 propertyNode = appendNode( generalNode, "Property" );
2071 addAttribute( propertyNode, "text", wxString::Format( "Type : %s", stackup_item->GetTypeName() ) );
2072 break;
2073
2075 {
2076 addAttribute( propertyNode, "text", "SOLDERMASK" );
2077 propertyNode = appendNode( generalNode, "Property" );
2078 addAttribute( propertyNode, "text", wxString::Format( "Color : %s", stackup_item->GetColor() ) );
2079 propertyNode = appendNode( generalNode, "Property" );
2080 addAttribute( propertyNode, "text", wxString::Format( "Type : %s", stackup_item->GetTypeName() ) );
2081
2082 // Generate Epsilon R if > 1.0 (value <= 1.0 means not specified)
2083 if( stackup_item->GetEpsilonR( sublayer_id ) > 1.0 )
2084 {
2085 wxXmlNode* dielectricNode = appendNode( specNode, "Dielectric" );
2086 addAttribute( dielectricNode, "type", "DIELECTRIC_CONSTANT" );
2087 propertyNode = appendNode( dielectricNode, "Property" );
2088 addAttribute( propertyNode, "value", floatVal( stackup_item->GetEpsilonR( sublayer_id ) ) );
2089 }
2090
2091 // Generate LossTangent if > 0.0 (value <= 0.0 means not specified)
2092 if( stackup_item->GetLossTangent( sublayer_id ) > 0.0 )
2093 {
2094 wxXmlNode* dielectricNode = appendNode( specNode, "Dielectric" );
2095 addAttribute( dielectricNode, "type", "LOSS_TANGENT" );
2096 propertyNode = appendNode( dielectricNode, "Property" );
2097 addAttribute( propertyNode, "value", floatVal( stackup_item->GetLossTangent( sublayer_id ) ) );
2098 }
2099
2100 break;
2101 }
2102
2103 default:
2104 break;
2105 }
2106 }
2107 }
2108
2109 // SurfaceFinish is only defined as a SpecificationType in IPC-2581C
2111 {
2112 surfaceFinishType finishType = getSurfaceFinishType( stackup.m_FinishType );
2113
2114 if( finishType != surfaceFinishType::NONE )
2115 {
2116 wxXmlNode* specNode = appendNode( aCadLayerNode, "Spec" );
2117 addAttribute( specNode, "name", "SURFACE_FINISH" );
2118
2119 wxXmlNode* surfaceFinishNode = appendNode( specNode, "SurfaceFinish" );
2120 addAttribute( surfaceFinishNode, "type", surfaceFinishTypeToString.at( finishType ) );
2121
2122 if( finishType == surfaceFinishType::OTHER )
2123 addAttribute( surfaceFinishNode, "comment", stackup.m_FinishType );
2124 }
2125 }
2126}
2127
2128
2129void PCB_IO_IPC2581::addCadHeader( wxXmlNode* aEcadNode )
2130{
2131 wxXmlNode* cadHeaderNode = appendNode( aEcadNode, "CadHeader" );
2132 addAttribute( cadHeaderNode, "units", m_units_str );
2133
2134 m_cad_header_node = cadHeaderNode;
2135
2136 if( included( IPC2581::SECTION::STACKUP ) || included( IPC2581::SECTION::DIELECTRIC ) )
2137 generateCadSpecs( cadHeaderNode );
2138}
2139
2140
2141void PCB_IO_IPC2581::addLayerAttributes( wxXmlNode* aNode, PCB_LAYER_ID aLayer )
2142{
2143 switch( aLayer )
2144 {
2145 case F_Adhes:
2146 case B_Adhes:
2147 addAttribute( aNode, "layerFunction", "GLUE" );
2148 addAttribute( aNode, "polarity", "POSITIVE" );
2149 addAttribute( aNode, "side", aLayer == F_Adhes ? "TOP" : "BOTTOM" );
2150 break;
2151
2152 case F_Paste:
2153 case B_Paste:
2154 addAttribute( aNode, "layerFunction", "SOLDERPASTE" );
2155 addAttribute( aNode, "polarity", "POSITIVE" );
2156 addAttribute( aNode, "side", aLayer == F_Paste ? "TOP" : "BOTTOM" );
2157 break;
2158
2159 case F_SilkS:
2160 case B_SilkS:
2161 addAttribute( aNode, "layerFunction", "SILKSCREEN" );
2162 addAttribute( aNode, "polarity", "POSITIVE" );
2163 addAttribute( aNode, "side", aLayer == F_SilkS ? "TOP" : "BOTTOM" );
2164 break;
2165
2166 case F_Mask:
2167 case B_Mask:
2168 addAttribute( aNode, "layerFunction", "SOLDERMASK" );
2169 addAttribute( aNode, "polarity", "POSITIVE" );
2170 addAttribute( aNode, "side", aLayer == F_Mask ? "TOP" : "BOTTOM" );
2171 break;
2172
2173 case Edge_Cuts:
2174 addAttribute( aNode, "layerFunction", "BOARD_OUTLINE" );
2175 addAttribute( aNode, "polarity", "POSITIVE" );
2176 addAttribute( aNode, "side", "ALL" );
2177 break;
2178
2179 case B_CrtYd:
2180 case F_CrtYd:
2181 addAttribute( aNode, "layerFunction", "COURTYARD" );
2182 addAttribute( aNode, "polarity", "POSITIVE" );
2183 addAttribute( aNode, "side", aLayer == F_CrtYd ? "TOP" : "BOTTOM" );
2184 break;
2185
2186 case B_Fab:
2187 case F_Fab:
2188 addAttribute( aNode, "layerFunction", "ASSEMBLY" );
2189 addAttribute( aNode, "polarity", "POSITIVE" );
2190 addAttribute( aNode, "side", aLayer == F_Fab ? "TOP" : "BOTTOM" );
2191 break;
2192
2193 case Dwgs_User:
2194 case Cmts_User:
2195 case Eco1_User:
2196 case Eco2_User:
2197 case Margin:
2198 addAttribute( aNode, "layerFunction", "DOCUMENT" );
2199 addAttribute( aNode, "polarity", "POSITIVE" );
2200 addAttribute( aNode, "side", "NONE" );
2201 break;
2202
2203 default:
2204 if( IsCopperLayer( aLayer ) )
2205 {
2206 addAttribute( aNode, "layerFunction", "CONDUCTOR" );
2207 addAttribute( aNode, "polarity", "POSITIVE" );
2208 addAttribute( aNode, "side", aLayer == F_Cu ? "TOP"
2209 : aLayer == B_Cu ? "BOTTOM"
2210 : "INTERNAL" );
2211 }
2212 else if( IsUserLayer( aLayer ) )
2213 {
2214 addAttribute( aNode, "layerFunction", "DOCUMENT" );
2215 addAttribute( aNode, "polarity", "POSITIVE" );
2216 addAttribute( aNode, "side", "NONE" );
2217 }
2218
2219 break; // Do not handle other layers
2220 }
2221}
2222
2223
2224void PCB_IO_IPC2581::generateStackup( wxXmlNode* aCadLayerNode )
2225{
2226 const BOARD_STACKUP& stackup = m_fabStackup->Stackup();
2227
2228 // The coating goes with the miscellaneous fab layers and its Spec needs IPC-2581C
2229 surfaceFinishType finishType = getSurfaceFinishType( stackup.m_FinishType );
2230 bool hasCoating = ( m_revision == IPC2581::REVISION::C
2231 && finishType != surfaceFinishType::NONE
2232 && included( IPC2581::SECTION::MISC_FAB ) );
2233
2234 wxXmlNode* stackupNode = appendNode( aCadLayerNode, "Stackup" );
2235 addAttribute( stackupNode, "name", "Primary_Stackup" );
2236 addAttribute( stackupNode, "overallThickness", floatVal( m_scale * stackup.BuildBoardThicknessFromStackup() ) );
2237 addAttribute( stackupNode, "tolPlus", "0.0" );
2238 addAttribute( stackupNode, "tolMinus", "0.0" );
2239 addAttribute( stackupNode, "whereMeasured", "MASK" );
2240
2242 addAttribute( stackupNode, "stackupStatus", "PROPOSED" );
2243
2244 wxXmlNode* stackupGroup = appendNode( stackupNode, "StackupGroup" );
2245 addAttribute( stackupGroup, "name", "Primary_Stackup_Group" );
2246 addAttribute( stackupGroup, "thickness", floatVal( m_scale * stackup.BuildBoardThicknessFromStackup() ) );
2247 addAttribute( stackupGroup, "tolPlus", "0.0" );
2248 addAttribute( stackupGroup, "tolMinus", "0.0" );
2249
2250 std::vector<BOARD_STACKUP_ITEM*> layers = stackup.GetList();
2251 std::set<PCB_LAYER_ID> added_layers;
2252 int sequence = 0;
2253
2254 for( int i = 0; i < stackup.GetCount(); i++ )
2255 {
2256 BOARD_STACKUP_ITEM* stackup_item = layers.at( i );
2257
2258 for( int sublayer_id = 0; sublayer_id < stackup_item->GetSublayersCount(); sublayer_id++ )
2259 {
2260 PCB_LAYER_ID layer_id = stackup_item->GetBrdLayerId();
2261
2262 // Insert top coating layer before F.Cu
2263 if( hasCoating && layer_id == F_Cu && sublayer_id == 0 )
2264 {
2265 wxXmlNode* coatingLayer = appendNode( stackupGroup, "StackupLayer" );
2266 addAttribute( coatingLayer, "layerOrGroupRef", "COATING_TOP" );
2267 addAttribute( coatingLayer, "thickness", "0.0" );
2268 addAttribute( coatingLayer, "tolPlus", "0.0" );
2269 addAttribute( coatingLayer, "tolMinus", "0.0" );
2270 addAttribute( coatingLayer, "sequence", wxString::Format( "%d", sequence++ ) );
2271
2272 wxXmlNode* specRefNode = appendNode( coatingLayer, "SpecRef" );
2273 addAttribute( specRefNode, "id", "SURFACE_FINISH" );
2274 }
2275
2276 // layerOrGroupKeyRef connects each row to a declared layer
2277 if( !stackupRowIncluded( stackup_item->GetType() == BS_ITEM_TYPE_DIELECTRIC, layer_id ) )
2278 continue;
2279
2280 wxXmlNode* stackupLayer = appendNode( stackupGroup, "StackupLayer" );
2281 wxString spec_name = stackupLayerName( stackup_item, sublayer_id, "SPEC_LAYER" );
2282 wxString ly_name = stackupLayerName( stackup_item, sublayer_id, "LAYER" );
2283
2284 addAttribute( stackupLayer, "layerOrGroupRef", ly_name );
2285 addAttribute( stackupLayer, "thickness", floatVal( m_scale * stackup_item->GetThickness() ) );
2286 addAttribute( stackupLayer, "tolPlus", "0.0" );
2287 addAttribute( stackupLayer, "tolMinus", "0.0" );
2288 addAttribute( stackupLayer, "sequence", wxString::Format( "%d", sequence++ ) );
2289
2290 wxXmlNode* specLayerNode = appendNode( stackupLayer, "SpecRef" );
2291 addAttribute( specLayerNode, "id", spec_name );
2292
2293 // Insert bottom coating layer after B.Cu
2294 if( hasCoating && layer_id == B_Cu && sublayer_id == stackup_item->GetSublayersCount() - 1 )
2295 {
2296 wxXmlNode* coatingLayer = appendNode( stackupGroup, "StackupLayer" );
2297 addAttribute( coatingLayer, "layerOrGroupRef", "COATING_BOTTOM" );
2298 addAttribute( coatingLayer, "thickness", "0.0" );
2299 addAttribute( coatingLayer, "tolPlus", "0.0" );
2300 addAttribute( coatingLayer, "tolMinus", "0.0" );
2301 addAttribute( coatingLayer, "sequence", wxString::Format( "%d", sequence++ ) );
2302
2303 wxXmlNode* specRefNode = appendNode( coatingLayer, "SpecRef" );
2304 addAttribute( specRefNode, "id", "SURFACE_FINISH" );
2305 }
2306 }
2307 }
2308}
2309
2310
2311void PCB_IO_IPC2581::generateCadLayers( wxXmlNode* aCadLayerNode )
2312{
2313 const BOARD_STACKUP& stackup = m_fabStackup->Stackup();
2314
2315 std::vector<BOARD_STACKUP_ITEM*> layers = stackup.GetList();
2316 std::set<PCB_LAYER_ID> added_layers;
2317
2318 for( int i = 0; i < stackup.GetCount(); i++ )
2319 {
2320 BOARD_STACKUP_ITEM* stackup_item = layers.at( i );
2321
2322 for( int sublayer_id = 0; sublayer_id < stackup_item->GetSublayersCount(); sublayer_id++ )
2323 {
2324 wxString ly_name = stackupLayerName( stackup_item, sublayer_id, "LAYER" );
2325 bool isDielectric = stackup_item->GetType() == BS_ITEM_TYPE_DIELECTRIC;
2326
2327 if( !stackupRowIncluded( isDielectric, stackup_item->GetBrdLayerId() ) )
2328 {
2329 if( !isDielectric )
2330 m_layer_name_map.emplace( stackup_item->GetBrdLayerId(), ly_name );
2331
2332 continue;
2333 }
2334
2335 wxXmlNode* cadLayerNode = appendNode( aCadLayerNode, "Layer" );
2336 addAttribute( cadLayerNode, "name", ly_name );
2337
2338 if( isDielectric )
2339 {
2340
2341 if( stackup_item->GetTypeName() == KEY_CORE )
2342 addAttribute( cadLayerNode, "layerFunction", "DIELCORE" );
2343 else
2344 addAttribute( cadLayerNode, "layerFunction", "DIELPREG" );
2345
2346 addAttribute( cadLayerNode, "polarity", "POSITIVE" );
2347 addAttribute( cadLayerNode, "side", "INTERNAL" );
2348 continue;
2349 }
2350 else
2351 {
2352 added_layers.insert( stackup_item->GetBrdLayerId() );
2353 addLayerAttributes( cadLayerNode, stackup_item->GetBrdLayerId() );
2354 m_layer_name_map.emplace( stackup_item->GetBrdLayerId(), ly_name );
2355 }
2356 }
2357 }
2358
2359 LSEQ layer_seq = m_board->GetEnabledLayers().Seq();
2360
2361 for( PCB_LAYER_ID layer : layer_seq )
2362 {
2363 if( added_layers.find( layer ) != added_layers.end() || !layerIncluded( layer ) )
2364 continue;
2365
2366 wxString layerName = genLayerString( layer, "LAYER" );
2367 m_layer_name_map.emplace( layer, layerName );
2368 added_layers.insert( layer );
2369 wxXmlNode* cadLayerNode = appendNode( aCadLayerNode, "Layer" );
2370 addAttribute( cadLayerNode, "name", layerName );
2371
2372 addLayerAttributes( cadLayerNode, layer );
2373 }
2374
2375 // COATINGCOND goes with the miscellaneous fab layers and its Spec needs IPC-2581C
2376 if( m_revision == IPC2581::REVISION::C && included( IPC2581::SECTION::MISC_FAB ) )
2377 {
2378 surfaceFinishType finishType = getSurfaceFinishType( stackup.m_FinishType );
2379
2380 if( finishType != surfaceFinishType::NONE )
2381 {
2382 wxXmlNode* topCoatingNode = appendNode( aCadLayerNode, "Layer" );
2383 addAttribute( topCoatingNode, "name", "COATING_TOP" );
2384 addAttribute( topCoatingNode, "layerFunction", "COATINGCOND" );
2385 addAttribute( topCoatingNode, "side", "TOP" );
2386 addAttribute( topCoatingNode, "polarity", "POSITIVE" );
2387
2388 wxXmlNode* botCoatingNode = appendNode( aCadLayerNode, "Layer" );
2389 addAttribute( botCoatingNode, "name", "COATING_BOTTOM" );
2390 addAttribute( botCoatingNode, "layerFunction", "COATINGCOND" );
2391 addAttribute( botCoatingNode, "side", "BOTTOM" );
2392 addAttribute( botCoatingNode, "polarity", "POSITIVE" );
2393 }
2394 }
2395}
2396
2397
2398void PCB_IO_IPC2581::generateDrillLayers( wxXmlNode* aCadLayerNode )
2399{
2400 for( const FAB_DRILL_LAYER& layer : m_fabDrillModel->Layers() )
2401 {
2402 if( layer.m_Span.m_IsBackdrill )
2403 continue;
2404
2405 for( const DRILL_OPERATION& op : layer.m_Holes )
2406 m_drill_layers[layer.m_Span].push_back( op.m_SourceItem );
2407
2408 for( const DRILL_OPERATION& op : layer.m_Slots )
2409 {
2410 if( op.m_SourceItem->Type() == PCB_PAD_T )
2411 m_slot_holes[layer.m_Span.Pair()].push_back( static_cast<PAD*>( op.m_SourceItem ) );
2412 }
2413 }
2414
2415 for( const auto& [span, vec] : m_drill_layers )
2416 {
2417 wxXmlNode* drillNode = appendNode( aCadLayerNode, "Layer" );
2418 drillNode->AddAttribute( "name", genLayersString( span.TopLayer(), span.BottomLayer(), "DRILL" ) );
2419 addAttribute( drillNode, "layerFunction", "DRILL" );
2420 addAttribute( drillNode, "polarity", "POSITIVE" );
2421 addAttribute( drillNode, "side", "ALL" );
2422
2423 wxXmlNode* spanNode = appendNode( drillNode, "Span" );
2424 addAttribute( spanNode, "fromLayer", genLayerString( span.TopLayer(), "LAYER" ) );
2425 addAttribute( spanNode, "toLayer", genLayerString( span.BottomLayer(), "LAYER" ) );
2426 }
2427
2428 for( const auto& [layers, vec] : m_slot_holes )
2429 {
2430 wxXmlNode* drillNode = appendNode( aCadLayerNode, "Layer" );
2431 drillNode->AddAttribute( "name", genLayersString( layers.first, layers.second, "SLOT" ) );
2432
2433 addAttribute( drillNode, "layerFunction", "ROUT" );
2434 addAttribute( drillNode, "polarity", "POSITIVE" );
2435 addAttribute( drillNode, "side", "ALL" );
2436
2437 wxXmlNode* spanNode = appendNode( drillNode, "Span" );
2438 addAttribute( spanNode, "fromLayer", genLayerString( layers.first, "LAYER" ) );
2439 addAttribute( spanNode, "toLayer", genLayerString( layers.second, "LAYER" ) );
2440 }
2441}
2442
2443
2444void PCB_IO_IPC2581::generateAuxilliaryLayers( wxXmlNode* aCadLayerNode )
2445{
2446 for( BOARD_ITEM* item : m_board->Tracks() )
2447 {
2448 if( item->Type() != PCB_VIA_T )
2449 continue;
2450
2451 PCB_VIA* via = static_cast<PCB_VIA*>( item );
2452 const DRILL_OPERATION* op = m_fabDrillModel->Find(
2453 DRILL_SPAN( via->TopLayer(), via->BottomLayer(), false, false ), via );
2454
2455 if( !op )
2456 continue;
2457
2458 std::vector<std::tuple<auxLayerType, PCB_LAYER_ID, PCB_LAYER_ID>> new_layers;
2459
2460 if( op->m_Filled )
2461 new_layers.emplace_back( auxLayerType::FILLING, via->TopLayer(), via->BottomLayer() );
2462
2463 if( op->m_Capped )
2464 new_layers.emplace_back( auxLayerType::CAPPING, via->TopLayer(), via->BottomLayer() );
2465
2466 for( PCB_LAYER_ID layer : { via->TopLayer(), via->BottomLayer() } )
2467 {
2468 bool top = layer == op->m_TopLayer;
2469
2470 if( top ? op->m_TopPlugged : op->m_BottomPlugged )
2471 new_layers.emplace_back( auxLayerType::PLUGGING, layer, UNDEFINED_LAYER );
2472
2473 if( top ? op->m_TopCovered : op->m_BottomCovered )
2474 new_layers.emplace_back( auxLayerType::COVERING, layer, UNDEFINED_LAYER );
2475
2476 if( top ? op->m_TopTented : op->m_BottomTented )
2477 new_layers.emplace_back( auxLayerType::TENTING, layer, UNDEFINED_LAYER );
2478 }
2479
2480 for( std::tuple<auxLayerType, PCB_LAYER_ID, PCB_LAYER_ID>& tuple : new_layers )
2481 m_auxilliary_Layers[tuple].push_back( via );
2482 }
2483
2484 for( const auto& [layers, vec] : m_auxilliary_Layers )
2485 {
2486 bool add_node = true;
2487
2488 wxString name;
2489 wxString layerFunction;
2490
2491 // clang-format off: suggestion is inconsitent
2492 switch( std::get<0>(layers) )
2493 {
2495 name = "COVERING";
2496 layerFunction = "COATINGNONCOND";
2497 break;
2499 name = "PLUGGING";
2500 layerFunction = "HOLEFILL";
2501 break;
2503 name = "TENTING";
2504 layerFunction = "COATINGNONCOND";
2505 break;
2507 name = "FILLING";
2508 layerFunction = "HOLEFILL";
2509 break;
2511 name = "CAPPING";
2512 layerFunction = "COATINGCOND";
2513 break;
2514 default:
2515 add_node = false;
2516 break;
2517 }
2518 // clang-format on: suggestion is inconsitent
2519
2520 if( add_node && !vec.empty() )
2521 {
2522 wxXmlNode* node = appendNode( aCadLayerNode, "Layer" );
2523 addAttribute( node, "layerFunction", layerFunction );
2524 addAttribute( node, "polarity", "POSITIVE" );
2525
2526 if( std::get<2>( layers ) == UNDEFINED_LAYER )
2527 {
2528 addAttribute( node, "name", genLayerString( std::get<1>( layers ), TO_UTF8( name ) ) );
2529 addAttribute( node, "side", IsFrontLayer( std::get<1>( layers ) ) ? "TOP" : "BOTTOM" );
2530 }
2531 else
2532 {
2533 addAttribute( node, "name", genLayersString( std::get<1>( layers ), std::get<2>( layers ),
2534 TO_UTF8( name ) ) );
2535
2536 const bool first_external = std::get<1>( layers ) == F_Cu || std::get<1>( layers ) == B_Cu;
2537 const bool second_external = std::get<2>( layers ) == F_Cu || std::get<2>( layers ) == B_Cu;
2538
2539 if( first_external )
2540 {
2541 if( second_external )
2542 addAttribute( node, "side", "ALL" );
2543 else
2544 addAttribute( node, "side", "TOP" );
2545 }
2546 else
2547 {
2548 if( second_external )
2549 addAttribute( node, "side", "BOTTOM" );
2550 else
2551 addAttribute( node, "side", "INTERNAL" );
2552 }
2553
2554 wxXmlNode* spanNode = appendNode( node, "Span" );
2555 addAttribute( spanNode, "fromLayer", genLayerString( std::get<1>( layers ), "LAYER" ) );
2556 addAttribute( spanNode, "toLayer", genLayerString( std::get<2>( layers ), "LAYER" ) );
2557 }
2558 }
2559 }
2560}
2561
2562
2563void PCB_IO_IPC2581::generateStepSection( wxXmlNode* aCadNode )
2564{
2565 wxXmlNode* stepNode = appendNode( aCadNode, "Step" );
2566 m_step_node = stepNode;
2567 wxFileName fn( m_board->GetFileName() );
2568 addAttribute( stepNode, "name", genString( fn.GetName(), "BOARD" ) );
2569
2571 addAttribute( stepNode, "type", "BOARD" );
2572
2573 wxXmlNode* datumNode = appendNode( stepNode, "Datum" );
2574 addAttribute( datumNode, "x", "0.0" );
2575 addAttribute( datumNode, "y", "0.0" );
2576
2577 if( included( IPC2581::SECTION::PROFILE ) )
2578 generateProfile( stepNode );
2579
2580 generateComponents( stepNode );
2581
2582 // The count shows the assembly size thus it goes with the component data
2583 if( componentRefEmitted() )
2584 {
2585 m_last_padstack = insertNode( stepNode, "NonstandardAttribute" );
2586 addAttribute( m_last_padstack, "name", "FOOTPRINT_COUNT" );
2587 addAttribute( m_last_padstack, "type", "INTEGER" );
2588 addAttribute( m_last_padstack, "value", wxString::Format( "%zu", m_board->Footprints().size() ) );
2589 }
2590
2591 if( included( IPC2581::SECTION::LOGICAL_NET ) )
2592 generateLogicalNets( stepNode );
2593
2594 if( included( IPC2581::SECTION::PHYSICAL_NET ) )
2595 generatePhyNetGroup( stepNode );
2596
2597 generateLayerFeatures( stepNode );
2598 generateLayerSetDrill( stepNode );
2599 generateLayerSetAuxilliary( stepNode );
2600}
2601
2602
2603void PCB_IO_IPC2581::addPad( wxXmlNode* aContentNode, const PAD* aPad, PCB_LAYER_ID aLayer )
2604{
2605 wxXmlNode* padNode = appendNode( aContentNode, "Pad" );
2606 FOOTPRINT* fp = aPad->GetParentFootprint();
2607
2608 addPadStack( padNode, aPad );
2609
2610 if( aPad->GetOrientation() != ANGLE_0 )
2611 {
2612 wxXmlNode* xformNode = appendNode( padNode, "Xform" );
2613 EDA_ANGLE angle = aPad->GetOrientation().Normalize();
2614
2615 xformNode->AddAttribute( "rotation", floatVal( angle.AsDegrees() ) );
2616 }
2617
2618 addLocationNode( padNode, *aPad, false, aLayer );
2619 addShape( padNode, *aPad, aLayer );
2620
2621 if( fp )
2622 {
2623 wxXmlNode* pinRefNode = appendNode( padNode, "PinRef" );
2624
2626 addAttribute( pinRefNode, "componentRef", componentName( fp ) );
2627
2628 addAttribute( pinRefNode, "pin", pinName( aPad ) );
2629 }
2630}
2631
2632
2633void PCB_IO_IPC2581::addVia( wxXmlNode* aContentNode, const PCB_VIA* aVia, PCB_LAYER_ID aLayer )
2634{
2635 if( !aVia->FlashLayer( aLayer ) )
2636 return;
2637
2638 wxXmlNode* padNode = appendNode( aContentNode, "Pad" );
2639
2640 addPadStack( padNode, aVia );
2641 addLocationNode( padNode, aVia->GetPosition().x, aVia->GetPosition().y );
2642
2643 PAD dummy( nullptr );
2644 int hole = aVia->GetDrillValue();
2645 dummy.SetDrillSize( VECTOR2I( hole, hole ) );
2646 dummy.SetPosition( aVia->GetStart() );
2647 dummy.SetSize( aLayer, VECTOR2I( aVia->GetWidth( aLayer ), aVia->GetWidth( aLayer ) ) );
2648
2649 addShape( padNode, dummy, aLayer );
2650}
2651
2652
2653void PCB_IO_IPC2581::addPadStack( wxXmlNode* aPadNode, const PAD* aPad )
2654{
2655 size_t hash = ipcPadstackHash( aPad );
2656 wxString name = wxString::Format( "PADSTACK_%zu", m_padstack_dict.size() + 1 );
2657 auto [ th_pair, success ] = m_padstack_dict.emplace( hash, name );
2658
2660 addAttribute( aPadNode, "padstackDefRef", th_pair->second );
2661
2662 // If we did not insert a new padstack, then we have already added it to the XML
2663 // and we don't need to add it again.
2664 if( !success || !included( IPC2581::SECTION::PADSTACKS ) )
2665 return;
2666
2667 wxXmlNode* padStackDefNode = new wxXmlNode( wxXML_ELEMENT_NODE, "PadStackDef" );
2668 addAttribute( padStackDefNode, "name", name );
2670
2671 // Use the anchor that exists and queue the node only if there is no anchor
2672 // A queued node that is already in the tree goes into the Step two times
2673 if( m_last_padstack )
2674 {
2675 insertNodeAfter( m_last_padstack, padStackDefNode );
2676 m_last_padstack = padStackDefNode;
2677 }
2678 else if( m_step_node )
2679 {
2680 insertNode( m_step_node, padStackDefNode );
2681 m_last_padstack = padStackDefNode;
2682 }
2683 else
2684 {
2685 m_padstacks.push_back( padStackDefNode );
2686 }
2687
2688 // Only handle round holes here because IPC2581 does not support non-round holes
2689 // These will be handled in a slot layer
2690 if( aPad->HasHole() && !IsDrillSlot( *aPad ) )
2691 {
2692 wxXmlNode* padStackHoleNode = appendNode( padStackDefNode, "PadstackHoleDef" );
2693 padStackHoleNode->AddAttribute( "name", wxString::Format( "%s%d_%d",
2694 aPad->GetAttribute() == PAD_ATTRIB::PTH ? "PTH"
2695 : "NPTH",
2696 aPad->GetDrillSize().x, aPad->GetDrillSize().y ) );
2697
2698 // A round drill with unequal axes is drilled at its smaller axis, like its layer hole
2699 int diameter = std::min( aPad->GetDrillSize().x, aPad->GetDrillSize().y );
2700 addAttribute( padStackHoleNode, "diameter", floatVal( m_scale * diameter ) );
2701 addAttribute( padStackHoleNode, "platingStatus", aPad->GetAttribute() == PAD_ATTRIB::PTH ? "PLATED"
2702 : "NONPLATED" );
2703 addAttribute( padStackHoleNode, "plusTol", "0.0" );
2704 addAttribute( padStackHoleNode, "minusTol", "0.0" );
2705 // A padstack has one hole, so it takes the front-side offset
2706 addXY( padStackHoleNode, aPad->GetOffset( aPad->Padstack().EffectiveLayerFor( F_Cu ) ) );
2707 }
2708
2709 LSEQ layer_seq = aPad->GetLayerSet().Seq();
2710
2711 for( PCB_LAYER_ID layer : layer_seq )
2712 {
2713 if( !m_board->IsLayerEnabled( layer ) || !layerIncluded( layer ) )
2714 continue;
2715
2716 const VECTOR2I& offset = aPad->GetOffset( aPad->Padstack().EffectiveLayerFor( layer ) );
2717
2718 wxXmlNode* padStackPadDefNode = appendNode( padStackDefNode, "PadstackPadDef" );
2719 addAttribute( padStackPadDefNode, "layerRef", m_layer_name_map[layer] );
2720 addAttribute( padStackPadDefNode, "padUse", "REGULAR" );
2721 addLocationNode( padStackPadDefNode, offset.x, offset.y );
2722
2723 if( !aPad->FlashLayer( layer ) )
2724 {
2725 PCB_SHAPE shape( nullptr, SHAPE_T::CIRCLE );
2726 shape.SetStart( offset );
2727 shape.SetEnd( offset + VECTOR2I( std::min( aPad->GetDrillSize().x, aPad->GetDrillSize().y ) / 2, 0 ) );
2728 addShape( padStackPadDefNode, shape );
2729 }
2730 else
2731 {
2732 addShape( padStackPadDefNode, *aPad, layer );
2733 }
2734 }
2735}
2736
2737
2738void PCB_IO_IPC2581::addPadStack( wxXmlNode* aContentNode, const PCB_VIA* aVia )
2739{
2740 const DRILL_OPERATION* op = m_fabDrillModel->Find( DRILL_SPAN( aVia->TopLayer(), aVia->BottomLayer(), false, false ),
2741 aVia );
2742 size_t hash = ipcPadstackHash( aVia, op );
2743 wxString name = wxString::Format( "PADSTACK_%zu", m_padstack_dict.size() + 1 );
2744 auto [ via_pair, success ] = m_padstack_dict.emplace( hash, name );
2745
2747 addAttribute( aContentNode, "padstackDefRef", via_pair->second );
2748
2749 // If we did not insert a new padstack, then we have already added it to the XML
2750 // and we don't need to add it again.
2751 if( !success || !included( IPC2581::SECTION::PADSTACKS ) )
2752 return;
2753
2754 wxXmlNode* padStackDefNode = new wxXmlNode( wxXML_ELEMENT_NODE, "PadStackDef" );
2755
2756 if( m_last_padstack )
2757 insertNodeAfter( m_last_padstack, padStackDefNode );
2758 else
2759 insertNode( m_step_node, padStackDefNode );
2760
2761 m_last_padstack = padStackDefNode;
2762 addAttribute( padStackDefNode, "name", name );
2764
2765 wxXmlNode* padStackHoleNode = appendNode( padStackDefNode, "PadstackHoleDef" );
2766 addAttribute( padStackHoleNode, "name", wxString::Format( "PH%d", aVia->GetDrillValue() ) );
2767 padStackHoleNode->AddAttribute( "diameter", floatVal( m_scale * aVia->GetDrillValue() ) );
2768 addAttribute( padStackHoleNode, "platingStatus", "VIA" );
2769 addAttribute( padStackHoleNode, "plusTol", "0.0" );
2770 addAttribute( padStackHoleNode, "minusTol", "0.0" );
2771 addAttribute( padStackHoleNode, "x", "0.0" );
2772 addAttribute( padStackHoleNode, "y", "0.0" );
2773
2774 LSEQ layer_seq = aVia->GetLayerSet().Seq();
2775
2776 auto addPadShape =
2777 [&]( PCB_LAYER_ID aLayer, const PCB_VIA* aViaShape, const wxString& aLayerRef, bool aDrill ) -> void
2778 {
2779 PCB_SHAPE shape( nullptr, SHAPE_T::CIRCLE );
2780
2781 if( aDrill )
2782 shape.SetEnd( { KiROUND( aViaShape->GetDrillValue() / 2.0 ), 0 } );
2783 else
2784 shape.SetEnd( { KiROUND( aViaShape->GetWidth( aLayer ) / 2.0 ), 0 } );
2785
2786 wxXmlNode* padStackPadDefNode = appendNode( padStackDefNode, "PadstackPadDef" );
2787 addAttribute( padStackPadDefNode, "layerRef", aLayerRef );
2788 addAttribute( padStackPadDefNode, "padUse", "REGULAR" );
2789
2790 addLocationNode( padStackPadDefNode, 0.0, 0.0 );
2791 addShape( padStackPadDefNode, shape );
2792 };
2793
2794 for( PCB_LAYER_ID layer : layer_seq )
2795 {
2796 if( !aVia->FlashLayer( layer ) || !m_board->IsLayerEnabled( layer ) || !layerIncluded( layer ) )
2797 continue;
2798
2799 addPadShape( layer, aVia, m_layer_name_map[layer], false );
2800 }
2801
2802 if( !op || !included( IPC2581::SECTION::MISC_FAB ) )
2803 return;
2804
2805 if( op->m_Filled )
2806 {
2807 wxString fillingRef = genLayersString( aVia->TopLayer(), aVia->BottomLayer(), "FILLING" );
2808 addPadShape( UNDEFINED_LAYER, aVia, fillingRef, true );
2809 }
2810
2811 if( op->m_Capped )
2812 {
2813 wxString cappingRef = genLayersString( aVia->TopLayer(), aVia->BottomLayer(), "CAPPING" );
2814 addPadShape( UNDEFINED_LAYER, aVia, cappingRef, true );
2815 }
2816
2817 for( PCB_LAYER_ID layer : { aVia->TopLayer(), aVia->BottomLayer() } )
2818 {
2819 bool top = layer == op->m_TopLayer;
2820
2821 if( top ? op->m_TopPlugged : op->m_BottomPlugged )
2822 addPadShape( layer, aVia, genLayerString( layer, "PLUGGING" ), true );
2823
2824 if( top ? op->m_TopCovered : op->m_BottomCovered )
2825 addPadShape( layer, aVia, genLayerString( layer, "COVERING" ), false );
2826
2827 if( top ? op->m_TopTented : op->m_BottomTented )
2828 addPadShape( layer, aVia, genLayerString( layer, "TENTING" ), false );
2829 }
2830}
2831
2832
2833// Map the top-level CadHeader/units value to the propertyUnitType enum used
2834// inside <Property unit="..."/>. The two enumerations differ (MILLIMETER vs MM).
2835static wxString propertyUnitForCadUnits( const wxString& aCadUnits )
2836{
2837 if( aCadUnits == wxT( "MILLIMETER" ) )
2838 return wxT( "MM" );
2839
2840 if( aCadUnits == wxT( "MICRON" ) )
2841 return wxT( "MICRON" );
2842
2843 if( aCadUnits == wxT( "INCH" ) )
2844 return wxT( "INCH" );
2845
2846 return wxT( "MM" );
2847}
2848
2849
2850void PCB_IO_IPC2581::ensureBackdrillSpecs( const wxString& aPadstackName, const PADSTACK& aPadstack )
2851{
2852 if( m_padstack_backdrill_specs.find( aPadstackName ) != m_padstack_backdrill_specs.end() )
2853 return;
2854
2855 const PADSTACK::DRILL_PROPS& secondary = aPadstack.SecondaryDrill();
2856 const PADSTACK::DRILL_PROPS& tertiary = aPadstack.TertiaryDrill();
2857
2858 auto hasBackdrill =
2859 []( const PADSTACK::DRILL_PROPS& aDrill )
2860 {
2861 return aDrill.start != UNDEFINED_LAYER && aDrill.end != UNDEFINED_LAYER
2862 && ( aDrill.size.x > 0 || aDrill.size.y > 0 );
2863 };
2864
2865 if( !hasBackdrill( secondary ) && !hasBackdrill( tertiary ) )
2866 return;
2867
2868 if( !m_cad_header_node )
2869 return;
2870
2871 auto layerHasRef =
2872 [&]( PCB_LAYER_ID aLayer ) -> bool
2873 {
2874 return m_layer_name_map.find( aLayer ) != m_layer_name_map.end();
2875 };
2876
2877 if( hasBackdrill( secondary )
2878 && ( !layerHasRef( secondary.start ) || !layerHasRef( secondary.end ) ) )
2879 {
2880 return;
2881 }
2882
2883 if( hasBackdrill( tertiary )
2884 && ( !layerHasRef( tertiary.start ) || !layerHasRef( tertiary.end ) ) )
2885 {
2886 return;
2887 }
2888
2889 const BOARD_STACKUP& stackup = m_fabStackup->Stackup();
2890
2891 LSEQ cuStack = m_board->GetEnabledLayers().CuStack();
2892
2893 auto createSpec =
2894 [&]( const PADSTACK::DRILL_PROPS& aDrill, const wxString& aSpecName ) -> wxString
2895 {
2896 if( !hasBackdrill( aDrill ) )
2897 return wxString();
2898
2899 auto startLayer = m_layer_name_map.find( aDrill.start );
2900
2901 if( startLayer == m_layer_name_map.end() )
2902 return wxString();
2903
2904 PCB_LAYER_ID mustNotCut = FabBackdrillMustNotCut( cuStack, aDrill.start, aDrill.end );
2905 auto mustNotCutEntry = m_layer_name_map.find( mustNotCut );
2906
2907 wxXmlNode* specNode = appendNode( m_cad_header_node, "Spec" );
2908 addAttribute( specNode, "name", aSpecName );
2909
2910 // START_LAYER
2911 {
2912 wxXmlNode* bd = appendNode( specNode, "Backdrill" );
2913 addAttribute( bd, "type", wxT( "START_LAYER" ) );
2914
2915 wxXmlNode* p = appendNode( bd, "Property" );
2916 addAttribute( p, "layerOrGroupRef", startLayer->second );
2917 }
2918
2919 // MUST_NOT_CUT_LAYER (only when a deeper signal layer exists)
2920 if( mustNotCut != UNDEFINED_LAYER && mustNotCutEntry != m_layer_name_map.end() )
2921 {
2922 wxXmlNode* bd = appendNode( specNode, "Backdrill" );
2923 addAttribute( bd, "type", wxT( "MUST_NOT_CUT_LAYER" ) );
2924
2925 wxXmlNode* p = appendNode( bd, "Property" );
2926 addAttribute( p, "layerOrGroupRef", mustNotCutEntry->second );
2927 }
2928
2929 // MAX_STUB_LENGTH: the maximum residual copper allowed above/below the signal layers (ie:
2930 // between the last signal layer and the must-cut layer). KiCad has no explicit fabricator
2931 // tolerance, so use half the dielectric thickness between must-cut and must-not-cut as a
2932 // nominal midpoint. Falls back to zero if no inner signal exists.
2933 int stubLength = 0;
2934
2935 if( mustNotCut != UNDEFINED_LAYER )
2936 {
2937 int dielectric = stackup.GetLayerDistance( aDrill.end, mustNotCut );
2938
2939 if( dielectric > 0 )
2940 stubLength = dielectric / 2;
2941 }
2942
2943 {
2944 wxXmlNode* bd = appendNode( specNode, "Backdrill" );
2945 addAttribute( bd, "type", wxT( "MAX_STUB_LENGTH" ) );
2946
2947 wxXmlNode* p = appendNode( bd, "Property" );
2948 addAttribute( p, "value", floatVal( m_scale * stubLength ) );
2950 }
2951
2952 m_backdrill_spec_nodes[aSpecName] = specNode;
2953
2954 return aSpecName;
2955 };
2956
2957 int specIndex = m_backdrill_spec_index + 1;
2958
2959 wxString secondarySpec = createSpec( secondary, wxString::Format( wxT( "BD_%dA" ), specIndex ) );
2960 wxString tertiarySpec = createSpec( tertiary, wxString::Format( wxT( "BD_%dB" ), specIndex ) );
2961
2962 if( secondarySpec.IsEmpty() && tertiarySpec.IsEmpty() )
2963 return;
2964
2965 m_backdrill_spec_index = specIndex;
2966 m_padstack_backdrill_specs.emplace( aPadstackName, std::array<wxString, 2>{ secondarySpec, tertiarySpec } );
2967}
2968
2969
2970void PCB_IO_IPC2581::addBackdrillSpecRefs( wxXmlNode* aHoleNode, const wxString& aPadstackName )
2971{
2972 auto it = m_padstack_backdrill_specs.find( aPadstackName );
2973
2974 if( it == m_padstack_backdrill_specs.end() )
2975 return;
2976
2977 auto addRef =
2978 [&]( const wxString& aSpecName )
2979 {
2980 if( aSpecName.IsEmpty() )
2981 return;
2982
2983 wxXmlNode* specRefNode = appendNode( aHoleNode, "SpecRef" );
2984 addAttribute( specRefNode, "id", aSpecName );
2985 m_backdrill_spec_used.insert( aSpecName );
2986 };
2987
2988 for( const wxString& specName : it->second )
2989 addRef( specName );
2990}
2991
2992
2993void PCB_IO_IPC2581::addPostMachiningSpecRefs( wxXmlNode* aHoleNode, const DRILL_OPERATION& aOperation )
2994{
2996 return;
2997
2998 auto addSide =
2999 [&]( const DRILL_POST_MACHINING& aMachining, bool aFront )
3000 {
3001 bool countersink = aMachining.m_Mode == PAD_DRILL_POST_MACHINING_MODE::COUNTERSINK;
3002 bool counterbore = aMachining.m_Mode == PAD_DRILL_POST_MACHINING_MODE::COUNTERBORE;
3003
3004 if( aMachining.m_Size <= 0 || ( !countersink && !counterbore ) )
3005 return;
3006
3007 auto key = std::make_tuple( aFront,
3008 static_cast<int>( aMachining.m_Mode ),
3009 aMachining.m_Size,
3010 counterbore ? aMachining.m_Depth : 0,
3011 countersink ? aMachining.m_Angle : 0 );
3012 auto [it, inserted] = m_post_machining_specs.try_emplace( key );
3013
3014 if( inserted )
3015 {
3016 it->second = wxString::Format( countersink ? wxT( "CSK_%zu" ) : wxT( "CBORE_%zu" ),
3017 m_post_machining_specs.size() );
3018 wxXmlNode* spec = appendNode( m_cad_header_node, "Spec" );
3019 addAttribute( spec, "name", it->second );
3020
3021 auto addValue =
3022 [&]( const wxString& aType, const wxString& aValue, const wxString& aUnit )
3023 {
3024 wxXmlNode* drill = appendNode( spec, "SecondaryDrill" );
3025 addAttribute( drill, "type", aType );
3026 wxXmlNode* property = appendNode( drill, "Property" );
3027 addAttribute( property, "value", aValue );
3028 addAttribute( property, "unit", aUnit );
3029 };
3030
3031 wxString lengthUnit = propertyUnitForCadUnits( m_units_str );
3032 addValue( wxT( "MAJOR_DIAMETER" ), floatVal( m_scale * aMachining.m_Size ), lengthUnit );
3033
3034 if( counterbore && aMachining.m_Depth > 0 )
3035 addValue( wxT( "DEPTH" ), floatVal( m_scale * aMachining.m_Depth ), lengthUnit );
3036
3037 if( countersink && aMachining.m_Angle > 0 )
3038 addValue( wxT( "ANGLE" ), floatVal( aMachining.m_Angle / 10.0 ), wxT( "DEGREES" ) );
3039
3040 wxXmlNode* side = appendNode( spec, "SecondaryDrill" );
3041 addAttribute( side, "type", "SIDE" );
3042 wxXmlNode* property = appendNode( side, "Property" );
3043 addAttribute( property, "text", aFront ? wxT( "TOP" ) : wxT( "BOTTOM" ) );
3044
3045 if( ( counterbore && aMachining.m_Depth <= 0 ) || ( countersink && aMachining.m_Angle <= 0 ) )
3046 {
3047 wxXmlNode* other = appendNode( spec, "SecondaryDrill" );
3048 addAttribute( other, "type", "OTHER" );
3049 addAttribute( other, "comment", counterbore ? wxT( "COUNTERBORE" ) : wxT( "COUNTERSINK" ) );
3050 }
3051 }
3052
3053 wxXmlNode* ref = appendNode( aHoleNode, "SpecRef" );
3054 addAttribute( ref, "id", it->second );
3055 };
3056
3057 addSide( aOperation.m_FrontPostMachining, true );
3058 addSide( aOperation.m_BackPostMachining, false );
3059}
3060
3061
3063{
3064 if( !m_cad_header_node )
3065 return;
3066
3067 auto it = m_backdrill_spec_nodes.begin();
3068
3069 while( it != m_backdrill_spec_nodes.end() )
3070 {
3071 if( m_backdrill_spec_used.find( it->first ) == m_backdrill_spec_used.end() )
3072 {
3073 wxXmlNode* specNode = it->second;
3074
3075 if( specNode )
3076 {
3077 m_cad_header_node->RemoveChild( specNode );
3078 deleteNode( specNode );
3079 }
3080
3081 it = m_backdrill_spec_nodes.erase( it );
3082 }
3083 else
3084 {
3085 ++it;
3086 }
3087 }
3088}
3089
3090
3091bool PCB_IO_IPC2581::addPolygonNode( wxXmlNode* aParentNode, const SHAPE_LINE_CHAIN& aPolygon, FILL_T aFillType,
3092 int aWidth, LINE_STYLE aDashType )
3093{
3094 wxXmlNode* polygonNode = nullptr;
3095
3096 if( aPolygon.PointCount() < 3 )
3097 return false;
3098
3099 auto make_node =
3100 [&]()
3101 {
3102 polygonNode = appendNode( aParentNode, "Polygon" );
3103 wxXmlNode* polybeginNode = appendNode( polygonNode, "PolyBegin" );
3104
3105 const std::vector<VECTOR2I>& pts = aPolygon.CPoints();
3106 addXY( polybeginNode, pts[0] );
3107
3108 for( size_t ii = 1; ii < pts.size(); ++ii )
3109 {
3110 wxXmlNode* polyNode = appendNode( polygonNode, "PolyStepSegment" );
3111 addXY( polyNode, pts[ii] );
3112 }
3113
3114 wxXmlNode* polyendNode = appendNode( polygonNode, "PolyStepSegment" );
3115 addXY( polyendNode, pts[0] );
3116 };
3117
3118 // Allow the case where we don't want line/fill information in the polygon
3119 if( aFillType == FILL_T::NO_FILL )
3120 {
3121 make_node();
3122 // If we specify a line width, we need to add a LineDescRef node and
3123 // since this is only valid for a non-filled polygon, we need to create
3124 // the fillNode as well
3125 if( aWidth > 0 )
3126 addLineDesc( polygonNode, aWidth, aDashType, true );
3127 }
3128 else
3129 {
3130 wxCHECK( aWidth == 0, false );
3131 make_node();
3132 }
3133
3134 addFillDesc( polygonNode, aFillType );
3135
3136 return true;
3137}
3138
3139
3140bool PCB_IO_IPC2581::addPolygonCutouts( wxXmlNode* aParentNode, const SHAPE_POLY_SET::POLYGON& aPolygon )
3141{
3142 for( size_t ii = 1; ii < aPolygon.size(); ++ii )
3143 {
3144 wxCHECK2( aPolygon[ii].PointCount() >= 3, continue );
3145
3146 wxXmlNode* cutoutNode = appendNode( aParentNode, "Cutout" );
3147 wxXmlNode* polybeginNode = appendNode( cutoutNode, "PolyBegin" );
3148
3149 const std::vector<VECTOR2I>& hole = aPolygon[ii].CPoints();
3150 addXY( polybeginNode, hole[0] );
3151
3152 for( size_t jj = 1; jj < hole.size(); ++jj )
3153 {
3154 wxXmlNode* polyNode = appendNode( cutoutNode, "PolyStepSegment" );
3155 addXY( polyNode, hole[jj] );
3156 }
3157
3158 wxXmlNode* polyendNode = appendNode( cutoutNode, "PolyStepSegment" );
3159 addXY( polyendNode, hole[0] );
3160 }
3161
3162 return true;
3163}
3164
3165
3166bool PCB_IO_IPC2581::addOutlineNode( wxXmlNode* aParentNode, const SHAPE_POLY_SET& aPolySet, int aWidth,
3167 LINE_STYLE aDashType )
3168{
3169 if( aPolySet.OutlineCount() == 0 )
3170 return false;
3171
3172 wxXmlNode* outlineNode = appendNode( aParentNode, "Outline" );
3173
3174 const SHAPE_POLY_SET* source = &aPolySet;
3175 SHAPE_POLY_SET merged;
3176
3177 if( aPolySet.OutlineCount() > 1 )
3178 {
3179 merged = aPolySet;
3180 merged.Simplify();
3181
3182 if( merged.OutlineCount() > 0 )
3183 source = &merged;
3184 }
3185
3186 for( int ii = 0; ii < source->OutlineCount(); ++ii )
3187 {
3188 if( !addPolygonNode( outlineNode, source->Outline( ii ) ) )
3189 wxLogTrace( traceIpc2581, wxS( "Failed to add polygon to outline" ) );
3190 }
3191
3192 if( !outlineNode->GetChildren() )
3193 {
3194 aParentNode->RemoveChild( outlineNode );
3195 deleteNode( outlineNode );
3196 return false;
3197 }
3198
3199 addLineDesc( outlineNode, aWidth, aDashType );
3200
3201 return true;
3202}
3203
3204
3205bool PCB_IO_IPC2581::addContourNode( wxXmlNode* aParentNode, const SHAPE_POLY_SET& aPolySet,
3206 int aOutline, FILL_T aFillType, int aWidth, LINE_STYLE aDashType )
3207{
3208 if( aPolySet.OutlineCount() < ( aOutline + 1 ) )
3209 return false;
3210
3211 wxXmlNode* contourNode = appendNode( aParentNode, "Contour" );
3212
3213 if( addPolygonNode( contourNode, aPolySet.Outline( aOutline ), aFillType, aWidth, aDashType ) )
3214 {
3215 // Do not attempt to add cutouts to shapes that are already hollow
3216 if( aFillType != FILL_T::NO_FILL )
3217 addPolygonCutouts( contourNode, aPolySet.Polygon( aOutline ) );
3218 }
3219 else
3220 {
3221 aParentNode->RemoveChild( contourNode );
3222 deleteNode( contourNode );
3223 return false;
3224 }
3225
3226 return true;
3227}
3228
3229
3230void PCB_IO_IPC2581::generateProfile( wxXmlNode* aStepNode )
3231{
3232 SHAPE_POLY_SET board_outline;
3233
3234 if( !m_board->GetBoardPolygonOutlines( board_outline, false ) || board_outline.OutlineCount() == 0 )
3235 {
3236 Report( _( "Board outline is invalid or missing. Please run DRC." ), RPT_SEVERITY_ERROR );
3237 return;
3238 }
3239
3240 wxXmlNode* profileNode = appendNode( aStepNode, "Profile" );
3241
3242 if( !addPolygonNode( profileNode, board_outline.Outline( 0 ) ) )
3243 {
3244 wxLogTrace( traceIpc2581, wxS( "Failed to add polygon to profile" ) );
3245 aStepNode->RemoveChild( profileNode );
3246 deleteNode( profileNode );
3247 return;
3248 }
3249
3250 addPolygonCutouts( profileNode, board_outline.Polygon( 0 ) );
3251}
3252
3253
3254static bool isOppositeSideSilk( const FOOTPRINT* aFootprint, PCB_LAYER_ID aLayer )
3255{
3256 if( !aFootprint )
3257 return false;
3258
3259 if( aLayer != F_SilkS && aLayer != B_SilkS )
3260 return false;
3261
3262 if( aFootprint->IsFlipped() )
3263 return aLayer == F_SilkS;
3264
3265 return aLayer == B_SilkS;
3266}
3267
3268
3269wxXmlNode* PCB_IO_IPC2581::addPackage( wxXmlNode* aContentNode, FOOTPRINT* aFp )
3270{
3271 std::unique_ptr<FOOTPRINT> fp( static_cast<FOOTPRINT*>( aFp->Clone() ) );
3272 fp->SetParentGroup( nullptr );
3273 fp->SetPosition( { 0, 0 } );
3274 fp->SetOrientation( ANGLE_0 );
3275
3276 // Track original flipped state before normalization. This is needed to correctly
3277 // determine OtherSideView content per IPC-2581C. After flipping, layer IDs swap,
3278 // so for bottom components, B_SilkS/B_Fab after flip is actually the primary view.
3279 bool wasFlipped = fp->IsFlipped();
3280
3281 // Normalize package geometry to the unflipped footprint coordinate system.
3282 if( fp->IsFlipped() )
3283 fp->Flip( fp->GetPosition(), FLIP_DIRECTION::TOP_BOTTOM );
3284
3285 size_t hash = hash_fp_item( fp.get(), HASH_POS | RELATIVE_TO_PARENT_COORDS );
3286 wxString name = genString( wxString::Format( "%s_%zu",
3287 fp->GetFPID().GetLibItemName().wx_str(),
3288 m_footprint_dict.size() + 1 ) );
3289
3290 auto [ iter, success ] = m_footprint_dict.emplace( hash, name );
3291
3293 addAttribute( aContentNode, "packageRef", iter->second );
3294
3295 // The BOM needs the name but the geometry below goes into the shape dictionaries
3296 // The export removes the Package thus it must not keep the geometry
3297 if( !success || !included( IPC2581::SECTION::PACKAGES ) )
3298 return nullptr;
3299
3300 // Package and Component nodes are at the same level, so we need to find the parent
3301 // which should be the Step node
3302 wxXmlNode* packageNode = new wxXmlNode( wxXML_ELEMENT_NODE, "Package" );
3303 wxXmlNode* otherSideViewNode = nullptr; // Only set this if we have elements on the back side
3304
3305 addAttribute( packageNode, "name", name );
3306 addAttribute( packageNode, "type", "OTHER" ); // TODO: Replace with actual package type once we encode this
3307
3308 // We don't specially identify pin 1 in our footprints, so we need to guess
3309 if( fp->FindPadByNumber( "1" ) )
3310 addAttribute( packageNode, "pinOne", "1" );
3311 else if ( fp->FindPadByNumber( "A1" ) )
3312 addAttribute( packageNode, "pinOne", "A1" );
3313 else if ( fp->FindPadByNumber( "A" ) )
3314 addAttribute( packageNode, "pinOne", "A" );
3315 else if ( fp->FindPadByNumber( "a" ) )
3316 addAttribute( packageNode, "pinOne", "a" );
3317 else if ( fp->FindPadByNumber( "a1" ) )
3318 addAttribute( packageNode, "pinOne", "a1" );
3319 else if ( fp->FindPadByNumber( "Anode" ) )
3320 addAttribute( packageNode, "pinOne", "Anode" );
3321 else if ( fp->FindPadByNumber( "ANODE" ) )
3322 addAttribute( packageNode, "pinOne", "ANODE" );
3323 else
3324 addAttribute( packageNode, "pinOne", "UNKNOWN" );
3325
3326 // Infer pinOneOrientation from pin 1 position relative to package centroid.
3327 // IPC-2581C 8.2.3.6 requires a comment attribute when OTHER is used.
3328 PAD* pinOnePad = fp->FindPadByNumber( "1" );
3329
3330 if( !pinOnePad )
3331 pinOnePad = fp->FindPadByNumber( "A1" );
3332
3333 if( pinOnePad && fp->Pads().size() >= 2 )
3334 {
3335 VECTOR2I pinPos = pinOnePad->GetFPRelativePosition();
3336 BOX2I fpBBox = fp->GetBoundingBox();
3337 VECTOR2I center = fpBBox.GetCenter();
3338
3339 // Use 5% of each dimension as the centerline tolerance band
3340 int tolX = fpBBox.GetWidth() / 20;
3341 int tolY = fpBBox.GetHeight() / 20;
3342
3343 bool onCenterX = std::abs( pinPos.x - center.x ) <= tolX;
3344 bool onCenterY = std::abs( pinPos.y - center.y ) <= tolY;
3345
3346 const char* orientation = "OTHER";
3347
3348 if( onCenterX && onCenterY )
3349 orientation = "CENTER";
3350 else if( onCenterX && pinPos.y < center.y )
3351 orientation = "UPPER_CENTER";
3352 else if( onCenterX && pinPos.y > center.y )
3353 orientation = "LOWER_CENTER";
3354 else if( onCenterY && pinPos.x < center.x )
3355 orientation = "LEFT";
3356 else if( onCenterY && pinPos.x > center.x )
3357 orientation = "RIGHT";
3358 else if( pinPos.x < center.x && pinPos.y < center.y )
3359 orientation = "UPPER_LEFT";
3360 else if( pinPos.x > center.x && pinPos.y < center.y )
3361 orientation = "UPPER_RIGHT";
3362 else if( pinPos.x < center.x && pinPos.y > center.y )
3363 orientation = "LOWER_LEFT";
3364 else
3365 orientation = "LOWER_RIGHT";
3366
3367 addAttribute( packageNode, "pinOneOrientation", orientation );
3368 }
3369 else
3370 {
3371 addAttribute( packageNode, "pinOneOrientation", "OTHER" );
3372 addAttribute( packageNode, "comment", "Pin 1 orientation could not be determined" );
3373 }
3374
3375 // After normalization: F_CrtYd is top, B_CrtYd is bottom.
3376 // For bottom components (wasFlipped), these are swapped from original orientation.
3377 const SHAPE_POLY_SET& courtyard_primary = wasFlipped ? fp->GetCourtyard( B_CrtYd )
3378 : fp->GetCourtyard( F_CrtYd );
3379 const SHAPE_POLY_SET& courtyard_other = wasFlipped ? fp->GetCourtyard( F_CrtYd )
3380 : fp->GetCourtyard( B_CrtYd );
3381
3382 if( courtyard_primary.OutlineCount() > 0 )
3383 {
3384 addOutlineNode( packageNode, courtyard_primary, courtyard_primary.Outline( 0 ).Width(), LINE_STYLE::SOLID );
3385 }
3386 else
3387 {
3388 SHAPE_POLY_SET bbox = fp->GetBoundingHull();
3389 addOutlineNode( packageNode, bbox );
3390 }
3391
3392 if( courtyard_other.OutlineCount() > 0 )
3393 {
3395 {
3396 otherSideViewNode = new wxXmlNode( wxXML_ELEMENT_NODE, "OtherSideView" );
3397 addOutlineNode( otherSideViewNode, courtyard_other, courtyard_other.Outline( 0 ).Width(),
3399 }
3400 }
3401
3402 wxXmlNode* pickupPointNode = appendNode( packageNode, "PickupPoint" );
3403 addAttribute( pickupPointNode, "x", "0.0" );
3404 addAttribute( pickupPointNode, "y", "0.0" );
3405
3406 std::map<PCB_LAYER_ID, std::map<bool, std::vector<BOARD_ITEM*>>> elements;
3407
3408 for( BOARD_ITEM* item : fp->GraphicalItems() )
3409 {
3410 PCB_LAYER_ID layer = item->GetLayer();
3411
3415 if( layer != F_SilkS && layer != B_SilkS && layer != F_Fab && layer != B_Fab )
3416 continue;
3417
3418 if( m_revision == IPC2581::REVISION::B && isOppositeSideSilk( fp.get(), layer ) )
3419 continue;
3420
3421 bool is_abs = true;
3422
3423 if( item->Type() == PCB_SHAPE_T )
3424 {
3425 PCB_SHAPE* shape = static_cast<PCB_SHAPE*>( item );
3426
3427 // Circles and Rectanges only have size information so we need to place them in
3428 // a separate node that has a location
3429 if( shape->GetShape() == SHAPE_T::CIRCLE || shape->GetShape() == SHAPE_T::RECTANGLE )
3430 is_abs = false;
3431 }
3432
3433 elements[item->GetLayer()][is_abs].push_back( item );
3434 }
3435
3436 auto add_base_node =
3437 [&]( PCB_LAYER_ID aLayer ) -> wxXmlNode*
3438 {
3439 wxXmlNode* parent = packageNode;
3440
3441 // Determine if this layer content should go in OtherSideView.
3442 // Per IPC-2581C, OtherSideView contains geometry visible from the opposite
3443 // side of the package body from the primary view.
3444 //
3445 // For non-flipped (top) components: B_SilkS/B_Fab → OtherSideView
3446 // For flipped (bottom) components after normalization: F_SilkS/F_Fab → OtherSideView
3447 // (because after flip, B_SilkS/B_Fab contains the original primary graphics)
3448 bool is_other_side = wasFlipped ? ( aLayer == F_SilkS || aLayer == F_Fab )
3449 : ( aLayer == B_SilkS || aLayer == B_Fab );
3450
3451 if( is_other_side && m_revision == IPC2581::REVISION::C )
3452 {
3453 if( !otherSideViewNode )
3454 otherSideViewNode = new wxXmlNode( wxXML_ELEMENT_NODE, "OtherSideView" );
3455
3456 parent = otherSideViewNode;
3457 }
3458
3459 wxString nodeName;
3460
3461 if( aLayer == F_SilkS || aLayer == B_SilkS )
3462 nodeName = "SilkScreen";
3463 else if( aLayer == F_Fab || aLayer == B_Fab )
3464 nodeName = "AssemblyDrawing";
3465 else
3466 wxASSERT( false );
3467
3468 wxXmlNode* new_node = appendNode( parent, nodeName );
3469 return new_node;
3470 };
3471
3472 auto add_marking_node =
3473 [&]( wxXmlNode* aNode ) -> wxXmlNode*
3474 {
3475 wxXmlNode* marking_node = appendNode( aNode, "Marking" );
3476 addAttribute( marking_node, "markingUsage", "NONE" );
3477 return marking_node;
3478 };
3479
3480 std::map<PCB_LAYER_ID, wxXmlNode*> layer_nodes;
3481 std::map<PCB_LAYER_ID, BOX2I> layer_bbox;
3482
3483 for( PCB_LAYER_ID layer : { F_Fab, B_Fab } )
3484 {
3485 if( elements.find( layer ) != elements.end() )
3486 {
3487 if( elements[layer][true].size() > 0 )
3488 layer_bbox[layer] = elements[layer][true][0]->GetBoundingBox();
3489 else if( elements[layer][false].size() > 0 )
3490 layer_bbox[layer] = elements[layer][false][0]->GetBoundingBox();
3491 }
3492 }
3493
3494 for( auto& [layer, map] : elements )
3495 {
3496 wxXmlNode* layer_node = add_base_node( layer );
3497 wxXmlNode* marking_node = add_marking_node( layer_node );
3498 wxXmlNode* group_node = appendNode( marking_node, "UserSpecial" );
3499 bool update_bbox = false;
3500
3501 if( layer == F_Fab || layer == B_Fab )
3502 {
3503 layer_nodes[layer] = layer_node;
3504 update_bbox = true;
3505 }
3506
3507 for( auto& [is_abs, vec] : map )
3508 {
3509 for( BOARD_ITEM* item : vec )
3510 {
3511 wxXmlNode* output_node = nullptr;
3512
3513 if( update_bbox )
3514 layer_bbox[layer].Merge( item->GetBoundingBox() );
3515
3516 if( !is_abs )
3517 output_node = add_marking_node( layer_node );
3518 else
3519 output_node = group_node;
3520
3521 switch( item->Type() )
3522 {
3523 case PCB_TEXT_T:
3524 {
3525 PCB_TEXT* text = static_cast<PCB_TEXT*>( item );
3526
3527 if( text->IsKnockout() )
3528 addKnockoutText( output_node, text );
3529 else
3530 addText( output_node, text, text->GetFontMetrics() );
3531
3532 break;
3533 }
3534
3535 case PCB_TEXTBOX_T:
3536 {
3537 PCB_TEXTBOX* text = static_cast<PCB_TEXTBOX*>( item );
3538 addText( output_node, text, text->GetFontMetrics() );
3539
3540 // We want to force this to be a polygon to get absolute coordinates
3541 if( text->IsBorderEnabled() )
3542 {
3543 SHAPE_POLY_SET poly_set;
3544 text->GetEffectiveShape()->TransformToPolygon( poly_set, 0, ERROR_INSIDE );
3545 addContourNode( output_node, poly_set, 0, FILL_T::NO_FILL, text->GetBorderWidth() );
3546 }
3547
3548 break;
3549 }
3550
3551 case PCB_SHAPE_T:
3552 {
3553 if( !is_abs )
3554 addLocationNode( output_node, *static_cast<PCB_SHAPE*>( item ) );
3555
3556 // When in Marking context (!is_abs), use inline geometry to avoid
3557 // unresolved UserPrimitiveRef errors in validators like Vu2581
3558 addShape( output_node, *static_cast<PCB_SHAPE*>( item ), !is_abs );
3559
3560 break;
3561 }
3562
3563 default: break;
3564 }
3565 }
3566 }
3567
3568 if( group_node->GetChildren() == nullptr )
3569 {
3570 marking_node->RemoveChild( group_node );
3571 layer_node->RemoveChild( marking_node );
3572 delete group_node;
3573 delete marking_node;
3574 }
3575 }
3576
3577 for( auto& [layer, bbox] : layer_bbox )
3578 {
3579 if( bbox.GetWidth() > 0 )
3580 {
3581 wxXmlNode* outlineNode = insertNode( layer_nodes[layer], "Outline" );
3582
3583 SHAPE_LINE_CHAIN outline;
3584 std::vector<VECTOR2I> points( 4 );
3585 points[0] = bbox.GetPosition();
3586 points[2] = bbox.GetEnd();
3587 points[1].x = points[0].x;
3588 points[1].y = points[2].y;
3589 points[3].x = points[2].x;
3590 points[3].y = points[0].y;
3591
3592 outline.Append( points );
3593 addPolygonNode( outlineNode, outline, FILL_T::NO_FILL, 0 );
3594 addLineDesc( outlineNode, 0, LINE_STYLE::SOLID );
3595 }
3596 }
3597
3598 std::map<wxString, wxXmlNode*> pin_nodes;
3599
3600 for( size_t ii = 0; ii < fp->Pads().size(); ++ii )
3601 {
3602 PAD* pad = fp->Pads()[ii];
3603 FAB_PIN fabPin = MakeFabPin( *pad, ii );
3604 wxString pin_name = sanitizeId( fabPin.m_name );
3605 wxXmlNode* pinNode = nullptr;
3606
3607 auto [ it, inserted ] = pin_nodes.emplace( pin_name, nullptr );
3608
3609 if( inserted )
3610 {
3611 pinNode = appendNode( packageNode, "Pin" );
3612 it->second = pinNode;
3613
3614 addAttribute( pinNode, "number", pin_name );
3615
3616 switch( fabPin.m_electrical )
3617 {
3618 case FAB_ELECTRICAL::MECHANICAL: addAttribute( pinNode, "electricalType", "MECHANICAL" ); break;
3619 case FAB_ELECTRICAL::ELECTRICAL: addAttribute( pinNode, "electricalType", "ELECTRICAL" ); break;
3620 case FAB_ELECTRICAL::UNDEFINED: addAttribute( pinNode, "electricalType", "UNDEFINED" ); break;
3621 }
3622
3623 if( pad->HasHole() )
3624 addAttribute( pinNode, "type", "THRU" );
3625 else
3626 addAttribute( pinNode, "type", "SURFACE" );
3627
3628 if( pad->GetFPRelativeOrientation() != ANGLE_0 )//|| fp->IsFlipped() )
3629 {
3630 wxXmlNode* xformNode = appendNode( pinNode, "Xform" );
3631 EDA_ANGLE pad_angle = pad->GetFPRelativeOrientation().Normalize();
3632
3633 if( fp->IsFlipped() )
3634 pad_angle = pad_angle.Invert().Normalize();
3635
3636 if( pad_angle != ANGLE_0 )
3637 xformNode->AddAttribute( "rotation", floatVal( pad_angle.AsDegrees() ) );
3638 }
3639
3640 addLocationNode( pinNode, *pad, true, pad->GetLayer() );
3641 addShape( pinNode, *pad, pad->GetLayer() );
3642 }
3643
3644 // We just need the padstack, we don't need the reference here. The reference will be
3645 // created in the LayerFeature set
3646 wxXmlNode dummy;
3647 addPadStack( &dummy, pad );
3648 }
3649
3650 if( otherSideViewNode )
3651 packageNode->AddChild( otherSideViewNode );
3652
3653 return packageNode;
3654}
3655
3656
3657void PCB_IO_IPC2581::generateComponents( wxXmlNode* aStepNode )
3658{
3659 std::vector<wxXmlNode*> componentNodes;
3660 std::vector<wxXmlNode*> packageNodes;
3661 std::set<wxString> packageNames;
3662
3663 bool generate_unique = m_OEMRef.empty();
3664
3665 reportPhase( _( "Generating components" ) );
3666
3667 if( m_progressReporter )
3668 m_progressReporter->SetMaxProgress( (int) m_board->Footprints().size() );
3669
3670 for( FOOTPRINT* fp : m_board->Footprints() )
3671 {
3672 tickProgress();
3674
3675 wxXmlNode* componentNode = new wxXmlNode( wxXML_ELEMENT_NODE, "Component" );
3676
3677 if( refDesEmitted() )
3678 addAttribute( componentNode, "refDes", componentName( fp ) );
3679
3680 // addPackage gets a copy without the duplicates
3681 // Thus it records only the first footprint of each Package
3682 for( PAD* pad : fp->Pads() )
3683 {
3684 if( pad->GetNetCode() > 0 )
3685 m_net_pin_dict[pad->GetNetCode()].emplace_back( componentName( fp ), pinName( pad ) );
3686 }
3687
3688 wxXmlNode* pkg = addPackage( componentNode, fp );
3689
3690 if( pkg )
3691 packageNodes.push_back( pkg );
3692
3693 wxString name = generate_unique ? wxString() : row.Field( m_OEMRef );
3694
3695 if( name.IsEmpty() )
3696 {
3697 name = wxString::Format( "%s_%s_%s",
3698 fp->GetFPID().GetFullLibraryName(),
3699 fp->GetFPID().GetLibItemName().wx_str(),
3700 row.Value() );
3701 }
3702
3703 if( !m_OEMRef_dict.emplace( fp, name ).second )
3704 {
3705 Report( _( "Duplicate footprint pointers encountered; IPC-2581 output may be incorrect." ),
3707 }
3708
3709 addAttribute( componentNode, "part", genString( name, "REF" ) );
3710 addAttribute( componentNode, "layerRef", m_layer_name_map[fp->GetLayer()] );
3711
3712 if( row.m_mount == FAB_MOUNT::THT )
3713 addAttribute( componentNode, "mountType", "THMT" );
3714 else if( row.m_mount == FAB_MOUNT::SMT )
3715 addAttribute( componentNode, "mountType", "SMT" );
3716 else
3717 addAttribute( componentNode, "mountType", "OTHER" );
3718
3719 if( fp->GetOrientation() != ANGLE_0 || fp->IsFlipped() )
3720 {
3721 wxXmlNode* xformNode = appendNode( componentNode, "Xform" );
3722
3723 EDA_ANGLE fp_angle = fp->GetOrientation().Normalize();
3724
3725 if( fp->IsFlipped() )
3726 fp_angle = ( fp_angle.Invert() - ANGLE_180 ).Normalize();
3727
3728 if( fp_angle != ANGLE_0 )
3729 addAttribute( xformNode, "rotation", floatVal( fp_angle.AsDegrees(), 2 ) );
3730
3731 if( fp->IsFlipped() )
3732 addAttribute( xformNode, "mirror", "true" );
3733 }
3734
3735 addLocationNode( componentNode, fp->GetPosition().x, fp->GetPosition().y );
3736
3737 componentNodes.push_back( componentNode );
3738 }
3739
3740 // The loop above fills the dictionaries that the BOM and the LogicalNet read
3741 // Thus it always runs and only the attachments below use the function mode
3742 auto attachOrDrop =
3743 [&]( std::vector<wxXmlNode*>& aNodes, IPC2581::SECTION aSection, bool aInsert )
3744 {
3745 bool keep = included( aSection );
3746
3747 for( wxXmlNode*& node : aNodes )
3748 {
3749 if( !keep )
3750 {
3751 deleteNode( node );
3752 }
3753 else if( aInsert )
3754 {
3755 insertNode( aStepNode, node );
3756 m_last_padstack = node;
3757 }
3758 else
3759 {
3760 aStepNode->AddChild( node );
3761 }
3762 }
3763
3764 if( !keep )
3765 aNodes.clear();
3766 };
3767
3768 attachOrDrop( m_padstacks, IPC2581::SECTION::PADSTACKS, true );
3769 attachOrDrop( packageNodes, IPC2581::SECTION::PACKAGES, false );
3770 attachOrDrop( componentNodes, IPC2581::SECTION::COMPONENTS, false );
3771}
3772
3773
3774void PCB_IO_IPC2581::generateLogicalNets( wxXmlNode* aStepNode )
3775{
3776 reportPhase( _( "Generating logical netlist" ) );
3777
3778 for( auto& [ net, pin_pair] : m_net_pin_dict )
3779 {
3780 wxXmlNode* netNode = appendNode( aStepNode, "LogicalNet" );
3781 addAttribute( netNode, "name", netName( m_board->GetNetInfo().GetNetItem( net )->GetNetname() ) ) ;
3782
3783 for( auto& [cmp, pin] : pin_pair )
3784 {
3785 wxXmlNode* netPinNode = appendNode( netNode, "PinRef" );
3786
3788 addAttribute( netPinNode, "componentRef", cmp );
3789
3790 addAttribute( netPinNode, "pin", pin );
3791 }
3792 //TODO: Finish
3793 }
3794}
3795
3796
3797void PCB_IO_IPC2581::generatePhyNetGroup( wxXmlNode* aStepNode )
3798{
3799 struct NET_POINT
3800 {
3801 VECTOR2I m_pos;
3802 PCB_LAYER_ID m_layer;
3803 PCB_LAYER_ID m_secondary;
3804 wxString m_exposure;
3805 bool m_end;
3806 bool m_via;
3807 int m_width;
3808 int m_drill;
3809 const PAD* m_pad;
3810 };
3811
3812 std::map<int, std::vector<NET_POINT>> byNet;
3813
3814 reportPhase( _( "Generating physical netlist" ) );
3815 std::vector<FAB_TEST_POINT> testPoints = FabTestPoints( *m_board );
3816
3817 if( m_progressReporter )
3818 m_progressReporter->SetMaxProgress( static_cast<int>( testPoints.size() ) );
3819
3820 // Section 8.2.3.9.1 makes a physical net point a location that you can probe
3821 // Thus only a feature on an outer layer applies
3822 auto exposure =
3823 []( const FAB_TEST_POINT& aPoint ) -> wxString
3824 {
3825 bool frontOpen = !aPoint.m_front || aPoint.m_frontMask;
3826 bool backOpen = !aPoint.m_back || aPoint.m_backMask;
3827
3828 if( frontOpen && backOpen )
3829 return wxT( "EXPOSED" );
3830
3831 if( backOpen )
3832 return wxT( "COVERED_PRIMARY" );
3833
3834 if( frontOpen )
3835 return wxT( "COVERED_SECONDARY" );
3836
3837 return wxT( "COVERED" );
3838 };
3839
3840 // Keep IPC-2581's pad-first order within each physical net
3841 for( bool wantPad : { true, false } )
3842 {
3843 for( const FAB_TEST_POINT& testPoint : testPoints )
3844 {
3845 // Buried vias and inner-only pads cannot be probed
3846 if( ( testPoint.m_pad != nullptr ) != wantPad
3847 || testPoint.m_netCode <= 0
3848 || ( !testPoint.m_front && !testPoint.m_back ) )
3849 {
3850 continue;
3851 }
3852
3853 NET_POINT point;
3854 point.m_pos = testPoint.m_position;
3855 point.m_layer = testPoint.m_front ? F_Cu : B_Cu;
3856 point.m_secondary = ( testPoint.m_front && testPoint.m_back ) ? B_Cu : UNDEFINED_LAYER;
3857 point.m_exposure = exposure( testPoint );
3858 point.m_end = testPoint.m_netEnd;
3859 point.m_via = testPoint.m_via != nullptr;
3860 point.m_width = testPoint.m_pad ? testPoint.m_pad->GetSize( point.m_layer ).x
3861 : testPoint.m_via->GetWidth( point.m_layer );
3862 point.m_drill = testPoint.m_drill;
3863 point.m_pad = testPoint.m_pad;
3864 byNet[testPoint.m_netCode].push_back( point );
3865 tickProgress();
3866 }
3867 }
3868
3869 if( byNet.empty() )
3870 return;
3871
3872 wxXmlNode* groupNode = appendNode( aStepNode, "PhyNetGroup" );
3873 addAttribute( groupNode, "name", "NETS" );
3874
3875 for( const auto& [netcode, points] : byNet )
3876 {
3877 NETINFO_ITEM* net = m_board->GetNetInfo().GetNetItem( netcode );
3878
3879 if( !net )
3880 continue;
3881
3882 // NetKey is one key for LogicalNet@name and PhyNet@name
3883 // Thus the two views of a net must not have the same name
3884 wxXmlNode* netNode = appendNode( groupNode, "PhyNet" );
3885 addAttribute( netNode, "name", netName( net->GetNetname(), wxT( "_PHY" ) ) );
3886
3887 for( const NET_POINT& point : points )
3888 {
3889 wxXmlNode* pointNode = appendNode( netNode, "PhyNetPoint" );
3890 addXY( pointNode, point.m_pos );
3891 addAttribute( pointNode, "layerRef", m_layer_name_map[point.m_layer] );
3892
3893 if( point.m_secondary != UNDEFINED_LAYER )
3894 addAttribute( pointNode, "secondaryLayerRef", m_layer_name_map[point.m_secondary] );
3895
3896 addAttribute( pointNode, "netNode", point.m_end ? "END" : "MIDDLE" );
3897 addAttribute( pointNode, "exposure", point.m_exposure );
3898
3899 if( point.m_via )
3900 addAttribute( pointNode, "via", "true" );
3901
3902 if( point.m_pad )
3903 {
3904 addShape( pointNode, *point.m_pad, point.m_layer );
3905 }
3906 else
3907 {
3908 PAD dummy( nullptr );
3909 dummy.SetPosition( point.m_pos );
3910 dummy.SetSize( point.m_layer, VECTOR2I( point.m_width, point.m_width ) );
3911
3912 if( point.m_drill > 0 )
3913 dummy.SetDrillSize( VECTOR2I( point.m_drill, point.m_drill ) );
3914
3915 addShape( pointNode, dummy, point.m_layer );
3916 }
3917 }
3918 }
3919}
3920
3921
3922void PCB_IO_IPC2581::generateLayerFeatures( wxXmlNode* aStepNode )
3923{
3924 LSEQ layers = m_board->GetEnabledLayers().Seq();
3925 const NETINFO_LIST& nets = m_board->GetNetInfo();
3927
3928 for( PCB_LAYER_ID layer : layers )
3929 {
3930 if( !layerIncluded( layer ) )
3931 continue;
3932
3933 if( m_progressReporter )
3934 m_progressReporter->SetMaxProgress( nets.GetNetCount() * layers.size() );
3935
3936
3937 wxXmlNode* layerNode = appendNode( aStepNode, "LayerFeature" );
3938 addAttribute( layerNode, "layerRef", m_layer_name_map[layer] );
3939
3940 const FAB_LAYER_INDEX::NET_ITEMS& netsOnLayer = index.Items( layer );
3941
3942 auto process_net =
3943 [&]( int net )
3944 {
3945 auto netItems = netsOnLayer.find( net );
3946
3947 if( netItems == netsOnLayer.end() || netItems->second.empty() )
3948 return;
3949
3950 generateLayerSetNet( layerNode, layer, netItems->second );
3951 };
3952
3953 for( const NETINFO_ITEM* net : nets )
3954 {
3955 tickProgress( wxString::Format( _( "Exporting layer %s, net %s" ),
3956 m_board->GetLayerName( layer ),
3957 net->GetNetname() ) );
3958
3959 process_net( net->GetNetCode() );
3960 }
3961
3962 if( layerNode->GetChildren() == nullptr )
3963 {
3964 aStepNode->RemoveChild( layerNode );
3965 deleteNode( layerNode );
3966 }
3967 }
3968}
3969
3970
3971void PCB_IO_IPC2581::generateLayerSetDrill( wxXmlNode* aLayerNode )
3972{
3973 if( !included( IPC2581::SECTION::DRILL_ROUT ) )
3974 return;
3975
3976 int hole_count = 1;
3977
3978 for( const auto& [span, vec] : m_drill_layers )
3979 {
3980 wxXmlNode* layerNode = appendNode( aLayerNode, "LayerFeature" );
3981 layerNode->AddAttribute( "layerRef", genLayersString( span.TopLayer(), span.BottomLayer(), "DRILL" ) );
3982
3983 for( BOARD_ITEM* item : vec )
3984 {
3985 if( item->Type() == PCB_VIA_T )
3986 {
3987 PCB_VIA* via = static_cast<PCB_VIA*>( item );
3988 const DRILL_OPERATION* operation = m_fabDrillModel->Find( span, via );
3989
3990 if( !operation )
3991 continue;
3992
3993 auto it = m_padstack_dict.find( ipcPadstackHash( via, operation ) );
3994
3995 if( it == m_padstack_dict.end() )
3996 {
3997 Report( _( "Via uses unsupported padstack; omitted from drill data." ), RPT_SEVERITY_WARNING );
3998 continue;
3999 }
4000
4001 wxXmlNode* padNode = appendNode( layerNode, "Set" );
4002 addAttribute( padNode, "geometry", it->second );
4003
4004 if( via->GetNetCode() > 0 )
4005 addAttribute( padNode, "net", netName( via->GetNetname() ) );
4006
4007 wxXmlNode* holeNode = appendNode( padNode, "Hole" );
4008 addAttribute( holeNode, "name", wxString::Format( "H%d", hole_count++ ) );
4009 addAttribute( holeNode, "diameter", floatVal( m_scale * operation->m_Diameter ) );
4010 addAttribute( holeNode, "platingStatus", "VIA" );
4011 addAttribute( holeNode, "plusTol", "0.0" );
4012 addAttribute( holeNode, "minusTol", "0.0" );
4013 addXY( holeNode, via->GetPosition() );
4014 addBackdrillSpecRefs( holeNode, it->second );
4015 addPostMachiningSpecRefs( holeNode, *operation );
4016 }
4017 else if( item->Type() == PCB_PAD_T )
4018 {
4019 PAD* pad = static_cast<PAD*>( item );
4020 const DRILL_OPERATION* operation = m_fabDrillModel->Find( span, pad, true );
4021
4022 if( !operation )
4023 continue;
4024
4025 auto it = m_padstack_dict.find( ipcPadstackHash( pad ) );
4026
4027 if( it == m_padstack_dict.end() )
4028 {
4029 Report( _( "Pad uses unsupported padstack; hole was omitted from drill data." ),
4031 continue;
4032 }
4033
4034 wxXmlNode* padNode = appendNode( layerNode, "Set" );
4035 addAttribute( padNode, "geometry", it->second );
4036
4037 if( pad->GetNetCode() > 0 )
4038 addAttribute( padNode, "net", netName( pad->GetNetname() ) );
4039
4040 wxXmlNode* holeNode = appendNode( padNode, "Hole" );
4041 addAttribute( holeNode, "name", wxString::Format( "H%d", hole_count++ ) );
4042 addAttribute( holeNode, "diameter", floatVal( m_scale * operation->m_Diameter ) );
4043 addAttribute( holeNode, "platingStatus", pad->GetAttribute() == PAD_ATTRIB::PTH ? "PLATED"
4044 : "NONPLATED" );
4045 addAttribute( holeNode, "plusTol", "0.0" );
4046 addAttribute( holeNode, "minusTol", "0.0" );
4047 addXY( holeNode, pad->GetPosition() );
4048 addBackdrillSpecRefs( holeNode, it->second );
4049 addPostMachiningSpecRefs( holeNode, *operation );
4050 }
4051 }
4052 }
4053
4054 hole_count = 1;
4055
4056 for( const auto& [layers, vec] : m_slot_holes )
4057 {
4058 wxXmlNode* layerNode = appendNode( aLayerNode, "LayerFeature" );
4059 layerNode->AddAttribute( "layerRef", genLayersString( layers.first, layers.second, "SLOT" ) );
4060
4061 for( PAD* pad : vec )
4062 {
4063 wxXmlNode* padNode = appendNode( layerNode, "Set" );
4064
4065 if( pad->GetNetCode() > 0 )
4066 addAttribute( padNode, "net", netName( pad->GetNetname() ) );
4067
4068 addSlotCavity( padNode, *pad, wxString::Format( "SLOT%d", hole_count++ ) );
4069
4070 // SlotCavity has no SpecRef, so the slot's own Set carries its post-machining
4071 DRILL_SPAN span( layers.first, layers.second, false, false );
4072
4073 if( const DRILL_OPERATION* operation = m_fabDrillModel->Find( span, pad, true ) )
4074 addPostMachiningSpecRefs( padNode, *operation );
4075 }
4076 }
4077}
4078
4079
4080void PCB_IO_IPC2581::generateLayerSetNet( wxXmlNode* aLayerNode, PCB_LAYER_ID aLayer,
4081 const std::vector<BOARD_ITEM*>& aItems )
4082{
4083 auto it = aItems.begin();
4084 wxXmlNode* layerSetNode = appendNode( aLayerNode, "Set" );
4085 wxXmlNode* featureSetNode = appendNode( layerSetNode, "Features" );
4086 wxXmlNode* specialNode = appendNode( featureSetNode, "UserSpecial" );
4087
4088 bool has_via = false;
4089 bool has_pad = false;
4090
4091 wxXmlNode* padSetNode = nullptr;
4092
4093 wxXmlNode* viaSetNode = nullptr;
4094
4095 wxXmlNode* teardropLayerSetNode = nullptr;
4096 wxXmlNode* teardropFeatureSetNode = nullptr;
4097
4098 bool teardrop_warning = false;
4099
4100 if( BOARD_CONNECTED_ITEM* item = dynamic_cast<BOARD_CONNECTED_ITEM*>( *it );
4101 IsCopperLayer( aLayer ) && item )
4102 {
4103 if( item->GetNetCode() > 0 )
4104 addAttribute( layerSetNode, "net", netName( item->GetNetname() ) );
4105 }
4106
4107 auto add_track =
4108 [&]( PCB_TRACK* track )
4109 {
4110 if( track->Type() == PCB_TRACE_T )
4111 {
4112 PCB_SHAPE shape( nullptr, SHAPE_T::SEGMENT );
4113 shape.SetStart( track->GetStart() );
4114 shape.SetEnd( track->GetEnd() );
4115 shape.SetWidth( FabTrackWidth( *track, aLayer ) );
4116 addShape( specialNode, shape );
4117 }
4118 else if( track->Type() == PCB_ARC_T )
4119 {
4120 const PCB_ARC* arc = static_cast<const PCB_ARC*>( track );
4121
4122 // Too small arcs cannot be really handled: arc center (and arc radius)
4123 // cannot be safely computed
4124 if( !arc->IsDegenerated( 10 /* in IU */ ) )
4125 {
4126 PCB_SHAPE shape( nullptr, SHAPE_T::ARC );
4127 shape.SetArcGeometry( arc->GetStart(), arc->GetMid(), arc->GetEnd() );
4128 shape.SetWidth( FabTrackWidth( *arc, aLayer ) );
4129 addShape( specialNode, shape );
4130 }
4131 else
4132 {
4133 // Approximate this very small arc by a segment.
4134 PCB_SHAPE shape( nullptr, SHAPE_T::SEGMENT );
4135 shape.SetStart( track->GetStart() );
4136 shape.SetEnd( track->GetEnd() );
4137 shape.SetWidth( FabTrackWidth( *track, aLayer ) );
4138 addShape( specialNode, shape );
4139 }
4140 }
4141 else
4142 {
4143 if( !viaSetNode )
4144 {
4145 if( !has_pad )
4146 {
4147 viaSetNode = layerSetNode;
4148 has_via = true;
4149 }
4150 else
4151 {
4152 viaSetNode = appendNode( layerSetNode, "Set" );
4153
4154 if( track->GetNetCode() > 0 )
4155 addAttribute( viaSetNode, "net", netName( track->GetNetname() ) );
4156 }
4157
4158 addAttribute( viaSetNode, "padUsage", "VIA" );
4159 }
4160
4161 addVia( viaSetNode, static_cast<PCB_VIA*>( track ), aLayer );
4162 }
4163 };
4164
4165 auto add_zone =
4166 [&]( ZONE* zone )
4167 {
4168 wxXmlNode* zoneFeatureNode = specialNode;
4169
4170 if( zone->IsTeardropArea() )
4171 {
4173 {
4174 if( !teardropFeatureSetNode )
4175 {
4176 teardropLayerSetNode = appendNode( aLayerNode, "Set" );
4177 addAttribute( teardropLayerSetNode, "geometryUsage", "TEARDROP" );
4178
4179 if( zone->GetNetCode() > 0 )
4180 addAttribute( teardropLayerSetNode, "net", netName( zone->GetNetname() ) );
4181
4182 wxXmlNode* new_teardrops = appendNode( teardropLayerSetNode, "Features" );
4183 addLocationNode( new_teardrops, 0.0, 0.0 );
4184 teardropFeatureSetNode = appendNode( new_teardrops, "UserSpecial" );
4185 }
4186
4187 zoneFeatureNode = teardropFeatureSetNode;
4188 }
4189 else if( !teardrop_warning )
4190 {
4191 Report( _( "Teardrops are not supported in IPC-2581 revision B; they were exported as zones." ),
4193 teardrop_warning = true;
4194 }
4195 }
4196 else
4197 {
4198 if( FOOTPRINT* fp = zone->GetParentFootprint() )
4199 {
4200 wxXmlNode* tempSetNode = appendNode( aLayerNode, "Set" );
4201 wxString refDes = componentName( fp );
4202
4203 if( componentRefEmitted() )
4204 addAttribute( tempSetNode, "componentRef", refDes );
4205
4206 wxXmlNode* newFeatures = appendNode( tempSetNode, "Features" );
4207 addLocationNode( newFeatures, 0.0, 0.0 );
4208 zoneFeatureNode = appendNode( newFeatures, "UserSpecial" );
4209 }
4210 }
4211
4212 SHAPE_POLY_SET& zone_shape = *zone->GetFilledPolysList( aLayer );
4213
4214 for( int ii = 0; ii < zone_shape.OutlineCount(); ++ii )
4215 addContourNode( zoneFeatureNode, zone_shape, ii );
4216 };
4217
4218 auto add_shape =
4219 [&] ( PCB_SHAPE* shape )
4220 {
4221 PCB_SHAPE adjusted( nullptr );
4222
4223 if( FabMaskShape( *shape, aLayer, adjusted ) )
4224 shape = &adjusted;
4225
4226 FOOTPRINT* fp = shape->GetParentFootprint();
4227
4228 if( fp )
4229 {
4230 wxXmlNode* tempSetNode = appendNode( aLayerNode, "Set" );
4231
4233 addAttribute( tempSetNode, "geometryUsage", "GRAPHIC" );
4234
4235 bool link_to_component = true;
4236
4238 link_to_component = false;
4239
4240 if( link_to_component )
4241 {
4242 if( componentRefEmitted() )
4243 addAttribute( tempSetNode, "componentRef", componentName( fp ) );
4244 }
4245
4246 wxXmlNode* tempFeature = appendNode( tempSetNode, "Features" );
4247
4248 addLocationNode( tempFeature, *shape );
4249 addShape( tempFeature, *shape );
4250 }
4251 else if( shape->GetShape() == SHAPE_T::CIRCLE
4252 || shape->GetShape() == SHAPE_T::RECTANGLE
4253 || shape->GetShape() == SHAPE_T::POLY )
4254 {
4255 wxXmlNode* tempSetNode = appendNode( aLayerNode, "Set" );
4256
4257 // The net only belongs on the copper feature; the mask copy of a netted
4258 // copper shape must not carry a net attribute.
4259 if( shape->GetNetCode() > 0 && IsCopperLayer( aLayer ) )
4260 addAttribute( tempSetNode, "net", netName( shape->GetNetname() ) );
4261
4262 wxXmlNode* tempFeature = appendNode( tempSetNode, "Features" );
4263 addLocationNode( tempFeature, *shape );
4264 addShape( tempFeature, *shape );
4265 }
4266 else
4267 {
4268 addShape( specialNode, *shape );
4269 }
4270 };
4271
4272 auto add_text =
4273 [&] ( BOARD_ITEM* text )
4274 {
4275 EDA_TEXT* text_item = nullptr;
4276 FOOTPRINT* fp = text->GetParentFootprint();
4277
4278 if( PCB_TEXT* pcb_text = dynamic_cast<PCB_TEXT*>( text ) )
4279 text_item = static_cast<EDA_TEXT*>( pcb_text );
4280 else if( PCB_TEXTBOX* pcb_textbox = dynamic_cast<PCB_TEXTBOX*>( text ) )
4281 text_item = static_cast<EDA_TEXT*>( pcb_textbox );
4282
4283 if( !text_item || !text_item->IsVisible() )
4284 return;
4285
4286 wxString content = text_item->GetShownText( RESOLVED );
4287
4288 if( content.empty() )
4289 return;
4290
4291 bool isWhitespace = content.Strip( wxString::both ).empty();
4292 bool isKnockout = text->Type() == PCB_TEXT_T && static_cast<PCB_TEXT*>( text )->IsKnockout();
4293 bool hasBorder = text->Type() == PCB_TEXTBOX_T && static_cast<PCB_TEXTBOX*>( text )->IsBorderEnabled();
4294
4295 if( isWhitespace && !isKnockout && !hasBorder )
4296 return;
4297
4298 wxXmlNode* tempSetNode = appendNode( aLayerNode, "Set" );
4299
4301 addAttribute( tempSetNode, "geometryUsage", "TEXT" );
4302
4303 bool link_to_component = fp != nullptr;
4304
4305 if( m_revision == IPC2581::REVISION::B && fp && isOppositeSideSilk( fp, text->GetLayer() ) )
4306 link_to_component = false;
4307
4308 if( link_to_component )
4309 {
4310 if( componentRefEmitted() )
4311 addAttribute( tempSetNode, "componentRef", componentName( fp ) );
4312 }
4313
4314 wxXmlNode* nonStandardAttributeNode = appendNode( tempSetNode, "NonstandardAttribute" );
4315 addAttribute( nonStandardAttributeNode, "name", "TEXT" );
4316 addAttribute( nonStandardAttributeNode, "value", content );
4317 addAttribute( nonStandardAttributeNode, "type", "STRING" );
4318
4319 if( !isWhitespace || isKnockout )
4320 {
4321 wxXmlNode* glyphFeature = appendNode( tempSetNode, "Features" );
4322 addLocationNode( glyphFeature, 0.0, 0.0 );
4323
4324 if( isKnockout )
4325 addKnockoutText( glyphFeature, static_cast<PCB_TEXT*>( text ) );
4326 else
4327 addText( glyphFeature, text_item, text->GetFontMetrics() );
4328 }
4329
4330 if( hasBorder )
4331 {
4332 PCB_SHAPE* border = static_cast<PCB_TEXTBOX*>( text );
4333 wxXmlNode* borderFeature = appendNode( tempSetNode, "Features" );
4334
4335 addLocationNode( borderFeature, *border );
4336 addShape( borderFeature, *border );
4337 }
4338 };
4339
4340 auto add_pad =
4341 [&]( PAD* pad )
4342 {
4343 if( !padSetNode )
4344 {
4345 if( !has_via )
4346 {
4347 padSetNode = layerSetNode;
4348 has_pad = true;
4349 }
4350 else
4351 {
4352 padSetNode = appendNode( aLayerNode, "Set" );
4353
4354 if( pad->GetNetCode() > 0 )
4355 addAttribute( padSetNode, "net", netName( pad->GetNetname() ) );
4356 }
4357 }
4358
4359 addPad( padSetNode, pad, aLayer );
4360 };
4361
4362 for( BOARD_ITEM* item : aItems )
4363 {
4364 switch( item->Type() )
4365 {
4366 case PCB_TRACE_T:
4367 case PCB_ARC_T:
4368 case PCB_VIA_T:
4369 add_track( static_cast<PCB_TRACK*>( item ) );
4370 break;
4371
4372 case PCB_ZONE_T:
4373 add_zone( static_cast<ZONE*>( item ) );
4374 break;
4375
4376 case PCB_PAD_T:
4377 add_pad( static_cast<PAD*>( item ) );
4378 break;
4379
4380 case PCB_SHAPE_T:
4381 add_shape( static_cast<PCB_SHAPE*>( item ) );
4382 break;
4383
4384 case PCB_TEXT_T:
4385 case PCB_TEXTBOX_T:
4386 case PCB_FIELD_T:
4387 add_text( item );
4388 break;
4389
4390 case PCB_DIMENSION_T:
4391 case PCB_TARGET_T:
4392 case PCB_DIM_ALIGNED_T:
4393 case PCB_DIM_LEADER_T:
4394 case PCB_DIM_CENTER_T:
4395 case PCB_DIM_RADIAL_T:
4397 //TODO: Add support for dimensions
4398 break;
4399
4400 default:
4401 wxLogTrace( traceIpc2581, wxS( "Unhandled type %s" ),
4402 ENUM_MAP<KICAD_T>::Instance().ToString( item->Type() ) );
4403 }
4404 }
4405
4406 if( specialNode->GetChildren() == nullptr )
4407 {
4408 featureSetNode->RemoveChild( specialNode );
4409 deleteNode( specialNode );
4410 }
4411
4412 if( featureSetNode->GetChildren() == nullptr )
4413 {
4414 layerSetNode->RemoveChild( featureSetNode );
4415 deleteNode( featureSetNode );
4416 }
4417
4418 if( layerSetNode->GetChildren() == nullptr )
4419 {
4420 aLayerNode->RemoveChild( layerSetNode );
4421 deleteNode( layerSetNode );
4422 }
4423}
4424
4426{
4427 if( !included( IPC2581::SECTION::MISC_FAB ) )
4428 return;
4429
4430 for( const auto& [layers, vec] : m_auxilliary_Layers )
4431 {
4432 bool add_node = true;
4433
4434 wxString name;
4435 bool hole = false;
4436
4437 // clang-format off: suggestion is inconsitent
4438 switch( std::get<0>(layers) )
4439 {
4441 name = "COVERING";
4442 break;
4444 name = "PLUGGING";
4445 hole = true;
4446 break;
4448 name = "TENTING";
4449 break;
4451 name = "FILLING";
4452 hole = true;
4453 break;
4455 name = "CAPPING";
4456 hole = true;
4457 break;
4458 default:
4459 add_node = false;
4460 break;
4461 }
4462 // clang-format on: suggestion is inconsitent
4463
4464 if( !add_node )
4465 continue;
4466
4467 wxXmlNode* layerNode = appendNode( aStepNode, "LayerFeature" );
4468
4469 if( std::get<2>( layers ) == UNDEFINED_LAYER )
4470 {
4471 layerNode->AddAttribute( "layerRef", genLayerString( std::get<1>( layers ), TO_UTF8( name ) ) );
4472 }
4473 else
4474 {
4475 layerNode->AddAttribute( "layerRef", genLayersString( std::get<1>( layers ),
4476 std::get<2>( layers ), TO_UTF8( name ) ) );
4477 }
4478
4479 wxXmlNode* setNode = appendNode( layerNode, "Set" );
4480
4481 for( BOARD_ITEM* item : vec )
4482 {
4483 if( item->Type() != PCB_VIA_T )
4484 continue;
4485
4486 PCB_VIA* via = static_cast<PCB_VIA*>( item );
4487
4488 PCB_SHAPE shape( nullptr, SHAPE_T::CIRCLE );
4489
4490 if( hole )
4491 shape.SetEnd( { KiROUND( via->GetDrillValue() / 2.0 ), 0 } );
4492 else
4493 shape.SetEnd( { KiROUND( via->GetWidth( std::get<1>( layers ) ) / 2.0 ), 0 } );
4494
4495 wxXmlNode* padNode = appendNode( setNode, "Pad" );
4496 addPadStack( padNode, via );
4497
4498 addLocationNode( padNode, via->GetPosition().x, via->GetPosition().y );
4499 addShape( padNode, shape );
4500 }
4501 }
4502}
4503
4504
4506{
4507 if( !included( IPC2581::SECTION::BOM_AVL ) )
4508 return nullptr;
4509
4510 reportPhase( _( "Generating vendor list" ) );
4511
4512 // Per IPC-2581 schema, Avl requires at least one AvlItem child element.
4513 // Don't emit Avl section if there are no items.
4514 if( m_OEMRef_dict.empty() )
4515 return nullptr;
4516
4517 wxXmlNode* avl = appendNode( m_xml_root, "Avl" );
4518 addAttribute( avl, "name", "Primary_Vendor_List" );
4519
4520 wxXmlNode* header = appendNode( avl, "AvlHeader" );
4521 addAttribute( header, "title", "BOM" );
4522 addAttribute( header, "source", "KiCad" );
4523 addAttribute( header, "author", "OWNER" );
4524 addAttribute( header, "datetime", wxDateTime::Now().FormatISOCombined() );
4525 addAttribute( header, "version", "1" );
4526
4527 std::set<wxString> unique_parts;
4528 std::map<wxString,wxString> unique_vendors;
4529 std::vector<FOOTPRINT*> footprints;
4530 std::vector<AVL_ROW> rows;
4531
4532 for( FOOTPRINT* fp : m_board->Footprints() )
4533 {
4534 auto it = m_OEMRef_dict.find( fp );
4535
4536 if( it == m_OEMRef_dict.end() )
4537 continue;
4538
4539 footprints.push_back( fp );
4540 rows.push_back( { it->second, fp->GetReference() } );
4541 }
4542
4543 for( size_t index : OrderAvlRows( rows ) )
4544 {
4545 FOOTPRINT* fp = footprints[index];
4546 const wxString& name = rows[index].m_oemName;
4547 auto [ it, success ] = unique_parts.insert( name );
4548
4549 if( !success )
4550 continue;
4551
4552 wxXmlNode* part = appendNode( avl, "AvlItem" );
4553 addAttribute( part, "OEMDesignNumber", genString( name, "REF" ) );
4554
4555 PCB_FIELD* nums[2] = { fp->GetField( m_mpn ), fp->GetField( m_distpn ) };
4556 PCB_FIELD* company[2] = { fp->GetField( m_mfg ), nullptr };
4557 wxString company_name[2] = { m_mfg, m_dist };
4558
4559 for ( int ii = 0; ii < 2; ++ii )
4560 {
4561 if( nums[ii] )
4562 {
4563 wxString mpn_name = nums[ii]->GetShownText( RESOLVED );
4564
4565 if( mpn_name.empty() )
4566 continue;
4567
4568 wxXmlNode* vmpn = appendNode( part, "AvlVmpn" );
4569 addAttribute( vmpn, "qualified", "false" );
4570 addAttribute( vmpn, "chosen", "false" );
4571
4572 wxXmlNode* mpn = appendNode( vmpn, "AvlMpn" );
4573 addAttribute( mpn, "name", mpn_name );
4574
4575 wxXmlNode* vendor = appendNode( vmpn, "AvlVendor" );
4576
4577 wxString vendor_name = wxT( "UNKNOWN" );
4578
4579 // If the field resolves, then use that field content unless it is empty
4580 if( !ii && company[ii] )
4581 {
4582 wxString tmp = company[ii]->GetShownText( RESOLVED );
4583
4584 if( !tmp.empty() )
4585 vendor_name = tmp;
4586 }
4587 // If it doesn't resolve but there is content from the dialog, use the static content
4588 else if( !ii && !company_name[ii].empty() )
4589 {
4590 vendor_name = company_name[ii];
4591 }
4592 else if( ii && !m_dist.empty() )
4593 {
4594 vendor_name = m_dist;
4595 }
4596
4597 auto [vendor_id, inserted] = unique_vendors.emplace( vendor_name,
4598 wxString::Format( "VENDOR_%zu",
4599 unique_vendors.size() ) );
4600
4601 addAttribute( vendor, "enterpriseRef", vendor_id->second );
4602
4603 if( inserted )
4604 {
4605 wxXmlNode* new_vendor = new wxXmlNode( wxXML_ELEMENT_NODE, "Enterprise" );
4606 addAttribute( new_vendor, "id", vendor_id->second );
4607 addAttribute( new_vendor, "name", vendor_name );
4608 addAttribute( new_vendor, "code", "NONE" );
4609 insertNodeAfter( m_enterpriseNode, new_vendor );
4610 m_enterpriseNode = new_vendor;
4611 }
4612 }
4613 }
4614 }
4615
4616 return avl;
4617}
4618
4619
4620void PCB_IO_IPC2581::SaveBoard( const wxString& aFileName, BOARD& aBoard,
4621 const std::map<std::string, UTF8>* aProperties )
4622{
4623 // Clean up any previous export state to allow multiple exports per plugin instance
4624 delete m_xml_doc;
4625 m_xml_doc = nullptr;
4626 m_xml_root = nullptr;
4627 m_contentNode = nullptr;
4628 m_lastAppendedNode = nullptr;
4629
4630 m_board = &aBoard;
4631
4632 FAB_STACKUP fabStackup( aBoard );
4633 SCOPED_SET_RESET<const FAB_STACKUP*> fabStackupScope( m_fabStackup, &fabStackup );
4634 FAB_DRILL_MODEL fabDrillModel( aBoard );
4635 SCOPED_SET_RESET<const FAB_DRILL_MODEL*> fabDrillScope( m_fabDrillModel, &fabDrillModel );
4636
4639 m_backdrill_spec_nodes.clear();
4640 m_backdrill_spec_used.clear();
4642 m_post_machining_specs.clear();
4643 m_cad_header_node = nullptr;
4644 m_layer_name_map.clear();
4645
4646 // Clear all internal dictionaries and caches
4647 m_user_shape_dict.clear();
4648 m_shape_user_node = nullptr;
4649 m_std_shape_dict.clear();
4650 m_shape_std_node = nullptr;
4651 m_line_dict.clear();
4652 m_line_node = nullptr;
4653 m_padstack_dict.clear();
4654 m_padstacks.clear();
4655 m_last_padstack = nullptr;
4656 m_footprint_dict.clear();
4659 m_OEMRef_dict.clear();
4660 m_net_pin_dict.clear();
4661 m_net_name_dict.clear();
4662 m_anon_refdes_dict.clear();
4663 m_drill_layers.clear();
4664 m_slot_holes.clear();
4665 m_auxilliary_Layers.clear();
4666 m_element_names.clear();
4667 m_generated_names.clear();
4668 m_acceptable_chars.clear();
4669 m_total_bytes = 0;
4670
4671 m_anonymizeNets = false;
4672 m_omitRefDes = false;
4673 m_haveCadData = true;
4674 m_step_node = nullptr;
4675 m_functionModeNode = nullptr;
4676
4677 m_units_str = "MILLIMETER";
4678 m_scale = 1.0 / PCB_IU_PER_MM;
4679 m_sigfig = 6;
4680
4681 // The base PCB_IO interface permits a null property set; alias it to an empty
4682 // map so the optional lookups below remain valid.
4683 const std::map<std::string, UTF8> emptyProperties;
4684
4685 if( !aProperties )
4686 aProperties = &emptyProperties;
4687
4688 if( auto it = aProperties->find( "variant" ); it != aProperties->end() )
4689 m_variantName = it->second.wx_str();
4690
4691 if( auto it = aProperties->find( "units" ); it != aProperties->end() )
4692 {
4693 if( it->second == "inch" )
4694 {
4695 m_units_str = "INCH";
4696 m_scale = ( 1.0 / 25.4 ) / PCB_IU_PER_MM;
4697 }
4698 }
4699
4700 if( auto it = aProperties->find( "sigfig" ); it != aProperties->end() )
4701 m_sigfig = std::stoi( it->second );
4702
4703 if( auto it = aProperties->find( "version" ); it != aProperties->end() )
4705
4706 if( auto it = aProperties->find( "OEMRef" ); it != aProperties->end() )
4707 m_OEMRef = it->second.wx_str();
4708
4709 if( auto it = aProperties->find( "mpn" ); it != aProperties->end() )
4710 m_mpn = it->second.wx_str();
4711
4712 if( auto it = aProperties->find( "mfg" ); it != aProperties->end() )
4713 m_mfg = it->second.wx_str();
4714
4715 if( auto it = aProperties->find( "dist" ); it != aProperties->end() )
4716 m_dist = it->second.wx_str();
4717
4718 if( auto it = aProperties->find( "distpn" ); it != aProperties->end() )
4719 m_distpn = it->second.wx_str();
4720
4721 if( auto it = aProperties->find( "bomrev" ); it != aProperties->end() )
4722 m_bomRev = it->second.wx_str();
4723
4724 m_mode = IPC2581::MODE::USERDEF;
4726
4727 if( auto it = aProperties->find( "mode" ); it != aProperties->end() )
4728 {
4729 std::optional<IPC2581::MODE> mode = IPC2581::ModeFromToken( it->second.wx_str() );
4730
4731 if( !mode )
4732 THROW_IO_ERROR( wxString::Format( _( "Unknown IPC-2581 data set '%s'." ), it->second.wx_str() ) );
4733
4734 m_mode = *mode;
4736 }
4737
4738 if( auto it = aProperties->find( "sections" ); it != aProperties->end() )
4739 {
4740 if( !IPC2581::SectionSetFromKeyString( it->second.wx_str(), m_requestedSections ) )
4741 THROW_IO_ERROR( wxString::Format( _( "Invalid IPC-2581 section key '%s'." ), it->second.wx_str() ) );
4742 }
4743
4744 if( auto it = aProperties->find( "netnames" ); it != aProperties->end() )
4745 m_anonymizeNets = it->second == "anonymize";
4746
4747 if( m_anonymizeNets )
4748 {
4749 for( const auto& [code, name] : FabAnonymousNetNames( *m_board ) )
4750 {
4751 if( NETINFO_ITEM* net = m_board->GetNetInfo().GetNetItem( code ) )
4752 m_net_name_dict.emplace( net->GetNetname(), name );
4753 }
4754 }
4755
4756 if( auto it = aProperties->find( "refdes" ); it != aProperties->end() )
4757 m_omitRefDes = it->second == "omit";
4758
4760
4761 if( !m_resolved.Ok() )
4762 THROW_IO_ERROR( m_resolved.m_conflicts.front().m_message );
4763
4765 {
4766 for( char c = 'a'; c <= 'z'; ++c )
4767 m_acceptable_chars.insert( c );
4768
4769 for( char c = 'A'; c <= 'Z'; ++c )
4770 m_acceptable_chars.insert( c );
4771
4772 for( char c = '0'; c <= '9'; ++c )
4773 m_acceptable_chars.insert( c );
4774
4775 // Add special characters
4776 std::string specialChars = "_\\-.+><";
4777
4778 for( char c : specialChars )
4779 m_acceptable_chars.insert( c );
4780 }
4781
4782 m_xml_doc = new wxXmlDocument();
4784
4785 if( m_progressReporter )
4786 {
4787 int phases = EXPORT_PHASES;
4788
4789 if( !included( IPC2581::SECTION::LOGICAL_NET ) )
4790 --phases;
4791
4792 if( !included( IPC2581::SECTION::PHYSICAL_NET ) )
4793 --phases;
4794
4795 if( !included( IPC2581::SECTION::BOM_AVL ) )
4796 phases -= 2;
4797
4798 m_progressReporter->SetNumPhases( phases );
4799 m_progressReporter->BeginPhase( 0 );
4800 }
4801
4803
4804 reportPhase( _( "Generating logistic section" ) );
4807
4808 wxXmlNode* ecad_node = generateEcadSection();
4809 wxXmlNode* bom_node = generateBOMSection( ecad_node );
4810 wxXmlNode* avl_node = generateAvlSection();
4811
4812 // Insert BomRef/AvlRef into Content section per IPC-2581C 4.1.1.2.
4813 // They go after LayerRef and before Dictionary* nodes.
4814 if( m_contentNode && ( bom_node || avl_node ) )
4815 {
4816 wxXmlNode* insertBefore = nullptr;
4817
4818 for( wxXmlNode* child = m_contentNode->GetChildren(); child; child = child->GetNext() )
4819 {
4820 if( child->GetName().StartsWith( "Dictionary" ) )
4821 {
4822 insertBefore = child;
4823 break;
4824 }
4825 }
4826
4827 auto insertRef =
4828 [&]( const wxString& aNodeName, wxXmlNode* aSection )
4829 {
4830 if( !aSection )
4831 return;
4832
4833 wxXmlNode* ref = new wxXmlNode( wxXML_ELEMENT_NODE, aNodeName );
4834 ref->AddAttribute( "name", aSection->GetAttribute( "name" ) );
4835
4836 if( insertBefore )
4837 m_contentNode->InsertChild( ref, insertBefore );
4838 else
4839 m_contentNode->AddChild( ref );
4840 };
4841
4842 insertRef( "BomRef", bom_node );
4843 insertRef( "AvlRef", avl_node );
4844 }
4845
4846 reportPhase( _( "Resolving references" ) );
4847
4848 if( m_functionModeNode )
4850
4851 reportPhase( _( "Saving file" ) );
4852
4853 wxFileOutputStreamWithProgress out_stream( aFileName );
4854 double written_bytes = 0.0;
4855 double last_yield = 0.0;
4856
4857 // This is a rough estimation of the size of the spaces in the file
4858 // We just need to total to be slightly larger than the value of the
4859 // progress bar, so accurately counting spaces is not terribly important
4861
4862 auto update_progress =
4863 [&]( size_t aBytes )
4864 {
4865 written_bytes += aBytes;
4866 double percent = written_bytes / static_cast<double>( m_total_bytes );
4867
4868 if( m_progressReporter )
4869 {
4870 // Only update every percent
4871 if( last_yield + 0.01 < percent )
4872 {
4873 last_yield = percent;
4874 m_progressReporter->SetCurrentProgress( percent );
4875 m_progressReporter->KeepRefreshing();
4876 }
4877 }
4878 };
4879
4880 out_stream.SetProgressCallback( update_progress );
4881
4882
4883 // wxXmlDocument writes a small number of bytes at a time
4884 // An unbuffered stream then makes millions of small writes
4885 wxBufferedOutputStream buffered_stream( out_stream, 1 << 20 );
4886
4887 if( !m_xml_doc->Save( buffered_stream ) )
4888 THROW_IO_ERROR( _( "Failed to save IPC-2581 data to buffer." ) );
4889
4890 buffered_stream.Sync();
4891
4892 // A large document needs seconds to release
4893 // The destructor releases it after the progress reporter stops and the dialog stops
4894 reportPhase( _( "Finishing" ) );
4895 delete m_xml_doc;
4896 m_xml_doc = nullptr;
4897 m_xml_root = nullptr;
4898 m_contentNode = nullptr;
4899 m_lastAppendedNode = nullptr;
4900 m_functionModeNode = nullptr;
4901 m_step_node = nullptr;
4902 m_last_padstack = nullptr;
4903 m_padstacks.clear();
4904}
int index
const char * name
@ ERROR_INSIDE
constexpr int ARC_HIGH_DEF
Definition base_units.h:144
constexpr double PCB_IU_PER_MM
Pcbnew IU is 1 nanometer.
Definition base_units.h:68
static bool hasBackdrill(const PADSTACK &aPadstack)
bool IsPrmSpecified(const wxString &aPrmValue)
@ BS_ITEM_TYPE_COPPER
@ BS_ITEM_TYPE_SILKSCREEN
@ BS_ITEM_TYPE_DIELECTRIC
@ BS_ITEM_TYPE_SOLDERMASK
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
wxString GetMajorMinorPatchVersion()
Get the major, minor and patch version in a string major.minor.patch This is extracted by CMake from ...
Bezier curves to polygon converter.
void GetPoly(std::vector< VECTOR2I > &aOutput, int aMaxError=10)
Convert a Bezier curve to a polygon.
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
FOOTPRINT * GetParentFootprint() const
VECTOR2I GetFPRelativePosition() const
int GetMaxError() const
Manage one layer needed to make a physical board.
wxString GetTypeName() const
int GetSublayersCount() const
double GetEpsilonR(int aDielectricSubLayer=0) const
wxString GetColor(int aDielectricSubLayer=0) const
wxString GetLayerName() const
PCB_LAYER_ID GetBrdLayerId() const
int GetThickness(int aDielectricSubLayer=0) const
BOARD_STACKUP_ITEM_TYPE GetType() const
wxString GetMaterial(int aDielectricSubLayer=0) const
int GetDielectricLayerId() const
double GetLossTangent(int aDielectricSubLayer=0) const
Manage layers needed to make a physical board.
const std::vector< BOARD_STACKUP_ITEM * > & GetList() const
int GetCount() const
int BuildBoardThicknessFromStackup() const
int GetLayerDistance(PCB_LAYER_ID aFirstLayer, PCB_LAYER_ID aSecondLayer) const
Calculate the distance (height) between the two given copper layers.
wxString m_FinishType
The name of external copper finish.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
wxString GetCurrentVariant() const
Definition board.h:530
constexpr size_type GetWidth() const
Definition box2.h:217
constexpr const Vec GetCenter() const
Definition box2.h:233
constexpr size_type GetHeight() const
Definition box2.h:218
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
const KIID m_Uuid
Definition eda_item.h:599
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
int GetEllipseMinorRadius() const
Definition eda_shape.h:404
const VECTOR2I & GetBezierC2() const
Definition eda_shape.h:377
const VECTOR2I & GetEllipseCenter() const
Definition eda_shape.h:386
EDA_ANGLE GetEllipseEndAngle() const
Definition eda_shape.h:432
FILL_T GetFillMode() const
Definition eda_shape.h:151
int GetEllipseMajorRadius() const
Definition eda_shape.h:395
int GetRectangleWidth() const
SHAPE_POLY_SET & GetPolyShape()
EDA_ANGLE GetEllipseRotation() const
Definition eda_shape.h:413
int GetRadius() const
SHAPE_T GetShape() const
Definition eda_shape.h:178
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:334
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:284
std::vector< VECTOR2I > GetRectCorners() const
EDA_ANGLE GetEllipseStartAngle() const
Definition eda_shape.h:423
const VECTOR2I & GetBezierC1() const
Definition eda_shape.h:374
int GetRectangleHeight() const
bool IsClockwiseArc() const
int GetCornerRadius() const
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:94
virtual bool IsVisible() const
Definition eda_text.h:226
virtual EDA_ANGLE GetDrawRotation() const
Definition eda_text.h:425
virtual VECTOR2I GetDrawPos() const
Definition eda_text.h:426
virtual wxString GetShownText(RESOLUTION_CONTEXT aContext, int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition eda_text.h:128
virtual KIFONT::FONT * GetDrawFont(const RENDER_SETTINGS *aSettings) const
Definition eda_text.cpp:648
const TEXT_ATTRIBUTES & GetAttributes() const
Definition eda_text.h:270
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition eda_text.cpp:428
static ENUM_MAP< T > & Instance()
Definition property.h:811
Set of schema sections.
SECTION_SET & Set(SECTION aSection, bool aOn=true)
Board items grouped by authored layer and net, sorted by FabItemLess.
std::map< int, std::vector< BOARD_ITEM * > > NET_ITEMS
Board stackup snapshot synchronized without changing the source board.
Definition fab_stackup.h:29
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
PCB_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this footprint.
std::deque< PAD * > & Pads()
Definition footprint.h:355
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition footprint.h:393
bool IsFlipped() const
Definition footprint.h:620
const wxString & GetReference() const
Definition footprint.h:793
wxString m_name
Name of the IO loader.
Definition io_base.h:261
virtual void Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) const
Definition io_base.cpp:124
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:267
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:94
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttributes, const METRICS &aFontMetrics, std::optional< VECTOR2I > aMousePos=std::nullopt, wxString *aActiveUrl=nullptr) const
Draw a string.
Definition font.cpp:243
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
double r
Red component.
Definition color4d.h:391
double g
Green component.
Definition color4d.h:392
double b
Blue component.
Definition color4d.h:393
wxString AsString() const
Definition kiid.cpp:264
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition lseq.h:47
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition lset.cpp:309
Handle the data for a net.
Definition netinfo.h:50
const wxString & GetNetname() const
Definition netinfo.h:110
Container for NETINFO_ITEM elements, which are the nets.
Definition netinfo.h:232
unsigned GetNetCount() const
Definition netinfo.h:255
A PADSTACK defines the characteristics of a single or multi-layer pad, in the IPC sense of the word.
Definition padstack.h:161
PCB_LAYER_ID EffectiveLayerFor(PCB_LAYER_ID aLayer) const
Determines which geometry layer should be used for the given input layer.
POST_MACHINING_PROPS & FrontPostMachining()
Definition padstack.h:372
DRILL_PROPS & TertiaryDrill()
Definition padstack.h:369
DRILL_PROPS & SecondaryDrill()
Definition padstack.h:366
POST_MACHINING_PROPS & BackPostMachining()
Definition padstack.h:375
A pad's resolved artwork on one fabrication layer.
const SHAPE_POLY_SET & Contour() const
const PAD & Pad() const
Definition pad.h:61
LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition pad.h:579
bool FlashLayer(int aLayer, bool aOnlyCheckIfPermitted=false) const
Check to see whether the pad should be flashed on the specific layer.
Definition pad.cpp:697
PAD_ATTRIB GetAttribute() const
Definition pad.h:582
const wxString & GetNumber() const
Definition pad.h:143
VECTOR2I GetPosition() const override
Definition pad.cpp:255
VECTOR2I GetOffset(PCB_LAYER_ID aLayer) const
Definition pad.cpp:848
VECTOR2I GetDrillSize() const
Definition pad.h:320
PAD_SHAPE GetShape(PCB_LAYER_ID aLayer) const
Definition pad.h:205
int GetSolderMaskExpansion(PCB_LAYER_ID aLayer) const
Definition pad.cpp:1960
VECTOR2I GetSize(PCB_LAYER_ID aLayer) const
Definition pad.cpp:297
const PADSTACK & Padstack() const
Definition pad.h:331
EDA_ANGLE GetOrientation() const
Return the rotation angle of the pad.
Definition pad.cpp:1672
PAD_DRILL_SHAPE GetDrillShape() const
Definition pad.h:438
int GetChamferPositions(PCB_LAYER_ID aLayer) const
Definition pad.h:878
double GetChamferRectRatio(PCB_LAYER_ID aLayer) const
Definition pad.h:860
VECTOR2I GetSolderPasteMargin(PCB_LAYER_ID aLayer) const
Usually < 0 (mask shape smaller than pad)because the margin can be dependent on the pad size,...
Definition pad.cpp:2022
bool HasHole() const override
Definition pad.h:113
bool TransformHoleToPolygon(SHAPE_POLY_SET &aBuffer, int aClearance, int aError, ERROR_LOC aErrorLoc=ERROR_INSIDE) const
Build the corner list of the polygonal drill shape in the board coordinate system.
Definition pad.cpp:3003
bool IsDegenerated(int aThreshold=5) const
const VECTOR2I & GetMid() const
Definition pcb_track.h:304
wxString GetShownText(RESOLUTION_CONTEXT aContext, int aDepth=0) const override
Return the string actually shown after processing of the base text.
void addText(wxXmlNode *aContentNode, EDA_TEXT *aShape, const KIFONT::METRICS &aFontMetrics)
wxString floatVal(double aVal, int aSigFig=-1) const
wxXmlNode * m_step_node
void generateLayerSetDrill(wxXmlNode *aStepNode)
wxString genLayerString(PCB_LAYER_ID aLayer, const char *aPrefix) const
wxXmlNode * generateContentSection()
Creates the Content section of the XML file.
wxXmlNode * appendNode(wxXmlNode *aParent, const wxString &aName)
void addBackdrillSpecRefs(wxXmlNode *aHoleNode, const wxString &aPadstackName)
void generateDrillLayers(wxXmlNode *aCadLayerNode)
wxString sanitizeId(const wxString &aStr) const
wxXmlNode * addPackage(wxXmlNode *aStepNode, FOOTPRINT *aFootprint)
void generateComponents(wxXmlNode *aStepNode)
IPC2581::SECTION_SET emittedSections() const
Schema sections that the finished document holds.
bool addContourNode(wxXmlNode *aParentNode, const SHAPE_POLY_SET &aPolySet, int aOutline=0, FILL_T aFillType=FILL_T::FILLED_SHAPE, int aWidth=0, LINE_STYLE aDashType=LINE_STYLE::SOLID)
bool refDesEmitted() const
True if a Component keeps a designator that a reference can name.
wxString componentName(FOOTPRINT *aFootprint)
void addShape(wxXmlNode *aContentNode, const PCB_SHAPE &aShape, bool aInline=false)
IPC2581::RESOLVE_RESULT m_resolved
std::map< FOOTPRINT *, wxString > m_anon_refdes_dict
bool componentRefEmitted() const
True if a component reference can name a Component that the export wrote.
bool addOutlineNode(wxXmlNode *aParentNode, const SHAPE_POLY_SET &aPolySet, int aWidth=0, LINE_STYLE aDashType=LINE_STYLE::SOLID)
void generateStackup(wxXmlNode *aCadLayerNode)
std::map< std::tuple< auxLayerType, PCB_LAYER_ID, PCB_LAYER_ID >, std::vector< BOARD_ITEM * > > m_auxilliary_Layers
bool addPolygonNode(wxXmlNode *aParentNode, const SHAPE_LINE_CHAIN &aPolygon, FILL_T aFillType=FILL_T::FILLED_SHAPE, int aWidth=0, LINE_STYLE aDashType=LINE_STYLE::SOLID)
void insertNodeAfter(wxXmlNode *aPrev, wxXmlNode *aNode)
void generateCadLayers(wxXmlNode *aCadLayerNode)
std::vector< wxXmlNode * > m_padstacks
wxString pinName(const PAD *aPad) const
bool stackupRowIncluded(bool aDielectric, PCB_LAYER_ID aLayer) const
True if a stackup row is in a schema section that this export holds.
void generateCadSpecs(wxXmlNode *aCadLayerNode)
void addVia(wxXmlNode *aContentNode, const PCB_VIA *aVia, PCB_LAYER_ID aLayer)
IPC2581::SECTION_SET m_requestedSections
void addSlotCavity(wxXmlNode *aContentNode, const PAD &aPad, const wxString &aName)
void deleteNode(wxXmlNode *&aNode)
wxXmlNode * m_last_padstack
size_t lineHash(int aWidth, LINE_STYLE aDashType)
void SaveBoard(const wxString &aFileName, BOARD &aBoard, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aBoard to a storage file in a format that this PCB_IO implementation knows about or it can be u...
std::map< size_t, wxString > m_std_shape_dict
wxString netName(const wxString &aNetname, const wxString &aSuffix=wxEmptyString)
Name to write for aNetname with the net name policy.
void addAttribute(wxXmlNode *aNode, const wxString &aName, const wxString &aValue)
wxXmlNode * m_shape_user_node
wxXmlNode * generateAvlSection()
Creates the Approved Vendor List section.
wxXmlNode * generateHistorySection()
Creates the history section.
wxString stackupLayerName(const BOARD_STACKUP_ITEM *aItem, int aSublayerId, const char *aPrefix) const
wxXmlNode * m_contentNode
IPC2581::MODE m_mode
void addXY(wxXmlNode *aNode, const VECTOR2I &aVec, const char *aXName=nullptr, const char *aYName=nullptr)
void reportPhase(const wxString &aMessage)
Go to the next progress phase and repaint.
wxXmlNode * m_shape_std_node
void addPadStack(wxXmlNode *aContentNode, const PAD *aPad)
std::map< FOOTPRINT *, wxString > m_OEMRef_dict
void clearLoadedFootprints()
Frees the memory allocated for the loaded footprints in m_loaded_footprints.
std::map< wxString, wxXmlNode * > m_backdrill_spec_nodes
std::map< std::pair< PCB_LAYER_ID, PCB_LAYER_ID >, std::vector< PAD * > > m_slot_holes
std::map< size_t, wxString > m_footprint_dict
wxXmlNode * m_functionModeNode
wxXmlNode * generateLogisticSection()
Creates the logistical data header.
std::map< std::tuple< bool, int, int, int, int >, wxString > m_post_machining_specs
std::map< wxString, wxString > m_net_name_dict
std::map< size_t, wxString > m_line_dict
void addLineDesc(wxXmlNode *aNode, int aWidth, LINE_STYLE aDashType, bool aForce=false)
void addKnockoutText(wxXmlNode *aContentNode, PCB_TEXT *aText)
std::map< size_t, wxString > m_padstack_dict
void addPostMachiningSpecRefs(wxXmlNode *aHoleNode, const DRILL_OPERATION &aOperation)
std::map< DRILL_SPAN, std::vector< BOARD_ITEM * > > m_drill_layers
std::map< wxString, FOOTPRINT * > m_footprint_refdes_dict
void generateProfile(wxXmlNode *aStepNode)
void generateLayerFeatures(wxXmlNode *aStepNode)
void generateLayerSetNet(wxXmlNode *aLayerNode, PCB_LAYER_ID aLayer, const std::vector< BOARD_ITEM * > &aItems)
bool suppressed(IPC2581::SUPPRESS aWhat) const
wxXmlNode * generateBOMSection(wxXmlNode *aEcadNode)
Creates the BOM section.
wxXmlNode * generateContentStackup(wxXmlNode *aContentNode)
const FAB_DRILL_MODEL * m_fabDrillModel
void generateAuxilliaryLayers(wxXmlNode *aCadLayerNode)
wxXmlNode * m_cad_header_node
void addCadHeader(wxXmlNode *aEcadNode)
void generateLayerSetAuxilliary(wxXmlNode *aStepNode)
bool layerIncluded(PCB_LAYER_ID aLayer) const
True if aLayer is in a schema section that this export holds.
std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints() override
Return a container with the cached library footprints generated in the last call to Load.
const std::map< std::string, UTF8 > * m_props
wxString genLayersString(PCB_LAYER_ID aTop, PCB_LAYER_ID aBottom, const char *aPrefix) const
void generateLogicalNets(wxXmlNode *aStepNode)
void addFillDesc(wxXmlNode *aNode, FILL_T aFillType, bool aForce=false)
std::map< size_t, wxString > m_user_shape_dict
void generatePhyNetGroup(wxXmlNode *aStepNode)
std::map< wxString, std::set< wxString > > m_element_names
~PCB_IO_IPC2581() override
wxString genString(const wxString &aStr, const char *aPrefix=nullptr) const
static constexpr int EXPORT_PHASES
Content logistic history CAD data components the two netlists BOM vendor list references the write an...
wxXmlNode * m_xml_root
std::vector< FOOTPRINT * > m_loaded_footprints
bool addPolygonCutouts(wxXmlNode *aParentNode, const SHAPE_POLY_SET::POLYGON &aPolygon)
void pruneUnusedBackdrillSpecs()
IPC2581::REVISION m_revision
std::set< wxUniChar > m_acceptable_chars
void addLayerAttributes(wxXmlNode *aNode, PCB_LAYER_ID aLayer)
wxXmlNode * generateXmlHeader()
Creates the XML header for IPC-2581.
void ensureBackdrillSpecs(const wxString &aPadstackName, const PADSTACK &aPadstack)
wxXmlNode * generateEcadSection()
Creates the ECAD section.
const FAB_STACKUP * m_fabStackup
wxXmlDocument * m_xml_doc
bool included(IPC2581::SECTION aSection) const
wxXmlNode * insertNode(wxXmlNode *aParent, const wxString &aName)
wxString m_variantName
void addPad(wxXmlNode *aContentNode, const PAD *aPad, PCB_LAYER_ID aLayer)
void generateContentLayerRefs(wxXmlNode *aCadDataNode)
void generateStepSection(wxXmlNode *aCadNode)
std::map< PCB_LAYER_ID, wxString > m_layer_name_map
void dropContentStepRefs()
Remove Content/StepRef when the export writes no Step.
std::map< wxString, wxString > m_generated_names
std::set< wxString > m_backdrill_spec_used
void addLocationNode(wxXmlNode *aContentNode, double aX, double aY)
std::map< wxString, std::array< wxString, 2 > > m_padstack_backdrill_specs
wxXmlNode * m_line_node
std::map< int, std::vector< std::pair< wxString, wxString > > > m_net_pin_dict
void tickProgress(const wxString &aMessage=wxEmptyString)
Count one unit of work in this phase and repaint at intervals.
std::map< FOOTPRINT *, wxString > m_footprint_refdes_reverse_dict
wxXmlNode * m_lastAppendedNode
Optimization for appendNode to avoid O(n) child traversal.
wxXmlNode * m_enterpriseNode
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition pcb_shape.h:80
void SetWidth(int aWidth) override
void SetEnd(const VECTOR2I &aEnd) override
STROKE_PARAMS GetStroke() const override
void SetStart(const VECTOR2I &aStart) override
void SetArcGeometry(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd) override
Set the three controlling points for an arc.
VECTOR2I GetPosition() const override
Definition pcb_shape.h:78
void TransformTextToPolySet(SHAPE_POLY_SET &aBuffer, int aClearance, int aMaxError, ERROR_LOC aErrorLoc) const
Convert the text to a polygonSet describing the actual character strokes (one per segment).
Definition pcb_text.cpp:784
const VECTOR2I & GetStart() const
Definition pcb_track.h:100
const VECTOR2I & GetEnd() const
Definition pcb_track.h:97
PCB_LAYER_ID BottomLayer() const
VECTOR2I GetPosition() const override
Definition pcb_track.h:615
bool FlashLayer(int aLayer) const
Check to see whether the via should have a pad on the specific layer.
const PADSTACK & Padstack() const
Definition pcb_track.h:435
int GetWidth() const override
PCB_LAYER_ID TopLayer() const
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.
RAII class that sets an value at construction and resets it to the original value at destruction.
SHAPE_LINE_CHAIN ConvertToPolyline(int aMaxError) const
Build a polyline approximation of the ellipse or arc.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int Width() const
Get the current width of the segments in the chain.
int PointCount() const
Return the number of points (vertices) in this line chain.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
const std::vector< VECTOR2I > & CPoints() const
Represent a set of closed polygons.
POLYGON & Polygon(int aIndex)
Return the aIndex-th subpolygon in the set.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
void Simplify()
Simplify the polyset (merges overlapping polys, eliminates degeneracy/self-intersections)
std::vector< SHAPE_LINE_CHAIN > POLYGON
represents a single polygon outline with holes.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int NewOutline()
Creates a new empty polygon in the set and returns its index.
int OutlineCount() const
Return the number of outlines in the set.
void Fracture(bool aSimplify=true)
Convert a set of polygons with holes to a single outline with "slits"/"fractures" connecting the oute...
int GetWidth() const
LINE_STYLE GetLineStyle() const
EDA_ORIENTATION m_Angle
Handle a list of polygons defining a copper zone.
Definition zone.h:70
void SetProgressCallback(std::function< void(size_t)> aCallback)
@ FOR_GUI
Definition common.h:93
@ FOR_CANVAS
Definition common.h:92
@ RESOLVED
Definition common.h:97
static bool empty(const wxTextEntryBase *aCtrl)
bool IsDrillSlot(const PAD &aPad)
Whether a pad's hole is a slot rather than a round drill.
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:472
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:474
static constexpr EDA_ANGLE ANGLE_180
Definition eda_angle.h:476
FILL_T
Definition eda_fill.h:29
@ NO_FILL
Definition eda_fill.h:30
@ FILLED_SHAPE
Fill with object color.
Definition eda_fill.h:31
@ UNDEFINED
Definition eda_shape.h:56
@ ELLIPSE
Definition eda_shape.h:63
@ SEGMENT
Definition eda_shape.h:57
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:58
@ ELLIPSE_ARC
Definition eda_shape.h:64
FAB_COMPONENT_ROW MakeFabComponentRow(const FOOTPRINT &aFootprint, const wxString &aVariant)
PCB_LAYER_ID FabBackdrillMustNotCut(const LSEQ &aCopperStack, PCB_LAYER_ID aStart, PCB_LAYER_ID aMustCut)
aMustCut is DRILL_PROPS.end; returns the next enabled copper past it away from aStart,...
Definition fab_drill.cpp:26
int FabTrackWidth(const PCB_TRACK &aTrack, PCB_LAYER_ID aLayer)
Use only for traces and arcs; vias require a layer-specific width.
bool FabMaskShape(const PCB_SHAPE &aShape, PCB_LAYER_ID aLayer, PCB_SHAPE &aResult)
Build an expanded mask copy of an exposed copper shape when needed.
std::map< int, wxString > FabAnonymousNetNames(const BOARD &aBoard)
Anonymous names NET_1 through NET_n keyed and numbered by net code.
PAD_LAYER_GEOMETRY ResolvePadLayer(const PAD &aPad, PCB_LAYER_ID aLayer, int aMaxError)
Artwork on one layer as plotted by PlotStandardLayer; keep the plotter independent as the parity orac...
VECTOR2I ResolvePadMargin(const PAD &aPad, PCB_LAYER_ID aLayer)
Per-side mask or paste margin as PlotStandardLayer applies it.
FAB_PIN MakeFabPin(const PAD &aPad, size_t aIndexInFootprint)
aIndexInFootprint is the pad's position in its footprint, used to name NPTH and unnumbered pads
Definition fab_pin.cpp:50
std::vector< FAB_TEST_POINT > FabTestPoints(const BOARD &aBoard)
The caller must build board connectivity before enumerating net endpoints.
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define THROW_IO_CANCELLED()
static const wxChar traceIpc2581[]
This program source code file is part of KiCad, a free EDA CAD application.
static constexpr void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition hash.h:28
static constexpr std::size_t hash_val(const Types &... args)
Definition hash.h:48
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
@ RELATIVE_TO_PARENT_COORDS
Definition hash_eda.h:44
std::string source
std::vector< size_t > OrderAvlRows(const std::vector< AVL_ROW > &aRows)
Return row indices sorted by OEM name, reference, then input order.
surfaceFinishType
IPC-6012 surface finish types from Table 3-3 "Final Finish and Coating Requirements".
@ OTHER
Non-standard finish.
@ DIG
Direct Immersion Gold.
@ IAG
Immersion Silver.
@ ISN
Immersion Tin.
@ S
Solder (HASL/SMOBC)
@ HT_OSP
High Temperature OSP.
@ ENEPIG_N
ENEPIG for soldering (normal gold thickness)
@ NONE
No surface finish / not specified - skip coating layer generation.
@ OSP
Organic Solderability Preservative.
@ G
Gold (hard gold)
@ ENIG_N
ENIG for soldering (normal gold thickness)
bool IsUserLayer(PCB_LAYER_ID aLayerId)
Test whether a layer is a non copper and a non tech layer.
Definition layer_ids.h:826
bool IsFrontLayer(PCB_LAYER_ID aLayerId)
Layer classification: check if it's a front layer.
Definition layer_ids.h:847
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition layer_ids.h:744
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
@ UNDEFINED_LAYER
Definition layer_ids.h:57
@ 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
bool IsValidLayer(int aLayerId)
Test whether a given integer is a valid layer index, i.e.
Definition layer_ids.h:722
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:28
wxString SectionKeyString(const SECTION_SET &aSet)
Give aSet as a sectionKey attribute value.
SECTION
Schema sections of the IPC-2581C function mode table, Table 4 p 80.
bool SectionSetFromKeyString(const wxString &aKey, SECTION_SET &aResult)
Read a sectionKey attribute value Return false for an unknown character.
SECTION_SET RecommendedOptionalSections(MODE aMode)
Optional schema sections that aMode selects by default.
std::optional< MODE > ModeFromToken(const wxString &aToken)
Read a Table 4 function mode token in upper case or in lower case.
wxString ModeToken(MODE aMode)
Table 4 function mode token such as FABRICATION.
bool NeedsCadData(const SECTION_SET &aSet)
True if an included schema section needs an Ecad/CadData element.
RESOLVE_RESULT ResolveSections(REVISION aRevision, MODE aMode, const SECTION_SET &aRequested)
Compare aRequested with Table 4 and give the attributes to remove and the conflicts.
std::optional< SECTION > SectionForLayerFunction(const wxString &aLayerFunction, const wxString &aSide)
Schema section that holds the layers with aLayerFunction.
@ PINREF_COMPONENTREF
Pad/PinRef@componentRef and LogicalNet/PinRef@componentRef.
@ PAD_PADSTACKDEFREF
Pad@padstackDefRef, and the PadStackDef elements themselves.
@ COMPONENT_PACKAGEREF
Component@packageRef.
@ BOM_REFDES_PACKAGEREF
Bom/BomItem/RefDes@packageRef.
@ BOM_REFDES
Bom/BomItem/RefDes and all its attributes.
@ BOM_REFDES_LAYERREF
Bom/BomItem/RefDes@layerRef.
std::optional< SECTION > SectionForBoardLayer(PCB_LAYER_ID aLayer)
Schema section that holds the artwork of aLayer.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:104
@ PTH
Plated through hole pad.
Definition padstack.h:99
@ CHAMFERED_RECT
Definition padstack.h:61
@ ROUNDRECT
Definition padstack.h:58
@ TRAPEZOID
Definition padstack.h:57
@ RECTANGLE
Definition padstack.h:55
static const std::map< surfaceFinishType, wxString > surfaceFinishTypeToString
Map surfaceFinishType enum to IPC-2581 XML string values.
static bool isOppositeSideSilk(const FOOTPRINT *aFootprint, PCB_LAYER_ID aLayer)
static void mixBackdrillIntoPadstackHash(size_t &aHash, const PADSTACK &aPadstack)
static size_t ipcPadstackHash(const PCB_VIA *aVia, const DRILL_OPERATION *aOperation)
static const std::map< wxString, surfaceFinishType > surfaceFinishMap
Map KiCad surface finish strings to IPC-6012 surfaceFinishType enum.
static wxString propertyUnitForCadUnits(const wxString &aCadUnits)
static surfaceFinishType getSurfaceFinishType(const wxString &aFinish)
see class PGM_BASE
CITER next(CITER it)
Definition ptree.cpp:120
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
static wxString nodeName(const wxString &aSymbolPin)
std::vector< FAB_LAYER_COLOR > dummy
const std::vector< FAB_LAYER_COLOR > & GetStandardColors(BOARD_STACKUP_ITEM_TYPE aType)
#define KEY_CORE
wxString FormatTrimmedDecimal(double aValue, int aPrecision)
Format a value to aPrecision decimal digits and strip trailing zeros, keeping one digit after the dec...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
LINE_STYLE
Dashed line types.
One machining action, which is also one NC hit and one tool assignment.
bool IsBackdrill() const
DRILL_POST_MACHINING m_BackPostMachining
PCB_LAYER_ID m_TopLayer
BOARD_ITEM * m_SourceItem
non-owning, valid only for this enumeration
DRILL_POST_MACHINING m_FrontPostMachining
int m_Size
int m_Depth
int m_Angle
PAD_DRILL_POST_MACHINING_MODE m_Mode
Footprint facts resolved for one assembly variant.
wxString Value() const
wxString Field(const wxString &aName) const
Variant-resolved shown text, empty when the field is missing.
wxString m_name
Definition fab_pin.h:58
FAB_ELECTRICAL m_electrical
Definition fab_pin.h:61
The properties of a padstack drill.
Definition padstack.h:277
PCB_LAYER_ID start
Definition padstack.h:280
PCB_LAYER_ID end
Definition padstack.h:281
@ DESCRIPTION
Field Description of part, i.e. "1/4W 1% Metal Film Resistor".
std::vector< DATABASE_CONNECTION::ROW > rows
static EDA_ORIENTATION orientation(double d)
KIBIS top(path, &reporter)
KIBIS_PIN * pin
VECTOR2I center
const SHAPE_LINE_CHAIN chain
int radius
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition typeinfo.h:98
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:95
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition typeinfo.h:96
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:85
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:100
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:84
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:82
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition typeinfo.h:99
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:94
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:79
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:90
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition typeinfo.h:92
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition typeinfo.h:97
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707