KiCad PCB EDA Suite
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 <trace_helpers.h>
34#include <locale_io.h>
35#include <sch_bitmap.h>
36#include <sch_bus_entry.h>
37#include <sch_symbol.h>
38#include <sch_edit_frame.h> // SYMBOL_ORIENTATION_T
39#include <sch_junction.h>
40#include <sch_line.h>
41#include <sch_shape.h>
42#include <sch_no_connect.h>
43#include <sch_text.h>
44#include <sch_textbox.h>
45#include <sch_sheet.h>
46#include <sch_sheet_pin.h>
47#include <schematic.h>
49#include <sch_screen.h>
50#include <lib_shape.h>
51#include <lib_pin.h>
52#include <lib_text.h>
53#include <lib_textbox.h>
54#include <eeschema_id.h> // for MAX_UNIT_COUNT_PER_PACKAGE definition
55#include <sch_file_versions.h>
56#include <schematic_lexer.h>
60#include <symbol_lib_table.h> // for PropPowerSymsOnly definition.
61#include <ee_selection.h>
62#include <string_utils.h>
63#include <wx_filename.h> // for ::ResolvePossibleSymlinks()
64#include <progress_reporter.h>
65#include <boost/algorithm/string/join.hpp>
66
67using namespace TSCHEMATIC_T;
68
69
70#define SCH_PARSE_ERROR( text, reader, pos ) \
71 THROW_PARSE_ERROR( text, reader.GetSource(), reader.Line(), \
72 reader.LineNumber(), pos - reader.Line() )
73
74
76 m_progressReporter( nullptr )
77{
78 init( nullptr );
79}
80
81
83{
84 delete m_cache;
85}
86
87
88void SCH_SEXPR_PLUGIN::init( SCHEMATIC* aSchematic, const STRING_UTF8_MAP* aProperties )
89{
90 m_version = 0;
91 m_appending = false;
92 m_rootSheet = nullptr;
93 m_schematic = aSchematic;
94 m_cache = nullptr;
95 m_out = nullptr;
96 m_nextFreeFieldId = 100; // number arbitrarily > MANDATORY_FIELDS or SHEET_MANDATORY_FIELDS
97}
98
99
100SCH_SHEET* SCH_SEXPR_PLUGIN::Load( const wxString& aFileName, SCHEMATIC* aSchematic,
101 SCH_SHEET* aAppendToMe, const STRING_UTF8_MAP* aProperties )
102{
103 wxASSERT( !aFileName || aSchematic != nullptr );
104
105 LOCALE_IO toggle; // toggles on, then off, the C locale.
106 SCH_SHEET* sheet;
107
108 wxFileName fn = aFileName;
109
110 // Unfortunately child sheet file names the legacy schematic file format are not fully
111 // qualified and are always appended to the project path. The aFileName attribute must
112 // always be an absolute path so the project path can be used for load child sheet files.
113 wxASSERT( fn.IsAbsolute() );
114
115 if( aAppendToMe )
116 {
117 m_appending = true;
118 wxLogTrace( traceSchPlugin, "Append \"%s\" to sheet \"%s\".",
119 aFileName, aAppendToMe->GetFileName() );
120
121 wxFileName normedFn = aAppendToMe->GetFileName();
122
123 if( !normedFn.IsAbsolute() )
124 {
125 if( aFileName.Right( normedFn.GetFullPath().Length() ) == normedFn.GetFullPath() )
126 m_path = aFileName.Left( aFileName.Length() - normedFn.GetFullPath().Length() );
127 }
128
129 if( m_path.IsEmpty() )
130 m_path = aSchematic->Prj().GetProjectPath();
131
132 wxLogTrace( traceSchPlugin, "Normalized append path \"%s\".", m_path );
133 }
134 else
135 {
136 m_path = aSchematic->Prj().GetProjectPath();
137 }
138
139 m_currentPath.push( m_path );
140 init( aSchematic, aProperties );
141
142 if( aAppendToMe == nullptr )
143 {
144 // Clean up any allocated memory if an exception occurs loading the schematic.
145 std::unique_ptr<SCH_SHEET> newSheet = std::make_unique<SCH_SHEET>( aSchematic );
146
147 wxFileName relPath( aFileName );
148
149 // Do not use wxPATH_UNIX as option in MakeRelativeTo(). It can create incorrect
150 // relative paths on Windows, because paths have a disk identifier (C:, D: ...)
151 relPath.MakeRelativeTo( aSchematic->Prj().GetProjectPath() );
152
153 newSheet->SetFileName( relPath.GetFullPath() );
154 m_rootSheet = newSheet.get();
155 loadHierarchy( SCH_SHEET_PATH(), newSheet.get() );
156
157 // If we got here, the schematic loaded successfully.
158 sheet = newSheet.release();
159 m_rootSheet = nullptr; // Quiet Coverity warning.
160 }
161 else
162 {
163 wxCHECK_MSG( aSchematic->IsValid(), nullptr, "Can't append to a schematic with no root!" );
164 m_rootSheet = &aSchematic->Root();
165 sheet = aAppendToMe;
166 loadHierarchy( SCH_SHEET_PATH(), sheet );
167 }
168
169 wxASSERT( m_currentPath.size() == 1 ); // only the project path should remain
170
171 m_currentPath.pop(); // Clear the path stack for next call to Load
172
173 return sheet;
174}
175
176
177// Everything below this comment is recursive. Modify with care.
178
179void SCH_SEXPR_PLUGIN::loadHierarchy( const SCH_SHEET_PATH& aParentSheetPath, SCH_SHEET* aSheet )
180{
182
183 SCH_SCREEN* screen = nullptr;
184
185 if( !aSheet->GetScreen() )
186 {
187 // SCH_SCREEN objects store the full path and file name where the SCH_SHEET object only
188 // stores the file name and extension. Add the project path to the file name and
189 // extension to compare when calling SCH_SHEET::SearchHierarchy().
190 wxFileName fileName = aSheet->GetFileName();
191
192 if( !fileName.IsAbsolute() )
193 fileName.MakeAbsolute( m_currentPath.top() );
194
195 // Save the current path so that it gets restored when descending and ascending the
196 // sheet hierarchy which allows for sheet schematic files to be nested in folders
197 // relative to the last path a schematic was loaded from.
198 wxLogTrace( traceSchPlugin, "Saving path '%s'", m_currentPath.top() );
199 m_currentPath.push( fileName.GetPath() );
200 wxLogTrace( traceSchPlugin, "Current path '%s'", m_currentPath.top() );
201 wxLogTrace( traceSchPlugin, "Loading '%s'", fileName.GetFullPath() );
202
203 SCH_SHEET_PATH ancestorSheetPath = aParentSheetPath;
204
205 while( !ancestorSheetPath.empty() )
206 {
207 if( ancestorSheetPath.LastScreen()->GetFileName() == fileName.GetFullPath() )
208 {
209 if( !m_error.IsEmpty() )
210 m_error += "\n";
211
212 m_error += wxString::Format( _( "Could not load sheet '%s' because it already "
213 "appears as a direct ancestor in the schematic "
214 "hierarchy." ),
215 fileName.GetFullPath() );
216
217 fileName = wxEmptyString;
218
219 break;
220 }
221
222 ancestorSheetPath.pop_back();
223 }
224
225 if( ancestorSheetPath.empty() )
226 {
227 // Existing schematics could be either in the root sheet path or the current sheet
228 // load path so we have to check both.
229 if( !m_rootSheet->SearchHierarchy( fileName.GetFullPath(), &screen ) )
230 m_currentSheetPath.at( 0 )->SearchHierarchy( fileName.GetFullPath(), &screen );
231 }
232
233 if( screen )
234 {
235 aSheet->SetScreen( screen );
236 aSheet->GetScreen()->SetParent( m_schematic );
237 // Do not need to load the sub-sheets - this has already been done.
238 }
239 else
240 {
241 aSheet->SetScreen( new SCH_SCREEN( m_schematic ) );
242 aSheet->GetScreen()->SetFileName( fileName.GetFullPath() );
243
244 try
245 {
246 loadFile( fileName.GetFullPath(), aSheet );
247 }
248 catch( const IO_ERROR& ioe )
249 {
250 // If there is a problem loading the root sheet, there is no recovery.
251 if( aSheet == m_rootSheet )
252 throw;
253
254 // For all subsheets, queue up the error message for the caller.
255 if( !m_error.IsEmpty() )
256 m_error += "\n";
257
258 m_error += ioe.What();
259 }
260
261 if( fileName.FileExists() )
262 {
263 aSheet->GetScreen()->SetFileReadOnly( !fileName.IsFileWritable() );
264 aSheet->GetScreen()->SetFileExists( true );
265 }
266 else
267 {
268 aSheet->GetScreen()->SetFileReadOnly( !fileName.IsDirWritable() );
269 aSheet->GetScreen()->SetFileExists( false );
270 }
271
272 SCH_SHEET_PATH currentSheetPath = aParentSheetPath;
273 currentSheetPath.push_back( aSheet );
274
275 // This was moved out of the try{} block so that any sheet definitions that
276 // the plugin fully parsed before the exception was raised will be loaded.
277 for( SCH_ITEM* aItem : aSheet->GetScreen()->Items().OfType( SCH_SHEET_T ) )
278 {
279 wxCHECK2( aItem->Type() == SCH_SHEET_T, /* do nothing */ );
280 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( aItem );
281
282 // Recursion starts here.
283 loadHierarchy( currentSheetPath, sheet );
284 }
285 }
286
287 m_currentPath.pop();
288 wxLogTrace( traceSchPlugin, "Restoring path \"%s\"", m_currentPath.top() );
289 }
290
292}
293
294
295void SCH_SEXPR_PLUGIN::loadFile( const wxString& aFileName, SCH_SHEET* aSheet )
296{
297 FILE_LINE_READER reader( aFileName );
298
299 size_t lineCount = 0;
300
302 {
303 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
304
306 THROW_IO_ERROR( ( "Open cancelled by user." ) );
307
308 while( reader.ReadLine() )
309 lineCount++;
310
311 reader.Rewind();
312 }
313
314 SCH_SEXPR_PARSER parser( &reader, m_progressReporter, lineCount, m_rootSheet, m_appending );
315
316 parser.ParseSchematic( aSheet );
317}
318
319
320void SCH_SEXPR_PLUGIN::LoadContent( LINE_READER& aReader, SCH_SHEET* aSheet, int aFileVersion )
321{
322 wxCHECK( aSheet, /* void */ );
323
324 LOCALE_IO toggle;
325 SCH_SEXPR_PARSER parser( &aReader );
326
327 parser.ParseSchematic( aSheet, true, aFileVersion );
328}
329
330
331void SCH_SEXPR_PLUGIN::Save( const wxString& aFileName, SCH_SHEET* aSheet, SCHEMATIC* aSchematic,
332 const STRING_UTF8_MAP* aProperties )
333{
334 wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET object." );
335 wxCHECK_RET( !aFileName.IsEmpty(), "No schematic file name defined." );
336
337 LOCALE_IO toggle; // toggles on, then off, the C locale, to write floating point values.
338
339 init( aSchematic, aProperties );
340
341 wxFileName fn = aFileName;
342
343 // File names should be absolute. Don't assume everything relative to the project path
344 // works properly.
345 wxASSERT( fn.IsAbsolute() );
346
347 FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
348
349 m_out = &formatter; // no ownership
350
351 Format( aSheet );
352
353 if( aSheet->GetScreen() )
354 aSheet->GetScreen()->SetFileExists( true );
355}
356
357
359{
360 wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET* object." );
361 wxCHECK_RET( m_schematic != nullptr, "NULL SCHEMATIC* object." );
362
363 SCH_SCREEN* screen = aSheet->GetScreen();
364
365 wxCHECK( screen, /* void */ );
366
367 m_out->Print( 0, "(kicad_sch (version %d) (generator eeschema)\n\n",
369
370 m_out->Print( 1, "(uuid %s)\n\n", TO_UTF8( screen->m_uuid.AsString() ) );
371
372 screen->GetPageSettings().Format( m_out, 1, 0 );
373 m_out->Print( 0, "\n" );
374 screen->GetTitleBlock().Format( m_out, 1, 0 );
375
376 // Save cache library.
377 m_out->Print( 1, "(lib_symbols\n" );
378
379 for( std::pair<const wxString, LIB_SYMBOL*>& libSymbol : screen->GetLibSymbols() )
380 SCH_SEXPR_PLUGIN_CACHE::SaveSymbol( libSymbol.second, *m_out, 2, libSymbol.first );
381
382 m_out->Print( 1, ")\n\n" );
383
384 for( const std::shared_ptr<BUS_ALIAS>& alias : screen->GetBusAliases() )
385 saveBusAlias( alias, 1 );
386
387 // Enforce item ordering
388 auto cmp =
389 []( const SCH_ITEM* a, const SCH_ITEM* b )
390 {
391 if( a->Type() != b->Type() )
392 return a->Type() < b->Type();
393
394 return a->m_Uuid < b->m_Uuid;
395 };
396
397 std::multiset<SCH_ITEM*, decltype( cmp )> save_map( cmp );
398
399 for( SCH_ITEM* item : screen->Items() )
400 save_map.insert( item );
401
402 KICAD_T itemType = TYPE_NOT_INIT;
404
405 for( SCH_ITEM* item : save_map )
406 {
407 if( itemType != item->Type() )
408 {
409 itemType = item->Type();
410
411 if( itemType != SCH_SYMBOL_T
412 && itemType != SCH_JUNCTION_T
413 && itemType != SCH_SHEET_T )
414 {
415 m_out->Print( 0, "\n" );
416 }
417 }
418
419 switch( item->Type() )
420 {
421 case SCH_SYMBOL_T:
422 m_out->Print( 0, "\n" );
423 saveSymbol( static_cast<SCH_SYMBOL*>( item ), *m_schematic, 1, false );
424 break;
425
426 case SCH_BITMAP_T:
427 saveBitmap( static_cast<SCH_BITMAP*>( item ), 1 );
428 break;
429
430 case SCH_SHEET_T:
431 m_out->Print( 0, "\n" );
432 saveSheet( static_cast<SCH_SHEET*>( item ), 1 );
433 break;
434
435 case SCH_JUNCTION_T:
436 saveJunction( static_cast<SCH_JUNCTION*>( item ), 1 );
437 break;
438
439 case SCH_NO_CONNECT_T:
440 saveNoConnect( static_cast<SCH_NO_CONNECT*>( item ), 1 );
441 break;
442
445 saveBusEntry( static_cast<SCH_BUS_ENTRY_BASE*>( item ), 1 );
446 break;
447
448 case SCH_LINE_T:
449 if( layer != item->GetLayer() )
450 {
451 if( layer == SCH_LAYER_ID_START )
452 {
453 layer = item->GetLayer();
454 }
455 else
456 {
457 layer = item->GetLayer();
458 m_out->Print( 0, "\n" );
459 }
460 }
461
462 saveLine( static_cast<SCH_LINE*>( item ), 1 );
463 break;
464
465 case SCH_SHAPE_T:
466 saveShape( static_cast<SCH_SHAPE*>( item ), 1 );
467 break;
468
469 case SCH_TEXT_T:
470 case SCH_LABEL_T:
472 case SCH_HIER_LABEL_T:
474 saveText( static_cast<SCH_TEXT*>( item ), 1 );
475 break;
476
477 case SCH_TEXTBOX_T:
478 saveTextBox( static_cast<SCH_TEXTBOX*>( item ), 1 );
479 break;
480
481 default:
482 wxASSERT( "Unexpected schematic object type in SCH_SEXPR_PLUGIN::Format()" );
483 }
484 }
485
486 if( aSheet->HasRootInstance() )
487 {
488 std::vector< SCH_SHEET_INSTANCE> instances;
489
490 instances.emplace_back( aSheet->GetRootInstance() );
491 saveInstances( instances, 1 );
492 }
493
494 m_out->Print( 0, ")\n" );
495}
496
497
498void SCH_SEXPR_PLUGIN::Format( EE_SELECTION* aSelection, SCH_SHEET_PATH* aSelectionPath,
499 SCHEMATIC& aSchematic, OUTPUTFORMATTER* aFormatter,
500 bool aForClipboard )
501{
502 wxCHECK( aSelection && aSelectionPath && aFormatter, /* void */ );
503
504 LOCALE_IO toggle;
505 SCH_SHEET_LIST fullHierarchy = aSchematic.GetSheets();
506
507 m_schematic = &aSchematic;
508 m_out = aFormatter;
509
510 size_t i;
511 SCH_ITEM* item;
512 std::map<wxString, LIB_SYMBOL*> libSymbols;
513 SCH_SCREEN* screen = aSelection->GetScreen();
514
515 for( i = 0; i < aSelection->GetSize(); ++i )
516 {
517 item = dynamic_cast<SCH_ITEM*>( aSelection->GetItem( i ) );
518
519 wxCHECK2( item, continue );
520
521 if( item->Type() != SCH_SYMBOL_T )
522 continue;
523
524 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item );
525
526 wxCHECK2( symbol, continue );
527
528 wxString libSymbolLookup = symbol->GetLibId().Format().wx_str();
529
530 if( !symbol->UseLibIdLookup() )
531 libSymbolLookup = symbol->GetSchSymbolLibraryName();
532
533 auto it = screen->GetLibSymbols().find( libSymbolLookup );
534
535 if( it != screen->GetLibSymbols().end() )
536 libSymbols[ libSymbolLookup ] = it->second;
537 }
538
539 if( !libSymbols.empty() )
540 {
541 m_out->Print( 0, "(lib_symbols\n" );
542
543 for( const std::pair<const wxString, LIB_SYMBOL*>& libSymbol : libSymbols )
544 SCH_SEXPR_PLUGIN_CACHE::SaveSymbol( libSymbol.second, *m_out, 1, libSymbol.first );
545
546 m_out->Print( 0, ")\n\n" );
547 }
548
549 // Store the selected sheets instance information
550 SCH_SHEET_LIST selectedSheets;
551 SCH_REFERENCE_LIST selectedSymbols;
552
553 for( i = 0; i < aSelection->GetSize(); ++i )
554 {
555 item = (SCH_ITEM*) aSelection->GetItem( i );
556
557 switch( item->Type() )
558 {
559 case SCH_SYMBOL_T:
560 saveSymbol( static_cast<SCH_SYMBOL*>( item ), aSchematic, 0, aForClipboard );
561
562 aSelectionPath->AppendSymbol( selectedSymbols, static_cast<SCH_SYMBOL*>( item ),
563 true, true );
564 break;
565
566 case SCH_BITMAP_T:
567 saveBitmap( static_cast< SCH_BITMAP* >( item ), 0 );
568 break;
569
570 case SCH_SHEET_T:
571 saveSheet( static_cast< SCH_SHEET* >( item ), 0 );
572
573 {
574 SCH_SHEET_PATH subSheetPath = *aSelectionPath;
575 subSheetPath.push_back( static_cast<SCH_SHEET*>( item ) );
576
577 fullHierarchy.GetSheetsWithinPath( selectedSheets, subSheetPath );
578 fullHierarchy.GetSymbolsWithinPath( selectedSymbols, subSheetPath, true, true );
579 }
580
581 break;
582
583 case SCH_JUNCTION_T:
584 saveJunction( static_cast< SCH_JUNCTION* >( item ), 0 );
585 break;
586
587 case SCH_NO_CONNECT_T:
588 saveNoConnect( static_cast< SCH_NO_CONNECT* >( item ), 0 );
589 break;
590
593 saveBusEntry( static_cast< SCH_BUS_ENTRY_BASE* >( item ), 0 );
594 break;
595
596 case SCH_LINE_T:
597 saveLine( static_cast< SCH_LINE* >( item ), 0 );
598 break;
599
600 case SCH_SHAPE_T:
601 saveShape( static_cast<SCH_SHAPE*>( item ), 0 );
602 break;
603
604 case SCH_TEXT_T:
605 case SCH_LABEL_T:
607 case SCH_HIER_LABEL_T:
609 saveText( static_cast<SCH_TEXT*>( item ), 0 );
610 break;
611
612 case SCH_TEXTBOX_T:
613 saveTextBox( static_cast<SCH_TEXTBOX*>( item ), 0 );
614 break;
615
616 default:
617 wxASSERT( "Unexpected schematic object type in SCH_SEXPR_PLUGIN::Format()" );
618 }
619 }
620
621 // Make all instance information relative to the selection path
622 KIID_PATH selectionPath = aSelectionPath->Path();
623
624 selectedSheets.SortByPageNumbers();
625 std::vector<SCH_SHEET_INSTANCE> sheetinstances = selectedSheets.GetSheetInstances();
626
627 for( SCH_SHEET_INSTANCE& sheetInstance : sheetinstances )
628 {
629 wxASSERT_MSG( sheetInstance.m_Path.MakeRelativeTo( selectionPath ),
630 "Sheet is not inside the selection path?" );
631 }
632
633 selectionPath = aSelectionPath->Path();
634 selectedSymbols.SortByReferenceOnly();
635 std::vector<SCH_SYMBOL_INSTANCE> symbolInstances = selectedSymbols.GetSymbolInstances();
636
637 for( SCH_SYMBOL_INSTANCE& symbolInstance : symbolInstances )
638 {
639 wxASSERT_MSG( symbolInstance.m_Path.MakeRelativeTo( selectionPath ),
640 "Symbol is not inside the selection path?" );
641 }
642}
643
644
645void SCH_SEXPR_PLUGIN::saveSymbol( SCH_SYMBOL* aSymbol, const SCHEMATIC& aSchematic,
646 int aNestLevel, bool aForClipboard )
647{
648 wxCHECK_RET( aSymbol != nullptr && m_out != nullptr, "" );
649
650 // Sort symbol instance data to minimize file churn.
652
653 std::string libName;
654
655 wxString symbol_name = aSymbol->GetLibId().Format();
656
657 if( symbol_name.size() )
658 {
659 libName = toUTFTildaText( symbol_name );
660 }
661 else
662 {
663 libName = "_NONAME_";
664 }
665
667 int orientation = aSymbol->GetOrientation() & ~( SYM_MIRROR_X | SYM_MIRROR_Y );
668
669 if( orientation == SYM_ORIENT_90 )
670 angle = ANGLE_90;
671 else if( orientation == SYM_ORIENT_180 )
673 else if( orientation == SYM_ORIENT_270 )
675 else
676 angle = ANGLE_0;
677
678 m_out->Print( aNestLevel, "(symbol" );
679
680 if( !aSymbol->UseLibIdLookup() )
681 {
682 m_out->Print( 0, " (lib_name %s)",
683 m_out->Quotew( aSymbol->GetSchSymbolLibraryName() ).c_str() );
684 }
685
686 m_out->Print( 0, " (lib_id %s) (at %s %s %s)",
687 m_out->Quotew( aSymbol->GetLibId().Format().wx_str() ).c_str(),
689 aSymbol->GetPosition().x ).c_str(),
691 aSymbol->GetPosition().y ).c_str(),
693
694 bool mirrorX = aSymbol->GetOrientation() & SYM_MIRROR_X;
695 bool mirrorY = aSymbol->GetOrientation() & SYM_MIRROR_Y;
696
697 if( mirrorX || mirrorY )
698 {
699 m_out->Print( 0, " (mirror" );
700
701 if( mirrorX )
702 m_out->Print( 0, " x" );
703
704 if( mirrorY )
705 m_out->Print( 0, " y" );
706
707 m_out->Print( 0, ")" );
708 }
709
710 // The symbol unit is always set to the first instance regardless of the current sheet
711 // instance to prevent file churn.
712 int unit = ( aSymbol->GetInstanceReferences().size() == 0 ) ?
713 aSymbol->GetUnit() :
714 aSymbol->GetInstanceReferences()[0].m_Unit;
715
716 m_out->Print( 0, " (unit %d)", unit );
717
718 if( aSymbol->GetConvert() == LIB_ITEM::LIB_CONVERT::DEMORGAN )
719 m_out->Print( 0, " (convert %d)", aSymbol->GetConvert() );
720
721 m_out->Print( 0, "\n" );
722
723 m_out->Print( aNestLevel + 1, "(in_bom %s)", ( aSymbol->GetIncludeInBom() ) ? "yes" : "no" );
724 m_out->Print( 0, " (on_board %s)", ( aSymbol->GetIncludeOnBoard() ) ? "yes" : "no" );
725 m_out->Print( 0, " (dnp %s)", ( aSymbol->GetDNP() ) ? "yes" : "no" );
726
727 if( aSymbol->GetFieldsAutoplaced() != FIELDS_AUTOPLACED_NO )
728 m_out->Print( 0, " (fields_autoplaced)" );
729
730 m_out->Print( 0, "\n" );
731
732 m_out->Print( aNestLevel + 1, "(uuid %s)\n", TO_UTF8( aSymbol->m_Uuid.AsString() ) );
733
735
736 for( SCH_FIELD& field : aSymbol->GetFields() )
737 {
738 int id = field.GetId();
739 wxString value = field.GetText();
740
741 if( !aForClipboard && aSymbol->GetInstanceReferences().size() )
742 {
743 // The instance fields are always set to the default instance regardless of the
744 // sheet instance to prevent file churn.
745 if( id == REFERENCE_FIELD )
746 {
747 field.SetText( aSymbol->GetInstanceReferences()[0].m_Reference );
748 }
749 else if( id == VALUE_FIELD )
750 {
751 field.SetText( aSymbol->GetValueFieldText( false ) );
752 }
753 else if( id == FOOTPRINT_FIELD )
754 {
755 field.SetText( aSymbol->GetFootprintFieldText( false ) );
756 }
757 }
758
759 try
760 {
761 saveField( &field, aNestLevel + 1 );
762 }
763 catch( ... )
764 {
765 // Restore the changed field text on write error.
766 if( id == REFERENCE_FIELD || id == VALUE_FIELD || id == FOOTPRINT_FIELD )
767 field.SetText( value );
768
769 throw;
770 }
771
772 if( id == REFERENCE_FIELD || id == VALUE_FIELD || id == FOOTPRINT_FIELD )
773 field.SetText( value );
774 }
775
776 for( const std::unique_ptr<SCH_PIN>& pin : aSymbol->GetRawPins() )
777 {
778 if( pin->GetAlt().IsEmpty() )
779 {
780 m_out->Print( aNestLevel + 1, "(pin %s (uuid %s))\n",
781 m_out->Quotew( pin->GetNumber() ).c_str(),
782 TO_UTF8( pin->m_Uuid.AsString() ) );
783 }
784 else
785 {
786 m_out->Print( aNestLevel + 1, "(pin %s (uuid %s) (alternate %s))\n",
787 m_out->Quotew( pin->GetNumber() ).c_str(),
788 TO_UTF8( pin->m_Uuid.AsString() ),
789 m_out->Quotew( pin->GetAlt() ).c_str() );
790 }
791 }
792
793 if( !aSymbol->GetInstanceReferences().empty() )
794 {
795 m_out->Print( aNestLevel + 1, "(instances\n" );
796
797 KIID lastProjectUuid;
798 KIID rootSheetUuid = aSchematic.Root().m_Uuid;
799 SCH_SHEET_LIST fullHierarchy = aSchematic.GetSheets();
800 bool project_open = false;
801
802 for( size_t i = 0; i < aSymbol->GetInstanceReferences().size(); i++ )
803 {
804 // If the instance data is part of this design but no longer has an associated sheet
805 // path, don't save it. This prevents large amounts of orphaned instance data for the
806 // current project from accumulating in the schematic files.
807 //
808 // Keep all instance data when copying to the clipboard. It may be needed on paste.
809 if( !aForClipboard
810 && ( aSymbol->GetInstanceReferences()[i].m_Path[0] == rootSheetUuid )
811 && !fullHierarchy.GetSheetPathByKIIDPath( aSymbol->GetInstanceReferences()[i].m_Path ) )
812 {
813 if( project_open && ( ( i + 1 == aSymbol->GetInstanceReferences().size() )
814 || lastProjectUuid != aSymbol->GetInstanceReferences()[i+1].m_Path[0] ) )
815 {
816 m_out->Print( aNestLevel + 2, ")\n" ); // Closes `project`.
817 project_open = false;
818 }
819
820 continue;
821 }
822
823 if( lastProjectUuid != aSymbol->GetInstanceReferences()[i].m_Path[0] )
824 {
825 wxString projectName;
826
827 if( aSymbol->GetInstanceReferences()[i].m_Path[0] == rootSheetUuid )
828 projectName = aSchematic.Prj().GetProjectName();
829 else
830 projectName = aSymbol->GetInstanceReferences()[i].m_ProjectName;
831
832 lastProjectUuid = aSymbol->GetInstanceReferences()[i].m_Path[0];
833 m_out->Print( aNestLevel + 2, "(project %s\n",
834 m_out->Quotew( projectName ).c_str() );
835 project_open = true;
836 }
837
838 wxString path = aSymbol->GetInstanceReferences()[i].m_Path.AsString();
839
840 m_out->Print( aNestLevel + 3, "(path %s\n",
841 m_out->Quotew( path ).c_str() );
842 m_out->Print( aNestLevel + 4, "(reference %s) (unit %d)\n",
843 m_out->Quotew( aSymbol->GetInstanceReferences()[i].m_Reference ).c_str(),
844 aSymbol->GetInstanceReferences()[i].m_Unit );
845 m_out->Print( aNestLevel + 3, ")\n" );
846
847 if( project_open && ( ( i + 1 == aSymbol->GetInstanceReferences().size() )
848 || lastProjectUuid != aSymbol->GetInstanceReferences()[i+1].m_Path[0] ) )
849 {
850 m_out->Print( aNestLevel + 2, ")\n" ); // Closes `project`.
851 project_open = false;
852 }
853 }
854
855 m_out->Print( aNestLevel + 1, ")\n" ); // Closes `instances`.
856 }
857
858 m_out->Print( aNestLevel, ")\n" ); // Closes `symbol`.
859}
860
861
862void SCH_SEXPR_PLUGIN::saveField( SCH_FIELD* aField, int aNestLevel )
863{
864 wxCHECK_RET( aField != nullptr && m_out != nullptr, "" );
865
866 wxString fieldName = aField->GetCanonicalName();
867 // For some reason (bug in legacy parser?) the field ID for non-mandatory fields is -1 so
868 // check for this in order to correctly use the field name.
869
870 if( aField->GetId() == -1 /* undefined ID */ )
871 {
872 aField->SetId( m_nextFreeFieldId );
874 }
875 else if( aField->GetId() >= m_nextFreeFieldId )
876 {
877 m_nextFreeFieldId = aField->GetId() + 1;
878 }
879
880 m_out->Print( aNestLevel, "(property %s %s (at %s %s %s)",
881 m_out->Quotew( fieldName ).c_str(),
882 m_out->Quotew( aField->GetText() ).c_str(),
884 aField->GetPosition().x ).c_str(),
886 aField->GetPosition().y ).c_str(),
887 EDA_UNIT_UTILS::FormatAngle( aField->GetTextAngle() ).c_str() );
888
889 if( aField->IsNameShown() )
890 m_out->Print( 0, " (show_name)" );
891
892 if( !aField->CanAutoplace() )
893 m_out->Print( 0, " (do_not_autoplace)" );
894
895 if( !aField->IsDefaultFormatting()
896 || ( aField->GetTextHeight() != schIUScale.MilsToIU( DEFAULT_SIZE_TEXT ) ) )
897 {
898 m_out->Print( 0, "\n" );
899 aField->Format( m_out, aNestLevel, 0 );
900 m_out->Print( aNestLevel, ")\n" ); // Closes property token with font effects.
901 }
902 else
903 {
904 m_out->Print( 0, ")\n" ); // Closes property token without font effects.
905 }
906}
907
908
909void SCH_SEXPR_PLUGIN::saveBitmap( SCH_BITMAP* aBitmap, int aNestLevel )
910{
911 wxCHECK_RET( aBitmap != nullptr && m_out != nullptr, "" );
912
913 const wxImage* image = aBitmap->GetImage()->GetImageData();
914
915 wxCHECK_RET( image != nullptr, "wxImage* is NULL" );
916
917 m_out->Print( aNestLevel, "(image (at %s %s)",
919 aBitmap->GetPosition().x ).c_str(),
921 aBitmap->GetPosition().y ).c_str() );
922
923 if( aBitmap->GetImage()->GetScale() != 1.0 )
924 m_out->Print( 0, " (scale %g)", aBitmap->GetImage()->GetScale() );
925
926 m_out->Print( 0, "\n" );
927
928 m_out->Print( aNestLevel + 1, "(uuid %s)\n", TO_UTF8( aBitmap->m_Uuid.AsString() ) );
929
930 m_out->Print( aNestLevel + 1, "(data" );
931
932 wxMemoryOutputStream stream;
933
934 image->SaveFile( stream, wxBITMAP_TYPE_PNG );
935
936 // Write binary data in hexadecimal form (ASCII)
937 wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
938 wxString out = wxBase64Encode( buffer->GetBufferStart(), buffer->GetBufferSize() );
939
940 // Apparently the MIME standard character width for base64 encoding is 76 (unconfirmed)
941 // so use it in a vein attempt to be standard like.
942#define MIME_BASE64_LENGTH 76
943
944 size_t first = 0;
945
946 while( first < out.Length() )
947 {
948 m_out->Print( 0, "\n" );
949 m_out->Print( aNestLevel + 2, "%s", TO_UTF8( out( first, MIME_BASE64_LENGTH ) ) );
950 first += MIME_BASE64_LENGTH;
951 }
952
953 m_out->Print( 0, "\n" );
954 m_out->Print( aNestLevel + 1, ")\n" ); // Closes data token.
955 m_out->Print( aNestLevel, ")\n" ); // Closes image token.
956}
957
958
959void SCH_SEXPR_PLUGIN::saveSheet( SCH_SHEET* aSheet, int aNestLevel )
960{
961 wxCHECK_RET( aSheet != nullptr && m_out != nullptr, "" );
962
963 m_out->Print( aNestLevel, "(sheet (at %s %s) (size %s %s)",
965 aSheet->GetPosition().x ).c_str(),
967 aSheet->GetPosition().y ).c_str(),
969 aSheet->GetSize().GetWidth() ).c_str(),
971 aSheet->GetSize().GetHeight() ).c_str() );
972
974 m_out->Print( 0, " (fields_autoplaced)" );
975
976 m_out->Print( 0, "\n" );
977
979 aSheet->GetBorderColor() );
980
981 stroke.SetWidth( aSheet->GetBorderWidth() );
982 stroke.Format( m_out, schIUScale, aNestLevel + 1 );
983
984 m_out->Print( 0, "\n" );
985
986 m_out->Print( aNestLevel + 1, "(fill (color %d %d %d %0.4f))\n",
987 KiROUND( aSheet->GetBackgroundColor().r * 255.0 ),
988 KiROUND( aSheet->GetBackgroundColor().g * 255.0 ),
989 KiROUND( aSheet->GetBackgroundColor().b * 255.0 ),
990 aSheet->GetBackgroundColor().a );
991
992 m_out->Print( aNestLevel + 1, "(uuid %s)\n", TO_UTF8( aSheet->m_Uuid.AsString() ) );
993
995
996 for( SCH_FIELD& field : aSheet->GetFields() )
997 {
998 saveField( &field, aNestLevel + 1 );
999 }
1000
1001 for( const SCH_SHEET_PIN* pin : aSheet->GetPins() )
1002 {
1003 m_out->Print( aNestLevel + 1, "(pin %s %s (at %s %s %s)\n",
1004 EscapedUTF8( pin->GetText() ).c_str(),
1005 getSheetPinShapeToken( pin->GetShape() ),
1007 pin->GetPosition().x ).c_str(),
1009 pin->GetPosition().y ).c_str(),
1010 EDA_UNIT_UTILS::FormatAngle( getSheetPinAngle( pin->GetSide() ) ).c_str() );
1011
1012 pin->Format( m_out, aNestLevel + 1, 0 );
1013
1014 m_out->Print( aNestLevel + 2, "(uuid %s)\n", TO_UTF8( pin->m_Uuid.AsString() ) );
1015
1016 m_out->Print( aNestLevel + 1, ")\n" ); // Closes pin token.
1017 }
1018
1019 // Save all sheet instances here except the root sheet instance.
1020 std::vector< SCH_SHEET_INSTANCE > sheetInstances = aSheet->GetInstances();
1021
1022 auto it = sheetInstances.begin();
1023
1024 while( it != sheetInstances.end() )
1025 {
1026 if( it->m_Path.size() == 0 )
1027 it = sheetInstances.erase( it );
1028 else
1029 it++;
1030 }
1031
1032 if( !sheetInstances.empty() )
1033 {
1034 m_out->Print( aNestLevel + 1, "(instances\n" );
1035
1036 KIID lastProjectUuid;
1037 KIID rootSheetUuid = m_schematic->Root().m_Uuid;
1038 SCH_SHEET_LIST fullHierarchy = m_schematic->GetSheets();
1039 bool project_open = false;
1040
1041 for( size_t i = 0; i < sheetInstances.size(); i++ )
1042 {
1043 // If the instance data is part of this design but no longer has an associated sheet
1044 // path, don't save it. This prevents large amounts of orphaned instance data for the
1045 // current project from accumulating in the schematic files.
1046 //
1047 // Keep all instance data when copying to the clipboard. It may be needed on paste.
1048 if( ( sheetInstances[i].m_Path[0] == rootSheetUuid )
1049 && !fullHierarchy.GetSheetPathByKIIDPath( sheetInstances[i].m_Path, false ) )
1050 {
1051 if( project_open && ( ( i + 1 == sheetInstances.size() )
1052 || lastProjectUuid != sheetInstances[i+1].m_Path[0] ) )
1053 {
1054 m_out->Print( aNestLevel + 2, ")\n" ); // Closes `project` token.
1055 project_open = false;
1056 }
1057
1058 continue;
1059 }
1060
1061 if( lastProjectUuid != sheetInstances[i].m_Path[0] )
1062 {
1063 wxString projectName;
1064
1065 if( sheetInstances[i].m_Path[0] == rootSheetUuid )
1066 projectName = m_schematic->Prj().GetProjectName();
1067 else
1068 projectName = sheetInstances[i].m_ProjectName;
1069
1070 lastProjectUuid = sheetInstances[i].m_Path[0];
1071 m_out->Print( aNestLevel + 2, "(project %s\n",
1072 m_out->Quotew( projectName ).c_str() );
1073 project_open = true;
1074 }
1075
1076 wxString path = sheetInstances[i].m_Path.AsString();
1077
1078 m_out->Print( aNestLevel + 3, "(path %s (page %s))\n",
1079 m_out->Quotew( path ).c_str(),
1080 m_out->Quotew( sheetInstances[i].m_PageNumber ).c_str() );
1081
1082 if( project_open && ( ( i + 1 == sheetInstances.size() )
1083 || lastProjectUuid != sheetInstances[i+1].m_Path[0] ) )
1084 {
1085 m_out->Print( aNestLevel + 2, ")\n" ); // Closes `project` token.
1086 project_open = false;
1087 }
1088 }
1089
1090 m_out->Print( aNestLevel + 1, ")\n" ); // Closes `instances` token.
1091 }
1092
1093 m_out->Print( aNestLevel, ")\n" ); // Closes sheet token.
1094}
1095
1096
1097void SCH_SEXPR_PLUGIN::saveJunction( SCH_JUNCTION* aJunction, int aNestLevel )
1098{
1099 wxCHECK_RET( aJunction != nullptr && m_out != nullptr, "" );
1100
1101 m_out->Print( aNestLevel, "(junction (at %s %s) (diameter %s) (color %d %d %d %s)\n",
1103 aJunction->GetPosition().x ).c_str(),
1105 aJunction->GetPosition().y ).c_str(),
1107 aJunction->GetDiameter() ).c_str(),
1108 KiROUND( aJunction->GetColor().r * 255.0 ),
1109 KiROUND( aJunction->GetColor().g * 255.0 ),
1110 KiROUND( aJunction->GetColor().b * 255.0 ),
1111 FormatDouble2Str( aJunction->GetColor().a ).c_str() );
1112
1113 m_out->Print( aNestLevel + 1, "(uuid %s)\n", TO_UTF8( aJunction->m_Uuid.AsString() ) );
1114
1115 m_out->Print( aNestLevel, ")\n" );
1116}
1117
1118
1119void SCH_SEXPR_PLUGIN::saveNoConnect( SCH_NO_CONNECT* aNoConnect, int aNestLevel )
1120{
1121 wxCHECK_RET( aNoConnect != nullptr && m_out != nullptr, "" );
1122
1123 m_out->Print( aNestLevel, "(no_connect (at %s %s) (uuid %s))\n",
1125 aNoConnect->GetPosition().x ).c_str(),
1127 aNoConnect->GetPosition().y ).c_str(),
1128 TO_UTF8( aNoConnect->m_Uuid.AsString() ) );
1129}
1130
1131
1132void SCH_SEXPR_PLUGIN::saveBusEntry( SCH_BUS_ENTRY_BASE* aBusEntry, int aNestLevel )
1133{
1134 wxCHECK_RET( aBusEntry != nullptr && m_out != nullptr, "" );
1135
1136 // Bus to bus entries are converted to bus line segments.
1137 if( aBusEntry->GetClass() == "SCH_BUS_BUS_ENTRY" )
1138 {
1139 SCH_LINE busEntryLine( aBusEntry->GetPosition(), LAYER_BUS );
1140
1141 busEntryLine.SetEndPoint( aBusEntry->GetEnd() );
1142 saveLine( &busEntryLine, aNestLevel );
1143 }
1144 else
1145 {
1146 m_out->Print( aNestLevel, "(bus_entry (at %s %s) (size %s %s)\n",
1148 aBusEntry->GetPosition().x ).c_str(),
1150 aBusEntry->GetPosition().y ).c_str(),
1152 aBusEntry->GetSize().GetWidth() ).c_str(),
1154 aBusEntry->GetSize().GetHeight() ).c_str() );
1155
1156 aBusEntry->GetStroke().Format( m_out, schIUScale, aNestLevel + 1 );
1157
1158 m_out->Print( 0, "\n" );
1159
1160 m_out->Print( aNestLevel + 1, "(uuid %s)\n", TO_UTF8( aBusEntry->m_Uuid.AsString() ) );
1161
1162 m_out->Print( aNestLevel, ")\n" );
1163 }
1164}
1165
1166
1167void SCH_SEXPR_PLUGIN::saveShape( SCH_SHAPE* aShape, int aNestLevel )
1168{
1169 wxCHECK_RET( aShape != nullptr && m_out != nullptr, "" );
1170
1171 switch( aShape->GetShape() )
1172 {
1173 case SHAPE_T::ARC:
1174 formatArc( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1175 aShape->GetFillColor(), aShape->m_Uuid );
1176 break;
1177
1178 case SHAPE_T::CIRCLE:
1179 formatCircle( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1180 aShape->GetFillColor(), aShape->m_Uuid );
1181 break;
1182
1183 case SHAPE_T::RECT:
1184 formatRect( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1185 aShape->GetFillColor(), aShape->m_Uuid );
1186 break;
1187
1188 case SHAPE_T::BEZIER:
1189 formatBezier( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1190 aShape->GetFillColor(), aShape->m_Uuid );
1191 break;
1192
1193 case SHAPE_T::POLY:
1194 formatPoly( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1195 aShape->GetFillColor(), aShape->m_Uuid );
1196 break;
1197
1198 default:
1200 }
1201}
1202
1203
1204void SCH_SEXPR_PLUGIN::saveLine( SCH_LINE* aLine, int aNestLevel )
1205{
1206 wxCHECK_RET( aLine != nullptr && m_out != nullptr, "" );
1207
1208 wxString lineType;
1209
1210 STROKE_PARAMS line_stroke = aLine->GetStroke();
1211
1212 switch( aLine->GetLayer() )
1213 {
1214 case LAYER_BUS: lineType = "bus"; break;
1215 case LAYER_WIRE: lineType = "wire"; break;
1216 case LAYER_NOTES: lineType = "polyline"; break;
1217 default:
1218 UNIMPLEMENTED_FOR( LayerName( aLine->GetLayer() ) );
1219 }
1220
1221 m_out->Print( aNestLevel, "(%s (pts (xy %s %s) (xy %s %s))\n",
1222 TO_UTF8( lineType ),
1224 aLine->GetStartPoint().x ).c_str(),
1226 aLine->GetStartPoint().y ).c_str(),
1228 aLine->GetEndPoint().x ).c_str(),
1230 aLine->GetEndPoint().y ).c_str() );
1231
1232 line_stroke.Format( m_out, schIUScale, aNestLevel + 1 );
1233 m_out->Print( 0, "\n" );
1234
1235 m_out->Print( aNestLevel + 1, "(uuid %s)\n", TO_UTF8( aLine->m_Uuid.AsString() ) );
1236
1237 m_out->Print( aNestLevel, ")\n" );
1238}
1239
1240
1241void SCH_SEXPR_PLUGIN::saveText( SCH_TEXT* aText, int aNestLevel )
1242{
1243 wxCHECK_RET( aText != nullptr && m_out != nullptr, "" );
1244
1245 // Note: label is nullptr SCH_TEXT, but not for SCH_LABEL_XXX,
1246 SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( aText );
1247
1248 m_out->Print( aNestLevel, "(%s %s",
1249 getTextTypeToken( aText->Type() ),
1250 m_out->Quotew( aText->GetText() ).c_str() );
1251
1252 if( aText->Type() == SCH_DIRECTIVE_LABEL_T )
1253 {
1254 SCH_DIRECTIVE_LABEL* flag = static_cast<SCH_DIRECTIVE_LABEL*>( aText );
1255
1256 m_out->Print( 0, " (length %s)",
1258 flag->GetPinLength() ).c_str() );
1259 }
1260
1261 EDA_ANGLE angle = aText->GetTextAngle();
1262
1263 if( label )
1264 {
1265 if( aText->Type() == SCH_GLOBAL_LABEL_T
1266 || aText->Type() == SCH_HIER_LABEL_T
1267 || aText->Type() == SCH_DIRECTIVE_LABEL_T )
1268 {
1269 m_out->Print( 0, " (shape %s)", getSheetPinShapeToken( label->GetShape() ) );
1270 }
1271
1272 // The angle of the text is always 0 or 90 degrees for readibility reasons,
1273 // but the item itself can have more rotation (-90 and 180 deg)
1274 switch( aText->GetTextSpinStyle() )
1275 {
1276 default:
1277 case TEXT_SPIN_STYLE::LEFT: angle += ANGLE_180; break;
1278 case TEXT_SPIN_STYLE::UP: break;
1279 case TEXT_SPIN_STYLE::RIGHT: break;
1280 case TEXT_SPIN_STYLE::BOTTOM: angle += ANGLE_180; break;
1281 }
1282 }
1283
1284 if( aText->GetText().Length() < 50 )
1285 {
1286 m_out->Print( 0, " (at %s %s %s)",
1288 aText->GetPosition().x ).c_str(),
1290 aText->GetPosition().y ).c_str(),
1292 }
1293 else
1294 {
1295 m_out->Print( 0, "\n" );
1296 m_out->Print( aNestLevel + 1, "(at %s %s %s)",
1298 aText->GetPosition().x ).c_str(),
1300 aText->GetPosition().y ).c_str(),
1302 }
1303
1305 m_out->Print( 0, " (fields_autoplaced)" );
1306
1307 m_out->Print( 0, "\n" );
1308 aText->EDA_TEXT::Format( m_out, aNestLevel, 0 );
1309
1310 m_out->Print( aNestLevel + 1, "(uuid %s)\n", TO_UTF8( aText->m_Uuid.AsString() ) );
1311
1312 if( label )
1313 {
1314 for( SCH_FIELD& field : label->GetFields() )
1315 saveField( &field, aNestLevel + 1 );
1316 }
1317
1318 m_out->Print( aNestLevel, ")\n" ); // Closes text token.
1319}
1320
1321
1322void SCH_SEXPR_PLUGIN::saveTextBox( SCH_TEXTBOX* aTextBox, int aNestLevel )
1323{
1324 wxCHECK_RET( aTextBox != nullptr && m_out != nullptr, "" );
1325
1326 m_out->Print( aNestLevel, "(text_box %s\n",
1327 m_out->Quotew( aTextBox->GetText() ).c_str() );
1328
1329 VECTOR2I pos = aTextBox->GetStart();
1330 VECTOR2I size = aTextBox->GetEnd() - pos;
1331
1332 m_out->Print( aNestLevel + 1, "(at %s %s %s) (size %s %s)\n",
1335 EDA_UNIT_UTILS::FormatAngle( aTextBox->GetTextAngle() ).c_str(),
1338
1339 aTextBox->GetStroke().Format( m_out, schIUScale, aNestLevel + 1 );
1340 m_out->Print( 0, "\n" );
1341 formatFill( m_out, aNestLevel + 1, aTextBox->GetFillMode(), aTextBox->GetFillColor() );
1342 m_out->Print( 0, "\n" );
1343
1344 aTextBox->EDA_TEXT::Format( m_out, aNestLevel, 0 );
1345
1346 if( aTextBox->m_Uuid != niluuid )
1347 m_out->Print( aNestLevel + 1, "(uuid %s)\n", TO_UTF8( aTextBox->m_Uuid.AsString() ) );
1348
1349 m_out->Print( aNestLevel, ")\n" );
1350}
1351
1352
1353void SCH_SEXPR_PLUGIN::saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias, int aNestLevel )
1354{
1355 wxCHECK_RET( aAlias != nullptr, "BUS_ALIAS* is NULL" );
1356
1357 wxString members;
1358
1359 for( const wxString& member : aAlias->Members() )
1360 {
1361 if( !members.IsEmpty() )
1362 members += wxS( " " );
1363
1364 members += m_out->Quotew( member );
1365 }
1366
1367 m_out->Print( aNestLevel, "(bus_alias %s (members %s))\n",
1368 m_out->Quotew( aAlias->GetName() ).c_str(),
1369 TO_UTF8( members ) );
1370}
1371
1372
1373void SCH_SEXPR_PLUGIN::saveInstances( const std::vector<SCH_SHEET_INSTANCE>& aInstances,
1374 int aNestLevel )
1375{
1376 if( aInstances.size() )
1377 {
1378 m_out->Print( 0, "\n" );
1379 m_out->Print( aNestLevel, "(sheet_instances\n" );
1380
1381 for( const SCH_SHEET_INSTANCE& instance : aInstances )
1382 {
1383 wxString path = instance.m_Path.AsString();
1384
1385 if( path.IsEmpty() )
1386 path = wxT( "/" ); // Root path
1387
1388 m_out->Print( aNestLevel + 1, "(path %s (page %s))\n",
1389 m_out->Quotew( path ).c_str(),
1390 m_out->Quotew( instance.m_PageNumber ).c_str() );
1391 }
1392
1393 m_out->Print( aNestLevel, ")\n" ); // Close sheet instances token.
1394 }
1395}
1396
1397
1398void SCH_SEXPR_PLUGIN::cacheLib( const wxString& aLibraryFileName,
1399 const STRING_UTF8_MAP* aProperties )
1400{
1401 if( !m_cache || !m_cache->IsFile( aLibraryFileName ) || m_cache->IsFileChanged() )
1402 {
1403 // a spectacular episode in memory management:
1404 delete m_cache;
1405 m_cache = new SCH_SEXPR_PLUGIN_CACHE( aLibraryFileName );
1406
1407 if( !isBuffering( aProperties ) )
1408 m_cache->Load();
1409 }
1410}
1411
1412
1414{
1415 return ( aProperties && aProperties->Exists( SCH_SEXPR_PLUGIN::PropBuffering ) );
1416}
1417
1418
1420{
1421 if( m_cache )
1422 return m_cache->GetModifyHash();
1423
1424 // If the cache hasn't been loaded, it hasn't been modified.
1425 return 0;
1426}
1427
1428
1429void SCH_SEXPR_PLUGIN::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
1430 const wxString& aLibraryPath,
1431 const STRING_UTF8_MAP* aProperties )
1432{
1433 LOCALE_IO toggle; // toggles on, then off, the C locale.
1434
1435 bool powerSymbolsOnly = ( aProperties &&
1436 aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
1437
1438 cacheLib( aLibraryPath, aProperties );
1439
1440 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
1441
1442 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
1443 {
1444 if( !powerSymbolsOnly || it->second->IsPower() )
1445 aSymbolNameList.Add( it->first );
1446 }
1447}
1448
1449
1450void SCH_SEXPR_PLUGIN::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
1451 const wxString& aLibraryPath,
1452 const STRING_UTF8_MAP* aProperties )
1453{
1454 LOCALE_IO toggle; // toggles on, then off, the C locale.
1455
1456 bool powerSymbolsOnly = ( aProperties &&
1457 aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
1458
1459 cacheLib( aLibraryPath, aProperties );
1460
1461 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
1462
1463 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
1464 {
1465 if( !powerSymbolsOnly || it->second->IsPower() )
1466 aSymbolList.push_back( it->second );
1467 }
1468}
1469
1470
1471LIB_SYMBOL* SCH_SEXPR_PLUGIN::LoadSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
1472 const STRING_UTF8_MAP* aProperties )
1473{
1474 LOCALE_IO toggle; // toggles on, then off, the C locale.
1475
1476 cacheLib( aLibraryPath, aProperties );
1477
1478 LIB_SYMBOL_MAP::const_iterator it = m_cache->m_symbols.find( aSymbolName );
1479
1480 if( it == m_cache->m_symbols.end() )
1481 return nullptr;
1482
1483 return it->second;
1484}
1485
1486
1487void SCH_SEXPR_PLUGIN::SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
1488 const STRING_UTF8_MAP* aProperties )
1489{
1490 LOCALE_IO toggle; // toggles on, then off, the C locale.
1491
1492 cacheLib( aLibraryPath, aProperties );
1493
1494 m_cache->AddSymbol( aSymbol );
1495
1496 if( !isBuffering( aProperties ) )
1497 m_cache->Save();
1498}
1499
1500
1501void SCH_SEXPR_PLUGIN::DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
1502 const STRING_UTF8_MAP* aProperties )
1503{
1504 LOCALE_IO toggle; // toggles on, then off, the C locale.
1505
1506 cacheLib( aLibraryPath, aProperties );
1507
1508 m_cache->DeleteSymbol( aSymbolName );
1509
1510 if( !isBuffering( aProperties ) )
1511 m_cache->Save();
1512}
1513
1514
1515void SCH_SEXPR_PLUGIN::CreateSymbolLib( const wxString& aLibraryPath,
1516 const STRING_UTF8_MAP* aProperties )
1517{
1518 if( wxFileExists( aLibraryPath ) )
1519 {
1520 THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' already exists." ),
1521 aLibraryPath.GetData() ) );
1522 }
1523
1524 LOCALE_IO toggle;
1525
1526 delete m_cache;
1527 m_cache = new SCH_SEXPR_PLUGIN_CACHE( aLibraryPath );
1529 m_cache->Save();
1530 m_cache->Load(); // update m_writable and m_mod_time
1531}
1532
1533
1534bool SCH_SEXPR_PLUGIN::DeleteSymbolLib( const wxString& aLibraryPath,
1535 const STRING_UTF8_MAP* aProperties )
1536{
1537 wxFileName fn = aLibraryPath;
1538
1539 if( !fn.FileExists() )
1540 return false;
1541
1542 // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
1543 // we don't want that. we want bare metal portability with no UI here.
1544 if( wxRemove( aLibraryPath ) )
1545 {
1546 THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' cannot be deleted." ),
1547 aLibraryPath.GetData() ) );
1548 }
1549
1550 if( m_cache && m_cache->IsFile( aLibraryPath ) )
1551 {
1552 delete m_cache;
1553 m_cache = nullptr;
1554 }
1555
1556 return true;
1557}
1558
1559
1560void SCH_SEXPR_PLUGIN::SaveLibrary( const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties )
1561{
1562 if( !m_cache )
1563 m_cache = new SCH_SEXPR_PLUGIN_CACHE( aLibraryPath );
1564
1565 wxString oldFileName = m_cache->GetFileName();
1566
1567 if( !m_cache->IsFile( aLibraryPath ) )
1568 {
1569 m_cache->SetFileName( aLibraryPath );
1570 }
1571
1572 // This is a forced save.
1574 m_cache->Save();
1575 m_cache->SetFileName( oldFileName );
1576}
1577
1578
1579bool SCH_SEXPR_PLUGIN::CheckHeader( const wxString& aFileName )
1580{
1581 // Open file and check first line
1582 wxTextFile tempFile;
1583
1584 tempFile.Open( aFileName );
1585 wxString firstline;
1586 // read the first line
1587 firstline = tempFile.GetFirstLine();
1588 tempFile.Close();
1589
1590 return firstline.StartsWith( wxS( "EESchema" ) );
1591}
1592
1593
1594bool SCH_SEXPR_PLUGIN::IsSymbolLibWritable( const wxString& aLibraryPath )
1595{
1596 wxFileName fn( aLibraryPath );
1597
1598 return ( fn.FileExists() && fn.IsFileWritable() ) || fn.IsDirWritable();
1599}
1600
1601
1602void SCH_SEXPR_PLUGIN::GetAvailableSymbolFields( std::vector<wxString>& aNames )
1603{
1604 if( !m_cache )
1605 return;
1606
1607 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
1608
1609 std::set<wxString> fieldNames;
1610
1611 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
1612 {
1613 std::vector<LIB_FIELD*> fields;
1614 it->second->GetFields( fields );
1615
1616 for( LIB_FIELD* field : fields )
1617 {
1618 if( field->IsMandatory() )
1619 continue;
1620
1621 // TODO(JE): enable configurability of this outside database libraries?
1622 // if( field->ShowInChooser() )
1623 fieldNames.insert( field->GetName() );
1624 }
1625 }
1626
1627 std::copy( fieldNames.begin(), fieldNames.end(), std::back_inserter( aNames ) );
1628}
1629
1630
1631void SCH_SEXPR_PLUGIN::GetDefaultSymbolFields( std::vector<wxString>& aNames )
1632{
1633 GetAvailableSymbolFields( aNames );
1634}
1635
1636
1638{
1639 LOCALE_IO toggle; // toggles on, then off, the C locale.
1640 LIB_SYMBOL_MAP map;
1641 SCH_SEXPR_PARSER parser( &aReader );
1642
1643 parser.NeedLEFT();
1644 parser.NextTok();
1645
1646 return parser.ParseSymbol( map, aFileVersion );
1647}
1648
1649
1651{
1652
1653 LOCALE_IO toggle; // toggles on, then off, the C locale.
1654 SCH_SEXPR_PLUGIN_CACHE::SaveSymbol( symbol, formatter );
1655}
1656
1657
1658const char* SCH_SEXPR_PLUGIN::PropBuffering = "buffering";
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:111
double GetScale() const
Definition: bitmap_base.h:78
wxImage * GetImageData()
Definition: bitmap_base.h:71
const KIID m_Uuid
Definition: eda_item.h:492
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:113
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:145
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:120
COLOR4D GetFillColor() const
Definition: eda_shape.h:105
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:75
int GetTextHeight() const
Definition: eda_text.h:202
bool IsDefaultFormatting() const
Definition: eda_text.cpp:766
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:120
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:87
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:781
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:173
void Rewind()
Rewind the file and resets the line number back to zero.
Definition: richio.h:222
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:219
Used for text file output.
Definition: richio.h:457
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
double r
Red component.
Definition: color4d.h:390
double g
Green component.
Definition: color4d.h:391
double a
Alpha component.
Definition: color4d.h:393
double b
Blue component.
Definition: color4d.h:392
Definition: kiid.h:48
wxString AsString() const
Definition: kiid.cpp:257
Field object used in symbol libraries.
Definition: lib_field.h:61
UTF8 Format() const
Definition: lib_id.cpp:117
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:81
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:41
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:310
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:501
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:433
void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the page class to aFormatter in s-expression form.
Definition: page_info.cpp:272
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:126
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:132
Holds all the data relating to one schematic.
Definition: schematic.h:61
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:86
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition: schematic.h:107
SCH_SHEET & Root() const
Definition: schematic.h:91
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Definition: schematic.h:76
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:136
BITMAP_BASE * GetImage() const
Definition: sch_bitmap.h:54
Base class for a bus or wire entry.
Definition: sch_bus_entry.h:38
VECTOR2I GetPosition() const override
virtual STROKE_PARAMS GetStroke() const override
Definition: sch_bus_entry.h:77
VECTOR2I GetEnd() const
wxSize GetSize() const
Definition: sch_bus_entry.h:71
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
VECTOR2I GetPosition() const override
Definition: sch_field.cpp:1061
bool IsNameShown() const
Definition: sch_field.h:158
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:845
int GetId() const
Definition: sch_field.h:125
bool CanAutoplace() const
Definition: sch_field.h:161
void SetId(int aId)
Definition: sch_field.cpp:138
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:147
virtual wxString GetClass() const override
Return the class name.
Definition: sch_item.h:157
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition: sch_item.h:246
FIELDS_AUTOPLACED GetFieldsAutoplaced() const
Return whether the fields have been automatically placed.
Definition: sch_item.h:424
COLOR4D GetColor() const
Definition: sch_junction.h:114
int GetDiameter() const
Definition: sch_junction.h:109
VECTOR2I GetPosition() const override
Definition: sch_junction.h:102
LABEL_FLAG_SHAPE GetShape() const override
Definition: sch_label.h:73
std::vector< SCH_FIELD > & GetFields()
Definition: sch_label.h:90
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:177
VECTOR2I GetEndPoint() const
Definition: sch_line.h:143
VECTOR2I GetStartPoint() const
Definition: sch_line.h:138
void SetEndPoint(const VECTOR2I &aPosition)
Definition: sch_line.h:144
VECTOR2I GetPosition() const override
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
void SortByReferenceOnly()
Sort the list of references by reference.
std::vector< SCH_SYMBOL_INSTANCE > GetSymbolInstances() const
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:131
std::map< wxString, LIB_SYMBOL * > & GetLibSymbols()
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:110
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:155
KIID m_uuid
A unique identifier for each schematic file.
Definition: sch_screen.h:642
void SetFileReadOnly(bool aIsReadOnly)
Definition: sch_screen.h:146
std::set< std::shared_ptr< BUS_ALIAS > > GetBusAliases() const
Return a list of bus aliases defined in this screen.
Definition: sch_screen.h:511
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)
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.
void saveText(SCH_TEXT *aText, int aNestLevel)
static LIB_SYMBOL * ParseLibSymbol(LINE_READER &aReader, int aVersion=SEXPR_SCHEMATIC_FILE_VERSION)
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 saveSymbol(SCH_SYMBOL *aSymbol, const SCHEMATIC &aSchematic, int aNestLevel, bool aForClipboard)
void saveSheet(SCH_SHEET *aSheet, int aNestLevel)
SCH_SHEET * Load(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,...
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)
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)
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)
bool CheckHeader(const wxString &aFileName) override
Return true if the first line in aFileName begins with the expected header.
void saveBusEntry(SCH_BUS_ENTRY_BASE *aBusEntry, int aNestLevel)
void Save(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,...
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.
void SortByPageNumbers(bool aUpdateVirtualPageNums=true)
Sort the list of sheets by page number.
std::vector< SCH_SHEET_INSTANCE > GetSheetInstances() const
Fetch the instance information for all of the sheets in the hiearchy.
void GetSymbolsWithinPath(SCH_REFERENCE_LIST &aReferences, const SCH_SHEET_PATH &aSheetPath, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanSymbols=false) const
Add a SCH_REFERENCE object to aReferences for each symbol in the list of sheets that are contained wi...
void GetSheetsWithinPath(SCH_SHEET_PATHS &aSheets, const SCH_SHEET_PATH &aSheetPath) const
Add a SCH_SHEET_PATH object to aSheets for each sheet in the list that are contained within aSheetPat...
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 AppendSymbol(SCH_REFERENCE_LIST &aReferences, SCH_SYMBOL *aSymbol, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanSymbols=false) const
Append a SCH_REFERENCE object to aReferences based on aSymbol.
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:302
bool HasRootInstance() const
Check to see if this sheet has a root sheet instance.
Definition: sch_sheet.cpp:1271
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:723
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:106
VECTOR2I GetPosition() const override
Definition: sch_sheet.h:368
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:1283
wxSize GetSize() const
Definition: sch_sheet.h:108
KIGFX::COLOR4D GetBorderColor() const
Definition: sch_sheet.h:114
int GetBorderWidth() const
Definition: sch_sheet.h:111
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition: sch_sheet.h:175
const std::vector< SCH_SHEET_INSTANCE > & GetInstances() const
Definition: sch_sheet.h:381
KIGFX::COLOR4D GetBackgroundColor() const
Definition: sch_sheet.h:117
Schematic symbol object.
Definition: sch_symbol.h:81
std::vector< std::unique_ptr< SCH_PIN > > & GetRawPins()
Definition: sch_symbol.h:547
int GetUnit() const
Definition: sch_symbol.h:228
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstanceReferences()
Definition: sch_symbol.h:140
bool UseLibIdLookup() const
Definition: sch_symbol.h:190
wxString GetSchSymbolLibraryName() const
Definition: sch_symbol.cpp:290
bool GetIncludeOnBoard() const
Definition: sch_symbol.h:750
bool GetIncludeInBom() const
Definition: sch_symbol.h:747
int GetConvert() const
Definition: sch_symbol.h:270
const wxString GetFootprintFieldText(bool aResolve) const
Definition: sch_symbol.cpp:828
VECTOR2I GetPosition() const override
Definition: sch_symbol.h:712
const wxString GetValueFieldText(bool aResolve) const
Definition: sch_symbol.cpp:813
int GetOrientation() const
Get the display symbol orientation.
const LIB_ID & GetLibId() const
Definition: sch_symbol.h:175
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with SCH_FIELDs.
Definition: sch_symbol.cpp:879
void SortInstances(bool(*aSortFunction)(const SCH_SYMBOL_INSTANCE &aLhs, const SCH_SYMBOL_INSTANCE &aRhs))
Definition: sch_symbol.cpp:543
bool GetDNP() const
Definition: sch_symbol.h:753
VECTOR2I GetPosition() const override
Definition: sch_text.h:203
TEXT_SPIN_STYLE GetTextSpinStyle() const
Definition: sch_text.h:148
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
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:88
void SetWidth(int aWidth)
Definition: stroke_params.h:99
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:46
#define _(s)
static constexpr EDA_ANGLE & ANGLE_180
Definition: eda_angle.h:427
static constexpr EDA_ANGLE & ANGLE_90
Definition: eda_angle.h:425
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:423
static constexpr EDA_ANGLE & ANGLE_270
Definition: eda_angle.h:428
#define DEFAULT_SIZE_TEXT
This is the "default-of-the-default" hardcoded text size; individual application define their own def...
Definition: eda_text.h:61
const wxChar *const traceSchPlugin
Flag to enable legacy schematic plugin debug output.
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
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:341
@ LAYER_WIRE
Definition: layer_ids.h:344
@ LAYER_NOTES
Definition: layer_ids.h:358
@ LAYER_BUS
Definition: layer_ids.h:345
@ SCH_LAYER_ID_START
Definition: layer_ids.h:342
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:120
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
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:139
std::string FormatAngle(const EDA_ANGLE &aAngle)
Converts aAngle from board units to a string appropriate for writing to file.
Definition: eda_units.cpp:131
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
@ 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:56
Schematic and symbol library s-expression file format parser definitions.
#define MIME_BASE64_LENGTH
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:51
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.
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 4 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_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