KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pads_binary_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) 2026 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#pragma once
25
26#include <string>
27#include <vector>
28#include <map>
29#include <cstdint>
30
31#include <wx/string.h>
32
33#include "pads_parser.h"
34
35namespace PADS_IO
36{
37
55{
56public:
59
60 void Parse( const wxString& aFileName );
61
66 static bool IsBinaryPadsFile( const wxString& aFileName );
67
68 const PARAMETERS& GetParameters() const { return m_parameters; }
69 const std::vector<PART>& GetParts() const { return m_parts; }
70 const std::vector<NET>& GetNets() const { return m_nets; }
71 const std::vector<ROUTE>& GetRoutes() const { return m_routes; }
72 const std::vector<TEXT>& GetTexts() const { return m_texts; }
73 const std::vector<POUR>& GetPours() const { return m_pours; }
74 const std::vector<POLYLINE>& GetBoardOutlines() const { return m_boardOutlines; }
75 const std::map<std::string, PART_DECAL>& GetPartDecals() const { return m_decals; }
76 int GetLayerCount() const { return m_parameters.layer_count; }
77 bool IsBasicUnits() const { return true; }
78
79 std::vector<LAYER_INFO> GetLayerInfos() const;
80
81private:
82 static constexpr uint16_t MAGIC = 0xFF00;
83 static constexpr int HEADER_SIZE = 10;
84 static constexpr int FOOTER_SIZE = 46;
85 static constexpr int DIR_ENTRY_SIZE = 16;
86 static constexpr int32_t ANGLE_SCALE = 1800000;
87
88 struct DirEntry
89 {
90 int index = 0;
91 uint32_t count = 0;
92 uint32_t totalBytes = 0;
93 uint32_t dataOffset = 0;
94 uint32_t perItem = 0;
95 };
96
97 // Version helpers
98 bool isOldFormat() const { return m_version == 0x2021; }
99 int dirEntryCount() const;
100
101 // Low-level readers
102 uint8_t readU8( size_t aOffset ) const;
103 uint16_t readU16( size_t aOffset ) const;
104 uint32_t readU32( size_t aOffset ) const;
105 int32_t readI32( size_t aOffset ) const;
106 std::string readFixedString( size_t aOffset, size_t aMaxLen ) const;
107
108 // File structure parsing
109 void parseHeader();
110 void parseFooter();
111 void parseDirectory();
112
113 // Section data accessors
114 const uint8_t* sectionData( int aIndex ) const;
115 uint32_t sectionSize( int aIndex ) const;
116 const DirEntry* getSection( int aIndex ) const;
117
118 // Section parsers
119 void parseBoardSetup();
120 void parseStringPool();
121 void parsePartPlacements();
122 void parseSection19Parts();
123 void parsePadStacks();
124 void parsePartDecals();
125 void parseFootprintDefs();
126 void parseLineVertices();
127 void parseBoardOutline();
128 void parseNetNames();
129 void parseMetadataRegion();
130 void parseDftConfig( size_t aStart, size_t aEnd );
131 void parseRouteVertices();
132 void parseTextRecords();
133 void parseCopperPours();
134
135 // DFT format parsers
136 std::map<std::string, std::string> parseDftDotPadded( size_t aPos, size_t aEnd ) const;
137 std::map<std::string, std::string> parseDftNullSeparated( size_t aPos, size_t aEnd ) const;
138
139 // Net name helpers
140 std::string extractNetName( const uint8_t* aData, size_t aOffset ) const;
141 bool isValidNetName( const std::string& aName ) const;
142
143 // String pool resolver
144 std::string resolveString( uint32_t aByteOffset ) const;
145
146 // Coordinate conversion from binary absolute to PADS_IO design-relative units
147 double toBasicCoordX( int32_t aRawValue ) const;
148 double toBasicCoordY( int32_t aRawValue ) const;
149 double toBasicAngle( int32_t aRawAngle ) const;
150
151 // File data
152 std::vector<uint8_t> m_data;
153 uint16_t m_version = 0;
155
156 // Directory
157 std::vector<DirEntry> m_dirEntries;
158
159 // String pool (section 57)
160 std::vector<uint8_t> m_stringPoolBytes;
161
162 // Line vertex pool (section 12)
164 {
165 int32_t x = 0;
166 int32_t y = 0;
167 uint32_t extra = 0;
168 };
169 std::vector<LineVertex> m_lineVertices;
170
171 // Coordinate origin from DFT_CONFIGURATION
172 int32_t m_originX = 0;
173 int32_t m_originY = 0;
174 bool m_originFound = false;
175
176 // Pad stack cache indexed by section 4 record number
177 std::map<int, std::vector<PAD_STACK_LAYER>> m_padStackCache;
178
179 // Footprint type name -> decal name mapping from section 17
180 std::map<std::string, std::string> m_fpTypeToDecal;
181
182 // Route segment from section 24 linking to section 60 vertices
184 {
185 int32_t x1 = 0;
186 int32_t y1 = 0;
187 int32_t x2 = 0;
188 int32_t y2 = 0;
189 int32_t width = 0;
190 };
191 std::vector<RouteSegment> m_routeSegments;
192
193 // Via locations from section 59 (pin connection endpoints)
195 {
196 int32_t x = 0;
197 int32_t y = 0;
198 };
199 std::vector<ViaLocation> m_viaLocations;
200
201 // Output data (same structs as ASCII parser)
203 std::vector<PART> m_parts;
204 std::vector<NET> m_nets;
205 std::vector<ROUTE> m_routes;
206 std::vector<TEXT> m_texts;
207 std::vector<POUR> m_pours;
208 std::vector<POLYLINE> m_boardOutlines;
209 std::map<std::string, PART_DECAL> m_decals;
210};
211
212} // namespace PADS_IO
std::map< std::string, std::string > parseDftNullSeparated(size_t aPos, size_t aEnd) const
uint8_t readU8(size_t aOffset) const
double toBasicCoordY(int32_t aRawValue) const
const std::map< std::string, PART_DECAL > & GetPartDecals() const
std::vector< uint8_t > m_stringPoolBytes
std::vector< NET > m_nets
const std::vector< POLYLINE > & GetBoardOutlines() const
std::string readFixedString(size_t aOffset, size_t aMaxLen) const
bool isValidNetName(const std::string &aName) const
static constexpr int FOOTER_SIZE
std::vector< PART > m_parts
uint32_t readU32(size_t aOffset) const
const std::vector< POUR > & GetPours() const
std::map< std::string, std::string > parseDftDotPadded(size_t aPos, size_t aEnd) const
std::map< int, std::vector< PAD_STACK_LAYER > > m_padStackCache
const DirEntry * getSection(int aIndex) const
static constexpr int32_t ANGLE_SCALE
std::vector< POUR > m_pours
static bool IsBinaryPadsFile(const wxString &aFileName)
Check if a file appears to be a PADS binary PCB file.
std::map< std::string, PART_DECAL > m_decals
static constexpr int HEADER_SIZE
std::vector< uint8_t > m_data
std::vector< POLYLINE > m_boardOutlines
std::string resolveString(uint32_t aByteOffset) const
uint16_t readU16(size_t aOffset) const
uint32_t sectionSize(int aIndex) const
void parseDftConfig(size_t aStart, size_t aEnd)
const std::vector< TEXT > & GetTexts() const
const PARAMETERS & GetParameters() const
std::string extractNetName(const uint8_t *aData, size_t aOffset) const
void Parse(const wxString &aFileName)
const std::vector< PART > & GetParts() const
double toBasicCoordX(int32_t aRawValue) const
const uint8_t * sectionData(int aIndex) const
static constexpr int DIR_ENTRY_SIZE
std::vector< RouteSegment > m_routeSegments
const std::vector< NET > & GetNets() const
static constexpr uint16_t MAGIC
std::map< std::string, std::string > m_fpTypeToDecal
std::vector< ViaLocation > m_viaLocations
std::vector< DirEntry > m_dirEntries
const std::vector< ROUTE > & GetRoutes() const
std::vector< ROUTE > m_routes
std::vector< LineVertex > m_lineVertices
std::vector< TEXT > m_texts
int32_t readI32(size_t aOffset) const
double toBasicAngle(int32_t aRawAngle) const
std::vector< LAYER_INFO > GetLayerInfos() const