24#include <unordered_map>
30#include <wx/translation.h>
62 wxString upper = aExpr.Upper();
63 return upper.Contains( wxT(
"INPOLY" ) ) || upper.Contains( wxT(
"ISPOLY" ) );
69 for(
const ARULE6& rule : aRulesByPriorityAsc )
83 int32_t aMaskExpansion,
int aLandDiameter )
88 if( aManual && aFromHole )
90 int64_t opening =
static_cast<int64_t
>( aHoleSize ) + 2LL * aMaskExpansion;
92 return opening <= static_cast<int64_t>( aLandDiameter );
104 if( aName.IsEmpty() )
107 static const std::unordered_map<std::string, ALTIUM_LAYER> hash_map = {
199 auto it = hash_map.find( std::string( aName.c_str() ) );
201 if( it != hash_map.end() )
205 const wxString mechanicalStr(
"MECHANICAL" );
207 if( aName.StartsWith( mechanicalStr ) )
209 unsigned long val = 0;
211 if( aName.Mid( mechanicalStr.length() ).ToULong( &val ) )
215 wxLogError(
_(
"Unknown mapping of the Altium layer '%s'." ), aName );
222 static const std::unordered_map<std::string, ALTIUM_MECHKIND> hash_map = {
269 auto it = hash_map.find( std::string( aName.c_str() ) );
271 if( it != hash_map.end() )
277 wxLogError(
_(
"Unknown mapping of the Altium layer kind '%s'." ), aName );
284 std::vector<ALTIUM_VERTICE>& aVertices )
286 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
288 const wxString si = std::to_string( i );
290 const wxString vxi = wxT(
"VX" ) + si;
291 const wxString vyi = wxT(
"VY" ) + si;
293 if( aProps.find( vxi ) == aProps.end() || aProps.find( vyi ) == aProps.end() )
305 aVertices.emplace_back( isRound,
radius, sa, ea, vp, cp );
315 if( mode == wxT(
"None" ) )
317 else if( mode == wxT(
"Rule" ) )
319 else if( mode == wxT(
"Manual" ) )
322 wxLogError(
_(
"Unknown Mode string: '%s'." ), mode );
332 if( record == wxT(
"Arc" ) )
334 else if( record == wxT(
"Pad" ) )
336 else if( record == wxT(
"Via" ) )
338 else if( record == wxT(
"Track" ) )
340 else if( record == wxT(
"Text" ) )
342 else if( record == wxT(
"Fill" ) )
344 else if( record == wxT(
"Region" ) )
346 else if( record == wxT(
"Model" ) )
349 wxLogError(
_(
"Unknown Record name string: '%s'." ), record );
356 const std::map<wxString, wxString>& aProps, wxString aKey )
360 if( parsedType == wxT(
"Mask" ) )
363 wxLogError(
_(
"Unknown Extended Primitive Information type: '%s'." ), parsedType );
377 const std::string& aSubrecordName,
size_t aExpectedLength,
378 size_t aActualLength )
380 if( aActualLength < aExpectedLength )
383 "which is unexpected (expected at least %d)",
384 aStreamType, aSubrecordName, aActualLength,
392 const std::map<wxString, wxString> props = aReader.
ReadProperties();
395 THROW_IO_ERROR( wxT(
"ExtendedPrimitiveInformation stream has no properties!" ) );
403 props, wxT(
"PASTEMASKEXPANSION_MANUAL" ), wxT(
"0mil" ) );
407 props, wxT(
"SOLDERMASKEXPANSION_MANUAL" ), wxT(
"0mil" ) );
412 uint32_t aLayerIdFallback )
430 mechenabled = !mechEnabled.Contains( wxS(
"FALSE" ) );
436 if( !mechKind.IsEmpty() )
444 return wxString::Format( wxT(
"%s|%d|%d" ), aMaterial, aHeight,
445 static_cast<int>( std::lround( aConst * 1000.0 ) ) );
457 std::map<wxString, double> tangents;
458 std::set<wxString> ambiguous;
460 auto scan = [&](
const wxString& aFamily )
462 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
464 const wxString prefix = aFamily + std::to_string( i );
466 if( aProps.find( prefix + wxT(
"NAME" ) ) == aProps.end() )
469 if( aProps.find( prefix + wxT(
"DIELLOSSTANGENT" ) ) == aProps.end() )
482 auto [it, inserted] = tangents.insert( { key, tangent } );
484 if( !inserted &&
std::abs( it->second - tangent ) > 1e-9 )
485 ambiguous.insert( key );
491 scan( wxT(
"V9_STACK_LAYER" ) );
492 scan( wxT(
"LAYER_V8_" ) );
494 for(
const wxString& key : ambiguous )
495 tangents.erase( key );
503 std::vector<ABOARD6_LAYER_STACKUP> stackup;
505 for(
size_t i = 1; i < std::numeric_limits<size_t>::max(); i++ )
507 const wxString layeri = wxString( wxT(
"LAYER" ) ) << std::to_string( i );
508 const wxString layername = layeri + wxT(
"NAME" );
510 auto layernameit = aProps.find( layername );
512 if( layernameit == aProps.end() )
516 stackup.push_back( l );
520 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
522 const wxString layeri = wxString( wxT(
"LAYERV7_" ) ) << std::to_string( i );
523 const wxString layername = layeri + wxT(
"NAME" );
525 auto layernameit = aProps.find( layername );
527 if( layernameit == aProps.end() )
531 stackup.push_back( l );
538 if( !tangents.empty() )
545 auto it = tangents.find( key );
547 if( it != tangents.end() )
548 l.dielectriclosstangent = it->second;
569 wxString originalName = l.name;
572 for(
int ii = 2; !
layerNames.insert( l.name ).second; ii++ )
573 l.name = wxString::Format( wxT(
"%s %d" ), originalName, ii );
577 THROW_IO_ERROR( wxT(
"Library stream was not parsed correctly!" ) );
599 wxString originalName = l.name;
602 for(
int ii = 2; !
layerNames.insert( l.name ).second; ii++ )
603 l.name = wxString::Format( wxT(
"%s %d" ), originalName, ii );
609 THROW_IO_ERROR( wxT(
"Board6 stream was not parsed correctly!" ) );
614 std::map<wxString, wxString> properties = aReader.
ReadProperties();
616 if( properties.empty() )
623 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
625 auto mit = properties.find( wxT(
"M" ) + wxString( std::to_string( i ) ) );
627 if( mit == properties.end() )
630 names.push_back( mit->second );
634 THROW_IO_ERROR( wxT(
"Classes6 stream was not parsed correctly" ) );
673 THROW_IO_ERROR( wxT(
"Components6 stream was not parsed correctly" ) );
713 for(
int i = 0; i < refcount; i++ )
715 const std::string refi =
"REFERENCE" + std::to_string( i ) +
"POINT";
716 const wxString ref( refi );
721 for(
size_t i = 1; i < std::numeric_limits<size_t>::max(); i++ )
723 const std::string texti =
"TEXT" + std::to_string( i );
724 const std::string textix = texti +
"X";
725 const std::string textiy = texti +
"Y";
727 if( props.find( textix ) == props.end() || props.find( textiy ) == props.end() )
745 THROW_IO_ERROR( wxT(
"Dimensions6 stream was not parsed correctly" ) );
750 std::map<wxString, wxString> properties = aReader.
ReadProperties();
752 if( properties.empty() )
772 std::map<wxString, wxString> properties = aReader.
ReadProperties();
774 if( properties.empty() )
785 std::map<wxString, wxString> properties = aReader.
ReadProperties();
787 if( properties.empty() )
819 THROW_IO_ERROR( wxT(
"Polygons6 stream was not parsed correctly" ) );
838 if( rulekind == wxT(
"Clearance" ) )
843 else if( rulekind == wxT(
"DiffPairsRouting" ) )
847 else if( rulekind == wxT(
"Height" ) )
851 else if( rulekind == wxT(
"HoleSize" ) )
857 else if( rulekind == wxT(
"HoleToHoleClearance" ) )
862 else if( rulekind == wxT(
"RoutingVias" ) )
872 else if( rulekind == wxT(
"Width" ) )
879 else if( rulekind == wxT(
"PasteMaskExpansion" ) )
884 else if( rulekind == wxT(
"SolderMaskExpansion" ) )
889 else if( rulekind == wxT(
"PlaneClearance" ) )
894 else if( rulekind == wxT(
"PolygonConnect" ) )
914 THROW_IO_ERROR( wxT(
"Rules6 stream was not parsed correctly" ) );
929 wxT(
"" ) ).IsEmpty();
939 THROW_IO_ERROR( wxT(
"SmartUnions stream was not parsed correctly" ) );
955 uint8_t flags1 = aReader.
Read<uint8_t>();
959 uint8_t flags2 = aReader.
Read<uint8_t>();
962 net = aReader.
Read<uint16_t>();
982 if( remaining >= 10 )
1002 THROW_IO_ERROR( wxT(
"ComponentsBodies6 stream has invalid recordtype" ) );
1010 std::map<wxString, wxString> properties = aReader.
ReadProperties();
1012 if( properties.empty() )
1013 THROW_IO_ERROR( wxT(
"ComponentsBodies6 stream has no properties" ) );
1035 THROW_IO_ERROR( wxT(
"Components6 stream was not parsed correctly" ) );
1048 if( subrecord1 == 0 )
1054 THROW_IO_ERROR( wxT(
"Pads6 stream has invalid subrecord1 length" ) );
1079 uint8_t flags1 = aReader.
Read<uint8_t>();
1085 uint8_t flags2 = aReader.
Read<uint8_t>();
1088 net = aReader.
Read<uint16_t>();
1118 if( subrecord5 == 110 )
1126 THROW_IO_ERROR( wxString::Format(
"Pads6 stream subrecord5 + 106 has value %d, which is unexpected",
1138 if( subrecord5 >= 120 )
1145 else if( subrecord5 == 171 )
1149 if( subrecord5 >= 202 )
1163 if( subrecord6 >= 596 )
1165 sizeAndShape = std::make_unique<APAD6_SIZE_AND_SHAPE>();
1196 else if( subrecord6 != 0 )
1198 wxLogError(
_(
"Pads6 stream has unexpected length for subrecord 6: %d." ), subrecord6 );
1206 THROW_IO_ERROR( wxT(
"Pads6 stream was not parsed correctly" ) );
1220 uint8_t flags1 = aReader.
Read<uint8_t>();
1226 uint8_t flags2 = aReader.
Read<uint8_t>();
1229 net = aReader.
Read<uint16_t>();
1238 if( subrecord1 <= 74 )
1244 uint8_t temp_byte = aReader.
Read<uint8_t>();
1263 temp_byte = aReader.
Read<uint8_t>();
1272 for(
int ii = 0; ii < 32; ++ii )
1278 if( subrecord1 >= 246 )
1288 if( subrecord1 >= 307 )
1299 THROW_IO_ERROR( wxT(
"Vias6 stream was not parsed correctly" ) );
1307 THROW_IO_ERROR( wxT(
"Tracks6 stream has invalid recordtype" ) );
1314 uint8_t flags1 = aReader.
Read<uint8_t>();
1318 uint8_t flags2 = aReader.
Read<uint8_t>();
1321 net = aReader.
Read<uint16_t>();
1333 if( remaining >= 9 )
1340 if( remaining >= 10 )
1350 THROW_IO_ERROR( wxT(
"Tracks6 stream was not parsed correctly" ) );
1376 if( subrecord1 < 123 )
1390 char fontData[64] = { 0 };
1391 aReader.
ReadBytes( fontData,
sizeof( fontData ) );
1392 fontname = wxString( fontData, wxMBConvUTF16LE(),
sizeof( fontData ) ).BeforeFirst(
'\0' );
1394 char tmpbyte = aReader.
Read<uint8_t>();
1410 if( remaining >= 93 )
1421 uint8_t unk8 = aReader.
Read<uint8_t>();
1427 aReader.
ReadBytes( fontData,
sizeof( fontData ) );
1428 barcode_fontname = wxString( fontData, wxMBConvUTF16LE(),
sizeof( fontData ) ).BeforeFirst(
'\0' );
1430 aReader.
Read<uint8_t>();
1436 if( remaining >= 103 )
1444 for(
size_t ii = 0; ii < 8; ++ii )
1446 uint8_t temp = aReader.
Peek<uint8_t>();
1448 wxLogTrace(
traceAltiumImport,
"3ATEXT6 %zu:\t Byte:%u, Kicad:%u\n", ii, temp, temp32 );
1458 if( remaining >= 115 )
1476 if( entry != aStringTable.end() )
1477 text = entry->second;
1482 text.Replace( wxT(
"\r\n" ), wxT(
"\n" ) );
1496 THROW_IO_ERROR( wxT(
"Texts6 stream was not parsed correctly" ) );
1511 uint8_t flags1 = aReader.
Read<uint8_t>();
1514 uint8_t flags2 = aReader.
Read<uint8_t>();
1517 net = aReader.
Read<uint16_t>();
1527 if( remaining >= 9 )
1533 if( remaining >= 10 )
1543 THROW_IO_ERROR( wxT(
"Fills6 stream was not parsed correctly" ) );
1551 THROW_IO_ERROR( wxT(
"Regions6 stream has invalid recordtype" ) );
1558 uint8_t flags1 = aReader.
Read<uint8_t>();
1562 uint8_t flags2 = aReader.
Read<uint8_t>();
1565 net = aReader.
Read<uint16_t>();
1572 std::map<wxString, wxString> properties = aReader.
ReadProperties();
1574 if( properties.empty() )
1626 uint32_t num_outline_vertices = aReader.
Read<uint32_t>();
1628 if( aExtendedVertices )
1629 num_outline_vertices++;
1631 for( uint32_t i = 0; i < num_outline_vertices; i++ )
1633 if( aExtendedVertices )
1635 bool isRound = aReader.
Read<uint8_t>() != 0;
1639 double angle1 = aReader.
Read<
double>();
1640 double angle2 = aReader.
Read<
double>();
1653 for( uint16_t k = 0; k <
holecount; k++ )
1655 uint32_t num_hole_vertices = aReader.
Read<uint32_t>();
1656 holes.at( k ).reserve( num_hole_vertices );
1658 for( uint32_t i = 0; i < num_hole_vertices; i++ )
1671 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
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]
VECTOR2< int32_t > VECTOR2I