26#include <unordered_map>
32#include <wx/translation.h>
69 static const std::unordered_map<std::string, ALTIUM_LAYER> hash_map = {
161 auto it = hash_map.find( std::string( aName.c_str() ) );
163 if( it != hash_map.end() )
167 const wxString mechanicalStr(
"MECHANICAL" );
169 if( aName.StartsWith( mechanicalStr ) )
171 unsigned long val = 0;
173 if( aName.Mid( mechanicalStr.length() ).ToULong( &val ) )
177 wxLogError(
_(
"Unknown mapping of the Altium layer '%s'." ), aName );
184 static const std::unordered_map<std::string, ALTIUM_MECHKIND> hash_map = {
231 auto it = hash_map.find( std::string( aName.c_str() ) );
233 if( it != hash_map.end() )
239 wxLogError(
_(
"Unknown mapping of the Altium layer kind '%s'." ), aName );
246 std::vector<ALTIUM_VERTICE>& aVertices )
248 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
250 const wxString si = std::to_string( i );
252 const wxString vxi = wxT(
"VX" ) + si;
253 const wxString vyi = wxT(
"VY" ) + si;
255 if( aProps.find( vxi ) == aProps.end() || aProps.find( vyi ) == aProps.end() )
267 aVertices.emplace_back( isRound,
radius, sa, ea, vp, cp );
277 if( mode == wxT(
"None" ) )
279 else if( mode == wxT(
"Rule" ) )
281 else if( mode == wxT(
"Manual" ) )
284 wxLogError(
_(
"Unknown Mode string: '%s'." ), mode );
294 if( record == wxT(
"Arc" ) )
296 else if( record == wxT(
"Pad" ) )
298 else if( record == wxT(
"Via" ) )
300 else if( record == wxT(
"Track" ) )
302 else if( record == wxT(
"Text" ) )
304 else if( record == wxT(
"Fill" ) )
306 else if( record == wxT(
"Region" ) )
308 else if( record == wxT(
"Model" ) )
311 wxLogError(
_(
"Unknown Record name string: '%s'." ), record );
318 const std::map<wxString, wxString>& aProps, wxString aKey )
322 if( parsedType == wxT(
"Mask" ) )
325 wxLogError(
_(
"Unknown Extended Primitive Information type: '%s'." ), parsedType );
339 const std::string& aSubrecordName,
size_t aExpectedLength,
340 size_t aActualLength )
342 if( aActualLength < aExpectedLength )
345 "which is unexpected (expected at least %d)",
346 aStreamType, aSubrecordName, aActualLength,
354 const std::map<wxString, wxString> props = aReader.
ReadProperties();
357 THROW_IO_ERROR( wxT(
"ExtendedPrimitiveInformation stream has no properties!" ) );
365 props, wxT(
"PASTEMASKEXPANSION_MANUAL" ), wxT(
"0mil" ) );
369 props, wxT(
"SOLDERMASKEXPANSION_MANUAL" ), wxT(
"0mil" ) );
374 uint32_t aLayerIdFallback )
392 mechenabled = !mechEnabled.Contains( wxS(
"FALSE" ) );
398 if( !mechKind.IsEmpty() )
406 std::vector<ABOARD6_LAYER_STACKUP> stackup;
408 for(
size_t i = 1; i < std::numeric_limits<size_t>::max(); i++ )
410 const wxString layeri = wxString( wxT(
"LAYER" ) ) << std::to_string( i );
411 const wxString layername = layeri + wxT(
"NAME" );
413 auto layernameit = aProps.find( layername );
415 if( layernameit == aProps.end() )
419 stackup.push_back( l );
423 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
425 const wxString layeri = wxString( wxT(
"LAYERV7_" ) ) << std::to_string( i );
426 const wxString layername = layeri + wxT(
"NAME" );
428 auto layernameit = aProps.find( layername );
430 if( layernameit == aProps.end() )
434 stackup.push_back( l );
454 wxString originalName = l.name;
457 for(
int ii = 2; !
layerNames.insert( l.name ).second; ii++ )
458 l.name = wxString::Format( wxT(
"%s %d" ), originalName, ii );
462 THROW_IO_ERROR( wxT(
"Library stream was not parsed correctly!" ) );
484 wxString originalName = l.name;
487 for(
int ii = 2; !
layerNames.insert( l.name ).second; ii++ )
488 l.name = wxString::Format( wxT(
"%s %d" ), originalName, ii );
494 THROW_IO_ERROR( wxT(
"Board6 stream was not parsed correctly!" ) );
499 std::map<wxString, wxString> properties = aReader.
ReadProperties();
501 if( properties.empty() )
508 for(
size_t i = 0; i < std::numeric_limits<size_t>::max(); i++ )
510 auto mit = properties.find( wxT(
"M" ) + wxString( std::to_string( i ) ) );
512 if( mit == properties.end() )
515 names.push_back( mit->second );
519 THROW_IO_ERROR( wxT(
"Classes6 stream was not parsed correctly" ) );
558 THROW_IO_ERROR( wxT(
"Components6 stream was not parsed correctly" ) );
598 for(
int i = 0; i < refcount; i++ )
600 const std::string refi =
"REFERENCE" + std::to_string( i ) +
"POINT";
601 const wxString ref( refi );
606 for(
size_t i = 1; i < std::numeric_limits<size_t>::max(); i++ )
608 const std::string texti =
"TEXT" + std::to_string( i );
609 const std::string textix = texti +
"X";
610 const std::string textiy = texti +
"Y";
612 if( props.find( textix ) == props.end() || props.find( textiy ) == props.end() )
630 THROW_IO_ERROR( wxT(
"Dimensions6 stream was not parsed correctly" ) );
635 std::map<wxString, wxString> properties = aReader.
ReadProperties();
637 if( properties.empty() )
657 std::map<wxString, wxString> properties = aReader.
ReadProperties();
659 if( properties.empty() )
670 std::map<wxString, wxString> properties = aReader.
ReadProperties();
672 if( properties.empty() )
704 THROW_IO_ERROR( wxT(
"Polygons6 stream was not parsed correctly" ) );
723 if( rulekind == wxT(
"Clearance" ) )
728 else if( rulekind == wxT(
"DiffPairsRouting" ) )
732 else if( rulekind == wxT(
"Height" ) )
736 else if( rulekind == wxT(
"HoleSize" ) )
742 else if( rulekind == wxT(
"HoleToHoleClearance" ) )
747 else if( rulekind == wxT(
"RoutingVias" ) )
757 else if( rulekind == wxT(
"Width" ) )
764 else if( rulekind == wxT(
"PasteMaskExpansion" ) )
769 else if( rulekind == wxT(
"SolderMaskExpansion" ) )
774 else if( rulekind == wxT(
"PlaneClearance" ) )
779 else if( rulekind == wxT(
"PolygonConnect" ) )
799 THROW_IO_ERROR( wxT(
"Rules6 stream was not parsed correctly" ) );
815 uint8_t flags1 = aReader.
Read<uint8_t>();
819 uint8_t flags2 = aReader.
Read<uint8_t>();
822 net = aReader.
Read<uint16_t>();
841 if( remaining >= 10 )
861 THROW_IO_ERROR( wxT(
"ComponentsBodies6 stream has invalid recordtype" ) );
869 std::map<wxString, wxString> properties = aReader.
ReadProperties();
871 if( properties.empty() )
872 THROW_IO_ERROR( wxT(
"ComponentsBodies6 stream has no properties" ) );
894 THROW_IO_ERROR( wxT(
"Components6 stream was not parsed correctly" ) );
907 if( subrecord1 == 0 )
913 THROW_IO_ERROR( wxT(
"Pads6 stream has invalid subrecord1 length" ) );
938 uint8_t flags1 = aReader.
Read<uint8_t>();
944 uint8_t flags2 = aReader.
Read<uint8_t>();
947 net = aReader.
Read<uint16_t>();
977 if( subrecord5 == 110 )
985 THROW_IO_ERROR( wxString::Format(
"Pads6 stream subrecord5 + 106 has value %d, which is unexpected",
997 if( subrecord5 >= 120 )
1004 else if( subrecord5 == 171 )
1008 if( subrecord5 >= 202 )
1022 if( subrecord6 >= 596 )
1024 sizeAndShape = std::make_unique<APAD6_SIZE_AND_SHAPE>();
1055 else if( subrecord6 != 0 )
1057 wxLogError(
_(
"Pads6 stream has unexpected length for subrecord 6: %d." ), subrecord6 );
1065 THROW_IO_ERROR( wxT(
"Pads6 stream was not parsed correctly" ) );
1079 uint8_t flags1 = aReader.
Read<uint8_t>();
1085 uint8_t flags2 = aReader.
Read<uint8_t>();
1088 net = aReader.
Read<uint16_t>();
1097 if( subrecord1 <= 74 )
1103 uint8_t temp_byte = aReader.
Read<uint8_t>();
1118 temp_byte = aReader.
Read<uint8_t>();
1125 for(
int ii = 0; ii < 32; ++ii )
1131 if( subrecord1 >= 246 )
1138 if( subrecord1 >= 307 )
1149 THROW_IO_ERROR( wxT(
"Vias6 stream was not parsed correctly" ) );
1157 THROW_IO_ERROR( wxT(
"Tracks6 stream has invalid recordtype" ) );
1164 uint8_t flags1 = aReader.
Read<uint8_t>();
1168 uint8_t flags2 = aReader.
Read<uint8_t>();
1171 net = aReader.
Read<uint16_t>();
1183 if( remaining >= 9 )
1189 if( remaining >= 10 )
1199 THROW_IO_ERROR( wxT(
"Tracks6 stream was not parsed correctly" ) );
1225 if( subrecord1 < 123 )
1239 char fontData[64] = { 0 };
1240 aReader.
ReadBytes( fontData,
sizeof( fontData ) );
1241 fontname = wxString( fontData, wxMBConvUTF16LE(),
sizeof( fontData ) ).BeforeFirst(
'\0' );
1243 char tmpbyte = aReader.
Read<uint8_t>();
1259 if( remaining >= 93 )
1270 uint8_t unk8 = aReader.
Read<uint8_t>();
1276 aReader.
ReadBytes( fontData,
sizeof( fontData ) );
1277 barcode_fontname = wxString( fontData, wxMBConvUTF16LE(),
sizeof( fontData ) ).BeforeFirst(
'\0' );
1279 aReader.
Read<uint8_t>();
1285 if( remaining >= 103 )
1293 for(
size_t ii = 0; ii < 8; ++ii )
1295 uint8_t temp = aReader.
Peek<uint8_t>();
1297 wxLogTrace(
traceAltiumImport,
"3ATEXT6 %zu:\t Byte:%u, Kicad:%u\n", ii, temp, temp32 );
1307 if( remaining >= 115 )
1325 if( entry != aStringTable.end() )
1326 text = entry->second;
1331 text.Replace( wxT(
"\r\n" ), wxT(
"\n" ) );
1345 THROW_IO_ERROR( wxT(
"Texts6 stream was not parsed correctly" ) );
1360 uint8_t flags1 = aReader.
Read<uint8_t>();
1363 uint8_t flags2 = aReader.
Read<uint8_t>();
1366 net = aReader.
Read<uint16_t>();
1376 if( remaining >= 9 )
1382 if( remaining >= 10 )
1392 THROW_IO_ERROR( wxT(
"Fills6 stream was not parsed correctly" ) );
1400 THROW_IO_ERROR( wxT(
"Regions6 stream has invalid recordtype" ) );
1407 uint8_t flags1 = aReader.
Read<uint8_t>();
1411 uint8_t flags2 = aReader.
Read<uint8_t>();
1414 net = aReader.
Read<uint16_t>();
1421 std::map<wxString, wxString> properties = aReader.
ReadProperties();
1423 if( properties.empty() )
1475 uint32_t num_outline_vertices = aReader.
Read<uint32_t>();
1477 if( aExtendedVertices )
1478 num_outline_vertices++;
1480 for( uint32_t i = 0; i < num_outline_vertices; i++ )
1482 if( aExtendedVertices )
1484 bool isRound = aReader.
Read<uint8_t>() != 0;
1488 double angle1 = aReader.
Read<
double>();
1489 double angle2 = aReader.
Read<
double>();
1502 for( uint16_t k = 0; k <
holecount; k++ )
1504 uint32_t num_hole_vertices = aReader.
Read<uint32_t>();
1505 holes.at( k ).reserve( num_hole_vertices );
1507 for( uint32_t i = 0; i < num_hole_vertices; i++ )
1520 THROW_IO_ERROR( wxT(
"Regions6 stream was not parsed correctly" ) );
void altium_parse_polygons(std::map< wxString, wxString > &aProps, std::vector< ALTIUM_VERTICE > &aVertices)
ALTIUM_MECHKIND altium_mechkind_from_name(const wxString &aName)
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 AEXTENDED_PRIMITIVE_INFORMATION_TYPE ReadAltiumExtendedPrimitiveInformationTypeFromProperties(const std::map< wxString, wxString > &aProps, wxString aKey)
ALTIUM_LAYER altium_versioned_layer(ALTIUM_LAYER aV6Layer, ALTIUM_LAYER aV7Layer)
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)
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
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
ARULE6(ALTIUM_BINARY_PARSER &aReader)
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
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