/* * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2017 CERN * Copyright (C) 2021-2024 KiCad Developers, see AUTHORS.txt for contributors. * * @author Alejandro GarcĂ­a Montoro * @author Maciej Suminski * @author Russell Oliver * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 3 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #ifndef SCH_IO_EAGLE_H_ #define SCH_IO_EAGLE_H_ #include #include #include #include #include class EDA_TEXT; class KIWAY; class LINE_READER; class SCH_SCREEN; class SCH_SHEET; class SCH_BITMAP; class SCH_JUNCTION; class SCH_NO_CONNECT; class SCH_SHAPE; class SCH_LINE; class SCH_BUS_ENTRY_BASE; class SCH_TEXT; class SCH_GLOBALLABEL; class SCH_SYMBOL; class SCH_FIELD; class STRING_UTF8_MAP; class LIB_SYMBOL; class SYMBOL_LIB; class SCH_PIN; class wxXmlNode; struct EAGLE_LIBRARY { wxString name; std::map> KiCadSymbols; /** * Map Eagle gate unit number (which are strings) to KiCad library symbol unit number. * * The look up name is #EDEVICE name + #EDEVICE name + #EGATE name separated by underscores. * Hashing would be faster but it would be nearly impossible to debug so use string look up * for now. */ std::unordered_map GateToUnitMap; std::unordered_map package; }; /** * A #SCH_IO derivation for loading 6.x+ Eagle schematic files. * * As with all #SCH_IO objects there are no UI dependencies i.e. windowing calls allowed. */ class SCH_IO_EAGLE : public SCH_IO { public: const double ARC_ACCURACY = SCH_IU_PER_MM * 0.01; // 0.01mm SCH_IO_EAGLE(); ~SCH_IO_EAGLE(); const IO_BASE::IO_FILE_DESC GetSchematicFileDesc() const override { return IO_BASE::IO_FILE_DESC( _HKI( "Eagle XML schematic files" ), { "sch" } ); } const IO_BASE::IO_FILE_DESC GetLibraryDesc() const override { return IO_BASE::IO_FILE_DESC( _HKI( "Eagle XML library files" ), { "lbr" } ); } bool CanReadSchematicFile( const wxString& aFileName ) const override; bool CanReadLibrary( const wxString& aFileName ) const override; int GetModifyHash() const override; SCH_SHEET* LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic, SCH_SHEET* aAppendToMe = nullptr, const STRING_UTF8_MAP* aProperties = nullptr ) override; void EnumerateSymbolLib( wxArrayString& aSymbolNameList, const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties ) override; void EnumerateSymbolLib( std::vector& aSymbolList, const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties ) override; LIB_SYMBOL* LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName, const STRING_UTF8_MAP* aProperties ) override; bool IsLibraryWritable( const wxString& aLibraryPath ) override { return false; } private: bool checkHeader( const wxString& aFileName ) const; wxXmlDocument loadXmlDocument( const wxString& aFileName ); long long getLibraryTimestamp( const wxString& aLibraryPath ) const; void ensureLoadedLibrary( const wxString& aLibraryPath ); void loadDrawing( const std::unique_ptr& aDrawing ); void loadLayerDefs( const std::vector>& aLayers ); void loadSchematic( const ESCHEMATIC& aSchematic ); void loadSheet( const std::unique_ptr& aSheet ); void loadInstance( const std::unique_ptr& aInstance, const std::map>& aParts ); SCH_SHEET* loadModuleInstance( const std::unique_ptr& aModuleInstance ); EAGLE_LIBRARY* loadLibrary( const ELIBRARY* aLibrary, EAGLE_LIBRARY* aEagleLib ); void countNets( const ESCHEMATIC& aSchematic ); /// Move any labels on the wire to the new end point of the wire. void moveLabels( SCH_LINE* aWire, const VECTOR2I& aNewEndPoint ); /// This function finds best way to place a bus entry symbol for when an Eagle wire segment /// ends on an Eagle bus segment. void addBusEntries(); /// Return the matching layer or return LAYER_NOTES SCH_LAYER_ID kiCadLayer( int aEagleLayer ); std::pair findNearestLinePoint( const VECTOR2I& aPoint, const std::vector& aLines ) const; void loadSegments( const std::vector>& aSegments, const wxString& aNetName, const wxString& aNetClass ); SCH_SHAPE* loadPolyLine( const std::unique_ptr& aPolygon ); SCH_ITEM* loadWire( const std::unique_ptr& aWire, SEG& endpoints ); SCH_SHAPE* loadCircle( const std::unique_ptr& aCircle ); SCH_SHAPE* loadRectangle( const std::unique_ptr& aRect ); SCH_TEXT* loadLabel( const std::unique_ptr& aLabel, const wxString& aNetName ); SCH_JUNCTION* loadJunction( const std::unique_ptr& aJunction ); SCH_TEXT* loadPlainText( const std::unique_ptr& aSchText ); void loadFrame( const std::unique_ptr& aFrame, std::vector& aItems ); bool loadSymbol( const std::unique_ptr& aEsymbol, std::unique_ptr& aSymbol, const std::unique_ptr& aDevice, int aGateNumber, const wxString& aGateName ); SCH_SHAPE* loadSymbolCircle( std::unique_ptr& aSymbol, const std::unique_ptr& aCircle, int aGateNumber ); SCH_SHAPE* loadSymbolRectangle( std::unique_ptr& aSymbol, const std::unique_ptr& aRectangle, int aGateNumber ); SCH_SHAPE* loadSymbolPolyLine( std::unique_ptr& aSymbol, const std::unique_ptr& aPolygon, int aGateNumber ); SCH_ITEM* loadSymbolWire( std::unique_ptr& aSymbol, const std::unique_ptr& aWire, int aGateNumber ); SCH_PIN* loadPin( std::unique_ptr& aSymbol, const std::unique_ptr& aPin, int aGateNumber ); SCH_TEXT* loadSymbolText( std::unique_ptr& aSymbol, const std::unique_ptr& aText, int aGateNumber ); void loadTextAttributes( EDA_TEXT* aText, const std::unique_ptr& aAttributes ) const; void loadFieldAttributes( SCH_FIELD* aField, const SCH_TEXT* aText ) const; ///< Move net labels that are detached from any wire to the nearest wire void adjustNetLabels(); /** * Translate an Eagle-style bus name into one that is KiCad-compatible. * * For vector buses such as A[7..0] this has no impact. For group buses, we translate from * Eagle-style to KiCad-style. * * @param aEagleName is the name of the bus from the Eagle schematic */ wxString translateEagleBusName( const wxString& aEagleName ) const; wxString getLibName(); wxFileName getLibFileName(); ///< Checks if there are other wires or pins at the position of the tested pin bool checkConnections( const SCH_SYMBOL* aSymbol, const SCH_PIN* aPin ) const; /** * Create net labels to emulate implicit connections in Eagle. * * Each named power input pin creates an implicit connection in Eagle. To emulate this behavior * one needs to attach global net labels to the mentioned pins. This is is also expected for the * units that are not instantiated in the schematics, therefore such units need to be stored * in order to create them at later stage. * * @param aSymbol is the symbol to process. * @param aScreen is the screen where net labels should be added. * @param aUpdateSet decides whether the missing units data should be updated. */ void addImplicitConnections( SCH_SYMBOL* aSymbol, SCH_SCREEN* aScreen, bool aUpdateSet ); bool netHasPowerDriver( SCH_LINE* aLine, const wxString& aNetName ) const; void getEagleSymbolFieldAttributes( const std::unique_ptr& aInstance, const wxString& aEagleFieldName, SCH_FIELD* aField ); const ESYMBOL* getEagleSymbol( const std::unique_ptr& aInstance ); SCH_SHEET* getCurrentSheet(); SCH_SCREEN* getCurrentScreen(); // Describe missing units containing pins creating implicit connections // (named power pins in Eagle). struct EAGLE_MISSING_CMP { EAGLE_MISSING_CMP( const SCH_SYMBOL* aSymbol = nullptr ) : cmp( aSymbol ) { } ///< Link to the parent symbol const SCH_SYMBOL* cmp; /* Map of the symbol units: for each unit there is a flag saying * whether the unit needs to be instantiated with appropriate net labels to * emulate implicit connections as is done in Eagle. */ std::map units; }; ///< Map references to missing symbol units data std::map m_missingCmps; SCH_SHEET* m_rootSheet; ///< The root sheet of the schematic being loaded SCH_SHEET_PATH m_sheetPath; ///< The current sheet path of the schematic being loaded. wxString m_version; ///< Eagle file version. wxFileName m_filename; wxString m_libName; ///< Library name to save symbols SCHEMATIC* m_schematic; ///< Passed to Load(), the schematic object being loaded EMODULE* m_module; ///< The current module being loaded or nullptr std::map m_partlist; std::map m_timestamps; std::map m_eagleLibs; std::map> m_eagleModules; std::unordered_map m_userValue; ///< deviceset/@uservalue for device. IO_RELEASER m_pi; ///< PI to create KiCad symbol library. std::unique_ptr m_properties; ///< Library plugin properties. int m_sheetIndex; std::map m_netCounts; std::map m_layerMap; std::map m_powerPorts; ///< map from symbol reference to global ///< label equivalent ///< Wire intersection points, used for quick checks whether placing a net label in a particular ///< place would short two nets. std::vector m_wireIntersections; ///< Wires and labels of a single connection (segment in Eagle nomenclature) struct SEG_DESC { ///< Test if a particular label is attached to any of the stored segments const SEG* LabelAttached( const SCH_TEXT* aLabel ) const; std::vector labels; std::vector segs; }; ///< Segments representing wires for intersection checking std::vector m_segments; ///< Nets as defined in the sections of an Eagle schematic file. std::map m_nets; ///< Positions of pins and wire endings mapped to its parent std::map> m_connPoints; ///< The fully parsed Eagle schematic file. std::unique_ptr m_eagleDoc; }; #endif // SCH_IO_EAGLE_H_