KiCad PCB EDA Suite
Loading...
Searching...
No Matches
diptrace_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 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 2
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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef DIPTRACE_SCH_PARSER_H_
21#define DIPTRACE_SCH_PARSER_H_
22
24
25#include <map>
26#include <memory>
27#include <set>
28#include <vector>
29#include <set>
30
31#include <math/vector2d.h>
32#include <wx/string.h>
33
34
35class LIB_SYMBOL;
37class REPORTER;
38class SCH_IO;
39class SCH_LABEL;
40class SCH_LINE;
41class SCH_PIN;
42class SCH_SCREEN;
43class SCH_SHEET;
44class SCH_SYMBOL;
45class SCHEMATIC;
46
47
48namespace DIPTRACE
49{
50
51// ---------------------------------------------------------------------------
52// Intermediate data structures for parsed .dch data
53// ---------------------------------------------------------------------------
54
57{
58 wxString name;
59 int field_a = 0;
60};
61
62
65{
66 int kindCode = 0;
67 int sheetIndex = 0;
68 int lineWidth = 0;
69 uint8_t color[3] = {};
70 std::vector<VECTOR2I> points;
71};
72
73
75struct DCH_PIN
76{
77 int index = 0;
78 bool hasHeader = false;
79 int headerA = 0;
80 int headerB = 0;
81 int headerC = 0;
82 int typeCode = 0;
83 int x = 0;
84 int y = 0;
85 int length = 0;
86 wxString name;
87 wxString number;
88 int netFlagA = 0;
89 int netFlagB = 0;
90 int labelXOff = 0;
91 int labelYOff = 0;
92 int numXOff = 0;
93 int numYOff = 0;
94 wxString midTailText;
95 int stubDx = 0;
96 int stubDy = 0;
97 int tailByte = 0;
98};
99
100
103{
104 uint8_t flags[3] = {};
105 int shapeField = 0;
106 int lineWidth = 0;
107 std::vector<VECTOR2I> points;
108 int fontX = 0;
109 int fontY = 0;
112 int kindCode = 0;
114 int kindFlag = 0;
115};
116
117
120{
121 uint8_t flags[3] = {};
122 int type = 0;
123 wxString fontName;
124 wxString text;
125 int fontSize = 0;
126 int fieldA = 0;
127 int coordX = 0;
128 int coordY = 0;
129 int fieldB = 0;
130 int fieldC = 0;
131 uint8_t flagA = 0;
132 uint8_t flagB = 0;
133 int fieldD = 0;
134 int fieldE = 0;
135 int fieldF = 0;
136 int fieldG = 0;
137 uint8_t flags2[4] = {};
138 int fieldH = 0;
139};
140
141
144{
145 size_t fileOffset = 0;
146 int bboxX1 = 0;
147 int bboxY1 = 0;
148 int bboxX2 = 0;
149 int bboxY2 = 0;
150 wxString compName;
151 wxString refdes;
152 wxString value;
153 wxString prefix;
154 wxString nameDup;
155 wxString partName;
156 wxString partNumber;
157 bool isMultiPart = false;
158 wxString partId;
159 int sheetIndex = 0;
160 int rotationE4 = 0;
161 wxString libPath;
162 wxString patternName;
163 wxString datasheet;
164
166 std::vector<std::pair<wxString, wxString>> additionalFields;
167
168 std::vector<DCH_PIN> pins;
169 std::vector<DCH_SHAPE> shapes;
170 std::vector<DCH_COMPONENT_TEXT> texts;
171};
172
173
176{
177 int coordX = 0;
178 int coordY = 0;
179 int sheetIndex = 0;
180 int busType = 0;
181 int instanceId = 0;
182 int signalCount = 0;
183 wxString name;
184};
185
186
189{
190 wxString name;
191 int coordX = 0;
192 int coordY = 0;
193 int field1 = 0;
194};
195
196
200{
201 bool found = false;
202 double widthMM = 0.0;
203 double heightMM = 0.0;
204};
205
206
209{
210 int sheetIndex = 0;
211 std::vector<VECTOR2I> points;
212 int object1 = 0;
213 int subObject1 = 0;
214 int bus1 = -1;
215 int object2 = 0;
216 int subObject2 = 0;
217 int bus2 = -1;
218};
219
220
221// ---------------------------------------------------------------------------
222// Parser class
223// ---------------------------------------------------------------------------
224
235{
236public:
244 SCH_PARSER( const wxString& aFileName, SCHEMATIC* aSchematic, SCH_SHEET* aRootSheet,
245 PROGRESS_REPORTER* aProgressReporter = nullptr, REPORTER* aReporter = nullptr );
246
247 ~SCH_PARSER();
248
254 void Parse();
255
257
258private:
259 // -- Binary format parsing ------------------------------------------------
260
261 void parseHeader();
264 void parseTextStyles();
266 void parseComponents( size_t aBusSectionOffset );
267 void parseOneComponent( size_t aCompEnd, bool aUseCompEnd = true );
268 void parsePin( int aPinIndex, DCH_COMPONENT& aComp );
269 void parseShape( DCH_COMPONENT& aComp );
271 bool parseComponentTextField( DCH_COMPONENT& aComp, size_t aCompEnd );
272 void parseEmbeddedPattern( DCH_COMPONENT& aComp, size_t aCompEnd );
273 void parseBusSection();
274 void parseNetSection();
275 void parseWireSection();
276 void parseSheetShapes();
277
280 void findPageGeometry();
281
284 VECTOR2I applyPageOffset( const VECTOR2I& aPos ) const;
285
286 // -- Structural scanning --------------------------------------------------
287
292 size_t findBusSection( size_t aSearchStart ) const;
293
298 size_t findTailStart() const;
299
304 std::vector<size_t> scanComponentBoundaries( size_t aFirstComp, size_t aBusSectionOffset ) const;
305
306 // -- Shape detection helpers -----------------------------------------------
307
311 bool isShapeStart( size_t aOffset ) const;
312 bool isFontBearingShapeStart( size_t aOffset ) const;
313
314 // -- KiCad object creation ------------------------------------------------
315
320 void createKiCadObjects();
321
325 SCH_SCREEN* getOrCreateSheet( int aSheetIndex );
328
335 LIB_SYMBOL* getOrCreateLibSymbol( const DCH_COMPONENT& aComp, int aUnit );
336 void populateLibSymbolUnit( LIB_SYMBOL* aLibSymbol, const DCH_COMPONENT& aComp, int aUnit );
338
344 wxString componentSymbolName( const DCH_COMPONENT& aComp ) const;
345 wxString normalizedRefdes( const DCH_COMPONENT& aComp ) const;
346
350 void createSymbolInstance( const DCH_COMPONENT& aComp, SCH_SCREEN* aFallbackScreen );
351
359 void createNetPortLabels();
360
364 int sheetForNearestWire( const VECTOR2I& aPos ) const;
365
367 void createWires();
368 void createSheetShapes();
369
371 void createJunctions();
372
376
380
383 bool isComponentHeaderAt( size_t aOffset ) const;
384
390 int sheetForPositions( const std::vector<VECTOR2I>& aPositions, int aFallback ) const;
391
399 int sheetForComponentPins( const std::vector<VECTOR2I>& aConnectionPoints );
400
403 int resolveSheetTally( const std::map<std::pair<int, int>, int>& aTally );
404
411 static int pinOrientationFromOffset( int aOffsetX, int aOffsetY, int aHalfWidth, int aHalfHeight );
412
423 static int toKiCadCoordX( int aDipTraceCoord );
424 static int toKiCadCoordY( int aDipTraceCoord );
425
431 static int toKiCadSize( int aDipTraceCoord );
432
436 wxString getLibName() const;
437
438 // -- Member data ----------------------------------------------------------
439
449 wxString m_fileName;
450
451 // Parsed intermediate data
453 std::vector<DCH_SHEET_DEF> m_sheetDefs;
454 std::vector<DCH_COMPONENT> m_components;
455 std::vector<DCH_BUS_ENTRY> m_buses;
456 std::vector<DCH_NET_ENTRY> m_nets;
457 std::vector<DCH_WIRE> m_wires;
458 std::vector<DCH_SHEET_SHAPE> m_sheetShapes;
459
462 std::map<std::pair<int, int>, std::set<int>> m_wirePointSheets;
463
466 std::map<std::pair<int, int>, std::vector<std::pair<int, int>>> m_pointPartSheets;
467
471
475
478
481
483 std::map<size_t, int> m_offsetToPartId;
484
487 std::map<int, int> m_partIdSheet;
488
491 std::set<wxString> m_netPortNames;
492
495
496 // KiCad object management
497 std::vector<SCH_SHEET*> m_sheets;
498 std::map<wxString, std::unique_ptr<LIB_SYMBOL>> m_libSymbols;
499 std::map<wxString, std::vector<SCH_SYMBOL*>> m_placedSymbolsByLibName;
500
502 std::map<wxString, int> m_refdesUnitMap;
503
506};
507
508} // namespace DIPTRACE
509
510#endif // DIPTRACE_SCH_PARSER_H_
Low-level binary reader for DipTrace file formats.
void parseOneComponent(size_t aCompEnd, bool aUseCompEnd=true)
static int toKiCadCoordY(int aDipTraceCoord)
std::map< wxString, int > m_refdesUnitMap
Map from refdes to the number of units already created for multi-unit symbols.
std::vector< DCH_SHEET_DEF > m_sheetDefs
int sheetForNearestWire(const VECTOR2I &aPos) const
Sheet whose decoded wire geometry is closest to aPos, or -1 when no wire exists.
bool isShapeStart(size_t aOffset) const
Check if the data at the given offset looks like a shape/polyline start.
void parseFontBearingShape(DCH_COMPONENT &aComp)
static int pinOrientationFromOffset(int aOffsetX, int aOffsetY, int aHalfWidth, int aHalfHeight)
Determine the pin orientation from the pin connection-point offset relative to the symbol body center...
void buildWirePointSheets()
Build the maps from wire-point position to the sheet(s) and part(s) connecting there,...
void createWires()
Emit SCH_LINE wire segments decoded from the net/wire section.
bool parseComponentTextField(DCH_COMPONENT &aComp, size_t aCompEnd)
size_t m_netPortLabelCount
Count of net-port labels emitted, for the import summary report.
wxString normalizedRefdes(const DCH_COMPONENT &aComp) const
std::vector< size_t > scanComponentBoundaries(size_t aFirstComp, size_t aBusSectionOffset) const
Pre-scan the file to find component start offsets using the bbox(4*int4) + 5-string pattern.
wxString componentSymbolName(const DCH_COMPONENT &aComp) const
Library symbol name for a component.
void parseComponents(size_t aBusSectionOffset)
void createJunctions()
Synthesize junctions where conductors coincide (DipTrace stores none explicitly).
void createSymbolInstance(const DCH_COMPONENT &aComp, SCH_SCREEN *aFallbackScreen)
Create a SCH_SYMBOL instance on the given screen from a DipTrace component.
std::vector< DCH_NET_ENTRY > m_nets
int sheetForComponentPins(const std::vector< VECTOR2I > &aConnectionPoints)
Resolve a symbol's sheet from its pin connection points.
DCH_PAGE m_page
Decoded page geometry and the resulting half-page placement offset (KiCad nm).
size_t findTailStart() const
Find where the int3(0) tail padding begins by scanning backward from the end of file.
bool isComponentHeaderAt(size_t aOffset) const
True if a component record header (placement + five header strings) starts at aOffset.
static int toKiCadSize(int aDipTraceCoord)
Convert a DipTrace length or stroke width to KiCad schematic internal units.
void buildComponentPartIds()
Enumerate every component header in the file (real components and net ports alike) so each component'...
std::map< wxString, std::vector< SCH_SYMBOL * > > m_placedSymbolsByLibName
std::map< size_t, int > m_offsetToPartId
Component start offset -> DipTrace part id (its index in the in-file component order).
size_t findBusSection(size_t aSearchStart) const
Find the bus section start offset by searching for the characteristic marker pattern: int4(10000) int...
void createNetPortLabels()
Create a global net label for every DipTrace net-port component (auto_net_ports library).
void Parse()
Parse the .dch file and populate the schematic with KiCad objects.
std::map< std::pair< int, int >, std::vector< std::pair< int, int > > > m_pointPartSheets
Wire-endpoint position (KiCad nm) -> (partId, sheet) pairs of the parts connecting there.
std::vector< DCH_WIRE > m_wires
wxString getLibName() const
Build a library name string for the import.
VECTOR2I applyPageOffset(const VECTOR2I &aPos) const
Apply the page-center offset to an absolute KiCad-nm placement so 0,0-centered DipTrace content lands...
size_t m_componentSectionStart
File offset of the component section start, used to enumerate components in part-id order.
bool isFontBearingShapeStart(size_t aOffset) const
std::set< wxString > m_netPortNames
Names of nets that own a placed net-port component; these are the only nets DipTrace draws a label fo...
std::vector< SCH_SHEET * > m_sheets
One per DipTrace sheet.
PROGRESS_REPORTER * m_progressReporter
void createKiCadObjects()
Create KiCad objects from the parsed intermediate data and add them to the appropriate schematic shee...
int m_lastSymbolPartId
Largest part id assigned to a symbol so far; enforces the monotonic part-id order used to disambiguat...
int resolveSheetTally(const std::map< std::pair< int, int >, int > &aTally)
Resolve a (partId, sheet) -> hit-count tally to a sheet, preferring the highest-count pair with a par...
size_t m_wireSectionEnd
End offset of the decoded wire section; the sheet-shape section follows it in modern files.
void parseShape(DCH_COMPONENT &aComp)
void populateLibSymbolUnit(LIB_SYMBOL *aLibSymbol, const DCH_COMPONENT &aComp, int aUnit)
void findPageGeometry()
Locate the page-geometry record (width/height/margins, each mm*30000) in the binary and fill m_page.
int ComponentBoundaryScanCount() const
std::map< int, int > m_partIdSheet
DipTrace part id -> sheet index, resolved from the wire connectivity.
int sheetForPositions(const std::vector< VECTOR2I > &aPositions, int aFallback) const
Pick the sheet a placed item belongs to by matching its connection points against the decoded wire ge...
std::vector< DCH_SHEET_SHAPE > m_sheetShapes
SCH_SCREEN * getOrCreateSheet(int aSheetIndex)
Get or create the KiCad sheet and screen for the given DipTrace sheet index.
void parsePin(int aPinIndex, DCH_COMPONENT &aComp)
std::vector< DCH_COMPONENT > m_components
std::map< wxString, std::unique_ptr< LIB_SYMBOL > > m_libSymbols
Symbol library cache.
void parseEmbeddedPattern(DCH_COMPONENT &aComp, size_t aCompEnd)
LIB_SYMBOL * getOrCreateLibSymbol(const DCH_COMPONENT &aComp, int aUnit)
Create a LIB_SYMBOL from the DipTrace component data.
static int toKiCadCoordX(int aDipTraceCoord)
Convert a DipTrace coordinate to KiCad schematic internal units.
SCH_PARSER(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, PROGRESS_REPORTER *aProgressReporter=nullptr, REPORTER *aReporter=nullptr)
std::vector< DCH_BUS_ENTRY > m_buses
std::map< std::pair< int, int >, std::set< int > > m_wirePointSheets
Wire-point position (KiCad nm) -> set of sheet indices carrying a wire there.
Define a library symbol object.
Definition lib_symbol.h:79
A progress reporter interface for use in multi-threaded environments.
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:71
Holds all the data relating to one schematic.
Definition schematic.h:90
Base class that schematic file and library loading and saving plugins should derive from.
Definition sch_io.h:59
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:38
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
Schematic symbol object.
Definition sch_symbol.h:69
A bus entry as read from the bus section.
int coordY
int coordX
int busType
int instanceId
int signalCount
wxString name
int sheetIndex
A stored component text field record that precedes the embedded footprint pattern.
A component as read from the .dch file.
wxString patternName
Embedded footprint pattern name (e.g. "LED100", "CR0805")
std::vector< DCH_COMPONENT_TEXT > texts
int rotationE4
Placement rotation in radians x 1e4 (0, 15708, 31416, 47124)
std::vector< DCH_SHAPE > shapes
std::vector< std::pair< wxString, wxString > > additionalFields
User-defined additional fields, as (name, value) pairs (e.g. "Part Number (Digi-Key)").
wxString datasheet
Datasheet URL stored in the placement tail.
std::vector< DCH_PIN > pins
A net label/wire entry from the net section.
int coordX
int field1
wxString name
int coordY
Decoded page geometry.
bool found
True when a page record was located in the binary.
A component pin as stored in the .dch file.
int netFlagB
1 when the pin name is shown, 0 when hidden
int x
DipTrace coordinate units (100/3 nm)
A graphical shape primitive (polyline) in a component.
std::vector< VECTOR2I > points
Points in DipTrace coord units.
int kindFlag
Leading kind int3; observed 0 for decoded drawing shapes.
int kindCode
Leading kind int3: 1 line, 3 arrow, 4 rect, 6 obround, 8 filled polygon, 9 outline polygon/polyline.
Sheet definition as read from the file header.
A top-level schematic sheet graphical primitive.
int kindCode
1 line, 4 rectangle.
std::vector< VECTOR2I > points
Points in DipTrace coord units.
A single schematic wire decoded from the net/wire section.
int object1
Connected item id at endpoint 1.
int sheetIndex
DipTrace sheet index.
std::vector< VECTOR2I > points
KiCad nm, ready for SCH_LINE.
int subObject1
Pin/sub index at endpoint 1.
int bus1
Bus index at endpoint 1 (-1 = none)
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683