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