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