28#include <unordered_map>
34#include <wx/translation.h>
66 wxString upper = aExpr.Upper();
67 return upper.Contains( wxT(
"INPOLY" ) ) || upper.Contains( wxT(
"ISPOLY" ) );
73 for(
const ARULE6& rule : aRulesByPriorityAsc )
87 int32_t aMaskExpansion,
int aLandDiameter )
92 if( aManual && aFromHole )
94 int64_t opening =
static_cast<int64_t
>( aHoleSize ) + 2LL * aMaskExpansion;
96 return opening <= static_cast<int64_t>( aLandDiameter );
108 if( aName.IsEmpty() )
111 static const std::unordered_map<std::string, ALTIUM_LAYER> hash_map = {
203 auto it = hash_map.find( std::string( aName.c_str() ) );
205 if( it != hash_map.end() )
209 const wxString mechanicalStr(
"MECHANICAL" );
211 if( aName.StartsWith( mechanicalStr ) )
213 unsigned long val = 0;
215 if( aName.Mid( mechanicalStr.length() ).ToULong( &val ) )
219 wxLogError(
_(
"Unknown mapping of the Altium layer '%s'." ), aName );
226 static const std::unordered_map<std::string, ALTIUM_MECHKIND> hash_map = {
273 auto it = hash_map.find( std::string( aName.c_str() ) );
275 if( it != hash_map.end() )
281 wxLogError(
_(
"Unknown mapping of the Altium layer kind '%s'." ), aName );
288 std::vector<ALTIUM_VERTICE>& aVertices )
290 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
292 const wxString si = std::to_string( i );
294 const wxString vxi = wxT(
"VX" ) + si;
295 const wxString vyi = wxT(
"VY" ) + si;
297 if( aProps.find( vxi ) == aProps.end() || aProps.find( vyi ) == aProps.end() )
309 aVertices.emplace_back( isRound,
radius, sa, ea, vp, cp );
319 if( mode == wxT(
"None" ) )
321 else if( mode == wxT(
"Rule" ) )
323 else if( mode == wxT(
"Manual" ) )
326 wxLogError(
_(
"Unknown Mode string: '%s'." ), mode );
336 if( record == wxT(
"Arc" ) )
338 else if( record == wxT(
"Pad" ) )
340 else if( record == wxT(
"Via" ) )
342 else if( record == wxT(
"Track" ) )
344 else if( record == wxT(
"Text" ) )
346 else if( record == wxT(
"Fill" ) )
348 else if( record == wxT(
"Region" ) )
350 else if( record == wxT(
"Model" ) )
353 wxLogError(
_(
"Unknown Record name string: '%s'." ), record );
360 const std::map<wxString, wxString>& aProps, wxString aKey )
364 if( parsedType == wxT(
"Mask" ) )
367 wxLogError(
_(
"Unknown Extended Primitive Information type: '%s'." ), parsedType );
381 const std::string& aSubrecordName,
size_t aExpectedLength,
382 size_t aActualLength )
384 if( aActualLength < aExpectedLength )
387 "which is unexpected (expected at least %d)",
388 aStreamType, aSubrecordName, aActualLength,
396 const std::map<wxString, wxString> props = aReader.
ReadProperties();
399 THROW_IO_ERROR( wxT(
"ExtendedPrimitiveInformation stream has no properties!" ) );
407 props, wxT(
"PASTEMASKEXPANSION_MANUAL" ), wxT(
"0mil" ) );
411 props, wxT(
"SOLDERMASKEXPANSION_MANUAL" ), wxT(
"0mil" ) );
416 uint32_t aLayerIdFallback )
434 mechenabled = !mechEnabled.Contains( wxS(
"FALSE" ) );
440 if( !mechKind.IsEmpty() )
448 return wxString::Format( wxT(
"%s|%d|%d" ), aMaterial, aHeight,
449 static_cast<int>( std::lround( aConst * 1000.0 ) ) );
461 std::map<wxString, double> tangents;
462 std::set<wxString> ambiguous;
464 auto scan = [&](
const wxString& aFamily )
466 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
468 const wxString prefix = aFamily + std::to_string( i );
470 if( aProps.find( prefix + wxT(
"NAME" ) ) == aProps.end() )
473 if( aProps.find( prefix + wxT(
"DIELLOSSTANGENT" ) ) == aProps.end() )
486 auto [it, inserted] = tangents.insert( { key, tangent } );
488 if( !inserted &&
std::abs( it->second - tangent ) > 1e-9 )
489 ambiguous.insert( key );
495 scan( wxT(
"V9_STACK_LAYER" ) );
496 scan( wxT(
"LAYER_V8_" ) );
498 for(
const wxString& key : ambiguous )
499 tangents.erase( key );
507 std::vector<ABOARD6_LAYER_STACKUP> stackup;
509 for(
size_t i = 1; i < std::numeric_limits<size_t>::max(); i++ )
511 const wxString layeri = wxString( wxT(
"LAYER" ) ) << std::to_string( i );
512 const wxString layername = layeri + wxT(
"NAME" );
514 auto layernameit = aProps.find( layername );
516 if( layernameit == aProps.end() )
520 stackup.push_back( l );
524 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
526 const wxString layeri = wxString( wxT(
"LAYERV7_" ) ) << std::to_string( i );
527 const wxString layername = layeri + wxT(
"NAME" );
529 auto layernameit = aProps.find( layername );
531 if( layernameit == aProps.end() )
535 stackup.push_back( l );
542 if( !tangents.empty() )
549 auto it = tangents.find( key );
551 if( it != tangents.end() )
552 l.dielectriclosstangent = it->second;
573 wxString originalName = l.name;
576 for(
int ii = 2; !
layerNames.insert( l.name ).second; ii++ )
577 l.name = wxString::Format( wxT(
"%s %d" ), originalName, ii );
581 THROW_IO_ERROR( wxT(
"Library stream was not parsed correctly!" ) );
603 wxString originalName = l.name;
606 for(
int ii = 2; !
layerNames.insert( l.name ).second; ii++ )
607 l.name = wxString::Format( wxT(
"%s %d" ), originalName, ii );
613 THROW_IO_ERROR( wxT(
"Board6 stream was not parsed correctly!" ) );
618 std::map<wxString, wxString> properties = aReader.
ReadProperties();
620 if( properties.empty() )
627 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
629 auto mit = properties.find( wxT(
"M" ) + wxString( std::to_string( i ) ) );
631 if( mit == properties.end() )
634 names.push_back( mit->second );
638 THROW_IO_ERROR( wxT(
"Classes6 stream was not parsed correctly" ) );
677 THROW_IO_ERROR( wxT(
"Components6 stream was not parsed correctly" ) );
717 for(
int i = 0; i < refcount; i++ )
719 const std::string refi =
"REFERENCE" + std::to_string( i ) +
"POINT";
720 const wxString ref( refi );
725 for(
size_t i = 1; i < std::numeric_limits<size_t>::max(); i++ )
727 const std::string texti =
"TEXT" + std::to_string( i );
728 const std::string textix = texti +
"X";
729 const std::string textiy = texti +
"Y";
731 if( props.find( textix ) == props.end() || props.find( textiy ) == props.end() )
749 THROW_IO_ERROR( wxT(
"Dimensions6 stream was not parsed correctly" ) );
754 std::map<wxString, wxString> properties = aReader.
ReadProperties();
756 if( properties.empty() )
776 std::map<wxString, wxString> properties = aReader.
ReadProperties();
778 if( properties.empty() )
789 std::map<wxString, wxString> properties = aReader.
ReadProperties();
791 if( properties.empty() )
823 THROW_IO_ERROR( wxT(
"Polygons6 stream was not parsed correctly" ) );
842 if( rulekind == wxT(
"Clearance" ) )
847 else if( rulekind == wxT(
"DiffPairsRouting" ) )
851 else if( rulekind == wxT(
"Height" ) )
855 else if( rulekind == wxT(
"HoleSize" ) )
861 else if( rulekind == wxT(
"HoleToHoleClearance" ) )
866 else if( rulekind == wxT(
"RoutingVias" ) )
876 else if( rulekind == wxT(
"Width" ) )
883 else if( rulekind == wxT(
"PasteMaskExpansion" ) )
888 else if( rulekind == wxT(
"SolderMaskExpansion" ) )
893 else if( rulekind == wxT(
"PlaneClearance" ) )
898 else if( rulekind == wxT(
"PolygonConnect" ) )
918 THROW_IO_ERROR( wxT(
"Rules6 stream was not parsed correctly" ) );
934 uint8_t flags1 = aReader.
Read<uint8_t>();
938 uint8_t flags2 = aReader.
Read<uint8_t>();
941 net = aReader.
Read<uint16_t>();
960 if( remaining >= 10 )
980 THROW_IO_ERROR( wxT(
"ComponentsBodies6 stream has invalid recordtype" ) );
988 std::map<wxString, wxString> properties = aReader.
ReadProperties();
990 if( properties.empty() )
991 THROW_IO_ERROR( wxT(
"ComponentsBodies6 stream has no properties" ) );
1013 THROW_IO_ERROR( wxT(
"Components6 stream was not parsed correctly" ) );
1026 if( subrecord1 == 0 )
1032 THROW_IO_ERROR( wxT(
"Pads6 stream has invalid subrecord1 length" ) );
1057 uint8_t flags1 = aReader.
Read<uint8_t>();
1063 uint8_t flags2 = aReader.
Read<uint8_t>();
1066 net = aReader.
Read<uint16_t>();
1096 if( subrecord5 == 110 )
1104 THROW_IO_ERROR( wxString::Format(
"Pads6 stream subrecord5 + 106 has value %d, which is unexpected",
1116 if( subrecord5 >= 120 )
1123 else if( subrecord5 == 171 )
1127 if( subrecord5 >= 202 )
1141 if( subrecord6 >= 596 )
1143 sizeAndShape = std::make_unique<APAD6_SIZE_AND_SHAPE>();
1174 else if( subrecord6 != 0 )
1176 wxLogError(
_(
"Pads6 stream has unexpected length for subrecord 6: %d." ), subrecord6 );
1184 THROW_IO_ERROR( wxT(
"Pads6 stream was not parsed correctly" ) );
1198 uint8_t flags1 = aReader.
Read<uint8_t>();
1204 uint8_t flags2 = aReader.
Read<uint8_t>();
1207 net = aReader.
Read<uint16_t>();
1216 if( subrecord1 <= 74 )
1222 uint8_t temp_byte = aReader.
Read<uint8_t>();
1241 temp_byte = aReader.
Read<uint8_t>();
1250 for(
int ii = 0; ii < 32; ++ii )
1256 if( subrecord1 >= 246 )
1266 if( subrecord1 >= 307 )
1277 THROW_IO_ERROR( wxT(
"Vias6 stream was not parsed correctly" ) );
1285 THROW_IO_ERROR( wxT(
"Tracks6 stream has invalid recordtype" ) );
1292 uint8_t flags1 = aReader.
Read<uint8_t>();
1296 uint8_t flags2 = aReader.
Read<uint8_t>();
1299 net = aReader.
Read<uint16_t>();
1311 if( remaining >= 9 )
1317 if( remaining >= 10 )
1327 THROW_IO_ERROR( wxT(
"Tracks6 stream was not parsed correctly" ) );
1353 if( subrecord1 < 123 )
1367 char fontData[64] = { 0 };
1368 aReader.
ReadBytes( fontData,
sizeof( fontData ) );
1369 fontname = wxString( fontData, wxMBConvUTF16LE(),
sizeof( fontData ) ).BeforeFirst(
'\0' );
1371 char tmpbyte = aReader.
Read<uint8_t>();
1387 if( remaining >= 93 )
1398 uint8_t unk8 = aReader.
Read<uint8_t>();
1404 aReader.
ReadBytes( fontData,
sizeof( fontData ) );
1405 barcode_fontname = wxString( fontData, wxMBConvUTF16LE(),
sizeof( fontData ) ).BeforeFirst(
'\0' );
1407 aReader.
Read<uint8_t>();
1413 if( remaining >= 103 )
1421 for(
size_t ii = 0; ii < 8; ++ii )
1423 uint8_t temp = aReader.
Peek<uint8_t>();
1425 wxLogTrace(
traceAltiumImport,
"3ATEXT6 %zu:\t Byte:%u, Kicad:%u\n", ii, temp, temp32 );
1435 if( remaining >= 115 )
1453 if( entry != aStringTable.end() )
1454 text = entry->second;
1459 text.Replace( wxT(
"\r\n" ), wxT(
"\n" ) );
1473 THROW_IO_ERROR( wxT(
"Texts6 stream was not parsed correctly" ) );
1488 uint8_t flags1 = aReader.
Read<uint8_t>();
1491 uint8_t flags2 = aReader.
Read<uint8_t>();
1494 net = aReader.
Read<uint16_t>();
1504 if( remaining >= 9 )
1510 if( remaining >= 10 )
1520 THROW_IO_ERROR( wxT(
"Fills6 stream was not parsed correctly" ) );
1528 THROW_IO_ERROR( wxT(
"Regions6 stream has invalid recordtype" ) );
1535 uint8_t flags1 = aReader.
Read<uint8_t>();
1539 uint8_t flags2 = aReader.
Read<uint8_t>();
1542 net = aReader.
Read<uint16_t>();
1549 std::map<wxString, wxString> properties = aReader.
ReadProperties();
1551 if( properties.empty() )
1603 uint32_t num_outline_vertices = aReader.
Read<uint32_t>();
1605 if( aExtendedVertices )
1606 num_outline_vertices++;
1608 for( uint32_t i = 0; i < num_outline_vertices; i++ )
1610 if( aExtendedVertices )
1612 bool isRound = aReader.
Read<uint8_t>() != 0;
1616 double angle1 = aReader.
Read<
double>();
1617 double angle2 = aReader.
Read<
double>();
1630 for( uint16_t k = 0; k <
holecount; k++ )
1632 uint32_t num_hole_vertices = aReader.
Read<uint32_t>();
1633 holes.at( k ).reserve( num_hole_vertices );
1635 for( uint32_t i = 0; i < num_hole_vertices; i++ )
1648 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.
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
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
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]
VECTOR2< int32_t > VECTOR2I