From b0c07a28b3d51407b1d62a21b2ce617650943388 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Mon, 5 Jul 2021 23:57:58 +0100 Subject: [PATCH] Add importing of Altium notes. Import is incomplete because KiCad doesn't support text boxes yet (so we can't do borders, word-wrap, background fills, etc.). Fixes https://gitlab.com/kicad/code/kicad/issues/8734 --- .../sch_plugins/altium/altium_parser_sch.cpp | 60 +++++++++++++------ .../sch_plugins/altium/altium_parser_sch.h | 30 +++++++++- .../sch_plugins/altium/sch_altium_plugin.cpp | 45 +++++++++++++- .../sch_plugins/altium/sch_altium_plugin.h | 1 + 4 files changed, 117 insertions(+), 19 deletions(-) diff --git a/eeschema/sch_plugins/altium/altium_parser_sch.cpp b/eeschema/sch_plugins/altium/altium_parser_sch.cpp index 36382b13f7..baf099f080 100644 --- a/eeschema/sch_plugins/altium/altium_parser_sch.cpp +++ b/eeschema/sch_plugins/altium/altium_parser_sch.cpp @@ -47,8 +47,8 @@ constexpr int Altium2KiCadUnit( const int val, const int frac ) } -int PropertiesReadKiCadUnitFrac( - const std::map& aProperties, const wxString& aKey ) +int PropertiesReadKiCadUnitFrac( const std::map& aProperties, + const wxString& aKey ) { // a unit is stored using two fields, denoting the size in mils and a fraction size int key = ALTIUM_PARSER::PropertiesReadInt( aProperties, aKey, 0 ); @@ -57,8 +57,8 @@ int PropertiesReadKiCadUnitFrac( } -int PropertiesReadKiCadUnitFrac1( - const std::map& aProperties, const wxString& aKey ) +int PropertiesReadKiCadUnitFrac1( const std::map& aProperties, + const wxString& aKey ) { // a unit is stored using two fields, denoting the size in mils and a fraction size // Dunno why Altium invents different units for the same purpose @@ -70,9 +70,10 @@ int PropertiesReadKiCadUnitFrac1( template T PropertiesReadEnum( const std::map& aProperties, const wxString& aKey, - int aLower, int aUpper, T aDefault ) + int aLower, int aUpper, T aDefault ) { int value = ALTIUM_PARSER::PropertiesReadInt( aProperties, aKey, static_cast( aDefault ) ); + if( value < aLower || value > aUpper ) return aDefault; else @@ -98,17 +99,17 @@ ASCH_SYMBOL::ASCH_SYMBOL( const std::map& aProperties ) { wxASSERT( PropertiesReadRecord( aProperties ) == ALTIUM_SCH_RECORD::COMPONENT ); - currentpartid = - ALTIUM_PARSER::PropertiesReadInt( aProperties, "CURRENTPARTID", ALTIUM_COMPONENT_NONE ); + currentpartid = ALTIUM_PARSER::PropertiesReadInt( aProperties, "CURRENTPARTID", + ALTIUM_COMPONENT_NONE ); libreference = ALTIUM_PARSER::PropertiesReadString( aProperties, "LIBREFERENCE", "" ); sourcelibraryname = ALTIUM_PARSER::PropertiesReadString( aProperties, "SOURCELIBRARYNAME", "" ); - componentdescription = - ALTIUM_PARSER::PropertiesReadString( aProperties, "COMPONENTDESCRIPTION", "" ); + componentdescription = ALTIUM_PARSER::PropertiesReadString( aProperties, "COMPONENTDESCRIPTION", + "" ); orientation = ALTIUM_PARSER::PropertiesReadInt( aProperties, "ORIENTATION", 0 ); isMirrored = ALTIUM_PARSER::PropertiesReadBool( aProperties, "ISMIRRORED", false ); location = wxPoint( PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.X" ), - -PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.Y" ) ); + -PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.Y" ) ); partcount = ALTIUM_PARSER::PropertiesReadInt( aProperties, "PARTCOUNT", 0 ); displaymodecount = ALTIUM_PARSER::PropertiesReadInt( aProperties, "DISPLAYMODECOUNT", 0 ); @@ -120,12 +121,12 @@ ASCH_PIN::ASCH_PIN( const std::map& aProperties ) { wxASSERT( PropertiesReadRecord( aProperties ) == ALTIUM_SCH_RECORD::PIN ); - ownerindex = - ALTIUM_PARSER::PropertiesReadInt( aProperties, "OWNERINDEX", ALTIUM_COMPONENT_NONE ); - ownerpartid = - ALTIUM_PARSER::PropertiesReadInt( aProperties, "OWNERPARTID", ALTIUM_COMPONENT_NONE ); - ownerpartdisplaymode = - ALTIUM_PARSER::PropertiesReadInt( aProperties, "OWNERPARTDISPLAYMODE", 0 ); + ownerindex = ALTIUM_PARSER::PropertiesReadInt( aProperties, "OWNERINDEX", + ALTIUM_COMPONENT_NONE ); + ownerpartid = ALTIUM_PARSER::PropertiesReadInt( aProperties, "OWNERPARTID", + ALTIUM_COMPONENT_NONE ); + ownerpartdisplaymode = ALTIUM_PARSER::PropertiesReadInt( aProperties, "OWNERPARTDISPLAYMODE", + 0 ); name = ALTIUM_PARSER::PropertiesReadString( aProperties, "NAME", "" ); text = ALTIUM_PARSER::PropertiesReadString( aProperties, "TEXT", "" ); @@ -209,7 +210,7 @@ ASCH_LABEL::ASCH_LABEL( const std::map& aProperties ) ALTIUM_PARSER::PropertiesReadInt( aProperties, "OWNERPARTID", ALTIUM_COMPONENT_NONE ); location = wxPoint( PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.X" ), - -PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.Y" ) ); + -PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.Y" ) ); text = ALTIUM_PARSER::PropertiesReadString( aProperties, "TEXT", "" ); @@ -221,6 +222,31 @@ ASCH_LABEL::ASCH_LABEL( const std::map& aProperties ) } +ASCH_NOTE::ASCH_NOTE( const std::map& aProperties ) +{ + wxASSERT( PropertiesReadRecord( aProperties ) == ALTIUM_SCH_RECORD::NOTE ); + + location = wxPoint( PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.X" ), + -PropertiesReadKiCadUnitFrac( aProperties, "LOCATION.Y" ) ); + size = wxSize( PropertiesReadKiCadUnitFrac( aProperties, "CORNER.X" ) - location.x, + -PropertiesReadKiCadUnitFrac( aProperties, "CORNER.Y" ) - location.y ); + + text = ALTIUM_PARSER::PropertiesReadString( aProperties, "TEXT", "" ); + text.Replace( "~1", "\n", true ); + + author = ALTIUM_PARSER::PropertiesReadString( aProperties, "AUTHOR", "" ); + + fontId = ALTIUM_PARSER::PropertiesReadInt( aProperties, "FONTID", 0 ); + isWordWrapped = ALTIUM_PARSER::PropertiesReadBool( aProperties, "WORDWRAP", false ); + border = ALTIUM_PARSER::PropertiesReadBool( aProperties, "SHOWBORDER", false ); + textMargin = PropertiesReadKiCadUnitFrac( aProperties, "TEXTMARGIN" ); + areaColor = ALTIUM_PARSER::PropertiesReadInt( aProperties, "AREACOLOR", 0 ); + + alignment = PropertiesReadEnum( + aProperties, "ALIGNMENT", 1, 3, ASCH_NOTE_ALIGNMENT::LEFT ); +} + + ASCH_BEZIER::ASCH_BEZIER( const std::map& aProperties ) { wxASSERT( PropertiesReadRecord( aProperties ) == ALTIUM_SCH_RECORD::BEZIER ); diff --git a/eeschema/sch_plugins/altium/altium_parser_sch.h b/eeschema/sch_plugins/altium/altium_parser_sch.h index d605c5f0d2..e3d76e0896 100644 --- a/eeschema/sch_plugins/altium/altium_parser_sch.h +++ b/eeschema/sch_plugins/altium/altium_parser_sch.h @@ -88,7 +88,7 @@ enum class ALTIUM_SCH_RECORD RECORD_46 = 46, RECORD_47 = 47, RECORD_48 = 48, - RECORD_209 = 209, + NOTE = 209, RECORD_215 = 215, RECORD_216 = 216, RECORD_217 = 217, @@ -236,6 +236,14 @@ enum class ASCH_LABEL_JUSTIFICATION }; +enum class ASCH_NOTE_ALIGNMENT +{ + LEFT = 1, + CENTER = 2, + RIGHT = 3 +}; + + struct ASCH_LABEL { int ownerindex; @@ -254,6 +262,26 @@ struct ASCH_LABEL }; +struct ASCH_NOTE +{ + wxPoint location; + wxSize size; + + wxString text; + wxString author; + + int fontId; + bool isWordWrapped; + bool border; + int textMargin; + int areaColor; + + ASCH_NOTE_ALIGNMENT alignment; + + explicit ASCH_NOTE( const std::map& aProperties ); +}; + + struct ASCH_BEZIER { int ownerindex; diff --git a/eeschema/sch_plugins/altium/sch_altium_plugin.cpp b/eeschema/sch_plugins/altium/sch_altium_plugin.cpp index 9f7f74d3dc..6605db9ed7 100644 --- a/eeschema/sch_plugins/altium/sch_altium_plugin.cpp +++ b/eeschema/sch_plugins/altium/sch_altium_plugin.cpp @@ -462,7 +462,8 @@ void SCH_ALTIUM_PLUGIN::ParseFileHeader( const CFB::CompoundFileReader& aReader break; case ALTIUM_SCH_RECORD::RECORD_48: break; - case ALTIUM_SCH_RECORD::RECORD_209: + case ALTIUM_SCH_RECORD::NOTE: + ParseNote( properties ); break; case ALTIUM_SCH_RECORD::RECORD_215: break; @@ -834,6 +835,48 @@ void SCH_ALTIUM_PLUGIN::ParseLabel( const std::map& aPropert } +void SCH_ALTIUM_PLUGIN::ParseNote( const std::map& aProperties ) +{ + ASCH_NOTE elem( aProperties ); + + SCH_TEXT* text = new SCH_TEXT( elem.location + m_sheetOffset, elem.text ); + + switch( elem.alignment ) + { + default: + case ASCH_NOTE_ALIGNMENT::LEFT: + text->SetLabelSpinStyle( LABEL_SPIN_STYLE::SPIN::RIGHT ); + break; + case ASCH_NOTE_ALIGNMENT::CENTER: + // No support for centered text in Eeschema yet... + text->SetLabelSpinStyle( LABEL_SPIN_STYLE::SPIN::RIGHT ); + break; + case ASCH_NOTE_ALIGNMENT::RIGHT: + text->SetLabelSpinStyle( LABEL_SPIN_STYLE::SPIN::LEFT ); + break; + } + + // TODO: set size and word-wrap once KiCad supports wrapped text. + + // TODO: set border and background color once KiCad supports them. + + // TODO: need some sort of propety system for storing author.... + + size_t fontId = static_cast( elem.fontId ); + + if( m_altiumSheet && fontId > 0 && fontId <= m_altiumSheet->fonts.size() ) + { + const ASCH_SHEET_FONT& font = m_altiumSheet->fonts.at( fontId - 1 ); + text->SetItalic( font.italic ); + text->SetBold( font.bold ); + text->SetTextSize( { font.size / 2, font.size / 2 } ); + } + + text->SetFlags( IS_NEW ); + m_currentSheet->GetScreen()->Append( text ); +} + + 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 e5cc12b527..02e6488120 100644 --- a/eeschema/sch_plugins/altium/sch_altium_plugin.h +++ b/eeschema/sch_plugins/altium/sch_altium_plugin.h @@ -115,6 +115,7 @@ private: void ParseComponent( int aIndex, const std::map& aProperties ); void ParsePin( const std::map& aProperties ); void ParseLabel( const std::map& aProperties ); + void ParseNote( const std::map& aProperties ); void ParseBezier( const std::map& aProperties ); void ParsePolyline( const std::map& aProperties ); void ParsePolygon( const std::map& aProperties );