KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io_kicad_sexpr.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2012 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25// base64 code. Needed for PCB_REFERENCE_IMAGE
26#define wxUSE_BASE64 1
27#include <wx/base64.h>
28#include <wx/dir.h>
29#include <wx/ffile.h>
30#include <wx/log.h>
31#include <wx/msgdlg.h>
32#include <wx/mstream.h>
33
34#include <board.h>
36#include <callback_gal.h>
37#include <confirm.h>
38#include <convert_basic_shapes_to_polygon.h> // for enum RECT_CHAMFER_POSITIONS definition
39#include <fmt/core.h>
40#include <font/fontconfig.h>
41#include <footprint.h>
43#include <kiface_base.h>
44#include <layer_range.h>
45#include <locale_io.h>
46#include <macros.h>
47#include <pad.h>
48#include <pcb_dimension.h>
49#include <pcb_generator.h>
50#include <pcb_group.h>
53#include <pcb_reference_image.h>
54#include <pcb_shape.h>
55#include <pcb_table.h>
56#include <pcb_tablecell.h>
57#include <pcb_target.h>
58#include <pcb_text.h>
59#include <pcb_textbox.h>
60#include <pcb_track.h>
61#include <pcbnew_settings.h>
62#include <pgm_base.h>
63#include <progress_reporter.h>
64#include <reporter.h>
65#include <string_utils.h>
66#include <trace_helpers.h>
68#include <zone.h>
69
70#include <build_version.h>
71#include <filter_reader.h>
72#include <ctl_flags.h>
73
74
75using namespace PCB_KEYS_T;
76
77
78FP_CACHE_ITEM::FP_CACHE_ITEM( FOOTPRINT* aFootprint, const WX_FILENAME& aFileName ) :
79 m_filename( aFileName ),
80 m_footprint( aFootprint )
81{ }
82
83
84FP_CACHE::FP_CACHE( PCB_IO_KICAD_SEXPR* aOwner, const wxString& aLibraryPath )
85{
86 m_owner = aOwner;
87 m_lib_raw_path = aLibraryPath;
88 m_lib_path.SetPath( aLibraryPath );
90 m_cache_dirty = true;
91}
92
93
94void FP_CACHE::Save( FOOTPRINT* aFootprint )
95{
97
98 if( !m_lib_path.DirExists() && !m_lib_path.Mkdir() )
99 {
100 THROW_IO_ERROR( wxString::Format( _( "Cannot create footprint library '%s'." ),
101 m_lib_raw_path ) );
102 }
103
104 if( !m_lib_path.IsDirWritable() )
105 {
106 THROW_IO_ERROR( wxString::Format( _( "Footprint library '%s' is read only." ),
107 m_lib_raw_path ) );
108 }
109
110 for( FP_CACHE_FOOTPRINT_MAP::iterator it = m_footprints.begin(); it != m_footprints.end(); ++it )
111 {
112 if( aFootprint && aFootprint != it->second->GetFootprint() )
113 continue;
114
115 // If we've requested to embed the fonts in the footprint, do so.
116 // Otherwise, clear the embedded fonts from the footprint. Embedded
117 // fonts will be used if available
118 if( aFootprint->GetAreFontsEmbedded() )
119 aFootprint->EmbedFonts();
120 else
121 aFootprint->GetEmbeddedFiles()->ClearEmbeddedFonts();
122
123 WX_FILENAME fn = it->second->GetFileName();
124
125 wxString tempFileName =
126#ifdef USE_TMP_FILE
127 wxFileName::CreateTempFileName( fn.GetPath() );
128#else
129 fn.GetFullPath();
130#endif
131 // Allow file output stream to go out of scope to close the file stream before
132 // renaming the file.
133 {
134 wxLogTrace( traceKicadPcbPlugin, wxT( "Creating temporary library file '%s'." ),
135 tempFileName );
136
137 PRETTIFIED_FILE_OUTPUTFORMATTER formatter( tempFileName );
138
139 m_owner->SetOutputFormatter( &formatter );
140 m_owner->Format( (BOARD_ITEM*) it->second->GetFootprint() );
141 }
142
143#ifdef USE_TMP_FILE
144 wxRemove( fn.GetFullPath() ); // it is not an error if this does not exist
145
146 // Even on Linux you can see an _intermittent_ error when calling wxRename(),
147 // and it is fully inexplicable. See if this dodges the error.
148 wxMilliSleep( 250L );
149
150 // Preserve the permissions of the current file
152
153 if( !wxRenameFile( tempFileName, fn.GetFullPath() ) )
154 {
155 wxString msg = wxString::Format( _( "Cannot rename temporary file '%s' to '%s'" ),
156 tempFileName,
157 fn.GetFullPath() );
158 THROW_IO_ERROR( msg );
159 }
160#endif
162 }
163
164 m_cache_timestamp += m_lib_path.GetModificationTime().GetValue().GetValue();
165
166 // If we've saved the full cache, we clear the dirty flag.
167 if( !aFootprint )
168 m_cache_dirty = false;
169}
170
171
173{
174 m_cache_dirty = false;
176
177 wxDir dir( m_lib_raw_path );
178
179 if( !dir.IsOpened() )
180 {
181 wxString msg = wxString::Format( _( "Footprint library '%s' not found." ),
183 THROW_IO_ERROR( msg );
184 }
185
186 wxString fullName;
187 wxString fileSpec = wxT( "*." ) + wxString( FILEEXT::KiCadFootprintFileExtension );
188
189 // wxFileName construction is egregiously slow. Construct it once and just swap out
190 // the filename thereafter.
191 WX_FILENAME fn( m_lib_raw_path, wxT( "dummyName" ) );
192
193 if( dir.GetFirst( &fullName, fileSpec ) )
194 {
195 wxString cacheError;
196
197 do
198 {
199 fn.SetFullName( fullName );
200
201 // Queue I/O errors so only files that fail to parse don't get loaded.
202 try
203 {
204 FILE_LINE_READER reader( fn.GetFullPath() );
205 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
206
207 FOOTPRINT* footprint = dynamic_cast<FOOTPRINT*>( parser.Parse() );
208 wxString fpName = fn.GetName();
209
210 if( !footprint )
211 THROW_IO_ERROR( wxEmptyString ); // caught locally, just below...
212
213 footprint->SetFPID( LIB_ID( wxEmptyString, fpName ) );
214 m_footprints.insert( fpName, new FP_CACHE_ITEM( footprint, fn ) );
215 }
216 catch( const IO_ERROR& ioe )
217 {
218 if( !cacheError.IsEmpty() )
219 cacheError += wxT( "\n\n" );
220
221 cacheError += wxString::Format( _( "Unable to read file '%s'" ) + '\n',
222 fn.GetFullPath() );
223 cacheError += ioe.What();
224 }
225 } while( dir.GetNext( &fullName ) );
226
228
229 if( !cacheError.IsEmpty() )
230 THROW_IO_ERROR( cacheError );
231 }
232}
233
234
235void FP_CACHE::Remove( const wxString& aFootprintName )
236{
237 FP_CACHE_FOOTPRINT_MAP::const_iterator it = m_footprints.find( aFootprintName );
238
239 if( it == m_footprints.end() )
240 {
241 wxString msg = wxString::Format( _( "Library '%s' has no footprint '%s'." ),
243 aFootprintName );
244 THROW_IO_ERROR( msg );
245 }
246
247 // Remove the footprint from the cache and delete the footprint file from the library.
248 wxString fullPath = it->second->GetFileName().GetFullPath();
249 m_footprints.erase( aFootprintName );
250 wxRemoveFile( fullPath );
251}
252
253
254bool FP_CACHE::IsPath( const wxString& aPath ) const
255{
256 return aPath == m_lib_raw_path;
257}
258
259
260void FP_CACHE::SetPath( const wxString& aPath )
261{
262 m_lib_raw_path = aPath;
263 m_lib_path.SetPath( aPath );
264
265
266 for( const auto& footprint : GetFootprints() )
267 {
268 footprint.second->SetFilePath( aPath );
269 }
270}
271
272
274{
276
277 return m_cache_dirty;
278}
279
280
281long long FP_CACHE::GetTimestamp( const wxString& aLibPath )
282{
283 wxString fileSpec = wxT( "*." ) + wxString( FILEEXT::KiCadFootprintFileExtension );
284
285 return TimestampDir( aLibPath, fileSpec );
286}
287
288
289bool PCB_IO_KICAD_SEXPR::CanReadBoard( const wxString& aFileName ) const
290{
291 if( !PCB_IO::CanReadBoard( aFileName ) )
292 return false;
293
294 try
295 {
296 FILE_LINE_READER reader( aFileName );
297 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, m_queryUserCallback );
298
299 return parser.IsValidBoardHeader();
300 }
301 catch( const IO_ERROR& )
302 {
303 }
304
305 return false;
306}
307
308
309void PCB_IO_KICAD_SEXPR::SaveBoard( const wxString& aFileName, BOARD* aBoard,
310 const std::map<std::string, UTF8>* aProperties )
311{
312 LOCALE_IO toggle; // toggles on, then off, the C locale.
313
314 wxString sanityResult = aBoard->GroupsSanityCheck();
315
316 if( sanityResult != wxEmptyString && m_queryUserCallback )
317 {
319 _( "Internal Group Data Error" ), wxICON_ERROR,
320 wxString::Format( _( "Please report this bug. Error validating group "
321 "structure: %s\n\nSave anyway?" ), sanityResult ),
322 _( "Save Anyway" ) ) )
323 {
324 return;
325 }
326 }
327
328 init( aProperties );
329
330 m_board = aBoard; // after init()
331
332 // If the user wants fonts embedded, make sure that they are added to the board. Otherwise,
333 // remove any fonts that were previously embedded.
336 else
338
339 // Prepare net mapping that assures that net codes saved in a file are consecutive integers
340 m_mapping->SetBoard( aBoard );
341
342 PRETTIFIED_FILE_OUTPUTFORMATTER formatter( aFileName );
343
344 m_out = &formatter; // no ownership
345
346 m_out->Print( "(kicad_pcb (version %d) (generator \"pcbnew\") (generator_version %s)",
348 m_out->Quotew( GetMajorMinorVersion() ).c_str() );
349
350 Format( aBoard );
351
352 m_out->Print( ")" );
353 m_out->Finish();
354
355 m_out = nullptr;
356}
357
358
359BOARD_ITEM* PCB_IO_KICAD_SEXPR::Parse( const wxString& aClipboardSourceInput )
360{
361 std::string input = TO_UTF8( aClipboardSourceInput );
362
363 STRING_LINE_READER reader( input, wxT( "clipboard" ) );
364 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, m_queryUserCallback );
365
366 try
367 {
368 return parser.Parse();
369 }
370 catch( const PARSE_ERROR& parse_error )
371 {
372 if( parser.IsTooRecent() )
373 throw FUTURE_FORMAT_ERROR( parse_error, parser.GetRequiredVersion() );
374 else
375 throw;
376 }
377}
378
379
380void PCB_IO_KICAD_SEXPR::Format( const BOARD_ITEM* aItem ) const
381{
382 LOCALE_IO toggle; // public API function, perform anything convenient for caller
383
384 switch( aItem->Type() )
385 {
386 case PCB_T:
387 format( static_cast<const BOARD*>( aItem ) );
388 break;
389
391 case PCB_DIM_CENTER_T:
392 case PCB_DIM_RADIAL_T:
394 case PCB_DIM_LEADER_T:
395 format( static_cast<const PCB_DIMENSION_BASE*>( aItem ) );
396 break;
397
398 case PCB_SHAPE_T:
399 format( static_cast<const PCB_SHAPE*>( aItem ) );
400 break;
401
403 format( static_cast<const PCB_REFERENCE_IMAGE*>( aItem ) );
404 break;
405
406 case PCB_TARGET_T:
407 format( static_cast<const PCB_TARGET*>( aItem ) );
408 break;
409
410 case PCB_FOOTPRINT_T:
411 format( static_cast<const FOOTPRINT*>( aItem ) );
412 break;
413
414 case PCB_PAD_T:
415 format( static_cast<const PAD*>( aItem ) );
416 break;
417
418 case PCB_FIELD_T:
419 // Handled in the footprint formatter when properties are formatted
420 break;
421
422 case PCB_TEXT_T:
423 format( static_cast<const PCB_TEXT*>( aItem ) );
424 break;
425
426 case PCB_TEXTBOX_T:
427 format( static_cast<const PCB_TEXTBOX*>( aItem ) );
428 break;
429
430 case PCB_TABLE_T:
431 format( static_cast<const PCB_TABLE*>( aItem ) );
432 break;
433
434 case PCB_GROUP_T:
435 format( static_cast<const PCB_GROUP*>( aItem ) );
436 break;
437
438 case PCB_GENERATOR_T:
439 format( static_cast<const PCB_GENERATOR*>( aItem ) );
440 break;
441
442 case PCB_TRACE_T:
443 case PCB_ARC_T:
444 case PCB_VIA_T:
445 format( static_cast<const PCB_TRACK*>( aItem ) );
446 break;
447
448 case PCB_ZONE_T:
449 format( static_cast<const ZONE*>( aItem ) );
450 break;
451
452 default:
453 wxFAIL_MSG( wxT( "Cannot format item " ) + aItem->GetClass() );
454 }
455}
456
457
458std::string formatInternalUnits( int aValue )
459{
461}
462
463
464std::string formatInternalUnits( const VECTOR2I& aCoord )
465{
467}
468
469
470std::string formatInternalUnits( const VECTOR2I& aCoord, const FOOTPRINT* aParentFP )
471{
472 if( aParentFP )
473 {
474 VECTOR2I coord = aCoord - aParentFP->GetPosition();
475 RotatePoint( coord, -aParentFP->GetOrientation() );
476 return formatInternalUnits( coord );
477 }
478
479 return formatInternalUnits( aCoord );
480}
481
482
483void PCB_IO_KICAD_SEXPR::formatLayer( PCB_LAYER_ID aLayer, bool aIsKnockout ) const
484{
485 m_out->Print( "(layer %s %s)",
486 m_out->Quotew( LSET::Name( aLayer ) ).c_str(),
487 aIsKnockout ? "knockout" : "" );
488}
489
490
492 const FOOTPRINT* aParentFP ) const
493{
494 m_out->Print( "(pts" );
495
496 for( int ii = 0; ii < outline.PointCount(); ++ii )
497 {
498 int ind = outline.ArcIndex( ii );
499
500 if( ind < 0 )
501 {
502 m_out->Print( "(xy %s)",
503 formatInternalUnits( outline.CPoint( ii ), aParentFP ).c_str() );
504 }
505 else
506 {
507 const SHAPE_ARC& arc = outline.Arc( ind );
508 m_out->Print( "(arc (start %s) (mid %s) (end %s))",
509 formatInternalUnits( arc.GetP0(), aParentFP ).c_str(),
510 formatInternalUnits( arc.GetArcMid(), aParentFP ).c_str(),
511 formatInternalUnits( arc.GetP1(), aParentFP ).c_str() );
512
513 do
514 {
515 ++ii;
516 } while( ii < outline.PointCount() && outline.ArcIndex( ii ) == ind );
517
518 --ii;
519 }
520 }
521
522 m_out->Print( ")" );
523}
524
525
527{
528 wxString resolvedText( aText->GetShownText( true ) );
529 std::vector<std::unique_ptr<KIFONT::GLYPH>>* cache = aText->GetRenderCache( aText->GetFont(),
530 resolvedText );
531
532 m_out->Print( "(render_cache %s %s",
533 m_out->Quotew( resolvedText ).c_str(),
534 EDA_UNIT_UTILS::FormatAngle( aText->GetDrawRotation() ).c_str() );
535
537
538 CALLBACK_GAL callback_gal( empty_opts,
539 // Polygon callback
540 [&]( const SHAPE_LINE_CHAIN& aPoly )
541 {
542 m_out->Print( "(polygon" );
543 formatPolyPts( aPoly );
544 m_out->Print( ")" );
545 } );
546
547 callback_gal.SetLineWidth( aText->GetTextThickness() );
548 callback_gal.DrawGlyphs( *cache );
549
550 m_out->Print( ")" );
551}
552
553
554void PCB_IO_KICAD_SEXPR::formatSetup( const BOARD* aBoard ) const
555{
556 // Setup
557 m_out->Print( "(setup" );
558
559 // Save the board physical stackup structure
560 const BOARD_STACKUP& stackup = aBoard->GetDesignSettings().GetStackupDescriptor();
561
562 if( aBoard->GetDesignSettings().m_HasStackup )
563 stackup.FormatBoardStackup( m_out, aBoard );
564
565 BOARD_DESIGN_SETTINGS& dsnSettings = aBoard->GetDesignSettings();
566
567 m_out->Print( "(pad_to_mask_clearance %s)",
568 formatInternalUnits( dsnSettings.m_SolderMaskExpansion ).c_str() );
569
570 if( dsnSettings.m_SolderMaskMinWidth )
571 {
572 m_out->Print( "(solder_mask_min_width %s)",
573 formatInternalUnits( dsnSettings.m_SolderMaskMinWidth ).c_str() );
574 }
575
576 if( dsnSettings.m_SolderPasteMargin != 0 )
577 {
578 m_out->Print( "(pad_to_paste_clearance %s)",
579 formatInternalUnits( dsnSettings.m_SolderPasteMargin ).c_str() );
580 }
581
582 if( dsnSettings.m_SolderPasteMarginRatio != 0 )
583 {
584 m_out->Print( "(pad_to_paste_clearance_ratio %s)",
585 FormatDouble2Str( dsnSettings.m_SolderPasteMarginRatio ).c_str() );
586 }
587
588 KICAD_FORMAT::FormatBool( m_out, "allow_soldermask_bridges_in_footprints",
589 dsnSettings.m_AllowSoldermaskBridgesInFPs );
590
591 if( dsnSettings.m_TentViasFront || dsnSettings.m_TentViasBack )
592 {
593 m_out->Print( "(tenting %s %s)",
594 dsnSettings.m_TentViasFront ? "front" : "",
595 dsnSettings.m_TentViasBack ? "back" : "" );
596 }
597 else
598 {
599 m_out->Print( "(tenting none)" );
600 }
601
602 VECTOR2I origin = dsnSettings.GetAuxOrigin();
603
604 if( origin != VECTOR2I( 0, 0 ) )
605 {
606 m_out->Print( "(aux_axis_origin %s %s)",
607 formatInternalUnits( origin.x ).c_str(),
608 formatInternalUnits( origin.y ).c_str() );
609 }
610
611 origin = dsnSettings.GetGridOrigin();
612
613 if( origin != VECTOR2I( 0, 0 ) )
614 {
615 m_out->Print( "(grid_origin %s %s)",
616 formatInternalUnits( origin.x ).c_str(),
617 formatInternalUnits( origin.y ).c_str() );
618 }
619
620 aBoard->GetPlotOptions().Format( m_out );
621
622 m_out->Print( ")" );
623}
624
625
626void PCB_IO_KICAD_SEXPR::formatGeneral( const BOARD* aBoard ) const
627{
628 const BOARD_DESIGN_SETTINGS& dsnSettings = aBoard->GetDesignSettings();
629
630 m_out->Print( "(general" );
631
632 m_out->Print( "(thickness %s)",
633 formatInternalUnits( dsnSettings.GetBoardThickness() ).c_str() );
634
635 KICAD_FORMAT::FormatBool( m_out, "legacy_teardrops", aBoard->LegacyTeardrops() );
636
637 m_out->Print( ")" );
638
639 aBoard->GetPageSettings().Format( m_out );
640 aBoard->GetTitleBlock().Format( m_out );
641}
642
643
645{
646 m_out->Print( "(layers" );
647
648 // Save only the used copper layers from front to back.
649
650 for( PCB_LAYER_ID layer : aBoard->GetEnabledLayers().CuStack() )
651 {
652 m_out->Print( "(%d %s %s %s)",
653 layer,
654 m_out->Quotew( LSET::Name( layer ) ).c_str(),
655 LAYER::ShowType( aBoard->GetLayerType( layer ) ),
656 LSET::Name( layer ) == m_board->GetLayerName( layer )
657 ? ""
658 : m_out->Quotew( m_board->GetLayerName( layer ) ).c_str() );
659
660 }
661
662 // Save used non-copper layers in the order they are defined.
663 LSEQ seq = aBoard->GetEnabledLayers().TechAndUserUIOrder();
664
665 for( PCB_LAYER_ID layer : seq )
666 {
667 m_out->Print( "(%d %s %s %s)",
668 layer,
669 m_out->Quotew( LSET::Name( layer ) ).c_str(),
670 layer >= User_1 && layer <= User_9
671 ? LAYER::ShowType( aBoard->GetLayerType( layer ) )
672 : "user",
673 m_board->GetLayerName( layer ) == LSET::Name( layer )
674 ? ""
675 : m_out->Quotew( m_board->GetLayerName( layer ) ).c_str() );
676 }
677
678 m_out->Print( ")" );
679}
680
681
683{
684 for( NETINFO_ITEM* net : *m_mapping )
685 {
686 if( net == nullptr ) // Skip not actually existing nets (orphan nets)
687 continue;
688
689 m_out->Print( "(net %d %s)",
690 m_mapping->Translate( net->GetNetCode() ),
691 m_out->Quotew( net->GetNetname() ).c_str() );
692 }
693}
694
695
697{
698 for( const std::pair<const wxString, wxString>& prop : aBoard->GetProperties() )
699 {
700 m_out->Print( "(property %s %s)",
701 m_out->Quotew( prop.first ).c_str(),
702 m_out->Quotew( prop.second ).c_str() );
703 }
704}
705
706
707void PCB_IO_KICAD_SEXPR::formatHeader( const BOARD* aBoard ) const
708{
709 formatGeneral( aBoard );
710
711 // Layers list.
712 formatBoardLayers( aBoard );
713
714 // Setup
715 formatSetup( aBoard );
716
717 // Properties
718 formatProperties( aBoard );
719
720 // Save net codes and names
721 formatNetInformation( aBoard );
722}
723
724
726{
727 static const TEARDROP_PARAMETERS defaults;
728
729 return tdParams.m_Enabled == defaults.m_Enabled
730 && tdParams.m_BestLengthRatio == defaults.m_BestLengthRatio
731 && tdParams.m_TdMaxLen == defaults.m_TdMaxLen
732 && tdParams.m_BestWidthRatio == defaults.m_BestWidthRatio
733 && tdParams.m_TdMaxWidth == defaults.m_TdMaxWidth
734 && tdParams.m_CurvedEdges == defaults.m_CurvedEdges
736 && tdParams.m_AllowUseTwoTracks == defaults.m_AllowUseTwoTracks
737 && tdParams.m_TdOnPadsInZones == defaults.m_TdOnPadsInZones;
738}
739
740
742{
743 m_out->Print( "(teardrops (best_length_ratio %s) (max_length %s) (best_width_ratio %s) "
744 "(max_width %s)",
745 FormatDouble2Str( tdParams.m_BestLengthRatio ).c_str(),
746 formatInternalUnits( tdParams.m_TdMaxLen ).c_str(),
747 FormatDouble2Str( tdParams.m_BestWidthRatio ).c_str(),
748 formatInternalUnits( tdParams.m_TdMaxWidth ).c_str() );
749
750 KICAD_FORMAT::FormatBool( m_out, "curved_edges", tdParams.m_CurvedEdges );
751
752 m_out->Print( "filter_ratio %s )",
753 FormatDouble2Str( tdParams.m_WidthtoSizeFilterRatio ).c_str() );
754
755 KICAD_FORMAT::FormatBool( m_out, "enabled", tdParams.m_Enabled );
756 KICAD_FORMAT::FormatBool( m_out, "allow_two_segments", tdParams.m_AllowUseTwoTracks );
757 KICAD_FORMAT::FormatBool( m_out, "prefer_zone_connections", !tdParams.m_TdOnPadsInZones );
758 m_out->Print( ")" );
759}
760
761
762void PCB_IO_KICAD_SEXPR::format( const BOARD* aBoard ) const
763{
764 std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_footprints( aBoard->Footprints().begin(),
765 aBoard->Footprints().end() );
766 std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_drawings( aBoard->Drawings().begin(),
767 aBoard->Drawings().end() );
768 std::set<PCB_TRACK*, PCB_TRACK::cmp_tracks> sorted_tracks( aBoard->Tracks().begin(),
769 aBoard->Tracks().end() );
770 std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_zones( aBoard->Zones().begin(),
771 aBoard->Zones().end() );
772 std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_groups( aBoard->Groups().begin(),
773 aBoard->Groups().end() );
774 std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_generators( aBoard->Generators().begin(),
775 aBoard->Generators().end() );
776 formatHeader( aBoard );
777
778 // Save the footprints.
779 for( BOARD_ITEM* footprint : sorted_footprints )
780 Format( footprint );
781
782 // Save the graphical items on the board (not owned by a footprint)
783 for( BOARD_ITEM* item : sorted_drawings )
784 Format( item );
785
786 // Do not save PCB_MARKERs, they can be regenerated easily.
787
788 // Save the tracks and vias.
789 for( PCB_TRACK* track : sorted_tracks )
790 Format( track );
791
792 // Save the polygon (which are the newer technology) zones.
793 for( auto zone : sorted_zones )
794 Format( zone );
795
796 // Save the groups
797 for( BOARD_ITEM* group : sorted_groups )
798 Format( group );
799
800 // Save the generators
801 for( BOARD_ITEM* gen : sorted_generators )
802 Format( gen );
803
804 // Save any embedded files
805 // Consolidate the embedded models in footprints into a single map
806 // to avoid duplicating the same model in the board file.
807 EMBEDDED_FILES files_to_write;
808
809 for( auto& file : aBoard->GetEmbeddedFiles()->EmbeddedFileMap() )
810 files_to_write.AddFile( file.second );
811
812 for( BOARD_ITEM* item : sorted_footprints )
813 {
814 FOOTPRINT* fp = static_cast<FOOTPRINT*>( item );
815
816 for( auto& file : fp->GetEmbeddedFiles()->EmbeddedFileMap() )
817 files_to_write.AddFile( file.second );
818 }
819
820 m_out->Print( "(embedded_fonts %s)",
821 aBoard->GetEmbeddedFiles()->GetAreFontsEmbedded() ? "yes" : "no" );
822
823 if( !files_to_write.IsEmpty() )
824 files_to_write.WriteEmbeddedFiles( *m_out, ( m_ctl & CTL_FOR_BOARD ) );
825
826 // Remove the files so that they are not freed in the DTOR
827 files_to_write.ClearEmbeddedFiles( false );
828}
829
830
831void PCB_IO_KICAD_SEXPR::format( const PCB_DIMENSION_BASE* aDimension ) const
832{
833 const PCB_DIM_ALIGNED* aligned = dynamic_cast<const PCB_DIM_ALIGNED*>( aDimension );
834 const PCB_DIM_ORTHOGONAL* ortho = dynamic_cast<const PCB_DIM_ORTHOGONAL*>( aDimension );
835 const PCB_DIM_CENTER* center = dynamic_cast<const PCB_DIM_CENTER*>( aDimension );
836 const PCB_DIM_RADIAL* radial = dynamic_cast<const PCB_DIM_RADIAL*>( aDimension );
837 const PCB_DIM_LEADER* leader = dynamic_cast<const PCB_DIM_LEADER*>( aDimension );
838
839 m_out->Print( "(dimension" );
840
841 if( ortho ) // must be tested before aligned, because ortho is derived from aligned
842 // and aligned is not null
843 m_out->Print( "(type orthogonal)" );
844 else if( aligned )
845 m_out->Print( "(type aligned)" );
846 else if( leader )
847 m_out->Print( "(type leader)" );
848 else if( center )
849 m_out->Print( "(type center)" );
850 else if( radial )
851 m_out->Print( "(type radial)" );
852 else
853 wxFAIL_MSG( wxT( "Cannot format unknown dimension type!" ) );
854
855 if( aDimension->IsLocked() )
856 KICAD_FORMAT::FormatBool( m_out, "locked", aDimension->IsLocked() );
857
858 formatLayer( aDimension->GetLayer() );
859
860 KICAD_FORMAT::FormatUuid( m_out, aDimension->m_Uuid );
861
862 m_out->Print( "(pts (xy %s %s) (xy %s %s))",
863 formatInternalUnits( aDimension->GetStart().x ).c_str(),
864 formatInternalUnits( aDimension->GetStart().y ).c_str(),
865 formatInternalUnits( aDimension->GetEnd().x ).c_str(),
866 formatInternalUnits( aDimension->GetEnd().y ).c_str() );
867
868 if( aligned )
869 m_out->Print( "(height %s)", formatInternalUnits( aligned->GetHeight() ).c_str() );
870
871 if( radial )
872 {
873 m_out->Print( "(leader_length %s)",
874 formatInternalUnits( radial->GetLeaderLength() ).c_str() );
875 }
876
877 if( ortho )
878 m_out->Print( "(orientation %d)", static_cast<int>( ortho->GetOrientation() ) );
879
880 if( !center )
881 {
882 m_out->Print( "(format (prefix %s) (suffix %s) (units %d) (units_format %d) (precision %d)",
883 m_out->Quotew( aDimension->GetPrefix() ).c_str(),
884 m_out->Quotew( aDimension->GetSuffix() ).c_str(),
885 static_cast<int>( aDimension->GetUnitsMode() ),
886 static_cast<int>( aDimension->GetUnitsFormat() ),
887 static_cast<int>( aDimension->GetPrecision() ) );
888
889 if( aDimension->GetOverrideTextEnabled() )
890 {
891 m_out->Print( "(override_value %s)",
892 m_out->Quotew( aDimension->GetOverrideText() ).c_str() );
893 }
894
895 if( aDimension->GetSuppressZeroes() )
896 KICAD_FORMAT::FormatBool( m_out, "suppress_zeroes", true );
897
898 m_out->Print( ")" );
899 }
900
901 m_out->Print( "(style (thickness %s) (arrow_length %s) (text_position_mode %d)",
902 formatInternalUnits( aDimension->GetLineThickness() ).c_str(),
903 formatInternalUnits( aDimension->GetArrowLength() ).c_str(),
904 static_cast<int>( aDimension->GetTextPositionMode() ) );
905
906 if( ortho || aligned )
907 {
908 switch( aDimension->GetArrowDirection() )
909 {
910 case DIM_ARROW_DIRECTION::OUTWARD:
911 m_out->Print( "(arrow_direction outward)" );
912 break;
913 case DIM_ARROW_DIRECTION::INWARD:
914 m_out->Print( "(arrow_direction inward)" );
915 break;
916 // No default, handle all cases
917 }
918 }
919
920 if( aligned )
921 {
922 m_out->Print( "(extension_height %s)",
923 formatInternalUnits( aligned->GetExtensionHeight() ).c_str() );
924 }
925
926 if( leader )
927 m_out->Print( "(text_frame %d)", static_cast<int>( leader->GetTextBorder() ) );
928
929 m_out->Print( "(extension_offset %s)",
930 formatInternalUnits( aDimension->GetExtensionOffset() ).c_str() );
931
932 if( aDimension->GetKeepTextAligned() )
933 KICAD_FORMAT::FormatBool( m_out, "keep_text_aligned", true );
934
935 m_out->Print( ")" );
936
937 // Write dimension text after all other options to be sure the
938 // text options are known when reading the file
939 if( !center )
940 format( static_cast<const PCB_TEXT*>( aDimension ) );
941
942 m_out->Print( ")" );
943}
944
945
946void PCB_IO_KICAD_SEXPR::format( const PCB_SHAPE* aShape ) const
947{
948 FOOTPRINT* parentFP = aShape->GetParentFootprint();
949 std::string prefix = parentFP ? "fp" : "gr";
950
951 switch( aShape->GetShape() )
952 {
953 case SHAPE_T::SEGMENT:
954 m_out->Print( "(%s_line (start %s) (end %s)",
955 prefix.c_str(),
956 formatInternalUnits( aShape->GetStart(), parentFP ).c_str(),
957 formatInternalUnits( aShape->GetEnd(), parentFP ).c_str() );
958 break;
959
960 case SHAPE_T::RECTANGLE:
961 m_out->Print( "(%s_rect (start %s) (end %s)",
962 prefix.c_str(),
963 formatInternalUnits( aShape->GetStart(), parentFP ).c_str(),
964 formatInternalUnits( aShape->GetEnd(), parentFP ).c_str() );
965 break;
966
967 case SHAPE_T::CIRCLE:
968 m_out->Print( "(%s_circle (center %s) (end %s)",
969 prefix.c_str(),
970 formatInternalUnits( aShape->GetStart(), parentFP ).c_str(),
971 formatInternalUnits( aShape->GetEnd(), parentFP ).c_str() );
972 break;
973
974 case SHAPE_T::ARC:
975 m_out->Print( "(%s_arc (start %s) (mid %s) (end %s)",
976 prefix.c_str(),
977 formatInternalUnits( aShape->GetStart(), parentFP ).c_str(),
978 formatInternalUnits( aShape->GetArcMid(), parentFP ).c_str(),
979 formatInternalUnits( aShape->GetEnd(), parentFP ).c_str() );
980 break;
981
982 case SHAPE_T::POLY:
983 if( aShape->IsPolyShapeValid() )
984 {
985 const SHAPE_POLY_SET& poly = aShape->GetPolyShape();
986 const SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
987
988 m_out->Print( "(%s_poly", prefix.c_str() );
989 formatPolyPts( outline, parentFP );
990 }
991 else
992 {
993 wxFAIL_MSG( wxT( "Cannot format invalid polygon." ) );
994 return;
995 }
996
997 break;
998
999 case SHAPE_T::BEZIER:
1000 m_out->Print( "(%s_curve (pts (xy %s) (xy %s) (xy %s) (xy %s))",
1001 prefix.c_str(),
1002 formatInternalUnits( aShape->GetStart(), parentFP ).c_str(),
1003 formatInternalUnits( aShape->GetBezierC1(), parentFP ).c_str(),
1004 formatInternalUnits( aShape->GetBezierC2(), parentFP ).c_str(),
1005 formatInternalUnits( aShape->GetEnd(), parentFP ).c_str() );
1006 break;
1007
1008 default:
1010 return;
1011 };
1012
1013 aShape->GetStroke().Format( m_out, pcbIUScale );
1014
1015 // The filled flag represents if a solid fill is present on circles, rectangles and polygons
1016 if( ( aShape->GetShape() == SHAPE_T::POLY )
1017 || ( aShape->GetShape() == SHAPE_T::RECTANGLE )
1018 || ( aShape->GetShape() == SHAPE_T::CIRCLE ) )
1019 {
1020 KICAD_FORMAT::FormatBool( m_out, "fill", aShape->IsFilled() );
1021 }
1022
1023 if( aShape->IsLocked() )
1024 KICAD_FORMAT::FormatBool( m_out, "locked", true );
1025
1026 if( aShape->GetLayerSet().count() > 1 )
1027 formatLayers( aShape->GetLayerSet() );
1028 else
1029 formatLayer( aShape->GetLayer() );
1030
1031 if( aShape->HasSolderMask()
1032 && aShape->GetLocalSolderMaskMargin().has_value()
1033 && IsExternalCopperLayer( aShape->GetLayer() ) )
1034 {
1035 m_out->Print( "(solder_mask_margin %s)",
1036 formatInternalUnits( aShape->GetLocalSolderMaskMargin().value() ).c_str() );
1037 }
1038
1039 if( aShape->GetNetCode() > 0 )
1040 m_out->Print( "(net %d)", m_mapping->Translate( aShape->GetNetCode() ) );
1041
1043 m_out->Print( ")" );
1044}
1045
1046
1048{
1049 wxCHECK_RET( aBitmap != nullptr && m_out != nullptr, "" );
1050
1051 const REFERENCE_IMAGE& refImage = aBitmap->GetReferenceImage();
1052
1053 const wxImage* image = refImage.GetImage().GetImageData();
1054
1055 wxCHECK_RET( image != nullptr, "wxImage* is NULL" );
1056
1057 m_out->Print( "(image (at %s %s)",
1058 formatInternalUnits( aBitmap->GetPosition().x ).c_str(),
1059 formatInternalUnits( aBitmap->GetPosition().y ).c_str() );
1060
1061 formatLayer( aBitmap->GetLayer() );
1062
1063 if( refImage.GetImageScale() != 1.0 )
1064 m_out->Print( "(scale %g)", refImage.GetImageScale() );
1065
1066 if( aBitmap->IsLocked() )
1067 KICAD_FORMAT::FormatBool( m_out, "locked", true );
1068
1069 m_out->Print( "(data" );
1070
1071 wxString out = wxBase64Encode( refImage.GetImage().GetImageDataBuffer() );
1072
1073 // Apparently the MIME standard character width for base64 encoding is 76 (unconfirmed)
1074 // so use it in a vain attempt to be standard like.
1075#define MIME_BASE64_LENGTH 76
1076
1077 size_t first = 0;
1078
1079 while( first < out.Length() )
1080 {
1081 m_out->Print( "\n\"%s\"", TO_UTF8( out( first, MIME_BASE64_LENGTH ) ) );
1082 first += MIME_BASE64_LENGTH;
1083 }
1084
1085 m_out->Print( ")" ); // Closes data token.
1086
1088 m_out->Print( ")" ); // Closes image token.
1089}
1090
1091
1092void PCB_IO_KICAD_SEXPR::format( const PCB_TARGET* aTarget ) const
1093{
1094 m_out->Print( "(target %s (at %s) (size %s)",
1095 ( aTarget->GetShape() ) ? "x" : "plus",
1096 formatInternalUnits( aTarget->GetPosition() ).c_str(),
1097 formatInternalUnits( aTarget->GetSize() ).c_str() );
1098
1099 if( aTarget->GetWidth() != 0 )
1100 m_out->Print( "(width %s)", formatInternalUnits( aTarget->GetWidth() ).c_str() );
1101
1102 formatLayer( aTarget->GetLayer() );
1104 m_out->Print( ")" );
1105}
1106
1107
1108void PCB_IO_KICAD_SEXPR::format( const FOOTPRINT* aFootprint ) const
1109{
1110 if( !( m_ctl & CTL_OMIT_INITIAL_COMMENTS ) )
1111 {
1112 const wxArrayString* initial_comments = aFootprint->GetInitialComments();
1113
1114 if( initial_comments )
1115 {
1116 for( unsigned i = 0; i < initial_comments->GetCount(); ++i )
1117 m_out->Print( "%s\n", TO_UTF8( (*initial_comments)[i] ) );
1118 }
1119 }
1120
1121 if( m_ctl & CTL_OMIT_LIBNAME )
1122 {
1123 m_out->Print( "(footprint %s",
1124 m_out->Quotes( aFootprint->GetFPID().GetLibItemName() ).c_str() );
1125 }
1126 else
1127 {
1128 m_out->Print( "(footprint %s",
1129 m_out->Quotes( aFootprint->GetFPID().Format() ).c_str() );
1130 }
1131
1133 {
1134 m_out->Print( "(version %d) (generator \"pcbnew\") (generator_version %s)",
1136 m_out->Quotew( GetMajorMinorVersion() ).c_str() );
1137 }
1138
1139 if( aFootprint->IsLocked() )
1140 KICAD_FORMAT::FormatBool( m_out, "locked", true );
1141
1142 if( aFootprint->IsPlaced() )
1143 KICAD_FORMAT::FormatBool( m_out, "placed", true );
1144
1145 formatLayer( aFootprint->GetLayer() );
1146
1147 if( !( m_ctl & CTL_OMIT_UUIDS ) )
1148 KICAD_FORMAT::FormatUuid( m_out, aFootprint->m_Uuid );
1149
1150 if( !( m_ctl & CTL_OMIT_AT ) )
1151 {
1152 m_out->Print( "(at %s %s)",
1153 formatInternalUnits( aFootprint->GetPosition() ).c_str(),
1154 aFootprint->GetOrientation().IsZero()
1155 ? ""
1156 : EDA_UNIT_UTILS::FormatAngle( aFootprint->GetOrientation() ).c_str() );
1157 }
1158
1159 if( !aFootprint->GetLibDescription().IsEmpty() )
1160 m_out->Print( "(descr %s)", m_out->Quotew( aFootprint->GetLibDescription() ).c_str() );
1161
1162 if( !aFootprint->GetKeywords().IsEmpty() )
1163 m_out->Print( "(tags %s)", m_out->Quotew( aFootprint->GetKeywords() ).c_str() );
1164
1165 for( const PCB_FIELD* field : aFootprint->GetFields() )
1166 {
1167 m_out->Print( "(property %s %s",
1168 m_out->Quotew( field->GetCanonicalName() ).c_str(),
1169 m_out->Quotew( field->GetText() ).c_str() );
1170
1171 format( field );
1172
1173 m_out->Print( ")" );
1174 }
1175
1176 if( const COMPONENT_CLASS* compClass = aFootprint->GetComponentClass() )
1177 {
1178 if( !compClass->IsEmpty() )
1179 {
1180 m_out->Print( "(component_classes" );
1181
1182 for( const COMPONENT_CLASS* constituent : compClass->GetConstituentClasses() )
1183 m_out->Print( "(class %s)", m_out->Quotew( constituent->GetFullName() ).c_str() );
1184
1185 m_out->Print( ")" );
1186 }
1187 }
1188
1189 if( !aFootprint->GetFilters().empty() )
1190 {
1191 m_out->Print( "(property ki_fp_filters %s)",
1192 m_out->Quotew( aFootprint->GetFilters() ).c_str() );
1193 }
1194
1195 if( !( m_ctl & CTL_OMIT_PATH ) && !aFootprint->GetPath().empty() )
1196 m_out->Print( "(path %s)", m_out->Quotew( aFootprint->GetPath().AsString() ).c_str() );
1197
1198 if( !aFootprint->GetSheetname().empty() )
1199 m_out->Print( "(sheetname %s)", m_out->Quotew( aFootprint->GetSheetname() ).c_str() );
1200
1201 if( !aFootprint->GetSheetfile().empty() )
1202 m_out->Print( "(sheetfile %s)", m_out->Quotew( aFootprint->GetSheetfile() ).c_str() );
1203
1204 if( aFootprint->GetLocalSolderMaskMargin().has_value() )
1205 {
1206 m_out->Print( "(solder_mask_margin %s)",
1207 formatInternalUnits( aFootprint->GetLocalSolderMaskMargin().value() ).c_str() );
1208 }
1209
1210 if( aFootprint->GetLocalSolderPasteMargin().has_value() )
1211 {
1212 m_out->Print( "(solder_paste_margin %s)",
1213 formatInternalUnits( aFootprint->GetLocalSolderPasteMargin().value() ).c_str() );
1214 }
1215
1216 if( aFootprint->GetLocalSolderPasteMarginRatio().has_value() )
1217 {
1218 m_out->Print( "(solder_paste_margin_ratio %s)",
1219 FormatDouble2Str( aFootprint->GetLocalSolderPasteMarginRatio().value() ).c_str() );
1220 }
1221
1222 if( aFootprint->GetLocalClearance().has_value() )
1223 {
1224 m_out->Print( "(clearance %s)",
1225 formatInternalUnits( aFootprint->GetLocalClearance().value() ).c_str() );
1226 }
1227
1228 if( aFootprint->GetLocalZoneConnection() != ZONE_CONNECTION::INHERITED )
1229 {
1230 m_out->Print( "(zone_connect %d)",
1231 static_cast<int>( aFootprint->GetLocalZoneConnection() ) );
1232 }
1233
1234 // Attributes
1235 if( aFootprint->GetAttributes() )
1236 {
1237 m_out->Print( "(attr" );
1238
1239 if( aFootprint->GetAttributes() & FP_SMD )
1240 m_out->Print( " smd" );
1241
1242 if( aFootprint->GetAttributes() & FP_THROUGH_HOLE )
1243 m_out->Print( " through_hole" );
1244
1245 if( aFootprint->GetAttributes() & FP_BOARD_ONLY )
1246 m_out->Print( " board_only" );
1247
1248 if( aFootprint->GetAttributes() & FP_EXCLUDE_FROM_POS_FILES )
1249 m_out->Print( " exclude_from_pos_files" );
1250
1251 if( aFootprint->GetAttributes() & FP_EXCLUDE_FROM_BOM )
1252 m_out->Print( " exclude_from_bom" );
1253
1254 if( aFootprint->GetAttributes() & FP_ALLOW_MISSING_COURTYARD )
1255 m_out->Print( " allow_missing_courtyard" );
1256
1257 if( aFootprint->GetAttributes() & FP_DNP )
1258 m_out->Print( " dnp" );
1259
1260 if( aFootprint->GetAttributes() & FP_ALLOW_SOLDERMASK_BRIDGES )
1261 m_out->Print( " allow_soldermask_bridges" );
1262
1263 m_out->Print( ")" );
1264 }
1265
1266 if( aFootprint->GetPrivateLayers().any() )
1267 {
1268 m_out->Print( "(private_layers" );
1269
1270 for( PCB_LAYER_ID layer : aFootprint->GetPrivateLayers().Seq() )
1271 {
1272 wxString canonicalName( LSET::Name( layer ) );
1273 m_out->Print( " %s", m_out->Quotew( canonicalName ).c_str() );
1274 }
1275
1276 m_out->Print( ")" );
1277 }
1278
1279 if( aFootprint->IsNetTie() )
1280 {
1281 m_out->Print( "(net_tie_pad_groups" );
1282
1283 for( const wxString& group : aFootprint->GetNetTiePadGroups() )
1284 m_out->Print( " %s", m_out->Quotew( group ).c_str() );
1285
1286 m_out->Print( ")" );
1287 }
1288
1289 Format( (BOARD_ITEM*) &aFootprint->Reference() );
1290 Format( (BOARD_ITEM*) &aFootprint->Value() );
1291
1292 std::set<PAD*, FOOTPRINT::cmp_pads> sorted_pads( aFootprint->Pads().begin(),
1293 aFootprint->Pads().end() );
1294 std::set<BOARD_ITEM*, FOOTPRINT::cmp_drawings> sorted_drawings(
1295 aFootprint->GraphicalItems().begin(),
1296 aFootprint->GraphicalItems().end() );
1297 std::set<ZONE*, FOOTPRINT::cmp_zones> sorted_zones( aFootprint->Zones().begin(),
1298 aFootprint->Zones().end() );
1299 std::set<BOARD_ITEM*, PCB_GROUP::ptr_cmp> sorted_groups( aFootprint->Groups().begin(),
1300 aFootprint->Groups().end() );
1301
1302 // Save drawing elements.
1303
1304 for( BOARD_ITEM* gr : sorted_drawings )
1305 Format( gr );
1306
1307 // Save pads.
1308 for( PAD* pad : sorted_pads )
1309 Format( pad );
1310
1311 // Save zones.
1312 for( BOARD_ITEM* zone : sorted_zones )
1313 Format( zone );
1314
1315 // Save groups.
1316 for( BOARD_ITEM* group : sorted_groups )
1317 Format( group );
1318
1319 KICAD_FORMAT::FormatBool( m_out, "embedded_fonts",
1320 aFootprint->GetEmbeddedFiles()->GetAreFontsEmbedded() );
1321
1322 if( !aFootprint->GetEmbeddedFiles()->IsEmpty() )
1323 aFootprint->WriteEmbeddedFiles( *m_out, !( m_ctl & CTL_FOR_BOARD ) );
1324
1325 // Save 3D info.
1326 auto bs3D = aFootprint->Models().begin();
1327 auto es3D = aFootprint->Models().end();
1328
1329 while( bs3D != es3D )
1330 {
1331 if( !bs3D->m_Filename.IsEmpty() )
1332 {
1333 m_out->Print( "(model %s", m_out->Quotew( bs3D->m_Filename ).c_str() );
1334
1335 if( !bs3D->m_Show )
1336 KICAD_FORMAT::FormatBool( m_out, "hide", !bs3D->m_Show );
1337
1338 if( bs3D->m_Opacity != 1.0 )
1339 m_out->Print( "(opacity %0.4f)", bs3D->m_Opacity );
1340
1341 m_out->Print( "(offset (xyz %s %s %s))",
1342 FormatDouble2Str( bs3D->m_Offset.x ).c_str(),
1343 FormatDouble2Str( bs3D->m_Offset.y ).c_str(),
1344 FormatDouble2Str( bs3D->m_Offset.z ).c_str() );
1345
1346 m_out->Print( "(scale (xyz %s %s %s))",
1347 FormatDouble2Str( bs3D->m_Scale.x ).c_str(),
1348 FormatDouble2Str( bs3D->m_Scale.y ).c_str(),
1349 FormatDouble2Str( bs3D->m_Scale.z ).c_str() );
1350
1351 m_out->Print( "(rotate (xyz %s %s %s))",
1352 FormatDouble2Str( bs3D->m_Rotation.x ).c_str(),
1353 FormatDouble2Str( bs3D->m_Rotation.y ).c_str(),
1354 FormatDouble2Str( bs3D->m_Rotation.z ).c_str() );
1355
1356 m_out->Print( ")" );
1357 }
1358
1359 ++bs3D;
1360 }
1361
1362 m_out->Print( ")" );
1363}
1364
1365
1367{
1368 static const LSET cu_all( LSET::AllCuMask( m_board->GetCopperLayerCount() ) );
1369 static const LSET fr_bk( { B_Cu, F_Cu } );
1370 static const LSET adhes( { B_Adhes, F_Adhes } );
1371 static const LSET paste( { B_Paste, F_Paste } );
1372 static const LSET silks( { B_SilkS, F_SilkS } );
1373 static const LSET mask( { B_Mask, F_Mask } );
1374 static const LSET crt_yd( { B_CrtYd, F_CrtYd } );
1375 static const LSET fab( { B_Fab, F_Fab } );
1376
1377 LSET cu_mask = cu_all;
1378
1379 std::string output;
1380
1381 // output copper layers first, then non copper
1382
1383 if( ( aLayerMask & cu_mask ) == cu_mask )
1384 {
1385 output += ' ' + m_out->Quotew( "*.Cu" );
1386 aLayerMask &= ~cu_all; // clear bits, so they are not output again below
1387 }
1388 else if( ( aLayerMask & cu_mask ) == fr_bk )
1389 {
1390 output += ' ' + m_out->Quotew( "F&B.Cu" );
1391 aLayerMask &= ~fr_bk;
1392 }
1393
1394 if( ( aLayerMask & adhes ) == adhes )
1395 {
1396 output += ' ' + m_out->Quotew( "*.Adhes" );
1397 aLayerMask &= ~adhes;
1398 }
1399
1400 if( ( aLayerMask & paste ) == paste )
1401 {
1402 output += ' ' + m_out->Quotew( "*.Paste" );
1403 aLayerMask &= ~paste;
1404 }
1405
1406 if( ( aLayerMask & silks ) == silks )
1407 {
1408 output += ' ' + m_out->Quotew( "*.SilkS" );
1409 aLayerMask &= ~silks;
1410 }
1411
1412 if( ( aLayerMask & mask ) == mask )
1413 {
1414 output += ' ' + m_out->Quotew( "*.Mask" );
1415 aLayerMask &= ~mask;
1416 }
1417
1418 if( ( aLayerMask & crt_yd ) == crt_yd )
1419 {
1420 output += ' ' + m_out->Quotew( "*.CrtYd" );
1421 aLayerMask &= ~crt_yd;
1422 }
1423
1424 if( ( aLayerMask & fab ) == fab )
1425 {
1426 output += ' ' + m_out->Quotew( "*.Fab" );
1427 aLayerMask &= ~fab;
1428 }
1429
1430 // output any individual layers not handled in wildcard combos above
1431 wxString layerName;
1432
1433 for( int layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
1434 {
1435 if( aLayerMask[layer] )
1436 output += ' ' + m_out->Quotew( LSET::Name( PCB_LAYER_ID( layer ) ) );
1437 }
1438
1439 m_out->Print( "(layers %s)", output.c_str() );
1440}
1441
1442
1443void PCB_IO_KICAD_SEXPR::format( const PAD* aPad ) const
1444{
1445 const BOARD* board = aPad->GetBoard();
1446
1447 auto shapeName =
1448 [&]( PCB_LAYER_ID aLayer )
1449 {
1450 switch( aPad->GetShape( aLayer ) )
1451 {
1452 case PAD_SHAPE::CIRCLE: return "circle";
1453 case PAD_SHAPE::RECTANGLE: return "rect";
1454 case PAD_SHAPE::OVAL: return "oval";
1455 case PAD_SHAPE::TRAPEZOID: return "trapezoid";
1456 case PAD_SHAPE::CHAMFERED_RECT:
1457 case PAD_SHAPE::ROUNDRECT: return "roundrect";
1458 case PAD_SHAPE::CUSTOM: return "custom";
1459
1460 default:
1461 THROW_IO_ERROR( wxString::Format( _( "unknown pad type: %d"),
1462 aPad->GetShape( aLayer ) ) );
1463 }
1464 };
1465
1466 const char* type;
1467
1468 switch( aPad->GetAttribute() )
1469 {
1470 case PAD_ATTRIB::PTH: type = "thru_hole"; break;
1471 case PAD_ATTRIB::SMD: type = "smd"; break;
1472 case PAD_ATTRIB::CONN: type = "connect"; break;
1473 case PAD_ATTRIB::NPTH: type = "np_thru_hole"; break;
1474
1475 default:
1476 THROW_IO_ERROR( wxString::Format( wxT( "unknown pad attribute: %d" ),
1477 aPad->GetAttribute() ) );
1478 }
1479
1480 const char* property = nullptr;
1481
1482 switch( aPad->GetProperty() )
1483 {
1484 case PAD_PROP::NONE: break; // could be "none"
1485 case PAD_PROP::BGA: property = "pad_prop_bga"; break;
1486 case PAD_PROP::FIDUCIAL_GLBL: property = "pad_prop_fiducial_glob"; break;
1487 case PAD_PROP::FIDUCIAL_LOCAL: property = "pad_prop_fiducial_loc"; break;
1488 case PAD_PROP::TESTPOINT: property = "pad_prop_testpoint"; break;
1489 case PAD_PROP::HEATSINK: property = "pad_prop_heatsink"; break;
1490 case PAD_PROP::CASTELLATED: property = "pad_prop_castellated"; break;
1491 case PAD_PROP::MECHANICAL: property = "pad_prop_mechanical"; break;
1492
1493 default:
1494 THROW_IO_ERROR( wxString::Format( wxT( "unknown pad property: %d" ),
1495 aPad->GetProperty() ) );
1496 }
1497
1498 m_out->Print( "(pad %s %s %s",
1499 m_out->Quotew( aPad->GetNumber() ).c_str(),
1500 type,
1501 shapeName( PADSTACK::ALL_LAYERS ) );
1502
1503 m_out->Print( "(at %s %s)",
1504 formatInternalUnits( aPad->GetFPRelativePosition() ).c_str(),
1505 aPad->GetOrientation().IsZero()
1506 ? ""
1507 : EDA_UNIT_UTILS::FormatAngle( aPad->GetOrientation() ).c_str() );
1508
1509 m_out->Print( "(size %s)", formatInternalUnits( aPad->GetSize( PADSTACK::ALL_LAYERS ) ).c_str() );
1510
1511 if( aPad->GetDelta( PADSTACK::ALL_LAYERS ).x != 0
1512 || aPad->GetDelta( PADSTACK::ALL_LAYERS ).y != 0 )
1513 {
1514 m_out->Print( "(rect_delta %s)",
1516 }
1517
1518 VECTOR2I sz = aPad->GetDrillSize();
1519 VECTOR2I shapeoffset = aPad->GetOffset( PADSTACK::ALL_LAYERS );
1520
1521 if( (sz.x > 0) || (sz.y > 0) ||
1522 (shapeoffset.x != 0) || (shapeoffset.y != 0) )
1523 {
1524 m_out->Print( "(drill" );
1525
1526 if( aPad->GetDrillShape() == PAD_DRILL_SHAPE::OBLONG )
1527 m_out->Print( " oval" );
1528
1529 if( sz.x > 0 )
1530 m_out->Print( " %s", formatInternalUnits( sz.x ).c_str() );
1531
1532 if( sz.y > 0 && sz.x != sz.y )
1533 m_out->Print( " %s", formatInternalUnits( sz.y ).c_str() );
1534
1535 // NOTE: Shape offest is a property of the copper shape, not of the drill, but this was put
1536 // in the file format under the drill section. So, it is left here to minimize file format
1537 // changes, but note that the other padstack layers (if present) will have an offset stored
1538 // separately.
1539 if( shapeoffset.x != 0 || shapeoffset.y != 0 )
1540 {
1541 m_out->Print( "(offset %s)",
1543 }
1544
1545 m_out->Print( ")" );
1546 }
1547
1548 // Add pad property, if exists.
1549 if( property )
1550 m_out->Print( "(property %s)", property );
1551
1552 formatLayers( aPad->GetLayerSet() );
1553
1554 if( aPad->GetAttribute() == PAD_ATTRIB::PTH )
1555 {
1556 KICAD_FORMAT::FormatBool( m_out, "remove_unused_layers", aPad->GetRemoveUnconnected() );
1557
1558 if( aPad->GetRemoveUnconnected() )
1559 {
1560 KICAD_FORMAT::FormatBool( m_out, "keep_end_layers", aPad->GetKeepTopBottom() );
1561
1562 if( board ) // Will be nullptr in footprint library
1563 {
1564 m_out->Print( "(zone_layer_connections" );
1565
1566 for( PCB_LAYER_ID layer : board->GetEnabledLayers().CuStack() )
1567 {
1568 if( aPad->GetZoneLayerOverride( layer ) == ZLO_FORCE_FLASHED )
1569 m_out->Print( " %s", m_out->Quotew( LSET::Name( layer ) ).c_str() );
1570 }
1571
1572 m_out->Print( ")" );
1573 }
1574 }
1575 }
1576
1577 auto formatCornerProperties =
1578 [&]( PCB_LAYER_ID aLayer )
1579 {
1580 // Output the radius ratio for rounded and chamfered rect pads
1581 if( aPad->GetShape( aLayer ) == PAD_SHAPE::ROUNDRECT
1582 || aPad->GetShape( aLayer ) == PAD_SHAPE::CHAMFERED_RECT)
1583 {
1584 m_out->Print( "(roundrect_rratio %s)",
1585 FormatDouble2Str( aPad->GetRoundRectRadiusRatio( aLayer ) ).c_str() );
1586 }
1587
1588 // Output the chamfer corners for chamfered rect pads
1589 if( aPad->GetShape( aLayer ) == PAD_SHAPE::CHAMFERED_RECT)
1590 {
1591 m_out->Print( "(chamfer_ratio %s)",
1592 FormatDouble2Str( aPad->GetChamferRectRatio( aLayer ) ).c_str() );
1593
1594 m_out->Print( "(chamfer" );
1595
1596 if( ( aPad->GetChamferPositions( aLayer ) & RECT_CHAMFER_TOP_LEFT ) )
1597 m_out->Print( " top_left" );
1598
1599 if( ( aPad->GetChamferPositions( aLayer ) & RECT_CHAMFER_TOP_RIGHT ) )
1600 m_out->Print( " top_right" );
1601
1602 if( ( aPad->GetChamferPositions( aLayer ) & RECT_CHAMFER_BOTTOM_LEFT ) )
1603 m_out->Print( " bottom_left" );
1604
1605 if( ( aPad->GetChamferPositions( aLayer ) & RECT_CHAMFER_BOTTOM_RIGHT ) )
1606 m_out->Print( " bottom_right" );
1607
1608 m_out->Print( ")" );
1609 }
1610
1611 };
1612
1613 // For normal padstacks, this is the one and only set of properties. For complex ones, this
1614 // will represent the front layer properties, and other layers will be formatted below
1615 formatCornerProperties( PADSTACK::ALL_LAYERS );
1616
1617 // Unconnected pad is default net so don't save it.
1619 {
1620 m_out->Print( "(net %d %s)", m_mapping->Translate( aPad->GetNetCode() ),
1621 m_out->Quotew( aPad->GetNetname() ).c_str() );
1622 }
1623
1624 // Pin functions and types are closely related to nets, so if CTL_OMIT_NETS is set, omit
1625 // them as well (for instance when saved from library editor).
1626 if( !( m_ctl & CTL_OMIT_PAD_NETS ) )
1627 {
1628 if( !aPad->GetPinFunction().IsEmpty() )
1629 m_out->Print( "(pinfunction %s)", m_out->Quotew( aPad->GetPinFunction() ).c_str() );
1630
1631 if( !aPad->GetPinType().IsEmpty() )
1632 m_out->Print( "(pintype %s)", m_out->Quotew( aPad->GetPinType() ).c_str() );
1633 }
1634
1635 if( aPad->GetPadToDieLength() != 0 )
1636 {
1637 m_out->Print( "(die_length %s)",
1638 formatInternalUnits( aPad->GetPadToDieLength() ).c_str() );
1639 }
1640
1641 if( aPad->GetLocalSolderMaskMargin().has_value() )
1642 {
1643 m_out->Print( "(solder_mask_margin %s)",
1644 formatInternalUnits( aPad->GetLocalSolderMaskMargin().value() ).c_str() );
1645 }
1646
1647 if( aPad->GetLocalSolderPasteMargin().has_value() )
1648 {
1649 m_out->Print( "(solder_paste_margin %s)",
1650 formatInternalUnits( aPad->GetLocalSolderPasteMargin().value() ).c_str() );
1651 }
1652
1653 if( aPad->GetLocalSolderPasteMarginRatio().has_value() )
1654 {
1655 m_out->Print( "(solder_paste_margin_ratio %s)",
1656 FormatDouble2Str( aPad->GetLocalSolderPasteMarginRatio().value() ).c_str() );
1657 }
1658
1659 if( aPad->GetLocalClearance().has_value() )
1660 {
1661 m_out->Print( "(clearance %s)",
1662 formatInternalUnits( aPad->GetLocalClearance().value() ).c_str() );
1663 }
1664
1665 if( aPad->GetLocalZoneConnection() != ZONE_CONNECTION::INHERITED )
1666 {
1667 m_out->Print( "(zone_connect %d)",
1668 static_cast<int>( aPad->GetLocalZoneConnection() ) );
1669 }
1670
1671 if( aPad->GetLocalThermalSpokeWidthOverride().has_value() )
1672 {
1673 m_out->Print( "(thermal_bridge_width %s)",
1674 formatInternalUnits( aPad->GetLocalThermalSpokeWidthOverride().value() ).c_str() );
1675 }
1676
1677 EDA_ANGLE defaultThermalSpokeAngle = ANGLE_90;
1678
1679 if( aPad->GetShape( PADSTACK::ALL_LAYERS ) == PAD_SHAPE::CIRCLE ||
1680 ( aPad->GetShape( PADSTACK::ALL_LAYERS ) == PAD_SHAPE::CUSTOM
1681 && aPad->GetAnchorPadShape( PADSTACK::ALL_LAYERS ) == PAD_SHAPE::CIRCLE ) )
1682 {
1683 defaultThermalSpokeAngle = ANGLE_45;
1684 }
1685
1686 if( aPad->GetThermalSpokeAngle() != defaultThermalSpokeAngle )
1687 {
1688 m_out->Print( "(thermal_bridge_angle %s)",
1690 }
1691
1692 if( aPad->GetLocalThermalGapOverride().has_value() )
1693 {
1694 m_out->Print( "(thermal_gap %s)",
1695 formatInternalUnits( aPad->GetLocalThermalGapOverride().value() ).c_str() );
1696 }
1697
1698 auto anchorShape =
1699 [&]( PCB_LAYER_ID aLayer )
1700 {
1701 switch( aPad->GetAnchorPadShape( aLayer ) )
1702 {
1703 case PAD_SHAPE::RECTANGLE: return "rect";
1704 default:
1705 case PAD_SHAPE::CIRCLE: return "circle";
1706 }
1707 };
1708
1709 auto formatPrimitives =
1710 [&]( PCB_LAYER_ID aLayer )
1711 {
1712 m_out->Print( "(primitives" );
1713
1714 // Output all basic shapes
1715 for( const std::shared_ptr<PCB_SHAPE>& primitive : aPad->GetPrimitives( aLayer ) )
1716 {
1717 switch( primitive->GetShape() )
1718 {
1719 case SHAPE_T::SEGMENT:
1720 if( primitive->IsProxyItem() )
1721 {
1722 m_out->Print( "(gr_vector (start %s) (end %s)",
1723 formatInternalUnits( primitive->GetStart() ).c_str(),
1724 formatInternalUnits( primitive->GetEnd() ).c_str() );
1725 }
1726 else
1727 {
1728 m_out->Print( "(gr_line (start %s) (end %s)",
1729 formatInternalUnits( primitive->GetStart() ).c_str(),
1730 formatInternalUnits( primitive->GetEnd() ).c_str() );
1731 }
1732 break;
1733
1734 case SHAPE_T::RECTANGLE:
1735 if( primitive->IsProxyItem() )
1736 {
1737 m_out->Print( "(gr_bbox (start %s) (end %s)",
1738 formatInternalUnits( primitive->GetStart() ).c_str(),
1739 formatInternalUnits( primitive->GetEnd() ).c_str() );
1740 }
1741 else
1742 {
1743 m_out->Print( "(gr_rect (start %s) (end %s)",
1744 formatInternalUnits( primitive->GetStart() ).c_str(),
1745 formatInternalUnits( primitive->GetEnd() ).c_str() );
1746 }
1747 break;
1748
1749 case SHAPE_T::ARC:
1750 m_out->Print( "(gr_arc (start %s) (mid %s) (end %s)",
1751 formatInternalUnits( primitive->GetStart() ).c_str(),
1752 formatInternalUnits( primitive->GetArcMid() ).c_str(),
1753 formatInternalUnits( primitive->GetEnd() ).c_str() );
1754 break;
1755
1756 case SHAPE_T::CIRCLE:
1757 m_out->Print( "(gr_circle (center %s) (end %s)",
1758 formatInternalUnits( primitive->GetStart() ).c_str(),
1759 formatInternalUnits( primitive->GetEnd() ).c_str() );
1760 break;
1761
1762 case SHAPE_T::BEZIER:
1763 m_out->Print( "(gr_curve (pts (xy %s) (xy %s) (xy %s) (xy %s))",
1764 formatInternalUnits( primitive->GetStart() ).c_str(),
1765 formatInternalUnits( primitive->GetBezierC1() ).c_str(),
1766 formatInternalUnits( primitive->GetBezierC2() ).c_str(),
1767 formatInternalUnits( primitive->GetEnd() ).c_str() );
1768 break;
1769
1770 case SHAPE_T::POLY:
1771 if( primitive->IsPolyShapeValid() )
1772 {
1773 const SHAPE_POLY_SET& poly = primitive->GetPolyShape();
1774 const SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
1775
1776 m_out->Print( "(gr_poly" );
1777 formatPolyPts( outline );
1778 }
1779 break;
1780
1781 default:
1782 break;
1783 }
1784
1785 if( !primitive->IsProxyItem() )
1786 m_out->Print( "(width %s)", formatInternalUnits( primitive->GetWidth() ).c_str() );
1787
1788 // The filled flag represents if a solid fill is present on circles,
1789 // rectangles and polygons
1790 if( ( primitive->GetShape() == SHAPE_T::POLY )
1791 || ( primitive->GetShape() == SHAPE_T::RECTANGLE )
1792 || ( primitive->GetShape() == SHAPE_T::CIRCLE ) )
1793 {
1794 KICAD_FORMAT::FormatBool( m_out, "fill", primitive->IsFilled() );
1795 }
1796
1797 m_out->Print( ")" );
1798 }
1799
1800 m_out->Print( ")" ); // end of (primitives
1801 };
1802
1803 if( aPad->GetShape( PADSTACK::ALL_LAYERS ) == PAD_SHAPE::CUSTOM )
1804 {
1805 m_out->Print( "(options" );
1806
1808 m_out->Print( "(clearance convexhull)" );
1809 else
1810 m_out->Print( "(clearance outline)" );
1811
1812 // Output the anchor pad shape (circle/rect)
1813 m_out->Print( "(anchor %s)", anchorShape( PADSTACK::ALL_LAYERS ) );
1814
1815 m_out->Print( ")"); // end of (options ...
1816
1817 // Output graphic primitive of the pad shape
1818 formatPrimitives( PADSTACK::ALL_LAYERS );
1819 }
1820
1823
1824 formatTenting( aPad->Padstack() );
1825
1827
1828 // TODO: Refactor so that we call formatPadLayer( ALL_LAYERS ) above instead of redundant code
1829 auto formatPadLayer =
1830 [&]( PCB_LAYER_ID aLayer )
1831 {
1832 const PADSTACK& padstack = aPad->Padstack();
1833
1834 m_out->Print( "(shape %s)", shapeName( aLayer ) );
1835 m_out->Print( "(size %s)", formatInternalUnits( aPad->GetSize( aLayer ) ).c_str() );
1836
1837 const VECTOR2I& delta = aPad->GetDelta( aLayer );
1838
1839 if( delta.x != 0 || delta.y != 0 )
1840 m_out->Print( "(rect_delta %s)", formatInternalUnits( delta ).c_str() );
1841
1842 shapeoffset = aPad->GetOffset( aLayer );
1843
1844 if( shapeoffset.x != 0 || shapeoffset.y != 0 )
1845 m_out->Print( "(offset %s)", formatInternalUnits( shapeoffset ).c_str() );
1846
1847 formatCornerProperties( aLayer );
1848
1849 if( aPad->GetShape( aLayer ) == PAD_SHAPE::CUSTOM )
1850 {
1851 m_out->Print( "(options" );
1852
1853 // Output the anchor pad shape (circle/rect)
1854 m_out->Print( "(anchor %s)", anchorShape( aLayer ) );
1855
1856 m_out->Print( ")" ); // end of (options ...
1857
1858 // Output graphic primitive of the pad shape
1859 formatPrimitives( aLayer );
1860 }
1861
1862 EDA_ANGLE defaultLayerAngle = ANGLE_90;
1863
1864 if( aPad->GetShape( aLayer ) == PAD_SHAPE::CIRCLE ||
1865 ( aPad->GetShape( aLayer ) == PAD_SHAPE::CUSTOM
1866 && aPad->GetAnchorPadShape( aLayer ) == PAD_SHAPE::CIRCLE ) )
1867 {
1868 defaultLayerAngle = ANGLE_45;
1869 }
1870
1871 EDA_ANGLE layerSpokeAngle = padstack.ThermalSpokeAngle( aLayer );
1872
1873 if( layerSpokeAngle != defaultLayerAngle )
1874 {
1875 m_out->Print( "(thermal_bridge_angle %s)",
1876 EDA_UNIT_UTILS::FormatAngle( layerSpokeAngle ).c_str() );
1877 }
1878
1879 if( padstack.ThermalGap( aLayer ).has_value() )
1880 {
1881 m_out->Print( "(thermal_gap %s)",
1882 formatInternalUnits( *padstack.ThermalGap( aLayer ) ).c_str() );
1883 }
1884
1885 if( padstack.ThermalSpokeWidth( aLayer ).has_value() )
1886 {
1887 m_out->Print( "(thermal_bridge_width %s)",
1888 formatInternalUnits( *padstack.ThermalSpokeWidth( aLayer ) ).c_str() );
1889 }
1890
1891 if( padstack.Clearance( aLayer ).has_value() )
1892 {
1893 m_out->Print( "(clearance %s)",
1894 formatInternalUnits( *padstack.Clearance( aLayer ) ).c_str() );
1895 }
1896
1897 if( padstack.ZoneConnection( aLayer ).has_value() )
1898 {
1899 m_out->Print( "(zone_connect %d)",
1900 static_cast<int>( *padstack.ZoneConnection( aLayer ) ) );
1901 }
1902 };
1903
1904
1905 if( aPad->Padstack().Mode() != PADSTACK::MODE::NORMAL )
1906 {
1908 {
1909 m_out->Print( "(padstack (mode front_inner_back)" );
1910
1911 m_out->Print( "(layer \"Inner\"" );
1912 formatPadLayer( PADSTACK::INNER_LAYERS );
1913 m_out->Print( ")" );
1914 m_out->Print( "(layer \"B.Cu\"" );
1915 formatPadLayer( B_Cu );
1916 m_out->Print( ")" );
1917 }
1918 else
1919 {
1920 m_out->Print( "(padstack (mode custom)" );
1921
1922 int layerCount = board ? board->GetCopperLayerCount() : MAX_CU_LAYERS;
1923
1924 for( PCB_LAYER_ID layer : LAYER_RANGE( F_Cu, B_Cu, layerCount ) )
1925 {
1926 if( layer == F_Cu )
1927 continue;
1928
1929 m_out->Print( "(layer %s", m_out->Quotew( LSET::Name( layer ) ).c_str() );
1930 formatPadLayer( layer );
1931 m_out->Print( ")" );
1932 }
1933 }
1934
1935 m_out->Print( ")" );
1936 }
1937
1938 m_out->Print( ")" );
1939}
1940
1941
1942void PCB_IO_KICAD_SEXPR::formatTenting( const PADSTACK& aPadstack ) const
1943{
1944 std::optional<bool> front = aPadstack.FrontOuterLayers().has_solder_mask;
1945 std::optional<bool> back = aPadstack.BackOuterLayers().has_solder_mask;
1946
1947 if( front.has_value() || back.has_value() )
1948 {
1949 if( front.value_or( false ) || back.value_or( false ) )
1950 {
1951 m_out->Print( "(tenting %s %s)",
1952 front.value_or( false ) ? "front" : "",
1953 back.value_or( false ) ? "back" : "" );
1954 }
1955 else
1956 {
1957 m_out->Print( "(tenting none)" );
1958 }
1959 }
1960}
1961
1962
1963void PCB_IO_KICAD_SEXPR::format( const PCB_TEXT* aText ) const
1964{
1965 FOOTPRINT* parentFP = aText->GetParentFootprint();
1966 std::string prefix;
1967 std::string type;
1968 VECTOR2I pos = aText->GetTextPos();
1969 bool isField = dynamic_cast<const PCB_FIELD*>( aText ) != nullptr;
1970
1971 // Always format dimension text as gr_text
1972 if( dynamic_cast<const PCB_DIMENSION_BASE*>( aText ) )
1973 parentFP = nullptr;
1974
1975 if( parentFP )
1976 {
1977 prefix = "fp";
1978 type = "user";
1979
1980 pos -= parentFP->GetPosition();
1981 RotatePoint( pos, -parentFP->GetOrientation() );
1982 }
1983 else
1984 {
1985 prefix = "gr";
1986 }
1987
1988 if( !isField )
1989 {
1990 m_out->Print( "(%s_text %s %s",
1991 prefix.c_str(),
1992 type.c_str(),
1993 m_out->Quotew( aText->GetText() ).c_str() );
1994
1995 if( aText->IsLocked() )
1996 KICAD_FORMAT::FormatBool( m_out, "locked", true );
1997 }
1998
1999 m_out->Print( "(at %s %s)",
2000 formatInternalUnits( pos ).c_str(),
2001 EDA_UNIT_UTILS::FormatAngle( aText->GetTextAngle() ).c_str() );
2002
2003 if( parentFP && !aText->IsKeepUpright() )
2004 KICAD_FORMAT::FormatBool( m_out, "unlocked", true );
2005
2006 formatLayer( aText->GetLayer(), aText->IsKnockout() );
2007
2008 if( parentFP && !aText->IsVisible() )
2009 KICAD_FORMAT::FormatBool( m_out, "hide", true );
2010
2012
2013 int ctl_flags = m_ctl | CTL_OMIT_HIDE;
2014
2015 // Currently, texts have no specific color and no hyperlink.
2016 // so ensure they are never written in kicad_pcb file
2017 ctl_flags |= CTL_OMIT_COLOR | CTL_OMIT_HYPERLINK;
2018
2019 aText->EDA_TEXT::Format( m_out, ctl_flags );
2020
2021 if( aText->GetFont() && aText->GetFont()->IsOutline() )
2022 formatRenderCache( aText );
2023
2024 if( !isField )
2025 m_out->Print( ")" );
2026}
2027
2028
2029void PCB_IO_KICAD_SEXPR::format( const PCB_TEXTBOX* aTextBox ) const
2030{
2031 FOOTPRINT* parentFP = aTextBox->GetParentFootprint();
2032
2033 m_out->Print( "(%s %s",
2034 aTextBox->Type() == PCB_TABLECELL_T ? "table_cell"
2035 : parentFP ? "fp_text_box"
2036 : "gr_text_box",
2037 m_out->Quotew( aTextBox->GetText() ).c_str() );
2038
2039 if( aTextBox->IsLocked() )
2040 KICAD_FORMAT::FormatBool( m_out, "locked", true );
2041
2042 if( aTextBox->GetShape() == SHAPE_T::RECTANGLE )
2043 {
2044 m_out->Print( "(start %s) (end %s)",
2045 formatInternalUnits( aTextBox->GetStart(), parentFP ).c_str(),
2046 formatInternalUnits( aTextBox->GetEnd(), parentFP ).c_str() );
2047 }
2048 else if( aTextBox->GetShape() == SHAPE_T::POLY )
2049 {
2050 const SHAPE_POLY_SET& poly = aTextBox->GetPolyShape();
2051 const SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
2052
2053 formatPolyPts( outline, parentFP );
2054 }
2055 else
2056 {
2057 UNIMPLEMENTED_FOR( aTextBox->SHAPE_T_asString() );
2058 }
2059
2060 m_out->Print( "(margins %s %s %s %s)",
2061 formatInternalUnits( aTextBox->GetMarginLeft() ).c_str(),
2062 formatInternalUnits( aTextBox->GetMarginTop() ).c_str(),
2063 formatInternalUnits( aTextBox->GetMarginRight() ).c_str(),
2064 formatInternalUnits( aTextBox->GetMarginBottom() ).c_str() );
2065
2066 if( const PCB_TABLECELL* cell = dynamic_cast<const PCB_TABLECELL*>( aTextBox ) )
2067 m_out->Print( "(span %d %d)", cell->GetColSpan(), cell->GetRowSpan() );
2068
2069 EDA_ANGLE angle = aTextBox->GetTextAngle();
2070
2071 if( parentFP )
2072 {
2073 angle -= parentFP->GetOrientation();
2074 angle.Normalize720();
2075 }
2076
2077 if( !angle.IsZero() )
2078 m_out->Print( "(angle %s)", EDA_UNIT_UTILS::FormatAngle( angle ).c_str() );
2079
2080 formatLayer( aTextBox->GetLayer() );
2081
2083
2084 // PCB_TEXTBOXes are never hidden, so always omit "hide" attribute
2085 aTextBox->EDA_TEXT::Format( m_out, m_ctl | CTL_OMIT_HIDE );
2086
2087 if( aTextBox->Type() != PCB_TABLECELL_T )
2088 {
2089 KICAD_FORMAT::FormatBool( m_out, "border", aTextBox->IsBorderEnabled() );
2090 aTextBox->GetStroke().Format( m_out, pcbIUScale );
2091 }
2092
2093 if( aTextBox->GetFont() && aTextBox->GetFont()->IsOutline() )
2094 formatRenderCache( aTextBox );
2095
2096 m_out->Print( ")" );
2097}
2098
2099
2100void PCB_IO_KICAD_SEXPR::format( const PCB_TABLE* aTable ) const
2101{
2102 wxCHECK_RET( aTable != nullptr && m_out != nullptr, "" );
2103
2104 m_out->Print( "(table (column_count %d)", aTable->GetColCount() );
2105
2106 if( aTable->IsLocked() )
2107 KICAD_FORMAT::FormatBool( m_out, "locked", true );
2108
2109 formatLayer( aTable->GetLayer() );
2110
2111 m_out->Print( "(border" );
2112 KICAD_FORMAT::FormatBool( m_out, "external", aTable->StrokeExternal() );
2113 KICAD_FORMAT::FormatBool( m_out, "header", aTable->StrokeHeader() );
2114
2115 if( aTable->StrokeExternal() || aTable->StrokeHeader() )
2116 aTable->GetBorderStroke().Format( m_out, pcbIUScale );
2117
2118 m_out->Print( ")" ); // Close `border` token.
2119
2120 m_out->Print( "(separators" );
2121 KICAD_FORMAT::FormatBool( m_out, "rows", aTable->StrokeRows() );
2122 KICAD_FORMAT::FormatBool( m_out, "cols", aTable->StrokeColumns() );
2123
2124 if( aTable->StrokeRows() || aTable->StrokeColumns() )
2126
2127 m_out->Print( ")" ); // Close `separators` token.
2128
2129 m_out->Print( "(column_widths" );
2130
2131 for( int col = 0; col < aTable->GetColCount(); ++col )
2132 m_out->Print( " %s", formatInternalUnits( aTable->GetColWidth( col ) ).c_str() );
2133
2134 m_out->Print( ")" );
2135
2136 m_out->Print( "(row_heights" );
2137
2138 for( int row = 0; row < aTable->GetRowCount(); ++row )
2139 m_out->Print( " %s", formatInternalUnits( aTable->GetRowHeight( row ) ).c_str() );
2140
2141 m_out->Print( ")" );
2142
2143 m_out->Print( "(cells" );
2144
2145 for( PCB_TABLECELL* cell : aTable->GetCells() )
2146 format( static_cast<PCB_TEXTBOX*>( cell ) );
2147
2148 m_out->Print( ")" ); // Close `cells` token.
2149 m_out->Print( ")" ); // Close `table` token.
2150}
2151
2152
2153void PCB_IO_KICAD_SEXPR::format( const PCB_GROUP* aGroup ) const
2154{
2155 // Don't write empty groups
2156 if( aGroup->GetItems().empty() )
2157 return;
2158
2159 m_out->Print( "(group %s", m_out->Quotew( aGroup->GetName() ).c_str() );
2160
2162
2163 if( aGroup->IsLocked() )
2164 KICAD_FORMAT::FormatBool( m_out, "locked", true );
2165
2166 wxArrayString memberIds;
2167
2168 for( BOARD_ITEM* member : aGroup->GetItems() )
2169 memberIds.Add( member->m_Uuid.AsString() );
2170
2171 memberIds.Sort();
2172
2173 m_out->Print( "(members" );
2174
2175 for( const wxString& memberId : memberIds )
2176 m_out->Print( " %s", m_out->Quotew( memberId ).c_str() );
2177
2178 m_out->Print( ")" ); // Close `members` token.
2179 m_out->Print( ")" ); // Close `group` token.
2180}
2181
2182
2183void PCB_IO_KICAD_SEXPR::format( const PCB_GENERATOR* aGenerator ) const
2184{
2185 // Some conditions appear to still be creating ghost tuning patterns. Don't save them.
2186 if( aGenerator->GetGeneratorType() == wxT( "tuning_pattern" )
2187 && aGenerator->GetItems().empty() )
2188 {
2189 return;
2190 }
2191
2192 m_out->Print( "(generated" );
2193
2194 KICAD_FORMAT::FormatUuid( m_out, aGenerator->m_Uuid );
2195
2196 m_out->Print( "(type %s) (name %s) (layer %s)",
2197 TO_UTF8( aGenerator->GetGeneratorType() ),
2198 m_out->Quotew( aGenerator->GetName() ).c_str(),
2199 m_out->Quotew( LSET::Name( aGenerator->GetLayer() ) ).c_str() );
2200
2201 if( aGenerator->IsLocked() )
2202 KICAD_FORMAT::FormatBool( m_out, "locked", true );
2203
2204 for( const auto& [key, value] : aGenerator->GetProperties() )
2205 {
2206 if( value.CheckType<double>() || value.CheckType<int>() || value.CheckType<long>()
2207 || value.CheckType<long long>() )
2208 {
2209 double val;
2210
2211 if( !value.GetAs( &val ) )
2212 continue;
2213
2214 std::string buf = fmt::format( "{:.10g}", val );
2215
2216 // Don't quote numbers
2217 m_out->Print( "(%s %s)", key.c_str(), buf.c_str() );
2218 }
2219 else if( value.CheckType<bool>() )
2220 {
2221 bool val;
2222 value.GetAs( &val );
2223
2224 KICAD_FORMAT::FormatBool( m_out, key, val );
2225 }
2226 else if( value.CheckType<VECTOR2I>() )
2227 {
2228 VECTOR2I val;
2229 value.GetAs( &val );
2230
2231 m_out->Print( "(%s (xy %s))",
2232 key.c_str(),
2233 formatInternalUnits( val ).c_str() );
2234 }
2235 else if( value.CheckType<SHAPE_LINE_CHAIN>() )
2236 {
2237 SHAPE_LINE_CHAIN val;
2238 value.GetAs( &val );
2239
2240 m_out->Print( "(%s ", key.c_str() );
2241 formatPolyPts( val );
2242 m_out->Print( ")" );
2243 }
2244 else
2245 {
2246 wxString val;
2247
2248 if( value.CheckType<wxString>() )
2249 {
2250 value.GetAs( &val );
2251 }
2252 else if( value.CheckType<std::string>() )
2253 {
2254 std::string str;
2255 value.GetAs( &str );
2256
2257 val = wxString::FromUTF8( str );
2258 }
2259
2260 m_out->Print( "(%s %s)", key.c_str(), m_out->Quotew( val ).c_str() );
2261 }
2262 }
2263
2264 wxArrayString memberIds;
2265
2266 for( BOARD_ITEM* member : aGenerator->GetItems() )
2267 memberIds.Add( member->m_Uuid.AsString() );
2268
2269 memberIds.Sort();
2270
2271 m_out->Print( "(members" );
2272
2273 for( const wxString& memberId : memberIds )
2274 m_out->Print( " %s", m_out->Quotew( memberId ).c_str() );
2275
2276 m_out->Print( ")" ); // Close `members` token.
2277 m_out->Print( ")" ); // Close `generated` token.
2278}
2279
2280
2281void PCB_IO_KICAD_SEXPR::format( const PCB_TRACK* aTrack ) const
2282{
2283 if( aTrack->Type() == PCB_VIA_T )
2284 {
2285 PCB_LAYER_ID layer1, layer2;
2286
2287 const PCB_VIA* via = static_cast<const PCB_VIA*>( aTrack );
2288 const BOARD* board = via->GetBoard();
2289
2290 wxCHECK_RET( board != nullptr, wxT( "Via has no parent." ) );
2291
2292 m_out->Print( "(via" );
2293
2294 via->LayerPair( &layer1, &layer2 );
2295
2296 switch( via->GetViaType() )
2297 {
2298 case VIATYPE::THROUGH: // Default shape not saved.
2299 break;
2300
2301 case VIATYPE::BLIND_BURIED:
2302 m_out->Print( " blind " );
2303 break;
2304
2305 case VIATYPE::MICROVIA:
2306 m_out->Print( " micro " );
2307 break;
2308
2309 default:
2310 THROW_IO_ERROR( wxString::Format( _( "unknown via type %d" ), via->GetViaType() ) );
2311 }
2312
2313 m_out->Print( "(at %s) (size %s)",
2314 formatInternalUnits( aTrack->GetStart() ).c_str(),
2315 formatInternalUnits( via->GetWidth( F_Cu ) ).c_str() );
2316
2317 // Old boards were using UNDEFINED_DRILL_DIAMETER value in file for via drill when
2318 // via drill was the netclass value.
2319 // recent boards always set the via drill to the actual value, but now we need to
2320 // always store the drill value, because netclass value is not stored in the board file.
2321 // Otherwise the drill value of some (old) vias can be unknown
2322 if( via->GetDrill() != UNDEFINED_DRILL_DIAMETER )
2323 m_out->Print( "(drill %s)", formatInternalUnits( via->GetDrill() ).c_str() );
2324 else
2325 m_out->Print( "(drill %s)", formatInternalUnits( via->GetDrillValue() ).c_str() );
2326
2327 m_out->Print( "(layers %s %s)",
2328 m_out->Quotew( LSET::Name( layer1 ) ).c_str(),
2329 m_out->Quotew( LSET::Name( layer2 ) ).c_str() );
2330
2331 switch( via->Padstack().UnconnectedLayerMode() )
2332 {
2334 KICAD_FORMAT::FormatBool( m_out, "remove_unused_layers", true );
2335 KICAD_FORMAT::FormatBool( m_out, "keep_end_layers", false );
2336 break;
2337
2339 KICAD_FORMAT::FormatBool( m_out, "remove_unused_layers", true );
2340 KICAD_FORMAT::FormatBool( m_out, "keep_end_layers", true );
2341 break;
2342
2344 break;
2345 }
2346
2347 if( via->IsLocked() )
2348 KICAD_FORMAT::FormatBool( m_out, "locked", true );
2349
2350 if( via->GetIsFree() )
2351 KICAD_FORMAT::FormatBool( m_out, "free", true );
2352
2353 if( via->GetRemoveUnconnected() )
2354 {
2355 m_out->Print( "(zone_layer_connections" );
2356
2357 for( PCB_LAYER_ID layer : board->GetEnabledLayers().CuStack() )
2358 {
2359 if( via->GetZoneLayerOverride( layer ) == ZLO_FORCE_FLASHED )
2360 m_out->Print( " %s", m_out->Quotew( LSET::Name( layer ) ).c_str() );
2361 }
2362
2363 m_out->Print( ")" );
2364 }
2365
2366 const PADSTACK& padstack = via->Padstack();
2367
2368 formatTenting( padstack );
2369
2370 if( padstack.Mode() != PADSTACK::MODE::NORMAL )
2371 {
2372 m_out->Print( "(padstack" );
2373
2374 if( padstack.Mode() == PADSTACK::MODE::FRONT_INNER_BACK )
2375 {
2376 m_out->Print( "(mode front_inner_back)" );
2377
2378 m_out->Print( "(layer \"Inner\"" );
2379 m_out->Print( "(size %s)",
2380 formatInternalUnits( padstack.Size( PADSTACK::INNER_LAYERS ).x ).c_str() );
2381 m_out->Print( ")" );
2382 m_out->Print( "(layer \"B.Cu\"" );
2383 m_out->Print( "(size %s)",
2384 formatInternalUnits( padstack.Size( B_Cu ).x ).c_str() );
2385 m_out->Print( ")" );
2386 }
2387 else
2388 {
2389 m_out->Print( "(mode custom)" );
2390
2391 for( PCB_LAYER_ID layer : LAYER_RANGE( F_Cu, B_Cu, board->GetCopperLayerCount() ) )
2392 {
2393 if( layer == F_Cu )
2394 continue;
2395
2396 m_out->Print( "(layer %s", m_out->Quotew( LSET::Name( layer ) ).c_str() );
2397 m_out->Print( "(size %s)",
2398 formatInternalUnits( padstack.Size( layer ).x ).c_str() );
2399 m_out->Print( ")" );
2400 }
2401 }
2402
2403 m_out->Print( ")" );
2404 }
2405
2406 if( !isDefaultTeardropParameters( via->GetTeardropParams() ) )
2407 formatTeardropParameters( via->GetTeardropParams() );
2408 }
2409 else
2410 {
2411 if( aTrack->Type() == PCB_ARC_T )
2412 {
2413 const PCB_ARC* arc = static_cast<const PCB_ARC*>( aTrack );
2414
2415 m_out->Print( "(arc (start %s) (mid %s) (end %s) (width %s)",
2416 formatInternalUnits( arc->GetStart() ).c_str(),
2417 formatInternalUnits( arc->GetMid() ).c_str(),
2418 formatInternalUnits( arc->GetEnd() ).c_str(),
2419 formatInternalUnits( arc->GetWidth() ).c_str() );
2420 }
2421 else
2422 {
2423 m_out->Print( "(segment (start %s) (end %s) (width %s)",
2424 formatInternalUnits( aTrack->GetStart() ).c_str(),
2425 formatInternalUnits( aTrack->GetEnd() ).c_str(),
2426 formatInternalUnits( aTrack->GetWidth() ).c_str() );
2427 }
2428
2429 if( aTrack->IsLocked() )
2430 KICAD_FORMAT::FormatBool( m_out, "locked", true );
2431
2432 if( aTrack->GetLayerSet().count() > 1 )
2433 formatLayers( aTrack->GetLayerSet() );
2434 else
2435 formatLayer( aTrack->GetLayer() );
2436
2437 if( aTrack->HasSolderMask()
2438 && aTrack->GetLocalSolderMaskMargin().has_value()
2439 && ( aTrack->IsOnLayer( F_Cu ) || aTrack->IsOnLayer( B_Cu ) ) )
2440 {
2441 m_out->Print( "(solder_mask_margin %s)",
2442 formatInternalUnits( aTrack->GetLocalSolderMaskMargin().value() ).c_str() );
2443 }
2444 }
2445
2446 m_out->Print( "(net %d)", m_mapping->Translate( aTrack->GetNetCode() ) );
2447
2449 m_out->Print( ")" );
2450}
2451
2452
2453void PCB_IO_KICAD_SEXPR::format( const ZONE* aZone ) const
2454{
2455 // Save the NET info.
2456 // For keepout and non copper zones, net code and net name are irrelevant
2457 // so be sure a dummy value is stored, just for ZONE compatibility
2458 // (perhaps netcode and netname should be not stored)
2459
2460 bool has_no_net = aZone->GetIsRuleArea() || !aZone->IsOnCopperLayer();
2461
2462 m_out->Print( "(zone (net %d) (net_name %s)",
2463 has_no_net ? 0 : m_mapping->Translate( aZone->GetNetCode() ),
2464 m_out->Quotew( has_no_net ? wxString( wxT("") ) : aZone->GetNetname() ).c_str() );
2465
2466 if( aZone->IsLocked() )
2467 KICAD_FORMAT::FormatBool( m_out, "locked", true );
2468
2469 // If a zone exists on multiple layers, format accordingly
2470 LSET layers = aZone->GetLayerSet();
2471
2472 if( aZone->GetBoard() )
2473 layers &= aZone->GetBoard()->GetEnabledLayers();
2474
2475 if( layers.count() > 1 )
2476 formatLayers( layers );
2477 else
2478 formatLayer( aZone->GetFirstLayer() );
2479
2481
2482 if( !aZone->GetZoneName().empty() )
2483 m_out->Print( "(name %s)", m_out->Quotew( aZone->GetZoneName() ).c_str() );
2484
2485 // Save the outline aux info
2486 std::string hatch;
2487
2488 switch( aZone->GetHatchStyle() )
2489 {
2490 default:
2491 case ZONE_BORDER_DISPLAY_STYLE::NO_HATCH: hatch = "none"; break;
2492 case ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_EDGE: hatch = "edge"; break;
2493 case ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_FULL: hatch = "full"; break;
2494 }
2495
2496 m_out->Print( "(hatch %s %s)", hatch.c_str(),
2497 formatInternalUnits( aZone->GetBorderHatchPitch() ).c_str() );
2498
2499
2500
2501 if( aZone->GetAssignedPriority() > 0 )
2502 m_out->Print( "(priority %d)", aZone->GetAssignedPriority() );
2503
2504 // Add teardrop keywords in file: (attr (teardrop (type xxx)))where xxx is the teardrop type
2505 if( aZone->IsTeardropArea() )
2506 {
2507 m_out->Print( "(attr (teardrop (type %s)))",
2508 aZone->GetTeardropAreaType() == TEARDROP_TYPE::TD_VIAPAD
2509 ? "padvia"
2510 : "track_end" );
2511 }
2512
2513 m_out->Print( "(connect_pads" );
2514
2515 switch( aZone->GetPadConnection() )
2516 {
2517 default:
2518 case ZONE_CONNECTION::THERMAL: // Default option not saved or loaded.
2519 break;
2520
2521 case ZONE_CONNECTION::THT_THERMAL:
2522 m_out->Print( " thru_hole_only" );
2523 break;
2524
2525 case ZONE_CONNECTION::FULL:
2526 m_out->Print( " yes" );
2527 break;
2528
2529 case ZONE_CONNECTION::NONE:
2530 m_out->Print( " no" );
2531 break;
2532 }
2533
2534 m_out->Print( "(clearance %s)",
2535 formatInternalUnits( aZone->GetLocalClearance().value() ).c_str() );
2536
2537 m_out->Print( ")" );
2538
2539 m_out->Print( "(min_thickness %s)",
2540 formatInternalUnits( aZone->GetMinThickness() ).c_str() );
2541
2542 // We continue to write this for 3rd-party parsers, but we no longer read it (as of V7).
2543 m_out->Print( "(filled_areas_thickness no)" );
2544
2545 if( aZone->GetIsRuleArea() )
2546 {
2547 // Keepout settings
2548 m_out->Print( "(keepout (tracks %s) (vias %s) (pads %s) (copperpour %s) (footprints %s))",
2549 aZone->GetDoNotAllowTracks() ? "not_allowed" : "allowed",
2550 aZone->GetDoNotAllowVias() ? "not_allowed" : "allowed",
2551 aZone->GetDoNotAllowPads() ? "not_allowed" : "allowed",
2552 aZone->GetDoNotAllowCopperPour() ? "not_allowed" : "allowed",
2553 aZone->GetDoNotAllowFootprints() ? "not_allowed" : "allowed" );
2554
2555 // Multichannel settings
2556 m_out->Print( "(placement" );
2558
2559 switch( aZone->GetRuleAreaPlacementSourceType() )
2560 {
2561 case RULE_AREA_PLACEMENT_SOURCE_TYPE::SHEETNAME:
2562 m_out->Print( "(sheetname %s)",
2563 m_out->Quotew( aZone->GetRuleAreaPlacementSource() ).c_str() );
2564 break;
2565 case RULE_AREA_PLACEMENT_SOURCE_TYPE::COMPONENT_CLASS:
2566 m_out->Print( "(component_class %s)",
2567 m_out->Quotew( aZone->GetRuleAreaPlacementSource() ).c_str() );
2568 break;
2569 }
2570
2571 m_out->Print( ")" );
2572 }
2573
2574 m_out->Print( "(fill" );
2575
2576 // Default is not filled.
2577 if( aZone->IsFilled() )
2578 m_out->Print( " yes" );
2579
2580 // Default is polygon filled.
2581 if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
2582 m_out->Print( "(mode hatch)" );
2583
2584 m_out->Print( "(thermal_gap %s) (thermal_bridge_width %s)",
2585 formatInternalUnits( aZone->GetThermalReliefGap() ).c_str(),
2586 formatInternalUnits( aZone->GetThermalReliefSpokeWidth() ).c_str() );
2587
2589 {
2590 switch( aZone->GetCornerSmoothingType() )
2591 {
2593 m_out->Print( "(smoothing chamfer)" );
2594 break;
2595
2597 m_out->Print( "(smoothing fillet)" );
2598 break;
2599
2600 default:
2601 THROW_IO_ERROR( wxString::Format( _( "unknown zone corner smoothing type %d" ),
2602 aZone->GetCornerSmoothingType() ) );
2603 }
2604
2605 if( aZone->GetCornerRadius() != 0 )
2606 m_out->Print( "(radius %s)", formatInternalUnits( aZone->GetCornerRadius() ).c_str() );
2607 }
2608
2609 if( aZone->GetIslandRemovalMode() != ISLAND_REMOVAL_MODE::ALWAYS )
2610 {
2611 m_out->Print( "(island_removal_mode %d) (island_area_min %s)",
2612 static_cast<int>( aZone->GetIslandRemovalMode() ),
2614 }
2615
2616 if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
2617 {
2618 m_out->Print( "(hatch_thickness %s) (hatch_gap %s) (hatch_orientation %s)",
2619 formatInternalUnits( aZone->GetHatchThickness() ).c_str(),
2620 formatInternalUnits( aZone->GetHatchGap() ).c_str(),
2621 FormatDouble2Str( aZone->GetHatchOrientation().AsDegrees() ).c_str() );
2622
2623 if( aZone->GetHatchSmoothingLevel() > 0 )
2624 {
2625 m_out->Print( "(hatch_smoothing_level %d) (hatch_smoothing_value %s)",
2626 aZone->GetHatchSmoothingLevel(),
2627 FormatDouble2Str( aZone->GetHatchSmoothingValue() ).c_str() );
2628 }
2629
2630 m_out->Print( "(hatch_border_algorithm %s) (hatch_min_hole_area %s)",
2631 aZone->GetHatchBorderAlgorithm() ? "hatch_thickness" : "min_thickness",
2632 FormatDouble2Str( aZone->GetHatchHoleMinArea() ).c_str() );
2633 }
2634
2635 m_out->Print( ")" );
2636
2637 if( aZone->GetNumCorners() )
2638 {
2639 SHAPE_POLY_SET::POLYGON poly = aZone->Outline()->Polygon(0);
2640
2641 for( const SHAPE_LINE_CHAIN& chain : poly )
2642 {
2643 m_out->Print( "(polygon" );
2644 formatPolyPts( chain );
2645 m_out->Print( ")" );
2646 }
2647 }
2648
2649 // Save the PolysList (filled areas)
2650 for( PCB_LAYER_ID layer : aZone->GetLayerSet().Seq() )
2651 {
2652 const std::shared_ptr<SHAPE_POLY_SET>& fv = aZone->GetFilledPolysList( layer );
2653
2654 for( int ii = 0; ii < fv->OutlineCount(); ++ii )
2655 {
2656 m_out->Print( "(filled_polygon" );
2657 m_out->Print( "(layer %s)", m_out->Quotew( LSET::Name( layer ) ).c_str() );
2658
2659 if( aZone->IsIsland( layer, ii ) )
2660 m_out->Print( "(island)" );
2661
2662 const SHAPE_LINE_CHAIN& chain = fv->COutline( ii );
2663
2664 formatPolyPts( chain );
2665 m_out->Print( ")" );
2666 }
2667 }
2668
2669 m_out->Print( ")" );
2670}
2671
2672
2673PCB_IO_KICAD_SEXPR::PCB_IO_KICAD_SEXPR( int aControlFlags ) : PCB_IO( wxS( "KiCad" ) ),
2674 m_cache( nullptr ),
2675 m_ctl( aControlFlags ),
2676 m_mapping( new NETINFO_MAPPING() )
2677{
2678 init( nullptr );
2679 m_out = &m_sf;
2680}
2681
2682
2684{
2685 delete m_cache;
2686 delete m_mapping;
2687}
2688
2689
2690BOARD* PCB_IO_KICAD_SEXPR::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
2691 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
2692{
2693 FILE_LINE_READER reader( aFileName );
2694
2695 unsigned lineCount = 0;
2696
2698
2699 if( m_progressReporter )
2700 {
2701 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
2702
2704 THROW_IO_ERROR( _( "Open cancelled by user." ) );
2705
2706 while( reader.ReadLine() )
2707 lineCount++;
2708
2709 reader.Rewind();
2710 }
2711
2712 BOARD* board = DoLoad( reader, aAppendToMe, aProperties, m_progressReporter, lineCount );
2713
2714 // Give the filename to the board if it's new
2715 if( !aAppendToMe )
2716 board->SetFileName( aFileName );
2717
2718 return board;
2719}
2720
2721
2722BOARD* PCB_IO_KICAD_SEXPR::DoLoad( LINE_READER& aReader, BOARD* aAppendToMe, const std::map<std::string, UTF8>* aProperties,
2723 PROGRESS_REPORTER* aProgressReporter, unsigned aLineCount)
2724{
2725 init( aProperties );
2726
2727 PCB_IO_KICAD_SEXPR_PARSER parser( &aReader, aAppendToMe, m_queryUserCallback, aProgressReporter, aLineCount );
2728 BOARD* board;
2729
2730 try
2731 {
2732 board = dynamic_cast<BOARD*>( parser.Parse() );
2733 }
2734 catch( const FUTURE_FORMAT_ERROR& )
2735 {
2736 // Don't wrap a FUTURE_FORMAT_ERROR in another
2737 throw;
2738 }
2739 catch( const PARSE_ERROR& parse_error )
2740 {
2741 if( parser.IsTooRecent() )
2742 throw FUTURE_FORMAT_ERROR( parse_error, parser.GetRequiredVersion() );
2743 else
2744 throw;
2745 }
2746
2747 if( !board )
2748 {
2749 // The parser loaded something that was valid, but wasn't a board.
2750 THROW_PARSE_ERROR( _( "This file does not contain a PCB." ), parser.CurSource(),
2751 parser.CurLine(), parser.CurLineNumber(), parser.CurOffset() );
2752 }
2753
2754 return board;
2755}
2756
2757
2758void PCB_IO_KICAD_SEXPR::init( const std::map<std::string, UTF8>* aProperties )
2759{
2760 m_board = nullptr;
2761 m_reader = nullptr;
2762 m_props = aProperties;
2763}
2764
2765
2766void PCB_IO_KICAD_SEXPR::validateCache( const wxString& aLibraryPath, bool checkModified )
2767{
2769
2770 if( !m_cache || !m_cache->IsPath( aLibraryPath ) || ( checkModified && m_cache->IsModified() ) )
2771 {
2772 // a spectacular episode in memory management:
2773 delete m_cache;
2774 m_cache = new FP_CACHE( this, aLibraryPath );
2775 m_cache->Load();
2776 }
2777}
2778
2779
2780void PCB_IO_KICAD_SEXPR::FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibPath,
2781 bool aBestEfforts, const std::map<std::string, UTF8>* aProperties )
2782{
2783 LOCALE_IO toggle; // toggles on, then off, the C locale.
2784 wxDir dir( aLibPath );
2785 wxString errorMsg;
2786
2787 init( aProperties );
2788
2789 try
2790 {
2791 validateCache( aLibPath );
2792 }
2793 catch( const IO_ERROR& ioe )
2794 {
2795 errorMsg = ioe.What();
2796 }
2797
2798 // Some of the files may have been parsed correctly so we want to add the valid files to
2799 // the library.
2800
2801 for( const auto& footprint : m_cache->GetFootprints() )
2802 aFootprintNames.Add( footprint.first );
2803
2804 if( !errorMsg.IsEmpty() && !aBestEfforts )
2805 THROW_IO_ERROR( errorMsg );
2806}
2807
2808
2809const FOOTPRINT* PCB_IO_KICAD_SEXPR::getFootprint( const wxString& aLibraryPath,
2810 const wxString& aFootprintName,
2811 const std::map<std::string, UTF8>* aProperties,
2812 bool checkModified )
2813{
2814 LOCALE_IO toggle; // toggles on, then off, the C locale.
2815
2816 init( aProperties );
2817
2818 try
2819 {
2820 validateCache( aLibraryPath, checkModified );
2821 }
2822 catch( const IO_ERROR& )
2823 {
2824 // do nothing with the error
2825 }
2826
2828 FP_CACHE_FOOTPRINT_MAP::const_iterator it = footprints.find( aFootprintName );
2829
2830 if( it == footprints.end() )
2831 return nullptr;
2832
2833 return it->second->GetFootprint();
2834}
2835
2836
2837const FOOTPRINT* PCB_IO_KICAD_SEXPR::GetEnumeratedFootprint( const wxString& aLibraryPath,
2838 const wxString& aFootprintName,
2839 const std::map<std::string, UTF8>* aProperties )
2840{
2841 return getFootprint( aLibraryPath, aFootprintName, aProperties, false );
2842}
2843
2844
2845bool PCB_IO_KICAD_SEXPR::FootprintExists( const wxString& aLibraryPath, const wxString& aFootprintName,
2846 const std::map<std::string, UTF8>* aProperties )
2847{
2848 // Note: checking the cache sounds like a good idea, but won't catch files which differ
2849 // only in case.
2850 //
2851 // Since this goes out to the native filesystem, we get platform differences (ie: MSW's
2852 // case-insensitive filesystem) handled "for free".
2853 // Warning: footprint names frequently contain a point. So be careful when initializing
2854 // wxFileName, and use a CTOR with extension specified
2855 wxFileName footprintFile( aLibraryPath, aFootprintName, FILEEXT::KiCadFootprintFileExtension );
2856
2857 return footprintFile.Exists();
2858}
2859
2860
2861FOOTPRINT* PCB_IO_KICAD_SEXPR::ImportFootprint( const wxString& aFootprintPath, wxString& aFootprintNameOut,
2862 const std::map<std::string, UTF8>* aProperties )
2863{
2864 wxString fcontents;
2865 wxFFile f( aFootprintPath );
2866
2868
2869 if( !f.IsOpened() )
2870 return nullptr;
2871
2872 f.ReadAll( &fcontents );
2873
2874 aFootprintNameOut = wxFileName( aFootprintPath ).GetName();
2875
2876 return dynamic_cast<FOOTPRINT*>( Parse( fcontents ) );
2877}
2878
2879
2880FOOTPRINT* PCB_IO_KICAD_SEXPR::FootprintLoad( const wxString& aLibraryPath,
2881 const wxString& aFootprintName,
2882 bool aKeepUUID,
2883 const std::map<std::string, UTF8>* aProperties )
2884{
2886
2887 const FOOTPRINT* footprint = getFootprint( aLibraryPath, aFootprintName, aProperties, true );
2888
2889 if( footprint )
2890 {
2891 FOOTPRINT* copy;
2892
2893 if( aKeepUUID )
2894 copy = static_cast<FOOTPRINT*>( footprint->Clone() );
2895 else
2896 copy = static_cast<FOOTPRINT*>( footprint->Duplicate() );
2897
2898 copy->SetParent( nullptr );
2899 return copy;
2900 }
2901
2902 return nullptr;
2903}
2904
2905
2906void PCB_IO_KICAD_SEXPR::FootprintSave( const wxString& aLibraryPath, const FOOTPRINT* aFootprint,
2907 const std::map<std::string, UTF8>* aProperties )
2908{
2909 LOCALE_IO toggle; // toggles on, then off, the C locale.
2910
2911 init( aProperties );
2912
2913 // In this public PLUGIN API function, we can safely assume it was
2914 // called for saving into a library path.
2916
2917 validateCache( aLibraryPath, !aProperties || !aProperties->contains( "skip_cache_validation" ) );
2918
2919 if( !m_cache->IsWritable() )
2920 {
2921 if( !m_cache->Exists() )
2922 {
2923 const wxString msg = wxString::Format( _( "Library '%s' does not exist.\n"
2924 "Would you like to create it?"),
2925 aLibraryPath );
2926
2927 if( !Pgm().IsGUI()
2928 || wxMessageBox( msg, _( "Library Not Found" ), wxYES_NO | wxICON_QUESTION )
2929 != wxYES )
2930 return;
2931
2932 // Save throws its own IO_ERROR on failure, so no need to recreate here
2933 m_cache->Save( nullptr );
2934 }
2935 else
2936 {
2937 wxString msg = wxString::Format( _( "Library '%s' is read only." ), aLibraryPath );
2938 THROW_IO_ERROR( msg );
2939 }
2940 }
2941
2942 wxString footprintName = aFootprint->GetFPID().GetLibItemName();
2943
2945 wxString fpName = aFootprint->GetFPID().GetLibItemName().wx_str();
2946 ReplaceIllegalFileNameChars( fpName, '_' );
2947
2948 // Quietly overwrite footprint and delete footprint file from path for any by same name.
2949 wxFileName fn( aLibraryPath, fpName, FILEEXT::KiCadFootprintFileExtension );
2950
2951 // Write through symlinks, don't replace them
2953
2954 if( !fn.IsOk() )
2955 {
2956 THROW_IO_ERROR( wxString::Format( _( "Footprint file name '%s' is not valid." ),
2957 fn.GetFullPath() ) );
2958 }
2959
2960 if( fn.FileExists() && !fn.IsFileWritable() )
2961 {
2962 THROW_IO_ERROR( wxString::Format( _( "Insufficient permissions to delete '%s'." ),
2963 fn.GetFullPath() ) );
2964 }
2965
2966 wxString fullPath = fn.GetFullPath();
2967 wxString fullName = fn.GetFullName();
2968 FP_CACHE_FOOTPRINT_MAP::const_iterator it = footprints.find( footprintName );
2969
2970 if( it != footprints.end() )
2971 {
2972 wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint file '%s'." ), fullPath );
2973 footprints.erase( footprintName );
2974 wxRemoveFile( fullPath );
2975 }
2976
2977 // I need my own copy for the cache
2978 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( aFootprint->Clone() );
2979
2980 // It's orientation should be zero and it should be on the front layer.
2981 footprint->SetOrientation( ANGLE_0 );
2982
2983 if( footprint->GetLayer() != F_Cu )
2984 {
2985 PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
2986
2987 if( cfg )
2988 footprint->Flip( footprint->GetPosition(), cfg->m_FlipDirection );
2989 else
2990 footprint->Flip( footprint->GetPosition(), FLIP_DIRECTION::TOP_BOTTOM );
2991 }
2992
2993 // Detach it from the board and its group
2994 footprint->SetParent( nullptr );
2995 footprint->SetParentGroup( nullptr );
2996
2997 wxLogTrace( traceKicadPcbPlugin, wxT( "Creating s-expr footprint file '%s'." ), fullPath );
2998 footprints.insert( footprintName,
2999 new FP_CACHE_ITEM( footprint, WX_FILENAME( fn.GetPath(), fullName ) ) );
3000 m_cache->Save( footprint );
3001}
3002
3003
3004void PCB_IO_KICAD_SEXPR::FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName,
3005 const std::map<std::string, UTF8>* aProperties )
3006{
3007 LOCALE_IO toggle; // toggles on, then off, the C locale.
3008
3009 init( aProperties );
3010
3011 validateCache( aLibraryPath );
3012
3013 if( !m_cache->IsWritable() )
3014 {
3015 THROW_IO_ERROR( wxString::Format( _( "Library '%s' is read only." ),
3016 aLibraryPath.GetData() ) );
3017 }
3018
3019 m_cache->Remove( aFootprintName );
3020}
3021
3022
3023
3024long long PCB_IO_KICAD_SEXPR::GetLibraryTimestamp( const wxString& aLibraryPath ) const
3025{
3026 return FP_CACHE::GetTimestamp( aLibraryPath );
3027}
3028
3029
3030void PCB_IO_KICAD_SEXPR::CreateLibrary( const wxString& aLibraryPath, const std::map<std::string, UTF8>* aProperties )
3031{
3032 if( wxDir::Exists( aLibraryPath ) )
3033 {
3034 THROW_IO_ERROR( wxString::Format( _( "Cannot overwrite library path '%s'." ),
3035 aLibraryPath.GetData() ) );
3036 }
3037
3038 LOCALE_IO toggle;
3039
3040 init( aProperties );
3041
3042 delete m_cache;
3043 m_cache = new FP_CACHE( this, aLibraryPath );
3044 m_cache->Save();
3045}
3046
3047
3048bool PCB_IO_KICAD_SEXPR::DeleteLibrary( const wxString& aLibraryPath, const std::map<std::string, UTF8>* aProperties )
3049{
3050 wxFileName fn;
3051 fn.SetPath( aLibraryPath );
3052
3053 // Return if there is no library path to delete.
3054 if( !fn.DirExists() )
3055 return false;
3056
3057 if( !fn.IsDirWritable() )
3058 {
3059 THROW_IO_ERROR( wxString::Format( _( "Insufficient permissions to delete folder '%s'." ),
3060 aLibraryPath.GetData() ) );
3061 }
3062
3063 wxDir dir( aLibraryPath );
3064
3065 if( dir.HasSubDirs() )
3066 {
3067 THROW_IO_ERROR( wxString::Format( _( "Library folder '%s' has unexpected sub-folders." ),
3068 aLibraryPath.GetData() ) );
3069 }
3070
3071 // All the footprint files must be deleted before the directory can be deleted.
3072 if( dir.HasFiles() )
3073 {
3074 unsigned i;
3075 wxFileName tmp;
3076 wxArrayString files;
3077
3078 wxDir::GetAllFiles( aLibraryPath, &files );
3079
3080 for( i = 0; i < files.GetCount(); i++ )
3081 {
3082 tmp = files[i];
3083
3084 if( tmp.GetExt() != FILEEXT::KiCadFootprintFileExtension )
3085 {
3086 THROW_IO_ERROR( wxString::Format( _( "Unexpected file '%s' found in library "
3087 "path '%s'." ),
3088 files[i].GetData(),
3089 aLibraryPath.GetData() ) );
3090 }
3091 }
3092
3093 for( i = 0; i < files.GetCount(); i++ )
3094 wxRemoveFile( files[i] );
3095 }
3096
3097 wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint library '%s'." ),
3098 aLibraryPath.GetData() );
3099
3100 // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
3101 // we don't want that. we want bare metal portability with no UI here.
3102 if( !wxRmdir( aLibraryPath ) )
3103 {
3104 THROW_IO_ERROR( wxString::Format( _( "Footprint library '%s' cannot be deleted." ),
3105 aLibraryPath.GetData() ) );
3106 }
3107
3108 // For some reason removing a directory in Windows is not immediately updated. This delay
3109 // prevents an error when attempting to immediately recreate the same directory when over
3110 // writing an existing library.
3111#ifdef __WINDOWS__
3112 wxMilliSleep( 250L );
3113#endif
3114
3115 if( m_cache && !m_cache->IsPath( aLibraryPath ) )
3116 {
3117 delete m_cache;
3118 m_cache = nullptr;
3119 }
3120
3121 return true;
3122}
3123
3124
3125bool PCB_IO_KICAD_SEXPR::IsLibraryWritable( const wxString& aLibraryPath )
3126{
3127 LOCALE_IO toggle;
3128
3129 init( nullptr );
3130
3131 validateCache( aLibraryPath );
3132
3133 return m_cache->IsWritable();
3134}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
@ ZLO_FORCE_FLASHED
Definition: board_item.h:68
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
const wxMemoryBuffer & GetImageDataBuffer() const
Definition: bitmap_base.h:246
wxImage * GetImageData()
Definition: bitmap_base.h:68
TEARDROP_PARAMETERS & GetTeardropParams()
Container for design settings for a BOARD object.
int GetBoardThickness() const
The full thickness of the board including copper and masks.
const VECTOR2I & GetGridOrigin()
const VECTOR2I & GetAuxOrigin()
BOARD_STACKUP & GetStackupDescriptor()
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:239
void SetParentGroup(PCB_GROUP *aGroup)
Definition: board_item.h:89
virtual bool IsKnockout() const
Definition: board_item.h:326
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:47
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:298
VECTOR2I GetFPRelativePosition() const
Definition: board_item.cpp:327
virtual bool IsLocked() const
Definition: board_item.cpp:75
Manage layers needed to make a physical board.
void FormatBoardStackup(OUTPUTFORMATTER *aFormatter, const BOARD *aBoard) const
Write the stackup info on board file.
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:295
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:817
EMBEDDED_FILES * GetEmbeddedFiles() override
Definition: board.cpp:2589
const GENERATORS & Generators() const
Definition: board.h:342
void SetFileName(const wxString &aFileName)
Definition: board.h:330
const PAGE_INFO & GetPageSettings() const
Definition: board.h:697
const ZONES & Zones() const
Definition: board.h:340
const GROUPS & Groups() const
The groups must maintain the following invariants.
Definition: board.h:363
LAYER_T GetLayerType(PCB_LAYER_ID aLayer) const
Return the type of the copper layer given by aLayer.
Definition: board.cpp:651
TITLE_BLOCK & GetTitleBlock()
Definition: board.h:703
int GetCopperLayerCount() const
Definition: board.cpp:780
const std::map< wxString, wxString > & GetProperties() const
Definition: board.h:367
const FOOTPRINTS & Footprints() const
Definition: board.h:336
const TRACKS & Tracks() const
Definition: board.h:334
const PCB_PLOT_PARAMS & GetPlotOptions() const
Definition: board.h:700
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:613
bool LegacyTeardrops() const
Definition: board.h:1269
wxString GroupsSanityCheck(bool repair=false)
Consistency check of internal m_groups structure.
Definition: board.cpp:2800
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:934
void EmbedFonts() override
Finds all fonts used in the board and embeds them in the file if permissions allow.
Definition: board.cpp:2635
const DRAWINGS & Drawings() const
Definition: board.h:338
const std::vector< COMPONENT_CLASS * > & GetConstituentClasses() const
Fetches a vector of the constituent classes for this (effective) class.
double AsDegrees() const
Definition: eda_angle.h:113
bool IsZero() const
Definition: eda_angle.h:133
EDA_ANGLE Normalize720()
Definition: eda_angle.h:271
const KIID m_Uuid
Definition: eda_item.h:490
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:104
virtual wxString GetClass() const =0
Return the class name.
const VECTOR2I & GetBezierC2() const
Definition: eda_shape.h:213
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:291
bool IsFilled() const
Definition: eda_shape.h:98
SHAPE_T GetShape() const
Definition: eda_shape.h:132
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:174
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:137
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:340
const VECTOR2I & GetBezierC1() const
Definition: eda_shape.h:210
bool IsPolyShapeValid() const
Definition: eda_shape.cpp:1590
VECTOR2I GetArcMid() const
Definition: eda_shape.cpp:810
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:80
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:260
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:134
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:98
bool IsKeepUpright() const
Definition: eda_text.h:193
virtual bool IsVisible() const
Definition: eda_text.h:174
KIFONT::FONT * GetFont() const
Definition: eda_text.h:234
std::vector< std::unique_ptr< KIFONT::GLYPH > > * GetRenderCache(const KIFONT::FONT *aFont, const wxString &forResolvedText, const VECTOR2I &aOffset={ 0, 0 }) const
Definition: eda_text.cpp:662
virtual EDA_ANGLE GetDrawRotation() const
Definition: eda_text.h:365
virtual wxString GetShownText(bool aAllowExtraText, int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:109
int GetTextThickness() const
Definition: eda_text.h:126
bool IsEmpty() const
void WriteEmbeddedFiles(OUTPUTFORMATTER &aOut, bool aWriteData) const
Output formatter for the embedded files.
void ClearEmbeddedFiles(bool aDeleteFiles=true)
void ClearEmbeddedFonts()
Remove all embedded fonts from the collection.
EMBEDDED_FILE * AddFile(const wxFileName &aName, bool aOverwrite)
Load a file from disk and adds it to the collection.
const std::map< wxString, EMBEDDED_FILE * > & EmbeddedFileMap() const
bool GetAreFontsEmbedded() const
A LINE_READER that reads from an open file.
Definition: richio.h:185
void Rewind()
Rewind the file and resets the line number back to zero.
Definition: richio.h:234
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:246
void EmbedFonts() override
Definition: footprint.cpp:3991
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:247
wxString GetLibDescription() const
Definition: footprint.h:255
ZONE_CONNECTION GetLocalZoneConnection() const
Definition: footprint.h:286
EDA_ANGLE GetOrientation() const
Definition: footprint.h:225
ZONES & Zones()
Definition: footprint.h:210
void SetOrientation(const EDA_ANGLE &aNewAngle)
Definition: footprint.cpp:2539
wxString GetSheetname() const
Definition: footprint.h:264
std::optional< int > GetLocalSolderPasteMargin() const
Definition: footprint.h:279
EDA_ITEM * Clone() const override
Invoke a function on all children.
Definition: footprint.cpp:2129
PCB_FIELD & Value()
read/write accessors:
Definition: footprint.h:656
std::optional< int > GetLocalClearance() const
Definition: footprint.h:273
BOARD_ITEM * Duplicate() const override
Create a copy of this BOARD_ITEM.
Definition: footprint.cpp:2570
std::deque< PAD * > & Pads()
Definition: footprint.h:204
int GetAttributes() const
Definition: footprint.h:288
const COMPONENT_CLASS * GetComponentClass() const
Definition: footprint.h:1026
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition: footprint.h:234
LSET GetPrivateLayers() const
Definition: footprint.h:145
wxString GetSheetfile() const
Definition: footprint.h:267
const std::vector< wxString > & GetNetTiePadGroups() const
Definition: footprint.h:337
const LIB_ID & GetFPID() const
Definition: footprint.h:246
bool IsLocked() const override
Definition: footprint.h:409
PCB_FIELD & Reference()
Definition: footprint.h:657
bool IsNetTie() const
Definition: footprint.h:295
std::optional< double > GetLocalSolderPasteMarginRatio() const
Definition: footprint.h:282
void Flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection) override
Flip this object, i.e.
Definition: footprint.cpp:2389
GROUPS & Groups()
Definition: footprint.h:213
wxString GetFilters() const
Definition: footprint.h:270
const wxArrayString * GetInitialComments() const
Return the initial comments block or NULL if none, without transfer of ownership.
Definition: footprint.h:977
void GetFields(std::vector< PCB_FIELD * > &aVector, bool aVisibleOnly) const
Populate a std::vector with PCB_TEXTs.
Definition: footprint.cpp:667
std::vector< FP_3DMODEL > & Models()
Definition: footprint.h:218
const KIID_PATH & GetPath() const
Definition: footprint.h:261
std::optional< int > GetLocalSolderMaskMargin() const
Definition: footprint.h:276
wxString GetKeywords() const
Definition: footprint.h:258
EMBEDDED_FILES * GetEmbeddedFiles() override
Definition: footprint.h:1005
bool IsPlaced() const
Definition: footprint.h:432
VECTOR2I GetPosition() const override
Definition: footprint.h:222
DRAWINGS & GraphicalItems()
Definition: footprint.h:207
Helper class for creating a footprint library cache.
FP_CACHE_ITEM(FOOTPRINT *aFootprint, const WX_FILENAME &aFileName)
static long long GetTimestamp(const wxString &aLibPath)
Generate a timestamp representing all source files in the cache (including the parent directory).
FP_CACHE_FOOTPRINT_MAP & GetFootprints()
bool Exists() const
void Save(FOOTPRINT *aFootprint=nullptr)
Save the footprint cache or a single footprint from it to disk.
PCB_IO_KICAD_SEXPR * m_owner
bool IsModified()
Return true if the cache is not up-to-date.
long long m_cache_timestamp
wxString m_lib_raw_path
void SetPath(const wxString &aPath)
wxFileName m_lib_path
bool IsPath(const wxString &aPath) const
Check if aPath is the same as the current cache path.
FP_CACHE_FOOTPRINT_MAP m_footprints
FP_CACHE(PCB_IO_KICAD_SEXPR *aOwner, const wxString &aLibraryPath)
bool IsWritable() const
void Remove(const wxString &aFootprintName)
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:226
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_base.h:95
virtual bool IsOutline() const
Definition: font.h:139
virtual void SetLineWidth(float aLineWidth)
Set the line width.
wxString AsString() const
Definition: kiid.cpp:356
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
UTF8 Format() const
Definition: lib_id.cpp:118
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:93
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition: lseq.h:47
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:37
LSEQ CuStack() const
Return a sequence of copper layers in starting from the front/top and extending to the back/bottom.
Definition: lset.cpp:245
LSEQ TechAndUserUIOrder() const
Return the technical and user layers in the order shown in layer widget.
Definition: lset.cpp:258
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:562
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:295
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition: lset.cpp:188
Handle the data for a net.
Definition: netinfo.h:56
static const int UNCONNECTED
Constant that holds the "unconnected net" number (typically 0) all items "connected" to this net are ...
Definition: netinfo.h:381
void SetBoard(const BOARD *aBoard)
Set a BOARD object that is used to prepare the net code map.
Definition: netinfo.h:223
int Translate(int aNetCode) const
Translate net number according to the map prepared by Update() function.
virtual bool Finish()
Performs any cleanup needed at the end of a write.
Definition: richio.h:431
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:545
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:460
virtual std::string Quotes(const std::string &aWrapee) const
Check aWrapee input string for a need to be quoted (e.g.
Definition: richio.cpp:506
A PADSTACK defines the characteristics of a single or multi-layer pad, in the IPC sense of the word.
Definition: padstack.h:124
std::optional< int > & Clearance(PCB_LAYER_ID aLayer=F_Cu)
Definition: padstack.cpp:1181
MASK_LAYER_PROPS & FrontOuterLayers()
Definition: padstack.h:312
std::optional< int > & ThermalSpokeWidth(PCB_LAYER_ID aLayer=F_Cu)
Definition: padstack.cpp:1246
EDA_ANGLE ThermalSpokeAngle(PCB_LAYER_ID aLayer=F_Cu) const
Definition: padstack.cpp:1281
std::optional< int > & ThermalGap(PCB_LAYER_ID aLayer=F_Cu)
Definition: padstack.cpp:1258
const VECTOR2I & Size(PCB_LAYER_ID aLayer) const
Definition: padstack.cpp:1068
@ NORMAL
Shape is the same on all layers.
@ FRONT_INNER_BACK
Up to three shapes can be defined (F_Cu, inner copper layers, B_Cu)
MODE Mode() const
Definition: padstack.h:287
MASK_LAYER_PROPS & BackOuterLayers()
Definition: padstack.h:315
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition: padstack.h:144
static constexpr PCB_LAYER_ID INNER_LAYERS
! The layer identifier to use for "inner layers" on top/inner/bottom padstacks
Definition: padstack.h:147
std::optional< ZONE_CONNECTION > & ZoneConnection(PCB_LAYER_ID aLayer=F_Cu)
Definition: padstack.cpp:1234
Definition: pad.h:54
PAD_PROP GetProperty() const
Definition: pad.h:441
bool GetRemoveUnconnected() const
Definition: pad.h:726
LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: pad.h:435
const std::vector< std::shared_ptr< PCB_SHAPE > > & GetPrimitives(PCB_LAYER_ID aLayer) const
Accessor to the basic shape list for custom-shaped pads.
Definition: pad.h:363
const ZONE_LAYER_OVERRIDE & GetZoneLayerOverride(PCB_LAYER_ID aLayer) const
Definition: pad.cpp:202
std::optional< double > GetLocalSolderPasteMarginRatio() const
Definition: pad.h:468
const wxString & GetPinType() const
Definition: pad.h:151
const VECTOR2I & GetDrillSize() const
Definition: pad.h:303
PAD_ATTRIB GetAttribute() const
Definition: pad.h:438
const wxString & GetPinFunction() const
Definition: pad.h:145
const wxString & GetNumber() const
Definition: pad.h:134
const VECTOR2I & GetDelta(PCB_LAYER_ID aLayer) const
Definition: pad.h:297
EDA_ANGLE GetThermalSpokeAngle() const
Definition: pad.h:617
double GetRoundRectRadiusRatio(PCB_LAYER_ID aLayer) const
Definition: pad.h:663
PAD_SHAPE GetShape(PCB_LAYER_ID aLayer) const
Definition: pad.h:193
bool GetKeepTopBottom() const
Definition: pad.h:742
std::optional< int > GetLocalClearance() const override
Return any local clearances set in the "classic" (ie: pre-rule) system.
Definition: pad.h:453
const PADSTACK & Padstack() const
Definition: pad.h:319
const VECTOR2I & GetOffset(PCB_LAYER_ID aLayer) const
Definition: pad.h:315
EDA_ANGLE GetOrientation() const
Return the rotation angle of the pad.
Definition: pad.h:406
PADSTACK::CUSTOM_SHAPE_ZONE_MODE GetCustomShapeInZoneOpt() const
Definition: pad.h:219
PAD_DRILL_SHAPE GetDrillShape() const
Definition: pad.h:420
int GetChamferPositions(PCB_LAYER_ID aLayer) const
Definition: pad.h:703
std::optional< int > GetLocalSolderPasteMargin() const
Definition: pad.h:462
std::optional< int > GetLocalSolderMaskMargin() const
Definition: pad.h:456
double GetChamferRectRatio(PCB_LAYER_ID aLayer) const
Definition: pad.h:686
std::optional< int > GetLocalThermalSpokeWidthOverride() const
Definition: pad.h:601
ZONE_CONNECTION GetLocalZoneConnection() const
Definition: pad.h:478
int GetLocalThermalGapOverride(wxString *aSource) const
Definition: pad.cpp:1263
PAD_SHAPE GetAnchorPadShape(PCB_LAYER_ID aLayer) const
Definition: pad.h:211
int GetPadToDieLength() const
Definition: pad.h:451
const VECTOR2I & GetSize(PCB_LAYER_ID aLayer) const
Definition: pad.h:262
void Format(OUTPUTFORMATTER *aFormatter) const
Output the page class to aFormatter in s-expression form.
Definition: page_info.cpp:275
FLIP_DIRECTION m_FlipDirection
const VECTOR2I & GetMid() const
Definition: pcb_track.h:305
Abstract dimension API.
wxString GetOverrideText() const
wxString GetSuffix() const
int GetLineThickness() const
DIM_TEXT_POSITION GetTextPositionMode() const
bool GetKeepTextAligned() const
DIM_PRECISION GetPrecision() const
wxString GetPrefix() const
DIM_UNITS_MODE GetUnitsMode() const
DIM_UNITS_FORMAT GetUnitsFormat() const
virtual const VECTOR2I & GetStart() const
The dimension's origin is the first feature point for the dimension.
DIM_ARROW_DIRECTION GetArrowDirection() const
bool GetSuppressZeroes() const
int GetExtensionOffset() const
int GetArrowLength() const
bool GetOverrideTextEnabled() const
virtual const VECTOR2I & GetEnd() const
For better understanding of the points that make a dimension:
int GetHeight() const
int GetExtensionHeight() const
Mark the center of a circle or arc with a cross shape.
A leader is a dimension-like object pointing to a specific point.
DIM_TEXT_BORDER GetTextBorder() const
An orthogonal dimension is like an aligned dimension, but the extension lines are locked to the X or ...
A radial dimension indicates either the radius or diameter of an arc or circle.
int GetLeaderLength() const
virtual const STRING_ANY_MAP GetProperties() const
virtual wxString GetGeneratorType() const
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:52
std::unordered_set< BOARD_ITEM * > & GetItems()
Definition: pcb_group.h:69
wxString GetName() const
Definition: pcb_group.h:66
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
bool IsTooRecent()
Return whether a version number, if any was parsed, was too recent.
bool IsValidBoardHeader()
Partially parse the input and check if it matches expected header.
wxString GetRequiredVersion()
Return a string representing the version of KiCad required to open this file.
A #PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
BOARD * DoLoad(LINE_READER &aReader, BOARD *aAppendToMe, const std::map< std::string, UTF8 > *aProperties, PROGRESS_REPORTER *aProgressReporter, unsigned aLineCount)
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PCB_IO can read the specified board file.
void formatProperties(const BOARD *aBoard) const
formats the Nets and Netclasses
FOOTPRINT * ImportFootprint(const wxString &aFootprintPath, wxString &aFootprintNameOut, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a single footprint from aFootprintPath and put its name in aFootprintNameOut.
void FootprintDelete(const wxString &aLibraryPath, const wxString &aFootprintName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Delete aFootprintName from the library at aLibraryPath.
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
bool IsLibraryWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
void formatTeardropParameters(const TEARDROP_PARAMETERS &tdParams) const
bool DeleteLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Delete an existing library and returns true, or if library does not exist returns false,...
NETINFO_MAPPING * m_mapping
mapping for net codes, so only not empty net codes are stored with consecutive integers as net codes
void formatNetInformation(const BOARD *aBoard) const
formats the Nets and Netclasses
const FOOTPRINT * GetEnumeratedFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management.
void CreateLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Create a new empty library at aLibraryPath empty.
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
void formatPolyPts(const SHAPE_LINE_CHAIN &outline, const FOOTPRINT *aParentFP=nullptr) const
void formatLayers(LSET aLayerMask) const
FP_CACHE * m_cache
Footprint library cache.
void formatBoardLayers(const BOARD *aBoard) const
formats the board layer information
FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
bool FootprintExists(const wxString &aLibraryPath, const wxString &aFootprintName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Check for the existence of a footprint.
void FootprintSave(const wxString &aLibraryPath, const FOOTPRINT *aFootprint, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aFootprint to an existing library located at aLibraryPath.
void format(const BOARD *aBoard) const
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...
void formatTenting(const PADSTACK &aPadstack) const
void formatGeneral(const BOARD *aBoard) const
formats the General section of the file
void formatSetup(const BOARD *aBoard) const
formats the board setup information
std::function< bool(wxString aTitle, int aIcon, wxString aMsg, wxString aAction)> m_queryUserCallback
BOARD_ITEM * Parse(const wxString &aClipboardSourceInput)
void init(const std::map< std::string, UTF8 > *aProperties)
STRING_FORMATTER m_sf
void Format(const BOARD_ITEM *aItem) const
Output aItem to aFormatter in s-expression format.
void formatLayer(PCB_LAYER_ID aLayer, bool aIsKnockout=false) const
void formatHeader(const BOARD *aBoard) const
writes everything that comes before the board_items, like settings and layers etc
const FOOTPRINT * getFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const std::map< std::string, UTF8 > *aProperties, bool checkModified)
void SetOutputFormatter(OUTPUTFORMATTER *aFormatter)
PCB_IO_KICAD_SEXPR(int aControlFlags=CTL_FOR_BOARD)
OUTPUTFORMATTER * m_out
output any Format()s to this, no ownership
void validateCache(const wxString &aLibraryPath, bool checkModified=true)
void formatRenderCache(const EDA_TEXT *aText) const
LINE_READER * m_reader
no ownership
A base class that BOARD loading and saving plugins should derive from.
Definition: pcb_io.h:71
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition: pcb_io.h:342
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition: pcb_io.cpp:42
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition: pcb_io.h:345
void Format(OUTPUTFORMATTER *aFormatter) const
Object to handle a bitmap image that can be inserted in a PCB.
VECTOR2I GetPosition() const override
Get the position of the image (this is the center of the image).
REFERENCE_IMAGE & GetReferenceImage()
std::optional< int > GetLocalSolderMaskMargin() const
Definition: pcb_shape.h:186
bool HasSolderMask() const
Definition: pcb_shape.h:183
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: pcb_shape.cpp:218
STROKE_PARAMS GetStroke() const override
Definition: pcb_shape.h:89
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition: pcb_shape.h:69
bool StrokeRows() const
Definition: pcb_table.h:86
int GetRowCount() const
Definition: pcb_table.h:103
bool StrokeColumns() const
Definition: pcb_table.h:83
bool StrokeExternal() const
Definition: pcb_table.h:53
bool StrokeHeader() const
Definition: pcb_table.h:56
std::vector< PCB_TABLECELL * > GetCells() const
Definition: pcb_table.h:138
int GetColCount() const
Definition: pcb_table.h:101
const STROKE_PARAMS & GetSeparatorsStroke() const
Definition: pcb_table.h:71
const STROKE_PARAMS & GetBorderStroke() const
Definition: pcb_table.h:59
int GetColWidth(int aCol) const
Definition: pcb_table.h:110
int GetRowHeight(int aRow) const
Definition: pcb_table.h:120
int GetShape() const
Definition: pcb_target.h:58
int GetWidth() const
Definition: pcb_target.h:64
int GetSize() const
Definition: pcb_target.h:61
VECTOR2I GetPosition() const override
Definition: pcb_target.h:55
bool IsBorderEnabled() const
Disables the border, this is done by changing the stroke internally.
int GetMarginBottom() const
Definition: pcb_textbox.h:90
int GetMarginLeft() const
Definition: pcb_textbox.h:87
int GetMarginRight() const
Definition: pcb_textbox.h:89
int GetMarginTop() const
Definition: pcb_textbox.h:88
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: pcb_track.cpp:1046
bool HasSolderMask() const
Definition: pcb_track.h:146
std::optional< int > GetLocalSolderMaskMargin() const
Definition: pcb_track.h:149
const VECTOR2I & GetStart() const
Definition: pcb_track.h:122
const VECTOR2I & GetEnd() const
Definition: pcb_track.h:119
bool IsOnLayer(PCB_LAYER_ID aLayer) const override
Test to see if this object is on the given layer.
Definition: pcb_track.cpp:958
virtual int GetWidth() const
Definition: pcb_track.h:116
A progress reporter interface for use in multi-threaded environments.
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
virtual void Report(const wxString &aMessage)=0
Display aMessage in the progress bar dialog.
Container for project specific data.
Definition: project.h:64
A REFERENCE_IMAGE is a wrapper around a BITMAP_IMAGE that is displayed in an editor as a reference fo...
const BITMAP_BASE & GetImage() const
Get the underlying image.
double GetImageScale() const
const VECTOR2I & GetArcMid() const
Definition: shape_arc.h:118
const VECTOR2I & GetP1() const
Definition: shape_arc.h:117
const VECTOR2I & GetP0() const
Definition: shape_arc.h:116
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
const SHAPE_ARC & Arc(size_t aArc) const
int PointCount() const
Return the number of points (vertices) in this line chain.
ssize_t ArcIndex(size_t aSegment) const
Return the arc index for the given segment index.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
Represent a set of closed polygons.
POLYGON & Polygon(int aIndex)
Return the aIndex-th subpolygon in the set.
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.
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:253
void Format(OUTPUTFORMATTER *out, const EDA_IU_SCALE &aIuScale) const
TEARDROP_PARAMETARS is a helper class to handle parameters needed to build teardrops for a board thes...
double m_BestWidthRatio
The height of a teardrop as ratio between height and size of pad/via.
int m_TdMaxLen
max allowed length for teardrops in IU. <= 0 to disable
bool m_AllowUseTwoTracks
True to create teardrops using 2 track segments if the first in too small.
int m_TdMaxWidth
max allowed height for teardrops in IU. <= 0 to disable
double m_BestLengthRatio
The length of a teardrop as ratio between length and size of pad/via.
double m_WidthtoSizeFilterRatio
The ratio (H/D) between the via/pad size and the track width max value to create a teardrop 1....
bool m_TdOnPadsInZones
A filter to exclude pads inside zone fills.
bool m_Enabled
Flag to enable teardrops.
bool m_CurvedEdges
True if the teardrop should be curved.
virtual void Format(OUTPUTFORMATTER *aFormatter) const
Output the object to aFormatter in s-expression form.
Definition: title_block.cpp:31
wxString wx_str() const
Definition: utf8.cpp:45
static REPORTER & GetInstance()
Definition: reporter.cpp:199
A wrapper around a wxFileName which is much more performant with a subset of the API.
Definition: wx_filename.h:50
void SetFullName(const wxString &aFileNameAndExtension)
Definition: wx_filename.cpp:34
static void ResolvePossibleSymlinks(wxFileName &aFilename)
Definition: wx_filename.cpp:91
wxString GetPath() const
Definition: wx_filename.cpp:60
wxString GetName() const
Definition: wx_filename.cpp:47
wxString GetFullPath() const
Definition: wx_filename.cpp:66
long long GetTimestamp()
Definition: wx_filename.cpp:80
Handle a list of polygons defining a copper zone.
Definition: zone.h:73
int GetHatchBorderAlgorithm() const
Definition: zone.h:306
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition: zone.h:724
std::optional< int > GetLocalClearance() const override
Definition: zone.cpp:717
wxString GetRuleAreaPlacementSource() const
Definition: zone.h:730
bool GetDoNotAllowVias() const
Definition: zone.h:732
const std::shared_ptr< SHAPE_POLY_SET > & GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition: zone.h:620
bool GetDoNotAllowPads() const
Definition: zone.h:734
bool GetDoNotAllowTracks() const
Definition: zone.h:733
bool GetRuleAreaPlacementEnabled() const
Definition: zone.h:725
bool IsFilled() const
Definition: zone.h:264
ISLAND_REMOVAL_MODE GetIslandRemovalMode() const
Definition: zone.h:753
SHAPE_POLY_SET * Outline()
Definition: zone.h:340
bool IsIsland(PCB_LAYER_ID aLayer, int aPolyIdx) const
Check if a given filled polygon is an insulated island.
Definition: zone.cpp:1321
long long int GetMinIslandArea() const
Definition: zone.h:756
const wxString & GetZoneName() const
Definition: zone.h:135
int GetMinThickness() const
Definition: zone.h:273
ZONE_CONNECTION GetPadConnection() const
Definition: zone.h:270
int GetHatchThickness() const
Definition: zone.h:288
double GetHatchHoleMinArea() const
Definition: zone.h:303
bool IsTeardropArea() const
Definition: zone.h:699
int GetThermalReliefSpokeWidth() const
Definition: zone.h:217
int GetBorderHatchPitch() const
HatchBorder related methods.
Definition: zone.cpp:1102
ZONE_BORDER_DISPLAY_STYLE GetHatchStyle() const
Definition: zone.h:609
EDA_ANGLE GetHatchOrientation() const
Definition: zone.h:294
bool GetDoNotAllowFootprints() const
Definition: zone.h:735
ZONE_FILL_MODE GetFillMode() const
Definition: zone.h:196
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: zone.h:133
bool GetDoNotAllowCopperPour() const
Definition: zone.h:731
int GetHatchGap() const
Definition: zone.h:291
RULE_AREA_PLACEMENT_SOURCE_TYPE GetRuleAreaPlacementSourceType() const
Definition: zone.h:726
TEARDROP_TYPE GetTeardropAreaType() const
Definition: zone.h:710
double GetHatchSmoothingValue() const
Definition: zone.h:300
int GetHatchSmoothingLevel() const
Definition: zone.h:297
unsigned int GetCornerRadius() const
Definition: zone.h:670
int GetCornerSmoothingType() const
Definition: zone.h:666
bool IsOnCopperLayer() const override
Definition: zone.cpp:480
PCB_LAYER_ID GetFirstLayer() const
Definition: zone.cpp:458
int GetThermalReliefGap() const
Definition: zone.h:206
unsigned GetAssignedPriority() const
Definition: zone.h:123
int GetNumCorners(void) const
Access to m_Poly parameters.
Definition: zone.h:519
static void SetReporter(REPORTER *aReporter)
Set the reporter to use for reporting font substitution warnings.
Definition: fontconfig.cpp:64
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
A copy of ConvertFileTimeToWx() because wxWidgets left it as a static function private to src/common/...
Definition: common.cpp:600
This file is part of the common library.
#define CTL_OMIT_HYPERLINK
Omit the hyperlink attribute in .kicad_xxx files.
Definition: ctl_flags.h:50
#define CTL_OMIT_UUIDS
Omit component unique ids (useless in library)
Definition: ctl_flags.h:30
#define CTL_OMIT_FOOTPRINT_VERSION
Omit the version string from the (footprint) sexpr group.
Definition: ctl_flags.h:43
#define CTL_OMIT_INITIAL_COMMENTS
Omit FOOTPRINT initial comments.
Definition: ctl_flags.h:47
#define CTL_OMIT_LIBNAME
Omit lib alias when saving (used for board/not library).
Definition: ctl_flags.h:41
#define CTL_OMIT_PATH
Omit component sheet time stamp (useless in library).
Definition: ctl_flags.h:33
#define CTL_OMIT_HIDE
Omit the hide attribute in .kicad_xxx files.
Definition: ctl_flags.h:39
#define CTL_OMIT_AT
Omit position and rotation.
Definition: ctl_flags.h:35
#define CTL_OMIT_PAD_NETS
Omit pads net names (useless in library).
Definition: ctl_flags.h:29
#define CTL_OMIT_COLOR
Omit the color attribute in .kicad_xxx files.
Definition: ctl_flags.h:49
#define MIME_BASE64_LENGTH
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:401
static constexpr EDA_ANGLE ANGLE_90
Definition: eda_angle.h:403
static constexpr EDA_ANGLE ANGLE_45
Definition: eda_angle.h:402
@ FP_SMD
Definition: footprint.h:80
@ FP_DNP
Definition: footprint.h:87
@ FP_ALLOW_MISSING_COURTYARD
Definition: footprint.h:86
@ FP_EXCLUDE_FROM_POS_FILES
Definition: footprint.h:81
@ FP_BOARD_ONLY
Definition: footprint.h:83
@ FP_EXCLUDE_FROM_BOM
Definition: footprint.h:82
@ FP_THROUGH_HOLE
Definition: footprint.h:79
@ FP_ALLOW_SOLDERMASK_BRIDGES
Definition: footprint.h:85
static const std::string KiCadFootprintFileExtension
const wxChar *const traceKicadPcbPlugin
Flag to enable GEDA PCB plugin debug output.
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:165
#define MAX_CU_LAYERS
Definition: layer_ids.h:140
bool IsExternalCopperLayer(int aLayerId)
Test whether a layer is an external (F_Cu or B_Cu) copper layer.
Definition: layer_ids.h:592
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ F_CrtYd
Definition: layer_ids.h:116
@ B_Adhes
Definition: layer_ids.h:103
@ F_Paste
Definition: layer_ids.h:104
@ F_Adhes
Definition: layer_ids.h:102
@ B_Mask
Definition: layer_ids.h:98
@ B_Cu
Definition: layer_ids.h:65
@ F_Mask
Definition: layer_ids.h:97
@ B_Paste
Definition: layer_ids.h:105
@ User_9
Definition: layer_ids.h:132
@ F_Fab
Definition: layer_ids.h:119
@ F_SilkS
Definition: layer_ids.h:100
@ B_CrtYd
Definition: layer_ids.h:115
@ User_1
Definition: layer_ids.h:124
@ B_SilkS
Definition: layer_ids.h:101
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:135
@ F_Cu
Definition: layer_ids.h:64
@ B_Fab
Definition: layer_ids.h:118
This file contains miscellaneous commonly used macros and functions.
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:96
KICOMMON_API std::string FormatInternalUnits(const EDA_IU_SCALE &aIuScale, int aValue)
Converts aValue from internal units to a string appropriate for writing to file.
Definition: eda_units.cpp:170
KICOMMON_API std::string FormatAngle(const EDA_ANGLE &aAngle)
Convert aAngle from board units to a string appropriate for writing to file.
Definition: eda_units.cpp:162
void FormatUuid(OUTPUTFORMATTER *aOut, const KIID &aUuid)
void FormatBool(OUTPUTFORMATTER *aOut, const wxString &aKey, bool aValue)
Writes a boolean to the formatter, in the style (aKey [yes|no])
bool DuplicatePermissions(const wxString &aSrc, const wxString &aDest)
Duplicates the file security data from one file to another ensuring that they are the same between bo...
Definition: unix/io.cpp:47
Class to handle a set of BOARD_ITEMs.
bool isDefaultTeardropParameters(const TEARDROP_PARAMETERS &tdParams)
std::string formatInternalUnits(int aValue)
#define SEXPR_BOARD_FILE_VERSION
Current s-expression file format version. 2 was the last legacy format version.
#define CTL_FOR_BOARD
The zero arg constructor when PCB_PLUGIN is used for PLUGIN::Load() and PLUGIN::Save()ing a BOARD fil...
boost::ptr_map< wxString, FP_CACHE_ITEM > FP_CACHE_FOOTPRINT_MAP
#define CTL_FOR_LIBRARY
Format output for a footprint library instead of clipboard or BOARD.
Pcbnew s-expression file format parser definition.
#define UNDEFINED_DRILL_DIAMETER
Definition: pcb_track.h:81
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:1073
see class PGM_BASE
bool ReplaceIllegalFileNameChars(std::string *aName, int aReplaceChar)
Checks aName for illegal file name characters.
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:398
const double IU_PER_MM
Definition: base_units.h:76
Variant of PARSE_ERROR indicating that a syntax or related error was likely caused by a file generate...
Definition: ki_exception.h:176
static const char * ShowType(LAYER_T aType)
Convert a LAYER_T enum to a string representation of the layer type.
Definition: board.cpp:683
std::optional< bool > has_solder_mask
True if this outer layer has mask (is not tented)
Definition: padstack.h:231
A filename or source description, a problem input line, a line number, a byte offset,...
Definition: ki_exception.h:120
constexpr int delta
wxLogTrace helper definitions.
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:229
@ PCB_T
Definition: typeinfo.h:82
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:105
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:102
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition: typeinfo.h:91
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:103
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:110
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:93
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition: typeinfo.h:107
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition: typeinfo.h:89
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:106
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition: typeinfo.h:95
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:101
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition: typeinfo.h:94
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:104
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695
Definition of file extensions used in Kicad.