KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_geda.h
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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * This file contains file format knowledge derived from the gEDA and
20 * Lepton EDA projects:
21 *
22 * gEDA/gaf - Copyright (C) 1998-2010 Ales Hvezda
23 * Copyright (C) 1998-2016 gEDA Contributors
24 * Lepton EDA - Copyright (C) 2017-2024 Lepton EDA Contributors
25 *
26 * Both projects are licensed under the GNU General Public License v2 or later.
27 * See https://github.com/lepton-eda/lepton-eda and
28 * https://github.com/rlutz/geda-gaf
29 */
30
31#ifndef SCH_IO_GEDA_H_
32#define SCH_IO_GEDA_H_
33
34#include <sch_io/sch_io.h>
35#include <sch_io/sch_io_mgr.h>
36
37#include <eda_shape.h>
38#include <stroke_params.h>
39
40#include <wx/filename.h>
41#include <wx/textfile.h>
42
43#include <map>
44#include <set>
45#include <vector>
46#include <memory>
47
48class LIB_SYMBOL;
49class SCH_SHEET;
50class SCH_SCREEN;
51class SCH_SYMBOL;
52class SCH_LINE;
53class SCH_TEXT;
54class SCH_SHAPE;
55class SCH_JUNCTION;
56class SCH_NO_CONNECT;
57class SCH_PIN;
58class SCHEMATIC;
59
60
73class SCH_IO_GEDA : public SCH_IO
74{
75public:
77 ~SCH_IO_GEDA() override;
78
80 {
81 return IO_BASE::IO_FILE_DESC( _HKI( "gEDA / Lepton EDA schematic files" ), { "sch" } );
82 }
83
85 {
86 return IO_BASE::IO_FILE_DESC( wxEmptyString, {} );
87 }
88
89 bool CanReadSchematicFile( const wxString& aFileName ) const override;
90
91 int GetModifyHash() const override { return 0; }
92
93 SCH_SHEET* LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic,
94 SCH_SHEET* aAppendToMe = nullptr,
95 const std::map<std::string, UTF8>* aProperties = nullptr ) override;
96
97 bool IsLibraryWritable( const wxString& aLibraryPath ) override { return false; }
98
100 static const std::map<wxString, wxString>& getBuiltinSymbols();
101
102private:
103 // -----------------------------------------------------------------------
104 // Data types
105 // -----------------------------------------------------------------------
106
109 {
110 wxString name;
111 wxString value;
112 int x = 0;
113 int y = 0;
114 int size = 10;
115 int angle = 0;
116 int align = 0;
117 bool visible = false;
118 int showNV = 0;
119 };
120
125 {
126 wxString basename;
127 int x = 0;
128 int y = 0;
129 int angle = 0;
130 int mirror = 0;
131 int selectable = 1;
132 std::vector<GEDA_ATTR> attrs;
133 bool embedded = false;
134 std::unique_ptr<LIB_SYMBOL> embeddedSym;
135 };
136
139 {
140 wxString path;
141 std::unique_ptr<LIB_SYMBOL> symbol;
142 wxString symversion;
143 wxString netAttr;
144 };
145
146 // -----------------------------------------------------------------------
147 // Coordinate transformation
148 // -----------------------------------------------------------------------
149
152 static constexpr int MILS_TO_IU = 254;
153
155 int toKiCadDist( int aMils ) const;
156
159 VECTOR2I toKiCad( int aGedaX, int aGedaY ) const;
160
161 // -----------------------------------------------------------------------
162 // File parsing
163 // -----------------------------------------------------------------------
164
166 bool parseVersionLine( const wxString& aLine );
167
169 std::vector<GEDA_ATTR> parseAttributes( wxTextFile& aFile, size_t& aLineIdx );
170
172 wxString findAttr( const std::vector<GEDA_ATTR>& aAttrs, const wxString& aName ) const;
173
175 const GEDA_ATTR* findAttrStruct( const std::vector<GEDA_ATTR>& aAttrs,
176 const wxString& aName ) const;
177
179 std::vector<GEDA_ATTR> maybeParseAttributes( wxTextFile& aFile, size_t& aLineIdx );
180
181 // -----------------------------------------------------------------------
182 // Object parsers - schematics
183 // -----------------------------------------------------------------------
184
185 void parseComponent( const wxString& aLine, wxTextFile& aFile, size_t& aLineIdx );
186 void parseNet( const wxString& aLine, wxTextFile& aFile, size_t& aLineIdx );
187 void parseBus( const wxString& aLine, wxTextFile& aFile, size_t& aLineIdx );
188 void parseText( const wxString& aLine, wxTextFile& aFile, size_t& aLineIdx );
189 void parseLine( const wxString& aLine, wxTextFile& aFile, size_t& aLineIdx );
190 void parseBox( const wxString& aLine, wxTextFile& aFile, size_t& aLineIdx );
191 void parseCircle( const wxString& aLine, wxTextFile& aFile, size_t& aLineIdx );
192 void parseArc( const wxString& aLine, wxTextFile& aFile, size_t& aLineIdx );
193 void parsePath( const wxString& aLine, wxTextFile& aFile, size_t& aLineIdx );
194 void parsePicture( const wxString& aLine, wxTextFile& aFile, size_t& aLineIdx );
195 void parsePin( const wxString& aLine, wxTextFile& aFile, size_t& aLineIdx );
196 void parseEmbeddedComponent( wxTextFile& aFile, size_t& aLineIdx );
197
198 // -----------------------------------------------------------------------
199 // Symbol loading
200 // -----------------------------------------------------------------------
201
203 void initSymbolLibrary();
204
206 void scanSymbolDir( const wxString& aDir, int aDepth = 0 );
207
209 void parseRcFileForLibraries( const wxString& aPath, const wxString& aBaseDir );
210
216 std::unique_ptr<LIB_SYMBOL> loadSymbolFile( const wxString& aPath,
217 wxString* aSymversion = nullptr,
218 wxString* aNetAttr = nullptr );
219
221 void parseSymbolObjects( wxTextFile& aFile, size_t& aLineIdx, LIB_SYMBOL& aSymbol,
222 size_t aEndLine );
223
225 void addSymbolPin( LIB_SYMBOL& aSymbol, int aX1, int aY1, int aX2, int aY2,
226 int aWhichEnd, const std::vector<GEDA_ATTR>& aAttrs );
227
229 void addSymbolGraphic( LIB_SYMBOL& aSymbol, const wxString& aLine, wxTextFile& aFile,
230 size_t& aLineIdx, wxChar aType );
231
235
238 void importHierarchicalSheet( const wxString& aSourceFile );
239
241 LIB_SYMBOL* getOrLoadSymbol( const wxString& aBasename );
242
244 std::unique_ptr<LIB_SYMBOL> loadBuiltinSymbol( const wxString& aBasename );
245
247 std::unique_ptr<LIB_SYMBOL> createFallbackSymbol( const wxString& aBasename );
248
250 int toKiCadOrientation( int aAngle, int aMirror ) const;
251
253 wxString getLibName() const;
254
255 // -----------------------------------------------------------------------
256 // Style mapping
257 // -----------------------------------------------------------------------
258
260 static LINE_STYLE toLineStyle( int aDashStyle );
261
263 static FILL_T toFillType( int aFillType );
264
265 // -----------------------------------------------------------------------
266 // Post-processing
267 // -----------------------------------------------------------------------
268
270 void trackEndpoint( int aGedaX, int aGedaY );
271
277 void postProcess();
278
281
283 void addJunctions();
284
286 void addBusEntries();
287
289 void loadDeferredSheets();
290
292 void fitPageToContent();
293
294 // -----------------------------------------------------------------------
295 // State
296 // -----------------------------------------------------------------------
297
301 wxFileName m_filename;
302
305
310
312 std::unique_ptr<PENDING_COMPONENT> m_pendingComp;
313
315 std::map<wxString, SYM_CACHE_ENTRY> m_symLibrary;
317
319 std::map<wxString, std::unique_ptr<LIB_SYMBOL>> m_libSymbols;
320
323
325 std::map<std::pair<int,int>, int> m_netEndpoints;
326
329 {
330 wxString netname;
331 wxString pinnumber;
333 };
334
335 std::vector<NET_ATTR_RECORD> m_netAttrRecords;
336
344
345 std::vector<BUS_SEGMENT> m_busSegments;
346
350 {
352 wxString sourceFile;
353 };
354
355 std::vector<DEFERRED_SHEET> m_deferredSheets;
356
359 std::set<wxString> m_importStack;
360
362 const std::map<std::string, UTF8>* m_properties;
363};
364
365#endif // SCH_IO_GEDA_H_
Define a library symbol object.
Definition lib_symbol.h:83
Holds all the data relating to one schematic.
Definition schematic.h:88
std::unique_ptr< PENDING_COMPONENT > m_pendingComp
Pending component awaiting symbol resolution.
void addBusEntries()
Create SCH_BUS_WIRE_ENTRY objects where net endpoints touch bus segments.
void importHierarchicalSheet(const wxString &aSourceFile)
Import a gEDA hierarchical sub-schematic as a KiCad SCH_SHEET.
int toKiCadDist(int aMils) const
Convert a gEDA distance in mils to KiCad IU.
SCHEMATIC * m_schematic
void addSymbolGraphic(LIB_SYMBOL &aSymbol, const wxString &aLine, wxTextFile &aFile, size_t &aLineIdx, wxChar aType)
Add a graphical item (line/box/circle/arc/path) to a LIB_SYMBOL.
void parseBus(const wxString &aLine, wxTextFile &aFile, size_t &aLineIdx)
void fitPageToContent()
Enlarge the page if needed and center all content on the sheet.
std::vector< GEDA_ATTR > maybeParseAttributes(wxTextFile &aFile, size_t &aLineIdx)
Check for and consume a { } attribute block at the current line position.
wxFileName m_filename
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,...
bool m_symLibraryInitialized
const GEDA_ATTR * findAttrStruct(const std::vector< GEDA_ATTR > &aAttrs, const wxString &aName) const
Find a GEDA_ATTR struct by name, returns nullptr if not found.
void parseEmbeddedComponent(wxTextFile &aFile, size_t &aLineIdx)
void addJunctions()
Place junctions where 3+ net/pin endpoints coincide.
void parseNet(const wxString &aLine, wxTextFile &aFile, size_t &aLineIdx)
void postProcess()
Run the full post-processing pipeline after parsing is complete:
long m_fileFormatVersion
std::map< std::pair< int, int >, int > m_netEndpoints
Net endpoint positions in raw gEDA coordinates for junction detection.
int m_powerCounter
Sequential counter for auto-generated #PWR references.
SCH_SHEET * m_rootSheet
static constexpr int MILS_TO_IU
gEDA coordinates are mils with Y-up.
SCH_SCREEN * m_screen
int m_maxY
The maximum Y coordinate seen during parsing (gEDA coords, before flip).
std::vector< DEFERRED_SHEET > m_deferredSheets
std::unique_ptr< LIB_SYMBOL > loadBuiltinSymbol(const wxString &aBasename)
Try loading a symbol from the built-in standard library embedded in the importer.
LIB_SYMBOL * getOrLoadSymbol(const wxString &aBasename)
Get or load a cached symbol by gEDA basename.
void parseText(const wxString &aLine, wxTextFile &aFile, size_t &aLineIdx)
void parseComponent(const wxString &aLine, wxTextFile &aFile, size_t &aLineIdx)
void parsePin(const wxString &aLine, wxTextFile &aFile, size_t &aLineIdx)
void parseSymbolObjects(wxTextFile &aFile, size_t &aLineIdx, LIB_SYMBOL &aSymbol, size_t aEndLine)
Parse objects from a .sym file or [] block into a LIB_SYMBOL.
int toKiCadOrientation(int aAngle, int aMirror) const
Map gEDA angle (0/90/180/270) + mirror to KiCad symbol orientation.
void parseLine(const wxString &aLine, wxTextFile &aFile, size_t &aLineIdx)
void parseCircle(const wxString &aLine, wxTextFile &aFile, size_t &aLineIdx)
std::map< wxString, std::unique_ptr< LIB_SYMBOL > > m_libSymbols
Loaded symbols for this import session, keyed by basename.
void parsePath(const wxString &aLine, wxTextFile &aFile, size_t &aLineIdx)
const IO_BASE::IO_FILE_DESC GetLibraryDesc() const override
Get the descriptor for the library container that this IO plugin operates on.
Definition sch_io_geda.h:84
void scanSymbolDir(const wxString &aDir, int aDepth=0)
Recursively scan a directory for .sym files and populate m_symLibrary.
std::set< wxString > m_importStack
Set of fully-resolved file paths currently in the import call stack, used to detect and prevent circu...
wxString getLibName() const
Derive the KiCad import library name from the schematic file.
void flushPendingComponent()
Instantiate the pending component: look up or load the symbol, create SCH_SYMBOL, apply transforms an...
std::unique_ptr< LIB_SYMBOL > createFallbackSymbol(const wxString &aBasename)
Create a fallback rectangular symbol when the .sym file is not found.
static const std::map< wxString, wxString > & getBuiltinSymbols()
Return the map of built-in gEDA symbol definitions (symbol name -> .sym content).
VECTOR2I toKiCad(int aGedaX, int aGedaY) const
Apply the Y-flip and scale to transform gEDA coords to KiCad.
static LINE_STYLE toLineStyle(int aDashStyle)
Map gEDA dashstyle (0-4) to KiCad LINE_STYLE.
void parseBox(const wxString &aLine, wxTextFile &aFile, size_t &aLineIdx)
std::vector< NET_ATTR_RECORD > m_netAttrRecords
wxString findAttr(const std::vector< GEDA_ATTR > &aAttrs, const wxString &aName) const
Find an attribute by name, returns empty string if not found.
std::vector< BUS_SEGMENT > m_busSegments
const IO_BASE::IO_FILE_DESC GetSchematicFileDesc() const override
Returns schematic file description for the SCH_IO.
Definition sch_io_geda.h:79
void parseArc(const wxString &aLine, wxTextFile &aFile, size_t &aLineIdx)
bool CanReadSchematicFile(const wxString &aFileName) const override
Checks if this SCH_IO can read the specified schematic file.
int GetModifyHash() const override
Return the modification hash from the library cache.
Definition sch_io_geda.h:91
std::unique_ptr< LIB_SYMBOL > loadSymbolFile(const wxString &aPath, wxString *aSymversion=nullptr, wxString *aNetAttr=nullptr)
Load a .sym file and return a LIB_SYMBOL.
void processNetAttributes()
For each component with net= attributes, create global labels at pin positions.
void initSymbolLibrary()
Ensure the symbol library hash is built by scanning gEDA library dirs.
std::map< wxString, SYM_CACHE_ENTRY > m_symLibrary
Symbol library cache: gEDA basename -> cache entry.
long m_releaseVersion
gEDA file version fields from the "v YYYYMMDD N" header line.
~SCH_IO_GEDA() override
void parsePicture(const wxString &aLine, wxTextFile &aFile, size_t &aLineIdx)
bool IsLibraryWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
Definition sch_io_geda.h:97
void parseRcFileForLibraries(const wxString &aPath, const wxString &aBaseDir)
Parse an RC file (gafrc or gschemrc) for component-library directives.
void trackEndpoint(int aGedaX, int aGedaY)
Track a point as a net or pin endpoint for junction detection.
bool parseVersionLine(const wxString &aLine)
Parse the "v YYYYMMDD N" version line and validate.
std::vector< GEDA_ATTR > parseAttributes(wxTextFile &aFile, size_t &aLineIdx)
Read a { } attribute block starting after the '{' line.
const std::map< std::string, UTF8 > * m_properties
Properties passed from the import framework (search paths, etc.)
void addSymbolPin(LIB_SYMBOL &aSymbol, int aX1, int aY1, int aX2, int aY2, int aWhichEnd, const std::vector< GEDA_ATTR > &aAttrs)
Create a pin on a LIB_SYMBOL from gEDA P line data and its attributes.
void loadDeferredSheets()
Load sub-schematics for any SCH_SHEET objects created during parsing.
static FILL_T toFillType(int aFillType)
Map gEDA filltype (0-4) to KiCad FILL_T.
SCH_IO(const wxString &aName)
Definition sch_io.h:375
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:42
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
Schematic symbol object.
Definition sch_symbol.h:76
FILL_T
Definition eda_shape.h:56
#define _HKI(x)
Definition page_info.cpp:44
LINE_STYLE
Dashed line types.
Container that describes file type info.
Definition io_base.h:43
Parsed bus segment in KiCad coordinates with gEDA ripper direction.
int ripperDir
-1, 0, or 1 from gEDA U line
Deferred hierarchical sheet loads.
wxString sourceFile
Resolved full path to the sub-schematic.
Parsed attribute from a gEDA T line inside a { } block.
int showNV
0=name+value, 1=value, 2=name
Components with net= attributes that need post-processing.
Pending component waiting for symbol resolution.
std::vector< GEDA_ATTR > attrs
std::unique_ptr< LIB_SYMBOL > embeddedSym
Entry in the symbol library search cache.
wxString symversion
symversion from the .sym file (if any)
wxString netAttr
net= attribute (e.g. "GND:1") identifying power symbols
std::unique_ptr< LIB_SYMBOL > symbol
Loaded symbol (null if not yet loaded)
wxString path
Full path to .sym file.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695