24#include <unordered_map>
30#include <wx/translation.h>
63 wxString upper = aExpr.Upper();
64 return upper.Contains( wxT(
"INPOLY" ) ) || upper.Contains( wxT(
"ISPOLY" ) );
70 for(
const ARULE6& rule : aRulesByPriorityAsc )
84 int32_t aMaskExpansion,
int aLandDiameter )
89 if( aManual && aFromHole )
91 int64_t opening =
static_cast<int64_t
>( aHoleSize ) + 2LL * aMaskExpansion;
93 return opening <= static_cast<int64_t>( aLandDiameter );
105 if( aName.IsEmpty() )
108 static const std::unordered_map<std::string, ALTIUM_LAYER> hash_map = {
200 auto it = hash_map.find( std::string( aName.c_str() ) );
202 if( it != hash_map.end() )
206 const wxString mechanicalStr(
"MECHANICAL" );
208 if( aName.StartsWith( mechanicalStr ) )
210 unsigned long val = 0;
212 if( aName.Mid( mechanicalStr.length() ).ToULong( &val ) )
216 wxLogTrace(
traceAltiumIo, wxT(
"Unknown mapping of the Altium layer '%s'." ), aName );
223 static const std::unordered_map<std::string, ALTIUM_MECHKIND> hash_map = {
270 auto it = hash_map.find( std::string( aName.c_str() ) );
272 if( it != hash_map.end() )
278 wxLogTrace(
traceAltiumIo, wxT(
"Unknown mapping of the Altium layer kind '%s'." ), aName );
285 std::vector<ALTIUM_VERTICE>& aVertices )
287 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
289 const wxString si = std::to_string( i );
291 const wxString vxi = wxT(
"VX" ) + si;
292 const wxString vyi = wxT(
"VY" ) + si;
294 if( aProps.find( vxi ) == aProps.end() || aProps.find( vyi ) == aProps.end() )
306 aVertices.emplace_back( isRound,
radius, sa, ea, vp, cp );
316 if( mode == wxT(
"None" ) )
318 else if( mode == wxT(
"Rule" ) )
320 else if( mode == wxT(
"Manual" ) )
323 wxLogTrace(
traceAltiumIo, wxT(
"Unknown Mode string: '%s'." ), mode );
333 if( record == wxT(
"Arc" ) )
335 else if( record == wxT(
"Pad" ) )
337 else if( record == wxT(
"Via" ) )
339 else if( record == wxT(
"Track" ) )
341 else if( record == wxT(
"Text" ) )
343 else if( record == wxT(
"Fill" ) )
345 else if( record == wxT(
"Region" ) )
347 else if( record == wxT(
"Model" ) )
350 wxLogTrace(
traceAltiumIo, wxT(
"Unknown Record name string: '%s'." ), record );
357 const std::map<wxString, wxString>& aProps, wxString aKey )
361 if( parsedType == wxT(
"Mask" ) )
364 wxLogTrace(
traceAltiumIo, wxT(
"Unknown Extended Primitive Information type: '%s'." ), parsedType );
378 const std::string& aSubrecordName,
size_t aExpectedLength,
379 size_t aActualLength )
381 if( aActualLength < aExpectedLength )
383 THROW_IO_ERRORF( wxT(
"%s stream %s has length %d, which is unexpected (expected at least %d)" ),
394 const std::map<wxString, wxString> props = aReader.
ReadProperties();
397 THROW_IO_ERROR( wxT(
"ExtendedPrimitiveInformation stream has no properties!" ) );
413 uint32_t aLayerIdFallback )
431 mechenabled = !mechEnabled.Contains( wxS(
"FALSE" ) );
437 if( !mechKind.IsEmpty() )
445 return wxString::Format( wxT(
"%s|%d|%d" ), aMaterial, aHeight,
446 static_cast<int>( std::lround( aConst * 1000.0 ) ) );
458 std::map<wxString, double> tangents;
459 std::set<wxString> ambiguous;
461 auto scan = [&](
const wxString& aFamily )
463 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
465 const wxString prefix = aFamily + std::to_string( i );
467 if( aProps.find( prefix + wxT(
"NAME" ) ) == aProps.end() )
470 if( aProps.find( prefix + wxT(
"DIELLOSSTANGENT" ) ) == aProps.end() )
483 auto [it, inserted] = tangents.insert( { key, tangent } );
485 if( !inserted &&
std::abs( it->second - tangent ) > 1e-9 )
486 ambiguous.insert( key );
492 scan( wxT(
"V9_STACK_LAYER" ) );
493 scan( wxT(
"LAYER_V8_" ) );
495 for(
const wxString& key : ambiguous )
496 tangents.erase( key );
504 std::vector<ABOARD6_LAYER_STACKUP> stackup;
506 for(
size_t i = 1; i < std::numeric_limits<size_t>::max(); i++ )
508 const wxString layeri = wxString( wxT(
"LAYER" ) ) << std::to_string( i );
509 const wxString layername = layeri + wxT(
"NAME" );
511 auto layernameit = aProps.find( layername );
513 if( layernameit == aProps.end() )
517 stackup.push_back( l );
521 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
523 const wxString layeri = wxString( wxT(
"LAYERV7_" ) ) << std::to_string( i );
524 const wxString layername = layeri + wxT(
"NAME" );
526 auto layernameit = aProps.find( layername );
528 if( layernameit == aProps.end() )
532 stackup.push_back( l );
539 if( !tangents.empty() )
546 auto it = tangents.find( key );
548 if( it != tangents.end() )
549 l.dielectriclosstangent = it->second;
570 wxString originalName = l.name;
573 for(
int ii = 2; !
layerNames.insert( l.name ).second; ii++ )
574 l.name = wxString::Format( wxT(
"%s %d" ), originalName, ii );
578 THROW_IO_ERROR( wxT(
"Library stream was not parsed correctly!" ) );
600 wxString originalName = l.name;
603 for(
int ii = 2; !
layerNames.insert( l.name ).second; ii++ )
604 l.name = wxString::Format( wxT(
"%s %d" ), originalName, ii );
610 THROW_IO_ERROR( wxT(
"Board6 stream was not parsed correctly!" ) );
615 std::map<wxString, wxString> properties = aReader.
ReadProperties();
617 if( properties.empty() )
624 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
626 auto mit = properties.find( wxT(
"M" ) + wxString( std::to_string( i ) ) );
628 if( mit == properties.end() )
631 names.push_back( mit->second );
635 THROW_IO_ERROR( wxT(
"Classes6 stream was not parsed correctly" ) );
674 THROW_IO_ERROR( wxT(
"Components6 stream was not parsed correctly" ) );
714 for(
int i = 0; i < refcount; i++ )
716 const std::string refi =
"REFERENCE" + std::to_string( i ) +
"POINT";
717 const wxString ref( refi );
722 for(
size_t i = 1; i < std::numeric_limits<size_t>::max(); i++ )
724 const std::string texti =
"TEXT" + std::to_string( i );
725 const std::string textix = texti +
"X";
726 const std::string textiy = texti +
"Y";
728 if( props.find( textix ) == props.end() || props.find( textiy ) == props.end() )
746 THROW_IO_ERROR( wxT(
"Dimensions6 stream was not parsed correctly" ) );
751 std::map<wxString, wxString> properties = aReader.
ReadProperties();
753 if( properties.empty() )
773 std::map<wxString, wxString> properties = aReader.
ReadProperties();
775 if( properties.empty() )
786 std::map<wxString, wxString> properties = aReader.
ReadProperties();
788 if( properties.empty() )
820 THROW_IO_ERROR( wxT(
"Polygons6 stream was not parsed correctly" ) );
839 if( rulekind == wxT(
"Clearance" ) )
844 else if( rulekind == wxT(
"DiffPairsRouting" ) )
848 else if( rulekind == wxT(
"Height" ) )
852 else if( rulekind == wxT(
"HoleSize" ) )
858 else if( rulekind == wxT(
"HoleToHoleClearance" ) )
863 else if( rulekind == wxT(
"RoutingVias" ) )
873 else if( rulekind == wxT(
"Width" ) )
880 else if( rulekind == wxT(
"PasteMaskExpansion" ) )
885 else if( rulekind == wxT(
"SolderMaskExpansion" ) )
890 else if( rulekind == wxT(
"PlaneClearance" ) )
895 else if( rulekind == wxT(
"PolygonConnect" ) )
915 THROW_IO_ERROR( wxT(
"Rules6 stream was not parsed correctly" ) );
930 wxT(
"" ) ).IsEmpty();
940 THROW_IO_ERROR( wxT(
"SmartUnions stream was not parsed correctly" ) );
956 uint8_t flags1 = aReader.
Read<uint8_t>();
960 uint8_t flags2 = aReader.
Read<uint8_t>();
963 net = aReader.
Read<uint16_t>();
983 if( remaining >= 10 )
1003 THROW_IO_ERROR( wxT(
"ComponentsBodies6 stream has invalid recordtype" ) );
1011 std::map<wxString, wxString> properties = aReader.
ReadProperties();
1013 if( properties.empty() )
1014 THROW_IO_ERROR( wxT(
"ComponentsBodies6 stream has no properties" ) );
1036 THROW_IO_ERROR( wxT(
"Components6 stream was not parsed correctly" ) );
1049 if( subrecord1 == 0 )
1055 THROW_IO_ERROR( wxT(
"Pads6 stream has invalid subrecord1 length" ) );
1080 uint8_t flags1 = aReader.
Read<uint8_t>();
1086 uint8_t flags2 = aReader.
Read<uint8_t>();
1089 net = aReader.
Read<uint16_t>();
1119 if( subrecord5 == 110 )
1137 if( subrecord5 >= 120 )
1144 else if( subrecord5 == 171 )
1148 if( subrecord5 >= 202 )
1162 if( subrecord6 >= 596 )
1164 sizeAndShape = std::make_unique<APAD6_SIZE_AND_SHAPE>();
1195 else if( subrecord6 != 0 )
1197 wxLogTrace(
traceAltiumIo, wxT(
"Pads6 stream has unexpected length for subrecord 6: %d." ), subrecord6 );
1205 THROW_IO_ERROR( wxT(
"Pads6 stream was not parsed correctly" ) );
1219 uint8_t flags1 = aReader.
Read<uint8_t>();
1225 uint8_t flags2 = aReader.
Read<uint8_t>();
1228 net = aReader.
Read<uint16_t>();
1237 if( subrecord1 <= 74 )
1243 uint8_t temp_byte = aReader.
Read<uint8_t>();
1262 temp_byte = aReader.
Read<uint8_t>();
1271 for(
int ii = 0; ii < 32; ++ii )
1277 if( subrecord1 >= 246 )
1287 if( subrecord1 >= 307 )
1298 THROW_IO_ERROR( wxT(
"Vias6 stream was not parsed correctly" ) );
1306 THROW_IO_ERROR( wxT(
"Tracks6 stream has invalid recordtype" ) );
1313 uint8_t flags1 = aReader.
Read<uint8_t>();
1317 uint8_t flags2 = aReader.
Read<uint8_t>();
1320 net = aReader.
Read<uint16_t>();
1332 if( remaining >= 9 )
1339 if( remaining >= 10 )
1349 THROW_IO_ERROR( wxT(
"Tracks6 stream was not parsed correctly" ) );
1375 if( subrecord1 < 123 )
1389 char fontData[64] = { 0 };
1390 aReader.
ReadBytes( fontData,
sizeof( fontData ) );
1391 fontname = wxString( fontData, wxMBConvUTF16LE(),
sizeof( fontData ) ).BeforeFirst(
'\0' );
1393 char tmpbyte = aReader.
Read<uint8_t>();
1409 if( remaining >= 93 )
1420 uint8_t unk8 = aReader.
Read<uint8_t>();
1426 aReader.
ReadBytes( fontData,
sizeof( fontData ) );
1427 barcode_fontname = wxString( fontData, wxMBConvUTF16LE(),
sizeof( fontData ) ).BeforeFirst(
'\0' );
1429 aReader.
Read<uint8_t>();
1435 if( remaining >= 103 )
1443 for(
size_t ii = 0; ii < 8; ++ii )
1445 uint8_t temp = aReader.
Peek<uint8_t>();
1447 wxLogTrace(
traceAltiumImport,
"3ATEXT6 %zu:\t Byte:%u, Kicad:%u\n", ii, temp, temp32 );
1457 if( remaining >= 115 )
1475 if( entry != aStringTable.end() )
1476 text = entry->second;
1481 text.Replace( wxT(
"\r\n" ), wxT(
"\n" ) );
1495 THROW_IO_ERROR( wxT(
"Texts6 stream was not parsed correctly" ) );
1510 uint8_t flags1 = aReader.
Read<uint8_t>();
1513 uint8_t flags2 = aReader.
Read<uint8_t>();
1516 net = aReader.
Read<uint16_t>();
1526 if( remaining >= 9 )
1532 if( remaining >= 10 )
1542 THROW_IO_ERROR( wxT(
"Fills6 stream was not parsed correctly" ) );
1550 THROW_IO_ERROR( wxT(
"Regions6 stream has invalid recordtype" ) );
1557 uint8_t flags1 = aReader.
Read<uint8_t>();
1561 uint8_t flags2 = aReader.
Read<uint8_t>();
1564 net = aReader.
Read<uint16_t>();
1571 std::map<wxString, wxString> properties = aReader.
ReadProperties();
1573 if( properties.empty() )
1625 uint32_t num_outline_vertices = aReader.
Read<uint32_t>();
1627 if( aExtendedVertices )
1628 num_outline_vertices++;
1630 for( uint32_t i = 0; i < num_outline_vertices; i++ )
1632 if( aExtendedVertices )
1634 bool isRound = aReader.
Read<uint8_t>() != 0;
1638 double angle1 = aReader.
Read<
double>();
1639 double angle2 = aReader.
Read<
double>();
1652 for( uint16_t k = 0; k <
holecount; k++ )
1654 uint32_t num_hole_vertices = aReader.
Read<uint32_t>();
1655 holes.at( k ).reserve( num_hole_vertices );
1657 for( uint32_t i = 0; i < num_hole_vertices; i++ )
1670 THROW_IO_ERROR( wxT(
"Regions6 stream was not parsed correctly" ) );
bool altiumScopeExprMatchesPolygon(const wxString &aExpr)
Return true if an Altium rule scope expression targets polygon pour primitives (matches InPolygon,...
void altium_parse_polygons(std::map< wxString, wxString > &aProps, std::vector< ALTIUM_VERTICE > &aVertices)
ALTIUM_MECHKIND altium_mechkind_from_name(const wxString &aName)
static std::map< wxString, double > ReadAltiumDielectricLossTangents(const std::map< wxString, wxString > &aProps)
static void ExpectSubrecordLengthAtLeast(const std::string &aStreamType, const std::string &aSubrecordName, size_t aExpectedLength, size_t aActualLength)
Throw an IO_ERROR if the actual length is less than the expected length.
ALTIUM_LAYER altium_layer_from_name(const wxString &aName)
static wxString MakeAltiumDielectricKey(const wxString &aMaterial, int32_t aHeight, double aConst)
static AEXTENDED_PRIMITIVE_INFORMATION_TYPE ReadAltiumExtendedPrimitiveInformationTypeFromProperties(const std::map< wxString, wxString > &aProps, wxString aKey)
ALTIUM_LAYER altium_versioned_layer(ALTIUM_LAYER aV6Layer, ALTIUM_LAYER aV7Layer)
const ARULE6 * selectAltiumPolygonRule(const std::vector< ARULE6 > &aRulesByPriorityAsc)
Select the highest Altium-priority rule whose scope references polygons.
static ALTIUM_MODE ReadAltiumModeFromProperties(const std::map< wxString, wxString > &aProps, wxString aKey)
static std::vector< ABOARD6_LAYER_STACKUP > ReadAltiumStackupFromProperties(const std::map< wxString, wxString > &aProps)
static ALTIUM_RECORD ReadAltiumRecordFromProperties(const std::map< wxString, wxString > &aProps, wxString aKey)
bool altiumViaSideIsTented(bool aTentFlag, bool aManual, bool aFromHole, uint32_t aHoleSize, int32_t aMaskExpansion, int aLandDiameter)
Decide whether one side of an Altium via should be tented when imported into KiCad.
const uint16_t ALTIUM_NET_UNCONNECTED
const uint16_t ALTIUM_POLYGON_NONE
AEXTENDED_PRIMITIVE_INFORMATION_TYPE
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
void Skip(size_t aLength)
size_t ReadAndSetSubrecordLength()
VECTOR2I ReadVector2ISize()
VECTOR2I ReadVector2IPos()
size_t GetRemainingSubrecordBytes() const
std::map< wxString, wxString > ReadProperties(std::function< std::map< wxString, wxString >(const std::string &)> handleBinaryData=[](const std::string &) { return std::map< wxString, wxString >();})
int ReadBytes(char *aOut, size_t aSize)
static int ReadInt(const std::map< wxString, wxString > &aProps, const wxString &aKey, int aDefault)
static int32_t ReadKicadUnit(const std::map< wxString, wxString > &aProps, const wxString &aKey, const wxString &aDefault)
static bool ReadBool(const std::map< wxString, wxString > &aProps, const wxString &aKey, bool aDefault)
static wxString ReadUnicodeString(const std::map< wxString, wxString > &aProps, const wxString &aKey, const wxString &aDefault)
static wxString ReadString(const std::map< wxString, wxString > &aProps, const wxString &aKey, const wxString &aDefault)
static double ReadDouble(const std::map< wxString, wxString > &aProps, const wxString &aKey, double aDefault)
static int32_t ConvertToKicadUnit(const double aValue)
static const wxChar * traceAltiumImport
Flag to enable Altium importer logging.
const wxChar *const traceAltiumIo
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define THROW_IO_ERRORF(msg,...)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
AARC6(ALTIUM_BINARY_PARSER &aReader)
uint8_t keepoutrestrictions
ABOARD6_LAYER_STACKUP(const std::map< wxString, wxString > &aProps, const wxString &aPrefix, uint32_t aLayerIdFallback)
wxString dielectricmaterial
std::set< wxString > layerNames
ABOARD6(ALTIUM_BINARY_PARSER &aReader)
std::vector< ABOARD6_LAYER_STACKUP > stackup
std::vector< ALTIUM_VERTICE > board_vertices
std::vector< wxString > names
ACLASS6(ALTIUM_BINARY_PARSER &aReader)
wxString sourceHierachicalPath
ALTIUM_TEXT_POSITION commentautoposition
ALTIUM_TEXT_POSITION nameautoposition
ACOMPONENT6(ALTIUM_BINARY_PARSER &aReader)
wxString sourcefootprintlibrary
wxString sourcelibreference
wxString sourcedesignator
wxString sourcecomponentlibrary
ACOMPONENTBODY6(ALTIUM_BINARY_PARSER &aReader)
std::vector< VECTOR2I > textPoint
ALTIUM_DIMENSION_KIND kind
ADIMENSION6(ALTIUM_BINARY_PARSER &aReader)
std::vector< VECTOR2I > referencePoint
uint8_t keepoutrestrictions
AFILL6(ALTIUM_BINARY_PARSER &aReader)
std::set< wxString > layerNames
std::vector< ABOARD6_LAYER_STACKUP > stackup
ALIBRARY(ALTIUM_BINARY_PARSER &aReader)
AMODEL(ALTIUM_BINARY_PARSER &aReader)
ANET6(ALTIUM_BINARY_PARSER &aReader)
int32_t soldermaskexpansionmanual
APAD6(ALTIUM_BINARY_PARSER &aReader)
std::unique_ptr< APAD6_SIZE_AND_SHAPE > sizeAndShape
ALTIUM_PAD_SHAPE topshape
ALTIUM_MODE pastemaskexpansionmode
ALTIUM_MODE soldermaskexpansionmode
ALTIUM_PAD_SHAPE botshape
ALTIUM_PAD_SHAPE midshape
int32_t pastemaskexpansionmanual
int32_t pad_to_die_length
std::vector< ALTIUM_VERTICE > vertices
APOLYGON6(ALTIUM_BINARY_PARSER &aReader)
ALTIUM_POLYGON_HATCHSTYLE hatchstyle
uint8_t keepoutrestrictions
AREGION6(ALTIUM_BINARY_PARSER &aReader, bool aExtendedVertices)
std::vector< ALTIUM_VERTICE > outline
std::vector< std::vector< ALTIUM_VERTICE > > holes
ALTIUM_CONNECT_STYLE polygonconnectStyle
int planeclearanceClearance
int32_t polygonconnectReliefconductorwidth
int32_t polygonconnectAirgapwidth
int polygonconnectReliefentries
ASMARTUNION6(ALTIUM_BINARY_PARSER &aReader)
uint32_t text_offset_width
uint32_t textbox_rect_height
ALTIUM_TEXT_POSITION textbox_rect_justification
uint32_t widestring_index
uint32_t textbox_rect_width
uint32_t margin_border_width
bool isJustificationValid
ALTIUM_BARCODE_TYPE barcode_type
ALTIUM_TEXT_TYPE fonttype
STROKE_FONT_TYPE strokefonttype
wxString barcode_fontname
ATEXT6(ALTIUM_BINARY_PARSER &aReader, std::map< uint32_t, wxString > &aStringTable)
ATRACK6(ALTIUM_BINARY_PARSER &aReader)
uint8_t keepoutrestrictions
bool soldermask_expansion_linked
uint32_t thermal_relief_conductorcount
int32_t thermal_relief_airgap
bool soldermask_expansion_from_hole
int32_t soldermask_expansion_front
bool soldermask_expansion_manual
AVIA6(ALTIUM_BINARY_PARSER &aReader)
uint32_t thermal_relief_conductorwidth
int32_t soldermask_expansion_back
uint32_t diameter_by_layer[32]
wxLogTrace helper definitions.
VECTOR2< int32_t > VECTOR2I