KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcad_sch_parser.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 (C) 2007, 2008 Lubo Racko <[email protected]>
5 * Copyright (C) 2012-2013 Alexander Lunev <[email protected]>
6 * Copyright (C) 2017 Eldar Khayrullin <[email protected]>
7 * Copyright (C) 2025 KiCad Developers, see AUTHORS.txt for contributors.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#ifndef PCAD_SCH_PARSER_H
24#define PCAD_SCH_PARSER_H
25
26#include <wx/string.h>
27#include <map>
28#include <vector>
29
30class XNODE;
31class wxXmlDocument;
32
33namespace PCAD_SCH
34{
35
42
43enum class LINE_KIND
44{
48};
49
52struct FONT
53{
54 bool isTrueType = false;
55 double height = 100.0; // mils
56 double strokeWidth = 10.0; // mils
57 bool isBold = false;
58 bool isItalic = false;
59};
60
62{
63 wxString name;
66 bool hasTtfFont = false;
67 bool displayTType = false;
68
69 const FONT& EffectiveFont() const
70 {
71 return ( displayTType && hasTtfFont ) ? ttfFont : strokeFont;
72 }
73};
74
78{
79 wxString text;
80 double x = 0, y = 0; // mils
81 double rotation = 0; // degrees CCW (P-CAD Y-up)
82 bool isFlipped = false;
83 bool isVisible = true;
85 wxString styleRef; // references a TEXT_STYLE by name
86};
87
88struct PIN
89{
90 wxString pinNum; // symbol pin ordinal inside the symbolDef
91 wxString defaultPinDes; // default pad designator when no compDef mapping exists
92 double x = 0, y = 0; // body attachment point, mils
93 double rotation = 0; // 0/90/180/270 CCW; already encodes any flip
94 double pinLength = 300.0; // mils - P-CAD default
95 wxString outsideEdgeStyle; // Dot, Clock, ...
97 bool showPinDes = true; // (pinDisplay (dispPinDes ...)) - designator text
98 bool showPinName = false;// (pinDisplay (dispPinName ...))
101};
102
103struct LINE
104{
105 std::vector<std::pair<double, double>> pts; // mils
106 double width = 10.0;
108};
109
111struct ARC
112{
113 double x = 0, y = 0, radius = 0; // mils
114 double startAngle = 0, sweepAngle = 0; // degrees
115 double width = 10.0;
116};
117
118struct POLY
119{
120 std::vector<std::pair<double, double>> pts; // mils
121};
122
134
136{
138 double x = 0, y = 0; // mils
139 double height = 0; // mils
140 double rotation = 0; // degrees
141 bool isFlipped = false;
142};
143
144struct ATTR
145{
146 wxString name;
147 TEXT_ITEM placement; // carries the attribute value as its text
148};
149
151{
152 wxString name;
153 wxString originalName;
154 std::vector<PIN> pins;
155 std::vector<LINE> lines;
156 std::vector<ARC> arcs;
157 std::vector<POLY> polys;
158 std::vector<TEXT_ITEM> texts;
159 std::vector<IEEE_SYMBOL> ieeeSymbols;
160 std::vector<ATTR> attrs;
161};
162
166{
167 wxString padDes; // node Name attribute - the pad designator
168 wxString pinName;
169 int partNum = 1;
170 wxString symPinNum;
171 wxString pinType; // Passive, Input, Output, Power, Bidirectional, ...
172};
173
175{
176 wxString name;
177 wxString originalName;
178 wxString refDesPrefix;
179 int numParts = 1;
180 bool isPower = false; // (compType Power)
181 wxString description;
182 wxString attachedPattern; // footprint name
183 std::vector<COMP_PIN> compPins;
184 std::vector<wxString> attachedSymbols; // indexed by partNum (1-based; [0] unused)
185};
186
188{
189 wxString refDes;
190 wxString compRef; // references a COMP_DEF name
191 wxString originalName;
192 wxString value; // compValue
193};
194
195struct WIRE
196{
197 std::vector<std::pair<double, double>> pts; // mils; a wire line may be a polyline
198 wxString netName;
199 bool dispName = false;
200 TEXT_ITEM label; // net-name label placement when dispName is set
201};
202
203struct BUS
204{
205 wxString name;
206 std::vector<std::pair<double, double>> pts;
207 bool dispName = false;
209};
210
212{
213 wxString busNameRef;
214 double x = 0, y = 0; // mils - wire-side end of the entry
215 wxString orient; // Left, Right, Up, Down - direction toward the bus
216};
217
218struct PORT
219{
220 double x = 0, y = 0; // mils
221 wxString netNameRef;
222 wxString portType; // e.g. NoOutline_Sgl_Vert, BothAngle_Dbl_Horz
223 double rotation = 0;
224 bool isFlipped = false;
225};
226
228{
229 double x = 0, y = 0; // mils
230 wxString netName;
231};
232
234{
235 wxString symbolRef; // references a SYMBOL_DEF name
236 wxString refDesRef;
237 int partNum = 1;
238 double x = 0, y = 0; // mils
239 double rotation = 0;
240 bool isFlipped = false;
241 std::vector<ATTR> attrs; // per-instance RefDes/Value/... placements
242};
243
247{
248 wxString name;
250};
251
252struct SHEET
253{
254 wxString name;
255 int sheetNum = 1;
256 std::vector<WIRE> wires;
257 std::vector<BUS> buses;
258 std::vector<BUS_ENTRY> busEntries;
259 std::vector<PORT> ports;
260 std::vector<JUNCTION> junctions;
261 std::vector<SYMBOL_INST> symbols;
262 std::vector<TEXT_ITEM> texts;
263 std::vector<LINE> lines;
264 std::vector<ARC> arcs;
265 std::vector<POLY> polys;
266 std::vector<IEEE_SYMBOL> ieeeSymbols;
267 std::vector<FIELD_PLACEMENT> fields;
268};
269
271{
272 std::map<wxString, wxString> fields; // fieldDef name -> value
273};
274
276{
277 double workspaceWidth = 17000.0; // mils
278 double workspaceHeight = 11000.0; // mils
279
280 std::vector<TEXT_STYLE> textStyles;
281 std::map<wxString, const TEXT_STYLE*> textStylesByName;
282
283 std::vector<SYMBOL_DEF> symbolDefs;
284 std::map<wxString, const SYMBOL_DEF*> symbolDefsByName;
285
286 std::vector<COMP_DEF> compDefs;
287 std::map<wxString, const COMP_DEF*> compDefsByName;
288
290 std::map<wxString, wxString> compAliases;
291
292 std::vector<COMP_INST> compInsts;
293 std::map<wxString, const COMP_INST*> compInstsByRef;
294
295 std::vector<SHEET> sheets;
297
298 const TEXT_STYLE* FindTextStyle( const wxString& aName ) const
299 {
300 auto it = textStylesByName.find( aName );
301 return it == textStylesByName.end() ? nullptr : it->second;
302 }
303};
304
305
307{
308public:
309 PCAD_SCH_PARSER() = default;
310
311 void LoadFromFile( const wxString& aFilename, SCHEMATIC& aSchematic );
312
313 static XNODE* FindChild( XNODE* aNode, const wxString& aTag );
314 static wxString NodeText( XNODE* aNode );
315
316private:
317 void parseHeader( XNODE* aNode, SCHEMATIC& aSchematic );
318 void parseLibrary( XNODE* aNode, SCHEMATIC& aSchematic );
319 void parseTextStyleDef( XNODE* aNode, SCHEMATIC& aSchematic );
320 void parseSymbolDef( XNODE* aNode, SYMBOL_DEF& aSymDef );
321 void parseCompDef( XNODE* aNode, COMP_DEF& aCompDef );
322
323 PIN parsePin( XNODE* aNode );
324 LINE parseLine( XNODE* aNode );
325 ARC parseArc( XNODE* aNode );
326 ARC parseTriplePointArc( XNODE* aNode );
327 POLY parsePoly( XNODE* aNode );
328 ATTR parseAttr( XNODE* aNode );
329 TEXT_ITEM parseText( XNODE* aNode );
331
332 void parseNetlist( XNODE* aNode, SCHEMATIC& aSchematic );
333 void parseSchematicDesign( XNODE* aNode, SCHEMATIC& aSchematic );
334 void parseFieldSets( XNODE* aNode, SCHEMATIC& aSchematic );
335 void parseSheet( XNODE* aNode, SHEET& aSheet );
336 WIRE parseWire( XNODE* aNode );
337 BUS parseBus( XNODE* aNode );
338
339 // Measurement-aware value parsing. A bare number uses the file default
340 // (fileUnits); an explicit suffix ("1.5mm", "0.198 mm") always wins.
341 double toMils( const wxString& aValue ) const;
342 std::vector<std::pair<double, double>> collectPts( XNODE* aNode ) const;
343 bool parsePt( XNODE* aNode, double& aX, double& aY ) const;
344 bool parsePtNode( XNODE* aPtNode, double& aX, double& aY ) const;
345 double childDouble( XNODE* aNode, const wxString& aTag, double aDefault = 0.0 ) const;
346 static wxString childStr( XNODE* aNode, const wxString& aTag,
347 const wxString& aDefault = wxEmptyString );
348 static bool childFlag( XNODE* aNode, const wxString& aTag );
349 static JUSTIFY parseJustify( const wxString& aValue );
350
351 bool m_isMetric = false;
352};
353
354} // namespace PCAD_SCH
355
356#endif // PCAD_SCH_PARSER_H
void LoadFromFile(const wxString &aFilename, SCHEMATIC &aSchematic)
void parseNetlist(XNODE *aNode, SCHEMATIC &aSchematic)
static bool childFlag(XNODE *aNode, const wxString &aTag)
void parseSymbolDef(XNODE *aNode, SYMBOL_DEF &aSymDef)
void parseSchematicDesign(XNODE *aNode, SCHEMATIC &aSchematic)
IEEE_SYMBOL parseIeeeSymbol(XNODE *aNode)
TEXT_ITEM parseText(XNODE *aNode)
void parseFieldSets(XNODE *aNode, SCHEMATIC &aSchematic)
std::vector< std::pair< double, double > > collectPts(XNODE *aNode) const
double childDouble(XNODE *aNode, const wxString &aTag, double aDefault=0.0) const
static wxString NodeText(XNODE *aNode)
void parseLibrary(XNODE *aNode, SCHEMATIC &aSchematic)
double toMils(const wxString &aValue) const
void parseCompDef(XNODE *aNode, COMP_DEF &aCompDef)
bool parsePtNode(XNODE *aPtNode, double &aX, double &aY) const
static XNODE * FindChild(XNODE *aNode, const wxString &aTag)
void parseTextStyleDef(XNODE *aNode, SCHEMATIC &aSchematic)
bool parsePt(XNODE *aNode, double &aX, double &aY) const
void parseSheet(XNODE *aNode, SHEET &aSheet)
void parseHeader(XNODE *aNode, SCHEMATIC &aSchematic)
static JUSTIFY parseJustify(const wxString &aValue)
static wxString childStr(XNODE *aNode, const wxString &aTag, const wxString &aDefault=wxEmptyString)
ARC parseTriplePointArc(XNODE *aNode)
An extension of wxXmlNode that can format its contents as KiCad-style s-expressions.
Definition xnode.h:67
Center/radius/angles form; triplePointArc is converted at parse time.
wxString orient
double x
double y
wxString busNameRef
std::vector< std::pair< double, double > > pts
std::vector< wxString > attachedSymbols
std::vector< COMP_PIN > compPins
A (compPin "padDes" ...) inside a compDef, mapping a symbol pin ordinal to the physical pad designato...
A placed title-block field whose name references the fieldDef holding the displayed text.
One font description inside a (textStyleDef ...).
std::vector< std::pair< double, double > > pts
TEXT_ITEM pinNameText
wxString defaultPinDes
wxString insideEdgeStyle
wxString outsideEdgeStyle
TEXT_ITEM pinDesText
std::vector< std::pair< double, double > > pts
std::map< wxString, const COMP_DEF * > compDefsByName
std::map< wxString, const COMP_INST * > compInstsByRef
std::vector< SHEET > sheets
std::vector< SYMBOL_DEF > symbolDefs
std::vector< TEXT_STYLE > textStyles
std::vector< COMP_INST > compInsts
std::map< wxString, const TEXT_STYLE * > textStylesByName
const TEXT_STYLE * FindTextStyle(const wxString &aName) const
std::map< wxString, wxString > compAliases
compAlias name -> compDef name
std::vector< COMP_DEF > compDefs
std::map< wxString, const SYMBOL_DEF * > symbolDefsByName
std::vector< BUS > buses
std::vector< PORT > ports
std::vector< BUS_ENTRY > busEntries
std::vector< WIRE > wires
std::vector< JUNCTION > junctions
std::vector< POLY > polys
std::vector< TEXT_ITEM > texts
std::vector< LINE > lines
std::vector< FIELD_PLACEMENT > fields
std::vector< IEEE_SYMBOL > ieeeSymbols
std::vector< SYMBOL_INST > symbols
std::vector< ARC > arcs
std::vector< POLY > polys
std::vector< ATTR > attrs
std::vector< PIN > pins
std::vector< TEXT_ITEM > texts
std::vector< IEEE_SYMBOL > ieeeSymbols
std::vector< LINE > lines
std::vector< ARC > arcs
std::vector< ATTR > attrs
A positioned text.
const FONT & EffectiveFont() const
std::map< wxString, wxString > fields
std::vector< std::pair< double, double > > pts