KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_kicad_legacy.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) 2016 CERN
5 * Copyright The 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
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <algorithm>
24#include <boost/algorithm/string/join.hpp>
25#include <cctype>
26#include <mutex>
27#include <set>
28
29#include <wx/mstream.h>
30#include <wx/filename.h>
31#include <wx/log.h>
32#include <wx/textfile.h>
33#include <wx/tokenzr.h>
34#include <wx_filename.h> // For ::ResolvePossibleSymlinks()
35
36#include <bitmap_base.h>
37#include <fmt/format.h>
38#include <kiway.h>
39#include <string_utils.h>
40#include <richio.h>
41#include <trace_helpers.h>
42#include <trigo.h>
43#include <progress_reporter.h>
44#include <general.h>
45#include <gr_text.h>
46#include <sch_bitmap.h>
47#include <sch_bus_entry.h>
48#include <sch_symbol.h>
49#include <sch_junction.h>
50#include <sch_line.h>
51#include <sch_marker.h>
52#include <sch_no_connect.h>
53#include <sch_text.h>
54#include <sch_sheet.h>
55#include <sch_sheet_pin.h>
56#include <bus_alias.h>
57#include <io/io_utils.h>
61#include <sch_screen.h>
62#include <schematic.h>
65#include <eeschema_id.h> // for MAX_UNIT_COUNT_PER_PACKAGE definition
66#include <tool/selection.h>
68
69
70// Tokens to read/save graphic lines style
71#define T_STYLE "style"
72#define T_COLOR "rgb" // cannot be modified (used by wxWidgets)
73#define T_COLORA "rgba" // cannot be modified (used by wxWidgets)
74#define T_WIDTH "width"
75
76
77SCH_IO_KICAD_LEGACY::SCH_IO_KICAD_LEGACY() : SCH_IO( wxS( "Eeschema legacy" ) ),
78 m_appending( false ),
79 m_lineReader( nullptr ),
81 m_lineCount( 0 )
82{
83 init( nullptr );
84}
85
86
91
92
93void SCH_IO_KICAD_LEGACY::init( SCHEMATIC* aSchematic, const std::map<std::string, UTF8>* aProperties )
94{
95 m_version = 0;
96 m_rootSheet = nullptr;
97 m_currentSheet = nullptr;
98 m_schematic = aSchematic;
99 m_cache = nullptr;
100 m_out = nullptr;
101}
102
103
105{
106 const unsigned PROGRESS_DELTA = 250;
107
109 {
110 unsigned curLine = m_lineReader->LineNumber();
111
112 if( curLine > m_lastProgressLine + PROGRESS_DELTA )
113 {
114 m_progressReporter->SetCurrentProgress( ( (double) curLine )
115 / std::max( 1U, m_lineCount ) );
116
117 if( !m_progressReporter->KeepRefreshing() )
119
120 m_lastProgressLine = curLine;
121 }
122 }
123}
124
125
126SCH_SHEET* SCH_IO_KICAD_LEGACY::LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic,
127 SCH_SHEET* aAppendToMe,
128 const std::map<std::string, UTF8>* aProperties )
129{
130 wxASSERT( !aFileName || aSchematic != nullptr );
131
132 SCH_SHEET* sheet;
133 wxFileName fn = aFileName;
134
135 // Unfortunately child sheet file names the legacy schematic file format are not fully
136 // qualified and are always appended to the project path. The aFileName attribute must
137 // always be an absolute path so the project path can be used for load child sheet files.
138 wxASSERT( fn.IsAbsolute() );
139
140 if( aAppendToMe )
141 {
142 wxLogTrace( traceSchLegacyPlugin, "Append \"%s\" to sheet \"%s\".",
143 aFileName, aAppendToMe->GetFileName() );
144
145 wxFileName normedFn = aAppendToMe->GetFileName();
146
147 if( !normedFn.IsAbsolute() )
148 {
149 if( aFileName.Right( normedFn.GetFullPath().Length() ) == normedFn.GetFullPath() )
150 m_path = aFileName.Left( aFileName.Length() - normedFn.GetFullPath().Length() );
151 }
152
153 if( m_path.IsEmpty() )
154 m_path = aSchematic->Project().GetProjectPath();
155
156 wxLogTrace( traceSchLegacyPlugin, "Normalized append path \"%s\".", m_path );
157 }
158 else
159 {
160 m_path = aSchematic->Project().GetProjectPath();
161 }
162
163 m_currentPath.push( m_path );
164 init( aSchematic, aProperties );
165
166 if( aAppendToMe == nullptr )
167 {
168 // Clean up any allocated memory if an exception occurs loading the schematic.
169 std::unique_ptr<SCH_SHEET> newSheet = std::make_unique<SCH_SHEET>( aSchematic );
170 newSheet->SetFileName( aFileName );
171 m_rootSheet = newSheet.get();
172 loadHierarchy( newSheet.get() );
173
174 // If we got here, the schematic loaded successfully.
175 sheet = newSheet.release();
176 m_rootSheet = nullptr; // Quiet Coverity warning.
177 }
178 else
179 {
180 m_appending = true;
181 wxCHECK_MSG( aSchematic->IsValid(), nullptr, "Can't append to a schematic with no root!" );
182 m_rootSheet = &aSchematic->Root();
183 sheet = aAppendToMe;
184 loadHierarchy( sheet );
185 }
186
187 wxASSERT( m_currentPath.size() == 1 ); // only the project path should remain
188
189 return sheet;
190}
191
192
193// Everything below this comment is recursive. Modify with care.
194
196{
197 SCH_SCREEN* screen = nullptr;
198
199 m_currentSheet = aSheet;
200
201 if( !aSheet->GetScreen() )
202 {
203 // SCH_SCREEN objects store the full path and file name where the SCH_SHEET object only
204 // stores the file name and extension. Add the project path to the file name and
205 // extension to compare when calling SCH_SHEET::SearchHierarchy().
206 wxFileName fileName = aSheet->GetFileName();
207 fileName.SetExt( "sch" );
208
209 if( !fileName.IsAbsolute() )
210 fileName.MakeAbsolute( m_currentPath.top() );
211
212 // Save the current path so that it gets restored when descending and ascending the
213 // sheet hierarchy which allows for sheet schematic files to be nested in folders
214 // relative to the last path a schematic was loaded from.
215 wxLogTrace( traceSchLegacyPlugin, "Saving path '%s'", m_currentPath.top() );
216 m_currentPath.push( fileName.GetPath() );
217 wxLogTrace( traceSchLegacyPlugin, "Current path '%s'", m_currentPath.top() );
218 wxLogTrace( traceSchLegacyPlugin, "Loading '%s'", fileName.GetFullPath() );
219
220 m_rootSheet->SearchHierarchy( fileName.GetFullPath(), &screen );
221
222 if( screen )
223 {
224 aSheet->SetScreen( screen );
225 screen->SetParent( m_schematic );
226 // Do not need to load the sub-sheets - this has already been done.
227 }
228 else
229 {
230 aSheet->SetScreen( new SCH_SCREEN( m_schematic ) );
231 aSheet->GetScreen()->SetFileName( fileName.GetFullPath() );
232
233 if( aSheet == m_rootSheet )
234 aSheet->SyncUuidToScreen();
235
236 try
237 {
238 loadFile( fileName.GetFullPath(), aSheet->GetScreen() );
239 }
240 catch( const IO_ERROR& ioe )
241 {
242 // If there is a problem loading the root sheet, there is no recovery.
243 if( aSheet == m_rootSheet )
244 throw( ioe );
245
246 // For all subsheets, queue up the error message for the caller.
247 if( !m_error.IsEmpty() )
248 m_error += "\n";
249
250 m_error += ioe.What();
251 }
252
253 aSheet->GetScreen()->SetFileReadOnly( !fileName.IsFileWritable() );
254 aSheet->GetScreen()->SetFileExists( true );
255
256 for( SCH_ITEM* aItem : aSheet->GetScreen()->Items().OfType( SCH_SHEET_T ) )
257 {
258 wxCHECK2( aItem->Type() == SCH_SHEET_T, continue );
259 auto sheet = static_cast<SCH_SHEET*>( aItem );
260
261 // Set the parent to aSheet. This effectively creates a method to find
262 // the root sheet from any sheet so a pointer to the root sheet does not
263 // need to be stored globally. Note: this is not the same as a hierarchy.
264 // Complex hierarchies can have multiple copies of a sheet. This only
265 // provides a simple tree to find the root sheet.
266 sheet->SetParent( aSheet );
267
268 // Recursion starts here.
269 loadHierarchy( sheet );
270 }
271 }
272
273 m_currentPath.pop();
274 wxLogTrace( traceSchLegacyPlugin, "Restoring path \"%s\"", m_currentPath.top() );
275 }
276}
277
278
279void SCH_IO_KICAD_LEGACY::loadFile( const wxString& aFileName, SCH_SCREEN* aScreen )
280{
281 FILE_LINE_READER reader( aFileName );
282
284 {
285 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
286
287 if( !m_progressReporter->KeepRefreshing() )
289
290 m_lineReader = &reader;
291 m_lineCount = 0;
292
293 while( reader.ReadLine() )
294 m_lineCount++;
295
296 reader.Rewind();
297 }
298
299 loadHeader( reader, aScreen );
300
301 LoadContent( reader, aScreen, m_version );
302
303 // Unfortunately schematic files prior to version 2 are not terminated with $EndSCHEMATC
304 // so checking for its existance will fail so just exit here and take our chances. :(
305 if( m_version > 1 )
306 {
307 char* line = reader.Line();
308
309 while( *line == ' ' )
310 line++;
311
312 if( !strCompare( "$EndSCHEMATC", line ) )
313 THROW_IO_ERROR( "'$EndSCHEMATC' not found" );
314 }
315}
316
317
318void SCH_IO_KICAD_LEGACY::LoadContent( LINE_READER& aReader, SCH_SCREEN* aScreen, int version )
319{
320 m_version = version;
321
322 // We cannot safely load content without a set root level.
323 wxCHECK_RET( m_rootSheet,
324 "Cannot call SCH_IO_KICAD_LEGACY::LoadContent() without setting root sheet." );
325
326 while( aReader.ReadLine() )
327 {
328 checkpoint();
329
330 char* line = aReader.Line();
331
332 while( *line == ' ' )
333 line++;
334
335 // Either an object will be loaded properly or the file load will fail and raise
336 // an exception.
337 if( strCompare( "$Descr", line ) )
338 loadPageSettings( aReader, aScreen );
339 else if( strCompare( "$Comp", line ) )
340 aScreen->Append( loadSymbol( aReader ) );
341 else if( strCompare( "$Sheet", line ) )
342 aScreen->Append( loadSheet( aReader ) );
343 else if( strCompare( "$Bitmap", line ) )
344 aScreen->Append( loadBitmap( aReader ) );
345 else if( strCompare( "Connection", line ) )
346 aScreen->Append( loadJunction( aReader ) );
347 else if( strCompare( "NoConn", line ) )
348 aScreen->Append( loadNoConnect( aReader ) );
349 else if( strCompare( "Wire", line ) )
350 aScreen->Append( loadWire( aReader ) );
351 else if( strCompare( "Entry", line ) )
352 aScreen->Append( loadBusEntry( aReader ) );
353 else if( strCompare( "Text", line ) )
354 aScreen->Append( loadText( aReader ) );
355 else if( strCompare( "BusAlias", line ) )
356 {
357 auto alias = loadBusAlias( aReader, aScreen );
358 const SCHEMATIC* schematic = aScreen->Schematic();
359
360 if( m_appending || !schematic || !schematic->HasProjectBusAliases() )
361 aScreen->AddBusAlias( std::move( alias ) );
362 }
363 else if( strCompare( "Kmarq", line ) )
364 continue; // Ignore legacy (until 2009) ERC marker entry
365 else if( strCompare( "$EndSCHEMATC", line ) )
366 return;
367 else
368 SCH_PARSE_ERROR( "unrecognized token", aReader, line );
369 }
370}
371
372
374{
375 const char* line = aReader.ReadLine();
376
377 if( !line || !strCompare( "Eeschema Schematic File Version", line, &line ) )
378 {
379 m_error.Printf( _( "'%s' does not appear to be an Eeschema file." ),
380 aScreen->GetFileName() );
382 }
383
384 // get the file version here.
385 m_version = parseInt( aReader, line, &line );
386
387 // The next lines are the lib list section, and are mainly comments, like:
388 // LIBS:power
389 // the lib list is not used, but is in schematic file just in case.
390 // It is usually not empty, but we accept empty list.
391 // If empty, there is a legacy section, not used
392 // EELAYER i j
393 // and the last line is
394 // EELAYER END
395 // Skip all lines until the end of header "EELAYER END" is found
396 while( aReader.ReadLine() )
397 {
398 checkpoint();
399
400 line = aReader.Line();
401
402 while( *line == ' ' )
403 line++;
404
405 if( strCompare( "EELAYER END", line ) )
406 return;
407 }
408
409 THROW_IO_ERROR( _( "Missing 'EELAYER END'" ) );
410}
411
412
414{
415 wxASSERT( aScreen != nullptr );
416
417 wxString buf;
418 const char* line = aReader.Line();
419
420 PAGE_INFO pageInfo;
421 TITLE_BLOCK tb;
422
423 wxCHECK_RET( strCompare( "$Descr", line, &line ), "Invalid sheet description" );
424
425 parseUnquotedString( buf, aReader, line, &line );
426
427 if( !pageInfo.SetType( buf ) )
428 SCH_PARSE_ERROR( "invalid page size", aReader, line );
429
430 int pagew = parseInt( aReader, line, &line );
431 int pageh = parseInt( aReader, line, &line );
432
433 if( pageInfo.GetType() == PAGE_SIZE_TYPE::User )
434 {
435 pageInfo.SetWidthMils( pagew );
436 pageInfo.SetHeightMils( pageh );
437 }
438 else
439 {
440 wxString orientation;
441
442 // Non custom size, set portrait if its present. Can be empty string which defaults
443 // to landscape.
444 parseUnquotedString( orientation, aReader, line, &line, true );
445
446 if( orientation == "portrait" )
447 pageInfo.SetPortrait( true );
448 }
449
450 aScreen->SetPageSettings( pageInfo );
451
452 while( line != nullptr )
453 {
454 buf.clear();
455
456 if( !aReader.ReadLine() )
457 SCH_PARSE_ERROR( _( "unexpected end of file" ), aReader, line );
458
459 line = aReader.Line();
460
461 if( strCompare( "Sheet", line, &line ) )
462 {
463 aScreen->SetVirtualPageNumber( parseInt( aReader, line, &line ) );
464 aScreen->SetPageCount( parseInt( aReader, line, &line ) );
465 }
466 else if( strCompare( "Title", line, &line ) )
467 {
468 parseQuotedString( buf, aReader, line, &line, true );
469 tb.SetTitle( buf );
470 }
471 else if( strCompare( "Date", line, &line ) )
472 {
473 parseQuotedString( buf, aReader, line, &line, true );
474 tb.SetDate( buf );
475 }
476 else if( strCompare( "Rev", line, &line ) )
477 {
478 parseQuotedString( buf, aReader, line, &line, true );
479 tb.SetRevision( buf );
480 }
481 else if( strCompare( "Comp", line, &line ) )
482 {
483 parseQuotedString( buf, aReader, line, &line, true );
484 tb.SetCompany( buf );
485 }
486 else if( strCompare( "Comment1", line, &line ) )
487 {
488 parseQuotedString( buf, aReader, line, &line, true );
489 tb.SetComment( 0, buf );
490 }
491 else if( strCompare( "Comment2", line, &line ) )
492 {
493 parseQuotedString( buf, aReader, line, &line, true );
494 tb.SetComment( 1, buf );
495 }
496 else if( strCompare( "Comment3", line, &line ) )
497 {
498 parseQuotedString( buf, aReader, line, &line, true );
499 tb.SetComment( 2, buf );
500 }
501 else if( strCompare( "Comment4", line, &line ) )
502 {
503 parseQuotedString( buf, aReader, line, &line, true );
504 tb.SetComment( 3, buf );
505 }
506 else if( strCompare( "Comment5", line, &line ) )
507 {
508 parseQuotedString( buf, aReader, line, &line, true );
509 tb.SetComment( 4, buf );
510 }
511 else if( strCompare( "Comment6", line, &line ) )
512 {
513 parseQuotedString( buf, aReader, line, &line, true );
514 tb.SetComment( 5, buf );
515 }
516 else if( strCompare( "Comment7", line, &line ) )
517 {
518 parseQuotedString( buf, aReader, line, &line, true );
519 tb.SetComment( 6, buf );
520 }
521 else if( strCompare( "Comment8", line, &line ) )
522 {
523 parseQuotedString( buf, aReader, line, &line, true );
524 tb.SetComment( 7, buf );
525 }
526 else if( strCompare( "Comment9", line, &line ) )
527 {
528 parseQuotedString( buf, aReader, line, &line, true );
529 tb.SetComment( 8, buf );
530 }
531 else if( strCompare( "$EndDescr", line ) )
532 {
533 aScreen->SetTitleBlock( tb );
534 return;
535 }
536 }
537
538 SCH_PARSE_ERROR( "missing 'EndDescr'", aReader, line );
539}
540
541
543{
544 std::unique_ptr<SCH_SHEET> sheet = std::make_unique<SCH_SHEET>();
545
546 const char* line = aReader.ReadLine();
547
548 while( line != nullptr )
549 {
550 if( strCompare( "S", line, &line ) ) // Sheet dimensions.
551 {
552 VECTOR2I position;
553
554 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
555 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
556 sheet->SetPosition( position );
557
558 VECTOR2I size;
559
560 size.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
561 size.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
562 sheet->SetSize( size );
563 }
564 else if( strCompare( "U", line, &line ) ) // Sheet UUID.
565 {
566 wxString text;
567 parseUnquotedString( text, aReader, line );
568
569 if( text != "00000000" )
570 const_cast<KIID&>( sheet->m_Uuid ) = KIID( text );
571 }
572 else if( *line == 'F' ) // Sheet field.
573 {
574 line++;
575
576 wxString text;
577 int size;
578 int legacy_field_id = parseInt( aReader, line, &line );
579
580 if( legacy_field_id == 0 || legacy_field_id == 1 ) // Sheet name and file name.
581 {
582 parseQuotedString( text, aReader, line, &line );
583 size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
584
585 SCH_FIELD* field = sheet->GetField( legacy_field_id == 0 ? FIELD_T::SHEET_NAME
587 field->SetText( text );
588 field->SetTextSize( VECTOR2I( size, size ) );
589 }
590 else // Sheet pin.
591 {
592 // Use a unique_ptr so that we clean up in the case of a throw
593 std::unique_ptr<SCH_SHEET_PIN> sheetPin = std::make_unique<SCH_SHEET_PIN>( sheet.get() );
594
595 sheetPin->SetNumber( legacy_field_id );
596
597 // Can be empty fields.
598 parseQuotedString( text, aReader, line, &line, true );
599
600 sheetPin->SetText( ConvertToNewOverbarNotation( text ) );
601
602 if( line == nullptr )
603 THROW_IO_ERROR( _( "unexpected end of line" ) );
604
605 switch( parseChar( aReader, line, &line ) )
606 {
607 case 'I': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_INPUT ); break;
608 case 'O': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_OUTPUT ); break;
609 case 'B': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_BIDI ); break;
610 case 'T': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_TRISTATE ); break;
611 case 'U': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_UNSPECIFIED ); break;
612 default: SCH_PARSE_ERROR( "invalid sheet pin type", aReader, line );
613 }
614
615 switch( parseChar( aReader, line, &line ) )
616 {
617 case 'R': sheetPin->SetSide( SHEET_SIDE::RIGHT ); break;
618 case 'T': sheetPin->SetSide( SHEET_SIDE::TOP ); break;
619 case 'B': sheetPin->SetSide( SHEET_SIDE::BOTTOM ); break;
620 case 'L': sheetPin->SetSide( SHEET_SIDE::LEFT ); break;
621 default: SCH_PARSE_ERROR( "invalid sheet pin side", aReader, line );
622 }
623
624 VECTOR2I position;
625
626 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
627 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
628 sheetPin->SetPosition( position );
629
630 size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
631
632 sheetPin->SetTextSize( VECTOR2I( size, size ) );
633
634 sheet->AddPin( sheetPin.release() );
635 }
636 }
637 else if( strCompare( "$EndSheet", line ) )
638 {
639 sheet->AutoplaceFields( nullptr, AUTOPLACE_AUTO );
640 return sheet.release();
641 }
642
643 line = aReader.ReadLine();
644 }
645
646 SCH_PARSE_ERROR( "missing '$EndSheet`", aReader, line );
647
648 return nullptr; // Prevents compiler warning. Should never get here.
649}
650
651
653{
654 std::unique_ptr<SCH_BITMAP> bitmap = std::make_unique<SCH_BITMAP>();
655 REFERENCE_IMAGE& refImage = bitmap->GetReferenceImage();
656
657 const char* line = aReader.Line();
658
659 wxCHECK( strCompare( "$Bitmap", line, &line ), nullptr );
660
661 line = aReader.ReadLine();
662
663 while( line != nullptr )
664 {
665 if( strCompare( "Pos", line, &line ) )
666 {
667 VECTOR2I position;
668
669 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
670 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
671 bitmap->SetPosition( position );
672 }
673 else if( strCompare( "Scale", line, &line ) )
674 {
675 auto scalefactor = parseDouble( aReader, line, &line );
676
677 // Prevent scalefactor values that cannot be displayed.
678 // In the case of a bad value, we accept that the image might be mis-scaled
679 // rather than removing the full image. Users can then edit the scale factor in
680 // Eeschema to the appropriate value
681 if( !std::isnormal( scalefactor ) )
682 scalefactor = 1.0;
683
684 refImage.SetImageScale( scalefactor );
685 }
686 else if( strCompare( "Data", line, &line ) )
687 {
688 wxMemoryBuffer buffer;
689
690 while( line )
691 {
692 if( !aReader.ReadLine() )
693 SCH_PARSE_ERROR( _( "Unexpected end of file" ), aReader, line );
694
695 line = aReader.Line();
696
697 if( strCompare( "EndData", line ) )
698 {
699 // all the PNG date is read.
700 refImage.ReadImageFile( buffer );
701
702 // Legacy file formats assumed 300 image PPI at load.
703 const BITMAP_BASE& bitmapImage = refImage.GetImage();
704 refImage.SetImageScale( refImage.GetImageScale() * bitmapImage.GetPPI()
705 / 300.0 );
706 break;
707 }
708
709 // Read PNG data, stored in hexadecimal,
710 // each byte = 2 hexadecimal digits and a space between 2 bytes
711 // and put it in memory stream buffer
712 // Note:
713 // Some old files created bu the V4 schematic versions have a extra
714 // "$EndBitmap" at the end of the hexadecimal data. (Probably due to
715 // a bug). So discard it
716 int len = strlen( line );
717
718 for( ; len > 0 && !isspace( *line ) && '$' != *line; len -= 3, line += 3 )
719 {
720 int value = 0;
721
722 if( sscanf( line, "%X", &value ) == 1 )
723 buffer.AppendByte( (char) value );
724 else
725 THROW_IO_ERROR( "invalid PNG data" );
726 }
727 }
728
729 if( line == nullptr )
730 THROW_IO_ERROR( _( "unexpected end of file" ) );
731 }
732 else if( strCompare( "$EndBitmap", line ) )
733 {
734 return bitmap.release();
735 }
736
737 line = aReader.ReadLine();
738 }
739
740 THROW_IO_ERROR( _( "unexpected end of file" ) );
741}
742
743
745{
746 std::unique_ptr<SCH_JUNCTION> junction = std::make_unique<SCH_JUNCTION>();
747
748 const char* line = aReader.Line();
749
750 wxCHECK( strCompare( "Connection", line, &line ), nullptr );
751
752 wxString name;
753
754 parseUnquotedString( name, aReader, line, &line );
755
756 VECTOR2I position;
757
758 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
759 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
760 junction->SetPosition( position );
761
762 return junction.release();
763}
764
765
767{
768 std::unique_ptr<SCH_NO_CONNECT> no_connect = std::make_unique<SCH_NO_CONNECT>();
769
770 const char* line = aReader.Line();
771
772 wxCHECK( strCompare( "NoConn", line, &line ), nullptr );
773
774 wxString name;
775
776 parseUnquotedString( name, aReader, line, &line );
777
778 VECTOR2I position;
779
780 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
781 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
782 no_connect->SetPosition( position );
783
784 return no_connect.release();
785}
786
787
789{
790 std::unique_ptr<SCH_LINE> wire = std::make_unique<SCH_LINE>();
791
792 const char* line = aReader.Line();
793
794 wxCHECK( strCompare( "Wire", line, &line ), nullptr );
795
796 if( strCompare( "Wire", line, &line ) )
797 wire->SetLayer( LAYER_WIRE );
798 else if( strCompare( "Bus", line, &line ) )
799 wire->SetLayer( LAYER_BUS );
800 else if( strCompare( "Notes", line, &line ) )
801 wire->SetLayer( LAYER_NOTES );
802 else
803 SCH_PARSE_ERROR( "invalid line type", aReader, line );
804
805 if( !strCompare( "Line", line, &line ) )
806 SCH_PARSE_ERROR( "invalid wire definition", aReader, line );
807
808 // The default graphical line style was Dashed.
809 if( wire->GetLayer() == LAYER_NOTES )
810 wire->SetLineStyle( LINE_STYLE::DASH );
811
812 // Since Sept 15, 2017, a line style is alloved (width, style, color)
813 // Only non default values are stored
814 while( !is_eol( *line ) )
815 {
816 wxString buf;
817
818 parseUnquotedString( buf, aReader, line, &line );
819
820 if( buf == ")" )
821 continue;
822
823 else if( buf == T_WIDTH )
824 {
825 int size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
826 wire->SetLineWidth( size );
827 }
828 else if( buf == T_STYLE )
829 {
830 parseUnquotedString( buf, aReader, line, &line );
831
832 if( buf == wxT( "solid" ) )
833 wire->SetLineStyle( LINE_STYLE::SOLID );
834 else if( buf == wxT( "dashed" ) )
835 wire->SetLineStyle( LINE_STYLE::DASH );
836 else if( buf == wxT( "dash_dot" ) )
837 wire->SetLineStyle( LINE_STYLE::DASHDOT );
838 else if( buf == wxT( "dotted" ) )
839 wire->SetLineStyle( LINE_STYLE::DOT );
840 }
841 else // should be the color parameter.
842 {
843 // The color param is something like rgb(150, 40, 191)
844 // and because there is no space between ( and 150
845 // the first param is inside buf.
846 // So break keyword and the first param into 2 separate strings.
847 wxString prm, keyword;
848 keyword = buf.BeforeLast( '(', &prm );
849
850 if( ( keyword == T_COLOR ) || ( keyword == T_COLORA ) )
851 {
852 long color[4] = { 0 };
853
854 int ii = 0;
855
856 if( !prm.IsEmpty() )
857 {
858 prm.ToLong( &color[ii] );
859 ii++;
860 }
861
862 int prm_count = ( keyword == T_COLORA ) ? 4 : 3;
863
864 // fix opacity to 1.0 or 255, when not exists in file
865 color[3] = 255;
866
867 for(; ii < prm_count && !is_eol( *line ); ii++ )
868 {
869 color[ii] = parseInt( aReader, line, &line );
870
871 // Skip the separator between values
872 if( *line == ',' || *line == ' ')
873 line++;
874 }
875
876 wire->SetLineColor( color[0]/255.0, color[1]/255.0, color[2]/255.0,color[3]/255.0 );
877 }
878 }
879 }
880
881 // Read the segment en points coordinates:
882 line = aReader.ReadLine();
883
884 VECTOR2I begin, end;
885
886 begin.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
887 begin.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
888 end.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
889 end.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
890
891 wire->SetStartPoint( begin );
892 wire->SetEndPoint( end );
893
894 return wire.release();
895}
896
897
899{
900 const char* line = aReader.Line();
901
902 wxCHECK( strCompare( "Entry", line, &line ), nullptr );
903
904 std::unique_ptr<SCH_BUS_ENTRY_BASE> busEntry;
905
906 if( strCompare( "Wire", line, &line ) )
907 {
908 busEntry = std::make_unique<SCH_BUS_WIRE_ENTRY>();
909
910 if( !strCompare( "Line", line, &line ) )
911 SCH_PARSE_ERROR( "invalid bus entry definition expected 'Line'", aReader, line );
912 }
913 else if( strCompare( "Bus", line, &line ) )
914 {
915 busEntry = std::make_unique<SCH_BUS_BUS_ENTRY>();
916
917 if( !strCompare( "Bus", line, &line ) )
918 SCH_PARSE_ERROR( "invalid bus entry definition expected 'Bus'", aReader, line );
919 }
920 else
921 {
922 SCH_PARSE_ERROR( "invalid bus entry type", aReader, line );
923 }
924
925 line = aReader.ReadLine();
926
927 VECTOR2I pos;
928 VECTOR2I size;
929
930 pos.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
931 pos.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
932 size.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
933 size.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
934
935 size.x -= pos.x;
936 size.y -= pos.y;
937
938 busEntry->SetPosition( pos );
939 busEntry->SetSize( size );
940
941 return busEntry.release();
942}
943
944
945// clang-format off
946const std::map<LABEL_FLAG_SHAPE, const char*> sheetLabelNames
947{
948 { LABEL_FLAG_SHAPE::L_INPUT, "Input" },
949 { LABEL_FLAG_SHAPE::L_OUTPUT, "Output" },
950 { LABEL_FLAG_SHAPE::L_BIDI, "BiDi" },
951 { LABEL_FLAG_SHAPE::L_TRISTATE, "3State" },
953};
954// clang-format on
955
956
958{
959 const char* line = aReader.Line();
960 KICAD_T textType = TYPE_NOT_INIT;
961
962 wxCHECK( strCompare( "Text", line, &line ), nullptr );
963
964 if( strCompare( "Notes", line, &line ) )
965 {
966 textType = SCH_TEXT_T;
967 }
968 else if( strCompare( "Label", line, &line ) )
969 {
970 textType = SCH_LABEL_T;
971 }
972 else if( strCompare( "HLabel", line, &line ) )
973 {
974 textType = SCH_HIER_LABEL_T;
975 }
976 else if( strCompare( "GLabel", line, &line ) )
977 {
978 // Prior to version 2, the SCH_GLOBALLABEL object did not exist.
979 if( m_version == 1 )
980 textType = SCH_HIER_LABEL_T;
981 else
982 textType = SCH_GLOBAL_LABEL_T;
983 }
984 else
985 {
986 SCH_PARSE_ERROR( "unknown Text type", aReader, line );
987 }
988
989 VECTOR2I position;
990
991 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
992 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
993
994 std::unique_ptr<SCH_TEXT> text;
995
996 switch( textType )
997 {
998 case SCH_TEXT_T: text.reset( new SCH_TEXT( position ) ); break;
999 case SCH_LABEL_T: text.reset( new SCH_LABEL( position ) ); break;
1000 case SCH_HIER_LABEL_T: text.reset( new SCH_HIERLABEL( position ) ); break;
1001 case SCH_GLOBAL_LABEL_T: text.reset( new SCH_GLOBALLABEL( position ) ); break;
1002 default: break;
1003 }
1004
1005 int spinStyle = parseInt( aReader, line, &line );
1006
1007 // Sadly we store the orientation of hierarchical and global labels using a different
1008 // int encoding than that for local labels:
1009 // Global Local
1010 // Left justified 0 2
1011 // Up 1 3
1012 // Right justified 2 0
1013 // Down 3 1
1014 // So we must flip it as the enum is setup with the "global" numbering
1015 if( textType != SCH_GLOBAL_LABEL_T && textType != SCH_HIER_LABEL_T )
1016 {
1017 if( spinStyle == 0 )
1018 spinStyle = 2;
1019 else if( spinStyle == 2 )
1020 spinStyle = 0;
1021 }
1022
1023 int size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1024
1025 text->SetTextSize( VECTOR2I( size, size ) );
1026
1027 if( textType == SCH_LABEL_T || textType == SCH_HIER_LABEL_T || textType == SCH_GLOBAL_LABEL_T )
1028 {
1029 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( text.get() );
1030
1031 label->SetSpinStyle( static_cast<SPIN_STYLE::SPIN>( spinStyle ) );
1032
1033 // Parse the global and hierarchical label type.
1034 if( textType == SCH_HIER_LABEL_T || textType == SCH_GLOBAL_LABEL_T )
1035 {
1036 auto resultIt = std::find_if( sheetLabelNames.begin(), sheetLabelNames.end(),
1037 [ &line ]( const auto& it )
1038 {
1039 return strCompare( it.second, line, &line );
1040 } );
1041
1042 if( resultIt != sheetLabelNames.end() )
1043 label->SetShape( resultIt->first );
1044 else
1045 SCH_PARSE_ERROR( "invalid label type", aReader, line );
1046 }
1047 }
1048 else if( textType == SCH_TEXT_T )
1049 {
1050 switch( spinStyle )
1051 {
1052 case SPIN_STYLE::RIGHT: // Horiz Normal Orientation
1053 text->SetTextAngle( ANGLE_HORIZONTAL );
1054 text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
1055 break;
1056
1057 case SPIN_STYLE::UP: // Vert Orientation UP
1058 text->SetTextAngle( ANGLE_VERTICAL );
1059 text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
1060 break;
1061
1062 case SPIN_STYLE::LEFT: // Horiz Orientation - Right justified
1063 text->SetTextAngle( ANGLE_HORIZONTAL );
1064 text->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
1065 break;
1066
1067 case SPIN_STYLE::BOTTOM: // Vert Orientation BOTTOM
1068 text->SetTextAngle( ANGLE_VERTICAL );
1069 text->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
1070 break;
1071 }
1072
1073 text->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
1074 }
1075
1076 int penWidth = 0;
1077
1078 // The following tokens do not exist in version 1 schematic files,
1079 // and not always in version 2 for HLabels and GLabels
1080 if( m_version > 1 )
1081 {
1082 if( m_version > 2 || *line >= ' ' )
1083 {
1084 if( strCompare( "Italic", line, &line ) )
1085 text->SetItalicFlag( true );
1086 else if( !strCompare( "~", line, &line ) )
1087 SCH_PARSE_ERROR( _( "expected 'Italics' or '~'" ), aReader, line );
1088 }
1089
1090 // The penWidth token does not exist in older versions of the schematic file format
1091 // so calling parseInt will be made only if the EOL is not reached.
1092 if( *line >= ' ' )
1093 penWidth = parseInt( aReader, line, &line );
1094 }
1095
1096 // Legacy bold is a non-zero pen width; store auto thickness and let the Bold flag drive it.
1097 text->SetBoldFlag( penWidth != 0 );
1098 text->SetTextThickness( 0 );
1099
1100 // Read the text string for the text.
1101 char* tmp = aReader.ReadLine();
1102
1103 tmp = strtok( tmp, "\r\n" );
1104 wxString val = From_UTF8( tmp );
1105
1106 for( ; ; )
1107 {
1108 size_t i = val.find( wxT( "\\n" ) );
1109
1110 if( i == wxString::npos )
1111 break;
1112
1113 val.erase( i, 2 );
1114 val.insert( i, wxT( "\n" ) );
1115 }
1116
1117 text->SetText( ConvertToNewOverbarNotation( val ) );
1118
1119 return text.release();
1120}
1121
1122
1124{
1125 const char* line = aReader.Line();
1126
1127 wxCHECK( strCompare( "$Comp", line, &line ), nullptr );
1128
1129 std::unique_ptr<SCH_SYMBOL> symbol = std::make_unique<SCH_SYMBOL>();
1130
1131 line = aReader.ReadLine();
1132
1133 while( line != nullptr )
1134 {
1135 if( strCompare( "L", line, &line ) )
1136 {
1137 wxString libName;
1138 size_t pos = 2; // "X" plus ' ' space character.
1139 wxString utf8Line = wxString::FromUTF8( line );
1140 wxStringTokenizer tokens( utf8Line, " \t\r\n" );
1141
1142 if( tokens.CountTokens() < 2 )
1143 {
1144 THROW_PARSE_ERROR( "invalid symbol library definition", aReader.GetSource(),
1145 aReader.Line(), aReader.LineNumber(), pos );
1146 }
1147
1148 libName = tokens.GetNextToken();
1149 libName.Replace( "~", " " );
1150
1151 LIB_ID libId;
1152
1153 // Prior to schematic version 4, library IDs did not have a library nickname so
1154 // parsing the symbol name with LIB_ID::Parse() would break symbol library links
1155 // that contained '/' and ':' characters.
1156 if( m_version > 3 )
1157 libId.Parse( libName, true );
1158 else
1159 libId.SetLibItemName( libName );
1160
1161 symbol->SetLibId( libId );
1162
1163 wxString refDesignator = tokens.GetNextToken();
1164
1165 refDesignator.Replace( "~", " " );
1166
1167 wxString prefix = refDesignator;
1168
1169 while( prefix.Length() )
1170 {
1171 if( ( prefix.Last() < '0' || prefix.Last() > '9') && prefix.Last() != '?' )
1172 break;
1173
1174 prefix.RemoveLast();
1175 }
1176
1177 // Avoid a prefix containing trailing/leading spaces
1178 prefix.Trim( true );
1179 prefix.Trim( false );
1180
1181 if( prefix.IsEmpty() )
1182 symbol->SetPrefix( wxString( "U" ) );
1183 else
1184 symbol->SetPrefix( prefix );
1185 }
1186 else if( strCompare( "U", line, &line ) )
1187 {
1188 // This fixes a potentially buggy files caused by unit being set to zero which
1189 // causes netlist issues. See https://bugs.launchpad.net/kicad/+bug/1677282.
1190 int unit = parseInt( aReader, line, &line );
1191
1192 if( unit == 0 )
1193 {
1194 unit = 1;
1195
1196 // Set the file as modified so the user can be warned.
1197 if( m_rootSheet->GetScreen() )
1198 m_rootSheet->GetScreen()->SetContentModified();
1199 }
1200
1201 symbol->SetUnit( unit );
1202
1203 // Same can also happen with the body style ("convert") parameter
1204 int bodyStyle = parseInt( aReader, line, &line );
1205
1206 if( bodyStyle == 0 )
1207 {
1208 bodyStyle = 1;
1209
1210 // Set the file as modified so the user can be warned.
1211 if( m_rootSheet->GetScreen() )
1212 m_rootSheet->GetScreen()->SetContentModified();
1213 }
1214
1215 symbol->SetBodyStyle( bodyStyle );
1216
1217 wxString text;
1218 parseUnquotedString( text, aReader, line, &line );
1219
1220 if( text != "00000000" )
1221 const_cast<KIID&>( symbol->m_Uuid ) = KIID( text );
1222 }
1223 else if( strCompare( "P", line, &line ) )
1224 {
1225 VECTOR2I pos;
1226
1227 pos.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1228 pos.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1229 symbol->SetPosition( pos );
1230 }
1231 else if( strCompare( "AR", line, &line ) )
1232 {
1233 const char* strCompare = "Path=";
1234 int len = strlen( strCompare );
1235
1236 if( strncasecmp( strCompare, line, len ) != 0 )
1237 SCH_PARSE_ERROR( "missing 'Path=' token", aReader, line );
1238
1239 line += len;
1240 wxString pathStr, reference, unit;
1241
1242 parseQuotedString( pathStr, aReader, line, &line );
1243
1244 // Note: AR path excludes root sheet, but includes symbol. Drop the symbol ID
1245 // since it's already defined in the symbol itself.
1246 KIID_PATH path( pathStr );
1247
1248 if( path.size() > 0 )
1249 path.pop_back();
1250
1251 // In the new file format, the root schematic UUID is used as the virtual SCH_SHEET
1252 // UUID so we need to prefix it to the symbol path so the symbol instance paths
1253 // get saved with the root schematic UUID.
1254 if( !m_appending )
1255 path.insert( path.begin(), m_rootSheet->GetScreen()->GetUuid() );
1256
1257 strCompare = "Ref=";
1258 len = strlen( strCompare );
1259
1260 if( strncasecmp( strCompare, line, len ) != 0 )
1261 SCH_PARSE_ERROR( "missing 'Ref=' token", aReader, line );
1262
1263 line+= len;
1264 parseQuotedString( reference, aReader, line, &line );
1265
1266 strCompare = "Part=";
1267 len = strlen( strCompare );
1268
1269 if( strncasecmp( strCompare, line, len ) != 0 )
1270 SCH_PARSE_ERROR( "missing 'Part=' token", aReader, line );
1271
1272 line+= len;
1273 parseQuotedString( unit, aReader, line, &line );
1274
1275 long tmp;
1276
1277 if( !unit.ToLong( &tmp, 10 ) )
1278 SCH_PARSE_ERROR( "expected integer value", aReader, line );
1279
1280 if( tmp < 0 || tmp > MAX_UNIT_COUNT_PER_PACKAGE )
1281 SCH_PARSE_ERROR( "unit value out of range", aReader, line );
1282
1283 symbol->AddHierarchicalReference( path, reference, (int)tmp );
1284 symbol->GetField( FIELD_T::REFERENCE )->SetText( reference );
1285 }
1286 else if( strCompare( "F", line, &line ) )
1287 {
1288 int legacy_field_id = parseInt( aReader, line, &line );
1289
1290 wxString text, name;
1291
1292 parseQuotedString( text, aReader, line, &line, true );
1293
1294 char orientation = parseChar( aReader, line, &line );
1295 VECTOR2I pos;
1296 pos.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1297 pos.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1298
1299 // Y got inverted in symbol coordinates
1300 pos.y = -( pos.y - symbol->GetY() ) + symbol->GetY();
1301
1302 int size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1303 int attributes = parseHex( aReader, line, &line );
1304
1305 SCH_FIELD* field;
1306
1307 // Map fixed legacy IDs
1308 switch( legacy_field_id )
1309 {
1310 case 0: field = symbol->GetField( FIELD_T::REFERENCE ); break;
1311 case 1: field = symbol->GetField( FIELD_T::VALUE ); break;
1312 case 2: field = symbol->GetField( FIELD_T::FOOTPRINT ); break;
1313 case 3: field = symbol->GetField( FIELD_T::DATASHEET ); break;
1314
1315 default:
1316 field = symbol->AddField( SCH_FIELD( symbol.get(), FIELD_T::USER ) );
1317 break;
1318 }
1319
1320 // Prior to version 2 of the schematic file format, none of the following existed.
1321 if( m_version > 1 )
1322 {
1323 wxString textAttrs;
1324 char hjustify = parseChar( aReader, line, &line );
1325
1326 parseUnquotedString( textAttrs, aReader, line, &line );
1327
1328 // The name of the field is optional.
1329 parseQuotedString( name, aReader, line, &line, true );
1330
1331 if( hjustify == 'L' )
1333 else if( hjustify == 'R' )
1335 else if( hjustify != 'C' )
1336 SCH_PARSE_ERROR( "symbol field text horizontal justification must be "
1337 "L, R, or C", aReader, line );
1338
1339 // We are guaranteed to have a least one character here for older file formats
1340 // otherwise an exception would have been raised..
1341 if( textAttrs[0] == 'T' )
1343 else if( textAttrs[0] == 'B' )
1345 else if( textAttrs[0] != 'C' )
1346 SCH_PARSE_ERROR( "symbol field text vertical justification must be "
1347 "B, T, or C", aReader, line );
1348
1349 // Newer file formats include the bold and italics text attribute.
1350 if( textAttrs.Length() > 1 )
1351 {
1352 if( textAttrs.Length() != 3 )
1353 {
1354 SCH_PARSE_ERROR( _( "symbol field text attributes must be 3 characters wide" ),
1355 aReader, line );
1356 }
1357
1358 if( textAttrs[1] == 'I' )
1359 {
1360 field->SetItalicFlag( true );
1361 }
1362 else if( textAttrs[1] != 'N' )
1363 {
1364 SCH_PARSE_ERROR( "symbol field text italics indicator must be I or N",
1365 aReader, line );
1366 }
1367
1368 if( textAttrs[2] == 'B' )
1369 {
1370 field->SetBoldFlag( true );
1371 }
1372 else if( textAttrs[2] != 'N' )
1373 {
1374 SCH_PARSE_ERROR( "symbol field text bold indicator must be B or N",
1375 aReader, line );
1376 }
1377 }
1378 }
1379
1380 field->SetText( text );
1381 field->SetTextPos( pos );
1382 field->SetVisible( !attributes );
1383 field->SetTextSize( VECTOR2I( size, size ) );
1384
1385 if( orientation == 'H' )
1387 else if( orientation == 'V' )
1388 field->SetTextAngle( ANGLE_VERTICAL );
1389 else
1390 SCH_PARSE_ERROR( "symbol field orientation must be H or V", aReader, line );
1391
1392 if( name.IsEmpty() )
1393 {
1394 if( field->IsMandatory() )
1396 else
1397 name = GetUserFieldName( legacy_field_id, UNTRANSLATED );
1398 }
1399
1400 field->SetName( name );
1401 }
1402 else if( strCompare( "$EndComp", line ) )
1403 {
1404 if( !m_appending )
1405 {
1407 {
1409 path.push_back( m_rootSheet->GetScreen()->GetUuid() );
1410
1411 SCH_SYMBOL_INSTANCE instance;
1412 instance.m_Path = path;
1413 instance.m_Reference = symbol->GetField( FIELD_T::REFERENCE )->GetText();
1414 instance.m_Unit = symbol->GetUnit();
1415 symbol->AddHierarchicalReference( instance );
1416 }
1417 else
1418 {
1419 for( const SCH_SYMBOL_INSTANCE& instance : symbol->GetInstances() )
1420 {
1421 SCH_SYMBOL_INSTANCE tmpInstance = instance;
1422 symbol->AddHierarchicalReference( tmpInstance );
1423 }
1424 }
1425 }
1426
1427 // Ensure all flags (some are set by previous initializations) are reset:
1428 symbol->ClearFlags();
1429 return symbol.release();
1430 }
1431 else
1432 {
1433 // There are two lines that begin with a tab or spaces that includes a line with the
1434 // redundant position information and the transform matrix settings.
1435
1436 // Parse the redundant position information just the same to check for formatting
1437 // errors.
1438 parseInt( aReader, line, &line ); // Always 1.
1439 parseInt( aReader, line, &line ); // The X coordinate.
1440 parseInt( aReader, line, &line ); // The Y coordinate.
1441
1442 line = aReader.ReadLine();
1443
1444 TRANSFORM transform;
1445
1446 transform.x1 = parseInt( aReader, line, &line );
1447
1448 if( transform.x1 < -1 || transform.x1 > 1 )
1449 SCH_PARSE_ERROR( "invalid symbol X1 transform value", aReader, line );
1450
1451 transform.y1 = -parseInt( aReader, line, &line );
1452
1453 if( transform.y1 < -1 || transform.y1 > 1 )
1454 SCH_PARSE_ERROR( "invalid symbol Y1 transform value", aReader, line );
1455
1456 transform.x2 = parseInt( aReader, line, &line );
1457
1458 if( transform.x2 < -1 || transform.x2 > 1 )
1459 SCH_PARSE_ERROR( "invalid symbol X2 transform value", aReader, line );
1460
1461 transform.y2 = -parseInt( aReader, line, &line );
1462
1463 if( transform.y2 < -1 || transform.y2 > 1 )
1464 SCH_PARSE_ERROR( "invalid symbol Y2 transform value", aReader, line );
1465
1466 symbol->SetTransform( transform );
1467 }
1468
1469 line = aReader.ReadLine();
1470 }
1471
1472 SCH_PARSE_ERROR( "invalid symbol line", aReader, line );
1473
1474 return nullptr; // Prevents compiler warning. Should never get here.
1475}
1476
1477
1478std::shared_ptr<BUS_ALIAS> SCH_IO_KICAD_LEGACY::loadBusAlias( LINE_READER& aReader,
1479 SCH_SCREEN* aScreen )
1480{
1481 // BUS_ALIAS does not take a SCH_SCREEN* in its constructor; create a default instance
1482 auto busAlias = std::make_shared<BUS_ALIAS>();
1483 const char* line = aReader.Line();
1484
1485 wxCHECK( strCompare( "BusAlias", line, &line ), nullptr );
1486
1487 wxString buf;
1488 parseUnquotedString( buf, aReader, line, &line );
1489 busAlias->SetName( buf );
1490
1491 while( *line != '\0' )
1492 {
1493 buf.clear();
1494 parseUnquotedString( buf, aReader, line, &line, true );
1495
1496 if( !buf.IsEmpty() )
1497 busAlias->AddMember( buf );
1498 }
1499
1500 return busAlias;
1501}
1502
1503
1504void SCH_IO_KICAD_LEGACY::SaveSchematicFile( const wxString& aFileName, SCH_SHEET* aSheet,
1505 SCHEMATIC* aSchematic,
1506 const std::map<std::string, UTF8>* aProperties )
1507{
1508 wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET object." );
1509 wxCHECK_RET( !aFileName.IsEmpty(), "No schematic file name defined." );
1510
1511 init( aSchematic, aProperties );
1512
1513 wxFileName fn = aFileName;
1514
1515 // File names should be absolute. Don't assume everything relative to the project path
1516 // works properly.
1517 wxASSERT( fn.IsAbsolute() );
1518
1519 FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
1520
1521 m_out = &formatter; // no ownership
1522
1523 Format( aSheet );
1524 formatter.Finish();
1525
1526 aSheet->GetScreen()->SetFileExists( true );
1527}
1528
1529
1531{
1532 wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET* object." );
1533 wxCHECK_RET( m_schematic != nullptr, "NULL SCHEMATIC* object." );
1534
1535 SCH_SCREEN* screen = aSheet->GetScreen();
1536
1537 wxCHECK( screen, /* void */ );
1538
1539 // Write the header
1540 m_out->Print( 0, "%s %s %d\n", "EESchema", SCHEMATIC_HEAD_STRING, EESCHEMA_VERSION );
1541
1542 // This section is not used, but written for file compatibility
1543 m_out->Print( 0, "EELAYER %d %d\n", SCH_LAYER_ID_COUNT, 0 );
1544 m_out->Print( 0, "EELAYER END\n" );
1545
1546 /* Write page info, ScreenNumber and NumberOfScreen; not very meaningful for
1547 * SheetNumber and Sheet Count in a complex hierarchy, but useful in
1548 * simple hierarchy and flat hierarchy. Used also to search the root
1549 * sheet ( ScreenNumber = 1 ) within the files
1550 */
1551 const TITLE_BLOCK& tb = screen->GetTitleBlock();
1552 const PAGE_INFO& page = screen->GetPageSettings();
1553
1554 m_out->Print( 0, "$Descr %s %d %d%s\n", TO_UTF8( page.GetTypeAsString() ),
1555 (int)page.GetWidthMils(),
1556 (int)page.GetHeightMils(),
1557 !page.IsCustom() && page.IsPortrait() ? " portrait" : "" );
1558 m_out->Print( 0, "encoding utf-8\n" );
1559 m_out->Print( 0, "Sheet %d %d\n", screen->GetVirtualPageNumber(), screen->GetPageCount() );
1560 m_out->Print( 0, "Title %s\n", EscapedUTF8( tb.GetTitle() ).c_str() );
1561 m_out->Print( 0, "Date %s\n", EscapedUTF8( tb.GetDate() ).c_str() );
1562 m_out->Print( 0, "Rev %s\n", EscapedUTF8( tb.GetRevision() ).c_str() );
1563 m_out->Print( 0, "Comp %s\n", EscapedUTF8( tb.GetCompany() ).c_str() );
1564 m_out->Print( 0, "Comment1 %s\n", EscapedUTF8( tb.GetComment( 0 ) ).c_str() );
1565 m_out->Print( 0, "Comment2 %s\n", EscapedUTF8( tb.GetComment( 1 ) ).c_str() );
1566 m_out->Print( 0, "Comment3 %s\n", EscapedUTF8( tb.GetComment( 2 ) ).c_str() );
1567 m_out->Print( 0, "Comment4 %s\n", EscapedUTF8( tb.GetComment( 3 ) ).c_str() );
1568 m_out->Print( 0, "Comment5 %s\n", EscapedUTF8( tb.GetComment( 4 ) ).c_str() );
1569 m_out->Print( 0, "Comment6 %s\n", EscapedUTF8( tb.GetComment( 5 ) ).c_str() );
1570 m_out->Print( 0, "Comment7 %s\n", EscapedUTF8( tb.GetComment( 6 ) ).c_str() );
1571 m_out->Print( 0, "Comment8 %s\n", EscapedUTF8( tb.GetComment( 7 ) ).c_str() );
1572 m_out->Print( 0, "Comment9 %s\n", EscapedUTF8( tb.GetComment( 8 ) ).c_str() );
1573 m_out->Print( 0, "$EndDescr\n" );
1574
1575 // Enforce item ordering
1576 auto cmp = []( const SCH_ITEM* a, const SCH_ITEM* b ) { return *a < *b; };
1577 std::multiset<SCH_ITEM*, decltype( cmp )> save_map( cmp );
1578
1579 for( SCH_ITEM* item : screen->Items() )
1580 save_map.insert( item );
1581
1582
1583 for( auto& item : save_map )
1584 {
1585 switch( item->Type() )
1586 {
1587 case SCH_SYMBOL_T:
1588 saveSymbol( static_cast<SCH_SYMBOL*>( item ) );
1589 break;
1590 case SCH_BITMAP_T:
1591 saveBitmap( static_cast<const SCH_BITMAP&>( *item ) );
1592 break;
1593 case SCH_SHEET_T:
1594 saveSheet( static_cast<SCH_SHEET*>( item ) );
1595 break;
1596 case SCH_JUNCTION_T:
1597 saveJunction( static_cast<SCH_JUNCTION*>( item ) );
1598 break;
1599 case SCH_NO_CONNECT_T:
1600 saveNoConnect( static_cast<SCH_NO_CONNECT*>( item ) );
1601 break;
1604 saveBusEntry( static_cast<SCH_BUS_ENTRY_BASE*>( item ) );
1605 break;
1606 case SCH_LINE_T:
1607 saveLine( static_cast<SCH_LINE*>( item ) );
1608 break;
1609 case SCH_TEXT_T:
1610 case SCH_LABEL_T:
1611 case SCH_GLOBAL_LABEL_T:
1612 case SCH_HIER_LABEL_T:
1613 saveText( static_cast<SCH_TEXT*>( item ) );
1614 break;
1615 default:
1616 wxASSERT( "Unexpected schematic object type in SCH_IO_KICAD_LEGACY::Format()" );
1617 }
1618 }
1619
1620 m_out->Print( 0, "$EndSCHEMATC\n" );
1621}
1622
1623
1625{
1626 m_out = aFormatter;
1627
1628 for( unsigned i = 0; i < aSelection->GetSize(); ++i )
1629 {
1630 SCH_ITEM* item = (SCH_ITEM*) aSelection->GetItem( i );
1631
1632 switch( item->Type() )
1633 {
1634 case SCH_SYMBOL_T:
1635 saveSymbol( static_cast< SCH_SYMBOL* >( item ) );
1636 break;
1637 case SCH_BITMAP_T:
1638 saveBitmap( static_cast< const SCH_BITMAP& >( *item ) );
1639 break;
1640 case SCH_SHEET_T:
1641 saveSheet( static_cast< SCH_SHEET* >( item ) );
1642 break;
1643 case SCH_JUNCTION_T:
1644 saveJunction( static_cast< SCH_JUNCTION* >( item ) );
1645 break;
1646 case SCH_NO_CONNECT_T:
1647 saveNoConnect( static_cast< SCH_NO_CONNECT* >( item ) );
1648 break;
1651 saveBusEntry( static_cast< SCH_BUS_ENTRY_BASE* >( item ) );
1652 break;
1653 case SCH_LINE_T:
1654 saveLine( static_cast< SCH_LINE* >( item ) );
1655 break;
1656 case SCH_TEXT_T:
1657 case SCH_LABEL_T:
1658 case SCH_GLOBAL_LABEL_T:
1659 case SCH_HIER_LABEL_T:
1660 saveText( static_cast< SCH_TEXT* >( item ) );
1661 break;
1662 default:
1663 wxASSERT( "Unexpected schematic object type in SCH_IO_KICAD_LEGACY::Format()" );
1664 }
1665 }
1666}
1667
1668
1670{
1671 std::string name1;
1672 std::string name2;
1673
1674 // This is redundant with the AR entries below, but it makes the files backwards-compatible.
1675 if( aSymbol->GetInstances().size() > 0 )
1676 {
1677 const SCH_SYMBOL_INSTANCE& instance = aSymbol->GetInstances()[0];
1678 name1 = toUTFTildaText( instance.m_Reference );
1679 }
1680 else
1681 {
1682 if( aSymbol->GetField( FIELD_T::REFERENCE )->GetText().IsEmpty() )
1683 name1 = toUTFTildaText( aSymbol->GetPrefix() );
1684 else
1685 name1 = toUTFTildaText( aSymbol->GetField( FIELD_T::REFERENCE )->GetText() );
1686 }
1687
1688 wxString symbol_name = aSymbol->GetLibId().Format();
1689
1690 if( symbol_name.size() )
1691 {
1692 name2 = toUTFTildaText( symbol_name );
1693 }
1694 else
1695 {
1696 name2 = "_NONAME_";
1697 }
1698
1699 m_out->Print( 0, "$Comp\n" );
1700 m_out->Print( 0, "L %s %s\n", name2.c_str(), name1.c_str() );
1701
1702 // Generate unit number, conversion and timestamp
1703 m_out->Print( 0, "U %d %d %8.8X\n",
1704 aSymbol->GetUnit(),
1705 aSymbol->GetBodyStyle(),
1706 aSymbol->m_Uuid.AsLegacyTimestamp() );
1707
1708 // Save the position
1709 m_out->Print( 0, "P %d %d\n",
1710 schIUScale.IUToMils( aSymbol->GetPosition().x ),
1711 schIUScale.IUToMils( aSymbol->GetPosition().y ) );
1712
1713 /* If this is a complex hierarchy; save hierarchical references.
1714 * but for simple hierarchies it is not necessary.
1715 * the reference inf is already saved
1716 * this is useful for old Eeschema version compatibility
1717 */
1718 if( aSymbol->GetInstances().size() > 1 )
1719 {
1720 for( const SCH_SYMBOL_INSTANCE& instance : aSymbol->GetInstances() )
1721 {
1722 /*format:
1723 * AR Path="/140/2" Ref="C99" Part="1"
1724 * where 140 is the uid of the containing sheet and 2 is the timestamp of this symbol.
1725 * (timestamps are actually 8 hex chars)
1726 * Ref is the conventional symbol reference designator for this 'path'
1727 * Part is the conventional symbol unit selection for this 'path'
1728 */
1729 wxString path = "/";
1730
1731 // Skip root sheet
1732 for( int i = 1; i < (int) instance.m_Path.size(); ++i )
1733 path += instance.m_Path[i].AsLegacyTimestampString() + "/";
1734
1735 m_out->Print( 0, "AR Path=\"%s\" Ref=\"%s\" Part=\"%d\" \n",
1737 TO_UTF8( instance.m_Reference ),
1738 instance.m_Unit );
1739 }
1740 }
1741
1742 // NB: FieldIDs in legacy libraries must be consecutive, and include user fields
1743 int legacy_field_id = 0;
1744
1745 for( SCH_FIELD& field : aSymbol->GetFields() )
1746 saveField( &field, legacy_field_id++ );
1747
1748 // Unit number, position, box ( old standard )
1749 m_out->Print( 0, "\t%-4d %-4d %-4d\n", aSymbol->GetUnit(),
1750 schIUScale.IUToMils( aSymbol->GetPosition().x ),
1751 schIUScale.IUToMils( aSymbol->GetPosition().y ) );
1752
1753 TRANSFORM transform = aSymbol->GetTransform();
1754
1755 m_out->Print( 0, "\t%-4d %-4d %-4d %-4d\n",
1756 transform.x1, transform.y1, transform.x2, transform.y2 );
1757 m_out->Print( 0, "$EndComp\n" );
1758}
1759
1760
1761void SCH_IO_KICAD_LEGACY::saveField( SCH_FIELD* aField, int aLegacyId )
1762{
1763 char hjustify = 'C';
1764
1765 if( aField->GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT )
1766 hjustify = 'L';
1767 else if( aField->GetHorizJustify() == GR_TEXT_H_ALIGN_RIGHT )
1768 hjustify = 'R';
1769
1770 char vjustify = 'C';
1771
1772 if( aField->GetVertJustify() == GR_TEXT_V_ALIGN_BOTTOM )
1773 vjustify = 'B';
1774 else if( aField->GetVertJustify() == GR_TEXT_V_ALIGN_TOP )
1775 vjustify = 'T';
1776
1777 m_out->Print( 0, "F %d %s %c %-3d %-3d %-3d %4.4X %c %c%c%c",
1778 aLegacyId,
1779 EscapedUTF8( aField->GetText() ).c_str(), // wraps in quotes too
1780 aField->GetTextAngle().IsHorizontal() ? 'H' : 'V',
1781 schIUScale.IUToMils( aField->GetLibPosition().x ),
1782 schIUScale.IUToMils( aField->GetLibPosition().y ),
1783 schIUScale.IUToMils( aField->GetTextWidth() ),
1784 !aField->IsVisible(),
1785 hjustify, vjustify,
1786 aField->IsItalic() ? 'I' : 'N',
1787 aField->IsBold() ? 'B' : 'N' );
1788
1789 // Save field name, if the name is user definable
1790 if( !aField->IsMandatory() )
1791 m_out->Print( 0, " %s", EscapedUTF8( aField->GetName() ).c_str() );
1792
1793 m_out->Print( 0, "\n" );
1794}
1795
1796
1798{
1799 const REFERENCE_IMAGE& refImage = aBitmap.GetReferenceImage();
1800
1801 const wxImage* image = refImage.GetImage().GetImageData();
1802
1803 wxCHECK_RET( image != nullptr, "wxImage* is NULL" );
1804
1805 m_out->Print( 0, "$Bitmap\n" );
1806 m_out->Print( 0, "Pos %-4d %-4d\n",
1807 schIUScale.IUToMils( aBitmap.GetPosition().x ),
1808 schIUScale.IUToMils( aBitmap.GetPosition().y ) );
1809 m_out->Print( "%s", fmt::format("Scale {:g}\n", refImage.GetImageScale()).c_str() );
1810 m_out->Print( 0, "Data\n" );
1811
1812 wxMemoryOutputStream stream;
1813
1814 image->SaveFile( stream, wxBITMAP_TYPE_PNG );
1815
1816 // Write binary data in hexadecimal form (ASCII)
1817 wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
1818 char* begin = (char*) buffer->GetBufferStart();
1819
1820 for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ )
1821 {
1822 if( ii >= 32 )
1823 {
1824 ii = 0;
1825
1826 m_out->Print( 0, "\n" );
1827 }
1828
1829 m_out->Print( 0, "%2.2X ", *begin & 0xFF );
1830 }
1831
1832 m_out->Print( 0, "\nEndData\n" );
1833 m_out->Print( 0, "$EndBitmap\n" );
1834}
1835
1836
1838{
1839 wxCHECK_RET( aSheet != nullptr, "SCH_SHEET* is NULL" );
1840
1841 m_out->Print( 0, "$Sheet\n" );
1842 m_out->Print( 0, "S %-4d %-4d %-4d %-4d\n",
1843 schIUScale.IUToMils( aSheet->GetPosition().x ),
1844 schIUScale.IUToMils( aSheet->GetPosition().y ),
1845 schIUScale.IUToMils( aSheet->GetSize().x ),
1846 schIUScale.IUToMils( aSheet->GetSize().y ) );
1847
1848 m_out->Print( 0, "U %8.8X\n", aSheet->m_Uuid.AsLegacyTimestamp() );
1849
1850 SCH_FIELD* sheetName = aSheet->GetField( FIELD_T::SHEET_NAME );
1851 SCH_FIELD* fileName = aSheet->GetField( FIELD_T::SHEET_FILENAME );
1852
1853 if( !sheetName->GetText().IsEmpty() )
1854 {
1855 m_out->Print( 0, "F0 %s %d\n",
1856 EscapedUTF8( sheetName->GetText() ).c_str(),
1857 schIUScale.IUToMils( sheetName->GetTextSize().x ) );
1858 }
1859
1860 if( !fileName->GetText().IsEmpty() )
1861 {
1862 m_out->Print( 0, "F1 %s %d\n",
1863 EscapedUTF8( fileName->GetText() ).c_str(),
1864 schIUScale.IUToMils( fileName->GetTextSize().x ) );
1865 }
1866
1867 for( const SCH_SHEET_PIN* pin : aSheet->GetPins() )
1868 {
1869 int type, side;
1870
1871 switch( pin->GetSide() )
1872 {
1873 default:
1874 case SHEET_SIDE::LEFT: side = 'L'; break;
1875 case SHEET_SIDE::RIGHT: side = 'R'; break;
1876 case SHEET_SIDE::TOP: side = 'T'; break;
1877 case SHEET_SIDE::BOTTOM: side = 'B'; break;
1878 }
1879
1880 switch( pin->GetShape() )
1881 {
1882 default:
1883 case LABEL_FLAG_SHAPE::L_UNSPECIFIED: type = 'U'; break;
1884 case LABEL_FLAG_SHAPE::L_INPUT: type = 'I'; break;
1885 case LABEL_FLAG_SHAPE::L_OUTPUT: type = 'O'; break;
1886 case LABEL_FLAG_SHAPE::L_BIDI: type = 'B'; break;
1887 case LABEL_FLAG_SHAPE::L_TRISTATE: type = 'T'; break;
1888 }
1889
1890 m_out->Print( 0, "F%d %s %c %c %-3d %-3d %-3d\n",
1891 pin->GetNumber(),
1892 EscapedUTF8( pin->GetText() ).c_str(), // supplies wrapping quotes
1893 type, side, schIUScale.IUToMils( pin->GetPosition().x ),
1894 schIUScale.IUToMils( pin->GetPosition().y ),
1895 schIUScale.IUToMils( pin->GetTextWidth() ) );
1896 }
1897
1898 m_out->Print( 0, "$EndSheet\n" );
1899}
1900
1901
1903{
1904 wxCHECK_RET( aJunction != nullptr, "SCH_JUNCTION* is NULL" );
1905
1906 m_out->Print( 0, "Connection ~ %-4d %-4d\n",
1907 schIUScale.IUToMils( aJunction->GetPosition().x ),
1908 schIUScale.IUToMils( aJunction->GetPosition().y ) );
1909}
1910
1911
1913{
1914 wxCHECK_RET( aNoConnect != nullptr, "SCH_NOCONNECT* is NULL" );
1915
1916 m_out->Print( 0, "NoConn ~ %-4d %-4d\n",
1917 schIUScale.IUToMils( aNoConnect->GetPosition().x ),
1918 schIUScale.IUToMils( aNoConnect->GetPosition().y ) );
1919}
1920
1921
1923{
1924 wxCHECK_RET( aBusEntry != nullptr, "SCH_BUS_ENTRY_BASE* is NULL" );
1925
1926 if( aBusEntry->GetLayer() == LAYER_WIRE )
1927 {
1928 m_out->Print( 0, "Entry Wire Line\n\t%-4d %-4d %-4d %-4d\n",
1929 schIUScale.IUToMils( aBusEntry->GetPosition().x ),
1930 schIUScale.IUToMils( aBusEntry->GetPosition().y ),
1931 schIUScale.IUToMils( aBusEntry->GetEnd().x ),
1932 schIUScale.IUToMils( aBusEntry->GetEnd().y ) );
1933 }
1934 else
1935 {
1936 m_out->Print( 0, "Entry Bus Bus\n\t%-4d %-4d %-4d %-4d\n",
1937 schIUScale.IUToMils( aBusEntry->GetPosition().x ),
1938 schIUScale.IUToMils( aBusEntry->GetPosition().y ),
1939 schIUScale.IUToMils( aBusEntry->GetEnd().x ),
1940 schIUScale.IUToMils( aBusEntry->GetEnd().y ) );
1941 }
1942}
1943
1944
1946{
1947 wxCHECK_RET( aLine != nullptr, "SCH_LINE* is NULL" );
1948
1949 const char* layer = "Notes";
1950 const char* width = "Line";
1951
1952 if( aLine->GetLayer() == LAYER_WIRE )
1953 layer = "Wire";
1954 else if( aLine->GetLayer() == LAYER_BUS )
1955 layer = "Bus";
1956
1957 m_out->Print( 0, "Wire %s %s", layer, width );
1958
1959 // Write line style (width, type, color) only for non default values
1960 if( aLine->IsGraphicLine() )
1961 {
1962 const STROKE_PARAMS& stroke = aLine->GetStroke();
1963
1964 if( stroke.GetWidth() != 0 )
1965 m_out->Print( 0, " %s %d", T_WIDTH, schIUScale.IUToMils( stroke.GetWidth() ) );
1966
1967 m_out->Print( 0, " %s %s",
1968 T_STYLE,
1970
1971 if( stroke.GetColor() != COLOR4D::UNSPECIFIED )
1972 m_out->Print( 0, " %s", TO_UTF8( stroke.GetColor().ToCSSString() ) );
1973 }
1974
1975 m_out->Print( 0, "\n" );
1976
1977 m_out->Print( 0, "\t%-4d %-4d %-4d %-4d",
1978 schIUScale.IUToMils( aLine->GetStartPoint().x ),
1979 schIUScale.IUToMils( aLine->GetStartPoint().y ),
1980 schIUScale.IUToMils( aLine->GetEndPoint().x ),
1981 schIUScale.IUToMils( aLine->GetEndPoint().y ) );
1982
1983 m_out->Print( 0, "\n");
1984}
1985
1986
1988{
1989 wxCHECK_RET( aText != nullptr, "SCH_TEXT* is NULL" );
1990
1991 const char* italics = "~";
1992 const char* textType = "Notes";
1993
1994 if( aText->IsItalic() )
1995 italics = "Italic";
1996
1997 wxString text = aText->GetText();
1998
1999 SCH_LAYER_ID layer = aText->GetLayer();
2000
2001 if( layer == LAYER_NOTES || layer == LAYER_LOCLABEL )
2002 {
2003 if( layer == LAYER_NOTES )
2004 {
2005 // For compatibility reasons, the text must be saved in only one text line
2006 // so replace all EOLs with \\n
2007 text.Replace( wxT( "\n" ), wxT( "\\n" ) );
2008
2009 // Here we should have no CR or LF character in line
2010 // This is not always the case if a multiline text was copied (using a copy/paste
2011 // function) from a text that uses E.O.L characters that differs from the current
2012 // EOL format. This is mainly the case under Linux using LF symbol when copying
2013 // a text from Windows (using CRLF symbol) so we must just remove the extra CR left
2014 // (or LF left under MacOSX)
2015 for( unsigned ii = 0; ii < text.Len(); )
2016 {
2017 if( text[ii] == 0x0A || text[ii] == 0x0D )
2018 text.erase( ii, 1 );
2019 else
2020 ii++;
2021 }
2022 }
2023 else
2024 {
2025 textType = "Label";
2026 }
2027
2028 int spinStyle = 0;
2029
2030 // Local labels must have their spin style inverted for left and right
2031 if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( aText ) )
2032 {
2033 spinStyle = static_cast<int>( label->GetSpinStyle() );
2034
2035 if( spinStyle == 0 )
2036 spinStyle = 2;
2037 else if( spinStyle == 2 )
2038 spinStyle = 0;
2039 }
2040
2041 m_out->Print( 0, "Text %s %-4d %-4d %-4d %-4d %s %d\n%s\n", textType,
2042 schIUScale.IUToMils( aText->GetPosition().x ),
2043 schIUScale.IUToMils( aText->GetPosition().y ),
2044 spinStyle,
2045 schIUScale.IUToMils( aText->GetTextWidth() ),
2046 italics, schIUScale.IUToMils( aText->GetTextThickness() ), TO_UTF8( text ) );
2047 }
2048 else if( layer == LAYER_GLOBLABEL || layer == LAYER_HIERLABEL )
2049 {
2050 textType = ( layer == LAYER_GLOBLABEL ) ? "GLabel" : "HLabel";
2051
2052 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( aText );
2053 auto shapeLabelIt = sheetLabelNames.find( label->GetShape() );
2054 wxCHECK_RET( shapeLabelIt != sheetLabelNames.end(), "Shape not found in names list" );
2055
2056 m_out->Print( 0, "Text %s %-4d %-4d %-4d %-4d %s %s %d\n%s\n", textType,
2057 schIUScale.IUToMils( aText->GetPosition().x ),
2058 schIUScale.IUToMils( aText->GetPosition().y ),
2059 static_cast<int>( label->GetSpinStyle() ),
2060 schIUScale.IUToMils( aText->GetTextWidth() ),
2061 shapeLabelIt->second,
2062 italics,
2063 schIUScale.IUToMils( aText->GetTextThickness() ), TO_UTF8( text ) );
2064 }
2065}
2066
2067
2068void SCH_IO_KICAD_LEGACY::saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias )
2069{
2070 wxCHECK_RET( aAlias != nullptr, "BUS_ALIAS* is NULL" );
2071
2072 wxString members = boost::algorithm::join( aAlias->Members(), " " );
2073
2074 m_out->Print( 0, "BusAlias %s %s\n",
2075 TO_UTF8( aAlias->GetName() ), TO_UTF8( members ) );
2076}
2077
2078
2079void SCH_IO_KICAD_LEGACY::cacheLib( const wxString& aLibraryFileName,
2080 const std::map<std::string, UTF8>* aProperties )
2081{
2082 if( !m_cache || !m_cache->IsFile( aLibraryFileName ) || m_cache->IsFileChanged() )
2083 {
2084 // a spectacular episode in memory management:
2085 delete m_cache;
2086 m_cache = new SCH_IO_KICAD_LEGACY_LIB_CACHE( aLibraryFileName );
2087
2088 if( !isBuffering( aProperties ) )
2089 m_cache->Load();
2090 }
2091}
2092
2093
2094bool SCH_IO_KICAD_LEGACY::writeDocFile( const std::map<std::string, UTF8>* aProperties )
2095{
2096 std::string propName( SCH_IO_KICAD_LEGACY::PropNoDocFile );
2097
2098 if( aProperties && aProperties->find( propName ) != aProperties->end() )
2099 return false;
2100
2101 return true;
2102}
2103
2104
2105bool SCH_IO_KICAD_LEGACY::isBuffering( const std::map<std::string, UTF8>* aProperties )
2106{
2107 return ( aProperties && aProperties->contains( SCH_IO_KICAD_LEGACY::PropBuffering ) );
2108}
2109
2110
2112{
2113 if( m_cache )
2114 return m_cache->GetModifyHash();
2115
2116 // If the cache hasn't been loaded, it hasn't been modified.
2117 return 0;
2118}
2119
2120
2121void SCH_IO_KICAD_LEGACY::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
2122 const wxString& aLibraryPath,
2123 const std::map<std::string, UTF8>* aProperties )
2124{
2125 bool powerSymbolsOnly = ( aProperties && aProperties->contains( SYMBOL_LIBRARY_ADAPTER::PropPowerSymsOnly ) );
2126
2127 cacheLib( aLibraryPath, aProperties );
2128
2129 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
2130
2131 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
2132 {
2133 if( !powerSymbolsOnly || it->second->IsGlobalPower() )
2134 aSymbolNameList.Add( it->first );
2135 }
2136}
2137
2138
2139void SCH_IO_KICAD_LEGACY::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
2140 const wxString& aLibraryPath,
2141 const std::map<std::string, UTF8>* aProperties )
2142{
2143 bool powerSymbolsOnly = ( aProperties && aProperties->contains( SYMBOL_LIBRARY_ADAPTER::PropPowerSymsOnly ) );
2144
2145 cacheLib( aLibraryPath, aProperties );
2146
2147 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
2148
2149 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
2150 {
2151 if( !powerSymbolsOnly || it->second->IsGlobalPower() )
2152 aSymbolList.push_back( it->second );
2153 }
2154}
2155
2156
2157LIB_SYMBOL* SCH_IO_KICAD_LEGACY::LoadSymbol( const wxString& aLibraryPath,
2158 const wxString& aSymbolName,
2159 const std::map<std::string, UTF8>* aProperties )
2160{
2161 cacheLib( aLibraryPath, aProperties );
2162
2163 LIB_SYMBOL_MAP::const_iterator it = m_cache->m_symbols.find( aSymbolName );
2164
2165 if( it == m_cache->m_symbols.end() )
2166 return nullptr;
2167
2168 return it->second;
2169}
2170
2171
2172void SCH_IO_KICAD_LEGACY::SaveSymbol( const wxString& aLibraryPath, std::unique_ptr<LIB_SYMBOL> aSymbol,
2173 const std::map<std::string, UTF8>* aProperties )
2174{
2175 cacheLib( aLibraryPath, aProperties );
2176
2177 m_cache->AddSymbol( std::move( aSymbol ) );
2178
2179 if( !isBuffering( aProperties ) )
2180 m_cache->Save( writeDocFile( aProperties ) );
2181}
2182
2183
2184void SCH_IO_KICAD_LEGACY::DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
2185 const std::map<std::string, UTF8>* aProperties )
2186{
2187 cacheLib( aLibraryPath, aProperties );
2188
2189 m_cache->DeleteSymbol( aSymbolName );
2190
2191 if( !isBuffering( aProperties ) )
2192 m_cache->Save( writeDocFile( aProperties ) );
2193}
2194
2195
2196void SCH_IO_KICAD_LEGACY::CreateLibrary( const wxString& aLibraryPath,
2197 const std::map<std::string, UTF8>* aProperties )
2198{
2199 if( wxFileExists( aLibraryPath ) )
2200 THROW_IO_ERRORF( _( "Symbol library '%s' already exists." ), aLibraryPath.GetData() );
2201
2202 delete m_cache;
2203 m_cache = new SCH_IO_KICAD_LEGACY_LIB_CACHE( aLibraryPath );
2204 m_cache->SetModified();
2205 m_cache->Save( writeDocFile( aProperties ) );
2206 m_cache->Load(); // update m_writable and m_timestamp
2207}
2208
2209
2210bool SCH_IO_KICAD_LEGACY::DeleteLibrary( const wxString& aLibraryPath,
2211 const std::map<std::string, UTF8>* aProperties )
2212{
2213 wxFileName fn = aLibraryPath;
2214
2215 if( !fn.FileExists() )
2216 return false;
2217
2218 // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
2219 // we don't want that. we want bare metal portability with no UI here.
2220 if( wxRemove( aLibraryPath ) )
2221 THROW_IO_ERRORF( _( "Symbol library '%s' cannot be deleted." ), aLibraryPath.GetData() );
2222
2223 if( m_cache && m_cache->IsFile( aLibraryPath ) )
2224 {
2225 delete m_cache;
2226 m_cache = nullptr;
2227 }
2228
2229 return true;
2230}
2231
2232
2233void SCH_IO_KICAD_LEGACY::SaveLibrary( const wxString& aLibraryPath,
2234 const std::map<std::string, UTF8>* aProperties )
2235{
2236 if( !m_cache )
2237 m_cache = new SCH_IO_KICAD_LEGACY_LIB_CACHE( aLibraryPath );
2238
2239 wxString oldFileName = m_cache->GetFileName();
2240
2241 if( !m_cache->IsFile( aLibraryPath ) )
2242 {
2243 m_cache->SetFileName( aLibraryPath );
2244 }
2245
2246 // This is a forced save.
2247 m_cache->SetModified();
2248 m_cache->Save( writeDocFile( aProperties ) );
2249 m_cache->SetFileName( oldFileName );
2250}
2251
2252
2253bool SCH_IO_KICAD_LEGACY::CanReadSchematicFile( const wxString& aFileName ) const
2254{
2255 if( !SCH_IO::CanReadSchematicFile( aFileName ) )
2256 return false;
2257
2258 return IO_UTILS::fileStartsWithPrefix( aFileName, wxT( "EESchema" ), true );
2259}
2260
2261
2262bool SCH_IO_KICAD_LEGACY::CanReadLibrary( const wxString& aFileName ) const
2263{
2264 if( !SCH_IO::CanReadLibrary( aFileName ) )
2265 return false;
2266
2267 return IO_UTILS::fileStartsWithPrefix( aFileName, wxT( "EESchema" ), true );
2268}
2269
2270
2271bool SCH_IO_KICAD_LEGACY::IsLibraryWritable( const wxString& aLibraryPath )
2272{
2273 // Writing legacy symbol libraries is deprecated.
2274 return false;
2275}
2276
2277
2279 int aMinorVersion )
2280{
2281 return SCH_IO_KICAD_LEGACY_LIB_CACHE::LoadPart( reader, aMajorVersion, aMinorVersion );
2282}
2283
2284
2286{
2287 SCH_IO_KICAD_LEGACY_LIB_CACHE::SaveSymbol( symbol, formatter );
2288}
2289
2290
2291
2292const char* SCH_IO_KICAD_LEGACY::PropBuffering = "buffering";
2293const char* SCH_IO_KICAD_LEGACY::PropNoDocFile = "no_doc_file";
const char * name
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:130
void SetPageCount(int aPageCount)
int GetPageCount() const
Definition base_screen.h:68
int GetVirtualPageNumber() const
Definition base_screen.h:71
void SetVirtualPageNumber(int aPageNumber)
Definition base_screen.h:72
This class handle bitmap images in KiCad.
Definition bitmap_base.h:45
int GetPPI() const
wxImage * GetImageData()
Definition bitmap_base.h:64
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:400
bool IsHorizontal() const
Definition eda_angle.h:141
const KIID m_Uuid
Definition eda_item.h:599
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:153
virtual VECTOR2I GetTextSize() const
Definition eda_text.h:301
bool IsItalic() const
Definition eda_text.h:200
virtual void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition eda_text.cpp:512
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:118
virtual bool IsVisible() const
Definition eda_text.h:226
virtual void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:556
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition eda_text.cpp:378
virtual int GetTextWidth() const
Definition eda_text.h:304
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition eda_text.h:239
void SetBoldFlag(bool aBold)
Set only the bold flag, without changing the font.
Definition eda_text.cpp:324
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:347
virtual EDA_ANGLE GetTextAngle() const
Definition eda_text.h:178
void SetItalicFlag(bool aItalic)
Set only the italic flag, without changing the font.
Definition eda_text.cpp:302
bool IsBold() const
Definition eda_text.h:215
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:242
virtual int GetTextThickness() const
Definition eda_text.h:159
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition eda_text.cpp:268
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition eda_text.cpp:370
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:248
A LINE_READER that reads from an open file.
Definition richio.h:157
void Rewind()
Rewind the file and resets the line number back to zero.
Definition richio.h:206
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:202
Used for text file output.
Definition richio.h:483
bool Finish() override
Flushes the temp file to disk and atomically renames it over the final target path.
Definition richio.cpp:682
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:241
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition io_base.cpp:71
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
wxString ToCSSString() const
Definition color4d.cpp:146
Definition kiid.h:46
wxString AsLegacyTimestampString() const
Definition kiid.cpp:284
timestamp_t AsLegacyTimestamp() const
Definition kiid.cpp:218
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition lib_id.cpp:124
UTF8 Format() const
Definition lib_id.cpp:132
Define a library symbol object.
Definition lib_symbol.h:114
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition richio.h:65
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
virtual const wxString & GetSource() const
Returns the name of the source of the lines in an abstract sense.
Definition richio.h:93
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition richio.h:119
char * Line() const
Return a pointer to the last line that was read in.
Definition richio.h:101
An interface used to output 8 bit text in a convenient way.
Definition richio.h:294
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:75
wxString GetTypeAsString() const
void SetPortrait(bool aIsPortrait)
Rotate the paper page 90 degrees.
bool SetType(PAGE_SIZE_TYPE aPageSize, bool aIsPortrait=false)
Set the name of the page type and also the sizes and margins commonly associated with that type name.
void SetHeightMils(double aHeightInMils)
double GetHeightMils() const
Definition page_info.h:142
double GetWidthMils() const
Definition page_info.h:137
bool IsCustom() const
bool IsPortrait() const
Definition page_info.h:123
void SetWidthMils(double aWidthInMils)
const PAGE_SIZE_TYPE & GetType() const
Definition page_info.h:97
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition project.cpp:183
A REFERENCE_IMAGE is a wrapper around a BITMAP_IMAGE that is displayed in an editor as a reference fo...
bool ReadImageFile(const wxString &aFullFilename)
Read and store an image file.
const BITMAP_BASE & GetImage() const
Get the underlying image.
double GetImageScale() const
void SetImageScale(double aScale)
Set the image "zoom" value.
Holds all the data relating to one schematic.
Definition schematic.h:149
PROJECT & Project() const
Return a reference to the project this schematic is part of.
Definition schematic.h:171
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition schematic.h:289
bool HasProjectBusAliases() const
SCH_SHEET & Root() const
Definition schematic.h:200
Object to handle a bitmap image that can be inserted in a schematic.
Definition sch_bitmap.h:36
VECTOR2I GetPosition() const override
REFERENCE_IMAGE & GetReferenceImage()
Definition sch_bitmap.h:50
Base class for a bus or wire entry.
VECTOR2I GetPosition() const override
VECTOR2I GetEnd() const
bool IsMandatory() const
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:139
FIELD_T GetId() const
Definition sch_field.h:143
VECTOR2I GetLibPosition() const
Definition sch_field.h:287
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
void SetName(const wxString &aName)
void SetText(const wxString &aText) override
A cache assistant for KiCad legacy symbol libraries.
static LIB_SYMBOL * LoadPart(LINE_READER &aReader, int aMajorVersion, int aMinorVersion, LIB_SYMBOL_MAP *aMap=nullptr)
static void SaveSymbol(LIB_SYMBOL *aSymbol, OUTPUTFORMATTER &aFormatter, LIB_SYMBOL_MAP *aMap=nullptr)
wxString m_error
For throwing exceptions or errors on partial schematic loads.
SCH_SHEET * m_currentSheet
The sheet currently being loaded.
void loadFile(const wxString &aFileName, SCH_SCREEN *aScreen)
void saveBusAlias(std::shared_ptr< BUS_ALIAS > aAlias)
void SaveSchematicFile(const wxString &aFileName, SCH_SHEET *aScreen, SCHEMATIC *aSchematic, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aSchematic to a storage file in a format that this SCH_IO implementation knows about,...
void saveText(SCH_TEXT *aText)
void saveJunction(SCH_JUNCTION *aJunction)
void loadPageSettings(LINE_READER &aReader, SCH_SCREEN *aScreen)
OUTPUTFORMATTER * m_out
The formatter for saving SCH_SCREEN objects.
void saveField(SCH_FIELD *aField, int aLegacyId)
bool isBuffering(const std::map< std::string, UTF8 > *aProperties)
void Format(SCH_SHEET *aSheet)
void loadHierarchy(SCH_SHEET *aSheet)
void saveBusEntry(SCH_BUS_ENTRY_BASE *aBusEntry)
SCH_SYMBOL * loadSymbol(LINE_READER &aReader)
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
std::stack< wxString > m_currentPath
Stack to maintain nested sheet paths.
int GetModifyHash() const override
Return the modification hash from the library cache.
wxString m_path
Root project path for loading child sheets.
void LoadContent(LINE_READER &aReader, SCH_SCREEN *aScreen, int version=EESCHEMA_VERSION)
void loadHeader(LINE_READER &aReader, SCH_SCREEN *aScreen)
SCH_TEXT * loadText(LINE_READER &aReader)
void init(SCHEMATIC *aSchematic, const std::map< std::string, UTF8 > *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 ...
static void FormatPart(LIB_SYMBOL *aSymbol, OUTPUTFORMATTER &aFormatter)
SCH_NO_CONNECT * loadNoConnect(LINE_READER &aReader)
bool m_appending
Indicate if we are appending the loaded schemitic or loading a full project.
int m_version
Version of file being loaded.
bool CanReadSchematicFile(const wxString &aFileName) const override
Checks if this SCH_IO can read the specified schematic file.
void saveLine(SCH_LINE *aLine)
SCH_BUS_ENTRY_BASE * loadBusEntry(LINE_READER &aReader)
SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load information from some input file format that this SCH_IO implementation knows about,...
void saveSymbol(SCH_SYMBOL *aSymbol)
SCH_SHEET * loadSheet(LINE_READER &aReader)
SCH_LINE * loadWire(LINE_READER &aReader)
void SaveSymbol(const wxString &aLibraryPath, std::unique_ptr< LIB_SYMBOL > aSymbol, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aSymbol to an existing library located at aLibraryPath.
void saveNoConnect(SCH_NO_CONNECT *aNoConnect)
unsigned m_lineCount
for progress reporting
void DeleteSymbol(const wxString &aLibraryPath, const wxString &aSymbolName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Delete the entire LIB_SYMBOL associated with aAliasName from the library aLibraryPath.
void SaveLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
std::shared_ptr< BUS_ALIAS > loadBusAlias(LINE_READER &aReader, SCH_SCREEN *aScreen)
SCH_IO_KICAD_LEGACY_LIB_CACHE * m_cache
SCH_BITMAP * loadBitmap(LINE_READER &aReader)
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
static LIB_SYMBOL * ParsePart(LINE_READER &aReader, int majorVersion=0, int minorVersion=0)
void cacheLib(const wxString &aLibraryFileName, const std::map< std::string, UTF8 > *aProperties)
SCH_SHEET * m_rootSheet
The root sheet of the schematic being loaded.
static const char * PropNoDocFile
The property used internally by the plugin to disable writing the library documentation (....
SCH_JUNCTION * loadJunction(LINE_READER &aReader)
void saveBitmap(const SCH_BITMAP &aBitmap)
void saveSheet(SCH_SHEET *aSheet)
bool IsLibraryWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
void CreateLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Create a new empty library at aLibraryPath empty.
bool CanReadLibrary(const wxString &aFileName) const override
Checks if this IO object can read the specified library file/directory.
bool DeleteLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Delete an existing library and returns true, or if library does not exist returns false,...
bool writeDocFile(const std::map< std::string, UTF8 > *aProperties)
LINE_READER * m_lineReader
for progress reporting
virtual bool CanReadSchematicFile(const wxString &aFileName) const
Checks if this SCH_IO can read the specified schematic file.
Definition sch_io.cpp:47
SCH_IO(const wxString &aName)
Definition sch_io.h:406
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
int GetBodyStyle() const
Definition sch_item.h:253
int GetUnit() const
Definition sch_item.h:243
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition sch_item.h:353
VECTOR2I GetPosition() const override
SPIN_STYLE GetSpinStyle() const
void SetShape(LABEL_FLAG_SHAPE aShape)
Definition sch_label.h:179
LABEL_FLAG_SHAPE GetShape() const
Definition sch_label.h:178
virtual void SetSpinStyle(SPIN_STYLE aSpinStyle)
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:39
virtual STROKE_PARAMS GetStroke() const override
Definition sch_line.h:198
VECTOR2I GetEndPoint() const
Definition sch_line.h:145
VECTOR2I GetStartPoint() const
Definition sch_line.h:136
bool IsGraphicLine() const
Return if the line is a graphic (non electrical line)
VECTOR2I GetPosition() const override
const PAGE_INFO & GetPageSettings() const
Definition sch_screen.h:144
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock)
Definition sch_screen.h:171
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
void AddBusAlias(std::shared_ptr< BUS_ALIAS > aAlias)
Add a bus alias definition.
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition sch_screen.h:145
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:122
const wxString & GetFileName() const
Definition sch_screen.h:157
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
SCHEMATIC * Schematic() const
TITLE_BLOCK & GetTitleBlock()
Definition sch_screen.h:168
void SetFileReadOnly(bool aIsReadOnly)
Definition sch_screen.h:159
void SetFileExists(bool aFileExists)
Definition sch_screen.h:162
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition sch_sheet.h:386
void SyncUuidToScreen()
Take the identity of the screen this sheet owns.
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this sheet.
VECTOR2I GetSize() const
Definition sch_sheet.h:147
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:145
VECTOR2I GetPosition() const override
Definition sch_sheet.h:506
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition sch_sheet.h:243
Schematic symbol object.
Definition sch_symbol.h:73
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition sch_symbol.h:133
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
VECTOR2I GetPosition() const override
Definition sch_symbol.h:895
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:163
wxString GetPrefix() const
Definition sch_symbol.h:238
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
VECTOR2I GetPosition() const override
Definition sch_text.h:143
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:104
Simple container to manage line stroke parameters.
int GetWidth() const
LINE_STYLE GetLineStyle() const
KIGFX::COLOR4D GetColor() const
static wxString GetLineStyleToken(LINE_STYLE aStyle)
static const char * PropPowerSymsOnly
const TRANSFORM & GetTransform() const
Definition symbol.h:247
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition title_block.h:38
const wxString & GetCompany() const
Definition title_block.h:93
void SetRevision(const wxString &aRevision)
Definition title_block.h:78
void SetComment(int aIdx, const wxString &aComment)
Definition title_block.h:98
const wxString & GetRevision() const
Definition title_block.h:83
void SetTitle(const wxString &aTitle)
Definition title_block.h:55
const wxString & GetDate() const
Definition title_block.h:73
const wxString & GetComment(int aIdx) const
void SetCompany(const wxString &aCompany)
Definition title_block.h:88
const wxString & GetTitle() const
Definition title_block.h:60
void SetDate(const wxString &aDate)
Set the date field, and defaults to the current time and date.
Definition title_block.h:68
for transforming drawing coordinates for a wxDC device context.
Definition transform.h:42
#define _(s)
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition eda_angle.h:445
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition eda_angle.h:444
#define MAX_UNIT_COUNT_PER_PACKAGE
The maximum number of units per package.
Definition eeschema_id.h:32
#define SCHEMATIC_HEAD_STRING
Definition general.h:32
#define EESCHEMA_VERSION
Definition general.h:31
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define THROW_IO_ERRORF(msg,...)
#define THROW_IO_CANCELLED()
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
const wxChar *const traceSchLegacyPlugin
Flag to enable legacy schematic plugin debug output.
static wxString From_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition idf_helpers.h:44
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition idf_helpers.h:37
#define SCH_LAYER_ID_COUNT
Definition layer_ids.h:530
SCH_LAYER_ID
Eeschema drawing layers.
Definition layer_ids.h:471
@ LAYER_HIERLABEL
Definition layer_ids.h:479
@ LAYER_GLOBLABEL
Definition layer_ids.h:478
@ LAYER_WIRE
Definition layer_ids.h:474
@ LAYER_NOTES
Definition layer_ids.h:489
@ LAYER_BUS
Definition layer_ids.h:475
@ LAYER_LOCLABEL
Definition layer_ids.h:477
Definition for symbol library class.
bool fileStartsWithPrefix(const wxString &aFilePath, const wxString &aPrefix, bool aIgnoreWhitespace)
Check if a file starts with a defined string.
Definition io_utils.cpp:33
const std::map< LABEL_FLAG_SHAPE, const char * > sheetLabelNames
#define T_COLORA
#define T_WIDTH
#define T_COLOR
#define T_STYLE
int parseInt(LINE_READER &aReader, const char *aLine, const char **aOutput)
Parse an ASCII integer string with possible leading whitespace into an integer and updates the pointe...
bool strCompare(const char *aString, const char *aLine, const char **aOutput)
Compare aString to the string starting at aLine and advances the character point to the end of String...
void parseQuotedString(wxString &aString, LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken, bool aCanBeEmpty)
Parse an quoted ASCII utf8 and updates the pointer at aOutput if it is not NULL.
void parseUnquotedString(wxString &aString, LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken, bool aCanBeEmpty)
Parse an unquoted utf8 string and updates the pointer at aOutput if it is not NULL.
uint32_t parseHex(LINE_READER &aReader, const char *aLine, const char **aOutput)
Parse an ASCII hex integer string with possible leading whitespace into a long integer and updates th...
bool is_eol(char c)
char parseChar(LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken)
Parse a single ASCII character and updates the pointer at aOutput if it is not NULL.
double parseDouble(LINE_READER &aReader, const char *aLine, const char **aOutput)
Parses an ASCII point string with possible leading whitespace into a double precision floating point ...
#define SCH_PARSE_ERROR(text, reader, pos)
@ AUTOPLACE_AUTO
Definition sch_item.h:75
@ L_BIDI
Definition sch_label.h:100
@ L_TRISTATE
Definition sch_label.h:101
@ L_UNSPECIFIED
Definition sch_label.h:102
@ L_OUTPUT
Definition sch_label.h:99
@ L_INPUT
Definition sch_label.h:98
std::string toUTFTildaText(const wxString &txt)
Convert a wxString to UTF8 and replace any control characters with a ~, where a control character is ...
wxString ConvertToNewOverbarNotation(const wxString &aOldStr)
Convert the old ~...~ overbar notation to the new ~{...} one.
std::string EscapedUTF8(const wxString &aString)
Return an 8 bit UTF8 string given aString in Unicode form.
A simple container for schematic symbol instance information.
std::map< wxString, LIB_SYMBOL *, LibSymbolMapSort > LIB_SYMBOL_MAP
Symbol library map sorted by the symbol name.
wxString GetUserFieldName(int aFieldNdx, TRANSLATION aTranslation)
wxString GetDefaultFieldName(FIELD_T aFieldId, TRANSLATION aTranslation)
Return a default symbol field name for a mandatory field type.
@ USER
The field ID hasn't been set yet; field is invalid.
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
@ DATASHEET
name of datasheet
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
@ UNTRANSLATED
std::string path
KIBIS_PIN * pin
VECTOR2I end
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_TOP
wxLogTrace helper definitions.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:70
@ SCH_LINE_T
Definition typeinfo.h:159
@ SCH_NO_CONNECT_T
Definition typeinfo.h:156
@ TYPE_NOT_INIT
Definition typeinfo.h:73
@ SCH_SYMBOL_T
Definition typeinfo.h:168
@ SCH_LABEL_T
Definition typeinfo.h:163
@ SCH_SHEET_T
Definition typeinfo.h:171
@ SCH_HIER_LABEL_T
Definition typeinfo.h:165
@ SCH_BUS_BUS_ENTRY_T
Definition typeinfo.h:158
@ SCH_TEXT_T
Definition typeinfo.h:147
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:157
@ SCH_BITMAP_T
Definition typeinfo.h:160
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:164
@ SCH_JUNCTION_T
Definition typeinfo.h:155
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
Definition of file extensions used in Kicad.