KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_eagle.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) 2017 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Alejandro GarcĂ­a Montoro <[email protected]>
8 * @author Maciej Suminski <[email protected]>
9 * @author Russell Oliver <[email protected]>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
26
27#include <algorithm>
28#include <memory>
29#include <wx/filename.h>
30#include <wx/string.h>
31#include <wx/tokenzr.h>
32#include <wx/wfstream.h>
33#include <wx/txtstrm.h>
34#include <wx/mstream.h>
35#include <wx/xml/xml.h>
36
37#include <font/fontconfig.h>
38#include <reporter.h>
40#include <lib_id.h>
41#include <progress_reporter.h>
42#include <project.h>
44#include <project_sch.h>
45#include <sch_bus_entry.h>
46#include <sch_edit_frame.h>
48#include <sch_junction.h>
49#include <sch_label.h>
50#include <sch_marker.h>
51#include <sch_pin.h>
52#include <sch_screen.h>
53#include <sch_shape.h>
54#include <sch_sheet.h>
55#include <sch_sheet_path.h>
56#include <sch_sheet_pin.h>
57#include <sch_symbol.h>
58#include <schematic.h>
59#include <string_utils.h>
62
63
64// Eagle schematic axes are aligned with x increasing left to right and Y increasing bottom to top
65// KiCad schematic axes are aligned with x increasing left to right and Y increasing top to bottom.
66
67using namespace std;
68
72static const std::map<wxString, ELECTRICAL_PINTYPE> pinDirectionsMap = {
73 { wxT( "sup" ), ELECTRICAL_PINTYPE::PT_POWER_IN },
74 { wxT( "pas" ), ELECTRICAL_PINTYPE::PT_PASSIVE },
75 { wxT( "out" ), ELECTRICAL_PINTYPE::PT_OUTPUT },
76 { wxT( "in" ), ELECTRICAL_PINTYPE::PT_INPUT },
77 { wxT( "nc" ), ELECTRICAL_PINTYPE::PT_NC },
78 { wxT( "io" ), ELECTRICAL_PINTYPE::PT_BIDI },
80 { wxT( "hiz" ), ELECTRICAL_PINTYPE::PT_TRISTATE },
81 { wxT( "pwr" ), ELECTRICAL_PINTYPE::PT_POWER_IN },
82};
83
84
86static BOX2I getSheetBbox( SCH_SHEET* aSheet )
87{
88 BOX2I bbox;
89
90 for( SCH_ITEM* item : aSheet->GetScreen()->Items() )
91 bbox.Merge( item->GetBoundingBox() );
92
93 return bbox;
94}
95
96
98static inline wxString extractNetName( const wxString& aPinName )
99{
100 return aPinName.BeforeFirst( '@' );
101}
102
103
108
109
111{
112 SCH_SHEET* currentSheet = m_sheetPath.Last();
113 wxCHECK( currentSheet, nullptr );
114 return currentSheet->GetScreen();
115}
116
117
119{
120 if( m_libName.IsEmpty() )
121 {
122 // Try to come up with a meaningful name
123 m_libName = m_schematic->Project().GetProjectName();
124
125 if( m_libName.IsEmpty() )
126 {
127 wxFileName fn( m_rootSheet->GetFileName() );
128 m_libName = fn.GetName();
129 }
130
131 if( m_libName.IsEmpty() )
132 m_libName = wxT( "noname" );
133
134 m_libName += wxT( "-eagle-import" );
136 }
137
138 return m_libName;
139}
140
141
143{
144 wxFileName fn;
145
146 wxCHECK( m_schematic, fn );
147
148 fn.Assign( m_schematic->Project().GetProjectPath(), getLibName(),
150
151 return fn;
152}
153
154
155void SCH_IO_EAGLE::loadLayerDefs( const std::vector<std::unique_ptr<ELAYER>>& aLayers )
156{
157 // match layers based on their names
158 for( const std::unique_ptr<ELAYER>& elayer : aLayers )
159 {
176
177 switch ( elayer->number)
178 {
179 case 91:
180 m_layerMap[elayer->number] = LAYER_WIRE;
181 break;
182 case 92:
183 m_layerMap[elayer->number] = LAYER_BUS;
184 break;
185 case 97:
186 case 98:
187 m_layerMap[elayer->number] = LAYER_NOTES;
188 break;
189
190 default:
191 break;
192 }
193 }
194}
195
196
198{
199 auto it = m_layerMap.find( aEagleLayer );
200 return it == m_layerMap.end() ? LAYER_NOTES : it->second;
201}
202
203
204// Return the KiCad symbol orientation based on eagle rotation degrees.
206{
207 int roti = int( eagleDegrees );
208
209 switch( roti )
210 {
211 case 0: return SYM_ORIENT_0;
212 case 90: return SYM_ORIENT_90;
213 case 180: return SYM_ORIENT_180;
214 case 270: return SYM_ORIENT_270;
215
216 default:
217 wxASSERT_MSG( false, wxString::Format( wxT( "Unhandled orientation (%d degrees)" ),
218 roti ) );
219 return SYM_ORIENT_0;
220 }
221}
222
223
224// Calculate text alignment based on the given Eagle text alignment parameters.
225static void eagleToKicadAlignment( EDA_TEXT* aText, int aEagleAlignment, int aRelDegress,
226 bool aMirror, bool aSpin, int aAbsDegress )
227{
228 int align = aEagleAlignment;
229
230 if( aRelDegress == 90 )
231 {
233 }
234 else if( aRelDegress == 180 )
235 {
236 align = -align;
237 }
238 else if( aRelDegress == 270 )
239 {
241 align = -align;
242 }
243
244 if( aMirror == true )
245 {
246 if( aAbsDegress == 90 || aAbsDegress == 270 )
247 {
248 if( align == ETEXT::BOTTOM_RIGHT )
249 align = ETEXT::TOP_RIGHT;
250 else if( align == ETEXT::BOTTOM_LEFT )
251 align = ETEXT::TOP_LEFT;
252 else if( align == ETEXT::TOP_LEFT )
253 align = ETEXT::BOTTOM_LEFT;
254 else if( align == ETEXT::TOP_RIGHT )
255 align = ETEXT::BOTTOM_RIGHT;
256 }
257 else if( aAbsDegress == 0 || aAbsDegress == 180 )
258 {
259 if( align == ETEXT::BOTTOM_RIGHT )
260 align = ETEXT::BOTTOM_LEFT;
261 else if( align == ETEXT::BOTTOM_LEFT )
262 align = ETEXT::BOTTOM_RIGHT;
263 else if( align == ETEXT::TOP_LEFT )
264 align = ETEXT::TOP_RIGHT;
265 else if( align == ETEXT::TOP_RIGHT )
266 align = ETEXT::TOP_LEFT;
267 else if( align == ETEXT::CENTER_LEFT )
268 align = ETEXT::CENTER_RIGHT;
269 else if( align == ETEXT::CENTER_RIGHT )
270 align = ETEXT::CENTER_LEFT;
271 }
272 }
273
274 switch( align )
275 {
276 case ETEXT::CENTER:
279 break;
280
284 break;
285
289 break;
290
294 break;
295
296 case ETEXT::TOP_LEFT:
299 break;
300
301 case ETEXT::TOP_RIGHT:
304 break;
305
309 break;
310
314 break;
315
319 break;
320
321 default:
324 break;
325 }
326}
327
328
329SCH_IO_EAGLE::SCH_IO_EAGLE() : SCH_IO( wxS( "EAGLE" ) ),
330 m_rootSheet( nullptr ),
331 m_schematic( nullptr ),
332 m_sheetIndex( 1 )
333{
335}
336
337
341
342
344{
345 return 0;
346}
347
348
349SCH_SHEET* SCH_IO_EAGLE::LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic,
350 SCH_SHEET* aAppendToMe,
351 const std::map<std::string, UTF8>* aProperties )
352{
353 wxASSERT( !aFileName || aSchematic != nullptr );
354
355 // Collect the font substitution warnings (RAII - automatically reset on scope exit)
357
358 m_filename = aFileName;
359 m_schematic = aSchematic;
360
362 {
363 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
364
365 if( !m_progressReporter->KeepRefreshing() )
366 THROW_IO_ERROR( ( "Open canceled by user." ) );
367 }
368
369 // Load the document
370 wxXmlDocument xmlDocument = loadXmlDocument( m_filename.GetFullPath() );
371
372 // Retrieve the root as current node
373 wxXmlNode* currentNode = xmlDocument.GetRoot();
374
376 m_progressReporter->SetNumPhases( static_cast<int>( GetNodeCount( currentNode ) ) );
377
378 wxFileName newFilename( m_filename );
379 newFilename.SetExt( FILEEXT::KiCadSchematicFileExtension );
380
381 // Owns the temporary VR for the non-append path so it is freed when this scope exits.
382 // The actual schematic VR will be created by SetTopLevelSheets() inside loadSchematic().
383 unique_ptr<SCH_SHEET> tempVROwner;
384
385 if( aAppendToMe )
386 {
387 wxCHECK_MSG( aSchematic->IsValid(), nullptr,
388 wxT( "Can't append to a schematic with no root!" ) );
389
390 m_rootSheet = &aSchematic->Root();
391
392 // We really should be passing the SCH_SHEET_PATH object to the aAppendToMe attribute
393 // instead of the SCH_SHEET. The full path is needed to properly generate instance
394 // data.
395 SCH_SHEET_LIST hierarchy( m_rootSheet );
396
397 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
398 {
399 if( sheetPath.Last() == aAppendToMe )
400 {
401 m_sheetPath = sheetPath;
402 break;
403 }
404 }
405 }
406 else
407 {
408 // Create a temporary local VR used only to anchor m_sheetPath during loading.
409 // loadSchematic() will call SetTopLevelSheets() with the real Eagle pages, which
410 // creates the actual schematic VR and re-parents the pages to it.
411 tempVROwner = std::make_unique<SCH_SHEET>( aSchematic );
412 m_rootSheet = tempVROwner.get();
413 const_cast<KIID&>( m_rootSheet->m_Uuid ) = niluuid;
414 }
415
416 if( !m_rootSheet->GetScreen() )
417 {
418 SCH_SCREEN* screen = new SCH_SCREEN( m_schematic );
419 screen->SetFileName( newFilename.GetFullPath() );
420 m_rootSheet->SetScreen( screen );
421
422 // Virtual root sheet UUID must be nil since all Eagle pages are loaded as subsheets.
423 const_cast<KIID&>( m_rootSheet->m_Uuid ) = niluuid;
424
425 // There is always at least a root sheet.
426 m_sheetPath.push_back( m_rootSheet );
427 }
428
430 LIBRARY_TABLE* table = adapter->ProjectTable().value_or( nullptr );
431 wxCHECK_MSG( table, nullptr, "Could not load symbol lib table." );
432
433 m_pi.reset( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
434
437 if( !table->HasRow( getLibName() ) )
438 {
439 // Create a new empty symbol library.
440 m_pi->CreateLibrary( getLibFileName().GetFullPath() );
441 wxString libTableUri = wxT( "${KIPRJMOD}/" ) + getLibFileName().GetFullName();
442
443 // Add the new library to the project symbol library table.
444 LIBRARY_TABLE_ROW& row = table->InsertRow();
445 row.SetNickname( getLibName() );
446 row.SetURI( libTableUri );
447 row.SetType( "KiCad" );
448
449 table->Save();
450
451 adapter->LoadOne( getLibName() );
452 }
453
454 m_eagleDoc = std::make_unique<EAGLE_DOC>( currentNode, this );
455
456 // If the attribute is found, store the Eagle version;
457 // otherwise, store the dummy "0.0" version.
458 m_version = ( m_eagleDoc->version.IsEmpty() ) ? wxString( wxS( "0.0" ) ) : m_eagleDoc->version;
459
460 // Load drawing
461 loadDrawing( m_eagleDoc->drawing );
462
463 if( !aAppendToMe )
464 m_rootSheet = &aSchematic->Root();
465
466 m_pi->SaveLibrary( getLibFileName().GetFullPath() );
467
468 SCH_SCREENS allSheets( m_rootSheet );
469 allSheets.UpdateSymbolLinks( &LOAD_INFO_REPORTER::GetInstance() ); // Update all symbol library links for all sheets.
470
471 return m_rootSheet;
472}
473
474
475void SCH_IO_EAGLE::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
476 const wxString& aLibraryPath,
477 const std::map<std::string, UTF8>* aProperties )
478{
479 m_filename = aLibraryPath;
480 m_libName = m_filename.GetName();
481
482 ensureLoadedLibrary( aLibraryPath );
483
484 auto it = m_eagleLibs.find( m_libName );
485
486 if( it != m_eagleLibs.end() )
487 {
488 for( const auto& [symName, libSymbol] : it->second.KiCadSymbols )
489 aSymbolNameList.push_back( symName );
490 }
491}
492
493
494void SCH_IO_EAGLE::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
495 const wxString& aLibraryPath,
496 const std::map<std::string, UTF8>* aProperties )
497{
498 m_filename = aLibraryPath;
499 m_libName = m_filename.GetName();
500
501 ensureLoadedLibrary( aLibraryPath );
502
503 auto it = m_eagleLibs.find( m_libName );
504
505 if( it != m_eagleLibs.end() )
506 {
507 for( const auto& [symName, libSymbol] : it->second.KiCadSymbols )
508 aSymbolList.push_back( libSymbol.get() );
509 }
510}
511
512
513LIB_SYMBOL* SCH_IO_EAGLE::LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
514 const std::map<std::string, UTF8>* aProperties )
515{
516 m_filename = aLibraryPath;
517 m_libName = m_filename.GetName();
518
519 ensureLoadedLibrary( aLibraryPath );
520
521 auto it = m_eagleLibs.find( m_libName );
522
523 if( it != m_eagleLibs.end() )
524 {
525 auto it2 = it->second.KiCadSymbols.find( aAliasName );
526
527 if( it2 != it->second.KiCadSymbols.end() )
528 return it2->second.get();
529 }
530
531 return nullptr;
532}
533
534
535long long SCH_IO_EAGLE::getLibraryTimestamp( const wxString& aLibraryPath ) const
536{
537 wxFileName fn( aLibraryPath );
538
539 if( fn.IsFileReadable() && fn.GetModificationTime().IsValid() )
540 return fn.GetModificationTime().GetValue().GetValue();
541 else
542 return 0;
543}
544
545
546void SCH_IO_EAGLE::ensureLoadedLibrary( const wxString& aLibraryPath )
547{
548 // Suppress font substitution warnings (RAII - automatically restored on scope exit)
549 FONTCONFIG_REPORTER_SCOPE fontconfigScope( nullptr );
550
551 if( m_eagleLibs.find( m_libName ) != m_eagleLibs.end() )
552 {
553 wxCHECK( m_timestamps.count( m_libName ), /*void*/ );
554
555 if( m_timestamps.at( m_libName ) == getLibraryTimestamp( aLibraryPath ) )
556 return;
557 }
558
560 {
561 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aLibraryPath ) );
562
563 if( !m_progressReporter->KeepRefreshing() )
564 THROW_IO_ERROR( ( "Open canceled by user." ) );
565 }
566
567 // Load the document
568 wxXmlDocument xmlDocument = loadXmlDocument( m_filename.GetFullPath() );
569
570 // Retrieve the root as current node
571 std::unique_ptr<EAGLE_DOC> doc = std::make_unique<EAGLE_DOC>( xmlDocument.GetRoot(), this );
572
573 // If the attribute is found, store the Eagle version;
574 // otherwise, store the dummy "0.0" version.
575 m_version = ( doc->version.IsEmpty() ) ? wxString( wxS( "0.0" ) ) : doc->version;
576
577 // Load drawing
578 loadDrawing( doc->drawing );
579
580 // Remember timestamp
581 m_timestamps[m_libName] = getLibraryTimestamp( aLibraryPath );
582}
583
584
585wxXmlDocument SCH_IO_EAGLE::loadXmlDocument( const wxString& aFileName )
586{
587 wxXmlDocument xmlDocument;
588 wxFFileInputStream stream( m_filename.GetFullPath() );
589
590 if( !stream.IsOk() )
591 {
593 wxString::Format( _( "Unable to read file '%s'." ), m_filename.GetFullPath() ) );
594 }
595
596 // read first line to check for Eagle XML format file
597 wxTextInputStream text( stream );
598 wxString line = text.ReadLine();
599
600 if( !line.StartsWith( wxT( "<?xml" ) ) && !line.StartsWith( wxT( "<!--" ) )
601 && !line.StartsWith( wxT( "<eagle " ) ) )
602 {
603 THROW_IO_ERROR( wxString::Format( _( "'%s' is an Eagle binary-format file; "
604 "only Eagle XML-format files can be imported." ),
605 m_filename.GetFullPath() ) );
606 }
607
608#if wxCHECK_VERSION( 3, 3, 0 )
609 wxXmlParseError err;
610
611 if( !xmlDocument.Load( stream, wxXMLDOC_NONE, &err ) )
612 {
613 if( err.message == wxS( "no element found" ) )
614 {
615 // Some files don't have the correct header, throwing off the xml parser
616 // So prepend the correct header
617 wxMemoryOutputStream memOutput;
618
619 wxString header;
620 header << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
621 header << "<!DOCTYPE eagle SYSTEM \"eagle.dtd\">\n";
622
623 wxScopedCharBuffer headerBuf = header.utf8_str();
624 memOutput.Write( headerBuf.data(), headerBuf.length() );
625
626 wxFFileInputStream stream2( m_filename.GetFullPath() );
627 memOutput.Write( stream2 );
628
629 wxMemoryInputStream memInput( memOutput );
630
631 if( !xmlDocument.Load( memInput, wxXMLDOC_NONE, &err ) )
632 {
633 THROW_IO_ERROR( wxString::Format( _( "Unable to read file '%s'." ), m_filename.GetFullPath() ) );
634 }
635 }
636 else
637 {
638 THROW_IO_ERROR( wxString::Format( _( "Unable to read file '%s'.\n'%s' at line %d, column %d, offset %d" ),
639 m_filename.GetFullPath(), err.message, err.line, err.column,
640 err.offset ) );
641 }
642 }
643#else
644 if( !xmlDocument.Load( stream ) )
645 {
646 // Some files don't have the correct header, throwing off the xml parser
647 // So prepend the correct header
648 wxMemoryOutputStream memOutput;
649
650 wxString header;
651 header << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
652 header << "<!DOCTYPE eagle SYSTEM \"eagle.dtd\">\n";
653
654 wxScopedCharBuffer headerBuf = header.utf8_str();
655 memOutput.Write( headerBuf.data(), headerBuf.length() );
656
657 wxFFileInputStream stream2( m_filename.GetFullPath() );
658 memOutput.Write( stream2 );
659
660 wxMemoryInputStream memInput( memOutput );
661
662 if( !xmlDocument.Load( memInput ) )
663 {
664 THROW_IO_ERROR( wxString::Format( _( "Unable to read file '%s'." ), m_filename.GetFullPath() ) );
665 }
666 }
667#endif
668
669 return xmlDocument;
670}
671
672
673void SCH_IO_EAGLE::loadDrawing( const std::unique_ptr<EDRAWING>& aDrawing )
674{
675 wxCHECK( aDrawing, /* void */ );
676
677 loadLayerDefs( aDrawing->layers );
678
679 if( aDrawing->library )
680 {
682 elib.name = m_libName;
683
684 loadLibrary( &aDrawing->library.value(), &elib );
685 }
686
687 if( aDrawing->schematic )
688 loadSchematic( *aDrawing->schematic );
689}
690
691
692void SCH_IO_EAGLE::countNets( const ESCHEMATIC& aSchematic )
693{
694 for( const std::unique_ptr<ESHEET>& esheet : aSchematic.sheets )
695 {
696 for( const std::unique_ptr<ENET>& enet : esheet->nets )
697 {
698 wxString netName = enet->netname;
699
700 if( m_netCounts.count( netName ) )
701 m_netCounts[netName] = m_netCounts[netName] + 1;
702 else
703 m_netCounts[netName] = 1;
704 }
705 }
706
707 for( const auto& [modname, emodule] : aSchematic.modules )
708 {
709 for( const std::unique_ptr<ESHEET>& esheet : emodule->sheets )
710 {
711 for( const std::unique_ptr<ENET>& enet : esheet->nets )
712 {
713 wxString netName = enet->netname;
714
715 if( m_netCounts.count( netName ) )
716 m_netCounts[netName] = m_netCounts[netName] + 1;
717 else
718 m_netCounts[netName] = 1;
719 }
720 }
721 }
722}
723
724
726{
727 // Map all children into a readable dictionary
728 if( aSchematic.sheets.empty() )
729 return;
730
731 // N.B. Eagle parts are case-insensitive in matching but we keep the display case
732 for( const auto& [name, epart] : aSchematic.parts )
733 m_partlist[name.Upper()] = epart.get();
734
735 for( const auto& [modName, emodule] : aSchematic.modules )
736 {
737 for( const auto& [partName, epart] : emodule->parts )
738 m_partlist[partName.Upper()] = epart.get();
739 }
740
741 if( !aSchematic.libraries.empty() )
742 {
743 for( const auto& [libName, elibrary] : aSchematic.libraries )
744 {
745 EAGLE_LIBRARY* elib = &m_eagleLibs[elibrary->GetName()];
746 elib->name = elibrary->GetName();
747
748 loadLibrary( elibrary.get(), &m_eagleLibs[elibrary->GetName()] );
749 }
750
751 m_pi->SaveLibrary( getLibFileName().GetFullPath() );
752 }
753
754 // find all nets and count how many sheets they appear on.
755 // local labels will be used for nets found only on that sheet.
756 countNets( aSchematic );
757
758 // Create all Eagle pages as top-level sheets (direct children of the virtual root).
759 // Collect them first so we can atomically replace any spurious default sheet created
760 // during schematic construction with exactly the set of real Eagle pages.
761 std::vector<SCH_SHEET*> eaglePages;
762 eaglePages.reserve( aSchematic.sheets.size() );
763
764 for( const std::unique_ptr<ESHEET>& esheet : aSchematic.sheets )
765 {
766 // Eagle schematics are never more than one sheet deep so the parent sheet is
767 // always the root sheet.
768 std::unique_ptr<SCH_SHEET> sheet = std::make_unique<SCH_SHEET>( m_rootSheet );
769 SCH_SCREEN* screen = new SCH_SCREEN( m_schematic );
770 sheet->SetScreen( screen );
771
772 wxCHECK2( sheet && screen, continue );
773
774 wxString pageNo = wxString::Format( wxT( "%d" ), m_sheetIndex );
775
776 m_sheetPath.push_back( sheet.get() );
777 loadSheet( esheet );
778
779 m_sheetPath.SetPageNumber( pageNo );
780 m_sheetPath.pop_back();
781
782 eaglePages.push_back( sheet.release() );
783
784 m_sheetIndex++;
785 }
786
787 if( !eaglePages.empty() )
788 {
789 // In the append path m_rootSheet is already the schematic's VR. Use
790 // AddTopLevelSheet to avoid discarding sheets already in the target.
791 // In the fresh-import path m_rootSheet is a temporary local VR, so we use
792 // SetTopLevelSheets to atomically replace any spurious default sheet with
793 // exactly the Eagle pages.
794 if( m_rootSheet == &m_schematic->Root() )
795 {
796 for( SCH_SHEET* page : eaglePages )
797 m_schematic->AddTopLevelSheet( page );
798 }
799 else
800 {
801 m_schematic->SetTopLevelSheets( eaglePages );
802 }
803 }
804
805 // Handle the missing symbol units that need to be instantiated
806 // to create the missing implicit connections
807
808 // Calculate the already placed items bounding box and the page size to determine
809 // placement for the new symbols
810 SCH_SHEET* schematicRoot = &m_schematic->Root();
811
812 struct MISSING_UNIT_PLACEMENT
813 {
814 VECTOR2I pageSizeIU;
815 BOX2I sheetBbox;
816 VECTOR2I newCmpPosition;
817 int maxY;
818 SCH_SHEET_PATH sheetpath;
819 SCH_SCREEN* screen;
820 };
821
822 std::map<SCH_SCREEN*, MISSING_UNIT_PLACEMENT> placements;
823
824 for( auto& cmp : m_missingCmps )
825 {
826 const SCH_SYMBOL* origSymbol = cmp.second.cmp;
827
828 for( auto& unitEntry : cmp.second.units )
829 {
830 if( unitEntry.second == false )
831 continue; // unit has been already processed
832
833 // Instantiate the missing symbol unit
834 int unit = unitEntry.first;
835 const wxString reference = origSymbol->GetField( FIELD_T::REFERENCE )->GetText();
836 std::unique_ptr<SCH_SYMBOL> symbol( (SCH_SYMBOL*) origSymbol->Duplicate( IGNORE_PARENT_GROUP ) );
837
838 SCH_SCREEN* targetScreen = cmp.second.screen;
839
840 if( !targetScreen )
841 {
842 SCH_SHEET* fallbackSheet = m_schematic->GetTopLevelSheet( 0 );
843
844 if( fallbackSheet )
845 targetScreen = fallbackSheet->GetScreen();
846 else
847 targetScreen = schematicRoot->GetScreen();
848 }
849
850 auto placementIt = placements.find( targetScreen );
851
852 if( placementIt == placements.end() )
853 {
854 MISSING_UNIT_PLACEMENT placement;
855 placement.screen = targetScreen;
856 placement.pageSizeIU = targetScreen->GetPageSettings().GetSizeIU( schIUScale.IU_PER_MILS );
857 schematicRoot->LocatePathOfScreen( targetScreen, &placement.sheetpath );
858
859 SCH_SHEET* targetSheet = placement.sheetpath.Last();
860
861 if( targetSheet )
862 placement.sheetBbox = getSheetBbox( targetSheet );
863
864 placement.newCmpPosition = VECTOR2I( placement.sheetBbox.GetLeft(),
865 placement.sheetBbox.GetBottom() );
866 placement.maxY = placement.sheetBbox.GetY();
867 placementIt = placements.emplace( targetScreen, placement ).first;
868 }
869
870 MISSING_UNIT_PLACEMENT& placement = placementIt->second;
871
872 symbol->SetUnitSelection( &placement.sheetpath, unit );
873 symbol->SetUnit( unit );
874 symbol->SetOrientation( 0 );
875 symbol->AddHierarchicalReference( placement.sheetpath.Path(), reference, unit );
876
877 // Calculate the placement position
878 BOX2I cmpBbox = symbol->GetBoundingBox();
879 int posY = placement.newCmpPosition.y + cmpBbox.GetHeight();
880 symbol->SetPosition( VECTOR2I( placement.newCmpPosition.x, posY ) );
881 placement.newCmpPosition.x += cmpBbox.GetWidth();
882 placement.maxY = std::max( placement.maxY, posY );
883
884 if( placement.newCmpPosition.x >= placement.pageSizeIU.x ) // reached the page boundary?
885 placement.newCmpPosition = VECTOR2I( placement.sheetBbox.GetLeft(),
886 placement.maxY ); // then start a new row
887
888 // Add the global net labels to recreate the implicit connections
889 addImplicitConnections( symbol.get(), placement.screen, false );
890 placement.screen->Append( symbol.release() );
891 }
892 }
893
894 m_missingCmps.clear();
895}
896
897
898void SCH_IO_EAGLE::loadSheet( const std::unique_ptr<ESHEET>& aSheet )
899{
900 SCH_SHEET* sheet = getCurrentSheet();
901 SCH_SCREEN* screen = getCurrentScreen();
902
903 wxCHECK( sheet && screen, /* void */ );
904
905 if( m_modules.empty() )
906 {
907 std::string filename;
908
909 filename = wxString::Format( wxT( "%s_%d" ), m_filename.GetName(), m_sheetIndex );
910
911 if( aSheet->description )
912 sheet->SetName( aSheet->description.value().text );
913 else
914 sheet->SetName( filename );
915
916 ReplaceIllegalFileNameChars( filename );
917 replace( filename.begin(), filename.end(), ' ', '_' );
918
919 // Use the project directory so saved pages land alongside the project file,
920 // not in the Eagle source directory.
921 wxFileName fn;
922 fn.SetPath( m_schematic->Project().GetProjectPath() );
923 fn.SetName( filename );
925
926 sheet->SetFileName( fn.GetFullName() );
927 screen->SetFileName( fn.GetFullPath() );
928 }
929
930 for( const auto& [name, moduleinst] : aSheet->moduleinsts )
931 loadModuleInstance( moduleinst );
932
933 sheet->AutoplaceFields( screen, AUTOPLACE_AUTO );
934
935 if( aSheet->plain )
936 {
937 for( const std::unique_ptr<EPOLYGON>& epoly : aSheet->plain->polygons )
938 screen->Append( loadPolyLine( epoly ) );
939
940 for( const std::unique_ptr<EWIRE>& ewire : aSheet->plain->wires )
941 {
942 SEG endpoints;
943 screen->Append( loadWire( ewire, endpoints ) );
944 }
945
946 for( const std::unique_ptr<ETEXT>& etext : aSheet->plain->texts )
947 screen->Append( loadPlainText( etext ) );
948
949 for( const std::unique_ptr<ECIRCLE>& ecircle : aSheet->plain->circles )
950 screen->Append( loadCircle( ecircle ) );
951
952 for( const std::unique_ptr<ERECT>& erectangle : aSheet->plain->rectangles )
953 screen->Append( loadRectangle( erectangle ) );
954
955 for( const std::unique_ptr<EFRAME>& eframe : aSheet->plain->frames )
956 {
957 std::vector<SCH_ITEM*> frameItems;
958
959 loadFrame( eframe, frameItems );
960
961 for( SCH_ITEM* item : frameItems )
962 screen->Append( item );
963 }
964
965 // Holes and splines currently not handled. Not sure hole has any meaning in scheamtics.
966 }
967
968 for( const std::unique_ptr<EINSTANCE>& einstance : aSheet->instances )
969 loadInstance( einstance, ( m_modules.size() ) ? m_modules.back()->parts
970 : m_eagleDoc->drawing->schematic->parts );
971
972 // Loop through all buses
973 // From the DTD: "Buses receive names which determine which signals they include.
974 // A bus is a drawing object. It does not create any electrical connections.
975 // These are always created by means of the nets and their names."
976 for( const std::unique_ptr<EBUS>& ebus : aSheet->busses )
977 {
978 // Get the bus name
979 wxString busName = translateEagleBusName( ebus->name );
980
981 // Load segments of this bus
982 loadSegments( ebus->segments, busName, wxString() );
983 }
984
985 for( const std::unique_ptr<ENET>& enet : aSheet->nets )
986 {
987 // Get the net name and class
988 wxString netName = enet->netname;
989 wxString netClass = wxString::Format( wxS( "%i" ), enet->netcode );
990
991 // Load segments of this net
992 loadSegments( enet->segments, netName, netClass );
993 }
994
995 adjustNetLabels(); // needs to be called before addBusEntries()
997
998 // Calculate the new sheet size.
999 BOX2I sheetBoundingBox = getSheetBbox( sheet );
1000 VECTOR2I targetSheetSize = sheetBoundingBox.GetSize();
1001 targetSheetSize += VECTOR2I( schIUScale.MilsToIU( 1500 ), schIUScale.MilsToIU( 1500 ) );
1002
1003 // Get current Eeschema sheet size.
1004 VECTOR2I pageSizeIU = screen->GetPageSettings().GetSizeIU( schIUScale.IU_PER_MILS );
1005 PAGE_INFO pageInfo = screen->GetPageSettings();
1006
1007 // Increase if necessary
1008 if( pageSizeIU.x < targetSheetSize.x )
1009 pageInfo.SetWidthMils( schIUScale.IUToMils( targetSheetSize.x ) );
1010
1011 if( pageSizeIU.y < targetSheetSize.y )
1012 pageInfo.SetHeightMils( schIUScale.IUToMils( targetSheetSize.y ) );
1013
1014 // Set the new sheet size.
1015 screen->SetPageSettings( pageInfo );
1016
1017 pageSizeIU = screen->GetPageSettings().GetSizeIU( schIUScale.IU_PER_MILS );
1018 VECTOR2I sheetcentre( pageSizeIU.x / 2, pageSizeIU.y / 2 );
1019 VECTOR2I itemsCentre = sheetBoundingBox.Centre();
1020
1021 // round the translation to nearest 100mil to place it on the grid.
1022 VECTOR2I translation = sheetcentre - itemsCentre;
1023 translation.x = translation.x - translation.x % schIUScale.MilsToIU( 100 );
1024 translation.y = translation.y - translation.y % schIUScale.MilsToIU( 100 );
1025
1026 // Add global net labels for the named power input pins in this sheet
1027 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
1028 {
1029 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1030 addImplicitConnections( symbol, screen, true );
1031 }
1032
1033 m_connPoints.clear();
1034
1035 // Translate the items.
1036 std::vector<SCH_ITEM*> allItems;
1037
1038 std::copy( screen->Items().begin(), screen->Items().end(), std::back_inserter( allItems ) );
1039
1040 for( SCH_ITEM* item : allItems )
1041 {
1042 item->SetPosition( item->GetPosition() + translation );
1043
1044 // We don't read positions of Eagle label fields (primarily intersheet refs), so we
1045 // need to autoplace them after applying the translation.
1046 if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( item ) )
1047 label->AutoplaceFields( screen, AUTOPLACE_AUTO );
1048
1049 item->ClearFlags();
1050 screen->Update( item );
1051 }
1052}
1053
1054
1055void SCH_IO_EAGLE::loadModuleInstance( const std::unique_ptr<EMODULEINST>& aModuleInstance )
1056{
1057 SCH_SHEET* currentSheet = getCurrentSheet();
1058 SCH_SCREEN* currentScreen = getCurrentScreen();
1059
1060 wxCHECK( currentSheet &&currentScreen, /* void */ );
1061
1062 m_sheetIndex++;
1063
1064 // Eagle document has already be checked for drawing and schematic nodes so this
1065 // should not segfault.
1066 auto it = m_eagleDoc->drawing->schematic->modules.find( aModuleInstance->moduleinst );
1067
1068 // Find the module referenced by the module instance.
1069 if( it == m_eagleDoc->drawing->schematic->modules.end() )
1070 {
1071 THROW_IO_ERROR( wxString::Format( _( "No module instance '%s' found in schematic "
1072 "file:\n%s" ),
1073 aModuleInstance->name, m_filename.GetFullPath() ) );
1074 }
1075
1076 wxFileName fn = m_filename;
1077 fn.SetName( aModuleInstance->moduleinst );
1079
1080 VECTOR2I portExtWireEndpoint;
1081 VECTOR2I size( it->second->dx.ToSchUnits(), it->second->dy.ToSchUnits() );
1082
1083 int halfX = KiROUND( size.x / 2.0 );
1084 int halfY = KiROUND( size.y / 2.0 );
1085 int portExtWireLength = schIUScale.mmToIU( 5.08 );
1086 VECTOR2I pos( aModuleInstance->x.ToSchUnits() - halfX,
1087 -aModuleInstance->y.ToSchUnits() - halfY );
1088
1089 std::unique_ptr<SCH_SHEET> newSheet = std::make_unique<SCH_SHEET>( currentSheet, pos, size );
1090
1091 // The Eagle module for this instance (SCH_SCREEN in KiCad) may have already been loaded.
1092 SCH_SCREEN* newScreen = nullptr;
1093 SCH_SCREENS schFiles( m_rootSheet );
1094
1095 for( SCH_SCREEN* schFile = schFiles.GetFirst(); schFile; schFile = schFiles.GetNext() )
1096 {
1097 if( schFile->GetFileName() == fn.GetFullPath() )
1098 {
1099 newScreen = schFile;
1100 break;
1101 }
1102 }
1103
1104 bool isNewSchFile = ( newScreen == nullptr );
1105
1106 if( !newScreen )
1107 {
1108 newScreen = new SCH_SCREEN( m_schematic );
1109 newScreen->SetFileName( fn.GetFullPath() );
1110 }
1111
1112 wxCHECK( newSheet && newScreen, /* void */ );
1113
1114 newSheet->SetScreen( newScreen );
1115 newSheet->SetFileName( fn.GetFullName() );
1116 newSheet->SetName( aModuleInstance->name );
1117
1118 for( const auto& [portName, port] : it->second->ports )
1119 {
1120 VECTOR2I pinPos( 0, 0 );
1121 int pinOffset = port->coord.ToSchUnits();
1123
1124 if( port->side == "left" )
1125 {
1126 side = SHEET_SIDE::LEFT;
1127 pinPos.x = pos.x;
1128 pinPos.y = pos.y + halfY - pinOffset;
1129 portExtWireEndpoint = pinPos;
1130 portExtWireEndpoint.x -= portExtWireLength;
1131 }
1132 else if( port->side == "right" )
1133 {
1134 side = SHEET_SIDE::RIGHT;
1135 pinPos.x = pos.x + size.x;
1136 pinPos.y = pos.y + halfY - pinOffset;
1137 portExtWireEndpoint = pinPos;
1138 portExtWireEndpoint.x += portExtWireLength;
1139 }
1140 else if( port->side == "top" )
1141 {
1142 side = SHEET_SIDE::TOP;
1143 pinPos.x = pos.x + halfX + pinOffset;
1144 pinPos.y = pos.y;
1145 portExtWireEndpoint = pinPos;
1146 portExtWireEndpoint.y -= portExtWireLength;
1147 }
1148 else if( port->side == "bottom" )
1149 {
1150 side = SHEET_SIDE::BOTTOM;
1151 pinPos.x = pos.x + halfX + pinOffset;
1152 pinPos.y = pos.y + size.y;
1153 portExtWireEndpoint = pinPos;
1154 portExtWireEndpoint.y += portExtWireLength;
1155 }
1156
1157 SCH_LINE* portExtWire = new SCH_LINE( pinPos, LAYER_WIRE );
1158 portExtWire->SetEndPoint( portExtWireEndpoint );
1159 currentScreen->Append( portExtWire );
1160
1162
1163 if( port->direction )
1164 {
1165 if( *port->direction == "in" )
1166 pinType = LABEL_FLAG_SHAPE::L_INPUT;
1167 else if( *port->direction == "out" )
1169 else if( *port->direction == "io" )
1170 pinType = LABEL_FLAG_SHAPE::L_BIDI;
1171 else if( *port->direction == "hiz" )
1173 else
1175
1176 // KiCad does not support passive, power, open collector, or no-connect sheet
1177 // pins that Eagle ports support. They are set to unspecified to minimize
1178 // ERC issues.
1179 }
1180
1181 SCH_SHEET_PIN* sheetPin = new SCH_SHEET_PIN( newSheet.get(), VECTOR2I( 0, 0 ), portName );
1182
1183 sheetPin->SetShape( pinType );
1184 sheetPin->SetPosition( pinPos );
1185 sheetPin->SetSide( side );
1186 newSheet->AddPin( sheetPin );
1187 }
1188
1189 wxString pageNo = wxString::Format( wxT( "%d" ), m_sheetIndex );
1190
1191 newSheet->SetParent( currentSheet );
1192 m_sheetPath.push_back( newSheet.get() );
1193 m_sheetPath.SetPageNumber( pageNo );
1194 currentScreen->Append( newSheet.release() );
1195
1196 m_modules.push_back( it->second.get() );
1197 m_moduleInstances.push_back( aModuleInstance.get() );
1198
1199 // Do not reload shared modules that are already loaded.
1200 if( isNewSchFile )
1201 {
1202 for( const std::unique_ptr<ESHEET>& esheet : it->second->sheets )
1203 loadSheet( esheet );
1204 }
1205 else
1206 {
1207 // Add instances for shared schematics.
1208 wxString refPrefix;
1209
1210 for( const EMODULEINST* emoduleInst : m_moduleInstances )
1211 {
1212 wxCHECK2( emoduleInst, continue );
1213
1214 refPrefix += emoduleInst->name + wxS( ":" );
1215 }
1216
1217 SCH_SCREEN* sharedScreen = m_sheetPath.LastScreen();
1218
1219 if( sharedScreen )
1220 {
1221 for( SCH_ITEM* schItem : sharedScreen->Items().OfType( SCH_SYMBOL_T ) )
1222 {
1223 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( schItem );
1224
1225 wxCHECK2( symbol && !symbol->GetInstances().empty(), continue );
1226
1227 SCH_SYMBOL_INSTANCE inst = symbol->GetInstances().at( 0 );
1228 wxString newReference = refPrefix + inst.m_Reference.AfterLast( ':' );
1229
1230 symbol->AddHierarchicalReference( m_sheetPath.Path(), newReference, inst.m_Unit );
1231 }
1232 }
1233 }
1234
1235 m_moduleInstances.pop_back();
1236 m_modules.pop_back();
1237 m_sheetPath.pop_back();
1238}
1239
1240
1241void SCH_IO_EAGLE::loadFrame( const std::unique_ptr<EFRAME>& aFrame,
1242 std::vector<SCH_ITEM*>& aItems )
1243{
1244 int xMin = aFrame->x1.ToSchUnits();
1245 int xMax = aFrame->x2.ToSchUnits();
1246 int yMin = -aFrame->y1.ToSchUnits();
1247 int yMax = -aFrame->y2.ToSchUnits();
1248
1249 if( xMin > xMax )
1250 std::swap( xMin, xMax );
1251
1252 if( yMin > yMax )
1253 std::swap( yMin, yMax );
1254
1255 SCH_SHAPE* lines = new SCH_SHAPE( SHAPE_T::POLY );
1256 lines->AddPoint( VECTOR2I( xMin, yMin ) );
1257 lines->AddPoint( VECTOR2I( xMax, yMin ) );
1258 lines->AddPoint( VECTOR2I( xMax, yMax ) );
1259 lines->AddPoint( VECTOR2I( xMin, yMax ) );
1260 lines->AddPoint( VECTOR2I( xMin, yMin ) );
1261 aItems.push_back( lines );
1262
1263 if( !( aFrame->border_left == false ) )
1264 {
1265 lines = new SCH_SHAPE( SHAPE_T::POLY );
1266 lines->AddPoint( VECTOR2I( xMin + schIUScale.MilsToIU( 150 ),
1267 yMin + schIUScale.MilsToIU( 150 ) ) );
1268 lines->AddPoint( VECTOR2I( xMin + schIUScale.MilsToIU( 150 ),
1269 yMax - schIUScale.MilsToIU( 150 ) ) );
1270 aItems.push_back( lines );
1271
1272 int i;
1273 int height = yMax - yMin;
1274 int x1 = xMin;
1275 int x2 = x1 + schIUScale.MilsToIU( 150 );
1276 int legendPosX = xMin + schIUScale.MilsToIU( 75 );
1277 double rowSpacing = height / double( aFrame->rows );
1278 double legendPosY = yMin + ( rowSpacing / 2 );
1279
1280 for( i = 1; i < aFrame->rows; i++ )
1281 {
1282 int newY = KiROUND( yMin + ( rowSpacing * (double) i ) );
1283 lines = new SCH_SHAPE( SHAPE_T::POLY );
1284 lines->AddPoint( VECTOR2I( x1, newY ) );
1285 lines->AddPoint( VECTOR2I( x2, newY ) );
1286 aItems.push_back( lines );
1287 }
1288
1289 char legendChar = 'A';
1290
1291 for( i = 0; i < aFrame->rows; i++ )
1292 {
1293 SCH_TEXT* legendText = new SCH_TEXT();
1294 legendText->SetPosition( VECTOR2I( legendPosX, KiROUND( legendPosY ) ) );
1297 legendText->SetText( wxString( legendChar ) );
1298 legendText->SetTextSize( VECTOR2I( schIUScale.MilsToIU( 90 ),
1299 schIUScale.MilsToIU( 100 ) ) );
1300 aItems.push_back( legendText );
1301 legendChar++;
1302 legendPosY += rowSpacing;
1303 }
1304 }
1305
1306 if( !( aFrame->border_right == false ) )
1307 {
1308 lines = new SCH_SHAPE( SHAPE_T::POLY );
1309 lines->AddPoint( VECTOR2I( xMax - schIUScale.MilsToIU( 150 ),
1310 yMin + schIUScale.MilsToIU( 150 ) ) );
1311 lines->AddPoint( VECTOR2I( xMax - schIUScale.MilsToIU( 150 ),
1312 yMax - schIUScale.MilsToIU( 150 ) ) );
1313 aItems.push_back( lines );
1314
1315 int i;
1316 int height = yMax - yMin;
1317 int x1 = xMax - schIUScale.MilsToIU( 150 );
1318 int x2 = xMax;
1319 int legendPosX = xMax - schIUScale.MilsToIU( 75 );
1320 double rowSpacing = height / double( aFrame->rows );
1321 double legendPosY = yMin + ( rowSpacing / 2 );
1322
1323 for( i = 1; i < aFrame->rows; i++ )
1324 {
1325 int newY = KiROUND( yMin + ( rowSpacing * (double) i ) );
1326 lines = new SCH_SHAPE( SHAPE_T::POLY );
1327 lines->AddPoint( VECTOR2I( x1, newY ) );
1328 lines->AddPoint( VECTOR2I( x2, newY ) );
1329 aItems.push_back( lines );
1330 }
1331
1332 char legendChar = 'A';
1333
1334 for( i = 0; i < aFrame->rows; i++ )
1335 {
1336 SCH_TEXT* legendText = new SCH_TEXT();
1337 legendText->SetPosition( VECTOR2I( legendPosX, KiROUND( legendPosY ) ) );
1340 legendText->SetText( wxString( legendChar ) );
1341 legendText->SetTextSize( VECTOR2I( schIUScale.MilsToIU( 90 ),
1342 schIUScale.MilsToIU( 100 ) ) );
1343 aItems.push_back( legendText );
1344 legendChar++;
1345 legendPosY += rowSpacing;
1346 }
1347 }
1348
1349 if( !( aFrame->border_top == false ) )
1350 {
1351 lines = new SCH_SHAPE( SHAPE_T::POLY );
1352 lines->AddPoint( VECTOR2I( xMax - schIUScale.MilsToIU( 150 ),
1353 yMin + schIUScale.MilsToIU( 150 ) ) );
1354 lines->AddPoint( VECTOR2I( xMin + schIUScale.MilsToIU( 150 ),
1355 yMin + schIUScale.MilsToIU( 150 ) ) );
1356 aItems.push_back( lines );
1357
1358 int i;
1359 int width = xMax - xMin;
1360 int y1 = yMin;
1361 int y2 = yMin + schIUScale.MilsToIU( 150 );
1362 int legendPosY = yMin + schIUScale.MilsToIU( 75 );
1363 double columnSpacing = width / double( aFrame->columns );
1364 double legendPosX = xMin + ( columnSpacing / 2 );
1365
1366 for( i = 1; i < aFrame->columns; i++ )
1367 {
1368 int newX = KiROUND( xMin + ( columnSpacing * (double) i ) );
1369 lines = new SCH_SHAPE( SHAPE_T::POLY );
1370 lines->AddPoint( VECTOR2I( newX, y1 ) );
1371 lines->AddPoint( VECTOR2I( newX, y2 ) );
1372 aItems.push_back( lines );
1373 }
1374
1375 char legendChar = '1';
1376
1377 for( i = 0; i < aFrame->columns; i++ )
1378 {
1379 SCH_TEXT* legendText = new SCH_TEXT();
1380 legendText->SetPosition( VECTOR2I( KiROUND( legendPosX ), legendPosY ) );
1383 legendText->SetText( wxString( legendChar ) );
1384 legendText->SetTextSize( VECTOR2I( schIUScale.MilsToIU( 90 ),
1385 schIUScale.MilsToIU( 100 ) ) );
1386 aItems.push_back( legendText );
1387 legendChar++;
1388 legendPosX += columnSpacing;
1389 }
1390 }
1391
1392 if( !( aFrame->border_bottom == false ) )
1393 {
1394 lines = new SCH_SHAPE( SHAPE_T::POLY );
1395 lines->AddPoint( VECTOR2I( xMax - schIUScale.MilsToIU( 150 ),
1396 yMax - schIUScale.MilsToIU( 150 ) ) );
1397 lines->AddPoint( VECTOR2I( xMin + schIUScale.MilsToIU( 150 ),
1398 yMax - schIUScale.MilsToIU( 150 ) ) );
1399 aItems.push_back( lines );
1400
1401 int i;
1402 int width = xMax - xMin;
1403 int y1 = yMax - schIUScale.MilsToIU( 150 );
1404 int y2 = yMax;
1405 int legendPosY = yMax - schIUScale.MilsToIU( 75 );
1406 double columnSpacing = width / double( aFrame->columns );
1407 double legendPosX = xMin + ( columnSpacing / 2 );
1408
1409 for( i = 1; i < aFrame->columns; i++ )
1410 {
1411 int newX = KiROUND( xMin + ( columnSpacing * (double) i ) );
1412 lines = new SCH_SHAPE( SHAPE_T::POLY );
1413 lines->AddPoint( VECTOR2I( newX, y1 ) );
1414 lines->AddPoint( VECTOR2I( newX, y2 ) );
1415 aItems.push_back( lines );
1416 }
1417
1418 char legendChar = '1';
1419
1420 for( i = 0; i < aFrame->columns; i++ )
1421 {
1422 SCH_TEXT* legendText = new SCH_TEXT();
1423 legendText->SetPosition( VECTOR2I( KiROUND( legendPosX ), legendPosY ) );
1426 legendText->SetText( wxString( legendChar ) );
1427 legendText->SetTextSize( VECTOR2I( schIUScale.MilsToIU( 90 ),
1428 schIUScale.MilsToIU( 100 ) ) );
1429 aItems.push_back( legendText );
1430 legendChar++;
1431 legendPosX += columnSpacing;
1432 }
1433 }
1434}
1435
1436
1437void SCH_IO_EAGLE::loadSegments( const std::vector<std::unique_ptr<ESEGMENT>>& aSegments,
1438 const wxString& netName,
1439 const wxString& aNetClass )
1440{
1441 // Loop through all segments
1442 SCH_SCREEN* screen = getCurrentScreen();
1443
1444 wxCHECK( screen, /* void */ );
1445
1446 size_t segmentCount = aSegments.size();
1447
1448 for( const std::unique_ptr<ESEGMENT>& esegment : aSegments )
1449 {
1450 bool labelled = false; // has a label been added to this continuously connected segment
1451 bool firstWireFound = false;
1452 SEG firstWire;
1453
1454 m_segments.emplace_back();
1455 SEG_DESC& segDesc = m_segments.back();
1456
1457 for( const std::unique_ptr<EWIRE>& ewire : esegment->wires )
1458 {
1459 // TODO: Check how intersections used in adjustNetLabels should be
1460 // calculated - for now we pretend that all wires are line segments.
1461 SEG thisWire;
1462 SCH_ITEM* wire = loadWire( ewire, thisWire );
1463 m_connPoints[thisWire.A].emplace( wire );
1464 m_connPoints[thisWire.B].emplace( wire );
1465
1466 if( !firstWireFound )
1467 {
1468 firstWire = thisWire;
1469 firstWireFound = true;
1470 }
1471
1472 // Test for intersections with other wires
1473 for( SEG_DESC& desc : m_segments )
1474 {
1475 if( !desc.labels.empty() && desc.labels.front()->GetText() == netName )
1476 continue; // no point in saving intersections of the same net
1477
1478 for( const SEG& seg : desc.segs )
1479 {
1480 OPT_VECTOR2I intersection = thisWire.Intersect( seg, true );
1481
1482 if( intersection )
1483 m_wireIntersections.push_back( *intersection );
1484 }
1485 }
1486
1487 segDesc.segs.push_back( thisWire );
1488 screen->Append( wire );
1489 }
1490
1491 for( const std::unique_ptr<EJUNCTION>& ejunction : esegment->junctions )
1492 screen->Append( loadJunction( ejunction ) );
1493
1494 for( const std::unique_ptr<ELABEL>& elabel : esegment->labels )
1495 {
1496 SCH_TEXT* label = loadLabel( elabel, netName );
1497 screen->Append( label );
1498
1499 wxASSERT( segDesc.labels.empty()
1500 || segDesc.labels.front()->GetText() == label->GetText() );
1501
1502 segDesc.labels.push_back( label );
1503 labelled = true;
1504 }
1505
1506 for( const std::unique_ptr<EPINREF>& epinref : esegment->pinRefs )
1507 {
1508 wxString part = epinref->part;
1509 wxString pin = epinref->pin;
1510
1511 auto powerPort = m_powerPorts.find( wxT( "#" ) + part );
1512
1513 if( powerPort != m_powerPorts.end()
1514 && powerPort->second == EscapeString( pin, CTX_NETNAME ) )
1515 {
1516 labelled = true;
1517 }
1518 }
1519
1520 // Add a small label to the net segment if it hasn't been labeled already or is not
1521 // connect to a power symbol with a pin on the same net. This preserves the named net
1522 // feature of Eagle schematics.
1523 if( !labelled && firstWireFound )
1524 {
1525 std::unique_ptr<SCH_LABEL_BASE> label;
1526
1527 // Add a global label if the net appears on more than one Eagle sheet
1528 if( m_netCounts[netName.ToStdString()] > 1 )
1529 label.reset( new SCH_GLOBALLABEL );
1530 else if( segmentCount > 1 )
1531 label.reset( new SCH_LABEL );
1532
1533 if( label )
1534 {
1535 label->SetPosition( firstWire.A );
1536 label->SetText( escapeName( netName ) );
1537 label->SetTextSize( VECTOR2I( schIUScale.MilsToIU( 40 ),
1538 schIUScale.MilsToIU( 40 ) ) );
1539
1540 if( firstWire.B.x > firstWire.A.x )
1541 label->SetSpinStyle( SPIN_STYLE::LEFT );
1542 else
1543 label->SetSpinStyle( SPIN_STYLE::RIGHT );
1544
1545 screen->Append( label.release() );
1546 }
1547 }
1548 }
1549}
1550
1551
1552SCH_SHAPE* SCH_IO_EAGLE::loadPolyLine( const std::unique_ptr<EPOLYGON>& aPolygon )
1553{
1554 std::unique_ptr<SCH_SHAPE> poly = std::make_unique<SCH_SHAPE>( SHAPE_T::POLY );
1555 VECTOR2I pt, prev_pt;
1556 opt_double prev_curve;
1557
1558 for( const std::unique_ptr<EVERTEX>& evertex : aPolygon->vertices )
1559 {
1560 pt = VECTOR2I( evertex->x.ToSchUnits(), -evertex->y.ToSchUnits() );
1561
1562 if( prev_curve )
1563 {
1564 SHAPE_ARC arc;
1565 arc.ConstructFromStartEndAngle( prev_pt, pt, -EDA_ANGLE( *prev_curve, DEGREES_T ) );
1566 poly->GetPolyShape().Append( arc, -1, -1, ARC_ACCURACY );
1567 }
1568 else
1569 {
1570 poly->AddPoint( pt );
1571 }
1572
1573 prev_pt = pt;
1574 prev_curve = evertex->curve;
1575 }
1576
1577 poly->SetLayer( kiCadLayer( aPolygon->layer ) );
1578 poly->SetStroke( STROKE_PARAMS( aPolygon->width.ToSchUnits(), LINE_STYLE::SOLID ) );
1579 poly->SetFillMode( FILL_T::FILLED_SHAPE );
1580
1581 return poly.release();
1582}
1583
1584
1585SCH_ITEM* SCH_IO_EAGLE::loadWire( const std::unique_ptr<EWIRE>& aWire, SEG& endpoints )
1586{
1587 VECTOR2I start, end;
1588
1589 start.x = aWire->x1.ToSchUnits();
1590 start.y = -aWire->y1.ToSchUnits();
1591 end.x = aWire->x2.ToSchUnits();
1592 end.y = -aWire->y2.ToSchUnits();
1593
1594 // For segment wires.
1595 endpoints = SEG( start, end );
1596
1597 int kicadLayer = kiCadLayer( aWire->layer );
1598
1599 // Don't process curved wires on an electrical layer into arcs, they aren't supported
1600 // in the rest of the code
1601 // TODO: When curved wires/buses are added, remove this restriction
1602 if( (kicadLayer == LAYER_NOTES) && aWire->curve )
1603 {
1604 std::unique_ptr<SCH_SHAPE> arc = std::make_unique<SCH_SHAPE>( SHAPE_T::ARC );
1605
1606 VECTOR2I center = ConvertArcCenter( start, end, *aWire->curve );
1607 arc->SetCenter( center );
1608 arc->SetStart( start );
1609
1610 // KiCad rotates the other way.
1611 arc->SetArcAngleAndEnd( -EDA_ANGLE( *aWire->curve, DEGREES_T ), true );
1612 arc->SetLayer( kiCadLayer( aWire->layer ) );
1613 arc->SetStroke( STROKE_PARAMS( aWire->width.ToSchUnits(), LINE_STYLE::SOLID ) );
1614
1615 return arc.release();
1616 }
1617 else
1618 {
1619 std::unique_ptr<SCH_LINE> line = std::make_unique<SCH_LINE>();
1620
1621 line->SetStartPoint( start );
1622 line->SetEndPoint( end );
1623 line->SetLayer( kiCadLayer( aWire->layer ) );
1624 line->SetStroke( STROKE_PARAMS( aWire->width.ToSchUnits(), LINE_STYLE::SOLID ) );
1625
1626 return line.release();
1627 }
1628}
1629
1630
1631SCH_SHAPE* SCH_IO_EAGLE::loadCircle( const std::unique_ptr<ECIRCLE>& aCircle )
1632{
1633 std::unique_ptr<SCH_SHAPE> circle = std::make_unique<SCH_SHAPE>( SHAPE_T::CIRCLE );
1634 VECTOR2I center( aCircle->x.ToSchUnits(), -aCircle->y.ToSchUnits() );
1635
1636 circle->SetLayer( kiCadLayer( aCircle->layer ) );
1637 circle->SetPosition( center );
1638 circle->SetEnd( VECTOR2I( center.x + aCircle->radius.ToSchUnits(), center.y ) );
1639 circle->SetStroke( STROKE_PARAMS( aCircle->width.ToSchUnits(), LINE_STYLE::SOLID ) );
1640
1641 return circle.release();
1642}
1643
1644
1645SCH_SHAPE* SCH_IO_EAGLE::loadRectangle( const std::unique_ptr<ERECT>& aRectangle )
1646{
1647 std::unique_ptr<SCH_SHAPE> rectangle = std::make_unique<SCH_SHAPE>( SHAPE_T::RECTANGLE );
1648
1649 rectangle->SetLayer( kiCadLayer( aRectangle->layer ) );
1650 rectangle->SetPosition( VECTOR2I( aRectangle->x1.ToSchUnits(), -aRectangle->y1.ToSchUnits() ) );
1651 rectangle->SetEnd( VECTOR2I( aRectangle->x2.ToSchUnits(), -aRectangle->y2.ToSchUnits() ) );
1652
1653 if( aRectangle->rot )
1654 {
1655 VECTOR2I pos( rectangle->GetPosition() );
1656 VECTOR2I end( rectangle->GetEnd() );
1657 VECTOR2I center( rectangle->GetCenter() );
1658
1659 RotatePoint( pos, center, EDA_ANGLE( aRectangle->rot->degrees, DEGREES_T ) );
1660 RotatePoint( end, center, EDA_ANGLE( aRectangle->rot->degrees, DEGREES_T ) );
1661
1662 rectangle->SetPosition( pos );
1663 rectangle->SetEnd( end );
1664 }
1665
1666 // Eagle rectangles are filled by definition.
1667 rectangle->SetFillMode( FILL_T::FILLED_SHAPE );
1668
1669 return rectangle.release();
1670}
1671
1672
1673SCH_JUNCTION* SCH_IO_EAGLE::loadJunction( const std::unique_ptr<EJUNCTION>& aJunction )
1674{
1675 std::unique_ptr<SCH_JUNCTION> junction = std::make_unique<SCH_JUNCTION>();
1676
1677 VECTOR2I pos( aJunction->x.ToSchUnits(), -aJunction->y.ToSchUnits() );
1678
1679 junction->SetPosition( pos );
1680
1681 return junction.release();
1682}
1683
1684
1685SCH_TEXT* SCH_IO_EAGLE::loadLabel( const std::unique_ptr<ELABEL>& aLabel,
1686 const wxString& aNetName )
1687{
1688 VECTOR2I elabelpos( aLabel->x.ToSchUnits(), -aLabel->y.ToSchUnits() );
1689
1690 // Determine if the label is local or global depending on
1691 // the number of sheets the net appears in
1692 bool global = m_netCounts[aNetName] > 1;
1693 std::unique_ptr<SCH_LABEL_BASE> label;
1694
1695 VECTOR2I textSize = KiROUND( aLabel->size.ToSchUnits() * 0.7, aLabel->size.ToSchUnits() * 0.7 );
1696
1697 if( m_modules.size() )
1698 {
1699 if( m_modules.back()->ports.find( aNetName ) != m_modules.back()->ports.end() )
1700 {
1701 label = std::make_unique<SCH_HIERLABEL>();
1702 label->SetText( escapeName( aNetName ) );
1703
1704 const auto it = m_modules.back()->ports.find( aNetName );
1705
1706 LABEL_SHAPE type;
1707
1708 if( it->second->direction )
1709 {
1710 wxString direction = *it->second->direction;
1711
1712 if( direction == "in" )
1714 else if( direction == "out" )
1716 else if( direction == "io" )
1718 else if( direction == "hiz" )
1720 else
1722
1723 // KiCad does not support passive, power, open collector, or no-connect sheet
1724 // pins that Eagle ports support. They are set to unspecified to minimize
1725 // ERC issues.
1726 label->SetLabelShape( type );
1727 }
1728 }
1729 else
1730 {
1731 label = std::make_unique<SCH_LABEL>();
1732 label->SetText( escapeName( aNetName ) );
1733 }
1734 }
1735 else if( global )
1736 {
1737 label = std::make_unique<SCH_GLOBALLABEL>();
1738 label->SetText( escapeName( aNetName ) );
1739 }
1740 else
1741 {
1742 label = std::make_unique<SCH_LABEL>();
1743 label->SetText( escapeName( aNetName ) );
1744 }
1745
1746 label->SetPosition( elabelpos );
1747 label->SetTextSize( textSize );
1748 label->SetSpinStyle( SPIN_STYLE::RIGHT );
1749
1750 if( aLabel->rot )
1751 {
1752 for( int i = 0; i < KiROUND( aLabel->rot->degrees / 90.0 ) %4; ++i )
1753 label->Rotate90( false );
1754
1755 if( aLabel->rot->mirror )
1756 label->MirrorSpinStyle( false );
1757 }
1758
1759 return label.release();
1760}
1761
1762
1763std::pair<VECTOR2I, const SEG*>
1765 const std::vector<SEG>& aLines ) const
1766{
1767 VECTOR2I nearestPoint;
1768 const SEG* nearestLine = nullptr;
1769
1770 double d, mindistance = std::numeric_limits<double>::max();
1771
1772 // Find the nearest start, middle or end of a line from the list of lines.
1773 for( const SEG& line : aLines )
1774 {
1775 VECTOR2I testpoint = line.A;
1776 d = aPoint.Distance( testpoint );
1777
1778 if( d < mindistance )
1779 {
1780 mindistance = d;
1781 nearestPoint = testpoint;
1782 nearestLine = &line;
1783 }
1784
1785 testpoint = line.Center();
1786 d = aPoint.Distance( testpoint );
1787
1788 if( d < mindistance )
1789 {
1790 mindistance = d;
1791 nearestPoint = testpoint;
1792 nearestLine = &line;
1793 }
1794
1795 testpoint = line.B;
1796 d = aPoint.Distance( testpoint );
1797
1798 if( d < mindistance )
1799 {
1800 mindistance = d;
1801 nearestPoint = testpoint;
1802 nearestLine = &line;
1803 }
1804 }
1805
1806 return std::make_pair( nearestPoint, nearestLine );
1807}
1808
1809
1810void SCH_IO_EAGLE::loadInstance( const std::unique_ptr<EINSTANCE>& aInstance,
1811 const std::map<wxString, std::unique_ptr<EPART>>& aParts )
1812{
1813 wxCHECK( aInstance, /* void */ );
1814
1815 SCH_SCREEN* screen = getCurrentScreen();
1816
1817 wxCHECK( screen, /* void */ );
1818
1819 const auto partIt = aParts.find( aInstance->part );
1820
1821 if( partIt == aParts.end() )
1822 {
1823 Report( wxString::Format( _( "Error parsing Eagle file. Could not find '%s' "
1824 "instance but it is referenced in the schematic." ),
1825 aInstance->part ),
1827
1828 return;
1829 }
1830
1831 const std::unique_ptr<EPART>& epart = partIt->second;
1832
1833 wxString libName = epart->library;
1834
1835 // Correctly handle versioned libraries.
1836 if( epart->libraryUrn )
1837 libName += wxS( "_" ) + epart->libraryUrn->assetId;
1838
1839 wxString gatename = epart->deviceset + wxS( "_" ) + epart->device + wxS( "_" ) +
1840 aInstance->gate;
1841 wxString symbolname = wxString( epart->deviceset + epart->device );
1842 symbolname.Replace( wxT( "*" ), wxEmptyString );
1843 wxString kisymbolname = EscapeString( symbolname, CTX_LIBID );
1844
1845 // Eagle schematics can have multiple libraries containing symbols with duplicate symbol
1846 // names. Because this parser stores all of the symbols in a single library, the
1847 // loadSymbol() function, prefixed the original Eagle library name to the symbol name
1848 // in case of a name clash. Check for the prefixed symbol first. This ensures that
1849 // the correct library symbol gets mapped on load.
1850 wxString altSymbolName = libName + wxT( "_" ) + symbolname;
1851 altSymbolName = EscapeString( altSymbolName, CTX_LIBID );
1852
1853 wxString libIdSymbolName = altSymbolName;
1854
1855 const auto libIt = m_eagleLibs.find( libName );
1856
1857 if( libIt == m_eagleLibs.end() )
1858 {
1859 Report( wxString::Format( wxS( "Eagle library '%s' not found while looking up symbol for "
1860 "deviceset '%s', device '%s', and gate '%s." ),
1861 libName, epart->deviceset, epart->device, aInstance->gate ) );
1862 return;
1863 }
1864
1865 const auto gateIt = libIt->second.GateToUnitMap.find( gatename );
1866
1867 if( gateIt == libIt->second.GateToUnitMap.end() )
1868 {
1869 Report( wxString::Format( wxS( "Symbol not found for deviceset '%s', device '%s', and "
1870 "gate '%s in library '%s'." ),
1871 epart->deviceset, epart->device, aInstance->gate, libName ) );
1872 return;
1873 }
1874
1875 int unit = gateIt->second;
1876
1877 wxString package;
1878 EAGLE_LIBRARY* elib = &m_eagleLibs[libName];
1879
1880 auto p = elib->package.find( kisymbolname );
1881
1882 if( p != elib->package.end() )
1883 package = p->second;
1884
1885 // set properties to prevent save file on every symbol save
1886 std::map<std::string, UTF8> properties;
1887 properties.emplace( SCH_IO_KICAD_SEXPR::PropBuffering, wxEmptyString );
1888
1889 LIB_SYMBOL* part = m_pi->LoadSymbol( getLibFileName().GetFullPath(), altSymbolName, &properties );
1890
1891 if( !part )
1892 {
1893 part = m_pi->LoadSymbol( getLibFileName().GetFullPath(), kisymbolname, &properties );
1894 libIdSymbolName = kisymbolname;
1895 }
1896
1897 if( !part )
1898 {
1899 Report( wxString::Format( _( "Could not find '%s' in the imported library." ),
1900 UnescapeString( kisymbolname ) ),
1902 return;
1903 }
1904
1905 LIB_ID libId( getLibName(), libIdSymbolName );
1906 std::unique_ptr<SCH_SYMBOL> symbol = std::make_unique<SCH_SYMBOL>();
1907 symbol->SetLibId( libId );
1908 symbol->SetUnit( unit );
1909 symbol->SetPosition( VECTOR2I( aInstance->x.ToSchUnits(), -aInstance->y.ToSchUnits() ) );
1910
1911 // assume that footprint library is identical to project name
1912 if( !package.IsEmpty() )
1913 {
1914 wxString footprint = m_schematic->Project().GetProjectName() + wxT( ":" ) + package;
1915 symbol->GetField( FIELD_T::FOOTPRINT )->SetText( footprint );
1916 }
1917
1918 if( aInstance->rot )
1919 {
1920 symbol->SetOrientation( kiCadComponentRotation( aInstance->rot->degrees ) );
1921
1922 if( aInstance->rot->mirror )
1923 symbol->MirrorHorizontally( aInstance->x.ToSchUnits() );
1924 }
1925
1926 std::vector<SCH_FIELD*> partFields;
1927 part->GetFields( partFields );
1928
1929 for( const SCH_FIELD* partField : partFields )
1930 {
1931 SCH_FIELD* symbolField;
1932
1933 if( partField->IsMandatory() )
1934 symbolField = symbol->GetField( partField->GetId() );
1935 else
1936 symbolField = symbol->GetField( partField->GetName() );
1937
1938 wxCHECK2( symbolField, continue );
1939
1940 symbolField->ImportValues( *partField );
1941 symbolField->SetTextPos( symbol->GetPosition() + partField->GetTextPos() );
1942 }
1943
1944 // If there is no footprint assigned, then prepend the reference value
1945 // with a hash character to mute netlist updater complaints
1946 wxString reference = package.IsEmpty() ? '#' + aInstance->part : aInstance->part;
1947
1948 // reference must end with a number but EAGLE does not enforce this
1949 if( reference.find_last_not_of( wxT( "0123456789" ) ) == ( reference.Length()-1 ) )
1950 reference.Append( wxT( "0" ) );
1951
1952 // EAGLE allows references to be single digits. This breaks KiCad netlisting, which requires
1953 // parts to have non-digit + digit annotation. If the reference begins with a number,
1954 // we prepend 'UNK' (unknown) for the symbol designator
1955 if( reference.find_first_not_of( wxT( "0123456789" ) ) != 0 )
1956 reference.Prepend( wxT( "UNK" ) );
1957
1958 // EAGLE allows designator to start with # but that is used in KiCad
1959 // for symbols which do not have a footprint
1960 if( aInstance->part.find_first_not_of( wxT( "#" ) ) != 0 )
1961 reference.Prepend( wxT( "UNK" ) );
1962
1963 SCH_FIELD* referenceField = symbol->GetField( FIELD_T::REFERENCE );
1964 referenceField->SetText( reference );
1965
1966 SCH_FIELD* valueField = symbol->GetField( FIELD_T::VALUE );
1967 bool userValue = m_userValue.at( libIdSymbolName );
1968
1969 if( part->GetUnitCount() > 1 )
1970 {
1971 getEagleSymbolFieldAttributes( aInstance, wxS( ">NAME" ), referenceField );
1972 getEagleSymbolFieldAttributes( aInstance, wxS( ">VALUE" ), valueField );
1973 }
1974
1975 if( epart->value && !epart->value.CGet().IsEmpty() )
1976 {
1977 valueField->SetText( *epart->value );
1978 }
1979 else
1980 {
1981 valueField->SetText( kisymbolname );
1982
1983 if( userValue )
1984 valueField->SetVisible( false );
1985 }
1986
1987 for( const auto& [ attrName, attr ] : epart->attributes )
1988 {
1989 SCH_FIELD newField( symbol.get(), FIELD_T::USER );
1990
1991 newField.SetName( attrName );
1992
1993 if( !symbol->GetFields().empty() )
1994 newField.SetTextPos( symbol->GetFields().back().GetPosition() );
1995
1996 if( attr->value )
1997 newField.SetText( *attr->value );
1998
1999 newField.SetVisible( ( attr->display == EATTR::Off ) ? false : true );
2000
2001 symbol->AddField( newField );
2002 }
2003
2004 for( const auto& [variantName, variant] : epart->variants )
2005 {
2006 SCH_FIELD* field = symbol->AddField( *symbol->GetField( FIELD_T::VALUE ) );
2007 field->SetName( wxT( "VARIANT_" ) + variant->name );
2008
2009 if( variant->value )
2010 field->SetText( *variant->value );
2011
2012 field->SetVisible( false );
2013 }
2014
2015 bool valueAttributeFound = false;
2016 bool nameAttributeFound = false;
2017
2018 // Parse attributes for the instance
2019 for( auto& [name, eattr] : aInstance->attributes )
2020 {
2021 SCH_FIELD* field = nullptr;
2022
2023 if( eattr->name.Lower() == wxT( "name" ) )
2024 {
2025 field = symbol->GetField( FIELD_T::REFERENCE );
2026 nameAttributeFound = true;
2027 }
2028 else if( eattr->name.Lower() == wxT( "value" ) )
2029 {
2030 field = symbol->GetField( FIELD_T::VALUE );
2031 valueAttributeFound = true;
2032 }
2033 else
2034 {
2035 field = symbol->GetField( eattr->name );
2036
2037 if( field )
2038 field->SetVisible( false );
2039 }
2040
2041 if( field )
2042 {
2043 field->SetPosition( VECTOR2I( eattr->x->ToSchUnits(), -eattr->y->ToSchUnits() ) );
2044 int align = eattr->align ? *eattr->align : ETEXT::BOTTOM_LEFT;
2045 int absdegrees = eattr->rot ? eattr->rot->degrees : 0;
2046 bool mirror = eattr->rot ? eattr->rot->mirror : false;
2047
2048 if( aInstance->rot && aInstance->rot->mirror )
2049 mirror = !mirror;
2050
2051 bool spin = eattr->rot ? eattr->rot->spin : false;
2052
2053 if( eattr->display == EATTR::Off || eattr->display == EATTR::NAME )
2054 field->SetVisible( false );
2055
2056 int rotation = aInstance->rot ? aInstance->rot->degrees : 0;
2057 int reldegrees = ( absdegrees - rotation + 360.0 );
2058 reldegrees %= 360;
2059
2060 eagleToKicadAlignment( field, align, reldegrees, mirror, spin, absdegrees );
2061 }
2062 }
2063
2064 // Use the instance attribute to determine the reference and value field visibility.
2065 if( aInstance->smashed && aInstance->smashed.Get() )
2066 {
2067 symbol->GetField( FIELD_T::VALUE )->SetVisible( valueAttributeFound );
2068 symbol->GetField( FIELD_T::REFERENCE )->SetVisible( nameAttributeFound );
2069 }
2070
2071 // Eagle has a brain dead module reference scheme where the module names separated by colons
2072 // are prefixed to the symbol references. This will get blown away in KiCad the first time
2073 // any annotation is performed. It is required for the initial synchronization between the
2074 // schematic and the board.
2075 wxString refPrefix;
2076
2077 for( const EMODULEINST* emoduleInst : m_moduleInstances )
2078 {
2079 wxCHECK2( emoduleInst, continue );
2080
2081 refPrefix += emoduleInst->name + wxS( ":" );
2082 }
2083
2084 symbol->AddHierarchicalReference( m_sheetPath.Path(), refPrefix + reference, unit );
2085
2086 // Cache the lib symbol so pin positions are available for connection-point tracking.
2087 // Use the already-loaded `part` directly rather than re-fetching through the adapter,
2088 // because the .kicad_sym library is still buffered in m_pi and has not yet been saved to
2089 // disk at the time loadInstance runs.
2090 symbol->SetLibSymbol( new LIB_SYMBOL( *part ) );
2091
2092 for( const SCH_PIN* pin : symbol->GetLibPins() )
2093 m_connPoints[symbol->GetPinPhysicalPosition( pin )].emplace( pin );
2094
2095 if( part->IsGlobalPower() )
2096 m_powerPorts[ reference ] = symbol->GetField( FIELD_T::VALUE )->GetText();
2097
2098 symbol->ClearFlags();
2099
2100 screen->Append( symbol.release() );
2101}
2102
2103
2105{
2106 wxCHECK( aLibrary && aEagleLibrary, nullptr );
2107
2108 // Loop through the device sets and load each of them
2109 for( const auto& [name, edeviceset] : aLibrary->devicesets )
2110 {
2111 // Get Device set information
2112 wxString prefix = edeviceset->prefix ? edeviceset->prefix.Get() : wxString( wxT( "" ) );
2113 wxString deviceSetDescr;
2114
2115 if( edeviceset->description )
2116 deviceSetDescr = convertDescription( UnescapeHTML( edeviceset->description->text ) );
2117
2118 // For each device in the device set:
2119 for( const std::unique_ptr<EDEVICE>& edevice : edeviceset->devices )
2120 {
2121 // Create symbol name from deviceset and device names.
2122 wxString symbolName = edeviceset->name + edevice->name;
2123 symbolName.Replace( wxT( "*" ), wxEmptyString );
2124 wxASSERT( !symbolName.IsEmpty() );
2125 symbolName = EscapeString( symbolName, CTX_LIBID );
2126
2127 if( edevice->package )
2128 aEagleLibrary->package[symbolName] = edevice->package.Get();
2129
2130 // Create KiCad symbol.
2131 std::unique_ptr<LIB_SYMBOL> libSymbol = std::make_unique<LIB_SYMBOL>( symbolName );
2132
2133 // Process each gate in the deviceset for this device.
2134 int gate_count = static_cast<int>( edeviceset->gates.size() );
2135 libSymbol->SetUnitCount( gate_count, true );
2136 libSymbol->LockUnits( true );
2137
2138 SCH_FIELD* reference = libSymbol->GetField( FIELD_T::REFERENCE );
2139
2140 if( prefix.length() == 0 )
2141 {
2142 reference->SetVisible( false );
2143 }
2144 else
2145 {
2146 // If there is no footprint assigned, then prepend the reference value
2147 // with a hash character to mute netlist updater complaints
2148 reference->SetText( edevice->package ? prefix : '#' + prefix );
2149 }
2150
2151 libSymbol->GetValueField().SetVisible( true );
2152
2153 int gateindex = 1;
2154 bool ispower = false;
2155
2156 for( const auto& [gateName, egate] : edeviceset->gates )
2157 {
2158 const auto it = aLibrary->symbols.find( egate->symbol );
2159
2160 if( it == aLibrary->symbols.end() )
2161 {
2162 Report( wxString::Format( wxS( "Eagle symbol '%s' not found in library '%s'." ),
2163 egate->symbol, aLibrary->GetName() ) );
2164 continue;
2165 }
2166
2167 wxString gateMapName = edeviceset->name + wxS( "_" ) + edevice->name +
2168 wxS( "_" ) + egate->name;
2169 aEagleLibrary->GateToUnitMap[gateMapName] = gateindex;
2170 ispower = loadSymbol( it->second, libSymbol, edevice, gateindex, egate->name );
2171
2172 gateindex++;
2173 }
2174
2175 libSymbol->SetUnitCount( gate_count, true );
2176
2177 if( gate_count == 1 && ispower )
2178 libSymbol->SetGlobalPower();
2179
2180 // Don't set the footprint field if no package is defined in the Eagle schematic.
2181 if( edevice->package )
2182 {
2183 wxString libName;
2184
2185 if( m_schematic )
2186 {
2187 // assume that footprint library is identical to project name
2188 libName = m_schematic->Project().GetProjectName();
2189 }
2190 else
2191 {
2192 libName = m_libName;
2193 }
2194
2195 wxString packageString = libName + wxT( ":" ) + aEagleLibrary->package[symbolName];
2196
2197 libSymbol->GetFootprintField().SetText( packageString );
2198 }
2199
2200 wxString libName = libSymbol->GetName();
2201 libSymbol->SetName( libName );
2202 libSymbol->SetDescription( deviceSetDescr );
2203
2204 if( m_pi )
2205 {
2206 // If duplicate symbol names exist in multiple Eagle symbol libraries, prefix the
2207 // Eagle symbol library name to the symbol which should ensure that it is unique.
2208 try
2209 {
2210 if( m_pi->LoadSymbol( getLibFileName().GetFullPath(), libName ) )
2211 {
2212 libName = aEagleLibrary->name + wxT( "_" ) + libName;
2213 libName = EscapeString( libName, CTX_LIBID );
2214 libSymbol->SetName( libName );
2215 }
2216
2217 // set properties to prevent save file on every symbol save
2218 std::map<std::string, UTF8> properties;
2219 properties.emplace( SCH_IO_KICAD_SEXPR::PropBuffering, wxEmptyString );
2220
2221 m_pi->SaveSymbol( getLibFileName().GetFullPath(), new LIB_SYMBOL( *libSymbol.get() ),
2222 &properties );
2223 }
2224 catch(...)
2225 {
2226 // A library symbol cannot be loaded for some reason.
2227 // Just skip this symbol creating an issue.
2228 // The issue will be reported later by the Reporter
2229 }
2230 }
2231
2232 aEagleLibrary->KiCadSymbols[ libName ] = std::move( libSymbol );
2233
2234 // Store information on whether the value of FIELD_T::VALUE for a part should be
2235 // part/@value or part/@deviceset + part/@device.
2236 m_userValue.emplace( std::make_pair( libName, edeviceset->uservalue == true ) );
2237 }
2238 }
2239
2240 return aEagleLibrary;
2241}
2242
2243
2244bool SCH_IO_EAGLE::loadSymbol( const std::unique_ptr<ESYMBOL>& aEsymbol,
2245 std::unique_ptr<LIB_SYMBOL>& aSymbol,
2246 const std::unique_ptr<EDEVICE>& aDevice, int aGateNumber,
2247 const wxString& aGateName )
2248{
2249 wxCHECK( aEsymbol && aSymbol && aDevice, false );
2250
2251 std::vector<SCH_ITEM*> items;
2252
2253 bool showRefDes = false;
2254 bool showValue = false;
2255 bool ispower = false;
2256 int pincount = 0;
2257
2258 for( const std::unique_ptr<ECIRCLE>& ecircle : aEsymbol->circles )
2259 aSymbol->AddDrawItem( loadSymbolCircle( aSymbol, ecircle, aGateNumber ) );
2260
2261 for( const std::unique_ptr<EPIN>& epin : aEsymbol->pins )
2262 {
2263 std::unique_ptr<SCH_PIN> pin( loadPin( aSymbol, epin, aGateNumber ) );
2264 pincount++;
2265
2266 pin->SetType( ELECTRICAL_PINTYPE::PT_BIDI );
2267
2268 if( epin->direction )
2269 {
2270 for( const auto& pinDir : pinDirectionsMap )
2271 {
2272 if( epin->direction->Lower() == pinDir.first )
2273 {
2274 pin->SetType( pinDir.second );
2275
2276 if( pinDir.first == wxT( "sup" ) ) // power supply symbol
2277 ispower = true;
2278
2279 break;
2280 }
2281 }
2282
2283 }
2284
2285 if( aDevice->connects.size() != 0 )
2286 {
2287 for( const std::unique_ptr<ECONNECT>& connect : aDevice->connects )
2288 {
2289 if( connect->gate == aGateName && pin->GetName() == connect->pin )
2290 {
2291 wxArrayString pads = wxSplit( wxString( connect->pad ), ' ' );
2292
2293 pin->SetUnit( aGateNumber );
2294 pin->SetName( escapeName( pin->GetName() ) );
2295
2296 if( pads.GetCount() > 1 )
2297 {
2298 pin->SetNumberTextSize( 0 );
2299 }
2300
2301 for( unsigned i = 0; i < pads.GetCount(); i++ )
2302 {
2303 SCH_PIN* apin = new SCH_PIN( *pin );
2304
2305 wxString padname( pads[i] );
2306 apin->SetNumber( padname );
2307 aSymbol->AddDrawItem( apin );
2308 }
2309
2310 break;
2311 }
2312 }
2313 }
2314 else
2315 {
2316 pin->SetUnit( aGateNumber );
2317 pin->SetNumber( wxString::Format( wxT( "%i" ), pincount ) );
2318 aSymbol->AddDrawItem( pin.release() );
2319 }
2320 }
2321
2322 for( const std::unique_ptr<EPOLYGON>& epolygon : aEsymbol->polygons )
2323 aSymbol->AddDrawItem( loadSymbolPolyLine( aSymbol, epolygon, aGateNumber ) );
2324
2325 for( const std::unique_ptr<ERECT>& erectangle : aEsymbol->rectangles )
2326 aSymbol->AddDrawItem( loadSymbolRectangle( aSymbol, erectangle, aGateNumber ) );
2327
2328 for( const std::unique_ptr<ETEXT>& etext : aEsymbol->texts )
2329 {
2330 std::unique_ptr<SCH_TEXT> libtext( loadSymbolText( aSymbol, etext, aGateNumber ) );
2331
2332 if( libtext->GetText() == wxT( "${REFERENCE}" ) )
2333 {
2334 // Move text & attributes to Reference field and discard LIB_TEXT item
2335 loadFieldAttributes( &aSymbol->GetReferenceField(), libtext.get() );
2336
2337 // Show Reference field if Eagle reference was uppercase
2338 showRefDes = etext->text == wxT( ">NAME" );
2339 }
2340 else if( libtext->GetText() == wxT( "${VALUE}" ) )
2341 {
2342 // Move text & attributes to Value field and discard LIB_TEXT item
2343 loadFieldAttributes( &aSymbol->GetValueField(), libtext.get() );
2344
2345 // Show Value field if Eagle reference was uppercase
2346 showValue = etext->text == wxT( ">VALUE" );
2347 }
2348 else
2349 {
2350 aSymbol->AddDrawItem( libtext.release() );
2351 }
2352 }
2353
2354 for( const std::unique_ptr<EWIRE>& ewire : aEsymbol->wires )
2355 aSymbol->AddDrawItem( loadSymbolWire( aSymbol, ewire, aGateNumber ) );
2356
2357 for( const std::unique_ptr<EFRAME>& eframe : aEsymbol->frames )
2358 {
2359 std::vector<SCH_ITEM*> frameItems;
2360
2361 loadFrame( eframe, frameItems );
2362
2363 for( SCH_ITEM* item : frameItems )
2364 {
2365 item->SetParent( aSymbol.get() );
2366 item->SetUnit( aGateNumber );
2367 aSymbol->AddDrawItem( item );
2368 }
2369 }
2370
2371 aSymbol->GetReferenceField().SetVisible( showRefDes );
2372 aSymbol->GetValueField().SetVisible( showValue );
2373
2374 return pincount == 1 ? ispower : false;
2375}
2376
2377
2378SCH_SHAPE* SCH_IO_EAGLE::loadSymbolCircle( std::unique_ptr<LIB_SYMBOL>& aSymbol,
2379 const std::unique_ptr<ECIRCLE>& aCircle,
2380 int aGateNumber )
2381{
2382 wxCHECK( aSymbol && aCircle, nullptr );
2383
2384 // Parse the circle properties
2386 VECTOR2I center( aCircle->x.ToSchUnits(), -aCircle->y.ToSchUnits() );
2387
2388 circle->SetParent( aSymbol.get() );
2389 circle->SetPosition( center );
2390 circle->SetEnd( VECTOR2I( center.x + aCircle->radius.ToSchUnits(), center.y ) );
2391 circle->SetStroke( STROKE_PARAMS( aCircle->width.ToSchUnits(), LINE_STYLE::SOLID ) );
2392 circle->SetUnit( aGateNumber );
2393
2394 return circle;
2395}
2396
2397
2398SCH_SHAPE* SCH_IO_EAGLE::loadSymbolRectangle( std::unique_ptr<LIB_SYMBOL>& aSymbol,
2399 const std::unique_ptr<ERECT>& aRectangle,
2400 int aGateNumber )
2401{
2402 wxCHECK( aSymbol && aRectangle, nullptr );
2403
2404 SCH_SHAPE* rectangle = new SCH_SHAPE( SHAPE_T::RECTANGLE );
2405
2406 rectangle->SetParent( aSymbol.get() );
2407 rectangle->SetPosition( VECTOR2I( aRectangle->x1.ToSchUnits(), -aRectangle->y1.ToSchUnits() ) );
2408 rectangle->SetEnd( VECTOR2I( aRectangle->x2.ToSchUnits(), -aRectangle->y2.ToSchUnits() ) );
2409
2410 if( aRectangle->rot )
2411 {
2412 VECTOR2I pos( rectangle->GetPosition() );
2413 VECTOR2I end( rectangle->GetEnd() );
2414 VECTOR2I center( rectangle->GetCenter() );
2415
2416 RotatePoint( pos, center, EDA_ANGLE( aRectangle->rot->degrees, DEGREES_T ) );
2417 RotatePoint( end, center, EDA_ANGLE( aRectangle->rot->degrees, DEGREES_T ) );
2418
2419 rectangle->SetPosition( pos );
2420 rectangle->SetEnd( end );
2421 }
2422
2423 rectangle->SetUnit( aGateNumber );
2424
2425 // Eagle rectangles are filled by definition.
2426 rectangle->SetFillMode( FILL_T::FILLED_SHAPE );
2427
2428 return rectangle;
2429}
2430
2431
2432SCH_ITEM* SCH_IO_EAGLE::loadSymbolWire( std::unique_ptr<LIB_SYMBOL>& aSymbol,
2433 const std::unique_ptr<EWIRE>& aWire, int aGateNumber )
2434{
2435 wxCHECK( aSymbol && aWire, nullptr );
2436
2437 VECTOR2I begin, end;
2438
2439 begin.x = aWire->x1.ToSchUnits();
2440 begin.y = -aWire->y1.ToSchUnits();
2441 end.x = aWire->x2.ToSchUnits();
2442 end.y = -aWire->y2.ToSchUnits();
2443
2444 if( begin == end )
2445 return nullptr;
2446
2447 // if the wire is an arc
2448 if( aWire->curve )
2449 {
2451 VECTOR2I center = ConvertArcCenter( begin, end, *aWire->curve );
2452 double radius = sqrt( ( ( center.x - begin.x ) * ( center.x - begin.x ) ) +
2453 ( ( center.y - begin.y ) * ( center.y - begin.y ) ) );
2454
2455 arc->SetParent( aSymbol.get() );
2456
2457 // this emulates the filled semicircles created by a thick arc with flat ends caps.
2458 if( aWire->cap == EWIRE::FLAT && aWire->width.ToSchUnits() >= 2 * radius )
2459 {
2460 VECTOR2I centerStartVector = ( begin - center ) *
2461 ( aWire->width.ToSchUnits() / radius );
2462 begin = center + centerStartVector;
2463
2466 }
2467 else
2468 {
2469 arc->SetStroke( STROKE_PARAMS( aWire->width.ToSchUnits(), LINE_STYLE::SOLID ) );
2470 }
2471
2472 arc->SetCenter( center );
2473 arc->SetStart( begin );
2474
2475 // KiCad rotates the other way.
2476 arc->SetArcAngleAndEnd( -EDA_ANGLE( *aWire->curve, DEGREES_T ), true );
2477 arc->SetUnit( aGateNumber );
2478
2479 return arc;
2480 }
2481 else
2482 {
2484
2485 poly->AddPoint( begin );
2486 poly->AddPoint( end );
2487 poly->SetUnit( aGateNumber );
2488 poly->SetStroke( STROKE_PARAMS( aWire->width.ToSchUnits(), LINE_STYLE::SOLID ) );
2489
2490 return poly;
2491 }
2492}
2493
2494
2495SCH_SHAPE* SCH_IO_EAGLE::loadSymbolPolyLine( std::unique_ptr<LIB_SYMBOL>& aSymbol,
2496 const std::unique_ptr<EPOLYGON>& aPolygon,
2497 int aGateNumber )
2498{
2499 wxCHECK( aSymbol && aPolygon, nullptr );
2500
2501 SCH_SHAPE* poly = new SCH_SHAPE( SHAPE_T::POLY );
2502 VECTOR2I pt, prev_pt;
2503 opt_double prev_curve;
2504
2505 poly->SetParent( aSymbol.get() );
2506
2507 for( const std::unique_ptr<EVERTEX>& evertex : aPolygon->vertices )
2508 {
2509 pt = VECTOR2I( evertex->x.ToSchUnits(), evertex->y.ToSchUnits() );
2510
2511 if( prev_curve )
2512 {
2513 SHAPE_ARC arc;
2514 arc.ConstructFromStartEndAngle( prev_pt, pt, -EDA_ANGLE( *prev_curve, DEGREES_T ) );
2515 poly->GetPolyShape().Append( arc, -1, -1, ARC_ACCURACY );
2516 }
2517 else
2518 {
2519 poly->AddPoint( pt );
2520 }
2521
2522 prev_pt = pt;
2523 prev_curve = evertex->curve;
2524 }
2525
2526 poly->SetStroke( STROKE_PARAMS( aPolygon->width.ToSchUnits(), LINE_STYLE::SOLID ) );
2528 poly->SetUnit( aGateNumber );
2529
2530 return poly;
2531}
2532
2533
2534SCH_PIN* SCH_IO_EAGLE::loadPin( std::unique_ptr<LIB_SYMBOL>& aSymbol,
2535 const std::unique_ptr<EPIN>& aPin, int aGateNumber )
2536{
2537 wxCHECK( aSymbol && aPin, nullptr );
2538
2539 std::unique_ptr<SCH_PIN> pin = std::make_unique<SCH_PIN>( aSymbol.get() );
2540 pin->SetPosition( VECTOR2I( aPin->x.ToSchUnits(), -aPin->y.ToSchUnits() ) );
2541 pin->SetName( aPin->name );
2542 pin->SetUnit( aGateNumber );
2543
2544 int roti = aPin->rot ? aPin->rot->degrees : 0;
2545
2546 switch( roti )
2547 {
2548 case 0: pin->SetOrientation( PIN_ORIENTATION::PIN_RIGHT ); break;
2549 case 90: pin->SetOrientation( PIN_ORIENTATION::PIN_UP ); break;
2550 case 180: pin->SetOrientation( PIN_ORIENTATION::PIN_LEFT ); break;
2551 case 270: pin->SetOrientation( PIN_ORIENTATION::PIN_DOWN ); break;
2552 default: wxFAIL_MSG( wxString::Format( wxT( "Unhandled orientation (%d degrees)." ), roti ) );
2553 }
2554
2555 pin->SetLength( schIUScale.MilsToIU( 300 ) ); // Default pin length when not defined.
2556
2557 if( aPin->length )
2558 {
2559 wxString length = aPin->length.Get();
2560
2561 if( length == wxT( "short" ) )
2562 pin->SetLength( schIUScale.MilsToIU( 100 ) );
2563 else if( length == wxT( "middle" ) )
2564 pin->SetLength( schIUScale.MilsToIU( 200 ) );
2565 else if( length == wxT( "long" ) )
2566 pin->SetLength( schIUScale.MilsToIU( 300 ) );
2567 else if( length == wxT( "point" ) )
2568 pin->SetLength( schIUScale.MilsToIU( 0 ) );
2569 }
2570
2571 // Pin names and numbers are fixed size in Eagle.
2572 pin->SetNumberTextSize( schIUScale.MilsToIU( 60 ) );
2573 pin->SetNameTextSize( schIUScale.MilsToIU( 60 ) );
2574
2575 // emulate the visibility of pin elements
2576 if( aPin->visible )
2577 {
2578 wxString visible = aPin->visible.Get();
2579
2580 if( visible == wxT( "off" ) )
2581 {
2582 pin->SetNameTextSize( 0 );
2583 pin->SetNumberTextSize( 0 );
2584 }
2585 else if( visible == wxT( "pad" ) )
2586 {
2587 pin->SetNameTextSize( 0 );
2588 }
2589 else if( visible == wxT( "pin" ) )
2590 {
2591 pin->SetNumberTextSize( 0 );
2592 }
2593
2594 /*
2595 * else if( visible == wxT( "both" ) )
2596 * {
2597 * }
2598 */
2599 }
2600
2601 if( aPin->function )
2602 {
2603 wxString function = aPin->function.Get();
2604
2605 if( function == wxT( "dot" ) )
2606 pin->SetShape( GRAPHIC_PINSHAPE::INVERTED );
2607 else if( function == wxT( "clk" ) )
2608 pin->SetShape( GRAPHIC_PINSHAPE::CLOCK );
2609 else if( function == wxT( "dotclk" ) )
2611 }
2612
2613 return pin.release();
2614}
2615
2616
2617SCH_TEXT* SCH_IO_EAGLE::loadSymbolText( std::unique_ptr<LIB_SYMBOL>& aSymbol,
2618 const std::unique_ptr<ETEXT>& aText, int aGateNumber )
2619{
2620 wxCHECK( aSymbol && aText, nullptr );
2621
2622 std::unique_ptr<SCH_TEXT> libtext = std::make_unique<SCH_TEXT>();
2623
2624 libtext->SetParent( aSymbol.get() );
2625 libtext->SetUnit( aGateNumber );
2626 libtext->SetPosition( VECTOR2I( aText->x.ToSchUnits(), -aText->y.ToSchUnits() ) );
2627
2628 const wxString& eagleText = aText->text;
2629 wxString adjustedText;
2630 wxStringTokenizer tokenizer( eagleText, "\r\n" );
2631
2632 // Strip the whitespace from both ends of each line.
2633 while( tokenizer.HasMoreTokens() )
2634 {
2635 wxString tmp = interpretText( tokenizer.GetNextToken().Trim( true ).Trim( false ) );
2636
2637 if( tokenizer.HasMoreTokens() )
2638 tmp += wxT( "\n" );
2639
2640 adjustedText += tmp;
2641 }
2642
2643 libtext->SetText( adjustedText.IsEmpty() ? wxString( wxS( "~" ) ) : adjustedText );
2644
2645 loadTextAttributes( libtext.get(), aText );
2646
2647 return libtext.release();
2648}
2649
2650
2651SCH_TEXT* SCH_IO_EAGLE::loadPlainText( const std::unique_ptr<ETEXT>& aText )
2652{
2653 wxCHECK( aText, nullptr );
2654
2655 std::unique_ptr<SCH_TEXT> schtext = std::make_unique<SCH_TEXT>();
2656
2657 const wxString& eagleText = aText->text;
2658 wxString adjustedText;
2659 wxStringTokenizer tokenizer( eagleText, "\r\n" );
2660
2661 // Strip the whitespace from both ends of each line.
2662 while( tokenizer.HasMoreTokens() )
2663 {
2664 wxString tmp = interpretText( tokenizer.GetNextToken().Trim( true ).Trim( false ) );
2665
2666 if( tokenizer.HasMoreTokens() )
2667 tmp += wxT( "\n" );
2668
2669 adjustedText += tmp;
2670 }
2671
2672 schtext->SetText( adjustedText.IsEmpty() ? wxString( wxS( "\" \"" ) )
2673 : escapeName( adjustedText ) );
2674
2675 schtext->SetPosition( VECTOR2I( aText->x.ToSchUnits(), -aText->y.ToSchUnits() ) );
2676 loadTextAttributes( schtext.get(), aText );
2677 schtext->SetItalic( false );
2678
2679 return schtext.release();
2680}
2681
2682
2684 const std::unique_ptr<ETEXT>& aAttributes ) const
2685{
2686 wxCHECK( aText && aAttributes, /* void */ );
2687
2688 aText->SetTextSize( aAttributes->ConvertSize() );
2689
2690 // Must come after SetTextSize()
2691 if( aAttributes->ratio && aAttributes->ratio.CGet() > 12 )
2692 aText->SetBold( true );
2693
2694 int align = aAttributes->align ? *aAttributes->align : ETEXT::BOTTOM_LEFT;
2695 int degrees = aAttributes->rot ? aAttributes->rot->degrees : 0;
2696 bool mirror = aAttributes->rot ? aAttributes->rot->mirror : false;
2697 bool spin = aAttributes->rot ? aAttributes->rot->spin : false;
2698
2699 eagleToKicadAlignment( aText, align, degrees, mirror, spin, 0 );
2700}
2701
2702
2703void SCH_IO_EAGLE::loadFieldAttributes( SCH_FIELD* aField, const SCH_TEXT* aText ) const
2704{
2705 wxCHECK( aField && aText, /* void */ );
2706
2707 aField->SetTextPos( aText->GetPosition() );
2708 aField->SetTextSize( aText->GetTextSize() );
2709 aField->SetTextAngle( aText->GetTextAngle() );
2710
2711 // Must come after SetTextSize()
2712 aField->SetBold( aText->IsBold() );
2713 aField->SetItalic( false );
2714
2715 aField->SetVertJustify( aText->GetVertJustify() );
2716 aField->SetHorizJustify( aText->GetHorizJustify() );
2717}
2718
2719
2721{
2722 // Eagle supports detached labels, so a label does not need to be placed on a wire
2723 // to be associated with it. KiCad needs to move them, so the labels actually touch the
2724 // corresponding wires.
2725
2726 // Sort the intersection points to speed up the search process
2727 std::sort( m_wireIntersections.begin(), m_wireIntersections.end() );
2728
2729 auto onIntersection =
2730 [&]( const VECTOR2I& aPos )
2731 {
2732 return std::binary_search( m_wireIntersections.begin(),
2733 m_wireIntersections.end(), aPos );
2734 };
2735
2736 for( SEG_DESC& segDesc : m_segments )
2737 {
2738 for( SCH_TEXT* label : segDesc.labels )
2739 {
2740 VECTOR2I labelPos( label->GetPosition() );
2741 const SEG* segAttached = segDesc.LabelAttached( label );
2742
2743 if( segAttached && !onIntersection( labelPos ) )
2744 continue; // label is placed correctly
2745
2746 // Move the label to the nearest wire
2747 if( !segAttached )
2748 {
2749 std::tie( labelPos, segAttached ) = findNearestLinePoint( label->GetPosition(),
2750 segDesc.segs );
2751
2752 if( !segAttached ) // we cannot do anything
2753 continue;
2754 }
2755
2756 // Create a vector pointing in the direction of the wire, 50 mils long
2757 VECTOR2I wireDirection( segAttached->B - segAttached->A );
2758 wireDirection = wireDirection.Resize( schIUScale.MilsToIU( 50 ) );
2759 const VECTOR2I origPos( labelPos );
2760
2761 // Flags determining the search direction
2762 bool checkPositive = true, checkNegative = true, move = false;
2763 int trial = 0;
2764
2765 // Be sure the label is not placed on a wire intersection
2766 while( ( !move || onIntersection( labelPos ) ) && ( checkPositive || checkNegative ) )
2767 {
2768 move = false;
2769
2770 // Move along the attached wire to find the new label position
2771 if( trial % 2 == 1 )
2772 {
2773 labelPos = VECTOR2I( origPos + wireDirection * trial / 2 );
2774 move = checkPositive = segAttached->Contains( labelPos );
2775 }
2776 else
2777 {
2778 labelPos = VECTOR2I( origPos - wireDirection * trial / 2 );
2779 move = checkNegative = segAttached->Contains( labelPos );
2780 }
2781
2782 ++trial;
2783 }
2784
2785 if( move )
2786 label->SetPosition( VECTOR2I( labelPos ) );
2787 }
2788 }
2789
2790 m_segments.clear();
2791 m_wireIntersections.clear();
2792}
2793
2794
2795bool SCH_IO_EAGLE::CanReadSchematicFile( const wxString& aFileName ) const
2796{
2797 if( !SCH_IO::CanReadSchematicFile( aFileName ) )
2798 return false;
2799
2800 return checkHeader( aFileName );
2801}
2802
2803
2804bool SCH_IO_EAGLE::CanReadLibrary( const wxString& aFileName ) const
2805{
2806 if( !SCH_IO::CanReadLibrary( aFileName ) )
2807 return false;
2808
2809 return checkHeader( aFileName );
2810}
2811
2812
2813bool SCH_IO_EAGLE::checkHeader( const wxString& aFileName ) const
2814{
2815 wxFileInputStream input( aFileName );
2816
2817 if( !input.IsOk() )
2818 return false;
2819
2820 wxTextInputStream text( input );
2821
2822 for( int i = 0; i < 8; i++ )
2823 {
2824 if( input.Eof() )
2825 return false;
2826
2827 if( text.ReadLine().Contains( wxS( "<eagle" ) ) )
2828 return true;
2829 }
2830
2831 return false;
2832}
2833
2834
2835void SCH_IO_EAGLE::moveLabels( SCH_LINE* aWire, const VECTOR2I& aNewEndPoint )
2836{
2837 wxCHECK( aWire, /* void */ );
2838
2839 SCH_SCREEN* screen = getCurrentScreen();
2840
2841 wxCHECK( screen, /* void */ );
2842
2843 for( SCH_ITEM* item : screen->Items().Overlapping( aWire->GetBoundingBox() ) )
2844 {
2845 if( !item->IsType( { SCH_LABEL_LOCATE_ANY_T } ) )
2846 continue;
2847
2848 if( TestSegmentHit( item->GetPosition(), aWire->GetStartPoint(), aWire->GetEndPoint(), 0 ) )
2849 item->SetPosition( aNewEndPoint );
2850 }
2851}
2852
2853
2855{
2856 // Add bus entry symbols
2857 // TODO: Cleanup this function and break into pieces
2858
2859 // for each wire segment, compare each end with all busses.
2860 // If the wire end is found to end on a bus segment, place a bus entry symbol.
2861
2862 std::vector<SCH_LINE*> buses;
2863 std::vector<SCH_LINE*> wires;
2864
2865 SCH_SCREEN* screen = getCurrentScreen();
2866
2867 wxCHECK( screen, /* void */ );
2868
2869 for( SCH_ITEM* ii : screen->Items().OfType( SCH_LINE_T ) )
2870 {
2871 SCH_LINE* line = static_cast<SCH_LINE*>( ii );
2872
2873 if( line->IsBus() )
2874 buses.push_back( line );
2875 else if( line->IsWire() )
2876 wires.push_back( line );
2877 }
2878
2879 for( SCH_LINE* wire : wires )
2880 {
2881 VECTOR2I wireStart = wire->GetStartPoint();
2882 VECTOR2I wireEnd = wire->GetEndPoint();
2883
2884 for( SCH_LINE* bus : buses )
2885 {
2886 VECTOR2I busStart = bus->GetStartPoint();
2887 VECTOR2I busEnd = bus->GetEndPoint();
2888
2889 auto entrySize =
2890 []( int signX, int signY ) -> VECTOR2I
2891 {
2892 return VECTOR2I( schIUScale.MilsToIU( DEFAULT_SCH_ENTRY_SIZE ) * signX,
2893 schIUScale.MilsToIU( DEFAULT_SCH_ENTRY_SIZE ) * signY );
2894 };
2895
2896 auto testBusHit =
2897 [&]( const VECTOR2I& aPt ) -> bool
2898 {
2899 return TestSegmentHit( aPt, busStart, busEnd, 0 );
2900 };
2901
2902 if( wireStart.y == wireEnd.y && busStart.x == busEnd.x )
2903 {
2904 // Horizontal wire and vertical bus
2905
2906 if( testBusHit( wireStart ) )
2907 {
2908 // Wire start is on the vertical bus
2909
2910 if( wireEnd.x < busStart.x )
2911 {
2912 /* the end of the wire is to the left of the bus
2913 * ⎥⎢
2914 * ——————⎥⎢
2915 * ⎥⎢
2916 */
2917 VECTOR2I p = wireStart + entrySize( -1, 0 );
2918
2919 if( testBusHit( wireStart + entrySize( 0, -1 ) ) )
2920 {
2921 /* there is room above the wire for the bus entry
2922 * ⎥⎢
2923 * _____/⎥⎢
2924 * ⎥⎢
2925 */
2926 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 1 );
2927 busEntry->SetFlags( IS_NEW );
2928 screen->Append( busEntry );
2929 moveLabels( wire, p );
2930 wire->SetStartPoint( p );
2931 }
2932 else if( testBusHit( wireStart + entrySize( 0, 1 ) ) )
2933 {
2934 /* there is room below the wire for the bus entry
2935 * _____ ⎥⎢
2936 * \⎥⎢
2937 * ⎥⎢
2938 */
2939 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 2 );
2940 busEntry->SetFlags( IS_NEW );
2941 screen->Append( busEntry );
2942 moveLabels( wire, p );
2943 wire->SetStartPoint( p );
2944 }
2945 else
2946 {
2947 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
2948 screen->Append( new SCH_MARKER( std::move( ercItem ), wireStart ) );
2949 }
2950 }
2951 else
2952 {
2953 /* the wire end is to the right of the bus
2954 * ⎥⎢
2955 * ⎥⎢——————
2956 * ⎥⎢
2957 */
2958 VECTOR2I p = wireStart + entrySize( 1, 0 );
2959
2960 if( testBusHit( wireStart + entrySize( 0, -1 ) ) )
2961 {
2962 /* There is room above the wire for the bus entry
2963 * ⎥⎢
2964 * ⎥⎢\_____
2965 * ⎥⎢
2966 */
2967 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p , 4 );
2968 busEntry->SetFlags( IS_NEW );
2969 screen->Append( busEntry );
2970 moveLabels( wire, p );
2971 wire->SetStartPoint( p );
2972 }
2973 else if( testBusHit( wireStart + entrySize( 0, 1 ) ) )
2974 {
2975 /* There is room below the wire for the bus entry
2976 * ⎥⎢ _____
2977 * ⎥⎢/
2978 * ⎥⎢
2979 */
2980 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 3 );
2981 busEntry->SetFlags( IS_NEW );
2982 screen->Append( busEntry );
2983 moveLabels( wire, p );
2984 wire->SetStartPoint( p );
2985 }
2986 else
2987 {
2988 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
2989 screen->Append( new SCH_MARKER( std::move( ercItem ), wireStart ) );
2990 }
2991 }
2992
2993 break;
2994 }
2995 else if( testBusHit( wireEnd ) )
2996 {
2997 // Wire end is on the vertical bus
2998
2999 if( wireStart.x < busStart.x )
3000 {
3001 /* start of the wire is to the left of the bus
3002 * ⎥⎢
3003 * ——————⎥⎢
3004 * ⎥⎢
3005 */
3006 VECTOR2I p = wireEnd + entrySize( -1, 0 );
3007
3008 if( testBusHit( wireEnd + entrySize( 0, -1 ) ) )
3009 {
3010 /* there is room above the wire for the bus entry
3011 * ⎥⎢
3012 * _____/⎥⎢
3013 * ⎥⎢
3014 */
3015 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 1 );
3016 busEntry->SetFlags( IS_NEW );
3017 screen->Append( busEntry );
3018 moveLabels( wire, p );
3019 wire->SetEndPoint( p );
3020 }
3021 else if( testBusHit( wireEnd + entrySize( 0, -1 ) ) )
3022 {
3023 /* there is room below the wire for the bus entry
3024 * _____ ⎥⎢
3025 * \⎥⎢
3026 * ⎥⎢
3027 */
3028 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 2 );
3029 busEntry->SetFlags( IS_NEW );
3030 screen->Append( busEntry );
3031 moveLabels( wire, wireEnd + entrySize( -1, 0 ) );
3032 wire->SetEndPoint( wireEnd + entrySize( -1, 0 ) );
3033 }
3034 else
3035 {
3036 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
3037 screen->Append( new SCH_MARKER( std::move( ercItem ), wireEnd ) );
3038 }
3039 }
3040 else
3041 {
3042 /* the start of the wire is to the right of the bus
3043 * ⎥⎢
3044 * ⎥⎢——————
3045 * ⎥⎢
3046 */
3047 VECTOR2I p = wireEnd + entrySize( 1, 0 );
3048
3049 if( testBusHit( wireEnd + entrySize( 0, -1 ) ) )
3050 {
3051 /* There is room above the wire for the bus entry
3052 * ⎥⎢
3053 * ⎥⎢\_____
3054 * ⎥⎢
3055 */
3056 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 4 );
3057 busEntry->SetFlags( IS_NEW );
3058 screen->Append( busEntry );
3059 moveLabels( wire, p );
3060 wire->SetEndPoint( p );
3061 }
3062 else if( testBusHit( wireEnd + entrySize( 0, 1 ) ) )
3063 {
3064 /* There is room below the wire for the bus entry
3065 * ⎥⎢ _____
3066 * ⎥⎢/
3067 * ⎥⎢
3068 */
3069 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 3 );
3070 busEntry->SetFlags( IS_NEW );
3071 screen->Append( busEntry );
3072 moveLabels( wire, p );
3073 wire->SetEndPoint( p );
3074 }
3075 else
3076 {
3077 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
3078 screen->Append( new SCH_MARKER( std::move( ercItem ), wireEnd ) );
3079 }
3080 }
3081
3082 break;
3083 }
3084 }
3085 else if( wireStart.x == wireEnd.x && busStart.y == busEnd.y )
3086 {
3087 // Vertical wire and horizontal bus
3088
3089 if( testBusHit( wireStart ) )
3090 {
3091 // Wire start is on the bus
3092
3093 if( wireEnd.y < busStart.y )
3094 {
3095 /* the end of the wire is above the bus
3096 * |
3097 * |
3098 * |
3099 * =======
3100 */
3101 VECTOR2I p = wireStart + entrySize( 0, -1 );
3102
3103 if( testBusHit( wireStart + entrySize( -1, 0 ) ) )
3104 {
3105 /* there is room to the left of the wire for the bus entry
3106 * |
3107 * |
3108 * /
3109 * =======
3110 */
3111 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 3 );
3112 busEntry->SetFlags( IS_NEW );
3113 screen->Append( busEntry );
3114 moveLabels( wire, p );
3115 wire->SetStartPoint( p );
3116 }
3117 else if( testBusHit( wireStart + entrySize( 1, 0 ) ) )
3118 {
3119 /* there is room to the right of the wire for the bus entry
3120 * |
3121 * |
3122 * \
3123 * =======
3124 */
3125 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 2 );
3126 busEntry->SetFlags( IS_NEW );
3127 screen->Append( busEntry );
3128 moveLabels( wire, p );
3129 wire->SetStartPoint( p );
3130 }
3131 else
3132 {
3133 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
3134 screen->Append( new SCH_MARKER( std::move( ercItem ), wireStart ) );
3135 }
3136 }
3137 else
3138 {
3139 /* wire end is below the bus
3140 * =======
3141 * |
3142 * |
3143 * |
3144 */
3145 VECTOR2I p = wireStart + entrySize( 0, 1 );
3146
3147 if( testBusHit( wireStart + entrySize( -1, 0 ) ) )
3148 {
3149 /* there is room to the left of the wire for the bus entry
3150 * =======
3151 * \
3152 * |
3153 * |
3154 */
3155 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 4 );
3156 busEntry->SetFlags( IS_NEW );
3157 screen->Append( busEntry );
3158 moveLabels( wire, p );
3159 wire->SetStartPoint( p );
3160 }
3161 else if( testBusHit( wireStart + entrySize( 1, 0 ) ) )
3162 {
3163 /* there is room to the right of the wire for the bus entry
3164 * =======
3165 * /
3166 * |
3167 * |
3168 */
3169 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 1 );
3170 busEntry->SetFlags( IS_NEW );
3171 screen->Append( busEntry );
3172 moveLabels( wire, p );
3173 wire->SetStartPoint( p );
3174 }
3175 else
3176 {
3177 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
3178 screen->Append( new SCH_MARKER( std::move( ercItem ), wireStart ) );
3179 }
3180 }
3181
3182 break;
3183 }
3184 else if( testBusHit( wireEnd ) )
3185 {
3186 // Wire end is on the bus
3187
3188 if( wireStart.y < busStart.y )
3189 {
3190 /* the start of the wire is above the bus
3191 * |
3192 * |
3193 * |
3194 * =======
3195 */
3196 VECTOR2I p = wireEnd + entrySize( 0, -1 );
3197
3198 if( testBusHit( wireEnd + entrySize( -1, 0 ) ) )
3199 {
3200 /* there is room to the left of the wire for the bus entry
3201 * |
3202 * |
3203 * /
3204 * =======
3205 */
3206 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 3 );
3207 busEntry->SetFlags( IS_NEW );
3208 screen->Append( busEntry );
3209 moveLabels( wire, p );
3210 wire->SetEndPoint( p );
3211 }
3212 else if( testBusHit( wireEnd + entrySize( 1, 0 ) ) )
3213 {
3214 /* there is room to the right of the wire for the bus entry
3215 * |
3216 * |
3217 * \
3218 * =======
3219 */
3220 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 2 );
3221 busEntry->SetFlags( IS_NEW );
3222 screen->Append( busEntry );
3223 moveLabels( wire, p );
3224 wire->SetEndPoint( p );
3225 }
3226 else
3227 {
3228 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
3229 screen->Append( new SCH_MARKER( std::move( ercItem ), wireEnd ) );
3230 }
3231 }
3232 else
3233 {
3234 /* wire start is below the bus
3235 * =======
3236 * |
3237 * |
3238 * |
3239 */
3240 VECTOR2I p = wireEnd + entrySize( 0, 1 );
3241
3242 if( testBusHit( wireEnd + entrySize( -1, 0 ) ) )
3243 {
3244 /* there is room to the left of the wire for the bus entry
3245 * =======
3246 * \
3247 * |
3248 * |
3249 */
3250 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 4 );
3251 busEntry->SetFlags( IS_NEW );
3252 screen->Append( busEntry );
3253 moveLabels( wire, p );
3254 wire->SetEndPoint( p );
3255 }
3256 else if( testBusHit( wireEnd + entrySize( 1, 0 ) ) )
3257 {
3258 /* there is room to the right of the wire for the bus entry
3259 * =======
3260 * /
3261 * |
3262 * |
3263 */
3264 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 1 );
3265 busEntry->SetFlags( IS_NEW );
3266 screen->Append( busEntry );
3267 moveLabels( wire, p );
3268 wire->SetEndPoint( p );
3269 }
3270 else
3271 {
3272 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
3273 screen->Append( new SCH_MARKER( std::move( ercItem ), wireEnd ) );
3274 }
3275 }
3276
3277 break;
3278 }
3279 }
3280 else
3281 {
3282 // Wire isn't horizontal or vertical
3283
3284 if( testBusHit( wireStart ) )
3285 {
3286 VECTOR2I wirevector = wireStart - wireEnd;
3287
3288 if( wirevector.x > 0 )
3289 {
3290 if( wirevector.y > 0 )
3291 {
3292 VECTOR2I p = wireStart + entrySize( -1, -1 );
3293 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 2 );
3294 busEntry->SetFlags( IS_NEW );
3295 screen->Append( busEntry );
3296
3297 moveLabels( wire, p );
3298 wire->SetStartPoint( p );
3299 }
3300 else
3301 {
3302 VECTOR2I p = wireStart + entrySize( -1, 1 );
3303 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 1 );
3304 busEntry->SetFlags( IS_NEW );
3305 screen->Append( busEntry );
3306
3307 moveLabels( wire, p );
3308 wire->SetStartPoint( p );
3309 }
3310 }
3311 else
3312 {
3313 if( wirevector.y > 0 )
3314 {
3315 VECTOR2I p = wireStart + entrySize( 1, -1 );
3316 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 3 );
3317 busEntry->SetFlags( IS_NEW );
3318 screen->Append( busEntry );
3319
3320 moveLabels( wire, p );
3321 wire->SetStartPoint( p );
3322 }
3323 else
3324 {
3325 VECTOR2I p = wireStart + entrySize( 1, 1 );
3326 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 4 );
3327 busEntry->SetFlags( IS_NEW );
3328 screen->Append( busEntry );
3329
3330 moveLabels( wire, p );
3331 wire->SetStartPoint( p );
3332 }
3333 }
3334
3335 break;
3336 }
3337 else if( testBusHit( wireEnd ) )
3338 {
3339 VECTOR2I wirevector = wireStart - wireEnd;
3340
3341 if( wirevector.x > 0 )
3342 {
3343 if( wirevector.y > 0 )
3344 {
3345 VECTOR2I p = wireEnd + entrySize( 1, 1 );
3346 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 4 );
3347 busEntry->SetFlags( IS_NEW );
3348 screen->Append( busEntry );
3349
3350 moveLabels( wire, p );
3351 wire->SetEndPoint( p );
3352 }
3353 else
3354 {
3355 VECTOR2I p = wireEnd + entrySize( 1, -1 );
3356 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 3 );
3357 busEntry->SetFlags( IS_NEW );
3358 screen->Append( busEntry );
3359
3360 moveLabels( wire, p );
3361 wire->SetEndPoint( p );
3362 }
3363 }
3364 else
3365 {
3366 if( wirevector.y > 0 )
3367 {
3368 VECTOR2I p = wireEnd + entrySize( -1, 1 );
3369 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 1 );
3370 busEntry->SetFlags( IS_NEW );
3371 screen->Append( busEntry );
3372
3373 moveLabels( wire, p );
3374 wire->SetEndPoint( p );
3375 }
3376 else
3377 {
3378 VECTOR2I p = wireEnd + entrySize( -1, -1 );
3379 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 2 );
3380 busEntry->SetFlags( IS_NEW );
3381 screen->Append( busEntry );
3382
3383 moveLabels( wire, p );
3384 wire->SetEndPoint( p );
3385 }
3386 }
3387
3388 break;
3389 }
3390 }
3391 }
3392 }
3393}
3394
3395
3397{
3398 wxCHECK( aLabel, nullptr );
3399
3400 VECTOR2I labelPos( aLabel->GetPosition() );
3401
3402 for( const SEG& seg : segs )
3403 {
3404 if( seg.Contains( labelPos ) )
3405 return &seg;
3406 }
3407
3408 return nullptr;
3409}
3410
3411
3412// TODO could be used to place junctions, instead of IsJunctionNeeded()
3413// (see SCH_EDIT_FRAME::importFile())
3414bool SCH_IO_EAGLE::checkConnections( const SCH_SYMBOL* aSymbol, const SCH_PIN* aPin ) const
3415{
3416 wxCHECK( aSymbol && aPin, false );
3417
3418 VECTOR2I pinPosition = aSymbol->GetPinPhysicalPosition( aPin );
3419 auto pointIt = m_connPoints.find( pinPosition );
3420
3421 if( pointIt == m_connPoints.end() )
3422 return false;
3423
3424 const auto& items = pointIt->second;
3425
3426 wxCHECK( items.find( aPin ) != items.end(), false );
3427
3428 return items.size() > 1;
3429}
3430
3431
3433 bool aUpdateSet )
3434{
3435 wxCHECK( aSymbol && aScreen && aSymbol->GetLibSymbolRef(), /*void*/ );
3436
3437 // Normally power parts also have power input pins,
3438 // but they already force net names on the attached wires
3439 if( aSymbol->GetLibSymbolRef()->IsGlobalPower() )
3440 return;
3441
3442 int unit = aSymbol->GetUnit();
3443 const wxString reference = aSymbol->GetField( FIELD_T::REFERENCE )->GetText();
3444 std::vector<SCH_PIN*> pins = aSymbol->GetLibSymbolRef()->GetGraphicalPins( 0, 0 );
3445 std::set<int> missingUnits;
3446
3447 // Search all units for pins creating implicit connections
3448 for( const SCH_PIN* pin : pins )
3449 {
3450 if( pin->GetType() == ELECTRICAL_PINTYPE::PT_POWER_IN )
3451 {
3452 bool pinInUnit = !unit || pin->GetUnit() == unit; // pin belongs to the tested unit
3453
3454 // Create a global net label only if there are no other wires/pins attached
3455 if( pinInUnit )
3456 {
3457 if( !checkConnections( aSymbol, pin ) )
3458 {
3459 // Create a net label to force the net name on the pin
3460 SCH_GLOBALLABEL* netLabel = new SCH_GLOBALLABEL;
3461 netLabel->SetPosition( aSymbol->GetPinPhysicalPosition( pin ) );
3462 netLabel->SetText( extractNetName( pin->GetName() ) );
3463 netLabel->SetTextSize( VECTOR2I( schIUScale.MilsToIU( 40 ),
3464 schIUScale.MilsToIU( 40 ) ) );
3465
3466 switch( pin->GetOrientation() )
3467 {
3468 default:
3470 netLabel->SetSpinStyle( SPIN_STYLE::LEFT );
3471 break;
3473 netLabel->SetSpinStyle( SPIN_STYLE::RIGHT );
3474 break;
3476 netLabel->SetSpinStyle( SPIN_STYLE::UP );
3477 break;
3479 netLabel->SetSpinStyle( SPIN_STYLE::BOTTOM );
3480 break;
3481 }
3482
3483 aScreen->Append( netLabel );
3484 }
3485 }
3486 else if( aUpdateSet )
3487 {
3488 // Found a pin creating implicit connection information in another unit.
3489 // Such units will be instantiated if they do not appear in another sheet and
3490 // processed later.
3491 wxASSERT( pin->GetUnit() );
3492 missingUnits.insert( pin->GetUnit() );
3493 }
3494 }
3495 }
3496
3497 if( aUpdateSet && aSymbol->GetLibSymbolRef()->GetUnitCount() > 1 )
3498 {
3499 auto cmpIt = m_missingCmps.find( reference );
3500
3501 // The first unit found has always already been processed.
3502 if( cmpIt == m_missingCmps.end() )
3503 {
3504 EAGLE_MISSING_CMP& entry = m_missingCmps[reference];
3505 entry.cmp = aSymbol;
3506 entry.screen = aScreen;
3507 entry.units.emplace( unit, false );
3508 }
3509 else
3510 {
3511 // Set the flag indicating this unit has been processed.
3512 cmpIt->second.units[unit] = false;
3513 }
3514
3515 if( !missingUnits.empty() ) // Save the units that need later processing
3516 {
3517 EAGLE_MISSING_CMP& entry = m_missingCmps[reference];
3518 entry.cmp = aSymbol;
3519 entry.screen = aScreen;
3520
3521 // Add units that haven't already been processed.
3522 for( int i : missingUnits )
3523 {
3524 if( entry.units.find( i ) != entry.units.end() )
3525 entry.units.emplace( i, true );
3526 }
3527 }
3528 }
3529}
3530
3531
3532wxString SCH_IO_EAGLE::translateEagleBusName( const wxString& aEagleName ) const
3533{
3534 if( NET_SETTINGS::ParseBusVector( aEagleName, nullptr, nullptr ) )
3535 return aEagleName;
3536
3537 wxString ret = wxT( "{" );
3538
3539 wxStringTokenizer tokenizer( aEagleName, "," );
3540
3541 while( tokenizer.HasMoreTokens() )
3542 {
3543 wxString member = tokenizer.GetNextToken();
3544
3545 // In Eagle, overbar text is automatically stopped at the end of the net name, even when
3546 // that net name is part of a bus definition. In KiCad, we don't (currently) do that, so
3547 // if there is an odd number of overbar markers in this net name, we need to append one
3548 // to close it out before appending the space.
3549
3550 if( member.Freq( '!' ) % 2 > 0 )
3551 member << wxT( "!" );
3552
3553 ret << member << wxS( " " );
3554 }
3555
3556 ret.Trim( true );
3557 ret << wxT( "}" );
3558
3559 return ret;
3560}
3561
3562
3563const ESYMBOL* SCH_IO_EAGLE::getEagleSymbol( const std::unique_ptr<EINSTANCE>& aInstance )
3564{
3565 wxCHECK( m_eagleDoc && m_eagleDoc->drawing && m_eagleDoc->drawing->schematic && aInstance,
3566 nullptr );
3567
3568 std::unique_ptr<EPART>& epart = m_eagleDoc->drawing->schematic->parts[aInstance->part];
3569
3570 if( !epart || epart->deviceset.IsEmpty() )
3571 return nullptr;
3572
3573 std::unique_ptr<ELIBRARY>& elibrary = m_eagleDoc->drawing->schematic->libraries[epart->library];
3574
3575 if( !elibrary )
3576 return nullptr;
3577
3578 std::unique_ptr<EDEVICE_SET>& edeviceset = elibrary->devicesets[epart->deviceset];
3579
3580 if( !edeviceset )
3581 return nullptr;
3582
3583 std::unique_ptr<EGATE>& egate = edeviceset->gates[aInstance->gate];
3584
3585 if( !egate )
3586 return nullptr;
3587
3588 std::unique_ptr<ESYMBOL>& esymbol = elibrary->symbols[egate->symbol];
3589
3590 if( esymbol )
3591 return esymbol.get();
3592
3593 return nullptr;
3594}
3595
3596
3597void SCH_IO_EAGLE::getEagleSymbolFieldAttributes( const std::unique_ptr<EINSTANCE>& aInstance,
3598 const wxString& aEagleFieldName,
3599 SCH_FIELD* aField )
3600{
3601 wxCHECK( aField && !aEagleFieldName.IsEmpty(), /* void */ );
3602
3603 const ESYMBOL* esymbol = getEagleSymbol( aInstance );
3604
3605 if( esymbol )
3606 {
3607 for( const std::unique_ptr<ETEXT>& text : esymbol->texts )
3608 {
3609 if( text->text == aEagleFieldName )
3610 {
3611 aField->SetVisible( true );
3612 VECTOR2I pos( text->x.ToSchUnits() + aInstance->x.ToSchUnits(),
3613 -text->y.ToSchUnits() - aInstance->y.ToSchUnits() );
3614
3615 bool mirror = text->rot ? text->rot->mirror : false;
3616
3617 if( aInstance->rot && aInstance->rot->mirror )
3618 mirror = !mirror;
3619
3620 if( mirror )
3621 pos.y = -aInstance->y.ToSchUnits() + text->y.ToSchUnits();
3622
3623 aField->SetPosition( pos );
3624 }
3625 }
3626 }
3627}
const char * name
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:127
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
constexpr size_type GetWidth() const
Definition box2.h:214
constexpr Vec Centre() const
Definition box2.h:97
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:658
constexpr size_type GetHeight() const
Definition box2.h:215
constexpr const SizeVec & GetSize() const
Definition box2.h:206
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:149
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:93
void SetCenter(const VECTOR2I &aCenter)
SHAPE_POLY_SET & GetPolyShape()
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:232
void SetStart(const VECTOR2I &aStart)
Definition eda_shape.h:194
void SetEnd(const VECTOR2I &aEnd)
Definition eda_shape.h:236
void SetArcAngleAndEnd(const EDA_ANGLE &aAngle, bool aCheckNegativeAngle=false)
Set the end point from the angle center and start.
void SetFillMode(FILL_T aFill)
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:93
const EDA_ANGLE & GetTextAngle() const
Definition eda_text.h:172
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition eda_text.cpp:532
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:114
void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:576
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition eda_text.cpp:416
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition eda_text.h:225
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:385
void SetBold(bool aBold)
Set the text to be bold - this will also update the font if needed.
Definition eda_text.cpp:334
bool IsBold() const
Definition eda_text.h:209
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:228
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:269
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition eda_text.cpp:298
void SetItalic(bool aItalic)
Set the text to be italic - this will also update the font if needed.
Definition eda_text.cpp:306
VECTOR2I GetTextSize() const
Definition eda_text.h:286
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition eda_text.cpp:408
EE_TYPE Overlapping(const BOX2I &aRect) const
Definition sch_rtree.h:230
ee_rtree::Iterator begin() const
Return a read/write iterator that points to the first.
Definition sch_rtree.h:265
ee_rtree::Iterator end() const
Return a read/write iterator that points to one past the last element in the EE_RTREE.
Definition sch_rtree.h:273
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:225
static std::shared_ptr< ERC_ITEM > Create(int aErrorCode)
Constructs an ERC_ITEM for the given error code.
Definition erc_item.cpp:317
RAII class to set and restore the fontconfig reporter.
Definition reporter.h:336
REPORTER * m_reporter
Reporter to log errors/warnings to, may be nullptr.
Definition io_base.h:237
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:240
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition io_base.cpp:71
virtual void Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Definition io_base.cpp:124
Definition kiid.h:48
std::optional< LIBRARY_TABLE * > ProjectTable() const
Retrieves the project library table for this adapter type, or nullopt if one doesn't exist.
void SetNickname(const wxString &aNickname)
void SetType(const wxString &aType)
void SetURI(const wxString &aUri)
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition lib_id.cpp:192
Define a library symbol object.
Definition lib_symbol.h:83
std::vector< const SCH_PIN * > GetGraphicalPins(int aUnit=0, int aBodyStyle=0) const
Graphical pins: Return schematic pin objects as drawn (unexpanded), filtered by unit/body.
void GetFields(std::vector< SCH_FIELD * > &aList, bool aVisibleOnly=false) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
bool IsGlobalPower() const override
int GetUnitCount() const override
static LOAD_INFO_REPORTER & GetInstance()
Definition reporter.cpp:249
static bool ParseBusVector(const wxString &aBus, wxString *aName, std::vector< wxString > *aMemberList)
Parse a bus vector (e.g.
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:79
void SetHeightMils(double aHeightInMils)
const VECTOR2D GetSizeIU(double aIUScale) const
Gets the page size in internal units.
Definition page_info.h:177
void SetWidthMils(double aWidthInMils)
static SYMBOL_LIBRARY_ADAPTER * SymbolLibAdapter(PROJECT *aProject)
Accessor for project symbol library manager adapter.
Holds all the data relating to one schematic.
Definition schematic.h:89
PROJECT & Project() const
Return a reference to the project this schematic is part of.
Definition schematic.h:104
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition schematic.h:173
SCH_SHEET & Root() const
Definition schematic.h:133
Class for a wire to bus entry.
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:126
void ImportValues(const SCH_FIELD &aSource)
Copy parameters from a SCH_FIELD source.
bool IsEmpty()
Return true if both the name and value of the field are empty.
Definition sch_field.h:169
void SetPosition(const VECTOR2I &aPosition) override
void SetName(const wxString &aName)
void SetText(const wxString &aText) override
void SetSpinStyle(SPIN_STYLE aSpinStyle) override
std::unique_ptr< EAGLE_DOC > m_eagleDoc
SCH_ITEM * loadWire(const std::unique_ptr< EWIRE > &aWire, SEG &endpoints)
void loadTextAttributes(EDA_TEXT *aText, const std::unique_ptr< ETEXT > &aAttributes) const
void loadModuleInstance(const std::unique_ptr< EMODULEINST > &aModuleInstance)
SCH_TEXT * loadLabel(const std::unique_ptr< ELABEL > &aLabel, const wxString &aNetName)
void ensureLoadedLibrary(const wxString &aLibraryPath)
void loadSchematic(const ESCHEMATIC &aSchematic)
SCH_TEXT * loadPlainText(const std::unique_ptr< ETEXT > &aSchText)
void loadSheet(const std::unique_ptr< ESHEET > &aSheet)
void loadLayerDefs(const std::vector< std::unique_ptr< ELAYER > > &aLayers)
const ESYMBOL * getEagleSymbol(const std::unique_ptr< EINSTANCE > &aInstance)
EAGLE_LIBRARY * loadLibrary(const ELIBRARY *aLibrary, EAGLE_LIBRARY *aEagleLib)
wxXmlDocument loadXmlDocument(const wxString &aFileName)
wxString translateEagleBusName(const wxString &aEagleName) const
Translate an Eagle-style bus name into one that is KiCad-compatible.
void loadFieldAttributes(SCH_FIELD *aField, const SCH_TEXT *aText) const
Move net labels that are detached from any wire to the nearest wire.
std::map< wxString, wxString > m_powerPorts
map from symbol reference to global label equivalent
SCH_SHEET_PATH m_sheetPath
The current sheet path of the schematic being loaded.
wxString m_libName
Library name to save symbols.
SCH_TEXT * loadSymbolText(std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< ETEXT > &aText, int aGateNumber)
std::pair< VECTOR2I, const SEG * > findNearestLinePoint(const VECTOR2I &aPoint, const std::vector< SEG > &aLines) const
std::map< wxString, long long > m_timestamps
void loadInstance(const std::unique_ptr< EINSTANCE > &aInstance, const std::map< wxString, std::unique_ptr< EPART > > &aParts)
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const std::map< std::string, UTF8 > *aProperties) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
std::unordered_map< wxString, bool > m_userValue
deviceset/@uservalue for device.
int GetModifyHash() const override
Return the modification hash from the library cache.
std::map< wxString, int > m_netCounts
wxFileName m_filename
std::map< wxString, EAGLE_LIBRARY > m_eagleLibs
bool loadSymbol(const std::unique_ptr< ESYMBOL > &aEsymbol, std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< EDEVICE > &aDevice, int aGateNumber, const wxString &aGateName)
SCH_SHEET * getCurrentSheet()
void loadDrawing(const std::unique_ptr< EDRAWING > &aDrawing)
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
SCH_SHAPE * loadSymbolPolyLine(std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< EPOLYGON > &aPolygon, int aGateNumber)
std::vector< VECTOR2I > m_wireIntersections
Wires and labels of a single connection (segment in Eagle nomenclature)
std::map< VECTOR2I, std::set< const EDA_ITEM * > > m_connPoints
The fully parsed Eagle schematic file.
void loadSegments(const std::vector< std::unique_ptr< ESEGMENT > > &aSegments, const wxString &aNetName, const wxString &aNetClass)
bool checkConnections(const SCH_SYMBOL *aSymbol, const SCH_PIN *aPin) const
IO_RELEASER< SCH_IO > m_pi
PI to create KiCad symbol library.
SCH_SHAPE * loadRectangle(const std::unique_ptr< ERECT > &aRect)
void addBusEntries()
This function finds best way to place a bus entry symbol for when an Eagle wire segment ends on an Ea...
bool CanReadSchematicFile(const wxString &aFileName) const override
Checks if this SCH_IO can read the specified schematic file.
void addImplicitConnections(SCH_SYMBOL *aSymbol, SCH_SCREEN *aScreen, bool aUpdateSet)
Create net labels to emulate implicit connections in Eagle.
std::map< int, SCH_LAYER_ID > m_layerMap
SCH_LAYER_ID kiCadLayer(int aEagleLayer)
Return the matching layer or return LAYER_NOTES.
wxString getLibName()
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,...
SCH_PIN * loadPin(std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< EPIN > &aPin, int aGateNumber)
SCH_SHAPE * loadCircle(const std::unique_ptr< ECIRCLE > &aCircle)
wxFileName getLibFileName()
Checks if there are other wires or pins at the position of the tested pin.
SCH_SHAPE * loadSymbolRectangle(std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< ERECT > &aRectangle, int aGateNumber)
SCH_JUNCTION * loadJunction(const std::unique_ptr< EJUNCTION > &aJunction)
wxString m_version
Eagle file version.
void getEagleSymbolFieldAttributes(const std::unique_ptr< EINSTANCE > &aInstance, const wxString &aEagleFieldName, SCH_FIELD *aField)
std::map< wxString, const EPART * > m_partlist
void moveLabels(SCH_LINE *aWire, const VECTOR2I &aNewEndPoint)
Move any labels on the wire to the new end point of the wire.
bool checkHeader(const wxString &aFileName) const
SCHEMATIC * m_schematic
Passed to Load(), the schematic object being loaded.
void countNets(const ESCHEMATIC &aSchematic)
SCH_SHEET * m_rootSheet
The root sheet of the schematic being loaded.
SCH_SHAPE * loadPolyLine(const std::unique_ptr< EPOLYGON > &aPolygon)
SCH_SHAPE * loadSymbolCircle(std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< ECIRCLE > &aCircle, int aGateNumber)
SCH_SCREEN * getCurrentScreen()
bool CanReadLibrary(const wxString &aFileName) const override
Checks if this IO object can read the specified library file/directory.
std::map< wxString, EAGLE_MISSING_CMP > m_missingCmps
void loadFrame(const std::unique_ptr< EFRAME > &aFrame, std::vector< SCH_ITEM * > &aItems)
const int ARC_ACCURACY
std::vector< SEG_DESC > m_segments
Nets as defined in the <nets> sections of an Eagle schematic file.
std::vector< EMODULE * > m_modules
The current module stack being loaded.
std::vector< EMODULEINST * > m_moduleInstances
SCH_ITEM * loadSymbolWire(std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< EWIRE > &aWire, int aGateNumber)
long long getLibraryTimestamp(const wxString &aLibraryPath) const
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
virtual bool CanReadSchematicFile(const wxString &aFileName) const
Checks if this SCH_IO can read the specified schematic file.
Definition sch_io.cpp:45
SCH_IO(const wxString &aName)
Definition sch_io.h:375
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:168
SCH_ITEM * Duplicate(bool addToParentGroup, SCH_COMMIT *aCommit=nullptr, bool doClone=false) const
Routine to create a new copy of given item.
Definition sch_item.cpp:164
int GetUnit() const
Definition sch_item.h:239
virtual void SetUnit(int aUnit)
Definition sch_item.h:238
void SetShape(LABEL_FLAG_SHAPE aShape)
Definition sch_label.h:183
void SetPosition(const VECTOR2I &aPosition) override
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:42
bool IsWire() const
Return true if the line is a wire.
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition sch_line.cpp:274
VECTOR2I GetEndPoint() const
Definition sch_line.h:148
VECTOR2I GetStartPoint() const
Definition sch_line.h:139
bool IsBus() const
Return true if the line is a bus.
void SetEndPoint(const VECTOR2I &aPosition)
Definition sch_line.h:149
void SetNumber(const wxString &aNumber)
Definition sch_pin.cpp:725
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:750
SCH_SCREEN * GetNext()
void UpdateSymbolLinks(REPORTER *aReporter=nullptr)
Initialize the LIB_SYMBOL reference for each SCH_SYMBOL found in the full schematic.
SCH_SCREEN * GetFirst()
const PAGE_INFO & GetPageSettings() const
Definition sch_screen.h:141
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition sch_screen.h:142
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:119
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
void Update(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Update aItem's bounding box in the tree.
void SetPosition(const VECTOR2I &aPos) override
Definition sch_shape.h:89
void SetStroke(const STROKE_PARAMS &aStroke) override
VECTOR2I GetCenter() const
Definition sch_shape.h:91
void AddPoint(const VECTOR2I &aPosition)
VECTOR2I GetPosition() const override
Definition sch_shape.h:88
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Define a sheet pin (label) used in sheets to create hierarchical schematics.
void SetPosition(const VECTOR2I &aPosition) override
void SetSide(SHEET_SIDE aEdge)
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:380
bool LocatePathOfScreen(SCH_SCREEN *aScreen, SCH_SHEET_PATH *aList)
Search the existing hierarchy for an instance of screen loaded from aFileName.
void SetName(const wxString &aName)
Definition sch_sheet.h:141
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:143
void AutoplaceFields(SCH_SCREEN *aScreen, AUTOPLACE_ALGO aAlgo) override
Schematic symbol object.
Definition sch_symbol.h:76
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition sch_symbol.h:135
void AddHierarchicalReference(const KIID_PATH &aPath, const wxString &aRef, int aUnit)
Add a full hierarchical reference to this symbol.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:184
VECTOR2I GetPinPhysicalPosition(const SCH_PIN *Pin) const
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
VECTOR2I GetPosition() const override
Definition sch_text.h:150
void SetPosition(const VECTOR2I &aPosition) override
Definition sch_text.h:151
Definition seg.h:42
VECTOR2I A
Definition seg.h:49
VECTOR2I B
Definition seg.h:50
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
Definition seg.cpp:446
bool Contains(const SEG &aSeg) const
Definition seg.h:324
SHAPE_ARC & ConstructFromStartEndAngle(const VECTOR2I &aStart, const VECTOR2I &aEnd, const EDA_ANGLE &aAngle, double aWidth=0)
Construct this arc from the given start, end and angle.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
Simple container to manage line stroke parameters.
An interface to the global shared library manager that is schematic-specific and linked to one projec...
std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib) override
Loads or reloads the given library, if it exists.
wxString wx_str() const
Definition utf8.cpp:45
double Distance(const VECTOR2< extended_type > &aVector) const
Compute the distance between two vectors.
Definition vector2d.h:553
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition vector2d.h:385
static REPORTER & GetInstance()
Definition reporter.cpp:222
#define DEFAULT_SCH_ENTRY_SIZE
The default text size in mils. (can be changed in preference menu)
wxString escapeName(const wxString &aNetName)
Translates Eagle special characters to their counterparts in KiCad.
wxString interpretText(const wxString &aText)
Interprets special characters in Eagle text and converts them to KiCAD notation.
size_t GetNodeCount(const wxXmlNode *aNode)
Fetch the number of XML nodes within aNode.
VECTOR2I ConvertArcCenter(const VECTOR2I &aStart, const VECTOR2I &aEnd, double aAngle)
Convert an Eagle curve end to a KiCad center for S_ARC.
wxString convertDescription(wxString aDescr)
Converts Eagle's HTML description into KiCad description format.
OPTIONAL_XML_ATTRIBUTE< double > opt_double
#define _(s)
@ DEGREES_T
Definition eda_angle.h:31
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition eda_angle.h:408
#define IGNORE_PARENT_GROUP
Definition eda_item.h:57
#define IS_NEW
New item, just created.
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:49
@ FILLED_SHAPE
Fill with object color.
Definition eda_shape.h:61
@ ERCE_BUS_ENTRY_NEEDED
Importer failed to auto-place a bus entry.
static const std::string KiCadSchematicFileExtension
static const std::string KiCadSymbolLibFileExtension
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
KIID niluuid(0)
SCH_LAYER_ID
Eeschema drawing layers.
Definition layer_ids.h:451
@ LAYER_DEVICE
Definition layer_ids.h:468
@ LAYER_WIRE
Definition layer_ids.h:454
@ LAYER_NOTES
Definition layer_ids.h:469
@ LAYER_BUS
Definition layer_ids.h:455
STL namespace.
@ PT_INPUT
usual pin input: must be connected
Definition pin_type.h:37
@ PT_NC
not connected (must be left open)
Definition pin_type.h:50
@ PT_OUTPUT
usual output
Definition pin_type.h:38
@ PT_TRISTATE
tri state bus pin
Definition pin_type.h:40
@ PT_BIDI
input or output (like port for a microprocessor)
Definition pin_type.h:39
@ PT_OPENCOLLECTOR
pin type open collector
Definition pin_type.h:48
@ PT_POWER_IN
power input (GND, VCC for ICs). Must be connected to a power output.
Definition pin_type.h:46
@ PT_PASSIVE
pin for passive symbols: must be connected, and can be connected to any pin.
Definition pin_type.h:43
@ PIN_UP
The pin extends upwards from the connection point: Probably on the bottom side of the symbol.
Definition pin_type.h:127
@ PIN_RIGHT
The pin extends rightwards from the connection point.
Definition pin_type.h:111
@ PIN_LEFT
The pin extends leftwards from the connection point: Probably on the right side of the symbol.
Definition pin_type.h:118
@ PIN_DOWN
The pin extends downwards from the connection: Probably on the top side of the symbol.
Definition pin_type.h:135
@ RPT_SEVERITY_ERROR
static wxString extractNetName(const wxString &aPinName)
static const std::map< wxString, ELECTRICAL_PINTYPE > pinDirectionsMap
Map of EAGLE pin type values to KiCad pin type values.
static SYMBOL_ORIENTATION_T kiCadComponentRotation(float eagleDegrees)
static void eagleToKicadAlignment(EDA_TEXT *aText, int aEagleAlignment, int aRelDegress, bool aMirror, bool aSpin, int aAbsDegress)
static BOX2I getSheetBbox(SCH_SHEET *aSheet)
Extract the net name part from a pin name (e.g. return 'GND' for pin named 'GND@2')
@ AUTOPLACE_AUTO
Definition sch_item.h:71
LABEL_FLAG_SHAPE
Definition sch_label.h:101
@ L_BIDI
Definition sch_label.h:104
@ L_TRISTATE
Definition sch_label.h:105
@ L_UNSPECIFIED
Definition sch_label.h:106
@ L_OUTPUT
Definition sch_label.h:103
@ L_INPUT
Definition sch_label.h:102
LABEL_SHAPE
Definition sch_label.h:119
@ LABEL_BIDI
Definition sch_label.h:122
@ LABEL_INPUT
Definition sch_label.h:120
@ LABEL_OUTPUT
Definition sch_label.h:121
@ LABEL_PASSIVE
Definition sch_label.h:124
@ LABEL_TRISTATE
Definition sch_label.h:123
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
SHEET_SIDE
Define the edge of the sheet that the sheet pin is positioned.
std::optional< VECTOR2I > OPT_VECTOR2I
Definition seg.h:39
wxString UnescapeString(const wxString &aSource)
bool ReplaceIllegalFileNameChars(std::string &aName, int aReplaceChar)
Checks aName for illegal file name characters.
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
wxString UnescapeHTML(const wxString &aString)
Return a new wxString unescaped from HTML format.
@ CTX_LIBID
@ CTX_NETNAME
std::map< wxString, std::unique_ptr< LIB_SYMBOL > > KiCadSymbols
std::unordered_map< wxString, wxString > package
wxString name
std::unordered_map< wxString, int > GateToUnitMap
Map Eagle gate unit number (which are strings) to KiCad library symbol unit number.
std::map< wxString, std::unique_ptr< EDEVICE_SET > > devicesets
wxString GetName() const
Fetch the fully unique library name.
std::map< wxString, std::unique_ptr< ESYMBOL > > symbols
std::map< wxString, std::unique_ptr< EMODULE > > modules
std::vector< std::unique_ptr< ESHEET > > sheets
std::map< wxString, std::unique_ptr< EPART > > parts
std::map< wxString, std::unique_ptr< ELIBRARY > > libraries
std::vector< std::unique_ptr< ETEXT > > texts
@ BOTTOM_CENTER
@ BOTTOM_RIGHT
@ CENTER_RIGHT
@ CENTER_LEFT
@ BOTTOM_LEFT
Map references to missing symbol units data.
const SCH_SYMBOL * cmp
Screen where the parent symbol is located.
std::map< int, bool > units
Segments representing wires for intersection checking.
std::vector< SEG > segs
std::vector< SCH_TEXT * > labels
const SEG * LabelAttached(const SCH_TEXT *aLabel) const
< Test if a particular label is attached to any of the stored segments
A simple container for schematic symbol instance information.
SYMBOL_ORIENTATION_T
enum used in RotationMiroir()
Definition symbol.h:35
@ SYM_ORIENT_270
Definition symbol.h:42
@ SYM_ORIENT_180
Definition symbol.h:41
@ SYM_ORIENT_90
Definition symbol.h:40
@ SYM_ORIENT_0
Definition symbol.h:39
@ USER
The field ID hasn't been set yet; field is invalid.
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
KIBIS_PIN * pin
std::vector< std::string > header
std::vector< std::vector< std::string > > table
VECTOR2I center
int radius
VECTOR2I end
SHAPE_CIRCLE circle(c.m_circle_center, c.m_circle_radius)
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP
bool TestSegmentHit(const VECTOR2I &aRefPoint, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aDist)
Test if aRefPoint is with aDistance on the line defined by aStart and aEnd.
Definition trigo.cpp:175
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:229
@ SCH_LINE_T
Definition typeinfo.h:164
@ SCH_SYMBOL_T
Definition typeinfo.h:173
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687
Definition of file extensions used in Kicad.