KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_sexpr_plugin.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) 2020 CERN
5 * Copyright (C) 2021-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Wayne Stambaugh <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <algorithm>
24
25// For some reason wxWidgets is built with wxUSE_BASE64 unset so expose the wxWidgets
26// base64 code.
27#define wxUSE_BASE64 1
28#include <wx/base64.h>
29#include <wx/log.h>
30#include <wx/mstream.h>
31#include <advanced_config.h>
32#include <base_units.h>
33#include <build_version.h>
34#include <trace_helpers.h>
35#include <locale_io.h>
36#include <sch_bitmap.h>
37#include <sch_bus_entry.h>
38#include <sch_symbol.h>
39#include <sch_edit_frame.h> // SYMBOL_ORIENTATION_T
40#include <sch_junction.h>
41#include <sch_line.h>
42#include <sch_shape.h>
43#include <sch_no_connect.h>
44#include <sch_text.h>
45#include <sch_textbox.h>
46#include <sch_sheet.h>
47#include <sch_sheet_pin.h>
48#include <schematic.h>
50#include <sch_screen.h>
51#include <lib_shape.h>
52#include <lib_pin.h>
53#include <lib_text.h>
54#include <lib_textbox.h>
55#include <eeschema_id.h> // for MAX_UNIT_COUNT_PER_PACKAGE definition
57#include <sch_file_versions.h>
58#include <schematic_lexer.h>
62#include <symbol_lib_table.h> // for PropPowerSymsOnly definition.
63#include <ee_selection.h>
64#include <string_utils.h>
65#include <wx_filename.h> // for ::ResolvePossibleSymlinks()
66#include <progress_reporter.h>
67#include <boost/algorithm/string/join.hpp>
68
69using namespace TSCHEMATIC_T;
70
71
72#define SCH_PARSE_ERROR( text, reader, pos ) \
73 THROW_PARSE_ERROR( text, reader.GetSource(), reader.Line(), \
74 reader.LineNumber(), pos - reader.Line() )
75
76
78 m_progressReporter( nullptr )
79{
80 init( nullptr );
81}
82
83
85{
86 delete m_cache;
87}
88
89
90void SCH_SEXPR_PLUGIN::init( SCHEMATIC* aSchematic, const STRING_UTF8_MAP* aProperties )
91{
92 m_version = 0;
93 m_appending = false;
94 m_rootSheet = nullptr;
95 m_schematic = aSchematic;
96 m_cache = nullptr;
97 m_out = nullptr;
98 m_nextFreeFieldId = 100; // number arbitrarily > MANDATORY_FIELDS or SHEET_MANDATORY_FIELDS
99}
100
101
102SCH_SHEET* SCH_SEXPR_PLUGIN::LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic,
103 SCH_SHEET* aAppendToMe,
104 const STRING_UTF8_MAP* aProperties )
105{
106 wxASSERT( !aFileName || aSchematic != nullptr );
107
108 LOCALE_IO toggle; // toggles on, then off, the C locale.
109 SCH_SHEET* sheet;
110
111 wxFileName fn = aFileName;
112
113 // Unfortunately child sheet file names the legacy schematic file format are not fully
114 // qualified and are always appended to the project path. The aFileName attribute must
115 // always be an absolute path so the project path can be used for load child sheet files.
116 wxASSERT( fn.IsAbsolute() );
117
118 if( aAppendToMe )
119 {
120 m_appending = true;
121 wxLogTrace( traceSchPlugin, "Append \"%s\" to sheet \"%s\".",
122 aFileName, aAppendToMe->GetFileName() );
123
124 wxFileName normedFn = aAppendToMe->GetFileName();
125
126 if( !normedFn.IsAbsolute() )
127 {
128 if( aFileName.Right( normedFn.GetFullPath().Length() ) == normedFn.GetFullPath() )
129 m_path = aFileName.Left( aFileName.Length() - normedFn.GetFullPath().Length() );
130 }
131
132 if( m_path.IsEmpty() )
133 m_path = aSchematic->Prj().GetProjectPath();
134
135 wxLogTrace( traceSchPlugin, "Normalized append path \"%s\".", m_path );
136 }
137 else
138 {
139 m_path = aSchematic->Prj().GetProjectPath();
140 }
141
142 m_currentPath.push( m_path );
143 init( aSchematic, aProperties );
144
145 if( aAppendToMe == nullptr )
146 {
147 // Clean up any allocated memory if an exception occurs loading the schematic.
148 std::unique_ptr<SCH_SHEET> newSheet = std::make_unique<SCH_SHEET>( aSchematic );
149
150 wxFileName relPath( aFileName );
151
152 // Do not use wxPATH_UNIX as option in MakeRelativeTo(). It can create incorrect
153 // relative paths on Windows, because paths have a disk identifier (C:, D: ...)
154 relPath.MakeRelativeTo( aSchematic->Prj().GetProjectPath() );
155
156 newSheet->SetFileName( relPath.GetFullPath() );
157 m_rootSheet = newSheet.get();
158 loadHierarchy( SCH_SHEET_PATH(), newSheet.get() );
159
160 // If we got here, the schematic loaded successfully.
161 sheet = newSheet.release();
162 m_rootSheet = nullptr; // Quiet Coverity warning.
163 }
164 else
165 {
166 wxCHECK_MSG( aSchematic->IsValid(), nullptr, "Can't append to a schematic with no root!" );
167 m_rootSheet = &aSchematic->Root();
168 sheet = aAppendToMe;
169 loadHierarchy( SCH_SHEET_PATH(), sheet );
170 }
171
172 wxASSERT( m_currentPath.size() == 1 ); // only the project path should remain
173
174 m_currentPath.pop(); // Clear the path stack for next call to Load
175
176 return sheet;
177}
178
179
180// Everything below this comment is recursive. Modify with care.
181
182void SCH_SEXPR_PLUGIN::loadHierarchy( const SCH_SHEET_PATH& aParentSheetPath, SCH_SHEET* aSheet )
183{
185
186 SCH_SCREEN* screen = nullptr;
187
188 if( !aSheet->GetScreen() )
189 {
190 // SCH_SCREEN objects store the full path and file name where the SCH_SHEET object only
191 // stores the file name and extension. Add the project path to the file name and
192 // extension to compare when calling SCH_SHEET::SearchHierarchy().
193 wxFileName fileName = aSheet->GetFileName();
194
195 if( !fileName.IsAbsolute() )
196 fileName.MakeAbsolute( m_currentPath.top() );
197
198 // Save the current path so that it gets restored when descending and ascending the
199 // sheet hierarchy which allows for sheet schematic files to be nested in folders
200 // relative to the last path a schematic was loaded from.
201 wxLogTrace( traceSchPlugin, "Saving path '%s'", m_currentPath.top() );
202 m_currentPath.push( fileName.GetPath() );
203 wxLogTrace( traceSchPlugin, "Current path '%s'", m_currentPath.top() );
204 wxLogTrace( traceSchPlugin, "Loading '%s'", fileName.GetFullPath() );
205
206 SCH_SHEET_PATH ancestorSheetPath = aParentSheetPath;
207
208 while( !ancestorSheetPath.empty() )
209 {
210 if( ancestorSheetPath.LastScreen()->GetFileName() == fileName.GetFullPath() )
211 {
212 if( !m_error.IsEmpty() )
213 m_error += "\n";
214
215 m_error += wxString::Format( _( "Could not load sheet '%s' because it already "
216 "appears as a direct ancestor in the schematic "
217 "hierarchy." ),
218 fileName.GetFullPath() );
219
220 fileName = wxEmptyString;
221
222 break;
223 }
224
225 ancestorSheetPath.pop_back();
226 }
227
228 if( ancestorSheetPath.empty() )
229 {
230 // Existing schematics could be either in the root sheet path or the current sheet
231 // load path so we have to check both.
232 if( !m_rootSheet->SearchHierarchy( fileName.GetFullPath(), &screen ) )
233 m_currentSheetPath.at( 0 )->SearchHierarchy( fileName.GetFullPath(), &screen );
234 }
235
236 if( screen )
237 {
238 aSheet->SetScreen( screen );
239 aSheet->GetScreen()->SetParent( m_schematic );
240 // Do not need to load the sub-sheets - this has already been done.
241 }
242 else
243 {
244 aSheet->SetScreen( new SCH_SCREEN( m_schematic ) );
245 aSheet->GetScreen()->SetFileName( fileName.GetFullPath() );
246
247 try
248 {
249 loadFile( fileName.GetFullPath(), aSheet );
250 }
251 catch( const IO_ERROR& ioe )
252 {
253 // If there is a problem loading the root sheet, there is no recovery.
254 if( aSheet == m_rootSheet )
255 throw;
256
257 // For all subsheets, queue up the error message for the caller.
258 if( !m_error.IsEmpty() )
259 m_error += "\n";
260
261 m_error += ioe.What();
262 }
263
264 if( fileName.FileExists() )
265 {
266 aSheet->GetScreen()->SetFileReadOnly( !fileName.IsFileWritable() );
267 aSheet->GetScreen()->SetFileExists( true );
268 }
269 else
270 {
271 aSheet->GetScreen()->SetFileReadOnly( !fileName.IsDirWritable() );
272 aSheet->GetScreen()->SetFileExists( false );
273 }
274
275 SCH_SHEET_PATH currentSheetPath = aParentSheetPath;
276 currentSheetPath.push_back( aSheet );
277
278 // This was moved out of the try{} block so that any sheet definitions that
279 // the plugin fully parsed before the exception was raised will be loaded.
280 for( SCH_ITEM* aItem : aSheet->GetScreen()->Items().OfType( SCH_SHEET_T ) )
281 {
282 wxCHECK2( aItem->Type() == SCH_SHEET_T, /* do nothing */ );
283 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( aItem );
284
285 // Recursion starts here.
286 loadHierarchy( currentSheetPath, sheet );
287 }
288 }
289
290 m_currentPath.pop();
291 wxLogTrace( traceSchPlugin, "Restoring path \"%s\"", m_currentPath.top() );
292 }
293
295}
296
297
298void SCH_SEXPR_PLUGIN::loadFile( const wxString& aFileName, SCH_SHEET* aSheet )
299{
300 FILE_LINE_READER reader( aFileName );
301
302 size_t lineCount = 0;
303
305 {
306 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
307
309 THROW_IO_ERROR( _( "Open cancelled by user." ) );
310
311 while( reader.ReadLine() )
312 lineCount++;
313
314 reader.Rewind();
315 }
316
317 SCH_SEXPR_PARSER parser( &reader, m_progressReporter, lineCount, m_rootSheet, m_appending );
318
319 parser.ParseSchematic( aSheet );
320}
321
322
323void SCH_SEXPR_PLUGIN::LoadContent( LINE_READER& aReader, SCH_SHEET* aSheet, int aFileVersion )
324{
325 wxCHECK( aSheet, /* void */ );
326
327 LOCALE_IO toggle;
328 SCH_SEXPR_PARSER parser( &aReader );
329
330 parser.ParseSchematic( aSheet, true, aFileVersion );
331}
332
333
334void SCH_SEXPR_PLUGIN::SaveSchematicFile( const wxString& aFileName, SCH_SHEET* aSheet,
335 SCHEMATIC* aSchematic,
336 const STRING_UTF8_MAP* aProperties )
337{
338 wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET object." );
339 wxCHECK_RET( !aFileName.IsEmpty(), "No schematic file name defined." );
340
341 LOCALE_IO toggle; // toggles on, then off, the C locale, to write floating point values.
342
343 init( aSchematic, aProperties );
344
345 wxFileName fn = aFileName;
346
347 // File names should be absolute. Don't assume everything relative to the project path
348 // works properly.
349 wxASSERT( fn.IsAbsolute() );
350
351 PRETTIFIED_FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
352
353 m_out = &formatter; // no ownership
354
355 Format( aSheet );
356
357 if( aSheet->GetScreen() )
358 aSheet->GetScreen()->SetFileExists( true );
359}
360
361
363{
364 wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET* object." );
365 wxCHECK_RET( m_schematic != nullptr, "NULL SCHEMATIC* object." );
366
367 SCH_SCREEN* screen = aSheet->GetScreen();
368
369 wxCHECK( screen, /* void */ );
370
371 m_out->Print( 0, "(kicad_sch (version %d) (generator \"eeschema\") (generator_version \"%s\")\n\n",
373
375
376 screen->GetPageSettings().Format( m_out, 1, 0 );
377 m_out->Print( 0, "\n" );
378 screen->GetTitleBlock().Format( m_out, 1, 0 );
379
380 // Save cache library.
381 m_out->Print( 1, "(lib_symbols\n" );
382
383 for( const auto& [ libItemName, libSymbol ] : screen->GetLibSymbols() )
384 SCH_SEXPR_PLUGIN_CACHE::SaveSymbol( libSymbol, *m_out, 2, libItemName );
385
386 m_out->Print( 1, ")\n\n" );
387
388 for( const std::shared_ptr<BUS_ALIAS>& alias : screen->GetBusAliases() )
389 saveBusAlias( alias, 1 );
390
391 // Enforce item ordering
392 auto cmp =
393 []( const SCH_ITEM* a, const SCH_ITEM* b )
394 {
395 if( a->Type() != b->Type() )
396 return a->Type() < b->Type();
397
398 return a->m_Uuid < b->m_Uuid;
399 };
400
401 std::multiset<SCH_ITEM*, decltype( cmp )> save_map( cmp );
402
403 for( SCH_ITEM* item : screen->Items() )
404 {
405 // Markers are not saved, so keep them from being considered below
406 if( item->Type() != SCH_MARKER_T )
407 save_map.insert( item );
408 }
409
410 KICAD_T itemType = TYPE_NOT_INIT;
412
413 for( SCH_ITEM* item : save_map )
414 {
415 if( itemType != item->Type() )
416 {
417 itemType = item->Type();
418
419 if( itemType != SCH_SYMBOL_T
420 && itemType != SCH_JUNCTION_T
421 && itemType != SCH_SHEET_T )
422 {
423 m_out->Print( 0, "\n" );
424 }
425 }
426
427 switch( item->Type() )
428 {
429 case SCH_SYMBOL_T:
430 m_out->Print( 0, "\n" );
431 saveSymbol( static_cast<SCH_SYMBOL*>( item ), *m_schematic, 1, false );
432 break;
433
434 case SCH_BITMAP_T:
435 saveBitmap( static_cast<SCH_BITMAP*>( item ), 1 );
436 break;
437
438 case SCH_SHEET_T:
439 m_out->Print( 0, "\n" );
440 saveSheet( static_cast<SCH_SHEET*>( item ), 1 );
441 break;
442
443 case SCH_JUNCTION_T:
444 saveJunction( static_cast<SCH_JUNCTION*>( item ), 1 );
445 break;
446
447 case SCH_NO_CONNECT_T:
448 saveNoConnect( static_cast<SCH_NO_CONNECT*>( item ), 1 );
449 break;
450
453 saveBusEntry( static_cast<SCH_BUS_ENTRY_BASE*>( item ), 1 );
454 break;
455
456 case SCH_LINE_T:
457 if( layer != item->GetLayer() )
458 {
459 if( layer == SCH_LAYER_ID_START )
460 {
461 layer = item->GetLayer();
462 }
463 else
464 {
465 layer = item->GetLayer();
466 m_out->Print( 0, "\n" );
467 }
468 }
469
470 saveLine( static_cast<SCH_LINE*>( item ), 1 );
471 break;
472
473 case SCH_SHAPE_T:
474 saveShape( static_cast<SCH_SHAPE*>( item ), 1 );
475 break;
476
477 case SCH_TEXT_T:
478 case SCH_LABEL_T:
480 case SCH_HIER_LABEL_T:
482 saveText( static_cast<SCH_TEXT*>( item ), 1 );
483 break;
484
485 case SCH_TEXTBOX_T:
486 saveTextBox( static_cast<SCH_TEXTBOX*>( item ), 1 );
487 break;
488
489 default:
490 wxASSERT( "Unexpected schematic object type in SCH_SEXPR_PLUGIN::Format()" );
491 }
492 }
493
494 if( aSheet->HasRootInstance() )
495 {
496 std::vector< SCH_SHEET_INSTANCE> instances;
497
498 instances.emplace_back( aSheet->GetRootInstance() );
499 saveInstances( instances, 1 );
500 }
501
502 m_out->Print( 0, ")\n" );
503}
504
505
506void SCH_SEXPR_PLUGIN::Format( EE_SELECTION* aSelection, SCH_SHEET_PATH* aSelectionPath,
507 SCHEMATIC& aSchematic, OUTPUTFORMATTER* aFormatter,
508 bool aForClipboard )
509{
510 wxCHECK( aSelection && aSelectionPath && aFormatter, /* void */ );
511
512 LOCALE_IO toggle;
513 SCH_SHEET_LIST fullHierarchy = aSchematic.GetSheets();
514
515 m_schematic = &aSchematic;
516 m_out = aFormatter;
517
518 size_t i;
519 SCH_ITEM* item;
520 std::map<wxString, LIB_SYMBOL*> libSymbols;
521 SCH_SCREEN* screen = aSelection->GetScreen();
522
523 for( i = 0; i < aSelection->GetSize(); ++i )
524 {
525 item = dynamic_cast<SCH_ITEM*>( aSelection->GetItem( i ) );
526
527 wxCHECK2( item, continue );
528
529 if( item->Type() != SCH_SYMBOL_T )
530 continue;
531
532 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item );
533
534 wxCHECK2( symbol, continue );
535
536 wxString libSymbolLookup = symbol->GetLibId().Format().wx_str();
537
538 if( !symbol->UseLibIdLookup() )
539 libSymbolLookup = symbol->GetSchSymbolLibraryName();
540
541 auto it = screen->GetLibSymbols().find( libSymbolLookup );
542
543 if( it != screen->GetLibSymbols().end() )
544 libSymbols[ libSymbolLookup ] = it->second;
545 }
546
547 if( !libSymbols.empty() )
548 {
549 m_out->Print( 0, "(lib_symbols\n" );
550
551 for( const std::pair<const wxString, LIB_SYMBOL*>& libSymbol : libSymbols )
552 SCH_SEXPR_PLUGIN_CACHE::SaveSymbol( libSymbol.second, *m_out, 1, libSymbol.first );
553
554 m_out->Print( 0, ")\n\n" );
555 }
556
557 for( i = 0; i < aSelection->GetSize(); ++i )
558 {
559 item = (SCH_ITEM*) aSelection->GetItem( i );
560
561 switch( item->Type() )
562 {
563 case SCH_SYMBOL_T:
564 saveSymbol( static_cast<SCH_SYMBOL*>( item ), aSchematic, 0, aForClipboard,
565 aSelectionPath );
566 break;
567
568 case SCH_BITMAP_T:
569 saveBitmap( static_cast< SCH_BITMAP* >( item ), 0 );
570 break;
571
572 case SCH_SHEET_T:
573 saveSheet( static_cast< SCH_SHEET* >( item ), 0 );
574 break;
575
576 case SCH_JUNCTION_T:
577 saveJunction( static_cast< SCH_JUNCTION* >( item ), 0 );
578 break;
579
580 case SCH_NO_CONNECT_T:
581 saveNoConnect( static_cast< SCH_NO_CONNECT* >( item ), 0 );
582 break;
583
586 saveBusEntry( static_cast< SCH_BUS_ENTRY_BASE* >( item ), 0 );
587 break;
588
589 case SCH_LINE_T:
590 saveLine( static_cast< SCH_LINE* >( item ), 0 );
591 break;
592
593 case SCH_SHAPE_T:
594 saveShape( static_cast<SCH_SHAPE*>( item ), 0 );
595 break;
596
597 case SCH_TEXT_T:
598 case SCH_LABEL_T:
600 case SCH_HIER_LABEL_T:
602 saveText( static_cast<SCH_TEXT*>( item ), 0 );
603 break;
604
605 case SCH_TEXTBOX_T:
606 saveTextBox( static_cast<SCH_TEXTBOX*>( item ), 0 );
607 break;
608
609 default:
610 wxASSERT( "Unexpected schematic object type in SCH_SEXPR_PLUGIN::Format()" );
611 }
612 }
613}
614
615
616void SCH_SEXPR_PLUGIN::saveSymbol( SCH_SYMBOL* aSymbol, const SCHEMATIC& aSchematic,
617 int aNestLevel, bool aForClipboard,
618 const SCH_SHEET_PATH* aRelativePath )
619{
620 wxCHECK_RET( aSymbol != nullptr && m_out != nullptr, "" );
621
622 // Sort symbol instance data to minimize file churn.
624
625 std::string libName;
626
627 wxString symbol_name = aSymbol->GetLibId().Format();
628
629 if( symbol_name.size() )
630 {
631 libName = toUTFTildaText( symbol_name );
632 }
633 else
634 {
635 libName = "_NONAME_";
636 }
637
638 EDA_ANGLE angle;
639 int orientation = aSymbol->GetOrientation() & ~( SYM_MIRROR_X | SYM_MIRROR_Y );
640
641 if( orientation == SYM_ORIENT_90 )
642 angle = ANGLE_90;
643 else if( orientation == SYM_ORIENT_180 )
644 angle = ANGLE_180;
645 else if( orientation == SYM_ORIENT_270 )
646 angle = ANGLE_270;
647 else
648 angle = ANGLE_0;
649
650 m_out->Print( aNestLevel, "(symbol" );
651
652 if( !aSymbol->UseLibIdLookup() )
653 {
654 m_out->Print( 0, " (lib_name %s)",
655 m_out->Quotew( aSymbol->GetSchSymbolLibraryName() ).c_str() );
656 }
657
658 m_out->Print( 0, " (lib_id %s) (at %s %s %s)",
659 m_out->Quotew( aSymbol->GetLibId().Format().wx_str() ).c_str(),
661 aSymbol->GetPosition().x ).c_str(),
663 aSymbol->GetPosition().y ).c_str(),
664 EDA_UNIT_UTILS::FormatAngle( angle ).c_str() );
665
666 bool mirrorX = aSymbol->GetOrientation() & SYM_MIRROR_X;
667 bool mirrorY = aSymbol->GetOrientation() & SYM_MIRROR_Y;
668
669 if( mirrorX || mirrorY )
670 {
671 m_out->Print( 0, " (mirror" );
672
673 if( mirrorX )
674 m_out->Print( 0, " x" );
675
676 if( mirrorY )
677 m_out->Print( 0, " y" );
678
679 m_out->Print( 0, ")" );
680 }
681
682 // The symbol unit is always set to the first instance regardless of the current sheet
683 // instance to prevent file churn.
684 int unit = ( aSymbol->GetInstanceReferences().size() == 0 ) ?
685 aSymbol->GetUnit() :
686 aSymbol->GetInstanceReferences()[0].m_Unit;
687
688 if( aForClipboard && aRelativePath )
689 {
690 SCH_SYMBOL_INSTANCE unitInstance;
691
692 if( aSymbol->GetInstance( unitInstance, aRelativePath->Path() ) )
693 unit = unitInstance.m_Unit;
694 }
695
696 m_out->Print( 0, " (unit %d)", unit );
697
699 m_out->Print( 0, " (convert %d)", aSymbol->GetConvert() );
700
701 m_out->Print( 0, "\n" );
702
703 m_out->Print( aNestLevel + 1, "(exclude_from_sim %s)",
704 ( aSymbol->GetExcludedFromSim() ) ? "yes" : "no" );
705 m_out->Print( 0, " (in_bom %s)", ( aSymbol->GetExcludedFromBOM() ) ? "no" : "yes" );
706 m_out->Print( 0, " (on_board %s)", ( aSymbol->GetExcludedFromBoard() ) ? "no" : "yes" );
707 m_out->Print( 0, " (dnp %s)", ( aSymbol->GetDNP() ) ? "yes" : "no" );
708
709 if( aSymbol->GetFieldsAutoplaced() != FIELDS_AUTOPLACED_NO )
710 m_out->Print( 0, " (fields_autoplaced yes)" );
711
712 m_out->Print( 0, "\n" );
713
715
717
718 for( SCH_FIELD& field : aSymbol->GetFields() )
719 {
720 int id = field.GetId();
721 wxString value = field.GetText();
722
723 if( !aForClipboard && aSymbol->GetInstanceReferences().size() )
724 {
725 // The instance fields are always set to the default instance regardless of the
726 // sheet instance to prevent file churn.
727 if( id == REFERENCE_FIELD )
728 {
729 field.SetText( aSymbol->GetInstanceReferences()[0].m_Reference );
730 }
731 else if( id == VALUE_FIELD )
732 {
733 field.SetText( aSymbol->GetField( VALUE_FIELD )->GetText() );
734 }
735 else if( id == FOOTPRINT_FIELD )
736 {
737 field.SetText( aSymbol->GetField( FOOTPRINT_FIELD )->GetText() );
738 }
739 }
740 else if( aForClipboard && aSymbol->GetInstanceReferences().size() && aRelativePath
741 && ( id == REFERENCE_FIELD ) )
742 {
743 SCH_SYMBOL_INSTANCE instance;
744
745 if( aSymbol->GetInstance( instance, aRelativePath->Path() ) )
746 field.SetText( instance.m_Reference );
747 }
748
749 try
750 {
751 saveField( &field, aNestLevel + 1 );
752 }
753 catch( ... )
754 {
755 // Restore the changed field text on write error.
756 if( id == REFERENCE_FIELD || id == VALUE_FIELD || id == FOOTPRINT_FIELD )
757 field.SetText( value );
758
759 throw;
760 }
761
762 if( id == REFERENCE_FIELD || id == VALUE_FIELD || id == FOOTPRINT_FIELD )
763 field.SetText( value );
764 }
765
766 for( const std::unique_ptr<SCH_PIN>& pin : aSymbol->GetRawPins() )
767 {
768 if( pin->GetAlt().IsEmpty() )
769 {
770 m_out->Print( aNestLevel + 1, "(pin %s ", m_out->Quotew( pin->GetNumber() ).c_str() );
772 m_out->Print( aNestLevel + 1, ")\n" );
773 }
774 else
775 {
776 m_out->Print( aNestLevel + 1, "(pin %s ", m_out->Quotew( pin->GetNumber() ).c_str() );
778 m_out->Print( aNestLevel + 1, " (alternate %s))\n",
779 m_out->Quotew( pin->GetAlt() ).c_str() );
780 }
781 }
782
783 if( !aSymbol->GetInstanceReferences().empty() )
784 {
785 m_out->Print( aNestLevel + 1, "(instances\n" );
786
787 KIID lastProjectUuid;
788 KIID rootSheetUuid = aSchematic.Root().m_Uuid;
789 SCH_SHEET_LIST fullHierarchy = aSchematic.GetSheets();
790 bool project_open = false;
791
792 for( size_t i = 0; i < aSymbol->GetInstanceReferences().size(); i++ )
793 {
794 // Zero length KIID_PATH objects are not valid and will cause a crash below.
795 wxCHECK2( aSymbol->GetInstanceReferences()[i].m_Path.size(), continue );
796
797 // If the instance data is part of this design but no longer has an associated sheet
798 // path, don't save it. This prevents large amounts of orphaned instance data for the
799 // current project from accumulating in the schematic files.
800 //
801 // Keep all instance data when copying to the clipboard. It may be needed on paste.
802 if( !aForClipboard
803 && ( aSymbol->GetInstanceReferences()[i].m_Path[0] == rootSheetUuid )
804 && !fullHierarchy.GetSheetPathByKIIDPath( aSymbol->GetInstanceReferences()[i].m_Path ) )
805 {
806 if( project_open && ( ( i + 1 == aSymbol->GetInstanceReferences().size() )
807 || lastProjectUuid != aSymbol->GetInstanceReferences()[i+1].m_Path[0] ) )
808 {
809 m_out->Print( aNestLevel + 2, ")\n" ); // Closes `project`.
810 project_open = false;
811 }
812
813 continue;
814 }
815
816 if( lastProjectUuid != aSymbol->GetInstanceReferences()[i].m_Path[0] )
817 {
818 wxString projectName;
819
820 if( aSymbol->GetInstanceReferences()[i].m_Path[0] == rootSheetUuid )
821 projectName = aSchematic.Prj().GetProjectName();
822 else
823 projectName = aSymbol->GetInstanceReferences()[i].m_ProjectName;
824
825 lastProjectUuid = aSymbol->GetInstanceReferences()[i].m_Path[0];
826 m_out->Print( aNestLevel + 2, "(project %s\n",
827 m_out->Quotew( projectName ).c_str() );
828 project_open = true;
829 }
830
831 wxString path;
832 KIID_PATH tmp = aSymbol->GetInstanceReferences()[i].m_Path;
833
834 if( aForClipboard && aRelativePath )
835 tmp.MakeRelativeTo( aRelativePath->Path() );
836
837 path = tmp.AsString();
838
839 m_out->Print( aNestLevel + 3, "(path %s\n",
840 m_out->Quotew( path ).c_str() );
841 m_out->Print( aNestLevel + 4, "(reference %s) (unit %d)\n",
842 m_out->Quotew( aSymbol->GetInstanceReferences()[i].m_Reference ).c_str(),
843 aSymbol->GetInstanceReferences()[i].m_Unit );
844 m_out->Print( aNestLevel + 3, ")\n" );
845
846 if( project_open && ( ( i + 1 == aSymbol->GetInstanceReferences().size() )
847 || lastProjectUuid != aSymbol->GetInstanceReferences()[i+1].m_Path[0] ) )
848 {
849 m_out->Print( aNestLevel + 2, ")\n" ); // Closes `project`.
850 project_open = false;
851 }
852 }
853
854 m_out->Print( aNestLevel + 1, ")\n" ); // Closes `instances`.
855 }
856
857 m_out->Print( aNestLevel, ")\n" ); // Closes `symbol`.
858}
859
860
861void SCH_SEXPR_PLUGIN::saveField( SCH_FIELD* aField, int aNestLevel )
862{
863 wxCHECK_RET( aField != nullptr && m_out != nullptr, "" );
864
865 wxString fieldName = aField->GetCanonicalName();
866 // For some reason (bug in legacy parser?) the field ID for non-mandatory fields is -1 so
867 // check for this in order to correctly use the field name.
868
869 if( aField->GetId() == -1 /* undefined ID */ )
870 {
871 // Replace the default name built by GetCanonicalName() by
872 // the field name if exists
873 if( !aField->GetName().IsEmpty() )
874 fieldName = aField->GetName();
875
876 aField->SetId( m_nextFreeFieldId );
878 }
879 else if( aField->GetId() >= m_nextFreeFieldId )
880 {
881 m_nextFreeFieldId = aField->GetId() + 1;
882 }
883
884 m_out->Print( aNestLevel, "(property %s %s (at %s %s %s)",
885 m_out->Quotew( fieldName ).c_str(),
886 m_out->Quotew( aField->GetText() ).c_str(),
888 aField->GetPosition().x ).c_str(),
890 aField->GetPosition().y ).c_str(),
891 EDA_UNIT_UTILS::FormatAngle( aField->GetTextAngle() ).c_str() );
892
893 if( aField->IsNameShown() )
894 m_out->Print( 0, " (show_name yes)" );
895
896 if( !aField->CanAutoplace() )
897 m_out->Print( 0, " (do_not_autoplace yes)" );
898
899 if( !aField->IsDefaultFormatting()
900 || ( aField->GetTextHeight() != schIUScale.MilsToIU( DEFAULT_SIZE_TEXT ) ) )
901 {
902 m_out->Print( 0, "\n" );
903 aField->Format( m_out, aNestLevel, 0 );
904 m_out->Print( aNestLevel, ")\n" ); // Closes property token with font effects.
905 }
906 else
907 {
908 m_out->Print( 0, ")\n" ); // Closes property token without font effects.
909 }
910}
911
912
913void SCH_SEXPR_PLUGIN::saveBitmap( SCH_BITMAP* aBitmap, int aNestLevel )
914{
915 wxCHECK_RET( aBitmap != nullptr && m_out != nullptr, "" );
916
917 const wxImage* image = aBitmap->GetImage()->GetImageData();
918
919 wxCHECK_RET( image != nullptr, "wxImage* is NULL" );
920
921 m_out->Print( aNestLevel, "(image (at %s %s)",
923 aBitmap->GetPosition().x ).c_str(),
925 aBitmap->GetPosition().y ).c_str() );
926
927 double scale = aBitmap->GetImage()->GetScale();
928
929 // 20230121 or older file format versions assumed 300 image PPI at load/save.
930 // Let's keep compatibility by changing image scale.
931 if( SEXPR_SCHEMATIC_FILE_VERSION <= 20230121 )
932 {
933 BITMAP_BASE* bm_image = aBitmap->GetImage();
934 scale = scale * 300.0 / bm_image->GetPPI();
935 }
936
937 if( scale != 1.0 )
938 m_out->Print( 0, " (scale %g)", scale );
939
940 m_out->Print( 0, "\n" );
941
943
944 m_out->Print( aNestLevel + 1, "(data" );
945
946 wxString out = wxBase64Encode( aBitmap->GetImage()->GetImageDataBuffer() );
947
948 // Apparently the MIME standard character width for base64 encoding is 76 (unconfirmed)
949 // so use it in a vein attempt to be standard like.
950#define MIME_BASE64_LENGTH 76
951
952 size_t first = 0;
953
954 while( first < out.Length() )
955 {
956 m_out->Print( 0, "\n" );
957 m_out->Print( aNestLevel + 2, "\"%s\"", TO_UTF8( out( first, MIME_BASE64_LENGTH ) ) );
958 first += MIME_BASE64_LENGTH;
959 }
960
961 m_out->Print( 0, "\n" );
962 m_out->Print( aNestLevel + 1, ")\n" ); // Closes data token.
963 m_out->Print( aNestLevel, ")\n" ); // Closes image token.
964}
965
966
967void SCH_SEXPR_PLUGIN::saveSheet( SCH_SHEET* aSheet, int aNestLevel )
968{
969 wxCHECK_RET( aSheet != nullptr && m_out != nullptr, "" );
970
971 m_out->Print( aNestLevel, "(sheet (at %s %s) (size %s %s)",
973 aSheet->GetPosition().x ).c_str(),
975 aSheet->GetPosition().y ).c_str(),
977 aSheet->GetSize().x ).c_str(),
979 aSheet->GetSize().y ).c_str() );
980
982 m_out->Print( 0, " (fields_autoplaced yes)" );
983
984 m_out->Print( 0, "\n" );
985
986 STROKE_PARAMS stroke( aSheet->GetBorderWidth(), LINE_STYLE::SOLID, aSheet->GetBorderColor() );
987
988 stroke.SetWidth( aSheet->GetBorderWidth() );
989 stroke.Format( m_out, schIUScale, aNestLevel + 1 );
990
991 m_out->Print( 0, "\n" );
992
993 m_out->Print( aNestLevel + 1, "(fill (color %d %d %d %0.4f))\n",
994 KiROUND( aSheet->GetBackgroundColor().r * 255.0 ),
995 KiROUND( aSheet->GetBackgroundColor().g * 255.0 ),
996 KiROUND( aSheet->GetBackgroundColor().b * 255.0 ),
997 aSheet->GetBackgroundColor().a );
998
1000
1002
1003 for( SCH_FIELD& field : aSheet->GetFields() )
1004 {
1005 saveField( &field, aNestLevel + 1 );
1006 }
1007
1008 for( const SCH_SHEET_PIN* pin : aSheet->GetPins() )
1009 {
1010 m_out->Print( aNestLevel + 1, "(pin %s %s (at %s %s %s)\n",
1011 EscapedUTF8( pin->GetText() ).c_str(),
1012 getSheetPinShapeToken( pin->GetShape() ),
1014 pin->GetPosition().x ).c_str(),
1016 pin->GetPosition().y ).c_str(),
1017 EDA_UNIT_UTILS::FormatAngle( getSheetPinAngle( pin->GetSide() ) ).c_str() );
1018
1019 pin->Format( m_out, aNestLevel + 1, 0 );
1020
1022
1023 m_out->Print( aNestLevel + 1, ")\n" ); // Closes pin token.
1024 }
1025
1026 // Save all sheet instances here except the root sheet instance.
1027 std::vector< SCH_SHEET_INSTANCE > sheetInstances = aSheet->GetInstances();
1028
1029 auto it = sheetInstances.begin();
1030
1031 while( it != sheetInstances.end() )
1032 {
1033 if( it->m_Path.size() == 0 )
1034 it = sheetInstances.erase( it );
1035 else
1036 it++;
1037 }
1038
1039 if( !sheetInstances.empty() )
1040 {
1041 m_out->Print( aNestLevel + 1, "(instances\n" );
1042
1043 KIID lastProjectUuid;
1044 KIID rootSheetUuid = m_schematic->Root().m_Uuid;
1045 SCH_SHEET_LIST fullHierarchy = m_schematic->GetSheets();
1046 bool project_open = false;
1047
1048 for( size_t i = 0; i < sheetInstances.size(); i++ )
1049 {
1050 // If the instance data is part of this design but no longer has an associated sheet
1051 // path, don't save it. This prevents large amounts of orphaned instance data for the
1052 // current project from accumulating in the schematic files.
1053 //
1054 // Keep all instance data when copying to the clipboard. It may be needed on paste.
1055 if( ( sheetInstances[i].m_Path[0] == rootSheetUuid )
1056 && !fullHierarchy.GetSheetPathByKIIDPath( sheetInstances[i].m_Path, false ) )
1057 {
1058 if( project_open && ( ( i + 1 == sheetInstances.size() )
1059 || lastProjectUuid != sheetInstances[i+1].m_Path[0] ) )
1060 {
1061 m_out->Print( aNestLevel + 2, ")\n" ); // Closes `project` token.
1062 project_open = false;
1063 }
1064
1065 continue;
1066 }
1067
1068 if( lastProjectUuid != sheetInstances[i].m_Path[0] )
1069 {
1070 wxString projectName;
1071
1072 if( sheetInstances[i].m_Path[0] == rootSheetUuid )
1073 projectName = m_schematic->Prj().GetProjectName();
1074 else
1075 projectName = sheetInstances[i].m_ProjectName;
1076
1077 lastProjectUuid = sheetInstances[i].m_Path[0];
1078 m_out->Print( aNestLevel + 2, "(project %s\n",
1079 m_out->Quotew( projectName ).c_str() );
1080 project_open = true;
1081 }
1082
1083 wxString path = sheetInstances[i].m_Path.AsString();
1084
1085 m_out->Print( aNestLevel + 3, "(path %s (page %s))\n",
1086 m_out->Quotew( path ).c_str(),
1087 m_out->Quotew( sheetInstances[i].m_PageNumber ).c_str() );
1088
1089 if( project_open && ( ( i + 1 == sheetInstances.size() )
1090 || lastProjectUuid != sheetInstances[i+1].m_Path[0] ) )
1091 {
1092 m_out->Print( aNestLevel + 2, ")\n" ); // Closes `project` token.
1093 project_open = false;
1094 }
1095 }
1096
1097 m_out->Print( aNestLevel + 1, ")\n" ); // Closes `instances` token.
1098 }
1099
1100 m_out->Print( aNestLevel, ")\n" ); // Closes sheet token.
1101}
1102
1103
1104void SCH_SEXPR_PLUGIN::saveJunction( SCH_JUNCTION* aJunction, int aNestLevel )
1105{
1106 wxCHECK_RET( aJunction != nullptr && m_out != nullptr, "" );
1107
1108 m_out->Print( aNestLevel, "(junction (at %s %s) (diameter %s) (color %d %d %d %s)\n",
1110 aJunction->GetPosition().x ).c_str(),
1112 aJunction->GetPosition().y ).c_str(),
1114 aJunction->GetDiameter() ).c_str(),
1115 KiROUND( aJunction->GetColor().r * 255.0 ),
1116 KiROUND( aJunction->GetColor().g * 255.0 ),
1117 KiROUND( aJunction->GetColor().b * 255.0 ),
1118 FormatDouble2Str( aJunction->GetColor().a ).c_str() );
1119
1120 KICAD_FORMAT::FormatUuid( m_out, aJunction->m_Uuid );
1121
1122 m_out->Print( aNestLevel, ")\n" );
1123}
1124
1125
1126void SCH_SEXPR_PLUGIN::saveNoConnect( SCH_NO_CONNECT* aNoConnect, int aNestLevel )
1127{
1128 wxCHECK_RET( aNoConnect != nullptr && m_out != nullptr, "" );
1129
1130 m_out->Print( aNestLevel, "(no_connect (at %s %s)",
1132 aNoConnect->GetPosition().x ).c_str(),
1134 aNoConnect->GetPosition().y ).c_str() );
1135 KICAD_FORMAT::FormatUuid( m_out, aNoConnect->m_Uuid );
1136 m_out->Print( aNestLevel, ")\n" );
1137}
1138
1139
1140void SCH_SEXPR_PLUGIN::saveBusEntry( SCH_BUS_ENTRY_BASE* aBusEntry, int aNestLevel )
1141{
1142 wxCHECK_RET( aBusEntry != nullptr && m_out != nullptr, "" );
1143
1144 // Bus to bus entries are converted to bus line segments.
1145 if( aBusEntry->GetClass() == "SCH_BUS_BUS_ENTRY" )
1146 {
1147 SCH_LINE busEntryLine( aBusEntry->GetPosition(), LAYER_BUS );
1148
1149 busEntryLine.SetEndPoint( aBusEntry->GetEnd() );
1150 saveLine( &busEntryLine, aNestLevel );
1151 }
1152 else
1153 {
1154 m_out->Print( aNestLevel, "(bus_entry (at %s %s) (size %s %s)\n",
1156 aBusEntry->GetPosition().x ).c_str(),
1158 aBusEntry->GetPosition().y ).c_str(),
1160 aBusEntry->GetSize().x ).c_str(),
1162 aBusEntry->GetSize().y ).c_str() );
1163
1164 aBusEntry->GetStroke().Format( m_out, schIUScale, aNestLevel + 1 );
1165
1166 m_out->Print( 0, "\n" );
1167
1168 KICAD_FORMAT::FormatUuid( m_out, aBusEntry->m_Uuid );
1169
1170 m_out->Print( aNestLevel, ")\n" );
1171 }
1172}
1173
1174
1175void SCH_SEXPR_PLUGIN::saveShape( SCH_SHAPE* aShape, int aNestLevel )
1176{
1177 wxCHECK_RET( aShape != nullptr && m_out != nullptr, "" );
1178
1179 switch( aShape->GetShape() )
1180 {
1181 case SHAPE_T::ARC:
1182 formatArc( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1183 aShape->GetFillColor(), aShape->m_Uuid );
1184 break;
1185
1186 case SHAPE_T::CIRCLE:
1187 formatCircle( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1188 aShape->GetFillColor(), aShape->m_Uuid );
1189 break;
1190
1191 case SHAPE_T::RECTANGLE:
1192 formatRect( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1193 aShape->GetFillColor(), aShape->m_Uuid );
1194 break;
1195
1196 case SHAPE_T::BEZIER:
1197 formatBezier( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1198 aShape->GetFillColor(), aShape->m_Uuid );
1199 break;
1200
1201 case SHAPE_T::POLY:
1202 formatPoly( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1203 aShape->GetFillColor(), aShape->m_Uuid );
1204 break;
1205
1206 default:
1208 }
1209}
1210
1211
1212void SCH_SEXPR_PLUGIN::saveLine( SCH_LINE* aLine, int aNestLevel )
1213{
1214 wxCHECK_RET( aLine != nullptr && m_out != nullptr, "" );
1215
1216 wxString lineType;
1217
1218 STROKE_PARAMS line_stroke = aLine->GetStroke();
1219
1220 switch( aLine->GetLayer() )
1221 {
1222 case LAYER_BUS: lineType = "bus"; break;
1223 case LAYER_WIRE: lineType = "wire"; break;
1224 case LAYER_NOTES: lineType = "polyline"; break;
1225 default:
1226 UNIMPLEMENTED_FOR( LayerName( aLine->GetLayer() ) );
1227 }
1228
1229 m_out->Print( aNestLevel, "(%s (pts (xy %s %s) (xy %s %s))\n",
1230 TO_UTF8( lineType ),
1232 aLine->GetStartPoint().x ).c_str(),
1234 aLine->GetStartPoint().y ).c_str(),
1236 aLine->GetEndPoint().x ).c_str(),
1238 aLine->GetEndPoint().y ).c_str() );
1239
1240 line_stroke.Format( m_out, schIUScale, aNestLevel + 1 );
1241 m_out->Print( 0, "\n" );
1242
1244
1245 m_out->Print( aNestLevel, ")\n" );
1246}
1247
1248
1249void SCH_SEXPR_PLUGIN::saveText( SCH_TEXT* aText, int aNestLevel )
1250{
1251 wxCHECK_RET( aText != nullptr && m_out != nullptr, "" );
1252
1253 // Note: label is nullptr SCH_TEXT, but not for SCH_LABEL_XXX,
1254 SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( aText );
1255
1256 m_out->Print( aNestLevel, "(%s %s",
1257 getTextTypeToken( aText->Type() ),
1258 m_out->Quotew( aText->GetText() ).c_str() );
1259
1260 if( aText->Type() == SCH_TEXT_T )
1261 {
1262 m_out->Print( 0, " (exclude_from_sim %s)\n", aText->GetExcludedFromSim() ? "yes" : "no" );
1263 }
1264 else if( aText->Type() == SCH_DIRECTIVE_LABEL_T )
1265 {
1266 SCH_DIRECTIVE_LABEL* flag = static_cast<SCH_DIRECTIVE_LABEL*>( aText );
1267
1268 m_out->Print( 0, " (length %s)",
1270 flag->GetPinLength() ).c_str() );
1271 }
1272
1273 EDA_ANGLE angle = aText->GetTextAngle();
1274
1275 if( label )
1276 {
1277 if( label->Type() == SCH_GLOBAL_LABEL_T
1278 || label->Type() == SCH_HIER_LABEL_T
1279 || label->Type() == SCH_DIRECTIVE_LABEL_T )
1280 {
1281 m_out->Print( 0, " (shape %s)", getSheetPinShapeToken( label->GetShape() ) );
1282 }
1283
1284 // The angle of the text is always 0 or 90 degrees for readibility reasons,
1285 // but the item itself can have more rotation (-90 and 180 deg)
1286 switch( label->GetSpinStyle() )
1287 {
1288 default:
1289 case SPIN_STYLE::LEFT: angle += ANGLE_180; break;
1290 case SPIN_STYLE::UP: break;
1291 case SPIN_STYLE::RIGHT: break;
1292 case SPIN_STYLE::BOTTOM: angle += ANGLE_180; break;
1293 }
1294 }
1295
1296 if( aText->GetText().Length() < 50 )
1297 {
1298 m_out->Print( 0, " (at %s %s %s)",
1300 aText->GetPosition().x ).c_str(),
1302 aText->GetPosition().y ).c_str(),
1303 EDA_UNIT_UTILS::FormatAngle( angle ).c_str() );
1304 }
1305 else
1306 {
1307 m_out->Print( 0, "\n" );
1308 m_out->Print( aNestLevel + 1, "(at %s %s %s)",
1310 aText->GetPosition().x ).c_str(),
1312 aText->GetPosition().y ).c_str(),
1313 EDA_UNIT_UTILS::FormatAngle( angle ).c_str() );
1314 }
1315
1317 m_out->Print( 0, " (fields_autoplaced yes)" );
1318
1319 m_out->Print( 0, "\n" );
1320 aText->EDA_TEXT::Format( m_out, aNestLevel, 0 );
1321
1323
1324 if( label )
1325 {
1326 for( SCH_FIELD& field : label->GetFields() )
1327 saveField( &field, aNestLevel + 1 );
1328 }
1329
1330 m_out->Print( aNestLevel, ")\n" ); // Closes text token.
1331}
1332
1333
1334void SCH_SEXPR_PLUGIN::saveTextBox( SCH_TEXTBOX* aTextBox, int aNestLevel )
1335{
1336 wxCHECK_RET( aTextBox != nullptr && m_out != nullptr, "" );
1337
1338 m_out->Print( aNestLevel, "(text_box %s\n",
1339 m_out->Quotew( aTextBox->GetText() ).c_str() );
1340
1341 VECTOR2I pos = aTextBox->GetStart();
1342 VECTOR2I size = aTextBox->GetEnd() - pos;
1343
1344 m_out->Print( aNestLevel + 1, "(exclude_from_sim %s) (at %s %s %s) (size %s %s)\n",
1345 aTextBox->GetExcludedFromSim() ? "yes" : "no",
1348 EDA_UNIT_UTILS::FormatAngle( aTextBox->GetTextAngle() ).c_str(),
1351
1352 aTextBox->GetStroke().Format( m_out, schIUScale, aNestLevel + 1 );
1353 m_out->Print( 0, "\n" );
1354 formatFill( m_out, aNestLevel + 1, aTextBox->GetFillMode(), aTextBox->GetFillColor() );
1355 m_out->Print( 0, "\n" );
1356
1357 aTextBox->EDA_TEXT::Format( m_out, aNestLevel, 0 );
1358
1359 if( aTextBox->m_Uuid != niluuid )
1361
1362 m_out->Print( aNestLevel, ")\n" );
1363}
1364
1365
1366void SCH_SEXPR_PLUGIN::saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias, int aNestLevel )
1367{
1368 wxCHECK_RET( aAlias != nullptr, "BUS_ALIAS* is NULL" );
1369
1370 wxString members;
1371
1372 for( const wxString& member : aAlias->Members() )
1373 {
1374 if( !members.IsEmpty() )
1375 members += wxS( " " );
1376
1377 members += m_out->Quotew( member );
1378 }
1379
1380 m_out->Print( aNestLevel, "(bus_alias %s (members %s))\n",
1381 m_out->Quotew( aAlias->GetName() ).c_str(),
1382 TO_UTF8( members ) );
1383}
1384
1385
1386void SCH_SEXPR_PLUGIN::saveInstances( const std::vector<SCH_SHEET_INSTANCE>& aInstances,
1387 int aNestLevel )
1388{
1389 if( aInstances.size() )
1390 {
1391 m_out->Print( 0, "\n" );
1392 m_out->Print( aNestLevel, "(sheet_instances\n" );
1393
1394 for( const SCH_SHEET_INSTANCE& instance : aInstances )
1395 {
1396 wxString path = instance.m_Path.AsString();
1397
1398 if( path.IsEmpty() )
1399 path = wxT( "/" ); // Root path
1400
1401 m_out->Print( aNestLevel + 1, "(path %s (page %s))\n",
1402 m_out->Quotew( path ).c_str(),
1403 m_out->Quotew( instance.m_PageNumber ).c_str() );
1404 }
1405
1406 m_out->Print( aNestLevel, ")\n" ); // Close sheet instances token.
1407 }
1408}
1409
1410
1411void SCH_SEXPR_PLUGIN::cacheLib( const wxString& aLibraryFileName,
1412 const STRING_UTF8_MAP* aProperties )
1413{
1414 if( !m_cache || !m_cache->IsFile( aLibraryFileName ) || m_cache->IsFileChanged() )
1415 {
1416 // a spectacular episode in memory management:
1417 delete m_cache;
1418 m_cache = new SCH_SEXPR_PLUGIN_CACHE( aLibraryFileName );
1419
1420 if( !isBuffering( aProperties ) )
1421 m_cache->Load();
1422 }
1423}
1424
1425
1427{
1428 return ( aProperties && aProperties->Exists( SCH_SEXPR_PLUGIN::PropBuffering ) );
1429}
1430
1431
1433{
1434 if( m_cache )
1435 return m_cache->GetModifyHash();
1436
1437 // If the cache hasn't been loaded, it hasn't been modified.
1438 return 0;
1439}
1440
1441
1442void SCH_SEXPR_PLUGIN::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
1443 const wxString& aLibraryPath,
1444 const STRING_UTF8_MAP* aProperties )
1445{
1446 LOCALE_IO toggle; // toggles on, then off, the C locale.
1447
1448 bool powerSymbolsOnly = ( aProperties &&
1449 aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
1450
1451 cacheLib( aLibraryPath, aProperties );
1452
1453 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
1454
1455 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
1456 {
1457 if( !powerSymbolsOnly || it->second->IsPower() )
1458 aSymbolNameList.Add( it->first );
1459 }
1460}
1461
1462
1463void SCH_SEXPR_PLUGIN::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
1464 const wxString& aLibraryPath,
1465 const STRING_UTF8_MAP* aProperties )
1466{
1467 LOCALE_IO toggle; // toggles on, then off, the C locale.
1468
1469 bool powerSymbolsOnly = ( aProperties &&
1470 aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
1471
1472 cacheLib( aLibraryPath, aProperties );
1473
1474 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
1475
1476 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
1477 {
1478 if( !powerSymbolsOnly || it->second->IsPower() )
1479 aSymbolList.push_back( it->second );
1480 }
1481}
1482
1483
1484LIB_SYMBOL* SCH_SEXPR_PLUGIN::LoadSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
1485 const STRING_UTF8_MAP* aProperties )
1486{
1487 LOCALE_IO toggle; // toggles on, then off, the C locale.
1488
1489 cacheLib( aLibraryPath, aProperties );
1490
1491 LIB_SYMBOL_MAP::const_iterator it = m_cache->m_symbols.find( aSymbolName );
1492
1493 // We no longer escape '/' in symbol names, but we used to.
1494 if( it == m_cache->m_symbols.end() && aSymbolName.Contains( '/' ) )
1495 it = m_cache->m_symbols.find( EscapeString( aSymbolName, CTX_LEGACY_LIBID ) );
1496
1497 if( it == m_cache->m_symbols.end() && aSymbolName.Contains( wxT( "{slash}" ) ) )
1498 {
1499 wxString unescaped = aSymbolName;
1500 unescaped.Replace( wxT( "{slash}" ), wxT( "/" ) );
1501 it = m_cache->m_symbols.find( unescaped );
1502 }
1503
1504 if( it == m_cache->m_symbols.end() )
1505 return nullptr;
1506
1507 return it->second;
1508}
1509
1510
1511void SCH_SEXPR_PLUGIN::SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
1512 const STRING_UTF8_MAP* aProperties )
1513{
1514 LOCALE_IO toggle; // toggles on, then off, the C locale.
1515
1516 cacheLib( aLibraryPath, aProperties );
1517
1518 m_cache->AddSymbol( aSymbol );
1519
1520 if( !isBuffering( aProperties ) )
1521 m_cache->Save();
1522}
1523
1524
1525void SCH_SEXPR_PLUGIN::DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
1526 const STRING_UTF8_MAP* aProperties )
1527{
1528 LOCALE_IO toggle; // toggles on, then off, the C locale.
1529
1530 cacheLib( aLibraryPath, aProperties );
1531
1532 m_cache->DeleteSymbol( aSymbolName );
1533
1534 if( !isBuffering( aProperties ) )
1535 m_cache->Save();
1536}
1537
1538
1539void SCH_SEXPR_PLUGIN::CreateSymbolLib( const wxString& aLibraryPath,
1540 const STRING_UTF8_MAP* aProperties )
1541{
1542 if( wxFileExists( aLibraryPath ) )
1543 {
1544 THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' already exists." ),
1545 aLibraryPath.GetData() ) );
1546 }
1547
1548 LOCALE_IO toggle;
1549
1550 delete m_cache;
1551 m_cache = new SCH_SEXPR_PLUGIN_CACHE( aLibraryPath );
1553 m_cache->Save();
1554 m_cache->Load(); // update m_writable and m_mod_time
1555}
1556
1557
1558bool SCH_SEXPR_PLUGIN::DeleteSymbolLib( const wxString& aLibraryPath,
1559 const STRING_UTF8_MAP* aProperties )
1560{
1561 wxFileName fn = aLibraryPath;
1562
1563 if( !fn.FileExists() )
1564 return false;
1565
1566 // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
1567 // we don't want that. we want bare metal portability with no UI here.
1568 if( wxRemove( aLibraryPath ) )
1569 {
1570 THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' cannot be deleted." ),
1571 aLibraryPath.GetData() ) );
1572 }
1573
1574 if( m_cache && m_cache->IsFile( aLibraryPath ) )
1575 {
1576 delete m_cache;
1577 m_cache = nullptr;
1578 }
1579
1580 return true;
1581}
1582
1583
1584void SCH_SEXPR_PLUGIN::SaveLibrary( const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties )
1585{
1586 if( !m_cache )
1587 m_cache = new SCH_SEXPR_PLUGIN_CACHE( aLibraryPath );
1588
1589 wxString oldFileName = m_cache->GetFileName();
1590
1591 if( !m_cache->IsFile( aLibraryPath ) )
1592 {
1593 m_cache->SetFileName( aLibraryPath );
1594 }
1595
1596 // This is a forced save.
1598 m_cache->Save();
1599 m_cache->SetFileName( oldFileName );
1600}
1601
1602
1603bool SCH_SEXPR_PLUGIN::IsSymbolLibWritable( const wxString& aLibraryPath )
1604{
1605 wxFileName fn( aLibraryPath );
1606
1607 return ( fn.FileExists() && fn.IsFileWritable() ) || fn.IsDirWritable();
1608}
1609
1610
1611void SCH_SEXPR_PLUGIN::GetAvailableSymbolFields( std::vector<wxString>& aNames )
1612{
1613 if( !m_cache )
1614 return;
1615
1616 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
1617
1618 std::set<wxString> fieldNames;
1619
1620 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
1621 {
1622 std::vector<LIB_FIELD*> fields;
1623 it->second->GetFields( fields );
1624
1625 for( LIB_FIELD* field : fields )
1626 {
1627 if( field->IsMandatory() )
1628 continue;
1629
1630 // TODO(JE): enable configurability of this outside database libraries?
1631 // if( field->ShowInChooser() )
1632 fieldNames.insert( field->GetName() );
1633 }
1634 }
1635
1636 std::copy( fieldNames.begin(), fieldNames.end(), std::back_inserter( aNames ) );
1637}
1638
1639
1640void SCH_SEXPR_PLUGIN::GetDefaultSymbolFields( std::vector<wxString>& aNames )
1641{
1642 GetAvailableSymbolFields( aNames );
1643}
1644
1645
1646std::vector<LIB_SYMBOL*> SCH_SEXPR_PLUGIN::ParseLibSymbols( std::string& aSymbolText,
1647 std::string aSource,
1648 int aFileVersion )
1649{
1650 LOCALE_IO toggle; // toggles on, then off, the C locale.
1651 LIB_SYMBOL* newSymbol = nullptr;
1652 LIB_SYMBOL_MAP map;
1653
1654 std::vector<LIB_SYMBOL*> newSymbols;
1655 std::unique_ptr<STRING_LINE_READER> reader = std::make_unique<STRING_LINE_READER>( aSymbolText, aSource );
1656
1657 do
1658 {
1659 SCH_SEXPR_PARSER parser( reader.get() );
1660
1661 newSymbol = parser.ParseSymbol( map, aFileVersion );
1662
1663 if( newSymbol )
1664 newSymbols.emplace_back( newSymbol );
1665
1666 reader.reset( new STRING_LINE_READER( *reader ) );
1667 }
1668 while( newSymbol );
1669
1670 return newSymbols;
1671}
1672
1673
1675{
1676
1677 LOCALE_IO toggle; // toggles on, then off, the C locale.
1678 SCH_SEXPR_PLUGIN_CACHE::SaveSymbol( symbol, formatter );
1679}
1680
1681
1682const char* SCH_SEXPR_PLUGIN::PropBuffering = "buffering";
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:111
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:48
double GetScale() const
Definition: bitmap_base.h:72
const wxMemoryBuffer & GetImageDataBuffer() const
Definition: bitmap_base.h:240
int GetPPI() const
Definition: bitmap_base.h:117
wxImage * GetImageData()
Definition: bitmap_base.h:67
const KIID m_Uuid
Definition: eda_item.h:482
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:100
FILL_T GetFillMode() const
Definition: eda_shape.h:101
SHAPE_T GetShape() const
Definition: eda_shape.h:119
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:151
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:126
COLOR4D GetFillColor() const
Definition: eda_shape.h:105
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:87
int GetTextHeight() const
Definition: eda_text.h:224
bool IsDefaultFormatting() const
Definition: eda_text.cpp:865
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:131
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:95
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:880
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:238
SCH_SCREEN * GetScreen()
Definition: ee_selection.h:52
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
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
double r
Red component.
Definition: color4d.h:376
double g
Green component.
Definition: color4d.h:377
double a
Alpha component.
Definition: color4d.h:379
double b
Blue component.
Definition: color4d.h:378
bool MakeRelativeTo(const KIID_PATH &aPath)
Definition: kiid.cpp:317
wxString AsString() const
Definition: kiid.cpp:362
Definition: kiid.h:49
Field object used in symbol libraries.
Definition: lib_field.h:62
UTF8 Format() const
Definition: lib_id.cpp:118
@ DEMORGAN
Definition: lib_item.h:77
Define a library symbol object.
Definition: lib_symbol.h:99
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
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
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
void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the page class to aFormatter in s-expression form.
Definition: page_info.cpp:275
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.
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:143
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:155
Holds all the data relating to one schematic.
Definition: schematic.h:75
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:100
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition: schematic.h:121
SCH_SHEET & Root() const
Definition: schematic.h:105
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Definition: schematic.h:90
Object to handle a bitmap image that can be inserted in a schematic.
Definition: sch_bitmap.h:41
VECTOR2I GetPosition() const override
Definition: sch_bitmap.h:147
BITMAP_BASE * GetImage() const
Definition: sch_bitmap.h:54
Base class for a bus or wire entry.
Definition: sch_bus_entry.h:38
VECTOR2I GetSize() const
Definition: sch_bus_entry.h:73
VECTOR2I GetPosition() const override
virtual STROKE_PARAMS GetStroke() const override
Definition: sch_bus_entry.h:81
VECTOR2I GetEnd() const
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:52
VECTOR2I GetPosition() const override
Definition: sch_field.cpp:1261
bool IsNameShown() const
Definition: sch_field.h:184
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
Definition: sch_field.cpp:1032
int GetId() const
Definition: sch_field.h:128
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: sch_field.cpp:1000
bool CanAutoplace() const
Definition: sch_field.h:195
void SetId(int aId)
Definition: sch_field.cpp:145
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:151
virtual wxString GetClass() const override
Return the class name.
Definition: sch_item.h:161
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition: sch_item.h:258
FIELDS_AUTOPLACED GetFieldsAutoplaced() const
Return whether the fields have been automatically placed.
Definition: sch_item.h:453
COLOR4D GetColor() const
Definition: sch_junction.h:118
int GetDiameter() const
Definition: sch_junction.h:113
VECTOR2I GetPosition() const override
Definition: sch_junction.h:106
SPIN_STYLE GetSpinStyle() const
Definition: sch_label.cpp:372
LABEL_FLAG_SHAPE GetShape() const
Definition: sch_label.h:167
std::vector< SCH_FIELD > & GetFields()
Definition: sch_label.h:191
bool IsFile(const wxString &aFullPathAndFileName) const
wxString GetFileName() const
void SetFileName(const wxString &aFileName)
virtual void AddSymbol(const LIB_SYMBOL *aSymbol)
void SetModified(bool aModified=true)
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:40
virtual STROKE_PARAMS GetStroke() const override
Definition: sch_line.h:180
VECTOR2I GetEndPoint() const
Definition: sch_line.h:145
VECTOR2I GetStartPoint() const
Definition: sch_line.h:140
void SetEndPoint(const VECTOR2I &aPosition)
Definition: sch_line.h:146
VECTOR2I GetPosition() const override
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:131
auto & GetBusAliases() const
Return a set of bus aliases defined in this screen.
Definition: sch_screen.h:510
const std::map< wxString, LIB_SYMBOL * > & GetLibSymbols() const
Fetch a list of unique LIB_SYMBOL object pointers required to properly render each SCH_SYMBOL in this...
Definition: sch_screen.h:481
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:109
const wxString & GetFileName() const
Definition: sch_screen.h:144
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Definition: sch_screen.cpp:115
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:155
KIID m_uuid
A unique identifier for each schematic file.
Definition: sch_screen.h:674
void SetFileReadOnly(bool aIsReadOnly)
Definition: sch_screen.h:146
void SetFileExists(bool aFileExists)
Definition: sch_screen.h:149
Object to parser s-expression symbol library and schematic file formats.
void ParseSchematic(SCH_SHEET *aSheet, bool aIsCopyablyOnly=false, int aFileVersion=SEXPR_SCHEMATIC_FILE_VERSION)
Parse the internal LINE_READER object into aSheet.
LIB_SYMBOL * ParseSymbol(LIB_SYMBOL_MAP &aSymbolLibMap, int aFileVersion=SEXPR_SYMBOL_LIB_FILE_VERSION)
Parse internal LINE_READER object into symbols and return all found.
A cache assistant for the KiCad s-expression symbol libraries.
static void SaveSymbol(LIB_SYMBOL *aSymbol, OUTPUTFORMATTER &aFormatter, int aNestLevel=0, const wxString &aLibName=wxEmptyString)
void Save(const std::optional< bool > &aOpt=std::nullopt) override
Save the entire library to file m_libFileName;.
void DeleteSymbol(const wxString &aName) override
wxString m_error
For throwing exceptions or errors on partial loads.
void saveBusAlias(std::shared_ptr< BUS_ALIAS > aAlias, int aNestLevel)
void GetDefaultSymbolFields(std::vector< wxString > &aNames) override
Retrieves a list of (custom) field names that should be shown by default for this library in the symb...
SCH_SHEET_PATH m_currentSheetPath
void cacheLib(const wxString &aLibraryFileName, const STRING_UTF8_MAP *aProperties)
wxString m_path
Root project path for loading child sheets.
void saveField(SCH_FIELD *aField, int aNestLevel)
void saveTextBox(SCH_TEXTBOX *aText, int aNestLevel)
SCHEMATIC * m_schematic
OUTPUTFORMATTER * m_out
The formatter for saving SCH_SCREEN objects.
void Format(SCH_SHEET *aSheet)
void SaveSymbol(const wxString &aLibraryPath, const LIB_SYMBOL *aSymbol, const STRING_UTF8_MAP *aProperties=nullptr) override
Write aSymbol to an existing library located at aLibraryPath.
static void FormatLibSymbol(LIB_SYMBOL *aPart, OUTPUTFORMATTER &aFormatter)
void SaveLibrary(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
void GetAvailableSymbolFields(std::vector< wxString > &aNames) override
Retrieves a list of (custom) field names that are present on symbols in this library.
bool DeleteSymbolLib(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
Delete an existing symbol library and returns true if successful, or if library does not exist return...
std::stack< wxString > m_currentPath
Stack to maintain nested sheet paths.
void loadFile(const wxString &aFileName, SCH_SHEET *aSheet)
void saveLine(SCH_LINE *aLine, int aNestLevel)
void saveInstances(const std::vector< SCH_SHEET_INSTANCE > &aSheets, int aNestLevel)
virtual ~SCH_SEXPR_PLUGIN()
bool m_appending
Schematic load append status.
SCH_SHEET * m_rootSheet
The root sheet of the schematic being loaded.
void DeleteSymbol(const wxString &aLibraryPath, const wxString &aSymbolName, const STRING_UTF8_MAP *aProperties=nullptr) override
Delete the entire LIB_SYMBOL associated with aAliasName from the library aLibraryPath.
SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const STRING_UTF8_MAP *aProperties=nullptr) override
Load information from some input file format that this SCH_PLUGIN implementation knows about,...
void saveText(SCH_TEXT *aText, int aNestLevel)
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
void saveSheet(SCH_SHEET *aSheet, int aNestLevel)
SCH_SEXPR_PLUGIN_CACHE * m_cache
void CreateSymbolLib(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
Create a new empty symbol library at aLibraryPath.
void LoadContent(LINE_READER &aReader, SCH_SHEET *aSheet, int aVersion=SEXPR_SCHEMATIC_FILE_VERSION)
void saveSymbol(SCH_SYMBOL *aSymbol, const SCHEMATIC &aSchematic, int aNestLevel, bool aForClipboard, const SCH_SHEET_PATH *aRelativePath=nullptr)
bool IsSymbolLibWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
void loadHierarchy(const SCH_SHEET_PATH &aParentSheetPath, SCH_SHEET *aSheet)
static std::vector< LIB_SYMBOL * > ParseLibSymbols(std::string &aSymbolText, std::string aSource, int aFileVersion=SEXPR_SCHEMATIC_FILE_VERSION)
bool isBuffering(const STRING_UTF8_MAP *aProperties)
PROGRESS_REPORTER * m_progressReporter
void init(SCHEMATIC *aSchematic, const STRING_UTF8_MAP *aProperties=nullptr)
initialize PLUGIN like a constructor would.
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
void saveShape(SCH_SHAPE *aShape, int aNestLevel)
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const STRING_UTF8_MAP *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
void saveBitmap(SCH_BITMAP *aBitmap, int aNestLevel)
void SaveSchematicFile(const wxString &aFileName, SCH_SHEET *aSheet, SCHEMATIC *aSchematic, const STRING_UTF8_MAP *aProperties=nullptr) override
Write aSchematic to a storage file in a format that this SCH_PLUGIN implementation knows about,...
void saveBusEntry(SCH_BUS_ENTRY_BASE *aBusEntry, int aNestLevel)
int m_version
Version of file being loaded.
int GetModifyHash() const override
Return the modification hash from the library cache.
void saveNoConnect(SCH_NO_CONNECT *aNoConnect, int aNestLevel)
void saveJunction(SCH_JUNCTION *aJunction, int aNestLevel)
STROKE_PARAMS GetStroke() const override
Definition: sch_shape.h:64
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
std::optional< SCH_SHEET_PATH > GetSheetPathByKIIDPath(const KIID_PATH &aPath, bool aIncludeLastSheet=true) const
Finds a SCH_SHEET_PATH that matches the provided KIID_PATH.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
bool empty() const
Forwarded method from std::vector.
KIID_PATH Path() const
Get the sheet path as an KIID_PATH.
SCH_SCREEN * LastScreen()
SCH_SHEET * at(size_t aIndex) const
Forwarded method from std::vector.
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
void pop_back()
Forwarded method from std::vector.
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet_pin.h:66
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:57
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:308
bool HasRootInstance() const
Check to see if this sheet has a root sheet instance.
Definition: sch_sheet.cpp:1325
std::vector< SCH_FIELD > & GetFields()
Definition: sch_sheet.h:93
bool SearchHierarchy(const wxString &aFilename, SCH_SCREEN **aScreen)
Search the existing hierarchy for an instance of screen loaded from aFileName.
Definition: sch_sheet.cpp:728
VECTOR2I GetSize() const
Definition: sch_sheet.h:112
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:110
VECTOR2I GetPosition() const override
Definition: sch_sheet.h:374
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:162
const SCH_SHEET_INSTANCE & GetRootInstance() const
Return the root sheet instance data.
Definition: sch_sheet.cpp:1337
KIGFX::COLOR4D GetBorderColor() const
Definition: sch_sheet.h:118
int GetBorderWidth() const
Definition: sch_sheet.h:115
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition: sch_sheet.h:181
const std::vector< SCH_SHEET_INSTANCE > & GetInstances() const
Definition: sch_sheet.h:388
KIGFX::COLOR4D GetBackgroundColor() const
Definition: sch_sheet.h:121
Schematic symbol object.
Definition: sch_symbol.h:81
std::vector< std::unique_ptr< SCH_PIN > > & GetRawPins()
Definition: sch_symbol.h:537
int GetUnit() const
Definition: sch_symbol.h:228
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstanceReferences() const
Definition: sch_symbol.h:140
bool UseLibIdLookup() const
Definition: sch_symbol.h:190
wxString GetSchSymbolLibraryName() const
Definition: sch_symbol.cpp:293
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:940
int GetConvert() const
Definition: sch_symbol.h:270
bool GetExcludedFromBOM() const
Definition: sch_symbol.h:739
VECTOR2I GetPosition() const override
Definition: sch_symbol.h:700
bool GetExcludedFromSim() const override
Definition: sch_symbol.h:736
bool GetInstance(SCH_SYMBOL_INSTANCE &aInstance, const KIID_PATH &aSheetPath, bool aTestFromEnd=false) const
Definition: sch_symbol.cpp:576
int GetOrientation() const
Get the display symbol orientation.
const LIB_ID & GetLibId() const
Definition: sch_symbol.h:175
bool GetExcludedFromBoard() const
Definition: sch_symbol.h:742
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with SCH_FIELDs.
Definition: sch_symbol.cpp:988
void SortInstances(bool(*aSortFunction)(const SCH_SYMBOL_INSTANCE &aLhs, const SCH_SYMBOL_INSTANCE &aRhs))
Definition: sch_symbol.cpp:628
bool GetDNP() const
Definition: sch_symbol.h:745
bool GetExcludedFromSim() const override
Definition: sch_textbox.h:83
VECTOR2I GetPosition() const override
Definition: sch_text.h:134
bool GetExcludedFromSim() const override
Definition: sch_text.h:82
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.cpp:75
virtual unsigned int GetSize() const override
Return the number of stored items.
Definition: selection.h:99
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:253
A name/value tuple with unique names and optional values.
bool Exists(const std::string &aProperty) const
Simple container to manage line stroke parameters.
Definition: stroke_params.h:81
void SetWidth(int aWidth)
Definition: stroke_params.h:92
void Format(OUTPUTFORMATTER *out, const EDA_IU_SCALE &aIuScale, int nestLevel) const
static const char * PropPowerSymsOnly
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
#define MIME_BASE64_LENGTH
#define _(s)
static constexpr EDA_ANGLE & ANGLE_180
Definition: eda_angle.h:441
static constexpr EDA_ANGLE & ANGLE_90
Definition: eda_angle.h:439
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:437
static constexpr EDA_ANGLE & ANGLE_270
Definition: eda_angle.h:442
#define DEFAULT_SIZE_TEXT
This is the "default-of-the-default" hardcoded text size; individual application define their own def...
Definition: eda_text.h:70
const wxChar *const traceSchPlugin
Flag to enable legacy schematic plugin debug output.
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
KIID niluuid(0)
wxString LayerName(int aLayer)
Returns the default display name for a given layer.
Definition: layer_id.cpp:30
SCH_LAYER_ID
Eeschema drawing layers.
Definition: layer_ids.h:346
@ LAYER_WIRE
Definition: layer_ids.h:349
@ LAYER_NOTES
Definition: layer_ids.h:363
@ LAYER_BUS
Definition: layer_ids.h:350
@ SCH_LAYER_ID_START
Definition: layer_ids.h:347
#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:153
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:145
void FormatUuid(OUTPUTFORMATTER *aOut, const KIID &aUuid, char aSuffix)
@ SYM_ORIENT_270
@ SYM_MIRROR_Y
@ SYM_ORIENT_180
@ SYM_MIRROR_X
@ SYM_ORIENT_90
#define SEXPR_SCHEMATIC_FILE_VERSION
Schematic file version.
@ FIELDS_AUTOPLACED_NO
Definition: sch_item.h:60
Schematic and symbol library s-expression file format parser definitions.
void formatCircle(OUTPUTFORMATTER *aFormatter, int aNestLevel, EDA_SHAPE *aCircle, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, const KIID &aUuid)
void formatArc(OUTPUTFORMATTER *aFormatter, int aNestLevel, EDA_SHAPE *aArc, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, const KIID &aUuid)
const char * getSheetPinShapeToken(LABEL_FLAG_SHAPE aShape)
void formatFill(OUTPUTFORMATTER *aFormatter, int aNestLevel, FILL_T aFillMode, const COLOR4D &aFillColor)
Fill token formatting helper.
const char * getTextTypeToken(KICAD_T aType)
void formatRect(OUTPUTFORMATTER *aFormatter, int aNestLevel, EDA_SHAPE *aRect, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, const KIID &aUuid)
void formatBezier(OUTPUTFORMATTER *aFormatter, int aNestLevel, EDA_SHAPE *aBezier, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, const KIID &aUuid)
void formatPoly(OUTPUTFORMATTER *aFormatter, int aNestLevel, EDA_SHAPE *aPolyLine, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, const KIID &aUuid)
EDA_ANGLE getSheetPinAngle(SHEET_SIDE aSide)
@ SHEET_MANDATORY_FIELDS
The first 2 are mandatory, and must be instantiated in SCH_SHEET.
Definition: sch_sheet.h:49
bool SortSymbolInstancesByProjectUuid(const SCH_SYMBOL_INSTANCE &aLhs, const SCH_SYMBOL_INSTANCE &aRhs)
std::string toUTFTildaText(const wxString &txt)
Convert a wxString to UTF8 and replace any control characters with a ~, where a control character is ...
Definition: sch_symbol.cpp:54
const int scale
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
std::string EscapedUTF8(const wxString &aString)
Return an 8 bit UTF8 string given aString in Unicode form.
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_LEGACY_LIBID
Definition: string_utils.h:55
constexpr int MilsToIU(int mils) const
Definition: base_units.h:94
A simple container for sheet instance information.
A simple container for schematic symbol instance information.
std::map< wxString, LIB_SYMBOL *, LibSymbolMapSort > LIB_SYMBOL_MAP
@ FOOTPRINT_FIELD
Field Name Module PCB, i.e. "16DIP300".
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ MANDATORY_FIELDS
The first 5 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors.
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
wxLogTrace helper definitions.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ SCH_LINE_T
Definition: typeinfo.h:146
@ SCH_NO_CONNECT_T
Definition: typeinfo.h:143
@ TYPE_NOT_INIT
Definition: typeinfo.h:81
@ SCH_SYMBOL_T
Definition: typeinfo.h:156
@ SCH_DIRECTIVE_LABEL_T
Definition: typeinfo.h:154
@ SCH_LABEL_T
Definition: typeinfo.h:151
@ SCH_SHEET_T
Definition: typeinfo.h:158
@ SCH_MARKER_T
Definition: typeinfo.h:141
@ SCH_SHAPE_T
Definition: typeinfo.h:147
@ SCH_HIER_LABEL_T
Definition: typeinfo.h:153
@ SCH_BUS_BUS_ENTRY_T
Definition: typeinfo.h:145
@ SCH_TEXT_T
Definition: typeinfo.h:150
@ SCH_BUS_WIRE_ENTRY_T
Definition: typeinfo.h:144
@ SCH_BITMAP_T
Definition: typeinfo.h:148
@ SCH_TEXTBOX_T
Definition: typeinfo.h:149
@ SCH_GLOBAL_LABEL_T
Definition: typeinfo.h:152
@ SCH_JUNCTION_T
Definition: typeinfo.h:142
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85