35#include <unordered_set>
37#include <fmt/format.h>
49 { 0x00,
"OF" }, { 0x01,
"RF" }, { 0x02,
"R" }, { 0x03,
"S" },
50 { 0x06,
"RT" }, { 0x07,
"ST" }, { 0x08,
"RA" }, { 0x09,
"SA" },
108 if( aVersion <= 0x2021 )
111 if( aVersion == 0x2022 )
179 std::vector<uint8_t> header;
192static void logParsePhase(
const char* aWhat, std::chrono::steady_clock::time_point aStart )
194 static const bool enabled = getenv(
"KICAD_PADS_PROFILE" ) !=
nullptr;
199 const double ms = std::chrono::duration<double, std::milli>( std::chrono::steady_clock::now() - aStart ).count();
201 fputs( fmt::format(
"PROF {:<32} {:>8.1f} ms\n", aWhat, ms ).c_str(), stderr );
207 std::vector<uint8_t>
bytes;
221#define KITIME( call ) \
224 auto _t0 = std::chrono::steady_clock::now(); \
226 logParsePhase( #call, _t0 ); \
277 return p.name.empty();
285 return m_sdb.Section( aIndex );
291 return static_cast<double>( aRawValue );
297 return static_cast<double>( aRawValue );
306 return static_cast<double>( aRawAngle ) /
static_cast<double>(
ANGLE_SCALE );
314 if( !
m_sdb.Coords().Found() )
317 uint32_t headerBase =
m_sdb.Coords().HeaderBase();
319 if( !
m_cursor.InBounds( headerBase, 20 ) )
323 uint32_t maxLayer =
m_sdb.RecordAt( headerBase ).U32( 16 );
325 if( maxLayer >= 1 && maxLayer <= 64 )
326 m_parameters.layer_count =
static_cast<int>( maxLayer );
337 int aNameOff,
const std::string& aRefDes )
const
356 constexpr size_t LOGICAL_ROTATION = 44;
361 if( section->
count == 0 )
365 || ( section->
stride != 96 && section->
stride != 112 ) )
368 const size_t logicalBase = section->
physicalOffset - LOGICAL_ROTATION;
375 const size_t base = logicalBase +
static_cast<size_t>(
index ) * section->
stride;
377 std::string refdes = record.
Str( 44, 16 );
378 const size_t nextBase = logicalBase +
static_cast<size_t>(
index + 1 ) * section->
stride;
379 SDB_RECORD nextRecord =
m_sdb.RecordAt(
static_cast<uint32_t
>( nextBase ) );
383 if( section->
stride == 112 )
386 if( section->
stride == 96 )
394 if( nextRecord.
U8( 16 ) & 0x10 )
398 if( section->
stride == 112 )
400 int32_t clusterId = record.
I32( 108 );
407 m_parts.push_back( std::move( part ) );
428 if( sec68->
count == 0 )
434 static constexpr size_t REC_SIZE = 60;
440 constexpr size_t SEC69_LEAD_IN = 12;
442 if( sec69Rec0 < SEC69_LEAD_IN || sec68->count > ( sec69Rec0 - SEC69_LEAD_IN ) / REC_SIZE )
445 size_t base = sec69Rec0 - SEC69_LEAD_IN -
static_cast<size_t>( sec68->
count ) * REC_SIZE;
447 for( uint32_t i = 0; i < sec68->
count; ++i )
450 std::string
name = rec.
Str( 4, 16 );
454 if( rec.
U32( 0 ) != i + 1 ||
name.empty() )
459 cluster.
id =
static_cast<int>( i ) + 1;
470 if( !entry || !layerSection )
473 const uint32_t expectedRecordSize =
m_version <= 0x2021 ? 52 :
m_version == 0x2022 ? 56 : 64;
476 ||
static_cast<uint64_t
>( entry->
count ) * expectedRecordSize != entry->
totalBytes )
479 if( entry->
count == 0 )
482 const uint32_t logicalRotation =
m_version == 0x2022 ? 20 :
m_version <= 0x2021 ? 24 : 28;
488 uint32_t recSize = entry->
stride;
495 std::string shapeName =
"R";
499 shapeName = shapeIt->second;
507 psl.
shape = shapeName;
508 psl.
sizeA =
static_cast<double>( padWidth );
509 psl.
drill =
static_cast<double>( drill );
515 psl.
plated = drill > 0 && padWidth - drill > OLD_NPTH_CLEARANCE;
519 psl.
plated = drill > 0 && drill < padWidth;
522 bool isFinger = ( shapeName ==
"RF" || shapeName ==
"OF" || shapeName ==
"RC" );
527 if( isFinger && finLength > 0 )
528 psl.
sizeB =
static_cast<double>( finLength );
530 psl.
sizeB =
static_cast<double>( padWidth );
535 const uint32_t poolStart = entry->
physicalOffset - logicalRotation;
536 const uint64_t poolBytes =
static_cast<uint64_t
>( entry->
physicalCount ) * recSize;
538 if( !
m_cursor.InBounds( poolStart, poolBytes ) )
543 std::vector<int32_t> maxPadDiameter( entry->
physicalCount, 0 );
545 const uint32_t layerRecordSize =
m_version <= 0x2021 ? 20 : 24;
546 const uint32_t layerTableHeaderSize =
m_version == 0x2022 ? 64 :
m_version <= 0x2021 ? 20 : 24;
547 uint64_t layerTableStart64 =
static_cast<uint64_t
>( poolStart ) + poolBytes + layerTableHeaderSize;
548 const uint64_t layerTableBytes =
static_cast<uint64_t
>( layerSection->
count ) * layerRecordSize;
550 if( layerTableBytes != layerSection->
totalBytes || layerTableStart64 >
m_data.size()
551 || layerTableBytes >
m_data.size() - layerTableStart64 )
556 const uint32_t layerTableStart =
static_cast<uint32_t
>( layerTableStart64 );
562 uint32_t base = poolStart + i * recSize;
575 if( ( nextRec.
U32( 0 ) & 8U ) != 0 )
583 layers.push_back( defaultLayer );
584 maxPadDiameter[i] =
static_cast<int32_t
>( defaultLayer.
sizeA );
596 bool successorMetadata = endCursor ||
m_version == 0x2027;
597 bool rawLayerSelector = successorMetadata;
600 && ( layerSection->
count == 0 || layerCount > layerSection->
count
602 ? layerStart > layerSection->
count
603 : layerStart >= layerSection->
count || layerCount > layerSection->
count - layerStart ) ) )
608 const uint32_t controllerPhase = endCursor ? 2 % layerSection->
count : 0;
610 for( uint32_t layerIdx = 0; layerIdx < layerCount; ++layerIdx )
614 uint32_t geometryIndex = endCursor ? ( layerStart + layerSection->
count - controllerPhase + layerIdx )
615 % layerSection->
count
616 : layerStart + layerIdx;
623 uint32_t metadataIndex = geometryIndex;
625 if( successorMetadata )
627 metadataIndex = endCursor ? ( geometryIndex + 1 ) % layerSection->
count
630 if( metadataIndex >= layerSection->
count
631 && !
m_cursor.InBounds( layerTableStart + metadataIndex * layerRecordSize, layerRecordSize ) )
633 metadataIndex = geometryIndex;
636 uint32_t geometryBase = layerTableStart + geometryIndex * layerRecordSize;
637 uint32_t metadataBase = layerTableStart + metadataIndex * layerRecordSize;
641 uint8_t selector = metadataRec.
U8( 0 );
642 uint8_t shapeCode = metadataRec.
U8( 1 );
644 maxPadDiameter[i] = std::max( maxPadDiameter[i], geometryRec.
I32( 4 ) );
650 layer.
layer = selector == 0 ? 0
651 : selector == 0xFF ? -1
652 :
static_cast<int>( selector ) + ( rawLayerSelector ? 0 : 1 );
653 layer.
shape = shapeIt->second;
654 layer.
sizeA =
static_cast<double>( geometryRec.
I32( 4 ) );
655 int32_t sizeB = geometryRec.
I32( 8 );
657 if( layer.
shape ==
"RT" || layer.
shape ==
"ST" )
664 layer.
sizeB = sizeB > 0 ?
static_cast<double>( sizeB ) : layer.
sizeA;
667 layers.push_back( std::move( layer ) );
679static void logResolvedBase(
int aSection,
const char* aWhat,
size_t aResolved, uint32_t aPhysical )
681 static const bool enabled = getenv(
"KICAD_PADS_SECBASE" ) !=
nullptr;
686 fprintf( stderr,
"SECBASE %d %s resolved=%zu physical=%u\n", aSection, aWhat, aResolved, aPhysical );
707 static constexpr int REC_SIZE = 112;
708 static constexpr int STACK_START_OFFSET = 44;
709 static constexpr int SENTINEL_OFFSET = 64;
710 static constexpr int START_OFFSET = 68;
711 static constexpr int COUNT_OFFSET = 72;
712 static constexpr int STACK_COUNT_OFFSET = 88;
719 if( sec14->
count == 0 )
724 if( start + 12 >
m_data.size() ||
m_sdb.RecordAt( start ).Str( 0, 12 ) !=
"JMPVIA_AAAAA" )
728 if(
static_cast<uint64_t
>( sec14->
count ) * REC_SIZE >
m_data.size() - start )
734 for( uint32_t k = 0; k < sec14->
count; ++k )
736 uint32_t off = start + k * REC_SIZE;
745 std::string
name = rec.
Str( 0, 41 );
748 int32_t startCursor = rec.
I32( START_OFFSET );
749 int32_t count = rec.
I32( COUNT_OFFSET );
751 if( !
name.empty() && count > 0 && count <= 1000 )
755 if( startCursor >= 0 )
758 int32_t stackCount = rec.
I32( STACK_COUNT_OFFSET );
760 if( stackCount > 0 && stackCount <= 1000 )
764 int32_t stackStart = rec.
I32( STACK_START_OFFSET );
766 if( stackStart >= 0 )
790 if( !sec14 || sec14->
stride == 0 )
793 static constexpr int STACK_START_OFFSET = 44;
794 static constexpr int SENTINEL_OFFSET = 64;
795 static constexpr int START_OFFSET = 68;
796 static constexpr int COUNT_OFFSET = 72;
799 size_t stride = sec14->
stride;
801 std::vector<std::string>
table;
802 std::map<std::string, uint32_t> counts;
803 std::map<std::string, int32_t> starts;
804 std::map<std::string, int32_t> stackCounts;
805 std::map<std::string, int32_t> stackStarts;
808 ||
static_cast<uint64_t
>( sec14->
count ) * stride >
static_cast<uint64_t
>(
m_data.size() - start ) )
815 for(
size_t k = 0; k < sec14->
count; ++k )
817 size_t off = start + k * stride;
822 table.emplace_back();
826 std::string
name = rec.
Str( 0, 40 );
829 if( off + COUNT_OFFSET + 4 <=
m_data.size() )
831 int32_t count = rec.
I32( COUNT_OFFSET );
832 int32_t
cursor = rec.
I32( START_OFFSET );
833 int32_t stackCount = rec.
I32( 88 );
835 if( !
name.empty() && count > 0 && count <= 1000 )
837 counts.emplace(
name,
static_cast<uint32_t
>( count ) );
846 if( stackCount > 0 && stackCount <= 1000 )
848 stackCounts.emplace(
name, stackCount );
856 int32_t stackStart = rec.
I32( STACK_START_OFFSET );
858 if( stackStart >= 0 )
859 stackStarts.emplace(
name, stackStart );
867 if(
table.empty() ||
table[0] !=
"JMPVIA_AAAAA" )
893 if( !sec17 || sec17->
count == 0 )
897 THROW_IO_ERROR(
"Invalid PADS parttype-controller record extent" );
901 if( recSize != 208 && recSize != 224 )
902 THROW_IO_ERROR(
"Unsupported PADS parttype-controller record stride" );
904 const bool isLegacyLayout = recSize == 208;
905 const uint32_t decalOff = isLegacyLayout ? 112 : 96;
915 const uint32_t nameOff = 44;
917 if( !
m_cursor.InBounds( start,
static_cast<size_t>( sec17->
count ) * recSize ) )
926 for( uint32_t k = 0; k < sec17->
count; ++k )
928 uint32_t off = start + k * recSize;
931 std::vector<int32_t> decalIndices;
933 for( uint32_t indexOff = decalOff; !isLegacyLayout && indexOff + 8 <= recSize; indexOff += 8 )
935 int32_t decalIndex = record.
I32( indexOff );
937 if( decalIndex < 0 || record.
I32( indexOff + 4 ) != decalIndex )
940 decalIndices.push_back( decalIndex );
943 if( isLegacyLayout && record.
I32( decalOff ) >= 0 )
944 decalIndices.push_back( record.
I32( decalOff ) );
979 if( section->
count == 0 )
982 const size_t stride =
m_version <= 0x2019 ? 20 : 36;
984 const size_t bytes =
static_cast<size_t>( section->
count ) * stride;
989 struct DIRECT_TERMINAL
996 std::vector<DIRECT_TERMINAL> terminals;
997 terminals.reserve( section->
count );
1004 terminals.push_back( { record.
I32( 4 ), record.
I32( 8 ), {} } );
1006 terminals.push_back( { record.
I32( 0 ), record.
I32( 4 ), record.
Str( 20, 4 ) } );
1019 const size_t start =
static_cast<size_t>( startIt->second );
1020 const size_t count = countIt->second;
1022 if( start > terminals.size() || count > terminals.size() - start )
1025 decal.terminals.clear();
1026 decal.terminals.reserve( count );
1030 const DIRECT_TERMINAL& serialized = terminals[start +
index];
1034 terminal.
name = serialized.name.empty() ? std::to_string(
index + 1 ) : serialized.name;
1035 decal.terminals.push_back( std::move( terminal ) );
1041 size_t pairCount = 0;
1049 pairCount = std::max( pairCount,
static_cast<size_t>( start ) +
static_cast<size_t>( countIt->second ) );
1053 const size_t pairBase = base +
bytes - (
m_version <= 0x2019 ? 16 : 0 );
1057 if( pairBase >
m_data.size() || pairCount * 8 >
m_data.size() - pairBase )
1060 std::vector<std::pair<int32_t, int32_t>> pairs;
1061 pairs.reserve( pairCount );
1067 pairs.emplace_back( record.
I32( 0 ), record.
I32( 4 ) );
1090 if( decal.terminals.empty() )
1105 int32_t aStart, int32_t aCount )
1107 if( aStart < 0 || aCount <= 0 ||
static_cast<size_t>( aStart ) +
static_cast<size_t>( aCount ) > aPairs.size() )
1110 for( int32_t p = 0; p < aCount; ++p )
1112 const std::pair<int32_t, int32_t>& pair = aPairs[
static_cast<size_t>( aStart ) + p];
1114 if( pair.first < 0 ||
static_cast<size_t>( pair.second ) >=
m_padStackPool.size()
1120 if( pair.first > 0 &&
static_cast<size_t>( pair.first ) > aDecal.
terminals.size() )
1131 return !aName.empty() && aName !=
"___Unassigned_Obstacles_";
1146 std::unordered_set<std::string> existing;
1150 const uint32_t netRecordSize =
m_version == 0x2024 ? 416 : 424;
1151 constexpr uint32_t NET_NAME = 76;
1152 constexpr uint32_t NET_NAME_LEN = 48;
1153 constexpr uint32_t NET_SELF_PTR = 144;
1154 constexpr uint32_t NET_CLASS_PTR = 148;
1161 if( nets->
count == 0 )
1172 for( uint32_t i = 0; i < nets->
count; ++i )
1174 SDB_RECORD rec =
m_sdb.RecordAt( base +
static_cast<size_t>( i ) * netRecordSize );
1175 std::string
name = rec.
Str( NET_NAME, NET_NAME_LEN );
1187 if( uint32_t owner = rec.
U32( NET_CLASS_PTR ) )
1190 if( uint32_t selfPtr = rec.
U32( NET_SELF_PTR ) )
1200 constexpr size_t RECORD_SIZE = 68;
1201 constexpr uint32_t MARKER = 0xFE000000;
1202 constexpr uint32_t FLAG = 0x0000FFFE;
1207 const size_t netRecordSize =
m_version == 0x2024 ? 416 : 424;
1209 if( !connections || !nets )
1212 if( connections->
count == 0 )
1215 if( nets->
stride != netRecordSize )
1218 constexpr size_t RING_PREFIX = 36;
1226 auto connectionU32 = [&]( uint32_t aIndex,
size_t aField )
1229 connections->
physicalOffset - RING_PREFIX +
static_cast<size_t>( aIndex ) * RECORD_SIZE + aField;
1233 size_t runBase =
static_cast<size_t>( connections->
physicalOffset ) - RING_PREFIX;
1234 size_t runCount =
static_cast<size_t>( connections->
count ) + 1;
1239 for( uint32_t recordIndex = 0; recordIndex < runCount; ++recordIndex )
1241 if( ( recordIndex > 0 && ( connectionU32( recordIndex, 20 ) & 0xFFFFFFC0U ) != MARKER )
1242 || ( recordIndex < connections->count && ( connectionU32( recordIndex, 52 ) & 0xFFFFU ) != FLAG ) )
1250 using PIN_ID = std::pair<uint32_t, uint32_t>;
1252 std::map<PIN_ID, size_t> pinIndex;
1253 std::vector<PIN_ID> pins;
1254 std::vector<size_t> parent;
1256 auto intern = [&](
const PIN_ID& aPin )
1258 auto [it, inserted] = pinIndex.emplace( aPin, parent.size() );
1262 pins.push_back( aPin );
1263 parent.push_back( parent.size() );
1269 auto findRoot = [&](
size_t aNode )
1271 while( parent[aNode] != aNode )
1273 parent[aNode] = parent[parent[aNode]];
1274 aNode = parent[aNode];
1280 auto join = [&](
const PIN_ID& aPin,
const PIN_ID& bPin )
1282 const size_t indexA = intern( aPin );
1283 const size_t indexB = intern( bPin );
1284 const size_t rootA = findRoot( indexA );
1285 const size_t rootB = findRoot( indexB );
1287 if( rootA != rootB )
1288 parent[rootA] = rootB;
1291 for( uint32_t recordIndex = 0; recordIndex + 1 < runCount; ++recordIndex )
1293 join( { connectionU32( recordIndex, 60 ), connectionU32( recordIndex + 1, 0 ) },
1294 { connectionU32( recordIndex, 64 ), connectionU32( recordIndex + 1, 4 ) } );
1297 std::map<size_t, size_t> componentNet;
1304 const size_t root = findRoot( intern( {
anchor.placementObject,
anchor.terminalOrdinal } ) );
1305 auto [it, inserted] = componentNet.emplace( root,
anchor.netIndex );
1307 if( !inserted && it->second !=
anchor.netIndex )
1311 const bool existingIsAlias =
m_nets[it->second].name.rfind(
"$$$", 0 ) == 0;
1312 const bool incomingIsAlias =
m_nets[
anchor.netIndex].name.rfind(
"$$$", 0 ) == 0;
1314 if( existingIsAlias && incomingIsAlias )
1316 else if( existingIsAlias != incomingIsAlias )
1317 it->second = incomingIsAlias ? it->second :
anchor.netIndex;
1326 auto netIt = componentNet.find( findRoot(
index ) );
1328 if( netIt == componentNet.end() )
1331 const auto [placementObject, terminalOrdinal] = pins[
index];
1337 m_nets[netIt->second].component_refs.push_back(
m_parts[partIt->second].name );
1344 using PIN_KEY = std::pair<std::string, std::string>;
1346 std::map<PIN_KEY, size_t> pinOwners;
1361 if( decalIt ==
m_decals.end() ||
anchor.terminalOrdinal > decalIt->second.terminals.size() )
1364 const std::string& terminalName = decalIt->second.terminals[
anchor.terminalOrdinal - 1].name;
1366 if( terminalName.empty() )
1369 PIN_KEY key = std::make_pair( part.
name, terminalName );
1370 auto [owner, inserted] = pinOwners.emplace( key,
anchor.netIndex );
1372 if( inserted || owner->second ==
anchor.netIndex )
1376 const bool existingIsAlias =
m_nets[owner->second].name.rfind(
"$$$", 0 ) == 0;
1377 const bool incomingIsAlias =
m_nets[
anchor.netIndex].name.rfind(
"$$$", 0 ) == 0;
1379 if( existingIsAlias && !incomingIsAlias )
1380 owner->second =
anchor.netIndex;
1381 else if( !existingIsAlias && incomingIsAlias )
1383 else if( existingIsAlias && incomingIsAlias )
1386 THROW_IO_ERROR(
"Conflicting PADS net ownership for pin '" + part.
name +
"." + terminalName +
"'" );
1395 for(
const auto& [key, netIndex] : pinOwners )
1398 pin.ref_des = key.first;
1399 pin.pin_name = key.second;
1400 m_nets[netIndex].pins.push_back( std::move(
pin ) );
1407 std::unordered_set<std::string> existing;
1408 std::unordered_map<std::string, size_t> netIndexByName;
1418 for(
size_t i = 0; i < netOffsets.size(); ++i )
1420 SDB_RECORD rec =
m_sdb.RecordAt(
static_cast<uint32_t
>( netOffsets[i] ) );
1421 std::string
name = rec.
Str( 12, 48 );
1428 if( existing.insert(
name ).second )
1433 netIndexByName.emplace(
name,
m_nets.size() - 1 );
1438 if( uint32_t owner = rec.
U32( 84 ) )
1448 constexpr uint32_t NET_RECORD_SIZE = 144;
1450 std::vector<size_t> offsets;
1453 if( !nets || nets->
stride != NET_RECORD_SIZE )
1456 const uint64_t base =
static_cast<uint64_t
>( nets->
physicalOffset ) + 20;
1458 if( base +
static_cast<uint64_t
>( nets->
count ) * NET_RECORD_SIZE >
m_data.size() )
1461 for(
size_t i = 0; i < nets->
count; ++i )
1462 offsets.push_back( base + i * NET_RECORD_SIZE );
1470 constexpr size_t RECORD_SIZE = 68;
1471 constexpr uint32_t ENDPOINT_FLAG = 0x0000FFFE;
1472 constexpr uint32_t MARKER = 0xFE000000;
1473 constexpr uint32_t NET_RECORD_SIZE = 144;
1474 constexpr uint32_t NET_FIRST = 8;
1475 constexpr uint32_t NET_NAME = 12;
1476 constexpr uint32_t NET_COUNT = 92;
1481 if( !connections || !nets )
1482 THROW_IO_ERROR(
"Missing PADS legacy net-connection controllers" );
1484 if( connections->
count == 0 )
1487 if( nets->
stride != NET_RECORD_SIZE )
1490 const uint64_t base =
static_cast<uint64_t
>( connections->
physicalOffset ) + 16;
1492 if( base +
static_cast<uint64_t
>( connections->
count ) * RECORD_SIZE >
m_data.size() )
1497 const size_t total = connections->
count;
1501 std::map<std::pair<uint32_t, uint32_t>,
size_t> pinIndex;
1502 std::vector<size_t> parent;
1504 auto intern = [&]( uint32_t aObject, uint32_t aOrdinal )
1506 auto [it, inserted] = pinIndex.emplace( std::make_pair( aObject, aOrdinal ), parent.size() );
1509 parent.push_back( parent.size() );
1514 auto findRoot = [&](
size_t aNode )
1516 while( parent[aNode] != aNode )
1518 parent[aNode] = parent[parent[aNode]];
1519 aNode = parent[aNode];
1534 std::vector<CONNECTION> stream;
1535 stream.reserve( total );
1537 for(
size_t i = 0; i < total; ++i )
1539 SDB_RECORD rec =
m_sdb.RecordAt(
static_cast<uint32_t
>( base + i * RECORD_SIZE ) );
1541 if( ( rec.
U32( 0 ) & 0xFFFFU ) != ENDPOINT_FLAG || ( rec.
U32( 36 ) & 0xFFFFFFC0U ) != MARKER )
1543 THROW_IO_ERROR(
"Invalid PADS legacy net-connection ring framing" );
1546 CONNECTION conn{ rec.
U32( 8 ), rec.
U32( 16 ), rec.
U32( 12 ), rec.
U32( 20 ), 0 };
1548 conn.pinA = intern( conn.objectA, conn.ordinalA );
1549 size_t pinB = intern( conn.objectB, conn.ordinalB );
1551 size_t rootA = findRoot( conn.pinA );
1552 size_t rootB = findRoot( pinB );
1554 if( rootA != rootB )
1555 parent[rootA] = rootB;
1557 stream.push_back( conn );
1560 std::map<size_t, size_t> componentSize;
1562 for(
size_t i = 0; i < stream.size(); ++i )
1564 size_t root = findRoot( stream[i].
pinA );
1566 ++componentSize[root];
1569 auto netRecordName = [&](
size_t aOffset, std::string& aName, uint32_t& aCount, uint32_t& aFirst,
1570 uint32_t& aAnchorObject, uint32_t& aAnchorOrdinal )
1572 if( !
m_cursor.InBounds( aOffset, NET_RECORD_SIZE ) )
1577 aName = rec.
Str( NET_NAME, 48 );
1578 aCount = rec.
U32( NET_COUNT );
1579 aFirst = rec.
U32( NET_FIRST );
1580 aAnchorObject = rec.
U32( 0 );
1581 aAnchorOrdinal = rec.
U32( 4 );
1583 return !aName.empty() && aCount > 0 && aCount <= connections->
count;
1586 std::map<std::string, size_t> netIndexByName;
1588 for(
size_t i = 0; i <
m_nets.size(); ++i )
1589 netIndexByName.emplace(
m_nets[i].name, i );
1591 std::set<size_t> assignedComponents;
1597 uint32_t edgeIndex = 0;
1598 uint32_t anchorObject = 0;
1599 uint32_t anchorOrdinal = 0;
1601 if( !netRecordName( offset,
name, count, edgeIndex, anchorObject, anchorOrdinal ) || !
isValidNetName(
name ) )
1604 if( edgeIndex >= stream.size() )
1607 auto anchor = pinIndex.find( { anchorObject, anchorOrdinal } );
1609 if(
anchor == pinIndex.end() )
1612 const size_t root = findRoot(
anchor->second );
1614 if( findRoot( stream[edgeIndex].
pinA ) != root )
1617 if( count != componentSize[root] )
1620 if( !assignedComponents.insert( root ).second )
1621 THROW_IO_ERROR(
"Conflicting PADS legacy net component owners" );
1623 auto netIt = netIndexByName.find(
name );
1625 if( netIt == netIndexByName.end() )
1628 for(
const CONNECTION& conn : stream )
1630 if( findRoot( conn.pinA ) != root )
1646 std::set<uint32_t> ownerSet;
1649 ownerSet.insert( owner );
1651 std::vector<uint32_t> owners( ownerSet.begin(), ownerSet.end() );
1652 std::map<uint32_t, size_t> ownerOrdinal;
1654 for(
size_t k = 0; k < owners.size(); ++k )
1655 ownerOrdinal[owners[k]] = k;
1662 const size_t NAME_STRIDE =
m_version <= 0x2022 ? 28 : 280;
1663 constexpr size_t NAME_OFFSET = 8;
1664 const size_t NAME_LEN =
m_version <= 0x2022 ? 8 : 48;
1668 ||
static_cast<uint64_t
>( names->
physicalOffset ) + owners.size() * NAME_STRIDE >
m_data.size() )
1676 for(
size_t k = 0; k < owners.size(); ++k )
1680 if(
name.empty() || !std::isalnum(
static_cast<unsigned char>(
name[0] ) ) )
1681 name =
"PADS_NetClass_" + std::to_string( k + 1 );
1688 auto it = ownerOrdinal.find( owner );
1690 if( it != ownerOrdinal.end() )
1699 std::sort( nc.nets.begin(), nc.nets.end() );
1700 std::sort( nc.ruleLayers.begin(), nc.ruleLayers.end() );
1701 nc.ruleLayers.erase( std::unique( nc.ruleLayers.begin(), nc.ruleLayers.end() ), nc.ruleLayers.end() );
1708 constexpr size_t RECORD_SIZE = 28;
1709 constexpr size_t RULE_KIND = 0;
1710 constexpr size_t RULE_DETAIL_HANDLE = 4;
1711 constexpr size_t SCOPE_TYPE = 8;
1712 constexpr uint32_t NET_CLASS_SCOPE = 0x42;
1713 constexpr size_t SCOPE_REFERENCE = 12;
1714 constexpr size_t LAYER = 24;
1716 std::vector<NET_CLASS_RULE_EDGE> edges;
1719 if( !relationships || relationships->
physicalBytes != relationships->
count * RECORD_SIZE )
1722 for( uint32_t i = 0; i < relationships->
count; ++i )
1724 size_t off = relationships->
physicalOffset +
static_cast<size_t>( i ) * RECORD_SIZE;
1727 if( rec.
U32( SCOPE_TYPE ) != NET_CLASS_SCOPE )
1730 uint32_t owner = rec.
U32( SCOPE_REFERENCE );
1732 if( !aOwnerSet.count( owner ) )
1735 edges.push_back( { owner, rec.
U32( RULE_KIND ), rec.
U32( RULE_DETAIL_HANDLE ),
1736 static_cast<int>( rec.
U32(
LAYER ) ), off } );
1744 const std::map<uint32_t, size_t>& aOwnerOrdinal )
1746 constexpr uint32_t CLEARANCE_RULE_KIND = 0x29;
1750 if( e.ruleKind != CLEARANCE_RULE_KIND )
1753 auto owner = aOwnerOrdinal.find( e.owner );
1755 if( owner != aOwnerOrdinal.end() )
1756 m_netClasses[owner->second].ruleLayers.push_back( e.layer );
1762 if( !relationships || !values || values->
physicalCount == 0 )
1765 constexpr size_t RELATIONSHIP_SIZE = 28;
1766 uint32_t firstClearanceHandle = UINT32_MAX;
1768 for( uint32_t i = 0; i < relationships->
count; ++i )
1772 if( rec.
U32( 0 ) == CLEARANCE_RULE_KIND )
1773 firstClearanceHandle = std::min( firstClearanceHandle, rec.
U32( 4 ) );
1776 if( firstClearanceHandle == UINT32_MAX )
1779 const uint32_t valueStride =
m_version == 0x2017 ? 180 : 188;
1783 if( edge.ruleKind != CLEARANCE_RULE_KIND || edge.layer != 0 || edge.rulePtr < firstClearanceHandle )
1786 uint32_t
delta = edge.rulePtr - firstClearanceHandle;
1788 if(
delta % valueStride != 0 )
1796 auto owner = aOwnerOrdinal.find( edge.owner );
1798 if( owner == aOwnerOrdinal.end() )
1816 constexpr size_t OBJECT_SIZE = 864;
1817 constexpr double F64_INHERIT = -1.0;
1818 constexpr int32_t I32_INHERIT = -1;
1821 constexpr size_t NET_A_HANDLE = 12;
1822 constexpr size_t NET_B_HANDLE = 16;
1823 constexpr size_t GAP_INHERIT = 40;
1824 constexpr size_t GAP_OVERRIDE = 56;
1825 constexpr size_t WIDTH_INHERIT = 592;
1826 constexpr size_t WIDTH_OVERRIDE = 600;
1841 if(
static_cast<uint64_t
>( base ) +
static_cast<uint64_t
>( records->
physicalCount ) * OBJECT_SIZE >
m_data.size() )
1844 std::set<std::pair<std::string, std::string>> seen;
1848 size_t objStart = base +
static_cast<size_t>( i ) * OBJECT_SIZE;
1860 if( !seen.insert( { nameA, nameB } ).second )
1863 double gapOverride = obj.F64( GAP_OVERRIDE );
1864 double gap = ( gapOverride != F64_INHERIT ) ? gapOverride : obj.F64( GAP_INHERIT );
1865 int32_t widthOverride = obj.I32( WIDTH_OVERRIDE );
1866 double width = ( widthOverride != I32_INHERIT ) ?
static_cast<double>( widthOverride )
1867 :
static_cast<double>( obj.I32( WIDTH_INHERIT ) );
1870 dp.
name = nameA +
"_" + nameB;
1873 dp.
gap = ( gap != F64_INHERIT ) ? gap : 0.0;
1874 dp.
width = ( width !=
static_cast<double>( I32_INHERIT ) ) ? width : 0.0;
1886 constexpr size_t GEOMETRY_BYTES = 36;
1887 const size_t recordSize = s8 ? s8->
stride : 0;
1888 const size_t ringRotation = recordSize >= GEOMETRY_BYTES ? recordSize - GEOMETRY_BYTES : 0;
1896 const bool validStringPool =
1899 if( ( recordSize != 64 && recordSize != 72 ) || s8->
physicalOffset < ringRotation || !validStringPool
1916 parseFreeText( *s8, recordBase, recordSize, ringRotation, poolBase, poolHi );
1921 size_t aRingRotation )
1923 auto fieldAttribute = [&]( uint32_t aIndex )
1925 SDB_RECORD record =
m_sdb.RecordAt( aRecordBase +
static_cast<size_t>( aIndex ) * aRecordSize );
1927 attribute.
height = record.
I32( aRingRotation );
1928 attribute.
width = record.
I32( aRingRotation + 4 );
1929 attribute.
x = record.
I32( aRingRotation + 8 );
1930 attribute.
y = record.
I32( aRingRotation + 12 );
1932 attribute.
mirrored = record.
I32( aRingRotation + 20 ) != 0;
1938 if( fieldStart < 0 )
1941 if( partIndex >=
m_parts.size() ||
static_cast<uint32_t
>( fieldStart ) >= aText.
physicalCount )
1942 THROW_IO_ERROR(
"Invalid PADS placement field-presentation link" );
1944 std::vector<ATTRIBUTE> attributes;
1945 std::set<uint32_t> visited;
1946 uint32_t fieldIndex =
static_cast<uint32_t
>( fieldStart );
1950 if( !visited.insert( fieldIndex ).second )
1956 ATTRIBUTE attribute = fieldAttribute( fieldIndex );
1957 SDB_RECORD metadata =
m_sdb.RecordAt( aRecordBase +
static_cast<size_t>( fieldIndex + 1 ) * aRecordSize );
1958 uint32_t presentation = metadata.
U32( 24 );
1959 uint8_t fieldKind =
static_cast<uint8_t
>( presentation >> 16 );
1960 attribute.
visible = ( fieldKind & 0x20 ) != 0;
1962 if( ( presentation & 0x11000000U ) == 0x11000000U )
1964 attribute.
hjust =
"CENTER";
1965 attribute.
vjust =
"CENTER";
1969 attribute.
hjust =
"LEFT";
1970 attribute.
vjust = ( presentation & 0x20000000U ) ?
"UP" :
"DOWN";
1973 if( ( fieldKind & 0x1FU ) == 0x03 )
1974 attribute.
name =
"Part Type";
1975 else if( ( fieldKind & 0x1FU ) == 0x02 )
1976 attribute.
name =
"Ref.Des.";
1978 if( !attribute.
name.empty() )
1979 attributes.push_back( std::move( attribute ) );
1981 uint32_t association = metadata.
U32( 12 );
1983 uint8_t associationKind =
static_cast<uint8_t
>( association >> 24 );
1985 if( associationKind == 0x16 )
1987 if( ( association & 0x00FFFFFFU ) != partIndex )
1993 if( associationKind != 0x08 )
1994 THROW_IO_ERROR(
"Invalid PADS placement field-list terminator" );
1996 fieldIndex = association & 0x00FFFFFFU;
1999 m_parts[partIndex].attributes = std::move( attributes );
2005 size_t aRingRotation,
size_t aPoolBase,
size_t aPoolHi )
2007 auto cStringStartAt = [
this, aPoolHi](
size_t aAbs ) ->
bool
2009 if( aAbs >= aPoolHi )
2014 while( e < aPoolHi &&
m_data[e] != 0 )
2028 SDB_RECORD meta =
m_sdb.RecordAt( aRecordBase +
static_cast<size_t>(
index + 1 ) * aRecordSize );
2030 if( meta.
U16( 4 ) != 0xFFFE || meta.
U32( 12 ) != 0 || ( aRecordSize == 72 && meta.
U32( 28 ) != 0x49000000 ) )
2033 uint32_t layerWord = meta.
U32( 24 );
2035 if( ( layerWord >> 16 ) != 0x0020 || geom.
I32( aRingRotation ) <= 0 || geom.
I32( aRingRotation + 4 ) <= 0 )
2038 size_t soff = aPoolBase + meta.
U32( 8 );
2040 if( soff < aPoolBase || soff >= aPoolHi || ( soff != aPoolBase &&
m_data[soff - 1] != 0 )
2041 || !cStringStartAt( soff ) )
2046 std::string content =
m_sdb.RecordAt( soff ).Str( 0, aPoolHi - soff );
2048 if( content.empty() )
2051 int32_t height = geom.
I32( aRingRotation );
2052 int32_t linewidth = geom.
I32( aRingRotation + 4 );
2053 int32_t x = geom.
I32( aRingRotation + 8 );
2054 int32_t y = geom.
I32( aRingRotation + 12 );
2055 int32_t angleRaw = geom.
I32( aRingRotation + 16 );
2058 text.content = content;
2061 text.height =
static_cast<double>( height );
2062 text.width =
static_cast<double>( linewidth );
2063 text.layer =
static_cast<int>( layerWord & 0xFF );
2065 text.mirrored = geom.
I32( aRingRotation + 20 ) != 0;
2099 return aCur.
U8At( aTypeByte ) ==
VIA_TYPE && aCur.
U8At( aTypeByte + 4 ) == 0x17
2100 && ( aCur.
U8At( aTypeByte + 5 ) & 0x02 ) != 0;
2106 constexpr uint32_t JUNCTION_TAG = 0x3C000000;
2107 constexpr uint32_t ROUTE_CHAIN_TAG = 0x18000000;
2108 constexpr uint32_t TAG_MASK = 0xFF000000;
2109 constexpr uint32_t INDEX_MASK = 0x00FFFFFF;
2116 if( !relationships || !junctions || !nets || !routeChains )
2119 std::map<uint32_t, size_t>
result;
2120 std::vector<std::pair<uint32_t, size_t>> junctionSignals;
2123 size_t signalIndex = 0;
2125 auto readU32 = [&]()
2137 if( signalIndex >= nets->
count )
2138 THROW_IO_ERROR(
"Invalid PADS route-relationship signal count" );
2140 for(
int direction = 0; direction < 2; ++direction )
2142 uint32_t numRelationships = readU32();
2144 for( uint32_t relationship = 0; relationship < numRelationships; ++relationship )
2146 uint32_t objectId = readU32();
2147 uint32_t numValues = readU32();
2152 uint32_t expectedObjectTag = direction == 0 ? JUNCTION_TAG : ROUTE_CHAIN_TAG;
2153 uint32_t objectIndex = objectId & INDEX_MASK;
2154 uint32_t objectLimit = direction == 0 ? junctions->
count : routeChains->
count;
2156 if( ( objectId & TAG_MASK ) != expectedObjectTag || objectIndex >= objectLimit )
2157 THROW_IO_ERROR(
"Invalid PADS route-relationship object identifier" );
2159 if( direction == 0 )
2161 junctionSignals.emplace_back( objectIndex, signalIndex );
2164 uint32_t expectedValueTag = direction == 0 ? ROUTE_CHAIN_TAG : JUNCTION_TAG;
2165 uint32_t valueLimit = direction == 0 ? routeChains->
count : junctions->
count;
2167 for( uint32_t valueIndex = 0; valueIndex < numValues; ++valueIndex )
2169 uint32_t value = readU32();
2171 if( ( value & TAG_MASK ) != expectedValueTag || ( value & INDEX_MASK ) >= valueLimit )
2172 THROW_IO_ERROR(
"Invalid PADS route-relationship member identifier" );
2183 for(
const auto& [junctionIndex, sourceSignalIndex] : junctionSignals )
2185 std::optional<size_t> netIndex;
2187 if( signalIndex == nets->
count )
2192 netIndex = netIt->second;
2196 netIndex = sourceSignalIndex;
2202 auto [it, inserted] =
result.emplace( junctionIndex, *netIndex );
2204 if( !inserted && it->second != *netIndex )
2205 THROW_IO_ERROR(
"Conflicting PADS route-junction relationship" );
2222 if( entry60->
count == 0 )
2231 for(
auto& [netName, route] : routes )
2233 if( route.tracks.empty() && route.vias.empty() )
2236 m_routes.push_back( std::move( route ) );
2250 if( entry60->
stride == 0 )
2253 uint32_t stride = entry60->
stride;
2255 if( stride < 31 || entry60->physicalCount != entry60->
count )
2260 std::vector<size_t> typeBytes;
2261 const size_t ringRotation = stride - 31;
2268 for( uint32_t rec = 0; rec < entry60->
physicalCount; ++rec )
2269 typeBytes.push_back( base +
static_cast<size_t>( rec ) * stride + stride - 4 );
2275 if( typeBytes.empty() )
2280 for(
size_t junction = 0; junction < typeBytes.size(); ++junction )
2282 auto netIt = junctionNets.find(
static_cast<uint32_t
>( junction ) );
2284 if( netIt == junctionNets.end() )
2287 if( netIt->second >=
m_nets.size() || typeBytes[junction] < 27 )
2290 const size_t typeByte = typeBytes[junction];
2291 uint32_t objectHandle =
m_cursor.U32At( typeByte - 19 );
2293 if( objectHandle != 0 )
2297 std::vector<VIA_LOCATION> viaLocations;
2299 for(
size_t junction = 0; junction < typeBytes.size(); ++junction )
2301 size_t typeByte = typeBytes[junction];
2302 uint8_t type =
m_cursor.U8At( typeByte );
2308 if( typeByte < 27 || !
m_cursor.InBounds( typeByte - 27, 8 ) )
2311 int32_t vx =
m_cursor.I32At( typeByte - 27 );
2312 int32_t vy =
m_cursor.I32At( typeByte - 23 );
2314 std::string netName;
2315 uint32_t netIdx =
m_cursor.U16At( typeByte + 1 );
2317 auto relationshipIt = junctionNets.find(
static_cast<uint32_t
>( junction ) );
2319 if( relationshipIt != junctionNets.end() && relationshipIt->second <
m_nets.size() )
2321 netName =
m_nets[relationshipIt->second].name;
2325 netName = it->second;
2331 via.netName = std::move( netName );
2333 via.relationshipNet = relationshipIt != junctionNets.end();
2334 viaLocations.push_back( std::move(
via ) );
2339 std::map<std::pair<int32_t, int32_t>,
VIA_LOCATION> uniqueVias;
2343 auto [it, inserted] = uniqueVias.try_emplace( std::make_pair(
via.x,
via.y ) );
2347 it->second = std::move(
via );
2356 if(
via.relationshipNet )
2359 THROW_IO_ERROR(
"Conflicting co-located PADS via relationships" );
2373 std::vector<VIA_LOCATION> deduplicated;
2374 deduplicated.reserve( uniqueVias.size() );
2376 for(
auto& [coordinate,
via] : uniqueVias )
2377 deduplicated.push_back( std::move(
via ) );
2379 return deduplicated;
2385 std::map<std::string, ROUTE> routes;
2389 ROUTE& route = routes[
via.netName];
2404 auto stackIt = decalIt->second.pad_stacks.find( 1 );
2406 if( stackIt == decalIt->second.pad_stacks.end() )
2407 stackIt = decalIt->second.pad_stacks.find( 0 );
2409 if( stackIt != decalIt->second.pad_stacks.end() && !stackIt->second.empty() )
2411 viaDef.
stack = stackIt->second;
2413 auto spanIt = decalIt->second.drill_spans.find( stackIt->first );
2415 if( spanIt != decalIt->second.drill_spans.end() && spanIt->second.first > 0
2416 && spanIt->second.second > 0 )
2421 int spanStart = std::min( spanIt->second.first, spanIt->second.second );
2422 int spanEnd = std::max( spanIt->second.first, spanIt->second.second );
2434 if( viaDef.
stack.empty() )
2438 : std::string(
"<out-of-range>" );
2440 wxString::Format(
"Invalid PADS via padstack reference %d (%s)",
via.viaIndex,
name.c_str() ) );
2443 route.
vias.push_back( std::move( viaDef ) );
2456 if( !routeObjects || !routeLayers || !routeCells )
2459 if( routeLayers->
count == 0 ||
static_cast<uint64_t
>( routeLayers->
count ) * 2 != routeLayers->
totalBytes )
2464 if(
static_cast<uint64_t
>( routeCells->
count ) * 12 != routeCells->
totalBytes )
2467 if( routeObjects->
count == 0 )
2475 if( ( routeObjects->
stride != 36 && routeObjects->
stride != 48 )
2476 ||
static_cast<uint64_t
>( routeObjects->
count ) * routeObjects->
stride != routeObjects->
totalBytes )
2485 uint32_t cellCount = 0;
2490 std::vector<int> serializedLayerOrder;
2491 bool legacyObjects = routeObjects->
stride == 36;
2492 size_t objectStride = legacyObjects ? 36 : 48;
2493 size_t widthOffset = legacyObjects ? 8 : 20;
2494 size_t cellCountOffset = legacyObjects ? 24 : 36;
2496 auto ringU32 = [&](
size_t aRingStart,
size_t aOffset )
2500 for(
size_t byte = 0;
byte < 4; ++byte )
2502 size_t physical = aRingStart + ( aOffset + byte ) % objectBytes;
2503 value |=
static_cast<uint32_t
>(
m_cursor.U8At( physical ) ) << (
byte * 8 );
2509 std::vector<bool> seenLayers( routeLayers->
physicalCount,
false );
2512 for( uint32_t layer = 0; layer < routeLayers->
physicalCount; ++layer )
2514 uint16_t serializedLayer =
m_cursor.U16At( layerTable +
static_cast<size_t>( layer ) * 2 );
2516 if( serializedLayer >= routeLayers->
physicalCount || seenLayers[serializedLayer] )
2519 seenLayers[serializedLayer] =
true;
2520 serializedLayerOrder.push_back( serializedLayer );
2523 if( layerTable == 0 )
2527 std::vector<ROUTE_OBJECT> objects;
2531 size_t base = ( 32 +
static_cast<size_t>( i ) * objectStride ) % objectBytes;
2532 ROUTE_OBJECT object;
2534 object.width =
static_cast<int32_t
>(
ringU32( ringStart, base + widthOffset ) ) * 4;
2535 object.style =
ringU32( ringStart, base + ( legacyObjects ? 20 : 32 ) );
2536 object.cellCount =
ringU32( ringStart, base + cellCountOffset );
2537 objects.push_back(
object );
2548 std::vector<ROUTE_CELL> cells;
2550 for( uint32_t i = 0; i < routeCells->
count; ++i )
2552 size_t base = cellStart +
static_cast<size_t>( i ) * 12;
2553 int32_t first =
m_cursor.I32At( base );
2554 int32_t second =
m_cursor.I32At( base + 4 );
2555 int32_t third =
m_cursor.I32At( base + 8 );
2557 cells.push_back( { first, second, third } );
2562 struct DECODED_TRACK
2565 std::string netName;
2568 std::vector<DECODED_TRACK> decodedPieces;
2574 const std::vector<int>& objectLayers = nodes.
layers;
2575 const std::vector<uint32_t>& objectHandles = nodes.
handles;
2576 const std::map<uint32_t, std::set<size_t>>& routeNodeNets = nodes.
handleNets;
2580 size_t objectIndex = 0;
2581 size_t cellStart = 0;
2584 std::vector<ROUTE_CHUNK> chunks;
2589 for(
size_t sequence = 0; sequence < objects.size(); ++sequence )
2591 size_t match = ( sequence + objects.size() - 1 ) % objects.size();
2593 if(
cursor + objects[match].cellCount > cells.size() )
2596 const ROUTE_OBJECT&
object = objects[match];
2601 if(
object.width > 0 && (
object.style & 0x1100 ) == 0 )
2602 chunks.push_back( { match,
cursor } );
2604 cursor +=
object.cellCount;
2607 if(
cursor != cells.size() )
2610 for(
const ROUTE_CHUNK& chunk : chunks )
2612 const ROUTE_OBJECT&
object = objects[chunk.objectIndex];
2615 track.
layer = objectLayers[chunk.objectIndex];
2616 track.
width =
object.width;
2623 if( routingDirection < 0 || routingDirection > 4 )
2626 bool fixedIsX = routingDirection == 1;
2628 for(
size_t j = 0; j <
object.cellCount; ++j )
2630 const ROUTE_CELL& cell = cells[chunk.cellStart + j];
2631 double x1 = fixedIsX ? cell.x1 : cell.y;
2632 double y1 = fixedIsX ? cell.y : cell.x1;
2633 double x2 = fixedIsX ? cell.x2 : cell.y;
2634 double y2 = fixedIsX ? cell.y : cell.x2;
2636 if( track.
points.empty() || track.
points.back().x != x1 || track.
points.back().y != y1 )
2638 track.
points.emplace_back( x1, y1 );
2641 if( x1 != x2 || y1 != y2 )
2642 track.
points.emplace_back( x2, y2 );
2645 if( track.
points.size() < 2 )
2648 auto handleIt = routeNodeNets.find( objectHandles[chunk.objectIndex] );
2649 std::optional<size_t> netIndex;
2651 if( handleIt == routeNodeNets.end() )
2654 const std::set<size_t>& handleNets = handleIt->second;
2656 if( handleNets.size() > 1 )
2657 THROW_IO_ERROR( wxString::Format(
"Conflicting PADS route-object handle nets "
2658 "(object %zu, handle 0x%08X, nets %zu)",
2659 chunk.objectIndex, objectHandles[chunk.objectIndex],
2660 handleNets.size() ) );
2662 if( handleNets.size() == 1 )
2663 netIndex = *handleNets.begin();
2665 if( !netIndex || *netIndex >=
m_nets.size() )
2669 THROW_IO_ERROR( fmt::format(
"Missing PADS route-object net relationship "
2670 "(object {}, handle 0x{:08X}, layer {}, cells {}, "
2671 "first {:.0f},{:.0f}, last {:.0f},{:.0f})",
2672 chunk.objectIndex, objectHandles[chunk.objectIndex], track.
layer,
2673 object.cellCount, first.
x, first.
y, last.
x, last.
y ) );
2676 decodedPieces.push_back( { std::move( track ),
m_nets[*netIndex].name } );
2679 for( DECODED_TRACK&
decoded : decodedPieces )
2690 const std::vector<int>& aSerializedLayerOrder )
2693 nodes.
layers.assign( aObjectCount, 0 );
2694 nodes.
handles.assign( aObjectCount, 0 );
2696 auto padsLayerForSerializedIndex = [&](
size_t aIndex )
2698 return aIndex < aSerializedLayerOrder.size() ? aSerializedLayerOrder[aIndex] + 1
2699 :
static_cast<int>( aIndex ) + 1;
2708 if( !routeController || !allocatorDescriptors || !layerObjectCounts || !layerObjectHandles || !routeNodes
2715 std::array<uint16_t, 4> allocatorPageCounts;
2720 size_t nodePageStart =
2721 static_cast<size_t>( allocatorPageCounts[0] ) + allocatorPageCounts[1] + allocatorPageCounts[2];
2722 size_t nodePageCount = allocatorPageCounts[3];
2724 if( nodePageCount == 0 || nodePageStart + nodePageCount > allocatorDescriptors->
physicalCount )
2730 uint32_t liveCount = 0;
2731 size_t firstOrdinal = 0;
2734 std::vector<NODE_PAGE> nodePages;
2735 size_t allocatedNodes = 0;
2737 for(
size_t page = 0; page < nodePageCount; ++page )
2739 size_t descriptor = allocatorDescriptors->
physicalOffset + ( nodePageStart + page ) * 12;
2742 if( page + 1 < nodePageCount )
2743 liveCount =
m_cursor.U32At( descriptor + 20 );
2744 else if( allocatedNodes <= routeNodes->physicalCount )
2749 nodePages.push_back( {
m_cursor.U32At( descriptor ), liveCount, allocatedNodes } );
2750 allocatedNodes += liveCount;
2756 std::vector<uint32_t> routeNodeHandles;
2759 for(
const NODE_PAGE& page : nodePages )
2762 routeNodeHandles.push_back( page.base +
index * 56 );
2765 std::map<uint32_t, size_t> routeNodeOrdinals;
2767 for(
size_t ordinal = 0; ordinal < routeNodeHandles.size(); ++ordinal )
2768 routeNodeOrdinals.emplace( routeNodeHandles[ordinal], ordinal );
2770 std::vector<std::vector<size_t>> routeNodeLinks( routeNodeHandles.size() );
2772 for(
size_t ordinal = 0; ordinal < routeNodeHandles.size(); ++ordinal )
2776 for( uint32_t linkedHandle : {
m_cursor.U32At( nodeOffset ),
m_cursor.U32At( nodeOffset + 4 ) } )
2778 auto linkedIt = routeNodeOrdinals.find( linkedHandle );
2780 if( linkedIt == routeNodeOrdinals.end() )
2783 routeNodeLinks[linkedIt->second].push_back( ordinal );
2791 std::map<uint32_t, std::set<size_t>>& routeNodeNets = nodes.
handleNets;
2792 std::vector<uint32_t> visitedStamp( routeNodeHandles.size(), UINT32_MAX );
2793 std::vector<size_t> frontier;
2794 std::vector<size_t>
next;
2796 for(
size_t root = 0; root < routeNodeHandles.size(); ++root )
2798 std::set<size_t> componentNets;
2800 visitedStamp[root] =
static_cast<uint32_t
>( root );
2801 frontier.assign( 1, root );
2803 while( !frontier.empty() && componentNets.empty() )
2807 for(
size_t ordinal : frontier )
2812 componentNets.insert( netIt->second.begin(), netIt->second.end() );
2814 for(
size_t linked : routeNodeLinks[ordinal] )
2816 if( visitedStamp[linked] ==
static_cast<uint32_t
>( root ) )
2819 visitedStamp[linked] =
static_cast<uint32_t
>( root );
2820 next.push_back( linked );
2824 frontier.swap(
next );
2827 routeNodeNets.emplace( routeNodeHandles[root], std::move( componentNets ) );
2830 auto nodeOrdinal = [&]( uint32_t aHandle ) -> std::optional<size_t>
2832 auto it = routeNodeOrdinals.find( aHandle );
2834 if( it != routeNodeOrdinals.end() )
2837 return std::nullopt;
2840 std::vector<int> serializedObjectLayers;
2841 std::vector<uint32_t> serializedObjectHandles;
2842 std::map<uint32_t, int> routeHandleLayers;
2843 size_t handleOrdinal = 0;
2845 for(
size_t layer = 0; layer < layerObjectCounts->
physicalCount; ++layer )
2849 if( handleOrdinal + count > layerObjectHandles->
physicalCount )
2859 int padsLayer = padsLayerForSerializedIndex( layer );
2860 auto [layerIt, inserted] = routeHandleLayers.emplace( handle, padsLayer );
2862 if( !inserted && layerIt->second != padsLayer )
2865 auto ordinal = nodeOrdinal( handle );
2872 if( ( classTag & 0x00800000 ) != 0 )
2874 serializedObjectLayers.push_back( padsLayer );
2875 serializedObjectHandles.push_back( handle );
2880 if( handleOrdinal != layerObjectHandles->
physicalCount || serializedObjectLayers.size() != aObjectCount )
2885 for(
size_t sequence = 0; sequence < serializedObjectLayers.size(); ++sequence )
2887 size_t object = ( sequence + aObjectCount - 1 ) % aObjectCount;
2888 nodes.
layers[object] = serializedObjectLayers[sequence];
2889 nodes.
handles[object] = serializedObjectHandles[sequence];
2905 if( !sec10 || !sec11 || !sec12 )
2908 const size_t pieceStride =
m_version <= 0x2024 ? 16 : 20;
2911 THROW_IO_ERROR(
"Invalid PADS copper-shape controller framing" );
2913 constexpr size_t MAX_COPPER_SHAPE_EDGES = 80;
2919 if(
name.size() < 4 ||
name.substr( 0, 3 ) !=
"DRW" )
2924 if( ( run.itemKind & 0xFFFFU ) != 3 )
2927 uint32_t sec11Index =
static_cast<uint32_t
>( run.pieceStart );
2929 if( sec11Index >= sec11->
count )
2942 std::vector<VECTOR2I> loop;
2960 const double cx = ( p0.
x + p1.
x ) / 2.0;
2961 const double cy = ( p0.
y + p1.
y ) / 2.0;
2962 const double radius = std::hypot( p1.
x - p0.
x, p1.
y - p0.
y ) / 2.0;
2964 constexpr int CIRCLE_SEGMENTS = 32;
2967 for(
int i = 0; i < CIRCLE_SEGMENTS; ++i )
2969 double angle = ( 2.0 *
M_PI *
static_cast<double>( i ) ) /
static_cast<double>( CIRCLE_SEGMENTS );
2970 loop.emplace_back(
static_cast<int32_t
>( std::lround( cx +
radius * std::cos( angle ) ) ),
2971 static_cast<int32_t
>( std::lround( cy +
radius * std::sin( angle ) ) ) );
2979 size_t pieceRotation = sec11->
totalBytes - ( pieceStride - 8 );
2980 uint32_t levelIndex = ( sec11Index + 1 ) % sec11->
count;
2981 uint8_t level =
ringU8( *sec11, pieceRotation, pieceStride, levelIndex, 1 );
2983 static_cast<double>(
ringI32( *sec11, pieceRotation, pieceStride, sec11Index, pieceStride - 8 ) );
2984 copper.
layer = level;
2988 int32_t rawX =
static_cast<int32_t
>( originX + pt.x );
2989 int32_t rawY =
static_cast<int32_t
>( originY + pt.y );
3015 for( uint32_t rec = 0; rec < sec10->
count; ++rec )
3019 if(
name.size() < 4 ||
name.substr( 0, 3 ) !=
"DIM" )
3027 int32_t startRow = it->second.vertexStart;
3029 int32_t bp1x = 0, bp1y = 0, bp2x = 0, bp2y = 0, arwx = 0, arwy = 0, attr = 0;
3032 || !
sec12Vertex( startRow + 4, arwx, arwy, attr ) )
3044 dim.
points.push_back( pt1 );
3045 dim.
points.push_back( pt2 );
3074 size_t aField )
const
3076 if( aSection.
totalBytes == 0 || aIndex >= aSection.
count || aField >= aStride )
3077 THROW_IO_ERROR(
"Invalid PADS circular-controller record access" );
3079 size_t logical =
static_cast<size_t>( aIndex ) * aStride + aField;
3080 size_t physical = ( aRotation + logical ) % aSection.
totalBytes;
3086 size_t aField )
const
3090 for(
size_t byte = 0;
byte < 4; ++byte )
3091 value |=
static_cast<uint32_t
>(
ringU8( aSection, aRotation, aStride, aIndex, aField +
byte ) ) << (
byte * 8 );
3098 size_t aField )
const
3100 return static_cast<int32_t
>(
ringU32( aSection, aRotation, aStride, aIndex, aField ) );
3105 size_t aField,
size_t aLength )
const
3108 value.reserve( aLength );
3110 for(
size_t byte = 0;
byte < aLength; ++byte )
3112 char c =
static_cast<char>(
ringU8( aSection, aRotation, aStride, aIndex, aField +
byte ) );
3117 value.push_back( c );
3133 if( sec10->
count == 0 )
3139 if( sec10->
totalBytes !=
static_cast<uint64_t
>( sec10->
count ) * stride )
3142 for( uint32_t ownerIndex = 0; ownerIndex < sec10->
count; ++ownerIndex )
3149 uint32_t lagIndex = ( ownerIndex + 1 ) % sec10->
count;
3163 const char* aWhat )
const
3165 uint64_t arcIndex =
static_cast<uint64_t
>( aArcStart ) +
static_cast<uint32_t
>( aAttr );
3167 if( aArcStart < 0 || arcIndex >= aArcParameters.
count
3168 || aArcParameters.
totalBytes !=
static_cast<uint64_t
>( aArcParameters.
count ) * 20 )
3170 THROW_IO_ERROR( wxString::Format(
"Invalid PADS %s arc range", aWhat ) );
3183 double xmin = aArcRecord.
I32( 0 );
3184 double ymin = aArcRecord.
I32( 4 );
3185 double xmax = aArcRecord.
I32( 8 );
3186 double ymax = aArcRecord.
I32( 12 );
3187 double centerX = ( xmin + xmax ) / 2.0;
3188 double centerY = ( ymin + ymax ) / 2.0;
3189 double startAngle = std::atan2( aStart.
y - centerY, aStart.
x - centerX ) * 180.0 /
M_PI;
3190 double endAngle = std::atan2( aEnd.
y - centerY, aEnd.
x - centerX ) * 180.0 /
M_PI;
3193 arc.
cx = centerX + aOriginX;
3194 arc.cy = centerY + aOriginY;
3195 arc.radius = ( xmax - xmin ) / 2.0;
3196 arc.start_angle = startAngle;
3210 if( !owners || !pieces || !vertices || !arcParameters )
3214 const size_t pieceStride =
m_version <= 0x2024 ? 16 : 20;
3215 const size_t pieceHead = pieceStride == 16 ? 8 : 12;
3216 const size_t cornerField = pieceStride == 16 ? 12 : 16;
3218 const size_t originYField = originXField + 4;
3219 const size_t pieceRotation = pieces->
totalBytes - pieceHead;
3223 if( ( run.itemKind & 0xFFFFU ) != 1 || run.pieceCount <= 0 || run.pieceStart < 0 || run.vertexStart < 0 )
3226 if(
static_cast<uint64_t
>( run.pieceStart ) +
static_cast<uint64_t
>( run.pieceCount ) > pieces->
count )
3231 int32_t vertexCursor = run.vertexStart;
3233 for( int32_t piece = 0; piece < run.pieceCount; ++piece )
3235 uint32_t pieceIndex =
static_cast<uint32_t
>( run.pieceStart + piece );
3236 int32_t corners =
ringI32( *pieces, pieceRotation, pieceStride, pieceIndex, cornerField );
3239 ||
static_cast<uint64_t
>( vertexCursor ) +
static_cast<uint64_t
>( corners ) > vertices->
count )
3242 std::vector<ARC_VERTEX>
decoded;
3243 decoded.reserve(
static_cast<size_t>( corners ) );
3245 for( int32_t corner = 0; corner < corners; ++corner )
3247 size_t offset = vertices->
physicalOffset +
static_cast<size_t>( vertexCursor + corner ) * 12;
3249 decoded.push_back( { record.
I32( 0 ), record.
I32( 4 ), record.
I32( 8 ) } );
3252 vertexCursor += corners;
3257 static_cast<double>(
ringI32( *pieces, pieceRotation, pieceStride, pieceIndex, pieceStride - 8 ) );
3264 double rawX =
static_cast<double>( vertex.
x ) + originX;
3265 double rawY =
static_cast<double>( vertex.
y ) + originY;
3272 outline.
points.emplace_back( rawX, rawY,
3277 outline.
points.emplace_back( rawX, rawY );
3294 if( !owners || !pieces || !vertices || !arcParameters )
3298 const size_t pieceStride =
m_version <= 0x2024 ? 16 : 20;
3299 const size_t pieceHead = pieceStride - 8;
3300 const size_t cornerField = pieceStride - 4;
3302 const size_t originYField = originXField + 4;
3303 const size_t pieceRotation = pieces->
totalBytes - pieceHead;
3305 if( vertices->
totalBytes !=
static_cast<uint64_t
>( vertices->
count ) * 12
3306 || arcParameters->
totalBytes !=
static_cast<uint64_t
>( arcParameters->
count ) * 20 )
3313 if( ( run.itemKind & 0xFFFFU ) != 0 ||
name.compare( 0, 3,
"DRW" ) != 0 || run.pieceCount <= 0
3314 || run.pieceStart < 0 || run.vertexStart < 0 )
3319 if(
static_cast<uint64_t
>( run.pieceStart ) +
static_cast<uint64_t
>( run.pieceCount ) > pieces->
count )
3324 int32_t vertexCursor = run.vertexStart;
3326 for( int32_t piece = 0; piece < run.pieceCount; ++piece )
3328 uint32_t pieceIndex =
static_cast<uint32_t
>( run.pieceStart + piece );
3329 int32_t corners =
ringI32( *pieces, pieceRotation, pieceStride, pieceIndex, cornerField );
3332 ||
static_cast<uint64_t
>( vertexCursor ) +
static_cast<uint64_t
>( corners ) > vertices->
count )
3334 THROW_IO_ERROR( wxString::Format(
"Invalid PADS graphic vertex range (%s piece %d, start %d, "
3335 "corners %d, count %u)",
3336 name.c_str(), piece, vertexCursor, corners, vertices->
count ) );
3341 graphic.
width =
ringI32( *pieces, pieceRotation, pieceStride, pieceIndex, pieceStride - 8 );
3342 graphic.
layer =
ringU8( *pieces, pieceRotation, pieceStride, ( pieceIndex + 1 ) % pieces->
count, 1 );
3343 graphic.
points.reserve(
static_cast<size_t>( corners ) );
3345 std::vector<ARC_VERTEX>
decoded;
3346 decoded.reserve(
static_cast<size_t>( corners ) );
3348 for( int32_t corner = 0; corner < corners; ++corner )
3351 +
static_cast<uint32_t
>( vertexCursor + corner ) * 12 );
3352 decoded.push_back( { record.
I32( 0 ), record.
I32( 4 ), record.
I32( 8 ) } );
3355 vertexCursor += corners;
3359 uint8_t pieceType =
ringU8( *pieces, pieceRotation, pieceStride, ( pieceIndex + 1 ) % pieces->
count, 0 );
3361 if( pieceType == 2 &&
decoded.size() == 2 )
3363 double centerX = (
decoded[0].x +
decoded[1].x ) / 2.0 + originX;
3364 double centerY = (
decoded[0].y +
decoded[1].y ) / 2.0 + originY;
3370 arc.start_angle = 0.0;
3371 arc.delta_angle = 360.0;
3373 graphic.
points.emplace_back( centerX +
radius, centerY, arc );
3381 double rawX =
static_cast<double>( vertex.
x ) + originX;
3382 double rawY =
static_cast<double>( vertex.
y ) + originY;
3389 graphic.
points.emplace_back( rawX, rawY,
3394 graphic.
points.emplace_back( rawX, rawY );
3417 aAttr = rec.
I32( 8 );
3433 if( !pieces || it->second.pieceCount < 1 || it->second.pieceStart < 0 )
3436 size_t pieceStride =
m_version <= 0x2024 ? 16 : 20;
3437 size_t pieceHead = pieceStride == 16 ? 8 : 12;
3438 size_t cornerField = pieceStride == 16 ? 12 : 16;
3439 size_t pieceRotation = pieces->
totalBytes - pieceHead;
3441 ringI32( *pieces, pieceRotation, pieceStride,
static_cast<uint32_t
>( it->second.pieceStart ), cornerField );
3443 if( corners < 4 ||
static_cast<size_t>( corners ) > aMaxVerts + 1 )
3446 int32_t startRow = it->second.vertexStart;
3452 if( !
sec12Vertex( startRow, firstX, firstY, attr ) )
3455 for( int32_t corner = 0; corner < corners - 1; ++corner )
3460 if( !
sec12Vertex( startRow + corner, x, y, attr ) )
3463 aOut.emplace_back( x, y );
3469 if( !
sec12Vertex( startRow + corners - 1, lastX, lastY, attr ) || lastX != firstX || lastY != firstY )
3488 if( !pieces || it->second.pieceCount < 1 || it->second.pieceStart < 0 )
3491 size_t pieceStride =
m_version <= 0x2024 ? 16 : 20;
3492 size_t pieceHead = pieceStride == 16 ? 8 : 12;
3493 size_t cornerField = pieceStride == 16 ? 12 : 16;
3494 size_t pieceRotation = pieces->
totalBytes - pieceHead;
3496 if(
ringI32( *pieces, pieceRotation, pieceStride,
static_cast<uint32_t
>( it->second.pieceStart ), cornerField )
3502 int32_t startRow = it->second.vertexStart;
3504 int32_t x0 = 0, y0 = 0, x1 = 0, y1 = 0, attr = 0;
3509 if( x0 == x1 && y0 == y1 )
3526 if( !sec10 || !sec12 )
3535 int32_t originX = 0;
3536 int32_t originY = 0;
3543 std::vector<Owner> owners;
3547 if( ( run.itemKind & 0xFFFFU ) != 10 )
3567 owners.push_back( std::move( owner ) );
3570 if( owners.empty() )
3573 constexpr size_t MAX_KEEP_OUT_VERTICES = 80;
3575 for(
const Owner& owner : owners )
3582 std::vector<VECTOR2I> structuralLoop;
3584 if(
fetchOwnerLoop( owner.name, MAX_KEEP_OUT_VERTICES, structuralLoop ) )
3586 for(
const VECTOR2I& vertex : structuralLoop )
3588 int32_t rawX = owner.originX +
static_cast<int32_t
>( vertex.x );
3589 int32_t rawY = owner.originY +
static_cast<int32_t
>( vertex.y );
3593 m_keepouts.push_back( std::move( keepout ) );
3599 int64_t spanX = owner.maxX - owner.minX;
3600 int64_t spanY = owner.maxY - owner.minY;
3602 if( spanX <= 0 || spanX != spanY )
3605 constexpr int ELLIPSE_SEGMENTS = 32;
3606 double cx =
static_cast<double>( owner.minX + owner.maxX ) / 2.0;
3607 double cy =
static_cast<double>( owner.minY + owner.maxY ) / 2.0;
3608 double radius =
static_cast<double>( spanX ) / 2.0;
3610 for(
int i = 0; i < ELLIPSE_SEGMENTS; ++i )
3612 double angle = ( 2.0 *
M_PI *
static_cast<double>( i ) ) /
static_cast<double>( ELLIPSE_SEGMENTS );
3613 int32_t rawX = owner.originX +
static_cast<int32_t
>( std::lround( cx +
radius * std::cos( angle ) ) );
3614 int32_t rawY = owner.originY +
static_cast<int32_t
>( std::lround( cy +
radius * std::sin( angle ) ) );
3618 m_keepouts.push_back( std::move( keepout ) );
3650 static constexpr size_t OWNER_SIZE = 88;
3651 static constexpr size_t PIECE_SIZE = 16;
3652 static constexpr size_t VERTEX_SIZE = 8;
3656 uint32_t pieceStart = 0;
3657 uint32_t vertexStart = 0;
3658 uint32_t pieceCount = 0;
3662 std::string netName;
3665 std::vector<POUR_OWNER> owners;
3671 if( !sec52 || !sec53 || !sec54 )
3681 const uint8_t outlineType =
m_cursor.U8At( offset + 87 );
3682 std::string
name =
m_cursor.StringAt( offset + 70, 14 );
3684 if( outlineType != 0x32 ||
name.rfind(
"POR", 0 ) != 0 )
3688 owner.pieceStart =
m_cursor.U32At( offset );
3689 owner.vertexStart =
m_cursor.U32At( offset + 4 );
3690 owner.rawX =
m_cursor.I32At( offset + 24 );
3691 owner.rawY =
m_cursor.I32At( offset + 28 );
3692 owner.pieceCount =
m_cursor.U32At( offset + 64 );
3693 owner.name = std::move(
name );
3696 owner.netName = netIt->second;
3698 owners.push_back( std::move( owner ) );
3701 for(
const POUR_OWNER& owner : owners )
3703 uint32_t vertexIndex = owner.vertexStart;
3705 for( uint32_t pieceOrdinal = 0; pieceOrdinal < owner.pieceCount; ++pieceOrdinal )
3707 const uint32_t pieceIndex = owner.pieceStart + pieceOrdinal;
3709 if( pieceIndex >= sec53->
count )
3712 const size_t pieceOff = sec53->
physicalOffset +
static_cast<size_t>( pieceIndex ) * PIECE_SIZE;
3713 uint32_t cornerCount =
m_cursor.U32At( pieceOff );
3714 int32_t width =
m_cursor.I32At( pieceOff + 8 );
3715 uint8_t pieceType =
m_cursor.U8At( pieceOff + 12 );
3716 uint8_t layer =
m_cursor.U8At( pieceOff + 13 );
3718 if( cornerCount == 0 || vertexIndex > sec54->
count || cornerCount > sec54->
count - vertexIndex )
3721 const size_t vOff = sec54->
physicalOffset +
static_cast<size_t>( vertexIndex ) * VERTEX_SIZE;
3722 vertexIndex += cornerCount;
3727 pour.
width =
static_cast<double>( width );
3728 pour.
layer =
static_cast<int>( layer );
3730 if( pieceType == 0x33 && cornerCount == 2 )
3735 int32_t x0 = owner.rawX +
m_cursor.I32At( vOff );
3736 int32_t y0 = owner.rawY +
m_cursor.I32At( vOff + 4 );
3737 int32_t x1 = owner.rawX +
m_cursor.I32At( vOff + VERTEX_SIZE );
3738 int32_t y1 = owner.rawY +
m_cursor.I32At( vOff + VERTEX_SIZE + 4 );
3740 double cx = ( x0 + x1 ) / 2.0;
3741 double cy = ( y0 + y1 ) / 2.0;
3742 double radius = std::hypot( x1 - x0, y1 - y0 ) / 2.0;
3744 static constexpr int CIRCLE_SEGMENTS = 48;
3746 for(
int s = 0; s < CIRCLE_SEGMENTS; ++s )
3748 double angle = 2.0 *
M_PI * s / CIRCLE_SEGMENTS;
3749 int32_t rawX =
static_cast<int32_t
>( std::lround( cx +
radius * std::cos( angle ) ) );
3750 int32_t rawY =
static_cast<int32_t
>( std::lround( cy +
radius * std::sin( angle ) ) );
3757 for( uint32_t vertex = 0; vertex < cornerCount; ++vertex )
3759 size_t offset = vOff +
static_cast<size_t>( vertex ) * VERTEX_SIZE;
3760 int32_t localX =
m_cursor.I32At( offset );
3761 int32_t localY =
m_cursor.I32At( offset + 4 );
3768 m_pours.push_back( std::move( pour ) );
3782 constexpr uint64_t CONTROLLER_LEAD_IN = 12;
3783 const uint64_t base =
static_cast<uint64_t
>( section->
physicalOffset ) + CONTROLLER_LEAD_IN;
3786 return base +
bytes <=
m_data.size() ?
static_cast<size_t>( base ) : 0;
3801 static constexpr size_t NAME_LEN = 24;
3802 static constexpr size_t OFF_ROUT = 32;
3803 static constexpr size_t OFF_LAYTH = 52;
3804 static constexpr size_t OFF_COPTH = 56;
3805 static constexpr size_t OFF_DIEL = 60;
3807 auto layerFunction = []( int32_t aSerializedType )
3809 switch( aSerializedType )
3824 if( recordBase == 0 )
3832 size_t rec = recordBase + k * section->
stride;
3836 info.number =
static_cast<int>( k );
3837 info.name = layerRec.
Str( 0, NAME_LEN );
3839 int32_t routingDir = layerRec.
I32( OFF_ROUT );
3840 info.routing_direction = routingDir;
3842 info.layer_thickness =
static_cast<double>( layerRec.
I32( OFF_LAYTH ) );
3843 info.copper_thickness =
static_cast<double>( layerRec.
I32( OFF_COPTH ) );
3845 float dielectric = 0.0f;
3846 std::memcpy( &dielectric, &
m_data[rec + OFF_DIEL],
sizeof(
float ) );
3847 info.dielectric_constant =
static_cast<double>( dielectric );
3851 const int32_t serializedType = k > 0 ?
m_cursor.I32At( rec - 4 ) : 0;
3885 for(
size_t partIdx = 0; partIdx <
m_parts.size(); ++partIdx )
3889 if( !part.
decal.empty() )
3897 uint32_t decalIndex = hintIt->second;
3904 if( !decalName.empty() &&
m_decals.count( decalName ) )
3905 part.
decal = decalName;
3918 for(
size_t partIdx = 0; partIdx <
m_parts.size(); ++partIdx )
3922 if( !part.
decal.empty() )
3930 uint32_t partTypeIdx = hintIt->second;
3935 uint8_t alternate = 0;
3939 alternate = alternateIt->second;
3942 int32_t decalIndex = decalIndices.empty() ? -1 : decalIndices.front();
3944 if( alternate < decalIndices.size() )
3945 decalIndex = decalIndices[alternate];
3953 decalIndex =
static_cast<int32_t
>( placedIt->second );
3956 if( decalIndex < 0 ||
static_cast<size_t>( decalIndex ) >=
m_decalNameTable.size() )
3961 if( !decalName.empty() &&
m_decals.count( decalName ) )
3962 part.
decal = decalName;
3972 if( !typeName.empty() && typeName != part.
decal )
3973 part.
value = typeName;
Bounds-checked little-endian read cursor over a PADS binary buffer.
uint8_t U8At(size_t aOffset) const
void decodeRoutedCopper(std::map< std::string, ROUTE > &aRoutes)
std::map< uint32_t, std::string > m_netSelfPtrToName
uint8_t ringU8(const SDB_SECTION &aSection, size_t aRotation, size_t aStride, uint32_t aIndex, size_t aField) const
double toBasicCoordY(int32_t aRawValue) const
ROUTE_OBJECT_NODES resolveRouteObjectNodes(const SDB_SECTION &aRouteLayers, size_t aObjectCount, const std::vector< int > &aSerializedLayerOrder)
std::vector< NET > m_nets
std::string ringStr(const SDB_SECTION &aSection, size_t aRotation, size_t aStride, uint32_t aIndex, size_t aField, size_t aLength) const
bool isValidNetName(const std::string &aName) const
std::map< std::string, uint32_t > m_decalTerminalCount
std::map< size_t, int > m_partClusterId
std::map< size_t, uint32_t > m_partDecalIndex
std::vector< NET_CLASS_RULE_EDGE > collectNetClassRuleEdges(const std::set< uint32_t > &aOwnerSet)
std::map< std::string, ROUTE > seedRoutesFromVias(const std::vector< VIA_LOCATION > &aVias) const
size_t layerStackupBase() const
Base of section 69's layer records after its fixed controller lead-in.
void parsePlacementFields(const SDB_SECTION &aText, size_t aRecordBase, size_t aRecordSize, size_t aRingRotation)
void parseDecalNameTableOld()
void parseNetConnectionsNew()
std::vector< VIA_LOCATION > decodeViaLocations()
std::map< uint32_t, size_t > parseRouteJunctionNets() const
std::vector< PART > m_parts
std::map< uint32_t, size_t > m_sec23RecordToNet
std::vector< NET_ANCHOR > m_netAnchors
bool usesDirectDecalChain() const
std::map< size_t, uint8_t > m_partDecalAlternate
std::map< std::string, uint32_t > m_netClassOwner
void parseDecalNameTable()
std::vector< COPPER_SHAPE > m_copper_shapes
void applyPadstackPairs(PART_DECAL &aDecal, const std::vector< std::pair< int32_t, int32_t > > &aPairs, int32_t aStart, int32_t aCount)
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
std::vector< DIFF_PAIR_DEF > m_diffPairs
std::map< std::string, int32_t > m_decalTerminalStart
std::vector< POLYLINE > m_boardOutlines
void parseFreeText(const SDB_SECTION &aText, size_t aRecordBase, size_t aRecordSize, size_t aRingRotation, size_t aPoolBase, size_t aPoolHi)
bool fetchOwnerCirclePoints(const std::string &aName, VECTOR2I &aP0, VECTOR2I &aP1) const
std::vector< size_t > oldNetRecordOffsets() const
std::map< std::string, int32_t > m_decalStackStart
std::vector< KEEPOUT > m_keepouts
void parsePartTypeTable()
std::map< std::string, int32_t > m_decalStackCount
uint32_t ringU32(const SDB_SECTION &aSection, size_t aRotation, size_t aStride, uint32_t aIndex, size_t aField) const
void parseRouteVertices()
std::map< size_t, uint32_t > m_partTypeIndex
std::map< uint32_t, size_t > m_placementObjectToPart
std::vector< std::pair< int, int > > m_padStackDrillSpans
const SDB_SECTION * getSection(int aIndex) const
std::vector< DIMENSION > m_dimensions
std::vector< LAYER_INFO > m_layerInfos
void Parse(const wxString &aFileName)
int32_t ringI32(const SDB_SECTION &aSection, size_t aRotation, size_t aStride, uint32_t aIndex, size_t aField) const
double toBasicCoordX(int32_t aRawValue) const
std::vector< std::vector< int32_t > > m_partTypeDecalIndices
void applyNetClassClearances(const std::vector< NET_CLASS_RULE_EDGE > &aEdges, const std::map< uint32_t, size_t > &aOwnerOrdinal)
bool fetchOwnerLoop(const std::string &aName, size_t aMaxVerts, std::vector< VECTOR2I > &aOut) const
PART makePlacementPart(const SDB_RECORD &aRec, int aXOff, std::optional< int > aYOff, int aAngleOff, int aNameOff, const std::string &aRefDes) const
void parseNetConnectionsOld()
bool sec12Vertex(int32_t aRow, int32_t &aX, int32_t &aY, int32_t &aAttr) const
const std::vector< uint8_t > & m_data
std::vector< std::string > m_partTypeNames
std::map< uint32_t, std::string > m_sec23IndexToNet
std::vector< NET_ANCHOR > m_netConnectionEndpoints
void assignDefaultPadStacks()
std::vector< std::vector< PAD_STACK_LAYER > > m_padStackPool
std::map< uint32_t, std::set< size_t > > m_junctionHandleNets
void parsePartPlacements()
std::map< size_t, int32_t > m_partFieldStart
void computeSec12CleanRows()
std::vector< ROUTE > m_routes
void parseBoardOutlineDirect()
std::vector< TEXT > m_texts
std::vector< GRAPHIC_LINE > m_graphicLines
std::vector< PART_CLUSTER > m_clusters
std::vector< BIN_NET_CLASS_DEF > m_netClasses
std::vector< std::string > m_decalNameTable
double toBasicAngle(int32_t aRawAngle) const
std::vector< LAYER_INFO > GetLayerInfos() const
std::map< std::string, OWNER_RUN > m_ownerRuns
SDB_RECORD arcRecordFor(const SDB_SECTION &aArcParameters, int32_t aArcStart, int32_t aAttr, const char *aWhat) const
static bool IsSupportedVersion(uint16_t aVersion)
A bounds-checked reader positioned at one record inside a buffer.
uint32_t U32(size_t aOffset) const
uint8_t U8(size_t aOffset) const
uint16_t U16(size_t aOffset) const
int32_t I32(size_t aOffset) const
std::string Str(size_t aOffset, size_t aMaxLen) const
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
constexpr size_t ORIGIN_X
constexpr size_t ROTATION
bool ReadFileHeader(const wxString &aFileName, std::vector< uint8_t > &aOut, size_t aCount)
Read at most aCount leading bytes of aFileName into aOut, which is sized to what was actually read.
static bool viaRecordValid(const BINARY_CURSOR &aCur, size_t aTypeByte)
bool HasSdbMagic(const std::vector< uint8_t > &aData, uint8_t aMagic1)
Check the PADS SDB container magic, a leading 0x00 followed by a format-specific second byte.
constexpr uint16_t SDB_RECORD_SENTINEL
constexpr double SDB_BASIC_PER_MIL
static constexpr int DECAL_NAME_OFFSET
Rotation between the physical section cursor and the logical record base.
static void logParsePhase(const char *aWhat, std::chrono::steady_clock::time_point aStart)
Enabled by setting KICAD_PADS_PROFILE.
static ARC deriveArc(const SDB_RECORD &aArcRecord, const ARC_VERTEX &aStart, const ARC_VERTEX &aEnd, double aOriginX, double aOriginY)
static void logResolvedBase(int aSection, const char *aWhat, size_t aResolved, uint32_t aPhysical)
Report a structurally derived physical section base for cross-checking the Kaitai grammar.
bool ReadFileToBuffer(const wxString &aFileName, std::vector< uint8_t > &aOut)
Read an entire file into aOut.
static const std::map< uint8_t, std::string > PAD_SHAPE_NAMES
uint16_t getU16LE(const std::vector< uint8_t > &aData, size_t aOffset)
static const PADSTACK_LAYOUT & padstackLayout(uint16_t aVersion)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
A polyline point that may instead be an arc segment.
One board-outline vertex triplet: [i32 X, i32 Y, i32 attr] where attr -1 is a plain corner and attr >...
Arc as center, radius and angles.
Every routed-copper object's PADS layer and route-node handle, indexed by object ordinal,...
std::vector< int > layers
std::vector< uint32_t > handles
std::map< uint32_t, std::set< size_t > > handleNets
One placed via recovered from the section-60 junction ring.
A PADS net class recovered from the binary design-rule graph.
Standalone copper area from the LINES section (type=COPPER), not part of a pour.
std::vector< ARC_POINT > outline
bool filled
COPCLS, COPCIR.
double width
For open polylines.
double crossbar_pos
Y for horizontal, X for vertical.
std::vector< POINT > points
Measurement endpoints.
Non-electrical drawing item from the LINES section (type=LINES).
std::vector< ARC_POINT > points
std::vector< ARC_POINT > outline
One type-66 net-class rule edge: its owner pointer, rule-detail page, full rule pointer (declaration ...
std::string shape
R, S, A, O, OF, RF, RT, ST, RA, SA, RC, OC.
double thermal_outer_diameter
Thermal or void in plane.
double slot_orientation
0-179.999 degrees
double sizeB
Height for rectangles/ovals.
double corner_radius
Always positive.
double sizeA
Diameter or width.
A PADS part cluster (named group of parts).
std::map< int, std::pair< int, int > > drill_spans
std::vector< TERMINAL > terminals
std::map< int, std::vector< PAD_STACK_LAYER > > pad_stacks
std::string decal
Primary decal (first in colon-separated list)
A polyline that may contain arc segments, used for board outlines and graphics.
std::vector< ARC_POINT > points
std::string owner_pour
Parent pour, 7th header field.
std::vector< ARC_POINT > points
std::vector< TRACK > tracks
The PADS PowerPCB .pcb file is a serialized snapshot of PADS' in-memory SDB (System DataBase) object ...
std::vector< ARC_POINT > points
std::vector< PAD_STACK_LAYER > stack
static std::vector< uint8_t > bytes(const std::string &aText)
static std::string decoded(const std::string &aBase64)
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I