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