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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef DIPTRACE_SCH_PARSER_H_
25#define DIPTRACE_SCH_PARSER_H_
26
28
29#include <map>
30#include <memory>
31#include <set>
32#include <vector>
33#include <set>
34
35#include <math/vector2d.h>
36#include <wx/string.h>
37
38
39class LIB_SYMBOL;
41class REPORTER;
42class SCH_IO;
43class SCH_LABEL;
44class SCH_LINE;
45class SCH_PIN;
46class SCH_SCREEN;
47class SCH_SHEET;
48class SCH_SYMBOL;
49class SCHEMATIC;
50
51
52namespace DIPTRACE
53{
54
55// ---------------------------------------------------------------------------
56// Intermediate data structures for parsed .dch data
57// ---------------------------------------------------------------------------
58
61{
62 wxString name;
63 int field_a = 0;
64};
65
66
69{
70 int kindCode = 0;
71 int sheetIndex = 0;
72 int lineWidth = 0;
73 uint8_t color[3] = {};
74 std::vector<VECTOR2I> points;
75};
76
77
79struct DCH_PIN
80{
81 int index = 0;
82 bool hasHeader = false;
83 int headerA = 0;
84 int headerB = 0;
85 int headerC = 0;
86 int typeCode = 0;
87 int x = 0;
88 int y = 0;
89 int length = 0;
90 wxString name;
91 wxString number;
92 int netFlagA = 0;
93 int netFlagB = 0;
94 int labelXOff = 0;
95 int labelYOff = 0;
96 int numXOff = 0;
97 int numYOff = 0;
98 wxString midTailText;
99 int stubDx = 0;
100 int stubDy = 0;
101 int tailByte = 0;
102};
103
104
107{
108 uint8_t flags[3] = {};
109 int shapeField = 0;
110 int lineWidth = 0;
111 std::vector<VECTOR2I> points;
112 int fontX = 0;
113 int fontY = 0;
116 int kindCode = 0;
118 int kindFlag = 0;
119};
120
121
124{
125 uint8_t flags[3] = {};
126 int type = 0;
127 wxString fontName;
128 wxString text;
129 int fontSize = 0;
130 int fieldA = 0;
131 int coordX = 0;
132 int coordY = 0;
133 int fieldB = 0;
134 int fieldC = 0;
135 uint8_t flagA = 0;
136 uint8_t flagB = 0;
137 int fieldD = 0;
138 int fieldE = 0;
139 int fieldF = 0;
140 int fieldG = 0;
141 uint8_t flags2[4] = {};
142 int fieldH = 0;
143};
144
145
148{
149 size_t fileOffset = 0;
150 int bboxX1 = 0;
151 int bboxY1 = 0;
152 int bboxX2 = 0;
153 int bboxY2 = 0;
154 wxString compName;
155 wxString refdes;
156 wxString value;
157 wxString prefix;
158 wxString nameDup;
159 wxString partName;
160 wxString partNumber;
161 bool isMultiPart = false;
162 wxString partId;
163 int sheetIndex = 0;
164 int rotationE4 = 0;
165 wxString libPath;
166 wxString patternName;
167 wxString datasheet;
168
170 std::vector<std::pair<wxString, wxString>> additionalFields;
171
172 std::vector<DCH_PIN> pins;
173 std::vector<DCH_SHAPE> shapes;
174 std::vector<DCH_COMPONENT_TEXT> texts;
175};
176
177
180{
181 int coordX = 0;
182 int coordY = 0;
183 int sheetIndex = 0;
184 int busType = 0;
185 int instanceId = 0;
186 int signalCount = 0;
187 wxString name;
188};
189
190
193{
194 wxString name;
195 int coordX = 0;
196 int coordY = 0;
197 int field1 = 0;
198};
199
200
204{
205 bool found = false;
206 double widthMM = 0.0;
207 double heightMM = 0.0;
208};
209
210
213{
214 int sheetIndex = 0;
215 std::vector<VECTOR2I> points;
216 int object1 = 0;
217 int subObject1 = 0;
218 int bus1 = -1;
219 int object2 = 0;
220 int subObject2 = 0;
221 int bus2 = -1;
222};
223
224
225// ---------------------------------------------------------------------------
226// Parser class
227// ---------------------------------------------------------------------------
228
239{
240public:
248 SCH_PARSER( const wxString& aFileName, SCHEMATIC* aSchematic, SCH_SHEET* aRootSheet,
249 PROGRESS_REPORTER* aProgressReporter = nullptr, REPORTER* aReporter = nullptr );
250
251 ~SCH_PARSER();
252
258 void Parse();
259
261
262private:
263 // -- Binary format parsing ------------------------------------------------
264
265 void parseHeader();
268 void parseTextStyles();
270 void parseComponents( size_t aBusSectionOffset );
271 void parseOneComponent( size_t aCompEnd, bool aUseCompEnd = true );
272 void parsePin( int aPinIndex, DCH_COMPONENT& aComp );
273 void parseShape( DCH_COMPONENT& aComp );
275 bool parseComponentTextField( DCH_COMPONENT& aComp, size_t aCompEnd );
276 void parseEmbeddedPattern( DCH_COMPONENT& aComp, size_t aCompEnd );
277 void parseBusSection();
278 void parseNetSection();
279 void parseWireSection();
280 void parseSheetShapes();
281
284 void findPageGeometry();
285
288 VECTOR2I applyPageOffset( const VECTOR2I& aPos ) const;
289
290 // -- Structural scanning --------------------------------------------------
291
296 size_t findBusSection( size_t aSearchStart ) const;
297
302 size_t findTailStart() const;
303
308 std::vector<size_t> scanComponentBoundaries( size_t aFirstComp, size_t aBusSectionOffset ) const;
309
310 // -- Shape detection helpers -----------------------------------------------
311
315 bool isShapeStart( size_t aOffset ) const;
316 bool isFontBearingShapeStart( size_t aOffset ) const;
317
318 // -- KiCad object creation ------------------------------------------------
319
324 void createKiCadObjects();
325
329 SCH_SCREEN* getOrCreateSheet( int aSheetIndex );
332
339 LIB_SYMBOL* getOrCreateLibSymbol( const DCH_COMPONENT& aComp, int aUnit );
340 void populateLibSymbolUnit( LIB_SYMBOL* aLibSymbol, const DCH_COMPONENT& aComp, int aUnit );
342
348 wxString componentSymbolName( const DCH_COMPONENT& aComp ) const;
349 wxString normalizedRefdes( const DCH_COMPONENT& aComp ) const;
350
354 void createSymbolInstance( const DCH_COMPONENT& aComp, SCH_SCREEN* aFallbackScreen );
355
363 void createNetPortLabels();
364
368 int sheetForNearestWire( const VECTOR2I& aPos ) const;
369
371 void createWires();
372 void createSheetShapes();
373
375 void createJunctions();
376
380
384
387 bool isComponentHeaderAt( size_t aOffset ) const;
388
394 int sheetForPositions( const std::vector<VECTOR2I>& aPositions, int aFallback ) const;
395
403 int sheetForComponentPins( const std::vector<VECTOR2I>& aConnectionPoints );
404
407 int resolveSheetTally( const std::map<std::pair<int, int>, int>& aTally );
408
415 static int pinOrientationFromOffset( int aOffsetX, int aOffsetY, int aHalfWidth, int aHalfHeight );
416
427 static int toKiCadCoordX( int aDipTraceCoord );
428 static int toKiCadCoordY( int aDipTraceCoord );
429
435 static int toKiCadSize( int aDipTraceCoord );
436
440 wxString getLibName() const;
441
442 // -- Member data ----------------------------------------------------------
443
453 wxString m_fileName;
454
455 // Parsed intermediate data
457 std::vector<DCH_SHEET_DEF> m_sheetDefs;
458 std::vector<DCH_COMPONENT> m_components;
459 std::vector<DCH_BUS_ENTRY> m_buses;
460 std::vector<DCH_NET_ENTRY> m_nets;
461 std::vector<DCH_WIRE> m_wires;
462 std::vector<DCH_SHEET_SHAPE> m_sheetShapes;
463
466 std::map<std::pair<int, int>, std::set<int>> m_wirePointSheets;
467
470 std::map<std::pair<int, int>, std::vector<std::pair<int, int>>> m_pointPartSheets;
471
475
479
482
485
487 std::map<size_t, int> m_offsetToPartId;
488
491 std::map<int, int> m_partIdSheet;
492
495 std::set<wxString> m_netPortNames;
496
499
500 // KiCad object management
501 std::vector<SCH_SHEET*> m_sheets;
502 std::map<wxString, std::unique_ptr<LIB_SYMBOL>> m_libSymbols;
503 std::map<wxString, std::vector<SCH_SYMBOL*>> m_placedSymbolsByLibName;
504
506 std::map<wxString, int> m_refdesUnitMap;
507
510};
511
512} // namespace DIPTRACE
513
514#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:83
A progress reporter interface for use in multi-threaded environments.
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:75
Holds all the data relating to one schematic.
Definition schematic.h:89
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: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:73
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:687