diff --git a/eeschema/sch_plugins/altium/altium_parser_sch.cpp b/eeschema/sch_plugins/altium/altium_parser_sch.cpp index 29b1533cd4..4c354ae08f 100644 --- a/eeschema/sch_plugins/altium/altium_parser_sch.cpp +++ b/eeschema/sch_plugins/altium/altium_parser_sch.cpp @@ -54,6 +54,18 @@ int PropertiesReadKiCadUnitFrac( } +template +T PropertiesReadEnum( const std::map& aProperties, const wxString& aKey, + int aLower, int aUpper, T aDefault ) +{ + int value = ALTIUM_PARSER::PropertiesReadInt( aProperties, aKey, static_cast( aDefault ) ); + if( value < aLower || value > aUpper ) + return aDefault; + else + return static_cast( value ); +} + + ASCH_COMPONENT::ASCH_COMPONENT( const std::map& aProperties ) { wxASSERT( PropertiesReadRecord( aProperties ) == ALTIUM_SCH_RECORD::COMPONENT ); @@ -98,11 +110,8 @@ ASCH_PIN::ASCH_PIN( const std::map& aProperties ) symbolInnerEdge = ( symbolInnerEdgeInt == 0 || symbolInnerEdgeInt == 3 ) ? static_cast( symbolInnerEdgeInt ) : ASCH_PIN_SYMBOL_INNEREDGE::UNKNOWN; - - int electricalInt = ALTIUM_PARSER::PropertiesReadInt( aProperties, "ELECTRICAL", 0 ); - electrical = ( electricalInt >= 0 && electricalInt <= 7 ) ? - static_cast( electricalInt ) : - ASCH_PIN_ELECTRICAL::UNKNOWN; + electrical = PropertiesReadEnum( + aProperties, "ELECTRICAL", 0, 7, ASCH_PIN_ELECTRICAL::UNKNOWN ); int pinconglomerate = ALTIUM_PARSER::PropertiesReadInt( aProperties, "PINCONGLOMERATE", 0 ); @@ -153,6 +162,25 @@ ASCH_PIN::ASCH_PIN( const std::map& aProperties ) } +ASCH_LABEL::ASCH_LABEL( const std::map& aProperties ) +{ + wxASSERT( PropertiesReadRecord( aProperties ) == ALTIUM_SCH_RECORD::LABEL ); + + ownerindex = + ALTIUM_PARSER::PropertiesReadInt( aProperties, "OWNERINDEX", ALTIUM_COMPONENT_NONE ); + ownerpartid = + ALTIUM_PARSER::PropertiesReadInt( aProperties, "OWNERPARTID", ALTIUM_COMPONENT_NONE ); + + location = wxPoint( PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.X" ), + -PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.Y" ) ); + + text = ALTIUM_PARSER::PropertiesReadString( aProperties, "TEXT", "" ); + isMirrored = ALTIUM_PARSER::PropertiesReadBool( aProperties, "ISMIRRORED", false ); + justification = PropertiesReadEnum( + aProperties, "JUSTIFICATION", 0, 8, ASCH_LABEL_JUSTIFICATION::UNKNOWN ); +} + + ASCH_BEZIER::ASCH_BEZIER( const std::map& aProperties ) { wxASSERT( PropertiesReadRecord( aProperties ) == ALTIUM_SCH_RECORD::BEZIER ); @@ -335,10 +363,8 @@ ASCH_NET_LABEL::ASCH_NET_LABEL( const std::map& aProperties location = wxPoint( PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.X" ), -PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.Y" ) ); - int orientationRaw = ALTIUM_PARSER::PropertiesReadInt( aProperties, "ORIENTATION", 0 ); - orientation = orientationRaw >= 0 && orientationRaw <= 3 ? - static_cast( orientationRaw ) : - ASCH_RECORD_ORIENTATION::RIGHTWARDS; + orientation = PropertiesReadEnum( + aProperties, "ORIENTATION", 0, 3, ASCH_RECORD_ORIENTATION::RIGHTWARDS ); } diff --git a/eeschema/sch_plugins/altium/altium_parser_sch.h b/eeschema/sch_plugins/altium/altium_parser_sch.h index 5b9ea86545..ecd20ff613 100644 --- a/eeschema/sch_plugins/altium/altium_parser_sch.h +++ b/eeschema/sch_plugins/altium/altium_parser_sch.h @@ -182,7 +182,7 @@ struct ASCH_PIN ASCH_PIN_SYMBOL_OUTEREDGE symbolOuterEdge; ASCH_PIN_SYMBOL_INNEREDGE symbolInnerEdge; - ASCH_PIN_ELECTRICAL electrical; + ASCH_PIN_ELECTRICAL electrical; ASCH_RECORD_ORIENTATION orientation; wxPoint location; @@ -197,6 +197,37 @@ struct ASCH_PIN }; +enum class ASCH_LABEL_JUSTIFICATION +{ + UNKNOWN = -1, + + BOTTOM_LEFT = 0, + BOTTOM_CENTER = 1, + BOTTOM_RIGHT = 2, + CENTER_LEFT = 3, + CENTER_CENTER = 4, + CENTER_RIGHT = 5, + TOP_LEFT = 6, + TOP_CENTER = 7, + TOP_RIGHT = 8 +}; + + +struct ASCH_LABEL +{ + int ownerindex; + int ownerpartid; + + wxPoint location; + + wxString text; + bool isMirrored; + ASCH_LABEL_JUSTIFICATION justification; + + explicit ASCH_LABEL( const std::map& aProperties ); +}; + + struct ASCH_BEZIER { int ownerindex; @@ -336,7 +367,7 @@ struct ASCH_NET_LABEL { wxString text; - wxPoint location; + wxPoint location; ASCH_RECORD_ORIENTATION orientation; diff --git a/eeschema/sch_plugins/altium/sch_altium_plugin.cpp b/eeschema/sch_plugins/altium/sch_altium_plugin.cpp index c908f61a39..aef6a78ead 100644 --- a/eeschema/sch_plugins/altium/sch_altium_plugin.cpp +++ b/eeschema/sch_plugins/altium/sch_altium_plugin.cpp @@ -308,6 +308,7 @@ void SCH_ALTIUM_PLUGIN::Parse( const CFB::CompoundFileReader& aReader ) case ALTIUM_SCH_RECORD::IEEE_SYMBOL: break; case ALTIUM_SCH_RECORD::LABEL: + ParseLabel( properties ); break; case ALTIUM_SCH_RECORD::BEZIER: ParseBezier( properties ); @@ -601,6 +602,91 @@ void SCH_ALTIUM_PLUGIN::ParsePin( const std::map& aPropertie } +void SetEdaTextJustification( EDA_TEXT* text, ASCH_LABEL_JUSTIFICATION justification ) +{ + switch( justification ) + { + default: + case ASCH_LABEL_JUSTIFICATION::UNKNOWN: + case ASCH_LABEL_JUSTIFICATION::BOTTOM_LEFT: + case ASCH_LABEL_JUSTIFICATION::BOTTOM_CENTER: + case ASCH_LABEL_JUSTIFICATION::BOTTOM_RIGHT: + text->SetVertJustify( EDA_TEXT_VJUSTIFY_T::GR_TEXT_VJUSTIFY_BOTTOM ); + break; + case ASCH_LABEL_JUSTIFICATION::CENTER_LEFT: + case ASCH_LABEL_JUSTIFICATION::CENTER_CENTER: + case ASCH_LABEL_JUSTIFICATION::CENTER_RIGHT: + text->SetVertJustify( EDA_TEXT_VJUSTIFY_T::GR_TEXT_VJUSTIFY_CENTER ); + break; + case ASCH_LABEL_JUSTIFICATION::TOP_LEFT: + case ASCH_LABEL_JUSTIFICATION::TOP_CENTER: + case ASCH_LABEL_JUSTIFICATION::TOP_RIGHT: + text->SetVertJustify( EDA_TEXT_VJUSTIFY_T::GR_TEXT_VJUSTIFY_TOP ); + break; + } + + switch( justification ) + { + default: + case ASCH_LABEL_JUSTIFICATION::UNKNOWN: + case ASCH_LABEL_JUSTIFICATION::BOTTOM_LEFT: + case ASCH_LABEL_JUSTIFICATION::CENTER_LEFT: + case ASCH_LABEL_JUSTIFICATION::TOP_LEFT: + text->SetHorizJustify( EDA_TEXT_HJUSTIFY_T::GR_TEXT_HJUSTIFY_LEFT ); + break; + case ASCH_LABEL_JUSTIFICATION::BOTTOM_CENTER: + case ASCH_LABEL_JUSTIFICATION::CENTER_CENTER: + case ASCH_LABEL_JUSTIFICATION::TOP_CENTER: + text->SetHorizJustify( EDA_TEXT_HJUSTIFY_T::GR_TEXT_HJUSTIFY_CENTER ); + break; + case ASCH_LABEL_JUSTIFICATION::BOTTOM_RIGHT: + case ASCH_LABEL_JUSTIFICATION::CENTER_RIGHT: + case ASCH_LABEL_JUSTIFICATION::TOP_RIGHT: + text->SetHorizJustify( EDA_TEXT_HJUSTIFY_T::GR_TEXT_HJUSTIFY_RIGHT ); + break; + } +} + + +void SCH_ALTIUM_PLUGIN::ParseLabel( const std::map& aProperties ) +{ + ASCH_LABEL elem( aProperties ); + + if( elem.ownerpartid == ALTIUM_COMPONENT_NONE ) + { + SCH_TEXT* text = new SCH_TEXT( elem.location, elem.text ); + text->SetMirrored( elem.isMirrored ); + + SetEdaTextJustification( text, elem.justification ); + + text->SetFlags( IS_NEW ); + m_currentSheet->GetScreen()->Append( text ); + } + else + { + const auto& symbol = m_symbols.find( elem.ownerindex ); + if( symbol == m_symbols.end() ) + { + // TODO: e.g. can depend on Template (RECORD=39 + wxLogWarning( wxString::Format( + "Label tries to access symbol with ownerindex %d which does not exist", + elem.ownerindex ) ); + return; + } + + const auto& component = m_components.at( symbol->first ); + + LIB_TEXT* text = new LIB_TEXT( symbol->second ); + symbol->second->AddDrawItem( text ); + + text->SetPosition( GetRelativePosition( elem.location, component ) ); + text->SetText( elem.text ); + + SetEdaTextJustification( text, elem.justification ); + } +} + + void SCH_ALTIUM_PLUGIN::ParseBezier( const std::map& aProperties ) { ASCH_BEZIER elem( aProperties ); diff --git a/eeschema/sch_plugins/altium/sch_altium_plugin.h b/eeschema/sch_plugins/altium/sch_altium_plugin.h index 263c00df3a..34319313de 100644 --- a/eeschema/sch_plugins/altium/sch_altium_plugin.h +++ b/eeschema/sch_plugins/altium/sch_altium_plugin.h @@ -100,6 +100,7 @@ public: private: void ParseComponent( int index, const std::map& aProperties ); void ParsePin( const std::map& aProperties ); + void ParseLabel( const std::map& aProperties ); void ParseBezier( const std::map& aProperties ); void ParsePolyline( const std::map& aProperties ); void ParsePolygon( const std::map& aProperties );